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