1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * driver for channel subsystem 4 * 5 * Copyright IBM Corp. 2002, 2010 6 * 7 * Author(s): Arnd Bergmann (arndb@de.ibm.com) 8 * Cornelia Huck (cornelia.huck@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/device.h> 17 #include <linux/slab.h> 18 #include <linux/errno.h> 19 #include <linux/list.h> 20 #include <linux/reboot.h> 21 #include <linux/proc_fs.h> 22 #include <linux/genalloc.h> 23 #include <linux/dma-mapping.h> 24 #include <asm/isc.h> 25 #include <asm/crw.h> 26 27 #include "css.h" 28 #include "cio.h" 29 #include "blacklist.h" 30 #include "cio_debug.h" 31 #include "ioasm.h" 32 #include "chsc.h" 33 #include "device.h" 34 #include "idset.h" 35 #include "chp.h" 36 37 int css_init_done = 0; 38 int max_ssid; 39 40 #define MAX_CSS_IDX 0 41 struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1]; 42 static struct bus_type css_bus_type; 43 44 int 45 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data) 46 { 47 struct subchannel_id schid; 48 int ret; 49 50 init_subchannel_id(&schid); 51 do { 52 do { 53 ret = fn(schid, data); 54 if (ret) 55 break; 56 } while (schid.sch_no++ < __MAX_SUBCHANNEL); 57 schid.sch_no = 0; 58 } while (schid.ssid++ < max_ssid); 59 return ret; 60 } 61 62 struct cb_data { 63 void *data; 64 struct idset *set; 65 int (*fn_known_sch)(struct subchannel *, void *); 66 int (*fn_unknown_sch)(struct subchannel_id, void *); 67 }; 68 69 static int call_fn_known_sch(struct device *dev, void *data) 70 { 71 struct subchannel *sch = to_subchannel(dev); 72 struct cb_data *cb = data; 73 int rc = 0; 74 75 if (cb->set) 76 idset_sch_del(cb->set, sch->schid); 77 if (cb->fn_known_sch) 78 rc = cb->fn_known_sch(sch, cb->data); 79 return rc; 80 } 81 82 static int call_fn_unknown_sch(struct subchannel_id schid, void *data) 83 { 84 struct cb_data *cb = data; 85 int rc = 0; 86 87 if (idset_sch_contains(cb->set, schid)) 88 rc = cb->fn_unknown_sch(schid, cb->data); 89 return rc; 90 } 91 92 static int call_fn_all_sch(struct subchannel_id schid, void *data) 93 { 94 struct cb_data *cb = data; 95 struct subchannel *sch; 96 int rc = 0; 97 98 sch = get_subchannel_by_schid(schid); 99 if (sch) { 100 if (cb->fn_known_sch) 101 rc = cb->fn_known_sch(sch, cb->data); 102 put_device(&sch->dev); 103 } else { 104 if (cb->fn_unknown_sch) 105 rc = cb->fn_unknown_sch(schid, cb->data); 106 } 107 108 return rc; 109 } 110 111 int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *), 112 int (*fn_unknown)(struct subchannel_id, 113 void *), void *data) 114 { 115 struct cb_data cb; 116 int rc; 117 118 cb.data = data; 119 cb.fn_known_sch = fn_known; 120 cb.fn_unknown_sch = fn_unknown; 121 122 if (fn_known && !fn_unknown) { 123 /* Skip idset allocation in case of known-only loop. */ 124 cb.set = NULL; 125 return bus_for_each_dev(&css_bus_type, NULL, &cb, 126 call_fn_known_sch); 127 } 128 129 cb.set = idset_sch_new(); 130 if (!cb.set) 131 /* fall back to brute force scanning in case of oom */ 132 return for_each_subchannel(call_fn_all_sch, &cb); 133 134 idset_fill(cb.set); 135 136 /* Process registered subchannels. */ 137 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch); 138 if (rc) 139 goto out; 140 /* Process unregistered subchannels. */ 141 if (fn_unknown) 142 rc = for_each_subchannel(call_fn_unknown_sch, &cb); 143 out: 144 idset_free(cb.set); 145 146 return rc; 147 } 148 149 static void css_sch_todo(struct work_struct *work); 150 151 static int css_sch_create_locks(struct subchannel *sch) 152 { 153 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL); 154 if (!sch->lock) 155 return -ENOMEM; 156 157 spin_lock_init(sch->lock); 158 mutex_init(&sch->reg_mutex); 159 160 return 0; 161 } 162 163 static void css_subchannel_release(struct device *dev) 164 { 165 struct subchannel *sch = to_subchannel(dev); 166 167 sch->config.intparm = 0; 168 cio_commit_config(sch); 169 kfree(sch->driver_override); 170 kfree(sch->lock); 171 kfree(sch); 172 } 173 174 static int css_validate_subchannel(struct subchannel_id schid, 175 struct schib *schib) 176 { 177 int err; 178 179 switch (schib->pmcw.st) { 180 case SUBCHANNEL_TYPE_IO: 181 case SUBCHANNEL_TYPE_MSG: 182 if (!css_sch_is_valid(schib)) 183 err = -ENODEV; 184 else if (is_blacklisted(schid.ssid, schib->pmcw.dev)) { 185 CIO_MSG_EVENT(6, "Blacklisted device detected " 186 "at devno %04X, subchannel set %x\n", 187 schib->pmcw.dev, schid.ssid); 188 err = -ENODEV; 189 } else 190 err = 0; 191 break; 192 default: 193 err = 0; 194 } 195 if (err) 196 goto out; 197 198 CIO_MSG_EVENT(4, "Subchannel 0.%x.%04x reports subchannel type %04X\n", 199 schid.ssid, schid.sch_no, schib->pmcw.st); 200 out: 201 return err; 202 } 203 204 struct subchannel *css_alloc_subchannel(struct subchannel_id schid, 205 struct schib *schib) 206 { 207 struct subchannel *sch; 208 int ret; 209 210 ret = css_validate_subchannel(schid, schib); 211 if (ret < 0) 212 return ERR_PTR(ret); 213 214 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA); 215 if (!sch) 216 return ERR_PTR(-ENOMEM); 217 218 sch->schid = schid; 219 sch->schib = *schib; 220 sch->st = schib->pmcw.st; 221 222 ret = css_sch_create_locks(sch); 223 if (ret) 224 goto err; 225 226 INIT_WORK(&sch->todo_work, css_sch_todo); 227 sch->dev.release = &css_subchannel_release; 228 sch->dev.dma_mask = &sch->dma_mask; 229 device_initialize(&sch->dev); 230 /* 231 * The physical addresses for some of the dma structures that can 232 * belong to a subchannel need to fit 31 bit width (e.g. ccw). 233 */ 234 ret = dma_set_coherent_mask(&sch->dev, DMA_BIT_MASK(31)); 235 if (ret) 236 goto err; 237 /* 238 * But we don't have such restrictions imposed on the stuff that 239 * is handled by the streaming API. 240 */ 241 ret = dma_set_mask(&sch->dev, DMA_BIT_MASK(64)); 242 if (ret) 243 goto err; 244 245 return sch; 246 247 err: 248 kfree(sch); 249 return ERR_PTR(ret); 250 } 251 252 static int css_sch_device_register(struct subchannel *sch) 253 { 254 int ret; 255 256 mutex_lock(&sch->reg_mutex); 257 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid, 258 sch->schid.sch_no); 259 ret = device_add(&sch->dev); 260 mutex_unlock(&sch->reg_mutex); 261 return ret; 262 } 263 264 /** 265 * css_sch_device_unregister - unregister a subchannel 266 * @sch: subchannel to be unregistered 267 */ 268 void css_sch_device_unregister(struct subchannel *sch) 269 { 270 mutex_lock(&sch->reg_mutex); 271 if (device_is_registered(&sch->dev)) 272 device_unregister(&sch->dev); 273 mutex_unlock(&sch->reg_mutex); 274 } 275 EXPORT_SYMBOL_GPL(css_sch_device_unregister); 276 277 static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw) 278 { 279 int i; 280 int mask; 281 282 memset(ssd, 0, sizeof(struct chsc_ssd_info)); 283 ssd->path_mask = pmcw->pim; 284 for (i = 0; i < 8; i++) { 285 mask = 0x80 >> i; 286 if (pmcw->pim & mask) { 287 chp_id_init(&ssd->chpid[i]); 288 ssd->chpid[i].id = pmcw->chpid[i]; 289 } 290 } 291 } 292 293 static void ssd_register_chpids(struct chsc_ssd_info *ssd) 294 { 295 int i; 296 int mask; 297 298 for (i = 0; i < 8; i++) { 299 mask = 0x80 >> i; 300 if (ssd->path_mask & mask) 301 chp_new(ssd->chpid[i]); 302 } 303 } 304 305 void css_update_ssd_info(struct subchannel *sch) 306 { 307 int ret; 308 309 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info); 310 if (ret) 311 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw); 312 313 ssd_register_chpids(&sch->ssd_info); 314 } 315 316 static ssize_t type_show(struct device *dev, struct device_attribute *attr, 317 char *buf) 318 { 319 struct subchannel *sch = to_subchannel(dev); 320 321 return sprintf(buf, "%01x\n", sch->st); 322 } 323 324 static DEVICE_ATTR_RO(type); 325 326 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, 327 char *buf) 328 { 329 struct subchannel *sch = to_subchannel(dev); 330 331 return sprintf(buf, "css:t%01X\n", sch->st); 332 } 333 334 static DEVICE_ATTR_RO(modalias); 335 336 static ssize_t driver_override_store(struct device *dev, 337 struct device_attribute *attr, 338 const char *buf, size_t count) 339 { 340 struct subchannel *sch = to_subchannel(dev); 341 char *driver_override, *old, *cp; 342 343 /* We need to keep extra room for a newline */ 344 if (count >= (PAGE_SIZE - 1)) 345 return -EINVAL; 346 347 driver_override = kstrndup(buf, count, GFP_KERNEL); 348 if (!driver_override) 349 return -ENOMEM; 350 351 cp = strchr(driver_override, '\n'); 352 if (cp) 353 *cp = '\0'; 354 355 device_lock(dev); 356 old = sch->driver_override; 357 if (strlen(driver_override)) { 358 sch->driver_override = driver_override; 359 } else { 360 kfree(driver_override); 361 sch->driver_override = NULL; 362 } 363 device_unlock(dev); 364 365 kfree(old); 366 367 return count; 368 } 369 370 static ssize_t driver_override_show(struct device *dev, 371 struct device_attribute *attr, char *buf) 372 { 373 struct subchannel *sch = to_subchannel(dev); 374 ssize_t len; 375 376 device_lock(dev); 377 len = snprintf(buf, PAGE_SIZE, "%s\n", sch->driver_override); 378 device_unlock(dev); 379 return len; 380 } 381 static DEVICE_ATTR_RW(driver_override); 382 383 static struct attribute *subch_attrs[] = { 384 &dev_attr_type.attr, 385 &dev_attr_modalias.attr, 386 &dev_attr_driver_override.attr, 387 NULL, 388 }; 389 390 static struct attribute_group subch_attr_group = { 391 .attrs = subch_attrs, 392 }; 393 394 static const struct attribute_group *default_subch_attr_groups[] = { 395 &subch_attr_group, 396 NULL, 397 }; 398 399 static ssize_t chpids_show(struct device *dev, 400 struct device_attribute *attr, 401 char *buf) 402 { 403 struct subchannel *sch = to_subchannel(dev); 404 struct chsc_ssd_info *ssd = &sch->ssd_info; 405 ssize_t ret = 0; 406 int mask; 407 int chp; 408 409 for (chp = 0; chp < 8; chp++) { 410 mask = 0x80 >> chp; 411 if (ssd->path_mask & mask) 412 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id); 413 else 414 ret += sprintf(buf + ret, "00 "); 415 } 416 ret += sprintf(buf + ret, "\n"); 417 return ret; 418 } 419 static DEVICE_ATTR_RO(chpids); 420 421 static ssize_t pimpampom_show(struct device *dev, 422 struct device_attribute *attr, 423 char *buf) 424 { 425 struct subchannel *sch = to_subchannel(dev); 426 struct pmcw *pmcw = &sch->schib.pmcw; 427 428 return sprintf(buf, "%02x %02x %02x\n", 429 pmcw->pim, pmcw->pam, pmcw->pom); 430 } 431 static DEVICE_ATTR_RO(pimpampom); 432 433 static ssize_t dev_busid_show(struct device *dev, 434 struct device_attribute *attr, 435 char *buf) 436 { 437 struct subchannel *sch = to_subchannel(dev); 438 struct pmcw *pmcw = &sch->schib.pmcw; 439 440 if ((pmcw->st == SUBCHANNEL_TYPE_IO || 441 pmcw->st == SUBCHANNEL_TYPE_MSG) && pmcw->dnv) 442 return sysfs_emit(buf, "0.%x.%04x\n", sch->schid.ssid, 443 pmcw->dev); 444 else 445 return sysfs_emit(buf, "none\n"); 446 } 447 static DEVICE_ATTR_RO(dev_busid); 448 449 static struct attribute *io_subchannel_type_attrs[] = { 450 &dev_attr_chpids.attr, 451 &dev_attr_pimpampom.attr, 452 &dev_attr_dev_busid.attr, 453 NULL, 454 }; 455 ATTRIBUTE_GROUPS(io_subchannel_type); 456 457 static const struct device_type io_subchannel_type = { 458 .groups = io_subchannel_type_groups, 459 }; 460 461 int css_register_subchannel(struct subchannel *sch) 462 { 463 int ret; 464 465 /* Initialize the subchannel structure */ 466 sch->dev.parent = &channel_subsystems[0]->device; 467 sch->dev.bus = &css_bus_type; 468 sch->dev.groups = default_subch_attr_groups; 469 470 if (sch->st == SUBCHANNEL_TYPE_IO) 471 sch->dev.type = &io_subchannel_type; 472 473 /* 474 * We don't want to generate uevents for I/O subchannels that don't 475 * have a working ccw device behind them since they will be 476 * unregistered before they can be used anyway, so we delay the add 477 * uevent until after device recognition was successful. 478 * Note that we suppress the uevent for all subchannel types; 479 * the subchannel driver can decide itself when it wants to inform 480 * userspace of its existence. 481 */ 482 dev_set_uevent_suppress(&sch->dev, 1); 483 css_update_ssd_info(sch); 484 /* make it known to the system */ 485 ret = css_sch_device_register(sch); 486 if (ret) { 487 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n", 488 sch->schid.ssid, sch->schid.sch_no, ret); 489 return ret; 490 } 491 if (!sch->driver) { 492 /* 493 * No driver matched. Generate the uevent now so that 494 * a fitting driver module may be loaded based on the 495 * modalias. 496 */ 497 dev_set_uevent_suppress(&sch->dev, 0); 498 kobject_uevent(&sch->dev.kobj, KOBJ_ADD); 499 } 500 return ret; 501 } 502 503 static int css_probe_device(struct subchannel_id schid, struct schib *schib) 504 { 505 struct subchannel *sch; 506 int ret; 507 508 sch = css_alloc_subchannel(schid, schib); 509 if (IS_ERR(sch)) 510 return PTR_ERR(sch); 511 512 ret = css_register_subchannel(sch); 513 if (ret) 514 put_device(&sch->dev); 515 516 return ret; 517 } 518 519 static int 520 check_subchannel(struct device *dev, const void *data) 521 { 522 struct subchannel *sch; 523 struct subchannel_id *schid = (void *)data; 524 525 sch = to_subchannel(dev); 526 return schid_equal(&sch->schid, schid); 527 } 528 529 struct subchannel * 530 get_subchannel_by_schid(struct subchannel_id schid) 531 { 532 struct device *dev; 533 534 dev = bus_find_device(&css_bus_type, NULL, 535 &schid, check_subchannel); 536 537 return dev ? to_subchannel(dev) : NULL; 538 } 539 540 /** 541 * css_sch_is_valid() - check if a subchannel is valid 542 * @schib: subchannel information block for the subchannel 543 */ 544 int css_sch_is_valid(struct schib *schib) 545 { 546 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv) 547 return 0; 548 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w) 549 return 0; 550 return 1; 551 } 552 EXPORT_SYMBOL_GPL(css_sch_is_valid); 553 554 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow) 555 { 556 struct schib schib; 557 int ccode; 558 559 if (!slow) { 560 /* Will be done on the slow path. */ 561 return -EAGAIN; 562 } 563 /* 564 * The first subchannel that is not-operational (ccode==3) 565 * indicates that there aren't any more devices available. 566 * If stsch gets an exception, it means the current subchannel set 567 * is not valid. 568 */ 569 ccode = stsch(schid, &schib); 570 if (ccode) 571 return (ccode == 3) ? -ENXIO : ccode; 572 573 return css_probe_device(schid, &schib); 574 } 575 576 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow) 577 { 578 int ret = 0; 579 580 if (sch->driver) { 581 if (sch->driver->sch_event) 582 ret = sch->driver->sch_event(sch, slow); 583 else 584 dev_dbg(&sch->dev, 585 "Got subchannel machine check but " 586 "no sch_event handler provided.\n"); 587 } 588 if (ret != 0 && ret != -EAGAIN) { 589 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n", 590 sch->schid.ssid, sch->schid.sch_no, ret); 591 } 592 return ret; 593 } 594 595 static void css_evaluate_subchannel(struct subchannel_id schid, int slow) 596 { 597 struct subchannel *sch; 598 int ret; 599 600 sch = get_subchannel_by_schid(schid); 601 if (sch) { 602 ret = css_evaluate_known_subchannel(sch, slow); 603 put_device(&sch->dev); 604 } else 605 ret = css_evaluate_new_subchannel(schid, slow); 606 if (ret == -EAGAIN) 607 css_schedule_eval(schid); 608 } 609 610 /** 611 * css_sched_sch_todo - schedule a subchannel operation 612 * @sch: subchannel 613 * @todo: todo 614 * 615 * Schedule the operation identified by @todo to be performed on the slow path 616 * workqueue. Do nothing if another operation with higher priority is already 617 * scheduled. Needs to be called with subchannel lock held. 618 */ 619 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo) 620 { 621 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n", 622 sch->schid.ssid, sch->schid.sch_no, todo); 623 if (sch->todo >= todo) 624 return; 625 /* Get workqueue ref. */ 626 if (!get_device(&sch->dev)) 627 return; 628 sch->todo = todo; 629 if (!queue_work(cio_work_q, &sch->todo_work)) { 630 /* Already queued, release workqueue ref. */ 631 put_device(&sch->dev); 632 } 633 } 634 EXPORT_SYMBOL_GPL(css_sched_sch_todo); 635 636 static void css_sch_todo(struct work_struct *work) 637 { 638 struct subchannel *sch; 639 enum sch_todo todo; 640 int ret; 641 642 sch = container_of(work, struct subchannel, todo_work); 643 /* Find out todo. */ 644 spin_lock_irq(sch->lock); 645 todo = sch->todo; 646 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid, 647 sch->schid.sch_no, todo); 648 sch->todo = SCH_TODO_NOTHING; 649 spin_unlock_irq(sch->lock); 650 /* Perform todo. */ 651 switch (todo) { 652 case SCH_TODO_NOTHING: 653 break; 654 case SCH_TODO_EVAL: 655 ret = css_evaluate_known_subchannel(sch, 1); 656 if (ret == -EAGAIN) { 657 spin_lock_irq(sch->lock); 658 css_sched_sch_todo(sch, todo); 659 spin_unlock_irq(sch->lock); 660 } 661 break; 662 case SCH_TODO_UNREG: 663 css_sch_device_unregister(sch); 664 break; 665 } 666 /* Release workqueue ref. */ 667 put_device(&sch->dev); 668 } 669 670 static struct idset *slow_subchannel_set; 671 static DEFINE_SPINLOCK(slow_subchannel_lock); 672 static DECLARE_WAIT_QUEUE_HEAD(css_eval_wq); 673 static atomic_t css_eval_scheduled; 674 675 static int __init slow_subchannel_init(void) 676 { 677 atomic_set(&css_eval_scheduled, 0); 678 slow_subchannel_set = idset_sch_new(); 679 if (!slow_subchannel_set) { 680 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n"); 681 return -ENOMEM; 682 } 683 return 0; 684 } 685 686 static int slow_eval_known_fn(struct subchannel *sch, void *data) 687 { 688 int eval; 689 int rc; 690 691 spin_lock_irq(&slow_subchannel_lock); 692 eval = idset_sch_contains(slow_subchannel_set, sch->schid); 693 idset_sch_del(slow_subchannel_set, sch->schid); 694 spin_unlock_irq(&slow_subchannel_lock); 695 if (eval) { 696 rc = css_evaluate_known_subchannel(sch, 1); 697 if (rc == -EAGAIN) 698 css_schedule_eval(sch->schid); 699 /* 700 * The loop might take long time for platforms with lots of 701 * known devices. Allow scheduling here. 702 */ 703 cond_resched(); 704 } 705 return 0; 706 } 707 708 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data) 709 { 710 int eval; 711 int rc = 0; 712 713 spin_lock_irq(&slow_subchannel_lock); 714 eval = idset_sch_contains(slow_subchannel_set, schid); 715 idset_sch_del(slow_subchannel_set, schid); 716 spin_unlock_irq(&slow_subchannel_lock); 717 if (eval) { 718 rc = css_evaluate_new_subchannel(schid, 1); 719 switch (rc) { 720 case -EAGAIN: 721 css_schedule_eval(schid); 722 rc = 0; 723 break; 724 case -ENXIO: 725 case -ENOMEM: 726 case -EIO: 727 /* These should abort looping */ 728 spin_lock_irq(&slow_subchannel_lock); 729 idset_sch_del_subseq(slow_subchannel_set, schid); 730 spin_unlock_irq(&slow_subchannel_lock); 731 break; 732 default: 733 rc = 0; 734 } 735 /* Allow scheduling here since the containing loop might 736 * take a while. */ 737 cond_resched(); 738 } 739 return rc; 740 } 741 742 static void css_slow_path_func(struct work_struct *unused) 743 { 744 unsigned long flags; 745 746 CIO_TRACE_EVENT(4, "slowpath"); 747 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn, 748 NULL); 749 spin_lock_irqsave(&slow_subchannel_lock, flags); 750 if (idset_is_empty(slow_subchannel_set)) { 751 atomic_set(&css_eval_scheduled, 0); 752 wake_up(&css_eval_wq); 753 } 754 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 755 } 756 757 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func); 758 struct workqueue_struct *cio_work_q; 759 760 void css_schedule_eval(struct subchannel_id schid) 761 { 762 unsigned long flags; 763 764 spin_lock_irqsave(&slow_subchannel_lock, flags); 765 idset_sch_add(slow_subchannel_set, schid); 766 atomic_set(&css_eval_scheduled, 1); 767 queue_delayed_work(cio_work_q, &slow_path_work, 0); 768 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 769 } 770 771 void css_schedule_eval_all(void) 772 { 773 unsigned long flags; 774 775 spin_lock_irqsave(&slow_subchannel_lock, flags); 776 idset_fill(slow_subchannel_set); 777 atomic_set(&css_eval_scheduled, 1); 778 queue_delayed_work(cio_work_q, &slow_path_work, 0); 779 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 780 } 781 782 static int __unset_registered(struct device *dev, void *data) 783 { 784 struct idset *set = data; 785 struct subchannel *sch = to_subchannel(dev); 786 787 idset_sch_del(set, sch->schid); 788 return 0; 789 } 790 791 void css_schedule_eval_all_unreg(unsigned long delay) 792 { 793 unsigned long flags; 794 struct idset *unreg_set; 795 796 /* Find unregistered subchannels. */ 797 unreg_set = idset_sch_new(); 798 if (!unreg_set) { 799 /* Fallback. */ 800 css_schedule_eval_all(); 801 return; 802 } 803 idset_fill(unreg_set); 804 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered); 805 /* Apply to slow_subchannel_set. */ 806 spin_lock_irqsave(&slow_subchannel_lock, flags); 807 idset_add_set(slow_subchannel_set, unreg_set); 808 atomic_set(&css_eval_scheduled, 1); 809 queue_delayed_work(cio_work_q, &slow_path_work, delay); 810 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 811 idset_free(unreg_set); 812 } 813 814 void css_wait_for_slow_path(void) 815 { 816 flush_workqueue(cio_work_q); 817 } 818 819 /* Schedule reprobing of all unregistered subchannels. */ 820 void css_schedule_reprobe(void) 821 { 822 /* Schedule with a delay to allow merging of subsequent calls. */ 823 css_schedule_eval_all_unreg(1 * HZ); 824 } 825 EXPORT_SYMBOL_GPL(css_schedule_reprobe); 826 827 /* 828 * Called from the machine check handler for subchannel report words. 829 */ 830 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow) 831 { 832 struct subchannel_id mchk_schid; 833 struct subchannel *sch; 834 835 if (overflow) { 836 css_schedule_eval_all(); 837 return; 838 } 839 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, " 840 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", 841 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc, 842 crw0->erc, crw0->rsid); 843 if (crw1) 844 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, " 845 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", 846 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc, 847 crw1->anc, crw1->erc, crw1->rsid); 848 init_subchannel_id(&mchk_schid); 849 mchk_schid.sch_no = crw0->rsid; 850 if (crw1) 851 mchk_schid.ssid = (crw1->rsid >> 4) & 3; 852 853 if (crw0->erc == CRW_ERC_PMOD) { 854 sch = get_subchannel_by_schid(mchk_schid); 855 if (sch) { 856 css_update_ssd_info(sch); 857 put_device(&sch->dev); 858 } 859 } 860 /* 861 * Since we are always presented with IPI in the CRW, we have to 862 * use stsch() to find out if the subchannel in question has come 863 * or gone. 864 */ 865 css_evaluate_subchannel(mchk_schid, 0); 866 } 867 868 static void __init 869 css_generate_pgid(struct channel_subsystem *css, u32 tod_high) 870 { 871 struct cpuid cpu_id; 872 873 if (css_general_characteristics.mcss) { 874 css->global_pgid.pgid_high.ext_cssid.version = 0x80; 875 css->global_pgid.pgid_high.ext_cssid.cssid = 876 css->id_valid ? css->cssid : 0; 877 } else { 878 css->global_pgid.pgid_high.cpu_addr = stap(); 879 } 880 get_cpu_id(&cpu_id); 881 css->global_pgid.cpu_id = cpu_id.ident; 882 css->global_pgid.cpu_model = cpu_id.machine; 883 css->global_pgid.tod_high = tod_high; 884 } 885 886 static void channel_subsystem_release(struct device *dev) 887 { 888 struct channel_subsystem *css = to_css(dev); 889 890 mutex_destroy(&css->mutex); 891 kfree(css); 892 } 893 894 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a, 895 char *buf) 896 { 897 struct channel_subsystem *css = to_css(dev); 898 899 if (!css->id_valid) 900 return -EINVAL; 901 902 return sprintf(buf, "%x\n", css->cssid); 903 } 904 static DEVICE_ATTR_RO(real_cssid); 905 906 static ssize_t rescan_store(struct device *dev, struct device_attribute *a, 907 const char *buf, size_t count) 908 { 909 CIO_TRACE_EVENT(4, "usr-rescan"); 910 911 css_schedule_eval_all(); 912 css_complete_work(); 913 914 return count; 915 } 916 static DEVICE_ATTR_WO(rescan); 917 918 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a, 919 char *buf) 920 { 921 struct channel_subsystem *css = to_css(dev); 922 int ret; 923 924 mutex_lock(&css->mutex); 925 ret = sprintf(buf, "%x\n", css->cm_enabled); 926 mutex_unlock(&css->mutex); 927 return ret; 928 } 929 930 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a, 931 const char *buf, size_t count) 932 { 933 struct channel_subsystem *css = to_css(dev); 934 unsigned long val; 935 int ret; 936 937 ret = kstrtoul(buf, 16, &val); 938 if (ret) 939 return ret; 940 mutex_lock(&css->mutex); 941 switch (val) { 942 case 0: 943 ret = css->cm_enabled ? chsc_secm(css, 0) : 0; 944 break; 945 case 1: 946 ret = css->cm_enabled ? 0 : chsc_secm(css, 1); 947 break; 948 default: 949 ret = -EINVAL; 950 } 951 mutex_unlock(&css->mutex); 952 return ret < 0 ? ret : count; 953 } 954 static DEVICE_ATTR_RW(cm_enable); 955 956 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr, 957 int index) 958 { 959 return css_chsc_characteristics.secm ? attr->mode : 0; 960 } 961 962 static struct attribute *cssdev_attrs[] = { 963 &dev_attr_real_cssid.attr, 964 &dev_attr_rescan.attr, 965 NULL, 966 }; 967 968 static struct attribute_group cssdev_attr_group = { 969 .attrs = cssdev_attrs, 970 }; 971 972 static struct attribute *cssdev_cm_attrs[] = { 973 &dev_attr_cm_enable.attr, 974 NULL, 975 }; 976 977 static struct attribute_group cssdev_cm_attr_group = { 978 .attrs = cssdev_cm_attrs, 979 .is_visible = cm_enable_mode, 980 }; 981 982 static const struct attribute_group *cssdev_attr_groups[] = { 983 &cssdev_attr_group, 984 &cssdev_cm_attr_group, 985 NULL, 986 }; 987 988 static int __init setup_css(int nr) 989 { 990 struct channel_subsystem *css; 991 int ret; 992 993 css = kzalloc(sizeof(*css), GFP_KERNEL); 994 if (!css) 995 return -ENOMEM; 996 997 channel_subsystems[nr] = css; 998 dev_set_name(&css->device, "css%x", nr); 999 css->device.groups = cssdev_attr_groups; 1000 css->device.release = channel_subsystem_release; 1001 /* 1002 * We currently allocate notifier bits with this (using 1003 * css->device as the device argument with the DMA API) 1004 * and are fine with 64 bit addresses. 1005 */ 1006 ret = dma_coerce_mask_and_coherent(&css->device, DMA_BIT_MASK(64)); 1007 if (ret) { 1008 kfree(css); 1009 goto out_err; 1010 } 1011 1012 mutex_init(&css->mutex); 1013 ret = chsc_get_cssid_iid(nr, &css->cssid, &css->iid); 1014 if (!ret) { 1015 css->id_valid = true; 1016 pr_info("Partition identifier %01x.%01x\n", css->cssid, 1017 css->iid); 1018 } 1019 css_generate_pgid(css, (u32) (get_tod_clock() >> 32)); 1020 1021 ret = device_register(&css->device); 1022 if (ret) { 1023 put_device(&css->device); 1024 goto out_err; 1025 } 1026 1027 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel), 1028 GFP_KERNEL); 1029 if (!css->pseudo_subchannel) { 1030 device_unregister(&css->device); 1031 ret = -ENOMEM; 1032 goto out_err; 1033 } 1034 1035 css->pseudo_subchannel->dev.parent = &css->device; 1036 css->pseudo_subchannel->dev.release = css_subchannel_release; 1037 mutex_init(&css->pseudo_subchannel->reg_mutex); 1038 ret = css_sch_create_locks(css->pseudo_subchannel); 1039 if (ret) { 1040 kfree(css->pseudo_subchannel); 1041 device_unregister(&css->device); 1042 goto out_err; 1043 } 1044 1045 dev_set_name(&css->pseudo_subchannel->dev, "defunct"); 1046 ret = device_register(&css->pseudo_subchannel->dev); 1047 if (ret) { 1048 put_device(&css->pseudo_subchannel->dev); 1049 device_unregister(&css->device); 1050 goto out_err; 1051 } 1052 1053 return ret; 1054 out_err: 1055 channel_subsystems[nr] = NULL; 1056 return ret; 1057 } 1058 1059 static int css_reboot_event(struct notifier_block *this, 1060 unsigned long event, 1061 void *ptr) 1062 { 1063 struct channel_subsystem *css; 1064 int ret; 1065 1066 ret = NOTIFY_DONE; 1067 for_each_css(css) { 1068 mutex_lock(&css->mutex); 1069 if (css->cm_enabled) 1070 if (chsc_secm(css, 0)) 1071 ret = NOTIFY_BAD; 1072 mutex_unlock(&css->mutex); 1073 } 1074 1075 return ret; 1076 } 1077 1078 static struct notifier_block css_reboot_notifier = { 1079 .notifier_call = css_reboot_event, 1080 }; 1081 1082 #define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO) 1083 static struct gen_pool *cio_dma_pool; 1084 1085 /* Currently cio supports only a single css */ 1086 struct device *cio_get_dma_css_dev(void) 1087 { 1088 return &channel_subsystems[0]->device; 1089 } 1090 1091 struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages) 1092 { 1093 struct gen_pool *gp_dma; 1094 void *cpu_addr; 1095 dma_addr_t dma_addr; 1096 int i; 1097 1098 gp_dma = gen_pool_create(3, -1); 1099 if (!gp_dma) 1100 return NULL; 1101 for (i = 0; i < nr_pages; ++i) { 1102 cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr, 1103 CIO_DMA_GFP); 1104 if (!cpu_addr) 1105 return gp_dma; 1106 gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr, 1107 dma_addr, PAGE_SIZE, -1); 1108 } 1109 return gp_dma; 1110 } 1111 1112 static void __gp_dma_free_dma(struct gen_pool *pool, 1113 struct gen_pool_chunk *chunk, void *data) 1114 { 1115 size_t chunk_size = chunk->end_addr - chunk->start_addr + 1; 1116 1117 dma_free_coherent((struct device *) data, chunk_size, 1118 (void *) chunk->start_addr, 1119 (dma_addr_t) chunk->phys_addr); 1120 } 1121 1122 void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev) 1123 { 1124 if (!gp_dma) 1125 return; 1126 /* this is quite ugly but no better idea */ 1127 gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev); 1128 gen_pool_destroy(gp_dma); 1129 } 1130 1131 static int cio_dma_pool_init(void) 1132 { 1133 /* No need to free up the resources: compiled in */ 1134 cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1); 1135 if (!cio_dma_pool) 1136 return -ENOMEM; 1137 return 0; 1138 } 1139 1140 void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev, 1141 size_t size) 1142 { 1143 dma_addr_t dma_addr; 1144 unsigned long addr; 1145 size_t chunk_size; 1146 1147 if (!gp_dma) 1148 return NULL; 1149 addr = gen_pool_alloc(gp_dma, size); 1150 while (!addr) { 1151 chunk_size = round_up(size, PAGE_SIZE); 1152 addr = (unsigned long) dma_alloc_coherent(dma_dev, 1153 chunk_size, &dma_addr, CIO_DMA_GFP); 1154 if (!addr) 1155 return NULL; 1156 gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1); 1157 addr = gen_pool_alloc(gp_dma, size); 1158 } 1159 return (void *) addr; 1160 } 1161 1162 void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size) 1163 { 1164 if (!cpu_addr) 1165 return; 1166 memset(cpu_addr, 0, size); 1167 gen_pool_free(gp_dma, (unsigned long) cpu_addr, size); 1168 } 1169 1170 /* 1171 * Allocate dma memory from the css global pool. Intended for memory not 1172 * specific to any single device within the css. The allocated memory 1173 * is not guaranteed to be 31-bit addressable. 1174 * 1175 * Caution: Not suitable for early stuff like console. 1176 */ 1177 void *cio_dma_zalloc(size_t size) 1178 { 1179 return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size); 1180 } 1181 1182 void cio_dma_free(void *cpu_addr, size_t size) 1183 { 1184 cio_gp_dma_free(cio_dma_pool, cpu_addr, size); 1185 } 1186 1187 /* 1188 * Now that the driver core is running, we can setup our channel subsystem. 1189 * The struct subchannel's are created during probing. 1190 */ 1191 static int __init css_bus_init(void) 1192 { 1193 int ret, i; 1194 1195 ret = chsc_init(); 1196 if (ret) 1197 return ret; 1198 1199 chsc_determine_css_characteristics(); 1200 /* Try to enable MSS. */ 1201 ret = chsc_enable_facility(CHSC_SDA_OC_MSS); 1202 if (ret) 1203 max_ssid = 0; 1204 else /* Success. */ 1205 max_ssid = __MAX_SSID; 1206 1207 ret = slow_subchannel_init(); 1208 if (ret) 1209 goto out; 1210 1211 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw); 1212 if (ret) 1213 goto out; 1214 1215 if ((ret = bus_register(&css_bus_type))) 1216 goto out; 1217 1218 /* Setup css structure. */ 1219 for (i = 0; i <= MAX_CSS_IDX; i++) { 1220 ret = setup_css(i); 1221 if (ret) 1222 goto out_unregister; 1223 } 1224 ret = register_reboot_notifier(&css_reboot_notifier); 1225 if (ret) 1226 goto out_unregister; 1227 ret = cio_dma_pool_init(); 1228 if (ret) 1229 goto out_unregister_rn; 1230 airq_init(); 1231 css_init_done = 1; 1232 1233 /* Enable default isc for I/O subchannels. */ 1234 isc_register(IO_SCH_ISC); 1235 1236 return 0; 1237 out_unregister_rn: 1238 unregister_reboot_notifier(&css_reboot_notifier); 1239 out_unregister: 1240 while (i-- > 0) { 1241 struct channel_subsystem *css = channel_subsystems[i]; 1242 device_unregister(&css->pseudo_subchannel->dev); 1243 device_unregister(&css->device); 1244 } 1245 bus_unregister(&css_bus_type); 1246 out: 1247 crw_unregister_handler(CRW_RSC_SCH); 1248 idset_free(slow_subchannel_set); 1249 chsc_init_cleanup(); 1250 pr_alert("The CSS device driver initialization failed with " 1251 "errno=%d\n", ret); 1252 return ret; 1253 } 1254 1255 static void __init css_bus_cleanup(void) 1256 { 1257 struct channel_subsystem *css; 1258 1259 for_each_css(css) { 1260 device_unregister(&css->pseudo_subchannel->dev); 1261 device_unregister(&css->device); 1262 } 1263 bus_unregister(&css_bus_type); 1264 crw_unregister_handler(CRW_RSC_SCH); 1265 idset_free(slow_subchannel_set); 1266 chsc_init_cleanup(); 1267 isc_unregister(IO_SCH_ISC); 1268 } 1269 1270 static int __init channel_subsystem_init(void) 1271 { 1272 int ret; 1273 1274 ret = css_bus_init(); 1275 if (ret) 1276 return ret; 1277 cio_work_q = create_singlethread_workqueue("cio"); 1278 if (!cio_work_q) { 1279 ret = -ENOMEM; 1280 goto out_bus; 1281 } 1282 ret = io_subchannel_init(); 1283 if (ret) 1284 goto out_wq; 1285 1286 /* Register subchannels which are already in use. */ 1287 cio_register_early_subchannels(); 1288 /* Start initial subchannel evaluation. */ 1289 css_schedule_eval_all(); 1290 1291 return ret; 1292 out_wq: 1293 destroy_workqueue(cio_work_q); 1294 out_bus: 1295 css_bus_cleanup(); 1296 return ret; 1297 } 1298 subsys_initcall(channel_subsystem_init); 1299 1300 static int css_settle(struct device_driver *drv, void *unused) 1301 { 1302 struct css_driver *cssdrv = to_cssdriver(drv); 1303 1304 if (cssdrv->settle) 1305 return cssdrv->settle(); 1306 return 0; 1307 } 1308 1309 int css_complete_work(void) 1310 { 1311 int ret; 1312 1313 /* Wait for the evaluation of subchannels to finish. */ 1314 ret = wait_event_interruptible(css_eval_wq, 1315 atomic_read(&css_eval_scheduled) == 0); 1316 if (ret) 1317 return -EINTR; 1318 flush_workqueue(cio_work_q); 1319 /* Wait for the subchannel type specific initialization to finish */ 1320 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle); 1321 } 1322 1323 1324 /* 1325 * Wait for the initialization of devices to finish, to make sure we are 1326 * done with our setup if the search for the root device starts. 1327 */ 1328 static int __init channel_subsystem_init_sync(void) 1329 { 1330 css_complete_work(); 1331 return 0; 1332 } 1333 subsys_initcall_sync(channel_subsystem_init_sync); 1334 1335 #ifdef CONFIG_PROC_FS 1336 static ssize_t cio_settle_write(struct file *file, const char __user *buf, 1337 size_t count, loff_t *ppos) 1338 { 1339 int ret; 1340 1341 /* Handle pending CRW's. */ 1342 crw_wait_for_channel_report(); 1343 ret = css_complete_work(); 1344 1345 return ret ? ret : count; 1346 } 1347 1348 static const struct proc_ops cio_settle_proc_ops = { 1349 .proc_open = nonseekable_open, 1350 .proc_write = cio_settle_write, 1351 .proc_lseek = no_llseek, 1352 }; 1353 1354 static int __init cio_settle_init(void) 1355 { 1356 struct proc_dir_entry *entry; 1357 1358 entry = proc_create("cio_settle", S_IWUSR, NULL, &cio_settle_proc_ops); 1359 if (!entry) 1360 return -ENOMEM; 1361 return 0; 1362 } 1363 device_initcall(cio_settle_init); 1364 #endif /*CONFIG_PROC_FS*/ 1365 1366 int sch_is_pseudo_sch(struct subchannel *sch) 1367 { 1368 if (!sch->dev.parent) 1369 return 0; 1370 return sch == to_css(sch->dev.parent)->pseudo_subchannel; 1371 } 1372 1373 static int css_bus_match(struct device *dev, struct device_driver *drv) 1374 { 1375 struct subchannel *sch = to_subchannel(dev); 1376 struct css_driver *driver = to_cssdriver(drv); 1377 struct css_device_id *id; 1378 1379 /* When driver_override is set, only bind to the matching driver */ 1380 if (sch->driver_override && strcmp(sch->driver_override, drv->name)) 1381 return 0; 1382 1383 for (id = driver->subchannel_type; id->match_flags; id++) { 1384 if (sch->st == id->type) 1385 return 1; 1386 } 1387 1388 return 0; 1389 } 1390 1391 static int css_probe(struct device *dev) 1392 { 1393 struct subchannel *sch; 1394 int ret; 1395 1396 sch = to_subchannel(dev); 1397 sch->driver = to_cssdriver(dev->driver); 1398 ret = sch->driver->probe ? sch->driver->probe(sch) : 0; 1399 if (ret) 1400 sch->driver = NULL; 1401 return ret; 1402 } 1403 1404 static void css_remove(struct device *dev) 1405 { 1406 struct subchannel *sch; 1407 1408 sch = to_subchannel(dev); 1409 if (sch->driver->remove) 1410 sch->driver->remove(sch); 1411 sch->driver = NULL; 1412 } 1413 1414 static void css_shutdown(struct device *dev) 1415 { 1416 struct subchannel *sch; 1417 1418 sch = to_subchannel(dev); 1419 if (sch->driver && sch->driver->shutdown) 1420 sch->driver->shutdown(sch); 1421 } 1422 1423 static int css_uevent(struct device *dev, struct kobj_uevent_env *env) 1424 { 1425 struct subchannel *sch = to_subchannel(dev); 1426 int ret; 1427 1428 ret = add_uevent_var(env, "ST=%01X", sch->st); 1429 if (ret) 1430 return ret; 1431 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st); 1432 return ret; 1433 } 1434 1435 static struct bus_type css_bus_type = { 1436 .name = "css", 1437 .match = css_bus_match, 1438 .probe = css_probe, 1439 .remove = css_remove, 1440 .shutdown = css_shutdown, 1441 .uevent = css_uevent, 1442 }; 1443 1444 /** 1445 * css_driver_register - register a css driver 1446 * @cdrv: css driver to register 1447 * 1448 * This is mainly a wrapper around driver_register that sets name 1449 * and bus_type in the embedded struct device_driver correctly. 1450 */ 1451 int css_driver_register(struct css_driver *cdrv) 1452 { 1453 cdrv->drv.bus = &css_bus_type; 1454 return driver_register(&cdrv->drv); 1455 } 1456 EXPORT_SYMBOL_GPL(css_driver_register); 1457 1458 /** 1459 * css_driver_unregister - unregister a css driver 1460 * @cdrv: css driver to unregister 1461 * 1462 * This is a wrapper around driver_unregister. 1463 */ 1464 void css_driver_unregister(struct css_driver *cdrv) 1465 { 1466 driver_unregister(&cdrv->drv); 1467 } 1468 EXPORT_SYMBOL_GPL(css_driver_unregister); 1469