bd95bd390b5c4d3d7cbf4f36b5c96f571f818192
[linux-2.6-block.git] / drivers / s390 / crypto / ap_bus.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright IBM Corp. 2006, 2023
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  *            Harald Freudenberger <freude@linux.ibm.com>
10  *
11  * Adjunct processor bus.
12  */
13
14 #define KMSG_COMPONENT "ap"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
17 #include <linux/kernel_stat.h>
18 #include <linux/moduleparam.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/freezer.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.h>
26 #include <linux/notifier.h>
27 #include <linux/kthread.h>
28 #include <linux/mutex.h>
29 #include <asm/machine.h>
30 #include <asm/airq.h>
31 #include <asm/tpi.h>
32 #include <linux/atomic.h>
33 #include <asm/isc.h>
34 #include <linux/hrtimer.h>
35 #include <linux/ktime.h>
36 #include <asm/facility.h>
37 #include <linux/crypto.h>
38 #include <linux/mod_devicetable.h>
39 #include <linux/debugfs.h>
40 #include <linux/ctype.h>
41 #include <linux/module.h>
42 #include <asm/uv.h>
43 #include <asm/chsc.h>
44 #include <linux/mempool.h>
45
46 #include "ap_bus.h"
47 #include "ap_debug.h"
48
49 MODULE_AUTHOR("IBM Corporation");
50 MODULE_DESCRIPTION("Adjunct Processor Bus driver");
51 MODULE_LICENSE("GPL");
52
53 int ap_domain_index = -1;       /* Adjunct Processor Domain Index */
54 static DEFINE_SPINLOCK(ap_domain_lock);
55 module_param_named(domain, ap_domain_index, int, 0440);
56 MODULE_PARM_DESC(domain, "domain index for ap devices");
57 EXPORT_SYMBOL(ap_domain_index);
58
59 static int ap_thread_flag;
60 module_param_named(poll_thread, ap_thread_flag, int, 0440);
61 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
62
63 static char *apm_str;
64 module_param_named(apmask, apm_str, charp, 0440);
65 MODULE_PARM_DESC(apmask, "AP bus adapter mask.");
66
67 static char *aqm_str;
68 module_param_named(aqmask, aqm_str, charp, 0440);
69 MODULE_PARM_DESC(aqmask, "AP bus domain mask.");
70
71 static int ap_useirq = 1;
72 module_param_named(useirq, ap_useirq, int, 0440);
73 MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on).");
74
75 atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE);
76 EXPORT_SYMBOL(ap_max_msg_size);
77
78 static struct device *ap_root_device;
79
80 /* Hashtable of all queue devices on the AP bus */
81 DEFINE_HASHTABLE(ap_queues, 8);
82 /* lock used for the ap_queues hashtable */
83 DEFINE_SPINLOCK(ap_queues_lock);
84
85 /* Default permissions (ioctl, card and domain masking) */
86 struct ap_perms ap_perms;
87 EXPORT_SYMBOL(ap_perms);
88 DEFINE_MUTEX(ap_perms_mutex);
89 EXPORT_SYMBOL(ap_perms_mutex);
90
91 /* # of bindings complete since init */
92 static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0);
93
94 /* completion for APQN bindings complete */
95 static DECLARE_COMPLETION(ap_apqn_bindings_complete);
96
97 static struct ap_config_info qci[2];
98 static struct ap_config_info *const ap_qci_info = &qci[0];
99 static struct ap_config_info *const ap_qci_info_old = &qci[1];
100
101 /*
102  * AP bus related debug feature things.
103  */
104 debug_info_t *ap_dbf_info;
105
106 /*
107  * There is a need for a do-not-allocate-memory path through the AP bus
108  * layer. The pkey layer may be triggered via the in-kernel interface from
109  * a protected key crypto algorithm (namely PAES) to convert a secure key
110  * into a protected key. This happens in a workqueue context, so sleeping
111  * is allowed but memory allocations causing IO operations are not permitted.
112  * To accomplish this, an AP message memory pool with pre-allocated space
113  * is established. When ap_init_apmsg() with use_mempool set to true is
114  * called, instead of kmalloc() the ap message buffer is allocated from
115  * the ap_msg_pool. This pool only holds a limited amount of buffers:
116  * ap_msg_pool_min_items with the item size AP_DEFAULT_MAX_MSG_SIZE and
117  * exactly one of these items (if available) is returned if ap_init_apmsg()
118  * with the use_mempool arg set to true is called. When this pool is exhausted
119  * and use_mempool is set true, ap_init_apmsg() returns -ENOMEM without
120  * any attempt to allocate memory and the caller has to deal with that.
121  */
122 static mempool_t *ap_msg_pool;
123 static unsigned int ap_msg_pool_min_items = 8;
124 module_param_named(msgpool_min_items, ap_msg_pool_min_items, uint, 0440);
125 MODULE_PARM_DESC(msgpool_min_items, "AP message pool minimal items");
126
127 /*
128  * AP bus rescan related things.
129  */
130 static bool ap_scan_bus(void);
131 static bool ap_scan_bus_result; /* result of last ap_scan_bus() */
132 static DEFINE_MUTEX(ap_scan_bus_mutex); /* mutex ap_scan_bus() invocations */
133 static struct task_struct *ap_scan_bus_task; /* thread holding the scan mutex */
134 static atomic64_t ap_scan_bus_count; /* counter ap_scan_bus() invocations */
135 static int ap_scan_bus_time = AP_CONFIG_TIME;
136 static struct timer_list ap_scan_bus_timer;
137 static void ap_scan_bus_wq_callback(struct work_struct *);
138 static DECLARE_WORK(ap_scan_bus_work, ap_scan_bus_wq_callback);
139
140 /*
141  * Tasklet & timer for AP request polling and interrupts
142  */
143 static void ap_tasklet_fn(unsigned long);
144 static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn);
145 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
146 static struct task_struct *ap_poll_kthread;
147 static DEFINE_MUTEX(ap_poll_thread_mutex);
148 static DEFINE_SPINLOCK(ap_poll_timer_lock);
149 static struct hrtimer ap_poll_timer;
150 /*
151  * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
152  * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.
153  */
154 static unsigned long poll_high_timeout = 250000UL;
155
156 /*
157  * Some state machine states only require a low frequency polling.
158  * We use 25 Hz frequency for these.
159  */
160 static unsigned long poll_low_timeout = 40000000UL;
161
162 /* Maximum domain id, if not given via qci */
163 static int ap_max_domain_id = 15;
164 /* Maximum adapter id, if not given via qci */
165 static int ap_max_adapter_id = 63;
166
167 static const struct bus_type ap_bus_type;
168
169 /* Adapter interrupt definitions */
170 static void ap_interrupt_handler(struct airq_struct *airq,
171                                  struct tpi_info *tpi_info);
172
173 static bool ap_irq_flag;
174
175 static struct airq_struct ap_airq = {
176         .handler = ap_interrupt_handler,
177         .isc = AP_ISC,
178 };
179
180 /**
181  * ap_airq_ptr() - Get the address of the adapter interrupt indicator
182  *
183  * Returns the address of the local-summary-indicator of the adapter
184  * interrupt handler for AP, or NULL if adapter interrupts are not
185  * available.
186  */
187 void *ap_airq_ptr(void)
188 {
189         if (ap_irq_flag)
190                 return ap_airq.lsi_ptr;
191         return NULL;
192 }
193
194 /**
195  * ap_interrupts_available(): Test if AP interrupts are available.
196  *
197  * Returns 1 if AP interrupts are available.
198  */
199 static int ap_interrupts_available(void)
200 {
201         return test_facility(65);
202 }
203
204 /**
205  * ap_qci_available(): Test if AP configuration
206  * information can be queried via QCI subfunction.
207  *
208  * Returns 1 if subfunction PQAP(QCI) is available.
209  */
210 static int ap_qci_available(void)
211 {
212         return test_facility(12);
213 }
214
215 /**
216  * ap_apft_available(): Test if AP facilities test (APFT)
217  * facility is available.
218  *
219  * Returns 1 if APFT is available.
220  */
221 static int ap_apft_available(void)
222 {
223         return test_facility(15);
224 }
225
226 /*
227  * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
228  *
229  * Returns 1 if the QACT subfunction is available.
230  */
231 static inline int ap_qact_available(void)
232 {
233         return ap_qci_info->qact;
234 }
235
236 /*
237  * ap_sb_available(): Test if the AP secure binding facility is available.
238  *
239  * Returns 1 if secure binding facility is available.
240  */
241 int ap_sb_available(void)
242 {
243         return ap_qci_info->apsb;
244 }
245
246 /*
247  * ap_is_se_guest(): Check for SE guest with AP pass-through support.
248  */
249 bool ap_is_se_guest(void)
250 {
251         return is_prot_virt_guest() && ap_sb_available();
252 }
253 EXPORT_SYMBOL(ap_is_se_guest);
254
255 /**
256  * ap_init_qci_info(): Allocate and query qci config info.
257  * Does also update the static variables ap_max_domain_id
258  * and ap_max_adapter_id if this info is available.
259  */
260 static void __init ap_init_qci_info(void)
261 {
262         if (!ap_qci_available() ||
263             ap_qci(ap_qci_info)) {
264                 AP_DBF_INFO("%s QCI not supported\n", __func__);
265                 return;
266         }
267         memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
268         AP_DBF_INFO("%s successful fetched initial qci info\n", __func__);
269
270         if (ap_qci_info->apxa) {
271                 if (ap_qci_info->na) {
272                         ap_max_adapter_id = ap_qci_info->na;
273                         AP_DBF_INFO("%s new ap_max_adapter_id is %d\n",
274                                     __func__, ap_max_adapter_id);
275                 }
276                 if (ap_qci_info->nd) {
277                         ap_max_domain_id = ap_qci_info->nd;
278                         AP_DBF_INFO("%s new ap_max_domain_id is %d\n",
279                                     __func__, ap_max_domain_id);
280                 }
281         }
282 }
283
284 /*
285  * ap_test_config(): helper function to extract the nrth bit
286  *                   within the unsigned int array field.
287  */
288 static inline int ap_test_config(unsigned int *field, unsigned int nr)
289 {
290         return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
291 }
292
293 /*
294  * ap_test_config_card_id(): Test, whether an AP card ID is configured.
295  *
296  * Returns 0 if the card is not configured
297  *         1 if the card is configured or
298  *           if the configuration information is not available
299  */
300 static inline int ap_test_config_card_id(unsigned int id)
301 {
302         if (id > ap_max_adapter_id)
303                 return 0;
304         if (ap_qci_info->flags)
305                 return ap_test_config(ap_qci_info->apm, id);
306         return 1;
307 }
308
309 /*
310  * ap_test_config_usage_domain(): Test, whether an AP usage domain
311  * is configured.
312  *
313  * Returns 0 if the usage domain is not configured
314  *         1 if the usage domain is configured or
315  *           if the configuration information is not available
316  */
317 int ap_test_config_usage_domain(unsigned int domain)
318 {
319         if (domain > ap_max_domain_id)
320                 return 0;
321         if (ap_qci_info->flags)
322                 return ap_test_config(ap_qci_info->aqm, domain);
323         return 1;
324 }
325 EXPORT_SYMBOL(ap_test_config_usage_domain);
326
327 /*
328  * ap_test_config_ctrl_domain(): Test, whether an AP control domain
329  * is configured.
330  * @domain AP control domain ID
331  *
332  * Returns 1 if the control domain is configured
333  *         0 in all other cases
334  */
335 int ap_test_config_ctrl_domain(unsigned int domain)
336 {
337         if (!ap_qci_info || domain > ap_max_domain_id)
338                 return 0;
339         return ap_test_config(ap_qci_info->adm, domain);
340 }
341 EXPORT_SYMBOL(ap_test_config_ctrl_domain);
342
343 /*
344  * ap_queue_info(): Check and get AP queue info.
345  * Returns: 1 if APQN exists and info is filled,
346  *          0 if APQN seems to exist but there is no info
347  *            available (eg. caused by an asynch pending error)
348  *         -1 invalid APQN, TAPQ error or AP queue status which
349  *            indicates there is no APQN.
350  */
351 static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo,
352                          bool *decfg, bool *cstop)
353 {
354         struct ap_queue_status status;
355
356         hwinfo->value = 0;
357
358         /* make sure we don't run into a specifiation exception */
359         if (AP_QID_CARD(qid) > ap_max_adapter_id ||
360             AP_QID_QUEUE(qid) > ap_max_domain_id)
361                 return -1;
362
363         /* call TAPQ on this APQN */
364         status = ap_test_queue(qid, ap_apft_available(), hwinfo);
365
366         switch (status.response_code) {
367         case AP_RESPONSE_NORMAL:
368         case AP_RESPONSE_RESET_IN_PROGRESS:
369         case AP_RESPONSE_DECONFIGURED:
370         case AP_RESPONSE_CHECKSTOPPED:
371         case AP_RESPONSE_BUSY:
372                 /* For all these RCs the tapq info should be available */
373                 break;
374         default:
375                 /* On a pending async error the info should be available */
376                 if (!status.async)
377                         return -1;
378                 break;
379         }
380
381         /* There should be at least one of the mode bits set */
382         if (WARN_ON_ONCE(!hwinfo->value))
383                 return 0;
384
385         *decfg = status.response_code == AP_RESPONSE_DECONFIGURED;
386         *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED;
387
388         return 1;
389 }
390
391 void ap_wait(enum ap_sm_wait wait)
392 {
393         ktime_t hr_time;
394
395         switch (wait) {
396         case AP_SM_WAIT_AGAIN:
397         case AP_SM_WAIT_INTERRUPT:
398                 if (ap_irq_flag)
399                         break;
400                 if (ap_poll_kthread) {
401                         wake_up(&ap_poll_wait);
402                         break;
403                 }
404                 fallthrough;
405         case AP_SM_WAIT_LOW_TIMEOUT:
406         case AP_SM_WAIT_HIGH_TIMEOUT:
407                 spin_lock_bh(&ap_poll_timer_lock);
408                 if (!hrtimer_is_queued(&ap_poll_timer)) {
409                         hr_time =
410                                 wait == AP_SM_WAIT_LOW_TIMEOUT ?
411                                 poll_low_timeout : poll_high_timeout;
412                         hrtimer_forward_now(&ap_poll_timer, hr_time);
413                         hrtimer_restart(&ap_poll_timer);
414                 }
415                 spin_unlock_bh(&ap_poll_timer_lock);
416                 break;
417         case AP_SM_WAIT_NONE:
418         default:
419                 break;
420         }
421 }
422
423 /**
424  * ap_request_timeout(): Handling of request timeouts
425  * @t: timer making this callback
426  *
427  * Handles request timeouts.
428  */
429 void ap_request_timeout(struct timer_list *t)
430 {
431         struct ap_queue *aq = timer_container_of(aq, t, timeout);
432
433         spin_lock_bh(&aq->lock);
434         ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT));
435         spin_unlock_bh(&aq->lock);
436 }
437
438 /**
439  * ap_poll_timeout(): AP receive polling for finished AP requests.
440  * @unused: Unused pointer.
441  *
442  * Schedules the AP tasklet using a high resolution timer.
443  */
444 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
445 {
446         tasklet_schedule(&ap_tasklet);
447         return HRTIMER_NORESTART;
448 }
449
450 /**
451  * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
452  * @airq: pointer to adapter interrupt descriptor
453  * @tpi_info: ignored
454  */
455 static void ap_interrupt_handler(struct airq_struct *airq,
456                                  struct tpi_info *tpi_info)
457 {
458         inc_irq_stat(IRQIO_APB);
459         tasklet_schedule(&ap_tasklet);
460 }
461
462 /**
463  * ap_tasklet_fn(): Tasklet to poll all AP devices.
464  * @dummy: Unused variable
465  *
466  * Poll all AP devices on the bus.
467  */
468 static void ap_tasklet_fn(unsigned long dummy)
469 {
470         int bkt;
471         struct ap_queue *aq;
472         enum ap_sm_wait wait = AP_SM_WAIT_NONE;
473
474         /* Reset the indicator if interrupts are used. Thus new interrupts can
475          * be received. Doing it in the beginning of the tasklet is therefore
476          * important that no requests on any AP get lost.
477          */
478         if (ap_irq_flag)
479                 WRITE_ONCE(*ap_airq.lsi_ptr, 0);
480
481         spin_lock_bh(&ap_queues_lock);
482         hash_for_each(ap_queues, bkt, aq, hnode) {
483                 spin_lock_bh(&aq->lock);
484                 wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL));
485                 spin_unlock_bh(&aq->lock);
486         }
487         spin_unlock_bh(&ap_queues_lock);
488
489         ap_wait(wait);
490 }
491
492 static int ap_pending_requests(void)
493 {
494         int bkt;
495         struct ap_queue *aq;
496
497         spin_lock_bh(&ap_queues_lock);
498         hash_for_each(ap_queues, bkt, aq, hnode) {
499                 if (aq->queue_count == 0)
500                         continue;
501                 spin_unlock_bh(&ap_queues_lock);
502                 return 1;
503         }
504         spin_unlock_bh(&ap_queues_lock);
505         return 0;
506 }
507
508 /**
509  * ap_poll_thread(): Thread that polls for finished requests.
510  * @data: Unused pointer
511  *
512  * AP bus poll thread. The purpose of this thread is to poll for
513  * finished requests in a loop if there is a "free" cpu - that is
514  * a cpu that doesn't have anything better to do. The polling stops
515  * as soon as there is another task or if all messages have been
516  * delivered.
517  */
518 static int ap_poll_thread(void *data)
519 {
520         DECLARE_WAITQUEUE(wait, current);
521
522         set_user_nice(current, MAX_NICE);
523         set_freezable();
524         while (!kthread_should_stop()) {
525                 add_wait_queue(&ap_poll_wait, &wait);
526                 set_current_state(TASK_INTERRUPTIBLE);
527                 if (!ap_pending_requests()) {
528                         schedule();
529                         try_to_freeze();
530                 }
531                 set_current_state(TASK_RUNNING);
532                 remove_wait_queue(&ap_poll_wait, &wait);
533                 if (need_resched()) {
534                         schedule();
535                         try_to_freeze();
536                         continue;
537                 }
538                 ap_tasklet_fn(0);
539         }
540
541         return 0;
542 }
543
544 static int ap_poll_thread_start(void)
545 {
546         int rc;
547
548         if (ap_irq_flag || ap_poll_kthread)
549                 return 0;
550         mutex_lock(&ap_poll_thread_mutex);
551         ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
552         rc = PTR_ERR_OR_ZERO(ap_poll_kthread);
553         if (rc)
554                 ap_poll_kthread = NULL;
555         mutex_unlock(&ap_poll_thread_mutex);
556         return rc;
557 }
558
559 static void ap_poll_thread_stop(void)
560 {
561         if (!ap_poll_kthread)
562                 return;
563         mutex_lock(&ap_poll_thread_mutex);
564         kthread_stop(ap_poll_kthread);
565         ap_poll_kthread = NULL;
566         mutex_unlock(&ap_poll_thread_mutex);
567 }
568
569 #define is_card_dev(x) ((x)->parent == ap_root_device)
570 #define is_queue_dev(x) ((x)->parent != ap_root_device)
571
572 /*
573  * ap_init_apmsg() - Initialize ap_message.
574  */
575 int ap_init_apmsg(struct ap_message *ap_msg, u32 flags)
576 {
577         unsigned int maxmsgsize;
578
579         memset(ap_msg, 0, sizeof(*ap_msg));
580         ap_msg->flags = flags;
581
582         if (flags & AP_MSG_FLAG_MEMPOOL) {
583                 ap_msg->msg = mempool_alloc_preallocated(ap_msg_pool);
584                 if (!ap_msg->msg)
585                         return -ENOMEM;
586                 ap_msg->bufsize = AP_DEFAULT_MAX_MSG_SIZE;
587                 return 0;
588         }
589
590         maxmsgsize = atomic_read(&ap_max_msg_size);
591         ap_msg->msg = kmalloc(maxmsgsize, GFP_KERNEL);
592         if (!ap_msg->msg)
593                 return -ENOMEM;
594         ap_msg->bufsize = maxmsgsize;
595
596         return 0;
597 }
598 EXPORT_SYMBOL(ap_init_apmsg);
599
600 /*
601  * ap_release_apmsg() - Release ap_message.
602  */
603 void ap_release_apmsg(struct ap_message *ap_msg)
604 {
605         if (ap_msg->flags & AP_MSG_FLAG_MEMPOOL) {
606                 memzero_explicit(ap_msg->msg, ap_msg->bufsize);
607                 mempool_free(ap_msg->msg, ap_msg_pool);
608         } else {
609                 kfree_sensitive(ap_msg->msg);
610         }
611 }
612 EXPORT_SYMBOL(ap_release_apmsg);
613
614 /**
615  * ap_bus_match()
616  * @dev: Pointer to device
617  * @drv: Pointer to device_driver
618  *
619  * AP bus driver registration/unregistration.
620  */
621 static int ap_bus_match(struct device *dev, const struct device_driver *drv)
622 {
623         const struct ap_driver *ap_drv = to_ap_drv(drv);
624         struct ap_device_id *id;
625
626         /*
627          * Compare device type of the device with the list of
628          * supported types of the device_driver.
629          */
630         for (id = ap_drv->ids; id->match_flags; id++) {
631                 if (is_card_dev(dev) &&
632                     id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
633                     id->dev_type == to_ap_dev(dev)->device_type)
634                         return 1;
635                 if (is_queue_dev(dev) &&
636                     id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
637                     id->dev_type == to_ap_dev(dev)->device_type)
638                         return 1;
639         }
640         return 0;
641 }
642
643 /**
644  * ap_uevent(): Uevent function for AP devices.
645  * @dev: Pointer to device
646  * @env: Pointer to kobj_uevent_env
647  *
648  * It sets up a single environment variable DEV_TYPE which contains the
649  * hardware device type.
650  */
651 static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env)
652 {
653         int rc = 0;
654         const struct ap_device *ap_dev = to_ap_dev(dev);
655
656         /* Uevents from ap bus core don't need extensions to the env */
657         if (dev == ap_root_device)
658                 return 0;
659
660         if (is_card_dev(dev)) {
661                 struct ap_card *ac = to_ap_card(&ap_dev->device);
662
663                 /* Set up DEV_TYPE environment variable. */
664                 rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
665                 if (rc)
666                         return rc;
667                 /* Add MODALIAS= */
668                 rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
669                 if (rc)
670                         return rc;
671
672                 /* Add MODE=<accel|cca|ep11> */
673                 if (ac->hwinfo.accel)
674                         rc = add_uevent_var(env, "MODE=accel");
675                 else if (ac->hwinfo.cca)
676                         rc = add_uevent_var(env, "MODE=cca");
677                 else if (ac->hwinfo.ep11)
678                         rc = add_uevent_var(env, "MODE=ep11");
679                 if (rc)
680                         return rc;
681         } else {
682                 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
683
684                 /* Add MODE=<accel|cca|ep11> */
685                 if (aq->card->hwinfo.accel)
686                         rc = add_uevent_var(env, "MODE=accel");
687                 else if (aq->card->hwinfo.cca)
688                         rc = add_uevent_var(env, "MODE=cca");
689                 else if (aq->card->hwinfo.ep11)
690                         rc = add_uevent_var(env, "MODE=ep11");
691                 if (rc)
692                         return rc;
693         }
694
695         return 0;
696 }
697
698 static void ap_send_init_scan_done_uevent(void)
699 {
700         char *envp[] = { "INITSCAN=done", NULL };
701
702         kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
703 }
704
705 static void ap_send_bindings_complete_uevent(void)
706 {
707         char buf[32];
708         char *envp[] = { "BINDINGS=complete", buf, NULL };
709
710         snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu",
711                  atomic64_inc_return(&ap_bindings_complete_count));
712         kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
713 }
714
715 void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg)
716 {
717         char buf[16];
718         char *envp[] = { buf, NULL };
719
720         snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0);
721
722         kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
723 }
724 EXPORT_SYMBOL(ap_send_config_uevent);
725
726 void ap_send_online_uevent(struct ap_device *ap_dev, int online)
727 {
728         char buf[16];
729         char *envp[] = { buf, NULL };
730
731         snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0);
732
733         kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp);
734 }
735 EXPORT_SYMBOL(ap_send_online_uevent);
736
737 static void ap_send_mask_changed_uevent(unsigned long *newapm,
738                                         unsigned long *newaqm)
739 {
740         char buf[100];
741         char *envp[] = { buf, NULL };
742
743         if (newapm)
744                 snprintf(buf, sizeof(buf),
745                          "APMASK=0x%016lx%016lx%016lx%016lx\n",
746                          newapm[0], newapm[1], newapm[2], newapm[3]);
747         else
748                 snprintf(buf, sizeof(buf),
749                          "AQMASK=0x%016lx%016lx%016lx%016lx\n",
750                          newaqm[0], newaqm[1], newaqm[2], newaqm[3]);
751
752         kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp);
753 }
754
755 /*
756  * calc # of bound APQNs
757  */
758
759 struct __ap_calc_ctrs {
760         unsigned int apqns;
761         unsigned int bound;
762 };
763
764 static int __ap_calc_helper(struct device *dev, void *arg)
765 {
766         struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg;
767
768         if (is_queue_dev(dev)) {
769                 pctrs->apqns++;
770                 if (dev->driver)
771                         pctrs->bound++;
772         }
773
774         return 0;
775 }
776
777 static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound)
778 {
779         struct __ap_calc_ctrs ctrs;
780
781         memset(&ctrs, 0, sizeof(ctrs));
782         bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper);
783
784         *apqns = ctrs.apqns;
785         *bound = ctrs.bound;
786 }
787
788 /*
789  * After ap bus scan do check if all existing APQNs are
790  * bound to device drivers.
791  */
792 static void ap_check_bindings_complete(void)
793 {
794         unsigned int apqns, bound;
795
796         if (atomic64_read(&ap_scan_bus_count) >= 1) {
797                 ap_calc_bound_apqns(&apqns, &bound);
798                 if (bound == apqns) {
799                         if (!completion_done(&ap_apqn_bindings_complete)) {
800                                 complete_all(&ap_apqn_bindings_complete);
801                                 ap_send_bindings_complete_uevent();
802                                 pr_debug("all apqn bindings complete\n");
803                         }
804                 }
805         }
806 }
807
808 /*
809  * Interface to wait for the AP bus to have done one initial ap bus
810  * scan and all detected APQNs have been bound to device drivers.
811  * If these both conditions are not fulfilled, this function blocks
812  * on a condition with wait_for_completion_interruptible_timeout().
813  * If these both conditions are fulfilled (before the timeout hits)
814  * the return value is 0. If the timeout (in jiffies) hits instead
815  * -ETIME is returned. On failures negative return values are
816  * returned to the caller.
817  */
818 int ap_wait_apqn_bindings_complete(unsigned long timeout)
819 {
820         int rc = 0;
821         long l;
822
823         if (completion_done(&ap_apqn_bindings_complete))
824                 return 0;
825
826         if (timeout)
827                 l = wait_for_completion_interruptible_timeout(
828                         &ap_apqn_bindings_complete, timeout);
829         else
830                 l = wait_for_completion_interruptible(
831                         &ap_apqn_bindings_complete);
832         if (l < 0)
833                 rc = l == -ERESTARTSYS ? -EINTR : l;
834         else if (l == 0 && timeout)
835                 rc = -ETIME;
836
837         pr_debug("rc=%d\n", rc);
838         return rc;
839 }
840 EXPORT_SYMBOL(ap_wait_apqn_bindings_complete);
841
842 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
843 {
844         if (is_queue_dev(dev) &&
845             AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data)
846                 device_unregister(dev);
847         return 0;
848 }
849
850 static int __ap_revise_reserved(struct device *dev, void *dummy)
851 {
852         int rc, card, queue, devres, drvres;
853
854         if (is_queue_dev(dev)) {
855                 card = AP_QID_CARD(to_ap_queue(dev)->qid);
856                 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
857                 mutex_lock(&ap_perms_mutex);
858                 devres = test_bit_inv(card, ap_perms.apm) &&
859                         test_bit_inv(queue, ap_perms.aqm);
860                 mutex_unlock(&ap_perms_mutex);
861                 drvres = to_ap_drv(dev->driver)->flags
862                         & AP_DRIVER_FLAG_DEFAULT;
863                 if (!!devres != !!drvres) {
864                         pr_debug("reprobing queue=%02x.%04x\n", card, queue);
865                         rc = device_reprobe(dev);
866                         if (rc)
867                                 AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n",
868                                             __func__, card, queue);
869                 }
870         }
871
872         return 0;
873 }
874
875 static void ap_bus_revise_bindings(void)
876 {
877         bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved);
878 }
879
880 /**
881  * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the
882  *                      default host driver or not.
883  * @card: the APID of the adapter card to check
884  * @queue: the APQI of the queue to check
885  *
886  * Note: the ap_perms_mutex must be locked by the caller of this function.
887  *
888  * Return: an int specifying whether the AP adapter is reserved for the host (1)
889  *         or not (0).
890  */
891 int ap_owned_by_def_drv(int card, int queue)
892 {
893         int rc = 0;
894
895         if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS)
896                 return -EINVAL;
897
898         if (test_bit_inv(card, ap_perms.apm) &&
899             test_bit_inv(queue, ap_perms.aqm))
900                 rc = 1;
901
902         return rc;
903 }
904 EXPORT_SYMBOL(ap_owned_by_def_drv);
905
906 /**
907  * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in
908  *                                     a set is reserved for the host drivers
909  *                                     or not.
910  * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check
911  * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check
912  *
913  * Note: the ap_perms_mutex must be locked by the caller of this function.
914  *
915  * Return: an int specifying whether each APQN is reserved for the host (1) or
916  *         not (0)
917  */
918 int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm,
919                                        unsigned long *aqm)
920 {
921         int card, queue, rc = 0;
922
923         for (card = 0; !rc && card < AP_DEVICES; card++)
924                 if (test_bit_inv(card, apm) &&
925                     test_bit_inv(card, ap_perms.apm))
926                         for (queue = 0; !rc && queue < AP_DOMAINS; queue++)
927                                 if (test_bit_inv(queue, aqm) &&
928                                     test_bit_inv(queue, ap_perms.aqm))
929                                         rc = 1;
930
931         return rc;
932 }
933 EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv);
934
935 static int ap_device_probe(struct device *dev)
936 {
937         struct ap_device *ap_dev = to_ap_dev(dev);
938         struct ap_driver *ap_drv = to_ap_drv(dev->driver);
939         int card, queue, devres, drvres, rc = -ENODEV;
940
941         if (!get_device(dev))
942                 return rc;
943
944         if (is_queue_dev(dev)) {
945                 /*
946                  * If the apqn is marked as reserved/used by ap bus and
947                  * default drivers, only probe with drivers with the default
948                  * flag set. If it is not marked, only probe with drivers
949                  * with the default flag not set.
950                  */
951                 card = AP_QID_CARD(to_ap_queue(dev)->qid);
952                 queue = AP_QID_QUEUE(to_ap_queue(dev)->qid);
953                 mutex_lock(&ap_perms_mutex);
954                 devres = test_bit_inv(card, ap_perms.apm) &&
955                         test_bit_inv(queue, ap_perms.aqm);
956                 mutex_unlock(&ap_perms_mutex);
957                 drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT;
958                 if (!!devres != !!drvres)
959                         goto out;
960         }
961
962         /*
963          * Rearm the bindings complete completion to trigger
964          * bindings complete when all devices are bound again
965          */
966         reinit_completion(&ap_apqn_bindings_complete);
967
968         /* Add queue/card to list of active queues/cards */
969         spin_lock_bh(&ap_queues_lock);
970         if (is_queue_dev(dev))
971                 hash_add(ap_queues, &to_ap_queue(dev)->hnode,
972                          to_ap_queue(dev)->qid);
973         spin_unlock_bh(&ap_queues_lock);
974
975         rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
976
977         if (rc) {
978                 spin_lock_bh(&ap_queues_lock);
979                 if (is_queue_dev(dev))
980                         hash_del(&to_ap_queue(dev)->hnode);
981                 spin_unlock_bh(&ap_queues_lock);
982         }
983
984 out:
985         if (rc)
986                 put_device(dev);
987         return rc;
988 }
989
990 static void ap_device_remove(struct device *dev)
991 {
992         struct ap_device *ap_dev = to_ap_dev(dev);
993         struct ap_driver *ap_drv = to_ap_drv(dev->driver);
994
995         /* prepare ap queue device removal */
996         if (is_queue_dev(dev))
997                 ap_queue_prepare_remove(to_ap_queue(dev));
998
999         /* driver's chance to clean up gracefully */
1000         if (ap_drv->remove)
1001                 ap_drv->remove(ap_dev);
1002
1003         /* now do the ap queue device remove */
1004         if (is_queue_dev(dev))
1005                 ap_queue_remove(to_ap_queue(dev));
1006
1007         /* Remove queue/card from list of active queues/cards */
1008         spin_lock_bh(&ap_queues_lock);
1009         if (is_queue_dev(dev))
1010                 hash_del(&to_ap_queue(dev)->hnode);
1011         spin_unlock_bh(&ap_queues_lock);
1012
1013         put_device(dev);
1014 }
1015
1016 struct ap_queue *ap_get_qdev(ap_qid_t qid)
1017 {
1018         int bkt;
1019         struct ap_queue *aq;
1020
1021         spin_lock_bh(&ap_queues_lock);
1022         hash_for_each(ap_queues, bkt, aq, hnode) {
1023                 if (aq->qid == qid) {
1024                         get_device(&aq->ap_dev.device);
1025                         spin_unlock_bh(&ap_queues_lock);
1026                         return aq;
1027                 }
1028         }
1029         spin_unlock_bh(&ap_queues_lock);
1030
1031         return NULL;
1032 }
1033 EXPORT_SYMBOL(ap_get_qdev);
1034
1035 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
1036                        char *name)
1037 {
1038         struct device_driver *drv = &ap_drv->driver;
1039         int rc;
1040
1041         drv->bus = &ap_bus_type;
1042         drv->owner = owner;
1043         drv->name = name;
1044         rc = driver_register(drv);
1045
1046         ap_check_bindings_complete();
1047
1048         return rc;
1049 }
1050 EXPORT_SYMBOL(ap_driver_register);
1051
1052 void ap_driver_unregister(struct ap_driver *ap_drv)
1053 {
1054         driver_unregister(&ap_drv->driver);
1055 }
1056 EXPORT_SYMBOL(ap_driver_unregister);
1057
1058 /*
1059  * Enforce a synchronous AP bus rescan.
1060  * Returns true if the bus scan finds a change in the AP configuration
1061  * and AP devices have been added or deleted when this function returns.
1062  */
1063 bool ap_bus_force_rescan(void)
1064 {
1065         unsigned long scan_counter = atomic64_read(&ap_scan_bus_count);
1066         bool rc = false;
1067
1068         pr_debug("> scan counter=%lu\n", scan_counter);
1069
1070         /* Only trigger AP bus scans after the initial scan is done */
1071         if (scan_counter <= 0)
1072                 goto out;
1073
1074         /*
1075          * There is one unlikely but nevertheless valid scenario where the
1076          * thread holding the mutex may try to send some crypto load but
1077          * all cards are offline so a rescan is triggered which causes
1078          * a recursive call of ap_bus_force_rescan(). A simple return if
1079          * the mutex is already locked by this thread solves this.
1080          */
1081         if (mutex_is_locked(&ap_scan_bus_mutex)) {
1082                 if (ap_scan_bus_task == current)
1083                         goto out;
1084         }
1085
1086         /* Try to acquire the AP scan bus mutex */
1087         if (mutex_trylock(&ap_scan_bus_mutex)) {
1088                 /* mutex acquired, run the AP bus scan */
1089                 ap_scan_bus_task = current;
1090                 ap_scan_bus_result = ap_scan_bus();
1091                 rc = ap_scan_bus_result;
1092                 ap_scan_bus_task = NULL;
1093                 mutex_unlock(&ap_scan_bus_mutex);
1094                 goto out;
1095         }
1096
1097         /*
1098          * Mutex acquire failed. So there is currently another task
1099          * already running the AP bus scan. Then let's simple wait
1100          * for the lock which means the other task has finished and
1101          * stored the result in ap_scan_bus_result.
1102          */
1103         if (mutex_lock_interruptible(&ap_scan_bus_mutex)) {
1104                 /* some error occurred, ignore and go out */
1105                 goto out;
1106         }
1107         rc = ap_scan_bus_result;
1108         mutex_unlock(&ap_scan_bus_mutex);
1109
1110 out:
1111         pr_debug("rc=%d\n", rc);
1112         return rc;
1113 }
1114 EXPORT_SYMBOL(ap_bus_force_rescan);
1115
1116 /*
1117  * A config change has happened, force an ap bus rescan.
1118  */
1119 static int ap_bus_cfg_chg(struct notifier_block *nb,
1120                           unsigned long action, void *data)
1121 {
1122         if (action != CHSC_NOTIFY_AP_CFG)
1123                 return NOTIFY_DONE;
1124
1125         pr_debug("config change, forcing bus rescan\n");
1126
1127         ap_bus_force_rescan();
1128
1129         return NOTIFY_OK;
1130 }
1131
1132 static struct notifier_block ap_bus_nb = {
1133         .notifier_call = ap_bus_cfg_chg,
1134 };
1135
1136 int ap_hex2bitmap(const char *str, unsigned long *bitmap, int bits)
1137 {
1138         int i, n, b;
1139
1140         /* bits needs to be a multiple of 8 */
1141         if (bits & 0x07)
1142                 return -EINVAL;
1143
1144         if (str[0] == '0' && str[1] == 'x')
1145                 str++;
1146         if (*str == 'x')
1147                 str++;
1148
1149         for (i = 0; isxdigit(*str) && i < bits; str++) {
1150                 b = hex_to_bin(*str);
1151                 for (n = 0; n < 4; n++)
1152                         if (b & (0x08 >> n))
1153                                 set_bit_inv(i + n, bitmap);
1154                 i += 4;
1155         }
1156
1157         if (*str == '\n')
1158                 str++;
1159         if (*str)
1160                 return -EINVAL;
1161         return 0;
1162 }
1163 EXPORT_SYMBOL(ap_hex2bitmap);
1164
1165 /*
1166  * modify_bitmap() - parse bitmask argument and modify an existing
1167  * bit mask accordingly. A concatenation (done with ',') of these
1168  * terms is recognized:
1169  *   +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>]
1170  * <bitnr> may be any valid number (hex, decimal or octal) in the range
1171  * 0...bits-1; the leading + or - is required. Here are some examples:
1172  *   +0-15,+32,-128,-0xFF
1173  *   -0-255,+1-16,+0x128
1174  *   +1,+2,+3,+4,-5,-7-10
1175  * Returns the new bitmap after all changes have been applied. Every
1176  * positive value in the string will set a bit and every negative value
1177  * in the string will clear a bit. As a bit may be touched more than once,
1178  * the last 'operation' wins:
1179  * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be
1180  * cleared again. All other bits are unmodified.
1181  */
1182 static int modify_bitmap(const char *str, unsigned long *bitmap, int bits)
1183 {
1184         unsigned long a, i, z;
1185         char *np, sign;
1186
1187         /* bits needs to be a multiple of 8 */
1188         if (bits & 0x07)
1189                 return -EINVAL;
1190
1191         while (*str) {
1192                 sign = *str++;
1193                 if (sign != '+' && sign != '-')
1194                         return -EINVAL;
1195                 a = z = simple_strtoul(str, &np, 0);
1196                 if (str == np || a >= bits)
1197                         return -EINVAL;
1198                 str = np;
1199                 if (*str == '-') {
1200                         z = simple_strtoul(++str, &np, 0);
1201                         if (str == np || a > z || z >= bits)
1202                                 return -EINVAL;
1203                         str = np;
1204                 }
1205                 for (i = a; i <= z; i++)
1206                         if (sign == '+')
1207                                 set_bit_inv(i, bitmap);
1208                         else
1209                                 clear_bit_inv(i, bitmap);
1210                 while (*str == ',' || *str == '\n')
1211                         str++;
1212         }
1213
1214         return 0;
1215 }
1216
1217 static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits,
1218                                unsigned long *newmap)
1219 {
1220         unsigned long size;
1221         int rc;
1222
1223         size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1224         if (*str == '+' || *str == '-') {
1225                 memcpy(newmap, bitmap, size);
1226                 rc = modify_bitmap(str, newmap, bits);
1227         } else {
1228                 memset(newmap, 0, size);
1229                 rc = ap_hex2bitmap(str, newmap, bits);
1230         }
1231         return rc;
1232 }
1233
1234 int ap_parse_mask_str(const char *str,
1235                       unsigned long *bitmap, int bits,
1236                       struct mutex *lock)
1237 {
1238         unsigned long *newmap, size;
1239         int rc;
1240
1241         /* bits needs to be a multiple of 8 */
1242         if (bits & 0x07)
1243                 return -EINVAL;
1244
1245         size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
1246         newmap = kmalloc(size, GFP_KERNEL);
1247         if (!newmap)
1248                 return -ENOMEM;
1249         if (mutex_lock_interruptible(lock)) {
1250                 kfree(newmap);
1251                 return -ERESTARTSYS;
1252         }
1253         rc = ap_parse_bitmap_str(str, bitmap, bits, newmap);
1254         if (rc == 0)
1255                 memcpy(bitmap, newmap, size);
1256         mutex_unlock(lock);
1257         kfree(newmap);
1258         return rc;
1259 }
1260 EXPORT_SYMBOL(ap_parse_mask_str);
1261
1262 /*
1263  * AP bus attributes.
1264  */
1265
1266 static ssize_t ap_domain_show(const struct bus_type *bus, char *buf)
1267 {
1268         return sysfs_emit(buf, "%d\n", ap_domain_index);
1269 }
1270
1271 static ssize_t ap_domain_store(const struct bus_type *bus,
1272                                const char *buf, size_t count)
1273 {
1274         int domain;
1275
1276         if (sscanf(buf, "%i\n", &domain) != 1 ||
1277             domain < 0 || domain > ap_max_domain_id ||
1278             !test_bit_inv(domain, ap_perms.aqm))
1279                 return -EINVAL;
1280
1281         spin_lock_bh(&ap_domain_lock);
1282         ap_domain_index = domain;
1283         spin_unlock_bh(&ap_domain_lock);
1284
1285         AP_DBF_INFO("%s stored new default domain=%d\n",
1286                     __func__, domain);
1287
1288         return count;
1289 }
1290
1291 static BUS_ATTR_RW(ap_domain);
1292
1293 static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf)
1294 {
1295         if (!ap_qci_info->flags)        /* QCI not supported */
1296                 return sysfs_emit(buf, "not supported\n");
1297
1298         return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1299                           ap_qci_info->adm[0], ap_qci_info->adm[1],
1300                           ap_qci_info->adm[2], ap_qci_info->adm[3],
1301                           ap_qci_info->adm[4], ap_qci_info->adm[5],
1302                           ap_qci_info->adm[6], ap_qci_info->adm[7]);
1303 }
1304
1305 static BUS_ATTR_RO(ap_control_domain_mask);
1306
1307 static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf)
1308 {
1309         if (!ap_qci_info->flags)        /* QCI not supported */
1310                 return sysfs_emit(buf, "not supported\n");
1311
1312         return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1313                           ap_qci_info->aqm[0], ap_qci_info->aqm[1],
1314                           ap_qci_info->aqm[2], ap_qci_info->aqm[3],
1315                           ap_qci_info->aqm[4], ap_qci_info->aqm[5],
1316                           ap_qci_info->aqm[6], ap_qci_info->aqm[7]);
1317 }
1318
1319 static BUS_ATTR_RO(ap_usage_domain_mask);
1320
1321 static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf)
1322 {
1323         if (!ap_qci_info->flags)        /* QCI not supported */
1324                 return sysfs_emit(buf, "not supported\n");
1325
1326         return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
1327                           ap_qci_info->apm[0], ap_qci_info->apm[1],
1328                           ap_qci_info->apm[2], ap_qci_info->apm[3],
1329                           ap_qci_info->apm[4], ap_qci_info->apm[5],
1330                           ap_qci_info->apm[6], ap_qci_info->apm[7]);
1331 }
1332
1333 static BUS_ATTR_RO(ap_adapter_mask);
1334
1335 static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf)
1336 {
1337         return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0);
1338 }
1339
1340 static BUS_ATTR_RO(ap_interrupts);
1341
1342 static ssize_t config_time_show(const struct bus_type *bus, char *buf)
1343 {
1344         return sysfs_emit(buf, "%d\n", ap_scan_bus_time);
1345 }
1346
1347 static ssize_t config_time_store(const struct bus_type *bus,
1348                                  const char *buf, size_t count)
1349 {
1350         int time;
1351
1352         if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
1353                 return -EINVAL;
1354         ap_scan_bus_time = time;
1355         mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
1356         return count;
1357 }
1358
1359 static BUS_ATTR_RW(config_time);
1360
1361 static ssize_t poll_thread_show(const struct bus_type *bus, char *buf)
1362 {
1363         return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0);
1364 }
1365
1366 static ssize_t poll_thread_store(const struct bus_type *bus,
1367                                  const char *buf, size_t count)
1368 {
1369         bool value;
1370         int rc;
1371
1372         rc = kstrtobool(buf, &value);
1373         if (rc)
1374                 return rc;
1375
1376         if (value) {
1377                 rc = ap_poll_thread_start();
1378                 if (rc)
1379                         count = rc;
1380         } else {
1381                 ap_poll_thread_stop();
1382         }
1383         return count;
1384 }
1385
1386 static BUS_ATTR_RW(poll_thread);
1387
1388 static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf)
1389 {
1390         return sysfs_emit(buf, "%lu\n", poll_high_timeout);
1391 }
1392
1393 static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf,
1394                                   size_t count)
1395 {
1396         unsigned long value;
1397         ktime_t hr_time;
1398         int rc;
1399
1400         rc = kstrtoul(buf, 0, &value);
1401         if (rc)
1402                 return rc;
1403
1404         /* 120 seconds = maximum poll interval */
1405         if (value > 120000000000UL)
1406                 return -EINVAL;
1407         poll_high_timeout = value;
1408         hr_time = poll_high_timeout;
1409
1410         spin_lock_bh(&ap_poll_timer_lock);
1411         hrtimer_cancel(&ap_poll_timer);
1412         hrtimer_set_expires(&ap_poll_timer, hr_time);
1413         hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
1414         spin_unlock_bh(&ap_poll_timer_lock);
1415
1416         return count;
1417 }
1418
1419 static BUS_ATTR_RW(poll_timeout);
1420
1421 static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf)
1422 {
1423         return sysfs_emit(buf, "%d\n", ap_max_domain_id);
1424 }
1425
1426 static BUS_ATTR_RO(ap_max_domain_id);
1427
1428 static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf)
1429 {
1430         return sysfs_emit(buf, "%d\n", ap_max_adapter_id);
1431 }
1432
1433 static BUS_ATTR_RO(ap_max_adapter_id);
1434
1435 static ssize_t apmask_show(const struct bus_type *bus, char *buf)
1436 {
1437         int rc;
1438
1439         if (mutex_lock_interruptible(&ap_perms_mutex))
1440                 return -ERESTARTSYS;
1441         rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1442                         ap_perms.apm[0], ap_perms.apm[1],
1443                         ap_perms.apm[2], ap_perms.apm[3]);
1444         mutex_unlock(&ap_perms_mutex);
1445
1446         return rc;
1447 }
1448
1449 static int __verify_card_reservations(struct device_driver *drv, void *data)
1450 {
1451         int rc = 0;
1452         struct ap_driver *ap_drv = to_ap_drv(drv);
1453         unsigned long *newapm = (unsigned long *)data;
1454
1455         /*
1456          * increase the driver's module refcounter to be sure it is not
1457          * going away when we invoke the callback function.
1458          */
1459         if (!try_module_get(drv->owner))
1460                 return 0;
1461
1462         if (ap_drv->in_use) {
1463                 rc = ap_drv->in_use(newapm, ap_perms.aqm);
1464                 if (rc)
1465                         rc = -EBUSY;
1466         }
1467
1468         /* release the driver's module */
1469         module_put(drv->owner);
1470
1471         return rc;
1472 }
1473
1474 static int apmask_commit(unsigned long *newapm)
1475 {
1476         int rc;
1477         unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)];
1478
1479         /*
1480          * Check if any bits in the apmask have been set which will
1481          * result in queues being removed from non-default drivers
1482          */
1483         if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) {
1484                 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1485                                       __verify_card_reservations);
1486                 if (rc)
1487                         return rc;
1488         }
1489
1490         memcpy(ap_perms.apm, newapm, APMASKSIZE);
1491
1492         return 0;
1493 }
1494
1495 static ssize_t apmask_store(const struct bus_type *bus, const char *buf,
1496                             size_t count)
1497 {
1498         int rc, changes = 0;
1499         DECLARE_BITMAP(newapm, AP_DEVICES);
1500
1501         if (mutex_lock_interruptible(&ap_perms_mutex))
1502                 return -ERESTARTSYS;
1503
1504         rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm);
1505         if (rc)
1506                 goto done;
1507
1508         changes = memcmp(ap_perms.apm, newapm, APMASKSIZE);
1509         if (changes)
1510                 rc = apmask_commit(newapm);
1511
1512 done:
1513         mutex_unlock(&ap_perms_mutex);
1514         if (rc)
1515                 return rc;
1516
1517         if (changes) {
1518                 ap_bus_revise_bindings();
1519                 ap_send_mask_changed_uevent(newapm, NULL);
1520         }
1521
1522         return count;
1523 }
1524
1525 static BUS_ATTR_RW(apmask);
1526
1527 static ssize_t aqmask_show(const struct bus_type *bus, char *buf)
1528 {
1529         int rc;
1530
1531         if (mutex_lock_interruptible(&ap_perms_mutex))
1532                 return -ERESTARTSYS;
1533         rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n",
1534                         ap_perms.aqm[0], ap_perms.aqm[1],
1535                         ap_perms.aqm[2], ap_perms.aqm[3]);
1536         mutex_unlock(&ap_perms_mutex);
1537
1538         return rc;
1539 }
1540
1541 static int __verify_queue_reservations(struct device_driver *drv, void *data)
1542 {
1543         int rc = 0;
1544         struct ap_driver *ap_drv = to_ap_drv(drv);
1545         unsigned long *newaqm = (unsigned long *)data;
1546
1547         /*
1548          * increase the driver's module refcounter to be sure it is not
1549          * going away when we invoke the callback function.
1550          */
1551         if (!try_module_get(drv->owner))
1552                 return 0;
1553
1554         if (ap_drv->in_use) {
1555                 rc = ap_drv->in_use(ap_perms.apm, newaqm);
1556                 if (rc)
1557                         rc = -EBUSY;
1558         }
1559
1560         /* release the driver's module */
1561         module_put(drv->owner);
1562
1563         return rc;
1564 }
1565
1566 static int aqmask_commit(unsigned long *newaqm)
1567 {
1568         int rc;
1569         unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)];
1570
1571         /*
1572          * Check if any bits in the aqmask have been set which will
1573          * result in queues being removed from non-default drivers
1574          */
1575         if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) {
1576                 rc = bus_for_each_drv(&ap_bus_type, NULL, reserved,
1577                                       __verify_queue_reservations);
1578                 if (rc)
1579                         return rc;
1580         }
1581
1582         memcpy(ap_perms.aqm, newaqm, AQMASKSIZE);
1583
1584         return 0;
1585 }
1586
1587 static ssize_t aqmask_store(const struct bus_type *bus, const char *buf,
1588                             size_t count)
1589 {
1590         int rc, changes = 0;
1591         DECLARE_BITMAP(newaqm, AP_DOMAINS);
1592
1593         if (mutex_lock_interruptible(&ap_perms_mutex))
1594                 return -ERESTARTSYS;
1595
1596         rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm);
1597         if (rc)
1598                 goto done;
1599
1600         changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE);
1601         if (changes)
1602                 rc = aqmask_commit(newaqm);
1603
1604 done:
1605         mutex_unlock(&ap_perms_mutex);
1606         if (rc)
1607                 return rc;
1608
1609         if (changes) {
1610                 ap_bus_revise_bindings();
1611                 ap_send_mask_changed_uevent(NULL, newaqm);
1612         }
1613
1614         return count;
1615 }
1616
1617 static BUS_ATTR_RW(aqmask);
1618
1619 static ssize_t scans_show(const struct bus_type *bus, char *buf)
1620 {
1621         return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count));
1622 }
1623
1624 static ssize_t scans_store(const struct bus_type *bus, const char *buf,
1625                            size_t count)
1626 {
1627         AP_DBF_INFO("%s force AP bus rescan\n", __func__);
1628
1629         ap_bus_force_rescan();
1630
1631         return count;
1632 }
1633
1634 static BUS_ATTR_RW(scans);
1635
1636 static ssize_t bindings_show(const struct bus_type *bus, char *buf)
1637 {
1638         int rc;
1639         unsigned int apqns, n;
1640
1641         ap_calc_bound_apqns(&apqns, &n);
1642         if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns)
1643                 rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns);
1644         else
1645                 rc = sysfs_emit(buf, "%u/%u\n", n, apqns);
1646
1647         return rc;
1648 }
1649
1650 static BUS_ATTR_RO(bindings);
1651
1652 static ssize_t features_show(const struct bus_type *bus, char *buf)
1653 {
1654         int n = 0;
1655
1656         if (!ap_qci_info->flags)        /* QCI not supported */
1657                 return sysfs_emit(buf, "-\n");
1658
1659         if (ap_qci_info->apsc)
1660                 n += sysfs_emit_at(buf, n, "APSC ");
1661         if (ap_qci_info->apxa)
1662                 n += sysfs_emit_at(buf, n, "APXA ");
1663         if (ap_qci_info->qact)
1664                 n += sysfs_emit_at(buf, n, "QACT ");
1665         if (ap_qci_info->rc8a)
1666                 n += sysfs_emit_at(buf, n, "RC8A ");
1667         if (ap_qci_info->apsb)
1668                 n += sysfs_emit_at(buf, n, "APSB ");
1669
1670         sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n");
1671
1672         return n;
1673 }
1674
1675 static BUS_ATTR_RO(features);
1676
1677 static struct attribute *ap_bus_attrs[] = {
1678         &bus_attr_ap_domain.attr,
1679         &bus_attr_ap_control_domain_mask.attr,
1680         &bus_attr_ap_usage_domain_mask.attr,
1681         &bus_attr_ap_adapter_mask.attr,
1682         &bus_attr_config_time.attr,
1683         &bus_attr_poll_thread.attr,
1684         &bus_attr_ap_interrupts.attr,
1685         &bus_attr_poll_timeout.attr,
1686         &bus_attr_ap_max_domain_id.attr,
1687         &bus_attr_ap_max_adapter_id.attr,
1688         &bus_attr_apmask.attr,
1689         &bus_attr_aqmask.attr,
1690         &bus_attr_scans.attr,
1691         &bus_attr_bindings.attr,
1692         &bus_attr_features.attr,
1693         NULL,
1694 };
1695 ATTRIBUTE_GROUPS(ap_bus);
1696
1697 static const struct bus_type ap_bus_type = {
1698         .name = "ap",
1699         .bus_groups = ap_bus_groups,
1700         .match = &ap_bus_match,
1701         .uevent = &ap_uevent,
1702         .probe = ap_device_probe,
1703         .remove = ap_device_remove,
1704 };
1705
1706 /**
1707  * ap_select_domain(): Select an AP domain if possible and we haven't
1708  * already done so before.
1709  */
1710 static void ap_select_domain(void)
1711 {
1712         struct ap_queue_status status;
1713         int card, dom;
1714
1715         /*
1716          * Choose the default domain. Either the one specified with
1717          * the "domain=" parameter or the first domain with at least
1718          * one valid APQN.
1719          */
1720         spin_lock_bh(&ap_domain_lock);
1721         if (ap_domain_index >= 0) {
1722                 /* Domain has already been selected. */
1723                 goto out;
1724         }
1725         for (dom = 0; dom <= ap_max_domain_id; dom++) {
1726                 if (!ap_test_config_usage_domain(dom) ||
1727                     !test_bit_inv(dom, ap_perms.aqm))
1728                         continue;
1729                 for (card = 0; card <= ap_max_adapter_id; card++) {
1730                         if (!ap_test_config_card_id(card) ||
1731                             !test_bit_inv(card, ap_perms.apm))
1732                                 continue;
1733                         status = ap_test_queue(AP_MKQID(card, dom),
1734                                                ap_apft_available(),
1735                                                NULL);
1736                         if (status.response_code == AP_RESPONSE_NORMAL)
1737                                 break;
1738                 }
1739                 if (card <= ap_max_adapter_id)
1740                         break;
1741         }
1742         if (dom <= ap_max_domain_id) {
1743                 ap_domain_index = dom;
1744                 AP_DBF_INFO("%s new default domain is %d\n",
1745                             __func__, ap_domain_index);
1746         }
1747 out:
1748         spin_unlock_bh(&ap_domain_lock);
1749 }
1750
1751 /*
1752  * This function checks the type and returns either 0 for not
1753  * supported or the highest compatible type value (which may
1754  * include the input type value).
1755  */
1756 static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
1757 {
1758         int comp_type = 0;
1759
1760         /* < CEX4 is not supported */
1761         if (rawtype < AP_DEVICE_TYPE_CEX4) {
1762                 AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n",
1763                             __func__, AP_QID_CARD(qid),
1764                             AP_QID_QUEUE(qid), rawtype);
1765                 return 0;
1766         }
1767         /* up to CEX8 known and fully supported */
1768         if (rawtype <= AP_DEVICE_TYPE_CEX8)
1769                 return rawtype;
1770         /*
1771          * unknown new type > CEX8, check for compatibility
1772          * to the highest known and supported type which is
1773          * currently CEX8 with the help of the QACT function.
1774          */
1775         if (ap_qact_available()) {
1776                 struct ap_queue_status status;
1777                 union ap_qact_ap_info apinfo = {0};
1778
1779                 apinfo.mode = (func >> 26) & 0x07;
1780                 apinfo.cat = AP_DEVICE_TYPE_CEX8;
1781                 status = ap_qact(qid, 0, &apinfo);
1782                 if (status.response_code == AP_RESPONSE_NORMAL &&
1783                     apinfo.cat >= AP_DEVICE_TYPE_CEX4 &&
1784                     apinfo.cat <= AP_DEVICE_TYPE_CEX8)
1785                         comp_type = apinfo.cat;
1786         }
1787         if (!comp_type)
1788                 AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n",
1789                             __func__, AP_QID_CARD(qid),
1790                             AP_QID_QUEUE(qid), rawtype);
1791         else if (comp_type != rawtype)
1792                 AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n",
1793                             __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1794                             rawtype, comp_type);
1795         return comp_type;
1796 }
1797
1798 /*
1799  * Helper function to be used with bus_find_dev
1800  * matches for the card device with the given id
1801  */
1802 static int __match_card_device_with_id(struct device *dev, const void *data)
1803 {
1804         return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data;
1805 }
1806
1807 /*
1808  * Helper function to be used with bus_find_dev
1809  * matches for the queue device with a given qid
1810  */
1811 static int __match_queue_device_with_qid(struct device *dev, const void *data)
1812 {
1813         return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data;
1814 }
1815
1816 /*
1817  * Helper function to be used with bus_find_dev
1818  * matches any queue device with given queue id
1819  */
1820 static int __match_queue_device_with_queue_id(struct device *dev, const void *data)
1821 {
1822         return is_queue_dev(dev) &&
1823                 AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data;
1824 }
1825
1826 /* Helper function for notify_config_changed */
1827 static int __drv_notify_config_changed(struct device_driver *drv, void *data)
1828 {
1829         struct ap_driver *ap_drv = to_ap_drv(drv);
1830
1831         if (try_module_get(drv->owner)) {
1832                 if (ap_drv->on_config_changed)
1833                         ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old);
1834                 module_put(drv->owner);
1835         }
1836
1837         return 0;
1838 }
1839
1840 /* Notify all drivers about an qci config change */
1841 static inline void notify_config_changed(void)
1842 {
1843         bus_for_each_drv(&ap_bus_type, NULL, NULL,
1844                          __drv_notify_config_changed);
1845 }
1846
1847 /* Helper function for notify_scan_complete */
1848 static int __drv_notify_scan_complete(struct device_driver *drv, void *data)
1849 {
1850         struct ap_driver *ap_drv = to_ap_drv(drv);
1851
1852         if (try_module_get(drv->owner)) {
1853                 if (ap_drv->on_scan_complete)
1854                         ap_drv->on_scan_complete(ap_qci_info,
1855                                                  ap_qci_info_old);
1856                 module_put(drv->owner);
1857         }
1858
1859         return 0;
1860 }
1861
1862 /* Notify all drivers about bus scan complete */
1863 static inline void notify_scan_complete(void)
1864 {
1865         bus_for_each_drv(&ap_bus_type, NULL, NULL,
1866                          __drv_notify_scan_complete);
1867 }
1868
1869 /*
1870  * Helper function for ap_scan_bus().
1871  * Remove card device and associated queue devices.
1872  */
1873 static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac)
1874 {
1875         bus_for_each_dev(&ap_bus_type, NULL,
1876                          (void *)(long)ac->id,
1877                          __ap_queue_devices_with_id_unregister);
1878         device_unregister(&ac->ap_dev.device);
1879 }
1880
1881 /*
1882  * Helper function for ap_scan_bus().
1883  * Does the scan bus job for all the domains within
1884  * a valid adapter given by an ap_card ptr.
1885  */
1886 static inline void ap_scan_domains(struct ap_card *ac)
1887 {
1888         struct ap_tapq_hwinfo hwinfo;
1889         bool decfg, chkstop;
1890         struct ap_queue *aq;
1891         struct device *dev;
1892         ap_qid_t qid;
1893         int rc, dom;
1894
1895         /*
1896          * Go through the configuration for the domains and compare them
1897          * to the existing queue devices. Also take care of the config
1898          * and error state for the queue devices.
1899          */
1900
1901         for (dom = 0; dom <= ap_max_domain_id; dom++) {
1902                 qid = AP_MKQID(ac->id, dom);
1903                 dev = bus_find_device(&ap_bus_type, NULL,
1904                                       (void *)(long)qid,
1905                                       __match_queue_device_with_qid);
1906                 aq = dev ? to_ap_queue(dev) : NULL;
1907                 if (!ap_test_config_usage_domain(dom)) {
1908                         if (dev) {
1909                                 AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n",
1910                                             __func__, ac->id, dom);
1911                                 device_unregister(dev);
1912                         }
1913                         goto put_dev_and_continue;
1914                 }
1915                 /* domain is valid, get info from this APQN */
1916                 rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop);
1917                 switch (rc) {
1918                 case -1:
1919                         if (dev) {
1920                                 AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n",
1921                                             __func__, ac->id, dom);
1922                                 device_unregister(dev);
1923                         }
1924                         fallthrough;
1925                 case 0:
1926                         goto put_dev_and_continue;
1927                 default:
1928                         break;
1929                 }
1930                 /* if no queue device exists, create a new one */
1931                 if (!aq) {
1932                         aq = ap_queue_create(qid, ac);
1933                         if (!aq) {
1934                                 AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n",
1935                                             __func__, ac->id, dom);
1936                                 continue;
1937                         }
1938                         aq->config = !decfg;
1939                         aq->chkstop = chkstop;
1940                         aq->se_bstate = hwinfo.bs;
1941                         dev = &aq->ap_dev.device;
1942                         dev->bus = &ap_bus_type;
1943                         dev->parent = &ac->ap_dev.device;
1944                         dev_set_name(dev, "%02x.%04x", ac->id, dom);
1945                         /* register queue device */
1946                         rc = device_register(dev);
1947                         if (rc) {
1948                                 AP_DBF_WARN("%s(%d,%d) device_register() failed\n",
1949                                             __func__, ac->id, dom);
1950                                 goto put_dev_and_continue;
1951                         }
1952                         /* get it and thus adjust reference counter */
1953                         get_device(dev);
1954                         if (decfg) {
1955                                 AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n",
1956                                             __func__, ac->id, dom);
1957                         } else if (chkstop) {
1958                                 AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n",
1959                                             __func__, ac->id, dom);
1960                         } else {
1961                                 /* nudge the queue's state machine */
1962                                 ap_queue_init_state(aq);
1963                                 AP_DBF_INFO("%s(%d,%d) new queue dev created\n",
1964                                             __func__, ac->id, dom);
1965                         }
1966                         goto put_dev_and_continue;
1967                 }
1968                 /* handle state changes on already existing queue device */
1969                 spin_lock_bh(&aq->lock);
1970                 /* SE bind state */
1971                 aq->se_bstate = hwinfo.bs;
1972                 /* checkstop state */
1973                 if (chkstop && !aq->chkstop) {
1974                         /* checkstop on */
1975                         aq->chkstop = true;
1976                         if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
1977                                 aq->dev_state = AP_DEV_STATE_ERROR;
1978                                 aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED;
1979                         }
1980                         spin_unlock_bh(&aq->lock);
1981                         pr_debug("(%d,%d) queue dev checkstop on\n",
1982                                  ac->id, dom);
1983                         /* 'receive' pending messages with -EAGAIN */
1984                         ap_flush_queue(aq);
1985                         goto put_dev_and_continue;
1986                 } else if (!chkstop && aq->chkstop) {
1987                         /* checkstop off */
1988                         aq->chkstop = false;
1989                         if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
1990                                 _ap_queue_init_state(aq);
1991                         spin_unlock_bh(&aq->lock);
1992                         pr_debug("(%d,%d) queue dev checkstop off\n",
1993                                  ac->id, dom);
1994                         goto put_dev_and_continue;
1995                 }
1996                 /* config state change */
1997                 if (decfg && aq->config) {
1998                         /* config off this queue device */
1999                         aq->config = false;
2000                         if (aq->dev_state > AP_DEV_STATE_UNINITIATED) {
2001                                 aq->dev_state = AP_DEV_STATE_ERROR;
2002                                 aq->last_err_rc = AP_RESPONSE_DECONFIGURED;
2003                         }
2004                         spin_unlock_bh(&aq->lock);
2005                         pr_debug("(%d,%d) queue dev config off\n",
2006                                  ac->id, dom);
2007                         ap_send_config_uevent(&aq->ap_dev, aq->config);
2008                         /* 'receive' pending messages with -EAGAIN */
2009                         ap_flush_queue(aq);
2010                         goto put_dev_and_continue;
2011                 } else if (!decfg && !aq->config) {
2012                         /* config on this queue device */
2013                         aq->config = true;
2014                         if (aq->dev_state > AP_DEV_STATE_UNINITIATED)
2015                                 _ap_queue_init_state(aq);
2016                         spin_unlock_bh(&aq->lock);
2017                         pr_debug("(%d,%d) queue dev config on\n",
2018                                  ac->id, dom);
2019                         ap_send_config_uevent(&aq->ap_dev, aq->config);
2020                         goto put_dev_and_continue;
2021                 }
2022                 /* handle other error states */
2023                 if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) {
2024                         spin_unlock_bh(&aq->lock);
2025                         /* 'receive' pending messages with -EAGAIN */
2026                         ap_flush_queue(aq);
2027                         /* re-init (with reset) the queue device */
2028                         ap_queue_init_state(aq);
2029                         AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n",
2030                                     __func__, ac->id, dom);
2031                         goto put_dev_and_continue;
2032                 }
2033                 spin_unlock_bh(&aq->lock);
2034 put_dev_and_continue:
2035                 put_device(dev);
2036         }
2037 }
2038
2039 /*
2040  * Helper function for ap_scan_bus().
2041  * Does the scan bus job for the given adapter id.
2042  */
2043 static inline void ap_scan_adapter(int ap)
2044 {
2045         struct ap_tapq_hwinfo hwinfo;
2046         int rc, dom, comp_type;
2047         bool decfg, chkstop;
2048         struct ap_card *ac;
2049         struct device *dev;
2050         ap_qid_t qid;
2051
2052         /* Is there currently a card device for this adapter ? */
2053         dev = bus_find_device(&ap_bus_type, NULL,
2054                               (void *)(long)ap,
2055                               __match_card_device_with_id);
2056         ac = dev ? to_ap_card(dev) : NULL;
2057
2058         /* Adapter not in configuration ? */
2059         if (!ap_test_config_card_id(ap)) {
2060                 if (ac) {
2061                         AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n",
2062                                     __func__, ap);
2063                         ap_scan_rm_card_dev_and_queue_devs(ac);
2064                         put_device(dev);
2065                 }
2066                 return;
2067         }
2068
2069         /*
2070          * Adapter ap is valid in the current configuration. So do some checks:
2071          * If no card device exists, build one. If a card device exists, check
2072          * for type and functions changed. For all this we need to find a valid
2073          * APQN first.
2074          */
2075
2076         for (dom = 0; dom <= ap_max_domain_id; dom++)
2077                 if (ap_test_config_usage_domain(dom)) {
2078                         qid = AP_MKQID(ap, dom);
2079                         if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0)
2080                                 break;
2081                 }
2082         if (dom > ap_max_domain_id) {
2083                 /* Could not find one valid APQN for this adapter */
2084                 if (ac) {
2085                         AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n",
2086                                     __func__, ap);
2087                         ap_scan_rm_card_dev_and_queue_devs(ac);
2088                         put_device(dev);
2089                 } else {
2090                         pr_debug("(%d) no type info (no APQN found), ignored\n",
2091                                  ap);
2092                 }
2093                 return;
2094         }
2095         if (!hwinfo.at) {
2096                 /* No apdater type info available, an unusable adapter */
2097                 if (ac) {
2098                         AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n",
2099                                     __func__, ap);
2100                         ap_scan_rm_card_dev_and_queue_devs(ac);
2101                         put_device(dev);
2102                 } else {
2103                         pr_debug("(%d) no valid type (0) info, ignored\n", ap);
2104                 }
2105                 return;
2106         }
2107         hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */
2108         if (ac) {
2109                 /* Check APQN against existing card device for changes */
2110                 if (ac->hwinfo.at != hwinfo.at) {
2111                         AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n",
2112                                     __func__, ap, hwinfo.at);
2113                         ap_scan_rm_card_dev_and_queue_devs(ac);
2114                         put_device(dev);
2115                         ac = NULL;
2116                 } else if (ac->hwinfo.fac != hwinfo.fac) {
2117                         AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n",
2118                                     __func__, ap, hwinfo.fac);
2119                         ap_scan_rm_card_dev_and_queue_devs(ac);
2120                         put_device(dev);
2121                         ac = NULL;
2122                 } else {
2123                         /* handle checkstop state change */
2124                         if (chkstop && !ac->chkstop) {
2125                                 /* checkstop on */
2126                                 ac->chkstop = true;
2127                                 AP_DBF_INFO("%s(%d) card dev checkstop on\n",
2128                                             __func__, ap);
2129                         } else if (!chkstop && ac->chkstop) {
2130                                 /* checkstop off */
2131                                 ac->chkstop = false;
2132                                 AP_DBF_INFO("%s(%d) card dev checkstop off\n",
2133                                             __func__, ap);
2134                         }
2135                         /* handle config state change */
2136                         if (decfg && ac->config) {
2137                                 ac->config = false;
2138                                 AP_DBF_INFO("%s(%d) card dev config off\n",
2139                                             __func__, ap);
2140                                 ap_send_config_uevent(&ac->ap_dev, ac->config);
2141                         } else if (!decfg && !ac->config) {
2142                                 ac->config = true;
2143                                 AP_DBF_INFO("%s(%d) card dev config on\n",
2144                                             __func__, ap);
2145                                 ap_send_config_uevent(&ac->ap_dev, ac->config);
2146                         }
2147                 }
2148         }
2149
2150         if (!ac) {
2151                 /* Build a new card device */
2152                 comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac);
2153                 if (!comp_type) {
2154                         AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n",
2155                                     __func__, ap, hwinfo.at);
2156                         return;
2157                 }
2158                 ac = ap_card_create(ap, hwinfo, comp_type);
2159                 if (!ac) {
2160                         AP_DBF_WARN("%s(%d) ap_card_create() failed\n",
2161                                     __func__, ap);
2162                         return;
2163                 }
2164                 ac->config = !decfg;
2165                 ac->chkstop = chkstop;
2166                 dev = &ac->ap_dev.device;
2167                 dev->bus = &ap_bus_type;
2168                 dev->parent = ap_root_device;
2169                 dev_set_name(dev, "card%02x", ap);
2170                 /* maybe enlarge ap_max_msg_size to support this card */
2171                 if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) {
2172                         atomic_set(&ap_max_msg_size, ac->maxmsgsize);
2173                         AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n",
2174                                     __func__, ap,
2175                                     atomic_read(&ap_max_msg_size));
2176                 }
2177                 /* Register the new card device with AP bus */
2178                 rc = device_register(dev);
2179                 if (rc) {
2180                         AP_DBF_WARN("%s(%d) device_register() failed\n",
2181                                     __func__, ap);
2182                         put_device(dev);
2183                         return;
2184                 }
2185                 /* get it and thus adjust reference counter */
2186                 get_device(dev);
2187                 if (decfg)
2188                         AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n",
2189                                     __func__, ap, hwinfo.at, hwinfo.fac);
2190                 else if (chkstop)
2191                         AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n",
2192                                     __func__, ap, hwinfo.at, hwinfo.fac);
2193                 else
2194                         AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n",
2195                                     __func__, ap, hwinfo.at, hwinfo.fac);
2196         }
2197
2198         /* Verify the domains and the queue devices for this card */
2199         ap_scan_domains(ac);
2200
2201         /* release the card device */
2202         put_device(&ac->ap_dev.device);
2203 }
2204
2205 /**
2206  * ap_get_configuration - get the host AP configuration
2207  *
2208  * Stores the host AP configuration information returned from the previous call
2209  * to Query Configuration Information (QCI), then retrieves and stores the
2210  * current AP configuration returned from QCI.
2211  *
2212  * Return: true if the host AP configuration changed between calls to QCI;
2213  * otherwise, return false.
2214  */
2215 static bool ap_get_configuration(void)
2216 {
2217         if (!ap_qci_info->flags)        /* QCI not supported */
2218                 return false;
2219
2220         memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info));
2221         ap_qci(ap_qci_info);
2222
2223         return memcmp(ap_qci_info, ap_qci_info_old,
2224                       sizeof(struct ap_config_info)) != 0;
2225 }
2226
2227 /*
2228  * ap_config_has_new_aps - Check current against old qci info if
2229  * new adapters have appeared. Returns true if at least one new
2230  * adapter in the apm mask is showing up. Existing adapters or
2231  * receding adapters are not counted.
2232  */
2233 static bool ap_config_has_new_aps(void)
2234 {
2235
2236         unsigned long m[BITS_TO_LONGS(AP_DEVICES)];
2237
2238         if (!ap_qci_info->flags)
2239                 return false;
2240
2241         bitmap_andnot(m, (unsigned long *)ap_qci_info->apm,
2242                       (unsigned long *)ap_qci_info_old->apm, AP_DEVICES);
2243         if (!bitmap_empty(m, AP_DEVICES))
2244                 return true;
2245
2246         return false;
2247 }
2248
2249 /*
2250  * ap_config_has_new_doms - Check current against old qci info if
2251  * new (usage) domains have appeared. Returns true if at least one
2252  * new domain in the aqm mask is showing up. Existing domains or
2253  * receding domains are not counted.
2254  */
2255 static bool ap_config_has_new_doms(void)
2256 {
2257         unsigned long m[BITS_TO_LONGS(AP_DOMAINS)];
2258
2259         if (!ap_qci_info->flags)
2260                 return false;
2261
2262         bitmap_andnot(m, (unsigned long *)ap_qci_info->aqm,
2263                       (unsigned long *)ap_qci_info_old->aqm, AP_DOMAINS);
2264         if (!bitmap_empty(m, AP_DOMAINS))
2265                 return true;
2266
2267         return false;
2268 }
2269
2270 /**
2271  * ap_scan_bus(): Scan the AP bus for new devices
2272  * Always run under mutex ap_scan_bus_mutex protection
2273  * which needs to get locked/unlocked by the caller!
2274  * Returns true if any config change has been detected
2275  * during the scan, otherwise false.
2276  */
2277 static bool ap_scan_bus(void)
2278 {
2279         bool config_changed;
2280         int ap;
2281
2282         pr_debug(">\n");
2283
2284         /* (re-)fetch configuration via QCI */
2285         config_changed = ap_get_configuration();
2286         if (config_changed) {
2287                 if (ap_config_has_new_aps() || ap_config_has_new_doms()) {
2288                         /*
2289                          * Appearance of new adapters and/or domains need to
2290                          * build new ap devices which need to get bound to an
2291                          * device driver. Thus reset the APQN bindings complete
2292                          * completion.
2293                          */
2294                         reinit_completion(&ap_apqn_bindings_complete);
2295                 }
2296                 /* post a config change notify */
2297                 notify_config_changed();
2298         }
2299         ap_select_domain();
2300
2301         /* loop over all possible adapters */
2302         for (ap = 0; ap <= ap_max_adapter_id; ap++)
2303                 ap_scan_adapter(ap);
2304
2305         /* scan complete notify */
2306         if (config_changed)
2307                 notify_scan_complete();
2308
2309         /* check if there is at least one queue available with default domain */
2310         if (ap_domain_index >= 0) {
2311                 struct device *dev =
2312                         bus_find_device(&ap_bus_type, NULL,
2313                                         (void *)(long)ap_domain_index,
2314                                         __match_queue_device_with_queue_id);
2315                 if (dev)
2316                         put_device(dev);
2317                 else
2318                         AP_DBF_INFO("%s no queue device with default domain %d available\n",
2319                                     __func__, ap_domain_index);
2320         }
2321
2322         if (atomic64_inc_return(&ap_scan_bus_count) == 1) {
2323                 pr_debug("init scan complete\n");
2324                 ap_send_init_scan_done_uevent();
2325         }
2326
2327         ap_check_bindings_complete();
2328
2329         mod_timer(&ap_scan_bus_timer, jiffies + ap_scan_bus_time * HZ);
2330
2331         pr_debug("< config_changed=%d\n", config_changed);
2332
2333         return config_changed;
2334 }
2335
2336 /*
2337  * Callback for the ap_scan_bus_timer
2338  * Runs periodically, workqueue timer (ap_scan_bus_time)
2339  */
2340 static void ap_scan_bus_timer_callback(struct timer_list *unused)
2341 {
2342         /*
2343          * schedule work into the system long wq which when
2344          * the work is finally executed, calls the AP bus scan.
2345          */
2346         queue_work(system_long_wq, &ap_scan_bus_work);
2347 }
2348
2349 /*
2350  * Callback for the ap_scan_bus_work
2351  */
2352 static void ap_scan_bus_wq_callback(struct work_struct *unused)
2353 {
2354         /*
2355          * Try to invoke an ap_scan_bus(). If the mutex acquisition
2356          * fails there is currently another task already running the
2357          * AP scan bus and there is no need to wait and re-trigger the
2358          * scan again. Please note at the end of the scan bus function
2359          * the AP scan bus timer is re-armed which triggers then the
2360          * ap_scan_bus_timer_callback which enqueues a work into the
2361          * system_long_wq which invokes this function here again.
2362          */
2363         if (mutex_trylock(&ap_scan_bus_mutex)) {
2364                 ap_scan_bus_task = current;
2365                 ap_scan_bus_result = ap_scan_bus();
2366                 ap_scan_bus_task = NULL;
2367                 mutex_unlock(&ap_scan_bus_mutex);
2368         }
2369 }
2370
2371 static inline void __exit ap_async_exit(void)
2372 {
2373         if (ap_thread_flag)
2374                 ap_poll_thread_stop();
2375         chsc_notifier_unregister(&ap_bus_nb);
2376         cancel_work(&ap_scan_bus_work);
2377         hrtimer_cancel(&ap_poll_timer);
2378         timer_delete(&ap_scan_bus_timer);
2379 }
2380
2381 static inline int __init ap_async_init(void)
2382 {
2383         int rc;
2384
2385         /* Setup the AP bus rescan timer. */
2386         timer_setup(&ap_scan_bus_timer, ap_scan_bus_timer_callback, 0);
2387
2388         /*
2389          * Setup the high resolution poll timer.
2390          * If we are running under z/VM adjust polling to z/VM polling rate.
2391          */
2392         if (machine_is_vm())
2393                 poll_high_timeout = 1500000;
2394         hrtimer_setup(&ap_poll_timer, ap_poll_timeout, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
2395
2396         queue_work(system_long_wq, &ap_scan_bus_work);
2397
2398         rc = chsc_notifier_register(&ap_bus_nb);
2399         if (rc)
2400                 goto out;
2401
2402         /* Start the low priority AP bus poll thread. */
2403         if (!ap_thread_flag)
2404                 return 0;
2405
2406         rc = ap_poll_thread_start();
2407         if (rc)
2408                 goto out_notifier;
2409
2410         return 0;
2411
2412 out_notifier:
2413         chsc_notifier_unregister(&ap_bus_nb);
2414 out:
2415         cancel_work(&ap_scan_bus_work);
2416         hrtimer_cancel(&ap_poll_timer);
2417         timer_delete(&ap_scan_bus_timer);
2418         return rc;
2419 }
2420
2421 static inline void ap_irq_exit(void)
2422 {
2423         if (ap_irq_flag)
2424                 unregister_adapter_interrupt(&ap_airq);
2425 }
2426
2427 static inline int __init ap_irq_init(void)
2428 {
2429         int rc;
2430
2431         if (!ap_interrupts_available() || !ap_useirq)
2432                 return 0;
2433
2434         rc = register_adapter_interrupt(&ap_airq);
2435         ap_irq_flag = (rc == 0);
2436
2437         return rc;
2438 }
2439
2440 static inline void ap_debug_exit(void)
2441 {
2442         debug_unregister(ap_dbf_info);
2443 }
2444
2445 static inline int __init ap_debug_init(void)
2446 {
2447         ap_dbf_info = debug_register("ap", 2, 1,
2448                                      AP_DBF_MAX_SPRINTF_ARGS * sizeof(long));
2449         debug_register_view(ap_dbf_info, &debug_sprintf_view);
2450         debug_set_level(ap_dbf_info, DBF_ERR);
2451
2452         return 0;
2453 }
2454
2455 static void __init ap_perms_init(void)
2456 {
2457         /* all resources usable if no kernel parameter string given */
2458         memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm));
2459         memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm));
2460         memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm));
2461
2462         /* apm kernel parameter string */
2463         if (apm_str) {
2464                 memset(&ap_perms.apm, 0, sizeof(ap_perms.apm));
2465                 ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES,
2466                                   &ap_perms_mutex);
2467         }
2468
2469         /* aqm kernel parameter string */
2470         if (aqm_str) {
2471                 memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm));
2472                 ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS,
2473                                   &ap_perms_mutex);
2474         }
2475 }
2476
2477 /**
2478  * ap_module_init(): The module initialization code.
2479  *
2480  * Initializes the module.
2481  */
2482 static int __init ap_module_init(void)
2483 {
2484         int rc;
2485
2486         rc = ap_debug_init();
2487         if (rc)
2488                 return rc;
2489
2490         if (!ap_instructions_available()) {
2491                 pr_warn("The hardware system does not support AP instructions\n");
2492                 return -ENODEV;
2493         }
2494
2495         /* init ap_queue hashtable */
2496         hash_init(ap_queues);
2497
2498         /* create ap msg buffer memory pool */
2499         ap_msg_pool = mempool_create_kmalloc_pool(ap_msg_pool_min_items,
2500                                                   AP_DEFAULT_MAX_MSG_SIZE);
2501         if (!ap_msg_pool) {
2502                 rc = -ENOMEM;
2503                 goto out;
2504         }
2505
2506         /* set up the AP permissions (ioctls, ap and aq masks) */
2507         ap_perms_init();
2508
2509         /* Get AP configuration data if available */
2510         ap_init_qci_info();
2511
2512         /* check default domain setting */
2513         if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id ||
2514             (ap_domain_index >= 0 &&
2515              !test_bit_inv(ap_domain_index, ap_perms.aqm))) {
2516                 pr_warn("%d is not a valid cryptographic domain\n",
2517                         ap_domain_index);
2518                 ap_domain_index = -1;
2519         }
2520
2521         /* Create /sys/bus/ap. */
2522         rc = bus_register(&ap_bus_type);
2523         if (rc)
2524                 goto out;
2525
2526         /* Create /sys/devices/ap. */
2527         ap_root_device = root_device_register("ap");
2528         rc = PTR_ERR_OR_ZERO(ap_root_device);
2529         if (rc)
2530                 goto out_bus;
2531         ap_root_device->bus = &ap_bus_type;
2532
2533         /* enable interrupts if available */
2534         rc = ap_irq_init();
2535         if (rc)
2536                 goto out_device;
2537
2538         /* Setup asynchronous work (timers, workqueue, etc). */
2539         rc = ap_async_init();
2540         if (rc)
2541                 goto out_irq;
2542
2543         return 0;
2544
2545 out_irq:
2546         ap_irq_exit();
2547 out_device:
2548         root_device_unregister(ap_root_device);
2549 out_bus:
2550         bus_unregister(&ap_bus_type);
2551 out:
2552         mempool_destroy(ap_msg_pool);
2553         ap_debug_exit();
2554         return rc;
2555 }
2556
2557 static void __exit ap_module_exit(void)
2558 {
2559         ap_async_exit();
2560         ap_irq_exit();
2561         root_device_unregister(ap_root_device);
2562         bus_unregister(&ap_bus_type);
2563         mempool_destroy(ap_msg_pool);
2564         ap_debug_exit();
2565 }
2566
2567 module_init(ap_module_init);
2568 module_exit(ap_module_exit);