]>
Commit | Line | Data |
---|---|---|
1534c382 | 1 | /* |
75014550 | 2 | * Copyright IBM Corp. 2006, 2012 |
1534c382 MS |
3 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
4 | * Martin Schwidefsky <schwidefsky@de.ibm.com> | |
5 | * Ralph Wuerthner <rwuerthn@de.ibm.com> | |
cb17a636 | 6 | * Felix Beck <felix.beck@de.ibm.com> |
6bed05bc | 7 | * Holger Dengler <hd@linux.vnet.ibm.com> |
1534c382 MS |
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 | ||
136f7a1c MS |
26 | #define KMSG_COMPONENT "ap" |
27 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
28 | ||
62d146ff | 29 | #include <linux/kernel_stat.h> |
50a0d46c | 30 | #include <linux/moduleparam.h> |
1534c382 MS |
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> | |
5a0e3ad6 | 36 | #include <linux/slab.h> |
1534c382 MS |
37 | #include <linux/notifier.h> |
38 | #include <linux/kthread.h> | |
39 | #include <linux/mutex.h> | |
83e9d5d2 | 40 | #include <linux/suspend.h> |
85eca850 | 41 | #include <asm/reset.h> |
cb17a636 | 42 | #include <asm/airq.h> |
60063497 | 43 | #include <linux/atomic.h> |
cb17a636 | 44 | #include <asm/isc.h> |
fe137230 FB |
45 | #include <linux/hrtimer.h> |
46 | #include <linux/ktime.h> | |
a0616cde | 47 | #include <asm/facility.h> |
5d26a105 | 48 | #include <linux/crypto.h> |
e28d2af4 | 49 | #include <linux/mod_devicetable.h> |
cccd85bf | 50 | #include <linux/debugfs.h> |
1534c382 MS |
51 | |
52 | #include "ap_bus.h" | |
0db78559 | 53 | #include "ap_asm.h" |
cccd85bf | 54 | #include "ap_debug.h" |
1534c382 | 55 | |
1749a81d | 56 | /* |
50a0d46c | 57 | * Module parameters; note though this file itself isn't modular. |
1534c382 MS |
58 | */ |
59 | int ap_domain_index = -1; /* Adjunct Processor Domain Index */ | |
fc1d3f02 | 60 | static DEFINE_SPINLOCK(ap_domain_lock); |
c1a42f49 | 61 | module_param_named(domain, ap_domain_index, int, S_IRUSR|S_IRGRP); |
1534c382 MS |
62 | MODULE_PARM_DESC(domain, "domain index for ap devices"); |
63 | EXPORT_SYMBOL(ap_domain_index); | |
64 | ||
b90b34c6 | 65 | static int ap_thread_flag = 0; |
c1a42f49 | 66 | module_param_named(poll_thread, ap_thread_flag, int, S_IRUSR|S_IRGRP); |
b90b34c6 | 67 | MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); |
1534c382 | 68 | |
e28d2af4 IT |
69 | static struct device *ap_root_device; |
70 | ||
71 | DEFINE_SPINLOCK(ap_list_lock); | |
72 | LIST_HEAD(ap_card_list); | |
73 | ||
75014550 | 74 | static struct ap_config_info *ap_configuration; |
e387753c | 75 | static bool initialised; |
1534c382 | 76 | |
cccd85bf HF |
77 | /* |
78 | * AP bus related debug feature things. | |
79 | */ | |
cccd85bf HF |
80 | debug_info_t *ap_dbf_info; |
81 | ||
1749a81d | 82 | /* |
8139b89d | 83 | * Workqueue timer for bus rescan. |
1534c382 | 84 | */ |
1534c382 MS |
85 | static struct timer_list ap_config_timer; |
86 | static int ap_config_time = AP_CONFIG_TIME; | |
3f3007af | 87 | static void ap_scan_bus(struct work_struct *); |
8139b89d | 88 | static DECLARE_WORK(ap_scan_work, ap_scan_bus); |
1534c382 | 89 | |
1749a81d | 90 | /* |
cb17a636 | 91 | * Tasklet & timer for AP request polling and interrupts |
1534c382 | 92 | */ |
3f3007af MS |
93 | static void ap_tasklet_fn(unsigned long); |
94 | static DECLARE_TASKLET(ap_tasklet, ap_tasklet_fn, 0); | |
1534c382 MS |
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); | |
93521314 | 98 | static DEFINE_SPINLOCK(ap_poll_timer_lock); |
fe137230 FB |
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; | |
1534c382 | 103 | |
772f5472 FB |
104 | /* Suspend flag */ |
105 | static int ap_suspend_flag; | |
889875a1 MS |
106 | /* Maximum domain id */ |
107 | static int ap_max_domain_id; | |
5314af69 FB |
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; | |
772f5472 FB |
112 | static struct bus_type ap_bus_type; |
113 | ||
f4eae94f | 114 | /* Adapter interrupt definitions */ |
3f3007af MS |
115 | static void ap_interrupt_handler(struct airq_struct *airq); |
116 | ||
f4eae94f MS |
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 | ||
cb17a636 FB |
124 | /** |
125 | * ap_using_interrupts() - Returns non-zero if interrupt support is | |
126 | * available. | |
127 | */ | |
128 | static inline int ap_using_interrupts(void) | |
129 | { | |
f4eae94f | 130 | return ap_airq_flag; |
cb17a636 FB |
131 | } |
132 | ||
e28d2af4 IT |
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 | */ | |
140 | void *ap_airq_ptr(void) | |
141 | { | |
142 | if (ap_using_interrupts()) | |
143 | return ap_airq.lsi_ptr; | |
144 | return NULL; | |
145 | } | |
146 | ||
cb17a636 FB |
147 | /** |
148 | * ap_interrupts_available(): Test if AP interrupts are available. | |
149 | * | |
150 | * Returns 1 if AP interrupts are available. | |
151 | */ | |
152 | static int ap_interrupts_available(void) | |
153 | { | |
86cd741b | 154 | return test_facility(65); |
cb17a636 FB |
155 | } |
156 | ||
75014550 HD |
157 | /** |
158 | * ap_configuration_available(): Test if AP configuration | |
159 | * information is available. | |
160 | * | |
161 | * Returns 1 if AP configuration information is available. | |
162 | */ | |
163 | static int ap_configuration_available(void) | |
164 | { | |
86cd741b | 165 | return test_facility(12); |
75014550 HD |
166 | } |
167 | ||
e7fc5146 TK |
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 | */ | |
174 | static int ap_apft_available(void) | |
175 | { | |
176 | return test_facility(15); | |
177 | } | |
178 | ||
1534c382 | 179 | /** |
1749a81d FB |
180 | * ap_test_queue(): Test adjunct processor queue. |
181 | * @qid: The AP queue number | |
e7fc5146 | 182 | * @tbit: Test facilities bit |
6acbe21f | 183 | * @info: Pointer to queue descriptor |
1534c382 | 184 | * |
1749a81d | 185 | * Returns AP queue status structure. |
1534c382 | 186 | */ |
e7fc5146 TK |
187 | struct ap_queue_status ap_test_queue(ap_qid_t qid, |
188 | int tbit, | |
189 | unsigned long *info) | |
1534c382 | 190 | { |
e7fc5146 TK |
191 | if (tbit) |
192 | qid |= 1UL << 23; /* set T bit*/ | |
0db78559 | 193 | return ap_tapq(qid, info); |
75014550 | 194 | } |
e7fc5146 | 195 | EXPORT_SYMBOL(ap_test_queue); |
75014550 | 196 | |
050349b5 HF |
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 | */ | |
205 | int ap_query_configuration(struct ap_config_info *info) | |
11d37673 | 206 | { |
050349b5 | 207 | if (!ap_configuration_available()) |
11d37673 | 208 | return -EOPNOTSUPP; |
050349b5 HF |
209 | if (!info) |
210 | return -EINVAL; | |
211 | return ap_qci(info); | |
11d37673 | 212 | } |
050349b5 | 213 | EXPORT_SYMBOL(ap_query_configuration); |
11d37673 | 214 | |
889875a1 MS |
215 | /** |
216 | * ap_init_configuration(): Allocate and query configuration array. | |
217 | */ | |
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; | |
050349b5 | 226 | if (ap_query_configuration(ap_configuration) != 0) { |
889875a1 MS |
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 | */ | |
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 | */ | |
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 | */ | |
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 | ||
1534c382 | 272 | /** |
1749a81d FB |
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 | |
6acbe21f | 277 | * @facilities: Pointer to facility indicator |
1534c382 | 278 | */ |
6acbe21f MS |
279 | static int ap_query_queue(ap_qid_t qid, int *queue_depth, int *device_type, |
280 | unsigned int *facilities) | |
1534c382 MS |
281 | { |
282 | struct ap_queue_status status; | |
6acbe21f | 283 | unsigned long info; |
889875a1 MS |
284 | int nd; |
285 | ||
e28d2af4 | 286 | if (!ap_test_config_card_id(AP_QID_CARD(qid))) |
889875a1 | 287 | return -ENODEV; |
1534c382 | 288 | |
e7fc5146 | 289 | status = ap_test_queue(qid, ap_apft_available(), &info); |
c50a160c IT |
290 | switch (status.response_code) { |
291 | case AP_RESPONSE_NORMAL: | |
6acbe21f MS |
292 | *queue_depth = (int)(info & 0xff); |
293 | *device_type = (int)((info >> 24) & 0xff); | |
294 | *facilities = (unsigned int)(info >> 32); | |
889875a1 MS |
295 | /* Update maximum domain id */ |
296 | nd = (info >> 16) & 0xff; | |
c1c1368d | 297 | /* if N bit is available, z13 and newer */ |
889875a1 MS |
298 | if ((info & (1UL << 57)) && nd > 0) |
299 | ap_max_domain_id = nd; | |
c1c1368d IT |
300 | else /* older machine types */ |
301 | ap_max_domain_id = 15; | |
14878424 HF |
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 | } | |
c50a160c IT |
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(); | |
1534c382 | 331 | } |
1534c382 MS |
332 | } |
333 | ||
e28d2af4 | 334 | void ap_wait(enum ap_wait wait) |
3f3007af MS |
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)) { | |
8b0e1953 | 351 | hr_time = poll_timeout; |
3f3007af MS |
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 | ||
3f3007af MS |
363 | /** |
364 | * ap_request_timeout(): Handling of request timeouts | |
365 | * @data: Holds the AP device. | |
366 | * | |
367 | * Handles request timeouts. | |
368 | */ | |
e28d2af4 | 369 | void ap_request_timeout(unsigned long data) |
3f3007af | 370 | { |
e28d2af4 | 371 | struct ap_queue *aq = (struct ap_queue *) data; |
3f3007af MS |
372 | |
373 | if (ap_suspend_flag) | |
374 | return; | |
e28d2af4 IT |
375 | spin_lock_bh(&aq->lock); |
376 | ap_wait(ap_sm_event(aq, AP_EVENT_TIMEOUT)); | |
377 | spin_unlock_bh(&aq->lock); | |
3f3007af MS |
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 | */ | |
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 | */ | |
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 | */ | |
410 | static void ap_tasklet_fn(unsigned long dummy) | |
411 | { | |
e28d2af4 IT |
412 | struct ap_card *ac; |
413 | struct ap_queue *aq; | |
3f3007af MS |
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 | ||
e28d2af4 IT |
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 | } | |
3f3007af | 430 | } |
e28d2af4 IT |
431 | spin_unlock_bh(&ap_list_lock); |
432 | ||
433 | ap_wait(wait); | |
af512ed0 RW |
434 | } |
435 | ||
9af3e04e MS |
436 | static int ap_pending_requests(void) |
437 | { | |
e28d2af4 IT |
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; | |
9af3e04e | 448 | } |
9af3e04e | 449 | } |
e28d2af4 IT |
450 | spin_unlock_bh(&ap_list_lock); |
451 | return 0; | |
9af3e04e MS |
452 | } |
453 | ||
83e9d5d2 MS |
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 | */ | |
464 | static int ap_poll_thread(void *data) | |
465 | { | |
466 | DECLARE_WAITQUEUE(wait, current); | |
83e9d5d2 MS |
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); | |
9af3e04e | 473 | if (ap_suspend_flag || !ap_pending_requests()) { |
83e9d5d2 MS |
474 | schedule(); |
475 | try_to_freeze(); | |
476 | } | |
477 | set_current_state(TASK_RUNNING); | |
478 | remove_wait_queue(&ap_poll_wait, &wait); | |
83e9d5d2 MS |
479 | if (need_resched()) { |
480 | schedule(); | |
481 | try_to_freeze(); | |
482 | continue; | |
483 | } | |
3f3007af | 484 | ap_tasklet_fn(0); |
9af3e04e MS |
485 | } |
486 | ||
83e9d5d2 MS |
487 | return 0; |
488 | } | |
489 | ||
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 | ||
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 | ||
e28d2af4 IT |
515 | #define is_card_dev(x) ((x)->parent == ap_root_device) |
516 | #define is_queue_dev(x) ((x)->parent != ap_root_device) | |
1534c382 MS |
517 | |
518 | /** | |
1749a81d FB |
519 | * ap_bus_match() |
520 | * @dev: Pointer to device | |
521 | * @drv: Pointer to device_driver | |
522 | * | |
1534c382 MS |
523 | * AP bus driver registration/unregistration. |
524 | */ | |
525 | static int ap_bus_match(struct device *dev, struct device_driver *drv) | |
526 | { | |
1534c382 MS |
527 | struct ap_driver *ap_drv = to_ap_drv(drv); |
528 | struct ap_device_id *id; | |
529 | ||
1749a81d | 530 | /* |
1534c382 MS |
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++) { | |
e28d2af4 IT |
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; | |
1534c382 MS |
543 | } |
544 | return 0; | |
545 | } | |
546 | ||
547 | /** | |
1749a81d FB |
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. | |
1534c382 | 554 | */ |
7eff2e7a | 555 | static int ap_uevent (struct device *dev, struct kobj_uevent_env *env) |
1534c382 MS |
556 | { |
557 | struct ap_device *ap_dev = to_ap_dev(dev); | |
7eff2e7a | 558 | int retval = 0; |
1534c382 MS |
559 | |
560 | if (!ap_dev) | |
561 | return -ENODEV; | |
562 | ||
563 | /* Set up DEV_TYPE environment variable. */ | |
7eff2e7a | 564 | retval = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); |
bf62456e ER |
565 | if (retval) |
566 | return retval; | |
567 | ||
66a4263b | 568 | /* Add MODALIAS= */ |
7eff2e7a | 569 | retval = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); |
bf62456e | 570 | |
bf62456e | 571 | return retval; |
1534c382 MS |
572 | } |
573 | ||
3e488c95 | 574 | static int ap_dev_suspend(struct device *dev) |
772f5472 FB |
575 | { |
576 | struct ap_device *ap_dev = to_ap_dev(dev); | |
772f5472 | 577 | |
e28d2af4 IT |
578 | if (ap_dev->drv && ap_dev->drv->suspend) |
579 | ap_dev->drv->suspend(ap_dev); | |
580 | return 0; | |
581 | } | |
582 | ||
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); | |
83e9d5d2 MS |
589 | return 0; |
590 | } | |
5314af69 | 591 | |
83e9d5d2 MS |
592 | static void ap_bus_suspend(void) |
593 | { | |
cccd85bf HF |
594 | AP_DBF(DBF_DEBUG, "ap_bus_suspend running\n"); |
595 | ||
83e9d5d2 MS |
596 | ap_suspend_flag = 1; |
597 | /* | |
598 | * Disable scanning for devices, thus we do not want to scan | |
599 | * for them after removing. | |
600 | */ | |
8139b89d | 601 | flush_work(&ap_scan_work); |
83e9d5d2 MS |
602 | tasklet_disable(&ap_tasklet); |
603 | } | |
604 | ||
e28d2af4 IT |
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 | ||
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 | ||
619 | static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) | |
83e9d5d2 | 620 | { |
e28d2af4 IT |
621 | if (is_queue_dev(dev) && |
622 | AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long) data) | |
623 | device_unregister(dev); | |
83e9d5d2 MS |
624 | return 0; |
625 | } | |
626 | ||
627 | static void ap_bus_resume(void) | |
772f5472 | 628 | { |
f4eae94f | 629 | int rc; |
772f5472 | 630 | |
cccd85bf HF |
631 | AP_DBF(DBF_DEBUG, "ap_bus_resume running\n"); |
632 | ||
e28d2af4 IT |
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 | ||
83e9d5d2 MS |
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); | |
5314af69 | 644 | } |
83e9d5d2 MS |
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); | |
8139b89d | 657 | queue_work(system_long_wq, &ap_scan_work); |
83e9d5d2 | 658 | } |
772f5472 | 659 | |
83e9d5d2 MS |
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; | |
772f5472 | 676 | } |
83e9d5d2 MS |
677 | static struct notifier_block ap_power_notifier = { |
678 | .notifier_call = ap_power_event, | |
679 | }; | |
772f5472 | 680 | |
e28d2af4 | 681 | static SIMPLE_DEV_PM_OPS(ap_bus_pm_ops, ap_dev_suspend, ap_dev_resume); |
3e488c95 | 682 | |
1534c382 MS |
683 | static struct bus_type ap_bus_type = { |
684 | .name = "ap", | |
685 | .match = &ap_bus_match, | |
686 | .uevent = &ap_uevent, | |
3e488c95 | 687 | .pm = &ap_bus_pm_ops, |
1534c382 MS |
688 | }; |
689 | ||
690 | static int ap_device_probe(struct device *dev) | |
691 | { | |
692 | struct ap_device *ap_dev = to_ap_dev(dev); | |
3f3007af MS |
693 | struct ap_driver *ap_drv = to_ap_drv(dev->driver); |
694 | int rc; | |
666e68e0 | 695 | |
e3850508 HF |
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 | ||
3f3007af | 705 | ap_dev->drv = ap_drv; |
1534c382 | 706 | rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; |
e3850508 HF |
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); | |
3f3007af | 715 | ap_dev->drv = NULL; |
e3850508 HF |
716 | } |
717 | ||
1534c382 MS |
718 | return rc; |
719 | } | |
720 | ||
1534c382 MS |
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 | ||
e3850508 HF |
726 | if (ap_drv->remove) |
727 | ap_drv->remove(ap_dev); | |
728 | ||
729 | /* Remove queue/card from list of active queues/cards */ | |
e28d2af4 IT |
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); | |
e3850508 | 736 | |
1534c382 MS |
737 | return 0; |
738 | } | |
739 | ||
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 | ||
e387753c SS |
745 | if (!initialised) |
746 | return -ENODEV; | |
747 | ||
1534c382 MS |
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 | ||
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 | ||
dabecb29 HD |
763 | void ap_bus_force_rescan(void) |
764 | { | |
83e9d5d2 MS |
765 | if (ap_suspend_flag) |
766 | return; | |
56bbe686 | 767 | /* processing a asynchronous bus rescan */ |
fcd0d1f6 | 768 | del_timer(&ap_config_timer); |
8139b89d MS |
769 | queue_work(system_long_wq, &ap_scan_work); |
770 | flush_work(&ap_scan_work); | |
dabecb29 HD |
771 | } |
772 | EXPORT_SYMBOL(ap_bus_force_rescan); | |
773 | ||
1749a81d | 774 | /* |
1534c382 MS |
775 | * AP bus attributes. |
776 | */ | |
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 | ||
fc1d3f02 IT |
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); | |
cccd85bf | 793 | |
ac994e80 | 794 | AP_DBF(DBF_DEBUG, "stored new default domain=%d\n", domain); |
cccd85bf | 795 | |
fc1d3f02 IT |
796 | return count; |
797 | } | |
798 | ||
799 | static BUS_ATTR(ap_domain, 0644, ap_domain_show, ap_domain_store); | |
1534c382 | 800 | |
91f3e3ea IT |
801 | static ssize_t ap_control_domain_mask_show(struct bus_type *bus, char *buf) |
802 | { | |
889875a1 MS |
803 | if (!ap_configuration) /* QCI not supported */ |
804 | return snprintf(buf, PAGE_SIZE, "not supported\n"); | |
e28d2af4 | 805 | |
889875a1 MS |
806 | return snprintf(buf, PAGE_SIZE, |
807 | "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", | |
91f3e3ea IT |
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]); | |
91f3e3ea IT |
812 | } |
813 | ||
814 | static BUS_ATTR(ap_control_domain_mask, 0444, | |
815 | ap_control_domain_mask_show, NULL); | |
816 | ||
e28d2af4 IT |
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 | ||
1534c382 MS |
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 | ||
cb17a636 FB |
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 | ||
1534c382 MS |
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; | |
fcd0d1f6 | 854 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
1534c382 MS |
855 | return count; |
856 | } | |
857 | ||
858 | static BUS_ATTR(config_time, 0644, ap_config_time_show, ap_config_time_store); | |
859 | ||
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 | ||
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) | |
83e9d5d2 MS |
875 | count = rc; |
876 | } else | |
1534c382 MS |
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 | ||
fe137230 FB |
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 | ||
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 */ | |
cb17a636 FB |
895 | if (sscanf(buf, "%llu\n", &time) != 1 || time < 1 || |
896 | time > 120000000000ULL) | |
fe137230 FB |
897 | return -EINVAL; |
898 | poll_timeout = time; | |
8b0e1953 | 899 | hr_time = poll_timeout; |
fe137230 | 900 | |
8cc2af7c IT |
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 | ||
fe137230 FB |
907 | return count; |
908 | } | |
909 | ||
910 | static BUS_ATTR(poll_timeout, 0644, poll_timeout_show, poll_timeout_store); | |
911 | ||
5bc334bf IT |
912 | static ssize_t ap_max_domain_id_show(struct bus_type *bus, char *buf) |
913 | { | |
889875a1 MS |
914 | int max_domain_id; |
915 | ||
916 | if (ap_configuration) | |
917 | max_domain_id = ap_max_domain_id ? : -1; | |
918 | else | |
5bc334bf | 919 | max_domain_id = 15; |
5bc334bf IT |
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 | ||
1534c382 MS |
925 | static struct bus_attribute *const ap_bus_attrs[] = { |
926 | &bus_attr_ap_domain, | |
91f3e3ea | 927 | &bus_attr_ap_control_domain_mask, |
e28d2af4 | 928 | &bus_attr_ap_usage_domain_mask, |
1534c382 MS |
929 | &bus_attr_config_time, |
930 | &bus_attr_poll_thread, | |
cb17a636 | 931 | &bus_attr_ap_interrupts, |
fe137230 | 932 | &bus_attr_poll_timeout, |
5bc334bf | 933 | &bus_attr_ap_max_domain_id, |
fe137230 | 934 | NULL, |
1534c382 MS |
935 | }; |
936 | ||
937 | /** | |
1749a81d FB |
938 | * ap_select_domain(): Select an AP domain. |
939 | * | |
940 | * Pick one of the 16 AP domains. | |
1534c382 | 941 | */ |
4d284cac | 942 | static int ap_select_domain(void) |
1534c382 | 943 | { |
6acbe21f MS |
944 | int count, max_count, best_domain; |
945 | struct ap_queue_status status; | |
946 | int i, j; | |
1534c382 | 947 | |
1749a81d | 948 | /* |
1534c382 MS |
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 | */ | |
fc1d3f02 IT |
953 | spin_lock_bh(&ap_domain_lock); |
954 | if (ap_domain_index >= 0) { | |
1534c382 | 955 | /* Domain has already been selected. */ |
fc1d3f02 | 956 | spin_unlock_bh(&ap_domain_lock); |
1534c382 | 957 | return 0; |
fc1d3f02 | 958 | } |
1534c382 MS |
959 | best_domain = -1; |
960 | max_count = 0; | |
961 | for (i = 0; i < AP_DOMAINS; i++) { | |
75014550 HD |
962 | if (!ap_test_config_domain(i)) |
963 | continue; | |
1534c382 MS |
964 | count = 0; |
965 | for (j = 0; j < AP_DEVICES; j++) { | |
75014550 HD |
966 | if (!ap_test_config_card_id(j)) |
967 | continue; | |
e7fc5146 TK |
968 | status = ap_test_queue(AP_MKQID(j, i), |
969 | ap_apft_available(), | |
970 | NULL); | |
6acbe21f | 971 | if (status.response_code != AP_RESPONSE_NORMAL) |
1534c382 MS |
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; | |
ac994e80 | 982 | AP_DBF(DBF_DEBUG, "new ap_domain_index=%d\n", ap_domain_index); |
fc1d3f02 | 983 | spin_unlock_bh(&ap_domain_lock); |
1534c382 MS |
984 | return 0; |
985 | } | |
fc1d3f02 | 986 | spin_unlock_bh(&ap_domain_lock); |
1534c382 MS |
987 | return -ENODEV; |
988 | } | |
989 | ||
e28d2af4 IT |
990 | /* |
991 | * helper function to be used with bus_find_dev | |
992 | * matches for the card device with the given id | |
1534c382 | 993 | */ |
e28d2af4 | 994 | static int __match_card_device_with_id(struct device *dev, void *data) |
1534c382 | 995 | { |
e28d2af4 | 996 | return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long) data; |
1534c382 MS |
997 | } |
998 | ||
e28d2af4 IT |
999 | /* helper function to be used with bus_find_dev |
1000 | * matches for the queue device with a given qid | |
1001 | */ | |
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 | */ | |
4927b3f7 | 1011 | static void ap_scan_bus(struct work_struct *unused) |
1534c382 | 1012 | { |
e28d2af4 IT |
1013 | struct ap_queue *aq; |
1014 | struct ap_card *ac; | |
1534c382 MS |
1015 | struct device *dev; |
1016 | ap_qid_t qid; | |
e28d2af4 IT |
1017 | int depth = 0, type = 0; |
1018 | unsigned int functions = 0; | |
ac994e80 | 1019 | int rc, id, dom, borked, domains, defdomdevs = 0; |
1534c382 | 1020 | |
cccd85bf HF |
1021 | AP_DBF(DBF_DEBUG, "ap_scan_bus running\n"); |
1022 | ||
050349b5 | 1023 | ap_query_configuration(ap_configuration); |
83e9d5d2 | 1024 | if (ap_select_domain() != 0) |
fcd0d1f6 | 1025 | goto out; |
889875a1 | 1026 | |
e28d2af4 IT |
1027 | for (id = 0; id < AP_DEVICES; id++) { |
1028 | /* check if device is registered */ | |
1534c382 | 1029 | dev = bus_find_device(&ap_bus_type, NULL, |
e28d2af4 IT |
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 */ | |
3f3007af | 1043 | device_unregister(dev); |
e28d2af4 IT |
1044 | put_device(dev); |
1045 | } | |
1534c382 MS |
1046 | continue; |
1047 | } | |
e28d2af4 IT |
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++; | |
ac994e80 HF |
1083 | if (dom == ap_domain_index) |
1084 | defdomdevs++; | |
e28d2af4 IT |
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); | |
e28d2af4 IT |
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); | |
e28d2af4 IT |
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) { | |
e28d2af4 IT |
1127 | put_device(&aq->ap_dev.device); |
1128 | continue; | |
1129 | } | |
1130 | domains++; | |
ac994e80 HF |
1131 | if (dom == ap_domain_index) |
1132 | defdomdevs++; | |
e28d2af4 IT |
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); | |
3f3007af | 1139 | } |
e28d2af4 | 1140 | } /* end device loop */ |
ac994e80 HF |
1141 | |
1142 | if (defdomdevs < 1) | |
1143 | AP_DBF(DBF_INFO, "no queue device with default domain %d available\n", | |
1144 | ap_domain_index); | |
1145 | ||
fcd0d1f6 MS |
1146 | out: |
1147 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); | |
1534c382 MS |
1148 | } |
1149 | ||
83e9d5d2 | 1150 | static void ap_config_timeout(unsigned long ptr) |
1534c382 | 1151 | { |
83e9d5d2 MS |
1152 | if (ap_suspend_flag) |
1153 | return; | |
8139b89d | 1154 | queue_work(system_long_wq, &ap_scan_work); |
1534c382 | 1155 | } |
13e742ba RW |
1156 | |
1157 | static void ap_reset_all(void) | |
85eca850 RW |
1158 | { |
1159 | int i, j; | |
1160 | ||
170387a8 IT |
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; | |
0db78559 | 1167 | ap_rapq(AP_MKQID(j, i)); |
170387a8 IT |
1168 | } |
1169 | } | |
85eca850 RW |
1170 | } |
1171 | ||
1172 | static struct reset_call ap_reset_call = { | |
13e742ba | 1173 | .fn = ap_reset_all, |
85eca850 RW |
1174 | }; |
1175 | ||
cccd85bf HF |
1176 | int __init ap_debug_init(void) |
1177 | { | |
cccd85bf HF |
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 | ||
1186 | void ap_debug_exit(void) | |
1187 | { | |
cccd85bf HF |
1188 | debug_unregister(ap_dbf_info); |
1189 | } | |
1190 | ||
1534c382 | 1191 | /** |
1749a81d FB |
1192 | * ap_module_init(): The module initialization code. |
1193 | * | |
1194 | * Initializes the module. | |
1534c382 MS |
1195 | */ |
1196 | int __init ap_module_init(void) | |
1197 | { | |
889875a1 | 1198 | int max_domain_id; |
1534c382 MS |
1199 | int rc, i; |
1200 | ||
cccd85bf HF |
1201 | rc = ap_debug_init(); |
1202 | if (rc) | |
1203 | return rc; | |
1204 | ||
889875a1 MS |
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) | |
ac994e80 HF |
1214 | max_domain_id = |
1215 | ap_max_domain_id ? ap_max_domain_id : AP_DOMAINS - 1; | |
889875a1 MS |
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); | |
ac994e80 | 1221 | ap_domain_index = -1; |
1534c382 | 1222 | } |
5314af69 FB |
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 | ||
cb17a636 | 1229 | if (ap_interrupts_available()) { |
f4eae94f MS |
1230 | rc = register_adapter_interrupt(&ap_airq); |
1231 | ap_airq_flag = (rc == 0); | |
cb17a636 FB |
1232 | } |
1233 | ||
85eca850 | 1234 | register_reset_call(&ap_reset_call); |
1534c382 MS |
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. */ | |
035da16f | 1247 | ap_root_device = root_device_register("ap"); |
ba8da213 | 1248 | rc = PTR_RET(ap_root_device); |
1534c382 MS |
1249 | if (rc) |
1250 | goto out_bus; | |
1251 | ||
1749a81d | 1252 | /* Setup the AP bus rescan timer. */ |
fcd0d1f6 | 1253 | setup_timer(&ap_config_timer, ap_config_timeout, 0); |
1534c382 | 1254 | |
3f3007af MS |
1255 | /* |
1256 | * Setup the high resultion poll timer. | |
fe137230 FB |
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; | |
93521314 | 1261 | spin_lock_init(&ap_poll_timer_lock); |
fe137230 FB |
1262 | hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
1263 | ap_poll_timer.function = ap_poll_timeout; | |
1264 | ||
1534c382 MS |
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 | ||
83e9d5d2 MS |
1272 | rc = register_pm_notifier(&ap_power_notifier); |
1273 | if (rc) | |
1274 | goto out_pm; | |
1275 | ||
8139b89d | 1276 | queue_work(system_long_wq, &ap_scan_work); |
e387753c | 1277 | initialised = true; |
3f3007af | 1278 | |
1534c382 MS |
1279 | return 0; |
1280 | ||
83e9d5d2 MS |
1281 | out_pm: |
1282 | ap_poll_thread_stop(); | |
1534c382 | 1283 | out_work: |
fe137230 | 1284 | hrtimer_cancel(&ap_poll_timer); |
035da16f | 1285 | root_device_unregister(ap_root_device); |
1534c382 MS |
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: | |
85eca850 | 1291 | unregister_reset_call(&ap_reset_call); |
f4eae94f MS |
1292 | if (ap_using_interrupts()) |
1293 | unregister_adapter_interrupt(&ap_airq); | |
889875a1 | 1294 | kfree(ap_configuration); |
1534c382 MS |
1295 | return rc; |
1296 | } | |
50a0d46c | 1297 | device_initcall(ap_module_init); |