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