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