[PATCH] s390: fix sclp memory corruption in tty pages list
[linux-2.6/verdex.git] / drivers / s390 / char / sclp.c
blob4138564402b8c2ee27b3bf04731bebe1c3caf35e
1 /*
2 * drivers/s390/char/sclp.c
3 * core function to access sclp interface
5 * S390 version
6 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
9 */
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/timer.h>
16 #include <linux/reboot.h>
17 #include <linux/jiffies.h>
18 #include <asm/types.h>
19 #include <asm/s390_ext.h>
21 #include "sclp.h"
23 #define SCLP_HEADER "sclp: "
25 /* Structure for register_early_external_interrupt. */
26 static ext_int_info_t ext_int_info_hwc;
28 /* Lock to protect internal data consistency. */
29 static DEFINE_SPINLOCK(sclp_lock);
31 /* Mask of events that we can receive from the sclp interface. */
32 static sccb_mask_t sclp_receive_mask;
34 /* Mask of events that we can send to the sclp interface. */
35 static sccb_mask_t sclp_send_mask;
37 /* List of registered event listeners and senders. */
38 static struct list_head sclp_reg_list;
40 /* List of queued requests. */
41 static struct list_head sclp_req_queue;
43 /* Data for read and and init requests. */
44 static struct sclp_req sclp_read_req;
45 static struct sclp_req sclp_init_req;
46 static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
47 static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
49 /* Timer for request retries. */
50 static struct timer_list sclp_request_timer;
52 /* Internal state: is the driver initialized? */
53 static volatile enum sclp_init_state_t {
54 sclp_init_state_uninitialized,
55 sclp_init_state_initializing,
56 sclp_init_state_initialized
57 } sclp_init_state = sclp_init_state_uninitialized;
59 /* Internal state: is a request active at the sclp? */
60 static volatile enum sclp_running_state_t {
61 sclp_running_state_idle,
62 sclp_running_state_running
63 } sclp_running_state = sclp_running_state_idle;
65 /* Internal state: is a read request pending? */
66 static volatile enum sclp_reading_state_t {
67 sclp_reading_state_idle,
68 sclp_reading_state_reading
69 } sclp_reading_state = sclp_reading_state_idle;
71 /* Internal state: is the driver currently serving requests? */
72 static volatile enum sclp_activation_state_t {
73 sclp_activation_state_active,
74 sclp_activation_state_deactivating,
75 sclp_activation_state_inactive,
76 sclp_activation_state_activating
77 } sclp_activation_state = sclp_activation_state_active;
79 /* Internal state: is an init mask request pending? */
80 static volatile enum sclp_mask_state_t {
81 sclp_mask_state_idle,
82 sclp_mask_state_initializing
83 } sclp_mask_state = sclp_mask_state_idle;
85 /* Maximum retry counts */
86 #define SCLP_INIT_RETRY 3
87 #define SCLP_MASK_RETRY 3
89 /* Timeout intervals in seconds.*/
90 #define SCLP_BUSY_INTERVAL 10
91 #define SCLP_RETRY_INTERVAL 15
93 static void sclp_process_queue(void);
94 static int sclp_init_mask(int calculate);
95 static int sclp_init(void);
97 /* Perform service call. Return 0 on success, non-zero otherwise. */
98 static int
99 service_call(sclp_cmdw_t command, void *sccb)
101 int cc;
103 __asm__ __volatile__(
104 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
105 " ipm %0\n"
106 " srl %0,28"
107 : "=&d" (cc)
108 : "d" (command), "a" (__pa(sccb))
109 : "cc", "memory" );
110 if (cc == 3)
111 return -EIO;
112 if (cc == 2)
113 return -EBUSY;
114 return 0;
117 /* Request timeout handler. Restart the request queue. If DATA is non-zero,
118 * force restart of running request. */
119 static void
120 sclp_request_timeout(unsigned long data)
122 unsigned long flags;
124 if (data) {
125 spin_lock_irqsave(&sclp_lock, flags);
126 sclp_running_state = sclp_running_state_idle;
127 spin_unlock_irqrestore(&sclp_lock, flags);
129 sclp_process_queue();
132 /* Set up request retry timer. Called while sclp_lock is locked. */
133 static inline void
134 __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
135 unsigned long data)
137 del_timer(&sclp_request_timer);
138 sclp_request_timer.function = function;
139 sclp_request_timer.data = data;
140 sclp_request_timer.expires = jiffies + time;
141 add_timer(&sclp_request_timer);
144 /* Try to start a request. Return zero if the request was successfully
145 * started or if it will be started at a later time. Return non-zero otherwise.
146 * Called while sclp_lock is locked. */
147 static int
148 __sclp_start_request(struct sclp_req *req)
150 int rc;
152 if (sclp_running_state != sclp_running_state_idle)
153 return 0;
154 del_timer(&sclp_request_timer);
155 rc = service_call(req->command, req->sccb);
156 req->start_count++;
158 if (rc == 0) {
159 /* Sucessfully started request */
160 req->status = SCLP_REQ_RUNNING;
161 sclp_running_state = sclp_running_state_running;
162 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
163 sclp_request_timeout, 1);
164 return 0;
165 } else if (rc == -EBUSY) {
166 /* Try again later */
167 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
168 sclp_request_timeout, 0);
169 return 0;
171 /* Request failed */
172 req->status = SCLP_REQ_FAILED;
173 return rc;
176 /* Try to start queued requests. */
177 static void
178 sclp_process_queue(void)
180 struct sclp_req *req;
181 int rc;
182 unsigned long flags;
184 spin_lock_irqsave(&sclp_lock, flags);
185 if (sclp_running_state != sclp_running_state_idle) {
186 spin_unlock_irqrestore(&sclp_lock, flags);
187 return;
189 del_timer(&sclp_request_timer);
190 while (!list_empty(&sclp_req_queue)) {
191 req = list_entry(sclp_req_queue.next, struct sclp_req, list);
192 rc = __sclp_start_request(req);
193 if (rc == 0)
194 break;
195 /* Request failed. */
196 list_del(&req->list);
197 if (req->callback) {
198 spin_unlock_irqrestore(&sclp_lock, flags);
199 req->callback(req, req->callback_data);
200 spin_lock_irqsave(&sclp_lock, flags);
203 spin_unlock_irqrestore(&sclp_lock, flags);
206 /* Queue a new request. Return zero on success, non-zero otherwise. */
208 sclp_add_request(struct sclp_req *req)
210 unsigned long flags;
211 int rc;
213 spin_lock_irqsave(&sclp_lock, flags);
214 if ((sclp_init_state != sclp_init_state_initialized ||
215 sclp_activation_state != sclp_activation_state_active) &&
216 req != &sclp_init_req) {
217 spin_unlock_irqrestore(&sclp_lock, flags);
218 return -EIO;
220 req->status = SCLP_REQ_QUEUED;
221 req->start_count = 0;
222 list_add_tail(&req->list, &sclp_req_queue);
223 rc = 0;
224 /* Start if request is first in list */
225 if (req->list.prev == &sclp_req_queue) {
226 rc = __sclp_start_request(req);
227 if (rc)
228 list_del(&req->list);
230 spin_unlock_irqrestore(&sclp_lock, flags);
231 return rc;
234 EXPORT_SYMBOL(sclp_add_request);
236 /* Dispatch events found in request buffer to registered listeners. Return 0
237 * if all events were dispatched, non-zero otherwise. */
238 static int
239 sclp_dispatch_evbufs(struct sccb_header *sccb)
241 unsigned long flags;
242 struct evbuf_header *evbuf;
243 struct list_head *l;
244 struct sclp_register *reg;
245 int offset;
246 int rc;
248 spin_lock_irqsave(&sclp_lock, flags);
249 rc = 0;
250 for (offset = sizeof(struct sccb_header); offset < sccb->length;
251 offset += evbuf->length) {
252 /* Search for event handler */
253 evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
254 reg = NULL;
255 list_for_each(l, &sclp_reg_list) {
256 reg = list_entry(l, struct sclp_register, list);
257 if (reg->receive_mask & (1 << (32 - evbuf->type)))
258 break;
259 else
260 reg = NULL;
262 if (reg && reg->receiver_fn) {
263 spin_unlock_irqrestore(&sclp_lock, flags);
264 reg->receiver_fn(evbuf);
265 spin_lock_irqsave(&sclp_lock, flags);
266 } else if (reg == NULL)
267 rc = -ENOSYS;
269 spin_unlock_irqrestore(&sclp_lock, flags);
270 return rc;
273 /* Read event data request callback. */
274 static void
275 sclp_read_cb(struct sclp_req *req, void *data)
277 unsigned long flags;
278 struct sccb_header *sccb;
280 sccb = (struct sccb_header *) req->sccb;
281 if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
282 sccb->response_code == 0x220))
283 sclp_dispatch_evbufs(sccb);
284 spin_lock_irqsave(&sclp_lock, flags);
285 sclp_reading_state = sclp_reading_state_idle;
286 spin_unlock_irqrestore(&sclp_lock, flags);
289 /* Prepare read event data request. Called while sclp_lock is locked. */
290 static inline void
291 __sclp_make_read_req(void)
293 struct sccb_header *sccb;
295 sccb = (struct sccb_header *) sclp_read_sccb;
296 clear_page(sccb);
297 memset(&sclp_read_req, 0, sizeof(struct sclp_req));
298 sclp_read_req.command = SCLP_CMDW_READDATA;
299 sclp_read_req.status = SCLP_REQ_QUEUED;
300 sclp_read_req.start_count = 0;
301 sclp_read_req.callback = sclp_read_cb;
302 sclp_read_req.sccb = sccb;
303 sccb->length = PAGE_SIZE;
304 sccb->function_code = 0;
305 sccb->control_mask[2] = 0x80;
308 /* Search request list for request with matching sccb. Return request if found,
309 * NULL otherwise. Called while sclp_lock is locked. */
310 static inline struct sclp_req *
311 __sclp_find_req(u32 sccb)
313 struct list_head *l;
314 struct sclp_req *req;
316 list_for_each(l, &sclp_req_queue) {
317 req = list_entry(l, struct sclp_req, list);
318 if (sccb == (u32) (addr_t) req->sccb)
319 return req;
321 return NULL;
324 /* Handler for external interruption. Perform request post-processing.
325 * Prepare read event data request if necessary. Start processing of next
326 * request on queue. */
327 static void
328 sclp_interrupt_handler(struct pt_regs *regs, __u16 code)
330 struct sclp_req *req;
331 u32 finished_sccb;
332 u32 evbuf_pending;
334 spin_lock(&sclp_lock);
335 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
336 evbuf_pending = S390_lowcore.ext_params & 0x3;
337 if (finished_sccb) {
338 req = __sclp_find_req(finished_sccb);
339 if (req) {
340 /* Request post-processing */
341 list_del(&req->list);
342 req->status = SCLP_REQ_DONE;
343 if (req->callback) {
344 spin_unlock(&sclp_lock);
345 req->callback(req, req->callback_data);
346 spin_lock(&sclp_lock);
349 sclp_running_state = sclp_running_state_idle;
351 if (evbuf_pending && sclp_receive_mask != 0 &&
352 sclp_reading_state == sclp_reading_state_idle &&
353 sclp_activation_state == sclp_activation_state_active ) {
354 sclp_reading_state = sclp_reading_state_reading;
355 __sclp_make_read_req();
356 /* Add request to head of queue */
357 list_add(&sclp_read_req.list, &sclp_req_queue);
359 spin_unlock(&sclp_lock);
360 sclp_process_queue();
363 /* Return current Time-Of-Day clock. */
364 static inline u64
365 sclp_get_clock(void)
367 u64 result;
369 asm volatile ("STCK 0(%1)" : "=m" (result) : "a" (&(result)) : "cc");
370 return result;
373 /* Convert interval in jiffies to TOD ticks. */
374 static inline u64
375 sclp_tod_from_jiffies(unsigned long jiffies)
377 return (u64) (jiffies / HZ) << 32;
380 /* Wait until a currently running request finished. Note: while this function
381 * is running, no timers are served on the calling CPU. */
382 void
383 sclp_sync_wait(void)
385 unsigned long psw_mask;
386 unsigned long cr0, cr0_sync;
387 u64 timeout;
389 /* We'll be disabling timer interrupts, so we need a custom timeout
390 * mechanism */
391 timeout = 0;
392 if (timer_pending(&sclp_request_timer)) {
393 /* Get timeout TOD value */
394 timeout = sclp_get_clock() +
395 sclp_tod_from_jiffies(sclp_request_timer.expires -
396 jiffies);
398 /* Prevent bottom half from executing once we force interrupts open */
399 local_bh_disable();
400 /* Enable service-signal interruption, disable timer interrupts */
401 __ctl_store(cr0, 0, 0);
402 cr0_sync = cr0;
403 cr0_sync |= 0x00000200;
404 cr0_sync &= 0xFFFFF3AC;
405 __ctl_load(cr0_sync, 0, 0);
406 asm volatile ("STOSM 0(%1),0x01"
407 : "=m" (psw_mask) : "a" (&psw_mask) : "memory");
408 /* Loop until driver state indicates finished request */
409 while (sclp_running_state != sclp_running_state_idle) {
410 /* Check for expired request timer */
411 if (timer_pending(&sclp_request_timer) &&
412 sclp_get_clock() > timeout &&
413 del_timer(&sclp_request_timer))
414 sclp_request_timer.function(sclp_request_timer.data);
415 barrier();
416 cpu_relax();
418 /* Restore interrupt settings */
419 asm volatile ("SSM 0(%0)"
420 : : "a" (&psw_mask) : "memory");
421 __ctl_load(cr0, 0, 0);
422 __local_bh_enable();
425 EXPORT_SYMBOL(sclp_sync_wait);
427 /* Dispatch changes in send and receive mask to registered listeners. */
428 static inline void
429 sclp_dispatch_state_change(void)
431 struct list_head *l;
432 struct sclp_register *reg;
433 unsigned long flags;
434 sccb_mask_t receive_mask;
435 sccb_mask_t send_mask;
437 do {
438 spin_lock_irqsave(&sclp_lock, flags);
439 reg = NULL;
440 list_for_each(l, &sclp_reg_list) {
441 reg = list_entry(l, struct sclp_register, list);
442 receive_mask = reg->receive_mask & sclp_receive_mask;
443 send_mask = reg->send_mask & sclp_send_mask;
444 if (reg->sclp_receive_mask != receive_mask ||
445 reg->sclp_send_mask != send_mask) {
446 reg->sclp_receive_mask = receive_mask;
447 reg->sclp_send_mask = send_mask;
448 break;
449 } else
450 reg = NULL;
452 spin_unlock_irqrestore(&sclp_lock, flags);
453 if (reg && reg->state_change_fn)
454 reg->state_change_fn(reg);
455 } while (reg);
458 struct sclp_statechangebuf {
459 struct evbuf_header header;
460 u8 validity_sclp_active_facility_mask : 1;
461 u8 validity_sclp_receive_mask : 1;
462 u8 validity_sclp_send_mask : 1;
463 u8 validity_read_data_function_mask : 1;
464 u16 _zeros : 12;
465 u16 mask_length;
466 u64 sclp_active_facility_mask;
467 sccb_mask_t sclp_receive_mask;
468 sccb_mask_t sclp_send_mask;
469 u32 read_data_function_mask;
470 } __attribute__((packed));
473 /* State change event callback. Inform listeners of changes. */
474 static void
475 sclp_state_change_cb(struct evbuf_header *evbuf)
477 unsigned long flags;
478 struct sclp_statechangebuf *scbuf;
480 scbuf = (struct sclp_statechangebuf *) evbuf;
481 if (scbuf->mask_length != sizeof(sccb_mask_t))
482 return;
483 spin_lock_irqsave(&sclp_lock, flags);
484 if (scbuf->validity_sclp_receive_mask)
485 sclp_receive_mask = scbuf->sclp_receive_mask;
486 if (scbuf->validity_sclp_send_mask)
487 sclp_send_mask = scbuf->sclp_send_mask;
488 spin_unlock_irqrestore(&sclp_lock, flags);
489 sclp_dispatch_state_change();
492 static struct sclp_register sclp_state_change_event = {
493 .receive_mask = EvTyp_StateChange_Mask,
494 .receiver_fn = sclp_state_change_cb
497 /* Calculate receive and send mask of currently registered listeners.
498 * Called while sclp_lock is locked. */
499 static inline void
500 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
502 struct list_head *l;
503 struct sclp_register *t;
505 *receive_mask = 0;
506 *send_mask = 0;
507 list_for_each(l, &sclp_reg_list) {
508 t = list_entry(l, struct sclp_register, list);
509 *receive_mask |= t->receive_mask;
510 *send_mask |= t->send_mask;
514 /* Register event listener. Return 0 on success, non-zero otherwise. */
516 sclp_register(struct sclp_register *reg)
518 unsigned long flags;
519 sccb_mask_t receive_mask;
520 sccb_mask_t send_mask;
521 int rc;
523 rc = sclp_init();
524 if (rc)
525 return rc;
526 spin_lock_irqsave(&sclp_lock, flags);
527 /* Check event mask for collisions */
528 __sclp_get_mask(&receive_mask, &send_mask);
529 if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
530 spin_unlock_irqrestore(&sclp_lock, flags);
531 return -EBUSY;
533 /* Trigger initial state change callback */
534 reg->sclp_receive_mask = 0;
535 reg->sclp_send_mask = 0;
536 list_add(&reg->list, &sclp_reg_list);
537 spin_unlock_irqrestore(&sclp_lock, flags);
538 rc = sclp_init_mask(1);
539 if (rc) {
540 spin_lock_irqsave(&sclp_lock, flags);
541 list_del(&reg->list);
542 spin_unlock_irqrestore(&sclp_lock, flags);
544 return rc;
547 EXPORT_SYMBOL(sclp_register);
549 /* Unregister event listener. */
550 void
551 sclp_unregister(struct sclp_register *reg)
553 unsigned long flags;
555 spin_lock_irqsave(&sclp_lock, flags);
556 list_del(&reg->list);
557 spin_unlock_irqrestore(&sclp_lock, flags);
558 sclp_init_mask(1);
561 EXPORT_SYMBOL(sclp_unregister);
563 /* Remove event buffers which are marked processed. Return the number of
564 * remaining event buffers. */
566 sclp_remove_processed(struct sccb_header *sccb)
568 struct evbuf_header *evbuf;
569 int unprocessed;
570 u16 remaining;
572 evbuf = (struct evbuf_header *) (sccb + 1);
573 unprocessed = 0;
574 remaining = sccb->length - sizeof(struct sccb_header);
575 while (remaining > 0) {
576 remaining -= evbuf->length;
577 if (evbuf->flags & 0x80) {
578 sccb->length -= evbuf->length;
579 memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
580 remaining);
581 } else {
582 unprocessed++;
583 evbuf = (struct evbuf_header *)
584 ((addr_t) evbuf + evbuf->length);
587 return unprocessed;
590 EXPORT_SYMBOL(sclp_remove_processed);
592 struct init_sccb {
593 struct sccb_header header;
594 u16 _reserved;
595 u16 mask_length;
596 sccb_mask_t receive_mask;
597 sccb_mask_t send_mask;
598 sccb_mask_t sclp_send_mask;
599 sccb_mask_t sclp_receive_mask;
600 } __attribute__((packed));
602 /* Prepare init mask request. Called while sclp_lock is locked. */
603 static inline void
604 __sclp_make_init_req(u32 receive_mask, u32 send_mask)
606 struct init_sccb *sccb;
608 sccb = (struct init_sccb *) sclp_init_sccb;
609 clear_page(sccb);
610 memset(&sclp_init_req, 0, sizeof(struct sclp_req));
611 sclp_init_req.command = SCLP_CMDW_WRITEMASK;
612 sclp_init_req.status = SCLP_REQ_FILLED;
613 sclp_init_req.start_count = 0;
614 sclp_init_req.callback = NULL;
615 sclp_init_req.callback_data = NULL;
616 sclp_init_req.sccb = sccb;
617 sccb->header.length = sizeof(struct init_sccb);
618 sccb->mask_length = sizeof(sccb_mask_t);
619 sccb->receive_mask = receive_mask;
620 sccb->send_mask = send_mask;
621 sccb->sclp_receive_mask = 0;
622 sccb->sclp_send_mask = 0;
625 /* Start init mask request. If calculate is non-zero, calculate the mask as
626 * requested by registered listeners. Use zero mask otherwise. Return 0 on
627 * success, non-zero otherwise. */
628 static int
629 sclp_init_mask(int calculate)
631 unsigned long flags;
632 struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
633 sccb_mask_t receive_mask;
634 sccb_mask_t send_mask;
635 int retry;
636 int rc;
637 unsigned long wait;
639 spin_lock_irqsave(&sclp_lock, flags);
640 /* Check if interface is in appropriate state */
641 if (sclp_mask_state != sclp_mask_state_idle) {
642 spin_unlock_irqrestore(&sclp_lock, flags);
643 return -EBUSY;
645 if (sclp_activation_state == sclp_activation_state_inactive) {
646 spin_unlock_irqrestore(&sclp_lock, flags);
647 return -EINVAL;
649 sclp_mask_state = sclp_mask_state_initializing;
650 /* Determine mask */
651 if (calculate)
652 __sclp_get_mask(&receive_mask, &send_mask);
653 else {
654 receive_mask = 0;
655 send_mask = 0;
657 rc = -EIO;
658 for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
659 /* Prepare request */
660 __sclp_make_init_req(receive_mask, send_mask);
661 spin_unlock_irqrestore(&sclp_lock, flags);
662 if (sclp_add_request(&sclp_init_req)) {
663 /* Try again later */
664 wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
665 while (time_before(jiffies, wait))
666 sclp_sync_wait();
667 spin_lock_irqsave(&sclp_lock, flags);
668 continue;
670 while (sclp_init_req.status != SCLP_REQ_DONE &&
671 sclp_init_req.status != SCLP_REQ_FAILED)
672 sclp_sync_wait();
673 spin_lock_irqsave(&sclp_lock, flags);
674 if (sclp_init_req.status == SCLP_REQ_DONE &&
675 sccb->header.response_code == 0x20) {
676 /* Successful request */
677 if (calculate) {
678 sclp_receive_mask = sccb->sclp_receive_mask;
679 sclp_send_mask = sccb->sclp_send_mask;
680 } else {
681 sclp_receive_mask = 0;
682 sclp_send_mask = 0;
684 spin_unlock_irqrestore(&sclp_lock, flags);
685 sclp_dispatch_state_change();
686 spin_lock_irqsave(&sclp_lock, flags);
687 rc = 0;
688 break;
691 sclp_mask_state = sclp_mask_state_idle;
692 spin_unlock_irqrestore(&sclp_lock, flags);
693 return rc;
696 /* Deactivate SCLP interface. On success, new requests will be rejected,
697 * events will no longer be dispatched. Return 0 on success, non-zero
698 * otherwise. */
700 sclp_deactivate(void)
702 unsigned long flags;
703 int rc;
705 spin_lock_irqsave(&sclp_lock, flags);
706 /* Deactivate can only be called when active */
707 if (sclp_activation_state != sclp_activation_state_active) {
708 spin_unlock_irqrestore(&sclp_lock, flags);
709 return -EINVAL;
711 sclp_activation_state = sclp_activation_state_deactivating;
712 spin_unlock_irqrestore(&sclp_lock, flags);
713 rc = sclp_init_mask(0);
714 spin_lock_irqsave(&sclp_lock, flags);
715 if (rc == 0)
716 sclp_activation_state = sclp_activation_state_inactive;
717 else
718 sclp_activation_state = sclp_activation_state_active;
719 spin_unlock_irqrestore(&sclp_lock, flags);
720 return rc;
723 EXPORT_SYMBOL(sclp_deactivate);
725 /* Reactivate SCLP interface after sclp_deactivate. On success, new
726 * requests will be accepted, events will be dispatched again. Return 0 on
727 * success, non-zero otherwise. */
729 sclp_reactivate(void)
731 unsigned long flags;
732 int rc;
734 spin_lock_irqsave(&sclp_lock, flags);
735 /* Reactivate can only be called when inactive */
736 if (sclp_activation_state != sclp_activation_state_inactive) {
737 spin_unlock_irqrestore(&sclp_lock, flags);
738 return -EINVAL;
740 sclp_activation_state = sclp_activation_state_activating;
741 spin_unlock_irqrestore(&sclp_lock, flags);
742 rc = sclp_init_mask(1);
743 spin_lock_irqsave(&sclp_lock, flags);
744 if (rc == 0)
745 sclp_activation_state = sclp_activation_state_active;
746 else
747 sclp_activation_state = sclp_activation_state_inactive;
748 spin_unlock_irqrestore(&sclp_lock, flags);
749 return rc;
752 EXPORT_SYMBOL(sclp_reactivate);
754 /* Handler for external interruption used during initialization. Modify
755 * request state to done. */
756 static void
757 sclp_check_handler(struct pt_regs *regs, __u16 code)
759 u32 finished_sccb;
761 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
762 /* Is this the interrupt we are waiting for? */
763 if (finished_sccb == 0)
764 return;
765 if (finished_sccb != (u32) (addr_t) sclp_init_sccb) {
766 printk(KERN_WARNING SCLP_HEADER "unsolicited interrupt "
767 "for buffer at 0x%x\n", finished_sccb);
768 return;
770 spin_lock(&sclp_lock);
771 if (sclp_running_state == sclp_running_state_running) {
772 sclp_init_req.status = SCLP_REQ_DONE;
773 sclp_running_state = sclp_running_state_idle;
775 spin_unlock(&sclp_lock);
778 /* Initial init mask request timed out. Modify request state to failed. */
779 static void
780 sclp_check_timeout(unsigned long data)
782 unsigned long flags;
784 spin_lock_irqsave(&sclp_lock, flags);
785 if (sclp_running_state == sclp_running_state_running) {
786 sclp_init_req.status = SCLP_REQ_FAILED;
787 sclp_running_state = sclp_running_state_idle;
789 spin_unlock_irqrestore(&sclp_lock, flags);
792 /* Perform a check of the SCLP interface. Return zero if the interface is
793 * available and there are no pending requests from a previous instance.
794 * Return non-zero otherwise. */
795 static int
796 sclp_check_interface(void)
798 struct init_sccb *sccb;
799 unsigned long flags;
800 int retry;
801 int rc;
803 spin_lock_irqsave(&sclp_lock, flags);
804 /* Prepare init mask command */
805 rc = register_early_external_interrupt(0x2401, sclp_check_handler,
806 &ext_int_info_hwc);
807 if (rc) {
808 spin_unlock_irqrestore(&sclp_lock, flags);
809 return rc;
811 for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
812 __sclp_make_init_req(0, 0);
813 sccb = (struct init_sccb *) sclp_init_req.sccb;
814 rc = service_call(sclp_init_req.command, sccb);
815 if (rc == -EIO)
816 break;
817 sclp_init_req.status = SCLP_REQ_RUNNING;
818 sclp_running_state = sclp_running_state_running;
819 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
820 sclp_check_timeout, 0);
821 spin_unlock_irqrestore(&sclp_lock, flags);
822 /* Enable service-signal interruption - needs to happen
823 * with IRQs enabled. */
824 ctl_set_bit(0, 9);
825 /* Wait for signal from interrupt or timeout */
826 sclp_sync_wait();
827 /* Disable service-signal interruption - needs to happen
828 * with IRQs enabled. */
829 ctl_clear_bit(0,9);
830 spin_lock_irqsave(&sclp_lock, flags);
831 del_timer(&sclp_request_timer);
832 if (sclp_init_req.status == SCLP_REQ_DONE &&
833 sccb->header.response_code == 0x20) {
834 rc = 0;
835 break;
836 } else
837 rc = -EBUSY;
839 unregister_early_external_interrupt(0x2401, sclp_check_handler,
840 &ext_int_info_hwc);
841 spin_unlock_irqrestore(&sclp_lock, flags);
842 return rc;
845 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
846 * events from interfering with rebooted system. */
847 static int
848 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
850 sclp_deactivate();
851 return NOTIFY_DONE;
854 static struct notifier_block sclp_reboot_notifier = {
855 .notifier_call = sclp_reboot_event
858 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
859 * otherwise. */
860 static int
861 sclp_init(void)
863 unsigned long flags;
864 int rc;
866 if (!MACHINE_HAS_SCLP)
867 return -ENODEV;
868 spin_lock_irqsave(&sclp_lock, flags);
869 /* Check for previous or running initialization */
870 if (sclp_init_state != sclp_init_state_uninitialized) {
871 spin_unlock_irqrestore(&sclp_lock, flags);
872 return 0;
874 sclp_init_state = sclp_init_state_initializing;
875 /* Set up variables */
876 INIT_LIST_HEAD(&sclp_req_queue);
877 INIT_LIST_HEAD(&sclp_reg_list);
878 list_add(&sclp_state_change_event.list, &sclp_reg_list);
879 init_timer(&sclp_request_timer);
880 /* Check interface */
881 spin_unlock_irqrestore(&sclp_lock, flags);
882 rc = sclp_check_interface();
883 spin_lock_irqsave(&sclp_lock, flags);
884 if (rc) {
885 sclp_init_state = sclp_init_state_uninitialized;
886 spin_unlock_irqrestore(&sclp_lock, flags);
887 return rc;
889 /* Register reboot handler */
890 rc = register_reboot_notifier(&sclp_reboot_notifier);
891 if (rc) {
892 sclp_init_state = sclp_init_state_uninitialized;
893 spin_unlock_irqrestore(&sclp_lock, flags);
894 return rc;
896 /* Register interrupt handler */
897 rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
898 &ext_int_info_hwc);
899 if (rc) {
900 unregister_reboot_notifier(&sclp_reboot_notifier);
901 sclp_init_state = sclp_init_state_uninitialized;
902 spin_unlock_irqrestore(&sclp_lock, flags);
903 return rc;
905 sclp_init_state = sclp_init_state_initialized;
906 spin_unlock_irqrestore(&sclp_lock, flags);
907 /* Enable service-signal external interruption - needs to happen with
908 * IRQs enabled. */
909 ctl_set_bit(0, 9);
910 sclp_init_mask(1);
911 return 0;