1 /* 2 * linux/drivers/s390/crypto/ap_bus.c 3 * 4 * Copyright (C) 2006 IBM Corporation 5 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> 6 * Martin Schwidefsky <schwidefsky@de.ibm.com> 7 * Ralph Wuerthner <rwuerthn@de.ibm.com> 8 * 9 * Adjunct processor bus. 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2, or (at your option) 14 * any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 24 */ 25 26 #include <linux/module.h> 27 #include <linux/init.h> 28 #include <linux/delay.h> 29 #include <linux/err.h> 30 #include <linux/interrupt.h> 31 #include <linux/workqueue.h> 32 #include <linux/notifier.h> 33 #include <linux/kthread.h> 34 #include <linux/mutex.h> 35 #include <asm/s390_rdev.h> 36 #include <asm/reset.h> 37 38 #include "ap_bus.h" 39 40 /* Some prototypes. */ 41 static void ap_scan_bus(struct work_struct *); 42 static void ap_poll_all(unsigned long); 43 static void ap_poll_timeout(unsigned long); 44 static int ap_poll_thread_start(void); 45 static void ap_poll_thread_stop(void); 46 47 /** 48 * Module description. 49 */ 50 MODULE_AUTHOR("IBM Corporation"); 51 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " 52 "Copyright 2006 IBM Corporation"); 53 MODULE_LICENSE("GPL"); 54 55 /** 56 * Module parameter 57 */ 58 int ap_domain_index = -1; /* Adjunct Processor Domain Index */ 59 module_param_named(domain, ap_domain_index, int, 0000); 60 MODULE_PARM_DESC(domain, "domain index for ap devices"); 61 EXPORT_SYMBOL(ap_domain_index); 62 63 static int ap_thread_flag = 1; 64 module_param_named(poll_thread, ap_thread_flag, int, 0000); 65 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 1 (on)."); 66 67 static struct device *ap_root_device = NULL; 68 69 /** 70 * Workqueue & timer for bus rescan. 71 */ 72 static struct workqueue_struct *ap_work_queue; 73 static struct timer_list ap_config_timer; 74 static int ap_config_time = AP_CONFIG_TIME; 75 static DECLARE_WORK(ap_config_work, ap_scan_bus); 76 77 /** 78 * Tasklet & timer for AP request polling. 79 */ 80 static struct timer_list ap_poll_timer = TIMER_INITIALIZER(ap_poll_timeout,0,0); 81 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0); 82 static atomic_t ap_poll_requests = ATOMIC_INIT(0); 83 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); 84 static struct task_struct *ap_poll_kthread = NULL; 85 static DEFINE_MUTEX(ap_poll_thread_mutex); 86 87 /** 88 * Test if ap instructions are available. 89 * 90 * Returns 0 if the ap instructions are installed. 91 */ 92 static inline int ap_instructions_available(void) 93 { 94 register unsigned long reg0 asm ("0") = AP_MKQID(0,0); 95 register unsigned long reg1 asm ("1") = -ENODEV; 96 register unsigned long reg2 asm ("2") = 0UL; 97 98 asm volatile( 99 " .long 0xb2af0000\n" /* PQAP(TAPQ) */ 100 "0: la %1,0\n" 101 "1:\n" 102 EX_TABLE(0b, 1b) 103 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" ); 104 return reg1; 105 } 106 107 /** 108 * Test adjunct processor queue. 109 * @qid: the ap queue number 110 * @queue_depth: pointer to queue depth value 111 * @device_type: pointer to device type value 112 * 113 * Returns ap queue status structure. 114 */ 115 static inline struct ap_queue_status 116 ap_test_queue(ap_qid_t qid, int *queue_depth, int *device_type) 117 { 118 register unsigned long reg0 asm ("0") = qid; 119 register struct ap_queue_status reg1 asm ("1"); 120 register unsigned long reg2 asm ("2") = 0UL; 121 122 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */ 123 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc"); 124 *device_type = (int) (reg2 >> 24); 125 *queue_depth = (int) (reg2 & 0xff); 126 return reg1; 127 } 128 129 /** 130 * Reset adjunct processor queue. 131 * @qid: the ap queue number 132 * 133 * Returns ap queue status structure. 134 */ 135 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid) 136 { 137 register unsigned long reg0 asm ("0") = qid | 0x01000000UL; 138 register struct ap_queue_status reg1 asm ("1"); 139 register unsigned long reg2 asm ("2") = 0UL; 140 141 asm volatile( 142 ".long 0xb2af0000" /* PQAP(RAPQ) */ 143 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc"); 144 return reg1; 145 } 146 147 /** 148 * Send message to adjunct processor queue. 149 * @qid: the ap queue number 150 * @psmid: the program supplied message identifier 151 * @msg: the message text 152 * @length: the message length 153 * 154 * Returns ap queue status structure. 155 * 156 * Condition code 1 on NQAP can't happen because the L bit is 1. 157 * 158 * Condition code 2 on NQAP also means the send is incomplete, 159 * because a segment boundary was reached. The NQAP is repeated. 160 */ 161 static inline struct ap_queue_status 162 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length) 163 { 164 typedef struct { char _[length]; } msgblock; 165 register unsigned long reg0 asm ("0") = qid | 0x40000000UL; 166 register struct ap_queue_status reg1 asm ("1"); 167 register unsigned long reg2 asm ("2") = (unsigned long) msg; 168 register unsigned long reg3 asm ("3") = (unsigned long) length; 169 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32); 170 register unsigned long reg5 asm ("5") = (unsigned int) psmid; 171 172 asm volatile ( 173 "0: .long 0xb2ad0042\n" /* DQAP */ 174 " brc 2,0b" 175 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3) 176 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg) 177 : "cc" ); 178 return reg1; 179 } 180 181 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length) 182 { 183 struct ap_queue_status status; 184 185 status = __ap_send(qid, psmid, msg, length); 186 switch (status.response_code) { 187 case AP_RESPONSE_NORMAL: 188 return 0; 189 case AP_RESPONSE_Q_FULL: 190 return -EBUSY; 191 default: /* Device is gone. */ 192 return -ENODEV; 193 } 194 } 195 EXPORT_SYMBOL(ap_send); 196 197 /* 198 * Receive message from adjunct processor queue. 199 * @qid: the ap queue number 200 * @psmid: pointer to program supplied message identifier 201 * @msg: the message text 202 * @length: the message length 203 * 204 * Returns ap queue status structure. 205 * 206 * Condition code 1 on DQAP means the receive has taken place 207 * but only partially. The response is incomplete, hence the 208 * DQAP is repeated. 209 * 210 * Condition code 2 on DQAP also means the receive is incomplete, 211 * this time because a segment boundary was reached. Again, the 212 * DQAP is repeated. 213 * 214 * Note that gpr2 is used by the DQAP instruction to keep track of 215 * any 'residual' length, in case the instruction gets interrupted. 216 * Hence it gets zeroed before the instruction. 217 */ 218 static inline struct ap_queue_status 219 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length) 220 { 221 typedef struct { char _[length]; } msgblock; 222 register unsigned long reg0 asm("0") = qid | 0x80000000UL; 223 register struct ap_queue_status reg1 asm ("1"); 224 register unsigned long reg2 asm("2") = 0UL; 225 register unsigned long reg4 asm("4") = (unsigned long) msg; 226 register unsigned long reg5 asm("5") = (unsigned long) length; 227 register unsigned long reg6 asm("6") = 0UL; 228 register unsigned long reg7 asm("7") = 0UL; 229 230 231 asm volatile( 232 "0: .long 0xb2ae0064\n" 233 " brc 6,0b\n" 234 : "+d" (reg0), "=d" (reg1), "+d" (reg2), 235 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7), 236 "=m" (*(msgblock *) msg) : : "cc" ); 237 *psmid = (((unsigned long long) reg6) << 32) + reg7; 238 return reg1; 239 } 240 241 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length) 242 { 243 struct ap_queue_status status; 244 245 status = __ap_recv(qid, psmid, msg, length); 246 switch (status.response_code) { 247 case AP_RESPONSE_NORMAL: 248 return 0; 249 case AP_RESPONSE_NO_PENDING_REPLY: 250 if (status.queue_empty) 251 return -ENOENT; 252 return -EBUSY; 253 default: 254 return -ENODEV; 255 } 256 } 257 EXPORT_SYMBOL(ap_recv); 258 259 /** 260 * Check if an AP queue is available. The test is repeated for 261 * AP_MAX_RESET times. 262 * @qid: the ap queue number 263 * @queue_depth: pointer to queue depth value 264 * @device_type: pointer to device type value 265 */ 266 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type) 267 { 268 struct ap_queue_status status; 269 int t_depth, t_device_type, rc, i; 270 271 rc = -EBUSY; 272 for (i = 0; i < AP_MAX_RESET; i++) { 273 status = ap_test_queue(qid, &t_depth, &t_device_type); 274 switch (status.response_code) { 275 case AP_RESPONSE_NORMAL: 276 *queue_depth = t_depth + 1; 277 *device_type = t_device_type; 278 rc = 0; 279 break; 280 case AP_RESPONSE_Q_NOT_AVAIL: 281 rc = -ENODEV; 282 break; 283 case AP_RESPONSE_RESET_IN_PROGRESS: 284 break; 285 case AP_RESPONSE_DECONFIGURED: 286 rc = -ENODEV; 287 break; 288 case AP_RESPONSE_CHECKSTOPPED: 289 rc = -ENODEV; 290 break; 291 case AP_RESPONSE_BUSY: 292 break; 293 default: 294 BUG(); 295 } 296 if (rc != -EBUSY) 297 break; 298 if (i < AP_MAX_RESET - 1) 299 udelay(5); 300 } 301 return rc; 302 } 303 304 /** 305 * Reset an AP queue and wait for it to become available again. 306 * @qid: the ap queue number 307 */ 308 static int ap_init_queue(ap_qid_t qid) 309 { 310 struct ap_queue_status status; 311 int rc, dummy, i; 312 313 rc = -ENODEV; 314 status = ap_reset_queue(qid); 315 for (i = 0; i < AP_MAX_RESET; i++) { 316 switch (status.response_code) { 317 case AP_RESPONSE_NORMAL: 318 if (status.queue_empty) 319 rc = 0; 320 break; 321 case AP_RESPONSE_Q_NOT_AVAIL: 322 case AP_RESPONSE_DECONFIGURED: 323 case AP_RESPONSE_CHECKSTOPPED: 324 i = AP_MAX_RESET; /* return with -ENODEV */ 325 break; 326 case AP_RESPONSE_RESET_IN_PROGRESS: 327 case AP_RESPONSE_BUSY: 328 default: 329 break; 330 } 331 if (rc != -ENODEV) 332 break; 333 if (i < AP_MAX_RESET - 1) { 334 udelay(5); 335 status = ap_test_queue(qid, &dummy, &dummy); 336 } 337 } 338 return rc; 339 } 340 341 /** 342 * AP device related attributes. 343 */ 344 static ssize_t ap_hwtype_show(struct device *dev, 345 struct device_attribute *attr, char *buf) 346 { 347 struct ap_device *ap_dev = to_ap_dev(dev); 348 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type); 349 } 350 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL); 351 352 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr, 353 char *buf) 354 { 355 struct ap_device *ap_dev = to_ap_dev(dev); 356 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth); 357 } 358 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL); 359 360 static ssize_t ap_request_count_show(struct device *dev, 361 struct device_attribute *attr, 362 char *buf) 363 { 364 struct ap_device *ap_dev = to_ap_dev(dev); 365 int rc; 366 367 spin_lock_bh(&ap_dev->lock); 368 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count); 369 spin_unlock_bh(&ap_dev->lock); 370 return rc; 371 } 372 373 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL); 374 375 static ssize_t ap_modalias_show(struct device *dev, 376 struct device_attribute *attr, char *buf) 377 { 378 return sprintf(buf, "ap:t%02X", to_ap_dev(dev)->device_type); 379 } 380 381 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL); 382 383 static struct attribute *ap_dev_attrs[] = { 384 &dev_attr_hwtype.attr, 385 &dev_attr_depth.attr, 386 &dev_attr_request_count.attr, 387 &dev_attr_modalias.attr, 388 NULL 389 }; 390 static struct attribute_group ap_dev_attr_group = { 391 .attrs = ap_dev_attrs 392 }; 393 394 /** 395 * AP bus driver registration/unregistration. 396 */ 397 static int ap_bus_match(struct device *dev, struct device_driver *drv) 398 { 399 struct ap_device *ap_dev = to_ap_dev(dev); 400 struct ap_driver *ap_drv = to_ap_drv(drv); 401 struct ap_device_id *id; 402 403 /** 404 * Compare device type of the device with the list of 405 * supported types of the device_driver. 406 */ 407 for (id = ap_drv->ids; id->match_flags; id++) { 408 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) && 409 (id->dev_type != ap_dev->device_type)) 410 continue; 411 return 1; 412 } 413 return 0; 414 } 415 416 /** 417 * uevent function for AP devices. It sets up a single environment 418 * variable DEV_TYPE which contains the hardware device type. 419 */ 420 static int ap_uevent (struct device *dev, char **envp, int num_envp, 421 char *buffer, int buffer_size) 422 { 423 struct ap_device *ap_dev = to_ap_dev(dev); 424 int length; 425 426 if (!ap_dev) 427 return -ENODEV; 428 429 /* Set up DEV_TYPE environment variable. */ 430 envp[0] = buffer; 431 length = scnprintf(buffer, buffer_size, "DEV_TYPE=%04X", 432 ap_dev->device_type); 433 if (buffer_size - length <= 0) 434 return -ENOMEM; 435 buffer += length; 436 buffer_size -= length; 437 /* Add MODALIAS= */ 438 envp[1] = buffer; 439 length = scnprintf(buffer, buffer_size, "MODALIAS=ap:t%02X", 440 ap_dev->device_type); 441 if (buffer_size - length <= 0) 442 return -ENOMEM; 443 envp[2] = NULL; 444 return 0; 445 } 446 447 static struct bus_type ap_bus_type = { 448 .name = "ap", 449 .match = &ap_bus_match, 450 .uevent = &ap_uevent, 451 }; 452 453 static int ap_device_probe(struct device *dev) 454 { 455 struct ap_device *ap_dev = to_ap_dev(dev); 456 struct ap_driver *ap_drv = to_ap_drv(dev->driver); 457 int rc; 458 459 ap_dev->drv = ap_drv; 460 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; 461 return rc; 462 } 463 464 /** 465 * Flush all requests from the request/pending queue of an AP device. 466 * @ap_dev: pointer to the AP device. 467 */ 468 static void __ap_flush_queue(struct ap_device *ap_dev) 469 { 470 struct ap_message *ap_msg, *next; 471 472 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) { 473 list_del_init(&ap_msg->list); 474 ap_dev->pendingq_count--; 475 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV)); 476 } 477 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) { 478 list_del_init(&ap_msg->list); 479 ap_dev->requestq_count--; 480 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV)); 481 } 482 } 483 484 void ap_flush_queue(struct ap_device *ap_dev) 485 { 486 spin_lock_bh(&ap_dev->lock); 487 __ap_flush_queue(ap_dev); 488 spin_unlock_bh(&ap_dev->lock); 489 } 490 EXPORT_SYMBOL(ap_flush_queue); 491 492 static int ap_device_remove(struct device *dev) 493 { 494 struct ap_device *ap_dev = to_ap_dev(dev); 495 struct ap_driver *ap_drv = ap_dev->drv; 496 497 ap_flush_queue(ap_dev); 498 if (ap_drv->remove) 499 ap_drv->remove(ap_dev); 500 return 0; 501 } 502 503 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, 504 char *name) 505 { 506 struct device_driver *drv = &ap_drv->driver; 507 508 drv->bus = &ap_bus_type; 509 drv->probe = ap_device_probe; 510 drv->remove = ap_device_remove; 511 drv->owner = owner; 512 drv->name = name; 513 return driver_register(drv); 514 } 515 EXPORT_SYMBOL(ap_driver_register); 516 517 void ap_driver_unregister(struct ap_driver *ap_drv) 518 { 519 driver_unregister(&ap_drv->driver); 520 } 521 EXPORT_SYMBOL(ap_driver_unregister); 522 523 /** 524 * AP bus attributes. 525 */ 526 static ssize_t ap_domain_show(struct bus_type *bus, char *buf) 527 { 528 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index); 529 } 530 531 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL); 532 533 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf) 534 { 535 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time); 536 } 537 538 static ssize_t ap_config_time_store(struct bus_type *bus, 539 const char *buf, size_t count) 540 { 541 int time; 542 543 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) 544 return -EINVAL; 545 ap_config_time = time; 546 if (!timer_pending(&ap_config_timer) || 547 !mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ)) { 548 ap_config_timer.expires = jiffies + ap_config_time * HZ; 549 add_timer(&ap_config_timer); 550 } 551 return count; 552 } 553 554 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); 555 556 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf) 557 { 558 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0); 559 } 560 561 static ssize_t ap_poll_thread_store(struct bus_type *bus, 562 const char *buf, size_t count) 563 { 564 int flag, rc; 565 566 if (sscanf(buf, "%d\n", &flag) != 1) 567 return -EINVAL; 568 if (flag) { 569 rc = ap_poll_thread_start(); 570 if (rc) 571 return rc; 572 } 573 else 574 ap_poll_thread_stop(); 575 return count; 576 } 577 578 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store); 579 580 static struct bus_attribute *const ap_bus_attrs[] = { 581 &bus_attr_ap_domain, 582 &bus_attr_config_time, 583 &bus_attr_poll_thread, 584 NULL 585 }; 586 587 /** 588 * Pick one of the 16 ap domains. 589 */ 590 static int ap_select_domain(void) 591 { 592 int queue_depth, device_type, count, max_count, best_domain; 593 int rc, i, j; 594 595 /** 596 * We want to use a single domain. Either the one specified with 597 * the "domain=" parameter or the domain with the maximum number 598 * of devices. 599 */ 600 if (ap_domain_index >= 0 && ap_domain_index < AP_DOMAINS) 601 /* Domain has already been selected. */ 602 return 0; 603 best_domain = -1; 604 max_count = 0; 605 for (i = 0; i < AP_DOMAINS; i++) { 606 count = 0; 607 for (j = 0; j < AP_DEVICES; j++) { 608 ap_qid_t qid = AP_MKQID(j, i); 609 rc = ap_query_queue(qid, &queue_depth, &device_type); 610 if (rc) 611 continue; 612 count++; 613 } 614 if (count > max_count) { 615 max_count = count; 616 best_domain = i; 617 } 618 } 619 if (best_domain >= 0){ 620 ap_domain_index = best_domain; 621 return 0; 622 } 623 return -ENODEV; 624 } 625 626 /** 627 * Find the device type if query queue returned a device type of 0. 628 * @ap_dev: pointer to the AP device. 629 */ 630 static int ap_probe_device_type(struct ap_device *ap_dev) 631 { 632 static unsigned char msg[] = { 633 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00, 634 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 635 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00, 636 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 637 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50, 638 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01, 639 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00, 640 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00, 641 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 642 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00, 643 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 644 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00, 645 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00, 646 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 647 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00, 648 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 649 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 650 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 651 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 652 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 653 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 654 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00, 655 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 656 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00, 657 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20, 658 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53, 659 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22, 660 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00, 661 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88, 662 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66, 663 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44, 664 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22, 665 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00, 666 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77, 667 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00, 668 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00, 669 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01, 670 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c, 671 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68, 672 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66, 673 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0, 674 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8, 675 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04, 676 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57, 677 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d, 678 }; 679 struct ap_queue_status status; 680 unsigned long long psmid; 681 char *reply; 682 int rc, i; 683 684 reply = (void *) get_zeroed_page(GFP_KERNEL); 685 if (!reply) { 686 rc = -ENOMEM; 687 goto out; 688 } 689 690 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL, 691 msg, sizeof(msg)); 692 if (status.response_code != AP_RESPONSE_NORMAL) { 693 rc = -ENODEV; 694 goto out_free; 695 } 696 697 /* Wait for the test message to complete. */ 698 for (i = 0; i < 6; i++) { 699 mdelay(300); 700 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096); 701 if (status.response_code == AP_RESPONSE_NORMAL && 702 psmid == 0x0102030405060708ULL) 703 break; 704 } 705 if (i < 6) { 706 /* Got an answer. */ 707 if (reply[0] == 0x00 && reply[1] == 0x86) 708 ap_dev->device_type = AP_DEVICE_TYPE_PCICC; 709 else 710 ap_dev->device_type = AP_DEVICE_TYPE_PCICA; 711 rc = 0; 712 } else 713 rc = -ENODEV; 714 715 out_free: 716 free_page((unsigned long) reply); 717 out: 718 return rc; 719 } 720 721 /** 722 * Scan the ap bus for new devices. 723 */ 724 static int __ap_scan_bus(struct device *dev, void *data) 725 { 726 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data; 727 } 728 729 static void ap_device_release(struct device *dev) 730 { 731 struct ap_device *ap_dev = to_ap_dev(dev); 732 733 kfree(ap_dev); 734 } 735 736 static void ap_scan_bus(struct work_struct *unused) 737 { 738 struct ap_device *ap_dev; 739 struct device *dev; 740 ap_qid_t qid; 741 int queue_depth, device_type; 742 int rc, i; 743 744 if (ap_select_domain() != 0) 745 return; 746 for (i = 0; i < AP_DEVICES; i++) { 747 qid = AP_MKQID(i, ap_domain_index); 748 dev = bus_find_device(&ap_bus_type, NULL, 749 (void *)(unsigned long)qid, 750 __ap_scan_bus); 751 rc = ap_query_queue(qid, &queue_depth, &device_type); 752 if (dev && rc) { 753 put_device(dev); 754 device_unregister(dev); 755 continue; 756 } 757 if (dev) { 758 put_device(dev); 759 continue; 760 } 761 if (rc) 762 continue; 763 rc = ap_init_queue(qid); 764 if (rc) 765 continue; 766 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL); 767 if (!ap_dev) 768 break; 769 ap_dev->qid = qid; 770 ap_dev->queue_depth = queue_depth; 771 ap_dev->unregistered = 1; 772 spin_lock_init(&ap_dev->lock); 773 INIT_LIST_HEAD(&ap_dev->pendingq); 774 INIT_LIST_HEAD(&ap_dev->requestq); 775 if (device_type == 0) 776 ap_probe_device_type(ap_dev); 777 else 778 ap_dev->device_type = device_type; 779 780 ap_dev->device.bus = &ap_bus_type; 781 ap_dev->device.parent = ap_root_device; 782 snprintf(ap_dev->device.bus_id, BUS_ID_SIZE, "card%02x", 783 AP_QID_DEVICE(ap_dev->qid)); 784 ap_dev->device.release = ap_device_release; 785 rc = device_register(&ap_dev->device); 786 if (rc) { 787 kfree(ap_dev); 788 continue; 789 } 790 /* Add device attributes. */ 791 rc = sysfs_create_group(&ap_dev->device.kobj, 792 &ap_dev_attr_group); 793 if (!rc) { 794 spin_lock_bh(&ap_dev->lock); 795 ap_dev->unregistered = 0; 796 spin_unlock_bh(&ap_dev->lock); 797 } 798 else 799 device_unregister(&ap_dev->device); 800 } 801 } 802 803 static void 804 ap_config_timeout(unsigned long ptr) 805 { 806 queue_work(ap_work_queue, &ap_config_work); 807 ap_config_timer.expires = jiffies + ap_config_time * HZ; 808 add_timer(&ap_config_timer); 809 } 810 811 /** 812 * Set up the timer to run the poll tasklet 813 */ 814 static inline void ap_schedule_poll_timer(void) 815 { 816 if (timer_pending(&ap_poll_timer)) 817 return; 818 mod_timer(&ap_poll_timer, jiffies + AP_POLL_TIME); 819 } 820 821 /** 822 * Receive pending reply messages from an AP device. 823 * @ap_dev: pointer to the AP device 824 * @flags: pointer to control flags, bit 2^0 is set if another poll is 825 * required, bit 2^1 is set if the poll timer needs to get armed 826 * Returns 0 if the device is still present, -ENODEV if not. 827 */ 828 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags) 829 { 830 struct ap_queue_status status; 831 struct ap_message *ap_msg; 832 833 if (ap_dev->queue_count <= 0) 834 return 0; 835 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid, 836 ap_dev->reply->message, ap_dev->reply->length); 837 switch (status.response_code) { 838 case AP_RESPONSE_NORMAL: 839 atomic_dec(&ap_poll_requests); 840 ap_dev->queue_count--; 841 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) { 842 if (ap_msg->psmid != ap_dev->reply->psmid) 843 continue; 844 list_del_init(&ap_msg->list); 845 ap_dev->pendingq_count--; 846 ap_dev->drv->receive(ap_dev, ap_msg, ap_dev->reply); 847 break; 848 } 849 if (ap_dev->queue_count > 0) 850 *flags |= 1; 851 break; 852 case AP_RESPONSE_NO_PENDING_REPLY: 853 if (status.queue_empty) { 854 /* The card shouldn't forget requests but who knows. */ 855 ap_dev->queue_count = 0; 856 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq); 857 ap_dev->requestq_count += ap_dev->pendingq_count; 858 ap_dev->pendingq_count = 0; 859 } else 860 *flags |= 2; 861 break; 862 default: 863 return -ENODEV; 864 } 865 return 0; 866 } 867 868 /** 869 * Send messages from the request queue to an AP device. 870 * @ap_dev: pointer to the AP device 871 * @flags: pointer to control flags, bit 2^0 is set if another poll is 872 * required, bit 2^1 is set if the poll timer needs to get armed 873 * Returns 0 if the device is still present, -ENODEV if not. 874 */ 875 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags) 876 { 877 struct ap_queue_status status; 878 struct ap_message *ap_msg; 879 880 if (ap_dev->requestq_count <= 0 || 881 ap_dev->queue_count >= ap_dev->queue_depth) 882 return 0; 883 /* Start the next request on the queue. */ 884 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list); 885 status = __ap_send(ap_dev->qid, ap_msg->psmid, 886 ap_msg->message, ap_msg->length); 887 switch (status.response_code) { 888 case AP_RESPONSE_NORMAL: 889 atomic_inc(&ap_poll_requests); 890 ap_dev->queue_count++; 891 list_move_tail(&ap_msg->list, &ap_dev->pendingq); 892 ap_dev->requestq_count--; 893 ap_dev->pendingq_count++; 894 if (ap_dev->queue_count < ap_dev->queue_depth && 895 ap_dev->requestq_count > 0) 896 *flags |= 1; 897 *flags |= 2; 898 break; 899 case AP_RESPONSE_Q_FULL: 900 *flags |= 2; 901 break; 902 case AP_RESPONSE_MESSAGE_TOO_BIG: 903 return -EINVAL; 904 default: 905 return -ENODEV; 906 } 907 return 0; 908 } 909 910 /** 911 * Poll AP device for pending replies and send new messages. If either 912 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device. 913 * @ap_dev: pointer to the bus device 914 * @flags: pointer to control flags, bit 2^0 is set if another poll is 915 * required, bit 2^1 is set if the poll timer needs to get armed 916 * Returns 0. 917 */ 918 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags) 919 { 920 int rc; 921 922 rc = ap_poll_read(ap_dev, flags); 923 if (rc) 924 return rc; 925 return ap_poll_write(ap_dev, flags); 926 } 927 928 /** 929 * Queue a message to a device. 930 * @ap_dev: pointer to the AP device 931 * @ap_msg: the message to be queued 932 */ 933 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg) 934 { 935 struct ap_queue_status status; 936 937 if (list_empty(&ap_dev->requestq) && 938 ap_dev->queue_count < ap_dev->queue_depth) { 939 status = __ap_send(ap_dev->qid, ap_msg->psmid, 940 ap_msg->message, ap_msg->length); 941 switch (status.response_code) { 942 case AP_RESPONSE_NORMAL: 943 list_add_tail(&ap_msg->list, &ap_dev->pendingq); 944 atomic_inc(&ap_poll_requests); 945 ap_dev->pendingq_count++; 946 ap_dev->queue_count++; 947 ap_dev->total_request_count++; 948 break; 949 case AP_RESPONSE_Q_FULL: 950 list_add_tail(&ap_msg->list, &ap_dev->requestq); 951 ap_dev->requestq_count++; 952 ap_dev->total_request_count++; 953 return -EBUSY; 954 case AP_RESPONSE_MESSAGE_TOO_BIG: 955 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-EINVAL)); 956 return -EINVAL; 957 default: /* Device is gone. */ 958 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV)); 959 return -ENODEV; 960 } 961 } else { 962 list_add_tail(&ap_msg->list, &ap_dev->requestq); 963 ap_dev->requestq_count++; 964 ap_dev->total_request_count++; 965 return -EBUSY; 966 } 967 ap_schedule_poll_timer(); 968 return 0; 969 } 970 971 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg) 972 { 973 unsigned long flags; 974 int rc; 975 976 spin_lock_bh(&ap_dev->lock); 977 if (!ap_dev->unregistered) { 978 /* Make room on the queue by polling for finished requests. */ 979 rc = ap_poll_queue(ap_dev, &flags); 980 if (!rc) 981 rc = __ap_queue_message(ap_dev, ap_msg); 982 if (!rc) 983 wake_up(&ap_poll_wait); 984 if (rc == -ENODEV) 985 ap_dev->unregistered = 1; 986 } else { 987 ap_dev->drv->receive(ap_dev, ap_msg, ERR_PTR(-ENODEV)); 988 rc = 0; 989 } 990 spin_unlock_bh(&ap_dev->lock); 991 if (rc == -ENODEV) 992 device_unregister(&ap_dev->device); 993 } 994 EXPORT_SYMBOL(ap_queue_message); 995 996 /** 997 * Cancel a crypto request. This is done by removing the request 998 * from the devive pendingq or requestq queue. Note that the 999 * request stays on the AP queue. When it finishes the message 1000 * reply will be discarded because the psmid can't be found. 1001 * @ap_dev: AP device that has the message queued 1002 * @ap_msg: the message that is to be removed 1003 */ 1004 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg) 1005 { 1006 struct ap_message *tmp; 1007 1008 spin_lock_bh(&ap_dev->lock); 1009 if (!list_empty(&ap_msg->list)) { 1010 list_for_each_entry(tmp, &ap_dev->pendingq, list) 1011 if (tmp->psmid == ap_msg->psmid) { 1012 ap_dev->pendingq_count--; 1013 goto found; 1014 } 1015 ap_dev->requestq_count--; 1016 found: 1017 list_del_init(&ap_msg->list); 1018 } 1019 spin_unlock_bh(&ap_dev->lock); 1020 } 1021 EXPORT_SYMBOL(ap_cancel_message); 1022 1023 /** 1024 * AP receive polling for finished AP requests 1025 */ 1026 static void ap_poll_timeout(unsigned long unused) 1027 { 1028 tasklet_schedule(&ap_tasklet); 1029 } 1030 1031 /** 1032 * Poll all AP devices on the bus in a round robin fashion. Continue 1033 * polling until bit 2^0 of the control flags is not set. If bit 2^1 1034 * of the control flags has been set arm the poll timer. 1035 */ 1036 static int __ap_poll_all(struct device *dev, void *data) 1037 { 1038 struct ap_device *ap_dev = to_ap_dev(dev); 1039 int rc; 1040 1041 spin_lock(&ap_dev->lock); 1042 if (!ap_dev->unregistered) { 1043 rc = ap_poll_queue(to_ap_dev(dev), (unsigned long *) data); 1044 if (rc) 1045 ap_dev->unregistered = 1; 1046 } else 1047 rc = 0; 1048 spin_unlock(&ap_dev->lock); 1049 if (rc) 1050 device_unregister(&ap_dev->device); 1051 return 0; 1052 } 1053 1054 static void ap_poll_all(unsigned long dummy) 1055 { 1056 unsigned long flags; 1057 1058 do { 1059 flags = 0; 1060 bus_for_each_dev(&ap_bus_type, NULL, &flags, __ap_poll_all); 1061 } while (flags & 1); 1062 if (flags & 2) 1063 ap_schedule_poll_timer(); 1064 } 1065 1066 /** 1067 * AP bus poll thread. The purpose of this thread is to poll for 1068 * finished requests in a loop if there is a "free" cpu - that is 1069 * a cpu that doesn't have anything better to do. The polling stops 1070 * as soon as there is another task or if all messages have been 1071 * delivered. 1072 */ 1073 static int ap_poll_thread(void *data) 1074 { 1075 DECLARE_WAITQUEUE(wait, current); 1076 unsigned long flags; 1077 int requests; 1078 1079 set_user_nice(current, 19); 1080 while (1) { 1081 if (need_resched()) { 1082 schedule(); 1083 continue; 1084 } 1085 add_wait_queue(&ap_poll_wait, &wait); 1086 set_current_state(TASK_INTERRUPTIBLE); 1087 if (kthread_should_stop()) 1088 break; 1089 requests = atomic_read(&ap_poll_requests); 1090 if (requests <= 0) 1091 schedule(); 1092 set_current_state(TASK_RUNNING); 1093 remove_wait_queue(&ap_poll_wait, &wait); 1094 1095 local_bh_disable(); 1096 flags = 0; 1097 bus_for_each_dev(&ap_bus_type, NULL, &flags, __ap_poll_all); 1098 local_bh_enable(); 1099 } 1100 set_current_state(TASK_RUNNING); 1101 remove_wait_queue(&ap_poll_wait, &wait); 1102 return 0; 1103 } 1104 1105 static int ap_poll_thread_start(void) 1106 { 1107 int rc; 1108 1109 mutex_lock(&ap_poll_thread_mutex); 1110 if (!ap_poll_kthread) { 1111 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); 1112 rc = IS_ERR(ap_poll_kthread) ? PTR_ERR(ap_poll_kthread) : 0; 1113 if (rc) 1114 ap_poll_kthread = NULL; 1115 } 1116 else 1117 rc = 0; 1118 mutex_unlock(&ap_poll_thread_mutex); 1119 return rc; 1120 } 1121 1122 static void ap_poll_thread_stop(void) 1123 { 1124 mutex_lock(&ap_poll_thread_mutex); 1125 if (ap_poll_kthread) { 1126 kthread_stop(ap_poll_kthread); 1127 ap_poll_kthread = NULL; 1128 } 1129 mutex_unlock(&ap_poll_thread_mutex); 1130 } 1131 1132 static void ap_reset_domain(void) 1133 { 1134 int i; 1135 1136 for (i = 0; i < AP_DEVICES; i++) 1137 ap_reset_queue(AP_MKQID(i, ap_domain_index)); 1138 } 1139 1140 static void ap_reset_all(void) 1141 { 1142 int i, j; 1143 1144 for (i = 0; i < AP_DOMAINS; i++) 1145 for (j = 0; j < AP_DEVICES; j++) 1146 ap_reset_queue(AP_MKQID(j, i)); 1147 } 1148 1149 static struct reset_call ap_reset_call = { 1150 .fn = ap_reset_all, 1151 }; 1152 1153 /** 1154 * The module initialization code. 1155 */ 1156 int __init ap_module_init(void) 1157 { 1158 int rc, i; 1159 1160 if (ap_domain_index < -1 || ap_domain_index >= AP_DOMAINS) { 1161 printk(KERN_WARNING "Invalid param: domain = %d. " 1162 " Not loading.\n", ap_domain_index); 1163 return -EINVAL; 1164 } 1165 if (ap_instructions_available() != 0) { 1166 printk(KERN_WARNING "AP instructions not installed.\n"); 1167 return -ENODEV; 1168 } 1169 register_reset_call(&ap_reset_call); 1170 1171 /* Create /sys/bus/ap. */ 1172 rc = bus_register(&ap_bus_type); 1173 if (rc) 1174 goto out; 1175 for (i = 0; ap_bus_attrs[i]; i++) { 1176 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]); 1177 if (rc) 1178 goto out_bus; 1179 } 1180 1181 /* Create /sys/devices/ap. */ 1182 ap_root_device = s390_root_dev_register("ap"); 1183 rc = IS_ERR(ap_root_device) ? PTR_ERR(ap_root_device) : 0; 1184 if (rc) 1185 goto out_bus; 1186 1187 ap_work_queue = create_singlethread_workqueue("kapwork"); 1188 if (!ap_work_queue) { 1189 rc = -ENOMEM; 1190 goto out_root; 1191 } 1192 1193 if (ap_select_domain() == 0) 1194 ap_scan_bus(NULL); 1195 1196 /* Setup the ap bus rescan timer. */ 1197 init_timer(&ap_config_timer); 1198 ap_config_timer.function = ap_config_timeout; 1199 ap_config_timer.data = 0; 1200 ap_config_timer.expires = jiffies + ap_config_time * HZ; 1201 add_timer(&ap_config_timer); 1202 1203 /* Start the low priority AP bus poll thread. */ 1204 if (ap_thread_flag) { 1205 rc = ap_poll_thread_start(); 1206 if (rc) 1207 goto out_work; 1208 } 1209 1210 return 0; 1211 1212 out_work: 1213 del_timer_sync(&ap_config_timer); 1214 del_timer_sync(&ap_poll_timer); 1215 destroy_workqueue(ap_work_queue); 1216 out_root: 1217 s390_root_dev_unregister(ap_root_device); 1218 out_bus: 1219 while (i--) 1220 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); 1221 bus_unregister(&ap_bus_type); 1222 out: 1223 unregister_reset_call(&ap_reset_call); 1224 return rc; 1225 } 1226 1227 static int __ap_match_all(struct device *dev, void *data) 1228 { 1229 return 1; 1230 } 1231 1232 /** 1233 * The module termination code 1234 */ 1235 void ap_module_exit(void) 1236 { 1237 int i; 1238 struct device *dev; 1239 1240 ap_reset_domain(); 1241 ap_poll_thread_stop(); 1242 del_timer_sync(&ap_config_timer); 1243 del_timer_sync(&ap_poll_timer); 1244 destroy_workqueue(ap_work_queue); 1245 tasklet_kill(&ap_tasklet); 1246 s390_root_dev_unregister(ap_root_device); 1247 while ((dev = bus_find_device(&ap_bus_type, NULL, NULL, 1248 __ap_match_all))) 1249 { 1250 device_unregister(dev); 1251 put_device(dev); 1252 } 1253 for (i = 0; ap_bus_attrs[i]; i++) 1254 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]); 1255 bus_unregister(&ap_bus_type); 1256 unregister_reset_call(&ap_reset_call); 1257 } 1258 1259 #ifndef CONFIG_ZCRYPT_MONOLITHIC 1260 module_init(ap_module_init); 1261 module_exit(ap_module_exit); 1262 #endif 1263