]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/s390/crypto/ap_bus.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / crypto / ap_bus.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright IBM Corp. 2006, 2012
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
7 * Felix Beck <felix.beck@de.ibm.com>
8 * Holger Dengler <hd@linux.vnet.ibm.com>
9 *
10 * Adjunct processor bus.
11 */
12
13 #define KMSG_COMPONENT "ap"
14 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16 #include <linux/kernel_stat.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
21 #include <linux/interrupt.h>
22 #include <linux/workqueue.h>
23 #include <linux/slab.h>
24 #include <linux/notifier.h>
25 #include <linux/kthread.h>
26 #include <linux/mutex.h>
27 #include <linux/suspend.h>
28 #include <asm/reset.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 * Module parameters; note though this file itself isn't modular.
45 */
46 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
47 static DEFINE_SPINLOCK(ap_domain_lock);
48 module_param_named(domain, ap_domain_index, int, 0440);
49 MODULE_PARM_DESC(domain, "domain index for ap devices");
50 EXPORT_SYMBOL(ap_domain_index);
51
52 static int ap_thread_flag;
53 module_param_named(poll_thread, ap_thread_flag, int, 0440);
54 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
55
56 static char *apm_str;
57 module_param_named(apmask, apm_str, charp, 0440);
58 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
59
60 static char *aqm_str;
61 module_param_named(aqmask, aqm_str, charp, 0440);
62 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
63
64 static struct device *ap_root_device;
65
66 DEFINE_SPINLOCK(ap_list_lock);
67 LIST_HEAD(ap_card_list);
68
69 /* Default permissions (card and domain masking) */
70 static struct ap_perms {
71 DECLARE_BITMAP(apm, AP_DEVICES);
72 DECLARE_BITMAP(aqm, AP_DOMAINS);
73 } ap_perms;
74 static DEFINE_MUTEX(ap_perms_mutex);
75
76 static struct ap_config_info *ap_configuration;
77 static bool initialised;
78
79 /*
80 * AP bus related debug feature things.
81 */
82 debug_info_t *ap_dbf_info;
83
84 /*
85 * Workqueue timer for bus rescan.
86 */
87 static struct timer_list ap_config_timer;
88 static int ap_config_time = AP_CONFIG_TIME;
89 static void ap_scan_bus(struct work_struct *);
90 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
91
92 /*
93 * Tasklet & timer for AP request polling and interrupts
94 */
95 static void ap_tasklet_fn(unsigned long);
96 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
97 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
98 static struct task_struct *ap_poll_kthread;
99 static DEFINE_MUTEX(ap_poll_thread_mutex);
100 static DEFINE_SPINLOCK(ap_poll_timer_lock);
101 static struct hrtimer ap_poll_timer;
102 /*
103 * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
104 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
105 */
106 static unsigned long long poll_timeout = 250000;
107
108 /* Suspend flag */
109 static int ap_suspend_flag;
110 /* Maximum domain id */
111 static int ap_max_domain_id;
112 /*
113 * Flag to check if domain was set through module parameter domain=. This is
114 * important when supsend and resume is done in a z/VM environment where the
115 * domain might change.
116 */
117 static int user_set_domain;
118 static struct bus_type ap_bus_type;
119
120 /* Adapter interrupt definitions */
121 static void ap_interrupt_handler(struct airq_struct *airq);
122
123 static int ap_airq_flag;
124
125 static struct airq_struct ap_airq = {
126 .handler = ap_interrupt_handler,
127 .isc = AP_ISC,
128 };
129
130 /**
131 * ap_using_interrupts() - Returns non-zero if interrupt support is
132 * available.
133 */
134 static inline int ap_using_interrupts(void)
135 {
136 return ap_airq_flag;
137 }
138
139 /**
140 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
141 *
142 * Returns the address of the local-summary-indicator of the adapter
143 * interrupt handler for AP, or NULL if adapter interrupts are not
144 * available.
145 */
146 void *ap_airq_ptr(void)
147 {
148 if (ap_using_interrupts())
149 return ap_airq.lsi_ptr;
150 return NULL;
151 }
152
153 /**
154 * ap_interrupts_available(): Test if AP interrupts are available.
155 *
156 * Returns 1 if AP interrupts are available.
157 */
158 static int ap_interrupts_available(void)
159 {
160 return test_facility(65);
161 }
162
163 /**
164 * ap_configuration_available(): Test if AP configuration
165 * information is available.
166 *
167 * Returns 1 if AP configuration information is available.
168 */
169 static int ap_configuration_available(void)
170 {
171 return test_facility(12);
172 }
173
174 /**
175 * ap_apft_available(): Test if AP facilities test (APFT)
176 * facility is available.
177 *
178 * Returns 1 if APFT is is available.
179 */
180 static int ap_apft_available(void)
181 {
182 return test_facility(15);
183 }
184
185 /*
186 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
187 *
188 * Returns 1 if the QACT subfunction is available.
189 */
190 static inline int ap_qact_available(void)
191 {
192 if (ap_configuration)
193 return ap_configuration->qact;
194 return 0;
195 }
196
197 /*
198 * ap_query_configuration(): Fetch cryptographic config info
199 *
200 * Returns the ap configuration info fetched via PQAP(QCI).
201 * On success 0 is returned, on failure a negative errno
202 * is returned, e.g. if the PQAP(QCI) instruction is not
203 * available, the return value will be -EOPNOTSUPP.
204 */
205 static inline int ap_query_configuration(struct ap_config_info *info)
206 {
207 if (!ap_configuration_available())
208 return -EOPNOTSUPP;
209 if (!info)
210 return -EINVAL;
211 return ap_qci(info);
212 }
213 EXPORT_SYMBOL(ap_query_configuration);
214
215 /**
216 * ap_init_configuration(): Allocate and query configuration array.
217 */
218 static void ap_init_configuration(void)
219 {
220 if (!ap_configuration_available())
221 return;
222
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
225 return;
226 if (ap_query_configuration(ap_configuration) != 0) {
227 kfree(ap_configuration);
228 ap_configuration = NULL;
229 return;
230 }
231 }
232
233 /*
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
236 */
237 static inline int ap_test_config(unsigned int *field, unsigned int nr)
238 {
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
240 }
241
242 /*
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
244 * @id AP card ID
245 *
246 * Returns 0 if the card is not configured
247 * 1 if the card is configured or
248 * if the configuration information is not available
249 */
250 static inline int ap_test_config_card_id(unsigned int id)
251 {
252 if (!ap_configuration) /* QCI not supported */
253 /* only ids 0...3F may be probed */
254 return id < 0x40 ? 1 : 0;
255 return ap_test_config(ap_configuration->apm, id);
256 }
257
258 /*
259 * ap_test_config_usage_domain(): Test, whether an AP usage domain
260 * is configured.
261 * @domain AP usage domain ID
262 *
263 * Returns 0 if the usage domain is not configured
264 * 1 if the usage domain is configured or
265 * if the configuration information is not available
266 */
267 int ap_test_config_usage_domain(unsigned int domain)
268 {
269 if (!ap_configuration) /* QCI not supported */
270 return domain < 16;
271 return ap_test_config(ap_configuration->aqm, domain);
272 }
273 EXPORT_SYMBOL(ap_test_config_usage_domain);
274
275 /*
276 * ap_test_config_ctrl_domain(): Test, whether an AP control domain
277 * is configured.
278 * @domain AP control domain ID
279 *
280 * Returns 1 if the control domain is configured
281 * 0 in all other cases
282 */
283 int ap_test_config_ctrl_domain(unsigned int domain)
284 {
285 if (!ap_configuration) /* QCI not supported */
286 return 0;
287 return ap_test_config(ap_configuration->adm, domain);
288 }
289 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
290
291 /**
292 * ap_query_queue(): Check if an AP queue is available.
293 * @qid: The AP queue number
294 * @queue_depth: Pointer to queue depth value
295 * @device_type: Pointer to device type value
296 * @facilities: Pointer to facility indicator
297 */
298 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
299 unsigned int *facilities)
300 {
301 struct ap_queue_status status;
302 unsigned long info;
303 int nd;
304
305 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
306 return -ENODEV;
307
308 status = ap_test_queue(qid, ap_apft_available(), &info);
309 switch (status.response_code) {
310 case AP_RESPONSE_NORMAL:
311 *queue_depth = (int)(info & 0xff);
312 *device_type = (int)((info >> 24) & 0xff);
313 *facilities = (unsigned int)(info >> 32);
314 /* Update maximum domain id */
315 nd = (info >> 16) & 0xff;
316 /* if N bit is available, z13 and newer */
317 if ((info & (1UL << 57)) && nd > 0)
318 ap_max_domain_id = nd;
319 else /* older machine types */
320 ap_max_domain_id = 15;
321 switch (*device_type) {
322 /* For CEX2 and CEX3 the available functions
323 * are not refrected by the facilities bits.
324 * Instead it is coded into the type. So here
325 * modify the function bits based on the type.
326 */
327 case AP_DEVICE_TYPE_CEX2A:
328 case AP_DEVICE_TYPE_CEX3A:
329 *facilities |= 0x08000000;
330 break;
331 case AP_DEVICE_TYPE_CEX2C:
332 case AP_DEVICE_TYPE_CEX3C:
333 *facilities |= 0x10000000;
334 break;
335 default:
336 break;
337 }
338 return 0;
339 case AP_RESPONSE_Q_NOT_AVAIL:
340 case AP_RESPONSE_DECONFIGURED:
341 case AP_RESPONSE_CHECKSTOPPED:
342 case AP_RESPONSE_INVALID_ADDRESS:
343 return -ENODEV;
344 case AP_RESPONSE_RESET_IN_PROGRESS:
345 case AP_RESPONSE_OTHERWISE_CHANGED:
346 case AP_RESPONSE_BUSY:
347 return -EBUSY;
348 default:
349 BUG();
350 }
351 }
352
353 void ap_wait(enum ap_wait wait)
354 {
355 ktime_t hr_time;
356
357 switch (wait) {
358 case AP_WAIT_AGAIN:
359 case AP_WAIT_INTERRUPT:
360 if (ap_using_interrupts())
361 break;
362 if (ap_poll_kthread) {
363 wake_up(&ap_poll_wait);
364 break;
365 }
366 /* Fall through */
367 case AP_WAIT_TIMEOUT:
368 spin_lock_bh(&ap_poll_timer_lock);
369 if (!hrtimer_is_queued(&ap_poll_timer)) {
370 hr_time = poll_timeout;
371 hrtimer_forward_now(&ap_poll_timer, hr_time);
372 hrtimer_restart(&ap_poll_timer);
373 }
374 spin_unlock_bh(&ap_poll_timer_lock);
375 break;
376 case AP_WAIT_NONE:
377 default:
378 break;
379 }
380 }
381
382 /**
383 * ap_request_timeout(): Handling of request timeouts
384 * @t: timer making this callback
385 *
386 * Handles request timeouts.
387 */
388 void ap_request_timeout(struct timer_list *t)
389 {
390 struct ap_queue *aq = from_timer(aq, t, timeout);
391
392 if (ap_suspend_flag)
393 return;
394 spin_lock_bh(&aq->lock);
395 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
396 spin_unlock_bh(&aq->lock);
397 }
398
399 /**
400 * ap_poll_timeout(): AP receive polling for finished AP requests.
401 * @unused: Unused pointer.
402 *
403 * Schedules the AP tasklet using a high resolution timer.
404 */
405 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
406 {
407 if (!ap_suspend_flag)
408 tasklet_schedule(&ap_tasklet);
409 return HRTIMER_NORESTART;
410 }
411
412 /**
413 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
414 * @airq: pointer to adapter interrupt descriptor
415 */
416 static void ap_interrupt_handler(struct airq_struct *airq)
417 {
418 inc_irq_stat(IRQIO_APB);
419 if (!ap_suspend_flag)
420 tasklet_schedule(&ap_tasklet);
421 }
422
423 /**
424 * ap_tasklet_fn(): Tasklet to poll all AP devices.
425 * @dummy: Unused variable
426 *
427 * Poll all AP devices on the bus.
428 */
429 static void ap_tasklet_fn(unsigned long dummy)
430 {
431 struct ap_card *ac;
432 struct ap_queue *aq;
433 enum ap_wait wait = AP_WAIT_NONE;
434
435 /* Reset the indicator if interrupts are used. Thus new interrupts can
436 * be received. Doing it in the beginning of the tasklet is therefor
437 * important that no requests on any AP get lost.
438 */
439 if (ap_using_interrupts())
440 xchg(ap_airq.lsi_ptr, 0);
441
442 spin_lock_bh(&ap_list_lock);
443 for_each_ap_card(ac) {
444 for_each_ap_queue(aq, ac) {
445 spin_lock_bh(&aq->lock);
446 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
447 spin_unlock_bh(&aq->lock);
448 }
449 }
450 spin_unlock_bh(&ap_list_lock);
451
452 ap_wait(wait);
453 }
454
455 static int ap_pending_requests(void)
456 {
457 struct ap_card *ac;
458 struct ap_queue *aq;
459
460 spin_lock_bh(&ap_list_lock);
461 for_each_ap_card(ac) {
462 for_each_ap_queue(aq, ac) {
463 if (aq->queue_count == 0)
464 continue;
465 spin_unlock_bh(&ap_list_lock);
466 return 1;
467 }
468 }
469 spin_unlock_bh(&ap_list_lock);
470 return 0;
471 }
472
473 /**
474 * ap_poll_thread(): Thread that polls for finished requests.
475 * @data: Unused pointer
476 *
477 * AP bus poll thread. The purpose of this thread is to poll for
478 * finished requests in a loop if there is a "free" cpu - that is
479 * a cpu that doesn't have anything better to do. The polling stops
480 * as soon as there is another task or if all messages have been
481 * delivered.
482 */
483 static int ap_poll_thread(void *data)
484 {
485 DECLARE_WAITQUEUE(wait, current);
486
487 set_user_nice(current, MAX_NICE);
488 set_freezable();
489 while (!kthread_should_stop()) {
490 add_wait_queue(&ap_poll_wait, &wait);
491 set_current_state(TASK_INTERRUPTIBLE);
492 if (ap_suspend_flag || !ap_pending_requests()) {
493 schedule();
494 try_to_freeze();
495 }
496 set_current_state(TASK_RUNNING);
497 remove_wait_queue(&ap_poll_wait, &wait);
498 if (need_resched()) {
499 schedule();
500 try_to_freeze();
501 continue;
502 }
503 ap_tasklet_fn(0);
504 }
505
506 return 0;
507 }
508
509 static int ap_poll_thread_start(void)
510 {
511 int rc;
512
513 if (ap_using_interrupts() || ap_poll_kthread)
514 return 0;
515 mutex_lock(&ap_poll_thread_mutex);
516 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
517 rc = PTR_RET(ap_poll_kthread);
518 if (rc)
519 ap_poll_kthread = NULL;
520 mutex_unlock(&ap_poll_thread_mutex);
521 return rc;
522 }
523
524 static void ap_poll_thread_stop(void)
525 {
526 if (!ap_poll_kthread)
527 return;
528 mutex_lock(&ap_poll_thread_mutex);
529 kthread_stop(ap_poll_kthread);
530 ap_poll_kthread = NULL;
531 mutex_unlock(&ap_poll_thread_mutex);
532 }
533
534 #define is_card_dev(x) ((x)->parent == ap_root_device)
535 #define is_queue_dev(x) ((x)->parent != ap_root_device)
536
537 /**
538 * ap_bus_match()
539 * @dev: Pointer to device
540 * @drv: Pointer to device_driver
541 *
542 * AP bus driver registration/unregistration.
543 */
544 static int ap_bus_match(struct device *dev, struct device_driver *drv)
545 {
546 struct ap_driver *ap_drv = to_ap_drv(drv);
547 struct ap_device_id *id;
548
549 /*
550 * Compare device type of the device with the list of
551 * supported types of the device_driver.
552 */
553 for (id = ap_drv->ids; id->match_flags; id++) {
554 if (is_card_dev(dev) &&
555 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
556 id->dev_type == to_ap_dev(dev)->device_type)
557 return 1;
558 if (is_queue_dev(dev) &&
559 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
560 id->dev_type == to_ap_dev(dev)->device_type)
561 return 1;
562 }
563 return 0;
564 }
565
566 /**
567 * ap_uevent(): Uevent function for AP devices.
568 * @dev: Pointer to device
569 * @env: Pointer to kobj_uevent_env
570 *
571 * It sets up a single environment variable DEV_TYPE which contains the
572 * hardware device type.
573 */
574 static int ap_uevent(struct device *dev, struct kobj_uevent_env *env)
575 {
576 struct ap_device *ap_dev = to_ap_dev(dev);
577 int retval = 0;
578
579 if (!ap_dev)
580 return -ENODEV;
581
582 /* Set up DEV_TYPE environment variable. */
583 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
584 if (retval)
585 return retval;
586
587 /* Add MODALIAS= */
588 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
589
590 return retval;
591 }
592
593 static int ap_dev_suspend(struct device *dev)
594 {
595 struct ap_device *ap_dev = to_ap_dev(dev);
596
597 if (ap_dev->drv && ap_dev->drv->suspend)
598 ap_dev->drv->suspend(ap_dev);
599 return 0;
600 }
601
602 static int ap_dev_resume(struct device *dev)
603 {
604 struct ap_device *ap_dev = to_ap_dev(dev);
605
606 if (ap_dev->drv && ap_dev->drv->resume)
607 ap_dev->drv->resume(ap_dev);
608 return 0;
609 }
610
611 static void ap_bus_suspend(void)
612 {
613 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
614
615 ap_suspend_flag = 1;
616 /*
617 * Disable scanning for devices, thus we do not want to scan
618 * for them after removing.
619 */
620 flush_work(&ap_scan_work);
621 tasklet_disable(&ap_tasklet);
622 }
623
624 static int __ap_card_devices_unregister(struct device *dev, void *dummy)
625 {
626 if (is_card_dev(dev))
627 device_unregister(dev);
628 return 0;
629 }
630
631 static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
632 {
633 if (is_queue_dev(dev))
634 device_unregister(dev);
635 return 0;
636 }
637
638 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
639 {
640 if (is_queue_dev(dev) &&
641 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
642 device_unregister(dev);
643 return 0;
644 }
645
646 static void ap_bus_resume(void)
647 {
648 int rc;
649
650 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
651
652 /* remove all queue devices */
653 bus_for_each_dev(&ap_bus_type, NULL, NULL,
654 __ap_queue_devices_unregister);
655 /* remove all card devices */
656 bus_for_each_dev(&ap_bus_type, NULL, NULL,
657 __ap_card_devices_unregister);
658
659 /* Reset thin interrupt setting */
660 if (ap_interrupts_available() && !ap_using_interrupts()) {
661 rc = register_adapter_interrupt(&ap_airq);
662 ap_airq_flag = (rc == 0);
663 }
664 if (!ap_interrupts_available() && ap_using_interrupts()) {
665 unregister_adapter_interrupt(&ap_airq);
666 ap_airq_flag = 0;
667 }
668 /* Reset domain */
669 if (!user_set_domain)
670 ap_domain_index = -1;
671 /* Get things going again */
672 ap_suspend_flag = 0;
673 if (ap_airq_flag)
674 xchg(ap_airq.lsi_ptr, 0);
675 tasklet_enable(&ap_tasklet);
676 queue_work(system_long_wq, &ap_scan_work);
677 }
678
679 static int ap_power_event(struct notifier_block *this, unsigned long event,
680 void *ptr)
681 {
682 switch (event) {
683 case PM_HIBERNATION_PREPARE:
684 case PM_SUSPEND_PREPARE:
685 ap_bus_suspend();
686 break;
687 case PM_POST_HIBERNATION:
688 case PM_POST_SUSPEND:
689 ap_bus_resume();
690 break;
691 default:
692 break;
693 }
694 return NOTIFY_DONE;
695 }
696 static struct notifier_block ap_power_notifier = {
697 .notifier_call = ap_power_event,
698 };
699
700 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
701
702 static struct bus_type ap_bus_type = {
703 .name = "ap",
704 .match = &ap_bus_match,
705 .uevent = &ap_uevent,
706 .pm = &ap_bus_pm_ops,
707 };
708
709 static int __ap_revise_reserved(struct device *dev, void *dummy)
710 {
711 int rc, card, queue, devres, drvres;
712
713 if (is_queue_dev(dev)) {
714 card = AP_QID_CARD(to_ap_queue(dev)->qid);
715 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
716 mutex_lock(&ap_perms_mutex);
717 devres = test_bit_inv(card, ap_perms.apm)
718 && test_bit_inv(queue, ap_perms.aqm);
719 mutex_unlock(&ap_perms_mutex);
720 drvres = to_ap_drv(dev->driver)->flags
721 & AP_DRIVER_FLAG_DEFAULT;
722 if (!!devres != !!drvres) {
723 AP_DBF(DBF_DEBUG, "reprobing queue=%02x.%04x\n",
724 card, queue);
725 rc = device_reprobe(dev);
726 }
727 }
728
729 return 0;
730 }
731
732 static void ap_bus_revise_bindings(void)
733 {
734 bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
735 }
736
737 int ap_owned_by_def_drv(int card, int queue)
738 {
739 int rc = 0;
740
741 if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
742 return -EINVAL;
743
744 mutex_lock(&ap_perms_mutex);
745
746 if (test_bit_inv(card, ap_perms.apm)
747 && test_bit_inv(queue, ap_perms.aqm))
748 rc = 1;
749
750 mutex_unlock(&ap_perms_mutex);
751
752 return rc;
753 }
754 EXPORT_SYMBOL(ap_owned_by_def_drv);
755
756 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
757 unsigned long *aqm)
758 {
759 int card, queue, rc = 0;
760
761 mutex_lock(&ap_perms_mutex);
762
763 for (card = 0; !rc && card < AP_DEVICES; card++)
764 if (test_bit_inv(card, apm) &&
765 test_bit_inv(card, ap_perms.apm))
766 for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
767 if (test_bit_inv(queue, aqm) &&
768 test_bit_inv(queue, ap_perms.aqm))
769 rc = 1;
770
771 mutex_unlock(&ap_perms_mutex);
772
773 return rc;
774 }
775 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
776
777 static int ap_device_probe(struct device *dev)
778 {
779 struct ap_device *ap_dev = to_ap_dev(dev);
780 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
781 int card, queue, devres, drvres, rc;
782
783 if (is_queue_dev(dev)) {
784 /*
785 * If the apqn is marked as reserved/used by ap bus and
786 * default drivers, only probe with drivers with the default
787 * flag set. If it is not marked, only probe with drivers
788 * with the default flag not set.
789 */
790 card = AP_QID_CARD(to_ap_queue(dev)->qid);
791 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
792 mutex_lock(&ap_perms_mutex);
793 devres = test_bit_inv(card, ap_perms.apm)
794 && test_bit_inv(queue, ap_perms.aqm);
795 mutex_unlock(&ap_perms_mutex);
796 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
797 if (!!devres != !!drvres)
798 return -ENODEV;
799 /* (re-)init queue's state machine */
800 ap_queue_reinit_state(to_ap_queue(dev));
801 }
802
803 /* Add queue/card to list of active queues/cards */
804 spin_lock_bh(&ap_list_lock);
805 if (is_card_dev(dev))
806 list_add(&to_ap_card(dev)->list, &ap_card_list);
807 else
808 list_add(&to_ap_queue(dev)->list,
809 &to_ap_queue(dev)->card->queues);
810 spin_unlock_bh(&ap_list_lock);
811
812 ap_dev->drv = ap_drv;
813 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
814
815 if (rc) {
816 spin_lock_bh(&ap_list_lock);
817 if (is_card_dev(dev))
818 list_del_init(&to_ap_card(dev)->list);
819 else
820 list_del_init(&to_ap_queue(dev)->list);
821 spin_unlock_bh(&ap_list_lock);
822 ap_dev->drv = NULL;
823 }
824
825 return rc;
826 }
827
828 static int ap_device_remove(struct device *dev)
829 {
830 struct ap_device *ap_dev = to_ap_dev(dev);
831 struct ap_driver *ap_drv = ap_dev->drv;
832
833 if (is_queue_dev(dev))
834 ap_queue_remove(to_ap_queue(dev));
835 if (ap_drv->remove)
836 ap_drv->remove(ap_dev);
837
838 /* Remove queue/card from list of active queues/cards */
839 spin_lock_bh(&ap_list_lock);
840 if (is_card_dev(dev))
841 list_del_init(&to_ap_card(dev)->list);
842 else
843 list_del_init(&to_ap_queue(dev)->list);
844 spin_unlock_bh(&ap_list_lock);
845
846 return 0;
847 }
848
849 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
850 char *name)
851 {
852 struct device_driver *drv = &ap_drv->driver;
853
854 if (!initialised)
855 return -ENODEV;
856
857 drv->bus = &ap_bus_type;
858 drv->probe = ap_device_probe;
859 drv->remove = ap_device_remove;
860 drv->owner = owner;
861 drv->name = name;
862 return driver_register(drv);
863 }
864 EXPORT_SYMBOL(ap_driver_register);
865
866 void ap_driver_unregister(struct ap_driver *ap_drv)
867 {
868 driver_unregister(&ap_drv->driver);
869 }
870 EXPORT_SYMBOL(ap_driver_unregister);
871
872 void ap_bus_force_rescan(void)
873 {
874 if (ap_suspend_flag)
875 return;
876 /* processing a asynchronous bus rescan */
877 del_timer(&ap_config_timer);
878 queue_work(system_long_wq, &ap_scan_work);
879 flush_work(&ap_scan_work);
880 }
881 EXPORT_SYMBOL(ap_bus_force_rescan);
882
883 /*
884 * hex2bitmap() - parse hex mask string and set bitmap.
885 * Valid strings are "0x012345678" with at least one valid hex number.
886 * Rest of the bitmap to the right is padded with 0. No spaces allowed
887 * within the string, the leading 0x may be omitted.
888 * Returns the bitmask with exactly the bits set as given by the hex
889 * string (both in big endian order).
890 */
891 static int hex2bitmap(const char *str, unsigned long *bitmap, int bits)
892 {
893 int i, n, b;
894
895 /* bits needs to be a multiple of 8 */
896 if (bits & 0x07)
897 return -EINVAL;
898
899 if (str[0] == '0' && str[1] == 'x')
900 str++;
901 if (*str == 'x')
902 str++;
903
904 for (i = 0; isxdigit(*str) && i < bits; str++) {
905 b = hex_to_bin(*str);
906 for (n = 0; n < 4; n++)
907 if (b & (0x08 >> n))
908 set_bit_inv(i + n, bitmap);
909 i += 4;
910 }
911
912 if (*str == '\n')
913 str++;
914 if (*str)
915 return -EINVAL;
916 return 0;
917 }
918
919 /*
920 * modify_bitmap() - parse bitmask argument and modify an existing
921 * bit mask accordingly. A concatenation (done with ',') of these
922 * terms is recognized:
923 * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
924 * <bitnr> may be any valid number (hex, decimal or octal) in the range
925 * 0...bits-1; the leading + or - is required. Here are some examples:
926 * +0-15,+32,-128,-0xFF
927 * -0-255,+1-16,+0x128
928 * +1,+2,+3,+4,-5,-7-10
929 * Returns the new bitmap after all changes have been applied. Every
930 * positive value in the string will set a bit and every negative value
931 * in the string will clear a bit. As a bit may be touched more than once,
932 * the last 'operation' wins:
933 * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
934 * cleared again. All other bits are unmodified.
935 */
936 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
937 {
938 int a, i, z;
939 char *np, sign;
940
941 /* bits needs to be a multiple of 8 */
942 if (bits & 0x07)
943 return -EINVAL;
944
945 while (*str) {
946 sign = *str++;
947 if (sign != '+' && sign != '-')
948 return -EINVAL;
949 a = z = simple_strtoul(str, &np, 0);
950 if (str == np || a >= bits)
951 return -EINVAL;
952 str = np;
953 if (*str == '-') {
954 z = simple_strtoul(++str, &np, 0);
955 if (str == np || a > z || z >= bits)
956 return -EINVAL;
957 str = np;
958 }
959 for (i = a; i <= z; i++)
960 if (sign == '+')
961 set_bit_inv(i, bitmap);
962 else
963 clear_bit_inv(i, bitmap);
964 while (*str == ',' || *str == '\n')
965 str++;
966 }
967
968 return 0;
969 }
970
971 /*
972 * process_mask_arg() - parse a bitmap string and clear/set the
973 * bits in the bitmap accordingly. The string may be given as
974 * absolute value, a hex string like 0x1F2E3D4C5B6A" simple over-
975 * writing the current content of the bitmap. Or as relative string
976 * like "+1-16,-32,-0x40,+128" where only single bits or ranges of
977 * bits are cleared or set. Distinction is done based on the very
978 * first character which may be '+' or '-' for the relative string
979 * and othewise assume to be an absolute value string. If parsing fails
980 * a negative errno value is returned. All arguments and bitmaps are
981 * big endian order.
982 */
983 static int process_mask_arg(const char *str,
984 unsigned long *bitmap, int bits,
985 struct mutex *lock)
986 {
987 unsigned long *newmap, size;
988 int rc;
989
990 /* bits needs to be a multiple of 8 */
991 if (bits & 0x07)
992 return -EINVAL;
993
994 size = BITS_TO_LONGS(bits)*sizeof(unsigned long);
995 newmap = kmalloc(size, GFP_KERNEL);
996 if (!newmap)
997 return -ENOMEM;
998 if (mutex_lock_interruptible(lock)) {
999 kfree(newmap);
1000 return -ERESTARTSYS;
1001 }
1002
1003 if (*str == '+' || *str == '-') {
1004 memcpy(newmap, bitmap, size);
1005 rc = modify_bitmap(str, newmap, bits);
1006 } else {
1007 memset(newmap, 0, size);
1008 rc = hex2bitmap(str, newmap, bits);
1009 }
1010 if (rc == 0)
1011 memcpy(bitmap, newmap, size);
1012 mutex_unlock(lock);
1013 kfree(newmap);
1014 return rc;
1015 }
1016
1017 /*
1018 * AP bus attributes.
1019 */
1020
1021 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
1022 {
1023 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
1024 }
1025
1026 static ssize_t ap_domain_store(struct bus_type *bus,
1027 const char *buf, size_t count)
1028 {
1029 int domain;
1030
1031 if (sscanf(buf, "%i\n", &domain) != 1 ||
1032 domain < 0 || domain > ap_max_domain_id ||
1033 !test_bit_inv(domain, ap_perms.aqm))
1034 return -EINVAL;
1035 spin_lock_bh(&ap_domain_lock);
1036 ap_domain_index = domain;
1037 spin_unlock_bh(&ap_domain_lock);
1038
1039 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
1040
1041 return count;
1042 }
1043
1044 static BUS_ATTR_RW(ap_domain);
1045
1046 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
1047 {
1048 if (!ap_configuration) /* QCI not supported */
1049 return snprintf(buf, PAGE_SIZE, "not supported\n");
1050
1051 return snprintf(buf, PAGE_SIZE,
1052 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1053 ap_configuration->adm[0], ap_configuration->adm[1],
1054 ap_configuration->adm[2], ap_configuration->adm[3],
1055 ap_configuration->adm[4], ap_configuration->adm[5],
1056 ap_configuration->adm[6], ap_configuration->adm[7]);
1057 }
1058
1059 static BUS_ATTR_RO(ap_control_domain_mask);
1060
1061 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
1062 {
1063 if (!ap_configuration) /* QCI not supported */
1064 return snprintf(buf, PAGE_SIZE, "not supported\n");
1065
1066 return snprintf(buf, PAGE_SIZE,
1067 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1068 ap_configuration->aqm[0], ap_configuration->aqm[1],
1069 ap_configuration->aqm[2], ap_configuration->aqm[3],
1070 ap_configuration->aqm[4], ap_configuration->aqm[5],
1071 ap_configuration->aqm[6], ap_configuration->aqm[7]);
1072 }
1073
1074 static BUS_ATTR_RO(ap_usage_domain_mask);
1075
1076 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
1077 {
1078 return snprintf(buf, PAGE_SIZE, "%d\n",
1079 ap_using_interrupts() ? 1 : 0);
1080 }
1081
1082 static BUS_ATTR_RO(ap_interrupts);
1083
1084 static ssize_t config_time_show(struct bus_type *bus, char *buf)
1085 {
1086 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
1087 }
1088
1089 static ssize_t config_time_store(struct bus_type *bus,
1090 const char *buf, size_t count)
1091 {
1092 int time;
1093
1094 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1095 return -EINVAL;
1096 ap_config_time = time;
1097 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1098 return count;
1099 }
1100
1101 static BUS_ATTR_RW(config_time);
1102
1103 static ssize_t poll_thread_show(struct bus_type *bus, char *buf)
1104 {
1105 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
1106 }
1107
1108 static ssize_t poll_thread_store(struct bus_type *bus,
1109 const char *buf, size_t count)
1110 {
1111 int flag, rc;
1112
1113 if (sscanf(buf, "%d\n", &flag) != 1)
1114 return -EINVAL;
1115 if (flag) {
1116 rc = ap_poll_thread_start();
1117 if (rc)
1118 count = rc;
1119 } else
1120 ap_poll_thread_stop();
1121 return count;
1122 }
1123
1124 static BUS_ATTR_RW(poll_thread);
1125
1126 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
1127 {
1128 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
1129 }
1130
1131 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
1132 size_t count)
1133 {
1134 unsigned long long time;
1135 ktime_t hr_time;
1136
1137 /* 120 seconds = maximum poll interval */
1138 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
1139 time > 120000000000ULL)
1140 return -EINVAL;
1141 poll_timeout = time;
1142 hr_time = poll_timeout;
1143
1144 spin_lock_bh(&ap_poll_timer_lock);
1145 hrtimer_cancel(&ap_poll_timer);
1146 hrtimer_set_expires(&ap_poll_timer, hr_time);
1147 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1148 spin_unlock_bh(&ap_poll_timer_lock);
1149
1150 return count;
1151 }
1152
1153 static BUS_ATTR_RW(poll_timeout);
1154
1155 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
1156 {
1157 int max_domain_id;
1158
1159 if (ap_configuration)
1160 max_domain_id = ap_max_domain_id ? : -1;
1161 else
1162 max_domain_id = 15;
1163 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
1164 }
1165
1166 static BUS_ATTR_RO(ap_max_domain_id);
1167
1168 static ssize_t apmask_show(struct bus_type *bus, char *buf)
1169 {
1170 int rc;
1171
1172 if (mutex_lock_interruptible(&ap_perms_mutex))
1173 return -ERESTARTSYS;
1174 rc = snprintf(buf, PAGE_SIZE,
1175 "0x%016lx%016lx%016lx%016lx\n",
1176 ap_perms.apm[0], ap_perms.apm[1],
1177 ap_perms.apm[2], ap_perms.apm[3]);
1178 mutex_unlock(&ap_perms_mutex);
1179
1180 return rc;
1181 }
1182
1183 static ssize_t apmask_store(struct bus_type *bus, const char *buf,
1184 size_t count)
1185 {
1186 int rc;
1187
1188 rc = process_mask_arg(buf, ap_perms.apm, AP_DEVICES, &ap_perms_mutex);
1189 if (rc)
1190 return rc;
1191
1192 ap_bus_revise_bindings();
1193
1194 return count;
1195 }
1196
1197 static BUS_ATTR_RW(apmask);
1198
1199 static ssize_t aqmask_show(struct bus_type *bus, char *buf)
1200 {
1201 int rc;
1202
1203 if (mutex_lock_interruptible(&ap_perms_mutex))
1204 return -ERESTARTSYS;
1205 rc = snprintf(buf, PAGE_SIZE,
1206 "0x%016lx%016lx%016lx%016lx\n",
1207 ap_perms.aqm[0], ap_perms.aqm[1],
1208 ap_perms.aqm[2], ap_perms.aqm[3]);
1209 mutex_unlock(&ap_perms_mutex);
1210
1211 return rc;
1212 }
1213
1214 static ssize_t aqmask_store(struct bus_type *bus, const char *buf,
1215 size_t count)
1216 {
1217 int rc;
1218
1219 rc = process_mask_arg(buf, ap_perms.aqm, AP_DOMAINS, &ap_perms_mutex);
1220 if (rc)
1221 return rc;
1222
1223 ap_bus_revise_bindings();
1224
1225 return count;
1226 }
1227
1228 static BUS_ATTR_RW(aqmask);
1229
1230 static struct bus_attribute *const ap_bus_attrs[] = {
1231 &bus_attr_ap_domain,
1232 &bus_attr_ap_control_domain_mask,
1233 &bus_attr_ap_usage_domain_mask,
1234 &bus_attr_config_time,
1235 &bus_attr_poll_thread,
1236 &bus_attr_ap_interrupts,
1237 &bus_attr_poll_timeout,
1238 &bus_attr_ap_max_domain_id,
1239 &bus_attr_apmask,
1240 &bus_attr_aqmask,
1241 NULL,
1242 };
1243
1244 /**
1245 * ap_select_domain(): Select an AP domain if possible and we haven't
1246 * already done so before.
1247 */
1248 static void ap_select_domain(void)
1249 {
1250 int count, max_count, best_domain;
1251 struct ap_queue_status status;
1252 int i, j;
1253
1254 /*
1255 * We want to use a single domain. Either the one specified with
1256 * the "domain=" parameter or the domain with the maximum number
1257 * of devices.
1258 */
1259 spin_lock_bh(&ap_domain_lock);
1260 if (ap_domain_index >= 0) {
1261 /* Domain has already been selected. */
1262 spin_unlock_bh(&ap_domain_lock);
1263 return;
1264 }
1265 best_domain = -1;
1266 max_count = 0;
1267 for (i = 0; i < AP_DOMAINS; i++) {
1268 if (!ap_test_config_usage_domain(i) ||
1269 !test_bit_inv(i, ap_perms.aqm))
1270 continue;
1271 count = 0;
1272 for (j = 0; j < AP_DEVICES; j++) {
1273 if (!ap_test_config_card_id(j))
1274 continue;
1275 status = ap_test_queue(AP_MKQID(j, i),
1276 ap_apft_available(),
1277 NULL);
1278 if (status.response_code != AP_RESPONSE_NORMAL)
1279 continue;
1280 count++;
1281 }
1282 if (count > max_count) {
1283 max_count = count;
1284 best_domain = i;
1285 }
1286 }
1287 if (best_domain >= 0) {
1288 ap_domain_index = best_domain;
1289 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
1290 }
1291 spin_unlock_bh(&ap_domain_lock);
1292 }
1293
1294 /*
1295 * This function checks the type and returns either 0 for not
1296 * supported or the highest compatible type value (which may
1297 * include the input type value).
1298 */
1299 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1300 {
1301 int comp_type = 0;
1302
1303 /* < CEX2A is not supported */
1304 if (rawtype < AP_DEVICE_TYPE_CEX2A)
1305 return 0;
1306 /* up to CEX6 known and fully supported */
1307 if (rawtype <= AP_DEVICE_TYPE_CEX6)
1308 return rawtype;
1309 /*
1310 * unknown new type > CEX6, check for compatibility
1311 * to the highest known and supported type which is
1312 * currently CEX6 with the help of the QACT function.
1313 */
1314 if (ap_qact_available()) {
1315 struct ap_queue_status status;
1316 union ap_qact_ap_info apinfo = {0};
1317
1318 apinfo.mode = (func >> 26) & 0x07;
1319 apinfo.cat = AP_DEVICE_TYPE_CEX6;
1320 status = ap_qact(qid, 0, &apinfo);
1321 if (status.response_code == AP_RESPONSE_NORMAL
1322 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1323 && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
1324 comp_type = apinfo.cat;
1325 }
1326 if (!comp_type)
1327 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1328 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1329 else if (comp_type != rawtype)
1330 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1331 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1332 return comp_type;
1333 }
1334
1335 /*
1336 * helper function to be used with bus_find_dev
1337 * matches for the card device with the given id
1338 */
1339 static int __match_card_device_with_id(struct device *dev, void *data)
1340 {
1341 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
1342 }
1343
1344 /* helper function to be used with bus_find_dev
1345 * matches for the queue device with a given qid
1346 */
1347 static int __match_queue_device_with_qid(struct device *dev, void *data)
1348 {
1349 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1350 }
1351
1352 /**
1353 * ap_scan_bus(): Scan the AP bus for new devices
1354 * Runs periodically, workqueue timer (ap_config_time)
1355 */
1356 static void ap_scan_bus(struct work_struct *unused)
1357 {
1358 struct ap_queue *aq;
1359 struct ap_card *ac;
1360 struct device *dev;
1361 ap_qid_t qid;
1362 int comp_type, depth = 0, type = 0;
1363 unsigned int func = 0;
1364 int rc, id, dom, borked, domains, defdomdevs = 0;
1365
1366 AP_DBF(DBF_DEBUG, "%s running\n", __func__);
1367
1368 ap_query_configuration(ap_configuration);
1369 ap_select_domain();
1370
1371 for (id = 0; id < AP_DEVICES; id++) {
1372 /* check if device is registered */
1373 dev = bus_find_device(&ap_bus_type, NULL,
1374 (void *)(long) id,
1375 __match_card_device_with_id);
1376 ac = dev ? to_ap_card(dev) : NULL;
1377 if (!ap_test_config_card_id(id)) {
1378 if (dev) {
1379 /* Card device has been removed from
1380 * configuration, remove the belonging
1381 * queue devices.
1382 */
1383 bus_for_each_dev(&ap_bus_type, NULL,
1384 (void *)(long) id,
1385 __ap_queue_devices_with_id_unregister);
1386 /* now remove the card device */
1387 device_unregister(dev);
1388 put_device(dev);
1389 }
1390 continue;
1391 }
1392 /* According to the configuration there should be a card
1393 * device, so check if there is at least one valid queue
1394 * and maybe create queue devices and the card device.
1395 */
1396 domains = 0;
1397 for (dom = 0; dom < AP_DOMAINS; dom++) {
1398 qid = AP_MKQID(id, dom);
1399 dev = bus_find_device(&ap_bus_type, NULL,
1400 (void *)(long) qid,
1401 __match_queue_device_with_qid);
1402 aq = dev ? to_ap_queue(dev) : NULL;
1403 if (!ap_test_config_usage_domain(dom)) {
1404 if (dev) {
1405 /* Queue device exists but has been
1406 * removed from configuration.
1407 */
1408 device_unregister(dev);
1409 put_device(dev);
1410 }
1411 continue;
1412 }
1413 rc = ap_query_queue(qid, &depth, &type, &func);
1414 if (dev) {
1415 spin_lock_bh(&aq->lock);
1416 if (rc == -ENODEV ||
1417 /* adapter reconfiguration */
1418 (ac && ac->functions != func))
1419 aq->state = AP_STATE_BORKED;
1420 borked = aq->state == AP_STATE_BORKED;
1421 spin_unlock_bh(&aq->lock);
1422 if (borked) /* Remove broken device */
1423 device_unregister(dev);
1424 put_device(dev);
1425 if (!borked) {
1426 domains++;
1427 if (dom == ap_domain_index)
1428 defdomdevs++;
1429 continue;
1430 }
1431 }
1432 if (rc)
1433 continue;
1434 /* a new queue device is needed, check out comp type */
1435 comp_type = ap_get_compatible_type(qid, type, func);
1436 if (!comp_type)
1437 continue;
1438 /* maybe a card device needs to be created first */
1439 if (!ac) {
1440 ac = ap_card_create(id, depth, type,
1441 comp_type, func);
1442 if (!ac)
1443 continue;
1444 ac->ap_dev.device.bus = &ap_bus_type;
1445 ac->ap_dev.device.parent = ap_root_device;
1446 dev_set_name(&ac->ap_dev.device,
1447 "card%02x", id);
1448 /* Register card with AP bus */
1449 rc = device_register(&ac->ap_dev.device);
1450 if (rc) {
1451 put_device(&ac->ap_dev.device);
1452 ac = NULL;
1453 break;
1454 }
1455 /* get it and thus adjust reference counter */
1456 get_device(&ac->ap_dev.device);
1457 }
1458 /* now create the new queue device */
1459 aq = ap_queue_create(qid, comp_type);
1460 if (!aq)
1461 continue;
1462 aq->card = ac;
1463 aq->ap_dev.device.bus = &ap_bus_type;
1464 aq->ap_dev.device.parent = &ac->ap_dev.device;
1465 dev_set_name(&aq->ap_dev.device,
1466 "%02x.%04x", id, dom);
1467 /* Register device */
1468 rc = device_register(&aq->ap_dev.device);
1469 if (rc) {
1470 put_device(&aq->ap_dev.device);
1471 continue;
1472 }
1473 domains++;
1474 if (dom == ap_domain_index)
1475 defdomdevs++;
1476 } /* end domain loop */
1477 if (ac) {
1478 /* remove card dev if there are no queue devices */
1479 if (!domains)
1480 device_unregister(&ac->ap_dev.device);
1481 put_device(&ac->ap_dev.device);
1482 }
1483 } /* end device loop */
1484
1485 if (ap_domain_index >= 0 && defdomdevs < 1)
1486 AP_DBF(DBF_INFO,
1487 "no queue device with default domain %d available\n",
1488 ap_domain_index);
1489
1490 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1491 }
1492
1493 static void ap_config_timeout(struct timer_list *unused)
1494 {
1495 if (ap_suspend_flag)
1496 return;
1497 queue_work(system_long_wq, &ap_scan_work);
1498 }
1499
1500 static void ap_reset_all(void)
1501 {
1502 int i, j;
1503
1504 for (i = 0; i < AP_DOMAINS; i++) {
1505 if (!ap_test_config_usage_domain(i))
1506 continue;
1507 for (j = 0; j < AP_DEVICES; j++) {
1508 if (!ap_test_config_card_id(j))
1509 continue;
1510 ap_rapq(AP_MKQID(j, i));
1511 }
1512 }
1513 }
1514
1515 static struct reset_call ap_reset_call = {
1516 .fn = ap_reset_all,
1517 };
1518
1519 int __init ap_debug_init(void)
1520 {
1521 ap_dbf_info = debug_register("ap", 1, 1,
1522 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1523 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1524 debug_set_level(ap_dbf_info, DBF_ERR);
1525
1526 return 0;
1527 }
1528
1529 static void __init ap_perms_init(void)
1530 {
1531 /* all resources useable if no kernel parameter string given */
1532 memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
1533 memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
1534
1535 /* apm kernel parameter string */
1536 if (apm_str) {
1537 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
1538 process_mask_arg(apm_str, ap_perms.apm, AP_DEVICES,
1539 &ap_perms_mutex);
1540 }
1541
1542 /* aqm kernel parameter string */
1543 if (aqm_str) {
1544 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
1545 process_mask_arg(aqm_str, ap_perms.aqm, AP_DOMAINS,
1546 &ap_perms_mutex);
1547 }
1548 }
1549
1550 /**
1551 * ap_module_init(): The module initialization code.
1552 *
1553 * Initializes the module.
1554 */
1555 int __init ap_module_init(void)
1556 {
1557 int max_domain_id;
1558 int rc, i;
1559
1560 rc = ap_debug_init();
1561 if (rc)
1562 return rc;
1563
1564 if (!ap_instructions_available()) {
1565 pr_warn("The hardware system does not support AP instructions\n");
1566 return -ENODEV;
1567 }
1568
1569 /* set up the AP permissions (ap and aq masks) */
1570 ap_perms_init();
1571
1572 /* Get AP configuration data if available */
1573 ap_init_configuration();
1574
1575 if (ap_configuration)
1576 max_domain_id =
1577 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1578 else
1579 max_domain_id = 15;
1580 if (ap_domain_index < -1 || ap_domain_index > max_domain_id ||
1581 (ap_domain_index >= 0 &&
1582 !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
1583 pr_warn("%d is not a valid cryptographic domain\n",
1584 ap_domain_index);
1585 ap_domain_index = -1;
1586 }
1587 /* In resume callback we need to know if the user had set the domain.
1588 * If so, we can not just reset it.
1589 */
1590 if (ap_domain_index >= 0)
1591 user_set_domain = 1;
1592
1593 if (ap_interrupts_available()) {
1594 rc = register_adapter_interrupt(&ap_airq);
1595 ap_airq_flag = (rc == 0);
1596 }
1597
1598 register_reset_call(&ap_reset_call);
1599
1600 /* Create /sys/bus/ap. */
1601 rc = bus_register(&ap_bus_type);
1602 if (rc)
1603 goto out;
1604 for (i = 0; ap_bus_attrs[i]; i++) {
1605 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1606 if (rc)
1607 goto out_bus;
1608 }
1609
1610 /* Create /sys/devices/ap. */
1611 ap_root_device = root_device_register("ap");
1612 rc = PTR_RET(ap_root_device);
1613 if (rc)
1614 goto out_bus;
1615
1616 /* Setup the AP bus rescan timer. */
1617 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1618
1619 /*
1620 * Setup the high resultion poll timer.
1621 * If we are running under z/VM adjust polling to z/VM polling rate.
1622 */
1623 if (MACHINE_IS_VM)
1624 poll_timeout = 1500000;
1625 spin_lock_init(&ap_poll_timer_lock);
1626 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1627 ap_poll_timer.function = ap_poll_timeout;
1628
1629 /* Start the low priority AP bus poll thread. */
1630 if (ap_thread_flag) {
1631 rc = ap_poll_thread_start();
1632 if (rc)
1633 goto out_work;
1634 }
1635
1636 rc = register_pm_notifier(&ap_power_notifier);
1637 if (rc)
1638 goto out_pm;
1639
1640 queue_work(system_long_wq, &ap_scan_work);
1641 initialised = true;
1642
1643 return 0;
1644
1645 out_pm:
1646 ap_poll_thread_stop();
1647 out_work:
1648 hrtimer_cancel(&ap_poll_timer);
1649 root_device_unregister(ap_root_device);
1650 out_bus:
1651 while (i--)
1652 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1653 bus_unregister(&ap_bus_type);
1654 out:
1655 unregister_reset_call(&ap_reset_call);
1656 if (ap_using_interrupts())
1657 unregister_adapter_interrupt(&ap_airq);
1658 kfree(ap_configuration);
1659 return rc;
1660 }
1661 device_initcall(ap_module_init);