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