Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright IBM Corp. 2006, 2012
4 * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
5 * Martin Schwidefsky <schwidefsky@de.ibm.com>
6 * Ralph Wuerthner <rwuerthn@de.ibm.com>
7 * Felix Beck <felix.beck@de.ibm.com>
8 * Holger Dengler <hd@linux.vnet.ibm.com>
9 *
10 * Adjunct processor bus.
11 */
12
13#define KMSG_COMPONENT "ap"
14#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15
16#include <linux/kernel_stat.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/err.h>
21#include <linux/interrupt.h>
22#include <linux/workqueue.h>
23#include <linux/slab.h>
24#include <linux/notifier.h>
25#include <linux/kthread.h>
26#include <linux/mutex.h>
27#include <linux/suspend.h>
28#include <asm/airq.h>
29#include <linux/atomic.h>
30#include <asm/isc.h>
31#include <linux/hrtimer.h>
32#include <linux/ktime.h>
33#include <asm/facility.h>
34#include <linux/crypto.h>
35#include <linux/mod_devicetable.h>
36#include <linux/debugfs.h>
37
38#include "ap_bus.h"
39#include "ap_asm.h"
40#include "ap_debug.h"
41
42/*
43 * Module parameters; note though this file itself isn't modular.
44 */
45int ap_domain_index = -1; /* Adjunct Processor Domain Index */
46static DEFINE_SPINLOCK(ap_domain_lock);
47module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP);
48MODULE_PARM_DESC(domain, "domain index for ap devices");
49EXPORT_SYMBOL(ap_domain_index);
50
51static int ap_thread_flag = 0;
52module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP);
53MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off).");
54
55static struct device *ap_root_device;
56
57DEFINE_SPINLOCK(ap_list_lock);
58LIST_HEAD(ap_card_list);
59
60static struct ap_config_info *ap_configuration;
61static bool initialised;
62
63/*
64 * AP bus related debug feature things.
65 */
66debug_info_t *ap_dbf_info;
67
68/*
69 * Workqueue timer for bus rescan.
70 */
71static struct timer_list ap_config_timer;
72static int ap_config_time = AP_CONFIG_TIME;
73static void ap_scan_bus(struct work_struct *);
74static DECLARE_WORK(ap_scan_work, ap_scan_bus);
75
76/*
77 * Tasklet & timer for AP request polling and interrupts
78 */
79static void ap_tasklet_fn(unsigned long);
80static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
81static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
82static struct task_struct *ap_poll_kthread = NULL;
83static DEFINE_MUTEX(ap_poll_thread_mutex);
84static DEFINE_SPINLOCK(ap_poll_timer_lock);
85static struct hrtimer ap_poll_timer;
86/* In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds.
87 * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling.*/
88static unsigned long long poll_timeout = 250000;
89
90/* Suspend flag */
91static int ap_suspend_flag;
92/* Maximum domain id */
93static int ap_max_domain_id;
94/* Flag to check if domain was set through module parameter domain=. This is
95 * important when supsend and resume is done in a z/VM environment where the
96 * domain might change. */
97static int user_set_domain = 0;
98static struct bus_type ap_bus_type;
99
100/* Adapter interrupt definitions */
101static void ap_interrupt_handler(struct airq_struct *airq);
102
103static int ap_airq_flag;
104
105static struct airq_struct ap_airq = {
106 .handler = ap_interrupt_handler,
107 .isc = AP_ISC,
108};
109
110/**
111 * ap_using_interrupts() - Returns non-zero if interrupt support is
112 * available.
113 */
114static inline int ap_using_interrupts(void)
115{
116 return ap_airq_flag;
117}
118
119/**
120 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
121 *
122 * Returns the address of the local-summary-indicator of the adapter
123 * interrupt handler for AP, or NULL if adapter interrupts are not
124 * available.
125 */
126void *ap_airq_ptr(void)
127{
128 if (ap_using_interrupts())
129 return ap_airq.lsi_ptr;
130 return NULL;
131}
132
133/**
134 * ap_interrupts_available(): Test if AP interrupts are available.
135 *
136 * Returns 1 if AP interrupts are available.
137 */
138static int ap_interrupts_available(void)
139{
140 return test_facility(65);
141}
142
143/**
144 * ap_configuration_available(): Test if AP configuration
145 * information is available.
146 *
147 * Returns 1 if AP configuration information is available.
148 */
149static int ap_configuration_available(void)
150{
151 return test_facility(12);
152}
153
154/**
155 * ap_apft_available(): Test if AP facilities test (APFT)
156 * facility is available.
157 *
158 * Returns 1 if APFT is is available.
159 */
160static int ap_apft_available(void)
161{
162 return test_facility(15);
163}
164
165/*
166 * ap_qact_available(): Test if the PQAP(QACT) subfunction is available.
167 *
168 * Returns 1 if the QACT subfunction is available.
169 */
170static inline int ap_qact_available(void)
171{
172 if (ap_configuration)
173 return ap_configuration->qact;
174 return 0;
175}
176
177/**
178 * ap_test_queue(): Test adjunct processor queue.
179 * @qid: The AP queue number
180 * @tbit: Test facilities bit
181 * @info: Pointer to queue descriptor
182 *
183 * Returns AP queue status structure.
184 */
185struct ap_queue_status ap_test_queue(ap_qid_t qid,
186 int tbit,
187 unsigned long *info)
188{
189 if (tbit)
190 qid |= 1UL << 23; /* set T bit*/
191 return ap_tapq(qid, info);
192}
193EXPORT_SYMBOL(ap_test_queue);
194
195/*
196 * ap_query_configuration(): Fetch cryptographic config info
197 *
198 * Returns the ap configuration info fetched via PQAP(QCI).
199 * On success 0 is returned, on failure a negative errno
200 * is returned, e.g. if the PQAP(QCI) instruction is not
201 * available, the return value will be -EOPNOTSUPP.
202 */
203int ap_query_configuration(struct ap_config_info *info)
204{
205 if (!ap_configuration_available())
206 return -EOPNOTSUPP;
207 if (!info)
208 return -EINVAL;
209 return ap_qci(info);
210}
211EXPORT_SYMBOL(ap_query_configuration);
212
213/**
214 * ap_init_configuration(): Allocate and query configuration array.
215 */
216static void ap_init_configuration(void)
217{
218 if (!ap_configuration_available())
219 return;
220
221 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
222 if (!ap_configuration)
223 return;
224 if (ap_query_configuration(ap_configuration) != 0) {
225 kfree(ap_configuration);
226 ap_configuration = NULL;
227 return;
228 }
229}
230
231/*
232 * ap_test_config(): helper function to extract the nrth bit
233 * within the unsigned int array field.
234 */
235static inline int ap_test_config(unsigned int *field, unsigned int nr)
236{
237 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
238}
239
240/*
241 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
242 * @id AP card ID
243 *
244 * Returns 0 if the card is not configured
245 * 1 if the card is configured or
246 * if the configuration information is not available
247 */
248static inline int ap_test_config_card_id(unsigned int id)
249{
250 if (!ap_configuration) /* QCI not supported */
251 return 1;
252 return ap_test_config(ap_configuration->apm, id);
253}
254
255/*
256 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
257 * @domain AP usage domain ID
258 *
259 * Returns 0 if the usage domain is not configured
260 * 1 if the usage domain is configured or
261 * if the configuration information is not available
262 */
263static inline int ap_test_config_domain(unsigned int domain)
264{
265 if (!ap_configuration) /* QCI not supported */
266 return domain < 16;
267 return ap_test_config(ap_configuration->aqm, domain);
268}
269
270/**
271 * ap_query_queue(): Check if an AP queue is available.
272 * @qid: The AP queue number
273 * @queue_depth: Pointer to queue depth value
274 * @device_type: Pointer to device type value
275 * @facilities: Pointer to facility indicator
276 */
277static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
278 unsigned int *facilities)
279{
280 struct ap_queue_status status;
281 unsigned long info;
282 int nd;
283
284 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
285 return -ENODEV;
286
287 status = ap_test_queue(qid, ap_apft_available(), &info);
288 switch (status.response_code) {
289 case AP_RESPONSE_NORMAL:
290 *queue_depth = (int)(info & 0xff);
291 *device_type = (int)((info >> 24) & 0xff);
292 *facilities = (unsigned int)(info >> 32);
293 /* Update maximum domain id */
294 nd = (info >> 16) & 0xff;
295 /* if N bit is available, z13 and newer */
296 if ((info & (1UL << 57)) && nd > 0)
297 ap_max_domain_id = nd;
298 else /* older machine types */
299 ap_max_domain_id = 15;
300 switch (*device_type) {
301 /* For CEX2 and CEX3 the available functions
302 * are not refrected by the facilities bits.
303 * Instead it is coded into the type. So here
304 * modify the function bits based on the type.
305 */
306 case AP_DEVICE_TYPE_CEX2A:
307 case AP_DEVICE_TYPE_CEX3A:
308 *facilities |= 0x08000000;
309 break;
310 case AP_DEVICE_TYPE_CEX2C:
311 case AP_DEVICE_TYPE_CEX3C:
312 *facilities |= 0x10000000;
313 break;
314 default:
315 break;
316 }
317 return 0;
318 case AP_RESPONSE_Q_NOT_AVAIL:
319 case AP_RESPONSE_DECONFIGURED:
320 case AP_RESPONSE_CHECKSTOPPED:
321 case AP_RESPONSE_INVALID_ADDRESS:
322 return -ENODEV;
323 case AP_RESPONSE_RESET_IN_PROGRESS:
324 case AP_RESPONSE_OTHERWISE_CHANGED:
325 case AP_RESPONSE_BUSY:
326 return -EBUSY;
327 default:
328 BUG();
329 }
330}
331
332void ap_wait(enum ap_wait wait)
333{
334 ktime_t hr_time;
335
336 switch (wait) {
337 case AP_WAIT_AGAIN:
338 case AP_WAIT_INTERRUPT:
339 if (ap_using_interrupts())
340 break;
341 if (ap_poll_kthread) {
342 wake_up(&ap_poll_wait);
343 break;
344 }
345 /* Fall through */
346 case AP_WAIT_TIMEOUT:
347 spin_lock_bh(&ap_poll_timer_lock);
348 if (!hrtimer_is_queued(&ap_poll_timer)) {
349 hr_time = poll_timeout;
350 hrtimer_forward_now(&ap_poll_timer, hr_time);
351 hrtimer_restart(&ap_poll_timer);
352 }
353 spin_unlock_bh(&ap_poll_timer_lock);
354 break;
355 case AP_WAIT_NONE:
356 default:
357 break;
358 }
359}
360
361/**
362 * ap_request_timeout(): Handling of request timeouts
363 * @t: timer making this callback
364 *
365 * Handles request timeouts.
366 */
367void ap_request_timeout(struct timer_list *t)
368{
369 struct ap_queue *aq = from_timer(aq, t, timeout);
370
371 if (ap_suspend_flag)
372 return;
373 spin_lock_bh(&aq->lock);
374 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
375 spin_unlock_bh(&aq->lock);
376}
377
378/**
379 * ap_poll_timeout(): AP receive polling for finished AP requests.
380 * @unused: Unused pointer.
381 *
382 * Schedules the AP tasklet using a high resolution timer.
383 */
384static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
385{
386 if (!ap_suspend_flag)
387 tasklet_schedule(&ap_tasklet);
388 return HRTIMER_NORESTART;
389}
390
391/**
392 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
393 * @airq: pointer to adapter interrupt descriptor
394 */
395static void ap_interrupt_handler(struct airq_struct *airq)
396{
397 inc_irq_stat(IRQIO_APB);
398 if (!ap_suspend_flag)
399 tasklet_schedule(&ap_tasklet);
400}
401
402/**
403 * ap_tasklet_fn(): Tasklet to poll all AP devices.
404 * @dummy: Unused variable
405 *
406 * Poll all AP devices on the bus.
407 */
408static void ap_tasklet_fn(unsigned long dummy)
409{
410 struct ap_card *ac;
411 struct ap_queue *aq;
412 enum ap_wait wait = AP_WAIT_NONE;
413
414 /* Reset the indicator if interrupts are used. Thus new interrupts can
415 * be received. Doing it in the beginning of the tasklet is therefor
416 * important that no requests on any AP get lost.
417 */
418 if (ap_using_interrupts())
419 xchg(ap_airq.lsi_ptr, 0);
420
421 spin_lock_bh(&ap_list_lock);
422 for_each_ap_card(ac) {
423 for_each_ap_queue(aq, ac) {
424 spin_lock_bh(&aq->lock);
425 wait = min(wait, ap_sm_event_loop(aq, AP_EVENT_POLL));
426 spin_unlock_bh(&aq->lock);
427 }
428 }
429 spin_unlock_bh(&ap_list_lock);
430
431 ap_wait(wait);
432}
433
434static int ap_pending_requests(void)
435{
436 struct ap_card *ac;
437 struct ap_queue *aq;
438
439 spin_lock_bh(&ap_list_lock);
440 for_each_ap_card(ac) {
441 for_each_ap_queue(aq, ac) {
442 if (aq->queue_count == 0)
443 continue;
444 spin_unlock_bh(&ap_list_lock);
445 return 1;
446 }
447 }
448 spin_unlock_bh(&ap_list_lock);
449 return 0;
450}
451
452/**
453 * ap_poll_thread(): Thread that polls for finished requests.
454 * @data: Unused pointer
455 *
456 * AP bus poll thread. The purpose of this thread is to poll for
457 * finished requests in a loop if there is a "free" cpu - that is
458 * a cpu that doesn't have anything better to do. The polling stops
459 * as soon as there is another task or if all messages have been
460 * delivered.
461 */
462static int ap_poll_thread(void *data)
463{
464 DECLARE_WAITQUEUE(wait, current);
465
466 set_user_nice(current, MAX_NICE);
467 set_freezable();
468 while (!kthread_should_stop()) {
469 add_wait_queue(&ap_poll_wait, &wait);
470 set_current_state(TASK_INTERRUPTIBLE);
471 if (ap_suspend_flag || !ap_pending_requests()) {
472 schedule();
473 try_to_freeze();
474 }
475 set_current_state(TASK_RUNNING);
476 remove_wait_queue(&ap_poll_wait, &wait);
477 if (need_resched()) {
478 schedule();
479 try_to_freeze();
480 continue;
481 }
482 ap_tasklet_fn(0);
483 }
484
485 return 0;
486}
487
488static int ap_poll_thread_start(void)
489{
490 int rc;
491
492 if (ap_using_interrupts() || ap_poll_kthread)
493 return 0;
494 mutex_lock(&ap_poll_thread_mutex);
495 ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll");
496 rc = PTR_RET(ap_poll_kthread);
497 if (rc)
498 ap_poll_kthread = NULL;
499 mutex_unlock(&ap_poll_thread_mutex);
500 return rc;
501}
502
503static void ap_poll_thread_stop(void)
504{
505 if (!ap_poll_kthread)
506 return;
507 mutex_lock(&ap_poll_thread_mutex);
508 kthread_stop(ap_poll_kthread);
509 ap_poll_kthread = NULL;
510 mutex_unlock(&ap_poll_thread_mutex);
511}
512
513#define is_card_dev(x) ((x)->parent == ap_root_device)
514#define is_queue_dev(x) ((x)->parent != ap_root_device)
515
516/**
517 * ap_bus_match()
518 * @dev: Pointer to device
519 * @drv: Pointer to device_driver
520 *
521 * AP bus driver registration/unregistration.
522 */
523static int ap_bus_match(struct device *dev, struct device_driver *drv)
524{
525 struct ap_driver *ap_drv = to_ap_drv(drv);
526 struct ap_device_id *id;
527
528 /*
529 * Compare device type of the device with the list of
530 * supported types of the device_driver.
531 */
532 for (id = ap_drv->ids; id->match_flags; id++) {
533 if (is_card_dev(dev) &&
534 id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE &&
535 id->dev_type == to_ap_dev(dev)->device_type)
536 return 1;
537 if (is_queue_dev(dev) &&
538 id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE &&
539 id->dev_type == to_ap_dev(dev)->device_type)
540 return 1;
541 }
542 return 0;
543}
544
545/**
546 * ap_uevent(): Uevent function for AP devices.
547 * @dev: Pointer to device
548 * @env: Pointer to kobj_uevent_env
549 *
550 * It sets up a single environment variable DEV_TYPE which contains the
551 * hardware device type.
552 */
553static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
554{
555 struct ap_device *ap_dev = to_ap_dev(dev);
556 int retval = 0;
557
558 if (!ap_dev)
559 return -ENODEV;
560
561 /* Set up DEV_TYPE environment variable. */
562 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
563 if (retval)
564 return retval;
565
566 /* Add MODALIAS= */
567 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
568
569 return retval;
570}
571
572static int ap_dev_suspend(struct device *dev)
573{
574 struct ap_device *ap_dev = to_ap_dev(dev);
575
576 if (ap_dev->drv && ap_dev->drv->suspend)
577 ap_dev->drv->suspend(ap_dev);
578 return 0;
579}
580
581static int ap_dev_resume(struct device *dev)
582{
583 struct ap_device *ap_dev = to_ap_dev(dev);
584
585 if (ap_dev->drv && ap_dev->drv->resume)
586 ap_dev->drv->resume(ap_dev);
587 return 0;
588}
589
590static void ap_bus_suspend(void)
591{
592 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
593
594 ap_suspend_flag = 1;
595 /*
596 * Disable scanning for devices, thus we do not want to scan
597 * for them after removing.
598 */
599 flush_work(&ap_scan_work);
600 tasklet_disable(&ap_tasklet);
601}
602
603static int __ap_card_devices_unregister(struct device *dev, void *dummy)
604{
605 if (is_card_dev(dev))
606 device_unregister(dev);
607 return 0;
608}
609
610static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
611{
612 if (is_queue_dev(dev))
613 device_unregister(dev);
614 return 0;
615}
616
617static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
618{
619 if (is_queue_dev(dev) &&
620 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
621 device_unregister(dev);
622 return 0;
623}
624
625static void ap_bus_resume(void)
626{
627 int rc;
628
629 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
630
631 /* remove all queue devices */
632 bus_for_each_dev(&ap_bus_type, NULL, NULL,
633 __ap_queue_devices_unregister);
634 /* remove all card devices */
635 bus_for_each_dev(&ap_bus_type, NULL, NULL,
636 __ap_card_devices_unregister);
637
638 /* Reset thin interrupt setting */
639 if (ap_interrupts_available() && !ap_using_interrupts()) {
640 rc = register_adapter_interrupt(&ap_airq);
641 ap_airq_flag = (rc == 0);
642 }
643 if (!ap_interrupts_available() && ap_using_interrupts()) {
644 unregister_adapter_interrupt(&ap_airq);
645 ap_airq_flag = 0;
646 }
647 /* Reset domain */
648 if (!user_set_domain)
649 ap_domain_index = -1;
650 /* Get things going again */
651 ap_suspend_flag = 0;
652 if (ap_airq_flag)
653 xchg(ap_airq.lsi_ptr, 0);
654 tasklet_enable(&ap_tasklet);
655 queue_work(system_long_wq, &ap_scan_work);
656}
657
658static int ap_power_event(struct notifier_block *this, unsigned long event,
659 void *ptr)
660{
661 switch (event) {
662 case PM_HIBERNATION_PREPARE:
663 case PM_SUSPEND_PREPARE:
664 ap_bus_suspend();
665 break;
666 case PM_POST_HIBERNATION:
667 case PM_POST_SUSPEND:
668 ap_bus_resume();
669 break;
670 default:
671 break;
672 }
673 return NOTIFY_DONE;
674}
675static struct notifier_block ap_power_notifier = {
676 .notifier_call = ap_power_event,
677};
678
679static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
680
681static struct bus_type ap_bus_type = {
682 .name = "ap",
683 .match = &ap_bus_match,
684 .uevent = &ap_uevent,
685 .pm = &ap_bus_pm_ops,
686};
687
688static int ap_device_probe(struct device *dev)
689{
690 struct ap_device *ap_dev = to_ap_dev(dev);
691 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
692 int rc;
693
694 /* Add queue/card to list of active queues/cards */
695 spin_lock_bh(&ap_list_lock);
696 if (is_card_dev(dev))
697 list_add(&to_ap_card(dev)->list, &ap_card_list);
698 else
699 list_add(&to_ap_queue(dev)->list,
700 &to_ap_queue(dev)->card->queues);
701 spin_unlock_bh(&ap_list_lock);
702
703 ap_dev->drv = ap_drv;
704 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
705
706 if (rc) {
707 spin_lock_bh(&ap_list_lock);
708 if (is_card_dev(dev))
709 list_del_init(&to_ap_card(dev)->list);
710 else
711 list_del_init(&to_ap_queue(dev)->list);
712 spin_unlock_bh(&ap_list_lock);
713 ap_dev->drv = NULL;
714 }
715
716 return rc;
717}
718
719static int ap_device_remove(struct device *dev)
720{
721 struct ap_device *ap_dev = to_ap_dev(dev);
722 struct ap_driver *ap_drv = ap_dev->drv;
723
724 if (ap_drv->remove)
725 ap_drv->remove(ap_dev);
726
727 /* Remove queue/card from list of active queues/cards */
728 spin_lock_bh(&ap_list_lock);
729 if (is_card_dev(dev))
730 list_del_init(&to_ap_card(dev)->list);
731 else
732 list_del_init(&to_ap_queue(dev)->list);
733 spin_unlock_bh(&ap_list_lock);
734
735 return 0;
736}
737
738int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
739 char *name)
740{
741 struct device_driver *drv = &ap_drv->driver;
742
743 if (!initialised)
744 return -ENODEV;
745
746 drv->bus = &ap_bus_type;
747 drv->probe = ap_device_probe;
748 drv->remove = ap_device_remove;
749 drv->owner = owner;
750 drv->name = name;
751 return driver_register(drv);
752}
753EXPORT_SYMBOL(ap_driver_register);
754
755void ap_driver_unregister(struct ap_driver *ap_drv)
756{
757 driver_unregister(&ap_drv->driver);
758}
759EXPORT_SYMBOL(ap_driver_unregister);
760
761void ap_bus_force_rescan(void)
762{
763 if (ap_suspend_flag)
764 return;
765 /* processing a asynchronous bus rescan */
766 del_timer(&ap_config_timer);
767 queue_work(system_long_wq, &ap_scan_work);
768 flush_work(&ap_scan_work);
769}
770EXPORT_SYMBOL(ap_bus_force_rescan);
771
772/*
773 * AP bus attributes.
774 */
775static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
776{
777 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
778}
779
780static ssize_t ap_domain_store(struct bus_type *bus,
781 const char *buf, size_t count)
782{
783 int domain;
784
785 if (sscanf(buf, "%i\n", &domain) != 1 ||
786 domain < 0 || domain > ap_max_domain_id)
787 return -EINVAL;
788 spin_lock_bh(&ap_domain_lock);
789 ap_domain_index = domain;
790 spin_unlock_bh(&ap_domain_lock);
791
792 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
793
794 return count;
795}
796
797static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
798
799static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
800{
801 if (!ap_configuration) /* QCI not supported */
802 return snprintf(buf, PAGE_SIZE, "not supported\n");
803
804 return snprintf(buf, PAGE_SIZE,
805 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
806 ap_configuration->adm[0], ap_configuration->adm[1],
807 ap_configuration->adm[2], ap_configuration->adm[3],
808 ap_configuration->adm[4], ap_configuration->adm[5],
809 ap_configuration->adm[6], ap_configuration->adm[7]);
810}
811
812static BUS_ATTR(ap_control_domain_mask, 0444,
813 ap_control_domain_mask_show, NULL);
814
815static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
816{
817 if (!ap_configuration) /* QCI not supported */
818 return snprintf(buf, PAGE_SIZE, "not supported\n");
819
820 return snprintf(buf, PAGE_SIZE,
821 "0x%08x%08x%08x%08x%08x%08x%08x%08x\n",
822 ap_configuration->aqm[0], ap_configuration->aqm[1],
823 ap_configuration->aqm[2], ap_configuration->aqm[3],
824 ap_configuration->aqm[4], ap_configuration->aqm[5],
825 ap_configuration->aqm[6], ap_configuration->aqm[7]);
826}
827
828static BUS_ATTR(ap_usage_domain_mask, 0444,
829 ap_usage_domain_mask_show, NULL);
830
831static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
832{
833 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
834}
835
836static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
837{
838 return snprintf(buf, PAGE_SIZE, "%d\n",
839 ap_using_interrupts() ? 1 : 0);
840}
841
842static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
843
844static ssize_t ap_config_time_store(struct bus_type *bus,
845 const char *buf, size_t count)
846{
847 int time;
848
849 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
850 return -EINVAL;
851 ap_config_time = time;
852 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
853 return count;
854}
855
856static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
857
858static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
859{
860 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
861}
862
863static ssize_t ap_poll_thread_store(struct bus_type *bus,
864 const char *buf, size_t count)
865{
866 int flag, rc;
867
868 if (sscanf(buf, "%d\n", &flag) != 1)
869 return -EINVAL;
870 if (flag) {
871 rc = ap_poll_thread_start();
872 if (rc)
873 count = rc;
874 } else
875 ap_poll_thread_stop();
876 return count;
877}
878
879static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
880
881static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
882{
883 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
884}
885
886static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
887 size_t count)
888{
889 unsigned long long time;
890 ktime_t hr_time;
891
892 /* 120 seconds = maximum poll interval */
893 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
894 time > 120000000000ULL)
895 return -EINVAL;
896 poll_timeout = time;
897 hr_time = poll_timeout;
898
899 spin_lock_bh(&ap_poll_timer_lock);
900 hrtimer_cancel(&ap_poll_timer);
901 hrtimer_set_expires(&ap_poll_timer, hr_time);
902 hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS);
903 spin_unlock_bh(&ap_poll_timer_lock);
904
905 return count;
906}
907
908static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
909
910static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
911{
912 int max_domain_id;
913
914 if (ap_configuration)
915 max_domain_id = ap_max_domain_id ? : -1;
916 else
917 max_domain_id = 15;
918 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
919}
920
921static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
922
923static struct bus_attribute *const ap_bus_attrs[] = {
924 &bus_attr_ap_domain,
925 &bus_attr_ap_control_domain_mask,
926 &bus_attr_ap_usage_domain_mask,
927 &bus_attr_config_time,
928 &bus_attr_poll_thread,
929 &bus_attr_ap_interrupts,
930 &bus_attr_poll_timeout,
931 &bus_attr_ap_max_domain_id,
932 NULL,
933};
934
935/**
936 * ap_select_domain(): Select an AP domain.
937 *
938 * Pick one of the 16 AP domains.
939 */
940static int ap_select_domain(void)
941{
942 int count, max_count, best_domain;
943 struct ap_queue_status status;
944 int i, j;
945
946 /*
947 * We want to use a single domain. Either the one specified with
948 * the "domain=" parameter or the domain with the maximum number
949 * of devices.
950 */
951 spin_lock_bh(&ap_domain_lock);
952 if (ap_domain_index >= 0) {
953 /* Domain has already been selected. */
954 spin_unlock_bh(&ap_domain_lock);
955 return 0;
956 }
957 best_domain = -1;
958 max_count = 0;
959 for (i = 0; i < AP_DOMAINS; i++) {
960 if (!ap_test_config_domain(i))
961 continue;
962 count = 0;
963 for (j = 0; j < AP_DEVICES; j++) {
964 if (!ap_test_config_card_id(j))
965 continue;
966 status = ap_test_queue(AP_MKQID(j, i),
967 ap_apft_available(),
968 NULL);
969 if (status.response_code != AP_RESPONSE_NORMAL)
970 continue;
971 count++;
972 }
973 if (count > max_count) {
974 max_count = count;
975 best_domain = i;
976 }
977 }
978 if (best_domain >= 0){
979 ap_domain_index = best_domain;
980 AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index);
981 spin_unlock_bh(&ap_domain_lock);
982 return 0;
983 }
984 spin_unlock_bh(&ap_domain_lock);
985 return -ENODEV;
986}
987
988/*
989 * This function checks the type and returns either 0 for not
990 * supported or the highest compatible type value (which may
991 * include the input type value).
992 */
993static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func)
994{
995 int comp_type = 0;
996
997 /* < CEX2A is not supported */
998 if (rawtype < AP_DEVICE_TYPE_CEX2A)
999 return 0;
1000 /* up to CEX6 known and fully supported */
1001 if (rawtype <= AP_DEVICE_TYPE_CEX6)
1002 return rawtype;
1003 /*
1004 * unknown new type > CEX6, check for compatibility
1005 * to the highest known and supported type which is
1006 * currently CEX6 with the help of the QACT function.
1007 */
1008 if (ap_qact_available()) {
1009 struct ap_queue_status status;
1010 union ap_qact_ap_info apinfo = {0};
1011
1012 apinfo.mode = (func >> 26) & 0x07;
1013 apinfo.cat = AP_DEVICE_TYPE_CEX6;
1014 status = ap_qact(qid, 0, &apinfo);
1015 if (status.response_code == AP_RESPONSE_NORMAL
1016 && apinfo.cat >= AP_DEVICE_TYPE_CEX2A
1017 && apinfo.cat <= AP_DEVICE_TYPE_CEX6)
1018 comp_type = apinfo.cat;
1019 }
1020 if (!comp_type)
1021 AP_DBF(DBF_WARN, "queue=%02x.%04x unable to map type %d\n",
1022 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype);
1023 else if (comp_type != rawtype)
1024 AP_DBF(DBF_INFO, "queue=%02x.%04x map type %d to %d\n",
1025 AP_QID_CARD(qid), AP_QID_QUEUE(qid), rawtype, comp_type);
1026 return comp_type;
1027}
1028
1029/*
1030 * helper function to be used with bus_find_dev
1031 * matches for the card device with the given id
1032 */
1033static int __match_card_device_with_id(struct device *dev, void *data)
1034{
1035 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
1036}
1037
1038/* helper function to be used with bus_find_dev
1039 * matches for the queue device with a given qid
1040 */
1041static int __match_queue_device_with_qid(struct device *dev, void *data)
1042{
1043 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1044}
1045
1046/**
1047 * ap_scan_bus(): Scan the AP bus for new devices
1048 * Runs periodically, workqueue timer (ap_config_time)
1049 */
1050static void ap_scan_bus(struct work_struct *unused)
1051{
1052 struct ap_queue *aq;
1053 struct ap_card *ac;
1054 struct device *dev;
1055 ap_qid_t qid;
1056 int comp_type, depth = 0, type = 0;
1057 unsigned int func = 0;
1058 int rc, id, dom, borked, domains, defdomdevs = 0;
1059
1060 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
1061
1062 ap_query_configuration(ap_configuration);
1063 if (ap_select_domain() != 0)
1064 goto out;
1065
1066 for (id = 0; id < AP_DEVICES; id++) {
1067 /* check if device is registered */
1068 dev = bus_find_device(&ap_bus_type, NULL,
1069 (void *)(long) id,
1070 __match_card_device_with_id);
1071 ac = dev ? to_ap_card(dev) : NULL;
1072 if (!ap_test_config_card_id(id)) {
1073 if (dev) {
1074 /* Card device has been removed from
1075 * configuration, remove the belonging
1076 * queue devices.
1077 */
1078 bus_for_each_dev(&ap_bus_type, NULL,
1079 (void *)(long) id,
1080 __ap_queue_devices_with_id_unregister);
1081 /* now remove the card device */
1082 device_unregister(dev);
1083 put_device(dev);
1084 }
1085 continue;
1086 }
1087 /* According to the configuration there should be a card
1088 * device, so check if there is at least one valid queue
1089 * and maybe create queue devices and the card device.
1090 */
1091 domains = 0;
1092 for (dom = 0; dom < AP_DOMAINS; dom++) {
1093 qid = AP_MKQID(id, dom);
1094 dev = bus_find_device(&ap_bus_type, NULL,
1095 (void *)(long) qid,
1096 __match_queue_device_with_qid);
1097 aq = dev ? to_ap_queue(dev) : NULL;
1098 if (!ap_test_config_domain(dom)) {
1099 if (dev) {
1100 /* Queue device exists but has been
1101 * removed from configuration.
1102 */
1103 device_unregister(dev);
1104 put_device(dev);
1105 }
1106 continue;
1107 }
1108 rc = ap_query_queue(qid, &depth, &type, &func);
1109 if (dev) {
1110 spin_lock_bh(&aq->lock);
1111 if (rc == -ENODEV ||
1112 /* adapter reconfiguration */
1113 (ac && ac->functions != func))
1114 aq->state = AP_STATE_BORKED;
1115 borked = aq->state == AP_STATE_BORKED;
1116 spin_unlock_bh(&aq->lock);
1117 if (borked) /* Remove broken device */
1118 device_unregister(dev);
1119 put_device(dev);
1120 if (!borked) {
1121 domains++;
1122 if (dom == ap_domain_index)
1123 defdomdevs++;
1124 continue;
1125 }
1126 }
1127 if (rc)
1128 continue;
1129 /* a new queue device is needed, check out comp type */
1130 comp_type = ap_get_compatible_type(qid, type, func);
1131 if (!comp_type)
1132 continue;
1133 /* maybe a card device needs to be created first */
1134 if (!ac) {
1135 ac = ap_card_create(id, depth, type,
1136 comp_type, func);
1137 if (!ac)
1138 continue;
1139 ac->ap_dev.device.bus = &ap_bus_type;
1140 ac->ap_dev.device.parent = ap_root_device;
1141 dev_set_name(&ac->ap_dev.device,
1142 "card%02x", id);
1143 /* Register card with AP bus */
1144 rc = device_register(&ac->ap_dev.device);
1145 if (rc) {
1146 put_device(&ac->ap_dev.device);
1147 ac = NULL;
1148 break;
1149 }
1150 /* get it and thus adjust reference counter */
1151 get_device(&ac->ap_dev.device);
1152 }
1153 /* now create the new queue device */
1154 aq = ap_queue_create(qid, comp_type);
1155 if (!aq)
1156 continue;
1157 aq->card = ac;
1158 aq->ap_dev.device.bus = &ap_bus_type;
1159 aq->ap_dev.device.parent = &ac->ap_dev.device;
1160 dev_set_name(&aq->ap_dev.device,
1161 "%02x.%04x", id, dom);
1162 /* Start with a device reset */
1163 spin_lock_bh(&aq->lock);
1164 ap_wait(ap_sm_event(aq, AP_EVENT_POLL));
1165 spin_unlock_bh(&aq->lock);
1166 /* Register device */
1167 rc = device_register(&aq->ap_dev.device);
1168 if (rc) {
1169 put_device(&aq->ap_dev.device);
1170 continue;
1171 }
1172 domains++;
1173 if (dom == ap_domain_index)
1174 defdomdevs++;
1175 } /* end domain loop */
1176 if (ac) {
1177 /* remove card dev if there are no queue devices */
1178 if (!domains)
1179 device_unregister(&ac->ap_dev.device);
1180 put_device(&ac->ap_dev.device);
1181 }
1182 } /* end device loop */
1183
1184 if (defdomdevs < 1)
1185 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n",
1186 ap_domain_index);
1187
1188out:
1189 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1190}
1191
1192static void ap_config_timeout(struct timer_list *unused)
1193{
1194 if (ap_suspend_flag)
1195 return;
1196 queue_work(system_long_wq, &ap_scan_work);
1197}
1198
1199static int __init ap_debug_init(void)
1200{
1201 ap_dbf_info = debug_register("ap", 1, 1,
1202 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1203 debug_register_view(ap_dbf_info, &debug_sprintf_view);
1204 debug_set_level(ap_dbf_info, DBF_ERR);
1205
1206 return 0;
1207}
1208
1209/**
1210 * ap_module_init(): The module initialization code.
1211 *
1212 * Initializes the module.
1213 */
1214static int __init ap_module_init(void)
1215{
1216 int max_domain_id;
1217 int rc, i;
1218
1219 rc = ap_debug_init();
1220 if (rc)
1221 return rc;
1222
1223 if (ap_instructions_available() != 0) {
1224 pr_warn("The hardware system does not support AP instructions\n");
1225 return -ENODEV;
1226 }
1227
1228 /* Get AP configuration data if available */
1229 ap_init_configuration();
1230
1231 if (ap_configuration)
1232 max_domain_id =
1233 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1234 else
1235 max_domain_id = 15;
1236 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1237 pr_warn("%d is not a valid cryptographic domain\n",
1238 ap_domain_index);
1239 ap_domain_index = -1;
1240 }
1241 /* In resume callback we need to know if the user had set the domain.
1242 * If so, we can not just reset it.
1243 */
1244 if (ap_domain_index >= 0)
1245 user_set_domain = 1;
1246
1247 if (ap_interrupts_available()) {
1248 rc = register_adapter_interrupt(&ap_airq);
1249 ap_airq_flag = (rc == 0);
1250 }
1251
1252 /* Create /sys/bus/ap. */
1253 rc = bus_register(&ap_bus_type);
1254 if (rc)
1255 goto out;
1256 for (i = 0; ap_bus_attrs[i]; i++) {
1257 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1258 if (rc)
1259 goto out_bus;
1260 }
1261
1262 /* Create /sys/devices/ap. */
1263 ap_root_device = root_device_register("ap");
1264 rc = PTR_RET(ap_root_device);
1265 if (rc)
1266 goto out_bus;
1267
1268 /* Setup the AP bus rescan timer. */
1269 timer_setup(&ap_config_timer, ap_config_timeout, 0);
1270
1271 /*
1272 * Setup the high resultion poll timer.
1273 * If we are running under z/VM adjust polling to z/VM polling rate.
1274 */
1275 if (MACHINE_IS_VM)
1276 poll_timeout = 1500000;
1277 spin_lock_init(&ap_poll_timer_lock);
1278 hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1279 ap_poll_timer.function = ap_poll_timeout;
1280
1281 /* Start the low priority AP bus poll thread. */
1282 if (ap_thread_flag) {
1283 rc = ap_poll_thread_start();
1284 if (rc)
1285 goto out_work;
1286 }
1287
1288 rc = register_pm_notifier(&ap_power_notifier);
1289 if (rc)
1290 goto out_pm;
1291
1292 queue_work(system_long_wq, &ap_scan_work);
1293 initialised = true;
1294
1295 return 0;
1296
1297out_pm:
1298 ap_poll_thread_stop();
1299out_work:
1300 hrtimer_cancel(&ap_poll_timer);
1301 root_device_unregister(ap_root_device);
1302out_bus:
1303 while (i--)
1304 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1305 bus_unregister(&ap_bus_type);
1306out:
1307 if (ap_using_interrupts())
1308 unregister_adapter_interrupt(&ap_airq);
1309 kfree(ap_configuration);
1310 return rc;
1311}
1312device_initcall(ap_module_init);