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