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