1
2
3
4
5
6
7
8
9
10
11
12
13#define KMSG_COMPONENT "ap"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16#include <linux/kernel_stat.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/freezer.h>
22#include <linux/interrupt.h>
23#include <linux/workqueue.h>
24#include <linux/slab.h>
25#include <linux/notifier.h>
26#include <linux/kthread.h>
27#include <linux/mutex.h>
28#include <asm/airq.h>
29#include <linux/atomic.h>
30#include <asm/isc.h>
31#include <linux/hrtimer.h>
32#include <linux/ktime.h>
33#include <asm/facility.h>
34#include <linux/crypto.h>
35#include <linux/mod_devicetable.h>
36#include <linux/debugfs.h>
37#include <linux/ctype.h>
38
39#include "ap_bus.h"
40#include "ap_debug.h"
41
42
43
44
45int ap_domain_index = -1;
46static DEFINE_SPINLOCK(ap_domain_lock);
47module_param_named(domain, ap_domain_index, int, 0440);
48MODULE_PARM_DESC(domain, "domain index for ap devices");
49EXPORT_SYMBOL(ap_domain_index);
50
51static int ap_thread_flag;
52module_param_named(poll_thread, ap_thread_flag, int, 0440);
53MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
54
55static char *apm_str;
56module_param_named(apmask, apm_str, charp, 0440);
57MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
58
59static char *aqm_str;
60module_param_named(aqmask, aqm_str, charp, 0440);
61MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
62
63static struct device *ap_root_device;
64
65
66DEFINE_HASHTABLE(ap_queues, 8);
67
68DEFINE_SPINLOCK(ap_queues_lock);
69
70
71struct ap_perms ap_perms;
72EXPORT_SYMBOL(ap_perms);
73DEFINE_MUTEX(ap_perms_mutex);
74EXPORT_SYMBOL(ap_perms_mutex);
75
76static struct ap_config_info *ap_configuration;
77static bool initialised;
78
79
80
81
82debug_info_t *ap_dbf_info;
83
84
85
86
87static struct timer_list ap_config_timer;
88static int ap_config_time = AP_CONFIG_TIME;
89static void ap_scan_bus(struct work_struct *);
90static DECLARE_WORK(ap_scan_work, ap_scan_bus);
91
92
93
94
95static void ap_tasklet_fn(unsigned long);
96static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
97static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
98static struct task_struct *ap_poll_kthread;
99static DEFINE_MUTEX(ap_poll_thread_mutex);
100static DEFINE_SPINLOCK(ap_poll_timer_lock);
101static struct hrtimer ap_poll_timer;
102
103
104
105
106static unsigned long long poll_timeout = 250000;
107
108
109static int ap_max_domain_id;
110
111static struct bus_type ap_bus_type;
112
113
114static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
115
116static int ap_airq_flag;
117
118static struct airq_struct ap_airq = {
119 .handler = ap_interrupt_handler,
120 .isc = AP_ISC,
121};
122
123
124
125
126
127static inline int ap_using_interrupts(void)
128{
129 return ap_airq_flag;
130}
131
132
133
134
135
136
137
138
139void *ap_airq_ptr(void)
140{
141 if (ap_using_interrupts())
142 return ap_airq.lsi_ptr;
143 return NULL;
144}
145
146
147
148
149
150
151static int ap_interrupts_available(void)
152{
153 return test_facility(65);
154}
155
156
157
158
159
160
161
162static int ap_configuration_available(void)
163{
164 return test_facility(12);
165}
166
167
168
169
170
171
172
173static int ap_apft_available(void)
174{
175 return test_facility(15);
176}
177
178
179
180
181
182
183static inline int ap_qact_available(void)
184{
185 if (ap_configuration)
186 return ap_configuration->qact;
187 return 0;
188}
189
190
191
192
193
194
195
196
197
198static inline int ap_query_configuration(struct ap_config_info *info)
199{
200 if (!ap_configuration_available())
201 return -EOPNOTSUPP;
202 if (!info)
203 return -EINVAL;
204 return ap_qci(info);
205}
206
207
208
209
210static void ap_init_configuration(void)
211{
212 if (!ap_configuration_available())
213 return;
214
215 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
216 if (!ap_configuration)
217 return;
218 if (ap_query_configuration(ap_configuration) != 0) {
219 kfree(ap_configuration);
220 ap_configuration = NULL;
221 return;
222 }
223}
224
225
226
227
228
229static inline int ap_test_config(unsigned int *field, unsigned int nr)
230{
231 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
232}
233
234
235
236
237
238
239
240
241
242static inline int ap_test_config_card_id(unsigned int id)
243{
244 if (!ap_configuration)
245
246 return id < 0x40 ? 1 : 0;
247 return ap_test_config(ap_configuration->apm, id);
248}
249
250
251
252
253
254
255
256
257
258
259int ap_test_config_usage_domain(unsigned int domain)
260{
261 if (!ap_configuration)
262 return domain < 16;
263 return ap_test_config(ap_configuration->aqm, domain);
264}
265EXPORT_SYMBOL(ap_test_config_usage_domain);
266
267
268
269
270
271
272
273
274
275int ap_test_config_ctrl_domain(unsigned int domain)
276{
277 if (!ap_configuration)
278 return 0;
279 return ap_test_config(ap_configuration->adm, domain);
280}
281EXPORT_SYMBOL(ap_test_config_ctrl_domain);
282
283
284
285
286
287
288
289
290static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
291 unsigned int *facilities)
292{
293 struct ap_queue_status status;
294 unsigned long info;
295 int nd;
296
297 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
298 return -ENODEV;
299
300 status = ap_test_queue(qid, ap_apft_available(), &info);
301 switch (status.response_code) {
302 case AP_RESPONSE_NORMAL:
303 *queue_depth = (int)(info & 0xff);
304 *device_type = (int)((info >> 24) & 0xff);
305 *facilities = (unsigned int)(info >> 32);
306
307 nd = (info >> 16) & 0xff;
308
309 if ((info & (1UL << 57)) && nd > 0)
310 ap_max_domain_id = nd;
311 else
312 ap_max_domain_id = 15;
313 switch (*device_type) {
314
315
316
317
318
319 case AP_DEVICE_TYPE_CEX2A:
320 case AP_DEVICE_TYPE_CEX3A:
321 *facilities |= 0x08000000;
322 break;
323 case AP_DEVICE_TYPE_CEX2C:
324 case AP_DEVICE_TYPE_CEX3C:
325 *facilities |= 0x10000000;
326 break;
327 default:
328 break;
329 }
330 return 0;
331 case AP_RESPONSE_Q_NOT_AVAIL:
332 case AP_RESPONSE_DECONFIGURED:
333 case AP_RESPONSE_CHECKSTOPPED:
334 case AP_RESPONSE_INVALID_ADDRESS:
335 return -ENODEV;
336 case AP_RESPONSE_RESET_IN_PROGRESS:
337 case AP_RESPONSE_OTHERWISE_CHANGED:
338 case AP_RESPONSE_BUSY:
339 return -EBUSY;
340 default:
341 BUG();
342 }
343}
344
345void ap_wait(enum ap_wait wait)
346{
347 ktime_t hr_time;
348
349 switch (wait) {
350 case AP_WAIT_AGAIN:
351 case AP_WAIT_INTERRUPT:
352 if (ap_using_interrupts())
353 break;
354 if (ap_poll_kthread) {
355 wake_up(&ap_poll_wait);
356 break;
357 }
358 fallthrough;
359 case AP_WAIT_TIMEOUT:
360 spin_lock_bh(&ap_poll_timer_lock);
361 if (!hrtimer_is_queued(&ap_poll_timer)) {
362 hr_time = poll_timeout;
363 hrtimer_forward_now(&ap_poll_timer, hr_time);
364 hrtimer_restart(&ap_poll_timer);
365 }
366 spin_unlock_bh(&ap_poll_timer_lock);
367 break;
368 case AP_WAIT_NONE:
369 default:
370 break;
371 }
372}
373
374
375
376
377
378
379
380void ap_request_timeout(struct timer_list *t)
381{
382 struct ap_queue *aq = from_timer(aq, t, timeout);
383
384 spin_lock_bh(&aq->lock);
385 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
386 spin_unlock_bh(&aq->lock);
387}
388
389
390
391
392
393
394
395static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
396{
397 tasklet_schedule(&ap_tasklet);
398 return HRTIMER_NORESTART;
399}
400
401
402
403
404
405static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
406{
407 inc_irq_stat(IRQIO_APB);
408 tasklet_schedule(&ap_tasklet);
409}
410
411
412
413
414
415
416
417static void ap_tasklet_fn(unsigned long dummy)
418{
419 int bkt;
420 struct ap_queue *aq;
421 enum ap_wait wait = AP_WAIT_NONE;
422
423
424
425
426
427 if (ap_using_interrupts())
428 xchg(ap_airq.lsi_ptr, 0);
429
430 spin_lock_bh(&ap_queues_lock);
431 hash_for_each(ap_queues, bkt, aq, hnode) {
432 spin_lock_bh(&aq->lock);
433 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
434 spin_unlock_bh(&aq->lock);
435 }
436 spin_unlock_bh(&ap_queues_lock);
437
438 ap_wait(wait);
439}
440
441static int ap_pending_requests(void)
442{
443 int bkt;
444 struct ap_queue *aq;
445
446 spin_lock_bh(&ap_queues_lock);
447 hash_for_each(ap_queues, bkt, aq, hnode) {
448 if (aq->queue_count == 0)
449 continue;
450 spin_unlock_bh(&ap_queues_lock);
451 return 1;
452 }
453 spin_unlock_bh(&ap_queues_lock);
454 return 0;
455}
456
457
458
459
460
461
462
463
464
465
466
467static int ap_poll_thread(void *data)
468{
469 DECLARE_WAITQUEUE(wait, current);
470
471 set_user_nice(current, MAX_NICE);
472 set_freezable();
473 while (!kthread_should_stop()) {
474 add_wait_queue(&ap_poll_wait, &wait);
475 set_current_state(TASK_INTERRUPTIBLE);
476 if (!ap_pending_requests()) {
477 schedule();
478 try_to_freeze();
479 }
480 set_current_state(TASK_RUNNING);
481 remove_wait_queue(&ap_poll_wait, &wait);
482 if (need_resched()) {
483 schedule();
484 try_to_freeze();
485 continue;
486 }
487 ap_tasklet_fn(0);
488 }
489
490 return 0;
491}
492
493static int ap_poll_thread_start(void)
494{
495 int rc;
496
497 if (ap_using_interrupts() || ap_poll_kthread)
498 return 0;
499 mutex_lock(&ap_poll_thread_mutex);
500 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
501 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
502 if (rc)
503 ap_poll_kthread = NULL;
504 mutex_unlock(&ap_poll_thread_mutex);
505 return rc;
506}
507
508static void ap_poll_thread_stop(void)
509{
510 if (!ap_poll_kthread)
511 return;
512 mutex_lock(&ap_poll_thread_mutex);
513 kthread_stop(ap_poll_kthread);
514 ap_poll_kthread = NULL;
515 mutex_unlock(&ap_poll_thread_mutex);
516}
517
518#define is_card_dev(x) ((x)->parent == ap_root_device)
519#define is_queue_dev(x) ((x)->parent != ap_root_device)
520
521
522
523
524
525
526
527
528static int ap_bus_match(struct device *dev, struct device_driver *drv)
529{
530 struct ap_driver *ap_drv = to_ap_drv(drv);
531 struct ap_device_id *id;
532
533
534
535
536
537 for (id = ap_drv->ids; id->match_flags; id++) {
538 if (is_card_dev(dev) &&
539 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
540 id->dev_type == to_ap_dev(dev)->device_type)
541 return 1;
542 if (is_queue_dev(dev) &&
543 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
544 id->dev_type == to_ap_dev(dev)->device_type)
545 return 1;
546 }
547 return 0;
548}
549
550
551
552
553
554
555
556
557
558static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
559{
560 struct ap_device *ap_dev = to_ap_dev(dev);
561 int retval = 0;
562
563 if (!ap_dev)
564 return -ENODEV;
565
566
567 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
568 if (retval)
569 return retval;
570
571
572 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
573
574 return retval;
575}
576
577static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
578{
579 if (is_queue_dev(dev) &&
580 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
581 device_unregister(dev);
582 return 0;
583}
584
585static struct bus_type ap_bus_type = {
586 .name = "ap",
587 .match = &ap_bus_match,
588 .uevent = &ap_uevent,
589};
590
591static int __ap_revise_reserved(struct device *dev, void *dummy)
592{
593 int rc, card, queue, devres, drvres;
594
595 if (is_queue_dev(dev)) {
596 card = AP_QID_CARD(to_ap_queue(dev)->qid);
597 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
598 mutex_lock(&ap_perms_mutex);
599 devres = test_bit_inv(card, ap_perms.apm)
600 && test_bit_inv(queue, ap_perms.aqm);
601 mutex_unlock(&ap_perms_mutex);
602 drvres = to_ap_drv(dev->driver)->flags
603 & AP_DRIVER_FLAG_DEFAULT;
604 if (!!devres != !!drvres) {
605 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
606 card, queue);
607 rc = device_reprobe(dev);
608 }
609 }
610
611 return 0;
612}
613
614static void ap_bus_revise_bindings(void)
615{
616 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
617}
618
619int ap_owned_by_def_drv(int card, int queue)
620{
621 int rc = 0;
622
623 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
624 return -EINVAL;
625
626 mutex_lock(&ap_perms_mutex);
627
628 if (test_bit_inv(card, ap_perms.apm)
629 && test_bit_inv(queue, ap_perms.aqm))
630 rc = 1;
631
632 mutex_unlock(&ap_perms_mutex);
633
634 return rc;
635}
636EXPORT_SYMBOL(ap_owned_by_def_drv);
637
638int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
639 unsigned long *aqm)
640{
641 int card, queue, rc = 0;
642
643 mutex_lock(&ap_perms_mutex);
644
645 for (card = 0; !rc && card < AP_DEVICES; card++)
646 if (test_bit_inv(card, apm) &&
647 test_bit_inv(card, ap_perms.apm))
648 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
649 if (test_bit_inv(queue, aqm) &&
650 test_bit_inv(queue, ap_perms.aqm))
651 rc = 1;
652
653 mutex_unlock(&ap_perms_mutex);
654
655 return rc;
656}
657EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
658
659static int ap_device_probe(struct device *dev)
660{
661 struct ap_device *ap_dev = to_ap_dev(dev);
662 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
663 int card, queue, devres, drvres, rc;
664
665 if (is_queue_dev(dev)) {
666
667
668
669
670
671
672 card = AP_QID_CARD(to_ap_queue(dev)->qid);
673 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
674 mutex_lock(&ap_perms_mutex);
675 devres = test_bit_inv(card, ap_perms.apm)
676 && test_bit_inv(queue, ap_perms.aqm);
677 mutex_unlock(&ap_perms_mutex);
678 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
679 if (!!devres != !!drvres)
680 return -ENODEV;
681 }
682
683
684 spin_lock_bh(&ap_queues_lock);
685 if (is_queue_dev(dev))
686 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
687 to_ap_queue(dev)->qid);
688 spin_unlock_bh(&ap_queues_lock);
689
690 ap_dev->drv = ap_drv;
691 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
692
693 if (rc) {
694 spin_lock_bh(&ap_queues_lock);
695 if (is_queue_dev(dev))
696 hash_del(&to_ap_queue(dev)->hnode);
697 spin_unlock_bh(&ap_queues_lock);
698 ap_dev->drv = NULL;
699 }
700
701 return rc;
702}
703
704static int ap_device_remove(struct device *dev)
705{
706 struct ap_device *ap_dev = to_ap_dev(dev);
707 struct ap_driver *ap_drv = ap_dev->drv;
708
709
710 if (is_queue_dev(dev))
711 ap_queue_prepare_remove(to_ap_queue(dev));
712
713
714 if (ap_drv->remove)
715 ap_drv->remove(ap_dev);
716
717
718 if (is_queue_dev(dev))
719 ap_queue_remove(to_ap_queue(dev));
720
721
722 spin_lock_bh(&ap_queues_lock);
723 if (is_queue_dev(dev))
724 hash_del(&to_ap_queue(dev)->hnode);
725 spin_unlock_bh(&ap_queues_lock);
726
727 return 0;
728}
729
730struct ap_queue *ap_get_qdev(ap_qid_t qid)
731{
732 int bkt;
733 struct ap_queue *aq;
734
735 spin_lock_bh(&ap_queues_lock);
736 hash_for_each(ap_queues, bkt, aq, hnode) {
737 if (aq->qid == qid) {
738 get_device(&aq->ap_dev.device);
739 spin_unlock_bh(&ap_queues_lock);
740 return aq;
741 }
742 }
743 spin_unlock_bh(&ap_queues_lock);
744
745 return NULL;
746}
747EXPORT_SYMBOL(ap_get_qdev);
748
749int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
750 char *name)
751{
752 struct device_driver *drv = &ap_drv->driver;
753
754 if (!initialised)
755 return -ENODEV;
756
757 drv->bus = &ap_bus_type;
758 drv->probe = ap_device_probe;
759 drv->remove = ap_device_remove;
760 drv->owner = owner;
761 drv->name = name;
762 return driver_register(drv);
763}
764EXPORT_SYMBOL(ap_driver_register);
765
766void ap_driver_unregister(struct ap_driver *ap_drv)
767{
768 driver_unregister(&ap_drv->driver);
769}
770EXPORT_SYMBOL(ap_driver_unregister);
771
772void ap_bus_force_rescan(void)
773{
774
775 del_timer(&ap_config_timer);
776 queue_work(system_long_wq, &ap_scan_work);
777 flush_work(&ap_scan_work);
778}
779EXPORT_SYMBOL(ap_bus_force_rescan);
780
781
782
783
784void ap_bus_cfg_chg(void)
785{
786 AP_DBF(DBF_INFO, "%s config change, forcing bus rescan\n", __func__);
787
788 ap_bus_force_rescan();
789}
790
791
792
793
794
795
796
797
798
799static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
800{
801 int i, n, b;
802
803
804 if (bits & 0x07)
805 return -EINVAL;
806
807 if (str[0] == '0' && str[1] == 'x')
808 str++;
809 if (*str == 'x')
810 str++;
811
812 for (i = 0; isxdigit(*str) && i < bits; str++) {
813 b = hex_to_bin(*str);
814 for (n = 0; n < 4; n++)
815 if (b & (0x08 >> n))
816 set_bit_inv(i + n, bitmap);
817 i += 4;
818 }
819
820 if (*str == '\n')
821 str++;
822 if (*str)
823 return -EINVAL;
824 return 0;
825}
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
845{
846 int a, i, z;
847 char *np, sign;
848
849
850 if (bits & 0x07)
851 return -EINVAL;
852
853 while (*str) {
854 sign = *str++;
855 if (sign != '+' && sign != '-')
856 return -EINVAL;
857 a = z = simple_strtoul(str, &np, 0);
858 if (str == np || a >= bits)
859 return -EINVAL;
860 str = np;
861 if (*str == '-') {
862 z = simple_strtoul(++str, &np, 0);
863 if (str == np || a > z || z >= bits)
864 return -EINVAL;
865 str = np;
866 }
867 for (i = a; i <= z; i++)
868 if (sign == '+')
869 set_bit_inv(i, bitmap);
870 else
871 clear_bit_inv(i, bitmap);
872 while (*str == ',' || *str == '\n')
873 str++;
874 }
875
876 return 0;
877}
878
879int ap_parse_mask_str(const char *str,
880 unsigned long *bitmap, int bits,
881 struct mutex *lock)
882{
883 unsigned long *newmap, size;
884 int rc;
885
886
887 if (bits & 0x07)
888 return -EINVAL;
889
890 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
891 newmap = kmalloc(size, GFP_KERNEL);
892 if (!newmap)
893 return -ENOMEM;
894 if (mutex_lock_interruptible(lock)) {
895 kfree(newmap);
896 return -ERESTARTSYS;
897 }
898
899 if (*str == '+' || *str == '-') {
900 memcpy(newmap, bitmap, size);
901 rc = modify_bitmap(str, newmap, bits);
902 } else {
903 memset(newmap, 0, size);
904 rc = hex2bitmap(str, newmap, bits);
905 }
906 if (rc == 0)
907 memcpy(bitmap, newmap, size);
908 mutex_unlock(lock);
909 kfree(newmap);
910 return rc;
911}
912EXPORT_SYMBOL(ap_parse_mask_str);
913
914
915
916
917
918static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
919{
920 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
921}
922
923static ssize_t ap_domain_store(struct bus_type *bus,
924 const char *buf, size_t count)
925{
926 int domain;
927
928 if (sscanf(buf, "%i\n", &domain) != 1 ||
929 domain < 0 || domain > ap_max_domain_id ||
930 !test_bit_inv(domain, ap_perms.aqm))
931 return -EINVAL;
932 spin_lock_bh(&ap_domain_lock);
933 ap_domain_index = domain;
934 spin_unlock_bh(&ap_domain_lock);
935
936 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
937
938 return count;
939}
940
941static BUS_ATTR_RW(ap_domain);
942
943static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
944{
945 if (!ap_configuration)
946 return scnprintf(buf, PAGE_SIZE, "not supported\n");
947
948 return scnprintf(buf, PAGE_SIZE,
949 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
950 ap_configuration->adm[0], ap_configuration->adm[1],
951 ap_configuration->adm[2], ap_configuration->adm[3],
952 ap_configuration->adm[4], ap_configuration->adm[5],
953 ap_configuration->adm[6], ap_configuration->adm[7]);
954}
955
956static BUS_ATTR_RO(ap_control_domain_mask);
957
958static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
959{
960 if (!ap_configuration)
961 return scnprintf(buf, PAGE_SIZE, "not supported\n");
962
963 return scnprintf(buf, PAGE_SIZE,
964 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
965 ap_configuration->aqm[0], ap_configuration->aqm[1],
966 ap_configuration->aqm[2], ap_configuration->aqm[3],
967 ap_configuration->aqm[4], ap_configuration->aqm[5],
968 ap_configuration->aqm[6], ap_configuration->aqm[7]);
969}
970
971static BUS_ATTR_RO(ap_usage_domain_mask);
972
973static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
974{
975 if (!ap_configuration)
976 return scnprintf(buf, PAGE_SIZE, "not supported\n");
977
978 return scnprintf(buf, PAGE_SIZE,
979 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
980 ap_configuration->apm[0], ap_configuration->apm[1],
981 ap_configuration->apm[2], ap_configuration->apm[3],
982 ap_configuration->apm[4], ap_configuration->apm[5],
983 ap_configuration->apm[6], ap_configuration->apm[7]);
984}
985
986static BUS_ATTR_RO(ap_adapter_mask);
987
988static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
989{
990 return scnprintf(buf, PAGE_SIZE, "%d\n",
991 ap_using_interrupts() ? 1 : 0);
992}
993
994static BUS_ATTR_RO(ap_interrupts);
995
996static ssize_t config_time_show(struct bus_type *bus, char *buf)
997{
998 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
999}
1000
1001static ssize_t config_time_store(struct bus_type *bus,
1002 const char *buf, size_t count)
1003{
1004 int time;
1005
1006 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1007 return -EINVAL;
1008 ap_config_time = time;
1009 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1010 return count;
1011}
1012
1013static BUS_ATTR_RW(config_time);
1014
1015static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1016{
1017 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1018}
1019
1020static ssize_t poll_thread_store(struct bus_type *bus,
1021 const char *buf, size_t count)
1022{
1023 int flag, rc;
1024
1025 if (sscanf(buf, "%d\n", &flag) != 1)
1026 return -EINVAL;
1027 if (flag) {
1028 rc = ap_poll_thread_start();
1029 if (rc)
1030 count = rc;
1031 } else
1032 ap_poll_thread_stop();
1033 return count;
1034}
1035
1036static BUS_ATTR_RW(poll_thread);
1037
1038static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1039{
1040 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1041}
1042
1043static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1044 size_t count)
1045{
1046 unsigned long long time;
1047 ktime_t hr_time;
1048
1049
1050 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1051 time > 120000000000ULL)
1052 return -EINVAL;
1053 poll_timeout = time;
1054 hr_time = poll_timeout;
1055
1056 spin_lock_bh(&ap_poll_timer_lock);
1057 hrtimer_cancel(&ap_poll_timer);
1058 hrtimer_set_expires(&ap_poll_timer, hr_time);
1059 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1060 spin_unlock_bh(&ap_poll_timer_lock);
1061
1062 return count;
1063}
1064
1065static BUS_ATTR_RW(poll_timeout);
1066
1067static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1068{
1069 int max_domain_id;
1070
1071 if (ap_configuration)
1072 max_domain_id = ap_max_domain_id ? : -1;
1073 else
1074 max_domain_id = 15;
1075 return scnprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1076}
1077
1078static BUS_ATTR_RO(ap_max_domain_id);
1079
1080static ssize_t apmask_show(struct bus_type *bus, char *buf)
1081{
1082 int rc;
1083
1084 if (mutex_lock_interruptible(&ap_perms_mutex))
1085 return -ERESTARTSYS;
1086 rc = scnprintf(buf, PAGE_SIZE,
1087 "0x%016lx%016lx%016lx%016lx\n",
1088 ap_perms.apm[0], ap_perms.apm[1],
1089 ap_perms.apm[2], ap_perms.apm[3]);
1090 mutex_unlock(&ap_perms_mutex);
1091
1092 return rc;
1093}
1094
1095static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1096 size_t count)
1097{
1098 int rc;
1099
1100 rc = ap_parse_mask_str(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1101 if (rc)
1102 return rc;
1103
1104 ap_bus_revise_bindings();
1105
1106 return count;
1107}
1108
1109static BUS_ATTR_RW(apmask);
1110
1111static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1112{
1113 int rc;
1114
1115 if (mutex_lock_interruptible(&ap_perms_mutex))
1116 return -ERESTARTSYS;
1117 rc = scnprintf(buf, PAGE_SIZE,
1118 "0x%016lx%016lx%016lx%016lx\n",
1119 ap_perms.aqm[0], ap_perms.aqm[1],
1120 ap_perms.aqm[2], ap_perms.aqm[3]);
1121 mutex_unlock(&ap_perms_mutex);
1122
1123 return rc;
1124}
1125
1126static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1127 size_t count)
1128{
1129 int rc;
1130
1131 rc = ap_parse_mask_str(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1132 if (rc)
1133 return rc;
1134
1135 ap_bus_revise_bindings();
1136
1137 return count;
1138}
1139
1140static BUS_ATTR_RW(aqmask);
1141
1142static struct bus_attribute *const ap_bus_attrs[] = {
1143 &bus_attr_ap_domain,
1144 &bus_attr_ap_control_domain_mask,
1145 &bus_attr_ap_usage_domain_mask,
1146 &bus_attr_ap_adapter_mask,
1147 &bus_attr_config_time,
1148 &bus_attr_poll_thread,
1149 &bus_attr_ap_interrupts,
1150 &bus_attr_poll_timeout,
1151 &bus_attr_ap_max_domain_id,
1152 &bus_attr_apmask,
1153 &bus_attr_aqmask,
1154 NULL,
1155};
1156
1157
1158
1159
1160
1161static void ap_select_domain(void)
1162{
1163 int count, max_count, best_domain;
1164 struct ap_queue_status status;
1165 int i, j;
1166
1167
1168
1169
1170
1171
1172 spin_lock_bh(&ap_domain_lock);
1173 if (ap_domain_index >= 0) {
1174
1175 spin_unlock_bh(&ap_domain_lock);
1176 return;
1177 }
1178 best_domain = -1;
1179 max_count = 0;
1180 for (i = 0; i < AP_DOMAINS; i++) {
1181 if (!ap_test_config_usage_domain(i) ||
1182 !test_bit_inv(i, ap_perms.aqm))
1183 continue;
1184 count = 0;
1185 for (j = 0; j < AP_DEVICES; j++) {
1186 if (!ap_test_config_card_id(j))
1187 continue;
1188 status = ap_test_queue(AP_MKQID(j, i),
1189 ap_apft_available(),
1190 NULL);
1191 if (status.response_code != AP_RESPONSE_NORMAL)
1192 continue;
1193 count++;
1194 }
1195 if (count > max_count) {
1196 max_count = count;
1197 best_domain = i;
1198 }
1199 }
1200 if (best_domain >= 0) {
1201 ap_domain_index = best_domain;
1202 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1203 }
1204 spin_unlock_bh(&ap_domain_lock);
1205}
1206
1207
1208
1209
1210
1211
1212static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1213{
1214 int comp_type = 0;
1215
1216
1217 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1218 return 0;
1219
1220 if (rawtype <= AP_DEVICE_TYPE_CEX7)
1221 return rawtype;
1222
1223
1224
1225
1226
1227 if (ap_qact_available()) {
1228 struct ap_queue_status status;
1229 union ap_qact_ap_info apinfo = {0};
1230
1231 apinfo.mode = (func >> 26) & 0x07;
1232 apinfo.cat = AP_DEVICE_TYPE_CEX7;
1233 status = ap_qact(qid, 0, &apinfo);
1234 if (status.response_code == AP_RESPONSE_NORMAL
1235 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1236 && apinfo.cat <= AP_DEVICE_TYPE_CEX7)
1237 comp_type = apinfo.cat;
1238 }
1239 if (!comp_type)
1240 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1241 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1242 else if (comp_type != rawtype)
1243 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1244 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1245 return comp_type;
1246}
1247
1248
1249
1250
1251
1252static int __match_card_device_with_id(struct device *dev, const void *data)
1253{
1254 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1255}
1256
1257
1258
1259
1260
1261static int __match_queue_device_with_qid(struct device *dev, const void *data)
1262{
1263 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1264}
1265
1266
1267
1268
1269
1270static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1271{
1272 return is_queue_dev(dev)
1273 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1274}
1275
1276
1277
1278
1279
1280static void _ap_scan_bus_adapter(int id)
1281{
1282 ap_qid_t qid;
1283 unsigned int func;
1284 struct ap_card *ac;
1285 struct device *dev;
1286 struct ap_queue *aq;
1287 int rc, dom, depth, type, comp_type, borked;
1288
1289
1290 dev = bus_find_device(&ap_bus_type, NULL,
1291 (void *)(long) id,
1292 __match_card_device_with_id);
1293 ac = dev ? to_ap_card(dev) : NULL;
1294 if (!ap_test_config_card_id(id)) {
1295 if (dev) {
1296
1297 bus_for_each_dev(&ap_bus_type, NULL,
1298 (void *)(long) id,
1299 __ap_queue_devices_with_id_unregister);
1300 device_unregister(dev);
1301 put_device(dev);
1302 }
1303 return;
1304 }
1305
1306
1307
1308
1309
1310
1311 if (ac) {
1312
1313 for (dom = 0; dom < AP_DOMAINS; dom++) {
1314 qid = AP_MKQID(id, dom);
1315 if (ap_query_queue(qid, &depth, &type, &func) == 0)
1316 break;
1317 }
1318 borked = 0;
1319 if (dom >= AP_DOMAINS) {
1320
1321 borked = 1;
1322 } else if (ac->raw_hwtype != type) {
1323
1324 AP_DBF(DBF_INFO, "card=%02x type changed.\n", id);
1325 borked = 1;
1326 } else if (ac->functions != func) {
1327
1328 AP_DBF(DBF_INFO, "card=%02x functions changed.\n", id);
1329 borked = 1;
1330 }
1331 if (borked) {
1332
1333 bus_for_each_dev(&ap_bus_type, NULL,
1334 (void *)(long) id,
1335 __ap_queue_devices_with_id_unregister);
1336 device_unregister(dev);
1337 put_device(dev);
1338
1339 if (dom >= AP_DOMAINS)
1340 return;
1341 ac = NULL;
1342 }
1343 }
1344
1345
1346
1347
1348
1349
1350 for (dom = 0; dom < AP_DOMAINS; dom++) {
1351 qid = AP_MKQID(id, dom);
1352 dev = bus_find_device(&ap_bus_type, NULL,
1353 (void *)(long) qid,
1354 __match_queue_device_with_qid);
1355 aq = dev ? to_ap_queue(dev) : NULL;
1356 if (!ap_test_config_usage_domain(dom)) {
1357 if (dev) {
1358
1359
1360
1361 device_unregister(dev);
1362 put_device(dev);
1363 }
1364 continue;
1365 }
1366
1367 rc = ap_query_queue(qid, &depth, &type, &func);
1368 if (dev) {
1369 if (rc == -ENODEV)
1370 borked = 1;
1371 else {
1372 spin_lock_bh(&aq->lock);
1373 borked = aq->state == AP_STATE_BORKED;
1374 spin_unlock_bh(&aq->lock);
1375 }
1376 if (borked) {
1377
1378 AP_DBF(DBF_DEBUG,
1379 "removing broken queue=%02x.%04x\n",
1380 id, dom);
1381 device_unregister(dev);
1382 }
1383 put_device(dev);
1384 continue;
1385 }
1386 if (rc)
1387 continue;
1388
1389 comp_type = ap_get_compatible_type(qid, type, func);
1390 if (!comp_type)
1391 continue;
1392
1393 if (!ac) {
1394 ac = ap_card_create(id, depth, type, comp_type, func);
1395 if (!ac)
1396 continue;
1397 ac->ap_dev.device.bus = &ap_bus_type;
1398 ac->ap_dev.device.parent = ap_root_device;
1399 dev_set_name(&ac->ap_dev.device, "card%02x", id);
1400
1401 rc = device_register(&ac->ap_dev.device);
1402 if (rc) {
1403 put_device(&ac->ap_dev.device);
1404 ac = NULL;
1405 break;
1406 }
1407
1408 get_device(&ac->ap_dev.device);
1409 }
1410
1411 aq = ap_queue_create(qid, comp_type);
1412 if (!aq)
1413 continue;
1414 aq->card = ac;
1415 aq->ap_dev.device.bus = &ap_bus_type;
1416 aq->ap_dev.device.parent = &ac->ap_dev.device;
1417 dev_set_name(&aq->ap_dev.device, "%02x.%04x", id, dom);
1418
1419 rc = device_register(&aq->ap_dev.device);
1420 if (rc) {
1421 put_device(&aq->ap_dev.device);
1422 continue;
1423 }
1424 }
1425
1426 if (ac)
1427 put_device(&ac->ap_dev.device);
1428}
1429
1430
1431
1432
1433
1434static void ap_scan_bus(struct work_struct *unused)
1435{
1436 int id;
1437
1438 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1439
1440 ap_query_configuration(ap_configuration);
1441 ap_select_domain();
1442
1443
1444 for (id = 0; id < AP_DEVICES; id++)
1445 _ap_scan_bus_adapter(id);
1446
1447
1448 if (ap_domain_index >= 0) {
1449 struct device *dev =
1450 bus_find_device(&ap_bus_type, NULL,
1451 (void *)(long) ap_domain_index,
1452 __match_queue_device_with_queue_id);
1453 if (dev)
1454 put_device(dev);
1455 else
1456 AP_DBF(DBF_INFO,
1457 "no queue device with default domain %d available\n",
1458 ap_domain_index);
1459 }
1460
1461 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1462}
1463
1464static void ap_config_timeout(struct timer_list *unused)
1465{
1466 queue_work(system_long_wq, &ap_scan_work);
1467}
1468
1469static int __init ap_debug_init(void)
1470{
1471 ap_dbf_info = debug_register("ap", 1, 1,
1472 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1473 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1474 debug_set_level(ap_dbf_info, DBF_ERR);
1475
1476 return 0;
1477}
1478
1479static void __init ap_perms_init(void)
1480{
1481
1482 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
1483 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1484 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1485
1486
1487 if (apm_str) {
1488 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1489 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
1490 &ap_perms_mutex);
1491 }
1492
1493
1494 if (aqm_str) {
1495 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1496 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
1497 &ap_perms_mutex);
1498 }
1499}
1500
1501
1502
1503
1504
1505
1506static int __init ap_module_init(void)
1507{
1508 int max_domain_id;
1509 int rc, i;
1510
1511 rc = ap_debug_init();
1512 if (rc)
1513 return rc;
1514
1515 if (!ap_instructions_available()) {
1516 pr_warn("The hardware system does not support AP instructions\n");
1517 return -ENODEV;
1518 }
1519
1520
1521 hash_init(ap_queues);
1522
1523
1524 ap_perms_init();
1525
1526
1527 ap_init_configuration();
1528
1529 if (ap_configuration)
1530 max_domain_id =
1531 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1532 else
1533 max_domain_id = 15;
1534 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1535 (ap_domain_index >= 0 &&
1536 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1537 pr_warn("%d is not a valid cryptographic domain\n",
1538 ap_domain_index);
1539 ap_domain_index = -1;
1540 }
1541
1542 if (ap_interrupts_available()) {
1543 rc = register_adapter_interrupt(&ap_airq);
1544 ap_airq_flag = (rc == 0);
1545 }
1546
1547
1548 rc = bus_register(&ap_bus_type);
1549 if (rc)
1550 goto out;
1551 for (i = 0; ap_bus_attrs[i]; i++) {
1552 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1553 if (rc)
1554 goto out_bus;
1555 }
1556
1557
1558 ap_root_device = root_device_register("ap");
1559 rc = PTR_ERR_OR_ZERO(ap_root_device);
1560 if (rc)
1561 goto out_bus;
1562
1563
1564 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1565
1566
1567
1568
1569
1570 if (MACHINE_IS_VM)
1571 poll_timeout = 1500000;
1572 spin_lock_init(&ap_poll_timer_lock);
1573 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1574 ap_poll_timer.function = ap_poll_timeout;
1575
1576
1577 if (ap_thread_flag) {
1578 rc = ap_poll_thread_start();
1579 if (rc)
1580 goto out_work;
1581 }
1582
1583 queue_work(system_long_wq, &ap_scan_work);
1584 initialised = true;
1585
1586 return 0;
1587
1588out_work:
1589 hrtimer_cancel(&ap_poll_timer);
1590 root_device_unregister(ap_root_device);
1591out_bus:
1592 while (i--)
1593 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1594 bus_unregister(&ap_bus_type);
1595out:
1596 if (ap_using_interrupts())
1597 unregister_adapter_interrupt(&ap_airq);
1598 kfree(ap_configuration);
1599 return rc;
1600}
1601device_initcall(ap_module_init);
1602