1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright IBM Corp. 2006, 2023 4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> 5 * Martin Schwidefsky <schwidefsky@de.ibm.com> 6 * Ralph Wuerthner <rwuerthn@de.ibm.com> 7 * Felix Beck <felix.beck@de.ibm.com> 8 * Holger Dengler <hd@linux.vnet.ibm.com> 9 * Harald Freudenberger <freude@linux.ibm.com> 10 * 11 * Adjunct processor bus. 12 */ 13 14 #define KMSG_COMPONENT "ap" 15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 16 17 #include <linux/kernel_stat.h> 18 #include <linux/moduleparam.h> 19 #include <linux/init.h> 20 #include <linux/delay.h> 21 #include <linux/err.h> 22 #include <linux/freezer.h> 23 #include <linux/interrupt.h> 24 #include <linux/workqueue.h> 25 #include <linux/slab.h> 26 #include <linux/notifier.h> 27 #include <linux/kthread.h> 28 #include <linux/mutex.h> 29 #include <asm/machine.h> 30 #include <asm/airq.h> 31 #include <asm/tpi.h> 32 #include <linux/atomic.h> 33 #include <asm/isc.h> 34 #include <linux/hrtimer.h> 35 #include <linux/ktime.h> 36 #include <asm/facility.h> 37 #include <linux/crypto.h> 38 #include <linux/mod_devicetable.h> 39 #include <linux/debugfs.h> 40 #include <linux/ctype.h> 41 #include <linux/module.h> 42 #include <asm/uv.h> 43 #include <asm/chsc.h> 44 45 #include "ap_bus.h" 46 #include "ap_debug.h" 47 48 MODULE_AUTHOR("IBM Corporation"); 49 MODULE_DESCRIPTION("Adjunct Processor Bus driver"); 50 MODULE_LICENSE("GPL"); 51 52 int ap_domain_index = -1; /* Adjunct Processor Domain Index */ 53 static DEFINE_SPINLOCK(ap_domain_lock); 54 module_param_named(domain, ap_domain_index, int, 0440); 55 MODULE_PARM_DESC(domain, "domain index for ap devices"); 56 EXPORT_SYMBOL(ap_domain_index); 57 58 static int ap_thread_flag; 59 module_param_named(poll_thread, ap_thread_flag, int, 0440); 60 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); 61 62 static char *apm_str; 63 module_param_named(apmask, apm_str, charp, 0440); 64 MODULE_PARM_DESC(apmask, "AP bus adapter mask."); 65 66 static char *aqm_str; 67 module_param_named(aqmask, aqm_str, charp, 0440); 68 MODULE_PARM_DESC(aqmask, "AP bus domain mask."); 69 70 static int ap_useirq = 1; 71 module_param_named(useirq, ap_useirq, int, 0440); 72 MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on)."); 73 74 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE); 75 EXPORT_SYMBOL(ap_max_msg_size); 76 77 static struct device *ap_root_device; 78 79 /* Hashtable of all queue devices on the AP bus */ 80 DEFINE_HASHTABLE(ap_queues, 8); 81 /* lock used for the ap_queues hashtable */ 82 DEFINE_SPINLOCK(ap_queues_lock); 83 84 /* Default permissions (ioctl, card and domain masking) */ 85 struct ap_perms ap_perms; 86 EXPORT_SYMBOL(ap_perms); 87 DEFINE_MUTEX(ap_perms_mutex); 88 EXPORT_SYMBOL(ap_perms_mutex); 89 90 /* # of bindings complete since init */ 91 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0); 92 93 /* completion for APQN bindings complete */ 94 static DECLARE_COMPLETION(ap_apqn_bindings_complete); 95 96 static struct ap_config_info qci[2]; 97 static struct ap_config_info *const ap_qci_info = &qci[0]; 98 static struct ap_config_info *const ap_qci_info_old = &qci[1]; 99 100 /* 101 * AP bus related debug feature things. 102 */ 103 debug_info_t *ap_dbf_info; 104 105 /* 106 * AP bus rescan related things. 107 */ 108 static bool ap_scan_bus(void); 109 static bool ap_scan_bus_result; /* result of last ap_scan_bus() */ 110 static DEFINE_MUTEX(ap_scan_bus_mutex); /* mutex ap_scan_bus() invocations */ 111 static struct task_struct *ap_scan_bus_task; /* thread holding the scan mutex */ 112 static atomic64_t ap_scan_bus_count; /* counter ap_scan_bus() invocations */ 113 static int ap_scan_bus_time = AP_CONFIG_TIME; 114 static struct timer_list ap_scan_bus_timer; 115 static void ap_scan_bus_wq_callback(struct work_struct *); 116 static DECLARE_WORK(ap_scan_bus_work, ap_scan_bus_wq_callback); 117 118 /* 119 * Tasklet & timer for AP request polling and interrupts 120 */ 121 static void ap_tasklet_fn(unsigned long); 122 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn); 123 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); 124 static struct task_struct *ap_poll_kthread; 125 static DEFINE_MUTEX(ap_poll_thread_mutex); 126 static DEFINE_SPINLOCK(ap_poll_timer_lock); 127 static struct hrtimer ap_poll_timer; 128 /* 129 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. 130 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling. 131 */ 132 static unsigned long poll_high_timeout = 250000UL; 133 134 /* 135 * Some state machine states only require a low frequency polling. 136 * We use 25 Hz frequency for these. 137 */ 138 static unsigned long poll_low_timeout = 40000000UL; 139 140 /* Maximum domain id, if not given via qci */ 141 static int ap_max_domain_id = 15; 142 /* Maximum adapter id, if not given via qci */ 143 static int ap_max_adapter_id = 63; 144 145 static const struct bus_type ap_bus_type; 146 147 /* Adapter interrupt definitions */ 148 static void ap_interrupt_handler(struct airq_struct *airq, 149 struct tpi_info *tpi_info); 150 151 static bool ap_irq_flag; 152 153 static struct airq_struct ap_airq = { 154 .handler = ap_interrupt_handler, 155 .isc = AP_ISC, 156 }; 157 158 /** 159 * ap_airq_ptr() - Get the address of the adapter interrupt indicator 160 * 161 * Returns the address of the local-summary-indicator of the adapter 162 * interrupt handler for AP, or NULL if adapter interrupts are not 163 * available. 164 */ 165 void *ap_airq_ptr(void) 166 { 167 if (ap_irq_flag) 168 return ap_airq.lsi_ptr; 169 return NULL; 170 } 171 172 /** 173 * ap_interrupts_available(): Test if AP interrupts are available. 174 * 175 * Returns 1 if AP interrupts are available. 176 */ 177 static int ap_interrupts_available(void) 178 { 179 return test_facility(65); 180 } 181 182 /** 183 * ap_qci_available(): Test if AP configuration 184 * information can be queried via QCI subfunction. 185 * 186 * Returns 1 if subfunction PQAP(QCI) is available. 187 */ 188 static int ap_qci_available(void) 189 { 190 return test_facility(12); 191 } 192 193 /** 194 * ap_apft_available(): Test if AP facilities test (APFT) 195 * facility is available. 196 * 197 * Returns 1 if APFT is available. 198 */ 199 static int ap_apft_available(void) 200 { 201 return test_facility(15); 202 } 203 204 /* 205 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available. 206 * 207 * Returns 1 if the QACT subfunction is available. 208 */ 209 static inline int ap_qact_available(void) 210 { 211 return ap_qci_info->qact; 212 } 213 214 /* 215 * ap_sb_available(): Test if the AP secure binding facility is available. 216 * 217 * Returns 1 if secure binding facility is available. 218 */ 219 int ap_sb_available(void) 220 { 221 return ap_qci_info->apsb; 222 } 223 224 /* 225 * ap_is_se_guest(): Check for SE guest with AP pass-through support. 226 */ 227 bool ap_is_se_guest(void) 228 { 229 return is_prot_virt_guest() && ap_sb_available(); 230 } 231 EXPORT_SYMBOL(ap_is_se_guest); 232 233 /** 234 * ap_init_qci_info(): Allocate and query qci config info. 235 * Does also update the static variables ap_max_domain_id 236 * and ap_max_adapter_id if this info is available. 237 */ 238 static void __init ap_init_qci_info(void) 239 { 240 if (!ap_qci_available() || 241 ap_qci(ap_qci_info)) { 242 AP_DBF_INFO("%s QCI not supported\n", __func__); 243 return; 244 } 245 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info)); 246 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__); 247 248 if (ap_qci_info->apxa) { 249 if (ap_qci_info->na) { 250 ap_max_adapter_id = ap_qci_info->na; 251 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n", 252 __func__, ap_max_adapter_id); 253 } 254 if (ap_qci_info->nd) { 255 ap_max_domain_id = ap_qci_info->nd; 256 AP_DBF_INFO("%s new ap_max_domain_id is %d\n", 257 __func__, ap_max_domain_id); 258 } 259 } 260 } 261 262 /* 263 * ap_test_config(): helper function to extract the nrth bit 264 * within the unsigned int array field. 265 */ 266 static inline int ap_test_config(unsigned int *field, unsigned int nr) 267 { 268 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); 269 } 270 271 /* 272 * ap_test_config_card_id(): Test, whether an AP card ID is configured. 273 * 274 * Returns 0 if the card is not configured 275 * 1 if the card is configured or 276 * if the configuration information is not available 277 */ 278 static inline int ap_test_config_card_id(unsigned int id) 279 { 280 if (id > ap_max_adapter_id) 281 return 0; 282 if (ap_qci_info->flags) 283 return ap_test_config(ap_qci_info->apm, id); 284 return 1; 285 } 286 287 /* 288 * ap_test_config_usage_domain(): Test, whether an AP usage domain 289 * is configured. 290 * 291 * Returns 0 if the usage domain is not configured 292 * 1 if the usage domain is configured or 293 * if the configuration information is not available 294 */ 295 int ap_test_config_usage_domain(unsigned int domain) 296 { 297 if (domain > ap_max_domain_id) 298 return 0; 299 if (ap_qci_info->flags) 300 return ap_test_config(ap_qci_info->aqm, domain); 301 return 1; 302 } 303 EXPORT_SYMBOL(ap_test_config_usage_domain); 304 305 /* 306 * ap_test_config_ctrl_domain(): Test, whether an AP control domain 307 * is configured. 308 * @domain AP control domain ID 309 * 310 * Returns 1 if the control domain is configured 311 * 0 in all other cases 312 */ 313 int ap_test_config_ctrl_domain(unsigned int domain) 314 { 315 if (!ap_qci_info || domain > ap_max_domain_id) 316 return 0; 317 return ap_test_config(ap_qci_info->adm, domain); 318 } 319 EXPORT_SYMBOL(ap_test_config_ctrl_domain); 320 321 /* 322 * ap_queue_info(): Check and get AP queue info. 323 * Returns: 1 if APQN exists and info is filled, 324 * 0 if APQN seems to exist but there is no info 325 * available (eg. caused by an asynch pending error) 326 * -1 invalid APQN, TAPQ error or AP queue status which 327 * indicates there is no APQN. 328 */ 329 static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo, 330 bool *decfg, bool *cstop) 331 { 332 struct ap_queue_status status; 333 334 hwinfo->value = 0; 335 336 /* make sure we don't run into a specifiation exception */ 337 if (AP_QID_CARD(qid) > ap_max_adapter_id || 338 AP_QID_QUEUE(qid) > ap_max_domain_id) 339 return -1; 340 341 /* call TAPQ on this APQN */ 342 status = ap_test_queue(qid, ap_apft_available(), hwinfo); 343 344 switch (status.response_code) { 345 case AP_RESPONSE_NORMAL: 346 case AP_RESPONSE_RESET_IN_PROGRESS: 347 case AP_RESPONSE_DECONFIGURED: 348 case AP_RESPONSE_CHECKSTOPPED: 349 case AP_RESPONSE_BUSY: 350 /* For all these RCs the tapq info should be available */ 351 break; 352 default: 353 /* On a pending async error the info should be available */ 354 if (!status.async) 355 return -1; 356 break; 357 } 358 359 /* There should be at least one of the mode bits set */ 360 if (WARN_ON_ONCE(!hwinfo->value)) 361 return 0; 362 363 *decfg = status.response_code == AP_RESPONSE_DECONFIGURED; 364 *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED; 365 366 return 1; 367 } 368 369 void ap_wait(enum ap_sm_wait wait) 370 { 371 ktime_t hr_time; 372 373 switch (wait) { 374 case AP_SM_WAIT_AGAIN: 375 case AP_SM_WAIT_INTERRUPT: 376 if (ap_irq_flag) 377 break; 378 if (ap_poll_kthread) { 379 wake_up(&ap_poll_wait); 380 break; 381 } 382 fallthrough; 383 case AP_SM_WAIT_LOW_TIMEOUT: 384 case AP_SM_WAIT_HIGH_TIMEOUT: 385 spin_lock_bh(&ap_poll_timer_lock); 386 if (!hrtimer_is_queued(&ap_poll_timer)) { 387 hr_time = 388 wait == AP_SM_WAIT_LOW_TIMEOUT ? 389 poll_low_timeout : poll_high_timeout; 390 hrtimer_forward_now(&ap_poll_timer, hr_time); 391 hrtimer_restart(&ap_poll_timer); 392 } 393 spin_unlock_bh(&ap_poll_timer_lock); 394 break; 395 case AP_SM_WAIT_NONE: 396 default: 397 break; 398 } 399 } 400 401 /** 402 * ap_request_timeout(): Handling of request timeouts 403 * @t: timer making this callback 404 * 405 * Handles request timeouts. 406 */ 407 void ap_request_timeout(struct timer_list *t) 408 { 409 struct ap_queue *aq = from_timer(aq, t, timeout); 410 411 spin_lock_bh(&aq->lock); 412 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT)); 413 spin_unlock_bh(&aq->lock); 414 } 415 416 /** 417 * ap_poll_timeout(): AP receive polling for finished AP requests. 418 * @unused: Unused pointer. 419 * 420 * Schedules the AP tasklet using a high resolution timer. 421 */ 422 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) 423 { 424 tasklet_schedule(&ap_tasklet); 425 return HRTIMER_NORESTART; 426 } 427 428 /** 429 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt 430 * @airq: pointer to adapter interrupt descriptor 431 * @tpi_info: ignored 432 */ 433 static void ap_interrupt_handler(struct airq_struct *airq, 434 struct tpi_info *tpi_info) 435 { 436 inc_irq_stat(IRQIO_APB); 437 tasklet_schedule(&ap_tasklet); 438 } 439 440 /** 441 * ap_tasklet_fn(): Tasklet to poll all AP devices. 442 * @dummy: Unused variable 443 * 444 * Poll all AP devices on the bus. 445 */ 446 static void ap_tasklet_fn(unsigned long dummy) 447 { 448 int bkt; 449 struct ap_queue *aq; 450 enum ap_sm_wait wait = AP_SM_WAIT_NONE; 451 452 /* Reset the indicator if interrupts are used. Thus new interrupts can 453 * be received. Doing it in the beginning of the tasklet is therefore 454 * important that no requests on any AP get lost. 455 */ 456 if (ap_irq_flag) 457 WRITE_ONCE(*ap_airq.lsi_ptr, 0); 458 459 spin_lock_bh(&ap_queues_lock); 460 hash_for_each(ap_queues, bkt, aq, hnode) { 461 spin_lock_bh(&aq->lock); 462 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL)); 463 spin_unlock_bh(&aq->lock); 464 } 465 spin_unlock_bh(&ap_queues_lock); 466 467 ap_wait(wait); 468 } 469 470 static int ap_pending_requests(void) 471 { 472 int bkt; 473 struct ap_queue *aq; 474 475 spin_lock_bh(&ap_queues_lock); 476 hash_for_each(ap_queues, bkt, aq, hnode) { 477 if (aq->queue_count == 0) 478 continue; 479 spin_unlock_bh(&ap_queues_lock); 480 return 1; 481 } 482 spin_unlock_bh(&ap_queues_lock); 483 return 0; 484 } 485 486 /** 487 * ap_poll_thread(): Thread that polls for finished requests. 488 * @data: Unused pointer 489 * 490 * AP bus poll thread. The purpose of this thread is to poll for 491 * finished requests in a loop if there is a "free" cpu - that is 492 * a cpu that doesn't have anything better to do. The polling stops 493 * as soon as there is another task or if all messages have been 494 * delivered. 495 */ 496 static int ap_poll_thread(void *data) 497 { 498 DECLARE_WAITQUEUE(wait, current); 499 500 set_user_nice(current, MAX_NICE); 501 set_freezable(); 502 while (!kthread_should_stop()) { 503 add_wait_queue(&ap_poll_wait, &wait); 504 set_current_state(TASK_INTERRUPTIBLE); 505 if (!ap_pending_requests()) { 506 schedule(); 507 try_to_freeze(); 508 } 509 set_current_state(TASK_RUNNING); 510 remove_wait_queue(&ap_poll_wait, &wait); 511 if (need_resched()) { 512 schedule(); 513 try_to_freeze(); 514 continue; 515 } 516 ap_tasklet_fn(0); 517 } 518 519 return 0; 520 } 521 522 static int ap_poll_thread_start(void) 523 { 524 int rc; 525 526 if (ap_irq_flag || ap_poll_kthread) 527 return 0; 528 mutex_lock(&ap_poll_thread_mutex); 529 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); 530 rc = PTR_ERR_OR_ZERO(ap_poll_kthread); 531 if (rc) 532 ap_poll_kthread = NULL; 533 mutex_unlock(&ap_poll_thread_mutex); 534 return rc; 535 } 536 537 static void ap_poll_thread_stop(void) 538 { 539 if (!ap_poll_kthread) 540 return; 541 mutex_lock(&ap_poll_thread_mutex); 542 kthread_stop(ap_poll_kthread); 543 ap_poll_kthread = NULL; 544 mutex_unlock(&ap_poll_thread_mutex); 545 } 546 547 #define is_card_dev(x) ((x)->parent == ap_root_device) 548 #define is_queue_dev(x) ((x)->parent != ap_root_device) 549 550 /** 551 * ap_bus_match() 552 * @dev: Pointer to device 553 * @drv: Pointer to device_driver 554 * 555 * AP bus driver registration/unregistration. 556 */ 557 static int ap_bus_match(struct device *dev, const struct device_driver *drv) 558 { 559 const struct ap_driver *ap_drv = to_ap_drv(drv); 560 struct ap_device_id *id; 561 562 /* 563 * Compare device type of the device with the list of 564 * supported types of the device_driver. 565 */ 566 for (id = ap_drv->ids; id->match_flags; id++) { 567 if (is_card_dev(dev) && 568 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && 569 id->dev_type == to_ap_dev(dev)->device_type) 570 return 1; 571 if (is_queue_dev(dev) && 572 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && 573 id->dev_type == to_ap_dev(dev)->device_type) 574 return 1; 575 } 576 return 0; 577 } 578 579 /** 580 * ap_uevent(): Uevent function for AP devices. 581 * @dev: Pointer to device 582 * @env: Pointer to kobj_uevent_env 583 * 584 * It sets up a single environment variable DEV_TYPE which contains the 585 * hardware device type. 586 */ 587 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env) 588 { 589 int rc = 0; 590 const struct ap_device *ap_dev = to_ap_dev(dev); 591 592 /* Uevents from ap bus core don't need extensions to the env */ 593 if (dev == ap_root_device) 594 return 0; 595 596 if (is_card_dev(dev)) { 597 struct ap_card *ac = to_ap_card(&ap_dev->device); 598 599 /* Set up DEV_TYPE environment variable. */ 600 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); 601 if (rc) 602 return rc; 603 /* Add MODALIAS= */ 604 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); 605 if (rc) 606 return rc; 607 608 /* Add MODE=<accel|cca|ep11> */ 609 if (ac->hwinfo.accel) 610 rc = add_uevent_var(env, "MODE=accel"); 611 else if (ac->hwinfo.cca) 612 rc = add_uevent_var(env, "MODE=cca"); 613 else if (ac->hwinfo.ep11) 614 rc = add_uevent_var(env, "MODE=ep11"); 615 if (rc) 616 return rc; 617 } else { 618 struct ap_queue *aq = to_ap_queue(&ap_dev->device); 619 620 /* Add MODE=<accel|cca|ep11> */ 621 if (aq->card->hwinfo.accel) 622 rc = add_uevent_var(env, "MODE=accel"); 623 else if (aq->card->hwinfo.cca) 624 rc = add_uevent_var(env, "MODE=cca"); 625 else if (aq->card->hwinfo.ep11) 626 rc = add_uevent_var(env, "MODE=ep11"); 627 if (rc) 628 return rc; 629 } 630 631 return 0; 632 } 633 634 static void ap_send_init_scan_done_uevent(void) 635 { 636 char *envp[] = { "INITSCAN=done", NULL }; 637 638 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); 639 } 640 641 static void ap_send_bindings_complete_uevent(void) 642 { 643 char buf[32]; 644 char *envp[] = { "BINDINGS=complete", buf, NULL }; 645 646 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu", 647 atomic64_inc_return(&ap_bindings_complete_count)); 648 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); 649 } 650 651 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg) 652 { 653 char buf[16]; 654 char *envp[] = { buf, NULL }; 655 656 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0); 657 658 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp); 659 } 660 EXPORT_SYMBOL(ap_send_config_uevent); 661 662 void ap_send_online_uevent(struct ap_device *ap_dev, int online) 663 { 664 char buf[16]; 665 char *envp[] = { buf, NULL }; 666 667 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0); 668 669 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp); 670 } 671 EXPORT_SYMBOL(ap_send_online_uevent); 672 673 static void ap_send_mask_changed_uevent(unsigned long *newapm, 674 unsigned long *newaqm) 675 { 676 char buf[100]; 677 char *envp[] = { buf, NULL }; 678 679 if (newapm) 680 snprintf(buf, sizeof(buf), 681 "APMASK=0x%016lx%016lx%016lx%016lx\n", 682 newapm[0], newapm[1], newapm[2], newapm[3]); 683 else 684 snprintf(buf, sizeof(buf), 685 "AQMASK=0x%016lx%016lx%016lx%016lx\n", 686 newaqm[0], newaqm[1], newaqm[2], newaqm[3]); 687 688 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); 689 } 690 691 /* 692 * calc # of bound APQNs 693 */ 694 695 struct __ap_calc_ctrs { 696 unsigned int apqns; 697 unsigned int bound; 698 }; 699 700 static int __ap_calc_helper(struct device *dev, void *arg) 701 { 702 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg; 703 704 if (is_queue_dev(dev)) { 705 pctrs->apqns++; 706 if (dev->driver) 707 pctrs->bound++; 708 } 709 710 return 0; 711 } 712 713 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound) 714 { 715 struct __ap_calc_ctrs ctrs; 716 717 memset(&ctrs, 0, sizeof(ctrs)); 718 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper); 719 720 *apqns = ctrs.apqns; 721 *bound = ctrs.bound; 722 } 723 724 /* 725 * After ap bus scan do check if all existing APQNs are 726 * bound to device drivers. 727 */ 728 static void ap_check_bindings_complete(void) 729 { 730 unsigned int apqns, bound; 731 732 if (atomic64_read(&ap_scan_bus_count) >= 1) { 733 ap_calc_bound_apqns(&apqns, &bound); 734 if (bound == apqns) { 735 if (!completion_done(&ap_apqn_bindings_complete)) { 736 complete_all(&ap_apqn_bindings_complete); 737 ap_send_bindings_complete_uevent(); 738 pr_debug("all apqn bindings complete\n"); 739 } 740 } 741 } 742 } 743 744 /* 745 * Interface to wait for the AP bus to have done one initial ap bus 746 * scan and all detected APQNs have been bound to device drivers. 747 * If these both conditions are not fulfilled, this function blocks 748 * on a condition with wait_for_completion_interruptible_timeout(). 749 * If these both conditions are fulfilled (before the timeout hits) 750 * the return value is 0. If the timeout (in jiffies) hits instead 751 * -ETIME is returned. On failures negative return values are 752 * returned to the caller. 753 */ 754 int ap_wait_apqn_bindings_complete(unsigned long timeout) 755 { 756 int rc = 0; 757 long l; 758 759 if (completion_done(&ap_apqn_bindings_complete)) 760 return 0; 761 762 if (timeout) 763 l = wait_for_completion_interruptible_timeout( 764 &ap_apqn_bindings_complete, timeout); 765 else 766 l = wait_for_completion_interruptible( 767 &ap_apqn_bindings_complete); 768 if (l < 0) 769 rc = l == -ERESTARTSYS ? -EINTR : l; 770 else if (l == 0 && timeout) 771 rc = -ETIME; 772 773 pr_debug("rc=%d\n", rc); 774 return rc; 775 } 776 EXPORT_SYMBOL(ap_wait_apqn_bindings_complete); 777 778 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) 779 { 780 if (is_queue_dev(dev) && 781 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data) 782 device_unregister(dev); 783 return 0; 784 } 785 786 static int __ap_revise_reserved(struct device *dev, void *dummy) 787 { 788 int rc, card, queue, devres, drvres; 789 790 if (is_queue_dev(dev)) { 791 card = AP_QID_CARD(to_ap_queue(dev)->qid); 792 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 793 mutex_lock(&ap_perms_mutex); 794 devres = test_bit_inv(card, ap_perms.apm) && 795 test_bit_inv(queue, ap_perms.aqm); 796 mutex_unlock(&ap_perms_mutex); 797 drvres = to_ap_drv(dev->driver)->flags 798 & AP_DRIVER_FLAG_DEFAULT; 799 if (!!devres != !!drvres) { 800 pr_debug("reprobing queue=%02x.%04x\n", card, queue); 801 rc = device_reprobe(dev); 802 if (rc) 803 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n", 804 __func__, card, queue); 805 } 806 } 807 808 return 0; 809 } 810 811 static void ap_bus_revise_bindings(void) 812 { 813 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved); 814 } 815 816 /** 817 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the 818 * default host driver or not. 819 * @card: the APID of the adapter card to check 820 * @queue: the APQI of the queue to check 821 * 822 * Note: the ap_perms_mutex must be locked by the caller of this function. 823 * 824 * Return: an int specifying whether the AP adapter is reserved for the host (1) 825 * or not (0). 826 */ 827 int ap_owned_by_def_drv(int card, int queue) 828 { 829 int rc = 0; 830 831 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS) 832 return -EINVAL; 833 834 if (test_bit_inv(card, ap_perms.apm) && 835 test_bit_inv(queue, ap_perms.aqm)) 836 rc = 1; 837 838 return rc; 839 } 840 EXPORT_SYMBOL(ap_owned_by_def_drv); 841 842 /** 843 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in 844 * a set is reserved for the host drivers 845 * or not. 846 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check 847 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check 848 * 849 * Note: the ap_perms_mutex must be locked by the caller of this function. 850 * 851 * Return: an int specifying whether each APQN is reserved for the host (1) or 852 * not (0) 853 */ 854 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm, 855 unsigned long *aqm) 856 { 857 int card, queue, rc = 0; 858 859 for (card = 0; !rc && card < AP_DEVICES; card++) 860 if (test_bit_inv(card, apm) && 861 test_bit_inv(card, ap_perms.apm)) 862 for (queue = 0; !rc && queue < AP_DOMAINS; queue++) 863 if (test_bit_inv(queue, aqm) && 864 test_bit_inv(queue, ap_perms.aqm)) 865 rc = 1; 866 867 return rc; 868 } 869 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv); 870 871 static int ap_device_probe(struct device *dev) 872 { 873 struct ap_device *ap_dev = to_ap_dev(dev); 874 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 875 int card, queue, devres, drvres, rc = -ENODEV; 876 877 if (!get_device(dev)) 878 return rc; 879 880 if (is_queue_dev(dev)) { 881 /* 882 * If the apqn is marked as reserved/used by ap bus and 883 * default drivers, only probe with drivers with the default 884 * flag set. If it is not marked, only probe with drivers 885 * with the default flag not set. 886 */ 887 card = AP_QID_CARD(to_ap_queue(dev)->qid); 888 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 889 mutex_lock(&ap_perms_mutex); 890 devres = test_bit_inv(card, ap_perms.apm) && 891 test_bit_inv(queue, ap_perms.aqm); 892 mutex_unlock(&ap_perms_mutex); 893 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT; 894 if (!!devres != !!drvres) 895 goto out; 896 } 897 898 /* 899 * Rearm the bindings complete completion to trigger 900 * bindings complete when all devices are bound again 901 */ 902 reinit_completion(&ap_apqn_bindings_complete); 903 904 /* Add queue/card to list of active queues/cards */ 905 spin_lock_bh(&ap_queues_lock); 906 if (is_queue_dev(dev)) 907 hash_add(ap_queues, &to_ap_queue(dev)->hnode, 908 to_ap_queue(dev)->qid); 909 spin_unlock_bh(&ap_queues_lock); 910 911 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 912 913 if (rc) { 914 spin_lock_bh(&ap_queues_lock); 915 if (is_queue_dev(dev)) 916 hash_del(&to_ap_queue(dev)->hnode); 917 spin_unlock_bh(&ap_queues_lock); 918 } 919 920 out: 921 if (rc) 922 put_device(dev); 923 return rc; 924 } 925 926 static void ap_device_remove(struct device *dev) 927 { 928 struct ap_device *ap_dev = to_ap_dev(dev); 929 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 930 931 /* prepare ap queue device removal */ 932 if (is_queue_dev(dev)) 933 ap_queue_prepare_remove(to_ap_queue(dev)); 934 935 /* driver's chance to clean up gracefully */ 936 if (ap_drv->remove) 937 ap_drv->remove(ap_dev); 938 939 /* now do the ap queue device remove */ 940 if (is_queue_dev(dev)) 941 ap_queue_remove(to_ap_queue(dev)); 942 943 /* Remove queue/card from list of active queues/cards */ 944 spin_lock_bh(&ap_queues_lock); 945 if (is_queue_dev(dev)) 946 hash_del(&to_ap_queue(dev)->hnode); 947 spin_unlock_bh(&ap_queues_lock); 948 949 put_device(dev); 950 } 951 952 struct ap_queue *ap_get_qdev(ap_qid_t qid) 953 { 954 int bkt; 955 struct ap_queue *aq; 956 957 spin_lock_bh(&ap_queues_lock); 958 hash_for_each(ap_queues, bkt, aq, hnode) { 959 if (aq->qid == qid) { 960 get_device(&aq->ap_dev.device); 961 spin_unlock_bh(&ap_queues_lock); 962 return aq; 963 } 964 } 965 spin_unlock_bh(&ap_queues_lock); 966 967 return NULL; 968 } 969 EXPORT_SYMBOL(ap_get_qdev); 970 971 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 972 char *name) 973 { 974 struct device_driver *drv = &ap_drv->driver; 975 int rc; 976 977 drv->bus = &ap_bus_type; 978 drv->owner = owner; 979 drv->name = name; 980 rc = driver_register(drv); 981 982 ap_check_bindings_complete(); 983 984 return rc; 985 } 986 EXPORT_SYMBOL(ap_driver_register); 987 988 void ap_driver_unregister(struct ap_driver *ap_drv) 989 { 990 driver_unregister(&ap_drv->driver); 991 } 992 EXPORT_SYMBOL(ap_driver_unregister); 993 994 /* 995 * Enforce a synchronous AP bus rescan. 996 * Returns true if the bus scan finds a change in the AP configuration 997 * and AP devices have been added or deleted when this function returns. 998 */ 999 bool ap_bus_force_rescan(void) 1000 { 1001 unsigned long scan_counter = atomic64_read(&ap_scan_bus_count); 1002 bool rc = false; 1003 1004 pr_debug("> scan counter=%lu\n", scan_counter); 1005 1006 /* Only trigger AP bus scans after the initial scan is done */ 1007 if (scan_counter <= 0) 1008 goto out; 1009 1010 /* 1011 * There is one unlikely but nevertheless valid scenario where the 1012 * thread holding the mutex may try to send some crypto load but 1013 * all cards are offline so a rescan is triggered which causes 1014 * a recursive call of ap_bus_force_rescan(). A simple return if 1015 * the mutex is already locked by this thread solves this. 1016 */ 1017 if (mutex_is_locked(&ap_scan_bus_mutex)) { 1018 if (ap_scan_bus_task == current) 1019 goto out; 1020 } 1021 1022 /* Try to acquire the AP scan bus mutex */ 1023 if (mutex_trylock(&ap_scan_bus_mutex)) { 1024 /* mutex acquired, run the AP bus scan */ 1025 ap_scan_bus_task = current; 1026 ap_scan_bus_result = ap_scan_bus(); 1027 rc = ap_scan_bus_result; 1028 ap_scan_bus_task = NULL; 1029 mutex_unlock(&ap_scan_bus_mutex); 1030 goto out; 1031 } 1032 1033 /* 1034 * Mutex acquire failed. So there is currently another task 1035 * already running the AP bus scan. Then let's simple wait 1036 * for the lock which means the other task has finished and 1037 * stored the result in ap_scan_bus_result. 1038 */ 1039 if (mutex_lock_interruptible(&ap_scan_bus_mutex)) { 1040 /* some error occurred, ignore and go out */ 1041 goto out; 1042 } 1043 rc = ap_scan_bus_result; 1044 mutex_unlock(&ap_scan_bus_mutex); 1045 1046 out: 1047 pr_debug("rc=%d\n", rc); 1048 return rc; 1049 } 1050 EXPORT_SYMBOL(ap_bus_force_rescan); 1051 1052 /* 1053 * A config change has happened, force an ap bus rescan. 1054 */ 1055 static int ap_bus_cfg_chg(struct notifier_block *nb, 1056 unsigned long action, void *data) 1057 { 1058 if (action != CHSC_NOTIFY_AP_CFG) 1059 return NOTIFY_DONE; 1060 1061 pr_debug("config change, forcing bus rescan\n"); 1062 1063 ap_bus_force_rescan(); 1064 1065 return NOTIFY_OK; 1066 } 1067 1068 static struct notifier_block ap_bus_nb = { 1069 .notifier_call = ap_bus_cfg_chg, 1070 }; 1071 1072 int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits) 1073 { 1074 int i, n, b; 1075 1076 /* bits needs to be a multiple of 8 */ 1077 if (bits & 0x07) 1078 return -EINVAL; 1079 1080 if (str[0] == '0' && str[1] == 'x') 1081 str++; 1082 if (*str == 'x') 1083 str++; 1084 1085 for (i = 0; isxdigit(*str) && i < bits; str++) { 1086 b = hex_to_bin(*str); 1087 for (n = 0; n < 4; n++) 1088 if (b & (0x08 >> n)) 1089 set_bit_inv(i + n, bitmap); 1090 i += 4; 1091 } 1092 1093 if (*str == '\n') 1094 str++; 1095 if (*str) 1096 return -EINVAL; 1097 return 0; 1098 } 1099 EXPORT_SYMBOL(ap_hex2bitmap); 1100 1101 /* 1102 * modify_bitmap() - parse bitmask argument and modify an existing 1103 * bit mask accordingly. A concatenation (done with ',') of these 1104 * terms is recognized: 1105 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>] 1106 * <bitnr> may be any valid number (hex, decimal or octal) in the range 1107 * 0...bits-1; the leading + or - is required. Here are some examples: 1108 * +0-15,+32,-128,-0xFF 1109 * -0-255,+1-16,+0x128 1110 * +1,+2,+3,+4,-5,-7-10 1111 * Returns the new bitmap after all changes have been applied. Every 1112 * positive value in the string will set a bit and every negative value 1113 * in the string will clear a bit. As a bit may be touched more than once, 1114 * the last 'operation' wins: 1115 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be 1116 * cleared again. All other bits are unmodified. 1117 */ 1118 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits) 1119 { 1120 unsigned long a, i, z; 1121 char *np, sign; 1122 1123 /* bits needs to be a multiple of 8 */ 1124 if (bits & 0x07) 1125 return -EINVAL; 1126 1127 while (*str) { 1128 sign = *str++; 1129 if (sign != '+' && sign != '-') 1130 return -EINVAL; 1131 a = z = simple_strtoul(str, &np, 0); 1132 if (str == np || a >= bits) 1133 return -EINVAL; 1134 str = np; 1135 if (*str == '-') { 1136 z = simple_strtoul(++str, &np, 0); 1137 if (str == np || a > z || z >= bits) 1138 return -EINVAL; 1139 str = np; 1140 } 1141 for (i = a; i <= z; i++) 1142 if (sign == '+') 1143 set_bit_inv(i, bitmap); 1144 else 1145 clear_bit_inv(i, bitmap); 1146 while (*str == ',' || *str == '\n') 1147 str++; 1148 } 1149 1150 return 0; 1151 } 1152 1153 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits, 1154 unsigned long *newmap) 1155 { 1156 unsigned long size; 1157 int rc; 1158 1159 size = BITS_TO_LONGS(bits) * sizeof(unsigned long); 1160 if (*str == '+' || *str == '-') { 1161 memcpy(newmap, bitmap, size); 1162 rc = modify_bitmap(str, newmap, bits); 1163 } else { 1164 memset(newmap, 0, size); 1165 rc = ap_hex2bitmap(str, newmap, bits); 1166 } 1167 return rc; 1168 } 1169 1170 int ap_parse_mask_str(const char *str, 1171 unsigned long *bitmap, int bits, 1172 struct mutex *lock) 1173 { 1174 unsigned long *newmap, size; 1175 int rc; 1176 1177 /* bits needs to be a multiple of 8 */ 1178 if (bits & 0x07) 1179 return -EINVAL; 1180 1181 size = BITS_TO_LONGS(bits) * sizeof(unsigned long); 1182 newmap = kmalloc(size, GFP_KERNEL); 1183 if (!newmap) 1184 return -ENOMEM; 1185 if (mutex_lock_interruptible(lock)) { 1186 kfree(newmap); 1187 return -ERESTARTSYS; 1188 } 1189 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap); 1190 if (rc == 0) 1191 memcpy(bitmap, newmap, size); 1192 mutex_unlock(lock); 1193 kfree(newmap); 1194 return rc; 1195 } 1196 EXPORT_SYMBOL(ap_parse_mask_str); 1197 1198 /* 1199 * AP bus attributes. 1200 */ 1201 1202 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf) 1203 { 1204 return sysfs_emit(buf, "%d\n", ap_domain_index); 1205 } 1206 1207 static ssize_t ap_domain_store(const struct bus_type *bus, 1208 const char *buf, size_t count) 1209 { 1210 int domain; 1211 1212 if (sscanf(buf, "%i\n", &domain) != 1 || 1213 domain < 0 || domain > ap_max_domain_id || 1214 !test_bit_inv(domain, ap_perms.aqm)) 1215 return -EINVAL; 1216 1217 spin_lock_bh(&ap_domain_lock); 1218 ap_domain_index = domain; 1219 spin_unlock_bh(&ap_domain_lock); 1220 1221 AP_DBF_INFO("%s stored new default domain=%d\n", 1222 __func__, domain); 1223 1224 return count; 1225 } 1226 1227 static BUS_ATTR_RW(ap_domain); 1228 1229 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf) 1230 { 1231 if (!ap_qci_info->flags) /* QCI not supported */ 1232 return sysfs_emit(buf, "not supported\n"); 1233 1234 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1235 ap_qci_info->adm[0], ap_qci_info->adm[1], 1236 ap_qci_info->adm[2], ap_qci_info->adm[3], 1237 ap_qci_info->adm[4], ap_qci_info->adm[5], 1238 ap_qci_info->adm[6], ap_qci_info->adm[7]); 1239 } 1240 1241 static BUS_ATTR_RO(ap_control_domain_mask); 1242 1243 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf) 1244 { 1245 if (!ap_qci_info->flags) /* QCI not supported */ 1246 return sysfs_emit(buf, "not supported\n"); 1247 1248 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1249 ap_qci_info->aqm[0], ap_qci_info->aqm[1], 1250 ap_qci_info->aqm[2], ap_qci_info->aqm[3], 1251 ap_qci_info->aqm[4], ap_qci_info->aqm[5], 1252 ap_qci_info->aqm[6], ap_qci_info->aqm[7]); 1253 } 1254 1255 static BUS_ATTR_RO(ap_usage_domain_mask); 1256 1257 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf) 1258 { 1259 if (!ap_qci_info->flags) /* QCI not supported */ 1260 return sysfs_emit(buf, "not supported\n"); 1261 1262 return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1263 ap_qci_info->apm[0], ap_qci_info->apm[1], 1264 ap_qci_info->apm[2], ap_qci_info->apm[3], 1265 ap_qci_info->apm[4], ap_qci_info->apm[5], 1266 ap_qci_info->apm[6], ap_qci_info->apm[7]); 1267 } 1268 1269 static BUS_ATTR_RO(ap_adapter_mask); 1270 1271 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf) 1272 { 1273 return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0); 1274 } 1275 1276 static BUS_ATTR_RO(ap_interrupts); 1277 1278 static ssize_t config_time_show(const struct bus_type *bus, char *buf) 1279 { 1280 return sysfs_emit(buf, "%d\n", ap_scan_bus_time); 1281 } 1282 1283 static ssize_t config_time_store(const struct bus_type *bus, 1284 const char *buf, size_t count) 1285 { 1286 int time; 1287 1288 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 1289 return -EINVAL; 1290 ap_scan_bus_time = time; 1291 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ); 1292 return count; 1293 } 1294 1295 static BUS_ATTR_RW(config_time); 1296 1297 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf) 1298 { 1299 return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0); 1300 } 1301 1302 static ssize_t poll_thread_store(const struct bus_type *bus, 1303 const char *buf, size_t count) 1304 { 1305 bool value; 1306 int rc; 1307 1308 rc = kstrtobool(buf, &value); 1309 if (rc) 1310 return rc; 1311 1312 if (value) { 1313 rc = ap_poll_thread_start(); 1314 if (rc) 1315 count = rc; 1316 } else { 1317 ap_poll_thread_stop(); 1318 } 1319 return count; 1320 } 1321 1322 static BUS_ATTR_RW(poll_thread); 1323 1324 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf) 1325 { 1326 return sysfs_emit(buf, "%lu\n", poll_high_timeout); 1327 } 1328 1329 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf, 1330 size_t count) 1331 { 1332 unsigned long value; 1333 ktime_t hr_time; 1334 int rc; 1335 1336 rc = kstrtoul(buf, 0, &value); 1337 if (rc) 1338 return rc; 1339 1340 /* 120 seconds = maximum poll interval */ 1341 if (value > 120000000000UL) 1342 return -EINVAL; 1343 poll_high_timeout = value; 1344 hr_time = poll_high_timeout; 1345 1346 spin_lock_bh(&ap_poll_timer_lock); 1347 hrtimer_cancel(&ap_poll_timer); 1348 hrtimer_set_expires(&ap_poll_timer, hr_time); 1349 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); 1350 spin_unlock_bh(&ap_poll_timer_lock); 1351 1352 return count; 1353 } 1354 1355 static BUS_ATTR_RW(poll_timeout); 1356 1357 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf) 1358 { 1359 return sysfs_emit(buf, "%d\n", ap_max_domain_id); 1360 } 1361 1362 static BUS_ATTR_RO(ap_max_domain_id); 1363 1364 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf) 1365 { 1366 return sysfs_emit(buf, "%d\n", ap_max_adapter_id); 1367 } 1368 1369 static BUS_ATTR_RO(ap_max_adapter_id); 1370 1371 static ssize_t apmask_show(const struct bus_type *bus, char *buf) 1372 { 1373 int rc; 1374 1375 if (mutex_lock_interruptible(&ap_perms_mutex)) 1376 return -ERESTARTSYS; 1377 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n", 1378 ap_perms.apm[0], ap_perms.apm[1], 1379 ap_perms.apm[2], ap_perms.apm[3]); 1380 mutex_unlock(&ap_perms_mutex); 1381 1382 return rc; 1383 } 1384 1385 static int __verify_card_reservations(struct device_driver *drv, void *data) 1386 { 1387 int rc = 0; 1388 struct ap_driver *ap_drv = to_ap_drv(drv); 1389 unsigned long *newapm = (unsigned long *)data; 1390 1391 /* 1392 * increase the driver's module refcounter to be sure it is not 1393 * going away when we invoke the callback function. 1394 */ 1395 if (!try_module_get(drv->owner)) 1396 return 0; 1397 1398 if (ap_drv->in_use) { 1399 rc = ap_drv->in_use(newapm, ap_perms.aqm); 1400 if (rc) 1401 rc = -EBUSY; 1402 } 1403 1404 /* release the driver's module */ 1405 module_put(drv->owner); 1406 1407 return rc; 1408 } 1409 1410 static int apmask_commit(unsigned long *newapm) 1411 { 1412 int rc; 1413 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)]; 1414 1415 /* 1416 * Check if any bits in the apmask have been set which will 1417 * result in queues being removed from non-default drivers 1418 */ 1419 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) { 1420 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, 1421 __verify_card_reservations); 1422 if (rc) 1423 return rc; 1424 } 1425 1426 memcpy(ap_perms.apm, newapm, APMASKSIZE); 1427 1428 return 0; 1429 } 1430 1431 static ssize_t apmask_store(const struct bus_type *bus, const char *buf, 1432 size_t count) 1433 { 1434 int rc, changes = 0; 1435 DECLARE_BITMAP(newapm, AP_DEVICES); 1436 1437 if (mutex_lock_interruptible(&ap_perms_mutex)) 1438 return -ERESTARTSYS; 1439 1440 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm); 1441 if (rc) 1442 goto done; 1443 1444 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE); 1445 if (changes) 1446 rc = apmask_commit(newapm); 1447 1448 done: 1449 mutex_unlock(&ap_perms_mutex); 1450 if (rc) 1451 return rc; 1452 1453 if (changes) { 1454 ap_bus_revise_bindings(); 1455 ap_send_mask_changed_uevent(newapm, NULL); 1456 } 1457 1458 return count; 1459 } 1460 1461 static BUS_ATTR_RW(apmask); 1462 1463 static ssize_t aqmask_show(const struct bus_type *bus, char *buf) 1464 { 1465 int rc; 1466 1467 if (mutex_lock_interruptible(&ap_perms_mutex)) 1468 return -ERESTARTSYS; 1469 rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n", 1470 ap_perms.aqm[0], ap_perms.aqm[1], 1471 ap_perms.aqm[2], ap_perms.aqm[3]); 1472 mutex_unlock(&ap_perms_mutex); 1473 1474 return rc; 1475 } 1476 1477 static int __verify_queue_reservations(struct device_driver *drv, void *data) 1478 { 1479 int rc = 0; 1480 struct ap_driver *ap_drv = to_ap_drv(drv); 1481 unsigned long *newaqm = (unsigned long *)data; 1482 1483 /* 1484 * increase the driver's module refcounter to be sure it is not 1485 * going away when we invoke the callback function. 1486 */ 1487 if (!try_module_get(drv->owner)) 1488 return 0; 1489 1490 if (ap_drv->in_use) { 1491 rc = ap_drv->in_use(ap_perms.apm, newaqm); 1492 if (rc) 1493 rc = -EBUSY; 1494 } 1495 1496 /* release the driver's module */ 1497 module_put(drv->owner); 1498 1499 return rc; 1500 } 1501 1502 static int aqmask_commit(unsigned long *newaqm) 1503 { 1504 int rc; 1505 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)]; 1506 1507 /* 1508 * Check if any bits in the aqmask have been set which will 1509 * result in queues being removed from non-default drivers 1510 */ 1511 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) { 1512 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, 1513 __verify_queue_reservations); 1514 if (rc) 1515 return rc; 1516 } 1517 1518 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE); 1519 1520 return 0; 1521 } 1522 1523 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf, 1524 size_t count) 1525 { 1526 int rc, changes = 0; 1527 DECLARE_BITMAP(newaqm, AP_DOMAINS); 1528 1529 if (mutex_lock_interruptible(&ap_perms_mutex)) 1530 return -ERESTARTSYS; 1531 1532 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm); 1533 if (rc) 1534 goto done; 1535 1536 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE); 1537 if (changes) 1538 rc = aqmask_commit(newaqm); 1539 1540 done: 1541 mutex_unlock(&ap_perms_mutex); 1542 if (rc) 1543 return rc; 1544 1545 if (changes) { 1546 ap_bus_revise_bindings(); 1547 ap_send_mask_changed_uevent(NULL, newaqm); 1548 } 1549 1550 return count; 1551 } 1552 1553 static BUS_ATTR_RW(aqmask); 1554 1555 static ssize_t scans_show(const struct bus_type *bus, char *buf) 1556 { 1557 return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count)); 1558 } 1559 1560 static ssize_t scans_store(const struct bus_type *bus, const char *buf, 1561 size_t count) 1562 { 1563 AP_DBF_INFO("%s force AP bus rescan\n", __func__); 1564 1565 ap_bus_force_rescan(); 1566 1567 return count; 1568 } 1569 1570 static BUS_ATTR_RW(scans); 1571 1572 static ssize_t bindings_show(const struct bus_type *bus, char *buf) 1573 { 1574 int rc; 1575 unsigned int apqns, n; 1576 1577 ap_calc_bound_apqns(&apqns, &n); 1578 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns) 1579 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns); 1580 else 1581 rc = sysfs_emit(buf, "%u/%u\n", n, apqns); 1582 1583 return rc; 1584 } 1585 1586 static BUS_ATTR_RO(bindings); 1587 1588 static ssize_t features_show(const struct bus_type *bus, char *buf) 1589 { 1590 int n = 0; 1591 1592 if (!ap_qci_info->flags) /* QCI not supported */ 1593 return sysfs_emit(buf, "-\n"); 1594 1595 if (ap_qci_info->apsc) 1596 n += sysfs_emit_at(buf, n, "APSC "); 1597 if (ap_qci_info->apxa) 1598 n += sysfs_emit_at(buf, n, "APXA "); 1599 if (ap_qci_info->qact) 1600 n += sysfs_emit_at(buf, n, "QACT "); 1601 if (ap_qci_info->rc8a) 1602 n += sysfs_emit_at(buf, n, "RC8A "); 1603 if (ap_qci_info->apsb) 1604 n += sysfs_emit_at(buf, n, "APSB "); 1605 1606 sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n"); 1607 1608 return n; 1609 } 1610 1611 static BUS_ATTR_RO(features); 1612 1613 static struct attribute *ap_bus_attrs[] = { 1614 &bus_attr_ap_domain.attr, 1615 &bus_attr_ap_control_domain_mask.attr, 1616 &bus_attr_ap_usage_domain_mask.attr, 1617 &bus_attr_ap_adapter_mask.attr, 1618 &bus_attr_config_time.attr, 1619 &bus_attr_poll_thread.attr, 1620 &bus_attr_ap_interrupts.attr, 1621 &bus_attr_poll_timeout.attr, 1622 &bus_attr_ap_max_domain_id.attr, 1623 &bus_attr_ap_max_adapter_id.attr, 1624 &bus_attr_apmask.attr, 1625 &bus_attr_aqmask.attr, 1626 &bus_attr_scans.attr, 1627 &bus_attr_bindings.attr, 1628 &bus_attr_features.attr, 1629 NULL, 1630 }; 1631 ATTRIBUTE_GROUPS(ap_bus); 1632 1633 static const struct bus_type ap_bus_type = { 1634 .name = "ap", 1635 .bus_groups = ap_bus_groups, 1636 .match = &ap_bus_match, 1637 .uevent = &ap_uevent, 1638 .probe = ap_device_probe, 1639 .remove = ap_device_remove, 1640 }; 1641 1642 /** 1643 * ap_select_domain(): Select an AP domain if possible and we haven't 1644 * already done so before. 1645 */ 1646 static void ap_select_domain(void) 1647 { 1648 struct ap_queue_status status; 1649 int card, dom; 1650 1651 /* 1652 * Choose the default domain. Either the one specified with 1653 * the "domain=" parameter or the first domain with at least 1654 * one valid APQN. 1655 */ 1656 spin_lock_bh(&ap_domain_lock); 1657 if (ap_domain_index >= 0) { 1658 /* Domain has already been selected. */ 1659 goto out; 1660 } 1661 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1662 if (!ap_test_config_usage_domain(dom) || 1663 !test_bit_inv(dom, ap_perms.aqm)) 1664 continue; 1665 for (card = 0; card <= ap_max_adapter_id; card++) { 1666 if (!ap_test_config_card_id(card) || 1667 !test_bit_inv(card, ap_perms.apm)) 1668 continue; 1669 status = ap_test_queue(AP_MKQID(card, dom), 1670 ap_apft_available(), 1671 NULL); 1672 if (status.response_code == AP_RESPONSE_NORMAL) 1673 break; 1674 } 1675 if (card <= ap_max_adapter_id) 1676 break; 1677 } 1678 if (dom <= ap_max_domain_id) { 1679 ap_domain_index = dom; 1680 AP_DBF_INFO("%s new default domain is %d\n", 1681 __func__, ap_domain_index); 1682 } 1683 out: 1684 spin_unlock_bh(&ap_domain_lock); 1685 } 1686 1687 /* 1688 * This function checks the type and returns either 0 for not 1689 * supported or the highest compatible type value (which may 1690 * include the input type value). 1691 */ 1692 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func) 1693 { 1694 int comp_type = 0; 1695 1696 /* < CEX4 is not supported */ 1697 if (rawtype < AP_DEVICE_TYPE_CEX4) { 1698 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n", 1699 __func__, AP_QID_CARD(qid), 1700 AP_QID_QUEUE(qid), rawtype); 1701 return 0; 1702 } 1703 /* up to CEX8 known and fully supported */ 1704 if (rawtype <= AP_DEVICE_TYPE_CEX8) 1705 return rawtype; 1706 /* 1707 * unknown new type > CEX8, check for compatibility 1708 * to the highest known and supported type which is 1709 * currently CEX8 with the help of the QACT function. 1710 */ 1711 if (ap_qact_available()) { 1712 struct ap_queue_status status; 1713 union ap_qact_ap_info apinfo = {0}; 1714 1715 apinfo.mode = (func >> 26) & 0x07; 1716 apinfo.cat = AP_DEVICE_TYPE_CEX8; 1717 status = ap_qact(qid, 0, &apinfo); 1718 if (status.response_code == AP_RESPONSE_NORMAL && 1719 apinfo.cat >= AP_DEVICE_TYPE_CEX4 && 1720 apinfo.cat <= AP_DEVICE_TYPE_CEX8) 1721 comp_type = apinfo.cat; 1722 } 1723 if (!comp_type) 1724 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n", 1725 __func__, AP_QID_CARD(qid), 1726 AP_QID_QUEUE(qid), rawtype); 1727 else if (comp_type != rawtype) 1728 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n", 1729 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid), 1730 rawtype, comp_type); 1731 return comp_type; 1732 } 1733 1734 /* 1735 * Helper function to be used with bus_find_dev 1736 * matches for the card device with the given id 1737 */ 1738 static int __match_card_device_with_id(struct device *dev, const void *data) 1739 { 1740 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data; 1741 } 1742 1743 /* 1744 * Helper function to be used with bus_find_dev 1745 * matches for the queue device with a given qid 1746 */ 1747 static int __match_queue_device_with_qid(struct device *dev, const void *data) 1748 { 1749 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data; 1750 } 1751 1752 /* 1753 * Helper function to be used with bus_find_dev 1754 * matches any queue device with given queue id 1755 */ 1756 static int __match_queue_device_with_queue_id(struct device *dev, const void *data) 1757 { 1758 return is_queue_dev(dev) && 1759 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data; 1760 } 1761 1762 /* Helper function for notify_config_changed */ 1763 static int __drv_notify_config_changed(struct device_driver *drv, void *data) 1764 { 1765 struct ap_driver *ap_drv = to_ap_drv(drv); 1766 1767 if (try_module_get(drv->owner)) { 1768 if (ap_drv->on_config_changed) 1769 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old); 1770 module_put(drv->owner); 1771 } 1772 1773 return 0; 1774 } 1775 1776 /* Notify all drivers about an qci config change */ 1777 static inline void notify_config_changed(void) 1778 { 1779 bus_for_each_drv(&ap_bus_type, NULL, NULL, 1780 __drv_notify_config_changed); 1781 } 1782 1783 /* Helper function for notify_scan_complete */ 1784 static int __drv_notify_scan_complete(struct device_driver *drv, void *data) 1785 { 1786 struct ap_driver *ap_drv = to_ap_drv(drv); 1787 1788 if (try_module_get(drv->owner)) { 1789 if (ap_drv->on_scan_complete) 1790 ap_drv->on_scan_complete(ap_qci_info, 1791 ap_qci_info_old); 1792 module_put(drv->owner); 1793 } 1794 1795 return 0; 1796 } 1797 1798 /* Notify all drivers about bus scan complete */ 1799 static inline void notify_scan_complete(void) 1800 { 1801 bus_for_each_drv(&ap_bus_type, NULL, NULL, 1802 __drv_notify_scan_complete); 1803 } 1804 1805 /* 1806 * Helper function for ap_scan_bus(). 1807 * Remove card device and associated queue devices. 1808 */ 1809 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac) 1810 { 1811 bus_for_each_dev(&ap_bus_type, NULL, 1812 (void *)(long)ac->id, 1813 __ap_queue_devices_with_id_unregister); 1814 device_unregister(&ac->ap_dev.device); 1815 } 1816 1817 /* 1818 * Helper function for ap_scan_bus(). 1819 * Does the scan bus job for all the domains within 1820 * a valid adapter given by an ap_card ptr. 1821 */ 1822 static inline void ap_scan_domains(struct ap_card *ac) 1823 { 1824 struct ap_tapq_hwinfo hwinfo; 1825 bool decfg, chkstop; 1826 struct ap_queue *aq; 1827 struct device *dev; 1828 ap_qid_t qid; 1829 int rc, dom; 1830 1831 /* 1832 * Go through the configuration for the domains and compare them 1833 * to the existing queue devices. Also take care of the config 1834 * and error state for the queue devices. 1835 */ 1836 1837 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1838 qid = AP_MKQID(ac->id, dom); 1839 dev = bus_find_device(&ap_bus_type, NULL, 1840 (void *)(long)qid, 1841 __match_queue_device_with_qid); 1842 aq = dev ? to_ap_queue(dev) : NULL; 1843 if (!ap_test_config_usage_domain(dom)) { 1844 if (dev) { 1845 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n", 1846 __func__, ac->id, dom); 1847 device_unregister(dev); 1848 } 1849 goto put_dev_and_continue; 1850 } 1851 /* domain is valid, get info from this APQN */ 1852 rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop); 1853 switch (rc) { 1854 case -1: 1855 if (dev) { 1856 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n", 1857 __func__, ac->id, dom); 1858 device_unregister(dev); 1859 } 1860 fallthrough; 1861 case 0: 1862 goto put_dev_and_continue; 1863 default: 1864 break; 1865 } 1866 /* if no queue device exists, create a new one */ 1867 if (!aq) { 1868 aq = ap_queue_create(qid, ac); 1869 if (!aq) { 1870 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n", 1871 __func__, ac->id, dom); 1872 continue; 1873 } 1874 aq->config = !decfg; 1875 aq->chkstop = chkstop; 1876 aq->se_bstate = hwinfo.bs; 1877 dev = &aq->ap_dev.device; 1878 dev->bus = &ap_bus_type; 1879 dev->parent = &ac->ap_dev.device; 1880 dev_set_name(dev, "%02x.%04x", ac->id, dom); 1881 /* register queue device */ 1882 rc = device_register(dev); 1883 if (rc) { 1884 AP_DBF_WARN("%s(%d,%d) device_register() failed\n", 1885 __func__, ac->id, dom); 1886 goto put_dev_and_continue; 1887 } 1888 /* get it and thus adjust reference counter */ 1889 get_device(dev); 1890 if (decfg) { 1891 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n", 1892 __func__, ac->id, dom); 1893 } else if (chkstop) { 1894 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n", 1895 __func__, ac->id, dom); 1896 } else { 1897 /* nudge the queue's state machine */ 1898 ap_queue_init_state(aq); 1899 AP_DBF_INFO("%s(%d,%d) new queue dev created\n", 1900 __func__, ac->id, dom); 1901 } 1902 goto put_dev_and_continue; 1903 } 1904 /* handle state changes on already existing queue device */ 1905 spin_lock_bh(&aq->lock); 1906 /* SE bind state */ 1907 aq->se_bstate = hwinfo.bs; 1908 /* checkstop state */ 1909 if (chkstop && !aq->chkstop) { 1910 /* checkstop on */ 1911 aq->chkstop = true; 1912 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1913 aq->dev_state = AP_DEV_STATE_ERROR; 1914 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED; 1915 } 1916 spin_unlock_bh(&aq->lock); 1917 pr_debug("(%d,%d) queue dev checkstop on\n", 1918 ac->id, dom); 1919 /* 'receive' pending messages with -EAGAIN */ 1920 ap_flush_queue(aq); 1921 goto put_dev_and_continue; 1922 } else if (!chkstop && aq->chkstop) { 1923 /* checkstop off */ 1924 aq->chkstop = false; 1925 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) 1926 _ap_queue_init_state(aq); 1927 spin_unlock_bh(&aq->lock); 1928 pr_debug("(%d,%d) queue dev checkstop off\n", 1929 ac->id, dom); 1930 goto put_dev_and_continue; 1931 } 1932 /* config state change */ 1933 if (decfg && aq->config) { 1934 /* config off this queue device */ 1935 aq->config = false; 1936 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1937 aq->dev_state = AP_DEV_STATE_ERROR; 1938 aq->last_err_rc = AP_RESPONSE_DECONFIGURED; 1939 } 1940 spin_unlock_bh(&aq->lock); 1941 pr_debug("(%d,%d) queue dev config off\n", 1942 ac->id, dom); 1943 ap_send_config_uevent(&aq->ap_dev, aq->config); 1944 /* 'receive' pending messages with -EAGAIN */ 1945 ap_flush_queue(aq); 1946 goto put_dev_and_continue; 1947 } else if (!decfg && !aq->config) { 1948 /* config on this queue device */ 1949 aq->config = true; 1950 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) 1951 _ap_queue_init_state(aq); 1952 spin_unlock_bh(&aq->lock); 1953 pr_debug("(%d,%d) queue dev config on\n", 1954 ac->id, dom); 1955 ap_send_config_uevent(&aq->ap_dev, aq->config); 1956 goto put_dev_and_continue; 1957 } 1958 /* handle other error states */ 1959 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) { 1960 spin_unlock_bh(&aq->lock); 1961 /* 'receive' pending messages with -EAGAIN */ 1962 ap_flush_queue(aq); 1963 /* re-init (with reset) the queue device */ 1964 ap_queue_init_state(aq); 1965 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n", 1966 __func__, ac->id, dom); 1967 goto put_dev_and_continue; 1968 } 1969 spin_unlock_bh(&aq->lock); 1970 put_dev_and_continue: 1971 put_device(dev); 1972 } 1973 } 1974 1975 /* 1976 * Helper function for ap_scan_bus(). 1977 * Does the scan bus job for the given adapter id. 1978 */ 1979 static inline void ap_scan_adapter(int ap) 1980 { 1981 struct ap_tapq_hwinfo hwinfo; 1982 int rc, dom, comp_type; 1983 bool decfg, chkstop; 1984 struct ap_card *ac; 1985 struct device *dev; 1986 ap_qid_t qid; 1987 1988 /* Is there currently a card device for this adapter ? */ 1989 dev = bus_find_device(&ap_bus_type, NULL, 1990 (void *)(long)ap, 1991 __match_card_device_with_id); 1992 ac = dev ? to_ap_card(dev) : NULL; 1993 1994 /* Adapter not in configuration ? */ 1995 if (!ap_test_config_card_id(ap)) { 1996 if (ac) { 1997 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n", 1998 __func__, ap); 1999 ap_scan_rm_card_dev_and_queue_devs(ac); 2000 put_device(dev); 2001 } 2002 return; 2003 } 2004 2005 /* 2006 * Adapter ap is valid in the current configuration. So do some checks: 2007 * If no card device exists, build one. If a card device exists, check 2008 * for type and functions changed. For all this we need to find a valid 2009 * APQN first. 2010 */ 2011 2012 for (dom = 0; dom <= ap_max_domain_id; dom++) 2013 if (ap_test_config_usage_domain(dom)) { 2014 qid = AP_MKQID(ap, dom); 2015 if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0) 2016 break; 2017 } 2018 if (dom > ap_max_domain_id) { 2019 /* Could not find one valid APQN for this adapter */ 2020 if (ac) { 2021 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n", 2022 __func__, ap); 2023 ap_scan_rm_card_dev_and_queue_devs(ac); 2024 put_device(dev); 2025 } else { 2026 pr_debug("(%d) no type info (no APQN found), ignored\n", 2027 ap); 2028 } 2029 return; 2030 } 2031 if (!hwinfo.at) { 2032 /* No apdater type info available, an unusable adapter */ 2033 if (ac) { 2034 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n", 2035 __func__, ap); 2036 ap_scan_rm_card_dev_and_queue_devs(ac); 2037 put_device(dev); 2038 } else { 2039 pr_debug("(%d) no valid type (0) info, ignored\n", ap); 2040 } 2041 return; 2042 } 2043 hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */ 2044 if (ac) { 2045 /* Check APQN against existing card device for changes */ 2046 if (ac->hwinfo.at != hwinfo.at) { 2047 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n", 2048 __func__, ap, hwinfo.at); 2049 ap_scan_rm_card_dev_and_queue_devs(ac); 2050 put_device(dev); 2051 ac = NULL; 2052 } else if (ac->hwinfo.fac != hwinfo.fac) { 2053 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n", 2054 __func__, ap, hwinfo.fac); 2055 ap_scan_rm_card_dev_and_queue_devs(ac); 2056 put_device(dev); 2057 ac = NULL; 2058 } else { 2059 /* handle checkstop state change */ 2060 if (chkstop && !ac->chkstop) { 2061 /* checkstop on */ 2062 ac->chkstop = true; 2063 AP_DBF_INFO("%s(%d) card dev checkstop on\n", 2064 __func__, ap); 2065 } else if (!chkstop && ac->chkstop) { 2066 /* checkstop off */ 2067 ac->chkstop = false; 2068 AP_DBF_INFO("%s(%d) card dev checkstop off\n", 2069 __func__, ap); 2070 } 2071 /* handle config state change */ 2072 if (decfg && ac->config) { 2073 ac->config = false; 2074 AP_DBF_INFO("%s(%d) card dev config off\n", 2075 __func__, ap); 2076 ap_send_config_uevent(&ac->ap_dev, ac->config); 2077 } else if (!decfg && !ac->config) { 2078 ac->config = true; 2079 AP_DBF_INFO("%s(%d) card dev config on\n", 2080 __func__, ap); 2081 ap_send_config_uevent(&ac->ap_dev, ac->config); 2082 } 2083 } 2084 } 2085 2086 if (!ac) { 2087 /* Build a new card device */ 2088 comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac); 2089 if (!comp_type) { 2090 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n", 2091 __func__, ap, hwinfo.at); 2092 return; 2093 } 2094 ac = ap_card_create(ap, hwinfo, comp_type); 2095 if (!ac) { 2096 AP_DBF_WARN("%s(%d) ap_card_create() failed\n", 2097 __func__, ap); 2098 return; 2099 } 2100 ac->config = !decfg; 2101 ac->chkstop = chkstop; 2102 dev = &ac->ap_dev.device; 2103 dev->bus = &ap_bus_type; 2104 dev->parent = ap_root_device; 2105 dev_set_name(dev, "card%02x", ap); 2106 /* maybe enlarge ap_max_msg_size to support this card */ 2107 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) { 2108 atomic_set(&ap_max_msg_size, ac->maxmsgsize); 2109 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n", 2110 __func__, ap, 2111 atomic_read(&ap_max_msg_size)); 2112 } 2113 /* Register the new card device with AP bus */ 2114 rc = device_register(dev); 2115 if (rc) { 2116 AP_DBF_WARN("%s(%d) device_register() failed\n", 2117 __func__, ap); 2118 put_device(dev); 2119 return; 2120 } 2121 /* get it and thus adjust reference counter */ 2122 get_device(dev); 2123 if (decfg) 2124 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n", 2125 __func__, ap, hwinfo.at, hwinfo.fac); 2126 else if (chkstop) 2127 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n", 2128 __func__, ap, hwinfo.at, hwinfo.fac); 2129 else 2130 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n", 2131 __func__, ap, hwinfo.at, hwinfo.fac); 2132 } 2133 2134 /* Verify the domains and the queue devices for this card */ 2135 ap_scan_domains(ac); 2136 2137 /* release the card device */ 2138 put_device(&ac->ap_dev.device); 2139 } 2140 2141 /** 2142 * ap_get_configuration - get the host AP configuration 2143 * 2144 * Stores the host AP configuration information returned from the previous call 2145 * to Query Configuration Information (QCI), then retrieves and stores the 2146 * current AP configuration returned from QCI. 2147 * 2148 * Return: true if the host AP configuration changed between calls to QCI; 2149 * otherwise, return false. 2150 */ 2151 static bool ap_get_configuration(void) 2152 { 2153 if (!ap_qci_info->flags) /* QCI not supported */ 2154 return false; 2155 2156 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info)); 2157 ap_qci(ap_qci_info); 2158 2159 return memcmp(ap_qci_info, ap_qci_info_old, 2160 sizeof(struct ap_config_info)) != 0; 2161 } 2162 2163 /* 2164 * ap_config_has_new_aps - Check current against old qci info if 2165 * new adapters have appeared. Returns true if at least one new 2166 * adapter in the apm mask is showing up. Existing adapters or 2167 * receding adapters are not counted. 2168 */ 2169 static bool ap_config_has_new_aps(void) 2170 { 2171 2172 unsigned long m[BITS_TO_LONGS(AP_DEVICES)]; 2173 2174 if (!ap_qci_info->flags) 2175 return false; 2176 2177 bitmap_andnot(m, (unsigned long *)ap_qci_info->apm, 2178 (unsigned long *)ap_qci_info_old->apm, AP_DEVICES); 2179 if (!bitmap_empty(m, AP_DEVICES)) 2180 return true; 2181 2182 return false; 2183 } 2184 2185 /* 2186 * ap_config_has_new_doms - Check current against old qci info if 2187 * new (usage) domains have appeared. Returns true if at least one 2188 * new domain in the aqm mask is showing up. Existing domains or 2189 * receding domains are not counted. 2190 */ 2191 static bool ap_config_has_new_doms(void) 2192 { 2193 unsigned long m[BITS_TO_LONGS(AP_DOMAINS)]; 2194 2195 if (!ap_qci_info->flags) 2196 return false; 2197 2198 bitmap_andnot(m, (unsigned long *)ap_qci_info->aqm, 2199 (unsigned long *)ap_qci_info_old->aqm, AP_DOMAINS); 2200 if (!bitmap_empty(m, AP_DOMAINS)) 2201 return true; 2202 2203 return false; 2204 } 2205 2206 /** 2207 * ap_scan_bus(): Scan the AP bus for new devices 2208 * Always run under mutex ap_scan_bus_mutex protection 2209 * which needs to get locked/unlocked by the caller! 2210 * Returns true if any config change has been detected 2211 * during the scan, otherwise false. 2212 */ 2213 static bool ap_scan_bus(void) 2214 { 2215 bool config_changed; 2216 int ap; 2217 2218 pr_debug(">\n"); 2219 2220 /* (re-)fetch configuration via QCI */ 2221 config_changed = ap_get_configuration(); 2222 if (config_changed) { 2223 if (ap_config_has_new_aps() || ap_config_has_new_doms()) { 2224 /* 2225 * Appearance of new adapters and/or domains need to 2226 * build new ap devices which need to get bound to an 2227 * device driver. Thus reset the APQN bindings complete 2228 * completion. 2229 */ 2230 reinit_completion(&ap_apqn_bindings_complete); 2231 } 2232 /* post a config change notify */ 2233 notify_config_changed(); 2234 } 2235 ap_select_domain(); 2236 2237 /* loop over all possible adapters */ 2238 for (ap = 0; ap <= ap_max_adapter_id; ap++) 2239 ap_scan_adapter(ap); 2240 2241 /* scan complete notify */ 2242 if (config_changed) 2243 notify_scan_complete(); 2244 2245 /* check if there is at least one queue available with default domain */ 2246 if (ap_domain_index >= 0) { 2247 struct device *dev = 2248 bus_find_device(&ap_bus_type, NULL, 2249 (void *)(long)ap_domain_index, 2250 __match_queue_device_with_queue_id); 2251 if (dev) 2252 put_device(dev); 2253 else 2254 AP_DBF_INFO("%s no queue device with default domain %d available\n", 2255 __func__, ap_domain_index); 2256 } 2257 2258 if (atomic64_inc_return(&ap_scan_bus_count) == 1) { 2259 pr_debug("init scan complete\n"); 2260 ap_send_init_scan_done_uevent(); 2261 } 2262 2263 ap_check_bindings_complete(); 2264 2265 mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ); 2266 2267 pr_debug("< config_changed=%d\n", config_changed); 2268 2269 return config_changed; 2270 } 2271 2272 /* 2273 * Callback for the ap_scan_bus_timer 2274 * Runs periodically, workqueue timer (ap_scan_bus_time) 2275 */ 2276 static void ap_scan_bus_timer_callback(struct timer_list *unused) 2277 { 2278 /* 2279 * schedule work into the system long wq which when 2280 * the work is finally executed, calls the AP bus scan. 2281 */ 2282 queue_work(system_long_wq, &ap_scan_bus_work); 2283 } 2284 2285 /* 2286 * Callback for the ap_scan_bus_work 2287 */ 2288 static void ap_scan_bus_wq_callback(struct work_struct *unused) 2289 { 2290 /* 2291 * Try to invoke an ap_scan_bus(). If the mutex acquisition 2292 * fails there is currently another task already running the 2293 * AP scan bus and there is no need to wait and re-trigger the 2294 * scan again. Please note at the end of the scan bus function 2295 * the AP scan bus timer is re-armed which triggers then the 2296 * ap_scan_bus_timer_callback which enqueues a work into the 2297 * system_long_wq which invokes this function here again. 2298 */ 2299 if (mutex_trylock(&ap_scan_bus_mutex)) { 2300 ap_scan_bus_task = current; 2301 ap_scan_bus_result = ap_scan_bus(); 2302 ap_scan_bus_task = NULL; 2303 mutex_unlock(&ap_scan_bus_mutex); 2304 } 2305 } 2306 2307 static inline void __exit ap_async_exit(void) 2308 { 2309 if (ap_thread_flag) 2310 ap_poll_thread_stop(); 2311 chsc_notifier_unregister(&ap_bus_nb); 2312 cancel_work(&ap_scan_bus_work); 2313 hrtimer_cancel(&ap_poll_timer); 2314 timer_delete(&ap_scan_bus_timer); 2315 } 2316 2317 static inline int __init ap_async_init(void) 2318 { 2319 int rc; 2320 2321 /* Setup the AP bus rescan timer. */ 2322 timer_setup(&ap_scan_bus_timer, ap_scan_bus_timer_callback, 0); 2323 2324 /* 2325 * Setup the high resolution poll timer. 2326 * If we are running under z/VM adjust polling to z/VM polling rate. 2327 */ 2328 if (machine_is_vm()) 2329 poll_high_timeout = 1500000; 2330 hrtimer_setup(&ap_poll_timer, ap_poll_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 2331 2332 queue_work(system_long_wq, &ap_scan_bus_work); 2333 2334 rc = chsc_notifier_register(&ap_bus_nb); 2335 if (rc) 2336 goto out; 2337 2338 /* Start the low priority AP bus poll thread. */ 2339 if (!ap_thread_flag) 2340 return 0; 2341 2342 rc = ap_poll_thread_start(); 2343 if (rc) 2344 goto out_notifier; 2345 2346 return 0; 2347 2348 out_notifier: 2349 chsc_notifier_unregister(&ap_bus_nb); 2350 out: 2351 cancel_work(&ap_scan_bus_work); 2352 hrtimer_cancel(&ap_poll_timer); 2353 timer_delete(&ap_scan_bus_timer); 2354 return rc; 2355 } 2356 2357 static inline void ap_irq_exit(void) 2358 { 2359 if (ap_irq_flag) 2360 unregister_adapter_interrupt(&ap_airq); 2361 } 2362 2363 static inline int __init ap_irq_init(void) 2364 { 2365 int rc; 2366 2367 if (!ap_interrupts_available() || !ap_useirq) 2368 return 0; 2369 2370 rc = register_adapter_interrupt(&ap_airq); 2371 ap_irq_flag = (rc == 0); 2372 2373 return rc; 2374 } 2375 2376 static inline void ap_debug_exit(void) 2377 { 2378 debug_unregister(ap_dbf_info); 2379 } 2380 2381 static inline int __init ap_debug_init(void) 2382 { 2383 ap_dbf_info = debug_register("ap", 2, 1, 2384 AP_DBF_MAX_SPRINTF_ARGS * sizeof(long)); 2385 debug_register_view(ap_dbf_info, &debug_sprintf_view); 2386 debug_set_level(ap_dbf_info, DBF_ERR); 2387 2388 return 0; 2389 } 2390 2391 static void __init ap_perms_init(void) 2392 { 2393 /* all resources usable if no kernel parameter string given */ 2394 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm)); 2395 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm)); 2396 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm)); 2397 2398 /* apm kernel parameter string */ 2399 if (apm_str) { 2400 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm)); 2401 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES, 2402 &ap_perms_mutex); 2403 } 2404 2405 /* aqm kernel parameter string */ 2406 if (aqm_str) { 2407 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm)); 2408 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS, 2409 &ap_perms_mutex); 2410 } 2411 } 2412 2413 /** 2414 * ap_module_init(): The module initialization code. 2415 * 2416 * Initializes the module. 2417 */ 2418 static int __init ap_module_init(void) 2419 { 2420 int rc; 2421 2422 rc = ap_debug_init(); 2423 if (rc) 2424 return rc; 2425 2426 if (!ap_instructions_available()) { 2427 pr_warn("The hardware system does not support AP instructions\n"); 2428 return -ENODEV; 2429 } 2430 2431 /* init ap_queue hashtable */ 2432 hash_init(ap_queues); 2433 2434 /* set up the AP permissions (ioctls, ap and aq masks) */ 2435 ap_perms_init(); 2436 2437 /* Get AP configuration data if available */ 2438 ap_init_qci_info(); 2439 2440 /* check default domain setting */ 2441 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id || 2442 (ap_domain_index >= 0 && 2443 !test_bit_inv(ap_domain_index, ap_perms.aqm))) { 2444 pr_warn("%d is not a valid cryptographic domain\n", 2445 ap_domain_index); 2446 ap_domain_index = -1; 2447 } 2448 2449 /* Create /sys/bus/ap. */ 2450 rc = bus_register(&ap_bus_type); 2451 if (rc) 2452 goto out; 2453 2454 /* Create /sys/devices/ap. */ 2455 ap_root_device = root_device_register("ap"); 2456 rc = PTR_ERR_OR_ZERO(ap_root_device); 2457 if (rc) 2458 goto out_bus; 2459 ap_root_device->bus = &ap_bus_type; 2460 2461 /* enable interrupts if available */ 2462 rc = ap_irq_init(); 2463 if (rc) 2464 goto out_device; 2465 2466 /* Setup asynchronous work (timers, workqueue, etc). */ 2467 rc = ap_async_init(); 2468 if (rc) 2469 goto out_irq; 2470 2471 return 0; 2472 2473 out_irq: 2474 ap_irq_exit(); 2475 out_device: 2476 root_device_unregister(ap_root_device); 2477 out_bus: 2478 bus_unregister(&ap_bus_type); 2479 out: 2480 ap_debug_exit(); 2481 return rc; 2482 } 2483 2484 static void __exit ap_module_exit(void) 2485 { 2486 ap_async_exit(); 2487 ap_irq_exit(); 2488 root_device_unregister(ap_root_device); 2489 bus_unregister(&ap_bus_type); 2490 ap_debug_exit(); 2491 } 2492 2493 module_init(ap_module_init); 2494 module_exit(ap_module_exit); 2495