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