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_apft_available(): Test if AP facilities test (APFT) 170 * facility is available. 171 * 172 * Returns 1 if APFT is is available. 173 */ 174 static int ap_apft_available(void) 175 { 176 return test_facility(15); 177 } 178 179 /* 180 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available. 181 * 182 * Returns 1 if the QACT subfunction is available. 183 */ 184 static inline int ap_qact_available(void) 185 { 186 if (ap_configuration) 187 return ap_configuration->qact; 188 return 0; 189 } 190 191 /** 192 * ap_test_queue(): Test adjunct processor queue. 193 * @qid: The AP queue number 194 * @tbit: Test facilities bit 195 * @info: Pointer to queue descriptor 196 * 197 * Returns AP queue status structure. 198 */ 199 struct ap_queue_status ap_test_queue(ap_qid_t qid, 200 int tbit, 201 unsigned long *info) 202 { 203 if (tbit) 204 qid |= 1UL << 23; /* set T bit*/ 205 return ap_tapq(qid, info); 206 } 207 EXPORT_SYMBOL(ap_test_queue); 208 209 /* 210 * ap_query_configuration(): Fetch cryptographic config info 211 * 212 * Returns the ap configuration info fetched via PQAP(QCI). 213 * On success 0 is returned, on failure a negative errno 214 * is returned, e.g. if the PQAP(QCI) instruction is not 215 * available, the return value will be -EOPNOTSUPP. 216 */ 217 int ap_query_configuration(struct ap_config_info *info) 218 { 219 if (!ap_configuration_available()) 220 return -EOPNOTSUPP; 221 if (!info) 222 return -EINVAL; 223 return ap_qci(info); 224 } 225 EXPORT_SYMBOL(ap_query_configuration); 226 227 /** 228 * ap_init_configuration(): Allocate and query configuration array. 229 */ 230 static void ap_init_configuration(void) 231 { 232 if (!ap_configuration_available()) 233 return; 234 235 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL); 236 if (!ap_configuration) 237 return; 238 if (ap_query_configuration(ap_configuration) != 0) { 239 kfree(ap_configuration); 240 ap_configuration = NULL; 241 return; 242 } 243 } 244 245 /* 246 * ap_test_config(): helper function to extract the nrth bit 247 * within the unsigned int array field. 248 */ 249 static inline int ap_test_config(unsigned int *field, unsigned int nr) 250 { 251 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); 252 } 253 254 /* 255 * ap_test_config_card_id(): Test, whether an AP card ID is configured. 256 * @id AP card ID 257 * 258 * Returns 0 if the card is not configured 259 * 1 if the card is configured or 260 * if the configuration information is not available 261 */ 262 static inline int ap_test_config_card_id(unsigned int id) 263 { 264 if (!ap_configuration) /* QCI not supported */ 265 return 1; 266 return ap_test_config(ap_configuration->apm, id); 267 } 268 269 /* 270 * ap_test_config_domain(): Test, whether an AP usage domain is configured. 271 * @domain AP usage domain ID 272 * 273 * Returns 0 if the usage domain is not configured 274 * 1 if the usage domain is configured or 275 * if the configuration information is not available 276 */ 277 static inline int ap_test_config_domain(unsigned int domain) 278 { 279 if (!ap_configuration) /* QCI not supported */ 280 return domain < 16; 281 return ap_test_config(ap_configuration->aqm, domain); 282 } 283 284 /** 285 * ap_query_queue(): Check if an AP queue is available. 286 * @qid: The AP queue number 287 * @queue_depth: Pointer to queue depth value 288 * @device_type: Pointer to device type value 289 * @facilities: Pointer to facility indicator 290 */ 291 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type, 292 unsigned int *facilities) 293 { 294 struct ap_queue_status status; 295 unsigned long info; 296 int nd; 297 298 if (!ap_test_config_card_id(AP_QID_CARD(qid))) 299 return -ENODEV; 300 301 status = ap_test_queue(qid, ap_apft_available(), &info); 302 switch (status.response_code) { 303 case AP_RESPONSE_NORMAL: 304 *queue_depth = (int)(info & 0xff); 305 *device_type = (int)((info >> 24) & 0xff); 306 *facilities = (unsigned int)(info >> 32); 307 /* Update maximum domain id */ 308 nd = (info >> 16) & 0xff; 309 /* if N bit is available, z13 and newer */ 310 if ((info & (1UL << 57)) && nd > 0) 311 ap_max_domain_id = nd; 312 else /* older machine types */ 313 ap_max_domain_id = 15; 314 switch (*device_type) { 315 /* For CEX2 and CEX3 the available functions 316 * are not refrected by the facilities bits. 317 * Instead it is coded into the type. So here 318 * modify the function bits based on the type. 319 */ 320 case AP_DEVICE_TYPE_CEX2A: 321 case AP_DEVICE_TYPE_CEX3A: 322 *facilities |= 0x08000000; 323 break; 324 case AP_DEVICE_TYPE_CEX2C: 325 case AP_DEVICE_TYPE_CEX3C: 326 *facilities |= 0x10000000; 327 break; 328 default: 329 break; 330 } 331 return 0; 332 case AP_RESPONSE_Q_NOT_AVAIL: 333 case AP_RESPONSE_DECONFIGURED: 334 case AP_RESPONSE_CHECKSTOPPED: 335 case AP_RESPONSE_INVALID_ADDRESS: 336 return -ENODEV; 337 case AP_RESPONSE_RESET_IN_PROGRESS: 338 case AP_RESPONSE_OTHERWISE_CHANGED: 339 case AP_RESPONSE_BUSY: 340 return -EBUSY; 341 default: 342 BUG(); 343 } 344 } 345 346 void ap_wait(enum ap_wait wait) 347 { 348 ktime_t hr_time; 349 350 switch (wait) { 351 case AP_WAIT_AGAIN: 352 case AP_WAIT_INTERRUPT: 353 if (ap_using_interrupts()) 354 break; 355 if (ap_poll_kthread) { 356 wake_up(&ap_poll_wait); 357 break; 358 } 359 /* Fall through */ 360 case AP_WAIT_TIMEOUT: 361 spin_lock_bh(&ap_poll_timer_lock); 362 if (!hrtimer_is_queued(&ap_poll_timer)) { 363 hr_time = poll_timeout; 364 hrtimer_forward_now(&ap_poll_timer, hr_time); 365 hrtimer_restart(&ap_poll_timer); 366 } 367 spin_unlock_bh(&ap_poll_timer_lock); 368 break; 369 case AP_WAIT_NONE: 370 default: 371 break; 372 } 373 } 374 375 /** 376 * ap_request_timeout(): Handling of request timeouts 377 * @t: timer making this callback 378 * 379 * Handles request timeouts. 380 */ 381 void ap_request_timeout(struct timer_list *t) 382 { 383 struct ap_queue *aq = from_timer(aq, t, timeout); 384 385 if (ap_suspend_flag) 386 return; 387 spin_lock_bh(&aq->lock); 388 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT)); 389 spin_unlock_bh(&aq->lock); 390 } 391 392 /** 393 * ap_poll_timeout(): AP receive polling for finished AP requests. 394 * @unused: Unused pointer. 395 * 396 * Schedules the AP tasklet using a high resolution timer. 397 */ 398 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) 399 { 400 if (!ap_suspend_flag) 401 tasklet_schedule(&ap_tasklet); 402 return HRTIMER_NORESTART; 403 } 404 405 /** 406 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt 407 * @airq: pointer to adapter interrupt descriptor 408 */ 409 static void ap_interrupt_handler(struct airq_struct *airq) 410 { 411 inc_irq_stat(IRQIO_APB); 412 if (!ap_suspend_flag) 413 tasklet_schedule(&ap_tasklet); 414 } 415 416 /** 417 * ap_tasklet_fn(): Tasklet to poll all AP devices. 418 * @dummy: Unused variable 419 * 420 * Poll all AP devices on the bus. 421 */ 422 static void ap_tasklet_fn(unsigned long dummy) 423 { 424 struct ap_card *ac; 425 struct ap_queue *aq; 426 enum ap_wait wait = AP_WAIT_NONE; 427 428 /* Reset the indicator if interrupts are used. Thus new interrupts can 429 * be received. Doing it in the beginning of the tasklet is therefor 430 * important that no requests on any AP get lost. 431 */ 432 if (ap_using_interrupts()) 433 xchg(ap_airq.lsi_ptr, 0); 434 435 spin_lock_bh(&ap_list_lock); 436 for_each_ap_card(ac) { 437 for_each_ap_queue(aq, ac) { 438 spin_lock_bh(&aq->lock); 439 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL)); 440 spin_unlock_bh(&aq->lock); 441 } 442 } 443 spin_unlock_bh(&ap_list_lock); 444 445 ap_wait(wait); 446 } 447 448 static int ap_pending_requests(void) 449 { 450 struct ap_card *ac; 451 struct ap_queue *aq; 452 453 spin_lock_bh(&ap_list_lock); 454 for_each_ap_card(ac) { 455 for_each_ap_queue(aq, ac) { 456 if (aq->queue_count == 0) 457 continue; 458 spin_unlock_bh(&ap_list_lock); 459 return 1; 460 } 461 } 462 spin_unlock_bh(&ap_list_lock); 463 return 0; 464 } 465 466 /** 467 * ap_poll_thread(): Thread that polls for finished requests. 468 * @data: Unused pointer 469 * 470 * AP bus poll thread. The purpose of this thread is to poll for 471 * finished requests in a loop if there is a "free" cpu - that is 472 * a cpu that doesn't have anything better to do. The polling stops 473 * as soon as there is another task or if all messages have been 474 * delivered. 475 */ 476 static int ap_poll_thread(void *data) 477 { 478 DECLARE_WAITQUEUE(wait, current); 479 480 set_user_nice(current, MAX_NICE); 481 set_freezable(); 482 while (!kthread_should_stop()) { 483 add_wait_queue(&ap_poll_wait, &wait); 484 set_current_state(TASK_INTERRUPTIBLE); 485 if (ap_suspend_flag || !ap_pending_requests()) { 486 schedule(); 487 try_to_freeze(); 488 } 489 set_current_state(TASK_RUNNING); 490 remove_wait_queue(&ap_poll_wait, &wait); 491 if (need_resched()) { 492 schedule(); 493 try_to_freeze(); 494 continue; 495 } 496 ap_tasklet_fn(0); 497 } 498 499 return 0; 500 } 501 502 static int ap_poll_thread_start(void) 503 { 504 int rc; 505 506 if (ap_using_interrupts() || ap_poll_kthread) 507 return 0; 508 mutex_lock(&ap_poll_thread_mutex); 509 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); 510 rc = PTR_RET(ap_poll_kthread); 511 if (rc) 512 ap_poll_kthread = NULL; 513 mutex_unlock(&ap_poll_thread_mutex); 514 return rc; 515 } 516 517 static void ap_poll_thread_stop(void) 518 { 519 if (!ap_poll_kthread) 520 return; 521 mutex_lock(&ap_poll_thread_mutex); 522 kthread_stop(ap_poll_kthread); 523 ap_poll_kthread = NULL; 524 mutex_unlock(&ap_poll_thread_mutex); 525 } 526 527 #define is_card_dev(x) ((x)->parent == ap_root_device) 528 #define is_queue_dev(x) ((x)->parent != ap_root_device) 529 530 /** 531 * ap_bus_match() 532 * @dev: Pointer to device 533 * @drv: Pointer to device_driver 534 * 535 * AP bus driver registration/unregistration. 536 */ 537 static int ap_bus_match(struct device *dev, struct device_driver *drv) 538 { 539 struct ap_driver *ap_drv = to_ap_drv(drv); 540 struct ap_device_id *id; 541 542 /* 543 * Compare device type of the device with the list of 544 * supported types of the device_driver. 545 */ 546 for (id = ap_drv->ids; id->match_flags; id++) { 547 if (is_card_dev(dev) && 548 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && 549 id->dev_type == to_ap_dev(dev)->device_type) 550 return 1; 551 if (is_queue_dev(dev) && 552 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && 553 id->dev_type == to_ap_dev(dev)->device_type) 554 return 1; 555 } 556 return 0; 557 } 558 559 /** 560 * ap_uevent(): Uevent function for AP devices. 561 * @dev: Pointer to device 562 * @env: Pointer to kobj_uevent_env 563 * 564 * It sets up a single environment variable DEV_TYPE which contains the 565 * hardware device type. 566 */ 567 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env) 568 { 569 struct ap_device *ap_dev = to_ap_dev(dev); 570 int retval = 0; 571 572 if (!ap_dev) 573 return -ENODEV; 574 575 /* Set up DEV_TYPE environment variable. */ 576 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); 577 if (retval) 578 return retval; 579 580 /* Add MODALIAS= */ 581 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); 582 583 return retval; 584 } 585 586 static int ap_dev_suspend(struct device *dev) 587 { 588 struct ap_device *ap_dev = to_ap_dev(dev); 589 590 if (ap_dev->drv && ap_dev->drv->suspend) 591 ap_dev->drv->suspend(ap_dev); 592 return 0; 593 } 594 595 static int ap_dev_resume(struct device *dev) 596 { 597 struct ap_device *ap_dev = to_ap_dev(dev); 598 599 if (ap_dev->drv && ap_dev->drv->resume) 600 ap_dev->drv->resume(ap_dev); 601 return 0; 602 } 603 604 static void ap_bus_suspend(void) 605 { 606 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n"); 607 608 ap_suspend_flag = 1; 609 /* 610 * Disable scanning for devices, thus we do not want to scan 611 * for them after removing. 612 */ 613 flush_work(&ap_scan_work); 614 tasklet_disable(&ap_tasklet); 615 } 616 617 static int __ap_card_devices_unregister(struct device *dev, void *dummy) 618 { 619 if (is_card_dev(dev)) 620 device_unregister(dev); 621 return 0; 622 } 623 624 static int __ap_queue_devices_unregister(struct device *dev, void *dummy) 625 { 626 if (is_queue_dev(dev)) 627 device_unregister(dev); 628 return 0; 629 } 630 631 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) 632 { 633 if (is_queue_dev(dev) && 634 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data) 635 device_unregister(dev); 636 return 0; 637 } 638 639 static void ap_bus_resume(void) 640 { 641 int rc; 642 643 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n"); 644 645 /* remove all queue devices */ 646 bus_for_each_dev(&ap_bus_type, NULL, NULL, 647 __ap_queue_devices_unregister); 648 /* remove all card devices */ 649 bus_for_each_dev(&ap_bus_type, NULL, NULL, 650 __ap_card_devices_unregister); 651 652 /* Reset thin interrupt setting */ 653 if (ap_interrupts_available() && !ap_using_interrupts()) { 654 rc = register_adapter_interrupt(&ap_airq); 655 ap_airq_flag = (rc == 0); 656 } 657 if (!ap_interrupts_available() && ap_using_interrupts()) { 658 unregister_adapter_interrupt(&ap_airq); 659 ap_airq_flag = 0; 660 } 661 /* Reset domain */ 662 if (!user_set_domain) 663 ap_domain_index = -1; 664 /* Get things going again */ 665 ap_suspend_flag = 0; 666 if (ap_airq_flag) 667 xchg(ap_airq.lsi_ptr, 0); 668 tasklet_enable(&ap_tasklet); 669 queue_work(system_long_wq, &ap_scan_work); 670 } 671 672 static int ap_power_event(struct notifier_block *this, unsigned long event, 673 void *ptr) 674 { 675 switch (event) { 676 case PM_HIBERNATION_PREPARE: 677 case PM_SUSPEND_PREPARE: 678 ap_bus_suspend(); 679 break; 680 case PM_POST_HIBERNATION: 681 case PM_POST_SUSPEND: 682 ap_bus_resume(); 683 break; 684 default: 685 break; 686 } 687 return NOTIFY_DONE; 688 } 689 static struct notifier_block ap_power_notifier = { 690 .notifier_call = ap_power_event, 691 }; 692 693 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume); 694 695 static struct bus_type ap_bus_type = { 696 .name = "ap", 697 .match = &ap_bus_match, 698 .uevent = &ap_uevent, 699 .pm = &ap_bus_pm_ops, 700 }; 701 702 static int ap_device_probe(struct device *dev) 703 { 704 struct ap_device *ap_dev = to_ap_dev(dev); 705 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 706 int rc; 707 708 /* Add queue/card to list of active queues/cards */ 709 spin_lock_bh(&ap_list_lock); 710 if (is_card_dev(dev)) 711 list_add(&to_ap_card(dev)->list, &ap_card_list); 712 else 713 list_add(&to_ap_queue(dev)->list, 714 &to_ap_queue(dev)->card->queues); 715 spin_unlock_bh(&ap_list_lock); 716 717 ap_dev->drv = ap_drv; 718 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 719 720 if (rc) { 721 spin_lock_bh(&ap_list_lock); 722 if (is_card_dev(dev)) 723 list_del_init(&to_ap_card(dev)->list); 724 else 725 list_del_init(&to_ap_queue(dev)->list); 726 spin_unlock_bh(&ap_list_lock); 727 ap_dev->drv = NULL; 728 } 729 730 return rc; 731 } 732 733 static int ap_device_remove(struct device *dev) 734 { 735 struct ap_device *ap_dev = to_ap_dev(dev); 736 struct ap_driver *ap_drv = ap_dev->drv; 737 738 if (ap_drv->remove) 739 ap_drv->remove(ap_dev); 740 741 /* Remove queue/card from list of active queues/cards */ 742 spin_lock_bh(&ap_list_lock); 743 if (is_card_dev(dev)) 744 list_del_init(&to_ap_card(dev)->list); 745 else 746 list_del_init(&to_ap_queue(dev)->list); 747 spin_unlock_bh(&ap_list_lock); 748 749 return 0; 750 } 751 752 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 753 char *name) 754 { 755 struct device_driver *drv = &ap_drv->driver; 756 757 if (!initialised) 758 return -ENODEV; 759 760 drv->bus = &ap_bus_type; 761 drv->probe = ap_device_probe; 762 drv->remove = ap_device_remove; 763 drv->owner = owner; 764 drv->name = name; 765 return driver_register(drv); 766 } 767 EXPORT_SYMBOL(ap_driver_register); 768 769 void ap_driver_unregister(struct ap_driver *ap_drv) 770 { 771 driver_unregister(&ap_drv->driver); 772 } 773 EXPORT_SYMBOL(ap_driver_unregister); 774 775 void ap_bus_force_rescan(void) 776 { 777 if (ap_suspend_flag) 778 return; 779 /* processing a asynchronous bus rescan */ 780 del_timer(&ap_config_timer); 781 queue_work(system_long_wq, &ap_scan_work); 782 flush_work(&ap_scan_work); 783 } 784 EXPORT_SYMBOL(ap_bus_force_rescan); 785 786 /* 787 * AP bus attributes. 788 */ 789 static ssize_t ap_domain_show(struct bus_type *bus, char *buf) 790 { 791 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); 792 } 793 794 static ssize_t ap_domain_store(struct bus_type *bus, 795 const char *buf, size_t count) 796 { 797 int domain; 798 799 if (sscanf(buf, "%i\n", &domain) != 1 || 800 domain < 0 || domain > ap_max_domain_id) 801 return -EINVAL; 802 spin_lock_bh(&ap_domain_lock); 803 ap_domain_index = domain; 804 spin_unlock_bh(&ap_domain_lock); 805 806 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain); 807 808 return count; 809 } 810 811 static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store); 812 813 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) 814 { 815 if (!ap_configuration) /* QCI not supported */ 816 return snprintf(buf, PAGE_SIZE, "not supported\n"); 817 818 return snprintf(buf, PAGE_SIZE, 819 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 820 ap_configuration->adm[0], ap_configuration->adm[1], 821 ap_configuration->adm[2], ap_configuration->adm[3], 822 ap_configuration->adm[4], ap_configuration->adm[5], 823 ap_configuration->adm[6], ap_configuration->adm[7]); 824 } 825 826 static BUS_ATTR(ap_control_domain_mask, 0444, 827 ap_control_domain_mask_show, NULL); 828 829 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf) 830 { 831 if (!ap_configuration) /* QCI not supported */ 832 return snprintf(buf, PAGE_SIZE, "not supported\n"); 833 834 return snprintf(buf, PAGE_SIZE, 835 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 836 ap_configuration->aqm[0], ap_configuration->aqm[1], 837 ap_configuration->aqm[2], ap_configuration->aqm[3], 838 ap_configuration->aqm[4], ap_configuration->aqm[5], 839 ap_configuration->aqm[6], ap_configuration->aqm[7]); 840 } 841 842 static BUS_ATTR(ap_usage_domain_mask, 0444, 843 ap_usage_domain_mask_show, NULL); 844 845 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf) 846 { 847 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); 848 } 849 850 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) 851 { 852 return snprintf(buf, PAGE_SIZE, "%d\n", 853 ap_using_interrupts() ? 1 : 0); 854 } 855 856 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL); 857 858 static ssize_t ap_config_time_store(struct bus_type *bus, 859 const char *buf, size_t count) 860 { 861 int time; 862 863 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 864 return -EINVAL; 865 ap_config_time = time; 866 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 867 return count; 868 } 869 870 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); 871 872 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf) 873 { 874 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); 875 } 876 877 static ssize_t ap_poll_thread_store(struct bus_type *bus, 878 const char *buf, size_t count) 879 { 880 int flag, rc; 881 882 if (sscanf(buf, "%d\n", &flag) != 1) 883 return -EINVAL; 884 if (flag) { 885 rc = ap_poll_thread_start(); 886 if (rc) 887 count = rc; 888 } else 889 ap_poll_thread_stop(); 890 return count; 891 } 892 893 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store); 894 895 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) 896 { 897 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); 898 } 899 900 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, 901 size_t count) 902 { 903 unsigned long long time; 904 ktime_t hr_time; 905 906 /* 120 seconds = maximum poll interval */ 907 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || 908 time > 120000000000ULL) 909 return -EINVAL; 910 poll_timeout = time; 911 hr_time = poll_timeout; 912 913 spin_lock_bh(&ap_poll_timer_lock); 914 hrtimer_cancel(&ap_poll_timer); 915 hrtimer_set_expires(&ap_poll_timer, hr_time); 916 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); 917 spin_unlock_bh(&ap_poll_timer_lock); 918 919 return count; 920 } 921 922 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store); 923 924 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) 925 { 926 int max_domain_id; 927 928 if (ap_configuration) 929 max_domain_id = ap_max_domain_id ? : -1; 930 else 931 max_domain_id = 15; 932 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id); 933 } 934 935 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL); 936 937 static struct bus_attribute *const ap_bus_attrs[] = { 938 &bus_attr_ap_domain, 939 &bus_attr_ap_control_domain_mask, 940 &bus_attr_ap_usage_domain_mask, 941 &bus_attr_config_time, 942 &bus_attr_poll_thread, 943 &bus_attr_ap_interrupts, 944 &bus_attr_poll_timeout, 945 &bus_attr_ap_max_domain_id, 946 NULL, 947 }; 948 949 /** 950 * ap_select_domain(): Select an AP domain. 951 * 952 * Pick one of the 16 AP domains. 953 */ 954 static int ap_select_domain(void) 955 { 956 int count, max_count, best_domain; 957 struct ap_queue_status status; 958 int i, j; 959 960 /* 961 * We want to use a single domain. Either the one specified with 962 * the "domain=" parameter or the domain with the maximum number 963 * of devices. 964 */ 965 spin_lock_bh(&ap_domain_lock); 966 if (ap_domain_index >= 0) { 967 /* Domain has already been selected. */ 968 spin_unlock_bh(&ap_domain_lock); 969 return 0; 970 } 971 best_domain = -1; 972 max_count = 0; 973 for (i = 0; i < AP_DOMAINS; i++) { 974 if (!ap_test_config_domain(i)) 975 continue; 976 count = 0; 977 for (j = 0; j < AP_DEVICES; j++) { 978 if (!ap_test_config_card_id(j)) 979 continue; 980 status = ap_test_queue(AP_MKQID(j, i), 981 ap_apft_available(), 982 NULL); 983 if (status.response_code != AP_RESPONSE_NORMAL) 984 continue; 985 count++; 986 } 987 if (count > max_count) { 988 max_count = count; 989 best_domain = i; 990 } 991 } 992 if (best_domain >= 0){ 993 ap_domain_index = best_domain; 994 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index); 995 spin_unlock_bh(&ap_domain_lock); 996 return 0; 997 } 998 spin_unlock_bh(&ap_domain_lock); 999 return -ENODEV; 1000 } 1001 1002 /* 1003 * This function checks the type and returns either 0 for not 1004 * supported or the highest compatible type value (which may 1005 * include the input type value). 1006 */ 1007 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func) 1008 { 1009 int comp_type = 0; 1010 1011 /* < CEX2A is not supported */ 1012 if (rawtype < AP_DEVICE_TYPE_CEX2A) 1013 return 0; 1014 /* up to CEX6 known and fully supported */ 1015 if (rawtype <= AP_DEVICE_TYPE_CEX6) 1016 return rawtype; 1017 /* 1018 * unknown new type > CEX6, check for compatibility 1019 * to the highest known and supported type which is 1020 * currently CEX6 with the help of the QACT function. 1021 */ 1022 if (ap_qact_available()) { 1023 struct ap_queue_status status; 1024 union ap_qact_ap_info apinfo = {0}; 1025 1026 apinfo.mode = (func >> 26) & 0x07; 1027 apinfo.cat = AP_DEVICE_TYPE_CEX6; 1028 status = ap_qact(qid, 0, &apinfo); 1029 if (status.response_code == AP_RESPONSE_NORMAL 1030 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A 1031 && apinfo.cat <= AP_DEVICE_TYPE_CEX6) 1032 comp_type = apinfo.cat; 1033 } 1034 if (!comp_type) 1035 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n", 1036 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype); 1037 else if (comp_type != rawtype) 1038 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n", 1039 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type); 1040 return comp_type; 1041 } 1042 1043 /* 1044 * helper function to be used with bus_find_dev 1045 * matches for the card device with the given id 1046 */ 1047 static int __match_card_device_with_id(struct device *dev, void *data) 1048 { 1049 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data; 1050 } 1051 1052 /* helper function to be used with bus_find_dev 1053 * matches for the queue device with a given qid 1054 */ 1055 static int __match_queue_device_with_qid(struct device *dev, void *data) 1056 { 1057 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data; 1058 } 1059 1060 /** 1061 * ap_scan_bus(): Scan the AP bus for new devices 1062 * Runs periodically, workqueue timer (ap_config_time) 1063 */ 1064 static void ap_scan_bus(struct work_struct *unused) 1065 { 1066 struct ap_queue *aq; 1067 struct ap_card *ac; 1068 struct device *dev; 1069 ap_qid_t qid; 1070 int comp_type, depth = 0, type = 0; 1071 unsigned int func = 0; 1072 int rc, id, dom, borked, domains, defdomdevs = 0; 1073 1074 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n"); 1075 1076 ap_query_configuration(ap_configuration); 1077 if (ap_select_domain() != 0) 1078 goto out; 1079 1080 for (id = 0; id < AP_DEVICES; id++) { 1081 /* check if device is registered */ 1082 dev = bus_find_device(&ap_bus_type, NULL, 1083 (void *)(long) id, 1084 __match_card_device_with_id); 1085 ac = dev ? to_ap_card(dev) : NULL; 1086 if (!ap_test_config_card_id(id)) { 1087 if (dev) { 1088 /* Card device has been removed from 1089 * configuration, remove the belonging 1090 * queue devices. 1091 */ 1092 bus_for_each_dev(&ap_bus_type, NULL, 1093 (void *)(long) id, 1094 __ap_queue_devices_with_id_unregister); 1095 /* now remove the card device */ 1096 device_unregister(dev); 1097 put_device(dev); 1098 } 1099 continue; 1100 } 1101 /* According to the configuration there should be a card 1102 * device, so check if there is at least one valid queue 1103 * and maybe create queue devices and the card device. 1104 */ 1105 domains = 0; 1106 for (dom = 0; dom < AP_DOMAINS; dom++) { 1107 qid = AP_MKQID(id, dom); 1108 dev = bus_find_device(&ap_bus_type, NULL, 1109 (void *)(long) qid, 1110 __match_queue_device_with_qid); 1111 aq = dev ? to_ap_queue(dev) : NULL; 1112 if (!ap_test_config_domain(dom)) { 1113 if (dev) { 1114 /* Queue device exists but has been 1115 * removed from configuration. 1116 */ 1117 device_unregister(dev); 1118 put_device(dev); 1119 } 1120 continue; 1121 } 1122 rc = ap_query_queue(qid, &depth, &type, &func); 1123 if (dev) { 1124 spin_lock_bh(&aq->lock); 1125 if (rc == -ENODEV || 1126 /* adapter reconfiguration */ 1127 (ac && ac->functions != func)) 1128 aq->state = AP_STATE_BORKED; 1129 borked = aq->state == AP_STATE_BORKED; 1130 spin_unlock_bh(&aq->lock); 1131 if (borked) /* Remove broken device */ 1132 device_unregister(dev); 1133 put_device(dev); 1134 if (!borked) { 1135 domains++; 1136 if (dom == ap_domain_index) 1137 defdomdevs++; 1138 continue; 1139 } 1140 } 1141 if (rc) 1142 continue; 1143 /* a new queue device is needed, check out comp type */ 1144 comp_type = ap_get_compatible_type(qid, type, func); 1145 if (!comp_type) 1146 continue; 1147 /* maybe a card device needs to be created first */ 1148 if (!ac) { 1149 ac = ap_card_create(id, depth, type, 1150 comp_type, func); 1151 if (!ac) 1152 continue; 1153 ac->ap_dev.device.bus = &ap_bus_type; 1154 ac->ap_dev.device.parent = ap_root_device; 1155 dev_set_name(&ac->ap_dev.device, 1156 "card%02x", id); 1157 /* Register card with AP bus */ 1158 rc = device_register(&ac->ap_dev.device); 1159 if (rc) { 1160 put_device(&ac->ap_dev.device); 1161 ac = NULL; 1162 break; 1163 } 1164 /* get it and thus adjust reference counter */ 1165 get_device(&ac->ap_dev.device); 1166 } 1167 /* now create the new queue device */ 1168 aq = ap_queue_create(qid, comp_type); 1169 if (!aq) 1170 continue; 1171 aq->card = ac; 1172 aq->ap_dev.device.bus = &ap_bus_type; 1173 aq->ap_dev.device.parent = &ac->ap_dev.device; 1174 dev_set_name(&aq->ap_dev.device, 1175 "%02x.%04x", id, dom); 1176 /* Start with a device reset */ 1177 spin_lock_bh(&aq->lock); 1178 ap_wait(ap_sm_event(aq, AP_EVENT_POLL)); 1179 spin_unlock_bh(&aq->lock); 1180 /* Register device */ 1181 rc = device_register(&aq->ap_dev.device); 1182 if (rc) { 1183 put_device(&aq->ap_dev.device); 1184 continue; 1185 } 1186 domains++; 1187 if (dom == ap_domain_index) 1188 defdomdevs++; 1189 } /* end domain loop */ 1190 if (ac) { 1191 /* remove card dev if there are no queue devices */ 1192 if (!domains) 1193 device_unregister(&ac->ap_dev.device); 1194 put_device(&ac->ap_dev.device); 1195 } 1196 } /* end device loop */ 1197 1198 if (defdomdevs < 1) 1199 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n", 1200 ap_domain_index); 1201 1202 out: 1203 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 1204 } 1205 1206 static void ap_config_timeout(struct timer_list *unused) 1207 { 1208 if (ap_suspend_flag) 1209 return; 1210 queue_work(system_long_wq, &ap_scan_work); 1211 } 1212 1213 static void ap_reset_all(void) 1214 { 1215 int i, j; 1216 1217 for (i = 0; i < AP_DOMAINS; i++) { 1218 if (!ap_test_config_domain(i)) 1219 continue; 1220 for (j = 0; j < AP_DEVICES; j++) { 1221 if (!ap_test_config_card_id(j)) 1222 continue; 1223 ap_rapq(AP_MKQID(j, i)); 1224 } 1225 } 1226 } 1227 1228 static struct reset_call ap_reset_call = { 1229 .fn = ap_reset_all, 1230 }; 1231 1232 int __init ap_debug_init(void) 1233 { 1234 ap_dbf_info = debug_register("ap", 1, 1, 1235 DBF_MAX_SPRINTF_ARGS * sizeof(long)); 1236 debug_register_view(ap_dbf_info, &debug_sprintf_view); 1237 debug_set_level(ap_dbf_info, DBF_ERR); 1238 1239 return 0; 1240 } 1241 1242 void ap_debug_exit(void) 1243 { 1244 debug_unregister(ap_dbf_info); 1245 } 1246 1247 /** 1248 * ap_module_init(): The module initialization code. 1249 * 1250 * Initializes the module. 1251 */ 1252 int __init ap_module_init(void) 1253 { 1254 int max_domain_id; 1255 int rc, i; 1256 1257 rc = ap_debug_init(); 1258 if (rc) 1259 return rc; 1260 1261 if (ap_instructions_available() != 0) { 1262 pr_warn("The hardware system does not support AP instructions\n"); 1263 return -ENODEV; 1264 } 1265 1266 /* Get AP configuration data if available */ 1267 ap_init_configuration(); 1268 1269 if (ap_configuration) 1270 max_domain_id = 1271 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1; 1272 else 1273 max_domain_id = 15; 1274 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) { 1275 pr_warn("%d is not a valid cryptographic domain\n", 1276 ap_domain_index); 1277 ap_domain_index = -1; 1278 } 1279 /* In resume callback we need to know if the user had set the domain. 1280 * If so, we can not just reset it. 1281 */ 1282 if (ap_domain_index >= 0) 1283 user_set_domain = 1; 1284 1285 if (ap_interrupts_available()) { 1286 rc = register_adapter_interrupt(&ap_airq); 1287 ap_airq_flag = (rc == 0); 1288 } 1289 1290 register_reset_call(&ap_reset_call); 1291 1292 /* Create /sys/bus/ap. */ 1293 rc = bus_register(&ap_bus_type); 1294 if (rc) 1295 goto out; 1296 for (i = 0; ap_bus_attrs[i]; i++) { 1297 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); 1298 if (rc) 1299 goto out_bus; 1300 } 1301 1302 /* Create /sys/devices/ap. */ 1303 ap_root_device = root_device_register("ap"); 1304 rc = PTR_RET(ap_root_device); 1305 if (rc) 1306 goto out_bus; 1307 1308 /* Setup the AP bus rescan timer. */ 1309 timer_setup(&ap_config_timer, ap_config_timeout, 0); 1310 1311 /* 1312 * Setup the high resultion poll timer. 1313 * If we are running under z/VM adjust polling to z/VM polling rate. 1314 */ 1315 if (MACHINE_IS_VM) 1316 poll_timeout = 1500000; 1317 spin_lock_init(&ap_poll_timer_lock); 1318 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 1319 ap_poll_timer.function = ap_poll_timeout; 1320 1321 /* Start the low priority AP bus poll thread. */ 1322 if (ap_thread_flag) { 1323 rc = ap_poll_thread_start(); 1324 if (rc) 1325 goto out_work; 1326 } 1327 1328 rc = register_pm_notifier(&ap_power_notifier); 1329 if (rc) 1330 goto out_pm; 1331 1332 queue_work(system_long_wq, &ap_scan_work); 1333 initialised = true; 1334 1335 return 0; 1336 1337 out_pm: 1338 ap_poll_thread_stop(); 1339 out_work: 1340 hrtimer_cancel(&ap_poll_timer); 1341 root_device_unregister(ap_root_device); 1342 out_bus: 1343 while (i--) 1344 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); 1345 bus_unregister(&ap_bus_type); 1346 out: 1347 unregister_reset_call(&ap_reset_call); 1348 if (ap_using_interrupts()) 1349 unregister_adapter_interrupt(&ap_airq); 1350 kfree(ap_configuration); 1351 return rc; 1352 } 1353 device_initcall(ap_module_init); 1354