1 /* 2 * Copyright IBM Corp. 2006, 2012 3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> 4 * Martin Schwidefsky <schwidefsky@de.ibm.com> 5 * Ralph Wuerthner <rwuerthn@de.ibm.com> 6 * Felix Beck <felix.beck@de.ibm.com> 7 * Holger Dengler <hd@linux.vnet.ibm.com> 8 * 9 * Adjunct processor bus. 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2, or (at your option) 14 * any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 */ 25 26 #define KMSG_COMPONENT "ap" 27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 28 29 #include <linux/kernel_stat.h> 30 #include <linux/moduleparam.h> 31 #include <linux/init.h> 32 #include <linux/delay.h> 33 #include <linux/err.h> 34 #include <linux/interrupt.h> 35 #include <linux/workqueue.h> 36 #include <linux/slab.h> 37 #include <linux/notifier.h> 38 #include <linux/kthread.h> 39 #include <linux/mutex.h> 40 #include <linux/suspend.h> 41 #include <asm/reset.h> 42 #include <asm/airq.h> 43 #include <linux/atomic.h> 44 #include <asm/isc.h> 45 #include <linux/hrtimer.h> 46 #include <linux/ktime.h> 47 #include <asm/facility.h> 48 #include <linux/crypto.h> 49 #include <linux/mod_devicetable.h> 50 #include <linux/debugfs.h> 51 52 #include "ap_bus.h" 53 #include "ap_asm.h" 54 #include "ap_debug.h" 55 56 /* 57 * Module parameters; note though this file itself isn't modular. 58 */ 59 int ap_domain_index = -1; /* Adjunct Processor Domain Index */ 60 static DEFINE_SPINLOCK(ap_domain_lock); 61 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP); 62 MODULE_PARM_DESC(domain, "domain index for ap devices"); 63 EXPORT_SYMBOL(ap_domain_index); 64 65 static int ap_thread_flag = 0; 66 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP); 67 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); 68 69 static struct device *ap_root_device; 70 71 DEFINE_SPINLOCK(ap_list_lock); 72 LIST_HEAD(ap_card_list); 73 74 static struct ap_config_info *ap_configuration; 75 static bool initialised; 76 77 /* 78 * AP bus related debug feature things. 79 */ 80 debug_info_t *ap_dbf_info; 81 82 /* 83 * Workqueue timer for bus rescan. 84 */ 85 static struct timer_list ap_config_timer; 86 static int ap_config_time = AP_CONFIG_TIME; 87 static void ap_scan_bus(struct work_struct *); 88 static DECLARE_WORK(ap_scan_work, ap_scan_bus); 89 90 /* 91 * Tasklet & timer for AP request polling and interrupts 92 */ 93 static void ap_tasklet_fn(unsigned long); 94 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0); 95 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); 96 static struct task_struct *ap_poll_kthread = NULL; 97 static DEFINE_MUTEX(ap_poll_thread_mutex); 98 static DEFINE_SPINLOCK(ap_poll_timer_lock); 99 static struct hrtimer ap_poll_timer; 100 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. 101 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/ 102 static unsigned long long poll_timeout = 250000; 103 104 /* Suspend flag */ 105 static int ap_suspend_flag; 106 /* Maximum domain id */ 107 static int ap_max_domain_id; 108 /* Flag to check if domain was set through module parameter domain=. This is 109 * important when supsend and resume is done in a z/VM environment where the 110 * domain might change. */ 111 static int user_set_domain = 0; 112 static struct bus_type ap_bus_type; 113 114 /* Adapter interrupt definitions */ 115 static void ap_interrupt_handler(struct airq_struct *airq); 116 117 static int ap_airq_flag; 118 119 static struct airq_struct ap_airq = { 120 .handler = ap_interrupt_handler, 121 .isc = AP_ISC, 122 }; 123 124 /** 125 * ap_using_interrupts() - Returns non-zero if interrupt support is 126 * available. 127 */ 128 static inline int ap_using_interrupts(void) 129 { 130 return ap_airq_flag; 131 } 132 133 /** 134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator 135 * 136 * Returns the address of the local-summary-indicator of the adapter 137 * interrupt handler for AP, or NULL if adapter interrupts are not 138 * available. 139 */ 140 void *ap_airq_ptr(void) 141 { 142 if (ap_using_interrupts()) 143 return ap_airq.lsi_ptr; 144 return NULL; 145 } 146 147 /** 148 * ap_interrupts_available(): Test if AP interrupts are available. 149 * 150 * Returns 1 if AP interrupts are available. 151 */ 152 static int ap_interrupts_available(void) 153 { 154 return test_facility(65); 155 } 156 157 /** 158 * ap_configuration_available(): Test if AP configuration 159 * information is available. 160 * 161 * Returns 1 if AP configuration information is available. 162 */ 163 static int ap_configuration_available(void) 164 { 165 return test_facility(12); 166 } 167 168 /** 169 * ap_test_queue(): Test adjunct processor queue. 170 * @qid: The AP queue number 171 * @info: Pointer to queue descriptor 172 * 173 * Returns AP queue status structure. 174 */ 175 static inline struct ap_queue_status 176 ap_test_queue(ap_qid_t qid, unsigned long *info) 177 { 178 if (test_facility(15)) 179 qid |= 1UL << 23; /* set APFT T bit*/ 180 return ap_tapq(qid, info); 181 } 182 183 static inline int ap_query_configuration(void) 184 { 185 if (!ap_configuration) 186 return -EOPNOTSUPP; 187 return ap_qci(ap_configuration); 188 } 189 190 /** 191 * ap_init_configuration(): Allocate and query configuration array. 192 */ 193 static void ap_init_configuration(void) 194 { 195 if (!ap_configuration_available()) 196 return; 197 198 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL); 199 if (!ap_configuration) 200 return; 201 if (ap_query_configuration() != 0) { 202 kfree(ap_configuration); 203 ap_configuration = NULL; 204 return; 205 } 206 } 207 208 /* 209 * ap_test_config(): helper function to extract the nrth bit 210 * within the unsigned int array field. 211 */ 212 static inline int ap_test_config(unsigned int *field, unsigned int nr) 213 { 214 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); 215 } 216 217 /* 218 * ap_test_config_card_id(): Test, whether an AP card ID is configured. 219 * @id AP card ID 220 * 221 * Returns 0 if the card is not configured 222 * 1 if the card is configured or 223 * if the configuration information is not available 224 */ 225 static inline int ap_test_config_card_id(unsigned int id) 226 { 227 if (!ap_configuration) /* QCI not supported */ 228 return 1; 229 return ap_test_config(ap_configuration->apm, id); 230 } 231 232 /* 233 * ap_test_config_domain(): Test, whether an AP usage domain is configured. 234 * @domain AP usage domain ID 235 * 236 * Returns 0 if the usage domain is not configured 237 * 1 if the usage domain is configured or 238 * if the configuration information is not available 239 */ 240 static inline int ap_test_config_domain(unsigned int domain) 241 { 242 if (!ap_configuration) /* QCI not supported */ 243 return domain < 16; 244 return ap_test_config(ap_configuration->aqm, domain); 245 } 246 247 /** 248 * ap_query_queue(): Check if an AP queue is available. 249 * @qid: The AP queue number 250 * @queue_depth: Pointer to queue depth value 251 * @device_type: Pointer to device type value 252 * @facilities: Pointer to facility indicator 253 */ 254 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type, 255 unsigned int *facilities) 256 { 257 struct ap_queue_status status; 258 unsigned long info; 259 int nd; 260 261 if (!ap_test_config_card_id(AP_QID_CARD(qid))) 262 return -ENODEV; 263 264 status = ap_test_queue(qid, &info); 265 switch (status.response_code) { 266 case AP_RESPONSE_NORMAL: 267 *queue_depth = (int)(info & 0xff); 268 *device_type = (int)((info >> 24) & 0xff); 269 *facilities = (unsigned int)(info >> 32); 270 /* Update maximum domain id */ 271 nd = (info >> 16) & 0xff; 272 /* if N bit is available, z13 and newer */ 273 if ((info & (1UL << 57)) && nd > 0) 274 ap_max_domain_id = nd; 275 else /* older machine types */ 276 ap_max_domain_id = 15; 277 switch (*device_type) { 278 /* For CEX2 and CEX3 the available functions 279 * are not refrected by the facilities bits. 280 * Instead it is coded into the type. So here 281 * modify the function bits based on the type. 282 */ 283 case AP_DEVICE_TYPE_CEX2A: 284 case AP_DEVICE_TYPE_CEX3A: 285 *facilities |= 0x08000000; 286 break; 287 case AP_DEVICE_TYPE_CEX2C: 288 case AP_DEVICE_TYPE_CEX3C: 289 *facilities |= 0x10000000; 290 break; 291 default: 292 break; 293 } 294 return 0; 295 case AP_RESPONSE_Q_NOT_AVAIL: 296 case AP_RESPONSE_DECONFIGURED: 297 case AP_RESPONSE_CHECKSTOPPED: 298 case AP_RESPONSE_INVALID_ADDRESS: 299 return -ENODEV; 300 case AP_RESPONSE_RESET_IN_PROGRESS: 301 case AP_RESPONSE_OTHERWISE_CHANGED: 302 case AP_RESPONSE_BUSY: 303 return -EBUSY; 304 default: 305 BUG(); 306 } 307 } 308 309 void ap_wait(enum ap_wait wait) 310 { 311 ktime_t hr_time; 312 313 switch (wait) { 314 case AP_WAIT_AGAIN: 315 case AP_WAIT_INTERRUPT: 316 if (ap_using_interrupts()) 317 break; 318 if (ap_poll_kthread) { 319 wake_up(&ap_poll_wait); 320 break; 321 } 322 /* Fall through */ 323 case AP_WAIT_TIMEOUT: 324 spin_lock_bh(&ap_poll_timer_lock); 325 if (!hrtimer_is_queued(&ap_poll_timer)) { 326 hr_time = poll_timeout; 327 hrtimer_forward_now(&ap_poll_timer, hr_time); 328 hrtimer_restart(&ap_poll_timer); 329 } 330 spin_unlock_bh(&ap_poll_timer_lock); 331 break; 332 case AP_WAIT_NONE: 333 default: 334 break; 335 } 336 } 337 338 /** 339 * ap_request_timeout(): Handling of request timeouts 340 * @data: Holds the AP device. 341 * 342 * Handles request timeouts. 343 */ 344 void ap_request_timeout(unsigned long data) 345 { 346 struct ap_queue *aq = (struct ap_queue *) data; 347 348 if (ap_suspend_flag) 349 return; 350 spin_lock_bh(&aq->lock); 351 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT)); 352 spin_unlock_bh(&aq->lock); 353 } 354 355 /** 356 * ap_poll_timeout(): AP receive polling for finished AP requests. 357 * @unused: Unused pointer. 358 * 359 * Schedules the AP tasklet using a high resolution timer. 360 */ 361 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) 362 { 363 if (!ap_suspend_flag) 364 tasklet_schedule(&ap_tasklet); 365 return HRTIMER_NORESTART; 366 } 367 368 /** 369 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt 370 * @airq: pointer to adapter interrupt descriptor 371 */ 372 static void ap_interrupt_handler(struct airq_struct *airq) 373 { 374 inc_irq_stat(IRQIO_APB); 375 if (!ap_suspend_flag) 376 tasklet_schedule(&ap_tasklet); 377 } 378 379 /** 380 * ap_tasklet_fn(): Tasklet to poll all AP devices. 381 * @dummy: Unused variable 382 * 383 * Poll all AP devices on the bus. 384 */ 385 static void ap_tasklet_fn(unsigned long dummy) 386 { 387 struct ap_card *ac; 388 struct ap_queue *aq; 389 enum ap_wait wait = AP_WAIT_NONE; 390 391 /* Reset the indicator if interrupts are used. Thus new interrupts can 392 * be received. Doing it in the beginning of the tasklet is therefor 393 * important that no requests on any AP get lost. 394 */ 395 if (ap_using_interrupts()) 396 xchg(ap_airq.lsi_ptr, 0); 397 398 spin_lock_bh(&ap_list_lock); 399 for_each_ap_card(ac) { 400 for_each_ap_queue(aq, ac) { 401 spin_lock_bh(&aq->lock); 402 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL)); 403 spin_unlock_bh(&aq->lock); 404 } 405 } 406 spin_unlock_bh(&ap_list_lock); 407 408 ap_wait(wait); 409 } 410 411 static int ap_pending_requests(void) 412 { 413 struct ap_card *ac; 414 struct ap_queue *aq; 415 416 spin_lock_bh(&ap_list_lock); 417 for_each_ap_card(ac) { 418 for_each_ap_queue(aq, ac) { 419 if (aq->queue_count == 0) 420 continue; 421 spin_unlock_bh(&ap_list_lock); 422 return 1; 423 } 424 } 425 spin_unlock_bh(&ap_list_lock); 426 return 0; 427 } 428 429 /** 430 * ap_poll_thread(): Thread that polls for finished requests. 431 * @data: Unused pointer 432 * 433 * AP bus poll thread. The purpose of this thread is to poll for 434 * finished requests in a loop if there is a "free" cpu - that is 435 * a cpu that doesn't have anything better to do. The polling stops 436 * as soon as there is another task or if all messages have been 437 * delivered. 438 */ 439 static int ap_poll_thread(void *data) 440 { 441 DECLARE_WAITQUEUE(wait, current); 442 443 set_user_nice(current, MAX_NICE); 444 set_freezable(); 445 while (!kthread_should_stop()) { 446 add_wait_queue(&ap_poll_wait, &wait); 447 set_current_state(TASK_INTERRUPTIBLE); 448 if (ap_suspend_flag || !ap_pending_requests()) { 449 schedule(); 450 try_to_freeze(); 451 } 452 set_current_state(TASK_RUNNING); 453 remove_wait_queue(&ap_poll_wait, &wait); 454 if (need_resched()) { 455 schedule(); 456 try_to_freeze(); 457 continue; 458 } 459 ap_tasklet_fn(0); 460 } 461 462 return 0; 463 } 464 465 static int ap_poll_thread_start(void) 466 { 467 int rc; 468 469 if (ap_using_interrupts() || ap_poll_kthread) 470 return 0; 471 mutex_lock(&ap_poll_thread_mutex); 472 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); 473 rc = PTR_RET(ap_poll_kthread); 474 if (rc) 475 ap_poll_kthread = NULL; 476 mutex_unlock(&ap_poll_thread_mutex); 477 return rc; 478 } 479 480 static void ap_poll_thread_stop(void) 481 { 482 if (!ap_poll_kthread) 483 return; 484 mutex_lock(&ap_poll_thread_mutex); 485 kthread_stop(ap_poll_kthread); 486 ap_poll_kthread = NULL; 487 mutex_unlock(&ap_poll_thread_mutex); 488 } 489 490 #define is_card_dev(x) ((x)->parent == ap_root_device) 491 #define is_queue_dev(x) ((x)->parent != ap_root_device) 492 493 /** 494 * ap_bus_match() 495 * @dev: Pointer to device 496 * @drv: Pointer to device_driver 497 * 498 * AP bus driver registration/unregistration. 499 */ 500 static int ap_bus_match(struct device *dev, struct device_driver *drv) 501 { 502 struct ap_driver *ap_drv = to_ap_drv(drv); 503 struct ap_device_id *id; 504 505 /* 506 * Compare device type of the device with the list of 507 * supported types of the device_driver. 508 */ 509 for (id = ap_drv->ids; id->match_flags; id++) { 510 if (is_card_dev(dev) && 511 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && 512 id->dev_type == to_ap_dev(dev)->device_type) 513 return 1; 514 if (is_queue_dev(dev) && 515 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && 516 id->dev_type == to_ap_dev(dev)->device_type) 517 return 1; 518 } 519 return 0; 520 } 521 522 /** 523 * ap_uevent(): Uevent function for AP devices. 524 * @dev: Pointer to device 525 * @env: Pointer to kobj_uevent_env 526 * 527 * It sets up a single environment variable DEV_TYPE which contains the 528 * hardware device type. 529 */ 530 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env) 531 { 532 struct ap_device *ap_dev = to_ap_dev(dev); 533 int retval = 0; 534 535 if (!ap_dev) 536 return -ENODEV; 537 538 /* Set up DEV_TYPE environment variable. */ 539 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); 540 if (retval) 541 return retval; 542 543 /* Add MODALIAS= */ 544 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); 545 546 return retval; 547 } 548 549 static int ap_dev_suspend(struct device *dev) 550 { 551 struct ap_device *ap_dev = to_ap_dev(dev); 552 553 if (ap_dev->drv && ap_dev->drv->suspend) 554 ap_dev->drv->suspend(ap_dev); 555 return 0; 556 } 557 558 static int ap_dev_resume(struct device *dev) 559 { 560 struct ap_device *ap_dev = to_ap_dev(dev); 561 562 if (ap_dev->drv && ap_dev->drv->resume) 563 ap_dev->drv->resume(ap_dev); 564 return 0; 565 } 566 567 static void ap_bus_suspend(void) 568 { 569 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n"); 570 571 ap_suspend_flag = 1; 572 /* 573 * Disable scanning for devices, thus we do not want to scan 574 * for them after removing. 575 */ 576 flush_work(&ap_scan_work); 577 tasklet_disable(&ap_tasklet); 578 } 579 580 static int __ap_card_devices_unregister(struct device *dev, void *dummy) 581 { 582 if (is_card_dev(dev)) 583 device_unregister(dev); 584 return 0; 585 } 586 587 static int __ap_queue_devices_unregister(struct device *dev, void *dummy) 588 { 589 if (is_queue_dev(dev)) 590 device_unregister(dev); 591 return 0; 592 } 593 594 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) 595 { 596 if (is_queue_dev(dev) && 597 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data) 598 device_unregister(dev); 599 return 0; 600 } 601 602 static void ap_bus_resume(void) 603 { 604 int rc; 605 606 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n"); 607 608 /* remove all queue devices */ 609 bus_for_each_dev(&ap_bus_type, NULL, NULL, 610 __ap_queue_devices_unregister); 611 /* remove all card devices */ 612 bus_for_each_dev(&ap_bus_type, NULL, NULL, 613 __ap_card_devices_unregister); 614 615 /* Reset thin interrupt setting */ 616 if (ap_interrupts_available() && !ap_using_interrupts()) { 617 rc = register_adapter_interrupt(&ap_airq); 618 ap_airq_flag = (rc == 0); 619 } 620 if (!ap_interrupts_available() && ap_using_interrupts()) { 621 unregister_adapter_interrupt(&ap_airq); 622 ap_airq_flag = 0; 623 } 624 /* Reset domain */ 625 if (!user_set_domain) 626 ap_domain_index = -1; 627 /* Get things going again */ 628 ap_suspend_flag = 0; 629 if (ap_airq_flag) 630 xchg(ap_airq.lsi_ptr, 0); 631 tasklet_enable(&ap_tasklet); 632 queue_work(system_long_wq, &ap_scan_work); 633 } 634 635 static int ap_power_event(struct notifier_block *this, unsigned long event, 636 void *ptr) 637 { 638 switch (event) { 639 case PM_HIBERNATION_PREPARE: 640 case PM_SUSPEND_PREPARE: 641 ap_bus_suspend(); 642 break; 643 case PM_POST_HIBERNATION: 644 case PM_POST_SUSPEND: 645 ap_bus_resume(); 646 break; 647 default: 648 break; 649 } 650 return NOTIFY_DONE; 651 } 652 static struct notifier_block ap_power_notifier = { 653 .notifier_call = ap_power_event, 654 }; 655 656 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume); 657 658 static struct bus_type ap_bus_type = { 659 .name = "ap", 660 .match = &ap_bus_match, 661 .uevent = &ap_uevent, 662 .pm = &ap_bus_pm_ops, 663 }; 664 665 static int ap_device_probe(struct device *dev) 666 { 667 struct ap_device *ap_dev = to_ap_dev(dev); 668 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 669 int rc; 670 671 ap_dev->drv = ap_drv; 672 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 673 if (rc) 674 ap_dev->drv = NULL; 675 return rc; 676 } 677 678 static int ap_device_remove(struct device *dev) 679 { 680 struct ap_device *ap_dev = to_ap_dev(dev); 681 struct ap_driver *ap_drv = ap_dev->drv; 682 683 spin_lock_bh(&ap_list_lock); 684 if (is_card_dev(dev)) 685 list_del_init(&to_ap_card(dev)->list); 686 else 687 list_del_init(&to_ap_queue(dev)->list); 688 spin_unlock_bh(&ap_list_lock); 689 if (ap_drv->remove) 690 ap_drv->remove(ap_dev); 691 return 0; 692 } 693 694 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 695 char *name) 696 { 697 struct device_driver *drv = &ap_drv->driver; 698 699 if (!initialised) 700 return -ENODEV; 701 702 drv->bus = &ap_bus_type; 703 drv->probe = ap_device_probe; 704 drv->remove = ap_device_remove; 705 drv->owner = owner; 706 drv->name = name; 707 return driver_register(drv); 708 } 709 EXPORT_SYMBOL(ap_driver_register); 710 711 void ap_driver_unregister(struct ap_driver *ap_drv) 712 { 713 driver_unregister(&ap_drv->driver); 714 } 715 EXPORT_SYMBOL(ap_driver_unregister); 716 717 void ap_bus_force_rescan(void) 718 { 719 if (ap_suspend_flag) 720 return; 721 /* processing a asynchronous bus rescan */ 722 del_timer(&ap_config_timer); 723 queue_work(system_long_wq, &ap_scan_work); 724 flush_work(&ap_scan_work); 725 } 726 EXPORT_SYMBOL(ap_bus_force_rescan); 727 728 /* 729 * AP bus attributes. 730 */ 731 static ssize_t ap_domain_show(struct bus_type *bus, char *buf) 732 { 733 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); 734 } 735 736 static ssize_t ap_domain_store(struct bus_type *bus, 737 const char *buf, size_t count) 738 { 739 int domain; 740 741 if (sscanf(buf, "%i\n", &domain) != 1 || 742 domain < 0 || domain > ap_max_domain_id) 743 return -EINVAL; 744 spin_lock_bh(&ap_domain_lock); 745 ap_domain_index = domain; 746 spin_unlock_bh(&ap_domain_lock); 747 748 AP_DBF(DBF_DEBUG, "store new default domain=%d\n", domain); 749 750 return count; 751 } 752 753 static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store); 754 755 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) 756 { 757 if (!ap_configuration) /* QCI not supported */ 758 return snprintf(buf, PAGE_SIZE, "not supported\n"); 759 760 return snprintf(buf, PAGE_SIZE, 761 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 762 ap_configuration->adm[0], ap_configuration->adm[1], 763 ap_configuration->adm[2], ap_configuration->adm[3], 764 ap_configuration->adm[4], ap_configuration->adm[5], 765 ap_configuration->adm[6], ap_configuration->adm[7]); 766 } 767 768 static BUS_ATTR(ap_control_domain_mask, 0444, 769 ap_control_domain_mask_show, NULL); 770 771 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf) 772 { 773 if (!ap_configuration) /* QCI not supported */ 774 return snprintf(buf, PAGE_SIZE, "not supported\n"); 775 776 return snprintf(buf, PAGE_SIZE, 777 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 778 ap_configuration->aqm[0], ap_configuration->aqm[1], 779 ap_configuration->aqm[2], ap_configuration->aqm[3], 780 ap_configuration->aqm[4], ap_configuration->aqm[5], 781 ap_configuration->aqm[6], ap_configuration->aqm[7]); 782 } 783 784 static BUS_ATTR(ap_usage_domain_mask, 0444, 785 ap_usage_domain_mask_show, NULL); 786 787 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf) 788 { 789 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); 790 } 791 792 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) 793 { 794 return snprintf(buf, PAGE_SIZE, "%d\n", 795 ap_using_interrupts() ? 1 : 0); 796 } 797 798 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL); 799 800 static ssize_t ap_config_time_store(struct bus_type *bus, 801 const char *buf, size_t count) 802 { 803 int time; 804 805 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 806 return -EINVAL; 807 ap_config_time = time; 808 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 809 return count; 810 } 811 812 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); 813 814 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf) 815 { 816 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); 817 } 818 819 static ssize_t ap_poll_thread_store(struct bus_type *bus, 820 const char *buf, size_t count) 821 { 822 int flag, rc; 823 824 if (sscanf(buf, "%d\n", &flag) != 1) 825 return -EINVAL; 826 if (flag) { 827 rc = ap_poll_thread_start(); 828 if (rc) 829 count = rc; 830 } else 831 ap_poll_thread_stop(); 832 return count; 833 } 834 835 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store); 836 837 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) 838 { 839 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); 840 } 841 842 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, 843 size_t count) 844 { 845 unsigned long long time; 846 ktime_t hr_time; 847 848 /* 120 seconds = maximum poll interval */ 849 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || 850 time > 120000000000ULL) 851 return -EINVAL; 852 poll_timeout = time; 853 hr_time = poll_timeout; 854 855 spin_lock_bh(&ap_poll_timer_lock); 856 hrtimer_cancel(&ap_poll_timer); 857 hrtimer_set_expires(&ap_poll_timer, hr_time); 858 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); 859 spin_unlock_bh(&ap_poll_timer_lock); 860 861 return count; 862 } 863 864 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store); 865 866 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) 867 { 868 int max_domain_id; 869 870 if (ap_configuration) 871 max_domain_id = ap_max_domain_id ? : -1; 872 else 873 max_domain_id = 15; 874 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id); 875 } 876 877 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL); 878 879 static struct bus_attribute *const ap_bus_attrs[] = { 880 &bus_attr_ap_domain, 881 &bus_attr_ap_control_domain_mask, 882 &bus_attr_ap_usage_domain_mask, 883 &bus_attr_config_time, 884 &bus_attr_poll_thread, 885 &bus_attr_ap_interrupts, 886 &bus_attr_poll_timeout, 887 &bus_attr_ap_max_domain_id, 888 NULL, 889 }; 890 891 /** 892 * ap_select_domain(): Select an AP domain. 893 * 894 * Pick one of the 16 AP domains. 895 */ 896 static int ap_select_domain(void) 897 { 898 int count, max_count, best_domain; 899 struct ap_queue_status status; 900 int i, j; 901 902 /* 903 * We want to use a single domain. Either the one specified with 904 * the "domain=" parameter or the domain with the maximum number 905 * of devices. 906 */ 907 spin_lock_bh(&ap_domain_lock); 908 if (ap_domain_index >= 0) { 909 /* Domain has already been selected. */ 910 spin_unlock_bh(&ap_domain_lock); 911 return 0; 912 } 913 best_domain = -1; 914 max_count = 0; 915 for (i = 0; i < AP_DOMAINS; i++) { 916 if (!ap_test_config_domain(i)) 917 continue; 918 count = 0; 919 for (j = 0; j < AP_DEVICES; j++) { 920 if (!ap_test_config_card_id(j)) 921 continue; 922 status = ap_test_queue(AP_MKQID(j, i), NULL); 923 if (status.response_code != AP_RESPONSE_NORMAL) 924 continue; 925 count++; 926 } 927 if (count > max_count) { 928 max_count = count; 929 best_domain = i; 930 } 931 } 932 if (best_domain >= 0){ 933 ap_domain_index = best_domain; 934 spin_unlock_bh(&ap_domain_lock); 935 return 0; 936 } 937 spin_unlock_bh(&ap_domain_lock); 938 return -ENODEV; 939 } 940 941 /* 942 * helper function to be used with bus_find_dev 943 * matches for the card device with the given id 944 */ 945 static int __match_card_device_with_id(struct device *dev, void *data) 946 { 947 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data; 948 } 949 950 /* helper function to be used with bus_find_dev 951 * matches for the queue device with a given qid 952 */ 953 static int __match_queue_device_with_qid(struct device *dev, void *data) 954 { 955 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data; 956 } 957 958 /** 959 * ap_scan_bus(): Scan the AP bus for new devices 960 * Runs periodically, workqueue timer (ap_config_time) 961 */ 962 static void ap_scan_bus(struct work_struct *unused) 963 { 964 struct ap_queue *aq; 965 struct ap_card *ac; 966 struct device *dev; 967 ap_qid_t qid; 968 int depth = 0, type = 0; 969 unsigned int functions = 0; 970 int rc, id, dom, borked, domains; 971 972 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n"); 973 974 ap_query_configuration(); 975 if (ap_select_domain() != 0) 976 goto out; 977 978 for (id = 0; id < AP_DEVICES; id++) { 979 /* check if device is registered */ 980 dev = bus_find_device(&ap_bus_type, NULL, 981 (void *)(long) id, 982 __match_card_device_with_id); 983 ac = dev ? to_ap_card(dev) : NULL; 984 if (!ap_test_config_card_id(id)) { 985 if (dev) { 986 /* Card device has been removed from 987 * configuration, remove the belonging 988 * queue devices. 989 */ 990 bus_for_each_dev(&ap_bus_type, NULL, 991 (void *)(long) id, 992 __ap_queue_devices_with_id_unregister); 993 /* now remove the card device */ 994 device_unregister(dev); 995 put_device(dev); 996 } 997 continue; 998 } 999 /* According to the configuration there should be a card 1000 * device, so check if there is at least one valid queue 1001 * and maybe create queue devices and the card device. 1002 */ 1003 domains = 0; 1004 for (dom = 0; dom < AP_DOMAINS; dom++) { 1005 qid = AP_MKQID(id, dom); 1006 dev = bus_find_device(&ap_bus_type, NULL, 1007 (void *)(long) qid, 1008 __match_queue_device_with_qid); 1009 aq = dev ? to_ap_queue(dev) : NULL; 1010 if (!ap_test_config_domain(dom)) { 1011 if (dev) { 1012 /* Queue device exists but has been 1013 * removed from configuration. 1014 */ 1015 device_unregister(dev); 1016 put_device(dev); 1017 } 1018 continue; 1019 } 1020 rc = ap_query_queue(qid, &depth, &type, &functions); 1021 if (dev) { 1022 spin_lock_bh(&aq->lock); 1023 if (rc == -ENODEV || 1024 /* adapter reconfiguration */ 1025 (ac && ac->functions != functions)) 1026 aq->state = AP_STATE_BORKED; 1027 borked = aq->state == AP_STATE_BORKED; 1028 spin_unlock_bh(&aq->lock); 1029 if (borked) /* Remove broken device */ 1030 device_unregister(dev); 1031 put_device(dev); 1032 if (!borked) { 1033 domains++; 1034 continue; 1035 } 1036 } 1037 if (rc) 1038 continue; 1039 /* new queue device needed */ 1040 if (!ac) { 1041 /* but first create the card device */ 1042 ac = ap_card_create(id, depth, 1043 type, functions); 1044 if (!ac) 1045 continue; 1046 ac->ap_dev.device.bus = &ap_bus_type; 1047 ac->ap_dev.device.parent = ap_root_device; 1048 dev_set_name(&ac->ap_dev.device, 1049 "card%02x", id); 1050 /* Register card with AP bus */ 1051 rc = device_register(&ac->ap_dev.device); 1052 if (rc) { 1053 put_device(&ac->ap_dev.device); 1054 ac = NULL; 1055 break; 1056 } 1057 /* get it and thus adjust reference counter */ 1058 get_device(&ac->ap_dev.device); 1059 /* Add card device to card list */ 1060 spin_lock_bh(&ap_list_lock); 1061 list_add(&ac->list, &ap_card_list); 1062 spin_unlock_bh(&ap_list_lock); 1063 } 1064 /* now create the new queue device */ 1065 aq = ap_queue_create(qid, type); 1066 if (!aq) 1067 continue; 1068 aq->card = ac; 1069 aq->ap_dev.device.bus = &ap_bus_type; 1070 aq->ap_dev.device.parent = &ac->ap_dev.device; 1071 dev_set_name(&aq->ap_dev.device, 1072 "%02x.%04x", id, dom); 1073 /* Add queue device to card queue list */ 1074 spin_lock_bh(&ap_list_lock); 1075 list_add(&aq->list, &ac->queues); 1076 spin_unlock_bh(&ap_list_lock); 1077 /* Start with a device reset */ 1078 spin_lock_bh(&aq->lock); 1079 ap_wait(ap_sm_event(aq, AP_EVENT_POLL)); 1080 spin_unlock_bh(&aq->lock); 1081 /* Register device */ 1082 rc = device_register(&aq->ap_dev.device); 1083 if (rc) { 1084 spin_lock_bh(&ap_list_lock); 1085 list_del_init(&aq->list); 1086 spin_unlock_bh(&ap_list_lock); 1087 put_device(&aq->ap_dev.device); 1088 continue; 1089 } 1090 domains++; 1091 } /* end domain loop */ 1092 if (ac) { 1093 /* remove card dev if there are no queue devices */ 1094 if (!domains) 1095 device_unregister(&ac->ap_dev.device); 1096 put_device(&ac->ap_dev.device); 1097 } 1098 } /* end device loop */ 1099 out: 1100 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 1101 } 1102 1103 static void ap_config_timeout(unsigned long ptr) 1104 { 1105 if (ap_suspend_flag) 1106 return; 1107 queue_work(system_long_wq, &ap_scan_work); 1108 } 1109 1110 static void ap_reset_all(void) 1111 { 1112 int i, j; 1113 1114 for (i = 0; i < AP_DOMAINS; i++) { 1115 if (!ap_test_config_domain(i)) 1116 continue; 1117 for (j = 0; j < AP_DEVICES; j++) { 1118 if (!ap_test_config_card_id(j)) 1119 continue; 1120 ap_rapq(AP_MKQID(j, i)); 1121 } 1122 } 1123 } 1124 1125 static struct reset_call ap_reset_call = { 1126 .fn = ap_reset_all, 1127 }; 1128 1129 int __init ap_debug_init(void) 1130 { 1131 ap_dbf_info = debug_register("ap", 1, 1, 1132 DBF_MAX_SPRINTF_ARGS * sizeof(long)); 1133 debug_register_view(ap_dbf_info, &debug_sprintf_view); 1134 debug_set_level(ap_dbf_info, DBF_ERR); 1135 1136 return 0; 1137 } 1138 1139 void ap_debug_exit(void) 1140 { 1141 debug_unregister(ap_dbf_info); 1142 } 1143 1144 /** 1145 * ap_module_init(): The module initialization code. 1146 * 1147 * Initializes the module. 1148 */ 1149 int __init ap_module_init(void) 1150 { 1151 int max_domain_id; 1152 int rc, i; 1153 1154 rc = ap_debug_init(); 1155 if (rc) 1156 return rc; 1157 1158 if (ap_instructions_available() != 0) { 1159 pr_warn("The hardware system does not support AP instructions\n"); 1160 return -ENODEV; 1161 } 1162 1163 /* Get AP configuration data if available */ 1164 ap_init_configuration(); 1165 1166 if (ap_configuration) 1167 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1); 1168 else 1169 max_domain_id = 15; 1170 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) { 1171 pr_warn("%d is not a valid cryptographic domain\n", 1172 ap_domain_index); 1173 rc = -EINVAL; 1174 goto out_free; 1175 } 1176 /* In resume callback we need to know if the user had set the domain. 1177 * If so, we can not just reset it. 1178 */ 1179 if (ap_domain_index >= 0) 1180 user_set_domain = 1; 1181 1182 if (ap_interrupts_available()) { 1183 rc = register_adapter_interrupt(&ap_airq); 1184 ap_airq_flag = (rc == 0); 1185 } 1186 1187 register_reset_call(&ap_reset_call); 1188 1189 /* Create /sys/bus/ap. */ 1190 rc = bus_register(&ap_bus_type); 1191 if (rc) 1192 goto out; 1193 for (i = 0; ap_bus_attrs[i]; i++) { 1194 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); 1195 if (rc) 1196 goto out_bus; 1197 } 1198 1199 /* Create /sys/devices/ap. */ 1200 ap_root_device = root_device_register("ap"); 1201 rc = PTR_RET(ap_root_device); 1202 if (rc) 1203 goto out_bus; 1204 1205 /* Setup the AP bus rescan timer. */ 1206 setup_timer(&ap_config_timer, ap_config_timeout, 0); 1207 1208 /* 1209 * Setup the high resultion poll timer. 1210 * If we are running under z/VM adjust polling to z/VM polling rate. 1211 */ 1212 if (MACHINE_IS_VM) 1213 poll_timeout = 1500000; 1214 spin_lock_init(&ap_poll_timer_lock); 1215 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 1216 ap_poll_timer.function = ap_poll_timeout; 1217 1218 /* Start the low priority AP bus poll thread. */ 1219 if (ap_thread_flag) { 1220 rc = ap_poll_thread_start(); 1221 if (rc) 1222 goto out_work; 1223 } 1224 1225 rc = register_pm_notifier(&ap_power_notifier); 1226 if (rc) 1227 goto out_pm; 1228 1229 queue_work(system_long_wq, &ap_scan_work); 1230 initialised = true; 1231 1232 return 0; 1233 1234 out_pm: 1235 ap_poll_thread_stop(); 1236 out_work: 1237 hrtimer_cancel(&ap_poll_timer); 1238 root_device_unregister(ap_root_device); 1239 out_bus: 1240 while (i--) 1241 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); 1242 bus_unregister(&ap_bus_type); 1243 out: 1244 unregister_reset_call(&ap_reset_call); 1245 if (ap_using_interrupts()) 1246 unregister_adapter_interrupt(&ap_airq); 1247 out_free: 1248 kfree(ap_configuration); 1249 return rc; 1250 } 1251 device_initcall(ap_module_init); 1252