7224bf7fa9bf1db740711f6aeab98579329127c4
[deliverable/linux.git] / drivers / s390 / crypto / ap_bus.c
1 /*
2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.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 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
29 #include <linux/kernel_stat.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <linux/suspend.h>
41 #include <asm/reset.h>
42 #include <asm/airq.h>
43 #include <linux/atomic.h>
44 #include <asm/isc.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47 #include <asm/facility.h>
48 #include <linux/crypto.h>
49
50 #include "ap_bus.h"
51
52 /* Some prototypes. */
53 static void ap_scan_bus(struct work_struct *);
54 static void ap_poll_all(unsigned long);
55 static void ap_request_timeout(unsigned long);
56 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags);
57 static int ap_device_remove(struct device *dev);
58 static int ap_device_probe(struct device *dev);
59 static void ap_interrupt_handler(struct airq_struct *airq);
60 static void ap_reset(struct ap_device *ap_dev, unsigned long *flags);
61 static int ap_select_domain(void);
62
63 /*
64 * Module description.
65 */
66 MODULE_AUTHOR("IBM Corporation");
67 MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
68 "Copyright IBM Corp. 2006, 2012");
69 MODULE_LICENSE("GPL");
70 MODULE_ALIAS_CRYPTO("z90crypt");
71
72 /*
73 * Module parameter
74 */
75 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
76 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
77 MODULE_PARM_DESC(domain, "domain index for ap devices");
78 EXPORT_SYMBOL(ap_domain_index);
79
80 static int ap_thread_flag = 0;
81 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
82 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
83
84 static struct device *ap_root_device = NULL;
85 static struct ap_config_info *ap_configuration;
86 static DEFINE_SPINLOCK(ap_device_list_lock);
87 static LIST_HEAD(ap_device_list);
88
89 /*
90 * Workqueue & timer for bus rescan.
91 */
92 static struct workqueue_struct *ap_work_queue;
93 static struct timer_list ap_config_timer;
94 static int ap_config_time = AP_CONFIG_TIME;
95 static DECLARE_WORK(ap_config_work, ap_scan_bus);
96
97 /*
98 * Tasklet & timer for AP request polling and interrupts
99 */
100 static DECLARE_TASKLET(ap_tasklet, ap_poll_all, 0);
101 static atomic_t ap_poll_requests = ATOMIC_INIT(0);
102 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
103 static struct task_struct *ap_poll_kthread = NULL;
104 static DEFINE_MUTEX(ap_poll_thread_mutex);
105 static DEFINE_SPINLOCK(ap_poll_timer_lock);
106 static struct hrtimer ap_poll_timer;
107 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
108 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
109 static unsigned long long poll_timeout = 250000;
110
111 /* Suspend flag */
112 static int ap_suspend_flag;
113 /* Maximum domain id */
114 static int ap_max_domain_id;
115 /* Flag to check if domain was set through module parameter domain=. This is
116 * important when supsend and resume is done in a z/VM environment where the
117 * domain might change. */
118 static int user_set_domain = 0;
119 static struct bus_type ap_bus_type;
120
121 /* Adapter interrupt definitions */
122 static int ap_airq_flag;
123
124 static struct airq_struct ap_airq = {
125 .handler = ap_interrupt_handler,
126 .isc = AP_ISC,
127 };
128
129 /**
130 * ap_using_interrupts() - Returns non-zero if interrupt support is
131 * available.
132 */
133 static inline int ap_using_interrupts(void)
134 {
135 return ap_airq_flag;
136 }
137
138 /**
139 * ap_intructions_available() - Test if AP instructions are available.
140 *
141 * Returns 0 if the AP instructions are installed.
142 */
143 static inline int ap_instructions_available(void)
144 {
145 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
146 register unsigned long reg1 asm ("1") = -ENODEV;
147 register unsigned long reg2 asm ("2") = 0UL;
148
149 asm volatile(
150 " .long 0xb2af0000\n" /* PQAP(TAPQ) */
151 "0: la %1,0\n"
152 "1:\n"
153 EX_TABLE(0b, 1b)
154 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
155 return reg1;
156 }
157
158 /**
159 * ap_interrupts_available(): Test if AP interrupts are available.
160 *
161 * Returns 1 if AP interrupts are available.
162 */
163 static int ap_interrupts_available(void)
164 {
165 return test_facility(65);
166 }
167
168 /**
169 * ap_configuration_available(): Test if AP configuration
170 * information is available.
171 *
172 * Returns 1 if AP configuration information is available.
173 */
174 static int ap_configuration_available(void)
175 {
176 return test_facility(12);
177 }
178
179 /**
180 * ap_test_queue(): Test adjunct processor queue.
181 * @qid: The AP queue number
182 * @info: Pointer to queue descriptor
183 *
184 * Returns AP queue status structure.
185 */
186 static inline struct ap_queue_status
187 ap_test_queue(ap_qid_t qid, unsigned long *info)
188 {
189 register unsigned long reg0 asm ("0") = qid;
190 register struct ap_queue_status reg1 asm ("1");
191 register unsigned long reg2 asm ("2") = 0UL;
192
193 if (test_facility(15))
194 reg0 |= 1UL << 23; /* set APFT T bit*/
195 asm volatile(".long 0xb2af0000" /* PQAP(TAPQ) */
196 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
197 if (info)
198 *info = reg2;
199 return reg1;
200 }
201
202 /**
203 * ap_reset_queue(): Reset adjunct processor queue.
204 * @qid: The AP queue number
205 *
206 * Returns AP queue status structure.
207 */
208 static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
209 {
210 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
211 register struct ap_queue_status reg1 asm ("1");
212 register unsigned long reg2 asm ("2") = 0UL;
213
214 asm volatile(
215 ".long 0xb2af0000" /* PQAP(RAPQ) */
216 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
217 return reg1;
218 }
219
220 /**
221 * ap_queue_interruption_control(): Enable interruption for a specific AP.
222 * @qid: The AP queue number
223 * @ind: The notification indicator byte
224 *
225 * Returns AP queue status.
226 */
227 static inline struct ap_queue_status
228 ap_queue_interruption_control(ap_qid_t qid, void *ind)
229 {
230 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
231 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
232 register struct ap_queue_status reg1_out asm ("1");
233 register void *reg2 asm ("2") = ind;
234 asm volatile(
235 ".long 0xb2af0000" /* PQAP(AQIC) */
236 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
237 :
238 : "cc" );
239 return reg1_out;
240 }
241
242 /**
243 * ap_query_configuration(): Get AP configuration data
244 *
245 * Returns 0 on success, or -EOPNOTSUPP.
246 */
247 static inline int ap_query_configuration(void)
248 {
249 register unsigned long reg0 asm ("0") = 0x04000000UL;
250 register unsigned long reg1 asm ("1") = -EINVAL;
251 register void *reg2 asm ("2") = (void *) ap_configuration;
252
253 if (!ap_configuration)
254 return -EOPNOTSUPP;
255 asm volatile(
256 ".long 0xb2af0000\n" /* PQAP(QCI) */
257 "0: la %1,0\n"
258 "1:\n"
259 EX_TABLE(0b, 1b)
260 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
261 :
262 : "cc");
263
264 return reg1;
265 }
266
267 /**
268 * ap_init_configuration(): Allocate and query configuration array.
269 */
270 static void ap_init_configuration(void)
271 {
272 if (!ap_configuration_available())
273 return;
274
275 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
276 if (!ap_configuration)
277 return;
278 if (ap_query_configuration() != 0) {
279 kfree(ap_configuration);
280 ap_configuration = NULL;
281 return;
282 }
283 }
284
285 /*
286 * ap_test_config(): helper function to extract the nrth bit
287 * within the unsigned int array field.
288 */
289 static inline int ap_test_config(unsigned int *field, unsigned int nr)
290 {
291 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
292 }
293
294 /*
295 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
296 * @id AP card ID
297 *
298 * Returns 0 if the card is not configured
299 * 1 if the card is configured or
300 * if the configuration information is not available
301 */
302 static inline int ap_test_config_card_id(unsigned int id)
303 {
304 if (!ap_configuration) /* QCI not supported */
305 return 1;
306 return ap_test_config(ap_configuration->apm, id);
307 }
308
309 /*
310 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
311 * @domain AP usage domain ID
312 *
313 * Returns 0 if the usage domain is not configured
314 * 1 if the usage domain is configured or
315 * if the configuration information is not available
316 */
317 static inline int ap_test_config_domain(unsigned int domain)
318 {
319 if (!ap_configuration) /* QCI not supported */
320 return domain < 16;
321 return ap_test_config(ap_configuration->aqm, domain);
322 }
323
324 /**
325 * ap_queue_enable_interruption(): Enable interruption on an AP.
326 * @qid: The AP queue number
327 * @ind: the notification indicator byte
328 *
329 * Enables interruption on AP queue via ap_queue_interruption_control(). Based
330 * on the return value it waits a while and tests the AP queue if interrupts
331 * have been switched on using ap_test_queue().
332 */
333 static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind)
334 {
335 struct ap_queue_status status;
336
337 status = ap_queue_interruption_control(ap_dev->qid, ind);
338 switch (status.response_code) {
339 case AP_RESPONSE_NORMAL:
340 case AP_RESPONSE_OTHERWISE_CHANGED:
341 return 0;
342 case AP_RESPONSE_Q_NOT_AVAIL:
343 case AP_RESPONSE_DECONFIGURED:
344 case AP_RESPONSE_CHECKSTOPPED:
345 case AP_RESPONSE_INVALID_ADDRESS:
346 return -ENODEV;
347 case AP_RESPONSE_RESET_IN_PROGRESS:
348 case AP_RESPONSE_BUSY:
349 default:
350 return -EBUSY;
351 }
352 }
353
354 /**
355 * __ap_send(): Send message to adjunct processor queue.
356 * @qid: The AP queue number
357 * @psmid: The program supplied message identifier
358 * @msg: The message text
359 * @length: The message length
360 * @special: Special Bit
361 *
362 * Returns AP queue status structure.
363 * Condition code 1 on NQAP can't happen because the L bit is 1.
364 * Condition code 2 on NQAP also means the send is incomplete,
365 * because a segment boundary was reached. The NQAP is repeated.
366 */
367 static inline struct ap_queue_status
368 __ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
369 unsigned int special)
370 {
371 typedef struct { char _[length]; } msgblock;
372 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
373 register struct ap_queue_status reg1 asm ("1");
374 register unsigned long reg2 asm ("2") = (unsigned long) msg;
375 register unsigned long reg3 asm ("3") = (unsigned long) length;
376 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
377 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
378
379 if (special == 1)
380 reg0 |= 0x400000UL;
381
382 asm volatile (
383 "0: .long 0xb2ad0042\n" /* NQAP */
384 " brc 2,0b"
385 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
386 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
387 : "cc" );
388 return reg1;
389 }
390
391 int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
392 {
393 struct ap_queue_status status;
394
395 status = __ap_send(qid, psmid, msg, length, 0);
396 switch (status.response_code) {
397 case AP_RESPONSE_NORMAL:
398 return 0;
399 case AP_RESPONSE_Q_FULL:
400 case AP_RESPONSE_RESET_IN_PROGRESS:
401 return -EBUSY;
402 case AP_RESPONSE_REQ_FAC_NOT_INST:
403 return -EINVAL;
404 default: /* Device is gone. */
405 return -ENODEV;
406 }
407 }
408 EXPORT_SYMBOL(ap_send);
409
410 /**
411 * __ap_recv(): Receive message from adjunct processor queue.
412 * @qid: The AP queue number
413 * @psmid: Pointer to program supplied message identifier
414 * @msg: The message text
415 * @length: The message length
416 *
417 * Returns AP queue status structure.
418 * Condition code 1 on DQAP means the receive has taken place
419 * but only partially. The response is incomplete, hence the
420 * DQAP is repeated.
421 * Condition code 2 on DQAP also means the receive is incomplete,
422 * this time because a segment boundary was reached. Again, the
423 * DQAP is repeated.
424 * Note that gpr2 is used by the DQAP instruction to keep track of
425 * any 'residual' length, in case the instruction gets interrupted.
426 * Hence it gets zeroed before the instruction.
427 */
428 static inline struct ap_queue_status
429 __ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
430 {
431 typedef struct { char _[length]; } msgblock;
432 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
433 register struct ap_queue_status reg1 asm ("1");
434 register unsigned long reg2 asm("2") = 0UL;
435 register unsigned long reg4 asm("4") = (unsigned long) msg;
436 register unsigned long reg5 asm("5") = (unsigned long) length;
437 register unsigned long reg6 asm("6") = 0UL;
438 register unsigned long reg7 asm("7") = 0UL;
439
440
441 asm volatile(
442 "0: .long 0xb2ae0064\n" /* DQAP */
443 " brc 6,0b\n"
444 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
445 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
446 "=m" (*(msgblock *) msg) : : "cc" );
447 *psmid = (((unsigned long long) reg6) << 32) + reg7;
448 return reg1;
449 }
450
451 int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
452 {
453 struct ap_queue_status status;
454
455 status = __ap_recv(qid, psmid, msg, length);
456 switch (status.response_code) {
457 case AP_RESPONSE_NORMAL:
458 return 0;
459 case AP_RESPONSE_NO_PENDING_REPLY:
460 if (status.queue_empty)
461 return -ENOENT;
462 return -EBUSY;
463 case AP_RESPONSE_RESET_IN_PROGRESS:
464 return -EBUSY;
465 default:
466 return -ENODEV;
467 }
468 }
469 EXPORT_SYMBOL(ap_recv);
470
471 /**
472 * __ap_schedule_poll_timer(): Schedule poll timer.
473 *
474 * Set up the timer to run the poll tasklet
475 */
476 static inline void __ap_schedule_poll_timer(void)
477 {
478 ktime_t hr_time;
479
480 spin_lock_bh(&ap_poll_timer_lock);
481 if (!hrtimer_is_queued(&ap_poll_timer) && !ap_suspend_flag) {
482 hr_time = ktime_set(0, poll_timeout);
483 hrtimer_forward_now(&ap_poll_timer, hr_time);
484 hrtimer_restart(&ap_poll_timer);
485 }
486 spin_unlock_bh(&ap_poll_timer_lock);
487 }
488
489 /**
490 * ap_schedule_poll_timer(): Schedule poll timer.
491 *
492 * Set up the timer to run the poll tasklet
493 */
494 static inline void ap_schedule_poll_timer(void)
495 {
496 if (ap_using_interrupts())
497 return;
498 __ap_schedule_poll_timer();
499 }
500
501
502 /**
503 * ap_query_queue(): Check if an AP queue is available.
504 * @qid: The AP queue number
505 * @queue_depth: Pointer to queue depth value
506 * @device_type: Pointer to device type value
507 * @facilities: Pointer to facility indicator
508 */
509 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
510 unsigned int *facilities)
511 {
512 struct ap_queue_status status;
513 unsigned long info;
514 int nd;
515
516 if (!ap_test_config_card_id(AP_QID_DEVICE(qid)))
517 return -ENODEV;
518
519 status = ap_test_queue(qid, &info);
520 switch (status.response_code) {
521 case AP_RESPONSE_NORMAL:
522 *queue_depth = (int)(info & 0xff);
523 *device_type = (int)((info >> 24) & 0xff);
524 *facilities = (unsigned int)(info >> 32);
525 /* Update maximum domain id */
526 nd = (info >> 16) & 0xff;
527 if ((info & (1UL << 57)) && nd > 0)
528 ap_max_domain_id = nd;
529 return 0;
530 case AP_RESPONSE_Q_NOT_AVAIL:
531 case AP_RESPONSE_DECONFIGURED:
532 case AP_RESPONSE_CHECKSTOPPED:
533 case AP_RESPONSE_INVALID_ADDRESS:
534 return -ENODEV;
535 case AP_RESPONSE_RESET_IN_PROGRESS:
536 case AP_RESPONSE_OTHERWISE_CHANGED:
537 case AP_RESPONSE_BUSY:
538 return -EBUSY;
539 default:
540 BUG();
541 }
542 }
543
544 /**
545 * ap_init_queue(): Reset an AP queue.
546 * @qid: The AP queue number
547 *
548 * Submit the Reset command to an AP queue.
549 * Since the reset is asynchron set the state to 'RESET_IN_PROGRESS'
550 * and check later via ap_poll_queue() if the reset is done.
551 */
552 static int ap_init_queue(struct ap_device *ap_dev)
553 {
554 struct ap_queue_status status;
555
556 status = ap_reset_queue(ap_dev->qid);
557 switch (status.response_code) {
558 case AP_RESPONSE_NORMAL:
559 ap_dev->interrupt = AP_INTR_DISABLED;
560 ap_dev->reset = AP_RESET_IN_PROGRESS;
561 return 0;
562 case AP_RESPONSE_RESET_IN_PROGRESS:
563 case AP_RESPONSE_BUSY:
564 return -EBUSY;
565 case AP_RESPONSE_Q_NOT_AVAIL:
566 case AP_RESPONSE_DECONFIGURED:
567 case AP_RESPONSE_CHECKSTOPPED:
568 default:
569 return -ENODEV;
570 }
571 }
572
573 /**
574 * ap_increase_queue_count(): Arm request timeout.
575 * @ap_dev: Pointer to an AP device.
576 *
577 * Arm request timeout if an AP device was idle and a new request is submitted.
578 */
579 static void ap_increase_queue_count(struct ap_device *ap_dev)
580 {
581 int timeout = ap_dev->drv->request_timeout;
582
583 ap_dev->queue_count++;
584 if (ap_dev->queue_count == 1) {
585 mod_timer(&ap_dev->timeout, jiffies + timeout);
586 ap_dev->reset = AP_RESET_ARMED;
587 }
588 }
589
590 /**
591 * ap_decrease_queue_count(): Decrease queue count.
592 * @ap_dev: Pointer to an AP device.
593 *
594 * If AP device is still alive, re-schedule request timeout if there are still
595 * pending requests.
596 */
597 static void ap_decrease_queue_count(struct ap_device *ap_dev)
598 {
599 int timeout = ap_dev->drv->request_timeout;
600
601 ap_dev->queue_count--;
602 if (ap_dev->queue_count > 0)
603 mod_timer(&ap_dev->timeout, jiffies + timeout);
604 else
605 /*
606 * The timeout timer should to be disabled now - since
607 * del_timer_sync() is very expensive, we just tell via the
608 * reset flag to ignore the pending timeout timer.
609 */
610 ap_dev->reset = AP_RESET_IGNORE;
611 }
612
613 /**
614 * ap_poll_thread(): Thread that polls for finished requests.
615 * @data: Unused pointer
616 *
617 * AP bus poll thread. The purpose of this thread is to poll for
618 * finished requests in a loop if there is a "free" cpu - that is
619 * a cpu that doesn't have anything better to do. The polling stops
620 * as soon as there is another task or if all messages have been
621 * delivered.
622 */
623 static int ap_poll_thread(void *data)
624 {
625 DECLARE_WAITQUEUE(wait, current);
626 unsigned long flags;
627 struct ap_device *ap_dev;
628
629 set_user_nice(current, MAX_NICE);
630 set_freezable();
631 while (!kthread_should_stop()) {
632 add_wait_queue(&ap_poll_wait, &wait);
633 set_current_state(TASK_INTERRUPTIBLE);
634 if (ap_suspend_flag ||
635 atomic_read(&ap_poll_requests) <= 0) {
636 schedule();
637 try_to_freeze();
638 }
639 set_current_state(TASK_RUNNING);
640 remove_wait_queue(&ap_poll_wait, &wait);
641
642 if (need_resched()) {
643 schedule();
644 try_to_freeze();
645 continue;
646 }
647
648 flags = 0;
649 spin_lock_bh(&ap_device_list_lock);
650 list_for_each_entry(ap_dev, &ap_device_list, list) {
651 spin_lock(&ap_dev->lock);
652 __ap_poll_device(ap_dev, &flags);
653 spin_unlock(&ap_dev->lock);
654 }
655 spin_unlock_bh(&ap_device_list_lock);
656 } while (!kthread_should_stop());
657 return 0;
658 }
659
660 static int ap_poll_thread_start(void)
661 {
662 int rc;
663
664 if (ap_using_interrupts() || ap_poll_kthread)
665 return 0;
666 mutex_lock(&ap_poll_thread_mutex);
667 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
668 rc = PTR_RET(ap_poll_kthread);
669 if (rc)
670 ap_poll_kthread = NULL;
671 mutex_unlock(&ap_poll_thread_mutex);
672 return rc;
673 }
674
675 static void ap_poll_thread_stop(void)
676 {
677 if (!ap_poll_kthread)
678 return;
679 mutex_lock(&ap_poll_thread_mutex);
680 kthread_stop(ap_poll_kthread);
681 ap_poll_kthread = NULL;
682 mutex_unlock(&ap_poll_thread_mutex);
683 }
684
685 /*
686 * AP device related attributes.
687 */
688 static ssize_t ap_hwtype_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
690 {
691 struct ap_device *ap_dev = to_ap_dev(dev);
692 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
693 }
694
695 static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
696
697 static ssize_t ap_raw_hwtype_show(struct device *dev,
698 struct device_attribute *attr, char *buf)
699 {
700 struct ap_device *ap_dev = to_ap_dev(dev);
701
702 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
703 }
704
705 static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
706
707 static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
708 char *buf)
709 {
710 struct ap_device *ap_dev = to_ap_dev(dev);
711 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
712 }
713
714 static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
715 static ssize_t ap_request_count_show(struct device *dev,
716 struct device_attribute *attr,
717 char *buf)
718 {
719 struct ap_device *ap_dev = to_ap_dev(dev);
720 int rc;
721
722 spin_lock_bh(&ap_dev->lock);
723 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
724 spin_unlock_bh(&ap_dev->lock);
725 return rc;
726 }
727
728 static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
729
730 static ssize_t ap_requestq_count_show(struct device *dev,
731 struct device_attribute *attr, char *buf)
732 {
733 struct ap_device *ap_dev = to_ap_dev(dev);
734 int rc;
735
736 spin_lock_bh(&ap_dev->lock);
737 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
738 spin_unlock_bh(&ap_dev->lock);
739 return rc;
740 }
741
742 static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
743
744 static ssize_t ap_pendingq_count_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
746 {
747 struct ap_device *ap_dev = to_ap_dev(dev);
748 int rc;
749
750 spin_lock_bh(&ap_dev->lock);
751 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
752 spin_unlock_bh(&ap_dev->lock);
753 return rc;
754 }
755
756 static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
757
758 static ssize_t ap_reset_show(struct device *dev,
759 struct device_attribute *attr, char *buf)
760 {
761 struct ap_device *ap_dev = to_ap_dev(dev);
762 int rc = 0;
763
764 spin_lock_bh(&ap_dev->lock);
765 switch (ap_dev->reset) {
766 case AP_RESET_IGNORE:
767 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
768 break;
769 case AP_RESET_ARMED:
770 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
771 break;
772 case AP_RESET_DO:
773 rc = snprintf(buf, PAGE_SIZE, "Reset Timer expired.\n");
774 break;
775 case AP_RESET_IN_PROGRESS:
776 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
777 break;
778 default:
779 break;
780 }
781 spin_unlock_bh(&ap_dev->lock);
782 return rc;
783 }
784
785 static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
786
787 static ssize_t ap_interrupt_show(struct device *dev,
788 struct device_attribute *attr, char *buf)
789 {
790 struct ap_device *ap_dev = to_ap_dev(dev);
791 int rc = 0;
792
793 spin_lock_bh(&ap_dev->lock);
794 switch (ap_dev->interrupt) {
795 case AP_INTR_DISABLED:
796 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
797 break;
798 case AP_INTR_ENABLED:
799 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
800 break;
801 case AP_INTR_IN_PROGRESS:
802 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
803 break;
804 }
805 spin_unlock_bh(&ap_dev->lock);
806 return rc;
807 }
808
809 static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
810
811 static ssize_t ap_modalias_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
813 {
814 return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type);
815 }
816
817 static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
818
819 static ssize_t ap_functions_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
821 {
822 struct ap_device *ap_dev = to_ap_dev(dev);
823 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
824 }
825
826 static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
827
828 static struct attribute *ap_dev_attrs[] = {
829 &dev_attr_hwtype.attr,
830 &dev_attr_raw_hwtype.attr,
831 &dev_attr_depth.attr,
832 &dev_attr_request_count.attr,
833 &dev_attr_requestq_count.attr,
834 &dev_attr_pendingq_count.attr,
835 &dev_attr_reset.attr,
836 &dev_attr_interrupt.attr,
837 &dev_attr_modalias.attr,
838 &dev_attr_ap_functions.attr,
839 NULL
840 };
841 static struct attribute_group ap_dev_attr_group = {
842 .attrs = ap_dev_attrs
843 };
844
845 /**
846 * ap_bus_match()
847 * @dev: Pointer to device
848 * @drv: Pointer to device_driver
849 *
850 * AP bus driver registration/unregistration.
851 */
852 static int ap_bus_match(struct device *dev, struct device_driver *drv)
853 {
854 struct ap_device *ap_dev = to_ap_dev(dev);
855 struct ap_driver *ap_drv = to_ap_drv(drv);
856 struct ap_device_id *id;
857
858 /*
859 * Compare device type of the device with the list of
860 * supported types of the device_driver.
861 */
862 for (id = ap_drv->ids; id->match_flags; id++) {
863 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
864 (id->dev_type != ap_dev->device_type))
865 continue;
866 return 1;
867 }
868 return 0;
869 }
870
871 /**
872 * ap_uevent(): Uevent function for AP devices.
873 * @dev: Pointer to device
874 * @env: Pointer to kobj_uevent_env
875 *
876 * It sets up a single environment variable DEV_TYPE which contains the
877 * hardware device type.
878 */
879 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
880 {
881 struct ap_device *ap_dev = to_ap_dev(dev);
882 int retval = 0;
883
884 if (!ap_dev)
885 return -ENODEV;
886
887 /* Set up DEV_TYPE environment variable. */
888 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
889 if (retval)
890 return retval;
891
892 /* Add MODALIAS= */
893 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
894
895 return retval;
896 }
897
898 static int ap_dev_suspend(struct device *dev, pm_message_t state)
899 {
900 struct ap_device *ap_dev = to_ap_dev(dev);
901 unsigned long flags;
902
903 /* Poll on the device until all requests are finished. */
904 do {
905 flags = 0;
906 spin_lock_bh(&ap_dev->lock);
907 __ap_poll_device(ap_dev, &flags);
908 spin_unlock_bh(&ap_dev->lock);
909 } while ((flags & 1) || (flags & 2));
910
911 return 0;
912 }
913
914 static int ap_dev_resume(struct device *dev)
915 {
916 return 0;
917 }
918
919 static void ap_bus_suspend(void)
920 {
921 ap_suspend_flag = 1;
922 /*
923 * Disable scanning for devices, thus we do not want to scan
924 * for them after removing.
925 */
926 flush_workqueue(ap_work_queue);
927 tasklet_disable(&ap_tasklet);
928 }
929
930 static int __ap_devices_unregister(struct device *dev, void *dummy)
931 {
932 device_unregister(dev);
933 return 0;
934 }
935
936 static void ap_bus_resume(void)
937 {
938 int rc;
939
940 /* Unconditionally remove all AP devices */
941 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
942 /* Reset thin interrupt setting */
943 if (ap_interrupts_available() && !ap_using_interrupts()) {
944 rc = register_adapter_interrupt(&ap_airq);
945 ap_airq_flag = (rc == 0);
946 }
947 if (!ap_interrupts_available() && ap_using_interrupts()) {
948 unregister_adapter_interrupt(&ap_airq);
949 ap_airq_flag = 0;
950 }
951 /* Reset domain */
952 if (!user_set_domain)
953 ap_domain_index = -1;
954 /* Get things going again */
955 ap_suspend_flag = 0;
956 if (ap_airq_flag)
957 xchg(ap_airq.lsi_ptr, 0);
958 tasklet_enable(&ap_tasklet);
959 queue_work(ap_work_queue, &ap_config_work);
960 wake_up(&ap_poll_wait);
961 }
962
963 static int ap_power_event(struct notifier_block *this, unsigned long event,
964 void *ptr)
965 {
966 switch (event) {
967 case PM_HIBERNATION_PREPARE:
968 case PM_SUSPEND_PREPARE:
969 ap_bus_suspend();
970 break;
971 case PM_POST_HIBERNATION:
972 case PM_POST_SUSPEND:
973 ap_bus_resume();
974 break;
975 default:
976 break;
977 }
978 return NOTIFY_DONE;
979 }
980 static struct notifier_block ap_power_notifier = {
981 .notifier_call = ap_power_event,
982 };
983
984 static struct bus_type ap_bus_type = {
985 .name = "ap",
986 .match = &ap_bus_match,
987 .uevent = &ap_uevent,
988 .suspend = ap_dev_suspend,
989 .resume = ap_dev_resume,
990 };
991
992 static int ap_device_probe(struct device *dev)
993 {
994 struct ap_device *ap_dev = to_ap_dev(dev);
995 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
996 int rc;
997
998 ap_dev->drv = ap_drv;
999
1000 spin_lock_bh(&ap_device_list_lock);
1001 list_add(&ap_dev->list, &ap_device_list);
1002 spin_unlock_bh(&ap_device_list_lock);
1003
1004 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
1005 if (rc) {
1006 spin_lock_bh(&ap_device_list_lock);
1007 list_del_init(&ap_dev->list);
1008 spin_unlock_bh(&ap_device_list_lock);
1009 } else {
1010 if (ap_dev->reset == AP_RESET_IN_PROGRESS ||
1011 ap_dev->interrupt == AP_INTR_IN_PROGRESS)
1012 __ap_schedule_poll_timer();
1013 }
1014 return rc;
1015 }
1016
1017 /**
1018 * __ap_flush_queue(): Flush requests.
1019 * @ap_dev: Pointer to the AP device
1020 *
1021 * Flush all requests from the request/pending queue of an AP device.
1022 */
1023 static void __ap_flush_queue(struct ap_device *ap_dev)
1024 {
1025 struct ap_message *ap_msg, *next;
1026
1027 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
1028 list_del_init(&ap_msg->list);
1029 ap_dev->pendingq_count--;
1030 ap_msg->rc = -EAGAIN;
1031 ap_msg->receive(ap_dev, ap_msg, NULL);
1032 }
1033 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
1034 list_del_init(&ap_msg->list);
1035 ap_dev->requestq_count--;
1036 ap_msg->rc = -EAGAIN;
1037 ap_msg->receive(ap_dev, ap_msg, NULL);
1038 }
1039 }
1040
1041 void ap_flush_queue(struct ap_device *ap_dev)
1042 {
1043 spin_lock_bh(&ap_dev->lock);
1044 __ap_flush_queue(ap_dev);
1045 spin_unlock_bh(&ap_dev->lock);
1046 }
1047 EXPORT_SYMBOL(ap_flush_queue);
1048
1049 static int ap_device_remove(struct device *dev)
1050 {
1051 struct ap_device *ap_dev = to_ap_dev(dev);
1052 struct ap_driver *ap_drv = ap_dev->drv;
1053
1054 ap_flush_queue(ap_dev);
1055 del_timer_sync(&ap_dev->timeout);
1056 spin_lock_bh(&ap_device_list_lock);
1057 list_del_init(&ap_dev->list);
1058 spin_unlock_bh(&ap_device_list_lock);
1059 if (ap_drv->remove)
1060 ap_drv->remove(ap_dev);
1061 spin_lock_bh(&ap_dev->lock);
1062 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1063 spin_unlock_bh(&ap_dev->lock);
1064 return 0;
1065 }
1066
1067 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1068 char *name)
1069 {
1070 struct device_driver *drv = &ap_drv->driver;
1071
1072 drv->bus = &ap_bus_type;
1073 drv->probe = ap_device_probe;
1074 drv->remove = ap_device_remove;
1075 drv->owner = owner;
1076 drv->name = name;
1077 return driver_register(drv);
1078 }
1079 EXPORT_SYMBOL(ap_driver_register);
1080
1081 void ap_driver_unregister(struct ap_driver *ap_drv)
1082 {
1083 driver_unregister(&ap_drv->driver);
1084 }
1085 EXPORT_SYMBOL(ap_driver_unregister);
1086
1087 void ap_bus_force_rescan(void)
1088 {
1089 if (ap_suspend_flag)
1090 return;
1091 /* processing a asynchronous bus rescan */
1092 del_timer(&ap_config_timer);
1093 queue_work(ap_work_queue, &ap_config_work);
1094 flush_work(&ap_config_work);
1095 }
1096 EXPORT_SYMBOL(ap_bus_force_rescan);
1097
1098 /*
1099 * AP bus attributes.
1100 */
1101 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1102 {
1103 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1104 }
1105
1106 static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
1107
1108 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1109 {
1110 if (!ap_configuration) /* QCI not supported */
1111 return snprintf(buf, PAGE_SIZE, "not supported\n");
1112 if (!test_facility(76))
1113 /* format 0 - 16 bit domain field */
1114 return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
1115 ap_configuration->adm[0],
1116 ap_configuration->adm[1]);
1117 /* format 1 - 256 bit domain field */
1118 return snprintf(buf, PAGE_SIZE,
1119 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1120 ap_configuration->adm[0], ap_configuration->adm[1],
1121 ap_configuration->adm[2], ap_configuration->adm[3],
1122 ap_configuration->adm[4], ap_configuration->adm[5],
1123 ap_configuration->adm[6], ap_configuration->adm[7]);
1124 }
1125
1126 static BUS_ATTR(ap_control_domain_mask, 0444,
1127 ap_control_domain_mask_show, NULL);
1128
1129 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
1130 {
1131 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1132 }
1133
1134 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1135 {
1136 return snprintf(buf, PAGE_SIZE, "%d\n",
1137 ap_using_interrupts() ? 1 : 0);
1138 }
1139
1140 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1141
1142 static ssize_t ap_config_time_store(struct bus_type *bus,
1143 const char *buf, size_t count)
1144 {
1145 int time;
1146
1147 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1148 return -EINVAL;
1149 ap_config_time = time;
1150 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1151 return count;
1152 }
1153
1154 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1155
1156 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1157 {
1158 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1159 }
1160
1161 static ssize_t ap_poll_thread_store(struct bus_type *bus,
1162 const char *buf, size_t count)
1163 {
1164 int flag, rc;
1165
1166 if (sscanf(buf, "%d\n", &flag) != 1)
1167 return -EINVAL;
1168 if (flag) {
1169 rc = ap_poll_thread_start();
1170 if (rc)
1171 count = rc;
1172 } else
1173 ap_poll_thread_stop();
1174 return count;
1175 }
1176
1177 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1178
1179 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1180 {
1181 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1182 }
1183
1184 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1185 size_t count)
1186 {
1187 unsigned long long time;
1188 ktime_t hr_time;
1189
1190 /* 120 seconds = maximum poll interval */
1191 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1192 time > 120000000000ULL)
1193 return -EINVAL;
1194 poll_timeout = time;
1195 hr_time = ktime_set(0, poll_timeout);
1196
1197 spin_lock_bh(&ap_poll_timer_lock);
1198 hrtimer_cancel(&ap_poll_timer);
1199 hrtimer_set_expires(&ap_poll_timer, hr_time);
1200 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1201 spin_unlock_bh(&ap_poll_timer_lock);
1202
1203 return count;
1204 }
1205
1206 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1207
1208 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1209 {
1210 int max_domain_id;
1211
1212 if (ap_configuration)
1213 max_domain_id = ap_max_domain_id ? : -1;
1214 else
1215 max_domain_id = 15;
1216 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1217 }
1218
1219 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
1220
1221 static struct bus_attribute *const ap_bus_attrs[] = {
1222 &bus_attr_ap_domain,
1223 &bus_attr_ap_control_domain_mask,
1224 &bus_attr_config_time,
1225 &bus_attr_poll_thread,
1226 &bus_attr_ap_interrupts,
1227 &bus_attr_poll_timeout,
1228 &bus_attr_ap_max_domain_id,
1229 NULL,
1230 };
1231
1232 /**
1233 * ap_select_domain(): Select an AP domain.
1234 *
1235 * Pick one of the 16 AP domains.
1236 */
1237 static int ap_select_domain(void)
1238 {
1239 int count, max_count, best_domain;
1240 struct ap_queue_status status;
1241 int i, j;
1242
1243 /*
1244 * We want to use a single domain. Either the one specified with
1245 * the "domain=" parameter or the domain with the maximum number
1246 * of devices.
1247 */
1248 if (ap_domain_index >= 0)
1249 /* Domain has already been selected. */
1250 return 0;
1251 best_domain = -1;
1252 max_count = 0;
1253 for (i = 0; i < AP_DOMAINS; i++) {
1254 if (!ap_test_config_domain(i))
1255 continue;
1256 count = 0;
1257 for (j = 0; j < AP_DEVICES; j++) {
1258 if (!ap_test_config_card_id(j))
1259 continue;
1260 status = ap_test_queue(AP_MKQID(j, i), NULL);
1261 if (status.response_code != AP_RESPONSE_NORMAL)
1262 continue;
1263 count++;
1264 }
1265 if (count > max_count) {
1266 max_count = count;
1267 best_domain = i;
1268 }
1269 }
1270 if (best_domain >= 0){
1271 ap_domain_index = best_domain;
1272 return 0;
1273 }
1274 return -ENODEV;
1275 }
1276
1277 /**
1278 * ap_probe_device_type(): Find the device type of an AP.
1279 * @ap_dev: pointer to the AP device.
1280 *
1281 * Find the device type if query queue returned a device type of 0.
1282 */
1283 static int ap_probe_device_type(struct ap_device *ap_dev)
1284 {
1285 static unsigned char msg[] = {
1286 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
1287 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1288 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,
1289 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1290 0x01,0x00,0x43,0x43,0x41,0x2d,0x41,0x50,
1291 0x50,0x4c,0x20,0x20,0x20,0x01,0x01,0x01,
1292 0x00,0x00,0x00,0x00,0x50,0x4b,0x00,0x00,
1293 0x00,0x00,0x01,0x1c,0x00,0x00,0x00,0x00,
1294 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1295 0x00,0x00,0x05,0xb8,0x00,0x00,0x00,0x00,
1296 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1297 0x70,0x00,0x41,0x00,0x00,0x00,0x00,0x00,
1298 0x00,0x00,0x54,0x32,0x01,0x00,0xa0,0x00,
1299 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1300 0x00,0x00,0x00,0x00,0xb8,0x05,0x00,0x00,
1301 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1302 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1303 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1304 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1305 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1306 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1307 0x00,0x00,0x0a,0x00,0x00,0x00,0x00,0x00,
1308 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
1309 0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,
1310 0x49,0x43,0x53,0x46,0x20,0x20,0x20,0x20,
1311 0x50,0x4b,0x0a,0x00,0x50,0x4b,0x43,0x53,
1312 0x2d,0x31,0x2e,0x32,0x37,0x00,0x11,0x22,
1313 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1314 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,
1315 0x99,0x00,0x11,0x22,0x33,0x44,0x55,0x66,
1316 0x77,0x88,0x99,0x00,0x11,0x22,0x33,0x44,
1317 0x55,0x66,0x77,0x88,0x99,0x00,0x11,0x22,
1318 0x33,0x44,0x55,0x66,0x77,0x88,0x99,0x00,
1319 0x11,0x22,0x33,0x5d,0x00,0x5b,0x00,0x77,
1320 0x88,0x1e,0x00,0x00,0x57,0x00,0x00,0x00,
1321 0x00,0x04,0x00,0x00,0x4f,0x00,0x00,0x00,
1322 0x03,0x02,0x00,0x00,0x40,0x01,0x00,0x01,
1323 0xce,0x02,0x68,0x2d,0x5f,0xa9,0xde,0x0c,
1324 0xf6,0xd2,0x7b,0x58,0x4b,0xf9,0x28,0x68,
1325 0x3d,0xb4,0xf4,0xef,0x78,0xd5,0xbe,0x66,
1326 0x63,0x42,0xef,0xf8,0xfd,0xa4,0xf8,0xb0,
1327 0x8e,0x29,0xc2,0xc9,0x2e,0xd8,0x45,0xb8,
1328 0x53,0x8c,0x6f,0x4e,0x72,0x8f,0x6c,0x04,
1329 0x9c,0x88,0xfc,0x1e,0xc5,0x83,0x55,0x57,
1330 0xf7,0xdd,0xfd,0x4f,0x11,0x36,0x95,0x5d,
1331 };
1332 struct ap_queue_status status;
1333 unsigned long long psmid;
1334 char *reply;
1335 int rc, i;
1336
1337 reply = (void *) get_zeroed_page(GFP_KERNEL);
1338 if (!reply) {
1339 rc = -ENOMEM;
1340 goto out;
1341 }
1342
1343 status = __ap_send(ap_dev->qid, 0x0102030405060708ULL,
1344 msg, sizeof(msg), 0);
1345 if (status.response_code != AP_RESPONSE_NORMAL) {
1346 rc = -ENODEV;
1347 goto out_free;
1348 }
1349
1350 /* Wait for the test message to complete. */
1351 for (i = 0; i < 6; i++) {
1352 msleep(300);
1353 status = __ap_recv(ap_dev->qid, &psmid, reply, 4096);
1354 if (status.response_code == AP_RESPONSE_NORMAL &&
1355 psmid == 0x0102030405060708ULL)
1356 break;
1357 }
1358 if (i < 6) {
1359 /* Got an answer. */
1360 if (reply[0] == 0x00 && reply[1] == 0x86)
1361 ap_dev->device_type = AP_DEVICE_TYPE_PCICC;
1362 else
1363 ap_dev->device_type = AP_DEVICE_TYPE_PCICA;
1364 rc = 0;
1365 } else
1366 rc = -ENODEV;
1367
1368 out_free:
1369 free_page((unsigned long) reply);
1370 out:
1371 return rc;
1372 }
1373
1374 static void ap_interrupt_handler(struct airq_struct *airq)
1375 {
1376 inc_irq_stat(IRQIO_APB);
1377 if (!ap_suspend_flag)
1378 tasklet_schedule(&ap_tasklet);
1379 }
1380
1381 /**
1382 * __ap_scan_bus(): Scan the AP bus.
1383 * @dev: Pointer to device
1384 * @data: Pointer to data
1385 *
1386 * Scan the AP bus for new devices.
1387 */
1388 static int __ap_scan_bus(struct device *dev, void *data)
1389 {
1390 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1391 }
1392
1393 static void ap_device_release(struct device *dev)
1394 {
1395 struct ap_device *ap_dev = to_ap_dev(dev);
1396
1397 kfree(ap_dev);
1398 }
1399
1400 static void ap_scan_bus(struct work_struct *unused)
1401 {
1402 struct ap_device *ap_dev;
1403 struct device *dev;
1404 ap_qid_t qid;
1405 int queue_depth = 0, device_type = 0;
1406 unsigned int device_functions;
1407 int rc, i;
1408
1409 ap_query_configuration();
1410 if (ap_select_domain() != 0)
1411 goto out;
1412
1413 for (i = 0; i < AP_DEVICES; i++) {
1414 qid = AP_MKQID(i, ap_domain_index);
1415 dev = bus_find_device(&ap_bus_type, NULL,
1416 (void *)(unsigned long)qid,
1417 __ap_scan_bus);
1418 rc = ap_query_queue(qid, &queue_depth, &device_type,
1419 &device_functions);
1420 if (dev) {
1421 ap_dev = to_ap_dev(dev);
1422 spin_lock_bh(&ap_dev->lock);
1423 if (rc == -ENODEV || ap_dev->unregistered) {
1424 spin_unlock_bh(&ap_dev->lock);
1425 if (ap_dev->unregistered)
1426 i--;
1427 device_unregister(dev);
1428 put_device(dev);
1429 continue;
1430 }
1431 spin_unlock_bh(&ap_dev->lock);
1432 put_device(dev);
1433 continue;
1434 }
1435 if (rc)
1436 continue;
1437 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1438 if (!ap_dev)
1439 break;
1440 ap_dev->qid = qid;
1441 rc = ap_init_queue(ap_dev);
1442 if ((rc != 0) && (rc != -EBUSY)) {
1443 kfree(ap_dev);
1444 continue;
1445 }
1446 ap_dev->queue_depth = queue_depth;
1447 ap_dev->raw_hwtype = device_type;
1448 ap_dev->device_type = device_type;
1449 ap_dev->functions = device_functions;
1450 ap_dev->unregistered = 1;
1451 spin_lock_init(&ap_dev->lock);
1452 INIT_LIST_HEAD(&ap_dev->pendingq);
1453 INIT_LIST_HEAD(&ap_dev->requestq);
1454 INIT_LIST_HEAD(&ap_dev->list);
1455 setup_timer(&ap_dev->timeout, ap_request_timeout,
1456 (unsigned long) ap_dev);
1457 if (ap_dev->device_type == 0)
1458 /* device type probing for old cards */
1459 if (ap_probe_device_type(ap_dev)) {
1460 kfree(ap_dev);
1461 continue;
1462 }
1463
1464 ap_dev->device.bus = &ap_bus_type;
1465 ap_dev->device.parent = ap_root_device;
1466 if (dev_set_name(&ap_dev->device, "card%02x",
1467 AP_QID_DEVICE(ap_dev->qid))) {
1468 kfree(ap_dev);
1469 continue;
1470 }
1471 ap_dev->device.release = ap_device_release;
1472 rc = device_register(&ap_dev->device);
1473 if (rc) {
1474 put_device(&ap_dev->device);
1475 continue;
1476 }
1477 /* Add device attributes. */
1478 rc = sysfs_create_group(&ap_dev->device.kobj,
1479 &ap_dev_attr_group);
1480 if (!rc) {
1481 spin_lock_bh(&ap_dev->lock);
1482 ap_dev->unregistered = 0;
1483 spin_unlock_bh(&ap_dev->lock);
1484 }
1485 else
1486 device_unregister(&ap_dev->device);
1487 }
1488 out:
1489 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1490 }
1491
1492 static void ap_config_timeout(unsigned long ptr)
1493 {
1494 if (ap_suspend_flag)
1495 return;
1496 queue_work(ap_work_queue, &ap_config_work);
1497 }
1498
1499 /**
1500 * ap_poll_read(): Receive pending reply messages from an AP device.
1501 * @ap_dev: pointer to the AP device
1502 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1503 * required, bit 2^1 is set if the poll timer needs to get armed
1504 *
1505 * Returns 0 if the device is still present, -ENODEV if not.
1506 */
1507 static int ap_poll_read(struct ap_device *ap_dev, unsigned long *flags)
1508 {
1509 struct ap_queue_status status;
1510 struct ap_message *ap_msg;
1511
1512 if (ap_dev->queue_count <= 0)
1513 return 0;
1514 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
1515 ap_dev->reply->message, ap_dev->reply->length);
1516 switch (status.response_code) {
1517 case AP_RESPONSE_NORMAL:
1518 ap_dev->interrupt = status.int_enabled;
1519 atomic_dec(&ap_poll_requests);
1520 ap_decrease_queue_count(ap_dev);
1521 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
1522 if (ap_msg->psmid != ap_dev->reply->psmid)
1523 continue;
1524 list_del_init(&ap_msg->list);
1525 ap_dev->pendingq_count--;
1526 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
1527 break;
1528 }
1529 if (ap_dev->queue_count > 0)
1530 *flags |= 1;
1531 break;
1532 case AP_RESPONSE_NO_PENDING_REPLY:
1533 ap_dev->interrupt = status.int_enabled;
1534 if (status.queue_empty) {
1535 /* The card shouldn't forget requests but who knows. */
1536 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1537 ap_dev->queue_count = 0;
1538 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1539 ap_dev->requestq_count += ap_dev->pendingq_count;
1540 ap_dev->pendingq_count = 0;
1541 } else
1542 *flags |= 2;
1543 break;
1544 default:
1545 return -ENODEV;
1546 }
1547 return 0;
1548 }
1549
1550 /**
1551 * ap_poll_write(): Send messages from the request queue to an AP device.
1552 * @ap_dev: pointer to the AP device
1553 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1554 * required, bit 2^1 is set if the poll timer needs to get armed
1555 *
1556 * Returns 0 if the device is still present, -ENODEV if not.
1557 */
1558 static int ap_poll_write(struct ap_device *ap_dev, unsigned long *flags)
1559 {
1560 struct ap_queue_status status;
1561 struct ap_message *ap_msg;
1562
1563 if (ap_dev->requestq_count <= 0 ||
1564 (ap_dev->queue_count >= ap_dev->queue_depth) ||
1565 (ap_dev->reset == AP_RESET_IN_PROGRESS))
1566 return 0;
1567 /* Start the next request on the queue. */
1568 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
1569 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1570 ap_msg->message, ap_msg->length, ap_msg->special);
1571 switch (status.response_code) {
1572 case AP_RESPONSE_NORMAL:
1573 atomic_inc(&ap_poll_requests);
1574 ap_increase_queue_count(ap_dev);
1575 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
1576 ap_dev->requestq_count--;
1577 ap_dev->pendingq_count++;
1578 if (ap_dev->queue_count < ap_dev->queue_depth &&
1579 ap_dev->requestq_count > 0)
1580 *flags |= 1;
1581 *flags |= 2;
1582 break;
1583 case AP_RESPONSE_RESET_IN_PROGRESS:
1584 __ap_schedule_poll_timer();
1585 case AP_RESPONSE_Q_FULL:
1586 *flags |= 2;
1587 break;
1588 case AP_RESPONSE_MESSAGE_TOO_BIG:
1589 case AP_RESPONSE_REQ_FAC_NOT_INST:
1590 return -EINVAL;
1591 default:
1592 return -ENODEV;
1593 }
1594 return 0;
1595 }
1596
1597 /**
1598 * ap_poll_queue(): Poll AP device for pending replies and send new messages.
1599 * Check if the queue has a pending reset. In case it's done re-enable
1600 * interrupts, otherwise reschedule the poll_timer for another attempt.
1601 * @ap_dev: pointer to the bus device
1602 * @flags: pointer to control flags, bit 2^0 is set if another poll is
1603 * required, bit 2^1 is set if the poll timer needs to get armed
1604 *
1605 * Poll AP device for pending replies and send new messages. If either
1606 * ap_poll_read or ap_poll_write returns -ENODEV unregister the device.
1607 * Returns 0.
1608 */
1609 static inline int ap_poll_queue(struct ap_device *ap_dev, unsigned long *flags)
1610 {
1611 struct ap_queue_status status;
1612 int rc;
1613
1614 if (ap_dev->reset == AP_RESET_IN_PROGRESS) {
1615 status = ap_test_queue(ap_dev->qid, NULL);
1616 switch (status.response_code) {
1617 case AP_RESPONSE_NORMAL:
1618 ap_dev->reset = AP_RESET_IGNORE;
1619 if (ap_using_interrupts()) {
1620 rc = ap_queue_enable_interruption(
1621 ap_dev, ap_airq.lsi_ptr);
1622 if (!rc)
1623 ap_dev->interrupt = AP_INTR_IN_PROGRESS;
1624 else if (rc == -ENODEV) {
1625 pr_err("Registering adapter interrupts for "
1626 "AP %d failed\n", AP_QID_DEVICE(ap_dev->qid));
1627 return rc;
1628 }
1629 }
1630 /* fall through */
1631 case AP_RESPONSE_BUSY:
1632 case AP_RESPONSE_RESET_IN_PROGRESS:
1633 *flags |= AP_POLL_AFTER_TIMEOUT;
1634 break;
1635 case AP_RESPONSE_Q_NOT_AVAIL:
1636 case AP_RESPONSE_DECONFIGURED:
1637 case AP_RESPONSE_CHECKSTOPPED:
1638 return -ENODEV;
1639 default:
1640 break;
1641 }
1642 }
1643
1644 if ((ap_dev->reset != AP_RESET_IN_PROGRESS) &&
1645 (ap_dev->interrupt == AP_INTR_IN_PROGRESS)) {
1646 status = ap_test_queue(ap_dev->qid, NULL);
1647 if (ap_using_interrupts()) {
1648 if (status.int_enabled == 1)
1649 ap_dev->interrupt = AP_INTR_ENABLED;
1650 else
1651 *flags |= AP_POLL_AFTER_TIMEOUT;
1652 } else
1653 ap_dev->interrupt = AP_INTR_DISABLED;
1654 }
1655
1656 rc = ap_poll_read(ap_dev, flags);
1657 if (rc)
1658 return rc;
1659 return ap_poll_write(ap_dev, flags);
1660 }
1661
1662 /**
1663 * __ap_queue_message(): Queue a message to a device.
1664 * @ap_dev: pointer to the AP device
1665 * @ap_msg: the message to be queued
1666 *
1667 * Queue a message to a device. Returns 0 if successful.
1668 */
1669 static int __ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1670 {
1671 struct ap_queue_status status;
1672
1673 if (list_empty(&ap_dev->requestq) &&
1674 (ap_dev->queue_count < ap_dev->queue_depth) &&
1675 (ap_dev->reset != AP_RESET_IN_PROGRESS)) {
1676 status = __ap_send(ap_dev->qid, ap_msg->psmid,
1677 ap_msg->message, ap_msg->length,
1678 ap_msg->special);
1679 switch (status.response_code) {
1680 case AP_RESPONSE_NORMAL:
1681 list_add_tail(&ap_msg->list, &ap_dev->pendingq);
1682 atomic_inc(&ap_poll_requests);
1683 ap_dev->pendingq_count++;
1684 ap_increase_queue_count(ap_dev);
1685 ap_dev->total_request_count++;
1686 break;
1687 case AP_RESPONSE_Q_FULL:
1688 case AP_RESPONSE_RESET_IN_PROGRESS:
1689 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1690 ap_dev->requestq_count++;
1691 ap_dev->total_request_count++;
1692 return -EBUSY;
1693 case AP_RESPONSE_REQ_FAC_NOT_INST:
1694 case AP_RESPONSE_MESSAGE_TOO_BIG:
1695 ap_msg->rc = -EINVAL;
1696 ap_msg->receive(ap_dev, ap_msg, NULL);
1697 return -EINVAL;
1698 default: /* Device is gone. */
1699 ap_msg->rc = -ENODEV;
1700 ap_msg->receive(ap_dev, ap_msg, NULL);
1701 return -ENODEV;
1702 }
1703 } else {
1704 list_add_tail(&ap_msg->list, &ap_dev->requestq);
1705 ap_dev->requestq_count++;
1706 ap_dev->total_request_count++;
1707 return -EBUSY;
1708 }
1709 ap_schedule_poll_timer();
1710 return 0;
1711 }
1712
1713 void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1714 {
1715 unsigned long flags;
1716 int rc;
1717
1718 /* For asynchronous message handling a valid receive-callback
1719 * is required. */
1720 BUG_ON(!ap_msg->receive);
1721
1722 spin_lock_bh(&ap_dev->lock);
1723 if (!ap_dev->unregistered) {
1724 /* Make room on the queue by polling for finished requests. */
1725 rc = ap_poll_queue(ap_dev, &flags);
1726 if (!rc)
1727 rc = __ap_queue_message(ap_dev, ap_msg);
1728 if (!rc)
1729 wake_up(&ap_poll_wait);
1730 if (rc == -ENODEV)
1731 ap_dev->unregistered = 1;
1732 } else {
1733 ap_msg->rc = -ENODEV;
1734 ap_msg->receive(ap_dev, ap_msg, NULL);
1735 rc = -ENODEV;
1736 }
1737 spin_unlock_bh(&ap_dev->lock);
1738 }
1739 EXPORT_SYMBOL(ap_queue_message);
1740
1741 /**
1742 * ap_cancel_message(): Cancel a crypto request.
1743 * @ap_dev: The AP device that has the message queued
1744 * @ap_msg: The message that is to be removed
1745 *
1746 * Cancel a crypto request. This is done by removing the request
1747 * from the device pending or request queue. Note that the
1748 * request stays on the AP queue. When it finishes the message
1749 * reply will be discarded because the psmid can't be found.
1750 */
1751 void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1752 {
1753 struct ap_message *tmp;
1754
1755 spin_lock_bh(&ap_dev->lock);
1756 if (!list_empty(&ap_msg->list)) {
1757 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1758 if (tmp->psmid == ap_msg->psmid) {
1759 ap_dev->pendingq_count--;
1760 goto found;
1761 }
1762 ap_dev->requestq_count--;
1763 found:
1764 list_del_init(&ap_msg->list);
1765 }
1766 spin_unlock_bh(&ap_dev->lock);
1767 }
1768 EXPORT_SYMBOL(ap_cancel_message);
1769
1770 /**
1771 * ap_poll_timeout(): AP receive polling for finished AP requests.
1772 * @unused: Unused pointer.
1773 *
1774 * Schedules the AP tasklet using a high resolution timer.
1775 */
1776 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
1777 {
1778 if (!ap_suspend_flag)
1779 tasklet_schedule(&ap_tasklet);
1780 return HRTIMER_NORESTART;
1781 }
1782
1783 /**
1784 * ap_reset(): Reset a not responding AP device.
1785 * @ap_dev: Pointer to the AP device
1786 *
1787 * Reset a not responding AP device and move all requests from the
1788 * pending queue to the request queue.
1789 */
1790 static void ap_reset(struct ap_device *ap_dev, unsigned long *flags)
1791 {
1792 int rc;
1793
1794 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1795 ap_dev->queue_count = 0;
1796 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
1797 ap_dev->requestq_count += ap_dev->pendingq_count;
1798 ap_dev->pendingq_count = 0;
1799 rc = ap_init_queue(ap_dev);
1800 if (rc == -ENODEV)
1801 ap_dev->unregistered = 1;
1802 else
1803 *flags |= AP_POLL_AFTER_TIMEOUT;
1804 }
1805
1806 static int __ap_poll_device(struct ap_device *ap_dev, unsigned long *flags)
1807 {
1808 if (!ap_dev->unregistered) {
1809 if (ap_poll_queue(ap_dev, flags))
1810 ap_dev->unregistered = 1;
1811 if (ap_dev->reset == AP_RESET_DO)
1812 ap_reset(ap_dev, flags);
1813 }
1814 return 0;
1815 }
1816
1817 /**
1818 * ap_poll_all(): Poll all AP devices.
1819 * @dummy: Unused variable
1820 *
1821 * Poll all AP devices on the bus in a round robin fashion. Continue
1822 * polling until bit 2^0 of the control flags is not set. If bit 2^1
1823 * of the control flags has been set arm the poll timer.
1824 */
1825 static void ap_poll_all(unsigned long dummy)
1826 {
1827 unsigned long flags;
1828 struct ap_device *ap_dev;
1829
1830 /* Reset the indicator if interrupts are used. Thus new interrupts can
1831 * be received. Doing it in the beginning of the tasklet is therefor
1832 * important that no requests on any AP get lost.
1833 */
1834 if (ap_using_interrupts())
1835 xchg(ap_airq.lsi_ptr, 0);
1836 do {
1837 flags = 0;
1838 spin_lock(&ap_device_list_lock);
1839 list_for_each_entry(ap_dev, &ap_device_list, list) {
1840 spin_lock(&ap_dev->lock);
1841 __ap_poll_device(ap_dev, &flags);
1842 spin_unlock(&ap_dev->lock);
1843 }
1844 spin_unlock(&ap_device_list_lock);
1845 } while (flags & AP_POLL_IMMEDIATELY);
1846 if (flags & AP_POLL_AFTER_TIMEOUT)
1847 __ap_schedule_poll_timer();
1848 }
1849
1850 /**
1851 * ap_request_timeout(): Handling of request timeouts
1852 * @data: Holds the AP device.
1853 *
1854 * Handles request timeouts.
1855 */
1856 static void ap_request_timeout(unsigned long data)
1857 {
1858 struct ap_device *ap_dev = (struct ap_device *) data;
1859
1860 if (ap_suspend_flag)
1861 return;
1862 if (ap_dev->reset == AP_RESET_ARMED) {
1863 ap_dev->reset = AP_RESET_DO;
1864
1865 if (ap_using_interrupts())
1866 tasklet_schedule(&ap_tasklet);
1867 }
1868 }
1869
1870 static void ap_reset_domain(void)
1871 {
1872 int i;
1873
1874 if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1875 return;
1876 for (i = 0; i < AP_DEVICES; i++)
1877 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1878 }
1879
1880 static void ap_reset_all(void)
1881 {
1882 int i, j;
1883
1884 for (i = 0; i < AP_DOMAINS; i++) {
1885 if (!ap_test_config_domain(i))
1886 continue;
1887 for (j = 0; j < AP_DEVICES; j++) {
1888 if (!ap_test_config_card_id(j))
1889 continue;
1890 ap_reset_queue(AP_MKQID(j, i));
1891 }
1892 }
1893 }
1894
1895 static struct reset_call ap_reset_call = {
1896 .fn = ap_reset_all,
1897 };
1898
1899 /**
1900 * ap_module_init(): The module initialization code.
1901 *
1902 * Initializes the module.
1903 */
1904 int __init ap_module_init(void)
1905 {
1906 int max_domain_id;
1907 int rc, i;
1908
1909 if (ap_instructions_available() != 0) {
1910 pr_warn("The hardware system does not support AP instructions\n");
1911 return -ENODEV;
1912 }
1913
1914 /* Get AP configuration data if available */
1915 ap_init_configuration();
1916
1917 if (ap_configuration)
1918 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1919 else
1920 max_domain_id = 15;
1921 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1922 pr_warn("%d is not a valid cryptographic domain\n",
1923 ap_domain_index);
1924 return -EINVAL;
1925 }
1926 /* In resume callback we need to know if the user had set the domain.
1927 * If so, we can not just reset it.
1928 */
1929 if (ap_domain_index >= 0)
1930 user_set_domain = 1;
1931
1932 if (ap_interrupts_available()) {
1933 rc = register_adapter_interrupt(&ap_airq);
1934 ap_airq_flag = (rc == 0);
1935 }
1936
1937 register_reset_call(&ap_reset_call);
1938
1939 /* Create /sys/bus/ap. */
1940 rc = bus_register(&ap_bus_type);
1941 if (rc)
1942 goto out;
1943 for (i = 0; ap_bus_attrs[i]; i++) {
1944 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1945 if (rc)
1946 goto out_bus;
1947 }
1948
1949 /* Create /sys/devices/ap. */
1950 ap_root_device = root_device_register("ap");
1951 rc = PTR_RET(ap_root_device);
1952 if (rc)
1953 goto out_bus;
1954
1955 ap_work_queue = create_singlethread_workqueue("kapwork");
1956 if (!ap_work_queue) {
1957 rc = -ENOMEM;
1958 goto out_root;
1959 }
1960
1961 if (ap_select_domain() == 0)
1962 ap_scan_bus(NULL);
1963
1964 /* Setup the AP bus rescan timer. */
1965 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1966 add_timer(&ap_config_timer);
1967
1968 /* Setup the high resultion poll timer.
1969 * If we are running under z/VM adjust polling to z/VM polling rate.
1970 */
1971 if (MACHINE_IS_VM)
1972 poll_timeout = 1500000;
1973 spin_lock_init(&ap_poll_timer_lock);
1974 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1975 ap_poll_timer.function = ap_poll_timeout;
1976
1977 /* Start the low priority AP bus poll thread. */
1978 if (ap_thread_flag) {
1979 rc = ap_poll_thread_start();
1980 if (rc)
1981 goto out_work;
1982 }
1983
1984 rc = register_pm_notifier(&ap_power_notifier);
1985 if (rc)
1986 goto out_pm;
1987
1988 return 0;
1989
1990 out_pm:
1991 ap_poll_thread_stop();
1992 out_work:
1993 del_timer_sync(&ap_config_timer);
1994 hrtimer_cancel(&ap_poll_timer);
1995 destroy_workqueue(ap_work_queue);
1996 out_root:
1997 root_device_unregister(ap_root_device);
1998 out_bus:
1999 while (i--)
2000 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2001 bus_unregister(&ap_bus_type);
2002 out:
2003 unregister_reset_call(&ap_reset_call);
2004 if (ap_using_interrupts())
2005 unregister_adapter_interrupt(&ap_airq);
2006 kfree(ap_configuration);
2007 return rc;
2008 }
2009
2010 /**
2011 * ap_modules_exit(): The module termination code
2012 *
2013 * Terminates the module.
2014 */
2015 void ap_module_exit(void)
2016 {
2017 int i;
2018
2019 ap_reset_domain();
2020 ap_poll_thread_stop();
2021 del_timer_sync(&ap_config_timer);
2022 hrtimer_cancel(&ap_poll_timer);
2023 destroy_workqueue(ap_work_queue);
2024 tasklet_kill(&ap_tasklet);
2025 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
2026 for (i = 0; ap_bus_attrs[i]; i++)
2027 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
2028 unregister_pm_notifier(&ap_power_notifier);
2029 root_device_unregister(ap_root_device);
2030 bus_unregister(&ap_bus_type);
2031 kfree(ap_configuration);
2032 unregister_reset_call(&ap_reset_call);
2033 if (ap_using_interrupts())
2034 unregister_adapter_interrupt(&ap_airq);
2035 }
2036
2037 module_init(ap_module_init);
2038 module_exit(ap_module_exit);
This page took 0.082423 seconds and 4 git commands to generate.