Commit | Line | Data |
---|---|---|
812141a9 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
1534c382 | 2 | /* |
5ac8c724 | 3 | * Copyright IBM Corp. 2006, 2023 |
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> |
837cd105 | 9 | * Harald Freudenberger <freude@linux.ibm.com> |
1534c382 MS |
10 | * |
11 | * Adjunct processor bus. | |
1534c382 MS |
12 | */ |
13 | ||
136f7a1c MS |
14 | #define KMSG_COMPONENT "ap" |
15 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | |
16 | ||
62d146ff | 17 | #include <linux/kernel_stat.h> |
50a0d46c | 18 | #include <linux/moduleparam.h> |
1534c382 MS |
19 | #include <linux/init.h> |
20 | #include <linux/delay.h> | |
21 | #include <linux/err.h> | |
41677b1d | 22 | #include <linux/freezer.h> |
1534c382 MS |
23 | #include <linux/interrupt.h> |
24 | #include <linux/workqueue.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
1534c382 MS |
26 | #include <linux/notifier.h> |
27 | #include <linux/kthread.h> | |
28 | #include <linux/mutex.h> | |
cb17a636 | 29 | #include <asm/airq.h> |
d2197485 | 30 | #include <asm/tpi.h> |
60063497 | 31 | #include <linux/atomic.h> |
cb17a636 | 32 | #include <asm/isc.h> |
fe137230 FB |
33 | #include <linux/hrtimer.h> |
34 | #include <linux/ktime.h> | |
a0616cde | 35 | #include <asm/facility.h> |
5d26a105 | 36 | #include <linux/crypto.h> |
e28d2af4 | 37 | #include <linux/mod_devicetable.h> |
cccd85bf | 38 | #include <linux/debugfs.h> |
7e0bdbe5 | 39 | #include <linux/ctype.h> |
4f8206b8 | 40 | #include <linux/module.h> |
d065bdb4 | 41 | #include <asm/uv.h> |
1534c382 MS |
42 | |
43 | #include "ap_bus.h" | |
cccd85bf | 44 | #include "ap_debug.h" |
1534c382 | 45 | |
1749a81d | 46 | /* |
50a0d46c | 47 | * Module parameters; note though this file itself isn't modular. |
1534c382 MS |
48 | */ |
49 | int ap_domain_index = -1; /* Adjunct Processor Domain Index */ | |
fc1d3f02 | 50 | static DEFINE_SPINLOCK(ap_domain_lock); |
ac2b96f3 | 51 | module_param_named(domain, ap_domain_index, int, 0440); |
1534c382 MS |
52 | MODULE_PARM_DESC(domain, "domain index for ap devices"); |
53 | EXPORT_SYMBOL(ap_domain_index); | |
54 | ||
ac2b96f3 HF |
55 | static int ap_thread_flag; |
56 | module_param_named(poll_thread, ap_thread_flag, int, 0440); | |
b90b34c6 | 57 | MODULE_PARM_DESC(poll_thread, "Turn on/off poll thread, default is 0 (off)."); |
1534c382 | 58 | |
7e0bdbe5 HF |
59 | static char *apm_str; |
60 | module_param_named(apmask, apm_str, charp, 0440); | |
61 | MODULE_PARM_DESC(apmask, "AP bus adapter mask."); | |
62 | ||
63 | static char *aqm_str; | |
64 | module_param_named(aqmask, aqm_str, charp, 0440); | |
65 | MODULE_PARM_DESC(aqmask, "AP bus domain mask."); | |
66 | ||
d0982725 HF |
67 | static int ap_useirq = 1; |
68 | module_param_named(useirq, ap_useirq, int, 0440); | |
69 | MODULE_PARM_DESC(useirq, "Use interrupt if available, default is 1 (on)."); | |
70 | ||
bd39654a HF |
71 | atomic_t ap_max_msg_size = ATOMIC_INIT(AP_DEFAULT_MAX_MSG_SIZE); |
72 | EXPORT_SYMBOL(ap_max_msg_size); | |
73 | ||
e28d2af4 IT |
74 | static struct device *ap_root_device; |
75 | ||
bc4b295e HF |
76 | /* Hashtable of all queue devices on the AP bus */ |
77 | DEFINE_HASHTABLE(ap_queues, 8); | |
78 | /* lock used for the ap_queues hashtable */ | |
79 | DEFINE_SPINLOCK(ap_queues_lock); | |
e28d2af4 | 80 | |
00fab235 HF |
81 | /* Default permissions (ioctl, card and domain masking) */ |
82 | struct ap_perms ap_perms; | |
83 | EXPORT_SYMBOL(ap_perms); | |
84 | DEFINE_MUTEX(ap_perms_mutex); | |
85 | EXPORT_SYMBOL(ap_perms_mutex); | |
7e0bdbe5 | 86 | |
837cd105 HF |
87 | /* # of bus scans since init */ |
88 | static atomic64_t ap_scan_bus_count; | |
89 | ||
0677519a HF |
90 | /* # of bindings complete since init */ |
91 | static atomic64_t ap_bindings_complete_count = ATOMIC64_INIT(0); | |
92 | ||
837cd105 HF |
93 | /* completion for initial APQN bindings complete */ |
94 | static DECLARE_COMPLETION(ap_init_apqn_bindings_complete); | |
95 | ||
c8337c47 | 96 | static struct ap_config_info *ap_qci_info; |
28391585 | 97 | static struct ap_config_info *ap_qci_info_old; |
1534c382 | 98 | |
cccd85bf HF |
99 | /* |
100 | * AP bus related debug feature things. | |
101 | */ | |
cccd85bf HF |
102 | debug_info_t *ap_dbf_info; |
103 | ||
1749a81d | 104 | /* |
8139b89d | 105 | * Workqueue timer for bus rescan. |
1534c382 | 106 | */ |
1534c382 MS |
107 | static struct timer_list ap_config_timer; |
108 | static int ap_config_time = AP_CONFIG_TIME; | |
3f3007af | 109 | static void ap_scan_bus(struct work_struct *); |
8139b89d | 110 | static DECLARE_WORK(ap_scan_work, ap_scan_bus); |
1534c382 | 111 | |
1749a81d | 112 | /* |
cb17a636 | 113 | * Tasklet & timer for AP request polling and interrupts |
1534c382 | 114 | */ |
3f3007af | 115 | static void ap_tasklet_fn(unsigned long); |
b13fecb1 | 116 | static DECLARE_TASKLET_OLD(ap_tasklet, ap_tasklet_fn); |
1534c382 | 117 | static DECLARE_WAIT_QUEUE_HEAD(ap_poll_wait); |
ac2b96f3 | 118 | static struct task_struct *ap_poll_kthread; |
1534c382 | 119 | static DEFINE_MUTEX(ap_poll_thread_mutex); |
93521314 | 120 | static DEFINE_SPINLOCK(ap_poll_timer_lock); |
fe137230 | 121 | static struct hrtimer ap_poll_timer; |
ac2b96f3 HF |
122 | /* |
123 | * In LPAR poll with 4kHz frequency. Poll every 250000 nanoseconds. | |
124 | * If z/VM change to 1500000 nanoseconds to adjust to z/VM polling. | |
125 | */ | |
263c8454 HF |
126 | static unsigned long poll_high_timeout = 250000UL; |
127 | ||
128 | /* | |
129 | * Some state machine states only require a low frequency polling. | |
130 | * We use 25 Hz frequency for these. | |
131 | */ | |
132 | static unsigned long poll_low_timeout = 40000000UL; | |
1534c382 | 133 | |
c8337c47 HF |
134 | /* Maximum domain id, if not given via qci */ |
135 | static int ap_max_domain_id = 15; | |
136 | /* Maximum adapter id, if not given via qci */ | |
137 | static int ap_max_adapter_id = 63; | |
41677b1d | 138 | |
5b431787 | 139 | static const struct bus_type ap_bus_type; |
772f5472 | 140 | |
f4eae94f | 141 | /* Adapter interrupt definitions */ |
d2197485 MR |
142 | static void ap_interrupt_handler(struct airq_struct *airq, |
143 | struct tpi_info *tpi_info); | |
3f3007af | 144 | |
cabebb69 | 145 | static bool ap_irq_flag; |
f4eae94f MS |
146 | |
147 | static struct airq_struct ap_airq = { | |
148 | .handler = ap_interrupt_handler, | |
149 | .isc = AP_ISC, | |
150 | }; | |
151 | ||
e28d2af4 IT |
152 | /** |
153 | * ap_airq_ptr() - Get the address of the adapter interrupt indicator | |
154 | * | |
155 | * Returns the address of the local-summary-indicator of the adapter | |
156 | * interrupt handler for AP, or NULL if adapter interrupts are not | |
157 | * available. | |
158 | */ | |
159 | void *ap_airq_ptr(void) | |
160 | { | |
cabebb69 | 161 | if (ap_irq_flag) |
e28d2af4 IT |
162 | return ap_airq.lsi_ptr; |
163 | return NULL; | |
164 | } | |
165 | ||
cb17a636 FB |
166 | /** |
167 | * ap_interrupts_available(): Test if AP interrupts are available. | |
168 | * | |
169 | * Returns 1 if AP interrupts are available. | |
170 | */ | |
171 | static int ap_interrupts_available(void) | |
172 | { | |
86cd741b | 173 | return test_facility(65); |
cb17a636 FB |
174 | } |
175 | ||
75014550 | 176 | /** |
c8337c47 HF |
177 | * ap_qci_available(): Test if AP configuration |
178 | * information can be queried via QCI subfunction. | |
75014550 | 179 | * |
c8337c47 | 180 | * Returns 1 if subfunction PQAP(QCI) is available. |
75014550 | 181 | */ |
c8337c47 | 182 | static int ap_qci_available(void) |
75014550 | 183 | { |
86cd741b | 184 | return test_facility(12); |
75014550 HD |
185 | } |
186 | ||
e7fc5146 TK |
187 | /** |
188 | * ap_apft_available(): Test if AP facilities test (APFT) | |
189 | * facility is available. | |
190 | * | |
2004b57c | 191 | * Returns 1 if APFT is available. |
e7fc5146 TK |
192 | */ |
193 | static int ap_apft_available(void) | |
194 | { | |
195 | return test_facility(15); | |
196 | } | |
197 | ||
9a564108 HF |
198 | /* |
199 | * ap_qact_available(): Test if the PQAP(QACT) subfunction is available. | |
200 | * | |
201 | * Returns 1 if the QACT subfunction is available. | |
202 | */ | |
203 | static inline int ap_qact_available(void) | |
204 | { | |
c8337c47 HF |
205 | if (ap_qci_info) |
206 | return ap_qci_info->qact; | |
9a564108 HF |
207 | return 0; |
208 | } | |
209 | ||
f6047040 HF |
210 | /* |
211 | * ap_sb_available(): Test if the AP secure binding facility is available. | |
212 | * | |
213 | * Returns 1 if secure binding facility is available. | |
214 | */ | |
215 | int ap_sb_available(void) | |
216 | { | |
217 | if (ap_qci_info) | |
218 | return ap_qci_info->apsb; | |
219 | return 0; | |
220 | } | |
221 | ||
386cb81e HD |
222 | /* |
223 | * ap_is_se_guest(): Check for SE guest with AP pass-through support. | |
224 | */ | |
225 | bool ap_is_se_guest(void) | |
226 | { | |
227 | return is_prot_virt_guest() && ap_sb_available(); | |
228 | } | |
229 | EXPORT_SYMBOL(ap_is_se_guest); | |
230 | ||
050349b5 | 231 | /* |
c8337c47 | 232 | * ap_fetch_qci_info(): Fetch cryptographic config info |
050349b5 HF |
233 | * |
234 | * Returns the ap configuration info fetched via PQAP(QCI). | |
235 | * On success 0 is returned, on failure a negative errno | |
236 | * is returned, e.g. if the PQAP(QCI) instruction is not | |
237 | * available, the return value will be -EOPNOTSUPP. | |
238 | */ | |
c8337c47 | 239 | static inline int ap_fetch_qci_info(struct ap_config_info *info) |
11d37673 | 240 | { |
c8337c47 | 241 | if (!ap_qci_available()) |
11d37673 | 242 | return -EOPNOTSUPP; |
050349b5 HF |
243 | if (!info) |
244 | return -EINVAL; | |
245 | return ap_qci(info); | |
11d37673 HC |
246 | } |
247 | ||
889875a1 | 248 | /** |
c8337c47 HF |
249 | * ap_init_qci_info(): Allocate and query qci config info. |
250 | * Does also update the static variables ap_max_domain_id | |
251 | * and ap_max_adapter_id if this info is available. | |
889875a1 | 252 | */ |
c8337c47 | 253 | static void __init ap_init_qci_info(void) |
889875a1 | 254 | { |
c8337c47 | 255 | if (!ap_qci_available()) { |
2ea2a609 | 256 | AP_DBF_INFO("%s QCI not supported\n", __func__); |
889875a1 | 257 | return; |
c8337c47 | 258 | } |
889875a1 | 259 | |
c8337c47 HF |
260 | ap_qci_info = kzalloc(sizeof(*ap_qci_info), GFP_KERNEL); |
261 | if (!ap_qci_info) | |
889875a1 | 262 | return; |
28391585 | 263 | ap_qci_info_old = kzalloc(sizeof(*ap_qci_info_old), GFP_KERNEL); |
9ac74f06 WY |
264 | if (!ap_qci_info_old) { |
265 | kfree(ap_qci_info); | |
266 | ap_qci_info = NULL; | |
28391585 | 267 | return; |
9ac74f06 | 268 | } |
c8337c47 HF |
269 | if (ap_fetch_qci_info(ap_qci_info) != 0) { |
270 | kfree(ap_qci_info); | |
28391585 | 271 | kfree(ap_qci_info_old); |
c8337c47 | 272 | ap_qci_info = NULL; |
28391585 | 273 | ap_qci_info_old = NULL; |
889875a1 MS |
274 | return; |
275 | } | |
2ea2a609 | 276 | AP_DBF_INFO("%s successful fetched initial qci info\n", __func__); |
c8337c47 HF |
277 | |
278 | if (ap_qci_info->apxa) { | |
f6047040 HF |
279 | if (ap_qci_info->na) { |
280 | ap_max_adapter_id = ap_qci_info->na; | |
2ea2a609 HF |
281 | AP_DBF_INFO("%s new ap_max_adapter_id is %d\n", |
282 | __func__, ap_max_adapter_id); | |
c8337c47 | 283 | } |
f6047040 HF |
284 | if (ap_qci_info->nd) { |
285 | ap_max_domain_id = ap_qci_info->nd; | |
2ea2a609 HF |
286 | AP_DBF_INFO("%s new ap_max_domain_id is %d\n", |
287 | __func__, ap_max_domain_id); | |
c8337c47 HF |
288 | } |
289 | } | |
28391585 TK |
290 | |
291 | memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info)); | |
889875a1 MS |
292 | } |
293 | ||
294 | /* | |
295 | * ap_test_config(): helper function to extract the nrth bit | |
296 | * within the unsigned int array field. | |
297 | */ | |
298 | static inline int ap_test_config(unsigned int *field, unsigned int nr) | |
299 | { | |
300 | return ap_test_bit((field + (nr >> 5)), (nr & 0x1f)); | |
301 | } | |
302 | ||
303 | /* | |
304 | * ap_test_config_card_id(): Test, whether an AP card ID is configured. | |
889875a1 MS |
305 | * |
306 | * Returns 0 if the card is not configured | |
307 | * 1 if the card is configured or | |
308 | * if the configuration information is not available | |
309 | */ | |
310 | static inline int ap_test_config_card_id(unsigned int id) | |
311 | { | |
c8337c47 HF |
312 | if (id > ap_max_adapter_id) |
313 | return 0; | |
314 | if (ap_qci_info) | |
315 | return ap_test_config(ap_qci_info->apm, id); | |
316 | return 1; | |
889875a1 MS |
317 | } |
318 | ||
319 | /* | |
7379e652 HF |
320 | * ap_test_config_usage_domain(): Test, whether an AP usage domain |
321 | * is configured. | |
889875a1 MS |
322 | * |
323 | * Returns 0 if the usage domain is not configured | |
324 | * 1 if the usage domain is configured or | |
325 | * if the configuration information is not available | |
326 | */ | |
7379e652 | 327 | int ap_test_config_usage_domain(unsigned int domain) |
889875a1 | 328 | { |
c8337c47 HF |
329 | if (domain > ap_max_domain_id) |
330 | return 0; | |
331 | if (ap_qci_info) | |
332 | return ap_test_config(ap_qci_info->aqm, domain); | |
333 | return 1; | |
889875a1 | 334 | } |
7379e652 HF |
335 | EXPORT_SYMBOL(ap_test_config_usage_domain); |
336 | ||
337 | /* | |
338 | * ap_test_config_ctrl_domain(): Test, whether an AP control domain | |
339 | * is configured. | |
340 | * @domain AP control domain ID | |
341 | * | |
342 | * Returns 1 if the control domain is configured | |
343 | * 0 in all other cases | |
344 | */ | |
345 | int ap_test_config_ctrl_domain(unsigned int domain) | |
346 | { | |
c8337c47 | 347 | if (!ap_qci_info || domain > ap_max_domain_id) |
7379e652 | 348 | return 0; |
c8337c47 | 349 | return ap_test_config(ap_qci_info->adm, domain); |
7379e652 HF |
350 | } |
351 | EXPORT_SYMBOL(ap_test_config_ctrl_domain); | |
889875a1 | 352 | |
c8337c47 HF |
353 | /* |
354 | * ap_queue_info(): Check and get AP queue info. | |
038c5bed | 355 | * Returns: 1 if APQN exists and info is filled, |
5cf1a563 | 356 | * 0 if APQN seems to exist but there is no info |
038c5bed HF |
357 | * available (eg. caused by an asynch pending error) |
358 | * -1 invalid APQN, TAPQ error or AP queue status which | |
359 | * indicates there is no APQN. | |
1534c382 | 360 | */ |
d4c53ae8 HF |
361 | static int ap_queue_info(ap_qid_t qid, struct ap_tapq_hwinfo *hwinfo, |
362 | bool *decfg, bool *cstop) | |
1534c382 MS |
363 | { |
364 | struct ap_queue_status status; | |
bd39654a | 365 | |
d4c53ae8 | 366 | hwinfo->value = 0; |
889875a1 | 367 | |
c8337c47 HF |
368 | /* make sure we don't run into a specifiation exception */ |
369 | if (AP_QID_CARD(qid) > ap_max_adapter_id || | |
370 | AP_QID_QUEUE(qid) > ap_max_domain_id) | |
038c5bed | 371 | return -1; |
1534c382 | 372 | |
c8337c47 | 373 | /* call TAPQ on this APQN */ |
d4c53ae8 | 374 | status = ap_test_queue(qid, ap_apft_available(), hwinfo); |
038c5bed | 375 | |
c50a160c IT |
376 | switch (status.response_code) { |
377 | case AP_RESPONSE_NORMAL: | |
c8337c47 | 378 | case AP_RESPONSE_RESET_IN_PROGRESS: |
4f2fcccd HF |
379 | case AP_RESPONSE_DECONFIGURED: |
380 | case AP_RESPONSE_CHECKSTOPPED: | |
381 | case AP_RESPONSE_BUSY: | |
5cf1a563 HF |
382 | /* For all these RCs the tapq info should be available */ |
383 | break; | |
c50a160c | 384 | default: |
5cf1a563 HF |
385 | /* On a pending async error the info should be available */ |
386 | if (!status.async) | |
387 | return -1; | |
388 | break; | |
1534c382 | 389 | } |
5cf1a563 HF |
390 | |
391 | /* There should be at least one of the mode bits set */ | |
d4c53ae8 | 392 | if (WARN_ON_ONCE(!hwinfo->value)) |
5cf1a563 HF |
393 | return 0; |
394 | ||
d4c53ae8 HF |
395 | *decfg = status.response_code == AP_RESPONSE_DECONFIGURED; |
396 | *cstop = status.response_code == AP_RESPONSE_CHECKSTOPPED; | |
5cf1a563 HF |
397 | |
398 | return 1; | |
1534c382 MS |
399 | } |
400 | ||
dc4b6ded | 401 | void ap_wait(enum ap_sm_wait wait) |
3f3007af MS |
402 | { |
403 | ktime_t hr_time; | |
404 | ||
405 | switch (wait) { | |
dc4b6ded HF |
406 | case AP_SM_WAIT_AGAIN: |
407 | case AP_SM_WAIT_INTERRUPT: | |
cabebb69 | 408 | if (ap_irq_flag) |
3f3007af MS |
409 | break; |
410 | if (ap_poll_kthread) { | |
411 | wake_up(&ap_poll_wait); | |
412 | break; | |
413 | } | |
fcf0220a | 414 | fallthrough; |
263c8454 HF |
415 | case AP_SM_WAIT_LOW_TIMEOUT: |
416 | case AP_SM_WAIT_HIGH_TIMEOUT: | |
3f3007af MS |
417 | spin_lock_bh(&ap_poll_timer_lock); |
418 | if (!hrtimer_is_queued(&ap_poll_timer)) { | |
263c8454 HF |
419 | hr_time = |
420 | wait == AP_SM_WAIT_LOW_TIMEOUT ? | |
421 | poll_low_timeout : poll_high_timeout; | |
3f3007af MS |
422 | hrtimer_forward_now(&ap_poll_timer, hr_time); |
423 | hrtimer_restart(&ap_poll_timer); | |
424 | } | |
425 | spin_unlock_bh(&ap_poll_timer_lock); | |
426 | break; | |
dc4b6ded | 427 | case AP_SM_WAIT_NONE: |
3f3007af MS |
428 | default: |
429 | break; | |
430 | } | |
431 | } | |
432 | ||
3f3007af MS |
433 | /** |
434 | * ap_request_timeout(): Handling of request timeouts | |
cefbeb5d | 435 | * @t: timer making this callback |
3f3007af MS |
436 | * |
437 | * Handles request timeouts. | |
438 | */ | |
cefbeb5d | 439 | void ap_request_timeout(struct timer_list *t) |
3f3007af | 440 | { |
cefbeb5d | 441 | struct ap_queue *aq = from_timer(aq, t, timeout); |
3f3007af | 442 | |
e28d2af4 | 443 | spin_lock_bh(&aq->lock); |
dc4b6ded | 444 | ap_wait(ap_sm_event(aq, AP_SM_EVENT_TIMEOUT)); |
e28d2af4 | 445 | spin_unlock_bh(&aq->lock); |
3f3007af MS |
446 | } |
447 | ||
448 | /** | |
449 | * ap_poll_timeout(): AP receive polling for finished AP requests. | |
450 | * @unused: Unused pointer. | |
451 | * | |
452 | * Schedules the AP tasklet using a high resolution timer. | |
453 | */ | |
454 | static enum hrtimer_restart ap_poll_timeout(struct hrtimer *unused) | |
455 | { | |
41677b1d | 456 | tasklet_schedule(&ap_tasklet); |
3f3007af MS |
457 | return HRTIMER_NORESTART; |
458 | } | |
459 | ||
460 | /** | |
461 | * ap_interrupt_handler() - Schedule ap_tasklet on interrupt | |
462 | * @airq: pointer to adapter interrupt descriptor | |
d2197485 | 463 | * @tpi_info: ignored |
3f3007af | 464 | */ |
d2197485 MR |
465 | static void ap_interrupt_handler(struct airq_struct *airq, |
466 | struct tpi_info *tpi_info) | |
3f3007af MS |
467 | { |
468 | inc_irq_stat(IRQIO_APB); | |
41677b1d | 469 | tasklet_schedule(&ap_tasklet); |
3f3007af MS |
470 | } |
471 | ||
472 | /** | |
473 | * ap_tasklet_fn(): Tasklet to poll all AP devices. | |
474 | * @dummy: Unused variable | |
475 | * | |
476 | * Poll all AP devices on the bus. | |
477 | */ | |
478 | static void ap_tasklet_fn(unsigned long dummy) | |
479 | { | |
bc4b295e | 480 | int bkt; |
e28d2af4 | 481 | struct ap_queue *aq; |
dc4b6ded | 482 | enum ap_sm_wait wait = AP_SM_WAIT_NONE; |
3f3007af MS |
483 | |
484 | /* Reset the indicator if interrupts are used. Thus new interrupts can | |
cada938a | 485 | * be received. Doing it in the beginning of the tasklet is therefore |
3f3007af MS |
486 | * important that no requests on any AP get lost. |
487 | */ | |
cabebb69 | 488 | if (ap_irq_flag) |
3f3007af MS |
489 | xchg(ap_airq.lsi_ptr, 0); |
490 | ||
bc4b295e HF |
491 | spin_lock_bh(&ap_queues_lock); |
492 | hash_for_each(ap_queues, bkt, aq, hnode) { | |
493 | spin_lock_bh(&aq->lock); | |
dc4b6ded | 494 | wait = min(wait, ap_sm_event_loop(aq, AP_SM_EVENT_POLL)); |
bc4b295e | 495 | spin_unlock_bh(&aq->lock); |
3f3007af | 496 | } |
bc4b295e | 497 | spin_unlock_bh(&ap_queues_lock); |
e28d2af4 IT |
498 | |
499 | ap_wait(wait); | |
af512ed0 RW |
500 | } |
501 | ||
9af3e04e MS |
502 | static int ap_pending_requests(void) |
503 | { | |
bc4b295e | 504 | int bkt; |
e28d2af4 IT |
505 | struct ap_queue *aq; |
506 | ||
bc4b295e HF |
507 | spin_lock_bh(&ap_queues_lock); |
508 | hash_for_each(ap_queues, bkt, aq, hnode) { | |
509 | if (aq->queue_count == 0) | |
510 | continue; | |
511 | spin_unlock_bh(&ap_queues_lock); | |
512 | return 1; | |
9af3e04e | 513 | } |
bc4b295e | 514 | spin_unlock_bh(&ap_queues_lock); |
e28d2af4 | 515 | return 0; |
9af3e04e MS |
516 | } |
517 | ||
83e9d5d2 MS |
518 | /** |
519 | * ap_poll_thread(): Thread that polls for finished requests. | |
520 | * @data: Unused pointer | |
521 | * | |
522 | * AP bus poll thread. The purpose of this thread is to poll for | |
523 | * finished requests in a loop if there is a "free" cpu - that is | |
524 | * a cpu that doesn't have anything better to do. The polling stops | |
525 | * as soon as there is another task or if all messages have been | |
526 | * delivered. | |
527 | */ | |
528 | static int ap_poll_thread(void *data) | |
529 | { | |
530 | DECLARE_WAITQUEUE(wait, current); | |
83e9d5d2 MS |
531 | |
532 | set_user_nice(current, MAX_NICE); | |
533 | set_freezable(); | |
534 | while (!kthread_should_stop()) { | |
535 | add_wait_queue(&ap_poll_wait, &wait); | |
536 | set_current_state(TASK_INTERRUPTIBLE); | |
41677b1d | 537 | if (!ap_pending_requests()) { |
83e9d5d2 MS |
538 | schedule(); |
539 | try_to_freeze(); | |
540 | } | |
541 | set_current_state(TASK_RUNNING); | |
542 | remove_wait_queue(&ap_poll_wait, &wait); | |
83e9d5d2 MS |
543 | if (need_resched()) { |
544 | schedule(); | |
545 | try_to_freeze(); | |
546 | continue; | |
547 | } | |
3f3007af | 548 | ap_tasklet_fn(0); |
9af3e04e MS |
549 | } |
550 | ||
83e9d5d2 MS |
551 | return 0; |
552 | } | |
553 | ||
554 | static int ap_poll_thread_start(void) | |
555 | { | |
556 | int rc; | |
557 | ||
cabebb69 | 558 | if (ap_irq_flag || ap_poll_kthread) |
83e9d5d2 MS |
559 | return 0; |
560 | mutex_lock(&ap_poll_thread_mutex); | |
561 | ap_poll_kthread = kthread_run(ap_poll_thread, NULL, "appoll"); | |
9c705206 | 562 | rc = PTR_ERR_OR_ZERO(ap_poll_kthread); |
83e9d5d2 MS |
563 | if (rc) |
564 | ap_poll_kthread = NULL; | |
565 | mutex_unlock(&ap_poll_thread_mutex); | |
566 | return rc; | |
567 | } | |
568 | ||
569 | static void ap_poll_thread_stop(void) | |
570 | { | |
571 | if (!ap_poll_kthread) | |
572 | return; | |
573 | mutex_lock(&ap_poll_thread_mutex); | |
574 | kthread_stop(ap_poll_kthread); | |
575 | ap_poll_kthread = NULL; | |
576 | mutex_unlock(&ap_poll_thread_mutex); | |
577 | } | |
578 | ||
e28d2af4 IT |
579 | #define is_card_dev(x) ((x)->parent == ap_root_device) |
580 | #define is_queue_dev(x) ((x)->parent != ap_root_device) | |
1534c382 MS |
581 | |
582 | /** | |
1749a81d FB |
583 | * ap_bus_match() |
584 | * @dev: Pointer to device | |
585 | * @drv: Pointer to device_driver | |
586 | * | |
1534c382 MS |
587 | * AP bus driver registration/unregistration. |
588 | */ | |
589 | static int ap_bus_match(struct device *dev, struct device_driver *drv) | |
590 | { | |
1534c382 MS |
591 | struct ap_driver *ap_drv = to_ap_drv(drv); |
592 | struct ap_device_id *id; | |
593 | ||
1749a81d | 594 | /* |
1534c382 MS |
595 | * Compare device type of the device with the list of |
596 | * supported types of the device_driver. | |
597 | */ | |
598 | for (id = ap_drv->ids; id->match_flags; id++) { | |
e28d2af4 IT |
599 | if (is_card_dev(dev) && |
600 | id->match_flags & AP_DEVICE_ID_MATCH_CARD_TYPE && | |
601 | id->dev_type == to_ap_dev(dev)->device_type) | |
602 | return 1; | |
603 | if (is_queue_dev(dev) && | |
604 | id->match_flags & AP_DEVICE_ID_MATCH_QUEUE_TYPE && | |
605 | id->dev_type == to_ap_dev(dev)->device_type) | |
606 | return 1; | |
1534c382 MS |
607 | } |
608 | return 0; | |
609 | } | |
610 | ||
611 | /** | |
1749a81d FB |
612 | * ap_uevent(): Uevent function for AP devices. |
613 | * @dev: Pointer to device | |
614 | * @env: Pointer to kobj_uevent_env | |
615 | * | |
616 | * It sets up a single environment variable DEV_TYPE which contains the | |
617 | * hardware device type. | |
1534c382 | 618 | */ |
2a81ada3 | 619 | static int ap_uevent(const struct device *dev, struct kobj_uevent_env *env) |
1534c382 | 620 | { |
df6f508c | 621 | int rc = 0; |
2a81ada3 | 622 | const struct ap_device *ap_dev = to_ap_dev(dev); |
1534c382 | 623 | |
837cd105 HF |
624 | /* Uevents from ap bus core don't need extensions to the env */ |
625 | if (dev == ap_root_device) | |
626 | return 0; | |
1534c382 | 627 | |
df6f508c HF |
628 | if (is_card_dev(dev)) { |
629 | struct ap_card *ac = to_ap_card(&ap_dev->device); | |
bf62456e | 630 | |
df6f508c HF |
631 | /* Set up DEV_TYPE environment variable. */ |
632 | rc = add_uevent_var(env, "DEV_TYPE=%04X", ap_dev->device_type); | |
633 | if (rc) | |
634 | return rc; | |
635 | /* Add MODALIAS= */ | |
636 | rc = add_uevent_var(env, "MODALIAS=ap:t%02X", ap_dev->device_type); | |
637 | if (rc) | |
638 | return rc; | |
639 | ||
640 | /* Add MODE=<accel|cca|ep11> */ | |
d4c53ae8 | 641 | if (ac->hwinfo.accel) |
df6f508c | 642 | rc = add_uevent_var(env, "MODE=accel"); |
d4c53ae8 | 643 | else if (ac->hwinfo.cca) |
df6f508c | 644 | rc = add_uevent_var(env, "MODE=cca"); |
d4c53ae8 | 645 | else if (ac->hwinfo.ep11) |
df6f508c HF |
646 | rc = add_uevent_var(env, "MODE=ep11"); |
647 | if (rc) | |
648 | return rc; | |
649 | } else { | |
650 | struct ap_queue *aq = to_ap_queue(&ap_dev->device); | |
651 | ||
652 | /* Add MODE=<accel|cca|ep11> */ | |
d4c53ae8 | 653 | if (aq->card->hwinfo.accel) |
df6f508c | 654 | rc = add_uevent_var(env, "MODE=accel"); |
d4c53ae8 | 655 | else if (aq->card->hwinfo.cca) |
df6f508c | 656 | rc = add_uevent_var(env, "MODE=cca"); |
d4c53ae8 | 657 | else if (aq->card->hwinfo.ep11) |
df6f508c HF |
658 | rc = add_uevent_var(env, "MODE=ep11"); |
659 | if (rc) | |
660 | return rc; | |
661 | } | |
837cd105 HF |
662 | |
663 | return 0; | |
664 | } | |
665 | ||
666 | static void ap_send_init_scan_done_uevent(void) | |
667 | { | |
668 | char *envp[] = { "INITSCAN=done", NULL }; | |
669 | ||
670 | kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); | |
671 | } | |
672 | ||
673 | static void ap_send_bindings_complete_uevent(void) | |
674 | { | |
0677519a HF |
675 | char buf[32]; |
676 | char *envp[] = { "BINDINGS=complete", buf, NULL }; | |
837cd105 | 677 | |
0677519a HF |
678 | snprintf(buf, sizeof(buf), "COMPLETECOUNT=%llu", |
679 | atomic64_inc_return(&ap_bindings_complete_count)); | |
837cd105 HF |
680 | kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); |
681 | } | |
682 | ||
df6f508c HF |
683 | void ap_send_config_uevent(struct ap_device *ap_dev, bool cfg) |
684 | { | |
685 | char buf[16]; | |
686 | char *envp[] = { buf, NULL }; | |
687 | ||
688 | snprintf(buf, sizeof(buf), "CONFIG=%d", cfg ? 1 : 0); | |
689 | ||
690 | kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp); | |
691 | } | |
692 | EXPORT_SYMBOL(ap_send_config_uevent); | |
693 | ||
694 | void ap_send_online_uevent(struct ap_device *ap_dev, int online) | |
695 | { | |
696 | char buf[16]; | |
697 | char *envp[] = { buf, NULL }; | |
698 | ||
699 | snprintf(buf, sizeof(buf), "ONLINE=%d", online ? 1 : 0); | |
700 | ||
701 | kobject_uevent_env(&ap_dev->device.kobj, KOBJ_CHANGE, envp); | |
702 | } | |
703 | EXPORT_SYMBOL(ap_send_online_uevent); | |
704 | ||
d9b38e9d HF |
705 | static void ap_send_mask_changed_uevent(unsigned long *newapm, |
706 | unsigned long *newaqm) | |
707 | { | |
708 | char buf[100]; | |
709 | char *envp[] = { buf, NULL }; | |
710 | ||
711 | if (newapm) | |
712 | snprintf(buf, sizeof(buf), | |
713 | "APMASK=0x%016lx%016lx%016lx%016lx\n", | |
714 | newapm[0], newapm[1], newapm[2], newapm[3]); | |
715 | else | |
716 | snprintf(buf, sizeof(buf), | |
717 | "AQMASK=0x%016lx%016lx%016lx%016lx\n", | |
718 | newaqm[0], newaqm[1], newaqm[2], newaqm[3]); | |
719 | ||
720 | kobject_uevent_env(&ap_root_device->kobj, KOBJ_CHANGE, envp); | |
721 | } | |
722 | ||
837cd105 HF |
723 | /* |
724 | * calc # of bound APQNs | |
725 | */ | |
726 | ||
727 | struct __ap_calc_ctrs { | |
728 | unsigned int apqns; | |
729 | unsigned int bound; | |
730 | }; | |
731 | ||
732 | static int __ap_calc_helper(struct device *dev, void *arg) | |
733 | { | |
2004b57c | 734 | struct __ap_calc_ctrs *pctrs = (struct __ap_calc_ctrs *)arg; |
837cd105 HF |
735 | |
736 | if (is_queue_dev(dev)) { | |
737 | pctrs->apqns++; | |
c8c68c5f | 738 | if (dev->driver) |
837cd105 HF |
739 | pctrs->bound++; |
740 | } | |
741 | ||
742 | return 0; | |
743 | } | |
744 | ||
745 | static void ap_calc_bound_apqns(unsigned int *apqns, unsigned int *bound) | |
746 | { | |
747 | struct __ap_calc_ctrs ctrs; | |
748 | ||
749 | memset(&ctrs, 0, sizeof(ctrs)); | |
2004b57c | 750 | bus_for_each_dev(&ap_bus_type, NULL, (void *)&ctrs, __ap_calc_helper); |
bf62456e | 751 | |
837cd105 HF |
752 | *apqns = ctrs.apqns; |
753 | *bound = ctrs.bound; | |
1534c382 MS |
754 | } |
755 | ||
837cd105 HF |
756 | /* |
757 | * After initial ap bus scan do check if all existing APQNs are | |
758 | * bound to device drivers. | |
759 | */ | |
760 | static void ap_check_bindings_complete(void) | |
761 | { | |
762 | unsigned int apqns, bound; | |
763 | ||
764 | if (atomic64_read(&ap_scan_bus_count) >= 1) { | |
765 | ap_calc_bound_apqns(&apqns, &bound); | |
766 | if (bound == apqns) { | |
767 | if (!completion_done(&ap_init_apqn_bindings_complete)) { | |
768 | complete_all(&ap_init_apqn_bindings_complete); | |
3f74eb5f | 769 | AP_DBF_INFO("%s complete\n", __func__); |
837cd105 HF |
770 | } |
771 | ap_send_bindings_complete_uevent(); | |
772 | } | |
773 | } | |
774 | } | |
775 | ||
776 | /* | |
777 | * Interface to wait for the AP bus to have done one initial ap bus | |
778 | * scan and all detected APQNs have been bound to device drivers. | |
779 | * If these both conditions are not fulfilled, this function blocks | |
780 | * on a condition with wait_for_completion_interruptible_timeout(). | |
781 | * If these both conditions are fulfilled (before the timeout hits) | |
782 | * the return value is 0. If the timeout (in jiffies) hits instead | |
783 | * -ETIME is returned. On failures negative return values are | |
784 | * returned to the caller. | |
785 | */ | |
786 | int ap_wait_init_apqn_bindings_complete(unsigned long timeout) | |
787 | { | |
788 | long l; | |
789 | ||
790 | if (completion_done(&ap_init_apqn_bindings_complete)) | |
791 | return 0; | |
792 | ||
793 | if (timeout) | |
794 | l = wait_for_completion_interruptible_timeout( | |
795 | &ap_init_apqn_bindings_complete, timeout); | |
796 | else | |
797 | l = wait_for_completion_interruptible( | |
798 | &ap_init_apqn_bindings_complete); | |
799 | if (l < 0) | |
800 | return l == -ERESTARTSYS ? -EINTR : l; | |
801 | else if (l == 0 && timeout) | |
802 | return -ETIME; | |
803 | ||
804 | return 0; | |
805 | } | |
806 | EXPORT_SYMBOL(ap_wait_init_apqn_bindings_complete); | |
807 | ||
e28d2af4 | 808 | static int __ap_queue_devices_with_id_unregister(struct device *dev, void *data) |
83e9d5d2 | 809 | { |
e28d2af4 | 810 | if (is_queue_dev(dev) && |
2004b57c | 811 | AP_QID_CARD(to_ap_queue(dev)->qid) == (int)(long)data) |
e28d2af4 | 812 | device_unregister(dev); |
83e9d5d2 MS |
813 | return 0; |
814 | } | |
815 | ||
7e0bdbe5 HF |
816 | static int __ap_revise_reserved(struct device *dev, void *dummy) |
817 | { | |
818 | int rc, card, queue, devres, drvres; | |
819 | ||
820 | if (is_queue_dev(dev)) { | |
821 | card = AP_QID_CARD(to_ap_queue(dev)->qid); | |
822 | queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); | |
823 | mutex_lock(&ap_perms_mutex); | |
2004b57c HF |
824 | devres = test_bit_inv(card, ap_perms.apm) && |
825 | test_bit_inv(queue, ap_perms.aqm); | |
7e0bdbe5 HF |
826 | mutex_unlock(&ap_perms_mutex); |
827 | drvres = to_ap_drv(dev->driver)->flags | |
828 | & AP_DRIVER_FLAG_DEFAULT; | |
829 | if (!!devres != !!drvres) { | |
08b2c370 HF |
830 | pr_debug("%s reprobing queue=%02x.%04x\n", |
831 | __func__, card, queue); | |
7e0bdbe5 | 832 | rc = device_reprobe(dev); |
132c1e74 HF |
833 | if (rc) |
834 | AP_DBF_WARN("%s reprobing queue=%02x.%04x failed\n", | |
835 | __func__, card, queue); | |
7e0bdbe5 HF |
836 | } |
837 | } | |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | static void ap_bus_revise_bindings(void) | |
843 | { | |
844 | bus_for_each_dev(&ap_bus_type, NULL, NULL, __ap_revise_reserved); | |
845 | } | |
846 | ||
3f85d1df TK |
847 | /** |
848 | * ap_owned_by_def_drv: indicates whether an AP adapter is reserved for the | |
849 | * default host driver or not. | |
850 | * @card: the APID of the adapter card to check | |
851 | * @queue: the APQI of the queue to check | |
852 | * | |
853 | * Note: the ap_perms_mutex must be locked by the caller of this function. | |
854 | * | |
855 | * Return: an int specifying whether the AP adapter is reserved for the host (1) | |
856 | * or not (0). | |
857 | */ | |
7e0bdbe5 HF |
858 | int ap_owned_by_def_drv(int card, int queue) |
859 | { | |
860 | int rc = 0; | |
861 | ||
862 | if (card < 0 || card >= AP_DEVICES || queue < 0 || queue >= AP_DOMAINS) | |
863 | return -EINVAL; | |
864 | ||
2004b57c HF |
865 | if (test_bit_inv(card, ap_perms.apm) && |
866 | test_bit_inv(queue, ap_perms.aqm)) | |
7e0bdbe5 HF |
867 | rc = 1; |
868 | ||
7e0bdbe5 HF |
869 | return rc; |
870 | } | |
871 | EXPORT_SYMBOL(ap_owned_by_def_drv); | |
872 | ||
3f85d1df TK |
873 | /** |
874 | * ap_apqn_in_matrix_owned_by_def_drv: indicates whether every APQN contained in | |
875 | * a set is reserved for the host drivers | |
876 | * or not. | |
877 | * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check | |
878 | * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check | |
879 | * | |
880 | * Note: the ap_perms_mutex must be locked by the caller of this function. | |
881 | * | |
882 | * Return: an int specifying whether each APQN is reserved for the host (1) or | |
883 | * not (0) | |
884 | */ | |
7e0bdbe5 HF |
885 | int ap_apqn_in_matrix_owned_by_def_drv(unsigned long *apm, |
886 | unsigned long *aqm) | |
887 | { | |
888 | int card, queue, rc = 0; | |
889 | ||
7e0bdbe5 HF |
890 | for (card = 0; !rc && card < AP_DEVICES; card++) |
891 | if (test_bit_inv(card, apm) && | |
892 | test_bit_inv(card, ap_perms.apm)) | |
893 | for (queue = 0; !rc && queue < AP_DOMAINS; queue++) | |
894 | if (test_bit_inv(queue, aqm) && | |
895 | test_bit_inv(queue, ap_perms.aqm)) | |
896 | rc = 1; | |
897 | ||
7e0bdbe5 HF |
898 | return rc; |
899 | } | |
900 | EXPORT_SYMBOL(ap_apqn_in_matrix_owned_by_def_drv); | |
901 | ||
1534c382 MS |
902 | static int ap_device_probe(struct device *dev) |
903 | { | |
904 | struct ap_device *ap_dev = to_ap_dev(dev); | |
3f3007af | 905 | struct ap_driver *ap_drv = to_ap_drv(dev->driver); |
29c2680f HF |
906 | int card, queue, devres, drvres, rc = -ENODEV; |
907 | ||
908 | if (!get_device(dev)) | |
909 | return rc; | |
7e0bdbe5 HF |
910 | |
911 | if (is_queue_dev(dev)) { | |
912 | /* | |
913 | * If the apqn is marked as reserved/used by ap bus and | |
914 | * default drivers, only probe with drivers with the default | |
915 | * flag set. If it is not marked, only probe with drivers | |
916 | * with the default flag not set. | |
917 | */ | |
918 | card = AP_QID_CARD(to_ap_queue(dev)->qid); | |
919 | queue = AP_QID_QUEUE(to_ap_queue(dev)->qid); | |
920 | mutex_lock(&ap_perms_mutex); | |
2004b57c HF |
921 | devres = test_bit_inv(card, ap_perms.apm) && |
922 | test_bit_inv(queue, ap_perms.aqm); | |
7e0bdbe5 HF |
923 | mutex_unlock(&ap_perms_mutex); |
924 | drvres = ap_drv->flags & AP_DRIVER_FLAG_DEFAULT; | |
925 | if (!!devres != !!drvres) | |
29c2680f | 926 | goto out; |
7e0bdbe5 | 927 | } |
666e68e0 | 928 | |
e3850508 | 929 | /* Add queue/card to list of active queues/cards */ |
bc4b295e HF |
930 | spin_lock_bh(&ap_queues_lock); |
931 | if (is_queue_dev(dev)) | |
932 | hash_add(ap_queues, &to_ap_queue(dev)->hnode, | |
933 | to_ap_queue(dev)->qid); | |
934 | spin_unlock_bh(&ap_queues_lock); | |
e3850508 | 935 | |
1534c382 | 936 | rc = ap_drv->probe ? ap_drv->probe(ap_dev) : -ENODEV; |
e3850508 HF |
937 | |
938 | if (rc) { | |
bc4b295e HF |
939 | spin_lock_bh(&ap_queues_lock); |
940 | if (is_queue_dev(dev)) | |
941 | hash_del(&to_ap_queue(dev)->hnode); | |
942 | spin_unlock_bh(&ap_queues_lock); | |
2004b57c | 943 | } else { |
837cd105 | 944 | ap_check_bindings_complete(); |
2004b57c | 945 | } |
e3850508 | 946 | |
29c2680f HF |
947 | out: |
948 | if (rc) | |
949 | put_device(dev); | |
1534c382 MS |
950 | return rc; |
951 | } | |
952 | ||
fc7a6209 | 953 | static void ap_device_remove(struct device *dev) |
1534c382 MS |
954 | { |
955 | struct ap_device *ap_dev = to_ap_dev(dev); | |
c8c68c5f | 956 | struct ap_driver *ap_drv = to_ap_drv(dev->driver); |
1534c382 | 957 | |
01396a37 | 958 | /* prepare ap queue device removal */ |
104f708f | 959 | if (is_queue_dev(dev)) |
01396a37 HF |
960 | ap_queue_prepare_remove(to_ap_queue(dev)); |
961 | ||
962 | /* driver's chance to clean up gracefully */ | |
e3850508 HF |
963 | if (ap_drv->remove) |
964 | ap_drv->remove(ap_dev); | |
965 | ||
01396a37 HF |
966 | /* now do the ap queue device remove */ |
967 | if (is_queue_dev(dev)) | |
968 | ap_queue_remove(to_ap_queue(dev)); | |
969 | ||
e3850508 | 970 | /* Remove queue/card from list of active queues/cards */ |
bc4b295e HF |
971 | spin_lock_bh(&ap_queues_lock); |
972 | if (is_queue_dev(dev)) | |
973 | hash_del(&to_ap_queue(dev)->hnode); | |
974 | spin_unlock_bh(&ap_queues_lock); | |
e3850508 | 975 | |
29c2680f | 976 | put_device(dev); |
1534c382 MS |
977 | } |
978 | ||
bc4b295e HF |
979 | struct ap_queue *ap_get_qdev(ap_qid_t qid) |
980 | { | |
981 | int bkt; | |
982 | struct ap_queue *aq; | |
983 | ||
984 | spin_lock_bh(&ap_queues_lock); | |
985 | hash_for_each(ap_queues, bkt, aq, hnode) { | |
986 | if (aq->qid == qid) { | |
987 | get_device(&aq->ap_dev.device); | |
988 | spin_unlock_bh(&ap_queues_lock); | |
989 | return aq; | |
990 | } | |
991 | } | |
992 | spin_unlock_bh(&ap_queues_lock); | |
993 | ||
994 | return NULL; | |
995 | } | |
996 | EXPORT_SYMBOL(ap_get_qdev); | |
997 | ||
1534c382 MS |
998 | int ap_driver_register(struct ap_driver *ap_drv, struct module *owner, |
999 | char *name) | |
1000 | { | |
1001 | struct device_driver *drv = &ap_drv->driver; | |
1002 | ||
1003 | drv->bus = &ap_bus_type; | |
1534c382 MS |
1004 | drv->owner = owner; |
1005 | drv->name = name; | |
1006 | return driver_register(drv); | |
1007 | } | |
1008 | EXPORT_SYMBOL(ap_driver_register); | |
1009 | ||
1010 | void ap_driver_unregister(struct ap_driver *ap_drv) | |
1011 | { | |
1012 | driver_unregister(&ap_drv->driver); | |
1013 | } | |
1014 | EXPORT_SYMBOL(ap_driver_unregister); | |
1015 | ||
dabecb29 HD |
1016 | void ap_bus_force_rescan(void) |
1017 | { | |
e14aec23 HF |
1018 | /* Only trigger AP bus scans after the initial scan is done */ |
1019 | if (atomic64_read(&ap_scan_bus_count) <= 0) | |
1020 | return; | |
1021 | ||
56bbe686 | 1022 | /* processing a asynchronous bus rescan */ |
fcd0d1f6 | 1023 | del_timer(&ap_config_timer); |
8139b89d MS |
1024 | queue_work(system_long_wq, &ap_scan_work); |
1025 | flush_work(&ap_scan_work); | |
dabecb29 HD |
1026 | } |
1027 | EXPORT_SYMBOL(ap_bus_force_rescan); | |
1028 | ||
0d9c038f | 1029 | /* |
2004b57c HF |
1030 | * A config change has happened, force an ap bus rescan. |
1031 | */ | |
0d9c038f TK |
1032 | void ap_bus_cfg_chg(void) |
1033 | { | |
08b2c370 | 1034 | pr_debug("%s config change, forcing bus rescan\n", __func__); |
0d9c038f TK |
1035 | |
1036 | ap_bus_force_rescan(); | |
1037 | } | |
1038 | ||
1749a81d | 1039 | /* |
3d8f60d3 HF |
1040 | * hex2bitmap() - parse hex mask string and set bitmap. |
1041 | * Valid strings are "0x012345678" with at least one valid hex number. | |
1042 | * Rest of the bitmap to the right is padded with 0. No spaces allowed | |
1043 | * within the string, the leading 0x may be omitted. | |
1044 | * Returns the bitmask with exactly the bits set as given by the hex | |
1045 | * string (both in big endian order). | |
1534c382 | 1046 | */ |
7e0bdbe5 HF |
1047 | static int hex2bitmap(const char *str, unsigned long *bitmap, int bits) |
1048 | { | |
1049 | int i, n, b; | |
1050 | ||
1051 | /* bits needs to be a multiple of 8 */ | |
1052 | if (bits & 0x07) | |
1053 | return -EINVAL; | |
1054 | ||
7e0bdbe5 HF |
1055 | if (str[0] == '0' && str[1] == 'x') |
1056 | str++; | |
1057 | if (*str == 'x') | |
1058 | str++; | |
1059 | ||
1060 | for (i = 0; isxdigit(*str) && i < bits; str++) { | |
1061 | b = hex_to_bin(*str); | |
1062 | for (n = 0; n < 4; n++) | |
1063 | if (b & (0x08 >> n)) | |
1064 | set_bit_inv(i + n, bitmap); | |
1065 | i += 4; | |
1066 | } | |
1067 | ||
3d8f60d3 HF |
1068 | if (*str == '\n') |
1069 | str++; | |
1070 | if (*str) | |
1071 | return -EINVAL; | |
1072 | return 0; | |
1073 | } | |
1074 | ||
1075 | /* | |
fa108f95 MS |
1076 | * modify_bitmap() - parse bitmask argument and modify an existing |
1077 | * bit mask accordingly. A concatenation (done with ',') of these | |
1078 | * terms is recognized: | |
3d8f60d3 HF |
1079 | * +<bitnr>[-<bitnr>] or -<bitnr>[-<bitnr>] |
1080 | * <bitnr> may be any valid number (hex, decimal or octal) in the range | |
1081 | * 0...bits-1; the leading + or - is required. Here are some examples: | |
1082 | * +0-15,+32,-128,-0xFF | |
1083 | * -0-255,+1-16,+0x128 | |
1084 | * +1,+2,+3,+4,-5,-7-10 | |
fa108f95 MS |
1085 | * Returns the new bitmap after all changes have been applied. Every |
1086 | * positive value in the string will set a bit and every negative value | |
1087 | * in the string will clear a bit. As a bit may be touched more than once, | |
1088 | * the last 'operation' wins: | |
1089 | * +0-255,-128 = first bits 0-255 will be set, then bit 128 will be | |
1090 | * cleared again. All other bits are unmodified. | |
3d8f60d3 | 1091 | */ |
fa108f95 | 1092 | static int modify_bitmap(const char *str, unsigned long *bitmap, int bits) |
3d8f60d3 HF |
1093 | { |
1094 | int a, i, z; | |
1095 | char *np, sign; | |
1096 | ||
1097 | /* bits needs to be a multiple of 8 */ | |
1098 | if (bits & 0x07) | |
7e0bdbe5 HF |
1099 | return -EINVAL; |
1100 | ||
3d8f60d3 HF |
1101 | while (*str) { |
1102 | sign = *str++; | |
1103 | if (sign != '+' && sign != '-') | |
1104 | return -EINVAL; | |
1105 | a = z = simple_strtoul(str, &np, 0); | |
1106 | if (str == np || a >= bits) | |
1107 | return -EINVAL; | |
1108 | str = np; | |
1109 | if (*str == '-') { | |
1110 | z = simple_strtoul(++str, &np, 0); | |
1111 | if (str == np || a > z || z >= bits) | |
1112 | return -EINVAL; | |
1113 | str = np; | |
1114 | } | |
1115 | for (i = a; i <= z; i++) | |
fa108f95 MS |
1116 | if (sign == '+') |
1117 | set_bit_inv(i, bitmap); | |
1118 | else | |
1119 | clear_bit_inv(i, bitmap); | |
3d8f60d3 HF |
1120 | while (*str == ',' || *str == '\n') |
1121 | str++; | |
1122 | } | |
1123 | ||
7e0bdbe5 HF |
1124 | return 0; |
1125 | } | |
1126 | ||
4f8206b8 TK |
1127 | static int ap_parse_bitmap_str(const char *str, unsigned long *bitmap, int bits, |
1128 | unsigned long *newmap) | |
1129 | { | |
1130 | unsigned long size; | |
1131 | int rc; | |
1132 | ||
1133 | size = BITS_TO_LONGS(bits) * sizeof(unsigned long); | |
1134 | if (*str == '+' || *str == '-') { | |
1135 | memcpy(newmap, bitmap, size); | |
1136 | rc = modify_bitmap(str, newmap, bits); | |
1137 | } else { | |
1138 | memset(newmap, 0, size); | |
1139 | rc = hex2bitmap(str, newmap, bits); | |
1140 | } | |
1141 | return rc; | |
1142 | } | |
1143 | ||
00fab235 HF |
1144 | int ap_parse_mask_str(const char *str, |
1145 | unsigned long *bitmap, int bits, | |
1146 | struct mutex *lock) | |
3d8f60d3 | 1147 | { |
fa108f95 MS |
1148 | unsigned long *newmap, size; |
1149 | int rc; | |
3d8f60d3 HF |
1150 | |
1151 | /* bits needs to be a multiple of 8 */ | |
1152 | if (bits & 0x07) | |
1153 | return -EINVAL; | |
1154 | ||
2004b57c | 1155 | size = BITS_TO_LONGS(bits) * sizeof(unsigned long); |
fa108f95 MS |
1156 | newmap = kmalloc(size, GFP_KERNEL); |
1157 | if (!newmap) | |
1158 | return -ENOMEM; | |
1159 | if (mutex_lock_interruptible(lock)) { | |
1160 | kfree(newmap); | |
1161 | return -ERESTARTSYS; | |
1162 | } | |
4f8206b8 | 1163 | rc = ap_parse_bitmap_str(str, bitmap, bits, newmap); |
fa108f95 MS |
1164 | if (rc == 0) |
1165 | memcpy(bitmap, newmap, size); | |
3d8f60d3 | 1166 | mutex_unlock(lock); |
fa108f95 MS |
1167 | kfree(newmap); |
1168 | return rc; | |
3d8f60d3 | 1169 | } |
00fab235 | 1170 | EXPORT_SYMBOL(ap_parse_mask_str); |
3d8f60d3 HF |
1171 | |
1172 | /* | |
1173 | * AP bus attributes. | |
1174 | */ | |
1175 | ||
75cff725 | 1176 | static ssize_t ap_domain_show(const struct bus_type *bus, char *buf) |
1534c382 | 1177 | { |
964d581d | 1178 | return sysfs_emit(buf, "%d\n", ap_domain_index); |
1534c382 MS |
1179 | } |
1180 | ||
75cff725 | 1181 | static ssize_t ap_domain_store(const struct bus_type *bus, |
fc1d3f02 IT |
1182 | const char *buf, size_t count) |
1183 | { | |
1184 | int domain; | |
1185 | ||
1186 | if (sscanf(buf, "%i\n", &domain) != 1 || | |
7e0bdbe5 HF |
1187 | domain < 0 || domain > ap_max_domain_id || |
1188 | !test_bit_inv(domain, ap_perms.aqm)) | |
fc1d3f02 | 1189 | return -EINVAL; |
c8337c47 | 1190 | |
fc1d3f02 IT |
1191 | spin_lock_bh(&ap_domain_lock); |
1192 | ap_domain_index = domain; | |
1193 | spin_unlock_bh(&ap_domain_lock); | |
cccd85bf | 1194 | |
3f74eb5f HF |
1195 | AP_DBF_INFO("%s stored new default domain=%d\n", |
1196 | __func__, domain); | |
cccd85bf | 1197 | |
fc1d3f02 IT |
1198 | return count; |
1199 | } | |
1200 | ||
ac2b96f3 | 1201 | static BUS_ATTR_RW(ap_domain); |
1534c382 | 1202 | |
75cff725 | 1203 | static ssize_t ap_control_domain_mask_show(const struct bus_type *bus, char *buf) |
91f3e3ea | 1204 | { |
c8337c47 | 1205 | if (!ap_qci_info) /* QCI not supported */ |
964d581d | 1206 | return sysfs_emit(buf, "not supported\n"); |
40501c70 | 1207 | |
964d581d HF |
1208 | return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
1209 | ap_qci_info->adm[0], ap_qci_info->adm[1], | |
1210 | ap_qci_info->adm[2], ap_qci_info->adm[3], | |
1211 | ap_qci_info->adm[4], ap_qci_info->adm[5], | |
1212 | ap_qci_info->adm[6], ap_qci_info->adm[7]); | |
91f3e3ea IT |
1213 | } |
1214 | ||
ac2b96f3 | 1215 | static BUS_ATTR_RO(ap_control_domain_mask); |
91f3e3ea | 1216 | |
75cff725 | 1217 | static ssize_t ap_usage_domain_mask_show(const struct bus_type *bus, char *buf) |
e28d2af4 | 1218 | { |
c8337c47 | 1219 | if (!ap_qci_info) /* QCI not supported */ |
964d581d | 1220 | return sysfs_emit(buf, "not supported\n"); |
40501c70 | 1221 | |
964d581d HF |
1222 | return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
1223 | ap_qci_info->aqm[0], ap_qci_info->aqm[1], | |
1224 | ap_qci_info->aqm[2], ap_qci_info->aqm[3], | |
1225 | ap_qci_info->aqm[4], ap_qci_info->aqm[5], | |
1226 | ap_qci_info->aqm[6], ap_qci_info->aqm[7]); | |
e28d2af4 IT |
1227 | } |
1228 | ||
ac2b96f3 | 1229 | static BUS_ATTR_RO(ap_usage_domain_mask); |
1534c382 | 1230 | |
75cff725 | 1231 | static ssize_t ap_adapter_mask_show(const struct bus_type *bus, char *buf) |
aa55bf5f | 1232 | { |
c8337c47 | 1233 | if (!ap_qci_info) /* QCI not supported */ |
964d581d | 1234 | return sysfs_emit(buf, "not supported\n"); |
40501c70 | 1235 | |
964d581d HF |
1236 | return sysfs_emit(buf, "0x%08x%08x%08x%08x%08x%08x%08x%08x\n", |
1237 | ap_qci_info->apm[0], ap_qci_info->apm[1], | |
1238 | ap_qci_info->apm[2], ap_qci_info->apm[3], | |
1239 | ap_qci_info->apm[4], ap_qci_info->apm[5], | |
1240 | ap_qci_info->apm[6], ap_qci_info->apm[7]); | |
aa55bf5f HF |
1241 | } |
1242 | ||
1243 | static BUS_ATTR_RO(ap_adapter_mask); | |
1244 | ||
75cff725 | 1245 | static ssize_t ap_interrupts_show(const struct bus_type *bus, char *buf) |
cb17a636 | 1246 | { |
964d581d | 1247 | return sysfs_emit(buf, "%d\n", ap_irq_flag ? 1 : 0); |
cb17a636 FB |
1248 | } |
1249 | ||
ac2b96f3 HF |
1250 | static BUS_ATTR_RO(ap_interrupts); |
1251 | ||
75cff725 | 1252 | static ssize_t config_time_show(const struct bus_type *bus, char *buf) |
ac2b96f3 | 1253 | { |
964d581d | 1254 | return sysfs_emit(buf, "%d\n", ap_config_time); |
ac2b96f3 | 1255 | } |
cb17a636 | 1256 | |
75cff725 | 1257 | static ssize_t config_time_store(const struct bus_type *bus, |
ac2b96f3 | 1258 | const char *buf, size_t count) |
1534c382 MS |
1259 | { |
1260 | int time; | |
1261 | ||
1262 | if (sscanf(buf, "%d\n", &time) != 1 || time < 5 || time > 120) | |
1263 | return -EINVAL; | |
1264 | ap_config_time = time; | |
fcd0d1f6 | 1265 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
1534c382 MS |
1266 | return count; |
1267 | } | |
1268 | ||
ac2b96f3 | 1269 | static BUS_ATTR_RW(config_time); |
1534c382 | 1270 | |
75cff725 | 1271 | static ssize_t poll_thread_show(const struct bus_type *bus, char *buf) |
1534c382 | 1272 | { |
964d581d | 1273 | return sysfs_emit(buf, "%d\n", ap_poll_kthread ? 1 : 0); |
1534c382 MS |
1274 | } |
1275 | ||
75cff725 | 1276 | static ssize_t poll_thread_store(const struct bus_type *bus, |
ac2b96f3 | 1277 | const char *buf, size_t count) |
1534c382 | 1278 | { |
263c8454 HF |
1279 | bool value; |
1280 | int rc; | |
1534c382 | 1281 | |
263c8454 HF |
1282 | rc = kstrtobool(buf, &value); |
1283 | if (rc) | |
1284 | return rc; | |
1285 | ||
1286 | if (value) { | |
1534c382 MS |
1287 | rc = ap_poll_thread_start(); |
1288 | if (rc) | |
83e9d5d2 | 1289 | count = rc; |
2004b57c | 1290 | } else { |
1534c382 | 1291 | ap_poll_thread_stop(); |
2004b57c | 1292 | } |
1534c382 MS |
1293 | return count; |
1294 | } | |
1295 | ||
ac2b96f3 | 1296 | static BUS_ATTR_RW(poll_thread); |
1534c382 | 1297 | |
75cff725 | 1298 | static ssize_t poll_timeout_show(const struct bus_type *bus, char *buf) |
fe137230 | 1299 | { |
263c8454 | 1300 | return sysfs_emit(buf, "%lu\n", poll_high_timeout); |
fe137230 FB |
1301 | } |
1302 | ||
75cff725 | 1303 | static ssize_t poll_timeout_store(const struct bus_type *bus, const char *buf, |
fe137230 FB |
1304 | size_t count) |
1305 | { | |
263c8454 | 1306 | unsigned long value; |
fe137230 | 1307 | ktime_t hr_time; |
263c8454 HF |
1308 | int rc; |
1309 | ||
1310 | rc = kstrtoul(buf, 0, &value); | |
1311 | if (rc) | |
1312 | return rc; | |
fe137230 FB |
1313 | |
1314 | /* 120 seconds = maximum poll interval */ | |
263c8454 | 1315 | if (value > 120000000000UL) |
fe137230 | 1316 | return -EINVAL; |
263c8454 HF |
1317 | poll_high_timeout = value; |
1318 | hr_time = poll_high_timeout; | |
fe137230 | 1319 | |
8cc2af7c IT |
1320 | spin_lock_bh(&ap_poll_timer_lock); |
1321 | hrtimer_cancel(&ap_poll_timer); | |
1322 | hrtimer_set_expires(&ap_poll_timer, hr_time); | |
1323 | hrtimer_start_expires(&ap_poll_timer, HRTIMER_MODE_ABS); | |
1324 | spin_unlock_bh(&ap_poll_timer_lock); | |
1325 | ||
fe137230 FB |
1326 | return count; |
1327 | } | |
1328 | ||
ac2b96f3 | 1329 | static BUS_ATTR_RW(poll_timeout); |
fe137230 | 1330 | |
75cff725 | 1331 | static ssize_t ap_max_domain_id_show(const struct bus_type *bus, char *buf) |
5bc334bf | 1332 | { |
964d581d | 1333 | return sysfs_emit(buf, "%d\n", ap_max_domain_id); |
5bc334bf IT |
1334 | } |
1335 | ||
ac2b96f3 | 1336 | static BUS_ATTR_RO(ap_max_domain_id); |
5bc334bf | 1337 | |
75cff725 | 1338 | static ssize_t ap_max_adapter_id_show(const struct bus_type *bus, char *buf) |
c8337c47 | 1339 | { |
964d581d | 1340 | return sysfs_emit(buf, "%d\n", ap_max_adapter_id); |
c8337c47 HF |
1341 | } |
1342 | ||
1343 | static BUS_ATTR_RO(ap_max_adapter_id); | |
1344 | ||
75cff725 | 1345 | static ssize_t apmask_show(const struct bus_type *bus, char *buf) |
7e0bdbe5 HF |
1346 | { |
1347 | int rc; | |
1348 | ||
1349 | if (mutex_lock_interruptible(&ap_perms_mutex)) | |
1350 | return -ERESTARTSYS; | |
964d581d HF |
1351 | rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n", |
1352 | ap_perms.apm[0], ap_perms.apm[1], | |
1353 | ap_perms.apm[2], ap_perms.apm[3]); | |
7e0bdbe5 HF |
1354 | mutex_unlock(&ap_perms_mutex); |
1355 | ||
1356 | return rc; | |
1357 | } | |
1358 | ||
4f8206b8 TK |
1359 | static int __verify_card_reservations(struct device_driver *drv, void *data) |
1360 | { | |
1361 | int rc = 0; | |
1362 | struct ap_driver *ap_drv = to_ap_drv(drv); | |
1363 | unsigned long *newapm = (unsigned long *)data; | |
1364 | ||
1365 | /* | |
1366 | * increase the driver's module refcounter to be sure it is not | |
1367 | * going away when we invoke the callback function. | |
1368 | */ | |
1369 | if (!try_module_get(drv->owner)) | |
1370 | return 0; | |
1371 | ||
1372 | if (ap_drv->in_use) { | |
1373 | rc = ap_drv->in_use(newapm, ap_perms.aqm); | |
1374 | if (rc) | |
1375 | rc = -EBUSY; | |
1376 | } | |
1377 | ||
1378 | /* release the driver's module */ | |
1379 | module_put(drv->owner); | |
1380 | ||
1381 | return rc; | |
1382 | } | |
1383 | ||
1384 | static int apmask_commit(unsigned long *newapm) | |
1385 | { | |
1386 | int rc; | |
1387 | unsigned long reserved[BITS_TO_LONGS(AP_DEVICES)]; | |
1388 | ||
1389 | /* | |
1390 | * Check if any bits in the apmask have been set which will | |
1391 | * result in queues being removed from non-default drivers | |
1392 | */ | |
1393 | if (bitmap_andnot(reserved, newapm, ap_perms.apm, AP_DEVICES)) { | |
1394 | rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, | |
1395 | __verify_card_reservations); | |
1396 | if (rc) | |
1397 | return rc; | |
1398 | } | |
1399 | ||
1400 | memcpy(ap_perms.apm, newapm, APMASKSIZE); | |
1401 | ||
1402 | return 0; | |
1403 | } | |
1404 | ||
75cff725 | 1405 | static ssize_t apmask_store(const struct bus_type *bus, const char *buf, |
7e0bdbe5 HF |
1406 | size_t count) |
1407 | { | |
d9b38e9d | 1408 | int rc, changes = 0; |
4f8206b8 TK |
1409 | DECLARE_BITMAP(newapm, AP_DEVICES); |
1410 | ||
1411 | if (mutex_lock_interruptible(&ap_perms_mutex)) | |
1412 | return -ERESTARTSYS; | |
7e0bdbe5 | 1413 | |
4f8206b8 TK |
1414 | rc = ap_parse_bitmap_str(buf, ap_perms.apm, AP_DEVICES, newapm); |
1415 | if (rc) | |
1416 | goto done; | |
1417 | ||
d9b38e9d HF |
1418 | changes = memcmp(ap_perms.apm, newapm, APMASKSIZE); |
1419 | if (changes) | |
1420 | rc = apmask_commit(newapm); | |
4f8206b8 TK |
1421 | |
1422 | done: | |
1423 | mutex_unlock(&ap_perms_mutex); | |
3d8f60d3 HF |
1424 | if (rc) |
1425 | return rc; | |
7e0bdbe5 | 1426 | |
d9b38e9d HF |
1427 | if (changes) { |
1428 | ap_bus_revise_bindings(); | |
1429 | ap_send_mask_changed_uevent(newapm, NULL); | |
1430 | } | |
7e0bdbe5 HF |
1431 | |
1432 | return count; | |
1433 | } | |
1434 | ||
1435 | static BUS_ATTR_RW(apmask); | |
1436 | ||
75cff725 | 1437 | static ssize_t aqmask_show(const struct bus_type *bus, char *buf) |
7e0bdbe5 HF |
1438 | { |
1439 | int rc; | |
1440 | ||
1441 | if (mutex_lock_interruptible(&ap_perms_mutex)) | |
1442 | return -ERESTARTSYS; | |
964d581d HF |
1443 | rc = sysfs_emit(buf, "0x%016lx%016lx%016lx%016lx\n", |
1444 | ap_perms.aqm[0], ap_perms.aqm[1], | |
1445 | ap_perms.aqm[2], ap_perms.aqm[3]); | |
7e0bdbe5 HF |
1446 | mutex_unlock(&ap_perms_mutex); |
1447 | ||
1448 | return rc; | |
1449 | } | |
1450 | ||
4f8206b8 TK |
1451 | static int __verify_queue_reservations(struct device_driver *drv, void *data) |
1452 | { | |
1453 | int rc = 0; | |
1454 | struct ap_driver *ap_drv = to_ap_drv(drv); | |
1455 | unsigned long *newaqm = (unsigned long *)data; | |
1456 | ||
1457 | /* | |
1458 | * increase the driver's module refcounter to be sure it is not | |
1459 | * going away when we invoke the callback function. | |
1460 | */ | |
1461 | if (!try_module_get(drv->owner)) | |
1462 | return 0; | |
1463 | ||
1464 | if (ap_drv->in_use) { | |
1465 | rc = ap_drv->in_use(ap_perms.apm, newaqm); | |
1466 | if (rc) | |
2f23256c | 1467 | rc = -EBUSY; |
4f8206b8 TK |
1468 | } |
1469 | ||
1470 | /* release the driver's module */ | |
1471 | module_put(drv->owner); | |
1472 | ||
1473 | return rc; | |
1474 | } | |
1475 | ||
1476 | static int aqmask_commit(unsigned long *newaqm) | |
1477 | { | |
1478 | int rc; | |
1479 | unsigned long reserved[BITS_TO_LONGS(AP_DOMAINS)]; | |
1480 | ||
1481 | /* | |
1482 | * Check if any bits in the aqmask have been set which will | |
1483 | * result in queues being removed from non-default drivers | |
1484 | */ | |
1485 | if (bitmap_andnot(reserved, newaqm, ap_perms.aqm, AP_DOMAINS)) { | |
1486 | rc = bus_for_each_drv(&ap_bus_type, NULL, reserved, | |
1487 | __verify_queue_reservations); | |
1488 | if (rc) | |
1489 | return rc; | |
1490 | } | |
1491 | ||
1492 | memcpy(ap_perms.aqm, newaqm, AQMASKSIZE); | |
1493 | ||
1494 | return 0; | |
1495 | } | |
1496 | ||
75cff725 | 1497 | static ssize_t aqmask_store(const struct bus_type *bus, const char *buf, |
7e0bdbe5 HF |
1498 | size_t count) |
1499 | { | |
d9b38e9d | 1500 | int rc, changes = 0; |
4f8206b8 | 1501 | DECLARE_BITMAP(newaqm, AP_DOMAINS); |
7e0bdbe5 | 1502 | |
4f8206b8 TK |
1503 | if (mutex_lock_interruptible(&ap_perms_mutex)) |
1504 | return -ERESTARTSYS; | |
1505 | ||
1506 | rc = ap_parse_bitmap_str(buf, ap_perms.aqm, AP_DOMAINS, newaqm); | |
1507 | if (rc) | |
1508 | goto done; | |
1509 | ||
d9b38e9d HF |
1510 | changes = memcmp(ap_perms.aqm, newaqm, APMASKSIZE); |
1511 | if (changes) | |
1512 | rc = aqmask_commit(newaqm); | |
4f8206b8 TK |
1513 | |
1514 | done: | |
1515 | mutex_unlock(&ap_perms_mutex); | |
3d8f60d3 HF |
1516 | if (rc) |
1517 | return rc; | |
7e0bdbe5 | 1518 | |
d9b38e9d HF |
1519 | if (changes) { |
1520 | ap_bus_revise_bindings(); | |
1521 | ap_send_mask_changed_uevent(NULL, newaqm); | |
1522 | } | |
7e0bdbe5 HF |
1523 | |
1524 | return count; | |
1525 | } | |
1526 | ||
1527 | static BUS_ATTR_RW(aqmask); | |
1528 | ||
75cff725 | 1529 | static ssize_t scans_show(const struct bus_type *bus, char *buf) |
837cd105 | 1530 | { |
964d581d | 1531 | return sysfs_emit(buf, "%llu\n", atomic64_read(&ap_scan_bus_count)); |
837cd105 HF |
1532 | } |
1533 | ||
75cff725 | 1534 | static ssize_t scans_store(const struct bus_type *bus, const char *buf, |
8944d05f HF |
1535 | size_t count) |
1536 | { | |
1537 | AP_DBF_INFO("%s force AP bus rescan\n", __func__); | |
1538 | ||
1539 | ap_bus_force_rescan(); | |
1540 | ||
1541 | return count; | |
1542 | } | |
1543 | ||
1544 | static BUS_ATTR_RW(scans); | |
837cd105 | 1545 | |
75cff725 | 1546 | static ssize_t bindings_show(const struct bus_type *bus, char *buf) |
837cd105 HF |
1547 | { |
1548 | int rc; | |
1549 | unsigned int apqns, n; | |
1550 | ||
1551 | ap_calc_bound_apqns(&apqns, &n); | |
1552 | if (atomic64_read(&ap_scan_bus_count) >= 1 && n == apqns) | |
964d581d | 1553 | rc = sysfs_emit(buf, "%u/%u (complete)\n", n, apqns); |
837cd105 | 1554 | else |
964d581d | 1555 | rc = sysfs_emit(buf, "%u/%u\n", n, apqns); |
837cd105 HF |
1556 | |
1557 | return rc; | |
1558 | } | |
1559 | ||
1560 | static BUS_ATTR_RO(bindings); | |
1561 | ||
10de638d | 1562 | static ssize_t features_show(const struct bus_type *bus, char *buf) |
d7b1813a HF |
1563 | { |
1564 | int n = 0; | |
1565 | ||
1566 | if (!ap_qci_info) /* QCI not supported */ | |
1567 | return sysfs_emit(buf, "-\n"); | |
1568 | ||
1569 | if (ap_qci_info->apsc) | |
1570 | n += sysfs_emit_at(buf, n, "APSC "); | |
1571 | if (ap_qci_info->apxa) | |
1572 | n += sysfs_emit_at(buf, n, "APXA "); | |
1573 | if (ap_qci_info->qact) | |
1574 | n += sysfs_emit_at(buf, n, "QACT "); | |
1575 | if (ap_qci_info->rc8a) | |
1576 | n += sysfs_emit_at(buf, n, "RC8A "); | |
1577 | if (ap_qci_info->apsb) | |
1578 | n += sysfs_emit_at(buf, n, "APSB "); | |
1579 | ||
1580 | sysfs_emit_at(buf, n == 0 ? 0 : n - 1, "\n"); | |
1581 | ||
1582 | return n; | |
1583 | } | |
1584 | ||
1585 | static BUS_ATTR_RO(features); | |
1586 | ||
8cb4c20f JW |
1587 | static struct attribute *ap_bus_attrs[] = { |
1588 | &bus_attr_ap_domain.attr, | |
1589 | &bus_attr_ap_control_domain_mask.attr, | |
1590 | &bus_attr_ap_usage_domain_mask.attr, | |
1591 | &bus_attr_ap_adapter_mask.attr, | |
1592 | &bus_attr_config_time.attr, | |
1593 | &bus_attr_poll_thread.attr, | |
1594 | &bus_attr_ap_interrupts.attr, | |
1595 | &bus_attr_poll_timeout.attr, | |
1596 | &bus_attr_ap_max_domain_id.attr, | |
1597 | &bus_attr_ap_max_adapter_id.attr, | |
1598 | &bus_attr_apmask.attr, | |
1599 | &bus_attr_aqmask.attr, | |
1600 | &bus_attr_scans.attr, | |
1601 | &bus_attr_bindings.attr, | |
d7b1813a | 1602 | &bus_attr_features.attr, |
fe137230 | 1603 | NULL, |
1534c382 | 1604 | }; |
8cb4c20f JW |
1605 | ATTRIBUTE_GROUPS(ap_bus); |
1606 | ||
5b431787 | 1607 | static const struct bus_type ap_bus_type = { |
8cb4c20f JW |
1608 | .name = "ap", |
1609 | .bus_groups = ap_bus_groups, | |
1610 | .match = &ap_bus_match, | |
1611 | .uevent = &ap_uevent, | |
95c09f03 JW |
1612 | .probe = ap_device_probe, |
1613 | .remove = ap_device_remove, | |
8cb4c20f | 1614 | }; |
1534c382 MS |
1615 | |
1616 | /** | |
1c472d46 HP |
1617 | * ap_select_domain(): Select an AP domain if possible and we haven't |
1618 | * already done so before. | |
1534c382 | 1619 | */ |
1c472d46 | 1620 | static void ap_select_domain(void) |
1534c382 | 1621 | { |
6acbe21f | 1622 | struct ap_queue_status status; |
c8337c47 | 1623 | int card, dom; |
1534c382 | 1624 | |
1749a81d | 1625 | /* |
c8337c47 HF |
1626 | * Choose the default domain. Either the one specified with |
1627 | * the "domain=" parameter or the first domain with at least | |
1628 | * one valid APQN. | |
1534c382 | 1629 | */ |
fc1d3f02 IT |
1630 | spin_lock_bh(&ap_domain_lock); |
1631 | if (ap_domain_index >= 0) { | |
1534c382 | 1632 | /* Domain has already been selected. */ |
c8337c47 | 1633 | goto out; |
fc1d3f02 | 1634 | } |
c8337c47 HF |
1635 | for (dom = 0; dom <= ap_max_domain_id; dom++) { |
1636 | if (!ap_test_config_usage_domain(dom) || | |
1637 | !test_bit_inv(dom, ap_perms.aqm)) | |
75014550 | 1638 | continue; |
c8337c47 HF |
1639 | for (card = 0; card <= ap_max_adapter_id; card++) { |
1640 | if (!ap_test_config_card_id(card) || | |
1641 | !test_bit_inv(card, ap_perms.apm)) | |
75014550 | 1642 | continue; |
c8337c47 | 1643 | status = ap_test_queue(AP_MKQID(card, dom), |
e7fc5146 TK |
1644 | ap_apft_available(), |
1645 | NULL); | |
c8337c47 HF |
1646 | if (status.response_code == AP_RESPONSE_NORMAL) |
1647 | break; | |
1534c382 | 1648 | } |
c8337c47 HF |
1649 | if (card <= ap_max_adapter_id) |
1650 | break; | |
1534c382 | 1651 | } |
c8337c47 HF |
1652 | if (dom <= ap_max_domain_id) { |
1653 | ap_domain_index = dom; | |
2ea2a609 HF |
1654 | AP_DBF_INFO("%s new default domain is %d\n", |
1655 | __func__, ap_domain_index); | |
1534c382 | 1656 | } |
c8337c47 | 1657 | out: |
fc1d3f02 | 1658 | spin_unlock_bh(&ap_domain_lock); |
1534c382 MS |
1659 | } |
1660 | ||
9a564108 HF |
1661 | /* |
1662 | * This function checks the type and returns either 0 for not | |
1663 | * supported or the highest compatible type value (which may | |
1664 | * include the input type value). | |
1665 | */ | |
1666 | static int ap_get_compatible_type(ap_qid_t qid, int rawtype, unsigned int func) | |
1667 | { | |
1668 | int comp_type = 0; | |
1669 | ||
5ac8c724 HF |
1670 | /* < CEX4 is not supported */ |
1671 | if (rawtype < AP_DEVICE_TYPE_CEX4) { | |
3f74eb5f HF |
1672 | AP_DBF_WARN("%s queue=%02x.%04x unsupported type %d\n", |
1673 | __func__, AP_QID_CARD(qid), | |
1674 | AP_QID_QUEUE(qid), rawtype); | |
9a564108 | 1675 | return 0; |
2ea2a609 | 1676 | } |
985214af HF |
1677 | /* up to CEX8 known and fully supported */ |
1678 | if (rawtype <= AP_DEVICE_TYPE_CEX8) | |
9a564108 HF |
1679 | return rawtype; |
1680 | /* | |
985214af | 1681 | * unknown new type > CEX8, check for compatibility |
9a564108 | 1682 | * to the highest known and supported type which is |
985214af | 1683 | * currently CEX8 with the help of the QACT function. |
9a564108 HF |
1684 | */ |
1685 | if (ap_qact_available()) { | |
1686 | struct ap_queue_status status; | |
56c5c683 | 1687 | union ap_qact_ap_info apinfo = {0}; |
9a564108 HF |
1688 | |
1689 | apinfo.mode = (func >> 26) & 0x07; | |
985214af | 1690 | apinfo.cat = AP_DEVICE_TYPE_CEX8; |
9a564108 | 1691 | status = ap_qact(qid, 0, &apinfo); |
2004b57c | 1692 | if (status.response_code == AP_RESPONSE_NORMAL && |
5ac8c724 | 1693 | apinfo.cat >= AP_DEVICE_TYPE_CEX4 && |
2004b57c | 1694 | apinfo.cat <= AP_DEVICE_TYPE_CEX8) |
9a564108 HF |
1695 | comp_type = apinfo.cat; |
1696 | } | |
1697 | if (!comp_type) | |
3f74eb5f HF |
1698 | AP_DBF_WARN("%s queue=%02x.%04x unable to map type %d\n", |
1699 | __func__, AP_QID_CARD(qid), | |
1700 | AP_QID_QUEUE(qid), rawtype); | |
9a564108 | 1701 | else if (comp_type != rawtype) |
3f74eb5f HF |
1702 | AP_DBF_INFO("%s queue=%02x.%04x map type %d to %d\n", |
1703 | __func__, AP_QID_CARD(qid), AP_QID_QUEUE(qid), | |
2ea2a609 | 1704 | rawtype, comp_type); |
9a564108 HF |
1705 | return comp_type; |
1706 | } | |
1707 | ||
e28d2af4 | 1708 | /* |
a7b1868a | 1709 | * Helper function to be used with bus_find_dev |
e28d2af4 | 1710 | * matches for the card device with the given id |
1534c382 | 1711 | */ |
418e3ea1 | 1712 | static int __match_card_device_with_id(struct device *dev, const void *data) |
1534c382 | 1713 | { |
2004b57c | 1714 | return is_card_dev(dev) && to_ap_card(dev)->id == (int)(long)(void *)data; |
1534c382 MS |
1715 | } |
1716 | ||
a7b1868a HF |
1717 | /* |
1718 | * Helper function to be used with bus_find_dev | |
e28d2af4 IT |
1719 | * matches for the queue device with a given qid |
1720 | */ | |
418e3ea1 | 1721 | static int __match_queue_device_with_qid(struct device *dev, const void *data) |
e28d2af4 | 1722 | { |
2004b57c | 1723 | return is_queue_dev(dev) && to_ap_queue(dev)->qid == (int)(long)data; |
e28d2af4 IT |
1724 | } |
1725 | ||
b1af7528 HF |
1726 | /* |
1727 | * Helper function to be used with bus_find_dev | |
1728 | * matches any queue device with given queue id | |
1729 | */ | |
418e3ea1 | 1730 | static int __match_queue_device_with_queue_id(struct device *dev, const void *data) |
b1af7528 | 1731 | { |
2004b57c HF |
1732 | return is_queue_dev(dev) && |
1733 | AP_QID_QUEUE(to_ap_queue(dev)->qid) == (int)(long)data; | |
b1af7528 HF |
1734 | } |
1735 | ||
28391585 TK |
1736 | /* Helper function for notify_config_changed */ |
1737 | static int __drv_notify_config_changed(struct device_driver *drv, void *data) | |
1738 | { | |
1739 | struct ap_driver *ap_drv = to_ap_drv(drv); | |
1740 | ||
1741 | if (try_module_get(drv->owner)) { | |
1742 | if (ap_drv->on_config_changed) | |
1743 | ap_drv->on_config_changed(ap_qci_info, ap_qci_info_old); | |
1744 | module_put(drv->owner); | |
1745 | } | |
1746 | ||
1747 | return 0; | |
1748 | } | |
1749 | ||
1750 | /* Notify all drivers about an qci config change */ | |
1751 | static inline void notify_config_changed(void) | |
1752 | { | |
1753 | bus_for_each_drv(&ap_bus_type, NULL, NULL, | |
1754 | __drv_notify_config_changed); | |
1755 | } | |
1756 | ||
1757 | /* Helper function for notify_scan_complete */ | |
1758 | static int __drv_notify_scan_complete(struct device_driver *drv, void *data) | |
1759 | { | |
1760 | struct ap_driver *ap_drv = to_ap_drv(drv); | |
1761 | ||
1762 | if (try_module_get(drv->owner)) { | |
1763 | if (ap_drv->on_scan_complete) | |
1764 | ap_drv->on_scan_complete(ap_qci_info, | |
1765 | ap_qci_info_old); | |
1766 | module_put(drv->owner); | |
1767 | } | |
1768 | ||
1769 | return 0; | |
1770 | } | |
1771 | ||
1772 | /* Notify all drivers about bus scan complete */ | |
1773 | static inline void notify_scan_complete(void) | |
1774 | { | |
1775 | bus_for_each_drv(&ap_bus_type, NULL, NULL, | |
1776 | __drv_notify_scan_complete); | |
1777 | } | |
1778 | ||
a7b1868a HF |
1779 | /* |
1780 | * Helper function for ap_scan_bus(). | |
4f2fcccd HF |
1781 | * Remove card device and associated queue devices. |
1782 | */ | |
1783 | static inline void ap_scan_rm_card_dev_and_queue_devs(struct ap_card *ac) | |
1784 | { | |
1785 | bus_for_each_dev(&ap_bus_type, NULL, | |
2004b57c | 1786 | (void *)(long)ac->id, |
4f2fcccd HF |
1787 | __ap_queue_devices_with_id_unregister); |
1788 | device_unregister(&ac->ap_dev.device); | |
1789 | } | |
1790 | ||
1791 | /* | |
1792 | * Helper function for ap_scan_bus(). | |
1793 | * Does the scan bus job for all the domains within | |
1794 | * a valid adapter given by an ap_card ptr. | |
e28d2af4 | 1795 | */ |
4f2fcccd | 1796 | static inline void ap_scan_domains(struct ap_card *ac) |
1534c382 | 1797 | { |
d4c53ae8 | 1798 | struct ap_tapq_hwinfo hwinfo; |
a7e701db | 1799 | bool decfg, chkstop; |
a7b1868a | 1800 | struct ap_queue *aq; |
038c5bed | 1801 | struct device *dev; |
038c5bed | 1802 | ap_qid_t qid; |
d4c53ae8 | 1803 | int rc, dom; |
4f2fcccd HF |
1804 | |
1805 | /* | |
1806 | * Go through the configuration for the domains and compare them | |
1807 | * to the existing queue devices. Also take care of the config | |
1808 | * and error state for the queue devices. | |
1809 | */ | |
1810 | ||
1811 | for (dom = 0; dom <= ap_max_domain_id; dom++) { | |
1812 | qid = AP_MKQID(ac->id, dom); | |
1813 | dev = bus_find_device(&ap_bus_type, NULL, | |
2004b57c | 1814 | (void *)(long)qid, |
4f2fcccd HF |
1815 | __match_queue_device_with_qid); |
1816 | aq = dev ? to_ap_queue(dev) : NULL; | |
1817 | if (!ap_test_config_usage_domain(dom)) { | |
1818 | if (dev) { | |
3f74eb5f | 1819 | AP_DBF_INFO("%s(%d,%d) not in config anymore, rm queue dev\n", |
4f2fcccd HF |
1820 | __func__, ac->id, dom); |
1821 | device_unregister(dev); | |
4f2fcccd | 1822 | } |
038c5bed | 1823 | goto put_dev_and_continue; |
4f2fcccd HF |
1824 | } |
1825 | /* domain is valid, get info from this APQN */ | |
d4c53ae8 | 1826 | rc = ap_queue_info(qid, &hwinfo, &decfg, &chkstop); |
038c5bed HF |
1827 | switch (rc) { |
1828 | case -1: | |
1829 | if (dev) { | |
3f74eb5f HF |
1830 | AP_DBF_INFO("%s(%d,%d) queue_info() failed, rm queue dev\n", |
1831 | __func__, ac->id, dom); | |
4f2fcccd | 1832 | device_unregister(dev); |
4f2fcccd | 1833 | } |
038c5bed HF |
1834 | fallthrough; |
1835 | case 0: | |
1836 | goto put_dev_and_continue; | |
1837 | default: | |
1838 | break; | |
4f2fcccd HF |
1839 | } |
1840 | /* if no queue device exists, create a new one */ | |
1841 | if (!aq) { | |
1842 | aq = ap_queue_create(qid, ac->ap_dev.device_type); | |
1843 | if (!aq) { | |
1844 | AP_DBF_WARN("%s(%d,%d) ap_queue_create() failed\n", | |
1845 | __func__, ac->id, dom); | |
1846 | continue; | |
1847 | } | |
1848 | aq->card = ac; | |
1849 | aq->config = !decfg; | |
a7e701db | 1850 | aq->chkstop = chkstop; |
207022d3 | 1851 | aq->se_bstate = hwinfo.bs; |
4f2fcccd HF |
1852 | dev = &aq->ap_dev.device; |
1853 | dev->bus = &ap_bus_type; | |
1854 | dev->parent = &ac->ap_dev.device; | |
1855 | dev_set_name(dev, "%02x.%04x", ac->id, dom); | |
1856 | /* register queue device */ | |
1857 | rc = device_register(dev); | |
1858 | if (rc) { | |
1859 | AP_DBF_WARN("%s(%d,%d) device_register() failed\n", | |
1860 | __func__, ac->id, dom); | |
1861 | goto put_dev_and_continue; | |
1862 | } | |
29c2680f HF |
1863 | /* get it and thus adjust reference counter */ |
1864 | get_device(dev); | |
32d1d920 | 1865 | if (decfg) { |
3f74eb5f | 1866 | AP_DBF_INFO("%s(%d,%d) new (decfg) queue dev created\n", |
4f2fcccd | 1867 | __func__, ac->id, dom); |
32d1d920 | 1868 | } else if (chkstop) { |
a7e701db HF |
1869 | AP_DBF_INFO("%s(%d,%d) new (chkstop) queue dev created\n", |
1870 | __func__, ac->id, dom); | |
32d1d920 HF |
1871 | } else { |
1872 | /* nudge the queue's state machine */ | |
1873 | ap_queue_init_state(aq); | |
3f74eb5f | 1874 | AP_DBF_INFO("%s(%d,%d) new queue dev created\n", |
4f2fcccd | 1875 | __func__, ac->id, dom); |
32d1d920 | 1876 | } |
4f2fcccd HF |
1877 | goto put_dev_and_continue; |
1878 | } | |
a7e701db | 1879 | /* handle state changes on already existing queue device */ |
4f2fcccd | 1880 | spin_lock_bh(&aq->lock); |
207022d3 HF |
1881 | /* SE bind state */ |
1882 | aq->se_bstate = hwinfo.bs; | |
a7e701db HF |
1883 | /* checkstop state */ |
1884 | if (chkstop && !aq->chkstop) { | |
1885 | /* checkstop on */ | |
1886 | aq->chkstop = true; | |
1887 | if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { | |
1888 | aq->dev_state = AP_DEV_STATE_ERROR; | |
1889 | aq->last_err_rc = AP_RESPONSE_CHECKSTOPPED; | |
1890 | } | |
1891 | spin_unlock_bh(&aq->lock); | |
08b2c370 HF |
1892 | pr_debug("%s(%d,%d) queue dev checkstop on\n", |
1893 | __func__, ac->id, dom); | |
a7e701db HF |
1894 | /* 'receive' pending messages with -EAGAIN */ |
1895 | ap_flush_queue(aq); | |
1896 | goto put_dev_and_continue; | |
1897 | } else if (!chkstop && aq->chkstop) { | |
1898 | /* checkstop off */ | |
1899 | aq->chkstop = false; | |
32d1d920 HF |
1900 | if (aq->dev_state > AP_DEV_STATE_UNINITIATED) |
1901 | _ap_queue_init_state(aq); | |
a7e701db | 1902 | spin_unlock_bh(&aq->lock); |
08b2c370 HF |
1903 | pr_debug("%s(%d,%d) queue dev checkstop off\n", |
1904 | __func__, ac->id, dom); | |
a7e701db HF |
1905 | goto put_dev_and_continue; |
1906 | } | |
1907 | /* config state change */ | |
4f2fcccd HF |
1908 | if (decfg && aq->config) { |
1909 | /* config off this queue device */ | |
1910 | aq->config = false; | |
1911 | if (aq->dev_state > AP_DEV_STATE_UNINITIATED) { | |
1912 | aq->dev_state = AP_DEV_STATE_ERROR; | |
1913 | aq->last_err_rc = AP_RESPONSE_DECONFIGURED; | |
1914 | } | |
1915 | spin_unlock_bh(&aq->lock); | |
08b2c370 HF |
1916 | pr_debug("%s(%d,%d) queue dev config off\n", |
1917 | __func__, ac->id, dom); | |
df6f508c | 1918 | ap_send_config_uevent(&aq->ap_dev, aq->config); |
4f2fcccd HF |
1919 | /* 'receive' pending messages with -EAGAIN */ |
1920 | ap_flush_queue(aq); | |
1921 | goto put_dev_and_continue; | |
a7e701db | 1922 | } else if (!decfg && !aq->config) { |
4f2fcccd HF |
1923 | /* config on this queue device */ |
1924 | aq->config = true; | |
32d1d920 HF |
1925 | if (aq->dev_state > AP_DEV_STATE_UNINITIATED) |
1926 | _ap_queue_init_state(aq); | |
4f2fcccd | 1927 | spin_unlock_bh(&aq->lock); |
08b2c370 HF |
1928 | pr_debug("%s(%d,%d) queue dev config on\n", |
1929 | __func__, ac->id, dom); | |
df6f508c | 1930 | ap_send_config_uevent(&aq->ap_dev, aq->config); |
4f2fcccd HF |
1931 | goto put_dev_and_continue; |
1932 | } | |
1933 | /* handle other error states */ | |
1934 | if (!decfg && aq->dev_state == AP_DEV_STATE_ERROR) { | |
1935 | spin_unlock_bh(&aq->lock); | |
1936 | /* 'receive' pending messages with -EAGAIN */ | |
1937 | ap_flush_queue(aq); | |
1938 | /* re-init (with reset) the queue device */ | |
1939 | ap_queue_init_state(aq); | |
3f74eb5f | 1940 | AP_DBF_INFO("%s(%d,%d) queue dev reinit enforced\n", |
4f2fcccd HF |
1941 | __func__, ac->id, dom); |
1942 | goto put_dev_and_continue; | |
1943 | } | |
1944 | spin_unlock_bh(&aq->lock); | |
1945 | put_dev_and_continue: | |
1946 | put_device(dev); | |
1947 | } | |
1948 | } | |
1949 | ||
1950 | /* | |
1951 | * Helper function for ap_scan_bus(). | |
1952 | * Does the scan bus job for the given adapter id. | |
1953 | */ | |
1954 | static inline void ap_scan_adapter(int ap) | |
1955 | { | |
d4c53ae8 HF |
1956 | struct ap_tapq_hwinfo hwinfo; |
1957 | int rc, dom, comp_type; | |
a7e701db | 1958 | bool decfg, chkstop; |
4f2fcccd | 1959 | struct ap_card *ac; |
038c5bed | 1960 | struct device *dev; |
038c5bed | 1961 | ap_qid_t qid; |
a7b1868a | 1962 | |
4f2fcccd | 1963 | /* Is there currently a card device for this adapter ? */ |
a7b1868a | 1964 | dev = bus_find_device(&ap_bus_type, NULL, |
2004b57c | 1965 | (void *)(long)ap, |
a7b1868a HF |
1966 | __match_card_device_with_id); |
1967 | ac = dev ? to_ap_card(dev) : NULL; | |
4f2fcccd HF |
1968 | |
1969 | /* Adapter not in configuration ? */ | |
1970 | if (!ap_test_config_card_id(ap)) { | |
1971 | if (ac) { | |
3f74eb5f | 1972 | AP_DBF_INFO("%s(%d) ap not in config any more, rm card and queue devs\n", |
4f2fcccd HF |
1973 | __func__, ap); |
1974 | ap_scan_rm_card_dev_and_queue_devs(ac); | |
a7b1868a HF |
1975 | put_device(dev); |
1976 | } | |
1977 | return; | |
1978 | } | |
cccd85bf | 1979 | |
a7b1868a | 1980 | /* |
4f2fcccd HF |
1981 | * Adapter ap is valid in the current configuration. So do some checks: |
1982 | * If no card device exists, build one. If a card device exists, check | |
1983 | * for type and functions changed. For all this we need to find a valid | |
1984 | * APQN first. | |
a7b1868a | 1985 | */ |
4f2fcccd HF |
1986 | |
1987 | for (dom = 0; dom <= ap_max_domain_id; dom++) | |
1988 | if (ap_test_config_usage_domain(dom)) { | |
1989 | qid = AP_MKQID(ap, dom); | |
d4c53ae8 | 1990 | if (ap_queue_info(qid, &hwinfo, &decfg, &chkstop) > 0) |
a7b1868a HF |
1991 | break; |
1992 | } | |
4f2fcccd | 1993 | if (dom > ap_max_domain_id) { |
038c5bed | 1994 | /* Could not find one valid APQN for this adapter */ |
4f2fcccd | 1995 | if (ac) { |
3f74eb5f HF |
1996 | AP_DBF_INFO("%s(%d) no type info (no APQN found), rm card and queue devs\n", |
1997 | __func__, ap); | |
4f2fcccd HF |
1998 | ap_scan_rm_card_dev_and_queue_devs(ac); |
1999 | put_device(dev); | |
2000 | } else { | |
08b2c370 HF |
2001 | pr_debug("%s(%d) no type info (no APQN found), ignored\n", |
2002 | __func__, ap); | |
a7b1868a | 2003 | } |
4f2fcccd HF |
2004 | return; |
2005 | } | |
d4c53ae8 | 2006 | if (!hwinfo.at) { |
4f2fcccd HF |
2007 | /* No apdater type info available, an unusable adapter */ |
2008 | if (ac) { | |
3f74eb5f | 2009 | AP_DBF_INFO("%s(%d) no valid type (0) info, rm card and queue devs\n", |
4f2fcccd HF |
2010 | __func__, ap); |
2011 | ap_scan_rm_card_dev_and_queue_devs(ac); | |
a7b1868a | 2012 | put_device(dev); |
4f2fcccd | 2013 | } else { |
08b2c370 HF |
2014 | pr_debug("%s(%d) no valid type (0) info, ignored\n", |
2015 | __func__, ap); | |
a7b1868a | 2016 | } |
4f2fcccd | 2017 | return; |
a7b1868a | 2018 | } |
d4c53ae8 | 2019 | hwinfo.value &= TAPQ_CARD_HWINFO_MASK; /* filter card specific hwinfo */ |
4f2fcccd HF |
2020 | if (ac) { |
2021 | /* Check APQN against existing card device for changes */ | |
d4c53ae8 | 2022 | if (ac->hwinfo.at != hwinfo.at) { |
3f74eb5f | 2023 | AP_DBF_INFO("%s(%d) hwtype %d changed, rm card and queue devs\n", |
d4c53ae8 | 2024 | __func__, ap, hwinfo.at); |
4f2fcccd HF |
2025 | ap_scan_rm_card_dev_and_queue_devs(ac); |
2026 | put_device(dev); | |
2027 | ac = NULL; | |
d4c53ae8 | 2028 | } else if (ac->hwinfo.fac != hwinfo.fac) { |
3f74eb5f | 2029 | AP_DBF_INFO("%s(%d) functions 0x%08x changed, rm card and queue devs\n", |
d4c53ae8 | 2030 | __func__, ap, hwinfo.fac); |
4f2fcccd HF |
2031 | ap_scan_rm_card_dev_and_queue_devs(ac); |
2032 | put_device(dev); | |
2033 | ac = NULL; | |
2034 | } else { | |
a7e701db HF |
2035 | /* handle checkstop state change */ |
2036 | if (chkstop && !ac->chkstop) { | |
2037 | /* checkstop on */ | |
2038 | ac->chkstop = true; | |
2039 | AP_DBF_INFO("%s(%d) card dev checkstop on\n", | |
2040 | __func__, ap); | |
2041 | } else if (!chkstop && ac->chkstop) { | |
2042 | /* checkstop off */ | |
2043 | ac->chkstop = false; | |
2044 | AP_DBF_INFO("%s(%d) card dev checkstop off\n", | |
2045 | __func__, ap); | |
2046 | } | |
2047 | /* handle config state change */ | |
4f2fcccd HF |
2048 | if (decfg && ac->config) { |
2049 | ac->config = false; | |
3f74eb5f | 2050 | AP_DBF_INFO("%s(%d) card dev config off\n", |
4f2fcccd | 2051 | __func__, ap); |
df6f508c | 2052 | ap_send_config_uevent(&ac->ap_dev, ac->config); |
a7e701db | 2053 | } else if (!decfg && !ac->config) { |
4f2fcccd | 2054 | ac->config = true; |
3f74eb5f | 2055 | AP_DBF_INFO("%s(%d) card dev config on\n", |
4f2fcccd | 2056 | __func__, ap); |
df6f508c | 2057 | ap_send_config_uevent(&ac->ap_dev, ac->config); |
b1af7528 | 2058 | } |
a7b1868a | 2059 | } |
4f2fcccd HF |
2060 | } |
2061 | ||
2062 | if (!ac) { | |
2063 | /* Build a new card device */ | |
d4c53ae8 | 2064 | comp_type = ap_get_compatible_type(qid, hwinfo.at, hwinfo.fac); |
4f2fcccd HF |
2065 | if (!comp_type) { |
2066 | AP_DBF_WARN("%s(%d) type %d, can't get compatibility type\n", | |
d4c53ae8 | 2067 | __func__, ap, hwinfo.at); |
4f2fcccd HF |
2068 | return; |
2069 | } | |
d4c53ae8 | 2070 | ac = ap_card_create(ap, hwinfo, comp_type); |
a7b1868a | 2071 | if (!ac) { |
4f2fcccd HF |
2072 | AP_DBF_WARN("%s(%d) ap_card_create() failed\n", |
2073 | __func__, ap); | |
2074 | return; | |
a7b1868a | 2075 | } |
4f2fcccd | 2076 | ac->config = !decfg; |
a7e701db | 2077 | ac->chkstop = chkstop; |
4f2fcccd HF |
2078 | dev = &ac->ap_dev.device; |
2079 | dev->bus = &ap_bus_type; | |
2080 | dev->parent = ap_root_device; | |
2081 | dev_set_name(dev, "card%02x", ap); | |
bd39654a HF |
2082 | /* maybe enlarge ap_max_msg_size to support this card */ |
2083 | if (ac->maxmsgsize > atomic_read(&ap_max_msg_size)) { | |
2084 | atomic_set(&ap_max_msg_size, ac->maxmsgsize); | |
2085 | AP_DBF_INFO("%s(%d) ap_max_msg_size update to %d byte\n", | |
3f74eb5f HF |
2086 | __func__, ap, |
2087 | atomic_read(&ap_max_msg_size)); | |
bd39654a | 2088 | } |
4f2fcccd HF |
2089 | /* Register the new card device with AP bus */ |
2090 | rc = device_register(dev); | |
a7b1868a | 2091 | if (rc) { |
4f2fcccd HF |
2092 | AP_DBF_WARN("%s(%d) device_register() failed\n", |
2093 | __func__, ap); | |
2094 | put_device(dev); | |
2095 | return; | |
3f3007af | 2096 | } |
4f2fcccd HF |
2097 | /* get it and thus adjust reference counter */ |
2098 | get_device(dev); | |
2099 | if (decfg) | |
3f74eb5f | 2100 | AP_DBF_INFO("%s(%d) new (decfg) card dev type=%d func=0x%08x created\n", |
d4c53ae8 | 2101 | __func__, ap, hwinfo.at, hwinfo.fac); |
a7e701db HF |
2102 | else if (chkstop) |
2103 | AP_DBF_INFO("%s(%d) new (chkstop) card dev type=%d func=0x%08x created\n", | |
d4c53ae8 | 2104 | __func__, ap, hwinfo.at, hwinfo.fac); |
4f2fcccd | 2105 | else |
3f74eb5f | 2106 | AP_DBF_INFO("%s(%d) new card dev type=%d func=0x%08x created\n", |
d4c53ae8 | 2107 | __func__, ap, hwinfo.at, hwinfo.fac); |
4f2fcccd HF |
2108 | } |
2109 | ||
2110 | /* Verify the domains and the queue devices for this card */ | |
2111 | ap_scan_domains(ac); | |
a7b1868a | 2112 | |
4f2fcccd HF |
2113 | /* release the card device */ |
2114 | put_device(&ac->ap_dev.device); | |
a7b1868a | 2115 | } |
ac994e80 | 2116 | |
28391585 TK |
2117 | /** |
2118 | * ap_get_configuration - get the host AP configuration | |
2119 | * | |
2120 | * Stores the host AP configuration information returned from the previous call | |
2121 | * to Query Configuration Information (QCI), then retrieves and stores the | |
2122 | * current AP configuration returned from QCI. | |
2123 | * | |
2124 | * Return: true if the host AP configuration changed between calls to QCI; | |
2125 | * otherwise, return false. | |
2126 | */ | |
2127 | static bool ap_get_configuration(void) | |
2128 | { | |
0fef40be HF |
2129 | if (!ap_qci_info) /* QCI not supported */ |
2130 | return false; | |
2131 | ||
28391585 TK |
2132 | memcpy(ap_qci_info_old, ap_qci_info, sizeof(*ap_qci_info)); |
2133 | ap_fetch_qci_info(ap_qci_info); | |
2134 | ||
2135 | return memcmp(ap_qci_info, ap_qci_info_old, | |
2136 | sizeof(struct ap_config_info)) != 0; | |
2137 | } | |
2138 | ||
a7b1868a HF |
2139 | /** |
2140 | * ap_scan_bus(): Scan the AP bus for new devices | |
2141 | * Runs periodically, workqueue timer (ap_config_time) | |
948e5055 | 2142 | * @unused: Unused pointer. |
a7b1868a HF |
2143 | */ |
2144 | static void ap_scan_bus(struct work_struct *unused) | |
2145 | { | |
28391585 | 2146 | int ap, config_changed = 0; |
a7b1868a | 2147 | |
08b2c370 HF |
2148 | pr_debug(">%s\n", __func__); |
2149 | ||
28391585 TK |
2150 | /* config change notify */ |
2151 | config_changed = ap_get_configuration(); | |
2152 | if (config_changed) | |
2153 | notify_config_changed(); | |
a7b1868a HF |
2154 | ap_select_domain(); |
2155 | ||
2156 | /* loop over all possible adapters */ | |
4f2fcccd HF |
2157 | for (ap = 0; ap <= ap_max_adapter_id; ap++) |
2158 | ap_scan_adapter(ap); | |
a7b1868a | 2159 | |
28391585 TK |
2160 | /* scan complete notify */ |
2161 | if (config_changed) | |
2162 | notify_scan_complete(); | |
2163 | ||
a7b1868a HF |
2164 | /* check if there is at least one queue available with default domain */ |
2165 | if (ap_domain_index >= 0) { | |
2166 | struct device *dev = | |
2167 | bus_find_device(&ap_bus_type, NULL, | |
2004b57c | 2168 | (void *)(long)ap_domain_index, |
b1af7528 | 2169 | __match_queue_device_with_queue_id); |
a7b1868a HF |
2170 | if (dev) |
2171 | put_device(dev); | |
2172 | else | |
3f74eb5f HF |
2173 | AP_DBF_INFO("%s no queue device with default domain %d available\n", |
2174 | __func__, ap_domain_index); | |
a7b1868a | 2175 | } |
ac994e80 | 2176 | |
837cd105 | 2177 | if (atomic64_inc_return(&ap_scan_bus_count) == 1) { |
08b2c370 | 2178 | pr_debug("%s init scan complete\n", __func__); |
837cd105 HF |
2179 | ap_send_init_scan_done_uevent(); |
2180 | ap_check_bindings_complete(); | |
2181 | } | |
2182 | ||
fcd0d1f6 | 2183 | mod_timer(&ap_config_timer, jiffies + ap_config_time * HZ); |
08b2c370 HF |
2184 | |
2185 | pr_debug("<%s\n", __func__); | |
1534c382 MS |
2186 | } |
2187 | ||
cefbeb5d | 2188 | static void ap_config_timeout(struct timer_list *unused) |
1534c382 | 2189 | { |
8139b89d | 2190 | queue_work(system_long_wq, &ap_scan_work); |
1534c382 | 2191 | } |
13e742ba | 2192 | |
efda7ade | 2193 | static int __init ap_debug_init(void) |
cccd85bf | 2194 | { |
3f74eb5f | 2195 | ap_dbf_info = debug_register("ap", 2, 1, |
88e4c0da | 2196 | AP_DBF_MAX_SPRINTF_ARGS * sizeof(long)); |
cccd85bf HF |
2197 | debug_register_view(ap_dbf_info, &debug_sprintf_view); |
2198 | debug_set_level(ap_dbf_info, DBF_ERR); | |
2199 | ||
2200 | return 0; | |
2201 | } | |
2202 | ||
7e0bdbe5 HF |
2203 | static void __init ap_perms_init(void) |
2204 | { | |
2004b57c | 2205 | /* all resources usable if no kernel parameter string given */ |
00fab235 | 2206 | memset(&ap_perms.ioctlm, 0xFF, sizeof(ap_perms.ioctlm)); |
7e0bdbe5 HF |
2207 | memset(&ap_perms.apm, 0xFF, sizeof(ap_perms.apm)); |
2208 | memset(&ap_perms.aqm, 0xFF, sizeof(ap_perms.aqm)); | |
2209 | ||
3d8f60d3 | 2210 | /* apm kernel parameter string */ |
7e0bdbe5 | 2211 | if (apm_str) { |
3d8f60d3 | 2212 | memset(&ap_perms.apm, 0, sizeof(ap_perms.apm)); |
00fab235 HF |
2213 | ap_parse_mask_str(apm_str, ap_perms.apm, AP_DEVICES, |
2214 | &ap_perms_mutex); | |
7e0bdbe5 | 2215 | } |
7e0bdbe5 | 2216 | |
3d8f60d3 HF |
2217 | /* aqm kernel parameter string */ |
2218 | if (aqm_str) { | |
2219 | memset(&ap_perms.aqm, 0, sizeof(ap_perms.aqm)); | |
00fab235 HF |
2220 | ap_parse_mask_str(aqm_str, ap_perms.aqm, AP_DOMAINS, |
2221 | &ap_perms_mutex); | |
7e0bdbe5 HF |
2222 | } |
2223 | } | |
2224 | ||
1534c382 | 2225 | /** |
1749a81d FB |
2226 | * ap_module_init(): The module initialization code. |
2227 | * | |
2228 | * Initializes the module. | |
1534c382 | 2229 | */ |
efda7ade | 2230 | static int __init ap_module_init(void) |
1534c382 | 2231 | { |
8cb4c20f | 2232 | int rc; |
1534c382 | 2233 | |
cccd85bf HF |
2234 | rc = ap_debug_init(); |
2235 | if (rc) | |
2236 | return rc; | |
2237 | ||
2395103b | 2238 | if (!ap_instructions_available()) { |
889875a1 MS |
2239 | pr_warn("The hardware system does not support AP instructions\n"); |
2240 | return -ENODEV; | |
2241 | } | |
2242 | ||
bc4b295e HF |
2243 | /* init ap_queue hashtable */ |
2244 | hash_init(ap_queues); | |
2245 | ||
00fab235 | 2246 | /* set up the AP permissions (ioctls, ap and aq masks) */ |
7e0bdbe5 HF |
2247 | ap_perms_init(); |
2248 | ||
889875a1 | 2249 | /* Get AP configuration data if available */ |
c8337c47 HF |
2250 | ap_init_qci_info(); |
2251 | ||
2252 | /* check default domain setting */ | |
2253 | if (ap_domain_index < -1 || ap_domain_index > ap_max_domain_id || | |
7e0bdbe5 HF |
2254 | (ap_domain_index >= 0 && |
2255 | !test_bit_inv(ap_domain_index, ap_perms.aqm))) { | |
889875a1 MS |
2256 | pr_warn("%d is not a valid cryptographic domain\n", |
2257 | ap_domain_index); | |
ac994e80 | 2258 | ap_domain_index = -1; |
1534c382 | 2259 | } |
5314af69 | 2260 | |
c8337c47 | 2261 | /* enable interrupts if available */ |
d0982725 | 2262 | if (ap_interrupts_available() && ap_useirq) { |
f4eae94f | 2263 | rc = register_adapter_interrupt(&ap_airq); |
cabebb69 | 2264 | ap_irq_flag = (rc == 0); |
cb17a636 FB |
2265 | } |
2266 | ||
1534c382 MS |
2267 | /* Create /sys/bus/ap. */ |
2268 | rc = bus_register(&ap_bus_type); | |
2269 | if (rc) | |
2270 | goto out; | |
1534c382 MS |
2271 | |
2272 | /* Create /sys/devices/ap. */ | |
035da16f | 2273 | ap_root_device = root_device_register("ap"); |
9c705206 | 2274 | rc = PTR_ERR_OR_ZERO(ap_root_device); |
1534c382 MS |
2275 | if (rc) |
2276 | goto out_bus; | |
837cd105 | 2277 | ap_root_device->bus = &ap_bus_type; |
1534c382 | 2278 | |
1749a81d | 2279 | /* Setup the AP bus rescan timer. */ |
cefbeb5d | 2280 | timer_setup(&ap_config_timer, ap_config_timeout, 0); |
1534c382 | 2281 | |
3f3007af | 2282 | /* |
cada938a | 2283 | * Setup the high resolution poll timer. |
fe137230 FB |
2284 | * If we are running under z/VM adjust polling to z/VM polling rate. |
2285 | */ | |
2286 | if (MACHINE_IS_VM) | |
263c8454 | 2287 | poll_high_timeout = 1500000; |
fe137230 FB |
2288 | hrtimer_init(&ap_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
2289 | ap_poll_timer.function = ap_poll_timeout; | |
2290 | ||
1534c382 MS |
2291 | /* Start the low priority AP bus poll thread. */ |
2292 | if (ap_thread_flag) { | |
2293 | rc = ap_poll_thread_start(); | |
2294 | if (rc) | |
2295 | goto out_work; | |
2296 | } | |
2297 | ||
8139b89d | 2298 | queue_work(system_long_wq, &ap_scan_work); |
3f3007af | 2299 | |
1534c382 MS |
2300 | return 0; |
2301 | ||
2302 | out_work: | |
fe137230 | 2303 | hrtimer_cancel(&ap_poll_timer); |
035da16f | 2304 | root_device_unregister(ap_root_device); |
1534c382 | 2305 | out_bus: |
1534c382 MS |
2306 | bus_unregister(&ap_bus_type); |
2307 | out: | |
cabebb69 | 2308 | if (ap_irq_flag) |
f4eae94f | 2309 | unregister_adapter_interrupt(&ap_airq); |
c8337c47 | 2310 | kfree(ap_qci_info); |
1534c382 MS |
2311 | return rc; |
2312 | } | |
50a0d46c | 2313 | device_initcall(ap_module_init); |