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