Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
62b74942 | 3 | * core function to access sclp interface |
1da177e4 | 4 | * |
62b74942 MH |
5 | * Copyright IBM Corp. 1999, 2009 |
6 | * | |
7 | * Author(s): Martin Peschke <mpeschke@de.ibm.com> | |
8 | * Martin Schwidefsky <schwidefsky@de.ibm.com> | |
1da177e4 LT |
9 | */ |
10 | ||
052ff461 | 11 | #include <linux/kernel_stat.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/err.h> | |
f39650de | 14 | #include <linux/panic_notifier.h> |
1da177e4 LT |
15 | #include <linux/spinlock.h> |
16 | #include <linux/interrupt.h> | |
17 | #include <linux/timer.h> | |
18 | #include <linux/reboot.h> | |
19 | #include <linux/jiffies.h> | |
b3d00c3b | 20 | #include <linux/init.h> |
62b74942 | 21 | #include <linux/platform_device.h> |
052ff461 HC |
22 | #include <asm/types.h> |
23 | #include <asm/irq.h> | |
70aa5d39 | 24 | #include <asm/debug.h> |
1da177e4 LT |
25 | |
26 | #include "sclp.h" | |
27 | ||
28 | #define SCLP_HEADER "sclp: " | |
29 | ||
70aa5d39 | 30 | struct sclp_trace_entry { |
ff8a58b0 | 31 | char id[4] __nonstring; |
70aa5d39 PO |
32 | u32 a; |
33 | u64 b; | |
34 | }; | |
35 | ||
36 | #define SCLP_TRACE_ENTRY_SIZE sizeof(struct sclp_trace_entry) | |
37 | #define SCLP_TRACE_MAX_SIZE 128 | |
38 | #define SCLP_TRACE_EVENT_MAX_SIZE 64 | |
39 | ||
40 | /* Debug trace area intended for all entries in abbreviated form. */ | |
41 | DEFINE_STATIC_DEBUG_INFO(sclp_debug, "sclp", 8, 1, SCLP_TRACE_ENTRY_SIZE, | |
42 | &debug_hex_ascii_view); | |
43 | ||
44 | /* Error trace area intended for full entries relating to failed requests. */ | |
45 | DEFINE_STATIC_DEBUG_INFO(sclp_debug_err, "sclp_err", 4, 1, | |
46 | SCLP_TRACE_ENTRY_SIZE, &debug_hex_ascii_view); | |
47 | ||
1da177e4 LT |
48 | /* Lock to protect internal data consistency. */ |
49 | static DEFINE_SPINLOCK(sclp_lock); | |
50 | ||
d082d3ce | 51 | /* Mask of events that we can send to the sclp interface. */ |
1da177e4 LT |
52 | static sccb_mask_t sclp_receive_mask; |
53 | ||
d082d3ce | 54 | /* Mask of events that we can receive from the sclp interface. */ |
1da177e4 LT |
55 | static sccb_mask_t sclp_send_mask; |
56 | ||
57 | /* List of registered event listeners and senders. */ | |
8bc00c04 | 58 | static LIST_HEAD(sclp_reg_list); |
1da177e4 LT |
59 | |
60 | /* List of queued requests. */ | |
8bc00c04 | 61 | static LIST_HEAD(sclp_req_queue); |
1da177e4 LT |
62 | |
63 | /* Data for read and and init requests. */ | |
64 | static struct sclp_req sclp_read_req; | |
65 | static struct sclp_req sclp_init_req; | |
087c4d74 GS |
66 | static void *sclp_read_sccb; |
67 | static struct init_sccb *sclp_init_sccb; | |
1da177e4 | 68 | |
25b41a7b MS |
69 | /* Number of console pages to allocate, used by sclp_con.c and sclp_vt220.c */ |
70 | int sclp_console_pages = SCLP_CONSOLE_PAGES; | |
71 | /* Flag to indicate if buffer pages are dropped on buffer full condition */ | |
00fd2cb0 | 72 | int sclp_console_drop = 1; |
25b41a7b MS |
73 | /* Number of times the console dropped buffer pages */ |
74 | unsigned long sclp_console_full; | |
75 | ||
70aa5d39 PO |
76 | /* The currently active SCLP command word. */ |
77 | static sclp_cmdw_t active_cmd; | |
78 | ||
79 | static inline void sclp_trace(int prio, char *id, u32 a, u64 b, bool err) | |
80 | { | |
81 | struct sclp_trace_entry e; | |
82 | ||
83 | memset(&e, 0, sizeof(e)); | |
84 | strncpy(e.id, id, sizeof(e.id)); | |
85 | e.a = a; | |
86 | e.b = b; | |
87 | debug_event(&sclp_debug, prio, &e, sizeof(e)); | |
88 | if (err) | |
89 | debug_event(&sclp_debug_err, 0, &e, sizeof(e)); | |
90 | } | |
91 | ||
92 | static inline int no_zeroes_len(void *data, int len) | |
93 | { | |
94 | char *d = data; | |
95 | ||
96 | /* Minimize trace area usage by not tracing trailing zeroes. */ | |
97 | while (len > SCLP_TRACE_ENTRY_SIZE && d[len - 1] == 0) | |
98 | len--; | |
99 | ||
100 | return len; | |
101 | } | |
102 | ||
103 | static inline void sclp_trace_bin(int prio, void *d, int len, int errlen) | |
104 | { | |
105 | debug_event(&sclp_debug, prio, d, no_zeroes_len(d, len)); | |
106 | if (errlen) | |
107 | debug_event(&sclp_debug_err, 0, d, no_zeroes_len(d, errlen)); | |
108 | } | |
109 | ||
110 | static inline int abbrev_len(sclp_cmdw_t cmd, struct sccb_header *sccb) | |
111 | { | |
112 | struct evbuf_header *evbuf = (struct evbuf_header *)(sccb + 1); | |
113 | int len = sccb->length, limit = SCLP_TRACE_MAX_SIZE; | |
114 | ||
115 | /* Full SCCB tracing if debug level is set to max. */ | |
116 | if (sclp_debug.level == DEBUG_MAX_LEVEL) | |
117 | return len; | |
118 | ||
119 | /* Minimal tracing for console writes. */ | |
120 | if (cmd == SCLP_CMDW_WRITE_EVENT_DATA && | |
121 | (evbuf->type == EVTYP_MSG || evbuf->type == EVTYP_VT220MSG)) | |
122 | limit = SCLP_TRACE_ENTRY_SIZE; | |
123 | ||
124 | return min(len, limit); | |
125 | } | |
126 | ||
127 | static inline void sclp_trace_sccb(int prio, char *id, u32 a, u64 b, | |
128 | sclp_cmdw_t cmd, struct sccb_header *sccb, | |
129 | bool err) | |
130 | { | |
131 | sclp_trace(prio, id, a, b, err); | |
132 | if (sccb) { | |
133 | sclp_trace_bin(prio + 1, sccb, abbrev_len(cmd, sccb), | |
134 | err ? sccb->length : 0); | |
135 | } | |
136 | } | |
137 | ||
138 | static inline void sclp_trace_evbuf(int prio, char *id, u32 a, u64 b, | |
139 | struct evbuf_header *evbuf, bool err) | |
140 | { | |
141 | sclp_trace(prio, id, a, b, err); | |
142 | sclp_trace_bin(prio + 1, evbuf, | |
143 | min((int)evbuf->length, (int)SCLP_TRACE_EVENT_MAX_SIZE), | |
144 | err ? evbuf->length : 0); | |
145 | } | |
146 | ||
147 | static inline void sclp_trace_req(int prio, char *id, struct sclp_req *req, | |
148 | bool err) | |
149 | { | |
150 | struct sccb_header *sccb = req->sccb; | |
151 | union { | |
152 | struct { | |
153 | u16 status; | |
154 | u16 response; | |
155 | u16 timeout; | |
156 | u16 start_count; | |
157 | }; | |
158 | u64 b; | |
159 | } summary; | |
160 | ||
161 | summary.status = req->status; | |
162 | summary.response = sccb ? sccb->response_code : 0; | |
163 | summary.timeout = (u16)req->queue_timeout; | |
164 | summary.start_count = (u16)req->start_count; | |
165 | ||
ada1da31 | 166 | sclp_trace(prio, id, __pa(sccb), summary.b, err); |
70aa5d39 PO |
167 | } |
168 | ||
169 | static inline void sclp_trace_register(int prio, char *id, u32 a, u64 b, | |
170 | struct sclp_register *reg) | |
171 | { | |
172 | struct { | |
173 | u64 receive; | |
174 | u64 send; | |
175 | } d; | |
176 | ||
177 | d.receive = reg->receive_mask; | |
178 | d.send = reg->send_mask; | |
179 | ||
180 | sclp_trace(prio, id, a, b, false); | |
181 | sclp_trace_bin(prio, &d, sizeof(d), 0); | |
182 | } | |
183 | ||
25b41a7b MS |
184 | static int __init sclp_setup_console_pages(char *str) |
185 | { | |
186 | int pages, rc; | |
187 | ||
188 | rc = kstrtoint(str, 0, &pages); | |
189 | if (!rc && pages >= SCLP_CONSOLE_PAGES) | |
190 | sclp_console_pages = pages; | |
191 | return 1; | |
192 | } | |
193 | ||
194 | __setup("sclp_con_pages=", sclp_setup_console_pages); | |
195 | ||
196 | static int __init sclp_setup_console_drop(char *str) | |
197 | { | |
198 | int drop, rc; | |
199 | ||
200 | rc = kstrtoint(str, 0, &drop); | |
00fd2cb0 PO |
201 | if (!rc) |
202 | sclp_console_drop = drop; | |
25b41a7b MS |
203 | return 1; |
204 | } | |
205 | ||
206 | __setup("sclp_con_drop=", sclp_setup_console_drop); | |
207 | ||
1da177e4 LT |
208 | /* Timer for request retries. */ |
209 | static struct timer_list sclp_request_timer; | |
210 | ||
9f0128f9 GS |
211 | /* Timer for queued requests. */ |
212 | static struct timer_list sclp_queue_timer; | |
213 | ||
1da177e4 LT |
214 | /* Internal state: is a request active at the sclp? */ |
215 | static volatile enum sclp_running_state_t { | |
216 | sclp_running_state_idle, | |
dbd8ae63 PO |
217 | sclp_running_state_running, |
218 | sclp_running_state_reset_pending | |
1da177e4 LT |
219 | } sclp_running_state = sclp_running_state_idle; |
220 | ||
221 | /* Internal state: is a read request pending? */ | |
222 | static volatile enum sclp_reading_state_t { | |
223 | sclp_reading_state_idle, | |
224 | sclp_reading_state_reading | |
225 | } sclp_reading_state = sclp_reading_state_idle; | |
226 | ||
227 | /* Internal state: is the driver currently serving requests? */ | |
228 | static volatile enum sclp_activation_state_t { | |
229 | sclp_activation_state_active, | |
230 | sclp_activation_state_deactivating, | |
231 | sclp_activation_state_inactive, | |
232 | sclp_activation_state_activating | |
233 | } sclp_activation_state = sclp_activation_state_active; | |
234 | ||
235 | /* Internal state: is an init mask request pending? */ | |
236 | static volatile enum sclp_mask_state_t { | |
237 | sclp_mask_state_idle, | |
238 | sclp_mask_state_initializing | |
239 | } sclp_mask_state = sclp_mask_state_idle; | |
240 | ||
241 | /* Maximum retry counts */ | |
242 | #define SCLP_INIT_RETRY 3 | |
243 | #define SCLP_MASK_RETRY 3 | |
1da177e4 LT |
244 | |
245 | /* Timeout intervals in seconds.*/ | |
25fab9eb | 246 | #define SCLP_BUSY_INTERVAL 10 |
dbd8ae63 | 247 | #define SCLP_RETRY_INTERVAL 30 |
1da177e4 | 248 | |
c9602ee7 | 249 | static void sclp_request_timeout(bool force_restart); |
1da177e4 | 250 | static void sclp_process_queue(void); |
364c8558 | 251 | static void __sclp_make_read_req(void); |
1da177e4 LT |
252 | static int sclp_init_mask(int calculate); |
253 | static int sclp_init(void); | |
254 | ||
1da177e4 | 255 | static void |
dbd8ae63 | 256 | __sclp_queue_read_req(void) |
1da177e4 | 257 | { |
dbd8ae63 PO |
258 | if (sclp_reading_state == sclp_reading_state_idle) { |
259 | sclp_reading_state = sclp_reading_state_reading; | |
260 | __sclp_make_read_req(); | |
261 | /* Add request to head of queue */ | |
262 | list_add(&sclp_read_req.list, &sclp_req_queue); | |
1da177e4 | 263 | } |
1da177e4 LT |
264 | } |
265 | ||
266 | /* Set up request retry timer. Called while sclp_lock is locked. */ | |
267 | static inline void | |
c9602ee7 | 268 | __sclp_set_request_timer(unsigned long time, void (*cb)(struct timer_list *)) |
1da177e4 LT |
269 | { |
270 | del_timer(&sclp_request_timer); | |
841b86f3 | 271 | sclp_request_timer.function = cb; |
1da177e4 LT |
272 | sclp_request_timer.expires = jiffies + time; |
273 | add_timer(&sclp_request_timer); | |
274 | } | |
275 | ||
c9602ee7 KC |
276 | static void sclp_request_timeout_restart(struct timer_list *unused) |
277 | { | |
278 | sclp_request_timeout(true); | |
279 | } | |
280 | ||
281 | static void sclp_request_timeout_normal(struct timer_list *unused) | |
282 | { | |
283 | sclp_request_timeout(false); | |
284 | } | |
285 | ||
286 | /* Request timeout handler. Restart the request queue. If force_restart, | |
dbd8ae63 | 287 | * force restart of running request. */ |
c9602ee7 | 288 | static void sclp_request_timeout(bool force_restart) |
dbd8ae63 PO |
289 | { |
290 | unsigned long flags; | |
291 | ||
70aa5d39 PO |
292 | /* TMO: A timeout occurred (a=force_restart) */ |
293 | sclp_trace(2, "TMO", force_restart, 0, true); | |
294 | ||
dbd8ae63 | 295 | spin_lock_irqsave(&sclp_lock, flags); |
c9602ee7 | 296 | if (force_restart) { |
dbd8ae63 PO |
297 | if (sclp_running_state == sclp_running_state_running) { |
298 | /* Break running state and queue NOP read event request | |
299 | * to get a defined interface state. */ | |
300 | __sclp_queue_read_req(); | |
301 | sclp_running_state = sclp_running_state_idle; | |
302 | } | |
303 | } else { | |
304 | __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, | |
c9602ee7 | 305 | sclp_request_timeout_normal); |
dbd8ae63 PO |
306 | } |
307 | spin_unlock_irqrestore(&sclp_lock, flags); | |
308 | sclp_process_queue(); | |
309 | } | |
310 | ||
9f0128f9 GS |
311 | /* |
312 | * Returns the expire value in jiffies of the next pending request timeout, | |
313 | * if any. Needs to be called with sclp_lock. | |
314 | */ | |
315 | static unsigned long __sclp_req_queue_find_next_timeout(void) | |
316 | { | |
317 | unsigned long expires_next = 0; | |
318 | struct sclp_req *req; | |
319 | ||
320 | list_for_each_entry(req, &sclp_req_queue, list) { | |
321 | if (!req->queue_expires) | |
322 | continue; | |
323 | if (!expires_next || | |
324 | (time_before(req->queue_expires, expires_next))) | |
325 | expires_next = req->queue_expires; | |
326 | } | |
327 | return expires_next; | |
328 | } | |
329 | ||
330 | /* | |
331 | * Returns expired request, if any, and removes it from the list. | |
332 | */ | |
333 | static struct sclp_req *__sclp_req_queue_remove_expired_req(void) | |
334 | { | |
335 | unsigned long flags, now; | |
336 | struct sclp_req *req; | |
337 | ||
338 | spin_lock_irqsave(&sclp_lock, flags); | |
339 | now = jiffies; | |
340 | /* Don't need list_for_each_safe because we break out after list_del */ | |
341 | list_for_each_entry(req, &sclp_req_queue, list) { | |
342 | if (!req->queue_expires) | |
343 | continue; | |
344 | if (time_before_eq(req->queue_expires, now)) { | |
345 | if (req->status == SCLP_REQ_QUEUED) { | |
346 | req->status = SCLP_REQ_QUEUED_TIMEOUT; | |
347 | list_del(&req->list); | |
348 | goto out; | |
349 | } | |
350 | } | |
351 | } | |
352 | req = NULL; | |
353 | out: | |
354 | spin_unlock_irqrestore(&sclp_lock, flags); | |
355 | return req; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Timeout handler for queued requests. Removes request from list and | |
360 | * invokes callback. This timer can be set per request in situations where | |
361 | * waiting too long would be harmful to the system, e.g. during SE reboot. | |
362 | */ | |
c9602ee7 | 363 | static void sclp_req_queue_timeout(struct timer_list *unused) |
9f0128f9 GS |
364 | { |
365 | unsigned long flags, expires_next; | |
366 | struct sclp_req *req; | |
367 | ||
368 | do { | |
369 | req = __sclp_req_queue_remove_expired_req(); | |
70aa5d39 PO |
370 | |
371 | if (req) { | |
372 | /* RQTM: Request timed out (a=sccb, b=summary) */ | |
373 | sclp_trace_req(2, "RQTM", req, true); | |
374 | } | |
375 | ||
9f0128f9 GS |
376 | if (req && req->callback) |
377 | req->callback(req, req->callback_data); | |
378 | } while (req); | |
379 | ||
380 | spin_lock_irqsave(&sclp_lock, flags); | |
381 | expires_next = __sclp_req_queue_find_next_timeout(); | |
382 | if (expires_next) | |
383 | mod_timer(&sclp_queue_timer, expires_next); | |
384 | spin_unlock_irqrestore(&sclp_lock, flags); | |
385 | } | |
386 | ||
70aa5d39 PO |
387 | static int sclp_service_call_trace(sclp_cmdw_t command, void *sccb) |
388 | { | |
389 | static u64 srvc_count; | |
390 | int rc; | |
391 | ||
392 | /* SRV1: Service call about to be issued (a=command, b=sccb address) */ | |
393 | sclp_trace_sccb(0, "SRV1", command, (u64)sccb, command, sccb, false); | |
394 | ||
395 | rc = sclp_service_call(command, sccb); | |
396 | ||
397 | /* SRV2: Service call was issued (a=rc, b=SRVC sequence number) */ | |
398 | sclp_trace(0, "SRV2", -rc, ++srvc_count, rc != 0); | |
399 | ||
400 | if (rc == 0) | |
401 | active_cmd = command; | |
402 | ||
403 | return rc; | |
404 | } | |
405 | ||
1da177e4 LT |
406 | /* Try to start a request. Return zero if the request was successfully |
407 | * started or if it will be started at a later time. Return non-zero otherwise. | |
408 | * Called while sclp_lock is locked. */ | |
409 | static int | |
410 | __sclp_start_request(struct sclp_req *req) | |
411 | { | |
412 | int rc; | |
413 | ||
414 | if (sclp_running_state != sclp_running_state_idle) | |
415 | return 0; | |
416 | del_timer(&sclp_request_timer); | |
70aa5d39 | 417 | rc = sclp_service_call_trace(req->command, req->sccb); |
25fab9eb PO |
418 | req->start_count++; |
419 | ||
1da177e4 | 420 | if (rc == 0) { |
3ad2f3fb | 421 | /* Successfully started request */ |
1da177e4 LT |
422 | req->status = SCLP_REQ_RUNNING; |
423 | sclp_running_state = sclp_running_state_running; | |
424 | __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ, | |
c9602ee7 | 425 | sclp_request_timeout_restart); |
1da177e4 LT |
426 | return 0; |
427 | } else if (rc == -EBUSY) { | |
428 | /* Try again later */ | |
429 | __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, | |
c9602ee7 | 430 | sclp_request_timeout_normal); |
1da177e4 LT |
431 | return 0; |
432 | } | |
433 | /* Request failed */ | |
434 | req->status = SCLP_REQ_FAILED; | |
435 | return rc; | |
436 | } | |
437 | ||
438 | /* Try to start queued requests. */ | |
439 | static void | |
440 | sclp_process_queue(void) | |
441 | { | |
442 | struct sclp_req *req; | |
443 | int rc; | |
444 | unsigned long flags; | |
445 | ||
446 | spin_lock_irqsave(&sclp_lock, flags); | |
447 | if (sclp_running_state != sclp_running_state_idle) { | |
448 | spin_unlock_irqrestore(&sclp_lock, flags); | |
449 | return; | |
450 | } | |
451 | del_timer(&sclp_request_timer); | |
452 | while (!list_empty(&sclp_req_queue)) { | |
453 | req = list_entry(sclp_req_queue.next, struct sclp_req, list); | |
454 | rc = __sclp_start_request(req); | |
455 | if (rc == 0) | |
456 | break; | |
dbd8ae63 PO |
457 | /* Request failed */ |
458 | if (req->start_count > 1) { | |
459 | /* Cannot abort already submitted request - could still | |
460 | * be active at the SCLP */ | |
461 | __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ, | |
c9602ee7 | 462 | sclp_request_timeout_normal); |
dbd8ae63 PO |
463 | break; |
464 | } | |
465 | /* Post-processing for aborted request */ | |
1da177e4 | 466 | list_del(&req->list); |
70aa5d39 PO |
467 | |
468 | /* RQAB: Request aborted (a=sccb, b=summary) */ | |
469 | sclp_trace_req(2, "RQAB", req, true); | |
470 | ||
1da177e4 LT |
471 | if (req->callback) { |
472 | spin_unlock_irqrestore(&sclp_lock, flags); | |
473 | req->callback(req, req->callback_data); | |
474 | spin_lock_irqsave(&sclp_lock, flags); | |
475 | } | |
476 | } | |
477 | spin_unlock_irqrestore(&sclp_lock, flags); | |
478 | } | |
479 | ||
62b74942 MH |
480 | static int __sclp_can_add_request(struct sclp_req *req) |
481 | { | |
2f7e5208 | 482 | if (req == &sclp_init_req) |
62b74942 | 483 | return 1; |
62b74942 MH |
484 | if (sclp_init_state != sclp_init_state_initialized) |
485 | return 0; | |
486 | if (sclp_activation_state != sclp_activation_state_active) | |
487 | return 0; | |
488 | return 1; | |
489 | } | |
490 | ||
1da177e4 LT |
491 | /* Queue a new request. Return zero on success, non-zero otherwise. */ |
492 | int | |
493 | sclp_add_request(struct sclp_req *req) | |
494 | { | |
495 | unsigned long flags; | |
496 | int rc; | |
497 | ||
498 | spin_lock_irqsave(&sclp_lock, flags); | |
62b74942 | 499 | if (!__sclp_can_add_request(req)) { |
1da177e4 LT |
500 | spin_unlock_irqrestore(&sclp_lock, flags); |
501 | return -EIO; | |
502 | } | |
70aa5d39 PO |
503 | |
504 | /* RQAD: Request was added (a=sccb, b=caller) */ | |
ada1da31 | 505 | sclp_trace(2, "RQAD", __pa(req->sccb), _RET_IP_, false); |
70aa5d39 | 506 | |
1da177e4 LT |
507 | req->status = SCLP_REQ_QUEUED; |
508 | req->start_count = 0; | |
509 | list_add_tail(&req->list, &sclp_req_queue); | |
510 | rc = 0; | |
9f0128f9 GS |
511 | if (req->queue_timeout) { |
512 | req->queue_expires = jiffies + req->queue_timeout * HZ; | |
513 | if (!timer_pending(&sclp_queue_timer) || | |
514 | time_after(sclp_queue_timer.expires, req->queue_expires)) | |
515 | mod_timer(&sclp_queue_timer, req->queue_expires); | |
516 | } else | |
517 | req->queue_expires = 0; | |
1da177e4 | 518 | /* Start if request is first in list */ |
dbd8ae63 PO |
519 | if (sclp_running_state == sclp_running_state_idle && |
520 | req->list.prev == &sclp_req_queue) { | |
1da177e4 LT |
521 | rc = __sclp_start_request(req); |
522 | if (rc) | |
523 | list_del(&req->list); | |
524 | } | |
525 | spin_unlock_irqrestore(&sclp_lock, flags); | |
526 | return rc; | |
527 | } | |
528 | ||
529 | EXPORT_SYMBOL(sclp_add_request); | |
530 | ||
531 | /* Dispatch events found in request buffer to registered listeners. Return 0 | |
532 | * if all events were dispatched, non-zero otherwise. */ | |
533 | static int | |
534 | sclp_dispatch_evbufs(struct sccb_header *sccb) | |
535 | { | |
536 | unsigned long flags; | |
537 | struct evbuf_header *evbuf; | |
538 | struct list_head *l; | |
539 | struct sclp_register *reg; | |
540 | int offset; | |
541 | int rc; | |
542 | ||
543 | spin_lock_irqsave(&sclp_lock, flags); | |
544 | rc = 0; | |
545 | for (offset = sizeof(struct sccb_header); offset < sccb->length; | |
546 | offset += evbuf->length) { | |
1da177e4 | 547 | evbuf = (struct evbuf_header *) ((addr_t) sccb + offset); |
e2e5a0f2 PO |
548 | /* Check for malformed hardware response */ |
549 | if (evbuf->length == 0) | |
550 | break; | |
551 | /* Search for event handler */ | |
1da177e4 LT |
552 | reg = NULL; |
553 | list_for_each(l, &sclp_reg_list) { | |
554 | reg = list_entry(l, struct sclp_register, list); | |
0ee5f8dc | 555 | if (reg->receive_mask & SCLP_EVTYP_MASK(evbuf->type)) |
1da177e4 LT |
556 | break; |
557 | else | |
558 | reg = NULL; | |
559 | } | |
70aa5d39 PO |
560 | |
561 | /* EVNT: Event callback (b=receiver) */ | |
562 | sclp_trace_evbuf(2, "EVNT", 0, reg ? (u64)reg->receiver_fn : 0, | |
563 | evbuf, !reg); | |
564 | ||
1da177e4 LT |
565 | if (reg && reg->receiver_fn) { |
566 | spin_unlock_irqrestore(&sclp_lock, flags); | |
567 | reg->receiver_fn(evbuf); | |
568 | spin_lock_irqsave(&sclp_lock, flags); | |
569 | } else if (reg == NULL) | |
d06cbda6 | 570 | rc = -EOPNOTSUPP; |
1da177e4 LT |
571 | } |
572 | spin_unlock_irqrestore(&sclp_lock, flags); | |
573 | return rc; | |
574 | } | |
575 | ||
576 | /* Read event data request callback. */ | |
577 | static void | |
578 | sclp_read_cb(struct sclp_req *req, void *data) | |
579 | { | |
580 | unsigned long flags; | |
581 | struct sccb_header *sccb; | |
582 | ||
583 | sccb = (struct sccb_header *) req->sccb; | |
584 | if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 || | |
585 | sccb->response_code == 0x220)) | |
586 | sclp_dispatch_evbufs(sccb); | |
587 | spin_lock_irqsave(&sclp_lock, flags); | |
588 | sclp_reading_state = sclp_reading_state_idle; | |
589 | spin_unlock_irqrestore(&sclp_lock, flags); | |
590 | } | |
591 | ||
592 | /* Prepare read event data request. Called while sclp_lock is locked. */ | |
364c8558 | 593 | static void __sclp_make_read_req(void) |
1da177e4 LT |
594 | { |
595 | struct sccb_header *sccb; | |
596 | ||
597 | sccb = (struct sccb_header *) sclp_read_sccb; | |
598 | clear_page(sccb); | |
599 | memset(&sclp_read_req, 0, sizeof(struct sclp_req)); | |
ab14de6c | 600 | sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA; |
1da177e4 LT |
601 | sclp_read_req.status = SCLP_REQ_QUEUED; |
602 | sclp_read_req.start_count = 0; | |
603 | sclp_read_req.callback = sclp_read_cb; | |
604 | sclp_read_req.sccb = sccb; | |
605 | sccb->length = PAGE_SIZE; | |
606 | sccb->function_code = 0; | |
607 | sccb->control_mask[2] = 0x80; | |
608 | } | |
609 | ||
610 | /* Search request list for request with matching sccb. Return request if found, | |
611 | * NULL otherwise. Called while sclp_lock is locked. */ | |
612 | static inline struct sclp_req * | |
613 | __sclp_find_req(u32 sccb) | |
614 | { | |
615 | struct list_head *l; | |
616 | struct sclp_req *req; | |
617 | ||
618 | list_for_each(l, &sclp_req_queue) { | |
619 | req = list_entry(l, struct sclp_req, list); | |
ada1da31 AG |
620 | if (sccb == __pa(req->sccb)) |
621 | return req; | |
1da177e4 LT |
622 | } |
623 | return NULL; | |
624 | } | |
625 | ||
70aa5d39 PO |
626 | static bool ok_response(u32 sccb_int, sclp_cmdw_t cmd) |
627 | { | |
ada1da31 | 628 | struct sccb_header *sccb = (struct sccb_header *)__va(sccb_int); |
70aa5d39 PO |
629 | struct evbuf_header *evbuf; |
630 | u16 response; | |
631 | ||
632 | if (!sccb) | |
633 | return true; | |
634 | ||
635 | /* Check SCCB response. */ | |
636 | response = sccb->response_code & 0xff; | |
637 | if (response != 0x10 && response != 0x20) | |
638 | return false; | |
639 | ||
640 | /* Check event-processed flag on outgoing events. */ | |
641 | if (cmd == SCLP_CMDW_WRITE_EVENT_DATA) { | |
642 | evbuf = (struct evbuf_header *)(sccb + 1); | |
643 | if (!(evbuf->flags & 0x80)) | |
644 | return false; | |
645 | } | |
646 | ||
647 | return true; | |
648 | } | |
649 | ||
1da177e4 LT |
650 | /* Handler for external interruption. Perform request post-processing. |
651 | * Prepare read event data request if necessary. Start processing of next | |
652 | * request on queue. */ | |
fde15c3a | 653 | static void sclp_interrupt_handler(struct ext_code ext_code, |
f6649a7e | 654 | unsigned int param32, unsigned long param64) |
1da177e4 LT |
655 | { |
656 | struct sclp_req *req; | |
657 | u32 finished_sccb; | |
658 | u32 evbuf_pending; | |
659 | ||
420f42ec | 660 | inc_irq_stat(IRQEXT_SCP); |
1da177e4 | 661 | spin_lock(&sclp_lock); |
f6649a7e MS |
662 | finished_sccb = param32 & 0xfffffff8; |
663 | evbuf_pending = param32 & 0x3; | |
70aa5d39 PO |
664 | |
665 | /* INT: Interrupt received (a=intparm, b=cmd) */ | |
666 | sclp_trace_sccb(0, "INT", param32, active_cmd, active_cmd, | |
ada1da31 | 667 | (struct sccb_header *)__va(finished_sccb), |
70aa5d39 PO |
668 | !ok_response(finished_sccb, active_cmd)); |
669 | ||
1da177e4 | 670 | if (finished_sccb) { |
dbd8ae63 PO |
671 | del_timer(&sclp_request_timer); |
672 | sclp_running_state = sclp_running_state_reset_pending; | |
1da177e4 LT |
673 | req = __sclp_find_req(finished_sccb); |
674 | if (req) { | |
675 | /* Request post-processing */ | |
676 | list_del(&req->list); | |
677 | req->status = SCLP_REQ_DONE; | |
70aa5d39 PO |
678 | |
679 | /* RQOK: Request success (a=sccb, b=summary) */ | |
680 | sclp_trace_req(2, "RQOK", req, false); | |
681 | ||
1da177e4 LT |
682 | if (req->callback) { |
683 | spin_unlock(&sclp_lock); | |
684 | req->callback(req, req->callback_data); | |
685 | spin_lock(&sclp_lock); | |
686 | } | |
70aa5d39 PO |
687 | } else { |
688 | /* UNEX: Unexpected SCCB completion (a=sccb address) */ | |
689 | sclp_trace(0, "UNEX", finished_sccb, 0, true); | |
1da177e4 LT |
690 | } |
691 | sclp_running_state = sclp_running_state_idle; | |
70aa5d39 | 692 | active_cmd = 0; |
1da177e4 | 693 | } |
d082d3ce | 694 | if (evbuf_pending && |
dbd8ae63 PO |
695 | sclp_activation_state == sclp_activation_state_active) |
696 | __sclp_queue_read_req(); | |
1da177e4 LT |
697 | spin_unlock(&sclp_lock); |
698 | sclp_process_queue(); | |
699 | } | |
700 | ||
1da177e4 LT |
701 | /* Convert interval in jiffies to TOD ticks. */ |
702 | static inline u64 | |
703 | sclp_tod_from_jiffies(unsigned long jiffies) | |
704 | { | |
705 | return (u64) (jiffies / HZ) << 32; | |
706 | } | |
707 | ||
708 | /* Wait until a currently running request finished. Note: while this function | |
709 | * is running, no timers are served on the calling CPU. */ | |
710 | void | |
711 | sclp_sync_wait(void) | |
712 | { | |
934b2857 | 713 | unsigned long long old_tick; |
1f194a4c | 714 | unsigned long flags; |
1da177e4 | 715 | unsigned long cr0, cr0_sync; |
70aa5d39 | 716 | static u64 sync_count; |
1da177e4 | 717 | u64 timeout; |
c59d744b | 718 | int irq_context; |
1da177e4 | 719 | |
70aa5d39 PO |
720 | /* SYN1: Synchronous wait start (a=runstate, b=sync count) */ |
721 | sclp_trace(4, "SYN1", sclp_running_state, ++sync_count, false); | |
722 | ||
1da177e4 LT |
723 | /* We'll be disabling timer interrupts, so we need a custom timeout |
724 | * mechanism */ | |
725 | timeout = 0; | |
726 | if (timer_pending(&sclp_request_timer)) { | |
727 | /* Get timeout TOD value */ | |
8c071b0f | 728 | timeout = get_tod_clock_fast() + |
1da177e4 LT |
729 | sclp_tod_from_jiffies(sclp_request_timer.expires - |
730 | jiffies); | |
731 | } | |
1f194a4c | 732 | local_irq_save(flags); |
1da177e4 | 733 | /* Prevent bottom half from executing once we force interrupts open */ |
c59d744b HC |
734 | irq_context = in_interrupt(); |
735 | if (!irq_context) | |
736 | local_bh_disable(); | |
1da177e4 | 737 | /* Enable service-signal interruption, disable timer interrupts */ |
934b2857 | 738 | old_tick = local_tick_disable(); |
1f194a4c | 739 | trace_hardirqs_on(); |
1da177e4 | 740 | __ctl_store(cr0, 0, 0); |
c2ab7282 HC |
741 | cr0_sync = cr0 & ~CR0_IRQ_SUBCLASS_MASK; |
742 | cr0_sync |= 1UL << (63 - 54); | |
1da177e4 | 743 | __ctl_load(cr0_sync, 0, 0); |
df9ee292 | 744 | __arch_local_irq_stosm(0x01); |
1da177e4 LT |
745 | /* Loop until driver state indicates finished request */ |
746 | while (sclp_running_state != sclp_running_state_idle) { | |
747 | /* Check for expired request timer */ | |
4f45c37f | 748 | if (get_tod_clock_fast() > timeout && del_timer(&sclp_request_timer)) |
841b86f3 | 749 | sclp_request_timer.function(&sclp_request_timer); |
1da177e4 LT |
750 | cpu_relax(); |
751 | } | |
1f194a4c | 752 | local_irq_disable(); |
1da177e4 | 753 | __ctl_load(cr0, 0, 0); |
c59d744b HC |
754 | if (!irq_context) |
755 | _local_bh_enable(); | |
934b2857 | 756 | local_tick_enable(old_tick); |
1f194a4c | 757 | local_irq_restore(flags); |
70aa5d39 PO |
758 | |
759 | /* SYN2: Synchronous wait end (a=runstate, b=sync_count) */ | |
760 | sclp_trace(4, "SYN2", sclp_running_state, sync_count, false); | |
1da177e4 | 761 | } |
1da177e4 LT |
762 | EXPORT_SYMBOL(sclp_sync_wait); |
763 | ||
764 | /* Dispatch changes in send and receive mask to registered listeners. */ | |
4d284cac | 765 | static void |
1da177e4 LT |
766 | sclp_dispatch_state_change(void) |
767 | { | |
768 | struct list_head *l; | |
769 | struct sclp_register *reg; | |
770 | unsigned long flags; | |
771 | sccb_mask_t receive_mask; | |
772 | sccb_mask_t send_mask; | |
773 | ||
774 | do { | |
775 | spin_lock_irqsave(&sclp_lock, flags); | |
776 | reg = NULL; | |
777 | list_for_each(l, &sclp_reg_list) { | |
778 | reg = list_entry(l, struct sclp_register, list); | |
d082d3ce PO |
779 | receive_mask = reg->send_mask & sclp_receive_mask; |
780 | send_mask = reg->receive_mask & sclp_send_mask; | |
1da177e4 LT |
781 | if (reg->sclp_receive_mask != receive_mask || |
782 | reg->sclp_send_mask != send_mask) { | |
783 | reg->sclp_receive_mask = receive_mask; | |
784 | reg->sclp_send_mask = send_mask; | |
785 | break; | |
786 | } else | |
787 | reg = NULL; | |
788 | } | |
789 | spin_unlock_irqrestore(&sclp_lock, flags); | |
70aa5d39 PO |
790 | if (reg && reg->state_change_fn) { |
791 | /* STCG: State-change callback (b=callback) */ | |
792 | sclp_trace(2, "STCG", 0, (u64)reg->state_change_fn, | |
793 | false); | |
794 | ||
1da177e4 | 795 | reg->state_change_fn(reg); |
70aa5d39 | 796 | } |
1da177e4 LT |
797 | } while (reg); |
798 | } | |
799 | ||
800 | struct sclp_statechangebuf { | |
801 | struct evbuf_header header; | |
802 | u8 validity_sclp_active_facility_mask : 1; | |
803 | u8 validity_sclp_receive_mask : 1; | |
804 | u8 validity_sclp_send_mask : 1; | |
805 | u8 validity_read_data_function_mask : 1; | |
806 | u16 _zeros : 12; | |
807 | u16 mask_length; | |
808 | u64 sclp_active_facility_mask; | |
b8435635 CI |
809 | u8 masks[2 * 1021 + 4]; /* variable length */ |
810 | /* | |
811 | * u8 sclp_receive_mask[mask_length]; | |
812 | * u8 sclp_send_mask[mask_length]; | |
813 | * u32 read_data_function_mask; | |
814 | */ | |
1da177e4 LT |
815 | } __attribute__((packed)); |
816 | ||
817 | ||
818 | /* State change event callback. Inform listeners of changes. */ | |
819 | static void | |
820 | sclp_state_change_cb(struct evbuf_header *evbuf) | |
821 | { | |
822 | unsigned long flags; | |
823 | struct sclp_statechangebuf *scbuf; | |
824 | ||
b8435635 CI |
825 | BUILD_BUG_ON(sizeof(struct sclp_statechangebuf) > PAGE_SIZE); |
826 | ||
1da177e4 | 827 | scbuf = (struct sclp_statechangebuf *) evbuf; |
1da177e4 LT |
828 | spin_lock_irqsave(&sclp_lock, flags); |
829 | if (scbuf->validity_sclp_receive_mask) | |
b8435635 | 830 | sclp_receive_mask = sccb_get_recv_mask(scbuf); |
1da177e4 | 831 | if (scbuf->validity_sclp_send_mask) |
b8435635 | 832 | sclp_send_mask = sccb_get_send_mask(scbuf); |
1da177e4 | 833 | spin_unlock_irqrestore(&sclp_lock, flags); |
887d935a | 834 | if (scbuf->validity_sclp_active_facility_mask) |
78335a30 | 835 | sclp.facilities = scbuf->sclp_active_facility_mask; |
1da177e4 LT |
836 | sclp_dispatch_state_change(); |
837 | } | |
838 | ||
839 | static struct sclp_register sclp_state_change_event = { | |
6d4740c8 | 840 | .receive_mask = EVTYP_STATECHANGE_MASK, |
1da177e4 LT |
841 | .receiver_fn = sclp_state_change_cb |
842 | }; | |
843 | ||
844 | /* Calculate receive and send mask of currently registered listeners. | |
845 | * Called while sclp_lock is locked. */ | |
846 | static inline void | |
847 | __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask) | |
848 | { | |
849 | struct list_head *l; | |
850 | struct sclp_register *t; | |
851 | ||
852 | *receive_mask = 0; | |
853 | *send_mask = 0; | |
854 | list_for_each(l, &sclp_reg_list) { | |
855 | t = list_entry(l, struct sclp_register, list); | |
856 | *receive_mask |= t->receive_mask; | |
857 | *send_mask |= t->send_mask; | |
858 | } | |
859 | } | |
860 | ||
861 | /* Register event listener. Return 0 on success, non-zero otherwise. */ | |
862 | int | |
863 | sclp_register(struct sclp_register *reg) | |
864 | { | |
865 | unsigned long flags; | |
866 | sccb_mask_t receive_mask; | |
867 | sccb_mask_t send_mask; | |
868 | int rc; | |
869 | ||
70aa5d39 PO |
870 | /* REG: Event listener registered (b=caller) */ |
871 | sclp_trace_register(2, "REG", 0, _RET_IP_, reg); | |
872 | ||
1da177e4 LT |
873 | rc = sclp_init(); |
874 | if (rc) | |
875 | return rc; | |
876 | spin_lock_irqsave(&sclp_lock, flags); | |
877 | /* Check event mask for collisions */ | |
878 | __sclp_get_mask(&receive_mask, &send_mask); | |
879 | if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) { | |
880 | spin_unlock_irqrestore(&sclp_lock, flags); | |
881 | return -EBUSY; | |
882 | } | |
883 | /* Trigger initial state change callback */ | |
884 | reg->sclp_receive_mask = 0; | |
885 | reg->sclp_send_mask = 0; | |
886 | list_add(®->list, &sclp_reg_list); | |
887 | spin_unlock_irqrestore(&sclp_lock, flags); | |
888 | rc = sclp_init_mask(1); | |
889 | if (rc) { | |
890 | spin_lock_irqsave(&sclp_lock, flags); | |
891 | list_del(®->list); | |
892 | spin_unlock_irqrestore(&sclp_lock, flags); | |
893 | } | |
894 | return rc; | |
895 | } | |
896 | ||
897 | EXPORT_SYMBOL(sclp_register); | |
898 | ||
899 | /* Unregister event listener. */ | |
900 | void | |
901 | sclp_unregister(struct sclp_register *reg) | |
902 | { | |
903 | unsigned long flags; | |
904 | ||
70aa5d39 PO |
905 | /* UREG: Event listener unregistered (b=caller) */ |
906 | sclp_trace_register(2, "UREG", 0, _RET_IP_, reg); | |
907 | ||
1da177e4 LT |
908 | spin_lock_irqsave(&sclp_lock, flags); |
909 | list_del(®->list); | |
910 | spin_unlock_irqrestore(&sclp_lock, flags); | |
911 | sclp_init_mask(1); | |
912 | } | |
913 | ||
914 | EXPORT_SYMBOL(sclp_unregister); | |
915 | ||
916 | /* Remove event buffers which are marked processed. Return the number of | |
917 | * remaining event buffers. */ | |
918 | int | |
919 | sclp_remove_processed(struct sccb_header *sccb) | |
920 | { | |
921 | struct evbuf_header *evbuf; | |
922 | int unprocessed; | |
923 | u16 remaining; | |
924 | ||
925 | evbuf = (struct evbuf_header *) (sccb + 1); | |
926 | unprocessed = 0; | |
927 | remaining = sccb->length - sizeof(struct sccb_header); | |
928 | while (remaining > 0) { | |
929 | remaining -= evbuf->length; | |
930 | if (evbuf->flags & 0x80) { | |
931 | sccb->length -= evbuf->length; | |
932 | memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length), | |
933 | remaining); | |
934 | } else { | |
935 | unprocessed++; | |
936 | evbuf = (struct evbuf_header *) | |
937 | ((addr_t) evbuf + evbuf->length); | |
938 | } | |
939 | } | |
940 | return unprocessed; | |
941 | } | |
942 | ||
943 | EXPORT_SYMBOL(sclp_remove_processed); | |
944 | ||
1da177e4 LT |
945 | /* Prepare init mask request. Called while sclp_lock is locked. */ |
946 | static inline void | |
0ee5f8dc | 947 | __sclp_make_init_req(sccb_mask_t receive_mask, sccb_mask_t send_mask) |
1da177e4 | 948 | { |
087c4d74 | 949 | struct init_sccb *sccb = sclp_init_sccb; |
1da177e4 | 950 | |
1da177e4 LT |
951 | clear_page(sccb); |
952 | memset(&sclp_init_req, 0, sizeof(struct sclp_req)); | |
ab14de6c | 953 | sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK; |
1da177e4 LT |
954 | sclp_init_req.status = SCLP_REQ_FILLED; |
955 | sclp_init_req.start_count = 0; | |
956 | sclp_init_req.callback = NULL; | |
957 | sclp_init_req.callback_data = NULL; | |
958 | sclp_init_req.sccb = sccb; | |
0ee5f8dc | 959 | sccb->header.length = sizeof(*sccb); |
0b0d1173 CI |
960 | if (sclp_mask_compat_mode) |
961 | sccb->mask_length = SCLP_MASK_SIZE_COMPAT; | |
962 | else | |
963 | sccb->mask_length = sizeof(sccb_mask_t); | |
b8435635 CI |
964 | sccb_set_recv_mask(sccb, receive_mask); |
965 | sccb_set_send_mask(sccb, send_mask); | |
966 | sccb_set_sclp_recv_mask(sccb, 0); | |
967 | sccb_set_sclp_send_mask(sccb, 0); | |
1da177e4 LT |
968 | } |
969 | ||
970 | /* Start init mask request. If calculate is non-zero, calculate the mask as | |
971 | * requested by registered listeners. Use zero mask otherwise. Return 0 on | |
972 | * success, non-zero otherwise. */ | |
973 | static int | |
974 | sclp_init_mask(int calculate) | |
975 | { | |
976 | unsigned long flags; | |
087c4d74 | 977 | struct init_sccb *sccb = sclp_init_sccb; |
1da177e4 LT |
978 | sccb_mask_t receive_mask; |
979 | sccb_mask_t send_mask; | |
980 | int retry; | |
981 | int rc; | |
982 | unsigned long wait; | |
983 | ||
984 | spin_lock_irqsave(&sclp_lock, flags); | |
985 | /* Check if interface is in appropriate state */ | |
986 | if (sclp_mask_state != sclp_mask_state_idle) { | |
987 | spin_unlock_irqrestore(&sclp_lock, flags); | |
988 | return -EBUSY; | |
989 | } | |
990 | if (sclp_activation_state == sclp_activation_state_inactive) { | |
991 | spin_unlock_irqrestore(&sclp_lock, flags); | |
992 | return -EINVAL; | |
993 | } | |
994 | sclp_mask_state = sclp_mask_state_initializing; | |
995 | /* Determine mask */ | |
996 | if (calculate) | |
997 | __sclp_get_mask(&receive_mask, &send_mask); | |
998 | else { | |
999 | receive_mask = 0; | |
1000 | send_mask = 0; | |
1001 | } | |
1002 | rc = -EIO; | |
1003 | for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) { | |
1004 | /* Prepare request */ | |
1005 | __sclp_make_init_req(receive_mask, send_mask); | |
1006 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1007 | if (sclp_add_request(&sclp_init_req)) { | |
1008 | /* Try again later */ | |
1009 | wait = jiffies + SCLP_BUSY_INTERVAL * HZ; | |
1010 | while (time_before(jiffies, wait)) | |
1011 | sclp_sync_wait(); | |
1012 | spin_lock_irqsave(&sclp_lock, flags); | |
1013 | continue; | |
1014 | } | |
1015 | while (sclp_init_req.status != SCLP_REQ_DONE && | |
1016 | sclp_init_req.status != SCLP_REQ_FAILED) | |
1017 | sclp_sync_wait(); | |
1018 | spin_lock_irqsave(&sclp_lock, flags); | |
1019 | if (sclp_init_req.status == SCLP_REQ_DONE && | |
1020 | sccb->header.response_code == 0x20) { | |
1021 | /* Successful request */ | |
1022 | if (calculate) { | |
b8435635 CI |
1023 | sclp_receive_mask = sccb_get_sclp_recv_mask(sccb); |
1024 | sclp_send_mask = sccb_get_sclp_send_mask(sccb); | |
1da177e4 LT |
1025 | } else { |
1026 | sclp_receive_mask = 0; | |
1027 | sclp_send_mask = 0; | |
1028 | } | |
1029 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1030 | sclp_dispatch_state_change(); | |
1031 | spin_lock_irqsave(&sclp_lock, flags); | |
1032 | rc = 0; | |
1033 | break; | |
1034 | } | |
1035 | } | |
1036 | sclp_mask_state = sclp_mask_state_idle; | |
1037 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1038 | return rc; | |
1039 | } | |
1040 | ||
1041 | /* Deactivate SCLP interface. On success, new requests will be rejected, | |
1042 | * events will no longer be dispatched. Return 0 on success, non-zero | |
1043 | * otherwise. */ | |
1044 | int | |
1045 | sclp_deactivate(void) | |
1046 | { | |
1047 | unsigned long flags; | |
1048 | int rc; | |
1049 | ||
1050 | spin_lock_irqsave(&sclp_lock, flags); | |
1051 | /* Deactivate can only be called when active */ | |
1052 | if (sclp_activation_state != sclp_activation_state_active) { | |
1053 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1054 | return -EINVAL; | |
1055 | } | |
1056 | sclp_activation_state = sclp_activation_state_deactivating; | |
1057 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1058 | rc = sclp_init_mask(0); | |
1059 | spin_lock_irqsave(&sclp_lock, flags); | |
1060 | if (rc == 0) | |
1061 | sclp_activation_state = sclp_activation_state_inactive; | |
1062 | else | |
1063 | sclp_activation_state = sclp_activation_state_active; | |
1064 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1065 | return rc; | |
1066 | } | |
1067 | ||
1068 | EXPORT_SYMBOL(sclp_deactivate); | |
1069 | ||
1070 | /* Reactivate SCLP interface after sclp_deactivate. On success, new | |
1071 | * requests will be accepted, events will be dispatched again. Return 0 on | |
1072 | * success, non-zero otherwise. */ | |
1073 | int | |
1074 | sclp_reactivate(void) | |
1075 | { | |
1076 | unsigned long flags; | |
1077 | int rc; | |
1078 | ||
1079 | spin_lock_irqsave(&sclp_lock, flags); | |
1080 | /* Reactivate can only be called when inactive */ | |
1081 | if (sclp_activation_state != sclp_activation_state_inactive) { | |
1082 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1083 | return -EINVAL; | |
1084 | } | |
1085 | sclp_activation_state = sclp_activation_state_activating; | |
1086 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1087 | rc = sclp_init_mask(1); | |
1088 | spin_lock_irqsave(&sclp_lock, flags); | |
1089 | if (rc == 0) | |
1090 | sclp_activation_state = sclp_activation_state_active; | |
1091 | else | |
1092 | sclp_activation_state = sclp_activation_state_inactive; | |
1093 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1094 | return rc; | |
1095 | } | |
1096 | ||
1097 | EXPORT_SYMBOL(sclp_reactivate); | |
1098 | ||
1099 | /* Handler for external interruption used during initialization. Modify | |
1100 | * request state to done. */ | |
fde15c3a | 1101 | static void sclp_check_handler(struct ext_code ext_code, |
f6649a7e | 1102 | unsigned int param32, unsigned long param64) |
1da177e4 LT |
1103 | { |
1104 | u32 finished_sccb; | |
1105 | ||
420f42ec | 1106 | inc_irq_stat(IRQEXT_SCP); |
f6649a7e | 1107 | finished_sccb = param32 & 0xfffffff8; |
1da177e4 LT |
1108 | /* Is this the interrupt we are waiting for? */ |
1109 | if (finished_sccb == 0) | |
1110 | return; | |
ada1da31 | 1111 | if (finished_sccb != __pa(sclp_init_sccb)) |
a12c53f4 MS |
1112 | panic("sclp: unsolicited interrupt for buffer at 0x%x\n", |
1113 | finished_sccb); | |
1da177e4 LT |
1114 | spin_lock(&sclp_lock); |
1115 | if (sclp_running_state == sclp_running_state_running) { | |
1116 | sclp_init_req.status = SCLP_REQ_DONE; | |
1117 | sclp_running_state = sclp_running_state_idle; | |
1118 | } | |
1119 | spin_unlock(&sclp_lock); | |
1120 | } | |
1121 | ||
1122 | /* Initial init mask request timed out. Modify request state to failed. */ | |
1123 | static void | |
c9602ee7 | 1124 | sclp_check_timeout(struct timer_list *unused) |
1da177e4 LT |
1125 | { |
1126 | unsigned long flags; | |
1127 | ||
1128 | spin_lock_irqsave(&sclp_lock, flags); | |
1129 | if (sclp_running_state == sclp_running_state_running) { | |
1130 | sclp_init_req.status = SCLP_REQ_FAILED; | |
1131 | sclp_running_state = sclp_running_state_idle; | |
1132 | } | |
1133 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1134 | } | |
1135 | ||
1136 | /* Perform a check of the SCLP interface. Return zero if the interface is | |
1137 | * available and there are no pending requests from a previous instance. | |
1138 | * Return non-zero otherwise. */ | |
1139 | static int | |
1140 | sclp_check_interface(void) | |
1141 | { | |
1142 | struct init_sccb *sccb; | |
1143 | unsigned long flags; | |
1144 | int retry; | |
1145 | int rc; | |
1146 | ||
1147 | spin_lock_irqsave(&sclp_lock, flags); | |
1148 | /* Prepare init mask command */ | |
1dad093b | 1149 | rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler); |
1da177e4 LT |
1150 | if (rc) { |
1151 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1152 | return rc; | |
1153 | } | |
1154 | for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) { | |
1155 | __sclp_make_init_req(0, 0); | |
1156 | sccb = (struct init_sccb *) sclp_init_req.sccb; | |
70aa5d39 | 1157 | rc = sclp_service_call_trace(sclp_init_req.command, sccb); |
1da177e4 LT |
1158 | if (rc == -EIO) |
1159 | break; | |
1160 | sclp_init_req.status = SCLP_REQ_RUNNING; | |
1161 | sclp_running_state = sclp_running_state_running; | |
1162 | __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ, | |
c9602ee7 | 1163 | sclp_check_timeout); |
1da177e4 LT |
1164 | spin_unlock_irqrestore(&sclp_lock, flags); |
1165 | /* Enable service-signal interruption - needs to happen | |
1166 | * with IRQs enabled. */ | |
82003c3e | 1167 | irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); |
1da177e4 LT |
1168 | /* Wait for signal from interrupt or timeout */ |
1169 | sclp_sync_wait(); | |
1170 | /* Disable service-signal interruption - needs to happen | |
1171 | * with IRQs enabled. */ | |
82003c3e | 1172 | irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL); |
1da177e4 LT |
1173 | spin_lock_irqsave(&sclp_lock, flags); |
1174 | del_timer(&sclp_request_timer); | |
0b0d1173 CI |
1175 | rc = -EBUSY; |
1176 | if (sclp_init_req.status == SCLP_REQ_DONE) { | |
1177 | if (sccb->header.response_code == 0x20) { | |
1178 | rc = 0; | |
1179 | break; | |
1180 | } else if (sccb->header.response_code == 0x74f0) { | |
1181 | if (!sclp_mask_compat_mode) { | |
1182 | sclp_mask_compat_mode = true; | |
1183 | retry = 0; | |
1184 | } | |
1185 | } | |
1186 | } | |
1da177e4 | 1187 | } |
1dad093b | 1188 | unregister_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler); |
1da177e4 LT |
1189 | spin_unlock_irqrestore(&sclp_lock, flags); |
1190 | return rc; | |
1191 | } | |
1192 | ||
1193 | /* Reboot event handler. Reset send and receive mask to prevent pending SCLP | |
1194 | * events from interfering with rebooted system. */ | |
1195 | static int | |
1196 | sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr) | |
1197 | { | |
1198 | sclp_deactivate(); | |
1199 | return NOTIFY_DONE; | |
1200 | } | |
1201 | ||
1202 | static struct notifier_block sclp_reboot_notifier = { | |
1203 | .notifier_call = sclp_reboot_event | |
1204 | }; | |
1205 | ||
36369569 | 1206 | static ssize_t con_pages_show(struct device_driver *dev, char *buf) |
25b41a7b MS |
1207 | { |
1208 | return sprintf(buf, "%i\n", sclp_console_pages); | |
1209 | } | |
1210 | ||
36369569 | 1211 | static DRIVER_ATTR_RO(con_pages); |
25b41a7b | 1212 | |
36369569 | 1213 | static ssize_t con_drop_show(struct device_driver *dev, char *buf) |
25b41a7b MS |
1214 | { |
1215 | return sprintf(buf, "%i\n", sclp_console_drop); | |
1216 | } | |
1217 | ||
36369569 | 1218 | static DRIVER_ATTR_RO(con_drop); |
25b41a7b | 1219 | |
36369569 | 1220 | static ssize_t con_full_show(struct device_driver *dev, char *buf) |
25b41a7b MS |
1221 | { |
1222 | return sprintf(buf, "%lu\n", sclp_console_full); | |
1223 | } | |
1224 | ||
36369569 | 1225 | static DRIVER_ATTR_RO(con_full); |
25b41a7b MS |
1226 | |
1227 | static struct attribute *sclp_drv_attrs[] = { | |
1228 | &driver_attr_con_pages.attr, | |
1229 | &driver_attr_con_drop.attr, | |
1230 | &driver_attr_con_full.attr, | |
1231 | NULL, | |
1232 | }; | |
1233 | static struct attribute_group sclp_drv_attr_group = { | |
1234 | .attrs = sclp_drv_attrs, | |
1235 | }; | |
1236 | static const struct attribute_group *sclp_drv_attr_groups[] = { | |
1237 | &sclp_drv_attr_group, | |
1238 | NULL, | |
1239 | }; | |
1240 | ||
62b74942 MH |
1241 | static struct platform_driver sclp_pdrv = { |
1242 | .driver = { | |
1243 | .name = "sclp", | |
25b41a7b | 1244 | .groups = sclp_drv_attr_groups, |
62b74942 MH |
1245 | }, |
1246 | }; | |
1247 | ||
1da177e4 LT |
1248 | /* Initialize SCLP driver. Return zero if driver is operational, non-zero |
1249 | * otherwise. */ | |
1250 | static int | |
1251 | sclp_init(void) | |
1252 | { | |
1253 | unsigned long flags; | |
62b74942 | 1254 | int rc = 0; |
1da177e4 | 1255 | |
1da177e4 LT |
1256 | spin_lock_irqsave(&sclp_lock, flags); |
1257 | /* Check for previous or running initialization */ | |
62b74942 MH |
1258 | if (sclp_init_state != sclp_init_state_uninitialized) |
1259 | goto fail_unlock; | |
1da177e4 | 1260 | sclp_init_state = sclp_init_state_initializing; |
087c4d74 GS |
1261 | sclp_read_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA); |
1262 | sclp_init_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA); | |
1263 | BUG_ON(!sclp_read_sccb || !sclp_init_sccb); | |
1da177e4 | 1264 | /* Set up variables */ |
1da177e4 | 1265 | list_add(&sclp_state_change_event.list, &sclp_reg_list); |
c9602ee7 KC |
1266 | timer_setup(&sclp_request_timer, NULL, 0); |
1267 | timer_setup(&sclp_queue_timer, sclp_req_queue_timeout, 0); | |
1da177e4 LT |
1268 | /* Check interface */ |
1269 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1270 | rc = sclp_check_interface(); | |
1271 | spin_lock_irqsave(&sclp_lock, flags); | |
62b74942 MH |
1272 | if (rc) |
1273 | goto fail_init_state_uninitialized; | |
1da177e4 LT |
1274 | /* Register reboot handler */ |
1275 | rc = register_reboot_notifier(&sclp_reboot_notifier); | |
62b74942 MH |
1276 | if (rc) |
1277 | goto fail_init_state_uninitialized; | |
1da177e4 | 1278 | /* Register interrupt handler */ |
1dad093b | 1279 | rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_interrupt_handler); |
62b74942 MH |
1280 | if (rc) |
1281 | goto fail_unregister_reboot_notifier; | |
1da177e4 LT |
1282 | sclp_init_state = sclp_init_state_initialized; |
1283 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1284 | /* Enable service-signal external interruption - needs to happen with | |
1285 | * IRQs enabled. */ | |
82003c3e | 1286 | irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL); |
1da177e4 LT |
1287 | sclp_init_mask(1); |
1288 | return 0; | |
62b74942 MH |
1289 | |
1290 | fail_unregister_reboot_notifier: | |
1291 | unregister_reboot_notifier(&sclp_reboot_notifier); | |
1292 | fail_init_state_uninitialized: | |
1293 | sclp_init_state = sclp_init_state_uninitialized; | |
087c4d74 GS |
1294 | free_page((unsigned long) sclp_read_sccb); |
1295 | free_page((unsigned long) sclp_init_sccb); | |
62b74942 MH |
1296 | fail_unlock: |
1297 | spin_unlock_irqrestore(&sclp_lock, flags); | |
1298 | return rc; | |
1da177e4 | 1299 | } |
b3d00c3b PO |
1300 | |
1301 | static __init int sclp_initcall(void) | |
1302 | { | |
62b74942 MH |
1303 | int rc; |
1304 | ||
1305 | rc = platform_driver_register(&sclp_pdrv); | |
1306 | if (rc) | |
1307 | return rc; | |
25b41a7b | 1308 | |
b3d00c3b PO |
1309 | return sclp_init(); |
1310 | } | |
1311 | ||
1312 | arch_initcall(sclp_initcall); |