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/moduleparam.h>
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/err.h>
34#include <linux/interrupt.h>
35#include <linux/workqueue.h>
36#include <linux/slab.h>
37#include <linux/notifier.h>
38#include <linux/kthread.h>
39#include <linux/mutex.h>
40#include <linux/suspend.h>
41#include <asm/reset.h>
42#include <asm/airq.h>
43#include <linux/atomic.h>
44#include <asm/isc.h>
45#include <linux/hrtimer.h>
46#include <linux/ktime.h>
47#include <asm/facility.h>
48#include <linux/crypto.h>
49#include <linux/mod_devicetable.h>
50#include <linux/debugfs.h>
51
52#include "ap_bus.h"
53#include "ap_asm.h"
54#include "ap_debug.h"
55
56
57
58
59int ap_domain_index = -1;
60static DEFINE_SPINLOCK(ap_domain_lock);
61module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
62MODULE_PARM_DESC(domain, "domain index for ap devices");
63EXPORT_SYMBOL(ap_domain_index);
64
65static int ap_thread_flag = 0;
66module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
67MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
68
69static struct device *ap_root_device;
70
71DEFINE_SPINLOCK(ap_list_lock);
72LIST_HEAD(ap_card_list);
73
74static struct ap_config_info *ap_configuration;
75static bool initialised;
76
77
78
79
80debug_info_t *ap_dbf_info;
81
82
83
84
85static struct timer_list ap_config_timer;
86static int ap_config_time = AP_CONFIG_TIME;
87static void ap_scan_bus(struct work_struct *);
88static DECLARE_WORK(ap_scan_work, ap_scan_bus);
89
90
91
92
93static void ap_tasklet_fn(unsigned long);
94static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
95static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96static struct task_struct *ap_poll_kthread = NULL;
97static DEFINE_MUTEX(ap_poll_thread_mutex);
98static DEFINE_SPINLOCK(ap_poll_timer_lock);
99static struct hrtimer ap_poll_timer;
100
101
102static unsigned long long poll_timeout = 250000;
103
104
105static int ap_suspend_flag;
106
107static int ap_max_domain_id;
108
109
110
111static int user_set_domain = 0;
112static struct bus_type ap_bus_type;
113
114
115static void ap_interrupt_handler(struct airq_struct *airq);
116
117static int ap_airq_flag;
118
119static struct airq_struct ap_airq = {
120 .handler = ap_interrupt_handler,
121 .isc = AP_ISC,
122};
123
124
125
126
127
128static inline int ap_using_interrupts(void)
129{
130 return ap_airq_flag;
131}
132
133
134
135
136
137
138
139
140void *ap_airq_ptr(void)
141{
142 if (ap_using_interrupts())
143 return ap_airq.lsi_ptr;
144 return NULL;
145}
146
147
148
149
150
151
152static int ap_interrupts_available(void)
153{
154 return test_facility(65);
155}
156
157
158
159
160
161
162
163static int ap_configuration_available(void)
164{
165 return test_facility(12);
166}
167
168
169
170
171
172
173
174static int ap_apft_available(void)
175{
176 return test_facility(15);
177}
178
179
180
181
182
183
184
185
186
187struct ap_queue_status ap_test_queue(ap_qid_t qid,
188 int tbit,
189 unsigned long *info)
190{
191 if (tbit)
192 qid |= 1UL << 23;
193 return ap_tapq(qid, info);
194}
195EXPORT_SYMBOL(ap_test_queue);
196
197
198
199
200
201
202
203
204
205int ap_query_configuration(struct ap_config_info *info)
206{
207 if (!ap_configuration_available())
208 return -EOPNOTSUPP;
209 if (!info)
210 return -EINVAL;
211 return ap_qci(info);
212}
213EXPORT_SYMBOL(ap_query_configuration);
214
215
216
217
218static void ap_init_configuration(void)
219{
220 if (!ap_configuration_available())
221 return;
222
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
225 return;
226 if (ap_query_configuration(ap_configuration) != 0) {
227 kfree(ap_configuration);
228 ap_configuration = NULL;
229 return;
230 }
231}
232
233
234
235
236
237static inline int ap_test_config(unsigned int *field, unsigned int nr)
238{
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
240}
241
242
243
244
245
246
247
248
249
250static inline int ap_test_config_card_id(unsigned int id)
251{
252 if (!ap_configuration)
253 return 1;
254 return ap_test_config(ap_configuration->apm, id);
255}
256
257
258
259
260
261
262
263
264
265static inline int ap_test_config_domain(unsigned int domain)
266{
267 if (!ap_configuration)
268 return domain < 16;
269 return ap_test_config(ap_configuration->aqm, domain);
270}
271
272
273
274
275
276
277
278
279static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
280 unsigned int *facilities)
281{
282 struct ap_queue_status status;
283 unsigned long info;
284 int nd;
285
286 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
287 return -ENODEV;
288
289 status = ap_test_queue(qid, ap_apft_available(), &info);
290 switch (status.response_code) {
291 case AP_RESPONSE_NORMAL:
292 *queue_depth = (int)(info & 0xff);
293 *device_type = (int)((info >> 24) & 0xff);
294 *facilities = (unsigned int)(info >> 32);
295
296 nd = (info >> 16) & 0xff;
297
298 if ((info & (1UL << 57)) && nd > 0)
299 ap_max_domain_id = nd;
300 else
301 ap_max_domain_id = 15;
302 switch (*device_type) {
303
304
305
306
307
308 case AP_DEVICE_TYPE_CEX2A:
309 case AP_DEVICE_TYPE_CEX3A:
310 *facilities |= 0x08000000;
311 break;
312 case AP_DEVICE_TYPE_CEX2C:
313 case AP_DEVICE_TYPE_CEX3C:
314 *facilities |= 0x10000000;
315 break;
316 default:
317 break;
318 }
319 return 0;
320 case AP_RESPONSE_Q_NOT_AVAIL:
321 case AP_RESPONSE_DECONFIGURED:
322 case AP_RESPONSE_CHECKSTOPPED:
323 case AP_RESPONSE_INVALID_ADDRESS:
324 return -ENODEV;
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 case AP_RESPONSE_OTHERWISE_CHANGED:
327 case AP_RESPONSE_BUSY:
328 return -EBUSY;
329 default:
330 BUG();
331 }
332}
333
334void ap_wait(enum ap_wait wait)
335{
336 ktime_t hr_time;
337
338 switch (wait) {
339 case AP_WAIT_AGAIN:
340 case AP_WAIT_INTERRUPT:
341 if (ap_using_interrupts())
342 break;
343 if (ap_poll_kthread) {
344 wake_up(&ap_poll_wait);
345 break;
346 }
347
348 case AP_WAIT_TIMEOUT:
349 spin_lock_bh(&ap_poll_timer_lock);
350 if (!hrtimer_is_queued(&ap_poll_timer)) {
351 hr_time = poll_timeout;
352 hrtimer_forward_now(&ap_poll_timer, hr_time);
353 hrtimer_restart(&ap_poll_timer);
354 }
355 spin_unlock_bh(&ap_poll_timer_lock);
356 break;
357 case AP_WAIT_NONE:
358 default:
359 break;
360 }
361}
362
363
364
365
366
367
368
369void ap_request_timeout(unsigned long data)
370{
371 struct ap_queue *aq = (struct ap_queue *) data;
372
373 if (ap_suspend_flag)
374 return;
375 spin_lock_bh(&aq->lock);
376 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
377 spin_unlock_bh(&aq->lock);
378}
379
380
381
382
383
384
385
386static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
387{
388 if (!ap_suspend_flag)
389 tasklet_schedule(&ap_tasklet);
390 return HRTIMER_NORESTART;
391}
392
393
394
395
396
397static void ap_interrupt_handler(struct airq_struct *airq)
398{
399 inc_irq_stat(IRQIO_APB);
400 if (!ap_suspend_flag)
401 tasklet_schedule(&ap_tasklet);
402}
403
404
405
406
407
408
409
410static void ap_tasklet_fn(unsigned long dummy)
411{
412 struct ap_card *ac;
413 struct ap_queue *aq;
414 enum ap_wait wait = AP_WAIT_NONE;
415
416
417
418
419
420 if (ap_using_interrupts())
421 xchg(ap_airq.lsi_ptr, 0);
422
423 spin_lock_bh(&ap_list_lock);
424 for_each_ap_card(ac) {
425 for_each_ap_queue(aq, ac) {
426 spin_lock_bh(&aq->lock);
427 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
428 spin_unlock_bh(&aq->lock);
429 }
430 }
431 spin_unlock_bh(&ap_list_lock);
432
433 ap_wait(wait);
434}
435
436static int ap_pending_requests(void)
437{
438 struct ap_card *ac;
439 struct ap_queue *aq;
440
441 spin_lock_bh(&ap_list_lock);
442 for_each_ap_card(ac) {
443 for_each_ap_queue(aq, ac) {
444 if (aq->queue_count == 0)
445 continue;
446 spin_unlock_bh(&ap_list_lock);
447 return 1;
448 }
449 }
450 spin_unlock_bh(&ap_list_lock);
451 return 0;
452}
453
454
455
456
457
458
459
460
461
462
463
464static int ap_poll_thread(void *data)
465{
466 DECLARE_WAITQUEUE(wait, current);
467
468 set_user_nice(current, MAX_NICE);
469 set_freezable();
470 while (!kthread_should_stop()) {
471 add_wait_queue(&ap_poll_wait, &wait);
472 set_current_state(TASK_INTERRUPTIBLE);
473 if (ap_suspend_flag || !ap_pending_requests()) {
474 schedule();
475 try_to_freeze();
476 }
477 set_current_state(TASK_RUNNING);
478 remove_wait_queue(&ap_poll_wait, &wait);
479 if (need_resched()) {
480 schedule();
481 try_to_freeze();
482 continue;
483 }
484 ap_tasklet_fn(0);
485 }
486
487 return 0;
488}
489
490static int ap_poll_thread_start(void)
491{
492 int rc;
493
494 if (ap_using_interrupts() || ap_poll_kthread)
495 return 0;
496 mutex_lock(&ap_poll_thread_mutex);
497 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
498 rc = PTR_RET(ap_poll_kthread);
499 if (rc)
500 ap_poll_kthread = NULL;
501 mutex_unlock(&ap_poll_thread_mutex);
502 return rc;
503}
504
505static void ap_poll_thread_stop(void)
506{
507 if (!ap_poll_kthread)
508 return;
509 mutex_lock(&ap_poll_thread_mutex);
510 kthread_stop(ap_poll_kthread);
511 ap_poll_kthread = NULL;
512 mutex_unlock(&ap_poll_thread_mutex);
513}
514
515#define is_card_dev(x) ((x)->parent == ap_root_device)
516#define is_queue_dev(x) ((x)->parent != ap_root_device)
517
518
519
520
521
522
523
524
525static int ap_bus_match(struct device *dev, struct device_driver *drv)
526{
527 struct ap_driver *ap_drv = to_ap_drv(drv);
528 struct ap_device_id *id;
529
530
531
532
533
534 for (id = ap_drv->ids; id->match_flags; id++) {
535 if (is_card_dev(dev) &&
536 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
537 id->dev_type == to_ap_dev(dev)->device_type)
538 return 1;
539 if (is_queue_dev(dev) &&
540 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
541 id->dev_type == to_ap_dev(dev)->device_type)
542 return 1;
543 }
544 return 0;
545}
546
547
548
549
550
551
552
553
554
555static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
556{
557 struct ap_device *ap_dev = to_ap_dev(dev);
558 int retval = 0;
559
560 if (!ap_dev)
561 return -ENODEV;
562
563
564 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
565 if (retval)
566 return retval;
567
568
569 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
570
571 return retval;
572}
573
574static int ap_dev_suspend(struct device *dev)
575{
576 struct ap_device *ap_dev = to_ap_dev(dev);
577
578 if (ap_dev->drv && ap_dev->drv->suspend)
579 ap_dev->drv->suspend(ap_dev);
580 return 0;
581}
582
583static int ap_dev_resume(struct device *dev)
584{
585 struct ap_device *ap_dev = to_ap_dev(dev);
586
587 if (ap_dev->drv && ap_dev->drv->resume)
588 ap_dev->drv->resume(ap_dev);
589 return 0;
590}
591
592static void ap_bus_suspend(void)
593{
594 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
595
596 ap_suspend_flag = 1;
597
598
599
600
601 flush_work(&ap_scan_work);
602 tasklet_disable(&ap_tasklet);
603}
604
605static int __ap_card_devices_unregister(struct device *dev, void *dummy)
606{
607 if (is_card_dev(dev))
608 device_unregister(dev);
609 return 0;
610}
611
612static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
613{
614 if (is_queue_dev(dev))
615 device_unregister(dev);
616 return 0;
617}
618
619static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
620{
621 if (is_queue_dev(dev) &&
622 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
623 device_unregister(dev);
624 return 0;
625}
626
627static void ap_bus_resume(void)
628{
629 int rc;
630
631 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
632
633
634 bus_for_each_dev(&ap_bus_type, NULL, NULL,
635 __ap_queue_devices_unregister);
636
637 bus_for_each_dev(&ap_bus_type, NULL, NULL,
638 __ap_card_devices_unregister);
639
640
641 if (ap_interrupts_available() && !ap_using_interrupts()) {
642 rc = register_adapter_interrupt(&ap_airq);
643 ap_airq_flag = (rc == 0);
644 }
645 if (!ap_interrupts_available() && ap_using_interrupts()) {
646 unregister_adapter_interrupt(&ap_airq);
647 ap_airq_flag = 0;
648 }
649
650 if (!user_set_domain)
651 ap_domain_index = -1;
652
653 ap_suspend_flag = 0;
654 if (ap_airq_flag)
655 xchg(ap_airq.lsi_ptr, 0);
656 tasklet_enable(&ap_tasklet);
657 queue_work(system_long_wq, &ap_scan_work);
658}
659
660static int ap_power_event(struct notifier_block *this, unsigned long event,
661 void *ptr)
662{
663 switch (event) {
664 case PM_HIBERNATION_PREPARE:
665 case PM_SUSPEND_PREPARE:
666 ap_bus_suspend();
667 break;
668 case PM_POST_HIBERNATION:
669 case PM_POST_SUSPEND:
670 ap_bus_resume();
671 break;
672 default:
673 break;
674 }
675 return NOTIFY_DONE;
676}
677static struct notifier_block ap_power_notifier = {
678 .notifier_call = ap_power_event,
679};
680
681static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
682
683static struct bus_type ap_bus_type = {
684 .name = "ap",
685 .match = &ap_bus_match,
686 .uevent = &ap_uevent,
687 .pm = &ap_bus_pm_ops,
688};
689
690static int ap_device_probe(struct device *dev)
691{
692 struct ap_device *ap_dev = to_ap_dev(dev);
693 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
694 int rc;
695
696
697 spin_lock_bh(&ap_list_lock);
698 if (is_card_dev(dev))
699 list_add(&to_ap_card(dev)->list, &ap_card_list);
700 else
701 list_add(&to_ap_queue(dev)->list,
702 &to_ap_queue(dev)->card->queues);
703 spin_unlock_bh(&ap_list_lock);
704
705 ap_dev->drv = ap_drv;
706 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
707
708 if (rc) {
709 spin_lock_bh(&ap_list_lock);
710 if (is_card_dev(dev))
711 list_del_init(&to_ap_card(dev)->list);
712 else
713 list_del_init(&to_ap_queue(dev)->list);
714 spin_unlock_bh(&ap_list_lock);
715 ap_dev->drv = NULL;
716 }
717
718 return rc;
719}
720
721static int ap_device_remove(struct device *dev)
722{
723 struct ap_device *ap_dev = to_ap_dev(dev);
724 struct ap_driver *ap_drv = ap_dev->drv;
725
726 if (ap_drv->remove)
727 ap_drv->remove(ap_dev);
728
729
730 spin_lock_bh(&ap_list_lock);
731 if (is_card_dev(dev))
732 list_del_init(&to_ap_card(dev)->list);
733 else
734 list_del_init(&to_ap_queue(dev)->list);
735 spin_unlock_bh(&ap_list_lock);
736
737 return 0;
738}
739
740int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
741 char *name)
742{
743 struct device_driver *drv = &ap_drv->driver;
744
745 if (!initialised)
746 return -ENODEV;
747
748 drv->bus = &ap_bus_type;
749 drv->probe = ap_device_probe;
750 drv->remove = ap_device_remove;
751 drv->owner = owner;
752 drv->name = name;
753 return driver_register(drv);
754}
755EXPORT_SYMBOL(ap_driver_register);
756
757void ap_driver_unregister(struct ap_driver *ap_drv)
758{
759 driver_unregister(&ap_drv->driver);
760}
761EXPORT_SYMBOL(ap_driver_unregister);
762
763void ap_bus_force_rescan(void)
764{
765 if (ap_suspend_flag)
766 return;
767
768 del_timer(&ap_config_timer);
769 queue_work(system_long_wq, &ap_scan_work);
770 flush_work(&ap_scan_work);
771}
772EXPORT_SYMBOL(ap_bus_force_rescan);
773
774
775
776
777static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
778{
779 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
780}
781
782static ssize_t ap_domain_store(struct bus_type *bus,
783 const char *buf, size_t count)
784{
785 int domain;
786
787 if (sscanf(buf, "%i\n", &domain) != 1 ||
788 domain < 0 || domain > ap_max_domain_id)
789 return -EINVAL;
790 spin_lock_bh(&ap_domain_lock);
791 ap_domain_index = domain;
792 spin_unlock_bh(&ap_domain_lock);
793
794 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
795
796 return count;
797}
798
799static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
800
801static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
802{
803 if (!ap_configuration)
804 return snprintf(buf, PAGE_SIZE, "not supported\n");
805
806 return snprintf(buf, PAGE_SIZE,
807 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
808 ap_configuration->adm[0], ap_configuration->adm[1],
809 ap_configuration->adm[2], ap_configuration->adm[3],
810 ap_configuration->adm[4], ap_configuration->adm[5],
811 ap_configuration->adm[6], ap_configuration->adm[7]);
812}
813
814static BUS_ATTR(ap_control_domain_mask, 0444,
815 ap_control_domain_mask_show, NULL);
816
817static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
818{
819 if (!ap_configuration)
820 return snprintf(buf, PAGE_SIZE, "not supported\n");
821
822 return snprintf(buf, PAGE_SIZE,
823 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
824 ap_configuration->aqm[0], ap_configuration->aqm[1],
825 ap_configuration->aqm[2], ap_configuration->aqm[3],
826 ap_configuration->aqm[4], ap_configuration->aqm[5],
827 ap_configuration->aqm[6], ap_configuration->aqm[7]);
828}
829
830static BUS_ATTR(ap_usage_domain_mask, 0444,
831 ap_usage_domain_mask_show, NULL);
832
833static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
834{
835 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
836}
837
838static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
839{
840 return snprintf(buf, PAGE_SIZE, "%d\n",
841 ap_using_interrupts() ? 1 : 0);
842}
843
844static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
845
846static ssize_t ap_config_time_store(struct bus_type *bus,
847 const char *buf, size_t count)
848{
849 int time;
850
851 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
852 return -EINVAL;
853 ap_config_time = time;
854 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
855 return count;
856}
857
858static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
859
860static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
861{
862 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
863}
864
865static ssize_t ap_poll_thread_store(struct bus_type *bus,
866 const char *buf, size_t count)
867{
868 int flag, rc;
869
870 if (sscanf(buf, "%d\n", &flag) != 1)
871 return -EINVAL;
872 if (flag) {
873 rc = ap_poll_thread_start();
874 if (rc)
875 count = rc;
876 } else
877 ap_poll_thread_stop();
878 return count;
879}
880
881static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
882
883static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
884{
885 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
886}
887
888static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
889 size_t count)
890{
891 unsigned long long time;
892 ktime_t hr_time;
893
894
895 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
896 time > 120000000000ULL)
897 return -EINVAL;
898 poll_timeout = time;
899 hr_time = poll_timeout;
900
901 spin_lock_bh(&ap_poll_timer_lock);
902 hrtimer_cancel(&ap_poll_timer);
903 hrtimer_set_expires(&ap_poll_timer, hr_time);
904 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
905 spin_unlock_bh(&ap_poll_timer_lock);
906
907 return count;
908}
909
910static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
911
912static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
913{
914 int max_domain_id;
915
916 if (ap_configuration)
917 max_domain_id = ap_max_domain_id ? : -1;
918 else
919 max_domain_id = 15;
920 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
921}
922
923static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
924
925static struct bus_attribute *const ap_bus_attrs[] = {
926 &bus_attr_ap_domain,
927 &bus_attr_ap_control_domain_mask,
928 &bus_attr_ap_usage_domain_mask,
929 &bus_attr_config_time,
930 &bus_attr_poll_thread,
931 &bus_attr_ap_interrupts,
932 &bus_attr_poll_timeout,
933 &bus_attr_ap_max_domain_id,
934 NULL,
935};
936
937
938
939
940
941
942static int ap_select_domain(void)
943{
944 int count, max_count, best_domain;
945 struct ap_queue_status status;
946 int i, j;
947
948
949
950
951
952
953 spin_lock_bh(&ap_domain_lock);
954 if (ap_domain_index >= 0) {
955
956 spin_unlock_bh(&ap_domain_lock);
957 return 0;
958 }
959 best_domain = -1;
960 max_count = 0;
961 for (i = 0; i < AP_DOMAINS; i++) {
962 if (!ap_test_config_domain(i))
963 continue;
964 count = 0;
965 for (j = 0; j < AP_DEVICES; j++) {
966 if (!ap_test_config_card_id(j))
967 continue;
968 status = ap_test_queue(AP_MKQID(j, i),
969 ap_apft_available(),
970 NULL);
971 if (status.response_code != AP_RESPONSE_NORMAL)
972 continue;
973 count++;
974 }
975 if (count > max_count) {
976 max_count = count;
977 best_domain = i;
978 }
979 }
980 if (best_domain >= 0){
981 ap_domain_index = best_domain;
982 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
983 spin_unlock_bh(&ap_domain_lock);
984 return 0;
985 }
986 spin_unlock_bh(&ap_domain_lock);
987 return -ENODEV;
988}
989
990
991
992
993
994static int __match_card_device_with_id(struct device *dev, void *data)
995{
996 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
997}
998
999
1000
1001
1002static int __match_queue_device_with_qid(struct device *dev, void *data)
1003{
1004 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1005}
1006
1007
1008
1009
1010
1011static void ap_scan_bus(struct work_struct *unused)
1012{
1013 struct ap_queue *aq;
1014 struct ap_card *ac;
1015 struct device *dev;
1016 ap_qid_t qid;
1017 int depth = 0, type = 0;
1018 unsigned int functions = 0;
1019 int rc, id, dom, borked, domains, defdomdevs = 0;
1020
1021 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
1022
1023 ap_query_configuration(ap_configuration);
1024 if (ap_select_domain() != 0)
1025 goto out;
1026
1027 for (id = 0; id < AP_DEVICES; id++) {
1028
1029 dev = bus_find_device(&ap_bus_type, NULL,
1030 (void *)(long) id,
1031 __match_card_device_with_id);
1032 ac = dev ? to_ap_card(dev) : NULL;
1033 if (!ap_test_config_card_id(id)) {
1034 if (dev) {
1035
1036
1037
1038
1039 bus_for_each_dev(&ap_bus_type, NULL,
1040 (void *)(long) id,
1041 __ap_queue_devices_with_id_unregister);
1042
1043 device_unregister(dev);
1044 put_device(dev);
1045 }
1046 continue;
1047 }
1048
1049
1050
1051
1052 domains = 0;
1053 for (dom = 0; dom < AP_DOMAINS; dom++) {
1054 qid = AP_MKQID(id, dom);
1055 dev = bus_find_device(&ap_bus_type, NULL,
1056 (void *)(long) qid,
1057 __match_queue_device_with_qid);
1058 aq = dev ? to_ap_queue(dev) : NULL;
1059 if (!ap_test_config_domain(dom)) {
1060 if (dev) {
1061
1062
1063
1064 device_unregister(dev);
1065 put_device(dev);
1066 }
1067 continue;
1068 }
1069 rc = ap_query_queue(qid, &depth, &type, &functions);
1070 if (dev) {
1071 spin_lock_bh(&aq->lock);
1072 if (rc == -ENODEV ||
1073
1074 (ac && ac->functions != functions))
1075 aq->state = AP_STATE_BORKED;
1076 borked = aq->state == AP_STATE_BORKED;
1077 spin_unlock_bh(&aq->lock);
1078 if (borked)
1079 device_unregister(dev);
1080 put_device(dev);
1081 if (!borked) {
1082 domains++;
1083 if (dom == ap_domain_index)
1084 defdomdevs++;
1085 continue;
1086 }
1087 }
1088 if (rc)
1089 continue;
1090
1091 if (!ac) {
1092
1093 ac = ap_card_create(id, depth,
1094 type, functions);
1095 if (!ac)
1096 continue;
1097 ac->ap_dev.device.bus = &ap_bus_type;
1098 ac->ap_dev.device.parent = ap_root_device;
1099 dev_set_name(&ac->ap_dev.device,
1100 "card%02x", id);
1101
1102 rc = device_register(&ac->ap_dev.device);
1103 if (rc) {
1104 put_device(&ac->ap_dev.device);
1105 ac = NULL;
1106 break;
1107 }
1108
1109 get_device(&ac->ap_dev.device);
1110 }
1111
1112 aq = ap_queue_create(qid, type);
1113 if (!aq)
1114 continue;
1115 aq->card = ac;
1116 aq->ap_dev.device.bus = &ap_bus_type;
1117 aq->ap_dev.device.parent = &ac->ap_dev.device;
1118 dev_set_name(&aq->ap_dev.device,
1119 "%02x.%04x", id, dom);
1120
1121 spin_lock_bh(&aq->lock);
1122 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1123 spin_unlock_bh(&aq->lock);
1124
1125 rc = device_register(&aq->ap_dev.device);
1126 if (rc) {
1127 put_device(&aq->ap_dev.device);
1128 continue;
1129 }
1130 domains++;
1131 if (dom == ap_domain_index)
1132 defdomdevs++;
1133 }
1134 if (ac) {
1135
1136 if (!domains)
1137 device_unregister(&ac->ap_dev.device);
1138 put_device(&ac->ap_dev.device);
1139 }
1140 }
1141
1142 if (defdomdevs < 1)
1143 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n",
1144 ap_domain_index);
1145
1146out:
1147 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1148}
1149
1150static void ap_config_timeout(unsigned long ptr)
1151{
1152 if (ap_suspend_flag)
1153 return;
1154 queue_work(system_long_wq, &ap_scan_work);
1155}
1156
1157static void ap_reset_all(void)
1158{
1159 int i, j;
1160
1161 for (i = 0; i < AP_DOMAINS; i++) {
1162 if (!ap_test_config_domain(i))
1163 continue;
1164 for (j = 0; j < AP_DEVICES; j++) {
1165 if (!ap_test_config_card_id(j))
1166 continue;
1167 ap_rapq(AP_MKQID(j, i));
1168 }
1169 }
1170}
1171
1172static struct reset_call ap_reset_call = {
1173 .fn = ap_reset_all,
1174};
1175
1176int __init ap_debug_init(void)
1177{
1178 ap_dbf_info = debug_register("ap", 1, 1,
1179 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1180 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1181 debug_set_level(ap_dbf_info, DBF_ERR);
1182
1183 return 0;
1184}
1185
1186void ap_debug_exit(void)
1187{
1188 debug_unregister(ap_dbf_info);
1189}
1190
1191
1192
1193
1194
1195
1196int __init ap_module_init(void)
1197{
1198 int max_domain_id;
1199 int rc, i;
1200
1201 rc = ap_debug_init();
1202 if (rc)
1203 return rc;
1204
1205 if (ap_instructions_available() != 0) {
1206 pr_warn("The hardware system does not support AP instructions\n");
1207 return -ENODEV;
1208 }
1209
1210
1211 ap_init_configuration();
1212
1213 if (ap_configuration)
1214 max_domain_id =
1215 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1216 else
1217 max_domain_id = 15;
1218 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1219 pr_warn("%d is not a valid cryptographic domain\n",
1220 ap_domain_index);
1221 ap_domain_index = -1;
1222 }
1223
1224
1225
1226 if (ap_domain_index >= 0)
1227 user_set_domain = 1;
1228
1229 if (ap_interrupts_available()) {
1230 rc = register_adapter_interrupt(&ap_airq);
1231 ap_airq_flag = (rc == 0);
1232 }
1233
1234 register_reset_call(&ap_reset_call);
1235
1236
1237 rc = bus_register(&ap_bus_type);
1238 if (rc)
1239 goto out;
1240 for (i = 0; ap_bus_attrs[i]; i++) {
1241 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1242 if (rc)
1243 goto out_bus;
1244 }
1245
1246
1247 ap_root_device = root_device_register("ap");
1248 rc = PTR_RET(ap_root_device);
1249 if (rc)
1250 goto out_bus;
1251
1252
1253 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1254
1255
1256
1257
1258
1259 if (MACHINE_IS_VM)
1260 poll_timeout = 1500000;
1261 spin_lock_init(&ap_poll_timer_lock);
1262 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1263 ap_poll_timer.function = ap_poll_timeout;
1264
1265
1266 if (ap_thread_flag) {
1267 rc = ap_poll_thread_start();
1268 if (rc)
1269 goto out_work;
1270 }
1271
1272 rc = register_pm_notifier(&ap_power_notifier);
1273 if (rc)
1274 goto out_pm;
1275
1276 queue_work(system_long_wq, &ap_scan_work);
1277 initialised = true;
1278
1279 return 0;
1280
1281out_pm:
1282 ap_poll_thread_stop();
1283out_work:
1284 hrtimer_cancel(&ap_poll_timer);
1285 root_device_unregister(ap_root_device);
1286out_bus:
1287 while (i--)
1288 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1289 bus_unregister(&ap_bus_type);
1290out:
1291 unregister_reset_call(&ap_reset_call);
1292 if (ap_using_interrupts())
1293 unregister_adapter_interrupt(&ap_airq);
1294 kfree(ap_configuration);
1295 return rc;
1296}
1297device_initcall(ap_module_init);
1298