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