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