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