1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright IBM Corp. 2006, 2012 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 * 10 * Adjunct processor bus. 11 */ 12 13 #define KMSG_COMPONENT "ap" 14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 15 16 #include <linux/kernel_stat.h> 17 #include <linux/moduleparam.h> 18 #include <linux/init.h> 19 #include <linux/delay.h> 20 #include <linux/err.h> 21 #include <linux/freezer.h> 22 #include <linux/interrupt.h> 23 #include <linux/workqueue.h> 24 #include <linux/slab.h> 25 #include <linux/notifier.h> 26 #include <linux/kthread.h> 27 #include <linux/mutex.h> 28 #include <asm/airq.h> 29 #include <linux/atomic.h> 30 #include <asm/isc.h> 31 #include <linux/hrtimer.h> 32 #include <linux/ktime.h> 33 #include <asm/facility.h> 34 #include <linux/crypto.h> 35 #include <linux/mod_devicetable.h> 36 #include <linux/debugfs.h> 37 #include <linux/ctype.h> 38 39 #include "ap_bus.h" 40 #include "ap_debug.h" 41 42 /* 43 * Module parameters; note though this file itself isn't modular. 44 */ 45 int ap_domain_index = -1; /* Adjunct Processor Domain Index */ 46 static DEFINE_SPINLOCK(ap_domain_lock); 47 module_param_named(domain, ap_domain_index, int, 0440); 48 MODULE_PARM_DESC(domain, "domain index for ap devices"); 49 EXPORT_SYMBOL(ap_domain_index); 50 51 static int ap_thread_flag; 52 module_param_named(poll_thread, ap_thread_flag, int, 0440); 53 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); 54 55 static char *apm_str; 56 module_param_named(apmask, apm_str, charp, 0440); 57 MODULE_PARM_DESC(apmask, "AP bus adapter mask."); 58 59 static char *aqm_str; 60 module_param_named(aqmask, aqm_str, charp, 0440); 61 MODULE_PARM_DESC(aqmask, "AP bus domain mask."); 62 63 static struct device *ap_root_device; 64 65 /* Hashtable of all queue devices on the AP bus */ 66 DEFINE_HASHTABLE(ap_queues, 8); 67 /* lock used for the ap_queues hashtable */ 68 DEFINE_SPINLOCK(ap_queues_lock); 69 70 /* Default permissions (ioctl, card and domain masking) */ 71 struct ap_perms ap_perms; 72 EXPORT_SYMBOL(ap_perms); 73 DEFINE_MUTEX(ap_perms_mutex); 74 EXPORT_SYMBOL(ap_perms_mutex); 75 76 static struct ap_config_info *ap_qci_info; 77 78 /* 79 * AP bus related debug feature things. 80 */ 81 debug_info_t *ap_dbf_info; 82 83 /* 84 * Workqueue timer for bus rescan. 85 */ 86 static struct timer_list ap_config_timer; 87 static int ap_config_time = AP_CONFIG_TIME; 88 static void ap_scan_bus(struct work_struct *); 89 static DECLARE_WORK(ap_scan_work, ap_scan_bus); 90 91 /* 92 * Tasklet & timer for AP request polling and interrupts 93 */ 94 static void ap_tasklet_fn(unsigned long); 95 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn); 96 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); 97 static struct task_struct *ap_poll_kthread; 98 static DEFINE_MUTEX(ap_poll_thread_mutex); 99 static DEFINE_SPINLOCK(ap_poll_timer_lock); 100 static struct hrtimer ap_poll_timer; 101 /* 102 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. 103 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling. 104 */ 105 static unsigned long long poll_timeout = 250000; 106 107 /* Maximum domain id, if not given via qci */ 108 static int ap_max_domain_id = 15; 109 /* Maximum adapter id, if not given via qci */ 110 static int ap_max_adapter_id = 63; 111 112 static struct bus_type ap_bus_type; 113 114 /* Adapter interrupt definitions */ 115 static void ap_interrupt_handler(struct airq_struct *airq, bool floating); 116 117 static int ap_airq_flag; 118 119 static struct airq_struct ap_airq = { 120 .handler = ap_interrupt_handler, 121 .isc = AP_ISC, 122 }; 123 124 /** 125 * ap_using_interrupts() - Returns non-zero if interrupt support is 126 * available. 127 */ 128 static inline int ap_using_interrupts(void) 129 { 130 return ap_airq_flag; 131 } 132 133 /** 134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator 135 * 136 * Returns the address of the local-summary-indicator of the adapter 137 * interrupt handler for AP, or NULL if adapter interrupts are not 138 * available. 139 */ 140 void *ap_airq_ptr(void) 141 { 142 if (ap_using_interrupts()) 143 return ap_airq.lsi_ptr; 144 return NULL; 145 } 146 147 /** 148 * ap_interrupts_available(): Test if AP interrupts are available. 149 * 150 * Returns 1 if AP interrupts are available. 151 */ 152 static int ap_interrupts_available(void) 153 { 154 return test_facility(65); 155 } 156 157 /** 158 * ap_qci_available(): Test if AP configuration 159 * information can be queried via QCI subfunction. 160 * 161 * Returns 1 if subfunction PQAP(QCI) is available. 162 */ 163 static int ap_qci_available(void) 164 { 165 return test_facility(12); 166 } 167 168 /** 169 * ap_apft_available(): Test if AP facilities test (APFT) 170 * facility is available. 171 * 172 * Returns 1 if APFT is is available. 173 */ 174 static int ap_apft_available(void) 175 { 176 return test_facility(15); 177 } 178 179 /* 180 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available. 181 * 182 * Returns 1 if the QACT subfunction is available. 183 */ 184 static inline int ap_qact_available(void) 185 { 186 if (ap_qci_info) 187 return ap_qci_info->qact; 188 return 0; 189 } 190 191 /* 192 * ap_fetch_qci_info(): Fetch cryptographic config info 193 * 194 * Returns the ap configuration info fetched via PQAP(QCI). 195 * On success 0 is returned, on failure a negative errno 196 * is returned, e.g. if the PQAP(QCI) instruction is not 197 * available, the return value will be -EOPNOTSUPP. 198 */ 199 static inline int ap_fetch_qci_info(struct ap_config_info *info) 200 { 201 if (!ap_qci_available()) 202 return -EOPNOTSUPP; 203 if (!info) 204 return -EINVAL; 205 return ap_qci(info); 206 } 207 208 /** 209 * ap_init_qci_info(): Allocate and query qci config info. 210 * Does also update the static variables ap_max_domain_id 211 * and ap_max_adapter_id if this info is available. 212 213 */ 214 static void __init ap_init_qci_info(void) 215 { 216 if (!ap_qci_available()) { 217 AP_DBF_INFO("%s QCI not supported\n", __func__); 218 return; 219 } 220 221 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL); 222 if (!ap_qci_info) 223 return; 224 if (ap_fetch_qci_info(ap_qci_info) != 0) { 225 kfree(ap_qci_info); 226 ap_qci_info = NULL; 227 return; 228 } 229 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__); 230 231 if (ap_qci_info->apxa) { 232 if (ap_qci_info->Na) { 233 ap_max_adapter_id = ap_qci_info->Na; 234 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n", 235 __func__, ap_max_adapter_id); 236 } 237 if (ap_qci_info->Nd) { 238 ap_max_domain_id = ap_qci_info->Nd; 239 AP_DBF_INFO("%s new ap_max_domain_id is %d\n", 240 __func__, ap_max_domain_id); 241 } 242 } 243 } 244 245 /* 246 * ap_test_config(): helper function to extract the nrth bit 247 * within the unsigned int array field. 248 */ 249 static inline int ap_test_config(unsigned int *field, unsigned int nr) 250 { 251 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); 252 } 253 254 /* 255 * ap_test_config_card_id(): Test, whether an AP card ID is configured. 256 * 257 * Returns 0 if the card is not configured 258 * 1 if the card is configured or 259 * if the configuration information is not available 260 */ 261 static inline int ap_test_config_card_id(unsigned int id) 262 { 263 if (id > ap_max_adapter_id) 264 return 0; 265 if (ap_qci_info) 266 return ap_test_config(ap_qci_info->apm, id); 267 return 1; 268 } 269 270 /* 271 * ap_test_config_usage_domain(): Test, whether an AP usage domain 272 * is configured. 273 * 274 * Returns 0 if the usage domain is not configured 275 * 1 if the usage domain is configured or 276 * if the configuration information is not available 277 */ 278 int ap_test_config_usage_domain(unsigned int domain) 279 { 280 if (domain > ap_max_domain_id) 281 return 0; 282 if (ap_qci_info) 283 return ap_test_config(ap_qci_info->aqm, domain); 284 return 1; 285 } 286 EXPORT_SYMBOL(ap_test_config_usage_domain); 287 288 /* 289 * ap_test_config_ctrl_domain(): Test, whether an AP control domain 290 * is configured. 291 * @domain AP control domain ID 292 * 293 * Returns 1 if the control domain is configured 294 * 0 in all other cases 295 */ 296 int ap_test_config_ctrl_domain(unsigned int domain) 297 { 298 if (!ap_qci_info || domain > ap_max_domain_id) 299 return 0; 300 return ap_test_config(ap_qci_info->adm, domain); 301 } 302 EXPORT_SYMBOL(ap_test_config_ctrl_domain); 303 304 /* 305 * ap_queue_info(): Check and get AP queue info. 306 * Returns true if TAPQ succeeded and the info is filled or 307 * false otherwise. 308 */ 309 static bool ap_queue_info(ap_qid_t qid, int *q_type, 310 unsigned int *q_fac, int *q_depth, bool *q_decfg) 311 { 312 struct ap_queue_status status; 313 unsigned long info = 0; 314 315 /* make sure we don't run into a specifiation exception */ 316 if (AP_QID_CARD(qid) > ap_max_adapter_id || 317 AP_QID_QUEUE(qid) > ap_max_domain_id) 318 return false; 319 320 /* call TAPQ on this APQN */ 321 status = ap_test_queue(qid, ap_apft_available(), &info); 322 switch (status.response_code) { 323 case AP_RESPONSE_NORMAL: 324 case AP_RESPONSE_RESET_IN_PROGRESS: 325 case AP_RESPONSE_DECONFIGURED: 326 case AP_RESPONSE_CHECKSTOPPED: 327 case AP_RESPONSE_BUSY: 328 /* 329 * According to the architecture in all these cases the 330 * info should be filled. All bits 0 is not possible as 331 * there is at least one of the mode bits set. 332 */ 333 if (WARN_ON_ONCE(!info)) 334 return false; 335 *q_type = (int)((info >> 24) & 0xff); 336 *q_fac = (unsigned int)(info >> 32); 337 *q_depth = (int)(info & 0xff); 338 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED; 339 switch (*q_type) { 340 /* For CEX2 and CEX3 the available functions 341 * are not reflected by the facilities bits. 342 * Instead it is coded into the type. So here 343 * modify the function bits based on the type. 344 */ 345 case AP_DEVICE_TYPE_CEX2A: 346 case AP_DEVICE_TYPE_CEX3A: 347 *q_fac |= 0x08000000; 348 break; 349 case AP_DEVICE_TYPE_CEX2C: 350 case AP_DEVICE_TYPE_CEX3C: 351 *q_fac |= 0x10000000; 352 break; 353 default: 354 break; 355 } 356 return true; 357 default: 358 /* 359 * A response code which indicates, there is no info available. 360 */ 361 return false; 362 } 363 } 364 365 void ap_wait(enum ap_sm_wait wait) 366 { 367 ktime_t hr_time; 368 369 switch (wait) { 370 case AP_SM_WAIT_AGAIN: 371 case AP_SM_WAIT_INTERRUPT: 372 if (ap_using_interrupts()) 373 break; 374 if (ap_poll_kthread) { 375 wake_up(&ap_poll_wait); 376 break; 377 } 378 fallthrough; 379 case AP_SM_WAIT_TIMEOUT: 380 spin_lock_bh(&ap_poll_timer_lock); 381 if (!hrtimer_is_queued(&ap_poll_timer)) { 382 hr_time = poll_timeout; 383 hrtimer_forward_now(&ap_poll_timer, hr_time); 384 hrtimer_restart(&ap_poll_timer); 385 } 386 spin_unlock_bh(&ap_poll_timer_lock); 387 break; 388 case AP_SM_WAIT_NONE: 389 default: 390 break; 391 } 392 } 393 394 /** 395 * ap_request_timeout(): Handling of request timeouts 396 * @t: timer making this callback 397 * 398 * Handles request timeouts. 399 */ 400 void ap_request_timeout(struct timer_list *t) 401 { 402 struct ap_queue *aq = from_timer(aq, t, timeout); 403 404 spin_lock_bh(&aq->lock); 405 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT)); 406 spin_unlock_bh(&aq->lock); 407 } 408 409 /** 410 * ap_poll_timeout(): AP receive polling for finished AP requests. 411 * @unused: Unused pointer. 412 * 413 * Schedules the AP tasklet using a high resolution timer. 414 */ 415 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) 416 { 417 tasklet_schedule(&ap_tasklet); 418 return HRTIMER_NORESTART; 419 } 420 421 /** 422 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt 423 * @airq: pointer to adapter interrupt descriptor 424 */ 425 static void ap_interrupt_handler(struct airq_struct *airq, bool floating) 426 { 427 inc_irq_stat(IRQIO_APB); 428 tasklet_schedule(&ap_tasklet); 429 } 430 431 /** 432 * ap_tasklet_fn(): Tasklet to poll all AP devices. 433 * @dummy: Unused variable 434 * 435 * Poll all AP devices on the bus. 436 */ 437 static void ap_tasklet_fn(unsigned long dummy) 438 { 439 int bkt; 440 struct ap_queue *aq; 441 enum ap_sm_wait wait = AP_SM_WAIT_NONE; 442 443 /* Reset the indicator if interrupts are used. Thus new interrupts can 444 * be received. Doing it in the beginning of the tasklet is therefor 445 * important that no requests on any AP get lost. 446 */ 447 if (ap_using_interrupts()) 448 xchg(ap_airq.lsi_ptr, 0); 449 450 spin_lock_bh(&ap_queues_lock); 451 hash_for_each(ap_queues, bkt, aq, hnode) { 452 spin_lock_bh(&aq->lock); 453 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL)); 454 spin_unlock_bh(&aq->lock); 455 } 456 spin_unlock_bh(&ap_queues_lock); 457 458 ap_wait(wait); 459 } 460 461 static int ap_pending_requests(void) 462 { 463 int bkt; 464 struct ap_queue *aq; 465 466 spin_lock_bh(&ap_queues_lock); 467 hash_for_each(ap_queues, bkt, aq, hnode) { 468 if (aq->queue_count == 0) 469 continue; 470 spin_unlock_bh(&ap_queues_lock); 471 return 1; 472 } 473 spin_unlock_bh(&ap_queues_lock); 474 return 0; 475 } 476 477 /** 478 * ap_poll_thread(): Thread that polls for finished requests. 479 * @data: Unused pointer 480 * 481 * AP bus poll thread. The purpose of this thread is to poll for 482 * finished requests in a loop if there is a "free" cpu - that is 483 * a cpu that doesn't have anything better to do. The polling stops 484 * as soon as there is another task or if all messages have been 485 * delivered. 486 */ 487 static int ap_poll_thread(void *data) 488 { 489 DECLARE_WAITQUEUE(wait, current); 490 491 set_user_nice(current, MAX_NICE); 492 set_freezable(); 493 while (!kthread_should_stop()) { 494 add_wait_queue(&ap_poll_wait, &wait); 495 set_current_state(TASK_INTERRUPTIBLE); 496 if (!ap_pending_requests()) { 497 schedule(); 498 try_to_freeze(); 499 } 500 set_current_state(TASK_RUNNING); 501 remove_wait_queue(&ap_poll_wait, &wait); 502 if (need_resched()) { 503 schedule(); 504 try_to_freeze(); 505 continue; 506 } 507 ap_tasklet_fn(0); 508 } 509 510 return 0; 511 } 512 513 static int ap_poll_thread_start(void) 514 { 515 int rc; 516 517 if (ap_using_interrupts() || ap_poll_kthread) 518 return 0; 519 mutex_lock(&ap_poll_thread_mutex); 520 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); 521 rc = PTR_ERR_OR_ZERO(ap_poll_kthread); 522 if (rc) 523 ap_poll_kthread = NULL; 524 mutex_unlock(&ap_poll_thread_mutex); 525 return rc; 526 } 527 528 static void ap_poll_thread_stop(void) 529 { 530 if (!ap_poll_kthread) 531 return; 532 mutex_lock(&ap_poll_thread_mutex); 533 kthread_stop(ap_poll_kthread); 534 ap_poll_kthread = NULL; 535 mutex_unlock(&ap_poll_thread_mutex); 536 } 537 538 #define is_card_dev(x) ((x)->parent == ap_root_device) 539 #define is_queue_dev(x) ((x)->parent != ap_root_device) 540 541 /** 542 * ap_bus_match() 543 * @dev: Pointer to device 544 * @drv: Pointer to device_driver 545 * 546 * AP bus driver registration/unregistration. 547 */ 548 static int ap_bus_match(struct device *dev, struct device_driver *drv) 549 { 550 struct ap_driver *ap_drv = to_ap_drv(drv); 551 struct ap_device_id *id; 552 553 /* 554 * Compare device type of the device with the list of 555 * supported types of the device_driver. 556 */ 557 for (id = ap_drv->ids; id->match_flags; id++) { 558 if (is_card_dev(dev) && 559 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && 560 id->dev_type == to_ap_dev(dev)->device_type) 561 return 1; 562 if (is_queue_dev(dev) && 563 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && 564 id->dev_type == to_ap_dev(dev)->device_type) 565 return 1; 566 } 567 return 0; 568 } 569 570 /** 571 * ap_uevent(): Uevent function for AP devices. 572 * @dev: Pointer to device 573 * @env: Pointer to kobj_uevent_env 574 * 575 * It sets up a single environment variable DEV_TYPE which contains the 576 * hardware device type. 577 */ 578 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env) 579 { 580 struct ap_device *ap_dev = to_ap_dev(dev); 581 int retval = 0; 582 583 if (!ap_dev) 584 return -ENODEV; 585 586 /* Set up DEV_TYPE environment variable. */ 587 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); 588 if (retval) 589 return retval; 590 591 /* Add MODALIAS= */ 592 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); 593 594 return retval; 595 } 596 597 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) 598 { 599 if (is_queue_dev(dev) && 600 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data) 601 device_unregister(dev); 602 return 0; 603 } 604 605 static struct bus_type ap_bus_type = { 606 .name = "ap", 607 .match = &ap_bus_match, 608 .uevent = &ap_uevent, 609 }; 610 611 static int __ap_revise_reserved(struct device *dev, void *dummy) 612 { 613 int rc, card, queue, devres, drvres; 614 615 if (is_queue_dev(dev)) { 616 card = AP_QID_CARD(to_ap_queue(dev)->qid); 617 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 618 mutex_lock(&ap_perms_mutex); 619 devres = test_bit_inv(card, ap_perms.apm) 620 && test_bit_inv(queue, ap_perms.aqm); 621 mutex_unlock(&ap_perms_mutex); 622 drvres = to_ap_drv(dev->driver)->flags 623 & AP_DRIVER_FLAG_DEFAULT; 624 if (!!devres != !!drvres) { 625 AP_DBF_DBG("reprobing queue=%02x.%04x\n", 626 card, queue); 627 rc = device_reprobe(dev); 628 } 629 } 630 631 return 0; 632 } 633 634 static void ap_bus_revise_bindings(void) 635 { 636 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved); 637 } 638 639 int ap_owned_by_def_drv(int card, int queue) 640 { 641 int rc = 0; 642 643 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS) 644 return -EINVAL; 645 646 mutex_lock(&ap_perms_mutex); 647 648 if (test_bit_inv(card, ap_perms.apm) 649 && test_bit_inv(queue, ap_perms.aqm)) 650 rc = 1; 651 652 mutex_unlock(&ap_perms_mutex); 653 654 return rc; 655 } 656 EXPORT_SYMBOL(ap_owned_by_def_drv); 657 658 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm, 659 unsigned long *aqm) 660 { 661 int card, queue, rc = 0; 662 663 mutex_lock(&ap_perms_mutex); 664 665 for (card = 0; !rc && card < AP_DEVICES; card++) 666 if (test_bit_inv(card, apm) && 667 test_bit_inv(card, ap_perms.apm)) 668 for (queue = 0; !rc && queue < AP_DOMAINS; queue++) 669 if (test_bit_inv(queue, aqm) && 670 test_bit_inv(queue, ap_perms.aqm)) 671 rc = 1; 672 673 mutex_unlock(&ap_perms_mutex); 674 675 return rc; 676 } 677 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv); 678 679 static int ap_device_probe(struct device *dev) 680 { 681 struct ap_device *ap_dev = to_ap_dev(dev); 682 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 683 int card, queue, devres, drvres, rc = -ENODEV; 684 685 if (!get_device(dev)) 686 return rc; 687 688 if (is_queue_dev(dev)) { 689 /* 690 * If the apqn is marked as reserved/used by ap bus and 691 * default drivers, only probe with drivers with the default 692 * flag set. If it is not marked, only probe with drivers 693 * with the default flag not set. 694 */ 695 card = AP_QID_CARD(to_ap_queue(dev)->qid); 696 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); 697 mutex_lock(&ap_perms_mutex); 698 devres = test_bit_inv(card, ap_perms.apm) 699 && test_bit_inv(queue, ap_perms.aqm); 700 mutex_unlock(&ap_perms_mutex); 701 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT; 702 if (!!devres != !!drvres) 703 goto out; 704 } 705 706 /* Add queue/card to list of active queues/cards */ 707 spin_lock_bh(&ap_queues_lock); 708 if (is_queue_dev(dev)) 709 hash_add(ap_queues, &to_ap_queue(dev)->hnode, 710 to_ap_queue(dev)->qid); 711 spin_unlock_bh(&ap_queues_lock); 712 713 ap_dev->drv = ap_drv; 714 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 715 716 if (rc) { 717 spin_lock_bh(&ap_queues_lock); 718 if (is_queue_dev(dev)) 719 hash_del(&to_ap_queue(dev)->hnode); 720 spin_unlock_bh(&ap_queues_lock); 721 ap_dev->drv = NULL; 722 } 723 724 out: 725 if (rc) 726 put_device(dev); 727 return rc; 728 } 729 730 static int ap_device_remove(struct device *dev) 731 { 732 struct ap_device *ap_dev = to_ap_dev(dev); 733 struct ap_driver *ap_drv = ap_dev->drv; 734 735 /* prepare ap queue device removal */ 736 if (is_queue_dev(dev)) 737 ap_queue_prepare_remove(to_ap_queue(dev)); 738 739 /* driver's chance to clean up gracefully */ 740 if (ap_drv->remove) 741 ap_drv->remove(ap_dev); 742 743 /* now do the ap queue device remove */ 744 if (is_queue_dev(dev)) 745 ap_queue_remove(to_ap_queue(dev)); 746 747 /* Remove queue/card from list of active queues/cards */ 748 spin_lock_bh(&ap_queues_lock); 749 if (is_queue_dev(dev)) 750 hash_del(&to_ap_queue(dev)->hnode); 751 spin_unlock_bh(&ap_queues_lock); 752 753 put_device(dev); 754 755 return 0; 756 } 757 758 struct ap_queue *ap_get_qdev(ap_qid_t qid) 759 { 760 int bkt; 761 struct ap_queue *aq; 762 763 spin_lock_bh(&ap_queues_lock); 764 hash_for_each(ap_queues, bkt, aq, hnode) { 765 if (aq->qid == qid) { 766 get_device(&aq->ap_dev.device); 767 spin_unlock_bh(&ap_queues_lock); 768 return aq; 769 } 770 } 771 spin_unlock_bh(&ap_queues_lock); 772 773 return NULL; 774 } 775 EXPORT_SYMBOL(ap_get_qdev); 776 777 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 778 char *name) 779 { 780 struct device_driver *drv = &ap_drv->driver; 781 782 drv->bus = &ap_bus_type; 783 drv->probe = ap_device_probe; 784 drv->remove = ap_device_remove; 785 drv->owner = owner; 786 drv->name = name; 787 return driver_register(drv); 788 } 789 EXPORT_SYMBOL(ap_driver_register); 790 791 void ap_driver_unregister(struct ap_driver *ap_drv) 792 { 793 driver_unregister(&ap_drv->driver); 794 } 795 EXPORT_SYMBOL(ap_driver_unregister); 796 797 void ap_bus_force_rescan(void) 798 { 799 /* processing a asynchronous bus rescan */ 800 del_timer(&ap_config_timer); 801 queue_work(system_long_wq, &ap_scan_work); 802 flush_work(&ap_scan_work); 803 } 804 EXPORT_SYMBOL(ap_bus_force_rescan); 805 806 /* 807 * A config change has happened, force an ap bus rescan. 808 */ 809 void ap_bus_cfg_chg(void) 810 { 811 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__); 812 813 ap_bus_force_rescan(); 814 } 815 816 /* 817 * hex2bitmap() - parse hex mask string and set bitmap. 818 * Valid strings are "0x012345678" with at least one valid hex number. 819 * Rest of the bitmap to the right is padded with 0. No spaces allowed 820 * within the string, the leading 0x may be omitted. 821 * Returns the bitmask with exactly the bits set as given by the hex 822 * string (both in big endian order). 823 */ 824 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits) 825 { 826 int i, n, b; 827 828 /* bits needs to be a multiple of 8 */ 829 if (bits & 0x07) 830 return -EINVAL; 831 832 if (str[0] == '0' && str[1] == 'x') 833 str++; 834 if (*str == 'x') 835 str++; 836 837 for (i = 0; isxdigit(*str) && i < bits; str++) { 838 b = hex_to_bin(*str); 839 for (n = 0; n < 4; n++) 840 if (b & (0x08 >> n)) 841 set_bit_inv(i + n, bitmap); 842 i += 4; 843 } 844 845 if (*str == '\n') 846 str++; 847 if (*str) 848 return -EINVAL; 849 return 0; 850 } 851 852 /* 853 * modify_bitmap() - parse bitmask argument and modify an existing 854 * bit mask accordingly. A concatenation (done with ',') of these 855 * terms is recognized: 856 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>] 857 * <bitnr> may be any valid number (hex, decimal or octal) in the range 858 * 0...bits-1; the leading + or - is required. Here are some examples: 859 * +0-15,+32,-128,-0xFF 860 * -0-255,+1-16,+0x128 861 * +1,+2,+3,+4,-5,-7-10 862 * Returns the new bitmap after all changes have been applied. Every 863 * positive value in the string will set a bit and every negative value 864 * in the string will clear a bit. As a bit may be touched more than once, 865 * the last 'operation' wins: 866 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be 867 * cleared again. All other bits are unmodified. 868 */ 869 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits) 870 { 871 int a, i, z; 872 char *np, sign; 873 874 /* bits needs to be a multiple of 8 */ 875 if (bits & 0x07) 876 return -EINVAL; 877 878 while (*str) { 879 sign = *str++; 880 if (sign != '+' && sign != '-') 881 return -EINVAL; 882 a = z = simple_strtoul(str, &np, 0); 883 if (str == np || a >= bits) 884 return -EINVAL; 885 str = np; 886 if (*str == '-') { 887 z = simple_strtoul(++str, &np, 0); 888 if (str == np || a > z || z >= bits) 889 return -EINVAL; 890 str = np; 891 } 892 for (i = a; i <= z; i++) 893 if (sign == '+') 894 set_bit_inv(i, bitmap); 895 else 896 clear_bit_inv(i, bitmap); 897 while (*str == ',' || *str == '\n') 898 str++; 899 } 900 901 return 0; 902 } 903 904 int ap_parse_mask_str(const char *str, 905 unsigned long *bitmap, int bits, 906 struct mutex *lock) 907 { 908 unsigned long *newmap, size; 909 int rc; 910 911 /* bits needs to be a multiple of 8 */ 912 if (bits & 0x07) 913 return -EINVAL; 914 915 size = BITS_TO_LONGS(bits)*sizeof(unsigned long); 916 newmap = kmalloc(size, GFP_KERNEL); 917 if (!newmap) 918 return -ENOMEM; 919 if (mutex_lock_interruptible(lock)) { 920 kfree(newmap); 921 return -ERESTARTSYS; 922 } 923 924 if (*str == '+' || *str == '-') { 925 memcpy(newmap, bitmap, size); 926 rc = modify_bitmap(str, newmap, bits); 927 } else { 928 memset(newmap, 0, size); 929 rc = hex2bitmap(str, newmap, bits); 930 } 931 if (rc == 0) 932 memcpy(bitmap, newmap, size); 933 mutex_unlock(lock); 934 kfree(newmap); 935 return rc; 936 } 937 EXPORT_SYMBOL(ap_parse_mask_str); 938 939 /* 940 * AP bus attributes. 941 */ 942 943 static ssize_t ap_domain_show(struct bus_type *bus, char *buf) 944 { 945 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); 946 } 947 948 static ssize_t ap_domain_store(struct bus_type *bus, 949 const char *buf, size_t count) 950 { 951 int domain; 952 953 if (sscanf(buf, "%i\n", &domain) != 1 || 954 domain < 0 || domain > ap_max_domain_id || 955 !test_bit_inv(domain, ap_perms.aqm)) 956 return -EINVAL; 957 958 spin_lock_bh(&ap_domain_lock); 959 ap_domain_index = domain; 960 spin_unlock_bh(&ap_domain_lock); 961 962 AP_DBF_INFO("stored new default domain=%d\n", domain); 963 964 return count; 965 } 966 967 static BUS_ATTR_RW(ap_domain); 968 969 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) 970 { 971 if (!ap_qci_info) /* QCI not supported */ 972 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 973 974 return scnprintf(buf, PAGE_SIZE, 975 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 976 ap_qci_info->adm[0], ap_qci_info->adm[1], 977 ap_qci_info->adm[2], ap_qci_info->adm[3], 978 ap_qci_info->adm[4], ap_qci_info->adm[5], 979 ap_qci_info->adm[6], ap_qci_info->adm[7]); 980 } 981 982 static BUS_ATTR_RO(ap_control_domain_mask); 983 984 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf) 985 { 986 if (!ap_qci_info) /* QCI not supported */ 987 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 988 989 return scnprintf(buf, PAGE_SIZE, 990 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 991 ap_qci_info->aqm[0], ap_qci_info->aqm[1], 992 ap_qci_info->aqm[2], ap_qci_info->aqm[3], 993 ap_qci_info->aqm[4], ap_qci_info->aqm[5], 994 ap_qci_info->aqm[6], ap_qci_info->aqm[7]); 995 } 996 997 static BUS_ATTR_RO(ap_usage_domain_mask); 998 999 static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf) 1000 { 1001 if (!ap_qci_info) /* QCI not supported */ 1002 return scnprintf(buf, PAGE_SIZE, "not supported\n"); 1003 1004 return scnprintf(buf, PAGE_SIZE, 1005 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", 1006 ap_qci_info->apm[0], ap_qci_info->apm[1], 1007 ap_qci_info->apm[2], ap_qci_info->apm[3], 1008 ap_qci_info->apm[4], ap_qci_info->apm[5], 1009 ap_qci_info->apm[6], ap_qci_info->apm[7]); 1010 } 1011 1012 static BUS_ATTR_RO(ap_adapter_mask); 1013 1014 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf) 1015 { 1016 return scnprintf(buf, PAGE_SIZE, "%d\n", 1017 ap_using_interrupts() ? 1 : 0); 1018 } 1019 1020 static BUS_ATTR_RO(ap_interrupts); 1021 1022 static ssize_t config_time_show(struct bus_type *bus, char *buf) 1023 { 1024 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); 1025 } 1026 1027 static ssize_t config_time_store(struct bus_type *bus, 1028 const char *buf, size_t count) 1029 { 1030 int time; 1031 1032 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 1033 return -EINVAL; 1034 ap_config_time = time; 1035 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 1036 return count; 1037 } 1038 1039 static BUS_ATTR_RW(config_time); 1040 1041 static ssize_t poll_thread_show(struct bus_type *bus, char *buf) 1042 { 1043 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); 1044 } 1045 1046 static ssize_t poll_thread_store(struct bus_type *bus, 1047 const char *buf, size_t count) 1048 { 1049 int flag, rc; 1050 1051 if (sscanf(buf, "%d\n", &flag) != 1) 1052 return -EINVAL; 1053 if (flag) { 1054 rc = ap_poll_thread_start(); 1055 if (rc) 1056 count = rc; 1057 } else 1058 ap_poll_thread_stop(); 1059 return count; 1060 } 1061 1062 static BUS_ATTR_RW(poll_thread); 1063 1064 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf) 1065 { 1066 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout); 1067 } 1068 1069 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf, 1070 size_t count) 1071 { 1072 unsigned long long time; 1073 ktime_t hr_time; 1074 1075 /* 120 seconds = maximum poll interval */ 1076 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || 1077 time > 120000000000ULL) 1078 return -EINVAL; 1079 poll_timeout = time; 1080 hr_time = poll_timeout; 1081 1082 spin_lock_bh(&ap_poll_timer_lock); 1083 hrtimer_cancel(&ap_poll_timer); 1084 hrtimer_set_expires(&ap_poll_timer, hr_time); 1085 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); 1086 spin_unlock_bh(&ap_poll_timer_lock); 1087 1088 return count; 1089 } 1090 1091 static BUS_ATTR_RW(poll_timeout); 1092 1093 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) 1094 { 1095 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id); 1096 } 1097 1098 static BUS_ATTR_RO(ap_max_domain_id); 1099 1100 static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf) 1101 { 1102 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id); 1103 } 1104 1105 static BUS_ATTR_RO(ap_max_adapter_id); 1106 1107 static ssize_t apmask_show(struct bus_type *bus, char *buf) 1108 { 1109 int rc; 1110 1111 if (mutex_lock_interruptible(&ap_perms_mutex)) 1112 return -ERESTARTSYS; 1113 rc = scnprintf(buf, PAGE_SIZE, 1114 "0x%016lx%016lx%016lx%016lx\n", 1115 ap_perms.apm[0], ap_perms.apm[1], 1116 ap_perms.apm[2], ap_perms.apm[3]); 1117 mutex_unlock(&ap_perms_mutex); 1118 1119 return rc; 1120 } 1121 1122 static ssize_t apmask_store(struct bus_type *bus, const char *buf, 1123 size_t count) 1124 { 1125 int rc; 1126 1127 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex); 1128 if (rc) 1129 return rc; 1130 1131 ap_bus_revise_bindings(); 1132 1133 return count; 1134 } 1135 1136 static BUS_ATTR_RW(apmask); 1137 1138 static ssize_t aqmask_show(struct bus_type *bus, char *buf) 1139 { 1140 int rc; 1141 1142 if (mutex_lock_interruptible(&ap_perms_mutex)) 1143 return -ERESTARTSYS; 1144 rc = scnprintf(buf, PAGE_SIZE, 1145 "0x%016lx%016lx%016lx%016lx\n", 1146 ap_perms.aqm[0], ap_perms.aqm[1], 1147 ap_perms.aqm[2], ap_perms.aqm[3]); 1148 mutex_unlock(&ap_perms_mutex); 1149 1150 return rc; 1151 } 1152 1153 static ssize_t aqmask_store(struct bus_type *bus, const char *buf, 1154 size_t count) 1155 { 1156 int rc; 1157 1158 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex); 1159 if (rc) 1160 return rc; 1161 1162 ap_bus_revise_bindings(); 1163 1164 return count; 1165 } 1166 1167 static BUS_ATTR_RW(aqmask); 1168 1169 static struct bus_attribute *const ap_bus_attrs[] = { 1170 &bus_attr_ap_domain, 1171 &bus_attr_ap_control_domain_mask, 1172 &bus_attr_ap_usage_domain_mask, 1173 &bus_attr_ap_adapter_mask, 1174 &bus_attr_config_time, 1175 &bus_attr_poll_thread, 1176 &bus_attr_ap_interrupts, 1177 &bus_attr_poll_timeout, 1178 &bus_attr_ap_max_domain_id, 1179 &bus_attr_ap_max_adapter_id, 1180 &bus_attr_apmask, 1181 &bus_attr_aqmask, 1182 NULL, 1183 }; 1184 1185 /** 1186 * ap_select_domain(): Select an AP domain if possible and we haven't 1187 * already done so before. 1188 */ 1189 static void ap_select_domain(void) 1190 { 1191 struct ap_queue_status status; 1192 int card, dom; 1193 1194 /* 1195 * Choose the default domain. Either the one specified with 1196 * the "domain=" parameter or the first domain with at least 1197 * one valid APQN. 1198 */ 1199 spin_lock_bh(&ap_domain_lock); 1200 if (ap_domain_index >= 0) { 1201 /* Domain has already been selected. */ 1202 goto out; 1203 } 1204 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1205 if (!ap_test_config_usage_domain(dom) || 1206 !test_bit_inv(dom, ap_perms.aqm)) 1207 continue; 1208 for (card = 0; card <= ap_max_adapter_id; card++) { 1209 if (!ap_test_config_card_id(card) || 1210 !test_bit_inv(card, ap_perms.apm)) 1211 continue; 1212 status = ap_test_queue(AP_MKQID(card, dom), 1213 ap_apft_available(), 1214 NULL); 1215 if (status.response_code == AP_RESPONSE_NORMAL) 1216 break; 1217 } 1218 if (card <= ap_max_adapter_id) 1219 break; 1220 } 1221 if (dom <= ap_max_domain_id) { 1222 ap_domain_index = dom; 1223 AP_DBF_INFO("%s new default domain is %d\n", 1224 __func__, ap_domain_index); 1225 } 1226 out: 1227 spin_unlock_bh(&ap_domain_lock); 1228 } 1229 1230 /* 1231 * This function checks the type and returns either 0 for not 1232 * supported or the highest compatible type value (which may 1233 * include the input type value). 1234 */ 1235 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func) 1236 { 1237 int comp_type = 0; 1238 1239 /* < CEX2A is not supported */ 1240 if (rawtype < AP_DEVICE_TYPE_CEX2A) { 1241 AP_DBF_WARN("get_comp_type queue=%02x.%04x unsupported type %d\n", 1242 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype); 1243 return 0; 1244 } 1245 /* up to CEX7 known and fully supported */ 1246 if (rawtype <= AP_DEVICE_TYPE_CEX7) 1247 return rawtype; 1248 /* 1249 * unknown new type > CEX7, check for compatibility 1250 * to the highest known and supported type which is 1251 * currently CEX7 with the help of the QACT function. 1252 */ 1253 if (ap_qact_available()) { 1254 struct ap_queue_status status; 1255 union ap_qact_ap_info apinfo = {0}; 1256 1257 apinfo.mode = (func >> 26) & 0x07; 1258 apinfo.cat = AP_DEVICE_TYPE_CEX7; 1259 status = ap_qact(qid, 0, &apinfo); 1260 if (status.response_code == AP_RESPONSE_NORMAL 1261 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A 1262 && apinfo.cat <= AP_DEVICE_TYPE_CEX7) 1263 comp_type = apinfo.cat; 1264 } 1265 if (!comp_type) 1266 AP_DBF_WARN("get_comp_type queue=%02x.%04x unable to map type %d\n", 1267 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype); 1268 else if (comp_type != rawtype) 1269 AP_DBF_INFO("get_comp_type queue=%02x.%04x map type %d to %d\n", 1270 AP_QID_CARD(qid), AP_QID_QUEUE(qid), 1271 rawtype, comp_type); 1272 return comp_type; 1273 } 1274 1275 /* 1276 * Helper function to be used with bus_find_dev 1277 * matches for the card device with the given id 1278 */ 1279 static int __match_card_device_with_id(struct device *dev, const void *data) 1280 { 1281 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data; 1282 } 1283 1284 /* 1285 * Helper function to be used with bus_find_dev 1286 * matches for the queue device with a given qid 1287 */ 1288 static int __match_queue_device_with_qid(struct device *dev, const void *data) 1289 { 1290 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data; 1291 } 1292 1293 /* 1294 * Helper function to be used with bus_find_dev 1295 * matches any queue device with given queue id 1296 */ 1297 static int __match_queue_device_with_queue_id(struct device *dev, const void *data) 1298 { 1299 return is_queue_dev(dev) 1300 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data; 1301 } 1302 1303 /* 1304 * Helper function for ap_scan_bus(). 1305 * Remove card device and associated queue devices. 1306 */ 1307 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac) 1308 { 1309 bus_for_each_dev(&ap_bus_type, NULL, 1310 (void *)(long) ac->id, 1311 __ap_queue_devices_with_id_unregister); 1312 device_unregister(&ac->ap_dev.device); 1313 } 1314 1315 /* 1316 * Helper function for ap_scan_bus(). 1317 * Does the scan bus job for all the domains within 1318 * a valid adapter given by an ap_card ptr. 1319 */ 1320 static inline void ap_scan_domains(struct ap_card *ac) 1321 { 1322 bool decfg; 1323 ap_qid_t qid; 1324 unsigned int func; 1325 struct device *dev; 1326 struct ap_queue *aq; 1327 int rc, dom, depth, type; 1328 1329 /* 1330 * Go through the configuration for the domains and compare them 1331 * to the existing queue devices. Also take care of the config 1332 * and error state for the queue devices. 1333 */ 1334 1335 for (dom = 0; dom <= ap_max_domain_id; dom++) { 1336 qid = AP_MKQID(ac->id, dom); 1337 dev = bus_find_device(&ap_bus_type, NULL, 1338 (void *)(long) qid, 1339 __match_queue_device_with_qid); 1340 aq = dev ? to_ap_queue(dev) : NULL; 1341 if (!ap_test_config_usage_domain(dom)) { 1342 if (dev) { 1343 AP_DBF_INFO("%s(%d,%d) not in config any more, rm queue device\n", 1344 __func__, ac->id, dom); 1345 device_unregister(dev); 1346 put_device(dev); 1347 } 1348 continue; 1349 } 1350 /* domain is valid, get info from this APQN */ 1351 if (!ap_queue_info(qid, &type, &func, &depth, &decfg)) { 1352 if (aq) { 1353 AP_DBF_INFO( 1354 "%s(%d,%d) ap_queue_info() not successful, rm queue device\n", 1355 __func__, ac->id, dom); 1356 device_unregister(dev); 1357 put_device(dev); 1358 } 1359 continue; 1360 } 1361 /* if no queue device exists, create a new one */ 1362 if (!aq) { 1363 aq = ap_queue_create(qid, ac->ap_dev.device_type); 1364 if (!aq) { 1365 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n", 1366 __func__, ac->id, dom); 1367 continue; 1368 } 1369 aq->card = ac; 1370 aq->config = !decfg; 1371 dev = &aq->ap_dev.device; 1372 dev->bus = &ap_bus_type; 1373 dev->parent = &ac->ap_dev.device; 1374 dev_set_name(dev, "%02x.%04x", ac->id, dom); 1375 /* register queue device */ 1376 rc = device_register(dev); 1377 if (rc) { 1378 AP_DBF_WARN("%s(%d,%d) device_register() failed\n", 1379 __func__, ac->id, dom); 1380 goto put_dev_and_continue; 1381 } 1382 /* get it and thus adjust reference counter */ 1383 get_device(dev); 1384 if (decfg) 1385 AP_DBF_INFO("%s(%d,%d) new (decfg) queue device created\n", 1386 __func__, ac->id, dom); 1387 else 1388 AP_DBF_INFO("%s(%d,%d) new queue device created\n", 1389 __func__, ac->id, dom); 1390 goto put_dev_and_continue; 1391 } 1392 /* Check config state on the already existing queue device */ 1393 spin_lock_bh(&aq->lock); 1394 if (decfg && aq->config) { 1395 /* config off this queue device */ 1396 aq->config = false; 1397 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1398 aq->dev_state = AP_DEV_STATE_ERROR; 1399 aq->last_err_rc = AP_RESPONSE_DECONFIGURED; 1400 } 1401 spin_unlock_bh(&aq->lock); 1402 AP_DBF_INFO("%s(%d,%d) queue device config off\n", 1403 __func__, ac->id, dom); 1404 /* 'receive' pending messages with -EAGAIN */ 1405 ap_flush_queue(aq); 1406 goto put_dev_and_continue; 1407 } 1408 if (!decfg && !aq->config) { 1409 /* config on this queue device */ 1410 aq->config = true; 1411 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { 1412 aq->dev_state = AP_DEV_STATE_OPERATING; 1413 aq->sm_state = AP_SM_STATE_RESET_START; 1414 } 1415 spin_unlock_bh(&aq->lock); 1416 AP_DBF_INFO("%s(%d,%d) queue device config on\n", 1417 __func__, ac->id, dom); 1418 goto put_dev_and_continue; 1419 } 1420 /* handle other error states */ 1421 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) { 1422 spin_unlock_bh(&aq->lock); 1423 /* 'receive' pending messages with -EAGAIN */ 1424 ap_flush_queue(aq); 1425 /* re-init (with reset) the queue device */ 1426 ap_queue_init_state(aq); 1427 AP_DBF_INFO("%s(%d,%d) queue device reinit enforced\n", 1428 __func__, ac->id, dom); 1429 goto put_dev_and_continue; 1430 } 1431 spin_unlock_bh(&aq->lock); 1432 put_dev_and_continue: 1433 put_device(dev); 1434 } 1435 } 1436 1437 /* 1438 * Helper function for ap_scan_bus(). 1439 * Does the scan bus job for the given adapter id. 1440 */ 1441 static inline void ap_scan_adapter(int ap) 1442 { 1443 bool decfg; 1444 ap_qid_t qid; 1445 unsigned int func; 1446 struct device *dev; 1447 struct ap_card *ac; 1448 int rc, dom, depth, type, comp_type; 1449 1450 /* Is there currently a card device for this adapter ? */ 1451 dev = bus_find_device(&ap_bus_type, NULL, 1452 (void *)(long) ap, 1453 __match_card_device_with_id); 1454 ac = dev ? to_ap_card(dev) : NULL; 1455 1456 /* Adapter not in configuration ? */ 1457 if (!ap_test_config_card_id(ap)) { 1458 if (ac) { 1459 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devices\n", 1460 __func__, ap); 1461 ap_scan_rm_card_dev_and_queue_devs(ac); 1462 put_device(dev); 1463 } 1464 return; 1465 } 1466 1467 /* 1468 * Adapter ap is valid in the current configuration. So do some checks: 1469 * If no card device exists, build one. If a card device exists, check 1470 * for type and functions changed. For all this we need to find a valid 1471 * APQN first. 1472 */ 1473 1474 for (dom = 0; dom <= ap_max_domain_id; dom++) 1475 if (ap_test_config_usage_domain(dom)) { 1476 qid = AP_MKQID(ap, dom); 1477 if (ap_queue_info(qid, &type, &func, &depth, &decfg)) 1478 break; 1479 } 1480 if (dom > ap_max_domain_id) { 1481 /* Could not find a valid APQN for this adapter */ 1482 if (ac) { 1483 AP_DBF_INFO( 1484 "%s(%d) no type info (no APQN found), rm card and queue devices\n", 1485 __func__, ap); 1486 ap_scan_rm_card_dev_and_queue_devs(ac); 1487 put_device(dev); 1488 } else { 1489 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n", 1490 __func__, ap); 1491 } 1492 return; 1493 } 1494 if (!type) { 1495 /* No apdater type info available, an unusable adapter */ 1496 if (ac) { 1497 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devices\n", 1498 __func__, ap); 1499 ap_scan_rm_card_dev_and_queue_devs(ac); 1500 put_device(dev); 1501 } else { 1502 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n", 1503 __func__, ap); 1504 } 1505 return; 1506 } 1507 1508 if (ac) { 1509 /* Check APQN against existing card device for changes */ 1510 if (ac->raw_hwtype != type) { 1511 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devices\n", 1512 __func__, ap, type); 1513 ap_scan_rm_card_dev_and_queue_devs(ac); 1514 put_device(dev); 1515 ac = NULL; 1516 } else if (ac->functions != func) { 1517 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devices\n", 1518 __func__, ap, type); 1519 ap_scan_rm_card_dev_and_queue_devs(ac); 1520 put_device(dev); 1521 ac = NULL; 1522 } else { 1523 if (decfg && ac->config) { 1524 ac->config = false; 1525 AP_DBF_INFO("%s(%d) card device config off\n", 1526 __func__, ap); 1527 1528 } 1529 if (!decfg && !ac->config) { 1530 ac->config = true; 1531 AP_DBF_INFO("%s(%d) card device config on\n", 1532 __func__, ap); 1533 } 1534 } 1535 } 1536 1537 if (!ac) { 1538 /* Build a new card device */ 1539 comp_type = ap_get_compatible_type(qid, type, func); 1540 if (!comp_type) { 1541 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n", 1542 __func__, ap, type); 1543 return; 1544 } 1545 ac = ap_card_create(ap, depth, type, comp_type, func); 1546 if (!ac) { 1547 AP_DBF_WARN("%s(%d) ap_card_create() failed\n", 1548 __func__, ap); 1549 return; 1550 } 1551 ac->config = !decfg; 1552 dev = &ac->ap_dev.device; 1553 dev->bus = &ap_bus_type; 1554 dev->parent = ap_root_device; 1555 dev_set_name(dev, "card%02x", ap); 1556 /* Register the new card device with AP bus */ 1557 rc = device_register(dev); 1558 if (rc) { 1559 AP_DBF_WARN("%s(%d) device_register() failed\n", 1560 __func__, ap); 1561 put_device(dev); 1562 return; 1563 } 1564 /* get it and thus adjust reference counter */ 1565 get_device(dev); 1566 if (decfg) 1567 AP_DBF_INFO("%s(%d) new (decfg) card device type=%d func=0x%08x created\n", 1568 __func__, ap, type, func); 1569 else 1570 AP_DBF_INFO("%s(%d) new card device type=%d func=0x%08x created\n", 1571 __func__, ap, type, func); 1572 } 1573 1574 /* Verify the domains and the queue devices for this card */ 1575 ap_scan_domains(ac); 1576 1577 /* release the card device */ 1578 put_device(&ac->ap_dev.device); 1579 } 1580 1581 /** 1582 * ap_scan_bus(): Scan the AP bus for new devices 1583 * Runs periodically, workqueue timer (ap_config_time) 1584 */ 1585 static void ap_scan_bus(struct work_struct *unused) 1586 { 1587 int ap; 1588 1589 ap_fetch_qci_info(ap_qci_info); 1590 ap_select_domain(); 1591 1592 AP_DBF_DBG("%s running\n", __func__); 1593 1594 /* loop over all possible adapters */ 1595 for (ap = 0; ap <= ap_max_adapter_id; ap++) 1596 ap_scan_adapter(ap); 1597 1598 /* check if there is at least one queue available with default domain */ 1599 if (ap_domain_index >= 0) { 1600 struct device *dev = 1601 bus_find_device(&ap_bus_type, NULL, 1602 (void *)(long) ap_domain_index, 1603 __match_queue_device_with_queue_id); 1604 if (dev) 1605 put_device(dev); 1606 else 1607 AP_DBF_INFO("no queue device with default domain %d available\n", 1608 ap_domain_index); 1609 } 1610 1611 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); 1612 } 1613 1614 static void ap_config_timeout(struct timer_list *unused) 1615 { 1616 queue_work(system_long_wq, &ap_scan_work); 1617 } 1618 1619 static int __init ap_debug_init(void) 1620 { 1621 ap_dbf_info = debug_register("ap", 1, 1, 1622 DBF_MAX_SPRINTF_ARGS * sizeof(long)); 1623 debug_register_view(ap_dbf_info, &debug_sprintf_view); 1624 debug_set_level(ap_dbf_info, DBF_ERR); 1625 1626 return 0; 1627 } 1628 1629 static void __init ap_perms_init(void) 1630 { 1631 /* all resources useable if no kernel parameter string given */ 1632 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm)); 1633 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm)); 1634 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm)); 1635 1636 /* apm kernel parameter string */ 1637 if (apm_str) { 1638 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm)); 1639 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES, 1640 &ap_perms_mutex); 1641 } 1642 1643 /* aqm kernel parameter string */ 1644 if (aqm_str) { 1645 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm)); 1646 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS, 1647 &ap_perms_mutex); 1648 } 1649 } 1650 1651 /** 1652 * ap_module_init(): The module initialization code. 1653 * 1654 * Initializes the module. 1655 */ 1656 static int __init ap_module_init(void) 1657 { 1658 int rc, i; 1659 1660 rc = ap_debug_init(); 1661 if (rc) 1662 return rc; 1663 1664 if (!ap_instructions_available()) { 1665 pr_warn("The hardware system does not support AP instructions\n"); 1666 return -ENODEV; 1667 } 1668 1669 /* init ap_queue hashtable */ 1670 hash_init(ap_queues); 1671 1672 /* set up the AP permissions (ioctls, ap and aq masks) */ 1673 ap_perms_init(); 1674 1675 /* Get AP configuration data if available */ 1676 ap_init_qci_info(); 1677 1678 /* check default domain setting */ 1679 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id || 1680 (ap_domain_index >= 0 && 1681 !test_bit_inv(ap_domain_index, ap_perms.aqm))) { 1682 pr_warn("%d is not a valid cryptographic domain\n", 1683 ap_domain_index); 1684 ap_domain_index = -1; 1685 } 1686 1687 /* enable interrupts if available */ 1688 if (ap_interrupts_available()) { 1689 rc = register_adapter_interrupt(&ap_airq); 1690 ap_airq_flag = (rc == 0); 1691 } 1692 1693 /* Create /sys/bus/ap. */ 1694 rc = bus_register(&ap_bus_type); 1695 if (rc) 1696 goto out; 1697 for (i = 0; ap_bus_attrs[i]; i++) { 1698 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); 1699 if (rc) 1700 goto out_bus; 1701 } 1702 1703 /* Create /sys/devices/ap. */ 1704 ap_root_device = root_device_register("ap"); 1705 rc = PTR_ERR_OR_ZERO(ap_root_device); 1706 if (rc) 1707 goto out_bus; 1708 1709 /* Setup the AP bus rescan timer. */ 1710 timer_setup(&ap_config_timer, ap_config_timeout, 0); 1711 1712 /* 1713 * Setup the high resultion poll timer. 1714 * If we are running under z/VM adjust polling to z/VM polling rate. 1715 */ 1716 if (MACHINE_IS_VM) 1717 poll_timeout = 1500000; 1718 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); 1719 ap_poll_timer.function = ap_poll_timeout; 1720 1721 /* Start the low priority AP bus poll thread. */ 1722 if (ap_thread_flag) { 1723 rc = ap_poll_thread_start(); 1724 if (rc) 1725 goto out_work; 1726 } 1727 1728 queue_work(system_long_wq, &ap_scan_work); 1729 1730 return 0; 1731 1732 out_work: 1733 hrtimer_cancel(&ap_poll_timer); 1734 root_device_unregister(ap_root_device); 1735 out_bus: 1736 while (i--) 1737 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); 1738 bus_unregister(&ap_bus_type); 1739 out: 1740 if (ap_using_interrupts()) 1741 unregister_adapter_interrupt(&ap_airq); 1742 kfree(ap_qci_info); 1743 return rc; 1744 } 1745 device_initcall(ap_module_init); 1746