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