2 * Copyright IBM Corp. 2006, 2012
3 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
4 * Martin Schwidefsky <schwidefsky@de.ibm.com>
5 * Ralph Wuerthner <rwuerthn@de.ibm.com>
6 * Felix Beck <felix.beck@de.ibm.com>
7 * Holger Dengler <hd@linux.vnet.ibm.com>
9 * Adjunct processor bus.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
29 #include <linux/kernel_stat.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34 #include <linux/interrupt.h>
35 #include <linux/workqueue.h>
36 #include <linux/slab.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <linux/suspend.h>
41 #include <asm/reset.h>
43 #include <linux/atomic.h>
45 #include <linux/hrtimer.h>
46 #include <linux/ktime.h>
47 #include <asm/facility.h>
48 #include <linux/crypto.h>
49 #include <linux/mod_devicetable.h>
50 #include <linux/debugfs.h>
57 * Module parameters; note though this file itself isn't modular.
59 int ap_domain_index = -1; /* Adjunct Processor Domain Index */
60 static DEFINE_SPINLOCK(ap_domain_lock);
61 module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
62 MODULE_PARM_DESC(domain, "domain index for ap devices");
63 EXPORT_SYMBOL(ap_domain_index);
65 static int ap_thread_flag = 0;
66 module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
67 MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
69 static struct device *ap_root_device;
71 DEFINE_SPINLOCK(ap_list_lock);
72 LIST_HEAD(ap_card_list);
74 static struct ap_config_info *ap_configuration;
75 static bool initialised;
78 * AP bus related debug feature things.
80 debug_info_t *ap_dbf_info;
83 * Workqueue timer for bus rescan.
85 static struct timer_list ap_config_timer;
86 static int ap_config_time = AP_CONFIG_TIME;
87 static void ap_scan_bus(struct work_struct *);
88 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
91 * Tasklet & timer for AP request polling and interrupts
93 static void ap_tasklet_fn(unsigned long);
94 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
95 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96 static struct task_struct *ap_poll_kthread = NULL;
97 static DEFINE_MUTEX(ap_poll_thread_mutex);
98 static DEFINE_SPINLOCK(ap_poll_timer_lock);
99 static struct hrtimer ap_poll_timer;
100 /* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
101 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
102 static unsigned long long poll_timeout = 250000;
105 static int ap_suspend_flag;
106 /* Maximum domain id */
107 static int ap_max_domain_id;
108 /* Flag to check if domain was set through module parameter domain=. This is
109 * important when supsend and resume is done in a z/VM environment where the
110 * domain might change. */
111 static int user_set_domain = 0;
112 static struct bus_type ap_bus_type;
114 /* Adapter interrupt definitions */
115 static void ap_interrupt_handler(struct airq_struct *airq);
117 static int ap_airq_flag;
119 static struct airq_struct ap_airq = {
120 .handler = ap_interrupt_handler,
125 * ap_using_interrupts() - Returns non-zero if interrupt support is
128 static inline int ap_using_interrupts(void)
134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
136 * Returns the address of the local-summary-indicator of the adapter
137 * interrupt handler for AP, or NULL if adapter interrupts are not
140 void *ap_airq_ptr(void)
142 if (ap_using_interrupts())
143 return ap_airq.lsi_ptr;
148 * ap_interrupts_available(): Test if AP interrupts are available.
150 * Returns 1 if AP interrupts are available.
152 static int ap_interrupts_available(void)
154 return test_facility(65);
158 * ap_configuration_available(): Test if AP configuration
159 * information is available.
161 * Returns 1 if AP configuration information is available.
163 static int ap_configuration_available(void)
165 return test_facility(12);
169 * ap_apft_available(): Test if AP facilities test (APFT)
170 * facility is available.
172 * Returns 1 if APFT is is available.
174 static int ap_apft_available(void)
176 return test_facility(15);
180 * ap_test_queue(): Test adjunct processor queue.
181 * @qid: The AP queue number
182 * @tbit: Test facilities bit
183 * @info: Pointer to queue descriptor
185 * Returns AP queue status structure.
187 struct ap_queue_status ap_test_queue(ap_qid_t qid,
192 qid |= 1UL << 23; /* set T bit*/
193 return ap_tapq(qid, info);
195 EXPORT_SYMBOL(ap_test_queue);
198 * ap_query_configuration(): Fetch cryptographic config info
200 * Returns the ap configuration info fetched via PQAP(QCI).
201 * On success 0 is returned, on failure a negative errno
202 * is returned, e.g. if the PQAP(QCI) instruction is not
203 * available, the return value will be -EOPNOTSUPP.
205 int ap_query_configuration(struct ap_config_info *info)
207 if (!ap_configuration_available())
213 EXPORT_SYMBOL(ap_query_configuration);
216 * ap_init_configuration(): Allocate and query configuration array.
218 static void ap_init_configuration(void)
220 if (!ap_configuration_available())
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
226 if (ap_query_configuration(ap_configuration) != 0) {
227 kfree(ap_configuration);
228 ap_configuration = NULL;
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
237 static inline int ap_test_config(unsigned int *field, unsigned int nr)
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
246 * Returns 0 if the card is not configured
247 * 1 if the card is configured or
248 * if the configuration information is not available
250 static inline int ap_test_config_card_id(unsigned int id)
252 if (!ap_configuration) /* QCI not supported */
254 return ap_test_config(ap_configuration->apm, id);
258 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
259 * @domain AP usage domain ID
261 * Returns 0 if the usage domain is not configured
262 * 1 if the usage domain is configured or
263 * if the configuration information is not available
265 static inline int ap_test_config_domain(unsigned int domain)
267 if (!ap_configuration) /* QCI not supported */
269 return ap_test_config(ap_configuration->aqm, domain);
273 * ap_query_queue(): Check if an AP queue is available.
274 * @qid: The AP queue number
275 * @queue_depth: Pointer to queue depth value
276 * @device_type: Pointer to device type value
277 * @facilities: Pointer to facility indicator
279 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
280 unsigned int *facilities)
282 struct ap_queue_status status;
286 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
289 status = ap_test_queue(qid, ap_apft_available(), &info);
290 switch (status.response_code) {
291 case AP_RESPONSE_NORMAL:
292 *queue_depth = (int)(info & 0xff);
293 *device_type = (int)((info >> 24) & 0xff);
294 *facilities = (unsigned int)(info >> 32);
295 /* Update maximum domain id */
296 nd = (info >> 16) & 0xff;
297 /* if N bit is available, z13 and newer */
298 if ((info & (1UL << 57)) && nd > 0)
299 ap_max_domain_id = nd;
300 else /* older machine types */
301 ap_max_domain_id = 15;
302 switch (*device_type) {
303 /* For CEX2 and CEX3 the available functions
304 * are not refrected by the facilities bits.
305 * Instead it is coded into the type. So here
306 * modify the function bits based on the type.
308 case AP_DEVICE_TYPE_CEX2A:
309 case AP_DEVICE_TYPE_CEX3A:
310 *facilities |= 0x08000000;
312 case AP_DEVICE_TYPE_CEX2C:
313 case AP_DEVICE_TYPE_CEX3C:
314 *facilities |= 0x10000000;
320 case AP_RESPONSE_Q_NOT_AVAIL:
321 case AP_RESPONSE_DECONFIGURED:
322 case AP_RESPONSE_CHECKSTOPPED:
323 case AP_RESPONSE_INVALID_ADDRESS:
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 case AP_RESPONSE_OTHERWISE_CHANGED:
327 case AP_RESPONSE_BUSY:
334 void ap_wait(enum ap_wait wait)
340 case AP_WAIT_INTERRUPT:
341 if (ap_using_interrupts())
343 if (ap_poll_kthread) {
344 wake_up(&ap_poll_wait);
348 case AP_WAIT_TIMEOUT:
349 spin_lock_bh(&ap_poll_timer_lock);
350 if (!hrtimer_is_queued(&ap_poll_timer)) {
351 hr_time = poll_timeout;
352 hrtimer_forward_now(&ap_poll_timer, hr_time);
353 hrtimer_restart(&ap_poll_timer);
355 spin_unlock_bh(&ap_poll_timer_lock);
364 * ap_request_timeout(): Handling of request timeouts
365 * @data: Holds the AP device.
367 * Handles request timeouts.
369 void ap_request_timeout(unsigned long data)
371 struct ap_queue *aq = (struct ap_queue *) data;
375 spin_lock_bh(&aq->lock);
376 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
377 spin_unlock_bh(&aq->lock);
381 * ap_poll_timeout(): AP receive polling for finished AP requests.
382 * @unused: Unused pointer.
384 * Schedules the AP tasklet using a high resolution timer.
386 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
388 if (!ap_suspend_flag)
389 tasklet_schedule(&ap_tasklet);
390 return HRTIMER_NORESTART;
394 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
395 * @airq: pointer to adapter interrupt descriptor
397 static void ap_interrupt_handler(struct airq_struct *airq)
399 inc_irq_stat(IRQIO_APB);
400 if (!ap_suspend_flag)
401 tasklet_schedule(&ap_tasklet);
405 * ap_tasklet_fn(): Tasklet to poll all AP devices.
406 * @dummy: Unused variable
408 * Poll all AP devices on the bus.
410 static void ap_tasklet_fn(unsigned long dummy)
414 enum ap_wait wait = AP_WAIT_NONE;
416 /* Reset the indicator if interrupts are used. Thus new interrupts can
417 * be received. Doing it in the beginning of the tasklet is therefor
418 * important that no requests on any AP get lost.
420 if (ap_using_interrupts())
421 xchg(ap_airq.lsi_ptr, 0);
423 spin_lock_bh(&ap_list_lock);
424 for_each_ap_card(ac) {
425 for_each_ap_queue(aq, ac) {
426 spin_lock_bh(&aq->lock);
427 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
428 spin_unlock_bh(&aq->lock);
431 spin_unlock_bh(&ap_list_lock);
436 static int ap_pending_requests(void)
441 spin_lock_bh(&ap_list_lock);
442 for_each_ap_card(ac) {
443 for_each_ap_queue(aq, ac) {
444 if (aq->queue_count == 0)
446 spin_unlock_bh(&ap_list_lock);
450 spin_unlock_bh(&ap_list_lock);
455 * ap_poll_thread(): Thread that polls for finished requests.
456 * @data: Unused pointer
458 * AP bus poll thread. The purpose of this thread is to poll for
459 * finished requests in a loop if there is a "free" cpu - that is
460 * a cpu that doesn't have anything better to do. The polling stops
461 * as soon as there is another task or if all messages have been
464 static int ap_poll_thread(void *data)
466 DECLARE_WAITQUEUE(wait, current);
468 set_user_nice(current, MAX_NICE);
470 while (!kthread_should_stop()) {
471 add_wait_queue(&ap_poll_wait, &wait);
472 set_current_state(TASK_INTERRUPTIBLE);
473 if (ap_suspend_flag || !ap_pending_requests()) {
477 set_current_state(TASK_RUNNING);
478 remove_wait_queue(&ap_poll_wait, &wait);
479 if (need_resched()) {
490 static int ap_poll_thread_start(void)
494 if (ap_using_interrupts() || ap_poll_kthread)
496 mutex_lock(&ap_poll_thread_mutex);
497 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
498 rc = PTR_RET(ap_poll_kthread);
500 ap_poll_kthread = NULL;
501 mutex_unlock(&ap_poll_thread_mutex);
505 static void ap_poll_thread_stop(void)
507 if (!ap_poll_kthread)
509 mutex_lock(&ap_poll_thread_mutex);
510 kthread_stop(ap_poll_kthread);
511 ap_poll_kthread = NULL;
512 mutex_unlock(&ap_poll_thread_mutex);
515 #define is_card_dev(x) ((x)->parent == ap_root_device)
516 #define is_queue_dev(x) ((x)->parent != ap_root_device)
520 * @dev: Pointer to device
521 * @drv: Pointer to device_driver
523 * AP bus driver registration/unregistration.
525 static int ap_bus_match(struct device *dev, struct device_driver *drv)
527 struct ap_driver *ap_drv = to_ap_drv(drv);
528 struct ap_device_id *id;
531 * Compare device type of the device with the list of
532 * supported types of the device_driver.
534 for (id = ap_drv->ids; id->match_flags; id++) {
535 if (is_card_dev(dev) &&
536 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
537 id->dev_type == to_ap_dev(dev)->device_type)
539 if (is_queue_dev(dev) &&
540 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
541 id->dev_type == to_ap_dev(dev)->device_type)
548 * ap_uevent(): Uevent function for AP devices.
549 * @dev: Pointer to device
550 * @env: Pointer to kobj_uevent_env
552 * It sets up a single environment variable DEV_TYPE which contains the
553 * hardware device type.
555 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
557 struct ap_device *ap_dev = to_ap_dev(dev);
563 /* Set up DEV_TYPE environment variable. */
564 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
569 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
574 static int ap_dev_suspend(struct device *dev)
576 struct ap_device *ap_dev = to_ap_dev(dev);
578 if (ap_dev->drv && ap_dev->drv->suspend)
579 ap_dev->drv->suspend(ap_dev);
583 static int ap_dev_resume(struct device *dev)
585 struct ap_device *ap_dev = to_ap_dev(dev);
587 if (ap_dev->drv && ap_dev->drv->resume)
588 ap_dev->drv->resume(ap_dev);
592 static void ap_bus_suspend(void)
594 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
598 * Disable scanning for devices, thus we do not want to scan
599 * for them after removing.
601 flush_work(&ap_scan_work);
602 tasklet_disable(&ap_tasklet);
605 static int __ap_card_devices_unregister(struct device *dev, void *dummy)
607 if (is_card_dev(dev))
608 device_unregister(dev);
612 static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
614 if (is_queue_dev(dev))
615 device_unregister(dev);
619 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
621 if (is_queue_dev(dev) &&
622 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
623 device_unregister(dev);
627 static void ap_bus_resume(void)
631 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
633 /* remove all queue devices */
634 bus_for_each_dev(&ap_bus_type, NULL, NULL,
635 __ap_queue_devices_unregister);
636 /* remove all card devices */
637 bus_for_each_dev(&ap_bus_type, NULL, NULL,
638 __ap_card_devices_unregister);
640 /* Reset thin interrupt setting */
641 if (ap_interrupts_available() && !ap_using_interrupts()) {
642 rc = register_adapter_interrupt(&ap_airq);
643 ap_airq_flag = (rc == 0);
645 if (!ap_interrupts_available() && ap_using_interrupts()) {
646 unregister_adapter_interrupt(&ap_airq);
650 if (!user_set_domain)
651 ap_domain_index = -1;
652 /* Get things going again */
655 xchg(ap_airq.lsi_ptr, 0);
656 tasklet_enable(&ap_tasklet);
657 queue_work(system_long_wq, &ap_scan_work);
660 static int ap_power_event(struct notifier_block *this, unsigned long event,
664 case PM_HIBERNATION_PREPARE:
665 case PM_SUSPEND_PREPARE:
668 case PM_POST_HIBERNATION:
669 case PM_POST_SUSPEND:
677 static struct notifier_block ap_power_notifier = {
678 .notifier_call = ap_power_event,
681 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
683 static struct bus_type ap_bus_type = {
685 .match = &ap_bus_match,
686 .uevent = &ap_uevent,
687 .pm = &ap_bus_pm_ops,
690 static int ap_device_probe(struct device *dev)
692 struct ap_device *ap_dev = to_ap_dev(dev);
693 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
696 /* Add queue/card to list of active queues/cards */
697 spin_lock_bh(&ap_list_lock);
698 if (is_card_dev(dev))
699 list_add(&to_ap_card(dev)->list, &ap_card_list);
701 list_add(&to_ap_queue(dev)->list,
702 &to_ap_queue(dev)->card->queues);
703 spin_unlock_bh(&ap_list_lock);
705 ap_dev->drv = ap_drv;
706 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
709 spin_lock_bh(&ap_list_lock);
710 if (is_card_dev(dev))
711 list_del_init(&to_ap_card(dev)->list);
713 list_del_init(&to_ap_queue(dev)->list);
714 spin_unlock_bh(&ap_list_lock);
721 static int ap_device_remove(struct device *dev)
723 struct ap_device *ap_dev = to_ap_dev(dev);
724 struct ap_driver *ap_drv = ap_dev->drv;
727 ap_drv->remove(ap_dev);
729 /* Remove queue/card from list of active queues/cards */
730 spin_lock_bh(&ap_list_lock);
731 if (is_card_dev(dev))
732 list_del_init(&to_ap_card(dev)->list);
734 list_del_init(&to_ap_queue(dev)->list);
735 spin_unlock_bh(&ap_list_lock);
740 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
743 struct device_driver *drv = &ap_drv->driver;
748 drv->bus = &ap_bus_type;
749 drv->probe = ap_device_probe;
750 drv->remove = ap_device_remove;
753 return driver_register(drv);
755 EXPORT_SYMBOL(ap_driver_register);
757 void ap_driver_unregister(struct ap_driver *ap_drv)
759 driver_unregister(&ap_drv->driver);
761 EXPORT_SYMBOL(ap_driver_unregister);
763 void ap_bus_force_rescan(void)
767 /* processing a asynchronous bus rescan */
768 del_timer(&ap_config_timer);
769 queue_work(system_long_wq, &ap_scan_work);
770 flush_work(&ap_scan_work);
772 EXPORT_SYMBOL(ap_bus_force_rescan);
777 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
779 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
782 static ssize_t ap_domain_store(struct bus_type *bus,
783 const char *buf, size_t count)
787 if (sscanf(buf, "%i\n", &domain) != 1 ||
788 domain < 0 || domain > ap_max_domain_id)
790 spin_lock_bh(&ap_domain_lock);
791 ap_domain_index = domain;
792 spin_unlock_bh(&ap_domain_lock);
794 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
799 static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
801 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
803 if (!ap_configuration) /* QCI not supported */
804 return snprintf(buf, PAGE_SIZE, "not supported\n");
806 return snprintf(buf, PAGE_SIZE,
807 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
808 ap_configuration->adm[0], ap_configuration->adm[1],
809 ap_configuration->adm[2], ap_configuration->adm[3],
810 ap_configuration->adm[4], ap_configuration->adm[5],
811 ap_configuration->adm[6], ap_configuration->adm[7]);
814 static BUS_ATTR(ap_control_domain_mask, 0444,
815 ap_control_domain_mask_show, NULL);
817 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
819 if (!ap_configuration) /* QCI not supported */
820 return snprintf(buf, PAGE_SIZE, "not supported\n");
822 return snprintf(buf, PAGE_SIZE,
823 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
824 ap_configuration->aqm[0], ap_configuration->aqm[1],
825 ap_configuration->aqm[2], ap_configuration->aqm[3],
826 ap_configuration->aqm[4], ap_configuration->aqm[5],
827 ap_configuration->aqm[6], ap_configuration->aqm[7]);
830 static BUS_ATTR(ap_usage_domain_mask, 0444,
831 ap_usage_domain_mask_show, NULL);
833 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
835 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
838 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
840 return snprintf(buf, PAGE_SIZE, "%d\n",
841 ap_using_interrupts() ? 1 : 0);
844 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
846 static ssize_t ap_config_time_store(struct bus_type *bus,
847 const char *buf, size_t count)
851 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
853 ap_config_time = time;
854 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
858 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
860 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
862 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
865 static ssize_t ap_poll_thread_store(struct bus_type *bus,
866 const char *buf, size_t count)
870 if (sscanf(buf, "%d\n", &flag) != 1)
873 rc = ap_poll_thread_start();
877 ap_poll_thread_stop();
881 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
883 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
885 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
888 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
891 unsigned long long time;
894 /* 120 seconds = maximum poll interval */
895 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
896 time > 120000000000ULL)
899 hr_time = poll_timeout;
901 spin_lock_bh(&ap_poll_timer_lock);
902 hrtimer_cancel(&ap_poll_timer);
903 hrtimer_set_expires(&ap_poll_timer, hr_time);
904 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
905 spin_unlock_bh(&ap_poll_timer_lock);
910 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
912 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
916 if (ap_configuration)
917 max_domain_id = ap_max_domain_id ? : -1;
920 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
923 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
925 static struct bus_attribute *const ap_bus_attrs[] = {
927 &bus_attr_ap_control_domain_mask,
928 &bus_attr_ap_usage_domain_mask,
929 &bus_attr_config_time,
930 &bus_attr_poll_thread,
931 &bus_attr_ap_interrupts,
932 &bus_attr_poll_timeout,
933 &bus_attr_ap_max_domain_id,
938 * ap_select_domain(): Select an AP domain.
940 * Pick one of the 16 AP domains.
942 static int ap_select_domain(void)
944 int count, max_count, best_domain;
945 struct ap_queue_status status;
949 * We want to use a single domain. Either the one specified with
950 * the "domain=" parameter or the domain with the maximum number
953 spin_lock_bh(&ap_domain_lock);
954 if (ap_domain_index >= 0) {
955 /* Domain has already been selected. */
956 spin_unlock_bh(&ap_domain_lock);
961 for (i = 0; i < AP_DOMAINS; i++) {
962 if (!ap_test_config_domain(i))
965 for (j = 0; j < AP_DEVICES; j++) {
966 if (!ap_test_config_card_id(j))
968 status = ap_test_queue(AP_MKQID(j, i),
971 if (status.response_code != AP_RESPONSE_NORMAL)
975 if (count > max_count) {
980 if (best_domain >= 0){
981 ap_domain_index = best_domain;
982 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
983 spin_unlock_bh(&ap_domain_lock);
986 spin_unlock_bh(&ap_domain_lock);
991 * helper function to be used with bus_find_dev
992 * matches for the card device with the given id
994 static int __match_card_device_with_id(struct device *dev, void *data)
996 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
999 /* helper function to be used with bus_find_dev
1000 * matches for the queue device with a given qid
1002 static int __match_queue_device_with_qid(struct device *dev, void *data)
1004 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1008 * ap_scan_bus(): Scan the AP bus for new devices
1009 * Runs periodically, workqueue timer (ap_config_time)
1011 static void ap_scan_bus(struct work_struct *unused)
1013 struct ap_queue *aq;
1017 int depth = 0, type = 0;
1018 unsigned int functions = 0;
1019 int rc, id, dom, borked, domains, defdomdevs = 0;
1021 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
1023 ap_query_configuration(ap_configuration);
1024 if (ap_select_domain() != 0)
1027 for (id = 0; id < AP_DEVICES; id++) {
1028 /* check if device is registered */
1029 dev = bus_find_device(&ap_bus_type, NULL,
1031 __match_card_device_with_id);
1032 ac = dev ? to_ap_card(dev) : NULL;
1033 if (!ap_test_config_card_id(id)) {
1035 /* Card device has been removed from
1036 * configuration, remove the belonging
1039 bus_for_each_dev(&ap_bus_type, NULL,
1041 __ap_queue_devices_with_id_unregister);
1042 /* now remove the card device */
1043 device_unregister(dev);
1048 /* According to the configuration there should be a card
1049 * device, so check if there is at least one valid queue
1050 * and maybe create queue devices and the card device.
1053 for (dom = 0; dom < AP_DOMAINS; dom++) {
1054 qid = AP_MKQID(id, dom);
1055 dev = bus_find_device(&ap_bus_type, NULL,
1057 __match_queue_device_with_qid);
1058 aq = dev ? to_ap_queue(dev) : NULL;
1059 if (!ap_test_config_domain(dom)) {
1061 /* Queue device exists but has been
1062 * removed from configuration.
1064 device_unregister(dev);
1069 rc = ap_query_queue(qid, &depth, &type, &functions);
1071 spin_lock_bh(&aq->lock);
1072 if (rc == -ENODEV ||
1073 /* adapter reconfiguration */
1074 (ac && ac->functions != functions))
1075 aq->state = AP_STATE_BORKED;
1076 borked = aq->state == AP_STATE_BORKED;
1077 spin_unlock_bh(&aq->lock);
1078 if (borked) /* Remove broken device */
1079 device_unregister(dev);
1083 if (dom == ap_domain_index)
1090 /* new queue device needed */
1092 /* but first create the card device */
1093 ac = ap_card_create(id, depth,
1097 ac->ap_dev.device.bus = &ap_bus_type;
1098 ac->ap_dev.device.parent = ap_root_device;
1099 dev_set_name(&ac->ap_dev.device,
1101 /* Register card with AP bus */
1102 rc = device_register(&ac->ap_dev.device);
1104 put_device(&ac->ap_dev.device);
1108 /* get it and thus adjust reference counter */
1109 get_device(&ac->ap_dev.device);
1111 /* now create the new queue device */
1112 aq = ap_queue_create(qid, type);
1116 aq->ap_dev.device.bus = &ap_bus_type;
1117 aq->ap_dev.device.parent = &ac->ap_dev.device;
1118 dev_set_name(&aq->ap_dev.device,
1119 "%02x.%04x", id, dom);
1120 /* Start with a device reset */
1121 spin_lock_bh(&aq->lock);
1122 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1123 spin_unlock_bh(&aq->lock);
1124 /* Register device */
1125 rc = device_register(&aq->ap_dev.device);
1127 put_device(&aq->ap_dev.device);
1131 if (dom == ap_domain_index)
1133 } /* end domain loop */
1135 /* remove card dev if there are no queue devices */
1137 device_unregister(&ac->ap_dev.device);
1138 put_device(&ac->ap_dev.device);
1140 } /* end device loop */
1143 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n",
1147 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1150 static void ap_config_timeout(unsigned long ptr)
1152 if (ap_suspend_flag)
1154 queue_work(system_long_wq, &ap_scan_work);
1157 static void ap_reset_all(void)
1161 for (i = 0; i < AP_DOMAINS; i++) {
1162 if (!ap_test_config_domain(i))
1164 for (j = 0; j < AP_DEVICES; j++) {
1165 if (!ap_test_config_card_id(j))
1167 ap_rapq(AP_MKQID(j, i));
1172 static struct reset_call ap_reset_call = {
1176 int __init ap_debug_init(void)
1178 ap_dbf_info = debug_register("ap", 1, 1,
1179 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1180 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1181 debug_set_level(ap_dbf_info, DBF_ERR);
1186 void ap_debug_exit(void)
1188 debug_unregister(ap_dbf_info);
1192 * ap_module_init(): The module initialization code.
1194 * Initializes the module.
1196 int __init ap_module_init(void)
1201 rc = ap_debug_init();
1205 if (ap_instructions_available() != 0) {
1206 pr_warn("The hardware system does not support AP instructions\n");
1210 /* Get AP configuration data if available */
1211 ap_init_configuration();
1213 if (ap_configuration)
1215 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1218 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1219 pr_warn("%d is not a valid cryptographic domain\n",
1221 ap_domain_index = -1;
1223 /* In resume callback we need to know if the user had set the domain.
1224 * If so, we can not just reset it.
1226 if (ap_domain_index >= 0)
1227 user_set_domain = 1;
1229 if (ap_interrupts_available()) {
1230 rc = register_adapter_interrupt(&ap_airq);
1231 ap_airq_flag = (rc == 0);
1234 register_reset_call(&ap_reset_call);
1236 /* Create /sys/bus/ap. */
1237 rc = bus_register(&ap_bus_type);
1240 for (i = 0; ap_bus_attrs[i]; i++) {
1241 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1246 /* Create /sys/devices/ap. */
1247 ap_root_device = root_device_register("ap");
1248 rc = PTR_RET(ap_root_device);
1252 /* Setup the AP bus rescan timer. */
1253 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1256 * Setup the high resultion poll timer.
1257 * If we are running under z/VM adjust polling to z/VM polling rate.
1260 poll_timeout = 1500000;
1261 spin_lock_init(&ap_poll_timer_lock);
1262 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1263 ap_poll_timer.function = ap_poll_timeout;
1265 /* Start the low priority AP bus poll thread. */
1266 if (ap_thread_flag) {
1267 rc = ap_poll_thread_start();
1272 rc = register_pm_notifier(&ap_power_notifier);
1276 queue_work(system_long_wq, &ap_scan_work);
1282 ap_poll_thread_stop();
1284 hrtimer_cancel(&ap_poll_timer);
1285 root_device_unregister(ap_root_device);
1288 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1289 bus_unregister(&ap_bus_type);
1291 unregister_reset_call(&ap_reset_call);
1292 if (ap_using_interrupts())
1293 unregister_adapter_interrupt(&ap_airq);
1294 kfree(ap_configuration);
1297 device_initcall(ap_module_init);