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 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 static void ap_send_mask_changed_uevent(unsigned long *newapm, 697 unsigned long *newaqm) 698 { 699 char buf[100]; 700 char *envp[] = { buf, NULL }; 701 702 if (newapm) 703 snprintf(buf, sizeof(buf), 704 "APMASK=0x%016lx%016lx%016lx%016lx\n", 705 newapm[0], newapm[1], newapm[2], newapm[3]); 706 else 707 snprintf(buf, sizeof(buf), 708 "AQMASK=0x%016lx%016lx%016lx%016lx\n", 709 newaqm[0], newaqm[1], newaqm[2], newaqm[3]); 710 711 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); 712 } 713 714 /* 715 * calc # of bound APQNs 716 */ 717 718 struct __ap_calc_ctrs { 719 unsigned int apqns; 720 unsigned int bound; 721 }; 722 723 static int __ap_calc_helper(struct device *dev, void *arg) 724 { 725 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg; 726 727 if (is_queue_dev(dev)) { 728 pctrs->apqns++; 729 if (dev->driver) 730 pctrs->bound++; 731 } 732 733 return 0; 734 } 735 736 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound) 737 { 738 struct __ap_calc_ctrs ctrs; 739 740 memset(&ctrs, 0, sizeof(ctrs)); 741 bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper); 742 743 *apqns = ctrs.apqns; 744 *bound = ctrs.bound; 745 } 746 747 /* 748 * After initial ap bus scan do check if all existing APQNs are 749 * bound to device drivers. 750 */ 751 static void ap_check_bindings_complete(void) 752 { 753 unsigned int apqns, bound; 754 755 if (atomic64_read(&ap_scan_bus_count) >= 1) { 756 ap_calc_bound_apqns(&apqns, &bound); 757 if (bound == apqns) { 758 if (!completion_done(&ap_init_apqn_bindings_complete)) { 759 complete_all(&ap_init_apqn_bindings_complete); 760 AP_DBF_INFO("%s complete\n", __func__); 761 } 762 ap_send_bindings_complete_uevent(); 763 } 764 } 765 } 766 767 /* 768 * Interface to wait for the AP bus to have done one initial ap bus 769 * scan and all detected APQNs have been bound to device drivers. 770 * If these both conditions are not fulfilled, this function blocks 771 * on a condition with wait_for_completion_interruptible_timeout(). 772 * If these both conditions are fulfilled (before the timeout hits) 773 * the return value is 0. If the timeout (in jiffies) hits instead 774 * -ETIME is returned. On failures negative return values are 775 * returned to the caller. 776 */ 777 int ap_wait_init_apqn_bindings_complete(unsigned long timeout) 778 { 779 long l; 780 781 if (completion_done(&ap_init_apqn_bindings_complete)) 782 return 0; 783 784 if (timeout) 785 l = wait_for_completion_interruptible_timeout( 786 &ap_init_apqn_bindings_complete, timeout); 787 else 788 l = wait_for_completion_interruptible( 789 &ap_init_apqn_bindings_complete); 790 if (l < 0) 791 return l == -ERESTARTSYS ? -EINTR : l; 792 else if (l == 0 && timeout) 793 return -ETIME; 794 795 return 0; 796 } 797 EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete); 798 799 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) 800 { 801 if (is_queue_dev(dev) && 802 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data) 803 device_unregister(dev); 804 return 0; 805 } 806 807 static int __ap_revise_reserved(struct device *dev, void *dummy) 808 { 809 int rc, card, queue, devres, drvres; 810 811 if (is_queue_dev(dev)) { 812 card = AP_QID_CARD(to_ap_queue(dev)->qid); 813 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 814 mutex_lock(&ap_perms_mutex); 815 devres = test_bit_inv(card, ap_perms.apm) && 816 test_bit_inv(queue, ap_perms.aqm); 817 mutex_unlock(&ap_perms_mutex); 818 drvres = to_ap_drv(dev->driver)->flags 819 & AP_DRIVER_FLAG_DEFAULT; 820 if (!!devres != !!drvres) { 821 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n", 822 __func__, card, queue); 823 rc = device_reprobe(dev); 824 if (rc) 825 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n", 826 __func__, card, queue); 827 } 828 } 829 830 return 0; 831 } 832 833 static void ap_bus_revise_bindings(void) 834 { 835 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved); 836 } 837 838 /** 839 * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the 840 * default host driver or not. 841 * @card: the APID of the adapter card to check 842 * @queue: the APQI of the queue to check 843 * 844 * Note: the ap_perms_mutex must be locked by the caller of this function. 845 * 846 * Return: an int specifying whether the AP adapter is reserved for the host (1) 847 * or not (0). 848 */ 849 int ap_owned_by_def_drv(int card, int queue) 850 { 851 int rc = 0; 852 853 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS) 854 return -EINVAL; 855 856 if (test_bit_inv(card, ap_perms.apm) && 857 test_bit_inv(queue, ap_perms.aqm)) 858 rc = 1; 859 860 return rc; 861 } 862 EXPORT_SYMBOL(ap_owned_by_def_drv); 863 864 /** 865 * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in 866 * a set is reserved for the host drivers 867 * or not. 868 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check 869 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check 870 * 871 * Note: the ap_perms_mutex must be locked by the caller of this function. 872 * 873 * Return: an int specifying whether each APQN is reserved for the host (1) or 874 * not (0) 875 */ 876 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm, 877 unsigned long *aqm) 878 { 879 int card, queue, rc = 0; 880 881 for (card = 0; !rc && card < AP_DEVICES; card++) 882 if (test_bit_inv(card, apm) && 883 test_bit_inv(card, ap_perms.apm)) 884 for (queue = 0; !rc && queue < AP_DOMAINS; queue++) 885 if (test_bit_inv(queue, aqm) && 886 test_bit_inv(queue, ap_perms.aqm)) 887 rc = 1; 888 889 return rc; 890 } 891 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv); 892 893 static int ap_device_probe(struct device *dev) 894 { 895 struct ap_device *ap_dev = to_ap_dev(dev); 896 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 897 int card, queue, devres, drvres, rc = -ENODEV; 898 899 if (!get_device(dev)) 900 return rc; 901 902 if (is_queue_dev(dev)) { 903 /* 904 * If the apqn is marked as reserved/used by ap bus and 905 * default drivers, only probe with drivers with the default 906 * flag set. If it is not marked, only probe with drivers 907 * with the default flag not set. 908 */ 909 card = AP_QID_CARD(to_ap_queue(dev)->qid); 910 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 911 mutex_lock(&ap_perms_mutex); 912 devres = test_bit_inv(card, ap_perms.apm) && 913 test_bit_inv(queue, ap_perms.aqm); 914 mutex_unlock(&ap_perms_mutex); 915 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT; 916 if (!!devres != !!drvres) 917 goto out; 918 } 919 920 /* Add queue/card to list of active queues/cards */ 921 spin_lock_bh(&ap_queues_lock); 922 if (is_queue_dev(dev)) 923 hash_add(ap_queues, &to_ap_queue(dev)->hnode, 924 to_ap_queue(dev)->qid); 925 spin_unlock_bh(&ap_queues_lock); 926 927 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 928 929 if (rc) { 930 spin_lock_bh(&ap_queues_lock); 931 if (is_queue_dev(dev)) 932 hash_del(&to_ap_queue(dev)->hnode); 933 spin_unlock_bh(&ap_queues_lock); 934 } else { 935 ap_check_bindings_complete(); 936 } 937 938 out: 939 if (rc) 940 put_device(dev); 941 return rc; 942 } 943 944 static void ap_device_remove(struct device *dev) 945 { 946 struct ap_device *ap_dev = to_ap_dev(dev); 947 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 948 949 /* prepare ap queue device removal */ 950 if (is_queue_dev(dev)) 951 ap_queue_prepare_remove(to_ap_queue(dev)); 952 953 /* driver's chance to clean up gracefully */ 954 if (ap_drv->remove) 955 ap_drv->remove(ap_dev); 956 957 /* now do the ap queue device remove */ 958 if (is_queue_dev(dev)) 959 ap_queue_remove(to_ap_queue(dev)); 960 961 /* Remove queue/card from list of active queues/cards */ 962 spin_lock_bh(&ap_queues_lock); 963 if (is_queue_dev(dev)) 964 hash_del(&to_ap_queue(dev)->hnode); 965 spin_unlock_bh(&ap_queues_lock); 966 967 put_device(dev); 968 } 969 970 struct ap_queue *ap_get_qdev(ap_qid_t qid) 971 { 972 int bkt; 973 struct ap_queue *aq; 974 975 spin_lock_bh(&ap_queues_lock); 976 hash_for_each(ap_queues, bkt, aq, hnode) { 977 if (aq->qid == qid) { 978 get_device(&aq->ap_dev.device); 979 spin_unlock_bh(&ap_queues_lock); 980 return aq; 981 } 982 } 983 spin_unlock_bh(&ap_queues_lock); 984 985 return NULL; 986 } 987 EXPORT_SYMBOL(ap_get_qdev); 988 989 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 990 char *name) 991 { 992 struct device_driver *drv = &ap_drv->driver; 993 994 drv->bus = &ap_bus_type; 995 drv->owner = owner; 996 drv->name = name; 997 return driver_register(drv); 998 } 999 EXPORT_SYMBOL(ap_driver_register); 1000 1001 void ap_driver_unregister(struct ap_driver *ap_drv) 1002 { 1003 driver_unregister(&ap_drv->driver); 1004 } 1005 EXPORT_SYMBOL(ap_driver_unregister); 1006 1007 void ap_bus_force_rescan(void) 1008 { 1009 /* processing a asynchronous bus rescan */ 1010 del_timer(&ap_config_timer); 1011 queue_work(system_long_wq, &ap_scan_work); 1012 flush_work(&ap_scan_work); 1013 } 1014 EXPORT_SYMBOL(ap_bus_force_rescan); 1015 1016 /* 1017 * A config change has happened, force an ap bus rescan. 1018 */ 1019 void ap_bus_cfg_chg(void) 1020 { 1021 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__); 1022 1023 ap_bus_force_rescan(); 1024 } 1025 1026 /* 1027 * hex2bitmap() - parse hex mask string and set bitmap. 1028 * Valid strings are "0x012345678" with at least one valid hex number. 1029 * Rest of the bitmap to the right is padded with 0. No spaces allowed 1030 * within the string, the leading 0x may be omitted. 1031 * Returns the bitmask with exactly the bits set as given by the hex 1032 * string (both in big endian order). 1033 */ 1034 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits) 1035 { 1036 int i, n, b; 1037 1038 /* bits needs to be a multiple of 8 */ 1039 if (bits & 0x07) 1040 return -EINVAL; 1041 1042 if (str[0] == '0' && str[1] == 'x') 1043 str++; 1044 if (*str == 'x') 1045 str++; 1046 1047 for (i = 0; isxdigit(*str) && i < bits; str++) { 1048 b = hex_to_bin(*str); 1049 for (n = 0; n < 4; n++) 1050 if (b & (0x08 >> n)) 1051 set_bit_inv(i + n, bitmap); 1052 i += 4; 1053 } 1054 1055 if (*str == '\n') 1056 str++; 1057 if (*str) 1058 return -EINVAL; 1059 return 0; 1060 } 1061 1062 /* 1063 * modify_bitmap() - parse bitmask argument and modify an existing 1064 * bit mask accordingly. A concatenation (done with ',') of these 1065 * terms is recognized: 1066 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>] 1067 * <bitnr> may be any valid number (hex, decimal or octal) in the range 1068 * 0...bits-1; the leading + or - is required. Here are some examples: 1069 * +0-15,+32,-128,-0xFF 1070 * -0-255,+1-16,+0x128 1071 * +1,+2,+3,+4,-5,-7-10 1072 * Returns the new bitmap after all changes have been applied. Every 1073 * positive value in the string will set a bit and every negative value 1074 * in the string will clear a bit. As a bit may be touched more than once, 1075 * the last 'operation' wins: 1076 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be 1077 * cleared again. All other bits are unmodified. 1078 */ 1079 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits) 1080 { 1081 int a, i, z; 1082 char *np, sign; 1083 1084 /* bits needs to be a multiple of 8 */ 1085 if (bits & 0x07) 1086 return -EINVAL; 1087 1088 while (*str) { 1089 sign = *str++; 1090 if (sign != '+' && sign != '-') 1091 return -EINVAL; 1092 a = z = simple_strtoul(str, &np, 0); 1093 if (str == np || a >= bits) 1094 return -EINVAL; 1095 str = np; 1096 if (*str == '-') { 1097 z = simple_strtoul(++str, &np, 0); 1098 if (str == np || a > z || z >= bits) 1099 return -EINVAL; 1100 str = np; 1101 } 1102 for (i = a; i <= z; i++) 1103 if (sign == '+') 1104 set_bit_inv(i, bitmap); 1105 else 1106 clear_bit_inv(i, bitmap); 1107 while (*str == ',' || *str == '\n') 1108 str++; 1109 } 1110 1111 return 0; 1112 } 1113 1114 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits, 1115 unsigned long *newmap) 1116 { 1117 unsigned long size; 1118 int rc; 1119 1120 size = BITS_TO_LONGS(bits) * sizeof(unsigned long); 1121 if (*str == '+' || *str == '-') { 1122 memcpy(newmap, bitmap, size); 1123 rc = modify_bitmap(str, newmap, bits); 1124 } else { 1125 memset(newmap, 0, size); 1126 rc = hex2bitmap(str, newmap, bits); 1127 } 1128 return rc; 1129 } 1130 1131 int ap_parse_mask_str(const char *str, 1132 unsigned long *bitmap, int bits, 1133 struct mutex *lock) 1134 { 1135 unsigned long *newmap, size; 1136 int rc; 1137 1138 /* bits needs to be a multiple of 8 */ 1139 if (bits & 0x07) 1140 return -EINVAL; 1141 1142 size = BITS_TO_LONGS(bits) * sizeof(unsigned long); 1143 newmap = kmalloc(size, GFP_KERNEL); 1144 if (!newmap) 1145 return -ENOMEM; 1146 if (mutex_lock_interruptible(lock)) { 1147 kfree(newmap); 1148 return -ERESTARTSYS; 1149 } 1150 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap); 1151 if (rc == 0) 1152 memcpy(bitmap, newmap, size); 1153 mutex_unlock(lock); 1154 kfree(newmap); 1155 return rc; 1156 } 1157 EXPORT_SYMBOL(ap_parse_mask_str); 1158 1159 /* 1160 * AP bus attributes. 1161 */ 1162 1163 static ssize_t ap_domain_show(struct bus_type *bus, char *buf) 1164 { 1165 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); 1166 } 1167 1168 static ssize_t ap_domain_store(struct bus_type *bus, 1169 const char *buf, size_t count) 1170 { 1171 int domain; 1172 1173 if (sscanf(buf, "%i\n", &domain) != 1 || 1174 domain < 0 || domain > ap_max_domain_id || 1175 !test_bit_inv(domain, ap_perms.aqm)) 1176 return -EINVAL; 1177 1178 spin_lock_bh(&ap_domain_lock); 1179 ap_domain_index = domain; 1180 spin_unlock_bh(&ap_domain_lock); 1181 1182 AP_DBF_INFO("%s stored new default domain=%d\n", 1183 __func__, domain); 1184 1185 return count; 1186 } 1187 1188 static BUS_ATTR_RW(ap_domain); 1189 1190 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) 1191 { 1192 if (!ap_qci_info) /* QCI not supported */ 1193 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 1194 1195 return scnprintf(buf, PAGE_SIZE, 1196 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1197 ap_qci_info->adm[0], ap_qci_info->adm[1], 1198 ap_qci_info->adm[2], ap_qci_info->adm[3], 1199 ap_qci_info->adm[4], ap_qci_info->adm[5], 1200 ap_qci_info->adm[6], ap_qci_info->adm[7]); 1201 } 1202 1203 static BUS_ATTR_RO(ap_control_domain_mask); 1204 1205 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf) 1206 { 1207 if (!ap_qci_info) /* QCI not supported */ 1208 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 1209 1210 return scnprintf(buf, PAGE_SIZE, 1211 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1212 ap_qci_info->aqm[0], ap_qci_info->aqm[1], 1213 ap_qci_info->aqm[2], ap_qci_info->aqm[3], 1214 ap_qci_info->aqm[4], ap_qci_info->aqm[5], 1215 ap_qci_info->aqm[6], ap_qci_info->aqm[7]); 1216 } 1217 1218 static BUS_ATTR_RO(ap_usage_domain_mask); 1219 1220 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf) 1221 { 1222 if (!ap_qci_info) /* QCI not supported */ 1223 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 1224 1225 return scnprintf(buf, PAGE_SIZE, 1226 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1227 ap_qci_info->apm[0], ap_qci_info->apm[1], 1228 ap_qci_info->apm[2], ap_qci_info->apm[3], 1229 ap_qci_info->apm[4], ap_qci_info->apm[5], 1230 ap_qci_info->apm[6], ap_qci_info->apm[7]); 1231 } 1232 1233 static BUS_ATTR_RO(ap_adapter_mask); 1234 1235 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) 1236 { 1237 return scnprintf(buf, PAGE_SIZE, "%d\n", 1238 ap_irq_flag ? 1 : 0); 1239 } 1240 1241 static BUS_ATTR_RO(ap_interrupts); 1242 1243 static ssize_t config_time_show(struct bus_type *bus, char *buf) 1244 { 1245 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); 1246 } 1247 1248 static ssize_t config_time_store(struct bus_type *bus, 1249 const char *buf, size_t count) 1250 { 1251 int time; 1252 1253 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 1254 return -EINVAL; 1255 ap_config_time = time; 1256 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 1257 return count; 1258 } 1259 1260 static BUS_ATTR_RW(config_time); 1261 1262 static ssize_t poll_thread_show(struct bus_type *bus, char *buf) 1263 { 1264 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); 1265 } 1266 1267 static ssize_t poll_thread_store(struct bus_type *bus, 1268 const char *buf, size_t count) 1269 { 1270 int flag, rc; 1271 1272 if (sscanf(buf, "%d\n", &flag) != 1) 1273 return -EINVAL; 1274 if (flag) { 1275 rc = ap_poll_thread_start(); 1276 if (rc) 1277 count = rc; 1278 } else { 1279 ap_poll_thread_stop(); 1280 } 1281 return count; 1282 } 1283 1284 static BUS_ATTR_RW(poll_thread); 1285 1286 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) 1287 { 1288 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); 1289 } 1290 1291 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, 1292 size_t count) 1293 { 1294 unsigned long long time; 1295 ktime_t hr_time; 1296 1297 /* 120 seconds = maximum poll interval */ 1298 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || 1299 time > 120000000000ULL) 1300 return -EINVAL; 1301 poll_timeout = time; 1302 hr_time = poll_timeout; 1303 1304 spin_lock_bh(&ap_poll_timer_lock); 1305 hrtimer_cancel(&ap_poll_timer); 1306 hrtimer_set_expires(&ap_poll_timer, hr_time); 1307 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); 1308 spin_unlock_bh(&ap_poll_timer_lock); 1309 1310 return count; 1311 } 1312 1313 static BUS_ATTR_RW(poll_timeout); 1314 1315 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) 1316 { 1317 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id); 1318 } 1319 1320 static BUS_ATTR_RO(ap_max_domain_id); 1321 1322 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf) 1323 { 1324 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id); 1325 } 1326 1327 static BUS_ATTR_RO(ap_max_adapter_id); 1328 1329 static ssize_t apmask_show(struct bus_type *bus, char *buf) 1330 { 1331 int rc; 1332 1333 if (mutex_lock_interruptible(&ap_perms_mutex)) 1334 return -ERESTARTSYS; 1335 rc = scnprintf(buf, PAGE_SIZE, 1336 "0x%016lx%016lx%016lx%016lx\n", 1337 ap_perms.apm[0], ap_perms.apm[1], 1338 ap_perms.apm[2], ap_perms.apm[3]); 1339 mutex_unlock(&ap_perms_mutex); 1340 1341 return rc; 1342 } 1343 1344 static int __verify_card_reservations(struct device_driver *drv, void *data) 1345 { 1346 int rc = 0; 1347 struct ap_driver *ap_drv = to_ap_drv(drv); 1348 unsigned long *newapm = (unsigned long *)data; 1349 1350 /* 1351 * increase the driver's module refcounter to be sure it is not 1352 * going away when we invoke the callback function. 1353 */ 1354 if (!try_module_get(drv->owner)) 1355 return 0; 1356 1357 if (ap_drv->in_use) { 1358 rc = ap_drv->in_use(newapm, ap_perms.aqm); 1359 if (rc) 1360 rc = -EBUSY; 1361 } 1362 1363 /* release the driver's module */ 1364 module_put(drv->owner); 1365 1366 return rc; 1367 } 1368 1369 static int apmask_commit(unsigned long *newapm) 1370 { 1371 int rc; 1372 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)]; 1373 1374 /* 1375 * Check if any bits in the apmask have been set which will 1376 * result in queues being removed from non-default drivers 1377 */ 1378 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) { 1379 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, 1380 __verify_card_reservations); 1381 if (rc) 1382 return rc; 1383 } 1384 1385 memcpy(ap_perms.apm, newapm, APMASKSIZE); 1386 1387 return 0; 1388 } 1389 1390 static ssize_t apmask_store(struct bus_type *bus, const char *buf, 1391 size_t count) 1392 { 1393 int rc, changes = 0; 1394 DECLARE_BITMAP(newapm, AP_DEVICES); 1395 1396 if (mutex_lock_interruptible(&ap_perms_mutex)) 1397 return -ERESTARTSYS; 1398 1399 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm); 1400 if (rc) 1401 goto done; 1402 1403 changes = memcmp(ap_perms.apm, newapm, APMASKSIZE); 1404 if (changes) 1405 rc = apmask_commit(newapm); 1406 1407 done: 1408 mutex_unlock(&ap_perms_mutex); 1409 if (rc) 1410 return rc; 1411 1412 if (changes) { 1413 ap_bus_revise_bindings(); 1414 ap_send_mask_changed_uevent(newapm, NULL); 1415 } 1416 1417 return count; 1418 } 1419 1420 static BUS_ATTR_RW(apmask); 1421 1422 static ssize_t aqmask_show(struct bus_type *bus, char *buf) 1423 { 1424 int rc; 1425 1426 if (mutex_lock_interruptible(&ap_perms_mutex)) 1427 return -ERESTARTSYS; 1428 rc = scnprintf(buf, PAGE_SIZE, 1429 "0x%016lx%016lx%016lx%016lx\n", 1430 ap_perms.aqm[0], ap_perms.aqm[1], 1431 ap_perms.aqm[2], ap_perms.aqm[3]); 1432 mutex_unlock(&ap_perms_mutex); 1433 1434 return rc; 1435 } 1436 1437 static int __verify_queue_reservations(struct device_driver *drv, void *data) 1438 { 1439 int rc = 0; 1440 struct ap_driver *ap_drv = to_ap_drv(drv); 1441 unsigned long *newaqm = (unsigned long *)data; 1442 1443 /* 1444 * increase the driver's module refcounter to be sure it is not 1445 * going away when we invoke the callback function. 1446 */ 1447 if (!try_module_get(drv->owner)) 1448 return 0; 1449 1450 if (ap_drv->in_use) { 1451 rc = ap_drv->in_use(ap_perms.apm, newaqm); 1452 if (rc) 1453 return -EBUSY; 1454 } 1455 1456 /* release the driver's module */ 1457 module_put(drv->owner); 1458 1459 return rc; 1460 } 1461 1462 static int aqmask_commit(unsigned long *newaqm) 1463 { 1464 int rc; 1465 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)]; 1466 1467 /* 1468 * Check if any bits in the aqmask have been set which will 1469 * result in queues being removed from non-default drivers 1470 */ 1471 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) { 1472 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, 1473 __verify_queue_reservations); 1474 if (rc) 1475 return rc; 1476 } 1477 1478 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE); 1479 1480 return 0; 1481 } 1482 1483 static ssize_t aqmask_store(struct bus_type *bus, const char *buf, 1484 size_t count) 1485 { 1486 int rc, changes = 0; 1487 DECLARE_BITMAP(newaqm, AP_DOMAINS); 1488 1489 if (mutex_lock_interruptible(&ap_perms_mutex)) 1490 return -ERESTARTSYS; 1491 1492 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm); 1493 if (rc) 1494 goto done; 1495 1496 changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE); 1497 if (changes) 1498 rc = aqmask_commit(newaqm); 1499 1500 done: 1501 mutex_unlock(&ap_perms_mutex); 1502 if (rc) 1503 return rc; 1504 1505 if (changes) { 1506 ap_bus_revise_bindings(); 1507 ap_send_mask_changed_uevent(NULL, newaqm); 1508 } 1509 1510 return count; 1511 } 1512 1513 static BUS_ATTR_RW(aqmask); 1514 1515 static ssize_t scans_show(struct bus_type *bus, char *buf) 1516 { 1517 return scnprintf(buf, PAGE_SIZE, "%llu\n", 1518 atomic64_read(&ap_scan_bus_count)); 1519 } 1520 1521 static ssize_t scans_store(struct bus_type *bus, const char *buf, 1522 size_t count) 1523 { 1524 AP_DBF_INFO("%s force AP bus rescan\n", __func__); 1525 1526 ap_bus_force_rescan(); 1527 1528 return count; 1529 } 1530 1531 static BUS_ATTR_RW(scans); 1532 1533 static ssize_t bindings_show(struct bus_type *bus, char *buf) 1534 { 1535 int rc; 1536 unsigned int apqns, n; 1537 1538 ap_calc_bound_apqns(&apqns, &n); 1539 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns) 1540 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns); 1541 else 1542 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns); 1543 1544 return rc; 1545 } 1546 1547 static BUS_ATTR_RO(bindings); 1548 1549 static struct attribute *ap_bus_attrs[] = { 1550 &bus_attr_ap_domain.attr, 1551 &bus_attr_ap_control_domain_mask.attr, 1552 &bus_attr_ap_usage_domain_mask.attr, 1553 &bus_attr_ap_adapter_mask.attr, 1554 &bus_attr_config_time.attr, 1555 &bus_attr_poll_thread.attr, 1556 &bus_attr_ap_interrupts.attr, 1557 &bus_attr_poll_timeout.attr, 1558 &bus_attr_ap_max_domain_id.attr, 1559 &bus_attr_ap_max_adapter_id.attr, 1560 &bus_attr_apmask.attr, 1561 &bus_attr_aqmask.attr, 1562 &bus_attr_scans.attr, 1563 &bus_attr_bindings.attr, 1564 NULL, 1565 }; 1566 ATTRIBUTE_GROUPS(ap_bus); 1567 1568 static struct bus_type ap_bus_type = { 1569 .name = "ap", 1570 .bus_groups = ap_bus_groups, 1571 .match = &ap_bus_match, 1572 .uevent = &ap_uevent, 1573 .probe = ap_device_probe, 1574 .remove = ap_device_remove, 1575 }; 1576 1577 /** 1578 * ap_select_domain(): Select an AP domain if possible and we haven't 1579 * already done so before. 1580 */ 1581 static void ap_select_domain(void) 1582 { 1583 struct ap_queue_status status; 1584 int card, dom; 1585 1586 /* 1587 * Choose the default domain. Either the one specified with 1588 * the "domain=" parameter or the first domain with at least 1589 * one valid APQN. 1590 */ 1591 spin_lock_bh(&ap_domain_lock); 1592 if (ap_domain_index >= 0) { 1593 /* Domain has already been selected. */ 1594 goto out; 1595 } 1596 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1597 if (!ap_test_config_usage_domain(dom) || 1598 !test_bit_inv(dom, ap_perms.aqm)) 1599 continue; 1600 for (card = 0; card <= ap_max_adapter_id; card++) { 1601 if (!ap_test_config_card_id(card) || 1602 !test_bit_inv(card, ap_perms.apm)) 1603 continue; 1604 status = ap_test_queue(AP_MKQID(card, dom), 1605 ap_apft_available(), 1606 NULL); 1607 if (status.response_code == AP_RESPONSE_NORMAL) 1608 break; 1609 } 1610 if (card <= ap_max_adapter_id) 1611 break; 1612 } 1613 if (dom <= ap_max_domain_id) { 1614 ap_domain_index = dom; 1615 AP_DBF_INFO("%s new default domain is %d\n", 1616 __func__, ap_domain_index); 1617 } 1618 out: 1619 spin_unlock_bh(&ap_domain_lock); 1620 } 1621 1622 /* 1623 * This function checks the type and returns either 0 for not 1624 * supported or the highest compatible type value (which may 1625 * include the input type value). 1626 */ 1627 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func) 1628 { 1629 int comp_type = 0; 1630 1631 /* < CEX2A is not supported */ 1632 if (rawtype < AP_DEVICE_TYPE_CEX2A) { 1633 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n", 1634 __func__, AP_QID_CARD(qid), 1635 AP_QID_QUEUE(qid), rawtype); 1636 return 0; 1637 } 1638 /* up to CEX8 known and fully supported */ 1639 if (rawtype <= AP_DEVICE_TYPE_CEX8) 1640 return rawtype; 1641 /* 1642 * unknown new type > CEX8, check for compatibility 1643 * to the highest known and supported type which is 1644 * currently CEX8 with the help of the QACT function. 1645 */ 1646 if (ap_qact_available()) { 1647 struct ap_queue_status status; 1648 union ap_qact_ap_info apinfo = {0}; 1649 1650 apinfo.mode = (func >> 26) & 0x07; 1651 apinfo.cat = AP_DEVICE_TYPE_CEX8; 1652 status = ap_qact(qid, 0, &apinfo); 1653 if (status.response_code == AP_RESPONSE_NORMAL && 1654 apinfo.cat >= AP_DEVICE_TYPE_CEX2A && 1655 apinfo.cat <= AP_DEVICE_TYPE_CEX8) 1656 comp_type = apinfo.cat; 1657 } 1658 if (!comp_type) 1659 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n", 1660 __func__, AP_QID_CARD(qid), 1661 AP_QID_QUEUE(qid), rawtype); 1662 else if (comp_type != rawtype) 1663 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n", 1664 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid), 1665 rawtype, comp_type); 1666 return comp_type; 1667 } 1668 1669 /* 1670 * Helper function to be used with bus_find_dev 1671 * matches for the card device with the given id 1672 */ 1673 static int __match_card_device_with_id(struct device *dev, const void *data) 1674 { 1675 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data; 1676 } 1677 1678 /* 1679 * Helper function to be used with bus_find_dev 1680 * matches for the queue device with a given qid 1681 */ 1682 static int __match_queue_device_with_qid(struct device *dev, const void *data) 1683 { 1684 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data; 1685 } 1686 1687 /* 1688 * Helper function to be used with bus_find_dev 1689 * matches any queue device with given queue id 1690 */ 1691 static int __match_queue_device_with_queue_id(struct device *dev, const void *data) 1692 { 1693 return is_queue_dev(dev) && 1694 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data; 1695 } 1696 1697 /* Helper function for notify_config_changed */ 1698 static int __drv_notify_config_changed(struct device_driver *drv, void *data) 1699 { 1700 struct ap_driver *ap_drv = to_ap_drv(drv); 1701 1702 if (try_module_get(drv->owner)) { 1703 if (ap_drv->on_config_changed) 1704 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old); 1705 module_put(drv->owner); 1706 } 1707 1708 return 0; 1709 } 1710 1711 /* Notify all drivers about an qci config change */ 1712 static inline void notify_config_changed(void) 1713 { 1714 bus_for_each_drv(&ap_bus_type, NULL, NULL, 1715 __drv_notify_config_changed); 1716 } 1717 1718 /* Helper function for notify_scan_complete */ 1719 static int __drv_notify_scan_complete(struct device_driver *drv, void *data) 1720 { 1721 struct ap_driver *ap_drv = to_ap_drv(drv); 1722 1723 if (try_module_get(drv->owner)) { 1724 if (ap_drv->on_scan_complete) 1725 ap_drv->on_scan_complete(ap_qci_info, 1726 ap_qci_info_old); 1727 module_put(drv->owner); 1728 } 1729 1730 return 0; 1731 } 1732 1733 /* Notify all drivers about bus scan complete */ 1734 static inline void notify_scan_complete(void) 1735 { 1736 bus_for_each_drv(&ap_bus_type, NULL, NULL, 1737 __drv_notify_scan_complete); 1738 } 1739 1740 /* 1741 * Helper function for ap_scan_bus(). 1742 * Remove card device and associated queue devices. 1743 */ 1744 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac) 1745 { 1746 bus_for_each_dev(&ap_bus_type, NULL, 1747 (void *)(long)ac->id, 1748 __ap_queue_devices_with_id_unregister); 1749 device_unregister(&ac->ap_dev.device); 1750 } 1751 1752 /* 1753 * Helper function for ap_scan_bus(). 1754 * Does the scan bus job for all the domains within 1755 * a valid adapter given by an ap_card ptr. 1756 */ 1757 static inline void ap_scan_domains(struct ap_card *ac) 1758 { 1759 bool decfg, chkstop; 1760 ap_qid_t qid; 1761 unsigned int func; 1762 struct device *dev; 1763 struct ap_queue *aq; 1764 int rc, dom, depth, type, ml; 1765 1766 /* 1767 * Go through the configuration for the domains and compare them 1768 * to the existing queue devices. Also take care of the config 1769 * and error state for the queue devices. 1770 */ 1771 1772 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1773 qid = AP_MKQID(ac->id, dom); 1774 dev = bus_find_device(&ap_bus_type, NULL, 1775 (void *)(long)qid, 1776 __match_queue_device_with_qid); 1777 aq = dev ? to_ap_queue(dev) : NULL; 1778 if (!ap_test_config_usage_domain(dom)) { 1779 if (dev) { 1780 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n", 1781 __func__, ac->id, dom); 1782 device_unregister(dev); 1783 put_device(dev); 1784 } 1785 continue; 1786 } 1787 /* domain is valid, get info from this APQN */ 1788 if (!ap_queue_info(qid, &type, &func, &depth, 1789 &ml, &decfg, &chkstop)) { 1790 if (aq) { 1791 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n", 1792 __func__, ac->id, dom); 1793 device_unregister(dev); 1794 put_device(dev); 1795 } 1796 continue; 1797 } 1798 /* if no queue device exists, create a new one */ 1799 if (!aq) { 1800 aq = ap_queue_create(qid, ac->ap_dev.device_type); 1801 if (!aq) { 1802 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n", 1803 __func__, ac->id, dom); 1804 continue; 1805 } 1806 aq->card = ac; 1807 aq->config = !decfg; 1808 aq->chkstop = chkstop; 1809 dev = &aq->ap_dev.device; 1810 dev->bus = &ap_bus_type; 1811 dev->parent = &ac->ap_dev.device; 1812 dev_set_name(dev, "%02x.%04x", ac->id, dom); 1813 /* register queue device */ 1814 rc = device_register(dev); 1815 if (rc) { 1816 AP_DBF_WARN("%s(%d,%d) device_register() failed\n", 1817 __func__, ac->id, dom); 1818 goto put_dev_and_continue; 1819 } 1820 /* get it and thus adjust reference counter */ 1821 get_device(dev); 1822 if (decfg) 1823 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n", 1824 __func__, ac->id, dom); 1825 else if (chkstop) 1826 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n", 1827 __func__, ac->id, dom); 1828 else 1829 AP_DBF_INFO("%s(%d,%d) new queue dev created\n", 1830 __func__, ac->id, dom); 1831 goto put_dev_and_continue; 1832 } 1833 /* handle state changes on already existing queue device */ 1834 spin_lock_bh(&aq->lock); 1835 /* checkstop state */ 1836 if (chkstop && !aq->chkstop) { 1837 /* checkstop on */ 1838 aq->chkstop = true; 1839 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1840 aq->dev_state = AP_DEV_STATE_ERROR; 1841 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED; 1842 } 1843 spin_unlock_bh(&aq->lock); 1844 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n", 1845 __func__, ac->id, dom); 1846 /* 'receive' pending messages with -EAGAIN */ 1847 ap_flush_queue(aq); 1848 goto put_dev_and_continue; 1849 } else if (!chkstop && aq->chkstop) { 1850 /* checkstop off */ 1851 aq->chkstop = false; 1852 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1853 aq->dev_state = AP_DEV_STATE_OPERATING; 1854 aq->sm_state = AP_SM_STATE_RESET_START; 1855 } 1856 spin_unlock_bh(&aq->lock); 1857 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n", 1858 __func__, ac->id, dom); 1859 goto put_dev_and_continue; 1860 } 1861 /* config state change */ 1862 if (decfg && aq->config) { 1863 /* config off this queue device */ 1864 aq->config = false; 1865 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1866 aq->dev_state = AP_DEV_STATE_ERROR; 1867 aq->last_err_rc = AP_RESPONSE_DECONFIGURED; 1868 } 1869 spin_unlock_bh(&aq->lock); 1870 AP_DBF_DBG("%s(%d,%d) queue dev config off\n", 1871 __func__, ac->id, dom); 1872 ap_send_config_uevent(&aq->ap_dev, aq->config); 1873 /* 'receive' pending messages with -EAGAIN */ 1874 ap_flush_queue(aq); 1875 goto put_dev_and_continue; 1876 } else if (!decfg && !aq->config) { 1877 /* config on this queue device */ 1878 aq->config = true; 1879 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1880 aq->dev_state = AP_DEV_STATE_OPERATING; 1881 aq->sm_state = AP_SM_STATE_RESET_START; 1882 } 1883 spin_unlock_bh(&aq->lock); 1884 AP_DBF_DBG("%s(%d,%d) queue dev config on\n", 1885 __func__, ac->id, dom); 1886 ap_send_config_uevent(&aq->ap_dev, aq->config); 1887 goto put_dev_and_continue; 1888 } 1889 /* handle other error states */ 1890 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) { 1891 spin_unlock_bh(&aq->lock); 1892 /* 'receive' pending messages with -EAGAIN */ 1893 ap_flush_queue(aq); 1894 /* re-init (with reset) the queue device */ 1895 ap_queue_init_state(aq); 1896 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n", 1897 __func__, ac->id, dom); 1898 goto put_dev_and_continue; 1899 } 1900 spin_unlock_bh(&aq->lock); 1901 put_dev_and_continue: 1902 put_device(dev); 1903 } 1904 } 1905 1906 /* 1907 * Helper function for ap_scan_bus(). 1908 * Does the scan bus job for the given adapter id. 1909 */ 1910 static inline void ap_scan_adapter(int ap) 1911 { 1912 bool decfg, chkstop; 1913 ap_qid_t qid; 1914 unsigned int func; 1915 struct device *dev; 1916 struct ap_card *ac; 1917 int rc, dom, depth, type, comp_type, ml; 1918 1919 /* Is there currently a card device for this adapter ? */ 1920 dev = bus_find_device(&ap_bus_type, NULL, 1921 (void *)(long)ap, 1922 __match_card_device_with_id); 1923 ac = dev ? to_ap_card(dev) : NULL; 1924 1925 /* Adapter not in configuration ? */ 1926 if (!ap_test_config_card_id(ap)) { 1927 if (ac) { 1928 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n", 1929 __func__, ap); 1930 ap_scan_rm_card_dev_and_queue_devs(ac); 1931 put_device(dev); 1932 } 1933 return; 1934 } 1935 1936 /* 1937 * Adapter ap is valid in the current configuration. So do some checks: 1938 * If no card device exists, build one. If a card device exists, check 1939 * for type and functions changed. For all this we need to find a valid 1940 * APQN first. 1941 */ 1942 1943 for (dom = 0; dom <= ap_max_domain_id; dom++) 1944 if (ap_test_config_usage_domain(dom)) { 1945 qid = AP_MKQID(ap, dom); 1946 if (ap_queue_info(qid, &type, &func, &depth, 1947 &ml, &decfg, &chkstop)) 1948 break; 1949 } 1950 if (dom > ap_max_domain_id) { 1951 /* Could not find a valid APQN for this adapter */ 1952 if (ac) { 1953 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n", 1954 __func__, ap); 1955 ap_scan_rm_card_dev_and_queue_devs(ac); 1956 put_device(dev); 1957 } else { 1958 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n", 1959 __func__, ap); 1960 } 1961 return; 1962 } 1963 if (!type) { 1964 /* No apdater type info available, an unusable adapter */ 1965 if (ac) { 1966 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n", 1967 __func__, ap); 1968 ap_scan_rm_card_dev_and_queue_devs(ac); 1969 put_device(dev); 1970 } else { 1971 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n", 1972 __func__, ap); 1973 } 1974 return; 1975 } 1976 1977 if (ac) { 1978 /* Check APQN against existing card device for changes */ 1979 if (ac->raw_hwtype != type) { 1980 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n", 1981 __func__, ap, type); 1982 ap_scan_rm_card_dev_and_queue_devs(ac); 1983 put_device(dev); 1984 ac = NULL; 1985 } else if (ac->functions != func) { 1986 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n", 1987 __func__, ap, type); 1988 ap_scan_rm_card_dev_and_queue_devs(ac); 1989 put_device(dev); 1990 ac = NULL; 1991 } else { 1992 /* handle checkstop state change */ 1993 if (chkstop && !ac->chkstop) { 1994 /* checkstop on */ 1995 ac->chkstop = true; 1996 AP_DBF_INFO("%s(%d) card dev checkstop on\n", 1997 __func__, ap); 1998 } else if (!chkstop && ac->chkstop) { 1999 /* checkstop off */ 2000 ac->chkstop = false; 2001 AP_DBF_INFO("%s(%d) card dev checkstop off\n", 2002 __func__, ap); 2003 } 2004 /* handle config state change */ 2005 if (decfg && ac->config) { 2006 ac->config = false; 2007 AP_DBF_INFO("%s(%d) card dev config off\n", 2008 __func__, ap); 2009 ap_send_config_uevent(&ac->ap_dev, ac->config); 2010 } else if (!decfg && !ac->config) { 2011 ac->config = true; 2012 AP_DBF_INFO("%s(%d) card dev config on\n", 2013 __func__, ap); 2014 ap_send_config_uevent(&ac->ap_dev, ac->config); 2015 } 2016 } 2017 } 2018 2019 if (!ac) { 2020 /* Build a new card device */ 2021 comp_type = ap_get_compatible_type(qid, type, func); 2022 if (!comp_type) { 2023 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n", 2024 __func__, ap, type); 2025 return; 2026 } 2027 ac = ap_card_create(ap, depth, type, comp_type, func, ml); 2028 if (!ac) { 2029 AP_DBF_WARN("%s(%d) ap_card_create() failed\n", 2030 __func__, ap); 2031 return; 2032 } 2033 ac->config = !decfg; 2034 ac->chkstop = chkstop; 2035 dev = &ac->ap_dev.device; 2036 dev->bus = &ap_bus_type; 2037 dev->parent = ap_root_device; 2038 dev_set_name(dev, "card%02x", ap); 2039 /* maybe enlarge ap_max_msg_size to support this card */ 2040 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) { 2041 atomic_set(&ap_max_msg_size, ac->maxmsgsize); 2042 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n", 2043 __func__, ap, 2044 atomic_read(&ap_max_msg_size)); 2045 } 2046 /* Register the new card device with AP bus */ 2047 rc = device_register(dev); 2048 if (rc) { 2049 AP_DBF_WARN("%s(%d) device_register() failed\n", 2050 __func__, ap); 2051 put_device(dev); 2052 return; 2053 } 2054 /* get it and thus adjust reference counter */ 2055 get_device(dev); 2056 if (decfg) 2057 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n", 2058 __func__, ap, type, func); 2059 else if (chkstop) 2060 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n", 2061 __func__, ap, type, func); 2062 else 2063 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n", 2064 __func__, ap, type, func); 2065 } 2066 2067 /* Verify the domains and the queue devices for this card */ 2068 ap_scan_domains(ac); 2069 2070 /* release the card device */ 2071 put_device(&ac->ap_dev.device); 2072 } 2073 2074 /** 2075 * ap_get_configuration - get the host AP configuration 2076 * 2077 * Stores the host AP configuration information returned from the previous call 2078 * to Query Configuration Information (QCI), then retrieves and stores the 2079 * current AP configuration returned from QCI. 2080 * 2081 * Return: true if the host AP configuration changed between calls to QCI; 2082 * otherwise, return false. 2083 */ 2084 static bool ap_get_configuration(void) 2085 { 2086 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info)); 2087 ap_fetch_qci_info(ap_qci_info); 2088 2089 return memcmp(ap_qci_info, ap_qci_info_old, 2090 sizeof(struct ap_config_info)) != 0; 2091 } 2092 2093 /** 2094 * ap_scan_bus(): Scan the AP bus for new devices 2095 * Runs periodically, workqueue timer (ap_config_time) 2096 * @unused: Unused pointer. 2097 */ 2098 static void ap_scan_bus(struct work_struct *unused) 2099 { 2100 int ap, config_changed = 0; 2101 2102 /* config change notify */ 2103 config_changed = ap_get_configuration(); 2104 if (config_changed) 2105 notify_config_changed(); 2106 ap_select_domain(); 2107 2108 AP_DBF_DBG("%s running\n", __func__); 2109 2110 /* loop over all possible adapters */ 2111 for (ap = 0; ap <= ap_max_adapter_id; ap++) 2112 ap_scan_adapter(ap); 2113 2114 /* scan complete notify */ 2115 if (config_changed) 2116 notify_scan_complete(); 2117 2118 /* check if there is at least one queue available with default domain */ 2119 if (ap_domain_index >= 0) { 2120 struct device *dev = 2121 bus_find_device(&ap_bus_type, NULL, 2122 (void *)(long)ap_domain_index, 2123 __match_queue_device_with_queue_id); 2124 if (dev) 2125 put_device(dev); 2126 else 2127 AP_DBF_INFO("%s no queue device with default domain %d available\n", 2128 __func__, ap_domain_index); 2129 } 2130 2131 if (atomic64_inc_return(&ap_scan_bus_count) == 1) { 2132 AP_DBF_DBG("%s init scan complete\n", __func__); 2133 ap_send_init_scan_done_uevent(); 2134 ap_check_bindings_complete(); 2135 } 2136 2137 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 2138 } 2139 2140 static void ap_config_timeout(struct timer_list *unused) 2141 { 2142 queue_work(system_long_wq, &ap_scan_work); 2143 } 2144 2145 static int __init ap_debug_init(void) 2146 { 2147 ap_dbf_info = debug_register("ap", 2, 1, 2148 DBF_MAX_SPRINTF_ARGS * sizeof(long)); 2149 debug_register_view(ap_dbf_info, &debug_sprintf_view); 2150 debug_set_level(ap_dbf_info, DBF_ERR); 2151 2152 return 0; 2153 } 2154 2155 static void __init ap_perms_init(void) 2156 { 2157 /* all resources usable if no kernel parameter string given */ 2158 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm)); 2159 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm)); 2160 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm)); 2161 2162 /* apm kernel parameter string */ 2163 if (apm_str) { 2164 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm)); 2165 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES, 2166 &ap_perms_mutex); 2167 } 2168 2169 /* aqm kernel parameter string */ 2170 if (aqm_str) { 2171 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm)); 2172 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS, 2173 &ap_perms_mutex); 2174 } 2175 } 2176 2177 /** 2178 * ap_module_init(): The module initialization code. 2179 * 2180 * Initializes the module. 2181 */ 2182 static int __init ap_module_init(void) 2183 { 2184 int rc; 2185 2186 rc = ap_debug_init(); 2187 if (rc) 2188 return rc; 2189 2190 if (!ap_instructions_available()) { 2191 pr_warn("The hardware system does not support AP instructions\n"); 2192 return -ENODEV; 2193 } 2194 2195 /* init ap_queue hashtable */ 2196 hash_init(ap_queues); 2197 2198 /* set up the AP permissions (ioctls, ap and aq masks) */ 2199 ap_perms_init(); 2200 2201 /* Get AP configuration data if available */ 2202 ap_init_qci_info(); 2203 2204 /* check default domain setting */ 2205 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id || 2206 (ap_domain_index >= 0 && 2207 !test_bit_inv(ap_domain_index, ap_perms.aqm))) { 2208 pr_warn("%d is not a valid cryptographic domain\n", 2209 ap_domain_index); 2210 ap_domain_index = -1; 2211 } 2212 2213 /* enable interrupts if available */ 2214 if (ap_interrupts_available() && ap_useirq) { 2215 rc = register_adapter_interrupt(&ap_airq); 2216 ap_irq_flag = (rc == 0); 2217 } 2218 2219 /* Create /sys/bus/ap. */ 2220 rc = bus_register(&ap_bus_type); 2221 if (rc) 2222 goto out; 2223 2224 /* Create /sys/devices/ap. */ 2225 ap_root_device = root_device_register("ap"); 2226 rc = PTR_ERR_OR_ZERO(ap_root_device); 2227 if (rc) 2228 goto out_bus; 2229 ap_root_device->bus = &ap_bus_type; 2230 2231 /* Setup the AP bus rescan timer. */ 2232 timer_setup(&ap_config_timer, ap_config_timeout, 0); 2233 2234 /* 2235 * Setup the high resultion poll timer. 2236 * If we are running under z/VM adjust polling to z/VM polling rate. 2237 */ 2238 if (MACHINE_IS_VM) 2239 poll_timeout = 1500000; 2240 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 2241 ap_poll_timer.function = ap_poll_timeout; 2242 2243 /* Start the low priority AP bus poll thread. */ 2244 if (ap_thread_flag) { 2245 rc = ap_poll_thread_start(); 2246 if (rc) 2247 goto out_work; 2248 } 2249 2250 queue_work(system_long_wq, &ap_scan_work); 2251 2252 return 0; 2253 2254 out_work: 2255 hrtimer_cancel(&ap_poll_timer); 2256 root_device_unregister(ap_root_device); 2257 out_bus: 2258 bus_unregister(&ap_bus_type); 2259 out: 2260 if (ap_irq_flag) 2261 unregister_adapter_interrupt(&ap_airq); 2262 kfree(ap_qci_info); 2263 return rc; 2264 } 2265 device_initcall(ap_module_init); 2266