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