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 && pmcw->dnv) || 441 (pmcw->st == SUBCHANNEL_TYPE_MSG && pmcw->w)) 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 css_update_ssd_info(sch); 474 /* make it known to the system */ 475 ret = css_sch_device_register(sch); 476 if (ret) { 477 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n", 478 sch->schid.ssid, sch->schid.sch_no, ret); 479 return ret; 480 } 481 return ret; 482 } 483 484 static int css_probe_device(struct subchannel_id schid, struct schib *schib) 485 { 486 struct subchannel *sch; 487 int ret; 488 489 sch = css_alloc_subchannel(schid, schib); 490 if (IS_ERR(sch)) 491 return PTR_ERR(sch); 492 493 ret = css_register_subchannel(sch); 494 if (ret) 495 put_device(&sch->dev); 496 497 return ret; 498 } 499 500 static int 501 check_subchannel(struct device *dev, const void *data) 502 { 503 struct subchannel *sch; 504 struct subchannel_id *schid = (void *)data; 505 506 sch = to_subchannel(dev); 507 return schid_equal(&sch->schid, schid); 508 } 509 510 struct subchannel * 511 get_subchannel_by_schid(struct subchannel_id schid) 512 { 513 struct device *dev; 514 515 dev = bus_find_device(&css_bus_type, NULL, 516 &schid, check_subchannel); 517 518 return dev ? to_subchannel(dev) : NULL; 519 } 520 521 /** 522 * css_sch_is_valid() - check if a subchannel is valid 523 * @schib: subchannel information block for the subchannel 524 */ 525 int css_sch_is_valid(struct schib *schib) 526 { 527 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv) 528 return 0; 529 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w) 530 return 0; 531 return 1; 532 } 533 EXPORT_SYMBOL_GPL(css_sch_is_valid); 534 535 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow) 536 { 537 struct schib schib; 538 int ccode; 539 540 if (!slow) { 541 /* Will be done on the slow path. */ 542 return -EAGAIN; 543 } 544 /* 545 * The first subchannel that is not-operational (ccode==3) 546 * indicates that there aren't any more devices available. 547 * If stsch gets an exception, it means the current subchannel set 548 * is not valid. 549 */ 550 ccode = stsch(schid, &schib); 551 if (ccode) 552 return (ccode == 3) ? -ENXIO : ccode; 553 554 return css_probe_device(schid, &schib); 555 } 556 557 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow) 558 { 559 int ret = 0; 560 561 if (sch->driver) { 562 if (sch->driver->sch_event) 563 ret = sch->driver->sch_event(sch, slow); 564 else 565 dev_dbg(&sch->dev, 566 "Got subchannel machine check but " 567 "no sch_event handler provided.\n"); 568 } 569 if (ret != 0 && ret != -EAGAIN) { 570 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n", 571 sch->schid.ssid, sch->schid.sch_no, ret); 572 } 573 return ret; 574 } 575 576 static void css_evaluate_subchannel(struct subchannel_id schid, int slow) 577 { 578 struct subchannel *sch; 579 int ret; 580 581 sch = get_subchannel_by_schid(schid); 582 if (sch) { 583 ret = css_evaluate_known_subchannel(sch, slow); 584 put_device(&sch->dev); 585 } else 586 ret = css_evaluate_new_subchannel(schid, slow); 587 if (ret == -EAGAIN) 588 css_schedule_eval(schid); 589 } 590 591 /** 592 * css_sched_sch_todo - schedule a subchannel operation 593 * @sch: subchannel 594 * @todo: todo 595 * 596 * Schedule the operation identified by @todo to be performed on the slow path 597 * workqueue. Do nothing if another operation with higher priority is already 598 * scheduled. Needs to be called with subchannel lock held. 599 */ 600 void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo) 601 { 602 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n", 603 sch->schid.ssid, sch->schid.sch_no, todo); 604 if (sch->todo >= todo) 605 return; 606 /* Get workqueue ref. */ 607 if (!get_device(&sch->dev)) 608 return; 609 sch->todo = todo; 610 if (!queue_work(cio_work_q, &sch->todo_work)) { 611 /* Already queued, release workqueue ref. */ 612 put_device(&sch->dev); 613 } 614 } 615 EXPORT_SYMBOL_GPL(css_sched_sch_todo); 616 617 static void css_sch_todo(struct work_struct *work) 618 { 619 struct subchannel *sch; 620 enum sch_todo todo; 621 int ret; 622 623 sch = container_of(work, struct subchannel, todo_work); 624 /* Find out todo. */ 625 spin_lock_irq(sch->lock); 626 todo = sch->todo; 627 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid, 628 sch->schid.sch_no, todo); 629 sch->todo = SCH_TODO_NOTHING; 630 spin_unlock_irq(sch->lock); 631 /* Perform todo. */ 632 switch (todo) { 633 case SCH_TODO_NOTHING: 634 break; 635 case SCH_TODO_EVAL: 636 ret = css_evaluate_known_subchannel(sch, 1); 637 if (ret == -EAGAIN) { 638 spin_lock_irq(sch->lock); 639 css_sched_sch_todo(sch, todo); 640 spin_unlock_irq(sch->lock); 641 } 642 break; 643 case SCH_TODO_UNREG: 644 css_sch_device_unregister(sch); 645 break; 646 } 647 /* Release workqueue ref. */ 648 put_device(&sch->dev); 649 } 650 651 static struct idset *slow_subchannel_set; 652 static DEFINE_SPINLOCK(slow_subchannel_lock); 653 static DECLARE_WAIT_QUEUE_HEAD(css_eval_wq); 654 static atomic_t css_eval_scheduled; 655 656 static int __init slow_subchannel_init(void) 657 { 658 atomic_set(&css_eval_scheduled, 0); 659 slow_subchannel_set = idset_sch_new(); 660 if (!slow_subchannel_set) { 661 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n"); 662 return -ENOMEM; 663 } 664 return 0; 665 } 666 667 static int slow_eval_known_fn(struct subchannel *sch, void *data) 668 { 669 int eval; 670 int rc; 671 672 spin_lock_irq(&slow_subchannel_lock); 673 eval = idset_sch_contains(slow_subchannel_set, sch->schid); 674 idset_sch_del(slow_subchannel_set, sch->schid); 675 spin_unlock_irq(&slow_subchannel_lock); 676 if (eval) { 677 rc = css_evaluate_known_subchannel(sch, 1); 678 if (rc == -EAGAIN) 679 css_schedule_eval(sch->schid); 680 /* 681 * The loop might take long time for platforms with lots of 682 * known devices. Allow scheduling here. 683 */ 684 cond_resched(); 685 } 686 return 0; 687 } 688 689 static int slow_eval_unknown_fn(struct subchannel_id schid, void *data) 690 { 691 int eval; 692 int rc = 0; 693 694 spin_lock_irq(&slow_subchannel_lock); 695 eval = idset_sch_contains(slow_subchannel_set, schid); 696 idset_sch_del(slow_subchannel_set, schid); 697 spin_unlock_irq(&slow_subchannel_lock); 698 if (eval) { 699 rc = css_evaluate_new_subchannel(schid, 1); 700 switch (rc) { 701 case -EAGAIN: 702 css_schedule_eval(schid); 703 rc = 0; 704 break; 705 case -ENXIO: 706 case -ENOMEM: 707 case -EIO: 708 /* These should abort looping */ 709 spin_lock_irq(&slow_subchannel_lock); 710 idset_sch_del_subseq(slow_subchannel_set, schid); 711 spin_unlock_irq(&slow_subchannel_lock); 712 break; 713 default: 714 rc = 0; 715 } 716 /* Allow scheduling here since the containing loop might 717 * take a while. */ 718 cond_resched(); 719 } 720 return rc; 721 } 722 723 static void css_slow_path_func(struct work_struct *unused) 724 { 725 unsigned long flags; 726 727 CIO_TRACE_EVENT(4, "slowpath"); 728 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn, 729 NULL); 730 spin_lock_irqsave(&slow_subchannel_lock, flags); 731 if (idset_is_empty(slow_subchannel_set)) { 732 atomic_set(&css_eval_scheduled, 0); 733 wake_up(&css_eval_wq); 734 } 735 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 736 } 737 738 static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func); 739 struct workqueue_struct *cio_work_q; 740 741 void css_schedule_eval(struct subchannel_id schid) 742 { 743 unsigned long flags; 744 745 spin_lock_irqsave(&slow_subchannel_lock, flags); 746 idset_sch_add(slow_subchannel_set, schid); 747 atomic_set(&css_eval_scheduled, 1); 748 queue_delayed_work(cio_work_q, &slow_path_work, 0); 749 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 750 } 751 752 void css_schedule_eval_all(void) 753 { 754 unsigned long flags; 755 756 spin_lock_irqsave(&slow_subchannel_lock, flags); 757 idset_fill(slow_subchannel_set); 758 atomic_set(&css_eval_scheduled, 1); 759 queue_delayed_work(cio_work_q, &slow_path_work, 0); 760 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 761 } 762 763 static int __unset_registered(struct device *dev, void *data) 764 { 765 struct idset *set = data; 766 struct subchannel *sch = to_subchannel(dev); 767 768 idset_sch_del(set, sch->schid); 769 return 0; 770 } 771 772 static int __unset_online(struct device *dev, void *data) 773 { 774 struct idset *set = data; 775 struct subchannel *sch = to_subchannel(dev); 776 struct ccw_device *cdev; 777 778 if (sch->st == SUBCHANNEL_TYPE_IO) { 779 cdev = sch_get_cdev(sch); 780 if (cdev && cdev->online) 781 idset_sch_del(set, sch->schid); 782 } 783 784 return 0; 785 } 786 787 void css_schedule_eval_cond(enum css_eval_cond cond, unsigned long delay) 788 { 789 unsigned long flags; 790 struct idset *set; 791 792 /* Find unregistered subchannels. */ 793 set = idset_sch_new(); 794 if (!set) { 795 /* Fallback. */ 796 css_schedule_eval_all(); 797 return; 798 } 799 idset_fill(set); 800 switch (cond) { 801 case CSS_EVAL_UNREG: 802 bus_for_each_dev(&css_bus_type, NULL, set, __unset_registered); 803 break; 804 case CSS_EVAL_NOT_ONLINE: 805 bus_for_each_dev(&css_bus_type, NULL, set, __unset_online); 806 break; 807 default: 808 break; 809 } 810 811 /* Apply to slow_subchannel_set. */ 812 spin_lock_irqsave(&slow_subchannel_lock, flags); 813 idset_add_set(slow_subchannel_set, set); 814 atomic_set(&css_eval_scheduled, 1); 815 queue_delayed_work(cio_work_q, &slow_path_work, delay); 816 spin_unlock_irqrestore(&slow_subchannel_lock, flags); 817 idset_free(set); 818 } 819 820 void css_wait_for_slow_path(void) 821 { 822 flush_workqueue(cio_work_q); 823 } 824 825 /* Schedule reprobing of all unregistered subchannels. */ 826 void css_schedule_reprobe(void) 827 { 828 /* Schedule with a delay to allow merging of subsequent calls. */ 829 css_schedule_eval_cond(CSS_EVAL_UNREG, 1 * HZ); 830 } 831 EXPORT_SYMBOL_GPL(css_schedule_reprobe); 832 833 /* 834 * Called from the machine check handler for subchannel report words. 835 */ 836 static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow) 837 { 838 struct subchannel_id mchk_schid; 839 struct subchannel *sch; 840 841 if (overflow) { 842 css_schedule_eval_all(); 843 return; 844 } 845 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, " 846 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", 847 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc, 848 crw0->erc, crw0->rsid); 849 if (crw1) 850 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, " 851 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n", 852 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc, 853 crw1->anc, crw1->erc, crw1->rsid); 854 init_subchannel_id(&mchk_schid); 855 mchk_schid.sch_no = crw0->rsid; 856 if (crw1) 857 mchk_schid.ssid = (crw1->rsid >> 4) & 3; 858 859 if (crw0->erc == CRW_ERC_PMOD) { 860 sch = get_subchannel_by_schid(mchk_schid); 861 if (sch) { 862 css_update_ssd_info(sch); 863 put_device(&sch->dev); 864 } 865 } 866 /* 867 * Since we are always presented with IPI in the CRW, we have to 868 * use stsch() to find out if the subchannel in question has come 869 * or gone. 870 */ 871 css_evaluate_subchannel(mchk_schid, 0); 872 } 873 874 static void __init 875 css_generate_pgid(struct channel_subsystem *css, u32 tod_high) 876 { 877 struct cpuid cpu_id; 878 879 if (css_general_characteristics.mcss) { 880 css->global_pgid.pgid_high.ext_cssid.version = 0x80; 881 css->global_pgid.pgid_high.ext_cssid.cssid = 882 css->id_valid ? css->cssid : 0; 883 } else { 884 css->global_pgid.pgid_high.cpu_addr = stap(); 885 } 886 get_cpu_id(&cpu_id); 887 css->global_pgid.cpu_id = cpu_id.ident; 888 css->global_pgid.cpu_model = cpu_id.machine; 889 css->global_pgid.tod_high = tod_high; 890 } 891 892 static void channel_subsystem_release(struct device *dev) 893 { 894 struct channel_subsystem *css = to_css(dev); 895 896 mutex_destroy(&css->mutex); 897 kfree(css); 898 } 899 900 static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a, 901 char *buf) 902 { 903 struct channel_subsystem *css = to_css(dev); 904 905 if (!css->id_valid) 906 return -EINVAL; 907 908 return sprintf(buf, "%x\n", css->cssid); 909 } 910 static DEVICE_ATTR_RO(real_cssid); 911 912 static ssize_t rescan_store(struct device *dev, struct device_attribute *a, 913 const char *buf, size_t count) 914 { 915 CIO_TRACE_EVENT(4, "usr-rescan"); 916 917 css_schedule_eval_all(); 918 css_complete_work(); 919 920 return count; 921 } 922 static DEVICE_ATTR_WO(rescan); 923 924 static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a, 925 char *buf) 926 { 927 struct channel_subsystem *css = to_css(dev); 928 int ret; 929 930 mutex_lock(&css->mutex); 931 ret = sprintf(buf, "%x\n", css->cm_enabled); 932 mutex_unlock(&css->mutex); 933 return ret; 934 } 935 936 static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a, 937 const char *buf, size_t count) 938 { 939 struct channel_subsystem *css = to_css(dev); 940 unsigned long val; 941 int ret; 942 943 ret = kstrtoul(buf, 16, &val); 944 if (ret) 945 return ret; 946 mutex_lock(&css->mutex); 947 switch (val) { 948 case 0: 949 ret = css->cm_enabled ? chsc_secm(css, 0) : 0; 950 break; 951 case 1: 952 ret = css->cm_enabled ? 0 : chsc_secm(css, 1); 953 break; 954 default: 955 ret = -EINVAL; 956 } 957 mutex_unlock(&css->mutex); 958 return ret < 0 ? ret : count; 959 } 960 static DEVICE_ATTR_RW(cm_enable); 961 962 static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr, 963 int index) 964 { 965 return css_chsc_characteristics.secm ? attr->mode : 0; 966 } 967 968 static struct attribute *cssdev_attrs[] = { 969 &dev_attr_real_cssid.attr, 970 &dev_attr_rescan.attr, 971 NULL, 972 }; 973 974 static struct attribute_group cssdev_attr_group = { 975 .attrs = cssdev_attrs, 976 }; 977 978 static struct attribute *cssdev_cm_attrs[] = { 979 &dev_attr_cm_enable.attr, 980 NULL, 981 }; 982 983 static struct attribute_group cssdev_cm_attr_group = { 984 .attrs = cssdev_cm_attrs, 985 .is_visible = cm_enable_mode, 986 }; 987 988 static const struct attribute_group *cssdev_attr_groups[] = { 989 &cssdev_attr_group, 990 &cssdev_cm_attr_group, 991 NULL, 992 }; 993 994 static int __init setup_css(int nr) 995 { 996 struct channel_subsystem *css; 997 int ret; 998 999 css = kzalloc(sizeof(*css), GFP_KERNEL); 1000 if (!css) 1001 return -ENOMEM; 1002 1003 channel_subsystems[nr] = css; 1004 dev_set_name(&css->device, "css%x", nr); 1005 css->device.groups = cssdev_attr_groups; 1006 css->device.release = channel_subsystem_release; 1007 /* 1008 * We currently allocate notifier bits with this (using 1009 * css->device as the device argument with the DMA API) 1010 * and are fine with 64 bit addresses. 1011 */ 1012 ret = dma_coerce_mask_and_coherent(&css->device, DMA_BIT_MASK(64)); 1013 if (ret) { 1014 kfree(css); 1015 goto out_err; 1016 } 1017 1018 mutex_init(&css->mutex); 1019 ret = chsc_get_cssid_iid(nr, &css->cssid, &css->iid); 1020 if (!ret) { 1021 css->id_valid = true; 1022 pr_info("Partition identifier %01x.%01x\n", css->cssid, 1023 css->iid); 1024 } 1025 css_generate_pgid(css, (u32) (get_tod_clock() >> 32)); 1026 1027 ret = device_register(&css->device); 1028 if (ret) { 1029 put_device(&css->device); 1030 goto out_err; 1031 } 1032 1033 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel), 1034 GFP_KERNEL); 1035 if (!css->pseudo_subchannel) { 1036 device_unregister(&css->device); 1037 ret = -ENOMEM; 1038 goto out_err; 1039 } 1040 1041 css->pseudo_subchannel->dev.parent = &css->device; 1042 css->pseudo_subchannel->dev.release = css_subchannel_release; 1043 mutex_init(&css->pseudo_subchannel->reg_mutex); 1044 ret = css_sch_create_locks(css->pseudo_subchannel); 1045 if (ret) { 1046 kfree(css->pseudo_subchannel); 1047 device_unregister(&css->device); 1048 goto out_err; 1049 } 1050 1051 dev_set_name(&css->pseudo_subchannel->dev, "defunct"); 1052 ret = device_register(&css->pseudo_subchannel->dev); 1053 if (ret) { 1054 put_device(&css->pseudo_subchannel->dev); 1055 device_unregister(&css->device); 1056 goto out_err; 1057 } 1058 1059 return ret; 1060 out_err: 1061 channel_subsystems[nr] = NULL; 1062 return ret; 1063 } 1064 1065 static int css_reboot_event(struct notifier_block *this, 1066 unsigned long event, 1067 void *ptr) 1068 { 1069 struct channel_subsystem *css; 1070 int ret; 1071 1072 ret = NOTIFY_DONE; 1073 for_each_css(css) { 1074 mutex_lock(&css->mutex); 1075 if (css->cm_enabled) 1076 if (chsc_secm(css, 0)) 1077 ret = NOTIFY_BAD; 1078 mutex_unlock(&css->mutex); 1079 } 1080 1081 return ret; 1082 } 1083 1084 static struct notifier_block css_reboot_notifier = { 1085 .notifier_call = css_reboot_event, 1086 }; 1087 1088 #define CIO_DMA_GFP (GFP_KERNEL | __GFP_ZERO) 1089 static struct gen_pool *cio_dma_pool; 1090 1091 /* Currently cio supports only a single css */ 1092 struct device *cio_get_dma_css_dev(void) 1093 { 1094 return &channel_subsystems[0]->device; 1095 } 1096 1097 struct gen_pool *cio_gp_dma_create(struct device *dma_dev, int nr_pages) 1098 { 1099 struct gen_pool *gp_dma; 1100 void *cpu_addr; 1101 dma_addr_t dma_addr; 1102 int i; 1103 1104 gp_dma = gen_pool_create(3, -1); 1105 if (!gp_dma) 1106 return NULL; 1107 for (i = 0; i < nr_pages; ++i) { 1108 cpu_addr = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr, 1109 CIO_DMA_GFP); 1110 if (!cpu_addr) 1111 return gp_dma; 1112 gen_pool_add_virt(gp_dma, (unsigned long) cpu_addr, 1113 dma_addr, PAGE_SIZE, -1); 1114 } 1115 return gp_dma; 1116 } 1117 1118 static void __gp_dma_free_dma(struct gen_pool *pool, 1119 struct gen_pool_chunk *chunk, void *data) 1120 { 1121 size_t chunk_size = chunk->end_addr - chunk->start_addr + 1; 1122 1123 dma_free_coherent((struct device *) data, chunk_size, 1124 (void *) chunk->start_addr, 1125 (dma_addr_t) chunk->phys_addr); 1126 } 1127 1128 void cio_gp_dma_destroy(struct gen_pool *gp_dma, struct device *dma_dev) 1129 { 1130 if (!gp_dma) 1131 return; 1132 /* this is quite ugly but no better idea */ 1133 gen_pool_for_each_chunk(gp_dma, __gp_dma_free_dma, dma_dev); 1134 gen_pool_destroy(gp_dma); 1135 } 1136 1137 static int cio_dma_pool_init(void) 1138 { 1139 /* No need to free up the resources: compiled in */ 1140 cio_dma_pool = cio_gp_dma_create(cio_get_dma_css_dev(), 1); 1141 if (!cio_dma_pool) 1142 return -ENOMEM; 1143 return 0; 1144 } 1145 1146 void *cio_gp_dma_zalloc(struct gen_pool *gp_dma, struct device *dma_dev, 1147 size_t size) 1148 { 1149 dma_addr_t dma_addr; 1150 unsigned long addr; 1151 size_t chunk_size; 1152 1153 if (!gp_dma) 1154 return NULL; 1155 addr = gen_pool_alloc(gp_dma, size); 1156 while (!addr) { 1157 chunk_size = round_up(size, PAGE_SIZE); 1158 addr = (unsigned long) dma_alloc_coherent(dma_dev, 1159 chunk_size, &dma_addr, CIO_DMA_GFP); 1160 if (!addr) 1161 return NULL; 1162 gen_pool_add_virt(gp_dma, addr, dma_addr, chunk_size, -1); 1163 addr = gen_pool_alloc(gp_dma, size); 1164 } 1165 return (void *) addr; 1166 } 1167 1168 void cio_gp_dma_free(struct gen_pool *gp_dma, void *cpu_addr, size_t size) 1169 { 1170 if (!cpu_addr) 1171 return; 1172 memset(cpu_addr, 0, size); 1173 gen_pool_free(gp_dma, (unsigned long) cpu_addr, size); 1174 } 1175 1176 /* 1177 * Allocate dma memory from the css global pool. Intended for memory not 1178 * specific to any single device within the css. The allocated memory 1179 * is not guaranteed to be 31-bit addressable. 1180 * 1181 * Caution: Not suitable for early stuff like console. 1182 */ 1183 void *cio_dma_zalloc(size_t size) 1184 { 1185 return cio_gp_dma_zalloc(cio_dma_pool, cio_get_dma_css_dev(), size); 1186 } 1187 1188 void cio_dma_free(void *cpu_addr, size_t size) 1189 { 1190 cio_gp_dma_free(cio_dma_pool, cpu_addr, size); 1191 } 1192 1193 /* 1194 * Now that the driver core is running, we can setup our channel subsystem. 1195 * The struct subchannel's are created during probing. 1196 */ 1197 static int __init css_bus_init(void) 1198 { 1199 int ret, i; 1200 1201 ret = chsc_init(); 1202 if (ret) 1203 return ret; 1204 1205 chsc_determine_css_characteristics(); 1206 /* Try to enable MSS. */ 1207 ret = chsc_enable_facility(CHSC_SDA_OC_MSS); 1208 if (ret) 1209 max_ssid = 0; 1210 else /* Success. */ 1211 max_ssid = __MAX_SSID; 1212 1213 ret = slow_subchannel_init(); 1214 if (ret) 1215 goto out; 1216 1217 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw); 1218 if (ret) 1219 goto out; 1220 1221 if ((ret = bus_register(&css_bus_type))) 1222 goto out; 1223 1224 /* Setup css structure. */ 1225 for (i = 0; i <= MAX_CSS_IDX; i++) { 1226 ret = setup_css(i); 1227 if (ret) 1228 goto out_unregister; 1229 } 1230 ret = register_reboot_notifier(&css_reboot_notifier); 1231 if (ret) 1232 goto out_unregister; 1233 ret = cio_dma_pool_init(); 1234 if (ret) 1235 goto out_unregister_rn; 1236 airq_init(); 1237 css_init_done = 1; 1238 1239 /* Enable default isc for I/O subchannels. */ 1240 isc_register(IO_SCH_ISC); 1241 1242 return 0; 1243 out_unregister_rn: 1244 unregister_reboot_notifier(&css_reboot_notifier); 1245 out_unregister: 1246 while (i-- > 0) { 1247 struct channel_subsystem *css = channel_subsystems[i]; 1248 device_unregister(&css->pseudo_subchannel->dev); 1249 device_unregister(&css->device); 1250 } 1251 bus_unregister(&css_bus_type); 1252 out: 1253 crw_unregister_handler(CRW_RSC_SCH); 1254 idset_free(slow_subchannel_set); 1255 chsc_init_cleanup(); 1256 pr_alert("The CSS device driver initialization failed with " 1257 "errno=%d\n", ret); 1258 return ret; 1259 } 1260 1261 static void __init css_bus_cleanup(void) 1262 { 1263 struct channel_subsystem *css; 1264 1265 for_each_css(css) { 1266 device_unregister(&css->pseudo_subchannel->dev); 1267 device_unregister(&css->device); 1268 } 1269 bus_unregister(&css_bus_type); 1270 crw_unregister_handler(CRW_RSC_SCH); 1271 idset_free(slow_subchannel_set); 1272 chsc_init_cleanup(); 1273 isc_unregister(IO_SCH_ISC); 1274 } 1275 1276 static int __init channel_subsystem_init(void) 1277 { 1278 int ret; 1279 1280 ret = css_bus_init(); 1281 if (ret) 1282 return ret; 1283 cio_work_q = create_singlethread_workqueue("cio"); 1284 if (!cio_work_q) { 1285 ret = -ENOMEM; 1286 goto out_bus; 1287 } 1288 ret = io_subchannel_init(); 1289 if (ret) 1290 goto out_wq; 1291 1292 /* Register subchannels which are already in use. */ 1293 cio_register_early_subchannels(); 1294 /* Start initial subchannel evaluation. */ 1295 css_schedule_eval_all(); 1296 1297 return ret; 1298 out_wq: 1299 destroy_workqueue(cio_work_q); 1300 out_bus: 1301 css_bus_cleanup(); 1302 return ret; 1303 } 1304 subsys_initcall(channel_subsystem_init); 1305 1306 static int css_settle(struct device_driver *drv, void *unused) 1307 { 1308 struct css_driver *cssdrv = to_cssdriver(drv); 1309 1310 if (cssdrv->settle) 1311 return cssdrv->settle(); 1312 return 0; 1313 } 1314 1315 int css_complete_work(void) 1316 { 1317 int ret; 1318 1319 /* Wait for the evaluation of subchannels to finish. */ 1320 ret = wait_event_interruptible(css_eval_wq, 1321 atomic_read(&css_eval_scheduled) == 0); 1322 if (ret) 1323 return -EINTR; 1324 flush_workqueue(cio_work_q); 1325 /* Wait for the subchannel type specific initialization to finish */ 1326 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle); 1327 } 1328 1329 1330 /* 1331 * Wait for the initialization of devices to finish, to make sure we are 1332 * done with our setup if the search for the root device starts. 1333 */ 1334 static int __init channel_subsystem_init_sync(void) 1335 { 1336 css_complete_work(); 1337 return 0; 1338 } 1339 subsys_initcall_sync(channel_subsystem_init_sync); 1340 1341 #ifdef CONFIG_PROC_FS 1342 static ssize_t cio_settle_write(struct file *file, const char __user *buf, 1343 size_t count, loff_t *ppos) 1344 { 1345 int ret; 1346 1347 /* Handle pending CRW's. */ 1348 crw_wait_for_channel_report(); 1349 ret = css_complete_work(); 1350 1351 return ret ? ret : count; 1352 } 1353 1354 static const struct proc_ops cio_settle_proc_ops = { 1355 .proc_open = nonseekable_open, 1356 .proc_write = cio_settle_write, 1357 .proc_lseek = no_llseek, 1358 }; 1359 1360 static int __init cio_settle_init(void) 1361 { 1362 struct proc_dir_entry *entry; 1363 1364 entry = proc_create("cio_settle", S_IWUSR, NULL, &cio_settle_proc_ops); 1365 if (!entry) 1366 return -ENOMEM; 1367 return 0; 1368 } 1369 device_initcall(cio_settle_init); 1370 #endif /*CONFIG_PROC_FS*/ 1371 1372 int sch_is_pseudo_sch(struct subchannel *sch) 1373 { 1374 if (!sch->dev.parent) 1375 return 0; 1376 return sch == to_css(sch->dev.parent)->pseudo_subchannel; 1377 } 1378 1379 static int css_bus_match(struct device *dev, struct device_driver *drv) 1380 { 1381 struct subchannel *sch = to_subchannel(dev); 1382 struct css_driver *driver = to_cssdriver(drv); 1383 struct css_device_id *id; 1384 1385 /* When driver_override is set, only bind to the matching driver */ 1386 if (sch->driver_override && strcmp(sch->driver_override, drv->name)) 1387 return 0; 1388 1389 for (id = driver->subchannel_type; id->match_flags; id++) { 1390 if (sch->st == id->type) 1391 return 1; 1392 } 1393 1394 return 0; 1395 } 1396 1397 static int css_probe(struct device *dev) 1398 { 1399 struct subchannel *sch; 1400 int ret; 1401 1402 sch = to_subchannel(dev); 1403 sch->driver = to_cssdriver(dev->driver); 1404 ret = sch->driver->probe ? sch->driver->probe(sch) : 0; 1405 if (ret) 1406 sch->driver = NULL; 1407 return ret; 1408 } 1409 1410 static void css_remove(struct device *dev) 1411 { 1412 struct subchannel *sch; 1413 1414 sch = to_subchannel(dev); 1415 if (sch->driver->remove) 1416 sch->driver->remove(sch); 1417 sch->driver = NULL; 1418 } 1419 1420 static void css_shutdown(struct device *dev) 1421 { 1422 struct subchannel *sch; 1423 1424 sch = to_subchannel(dev); 1425 if (sch->driver && sch->driver->shutdown) 1426 sch->driver->shutdown(sch); 1427 } 1428 1429 static int css_uevent(struct device *dev, struct kobj_uevent_env *env) 1430 { 1431 struct subchannel *sch = to_subchannel(dev); 1432 int ret; 1433 1434 ret = add_uevent_var(env, "ST=%01X", sch->st); 1435 if (ret) 1436 return ret; 1437 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st); 1438 return ret; 1439 } 1440 1441 static struct bus_type css_bus_type = { 1442 .name = "css", 1443 .match = css_bus_match, 1444 .probe = css_probe, 1445 .remove = css_remove, 1446 .shutdown = css_shutdown, 1447 .uevent = css_uevent, 1448 }; 1449 1450 /** 1451 * css_driver_register - register a css driver 1452 * @cdrv: css driver to register 1453 * 1454 * This is mainly a wrapper around driver_register that sets name 1455 * and bus_type in the embedded struct device_driver correctly. 1456 */ 1457 int css_driver_register(struct css_driver *cdrv) 1458 { 1459 cdrv->drv.bus = &css_bus_type; 1460 return driver_register(&cdrv->drv); 1461 } 1462 EXPORT_SYMBOL_GPL(css_driver_register); 1463 1464 /** 1465 * css_driver_unregister - unregister a css driver 1466 * @cdrv: css driver to unregister 1467 * 1468 * This is a wrapper around driver_unregister. 1469 */ 1470 void css_driver_unregister(struct css_driver *cdrv) 1471 { 1472 driver_unregister(&cdrv->drv); 1473 } 1474 EXPORT_SYMBOL_GPL(css_driver_unregister); 1475