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