1
2
3
4
5
6
7
8
9
10
11
12
13
14#define KMSG_COMPONENT "ap"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
17#include <linux/kernel_stat.h>
18#include <linux/moduleparam.h>
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/err.h>
22#include <linux/freezer.h>
23#include <linux/interrupt.h>
24#include <linux/workqueue.h>
25#include <linux/slab.h>
26#include <linux/notifier.h>
27#include <linux/kthread.h>
28#include <linux/mutex.h>
29#include <asm/airq.h>
30#include <linux/atomic.h>
31#include <asm/isc.h>
32#include <linux/hrtimer.h>
33#include <linux/ktime.h>
34#include <asm/facility.h>
35#include <linux/crypto.h>
36#include <linux/mod_devicetable.h>
37#include <linux/debugfs.h>
38#include <linux/ctype.h>
39#include <linux/module.h>
40
41#include "ap_bus.h"
42#include "ap_debug.h"
43
44
45
46
47int ap_domain_index = -1;
48static DEFINE_SPINLOCK(ap_domain_lock);
49module_param_named(domain, ap_domain_index, int, 0440);
50MODULE_PARM_DESC(domain, "domain index for ap devices");
51EXPORT_SYMBOL(ap_domain_index);
52
53static int ap_thread_flag;
54module_param_named(poll_thread, ap_thread_flag, int, 0440);
55MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
56
57static char *apm_str;
58module_param_named(apmask, apm_str, charp, 0440);
59MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
60
61static char *aqm_str;
62module_param_named(aqmask, aqm_str, charp, 0440);
63MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
64
65static int ap_useirq = 1;
66module_param_named(useirq, ap_useirq, int, 0440);
67MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
68
69atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
70EXPORT_SYMBOL(ap_max_msg_size);
71
72static struct device *ap_root_device;
73
74
75DEFINE_HASHTABLE(ap_queues, 8);
76
77DEFINE_SPINLOCK(ap_queues_lock);
78
79
80struct ap_perms ap_perms;
81EXPORT_SYMBOL(ap_perms);
82DEFINE_MUTEX(ap_perms_mutex);
83EXPORT_SYMBOL(ap_perms_mutex);
84
85
86static atomic64_t ap_scan_bus_count;
87
88
89static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
90
91
92static DECLARE_COMPLETION(ap_init_apqn_bindings_complete);
93
94static struct ap_config_info *ap_qci_info;
95static struct ap_config_info *ap_qci_info_old;
96
97
98
99
100debug_info_t *ap_dbf_info;
101
102
103
104
105static struct timer_list ap_config_timer;
106static int ap_config_time = AP_CONFIG_TIME;
107static void ap_scan_bus(struct work_struct *);
108static DECLARE_WORK(ap_scan_work, ap_scan_bus);
109
110
111
112
113static void ap_tasklet_fn(unsigned long);
114static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
115static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
116static struct task_struct *ap_poll_kthread;
117static DEFINE_MUTEX(ap_poll_thread_mutex);
118static DEFINE_SPINLOCK(ap_poll_timer_lock);
119static struct hrtimer ap_poll_timer;
120
121
122
123
124static unsigned long long poll_timeout = 250000;
125
126
127static int ap_max_domain_id = 15;
128
129static int ap_max_adapter_id = 63;
130
131static struct bus_type ap_bus_type;
132
133
134static void ap_interrupt_handler(struct airq_struct *airq, bool floating);
135
136static bool ap_irq_flag;
137
138static struct airq_struct ap_airq = {
139 .handler = ap_interrupt_handler,
140 .isc = AP_ISC,
141};
142
143
144
145
146
147
148
149
150void *ap_airq_ptr(void)
151{
152 if (ap_irq_flag)
153 return ap_airq.lsi_ptr;
154 return NULL;
155}
156
157
158
159
160
161
162static int ap_interrupts_available(void)
163{
164 return test_facility(65);
165}
166
167
168
169
170
171
172
173static int ap_qci_available(void)
174{
175 return test_facility(12);
176}
177
178
179
180
181
182
183
184static int ap_apft_available(void)
185{
186 return test_facility(15);
187}
188
189
190
191
192
193
194static inline int ap_qact_available(void)
195{
196 if (ap_qci_info)
197 return ap_qci_info->qact;
198 return 0;
199}
200
201
202
203
204
205
206
207
208
209static inline int ap_fetch_qci_info(struct ap_config_info *info)
210{
211 if (!ap_qci_available())
212 return -EOPNOTSUPP;
213 if (!info)
214 return -EINVAL;
215 return ap_qci(info);
216}
217
218
219
220
221
222
223static void __init ap_init_qci_info(void)
224{
225 if (!ap_qci_available()) {
226 AP_DBF_INFO("%s QCI not supported\n", __func__);
227 return;
228 }
229
230 ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL);
231 if (!ap_qci_info)
232 return;
233 ap_qci_info_old = kzalloc(sizeof(*ap_qci_info_old), GFP_KERNEL);
234 if (!ap_qci_info_old)
235 return;
236 if (ap_fetch_qci_info(ap_qci_info) != 0) {
237 kfree(ap_qci_info);
238 kfree(ap_qci_info_old);
239 ap_qci_info = NULL;
240 ap_qci_info_old = NULL;
241 return;
242 }
243 AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
244
245 if (ap_qci_info->apxa) {
246 if (ap_qci_info->Na) {
247 ap_max_adapter_id = ap_qci_info->Na;
248 AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
249 __func__, ap_max_adapter_id);
250 }
251 if (ap_qci_info->Nd) {
252 ap_max_domain_id = ap_qci_info->Nd;
253 AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
254 __func__, ap_max_domain_id);
255 }
256 }
257
258 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
259}
260
261
262
263
264
265static inline int ap_test_config(unsigned int *field, unsigned int nr)
266{
267 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
268}
269
270
271
272
273
274
275
276
277static inline int ap_test_config_card_id(unsigned int id)
278{
279 if (id > ap_max_adapter_id)
280 return 0;
281 if (ap_qci_info)
282 return ap_test_config(ap_qci_info->apm, id);
283 return 1;
284}
285
286
287
288
289
290
291
292
293
294int ap_test_config_usage_domain(unsigned int domain)
295{
296 if (domain > ap_max_domain_id)
297 return 0;
298 if (ap_qci_info)
299 return ap_test_config(ap_qci_info->aqm, domain);
300 return 1;
301}
302EXPORT_SYMBOL(ap_test_config_usage_domain);
303
304
305
306
307
308
309
310
311
312int ap_test_config_ctrl_domain(unsigned int domain)
313{
314 if (!ap_qci_info || domain > ap_max_domain_id)
315 return 0;
316 return ap_test_config(ap_qci_info->adm, domain);
317}
318EXPORT_SYMBOL(ap_test_config_ctrl_domain);
319
320
321
322
323
324
325static bool ap_queue_info(ap_qid_t qid, int *q_type, unsigned int *q_fac,
326 int *q_depth, int *q_ml, bool *q_decfg, bool *q_cstop)
327{
328 struct ap_queue_status status;
329 union {
330 unsigned long value;
331 struct {
332 unsigned int fac : 32;
333 unsigned int at : 8;
334 unsigned int _res1 : 8;
335 unsigned int _res2 : 4;
336 unsigned int ml : 4;
337 unsigned int _res3 : 4;
338 unsigned int qd : 4;
339 } tapq_gr2;
340 } tapq_info;
341
342 tapq_info.value = 0;
343
344
345 if (AP_QID_CARD(qid) > ap_max_adapter_id ||
346 AP_QID_QUEUE(qid) > ap_max_domain_id)
347 return false;
348
349
350 status = ap_test_queue(qid, ap_apft_available(), &tapq_info.value);
351 switch (status.response_code) {
352 case AP_RESPONSE_NORMAL:
353 case AP_RESPONSE_RESET_IN_PROGRESS:
354 case AP_RESPONSE_DECONFIGURED:
355 case AP_RESPONSE_CHECKSTOPPED:
356 case AP_RESPONSE_BUSY:
357
358
359
360
361
362 if (WARN_ON_ONCE(!tapq_info.value))
363 return false;
364 *q_type = tapq_info.tapq_gr2.at;
365 *q_fac = tapq_info.tapq_gr2.fac;
366 *q_depth = tapq_info.tapq_gr2.qd;
367 *q_ml = tapq_info.tapq_gr2.ml;
368 *q_decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
369 *q_cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
370 switch (*q_type) {
371
372
373
374
375
376 case AP_DEVICE_TYPE_CEX2A:
377 case AP_DEVICE_TYPE_CEX3A:
378 *q_fac |= 0x08000000;
379 break;
380 case AP_DEVICE_TYPE_CEX2C:
381 case AP_DEVICE_TYPE_CEX3C:
382 *q_fac |= 0x10000000;
383 break;
384 default:
385 break;
386 }
387 return true;
388 default:
389
390
391
392 return false;
393 }
394}
395
396void ap_wait(enum ap_sm_wait wait)
397{
398 ktime_t hr_time;
399
400 switch (wait) {
401 case AP_SM_WAIT_AGAIN:
402 case AP_SM_WAIT_INTERRUPT:
403 if (ap_irq_flag)
404 break;
405 if (ap_poll_kthread) {
406 wake_up(&ap_poll_wait);
407 break;
408 }
409 fallthrough;
410 case AP_SM_WAIT_TIMEOUT:
411 spin_lock_bh(&ap_poll_timer_lock);
412 if (!hrtimer_is_queued(&ap_poll_timer)) {
413 hr_time = poll_timeout;
414 hrtimer_forward_now(&ap_poll_timer, hr_time);
415 hrtimer_restart(&ap_poll_timer);
416 }
417 spin_unlock_bh(&ap_poll_timer_lock);
418 break;
419 case AP_SM_WAIT_NONE:
420 default:
421 break;
422 }
423}
424
425
426
427
428
429
430
431void ap_request_timeout(struct timer_list *t)
432{
433 struct ap_queue *aq = from_timer(aq, t, timeout);
434
435 spin_lock_bh(&aq->lock);
436 ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
437 spin_unlock_bh(&aq->lock);
438}
439
440
441
442
443
444
445
446static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
447{
448 tasklet_schedule(&ap_tasklet);
449 return HRTIMER_NORESTART;
450}
451
452
453
454
455
456
457static void ap_interrupt_handler(struct airq_struct *airq, bool floating)
458{
459 inc_irq_stat(IRQIO_APB);
460 tasklet_schedule(&ap_tasklet);
461}
462
463
464
465
466
467
468
469static void ap_tasklet_fn(unsigned long dummy)
470{
471 int bkt;
472 struct ap_queue *aq;
473 enum ap_sm_wait wait = AP_SM_WAIT_NONE;
474
475
476
477
478
479 if (ap_irq_flag)
480 xchg(ap_airq.lsi_ptr, 0);
481
482 spin_lock_bh(&ap_queues_lock);
483 hash_for_each(ap_queues, bkt, aq, hnode) {
484 spin_lock_bh(&aq->lock);
485 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
486 spin_unlock_bh(&aq->lock);
487 }
488 spin_unlock_bh(&ap_queues_lock);
489
490 ap_wait(wait);
491}
492
493static int ap_pending_requests(void)
494{
495 int bkt;
496 struct ap_queue *aq;
497
498 spin_lock_bh(&ap_queues_lock);
499 hash_for_each(ap_queues, bkt, aq, hnode) {
500 if (aq->queue_count == 0)
501 continue;
502 spin_unlock_bh(&ap_queues_lock);
503 return 1;
504 }
505 spin_unlock_bh(&ap_queues_lock);
506 return 0;
507}
508
509
510
511
512
513
514
515
516
517
518
519static int ap_poll_thread(void *data)
520{
521 DECLARE_WAITQUEUE(wait, current);
522
523 set_user_nice(current, MAX_NICE);
524 set_freezable();
525 while (!kthread_should_stop()) {
526 add_wait_queue(&ap_poll_wait, &wait);
527 set_current_state(TASK_INTERRUPTIBLE);
528 if (!ap_pending_requests()) {
529 schedule();
530 try_to_freeze();
531 }
532 set_current_state(TASK_RUNNING);
533 remove_wait_queue(&ap_poll_wait, &wait);
534 if (need_resched()) {
535 schedule();
536 try_to_freeze();
537 continue;
538 }
539 ap_tasklet_fn(0);
540 }
541
542 return 0;
543}
544
545static int ap_poll_thread_start(void)
546{
547 int rc;
548
549 if (ap_irq_flag || ap_poll_kthread)
550 return 0;
551 mutex_lock(&ap_poll_thread_mutex);
552 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
553 rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
554 if (rc)
555 ap_poll_kthread = NULL;
556 mutex_unlock(&ap_poll_thread_mutex);
557 return rc;
558}
559
560static void ap_poll_thread_stop(void)
561{
562 if (!ap_poll_kthread)
563 return;
564 mutex_lock(&ap_poll_thread_mutex);
565 kthread_stop(ap_poll_kthread);
566 ap_poll_kthread = NULL;
567 mutex_unlock(&ap_poll_thread_mutex);
568}
569
570#define is_card_dev(x) ((x)->parent == ap_root_device)
571#define is_queue_dev(x) ((x)->parent != ap_root_device)
572
573
574
575
576
577
578
579
580static int ap_bus_match(struct device *dev, struct device_driver *drv)
581{
582 struct ap_driver *ap_drv = to_ap_drv(drv);
583 struct ap_device_id *id;
584
585
586
587
588
589 for (id = ap_drv->ids; id->match_flags; id++) {
590 if (is_card_dev(dev) &&
591 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
592 id->dev_type == to_ap_dev(dev)->device_type)
593 return 1;
594 if (is_queue_dev(dev) &&
595 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
596 id->dev_type == to_ap_dev(dev)->device_type)
597 return 1;
598 }
599 return 0;
600}
601
602
603
604
605
606
607
608
609
610static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
611{
612 int rc = 0;
613 struct ap_device *ap_dev = to_ap_dev(dev);
614
615
616 if (dev == ap_root_device)
617 return 0;
618
619 if (is_card_dev(dev)) {
620 struct ap_card *ac = to_ap_card(&ap_dev->device);
621
622
623 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
624 if (rc)
625 return rc;
626
627 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
628 if (rc)
629 return rc;
630
631
632 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL))
633 rc = add_uevent_var(env, "MODE=accel");
634 else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
635 rc = add_uevent_var(env, "MODE=cca");
636 else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
637 rc = add_uevent_var(env, "MODE=ep11");
638 if (rc)
639 return rc;
640 } else {
641 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
642
643
644 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL))
645 rc = add_uevent_var(env, "MODE=accel");
646 else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
647 rc = add_uevent_var(env, "MODE=cca");
648 else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
649 rc = add_uevent_var(env, "MODE=ep11");
650 if (rc)
651 return rc;
652 }
653
654 return 0;
655}
656
657static void ap_send_init_scan_done_uevent(void)
658{
659 char *envp[] = { "INITSCAN=done", NULL };
660
661 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
662}
663
664static void ap_send_bindings_complete_uevent(void)
665{
666 char buf[32];
667 char *envp[] = { "BINDINGS=complete", buf, NULL };
668
669 snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
670 atomic64_inc_return(&ap_bindings_complete_count));
671 kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
672}
673
674void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
675{
676 char buf[16];
677 char *envp[] = { buf, NULL };
678
679 snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
680
681 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
682}
683EXPORT_SYMBOL(ap_send_config_uevent);
684
685void ap_send_online_uevent(struct ap_device *ap_dev, int online)
686{
687 char buf[16];
688 char *envp[] = { buf, NULL };
689
690 snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
691
692 kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
693}
694EXPORT_SYMBOL(ap_send_online_uevent);
695
696
697
698
699
700struct __ap_calc_ctrs {
701 unsigned int apqns;
702 unsigned int bound;
703};
704
705static int __ap_calc_helper(struct device *dev, void *arg)
706{
707 struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *) arg;
708
709 if (is_queue_dev(dev)) {
710 pctrs->apqns++;
711 if (dev->driver)
712 pctrs->bound++;
713 }
714
715 return 0;
716}
717
718static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
719{
720 struct __ap_calc_ctrs ctrs;
721
722 memset(&ctrs, 0, sizeof(ctrs));
723 bus_for_each_dev(&ap_bus_type, NULL, (void *) &ctrs, __ap_calc_helper);
724
725 *apqns = ctrs.apqns;
726 *bound = ctrs.bound;
727}
728
729
730
731
732
733static void ap_check_bindings_complete(void)
734{
735 unsigned int apqns, bound;
736
737 if (atomic64_read(&ap_scan_bus_count) >= 1) {
738 ap_calc_bound_apqns(&apqns, &bound);
739 if (bound == apqns) {
740 if (!completion_done(&ap_init_apqn_bindings_complete)) {
741 complete_all(&ap_init_apqn_bindings_complete);
742 AP_DBF_INFO("%s complete\n", __func__);
743 }
744 ap_send_bindings_complete_uevent();
745 }
746 }
747}
748
749
750
751
752
753
754
755
756
757
758
759int ap_wait_init_apqn_bindings_complete(unsigned long timeout)
760{
761 long l;
762
763 if (completion_done(&ap_init_apqn_bindings_complete))
764 return 0;
765
766 if (timeout)
767 l = wait_for_completion_interruptible_timeout(
768 &ap_init_apqn_bindings_complete, timeout);
769 else
770 l = wait_for_completion_interruptible(
771 &ap_init_apqn_bindings_complete);
772 if (l < 0)
773 return l == -ERESTARTSYS ? -EINTR : l;
774 else if (l == 0 && timeout)
775 return -ETIME;
776
777 return 0;
778}
779EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete);
780
781static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
782{
783 if (is_queue_dev(dev) &&
784 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
785 device_unregister(dev);
786 return 0;
787}
788
789static int __ap_revise_reserved(struct device *dev, void *dummy)
790{
791 int rc, card, queue, devres, drvres;
792
793 if (is_queue_dev(dev)) {
794 card = AP_QID_CARD(to_ap_queue(dev)->qid);
795 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
796 mutex_lock(&ap_perms_mutex);
797 devres = test_bit_inv(card, ap_perms.apm)
798 && test_bit_inv(queue, ap_perms.aqm);
799 mutex_unlock(&ap_perms_mutex);
800 drvres = to_ap_drv(dev->driver)->flags
801 & AP_DRIVER_FLAG_DEFAULT;
802 if (!!devres != !!drvres) {
803 AP_DBF_DBG("%s reprobing queue=%02x.%04x\n",
804 __func__, card, queue);
805 rc = device_reprobe(dev);
806 if (rc)
807 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
808 __func__, card, queue);
809 }
810 }
811
812 return 0;
813}
814
815static void ap_bus_revise_bindings(void)
816{
817 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
818}
819
820int ap_owned_by_def_drv(int card, int queue)
821{
822 int rc = 0;
823
824 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
825 return -EINVAL;
826
827 mutex_lock(&ap_perms_mutex);
828
829 if (test_bit_inv(card, ap_perms.apm)
830 && test_bit_inv(queue, ap_perms.aqm))
831 rc = 1;
832
833 mutex_unlock(&ap_perms_mutex);
834
835 return rc;
836}
837EXPORT_SYMBOL(ap_owned_by_def_drv);
838
839int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
840 unsigned long *aqm)
841{
842 int card, queue, rc = 0;
843
844 mutex_lock(&ap_perms_mutex);
845
846 for (card = 0; !rc && card < AP_DEVICES; card++)
847 if (test_bit_inv(card, apm) &&
848 test_bit_inv(card, ap_perms.apm))
849 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
850 if (test_bit_inv(queue, aqm) &&
851 test_bit_inv(queue, ap_perms.aqm))
852 rc = 1;
853
854 mutex_unlock(&ap_perms_mutex);
855
856 return rc;
857}
858EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
859
860static int ap_device_probe(struct device *dev)
861{
862 struct ap_device *ap_dev = to_ap_dev(dev);
863 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
864 int card, queue, devres, drvres, rc = -ENODEV;
865
866 if (!get_device(dev))
867 return rc;
868
869 if (is_queue_dev(dev)) {
870
871
872
873
874
875
876 card = AP_QID_CARD(to_ap_queue(dev)->qid);
877 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
878 mutex_lock(&ap_perms_mutex);
879 devres = test_bit_inv(card, ap_perms.apm)
880 && test_bit_inv(queue, ap_perms.aqm);
881 mutex_unlock(&ap_perms_mutex);
882 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
883 if (!!devres != !!drvres)
884 goto out;
885 }
886
887
888 spin_lock_bh(&ap_queues_lock);
889 if (is_queue_dev(dev))
890 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
891 to_ap_queue(dev)->qid);
892 spin_unlock_bh(&ap_queues_lock);
893
894 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
895
896 if (rc) {
897 spin_lock_bh(&ap_queues_lock);
898 if (is_queue_dev(dev))
899 hash_del(&to_ap_queue(dev)->hnode);
900 spin_unlock_bh(&ap_queues_lock);
901 } else
902 ap_check_bindings_complete();
903
904out:
905 if (rc)
906 put_device(dev);
907 return rc;
908}
909
910static void ap_device_remove(struct device *dev)
911{
912 struct ap_device *ap_dev = to_ap_dev(dev);
913 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
914
915
916 if (is_queue_dev(dev))
917 ap_queue_prepare_remove(to_ap_queue(dev));
918
919
920 if (ap_drv->remove)
921 ap_drv->remove(ap_dev);
922
923
924 if (is_queue_dev(dev))
925 ap_queue_remove(to_ap_queue(dev));
926
927
928 spin_lock_bh(&ap_queues_lock);
929 if (is_queue_dev(dev))
930 hash_del(&to_ap_queue(dev)->hnode);
931 spin_unlock_bh(&ap_queues_lock);
932
933 put_device(dev);
934}
935
936struct ap_queue *ap_get_qdev(ap_qid_t qid)
937{
938 int bkt;
939 struct ap_queue *aq;
940
941 spin_lock_bh(&ap_queues_lock);
942 hash_for_each(ap_queues, bkt, aq, hnode) {
943 if (aq->qid == qid) {
944 get_device(&aq->ap_dev.device);
945 spin_unlock_bh(&ap_queues_lock);
946 return aq;
947 }
948 }
949 spin_unlock_bh(&ap_queues_lock);
950
951 return NULL;
952}
953EXPORT_SYMBOL(ap_get_qdev);
954
955int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
956 char *name)
957{
958 struct device_driver *drv = &ap_drv->driver;
959
960 drv->bus = &ap_bus_type;
961 drv->owner = owner;
962 drv->name = name;
963 return driver_register(drv);
964}
965EXPORT_SYMBOL(ap_driver_register);
966
967void ap_driver_unregister(struct ap_driver *ap_drv)
968{
969 driver_unregister(&ap_drv->driver);
970}
971EXPORT_SYMBOL(ap_driver_unregister);
972
973void ap_bus_force_rescan(void)
974{
975
976 del_timer(&ap_config_timer);
977 queue_work(system_long_wq, &ap_scan_work);
978 flush_work(&ap_scan_work);
979}
980EXPORT_SYMBOL(ap_bus_force_rescan);
981
982
983
984
985void ap_bus_cfg_chg(void)
986{
987 AP_DBF_DBG("%s config change, forcing bus rescan\n", __func__);
988
989 ap_bus_force_rescan();
990}
991
992
993
994
995
996
997
998
999
1000static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1001{
1002 int i, n, b;
1003
1004
1005 if (bits & 0x07)
1006 return -EINVAL;
1007
1008 if (str[0] == '0' && str[1] == 'x')
1009 str++;
1010 if (*str == 'x')
1011 str++;
1012
1013 for (i = 0; isxdigit(*str) && i < bits; str++) {
1014 b = hex_to_bin(*str);
1015 for (n = 0; n < 4; n++)
1016 if (b & (0x08 >> n))
1017 set_bit_inv(i + n, bitmap);
1018 i += 4;
1019 }
1020
1021 if (*str == '\n')
1022 str++;
1023 if (*str)
1024 return -EINVAL;
1025 return 0;
1026}
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1046{
1047 int a, i, z;
1048 char *np, sign;
1049
1050
1051 if (bits & 0x07)
1052 return -EINVAL;
1053
1054 while (*str) {
1055 sign = *str++;
1056 if (sign != '+' && sign != '-')
1057 return -EINVAL;
1058 a = z = simple_strtoul(str, &np, 0);
1059 if (str == np || a >= bits)
1060 return -EINVAL;
1061 str = np;
1062 if (*str == '-') {
1063 z = simple_strtoul(++str, &np, 0);
1064 if (str == np || a > z || z >= bits)
1065 return -EINVAL;
1066 str = np;
1067 }
1068 for (i = a; i <= z; i++)
1069 if (sign == '+')
1070 set_bit_inv(i, bitmap);
1071 else
1072 clear_bit_inv(i, bitmap);
1073 while (*str == ',' || *str == '\n')
1074 str++;
1075 }
1076
1077 return 0;
1078}
1079
1080static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1081 unsigned long *newmap)
1082{
1083 unsigned long size;
1084 int rc;
1085
1086 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1087 if (*str == '+' || *str == '-') {
1088 memcpy(newmap, bitmap, size);
1089 rc = modify_bitmap(str, newmap, bits);
1090 } else {
1091 memset(newmap, 0, size);
1092 rc = hex2bitmap(str, newmap, bits);
1093 }
1094 return rc;
1095}
1096
1097int ap_parse_mask_str(const char *str,
1098 unsigned long *bitmap, int bits,
1099 struct mutex *lock)
1100{
1101 unsigned long *newmap, size;
1102 int rc;
1103
1104
1105 if (bits & 0x07)
1106 return -EINVAL;
1107
1108 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
1109 newmap = kmalloc(size, GFP_KERNEL);
1110 if (!newmap)
1111 return -ENOMEM;
1112 if (mutex_lock_interruptible(lock)) {
1113 kfree(newmap);
1114 return -ERESTARTSYS;
1115 }
1116 rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1117 if (rc == 0)
1118 memcpy(bitmap, newmap, size);
1119 mutex_unlock(lock);
1120 kfree(newmap);
1121 return rc;
1122}
1123EXPORT_SYMBOL(ap_parse_mask_str);
1124
1125
1126
1127
1128
1129static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1130{
1131 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1132}
1133
1134static ssize_t ap_domain_store(struct bus_type *bus,
1135 const char *buf, size_t count)
1136{
1137 int domain;
1138
1139 if (sscanf(buf, "%i\n", &domain) != 1 ||
1140 domain < 0 || domain > ap_max_domain_id ||
1141 !test_bit_inv(domain, ap_perms.aqm))
1142 return -EINVAL;
1143
1144 spin_lock_bh(&ap_domain_lock);
1145 ap_domain_index = domain;
1146 spin_unlock_bh(&ap_domain_lock);
1147
1148 AP_DBF_INFO("%s stored new default domain=%d\n",
1149 __func__, domain);
1150
1151 return count;
1152}
1153
1154static BUS_ATTR_RW(ap_domain);
1155
1156static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1157{
1158 if (!ap_qci_info)
1159 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1160
1161 return scnprintf(buf, PAGE_SIZE,
1162 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1163 ap_qci_info->adm[0], ap_qci_info->adm[1],
1164 ap_qci_info->adm[2], ap_qci_info->adm[3],
1165 ap_qci_info->adm[4], ap_qci_info->adm[5],
1166 ap_qci_info->adm[6], ap_qci_info->adm[7]);
1167}
1168
1169static BUS_ATTR_RO(ap_control_domain_mask);
1170
1171static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1172{
1173 if (!ap_qci_info)
1174 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1175
1176 return scnprintf(buf, PAGE_SIZE,
1177 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1178 ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1179 ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1180 ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1181 ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1182}
1183
1184static BUS_ATTR_RO(ap_usage_domain_mask);
1185
1186static ssize_t ap_adapter_mask_show(struct bus_type *bus, char *buf)
1187{
1188 if (!ap_qci_info)
1189 return scnprintf(buf, PAGE_SIZE, "not supported\n");
1190
1191 return scnprintf(buf, PAGE_SIZE,
1192 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1193 ap_qci_info->apm[0], ap_qci_info->apm[1],
1194 ap_qci_info->apm[2], ap_qci_info->apm[3],
1195 ap_qci_info->apm[4], ap_qci_info->apm[5],
1196 ap_qci_info->apm[6], ap_qci_info->apm[7]);
1197}
1198
1199static BUS_ATTR_RO(ap_adapter_mask);
1200
1201static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1202{
1203 return scnprintf(buf, PAGE_SIZE, "%d\n",
1204 ap_irq_flag ? 1 : 0);
1205}
1206
1207static BUS_ATTR_RO(ap_interrupts);
1208
1209static ssize_t config_time_show(struct bus_type *bus, char *buf)
1210{
1211 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1212}
1213
1214static ssize_t config_time_store(struct bus_type *bus,
1215 const char *buf, size_t count)
1216{
1217 int time;
1218
1219 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1220 return -EINVAL;
1221 ap_config_time = time;
1222 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1223 return count;
1224}
1225
1226static BUS_ATTR_RW(config_time);
1227
1228static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1229{
1230 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1231}
1232
1233static ssize_t poll_thread_store(struct bus_type *bus,
1234 const char *buf, size_t count)
1235{
1236 int flag, rc;
1237
1238 if (sscanf(buf, "%d\n", &flag) != 1)
1239 return -EINVAL;
1240 if (flag) {
1241 rc = ap_poll_thread_start();
1242 if (rc)
1243 count = rc;
1244 } else
1245 ap_poll_thread_stop();
1246 return count;
1247}
1248
1249static BUS_ATTR_RW(poll_thread);
1250
1251static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1252{
1253 return scnprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1254}
1255
1256static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1257 size_t count)
1258{
1259 unsigned long long time;
1260 ktime_t hr_time;
1261
1262
1263 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1264 time > 120000000000ULL)
1265 return -EINVAL;
1266 poll_timeout = time;
1267 hr_time = poll_timeout;
1268
1269 spin_lock_bh(&ap_poll_timer_lock);
1270 hrtimer_cancel(&ap_poll_timer);
1271 hrtimer_set_expires(&ap_poll_timer, hr_time);
1272 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1273 spin_unlock_bh(&ap_poll_timer_lock);
1274
1275 return count;
1276}
1277
1278static BUS_ATTR_RW(poll_timeout);
1279
1280static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1281{
1282 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_domain_id);
1283}
1284
1285static BUS_ATTR_RO(ap_max_domain_id);
1286
1287static ssize_t ap_max_adapter_id_show(struct bus_type *bus, char *buf)
1288{
1289 return scnprintf(buf, PAGE_SIZE, "%d\n", ap_max_adapter_id);
1290}
1291
1292static BUS_ATTR_RO(ap_max_adapter_id);
1293
1294static ssize_t apmask_show(struct bus_type *bus, char *buf)
1295{
1296 int rc;
1297
1298 if (mutex_lock_interruptible(&ap_perms_mutex))
1299 return -ERESTARTSYS;
1300 rc = scnprintf(buf, PAGE_SIZE,
1301 "0x%016lx%016lx%016lx%016lx\n",
1302 ap_perms.apm[0], ap_perms.apm[1],
1303 ap_perms.apm[2], ap_perms.apm[3]);
1304 mutex_unlock(&ap_perms_mutex);
1305
1306 return rc;
1307}
1308
1309static int __verify_card_reservations(struct device_driver *drv, void *data)
1310{
1311 int rc = 0;
1312 struct ap_driver *ap_drv = to_ap_drv(drv);
1313 unsigned long *newapm = (unsigned long *)data;
1314
1315
1316
1317
1318
1319 if (!try_module_get(drv->owner))
1320 return 0;
1321
1322 if (ap_drv->in_use) {
1323 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1324 if (rc)
1325 rc = -EBUSY;
1326 }
1327
1328
1329 module_put(drv->owner);
1330
1331 return rc;
1332}
1333
1334static int apmask_commit(unsigned long *newapm)
1335{
1336 int rc;
1337 unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1338
1339
1340
1341
1342
1343 if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1344 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1345 __verify_card_reservations);
1346 if (rc)
1347 return rc;
1348 }
1349
1350 memcpy(ap_perms.apm, newapm, APMASKSIZE);
1351
1352 return 0;
1353}
1354
1355static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1356 size_t count)
1357{
1358 int rc;
1359 DECLARE_BITMAP(newapm, AP_DEVICES);
1360
1361 if (mutex_lock_interruptible(&ap_perms_mutex))
1362 return -ERESTARTSYS;
1363
1364 rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1365 if (rc)
1366 goto done;
1367
1368 rc = apmask_commit(newapm);
1369
1370done:
1371 mutex_unlock(&ap_perms_mutex);
1372 if (rc)
1373 return rc;
1374
1375 ap_bus_revise_bindings();
1376
1377 return count;
1378}
1379
1380static BUS_ATTR_RW(apmask);
1381
1382static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1383{
1384 int rc;
1385
1386 if (mutex_lock_interruptible(&ap_perms_mutex))
1387 return -ERESTARTSYS;
1388 rc = scnprintf(buf, PAGE_SIZE,
1389 "0x%016lx%016lx%016lx%016lx\n",
1390 ap_perms.aqm[0], ap_perms.aqm[1],
1391 ap_perms.aqm[2], ap_perms.aqm[3]);
1392 mutex_unlock(&ap_perms_mutex);
1393
1394 return rc;
1395}
1396
1397static int __verify_queue_reservations(struct device_driver *drv, void *data)
1398{
1399 int rc = 0;
1400 struct ap_driver *ap_drv = to_ap_drv(drv);
1401 unsigned long *newaqm = (unsigned long *)data;
1402
1403
1404
1405
1406
1407 if (!try_module_get(drv->owner))
1408 return 0;
1409
1410 if (ap_drv->in_use) {
1411 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1412 if (rc)
1413 return -EBUSY;
1414 }
1415
1416
1417 module_put(drv->owner);
1418
1419 return rc;
1420}
1421
1422static int aqmask_commit(unsigned long *newaqm)
1423{
1424 int rc;
1425 unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1426
1427
1428
1429
1430
1431 if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1432 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1433 __verify_queue_reservations);
1434 if (rc)
1435 return rc;
1436 }
1437
1438 memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1439
1440 return 0;
1441}
1442
1443static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1444 size_t count)
1445{
1446 int rc;
1447 DECLARE_BITMAP(newaqm, AP_DOMAINS);
1448
1449 if (mutex_lock_interruptible(&ap_perms_mutex))
1450 return -ERESTARTSYS;
1451
1452 rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1453 if (rc)
1454 goto done;
1455
1456 rc = aqmask_commit(newaqm);
1457
1458done:
1459 mutex_unlock(&ap_perms_mutex);
1460 if (rc)
1461 return rc;
1462
1463 ap_bus_revise_bindings();
1464
1465 return count;
1466}
1467
1468static BUS_ATTR_RW(aqmask);
1469
1470static ssize_t scans_show(struct bus_type *bus, char *buf)
1471{
1472 return scnprintf(buf, PAGE_SIZE, "%llu\n",
1473 atomic64_read(&ap_scan_bus_count));
1474}
1475
1476static ssize_t scans_store(struct bus_type *bus, const char *buf,
1477 size_t count)
1478{
1479 AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1480
1481 ap_bus_force_rescan();
1482
1483 return count;
1484}
1485
1486static BUS_ATTR_RW(scans);
1487
1488static ssize_t bindings_show(struct bus_type *bus, char *buf)
1489{
1490 int rc;
1491 unsigned int apqns, n;
1492
1493 ap_calc_bound_apqns(&apqns, &n);
1494 if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1495 rc = scnprintf(buf, PAGE_SIZE, "%u/%u (complete)\n", n, apqns);
1496 else
1497 rc = scnprintf(buf, PAGE_SIZE, "%u/%u\n", n, apqns);
1498
1499 return rc;
1500}
1501
1502static BUS_ATTR_RO(bindings);
1503
1504static struct attribute *ap_bus_attrs[] = {
1505 &bus_attr_ap_domain.attr,
1506 &bus_attr_ap_control_domain_mask.attr,
1507 &bus_attr_ap_usage_domain_mask.attr,
1508 &bus_attr_ap_adapter_mask.attr,
1509 &bus_attr_config_time.attr,
1510 &bus_attr_poll_thread.attr,
1511 &bus_attr_ap_interrupts.attr,
1512 &bus_attr_poll_timeout.attr,
1513 &bus_attr_ap_max_domain_id.attr,
1514 &bus_attr_ap_max_adapter_id.attr,
1515 &bus_attr_apmask.attr,
1516 &bus_attr_aqmask.attr,
1517 &bus_attr_scans.attr,
1518 &bus_attr_bindings.attr,
1519 NULL,
1520};
1521ATTRIBUTE_GROUPS(ap_bus);
1522
1523static struct bus_type ap_bus_type = {
1524 .name = "ap",
1525 .bus_groups = ap_bus_groups,
1526 .match = &ap_bus_match,
1527 .uevent = &ap_uevent,
1528 .probe = ap_device_probe,
1529 .remove = ap_device_remove,
1530};
1531
1532
1533
1534
1535
1536static void ap_select_domain(void)
1537{
1538 struct ap_queue_status status;
1539 int card, dom;
1540
1541
1542
1543
1544
1545
1546 spin_lock_bh(&ap_domain_lock);
1547 if (ap_domain_index >= 0) {
1548
1549 goto out;
1550 }
1551 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1552 if (!ap_test_config_usage_domain(dom) ||
1553 !test_bit_inv(dom, ap_perms.aqm))
1554 continue;
1555 for (card = 0; card <= ap_max_adapter_id; card++) {
1556 if (!ap_test_config_card_id(card) ||
1557 !test_bit_inv(card, ap_perms.apm))
1558 continue;
1559 status = ap_test_queue(AP_MKQID(card, dom),
1560 ap_apft_available(),
1561 NULL);
1562 if (status.response_code == AP_RESPONSE_NORMAL)
1563 break;
1564 }
1565 if (card <= ap_max_adapter_id)
1566 break;
1567 }
1568 if (dom <= ap_max_domain_id) {
1569 ap_domain_index = dom;
1570 AP_DBF_INFO("%s new default domain is %d\n",
1571 __func__, ap_domain_index);
1572 }
1573out:
1574 spin_unlock_bh(&ap_domain_lock);
1575}
1576
1577
1578
1579
1580
1581
1582static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1583{
1584 int comp_type = 0;
1585
1586
1587 if (rawtype < AP_DEVICE_TYPE_CEX2A) {
1588 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1589 __func__, AP_QID_CARD(qid),
1590 AP_QID_QUEUE(qid), rawtype);
1591 return 0;
1592 }
1593
1594 if (rawtype <= AP_DEVICE_TYPE_CEX8)
1595 return rawtype;
1596
1597
1598
1599
1600
1601 if (ap_qact_available()) {
1602 struct ap_queue_status status;
1603 union ap_qact_ap_info apinfo = {0};
1604
1605 apinfo.mode = (func >> 26) & 0x07;
1606 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1607 status = ap_qact(qid, 0, &apinfo);
1608 if (status.response_code == AP_RESPONSE_NORMAL
1609 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1610 && apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1611 comp_type = apinfo.cat;
1612 }
1613 if (!comp_type)
1614 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1615 __func__, AP_QID_CARD(qid),
1616 AP_QID_QUEUE(qid), rawtype);
1617 else if (comp_type != rawtype)
1618 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1619 __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1620 rawtype, comp_type);
1621 return comp_type;
1622}
1623
1624
1625
1626
1627
1628static int __match_card_device_with_id(struct device *dev, const void *data)
1629{
1630 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *) data;
1631}
1632
1633
1634
1635
1636
1637static int __match_queue_device_with_qid(struct device *dev, const void *data)
1638{
1639 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1640}
1641
1642
1643
1644
1645
1646static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1647{
1648 return is_queue_dev(dev)
1649 && AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long) data;
1650}
1651
1652
1653static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1654{
1655 struct ap_driver *ap_drv = to_ap_drv(drv);
1656
1657 if (try_module_get(drv->owner)) {
1658 if (ap_drv->on_config_changed)
1659 ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1660 module_put(drv->owner);
1661 }
1662
1663 return 0;
1664}
1665
1666
1667static inline void notify_config_changed(void)
1668{
1669 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1670 __drv_notify_config_changed);
1671}
1672
1673
1674static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1675{
1676 struct ap_driver *ap_drv = to_ap_drv(drv);
1677
1678 if (try_module_get(drv->owner)) {
1679 if (ap_drv->on_scan_complete)
1680 ap_drv->on_scan_complete(ap_qci_info,
1681 ap_qci_info_old);
1682 module_put(drv->owner);
1683 }
1684
1685 return 0;
1686}
1687
1688
1689static inline void notify_scan_complete(void)
1690{
1691 bus_for_each_drv(&ap_bus_type, NULL, NULL,
1692 __drv_notify_scan_complete);
1693}
1694
1695
1696
1697
1698
1699static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1700{
1701 bus_for_each_dev(&ap_bus_type, NULL,
1702 (void *)(long) ac->id,
1703 __ap_queue_devices_with_id_unregister);
1704 device_unregister(&ac->ap_dev.device);
1705}
1706
1707
1708
1709
1710
1711
1712static inline void ap_scan_domains(struct ap_card *ac)
1713{
1714 bool decfg, chkstop;
1715 ap_qid_t qid;
1716 unsigned int func;
1717 struct device *dev;
1718 struct ap_queue *aq;
1719 int rc, dom, depth, type, ml;
1720
1721
1722
1723
1724
1725
1726
1727 for (dom = 0; dom <= ap_max_domain_id; dom++) {
1728 qid = AP_MKQID(ac->id, dom);
1729 dev = bus_find_device(&ap_bus_type, NULL,
1730 (void *)(long) qid,
1731 __match_queue_device_with_qid);
1732 aq = dev ? to_ap_queue(dev) : NULL;
1733 if (!ap_test_config_usage_domain(dom)) {
1734 if (dev) {
1735 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1736 __func__, ac->id, dom);
1737 device_unregister(dev);
1738 put_device(dev);
1739 }
1740 continue;
1741 }
1742
1743 if (!ap_queue_info(qid, &type, &func, &depth,
1744 &ml, &decfg, &chkstop)) {
1745 if (aq) {
1746 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1747 __func__, ac->id, dom);
1748 device_unregister(dev);
1749 put_device(dev);
1750 }
1751 continue;
1752 }
1753
1754 if (!aq) {
1755 aq = ap_queue_create(qid, ac->ap_dev.device_type);
1756 if (!aq) {
1757 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1758 __func__, ac->id, dom);
1759 continue;
1760 }
1761 aq->card = ac;
1762 aq->config = !decfg;
1763 aq->chkstop = chkstop;
1764 dev = &aq->ap_dev.device;
1765 dev->bus = &ap_bus_type;
1766 dev->parent = &ac->ap_dev.device;
1767 dev_set_name(dev, "%02x.%04x", ac->id, dom);
1768
1769 rc = device_register(dev);
1770 if (rc) {
1771 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1772 __func__, ac->id, dom);
1773 goto put_dev_and_continue;
1774 }
1775
1776 get_device(dev);
1777 if (decfg)
1778 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1779 __func__, ac->id, dom);
1780 else if (chkstop)
1781 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1782 __func__, ac->id, dom);
1783 else
1784 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1785 __func__, ac->id, dom);
1786 goto put_dev_and_continue;
1787 }
1788
1789 spin_lock_bh(&aq->lock);
1790
1791 if (chkstop && !aq->chkstop) {
1792
1793 aq->chkstop = true;
1794 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1795 aq->dev_state = AP_DEV_STATE_ERROR;
1796 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1797 }
1798 spin_unlock_bh(&aq->lock);
1799 AP_DBF_DBG("%s(%d,%d) queue dev checkstop on\n",
1800 __func__, ac->id, dom);
1801
1802 ap_flush_queue(aq);
1803 goto put_dev_and_continue;
1804 } else if (!chkstop && aq->chkstop) {
1805
1806 aq->chkstop = false;
1807 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1808 aq->dev_state = AP_DEV_STATE_OPERATING;
1809 aq->sm_state = AP_SM_STATE_RESET_START;
1810 }
1811 spin_unlock_bh(&aq->lock);
1812 AP_DBF_DBG("%s(%d,%d) queue dev checkstop off\n",
1813 __func__, ac->id, dom);
1814 goto put_dev_and_continue;
1815 }
1816
1817 if (decfg && aq->config) {
1818
1819 aq->config = false;
1820 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1821 aq->dev_state = AP_DEV_STATE_ERROR;
1822 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
1823 }
1824 spin_unlock_bh(&aq->lock);
1825 AP_DBF_DBG("%s(%d,%d) queue dev config off\n",
1826 __func__, ac->id, dom);
1827 ap_send_config_uevent(&aq->ap_dev, aq->config);
1828
1829 ap_flush_queue(aq);
1830 goto put_dev_and_continue;
1831 } else if (!decfg && !aq->config) {
1832
1833 aq->config = true;
1834 if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1835 aq->dev_state = AP_DEV_STATE_OPERATING;
1836 aq->sm_state = AP_SM_STATE_RESET_START;
1837 }
1838 spin_unlock_bh(&aq->lock);
1839 AP_DBF_DBG("%s(%d,%d) queue dev config on\n",
1840 __func__, ac->id, dom);
1841 ap_send_config_uevent(&aq->ap_dev, aq->config);
1842 goto put_dev_and_continue;
1843 }
1844
1845 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
1846 spin_unlock_bh(&aq->lock);
1847
1848 ap_flush_queue(aq);
1849
1850 ap_queue_init_state(aq);
1851 AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
1852 __func__, ac->id, dom);
1853 goto put_dev_and_continue;
1854 }
1855 spin_unlock_bh(&aq->lock);
1856put_dev_and_continue:
1857 put_device(dev);
1858 }
1859}
1860
1861
1862
1863
1864
1865static inline void ap_scan_adapter(int ap)
1866{
1867 bool decfg, chkstop;
1868 ap_qid_t qid;
1869 unsigned int func;
1870 struct device *dev;
1871 struct ap_card *ac;
1872 int rc, dom, depth, type, comp_type, ml;
1873
1874
1875 dev = bus_find_device(&ap_bus_type, NULL,
1876 (void *)(long) ap,
1877 __match_card_device_with_id);
1878 ac = dev ? to_ap_card(dev) : NULL;
1879
1880
1881 if (!ap_test_config_card_id(ap)) {
1882 if (ac) {
1883 AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
1884 __func__, ap);
1885 ap_scan_rm_card_dev_and_queue_devs(ac);
1886 put_device(dev);
1887 }
1888 return;
1889 }
1890
1891
1892
1893
1894
1895
1896
1897
1898 for (dom = 0; dom <= ap_max_domain_id; dom++)
1899 if (ap_test_config_usage_domain(dom)) {
1900 qid = AP_MKQID(ap, dom);
1901 if (ap_queue_info(qid, &type, &func, &depth,
1902 &ml, &decfg, &chkstop))
1903 break;
1904 }
1905 if (dom > ap_max_domain_id) {
1906
1907 if (ac) {
1908 AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
1909 __func__, ap);
1910 ap_scan_rm_card_dev_and_queue_devs(ac);
1911 put_device(dev);
1912 } else {
1913 AP_DBF_DBG("%s(%d) no type info (no APQN found), ignored\n",
1914 __func__, ap);
1915 }
1916 return;
1917 }
1918 if (!type) {
1919
1920 if (ac) {
1921 AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
1922 __func__, ap);
1923 ap_scan_rm_card_dev_and_queue_devs(ac);
1924 put_device(dev);
1925 } else {
1926 AP_DBF_DBG("%s(%d) no valid type (0) info, ignored\n",
1927 __func__, ap);
1928 }
1929 return;
1930 }
1931
1932 if (ac) {
1933
1934 if (ac->raw_hwtype != type) {
1935 AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
1936 __func__, ap, type);
1937 ap_scan_rm_card_dev_and_queue_devs(ac);
1938 put_device(dev);
1939 ac = NULL;
1940 } else if (ac->functions != func) {
1941 AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
1942 __func__, ap, type);
1943 ap_scan_rm_card_dev_and_queue_devs(ac);
1944 put_device(dev);
1945 ac = NULL;
1946 } else {
1947
1948 if (chkstop && !ac->chkstop) {
1949
1950 ac->chkstop = true;
1951 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
1952 __func__, ap);
1953 } else if (!chkstop && ac->chkstop) {
1954
1955 ac->chkstop = false;
1956 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
1957 __func__, ap);
1958 }
1959
1960 if (decfg && ac->config) {
1961 ac->config = false;
1962 AP_DBF_INFO("%s(%d) card dev config off\n",
1963 __func__, ap);
1964 ap_send_config_uevent(&ac->ap_dev, ac->config);
1965 } else if (!decfg && !ac->config) {
1966 ac->config = true;
1967 AP_DBF_INFO("%s(%d) card dev config on\n",
1968 __func__, ap);
1969 ap_send_config_uevent(&ac->ap_dev, ac->config);
1970 }
1971 }
1972 }
1973
1974 if (!ac) {
1975
1976 comp_type = ap_get_compatible_type(qid, type, func);
1977 if (!comp_type) {
1978 AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
1979 __func__, ap, type);
1980 return;
1981 }
1982 ac = ap_card_create(ap, depth, type, comp_type, func, ml);
1983 if (!ac) {
1984 AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
1985 __func__, ap);
1986 return;
1987 }
1988 ac->config = !decfg;
1989 ac->chkstop = chkstop;
1990 dev = &ac->ap_dev.device;
1991 dev->bus = &ap_bus_type;
1992 dev->parent = ap_root_device;
1993 dev_set_name(dev, "card%02x", ap);
1994
1995 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
1996 atomic_set(&ap_max_msg_size, ac->maxmsgsize);
1997 AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
1998 __func__, ap,
1999 atomic_read(&ap_max_msg_size));
2000 }
2001
2002 rc = device_register(dev);
2003 if (rc) {
2004 AP_DBF_WARN("%s(%d) device_register() failed\n",
2005 __func__, ap);
2006 put_device(dev);
2007 return;
2008 }
2009
2010 get_device(dev);
2011 if (decfg)
2012 AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2013 __func__, ap, type, func);
2014 else if (chkstop)
2015 AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2016 __func__, ap, type, func);
2017 else
2018 AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2019 __func__, ap, type, func);
2020 }
2021
2022
2023 ap_scan_domains(ac);
2024
2025
2026 put_device(&ac->ap_dev.device);
2027}
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039static bool ap_get_configuration(void)
2040{
2041 memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2042 ap_fetch_qci_info(ap_qci_info);
2043
2044 return memcmp(ap_qci_info, ap_qci_info_old,
2045 sizeof(struct ap_config_info)) != 0;
2046}
2047
2048
2049
2050
2051
2052
2053static void ap_scan_bus(struct work_struct *unused)
2054{
2055 int ap, config_changed = 0;
2056
2057
2058 config_changed = ap_get_configuration();
2059 if (config_changed)
2060 notify_config_changed();
2061 ap_select_domain();
2062
2063 AP_DBF_DBG("%s running\n", __func__);
2064
2065
2066 for (ap = 0; ap <= ap_max_adapter_id; ap++)
2067 ap_scan_adapter(ap);
2068
2069
2070 if (config_changed)
2071 notify_scan_complete();
2072
2073
2074 if (ap_domain_index >= 0) {
2075 struct device *dev =
2076 bus_find_device(&ap_bus_type, NULL,
2077 (void *)(long) ap_domain_index,
2078 __match_queue_device_with_queue_id);
2079 if (dev)
2080 put_device(dev);
2081 else
2082 AP_DBF_INFO("%s no queue device with default domain %d available\n",
2083 __func__, ap_domain_index);
2084 }
2085
2086 if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2087 AP_DBF_DBG("%s init scan complete\n", __func__);
2088 ap_send_init_scan_done_uevent();
2089 ap_check_bindings_complete();
2090 }
2091
2092 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
2093}
2094
2095static void ap_config_timeout(struct timer_list *unused)
2096{
2097 queue_work(system_long_wq, &ap_scan_work);
2098}
2099
2100static int __init ap_debug_init(void)
2101{
2102 ap_dbf_info = debug_register("ap", 2, 1,
2103 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2104 debug_register_view(ap_dbf_info, &debug_sprintf_view);
2105 debug_set_level(ap_dbf_info, DBF_ERR);
2106
2107 return 0;
2108}
2109
2110static void __init ap_perms_init(void)
2111{
2112
2113 memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2114 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2115 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2116
2117
2118 if (apm_str) {
2119 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2120 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2121 &ap_perms_mutex);
2122 }
2123
2124
2125 if (aqm_str) {
2126 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2127 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2128 &ap_perms_mutex);
2129 }
2130}
2131
2132
2133
2134
2135
2136
2137static int __init ap_module_init(void)
2138{
2139 int rc;
2140
2141 rc = ap_debug_init();
2142 if (rc)
2143 return rc;
2144
2145 if (!ap_instructions_available()) {
2146 pr_warn("The hardware system does not support AP instructions\n");
2147 return -ENODEV;
2148 }
2149
2150
2151 hash_init(ap_queues);
2152
2153
2154 ap_perms_init();
2155
2156
2157 ap_init_qci_info();
2158
2159
2160 if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2161 (ap_domain_index >= 0 &&
2162 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2163 pr_warn("%d is not a valid cryptographic domain\n",
2164 ap_domain_index);
2165 ap_domain_index = -1;
2166 }
2167
2168
2169 if (ap_interrupts_available() && ap_useirq) {
2170 rc = register_adapter_interrupt(&ap_airq);
2171 ap_irq_flag = (rc == 0);
2172 }
2173
2174
2175 rc = bus_register(&ap_bus_type);
2176 if (rc)
2177 goto out;
2178
2179
2180 ap_root_device = root_device_register("ap");
2181 rc = PTR_ERR_OR_ZERO(ap_root_device);
2182 if (rc)
2183 goto out_bus;
2184 ap_root_device->bus = &ap_bus_type;
2185
2186
2187 timer_setup(&ap_config_timer, ap_config_timeout, 0);
2188
2189
2190
2191
2192
2193 if (MACHINE_IS_VM)
2194 poll_timeout = 1500000;
2195 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2196 ap_poll_timer.function = ap_poll_timeout;
2197
2198
2199 if (ap_thread_flag) {
2200 rc = ap_poll_thread_start();
2201 if (rc)
2202 goto out_work;
2203 }
2204
2205 queue_work(system_long_wq, &ap_scan_work);
2206
2207 return 0;
2208
2209out_work:
2210 hrtimer_cancel(&ap_poll_timer);
2211 root_device_unregister(ap_root_device);
2212out_bus:
2213 bus_unregister(&ap_bus_type);
2214out:
2215 if (ap_irq_flag)
2216 unregister_adapter_interrupt(&ap_airq);
2217 kfree(ap_qci_info);
2218 return rc;
2219}
2220device_initcall(ap_module_init);
2221