1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
55MODULE_AUTHOR("IBM Corporation");
56MODULE_DESCRIPTION("Adjunct Processor Bus driver, " \
57 "Copyright IBM Corp. 2006, 2012");
58MODULE_LICENSE("GPL");
59MODULE_ALIAS_CRYPTO("z90crypt");
60
61
62
63
64int ap_domain_index = -1;
65module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
66MODULE_PARM_DESC(domain, "domain index for ap devices");
67EXPORT_SYMBOL(ap_domain_index);
68
69static int ap_thread_flag = 0;
70module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
71MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
72
73static struct device *ap_root_device = NULL;
74static struct ap_config_info *ap_configuration;
75static DEFINE_SPINLOCK(ap_device_list_lock);
76static LIST_HEAD(ap_device_list);
77static bool initialised;
78
79
80
81
82static struct timer_list ap_config_timer;
83static int ap_config_time = AP_CONFIG_TIME;
84static void ap_scan_bus(struct work_struct *);
85static DECLARE_WORK(ap_scan_work, ap_scan_bus);
86
87
88
89
90static void ap_tasklet_fn(unsigned long);
91static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
92static atomic_t ap_poll_requests = ATOMIC_INIT(0);
93static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
94static struct task_struct *ap_poll_kthread = NULL;
95static DEFINE_MUTEX(ap_poll_thread_mutex);
96static DEFINE_SPINLOCK(ap_poll_timer_lock);
97static struct hrtimer ap_poll_timer;
98
99
100static unsigned long long poll_timeout = 250000;
101
102
103static int ap_suspend_flag;
104
105static int ap_max_domain_id;
106
107
108
109static int user_set_domain = 0;
110static struct bus_type ap_bus_type;
111
112
113static void ap_interrupt_handler(struct airq_struct *airq);
114
115static int ap_airq_flag;
116
117static struct airq_struct ap_airq = {
118 .handler = ap_interrupt_handler,
119 .isc = AP_ISC,
120};
121
122
123
124
125
126static inline int ap_using_interrupts(void)
127{
128 return ap_airq_flag;
129}
130
131
132
133
134
135
136static inline int ap_instructions_available(void)
137{
138 register unsigned long reg0 asm ("0") = AP_MKQID(0,0);
139 register unsigned long reg1 asm ("1") = -ENODEV;
140 register unsigned long reg2 asm ("2") = 0UL;
141
142 asm volatile(
143 " .long 0xb2af0000\n"
144 "0: la %1,0\n"
145 "1:\n"
146 EX_TABLE(0b, 1b)
147 : "+d" (reg0), "+d" (reg1), "+d" (reg2) : : "cc" );
148 return reg1;
149}
150
151
152
153
154
155
156static int ap_interrupts_available(void)
157{
158 return test_facility(65);
159}
160
161
162
163
164
165
166
167static int ap_configuration_available(void)
168{
169 return test_facility(12);
170}
171
172
173
174
175
176
177
178
179static inline struct ap_queue_status
180ap_test_queue(ap_qid_t qid, unsigned long *info)
181{
182 register unsigned long reg0 asm ("0") = qid;
183 register struct ap_queue_status reg1 asm ("1");
184 register unsigned long reg2 asm ("2") = 0UL;
185
186 if (test_facility(15))
187 reg0 |= 1UL << 23;
188 asm volatile(".long 0xb2af0000"
189 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
190 if (info)
191 *info = reg2;
192 return reg1;
193}
194
195
196
197
198
199
200
201static inline struct ap_queue_status ap_reset_queue(ap_qid_t qid)
202{
203 register unsigned long reg0 asm ("0") = qid | 0x01000000UL;
204 register struct ap_queue_status reg1 asm ("1");
205 register unsigned long reg2 asm ("2") = 0UL;
206
207 asm volatile(
208 ".long 0xb2af0000"
209 : "+d" (reg0), "=d" (reg1), "+d" (reg2) : : "cc");
210 return reg1;
211}
212
213
214
215
216
217
218
219
220static inline struct ap_queue_status
221ap_queue_interruption_control(ap_qid_t qid, void *ind)
222{
223 register unsigned long reg0 asm ("0") = qid | 0x03000000UL;
224 register unsigned long reg1_in asm ("1") = 0x0000800000000000UL | AP_ISC;
225 register struct ap_queue_status reg1_out asm ("1");
226 register void *reg2 asm ("2") = ind;
227 asm volatile(
228 ".long 0xb2af0000"
229 : "+d" (reg0), "+d" (reg1_in), "=d" (reg1_out), "+d" (reg2)
230 :
231 : "cc" );
232 return reg1_out;
233}
234
235
236
237
238
239
240static inline int ap_query_configuration(void)
241{
242 register unsigned long reg0 asm ("0") = 0x04000000UL;
243 register unsigned long reg1 asm ("1") = -EINVAL;
244 register void *reg2 asm ("2") = (void *) ap_configuration;
245
246 if (!ap_configuration)
247 return -EOPNOTSUPP;
248 asm volatile(
249 ".long 0xb2af0000\n"
250 "0: la %1,0\n"
251 "1:\n"
252 EX_TABLE(0b, 1b)
253 : "+d" (reg0), "+d" (reg1), "+d" (reg2)
254 :
255 : "cc");
256
257 return reg1;
258}
259
260
261
262
263static void ap_init_configuration(void)
264{
265 if (!ap_configuration_available())
266 return;
267
268 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
269 if (!ap_configuration)
270 return;
271 if (ap_query_configuration() != 0) {
272 kfree(ap_configuration);
273 ap_configuration = NULL;
274 return;
275 }
276}
277
278
279
280
281
282static inline int ap_test_config(unsigned int *field, unsigned int nr)
283{
284 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
285}
286
287
288
289
290
291
292
293
294
295static inline int ap_test_config_card_id(unsigned int id)
296{
297 if (!ap_configuration)
298 return 1;
299 return ap_test_config(ap_configuration->apm, id);
300}
301
302
303
304
305
306
307
308
309
310static inline int ap_test_config_domain(unsigned int domain)
311{
312 if (!ap_configuration)
313 return domain < 16;
314 return ap_test_config(ap_configuration->aqm, domain);
315}
316
317
318
319
320
321
322
323
324
325
326static int ap_queue_enable_interruption(struct ap_device *ap_dev, void *ind)
327{
328 struct ap_queue_status status;
329
330 status = ap_queue_interruption_control(ap_dev->qid, ind);
331 switch (status.response_code) {
332 case AP_RESPONSE_NORMAL:
333 case AP_RESPONSE_OTHERWISE_CHANGED:
334 return 0;
335 case AP_RESPONSE_Q_NOT_AVAIL:
336 case AP_RESPONSE_DECONFIGURED:
337 case AP_RESPONSE_CHECKSTOPPED:
338 case AP_RESPONSE_INVALID_ADDRESS:
339 pr_err("Registering adapter interrupts for AP %d failed\n",
340 AP_QID_DEVICE(ap_dev->qid));
341 return -EOPNOTSUPP;
342 case AP_RESPONSE_RESET_IN_PROGRESS:
343 case AP_RESPONSE_BUSY:
344 default:
345 return -EBUSY;
346 }
347}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362static inline struct ap_queue_status
363__ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length,
364 unsigned int special)
365{
366 typedef struct { char _[length]; } msgblock;
367 register unsigned long reg0 asm ("0") = qid | 0x40000000UL;
368 register struct ap_queue_status reg1 asm ("1");
369 register unsigned long reg2 asm ("2") = (unsigned long) msg;
370 register unsigned long reg3 asm ("3") = (unsigned long) length;
371 register unsigned long reg4 asm ("4") = (unsigned int) (psmid >> 32);
372 register unsigned long reg5 asm ("5") = psmid & 0xffffffff;
373
374 if (special == 1)
375 reg0 |= 0x400000UL;
376
377 asm volatile (
378 "0: .long 0xb2ad0042\n"
379 " brc 2,0b"
380 : "+d" (reg0), "=d" (reg1), "+d" (reg2), "+d" (reg3)
381 : "d" (reg4), "d" (reg5), "m" (*(msgblock *) msg)
382 : "cc" );
383 return reg1;
384}
385
386int ap_send(ap_qid_t qid, unsigned long long psmid, void *msg, size_t length)
387{
388 struct ap_queue_status status;
389
390 status = __ap_send(qid, psmid, msg, length, 0);
391 switch (status.response_code) {
392 case AP_RESPONSE_NORMAL:
393 return 0;
394 case AP_RESPONSE_Q_FULL:
395 case AP_RESPONSE_RESET_IN_PROGRESS:
396 return -EBUSY;
397 case AP_RESPONSE_REQ_FAC_NOT_INST:
398 return -EINVAL;
399 default:
400 return -ENODEV;
401 }
402}
403EXPORT_SYMBOL(ap_send);
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423static inline struct ap_queue_status
424__ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
425{
426 typedef struct { char _[length]; } msgblock;
427 register unsigned long reg0 asm("0") = qid | 0x80000000UL;
428 register struct ap_queue_status reg1 asm ("1");
429 register unsigned long reg2 asm("2") = 0UL;
430 register unsigned long reg4 asm("4") = (unsigned long) msg;
431 register unsigned long reg5 asm("5") = (unsigned long) length;
432 register unsigned long reg6 asm("6") = 0UL;
433 register unsigned long reg7 asm("7") = 0UL;
434
435
436 asm volatile(
437 "0: .long 0xb2ae0064\n"
438 " brc 6,0b\n"
439 : "+d" (reg0), "=d" (reg1), "+d" (reg2),
440 "+d" (reg4), "+d" (reg5), "+d" (reg6), "+d" (reg7),
441 "=m" (*(msgblock *) msg) : : "cc" );
442 *psmid = (((unsigned long long) reg6) << 32) + reg7;
443 return reg1;
444}
445
446int ap_recv(ap_qid_t qid, unsigned long long *psmid, void *msg, size_t length)
447{
448 struct ap_queue_status status;
449
450 status = __ap_recv(qid, psmid, msg, length);
451 switch (status.response_code) {
452 case AP_RESPONSE_NORMAL:
453 return 0;
454 case AP_RESPONSE_NO_PENDING_REPLY:
455 if (status.queue_empty)
456 return -ENOENT;
457 return -EBUSY;
458 case AP_RESPONSE_RESET_IN_PROGRESS:
459 return -EBUSY;
460 default:
461 return -ENODEV;
462 }
463}
464EXPORT_SYMBOL(ap_recv);
465
466
467
468
469
470
471
472
473static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
474 unsigned int *facilities)
475{
476 struct ap_queue_status status;
477 unsigned long info;
478 int nd;
479
480 if (!ap_test_config_card_id(AP_QID_DEVICE(qid)))
481 return -ENODEV;
482
483 status = ap_test_queue(qid, &info);
484 switch (status.response_code) {
485 case AP_RESPONSE_NORMAL:
486 *queue_depth = (int)(info & 0xff);
487 *device_type = (int)((info >> 24) & 0xff);
488 *facilities = (unsigned int)(info >> 32);
489
490 nd = (info >> 16) & 0xff;
491 if ((info & (1UL << 57)) && nd > 0)
492 ap_max_domain_id = nd;
493 return 0;
494 case AP_RESPONSE_Q_NOT_AVAIL:
495 case AP_RESPONSE_DECONFIGURED:
496 case AP_RESPONSE_CHECKSTOPPED:
497 case AP_RESPONSE_INVALID_ADDRESS:
498 return -ENODEV;
499 case AP_RESPONSE_RESET_IN_PROGRESS:
500 case AP_RESPONSE_OTHERWISE_CHANGED:
501 case AP_RESPONSE_BUSY:
502 return -EBUSY;
503 default:
504 BUG();
505 }
506}
507
508
509
510static void ap_sm_wait(enum ap_wait wait)
511{
512 ktime_t hr_time;
513
514 switch (wait) {
515 case AP_WAIT_AGAIN:
516 case AP_WAIT_INTERRUPT:
517 if (ap_using_interrupts())
518 break;
519 if (ap_poll_kthread) {
520 wake_up(&ap_poll_wait);
521 break;
522 }
523
524 case AP_WAIT_TIMEOUT:
525 spin_lock_bh(&ap_poll_timer_lock);
526 if (!hrtimer_is_queued(&ap_poll_timer)) {
527 hr_time = ktime_set(0, poll_timeout);
528 hrtimer_forward_now(&ap_poll_timer, hr_time);
529 hrtimer_restart(&ap_poll_timer);
530 }
531 spin_unlock_bh(&ap_poll_timer_lock);
532 break;
533 case AP_WAIT_NONE:
534 default:
535 break;
536 }
537}
538
539static enum ap_wait ap_sm_nop(struct ap_device *ap_dev)
540{
541 return AP_WAIT_NONE;
542}
543
544
545
546
547
548
549
550
551static struct ap_queue_status ap_sm_recv(struct ap_device *ap_dev)
552{
553 struct ap_queue_status status;
554 struct ap_message *ap_msg;
555
556 status = __ap_recv(ap_dev->qid, &ap_dev->reply->psmid,
557 ap_dev->reply->message, ap_dev->reply->length);
558 switch (status.response_code) {
559 case AP_RESPONSE_NORMAL:
560 atomic_dec(&ap_poll_requests);
561 ap_dev->queue_count--;
562 if (ap_dev->queue_count > 0)
563 mod_timer(&ap_dev->timeout,
564 jiffies + ap_dev->drv->request_timeout);
565 list_for_each_entry(ap_msg, &ap_dev->pendingq, list) {
566 if (ap_msg->psmid != ap_dev->reply->psmid)
567 continue;
568 list_del_init(&ap_msg->list);
569 ap_dev->pendingq_count--;
570 ap_msg->receive(ap_dev, ap_msg, ap_dev->reply);
571 break;
572 }
573 case AP_RESPONSE_NO_PENDING_REPLY:
574 if (!status.queue_empty || ap_dev->queue_count <= 0)
575 break;
576
577 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
578 ap_dev->queue_count = 0;
579 list_splice_init(&ap_dev->pendingq, &ap_dev->requestq);
580 ap_dev->requestq_count += ap_dev->pendingq_count;
581 ap_dev->pendingq_count = 0;
582 break;
583 default:
584 break;
585 }
586 return status;
587}
588
589
590
591
592
593
594
595static enum ap_wait ap_sm_read(struct ap_device *ap_dev)
596{
597 struct ap_queue_status status;
598
599 status = ap_sm_recv(ap_dev);
600 switch (status.response_code) {
601 case AP_RESPONSE_NORMAL:
602 if (ap_dev->queue_count > 0) {
603 ap_dev->state = AP_STATE_WORKING;
604 return AP_WAIT_AGAIN;
605 }
606 ap_dev->state = AP_STATE_IDLE;
607 return AP_WAIT_NONE;
608 case AP_RESPONSE_NO_PENDING_REPLY:
609 if (ap_dev->queue_count > 0)
610 return AP_WAIT_INTERRUPT;
611 ap_dev->state = AP_STATE_IDLE;
612 return AP_WAIT_NONE;
613 default:
614 ap_dev->state = AP_STATE_BORKED;
615 return AP_WAIT_NONE;
616 }
617}
618
619
620
621
622
623
624
625static enum ap_wait ap_sm_write(struct ap_device *ap_dev)
626{
627 struct ap_queue_status status;
628 struct ap_message *ap_msg;
629
630 if (ap_dev->requestq_count <= 0)
631 return AP_WAIT_NONE;
632
633 ap_msg = list_entry(ap_dev->requestq.next, struct ap_message, list);
634 status = __ap_send(ap_dev->qid, ap_msg->psmid,
635 ap_msg->message, ap_msg->length, ap_msg->special);
636 switch (status.response_code) {
637 case AP_RESPONSE_NORMAL:
638 atomic_inc(&ap_poll_requests);
639 ap_dev->queue_count++;
640 if (ap_dev->queue_count == 1)
641 mod_timer(&ap_dev->timeout,
642 jiffies + ap_dev->drv->request_timeout);
643 list_move_tail(&ap_msg->list, &ap_dev->pendingq);
644 ap_dev->requestq_count--;
645 ap_dev->pendingq_count++;
646 if (ap_dev->queue_count < ap_dev->queue_depth) {
647 ap_dev->state = AP_STATE_WORKING;
648 return AP_WAIT_AGAIN;
649 }
650
651 case AP_RESPONSE_Q_FULL:
652 ap_dev->state = AP_STATE_QUEUE_FULL;
653 return AP_WAIT_INTERRUPT;
654 case AP_RESPONSE_RESET_IN_PROGRESS:
655 ap_dev->state = AP_STATE_RESET_WAIT;
656 return AP_WAIT_TIMEOUT;
657 case AP_RESPONSE_MESSAGE_TOO_BIG:
658 case AP_RESPONSE_REQ_FAC_NOT_INST:
659 list_del_init(&ap_msg->list);
660 ap_dev->requestq_count--;
661 ap_msg->rc = -EINVAL;
662 ap_msg->receive(ap_dev, ap_msg, NULL);
663 return AP_WAIT_AGAIN;
664 default:
665 ap_dev->state = AP_STATE_BORKED;
666 return AP_WAIT_NONE;
667 }
668}
669
670
671
672
673
674
675
676static enum ap_wait ap_sm_read_write(struct ap_device *ap_dev)
677{
678 return min(ap_sm_read(ap_dev), ap_sm_write(ap_dev));
679}
680
681
682
683
684
685
686
687static enum ap_wait ap_sm_reset(struct ap_device *ap_dev)
688{
689 struct ap_queue_status status;
690
691 status = ap_reset_queue(ap_dev->qid);
692 switch (status.response_code) {
693 case AP_RESPONSE_NORMAL:
694 case AP_RESPONSE_RESET_IN_PROGRESS:
695 ap_dev->state = AP_STATE_RESET_WAIT;
696 ap_dev->interrupt = AP_INTR_DISABLED;
697 return AP_WAIT_TIMEOUT;
698 case AP_RESPONSE_BUSY:
699 return AP_WAIT_TIMEOUT;
700 case AP_RESPONSE_Q_NOT_AVAIL:
701 case AP_RESPONSE_DECONFIGURED:
702 case AP_RESPONSE_CHECKSTOPPED:
703 default:
704 ap_dev->state = AP_STATE_BORKED;
705 return AP_WAIT_NONE;
706 }
707}
708
709
710
711
712
713
714
715static enum ap_wait ap_sm_reset_wait(struct ap_device *ap_dev)
716{
717 struct ap_queue_status status;
718 unsigned long info;
719
720 if (ap_dev->queue_count > 0)
721
722 status = ap_sm_recv(ap_dev);
723 else
724
725 status = ap_test_queue(ap_dev->qid, &info);
726
727 switch (status.response_code) {
728 case AP_RESPONSE_NORMAL:
729 if (ap_using_interrupts() &&
730 ap_queue_enable_interruption(ap_dev,
731 ap_airq.lsi_ptr) == 0)
732 ap_dev->state = AP_STATE_SETIRQ_WAIT;
733 else
734 ap_dev->state = (ap_dev->queue_count > 0) ?
735 AP_STATE_WORKING : AP_STATE_IDLE;
736 return AP_WAIT_AGAIN;
737 case AP_RESPONSE_BUSY:
738 case AP_RESPONSE_RESET_IN_PROGRESS:
739 return AP_WAIT_TIMEOUT;
740 case AP_RESPONSE_Q_NOT_AVAIL:
741 case AP_RESPONSE_DECONFIGURED:
742 case AP_RESPONSE_CHECKSTOPPED:
743 default:
744 ap_dev->state = AP_STATE_BORKED;
745 return AP_WAIT_NONE;
746 }
747}
748
749
750
751
752
753
754
755static enum ap_wait ap_sm_setirq_wait(struct ap_device *ap_dev)
756{
757 struct ap_queue_status status;
758 unsigned long info;
759
760 if (ap_dev->queue_count > 0)
761
762 status = ap_sm_recv(ap_dev);
763 else
764
765 status = ap_test_queue(ap_dev->qid, &info);
766
767 if (status.int_enabled == 1) {
768
769 ap_dev->interrupt = AP_INTR_ENABLED;
770 ap_dev->state = (ap_dev->queue_count > 0) ?
771 AP_STATE_WORKING : AP_STATE_IDLE;
772 }
773
774 switch (status.response_code) {
775 case AP_RESPONSE_NORMAL:
776 if (ap_dev->queue_count > 0)
777 return AP_WAIT_AGAIN;
778
779 case AP_RESPONSE_NO_PENDING_REPLY:
780 return AP_WAIT_TIMEOUT;
781 default:
782 ap_dev->state = AP_STATE_BORKED;
783 return AP_WAIT_NONE;
784 }
785}
786
787
788
789
790ap_func_t *ap_jumptable[NR_AP_STATES][NR_AP_EVENTS] = {
791 [AP_STATE_RESET_START] = {
792 [AP_EVENT_POLL] = ap_sm_reset,
793 [AP_EVENT_TIMEOUT] = ap_sm_nop,
794 },
795 [AP_STATE_RESET_WAIT] = {
796 [AP_EVENT_POLL] = ap_sm_reset_wait,
797 [AP_EVENT_TIMEOUT] = ap_sm_nop,
798 },
799 [AP_STATE_SETIRQ_WAIT] = {
800 [AP_EVENT_POLL] = ap_sm_setirq_wait,
801 [AP_EVENT_TIMEOUT] = ap_sm_nop,
802 },
803 [AP_STATE_IDLE] = {
804 [AP_EVENT_POLL] = ap_sm_write,
805 [AP_EVENT_TIMEOUT] = ap_sm_nop,
806 },
807 [AP_STATE_WORKING] = {
808 [AP_EVENT_POLL] = ap_sm_read_write,
809 [AP_EVENT_TIMEOUT] = ap_sm_reset,
810 },
811 [AP_STATE_QUEUE_FULL] = {
812 [AP_EVENT_POLL] = ap_sm_read,
813 [AP_EVENT_TIMEOUT] = ap_sm_reset,
814 },
815 [AP_STATE_SUSPEND_WAIT] = {
816 [AP_EVENT_POLL] = ap_sm_read,
817 [AP_EVENT_TIMEOUT] = ap_sm_nop,
818 },
819 [AP_STATE_BORKED] = {
820 [AP_EVENT_POLL] = ap_sm_nop,
821 [AP_EVENT_TIMEOUT] = ap_sm_nop,
822 },
823};
824
825static inline enum ap_wait ap_sm_event(struct ap_device *ap_dev,
826 enum ap_event event)
827{
828 return ap_jumptable[ap_dev->state][event](ap_dev);
829}
830
831static inline enum ap_wait ap_sm_event_loop(struct ap_device *ap_dev,
832 enum ap_event event)
833{
834 enum ap_wait wait;
835
836 while ((wait = ap_sm_event(ap_dev, event)) == AP_WAIT_AGAIN)
837 ;
838 return wait;
839}
840
841
842
843
844
845
846
847static void ap_request_timeout(unsigned long data)
848{
849 struct ap_device *ap_dev = (struct ap_device *) data;
850
851 if (ap_suspend_flag)
852 return;
853 spin_lock_bh(&ap_dev->lock);
854 ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_TIMEOUT));
855 spin_unlock_bh(&ap_dev->lock);
856}
857
858
859
860
861
862
863
864static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
865{
866 if (!ap_suspend_flag)
867 tasklet_schedule(&ap_tasklet);
868 return HRTIMER_NORESTART;
869}
870
871
872
873
874
875static void ap_interrupt_handler(struct airq_struct *airq)
876{
877 inc_irq_stat(IRQIO_APB);
878 if (!ap_suspend_flag)
879 tasklet_schedule(&ap_tasklet);
880}
881
882
883
884
885
886
887
888static void ap_tasklet_fn(unsigned long dummy)
889{
890 struct ap_device *ap_dev;
891 enum ap_wait wait = AP_WAIT_NONE;
892
893
894
895
896
897 if (ap_using_interrupts())
898 xchg(ap_airq.lsi_ptr, 0);
899
900 spin_lock(&ap_device_list_lock);
901 list_for_each_entry(ap_dev, &ap_device_list, list) {
902 spin_lock_bh(&ap_dev->lock);
903 wait = min(wait, ap_sm_event_loop(ap_dev, AP_EVENT_POLL));
904 spin_unlock_bh(&ap_dev->lock);
905 }
906 spin_unlock(&ap_device_list_lock);
907 ap_sm_wait(wait);
908}
909
910
911
912
913
914
915
916
917
918
919
920static int ap_poll_thread(void *data)
921{
922 DECLARE_WAITQUEUE(wait, current);
923
924 set_user_nice(current, MAX_NICE);
925 set_freezable();
926 while (!kthread_should_stop()) {
927 add_wait_queue(&ap_poll_wait, &wait);
928 set_current_state(TASK_INTERRUPTIBLE);
929 if (ap_suspend_flag ||
930 atomic_read(&ap_poll_requests) <= 0) {
931 schedule();
932 try_to_freeze();
933 }
934 set_current_state(TASK_RUNNING);
935 remove_wait_queue(&ap_poll_wait, &wait);
936 if (need_resched()) {
937 schedule();
938 try_to_freeze();
939 continue;
940 }
941 ap_tasklet_fn(0);
942 } while (!kthread_should_stop());
943 return 0;
944}
945
946static int ap_poll_thread_start(void)
947{
948 int rc;
949
950 if (ap_using_interrupts() || ap_poll_kthread)
951 return 0;
952 mutex_lock(&ap_poll_thread_mutex);
953 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
954 rc = PTR_RET(ap_poll_kthread);
955 if (rc)
956 ap_poll_kthread = NULL;
957 mutex_unlock(&ap_poll_thread_mutex);
958 return rc;
959}
960
961static void ap_poll_thread_stop(void)
962{
963 if (!ap_poll_kthread)
964 return;
965 mutex_lock(&ap_poll_thread_mutex);
966 kthread_stop(ap_poll_kthread);
967 ap_poll_kthread = NULL;
968 mutex_unlock(&ap_poll_thread_mutex);
969}
970
971
972
973
974
975
976void ap_queue_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
977{
978
979
980 BUG_ON(!ap_msg->receive);
981
982 spin_lock_bh(&ap_dev->lock);
983
984 list_add_tail(&ap_msg->list, &ap_dev->requestq);
985 ap_dev->requestq_count++;
986 ap_dev->total_request_count++;
987
988 ap_sm_wait(ap_sm_event_loop(ap_dev, AP_EVENT_POLL));
989 spin_unlock_bh(&ap_dev->lock);
990}
991EXPORT_SYMBOL(ap_queue_message);
992
993
994
995
996
997
998
999
1000
1001
1002
1003void ap_cancel_message(struct ap_device *ap_dev, struct ap_message *ap_msg)
1004{
1005 struct ap_message *tmp;
1006
1007 spin_lock_bh(&ap_dev->lock);
1008 if (!list_empty(&ap_msg->list)) {
1009 list_for_each_entry(tmp, &ap_dev->pendingq, list)
1010 if (tmp->psmid == ap_msg->psmid) {
1011 ap_dev->pendingq_count--;
1012 goto found;
1013 }
1014 ap_dev->requestq_count--;
1015found:
1016 list_del_init(&ap_msg->list);
1017 }
1018 spin_unlock_bh(&ap_dev->lock);
1019}
1020EXPORT_SYMBOL(ap_cancel_message);
1021
1022
1023
1024
1025static ssize_t ap_hwtype_show(struct device *dev,
1026 struct device_attribute *attr, char *buf)
1027{
1028 struct ap_device *ap_dev = to_ap_dev(dev);
1029 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->device_type);
1030}
1031
1032static DEVICE_ATTR(hwtype, 0444, ap_hwtype_show, NULL);
1033
1034static ssize_t ap_raw_hwtype_show(struct device *dev,
1035 struct device_attribute *attr, char *buf)
1036{
1037 struct ap_device *ap_dev = to_ap_dev(dev);
1038
1039 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->raw_hwtype);
1040}
1041
1042static DEVICE_ATTR(raw_hwtype, 0444, ap_raw_hwtype_show, NULL);
1043
1044static ssize_t ap_depth_show(struct device *dev, struct device_attribute *attr,
1045 char *buf)
1046{
1047 struct ap_device *ap_dev = to_ap_dev(dev);
1048 return snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->queue_depth);
1049}
1050
1051static DEVICE_ATTR(depth, 0444, ap_depth_show, NULL);
1052static ssize_t ap_request_count_show(struct device *dev,
1053 struct device_attribute *attr,
1054 char *buf)
1055{
1056 struct ap_device *ap_dev = to_ap_dev(dev);
1057 int rc;
1058
1059 spin_lock_bh(&ap_dev->lock);
1060 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->total_request_count);
1061 spin_unlock_bh(&ap_dev->lock);
1062 return rc;
1063}
1064
1065static DEVICE_ATTR(request_count, 0444, ap_request_count_show, NULL);
1066
1067static ssize_t ap_requestq_count_show(struct device *dev,
1068 struct device_attribute *attr, char *buf)
1069{
1070 struct ap_device *ap_dev = to_ap_dev(dev);
1071 int rc;
1072
1073 spin_lock_bh(&ap_dev->lock);
1074 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->requestq_count);
1075 spin_unlock_bh(&ap_dev->lock);
1076 return rc;
1077}
1078
1079static DEVICE_ATTR(requestq_count, 0444, ap_requestq_count_show, NULL);
1080
1081static ssize_t ap_pendingq_count_show(struct device *dev,
1082 struct device_attribute *attr, char *buf)
1083{
1084 struct ap_device *ap_dev = to_ap_dev(dev);
1085 int rc;
1086
1087 spin_lock_bh(&ap_dev->lock);
1088 rc = snprintf(buf, PAGE_SIZE, "%d\n", ap_dev->pendingq_count);
1089 spin_unlock_bh(&ap_dev->lock);
1090 return rc;
1091}
1092
1093static DEVICE_ATTR(pendingq_count, 0444, ap_pendingq_count_show, NULL);
1094
1095static ssize_t ap_reset_show(struct device *dev,
1096 struct device_attribute *attr, char *buf)
1097{
1098 struct ap_device *ap_dev = to_ap_dev(dev);
1099 int rc = 0;
1100
1101 spin_lock_bh(&ap_dev->lock);
1102 switch (ap_dev->state) {
1103 case AP_STATE_RESET_START:
1104 case AP_STATE_RESET_WAIT:
1105 rc = snprintf(buf, PAGE_SIZE, "Reset in progress.\n");
1106 break;
1107 case AP_STATE_WORKING:
1108 case AP_STATE_QUEUE_FULL:
1109 rc = snprintf(buf, PAGE_SIZE, "Reset Timer armed.\n");
1110 break;
1111 default:
1112 rc = snprintf(buf, PAGE_SIZE, "No Reset Timer set.\n");
1113 }
1114 spin_unlock_bh(&ap_dev->lock);
1115 return rc;
1116}
1117
1118static DEVICE_ATTR(reset, 0444, ap_reset_show, NULL);
1119
1120static ssize_t ap_interrupt_show(struct device *dev,
1121 struct device_attribute *attr, char *buf)
1122{
1123 struct ap_device *ap_dev = to_ap_dev(dev);
1124 int rc = 0;
1125
1126 spin_lock_bh(&ap_dev->lock);
1127 if (ap_dev->state == AP_STATE_SETIRQ_WAIT)
1128 rc = snprintf(buf, PAGE_SIZE, "Enable Interrupt pending.\n");
1129 else if (ap_dev->interrupt == AP_INTR_ENABLED)
1130 rc = snprintf(buf, PAGE_SIZE, "Interrupts enabled.\n");
1131 else
1132 rc = snprintf(buf, PAGE_SIZE, "Interrupts disabled.\n");
1133 spin_unlock_bh(&ap_dev->lock);
1134 return rc;
1135}
1136
1137static DEVICE_ATTR(interrupt, 0444, ap_interrupt_show, NULL);
1138
1139static ssize_t ap_modalias_show(struct device *dev,
1140 struct device_attribute *attr, char *buf)
1141{
1142 return sprintf(buf, "ap:t%02X\n", to_ap_dev(dev)->device_type);
1143}
1144
1145static DEVICE_ATTR(modalias, 0444, ap_modalias_show, NULL);
1146
1147static ssize_t ap_functions_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149{
1150 struct ap_device *ap_dev = to_ap_dev(dev);
1151 return snprintf(buf, PAGE_SIZE, "0x%08X\n", ap_dev->functions);
1152}
1153
1154static DEVICE_ATTR(ap_functions, 0444, ap_functions_show, NULL);
1155
1156static struct attribute *ap_dev_attrs[] = {
1157 &dev_attr_hwtype.attr,
1158 &dev_attr_raw_hwtype.attr,
1159 &dev_attr_depth.attr,
1160 &dev_attr_request_count.attr,
1161 &dev_attr_requestq_count.attr,
1162 &dev_attr_pendingq_count.attr,
1163 &dev_attr_reset.attr,
1164 &dev_attr_interrupt.attr,
1165 &dev_attr_modalias.attr,
1166 &dev_attr_ap_functions.attr,
1167 NULL
1168};
1169static struct attribute_group ap_dev_attr_group = {
1170 .attrs = ap_dev_attrs
1171};
1172
1173
1174
1175
1176
1177
1178
1179
1180static int ap_bus_match(struct device *dev, struct device_driver *drv)
1181{
1182 struct ap_device *ap_dev = to_ap_dev(dev);
1183 struct ap_driver *ap_drv = to_ap_drv(drv);
1184 struct ap_device_id *id;
1185
1186
1187
1188
1189
1190 for (id = ap_drv->ids; id->match_flags; id++) {
1191 if ((id->match_flags & AP_DEVICE_ID_MATCH_DEVICE_TYPE) &&
1192 (id->dev_type != ap_dev->device_type))
1193 continue;
1194 return 1;
1195 }
1196 return 0;
1197}
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
1208{
1209 struct ap_device *ap_dev = to_ap_dev(dev);
1210 int retval = 0;
1211
1212 if (!ap_dev)
1213 return -ENODEV;
1214
1215
1216 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
1217 if (retval)
1218 return retval;
1219
1220
1221 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
1222
1223 return retval;
1224}
1225
1226static int ap_dev_suspend(struct device *dev, pm_message_t state)
1227{
1228 struct ap_device *ap_dev = to_ap_dev(dev);
1229
1230
1231 spin_lock_bh(&ap_dev->lock);
1232 ap_dev->state = AP_STATE_SUSPEND_WAIT;
1233 while (ap_sm_event(ap_dev, AP_EVENT_POLL) != AP_WAIT_NONE)
1234 ;
1235 ap_dev->state = AP_STATE_BORKED;
1236 spin_unlock_bh(&ap_dev->lock);
1237 return 0;
1238}
1239
1240static int ap_dev_resume(struct device *dev)
1241{
1242 return 0;
1243}
1244
1245static void ap_bus_suspend(void)
1246{
1247 ap_suspend_flag = 1;
1248
1249
1250
1251
1252 flush_work(&ap_scan_work);
1253 tasklet_disable(&ap_tasklet);
1254}
1255
1256static int __ap_devices_unregister(struct device *dev, void *dummy)
1257{
1258 device_unregister(dev);
1259 return 0;
1260}
1261
1262static void ap_bus_resume(void)
1263{
1264 int rc;
1265
1266
1267 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
1268
1269 if (ap_interrupts_available() && !ap_using_interrupts()) {
1270 rc = register_adapter_interrupt(&ap_airq);
1271 ap_airq_flag = (rc == 0);
1272 }
1273 if (!ap_interrupts_available() && ap_using_interrupts()) {
1274 unregister_adapter_interrupt(&ap_airq);
1275 ap_airq_flag = 0;
1276 }
1277
1278 if (!user_set_domain)
1279 ap_domain_index = -1;
1280
1281 ap_suspend_flag = 0;
1282 if (ap_airq_flag)
1283 xchg(ap_airq.lsi_ptr, 0);
1284 tasklet_enable(&ap_tasklet);
1285 queue_work(system_long_wq, &ap_scan_work);
1286}
1287
1288static int ap_power_event(struct notifier_block *this, unsigned long event,
1289 void *ptr)
1290{
1291 switch (event) {
1292 case PM_HIBERNATION_PREPARE:
1293 case PM_SUSPEND_PREPARE:
1294 ap_bus_suspend();
1295 break;
1296 case PM_POST_HIBERNATION:
1297 case PM_POST_SUSPEND:
1298 ap_bus_resume();
1299 break;
1300 default:
1301 break;
1302 }
1303 return NOTIFY_DONE;
1304}
1305static struct notifier_block ap_power_notifier = {
1306 .notifier_call = ap_power_event,
1307};
1308
1309static struct bus_type ap_bus_type = {
1310 .name = "ap",
1311 .match = &ap_bus_match,
1312 .uevent = &ap_uevent,
1313 .suspend = ap_dev_suspend,
1314 .resume = ap_dev_resume,
1315};
1316
1317static int ap_device_probe(struct device *dev)
1318{
1319 struct ap_device *ap_dev = to_ap_dev(dev);
1320 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
1321 int rc;
1322
1323 ap_dev->drv = ap_drv;
1324 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
1325 if (rc)
1326 ap_dev->drv = NULL;
1327 return rc;
1328}
1329
1330
1331
1332
1333
1334
1335
1336static void __ap_flush_queue(struct ap_device *ap_dev)
1337{
1338 struct ap_message *ap_msg, *next;
1339
1340 list_for_each_entry_safe(ap_msg, next, &ap_dev->pendingq, list) {
1341 list_del_init(&ap_msg->list);
1342 ap_dev->pendingq_count--;
1343 ap_msg->rc = -EAGAIN;
1344 ap_msg->receive(ap_dev, ap_msg, NULL);
1345 }
1346 list_for_each_entry_safe(ap_msg, next, &ap_dev->requestq, list) {
1347 list_del_init(&ap_msg->list);
1348 ap_dev->requestq_count--;
1349 ap_msg->rc = -EAGAIN;
1350 ap_msg->receive(ap_dev, ap_msg, NULL);
1351 }
1352}
1353
1354void ap_flush_queue(struct ap_device *ap_dev)
1355{
1356 spin_lock_bh(&ap_dev->lock);
1357 __ap_flush_queue(ap_dev);
1358 spin_unlock_bh(&ap_dev->lock);
1359}
1360EXPORT_SYMBOL(ap_flush_queue);
1361
1362static int ap_device_remove(struct device *dev)
1363{
1364 struct ap_device *ap_dev = to_ap_dev(dev);
1365 struct ap_driver *ap_drv = ap_dev->drv;
1366
1367 ap_flush_queue(ap_dev);
1368 del_timer_sync(&ap_dev->timeout);
1369 spin_lock_bh(&ap_device_list_lock);
1370 list_del_init(&ap_dev->list);
1371 spin_unlock_bh(&ap_device_list_lock);
1372 if (ap_drv->remove)
1373 ap_drv->remove(ap_dev);
1374 spin_lock_bh(&ap_dev->lock);
1375 atomic_sub(ap_dev->queue_count, &ap_poll_requests);
1376 spin_unlock_bh(&ap_dev->lock);
1377 return 0;
1378}
1379
1380static void ap_device_release(struct device *dev)
1381{
1382 kfree(to_ap_dev(dev));
1383}
1384
1385int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1386 char *name)
1387{
1388 struct device_driver *drv = &ap_drv->driver;
1389
1390 if (!initialised)
1391 return -ENODEV;
1392
1393 drv->bus = &ap_bus_type;
1394 drv->probe = ap_device_probe;
1395 drv->remove = ap_device_remove;
1396 drv->owner = owner;
1397 drv->name = name;
1398 return driver_register(drv);
1399}
1400EXPORT_SYMBOL(ap_driver_register);
1401
1402void ap_driver_unregister(struct ap_driver *ap_drv)
1403{
1404 driver_unregister(&ap_drv->driver);
1405}
1406EXPORT_SYMBOL(ap_driver_unregister);
1407
1408void ap_bus_force_rescan(void)
1409{
1410 if (ap_suspend_flag)
1411 return;
1412
1413 del_timer(&ap_config_timer);
1414 queue_work(system_long_wq, &ap_scan_work);
1415 flush_work(&ap_scan_work);
1416}
1417EXPORT_SYMBOL(ap_bus_force_rescan);
1418
1419
1420
1421
1422static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1423{
1424 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1425}
1426
1427static BUS_ATTR(ap_domain, 0444, ap_domain_show, NULL);
1428
1429static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1430{
1431 if (!ap_configuration)
1432 return snprintf(buf, PAGE_SIZE, "not supported\n");
1433 if (!test_facility(76))
1434
1435 return snprintf(buf, PAGE_SIZE, "%08x%08x\n",
1436 ap_configuration->adm[0],
1437 ap_configuration->adm[1]);
1438
1439 return snprintf(buf, PAGE_SIZE,
1440 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1441 ap_configuration->adm[0], ap_configuration->adm[1],
1442 ap_configuration->adm[2], ap_configuration->adm[3],
1443 ap_configuration->adm[4], ap_configuration->adm[5],
1444 ap_configuration->adm[6], ap_configuration->adm[7]);
1445}
1446
1447static BUS_ATTR(ap_control_domain_mask, 0444,
1448 ap_control_domain_mask_show, NULL);
1449
1450static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
1451{
1452 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1453}
1454
1455static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1456{
1457 return snprintf(buf, PAGE_SIZE, "%d\n",
1458 ap_using_interrupts() ? 1 : 0);
1459}
1460
1461static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
1462
1463static ssize_t ap_config_time_store(struct bus_type *bus,
1464 const char *buf, size_t count)
1465{
1466 int time;
1467
1468 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1469 return -EINVAL;
1470 ap_config_time = time;
1471 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1472 return count;
1473}
1474
1475static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
1476
1477static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
1478{
1479 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1480}
1481
1482static ssize_t ap_poll_thread_store(struct bus_type *bus,
1483 const char *buf, size_t count)
1484{
1485 int flag, rc;
1486
1487 if (sscanf(buf, "%d\n", &flag) != 1)
1488 return -EINVAL;
1489 if (flag) {
1490 rc = ap_poll_thread_start();
1491 if (rc)
1492 count = rc;
1493 } else
1494 ap_poll_thread_stop();
1495 return count;
1496}
1497
1498static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
1499
1500static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1501{
1502 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1503}
1504
1505static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1506 size_t count)
1507{
1508 unsigned long long time;
1509 ktime_t hr_time;
1510
1511
1512 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1513 time > 120000000000ULL)
1514 return -EINVAL;
1515 poll_timeout = time;
1516 hr_time = ktime_set(0, poll_timeout);
1517
1518 spin_lock_bh(&ap_poll_timer_lock);
1519 hrtimer_cancel(&ap_poll_timer);
1520 hrtimer_set_expires(&ap_poll_timer, hr_time);
1521 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1522 spin_unlock_bh(&ap_poll_timer_lock);
1523
1524 return count;
1525}
1526
1527static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
1528
1529static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1530{
1531 int max_domain_id;
1532
1533 if (ap_configuration)
1534 max_domain_id = ap_max_domain_id ? : -1;
1535 else
1536 max_domain_id = 15;
1537 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1538}
1539
1540static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
1541
1542static struct bus_attribute *const ap_bus_attrs[] = {
1543 &bus_attr_ap_domain,
1544 &bus_attr_ap_control_domain_mask,
1545 &bus_attr_config_time,
1546 &bus_attr_poll_thread,
1547 &bus_attr_ap_interrupts,
1548 &bus_attr_poll_timeout,
1549 &bus_attr_ap_max_domain_id,
1550 NULL,
1551};
1552
1553
1554
1555
1556
1557
1558static int ap_select_domain(void)
1559{
1560 int count, max_count, best_domain;
1561 struct ap_queue_status status;
1562 int i, j;
1563
1564
1565
1566
1567
1568
1569 if (ap_domain_index >= 0)
1570
1571 return 0;
1572 best_domain = -1;
1573 max_count = 0;
1574 for (i = 0; i < AP_DOMAINS; i++) {
1575 if (!ap_test_config_domain(i))
1576 continue;
1577 count = 0;
1578 for (j = 0; j < AP_DEVICES; j++) {
1579 if (!ap_test_config_card_id(j))
1580 continue;
1581 status = ap_test_queue(AP_MKQID(j, i), NULL);
1582 if (status.response_code != AP_RESPONSE_NORMAL)
1583 continue;
1584 count++;
1585 }
1586 if (count > max_count) {
1587 max_count = count;
1588 best_domain = i;
1589 }
1590 }
1591 if (best_domain >= 0){
1592 ap_domain_index = best_domain;
1593 return 0;
1594 }
1595 return -ENODEV;
1596}
1597
1598
1599
1600
1601
1602
1603
1604
1605static int __ap_scan_bus(struct device *dev, void *data)
1606{
1607 return to_ap_dev(dev)->qid == (ap_qid_t)(unsigned long) data;
1608}
1609
1610static void ap_scan_bus(struct work_struct *unused)
1611{
1612 struct ap_device *ap_dev;
1613 struct device *dev;
1614 ap_qid_t qid;
1615 int queue_depth = 0, device_type = 0;
1616 unsigned int device_functions = 0;
1617 int rc, i, borked;
1618
1619 ap_query_configuration();
1620 if (ap_select_domain() != 0)
1621 goto out;
1622
1623 for (i = 0; i < AP_DEVICES; i++) {
1624 qid = AP_MKQID(i, ap_domain_index);
1625 dev = bus_find_device(&ap_bus_type, NULL,
1626 (void *)(unsigned long)qid,
1627 __ap_scan_bus);
1628 rc = ap_query_queue(qid, &queue_depth, &device_type,
1629 &device_functions);
1630 if (dev) {
1631 ap_dev = to_ap_dev(dev);
1632 spin_lock_bh(&ap_dev->lock);
1633 if (rc == -ENODEV)
1634 ap_dev->state = AP_STATE_BORKED;
1635 borked = ap_dev->state == AP_STATE_BORKED;
1636 spin_unlock_bh(&ap_dev->lock);
1637 if (borked)
1638 device_unregister(dev);
1639 put_device(dev);
1640 if (!borked)
1641 continue;
1642 }
1643 if (rc)
1644 continue;
1645 ap_dev = kzalloc(sizeof(*ap_dev), GFP_KERNEL);
1646 if (!ap_dev)
1647 break;
1648 ap_dev->qid = qid;
1649 ap_dev->state = AP_STATE_RESET_START;
1650 ap_dev->interrupt = AP_INTR_DISABLED;
1651 ap_dev->queue_depth = queue_depth;
1652 ap_dev->raw_hwtype = device_type;
1653 ap_dev->device_type = device_type;
1654 ap_dev->functions = device_functions;
1655 spin_lock_init(&ap_dev->lock);
1656 INIT_LIST_HEAD(&ap_dev->pendingq);
1657 INIT_LIST_HEAD(&ap_dev->requestq);
1658 INIT_LIST_HEAD(&ap_dev->list);
1659 setup_timer(&ap_dev->timeout, ap_request_timeout,
1660 (unsigned long) ap_dev);
1661
1662 ap_dev->device.bus = &ap_bus_type;
1663 ap_dev->device.parent = ap_root_device;
1664 rc = dev_set_name(&ap_dev->device, "card%02x",
1665 AP_QID_DEVICE(ap_dev->qid));
1666 if (rc) {
1667 kfree(ap_dev);
1668 continue;
1669 }
1670
1671 spin_lock_bh(&ap_device_list_lock);
1672 list_add(&ap_dev->list, &ap_device_list);
1673 spin_unlock_bh(&ap_device_list_lock);
1674
1675 spin_lock_bh(&ap_dev->lock);
1676 ap_sm_wait(ap_sm_event(ap_dev, AP_EVENT_POLL));
1677 spin_unlock_bh(&ap_dev->lock);
1678
1679 ap_dev->device.release = ap_device_release;
1680 rc = device_register(&ap_dev->device);
1681 if (rc) {
1682 spin_lock_bh(&ap_dev->lock);
1683 list_del_init(&ap_dev->list);
1684 spin_unlock_bh(&ap_dev->lock);
1685 put_device(&ap_dev->device);
1686 continue;
1687 }
1688
1689 rc = sysfs_create_group(&ap_dev->device.kobj,
1690 &ap_dev_attr_group);
1691 if (rc) {
1692 device_unregister(&ap_dev->device);
1693 continue;
1694 }
1695 }
1696out:
1697 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1698}
1699
1700static void ap_config_timeout(unsigned long ptr)
1701{
1702 if (ap_suspend_flag)
1703 return;
1704 queue_work(system_long_wq, &ap_scan_work);
1705}
1706
1707static void ap_reset_domain(void)
1708{
1709 int i;
1710
1711 if (ap_domain_index == -1 || !ap_test_config_domain(ap_domain_index))
1712 return;
1713 for (i = 0; i < AP_DEVICES; i++)
1714 ap_reset_queue(AP_MKQID(i, ap_domain_index));
1715}
1716
1717static void ap_reset_all(void)
1718{
1719 int i, j;
1720
1721 for (i = 0; i < AP_DOMAINS; i++) {
1722 if (!ap_test_config_domain(i))
1723 continue;
1724 for (j = 0; j < AP_DEVICES; j++) {
1725 if (!ap_test_config_card_id(j))
1726 continue;
1727 ap_reset_queue(AP_MKQID(j, i));
1728 }
1729 }
1730}
1731
1732static struct reset_call ap_reset_call = {
1733 .fn = ap_reset_all,
1734};
1735
1736
1737
1738
1739
1740
1741int __init ap_module_init(void)
1742{
1743 int max_domain_id;
1744 int rc, i;
1745
1746 if (ap_instructions_available() != 0) {
1747 pr_warn("The hardware system does not support AP instructions\n");
1748 return -ENODEV;
1749 }
1750
1751
1752 ap_init_configuration();
1753
1754 if (ap_configuration)
1755 max_domain_id = ap_max_domain_id ? : (AP_DOMAINS - 1);
1756 else
1757 max_domain_id = 15;
1758 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1759 pr_warn("%d is not a valid cryptographic domain\n",
1760 ap_domain_index);
1761 return -EINVAL;
1762 }
1763
1764
1765
1766 if (ap_domain_index >= 0)
1767 user_set_domain = 1;
1768
1769 if (ap_interrupts_available()) {
1770 rc = register_adapter_interrupt(&ap_airq);
1771 ap_airq_flag = (rc == 0);
1772 }
1773
1774 register_reset_call(&ap_reset_call);
1775
1776
1777 rc = bus_register(&ap_bus_type);
1778 if (rc)
1779 goto out;
1780 for (i = 0; ap_bus_attrs[i]; i++) {
1781 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1782 if (rc)
1783 goto out_bus;
1784 }
1785
1786
1787 ap_root_device = root_device_register("ap");
1788 rc = PTR_RET(ap_root_device);
1789 if (rc)
1790 goto out_bus;
1791
1792
1793 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1794
1795
1796
1797
1798
1799 if (MACHINE_IS_VM)
1800 poll_timeout = 1500000;
1801 spin_lock_init(&ap_poll_timer_lock);
1802 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1803 ap_poll_timer.function = ap_poll_timeout;
1804
1805
1806 if (ap_thread_flag) {
1807 rc = ap_poll_thread_start();
1808 if (rc)
1809 goto out_work;
1810 }
1811
1812 rc = register_pm_notifier(&ap_power_notifier);
1813 if (rc)
1814 goto out_pm;
1815
1816 queue_work(system_long_wq, &ap_scan_work);
1817 initialised = true;
1818
1819 return 0;
1820
1821out_pm:
1822 ap_poll_thread_stop();
1823out_work:
1824 hrtimer_cancel(&ap_poll_timer);
1825 root_device_unregister(ap_root_device);
1826out_bus:
1827 while (i--)
1828 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1829 bus_unregister(&ap_bus_type);
1830out:
1831 unregister_reset_call(&ap_reset_call);
1832 if (ap_using_interrupts())
1833 unregister_adapter_interrupt(&ap_airq);
1834 kfree(ap_configuration);
1835 return rc;
1836}
1837
1838
1839
1840
1841
1842
1843void ap_module_exit(void)
1844{
1845 int i;
1846
1847 initialised = false;
1848 ap_reset_domain();
1849 ap_poll_thread_stop();
1850 del_timer_sync(&ap_config_timer);
1851 hrtimer_cancel(&ap_poll_timer);
1852 tasklet_kill(&ap_tasklet);
1853 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_devices_unregister);
1854 for (i = 0; ap_bus_attrs[i]; i++)
1855 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1856 unregister_pm_notifier(&ap_power_notifier);
1857 root_device_unregister(ap_root_device);
1858 bus_unregister(&ap_bus_type);
1859 kfree(ap_configuration);
1860 unregister_reset_call(&ap_reset_call);
1861 if (ap_using_interrupts())
1862 unregister_adapter_interrupt(&ap_airq);
1863}
1864
1865module_init(ap_module_init);
1866module_exit(ap_module_exit);
1867