1 /*
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>
8 *
9 * Adjunct processor bus.
10 *
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)
14 * any later version.
15 *
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.
20 *
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.
24 */
25
26 #define KMSG_COMPONENT "ap"
27 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
28
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>
42 #include <asm/airq.h>
43 #include <linux/atomic.h>
44 #include <asm/isc.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>
51
52 #include "ap_bus.h"
53 #include "ap_asm.h"
54 #include "ap_debug.h"
55
56 /*
57 * Module parameters; note though this file itself isn't modular.
58 */
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);
64
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).");
68
69 static struct device *ap_root_device;
70
71 DEFINE_SPINLOCK(ap_list_lock);
72 LIST_HEAD(ap_card_list);
73
74 static struct ap_config_info *ap_configuration;
75 static bool initialised;
76
77 /*
78 * AP bus related debug feature things.
79 */
80 debug_info_t *ap_dbf_info;
81
82 /*
83 * Workqueue timer for bus rescan.
84 */
85 static struct timer_list ap_config_timer;
86 static int ap_config_time = AP_CONFIG_TIME;
87 static void ap_scan_bus(struct work_struct *);
88 static DECLARE_WORK(ap_scan_work, ap_scan_bus);
89
90 /*
91 * Tasklet & timer for AP request polling and interrupts
92 */
93 static void ap_tasklet_fn(unsigned long);
94 static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0);
95 static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait);
96 static struct task_struct *ap_poll_kthread = 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;
103
104 /* Suspend flag */
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;
113
114 /* Adapter interrupt definitions */
115 static void ap_interrupt_handler(struct airq_struct *airq);
116
117 static int ap_airq_flag;
118
119 static struct airq_struct ap_airq = {
120 .handler = ap_interrupt_handler,
121 .isc = AP_ISC,
122 };
123
124 /**
125 * ap_using_interrupts() - Returns non-zero if interrupt support is
126 * available.
127 */
ap_using_interrupts(void)128 static inline int ap_using_interrupts(void)
129 {
130 return ap_airq_flag;
131 }
132
133 /**
134 * ap_airq_ptr() - Get the address of the adapter interrupt indicator
135 *
136 * Returns the address of the local-summary-indicator of the adapter
137 * interrupt handler for AP, or NULL if adapter interrupts are not
138 * available.
139 */
ap_airq_ptr(void)140 void *ap_airq_ptr(void)
141 {
142 if (ap_using_interrupts())
143 return ap_airq.lsi_ptr;
144 return NULL;
145 }
146
147 /**
148 * ap_interrupts_available(): Test if AP interrupts are available.
149 *
150 * Returns 1 if AP interrupts are available.
151 */
ap_interrupts_available(void)152 static int ap_interrupts_available(void)
153 {
154 return test_facility(65);
155 }
156
157 /**
158 * ap_configuration_available(): Test if AP configuration
159 * information is available.
160 *
161 * Returns 1 if AP configuration information is available.
162 */
ap_configuration_available(void)163 static int ap_configuration_available(void)
164 {
165 return test_facility(12);
166 }
167
168 /**
169 * ap_apft_available(): Test if AP facilities test (APFT)
170 * facility is available.
171 *
172 * Returns 1 if APFT is is available.
173 */
ap_apft_available(void)174 static int ap_apft_available(void)
175 {
176 return test_facility(15);
177 }
178
179 /**
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
184 *
185 * Returns AP queue status structure.
186 */
ap_test_queue(ap_qid_t qid,int tbit,unsigned long * info)187 struct ap_queue_status ap_test_queue(ap_qid_t qid,
188 int tbit,
189 unsigned long *info)
190 {
191 if (tbit)
192 qid |= 1UL << 23; /* set T bit*/
193 return ap_tapq(qid, info);
194 }
195 EXPORT_SYMBOL(ap_test_queue);
196
197 /*
198 * ap_query_configuration(): Fetch cryptographic config info
199 *
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.
204 */
ap_query_configuration(struct ap_config_info * info)205 int ap_query_configuration(struct ap_config_info *info)
206 {
207 if (!ap_configuration_available())
208 return -EOPNOTSUPP;
209 if (!info)
210 return -EINVAL;
211 return ap_qci(info);
212 }
213 EXPORT_SYMBOL(ap_query_configuration);
214
215 /**
216 * ap_init_configuration(): Allocate and query configuration array.
217 */
ap_init_configuration(void)218 static void ap_init_configuration(void)
219 {
220 if (!ap_configuration_available())
221 return;
222
223 ap_configuration = kzalloc(sizeof(*ap_configuration), GFP_KERNEL);
224 if (!ap_configuration)
225 return;
226 if (ap_query_configuration(ap_configuration) != 0) {
227 kfree(ap_configuration);
228 ap_configuration = NULL;
229 return;
230 }
231 }
232
233 /*
234 * ap_test_config(): helper function to extract the nrth bit
235 * within the unsigned int array field.
236 */
ap_test_config(unsigned int * field,unsigned int nr)237 static inline int ap_test_config(unsigned int *field, unsigned int nr)
238 {
239 return ap_test_bit((field + (nr >> 5)), (nr & 0x1f));
240 }
241
242 /*
243 * ap_test_config_card_id(): Test, whether an AP card ID is configured.
244 * @id AP card ID
245 *
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
249 */
ap_test_config_card_id(unsigned int id)250 static inline int ap_test_config_card_id(unsigned int id)
251 {
252 if (!ap_configuration) /* QCI not supported */
253 return 1;
254 return ap_test_config(ap_configuration->apm, id);
255 }
256
257 /*
258 * ap_test_config_domain(): Test, whether an AP usage domain is configured.
259 * @domain AP usage domain ID
260 *
261 * Returns 0 if the usage domain is not configured
262 * 1 if the usage domain is configured or
263 * if the configuration information is not available
264 */
ap_test_config_domain(unsigned int domain)265 static inline int ap_test_config_domain(unsigned int domain)
266 {
267 if (!ap_configuration) /* QCI not supported */
268 return domain < 16;
269 return ap_test_config(ap_configuration->aqm, domain);
270 }
271
272 /**
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
278 */
ap_query_queue(ap_qid_t qid,int * queue_depth,int * device_type,unsigned int * facilities)279 static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type,
280 unsigned int *facilities)
281 {
282 struct ap_queue_status status;
283 unsigned long info;
284 int nd;
285
286 if (!ap_test_config_card_id(AP_QID_CARD(qid)))
287 return -ENODEV;
288
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.
307 */
308 case AP_DEVICE_TYPE_CEX2A:
309 case AP_DEVICE_TYPE_CEX3A:
310 *facilities |= 0x08000000;
311 break;
312 case AP_DEVICE_TYPE_CEX2C:
313 case AP_DEVICE_TYPE_CEX3C:
314 *facilities |= 0x10000000;
315 break;
316 default:
317 break;
318 }
319 return 0;
320 case AP_RESPONSE_Q_NOT_AVAIL:
321 case AP_RESPONSE_DECONFIGURED:
322 case AP_RESPONSE_CHECKSTOPPED:
323 case AP_RESPONSE_INVALID_ADDRESS:
324 return -ENODEV;
325 case AP_RESPONSE_RESET_IN_PROGRESS:
326 case AP_RESPONSE_OTHERWISE_CHANGED:
327 case AP_RESPONSE_BUSY:
328 return -EBUSY;
329 default:
330 BUG();
331 }
332 }
333
ap_wait(enum ap_wait wait)334 void ap_wait(enum ap_wait wait)
335 {
336 ktime_t hr_time;
337
338 switch (wait) {
339 case AP_WAIT_AGAIN:
340 case AP_WAIT_INTERRUPT:
341 if (ap_using_interrupts())
342 break;
343 if (ap_poll_kthread) {
344 wake_up(&ap_poll_wait);
345 break;
346 }
347 /* Fall through */
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);
354 }
355 spin_unlock_bh(&ap_poll_timer_lock);
356 break;
357 case AP_WAIT_NONE:
358 default:
359 break;
360 }
361 }
362
363 /**
364 * ap_request_timeout(): Handling of request timeouts
365 * @data: Holds the AP device.
366 *
367 * Handles request timeouts.
368 */
ap_request_timeout(unsigned long data)369 void ap_request_timeout(unsigned long data)
370 {
371 struct ap_queue *aq = (struct ap_queue *) data;
372
373 if (ap_suspend_flag)
374 return;
375 spin_lock_bh(&aq->lock);
376 ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT));
377 spin_unlock_bh(&aq->lock);
378 }
379
380 /**
381 * ap_poll_timeout(): AP receive polling for finished AP requests.
382 * @unused: Unused pointer.
383 *
384 * Schedules the AP tasklet using a high resolution timer.
385 */
ap_poll_timeout(struct hrtimer * unused)386 static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused)
387 {
388 if (!ap_suspend_flag)
389 tasklet_schedule(&ap_tasklet);
390 return HRTIMER_NORESTART;
391 }
392
393 /**
394 * ap_interrupt_handler() - Schedule ap_tasklet on interrupt
395 * @airq: pointer to adapter interrupt descriptor
396 */
ap_interrupt_handler(struct airq_struct * airq)397 static void ap_interrupt_handler(struct airq_struct *airq)
398 {
399 inc_irq_stat(IRQIO_APB);
400 if (!ap_suspend_flag)
401 tasklet_schedule(&ap_tasklet);
402 }
403
404 /**
405 * ap_tasklet_fn(): Tasklet to poll all AP devices.
406 * @dummy: Unused variable
407 *
408 * Poll all AP devices on the bus.
409 */
ap_tasklet_fn(unsigned long dummy)410 static void ap_tasklet_fn(unsigned long dummy)
411 {
412 struct ap_card *ac;
413 struct ap_queue *aq;
414 enum ap_wait wait = AP_WAIT_NONE;
415
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.
419 */
420 if (ap_using_interrupts())
421 xchg(ap_airq.lsi_ptr, 0);
422
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);
429 }
430 }
431 spin_unlock_bh(&ap_list_lock);
432
433 ap_wait(wait);
434 }
435
ap_pending_requests(void)436 static int ap_pending_requests(void)
437 {
438 struct ap_card *ac;
439 struct ap_queue *aq;
440
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)
445 continue;
446 spin_unlock_bh(&ap_list_lock);
447 return 1;
448 }
449 }
450 spin_unlock_bh(&ap_list_lock);
451 return 0;
452 }
453
454 /**
455 * ap_poll_thread(): Thread that polls for finished requests.
456 * @data: Unused pointer
457 *
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
462 * delivered.
463 */
ap_poll_thread(void * data)464 static int ap_poll_thread(void *data)
465 {
466 DECLARE_WAITQUEUE(wait, current);
467
468 set_user_nice(current, MAX_NICE);
469 set_freezable();
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()) {
474 schedule();
475 try_to_freeze();
476 }
477 set_current_state(TASK_RUNNING);
478 remove_wait_queue(&ap_poll_wait, &wait);
479 if (need_resched()) {
480 schedule();
481 try_to_freeze();
482 continue;
483 }
484 ap_tasklet_fn(0);
485 }
486
487 return 0;
488 }
489
ap_poll_thread_start(void)490 static int ap_poll_thread_start(void)
491 {
492 int rc;
493
494 if (ap_using_interrupts() || ap_poll_kthread)
495 return 0;
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);
499 if (rc)
500 ap_poll_kthread = NULL;
501 mutex_unlock(&ap_poll_thread_mutex);
502 return rc;
503 }
504
ap_poll_thread_stop(void)505 static void ap_poll_thread_stop(void)
506 {
507 if (!ap_poll_kthread)
508 return;
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);
513 }
514
515 #define is_card_dev(x) ((x)->parent == ap_root_device)
516 #define is_queue_dev(x) ((x)->parent != ap_root_device)
517
518 /**
519 * ap_bus_match()
520 * @dev: Pointer to device
521 * @drv: Pointer to device_driver
522 *
523 * AP bus driver registration/unregistration.
524 */
ap_bus_match(struct device * dev,struct device_driver * drv)525 static int ap_bus_match(struct device *dev, struct device_driver *drv)
526 {
527 struct ap_driver *ap_drv = to_ap_drv(drv);
528 struct ap_device_id *id;
529
530 /*
531 * Compare device type of the device with the list of
532 * supported types of the device_driver.
533 */
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)
538 return 1;
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)
542 return 1;
543 }
544 return 0;
545 }
546
547 /**
548 * ap_uevent(): Uevent function for AP devices.
549 * @dev: Pointer to device
550 * @env: Pointer to kobj_uevent_env
551 *
552 * It sets up a single environment variable DEV_TYPE which contains the
553 * hardware device type.
554 */
ap_uevent(struct device * dev,struct kobj_uevent_env * env)555 static int ap_uevent (struct device *dev, struct kobj_uevent_env *env)
556 {
557 struct ap_device *ap_dev = to_ap_dev(dev);
558 int retval = 0;
559
560 if (!ap_dev)
561 return -ENODEV;
562
563 /* Set up DEV_TYPE environment variable. */
564 retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type);
565 if (retval)
566 return retval;
567
568 /* Add MODALIAS= */
569 retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type);
570
571 return retval;
572 }
573
ap_dev_suspend(struct device * dev)574 static int ap_dev_suspend(struct device *dev)
575 {
576 struct ap_device *ap_dev = to_ap_dev(dev);
577
578 if (ap_dev->drv && ap_dev->drv->suspend)
579 ap_dev->drv->suspend(ap_dev);
580 return 0;
581 }
582
ap_dev_resume(struct device * dev)583 static int ap_dev_resume(struct device *dev)
584 {
585 struct ap_device *ap_dev = to_ap_dev(dev);
586
587 if (ap_dev->drv && ap_dev->drv->resume)
588 ap_dev->drv->resume(ap_dev);
589 return 0;
590 }
591
ap_bus_suspend(void)592 static void ap_bus_suspend(void)
593 {
594 AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n");
595
596 ap_suspend_flag = 1;
597 /*
598 * Disable scanning for devices, thus we do not want to scan
599 * for them after removing.
600 */
601 flush_work(&ap_scan_work);
602 tasklet_disable(&ap_tasklet);
603 }
604
__ap_card_devices_unregister(struct device * dev,void * dummy)605 static int __ap_card_devices_unregister(struct device *dev, void *dummy)
606 {
607 if (is_card_dev(dev))
608 device_unregister(dev);
609 return 0;
610 }
611
__ap_queue_devices_unregister(struct device * dev,void * dummy)612 static int __ap_queue_devices_unregister(struct device *dev, void *dummy)
613 {
614 if (is_queue_dev(dev))
615 device_unregister(dev);
616 return 0;
617 }
618
__ap_queue_devices_with_id_unregister(struct device * dev,void * data)619 static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data)
620 {
621 if (is_queue_dev(dev) &&
622 AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data)
623 device_unregister(dev);
624 return 0;
625 }
626
ap_bus_resume(void)627 static void ap_bus_resume(void)
628 {
629 int rc;
630
631 AP_DBF(DBF_DEBUG, "ap_bus_resume running\n");
632
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);
639
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);
644 }
645 if (!ap_interrupts_available() && ap_using_interrupts()) {
646 unregister_adapter_interrupt(&ap_airq);
647 ap_airq_flag = 0;
648 }
649 /* Reset domain */
650 if (!user_set_domain)
651 ap_domain_index = -1;
652 /* Get things going again */
653 ap_suspend_flag = 0;
654 if (ap_airq_flag)
655 xchg(ap_airq.lsi_ptr, 0);
656 tasklet_enable(&ap_tasklet);
657 queue_work(system_long_wq, &ap_scan_work);
658 }
659
ap_power_event(struct notifier_block * this,unsigned long event,void * ptr)660 static int ap_power_event(struct notifier_block *this, unsigned long event,
661 void *ptr)
662 {
663 switch (event) {
664 case PM_HIBERNATION_PREPARE:
665 case PM_SUSPEND_PREPARE:
666 ap_bus_suspend();
667 break;
668 case PM_POST_HIBERNATION:
669 case PM_POST_SUSPEND:
670 ap_bus_resume();
671 break;
672 default:
673 break;
674 }
675 return NOTIFY_DONE;
676 }
677 static struct notifier_block ap_power_notifier = {
678 .notifier_call = ap_power_event,
679 };
680
681 static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume);
682
683 static struct bus_type ap_bus_type = {
684 .name = "ap",
685 .match = &ap_bus_match,
686 .uevent = &ap_uevent,
687 .pm = &ap_bus_pm_ops,
688 };
689
ap_device_probe(struct device * dev)690 static int ap_device_probe(struct device *dev)
691 {
692 struct ap_device *ap_dev = to_ap_dev(dev);
693 struct ap_driver *ap_drv = to_ap_drv(dev->driver);
694 int rc;
695
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);
700 else
701 list_add(&to_ap_queue(dev)->list,
702 &to_ap_queue(dev)->card->queues);
703 spin_unlock_bh(&ap_list_lock);
704
705 ap_dev->drv = ap_drv;
706 rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV;
707
708 if (rc) {
709 spin_lock_bh(&ap_list_lock);
710 if (is_card_dev(dev))
711 list_del_init(&to_ap_card(dev)->list);
712 else
713 list_del_init(&to_ap_queue(dev)->list);
714 spin_unlock_bh(&ap_list_lock);
715 ap_dev->drv = NULL;
716 }
717
718 return rc;
719 }
720
ap_device_remove(struct device * dev)721 static int ap_device_remove(struct device *dev)
722 {
723 struct ap_device *ap_dev = to_ap_dev(dev);
724 struct ap_driver *ap_drv = ap_dev->drv;
725
726 if (ap_drv->remove)
727 ap_drv->remove(ap_dev);
728
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);
733 else
734 list_del_init(&to_ap_queue(dev)->list);
735 spin_unlock_bh(&ap_list_lock);
736
737 return 0;
738 }
739
ap_driver_register(struct ap_driver * ap_drv,struct module * owner,char * name)740 int ap_driver_register(struct ap_driver *ap_drv, struct module *owner,
741 char *name)
742 {
743 struct device_driver *drv = &ap_drv->driver;
744
745 if (!initialised)
746 return -ENODEV;
747
748 drv->bus = &ap_bus_type;
749 drv->probe = ap_device_probe;
750 drv->remove = ap_device_remove;
751 drv->owner = owner;
752 drv->name = name;
753 return driver_register(drv);
754 }
755 EXPORT_SYMBOL(ap_driver_register);
756
ap_driver_unregister(struct ap_driver * ap_drv)757 void ap_driver_unregister(struct ap_driver *ap_drv)
758 {
759 driver_unregister(&ap_drv->driver);
760 }
761 EXPORT_SYMBOL(ap_driver_unregister);
762
ap_bus_force_rescan(void)763 void ap_bus_force_rescan(void)
764 {
765 if (ap_suspend_flag)
766 return;
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);
771 }
772 EXPORT_SYMBOL(ap_bus_force_rescan);
773
774 /*
775 * AP bus attributes.
776 */
ap_domain_show(struct bus_type * bus,char * buf)777 static ssize_t ap_domain_show(struct bus_type *bus, char *buf)
778 {
779 return snprintf(buf, PAGE_SIZE, "%d\n", ap_domain_index);
780 }
781
ap_domain_store(struct bus_type * bus,const char * buf,size_t count)782 static ssize_t ap_domain_store(struct bus_type *bus,
783 const char *buf, size_t count)
784 {
785 int domain;
786
787 if (sscanf(buf, "%i\n", &domain) != 1 ||
788 domain < 0 || domain > ap_max_domain_id)
789 return -EINVAL;
790 spin_lock_bh(&ap_domain_lock);
791 ap_domain_index = domain;
792 spin_unlock_bh(&ap_domain_lock);
793
794 AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain);
795
796 return count;
797 }
798
799 static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store);
800
ap_control_domain_mask_show(struct bus_type * bus,char * buf)801 static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf)
802 {
803 if (!ap_configuration) /* QCI not supported */
804 return snprintf(buf, PAGE_SIZE, "not supported\n");
805
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]);
812 }
813
814 static BUS_ATTR(ap_control_domain_mask, 0444,
815 ap_control_domain_mask_show, NULL);
816
ap_usage_domain_mask_show(struct bus_type * bus,char * buf)817 static ssize_t ap_usage_domain_mask_show(struct bus_type *bus, char *buf)
818 {
819 if (!ap_configuration) /* QCI not supported */
820 return snprintf(buf, PAGE_SIZE, "not supported\n");
821
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]);
828 }
829
830 static BUS_ATTR(ap_usage_domain_mask, 0444,
831 ap_usage_domain_mask_show, NULL);
832
ap_config_time_show(struct bus_type * bus,char * buf)833 static ssize_t ap_config_time_show(struct bus_type *bus, char *buf)
834 {
835 return snprintf(buf, PAGE_SIZE, "%d\n", ap_config_time);
836 }
837
ap_interrupts_show(struct bus_type * bus,char * buf)838 static ssize_t ap_interrupts_show(struct bus_type *bus, char *buf)
839 {
840 return snprintf(buf, PAGE_SIZE, "%d\n",
841 ap_using_interrupts() ? 1 : 0);
842 }
843
844 static BUS_ATTR(ap_interrupts, 0444, ap_interrupts_show, NULL);
845
ap_config_time_store(struct bus_type * bus,const char * buf,size_t count)846 static ssize_t ap_config_time_store(struct bus_type *bus,
847 const char *buf, size_t count)
848 {
849 int time;
850
851 if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120)
852 return -EINVAL;
853 ap_config_time = time;
854 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
855 return count;
856 }
857
858 static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store);
859
ap_poll_thread_show(struct bus_type * bus,char * buf)860 static ssize_t ap_poll_thread_show(struct bus_type *bus, char *buf)
861 {
862 return snprintf(buf, PAGE_SIZE, "%d\n", ap_poll_kthread ? 1 : 0);
863 }
864
ap_poll_thread_store(struct bus_type * bus,const char * buf,size_t count)865 static ssize_t ap_poll_thread_store(struct bus_type *bus,
866 const char *buf, size_t count)
867 {
868 int flag, rc;
869
870 if (sscanf(buf, "%d\n", &flag) != 1)
871 return -EINVAL;
872 if (flag) {
873 rc = ap_poll_thread_start();
874 if (rc)
875 count = rc;
876 } else
877 ap_poll_thread_stop();
878 return count;
879 }
880
881 static BUS_ATTR(poll_thread, 0644, ap_poll_thread_show, ap_poll_thread_store);
882
poll_timeout_show(struct bus_type * bus,char * buf)883 static ssize_t poll_timeout_show(struct bus_type *bus, char *buf)
884 {
885 return snprintf(buf, PAGE_SIZE, "%llu\n", poll_timeout);
886 }
887
poll_timeout_store(struct bus_type * bus,const char * buf,size_t count)888 static ssize_t poll_timeout_store(struct bus_type *bus, const char *buf,
889 size_t count)
890 {
891 unsigned long long time;
892 ktime_t hr_time;
893
894 /* 120 seconds = maximum poll interval */
895 if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 ||
896 time > 120000000000ULL)
897 return -EINVAL;
898 poll_timeout = time;
899 hr_time = poll_timeout;
900
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);
906
907 return count;
908 }
909
910 static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store);
911
ap_max_domain_id_show(struct bus_type * bus,char * buf)912 static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf)
913 {
914 int max_domain_id;
915
916 if (ap_configuration)
917 max_domain_id = ap_max_domain_id ? : -1;
918 else
919 max_domain_id = 15;
920 return snprintf(buf, PAGE_SIZE, "%d\n", max_domain_id);
921 }
922
923 static BUS_ATTR(ap_max_domain_id, 0444, ap_max_domain_id_show, NULL);
924
925 static struct bus_attribute *const ap_bus_attrs[] = {
926 &bus_attr_ap_domain,
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,
934 NULL,
935 };
936
937 /**
938 * ap_select_domain(): Select an AP domain.
939 *
940 * Pick one of the 16 AP domains.
941 */
ap_select_domain(void)942 static int ap_select_domain(void)
943 {
944 int count, max_count, best_domain;
945 struct ap_queue_status status;
946 int i, j;
947
948 /*
949 * We want to use a single domain. Either the one specified with
950 * the "domain=" parameter or the domain with the maximum number
951 * of devices.
952 */
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);
957 return 0;
958 }
959 best_domain = -1;
960 max_count = 0;
961 for (i = 0; i < AP_DOMAINS; i++) {
962 if (!ap_test_config_domain(i))
963 continue;
964 count = 0;
965 for (j = 0; j < AP_DEVICES; j++) {
966 if (!ap_test_config_card_id(j))
967 continue;
968 status = ap_test_queue(AP_MKQID(j, i),
969 ap_apft_available(),
970 NULL);
971 if (status.response_code != AP_RESPONSE_NORMAL)
972 continue;
973 count++;
974 }
975 if (count > max_count) {
976 max_count = count;
977 best_domain = i;
978 }
979 }
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);
984 return 0;
985 }
986 spin_unlock_bh(&ap_domain_lock);
987 return -ENODEV;
988 }
989
990 /*
991 * helper function to be used with bus_find_dev
992 * matches for the card device with the given id
993 */
__match_card_device_with_id(struct device * dev,void * data)994 static int __match_card_device_with_id(struct device *dev, void *data)
995 {
996 return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data;
997 }
998
999 /* helper function to be used with bus_find_dev
1000 * matches for the queue device with a given qid
1001 */
__match_queue_device_with_qid(struct device * dev,void * data)1002 static int __match_queue_device_with_qid(struct device *dev, void *data)
1003 {
1004 return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long) data;
1005 }
1006
1007 /**
1008 * ap_scan_bus(): Scan the AP bus for new devices
1009 * Runs periodically, workqueue timer (ap_config_time)
1010 */
ap_scan_bus(struct work_struct * unused)1011 static void ap_scan_bus(struct work_struct *unused)
1012 {
1013 struct ap_queue *aq;
1014 struct ap_card *ac;
1015 struct device *dev;
1016 ap_qid_t qid;
1017 int depth = 0, type = 0;
1018 unsigned int functions = 0;
1019 int rc, id, dom, borked, domains, defdomdevs = 0;
1020
1021 AP_DBF(DBF_DEBUG, "ap_scan_bus running\n");
1022
1023 ap_query_configuration(ap_configuration);
1024 if (ap_select_domain() != 0)
1025 goto out;
1026
1027 for (id = 0; id < AP_DEVICES; id++) {
1028 /* check if device is registered */
1029 dev = bus_find_device(&ap_bus_type, NULL,
1030 (void *)(long) id,
1031 __match_card_device_with_id);
1032 ac = dev ? to_ap_card(dev) : NULL;
1033 if (!ap_test_config_card_id(id)) {
1034 if (dev) {
1035 /* Card device has been removed from
1036 * configuration, remove the belonging
1037 * queue devices.
1038 */
1039 bus_for_each_dev(&ap_bus_type, NULL,
1040 (void *)(long) id,
1041 __ap_queue_devices_with_id_unregister);
1042 /* now remove the card device */
1043 device_unregister(dev);
1044 put_device(dev);
1045 }
1046 continue;
1047 }
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.
1051 */
1052 domains = 0;
1053 for (dom = 0; dom < AP_DOMAINS; dom++) {
1054 qid = AP_MKQID(id, dom);
1055 dev = bus_find_device(&ap_bus_type, NULL,
1056 (void *)(long) qid,
1057 __match_queue_device_with_qid);
1058 aq = dev ? to_ap_queue(dev) : NULL;
1059 if (!ap_test_config_domain(dom)) {
1060 if (dev) {
1061 /* Queue device exists but has been
1062 * removed from configuration.
1063 */
1064 device_unregister(dev);
1065 put_device(dev);
1066 }
1067 continue;
1068 }
1069 rc = ap_query_queue(qid, &depth, &type, &functions);
1070 if (dev) {
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);
1080 put_device(dev);
1081 if (!borked) {
1082 domains++;
1083 if (dom == ap_domain_index)
1084 defdomdevs++;
1085 continue;
1086 }
1087 }
1088 if (rc)
1089 continue;
1090 /* new queue device needed */
1091 if (!ac) {
1092 /* but first create the card device */
1093 ac = ap_card_create(id, depth,
1094 type, functions);
1095 if (!ac)
1096 continue;
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,
1100 "card%02x", id);
1101 /* Register card with AP bus */
1102 rc = device_register(&ac->ap_dev.device);
1103 if (rc) {
1104 put_device(&ac->ap_dev.device);
1105 ac = NULL;
1106 break;
1107 }
1108 /* get it and thus adjust reference counter */
1109 get_device(&ac->ap_dev.device);
1110 }
1111 /* now create the new queue device */
1112 aq = ap_queue_create(qid, type);
1113 if (!aq)
1114 continue;
1115 aq->card = ac;
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);
1126 if (rc) {
1127 put_device(&aq->ap_dev.device);
1128 continue;
1129 }
1130 domains++;
1131 if (dom == ap_domain_index)
1132 defdomdevs++;
1133 } /* end domain loop */
1134 if (ac) {
1135 /* remove card dev if there are no queue devices */
1136 if (!domains)
1137 device_unregister(&ac->ap_dev.device);
1138 put_device(&ac->ap_dev.device);
1139 }
1140 } /* end device loop */
1141
1142 if (defdomdevs < 1)
1143 AP_DBF(DBF_INFO, "no queue device with default domain %d available\n",
1144 ap_domain_index);
1145
1146 out:
1147 mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ);
1148 }
1149
ap_config_timeout(unsigned long ptr)1150 static void ap_config_timeout(unsigned long ptr)
1151 {
1152 if (ap_suspend_flag)
1153 return;
1154 queue_work(system_long_wq, &ap_scan_work);
1155 }
1156
ap_reset_all(void)1157 static void ap_reset_all(void)
1158 {
1159 int i, j;
1160
1161 for (i = 0; i < AP_DOMAINS; i++) {
1162 if (!ap_test_config_domain(i))
1163 continue;
1164 for (j = 0; j < AP_DEVICES; j++) {
1165 if (!ap_test_config_card_id(j))
1166 continue;
1167 ap_rapq(AP_MKQID(j, i));
1168 }
1169 }
1170 }
1171
1172 static struct reset_call ap_reset_call = {
1173 .fn = ap_reset_all,
1174 };
1175
ap_debug_init(void)1176 int __init ap_debug_init(void)
1177 {
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);
1182
1183 return 0;
1184 }
1185
ap_debug_exit(void)1186 void ap_debug_exit(void)
1187 {
1188 debug_unregister(ap_dbf_info);
1189 }
1190
1191 /**
1192 * ap_module_init(): The module initialization code.
1193 *
1194 * Initializes the module.
1195 */
ap_module_init(void)1196 int __init ap_module_init(void)
1197 {
1198 int max_domain_id;
1199 int rc, i;
1200
1201 rc = ap_debug_init();
1202 if (rc)
1203 return rc;
1204
1205 if (ap_instructions_available() != 0) {
1206 pr_warn("The hardware system does not support AP instructions\n");
1207 return -ENODEV;
1208 }
1209
1210 /* Get AP configuration data if available */
1211 ap_init_configuration();
1212
1213 if (ap_configuration)
1214 max_domain_id =
1215 ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1;
1216 else
1217 max_domain_id = 15;
1218 if (ap_domain_index < -1 || ap_domain_index > max_domain_id) {
1219 pr_warn("%d is not a valid cryptographic domain\n",
1220 ap_domain_index);
1221 ap_domain_index = -1;
1222 }
1223 /* In resume callback we need to know if the user had set the domain.
1224 * If so, we can not just reset it.
1225 */
1226 if (ap_domain_index >= 0)
1227 user_set_domain = 1;
1228
1229 if (ap_interrupts_available()) {
1230 rc = register_adapter_interrupt(&ap_airq);
1231 ap_airq_flag = (rc == 0);
1232 }
1233
1234 register_reset_call(&ap_reset_call);
1235
1236 /* Create /sys/bus/ap. */
1237 rc = bus_register(&ap_bus_type);
1238 if (rc)
1239 goto out;
1240 for (i = 0; ap_bus_attrs[i]; i++) {
1241 rc = bus_create_file(&ap_bus_type, ap_bus_attrs[i]);
1242 if (rc)
1243 goto out_bus;
1244 }
1245
1246 /* Create /sys/devices/ap. */
1247 ap_root_device = root_device_register("ap");
1248 rc = PTR_RET(ap_root_device);
1249 if (rc)
1250 goto out_bus;
1251
1252 /* Setup the AP bus rescan timer. */
1253 setup_timer(&ap_config_timer, ap_config_timeout, 0);
1254
1255 /*
1256 * Setup the high resultion poll timer.
1257 * If we are running under z/VM adjust polling to z/VM polling rate.
1258 */
1259 if (MACHINE_IS_VM)
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;
1264
1265 /* Start the low priority AP bus poll thread. */
1266 if (ap_thread_flag) {
1267 rc = ap_poll_thread_start();
1268 if (rc)
1269 goto out_work;
1270 }
1271
1272 rc = register_pm_notifier(&ap_power_notifier);
1273 if (rc)
1274 goto out_pm;
1275
1276 queue_work(system_long_wq, &ap_scan_work);
1277 initialised = true;
1278
1279 return 0;
1280
1281 out_pm:
1282 ap_poll_thread_stop();
1283 out_work:
1284 hrtimer_cancel(&ap_poll_timer);
1285 root_device_unregister(ap_root_device);
1286 out_bus:
1287 while (i--)
1288 bus_remove_file(&ap_bus_type, ap_bus_attrs[i]);
1289 bus_unregister(&ap_bus_type);
1290 out:
1291 unregister_reset_call(&ap_reset_call);
1292 if (ap_using_interrupts())
1293 unregister_adapter_interrupt(&ap_airq);
1294 kfree(ap_configuration);
1295 return rc;
1296 }
1297 device_initcall(ap_module_init);
1298