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