Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / s390 / char / sclp.c
blobfa62e69440578375fb8028721a6e2c8c0a52ad96
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 <linux/init.h>
19 #include <asm/types.h>
20 #include <asm/s390_ext.h>
22 #include "sclp.h"
24 #define SCLP_HEADER "sclp: "
26 /* Structure for register_early_external_interrupt. */
27 static ext_int_info_t ext_int_info_hwc;
29 /* Lock to protect internal data consistency. */
30 static DEFINE_SPINLOCK(sclp_lock);
32 /* Mask of events that we can receive from the sclp interface. */
33 static sccb_mask_t sclp_receive_mask;
35 /* Mask of events that we can send to the sclp interface. */
36 static sccb_mask_t sclp_send_mask;
38 /* List of registered event listeners and senders. */
39 static struct list_head sclp_reg_list;
41 /* List of queued requests. */
42 static struct list_head sclp_req_queue;
44 /* Data for read and and init requests. */
45 static struct sclp_req sclp_read_req;
46 static struct sclp_req sclp_init_req;
47 static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
48 static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
50 /* Timer for request retries. */
51 static struct timer_list sclp_request_timer;
53 /* Internal state: is the driver initialized? */
54 static volatile enum sclp_init_state_t {
55 sclp_init_state_uninitialized,
56 sclp_init_state_initializing,
57 sclp_init_state_initialized
58 } sclp_init_state = sclp_init_state_uninitialized;
60 /* Internal state: is a request active at the sclp? */
61 static volatile enum sclp_running_state_t {
62 sclp_running_state_idle,
63 sclp_running_state_running,
64 sclp_running_state_reset_pending
65 } sclp_running_state = sclp_running_state_idle;
67 /* Internal state: is a read request pending? */
68 static volatile enum sclp_reading_state_t {
69 sclp_reading_state_idle,
70 sclp_reading_state_reading
71 } sclp_reading_state = sclp_reading_state_idle;
73 /* Internal state: is the driver currently serving requests? */
74 static volatile enum sclp_activation_state_t {
75 sclp_activation_state_active,
76 sclp_activation_state_deactivating,
77 sclp_activation_state_inactive,
78 sclp_activation_state_activating
79 } sclp_activation_state = sclp_activation_state_active;
81 /* Internal state: is an init mask request pending? */
82 static volatile enum sclp_mask_state_t {
83 sclp_mask_state_idle,
84 sclp_mask_state_initializing
85 } sclp_mask_state = sclp_mask_state_idle;
87 /* Maximum retry counts */
88 #define SCLP_INIT_RETRY 3
89 #define SCLP_MASK_RETRY 3
91 /* Timeout intervals in seconds.*/
92 #define SCLP_BUSY_INTERVAL 10
93 #define SCLP_RETRY_INTERVAL 30
95 static void sclp_process_queue(void);
96 static int sclp_init_mask(int calculate);
97 static int sclp_init(void);
99 /* Perform service call. Return 0 on success, non-zero otherwise. */
101 sclp_service_call(sclp_cmdw_t command, void *sccb)
103 int cc;
105 asm volatile(
106 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
107 " ipm %0\n"
108 " srl %0,28"
109 : "=&d" (cc) : "d" (command), "a" (__pa(sccb))
110 : "cc", "memory");
111 if (cc == 3)
112 return -EIO;
113 if (cc == 2)
114 return -EBUSY;
115 return 0;
118 static inline void __sclp_make_read_req(void);
120 static void
121 __sclp_queue_read_req(void)
123 if (sclp_reading_state == sclp_reading_state_idle) {
124 sclp_reading_state = sclp_reading_state_reading;
125 __sclp_make_read_req();
126 /* Add request to head of queue */
127 list_add(&sclp_read_req.list, &sclp_req_queue);
131 /* Set up request retry timer. Called while sclp_lock is locked. */
132 static inline void
133 __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
134 unsigned long data)
136 del_timer(&sclp_request_timer);
137 sclp_request_timer.function = function;
138 sclp_request_timer.data = data;
139 sclp_request_timer.expires = jiffies + time;
140 add_timer(&sclp_request_timer);
143 /* Request timeout handler. Restart the request queue. If DATA is non-zero,
144 * force restart of running request. */
145 static void
146 sclp_request_timeout(unsigned long data)
148 unsigned long flags;
150 spin_lock_irqsave(&sclp_lock, flags);
151 if (data) {
152 if (sclp_running_state == sclp_running_state_running) {
153 /* Break running state and queue NOP read event request
154 * to get a defined interface state. */
155 __sclp_queue_read_req();
156 sclp_running_state = sclp_running_state_idle;
158 } else {
159 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
160 sclp_request_timeout, 0);
162 spin_unlock_irqrestore(&sclp_lock, flags);
163 sclp_process_queue();
166 /* Try to start a request. Return zero if the request was successfully
167 * started or if it will be started at a later time. Return non-zero otherwise.
168 * Called while sclp_lock is locked. */
169 static int
170 __sclp_start_request(struct sclp_req *req)
172 int rc;
174 if (sclp_running_state != sclp_running_state_idle)
175 return 0;
176 del_timer(&sclp_request_timer);
177 rc = sclp_service_call(req->command, req->sccb);
178 req->start_count++;
180 if (rc == 0) {
181 /* Sucessfully started request */
182 req->status = SCLP_REQ_RUNNING;
183 sclp_running_state = sclp_running_state_running;
184 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
185 sclp_request_timeout, 1);
186 return 0;
187 } else if (rc == -EBUSY) {
188 /* Try again later */
189 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
190 sclp_request_timeout, 0);
191 return 0;
193 /* Request failed */
194 req->status = SCLP_REQ_FAILED;
195 return rc;
198 /* Try to start queued requests. */
199 static void
200 sclp_process_queue(void)
202 struct sclp_req *req;
203 int rc;
204 unsigned long flags;
206 spin_lock_irqsave(&sclp_lock, flags);
207 if (sclp_running_state != sclp_running_state_idle) {
208 spin_unlock_irqrestore(&sclp_lock, flags);
209 return;
211 del_timer(&sclp_request_timer);
212 while (!list_empty(&sclp_req_queue)) {
213 req = list_entry(sclp_req_queue.next, struct sclp_req, list);
214 rc = __sclp_start_request(req);
215 if (rc == 0)
216 break;
217 /* Request failed */
218 if (req->start_count > 1) {
219 /* Cannot abort already submitted request - could still
220 * be active at the SCLP */
221 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
222 sclp_request_timeout, 0);
223 break;
225 /* Post-processing for aborted request */
226 list_del(&req->list);
227 if (req->callback) {
228 spin_unlock_irqrestore(&sclp_lock, flags);
229 req->callback(req, req->callback_data);
230 spin_lock_irqsave(&sclp_lock, flags);
233 spin_unlock_irqrestore(&sclp_lock, flags);
236 /* Queue a new request. Return zero on success, non-zero otherwise. */
238 sclp_add_request(struct sclp_req *req)
240 unsigned long flags;
241 int rc;
243 spin_lock_irqsave(&sclp_lock, flags);
244 if ((sclp_init_state != sclp_init_state_initialized ||
245 sclp_activation_state != sclp_activation_state_active) &&
246 req != &sclp_init_req) {
247 spin_unlock_irqrestore(&sclp_lock, flags);
248 return -EIO;
250 req->status = SCLP_REQ_QUEUED;
251 req->start_count = 0;
252 list_add_tail(&req->list, &sclp_req_queue);
253 rc = 0;
254 /* Start if request is first in list */
255 if (sclp_running_state == sclp_running_state_idle &&
256 req->list.prev == &sclp_req_queue) {
257 rc = __sclp_start_request(req);
258 if (rc)
259 list_del(&req->list);
261 spin_unlock_irqrestore(&sclp_lock, flags);
262 return rc;
265 EXPORT_SYMBOL(sclp_add_request);
267 /* Dispatch events found in request buffer to registered listeners. Return 0
268 * if all events were dispatched, non-zero otherwise. */
269 static int
270 sclp_dispatch_evbufs(struct sccb_header *sccb)
272 unsigned long flags;
273 struct evbuf_header *evbuf;
274 struct list_head *l;
275 struct sclp_register *reg;
276 int offset;
277 int rc;
279 spin_lock_irqsave(&sclp_lock, flags);
280 rc = 0;
281 for (offset = sizeof(struct sccb_header); offset < sccb->length;
282 offset += evbuf->length) {
283 /* Search for event handler */
284 evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
285 reg = NULL;
286 list_for_each(l, &sclp_reg_list) {
287 reg = list_entry(l, struct sclp_register, list);
288 if (reg->receive_mask & (1 << (32 - evbuf->type)))
289 break;
290 else
291 reg = NULL;
293 if (reg && reg->receiver_fn) {
294 spin_unlock_irqrestore(&sclp_lock, flags);
295 reg->receiver_fn(evbuf);
296 spin_lock_irqsave(&sclp_lock, flags);
297 } else if (reg == NULL)
298 rc = -ENOSYS;
300 spin_unlock_irqrestore(&sclp_lock, flags);
301 return rc;
304 /* Read event data request callback. */
305 static void
306 sclp_read_cb(struct sclp_req *req, void *data)
308 unsigned long flags;
309 struct sccb_header *sccb;
311 sccb = (struct sccb_header *) req->sccb;
312 if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
313 sccb->response_code == 0x220))
314 sclp_dispatch_evbufs(sccb);
315 spin_lock_irqsave(&sclp_lock, flags);
316 sclp_reading_state = sclp_reading_state_idle;
317 spin_unlock_irqrestore(&sclp_lock, flags);
320 /* Prepare read event data request. Called while sclp_lock is locked. */
321 static inline void
322 __sclp_make_read_req(void)
324 struct sccb_header *sccb;
326 sccb = (struct sccb_header *) sclp_read_sccb;
327 clear_page(sccb);
328 memset(&sclp_read_req, 0, sizeof(struct sclp_req));
329 sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
330 sclp_read_req.status = SCLP_REQ_QUEUED;
331 sclp_read_req.start_count = 0;
332 sclp_read_req.callback = sclp_read_cb;
333 sclp_read_req.sccb = sccb;
334 sccb->length = PAGE_SIZE;
335 sccb->function_code = 0;
336 sccb->control_mask[2] = 0x80;
339 /* Search request list for request with matching sccb. Return request if found,
340 * NULL otherwise. Called while sclp_lock is locked. */
341 static inline struct sclp_req *
342 __sclp_find_req(u32 sccb)
344 struct list_head *l;
345 struct sclp_req *req;
347 list_for_each(l, &sclp_req_queue) {
348 req = list_entry(l, struct sclp_req, list);
349 if (sccb == (u32) (addr_t) req->sccb)
350 return req;
352 return NULL;
355 /* Handler for external interruption. Perform request post-processing.
356 * Prepare read event data request if necessary. Start processing of next
357 * request on queue. */
358 static void
359 sclp_interrupt_handler(__u16 code)
361 struct sclp_req *req;
362 u32 finished_sccb;
363 u32 evbuf_pending;
365 spin_lock(&sclp_lock);
366 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
367 evbuf_pending = S390_lowcore.ext_params & 0x3;
368 if (finished_sccb) {
369 del_timer(&sclp_request_timer);
370 sclp_running_state = sclp_running_state_reset_pending;
371 req = __sclp_find_req(finished_sccb);
372 if (req) {
373 /* Request post-processing */
374 list_del(&req->list);
375 req->status = SCLP_REQ_DONE;
376 if (req->callback) {
377 spin_unlock(&sclp_lock);
378 req->callback(req, req->callback_data);
379 spin_lock(&sclp_lock);
382 sclp_running_state = sclp_running_state_idle;
384 if (evbuf_pending && sclp_receive_mask != 0 &&
385 sclp_activation_state == sclp_activation_state_active)
386 __sclp_queue_read_req();
387 spin_unlock(&sclp_lock);
388 sclp_process_queue();
391 /* Convert interval in jiffies to TOD ticks. */
392 static inline u64
393 sclp_tod_from_jiffies(unsigned long jiffies)
395 return (u64) (jiffies / HZ) << 32;
398 /* Wait until a currently running request finished. Note: while this function
399 * is running, no timers are served on the calling CPU. */
400 void
401 sclp_sync_wait(void)
403 unsigned long flags;
404 unsigned long cr0, cr0_sync;
405 u64 timeout;
406 int irq_context;
408 /* We'll be disabling timer interrupts, so we need a custom timeout
409 * mechanism */
410 timeout = 0;
411 if (timer_pending(&sclp_request_timer)) {
412 /* Get timeout TOD value */
413 timeout = get_clock() +
414 sclp_tod_from_jiffies(sclp_request_timer.expires -
415 jiffies);
417 local_irq_save(flags);
418 /* Prevent bottom half from executing once we force interrupts open */
419 irq_context = in_interrupt();
420 if (!irq_context)
421 local_bh_disable();
422 /* Enable service-signal interruption, disable timer interrupts */
423 trace_hardirqs_on();
424 __ctl_store(cr0, 0, 0);
425 cr0_sync = cr0;
426 cr0_sync |= 0x00000200;
427 cr0_sync &= 0xFFFFF3AC;
428 __ctl_load(cr0_sync, 0, 0);
429 __raw_local_irq_stosm(0x01);
430 /* Loop until driver state indicates finished request */
431 while (sclp_running_state != sclp_running_state_idle) {
432 /* Check for expired request timer */
433 if (timer_pending(&sclp_request_timer) &&
434 get_clock() > timeout &&
435 del_timer(&sclp_request_timer))
436 sclp_request_timer.function(sclp_request_timer.data);
437 cpu_relax();
439 local_irq_disable();
440 __ctl_load(cr0, 0, 0);
441 if (!irq_context)
442 _local_bh_enable();
443 local_irq_restore(flags);
446 EXPORT_SYMBOL(sclp_sync_wait);
448 /* Dispatch changes in send and receive mask to registered listeners. */
449 static void
450 sclp_dispatch_state_change(void)
452 struct list_head *l;
453 struct sclp_register *reg;
454 unsigned long flags;
455 sccb_mask_t receive_mask;
456 sccb_mask_t send_mask;
458 do {
459 spin_lock_irqsave(&sclp_lock, flags);
460 reg = NULL;
461 list_for_each(l, &sclp_reg_list) {
462 reg = list_entry(l, struct sclp_register, list);
463 receive_mask = reg->receive_mask & sclp_receive_mask;
464 send_mask = reg->send_mask & sclp_send_mask;
465 if (reg->sclp_receive_mask != receive_mask ||
466 reg->sclp_send_mask != send_mask) {
467 reg->sclp_receive_mask = receive_mask;
468 reg->sclp_send_mask = send_mask;
469 break;
470 } else
471 reg = NULL;
473 spin_unlock_irqrestore(&sclp_lock, flags);
474 if (reg && reg->state_change_fn)
475 reg->state_change_fn(reg);
476 } while (reg);
479 struct sclp_statechangebuf {
480 struct evbuf_header header;
481 u8 validity_sclp_active_facility_mask : 1;
482 u8 validity_sclp_receive_mask : 1;
483 u8 validity_sclp_send_mask : 1;
484 u8 validity_read_data_function_mask : 1;
485 u16 _zeros : 12;
486 u16 mask_length;
487 u64 sclp_active_facility_mask;
488 sccb_mask_t sclp_receive_mask;
489 sccb_mask_t sclp_send_mask;
490 u32 read_data_function_mask;
491 } __attribute__((packed));
494 /* State change event callback. Inform listeners of changes. */
495 static void
496 sclp_state_change_cb(struct evbuf_header *evbuf)
498 unsigned long flags;
499 struct sclp_statechangebuf *scbuf;
501 scbuf = (struct sclp_statechangebuf *) evbuf;
502 if (scbuf->mask_length != sizeof(sccb_mask_t))
503 return;
504 spin_lock_irqsave(&sclp_lock, flags);
505 if (scbuf->validity_sclp_receive_mask)
506 sclp_receive_mask = scbuf->sclp_receive_mask;
507 if (scbuf->validity_sclp_send_mask)
508 sclp_send_mask = scbuf->sclp_send_mask;
509 spin_unlock_irqrestore(&sclp_lock, flags);
510 sclp_dispatch_state_change();
513 static struct sclp_register sclp_state_change_event = {
514 .receive_mask = EVTYP_STATECHANGE_MASK,
515 .receiver_fn = sclp_state_change_cb
518 /* Calculate receive and send mask of currently registered listeners.
519 * Called while sclp_lock is locked. */
520 static inline void
521 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
523 struct list_head *l;
524 struct sclp_register *t;
526 *receive_mask = 0;
527 *send_mask = 0;
528 list_for_each(l, &sclp_reg_list) {
529 t = list_entry(l, struct sclp_register, list);
530 *receive_mask |= t->receive_mask;
531 *send_mask |= t->send_mask;
535 /* Register event listener. Return 0 on success, non-zero otherwise. */
537 sclp_register(struct sclp_register *reg)
539 unsigned long flags;
540 sccb_mask_t receive_mask;
541 sccb_mask_t send_mask;
542 int rc;
544 rc = sclp_init();
545 if (rc)
546 return rc;
547 spin_lock_irqsave(&sclp_lock, flags);
548 /* Check event mask for collisions */
549 __sclp_get_mask(&receive_mask, &send_mask);
550 if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
551 spin_unlock_irqrestore(&sclp_lock, flags);
552 return -EBUSY;
554 /* Trigger initial state change callback */
555 reg->sclp_receive_mask = 0;
556 reg->sclp_send_mask = 0;
557 list_add(&reg->list, &sclp_reg_list);
558 spin_unlock_irqrestore(&sclp_lock, flags);
559 rc = sclp_init_mask(1);
560 if (rc) {
561 spin_lock_irqsave(&sclp_lock, flags);
562 list_del(&reg->list);
563 spin_unlock_irqrestore(&sclp_lock, flags);
565 return rc;
568 EXPORT_SYMBOL(sclp_register);
570 /* Unregister event listener. */
571 void
572 sclp_unregister(struct sclp_register *reg)
574 unsigned long flags;
576 spin_lock_irqsave(&sclp_lock, flags);
577 list_del(&reg->list);
578 spin_unlock_irqrestore(&sclp_lock, flags);
579 sclp_init_mask(1);
582 EXPORT_SYMBOL(sclp_unregister);
584 /* Remove event buffers which are marked processed. Return the number of
585 * remaining event buffers. */
587 sclp_remove_processed(struct sccb_header *sccb)
589 struct evbuf_header *evbuf;
590 int unprocessed;
591 u16 remaining;
593 evbuf = (struct evbuf_header *) (sccb + 1);
594 unprocessed = 0;
595 remaining = sccb->length - sizeof(struct sccb_header);
596 while (remaining > 0) {
597 remaining -= evbuf->length;
598 if (evbuf->flags & 0x80) {
599 sccb->length -= evbuf->length;
600 memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
601 remaining);
602 } else {
603 unprocessed++;
604 evbuf = (struct evbuf_header *)
605 ((addr_t) evbuf + evbuf->length);
608 return unprocessed;
611 EXPORT_SYMBOL(sclp_remove_processed);
613 struct init_sccb {
614 struct sccb_header header;
615 u16 _reserved;
616 u16 mask_length;
617 sccb_mask_t receive_mask;
618 sccb_mask_t send_mask;
619 sccb_mask_t sclp_send_mask;
620 sccb_mask_t sclp_receive_mask;
621 } __attribute__((packed));
623 /* Prepare init mask request. Called while sclp_lock is locked. */
624 static inline void
625 __sclp_make_init_req(u32 receive_mask, u32 send_mask)
627 struct init_sccb *sccb;
629 sccb = (struct init_sccb *) sclp_init_sccb;
630 clear_page(sccb);
631 memset(&sclp_init_req, 0, sizeof(struct sclp_req));
632 sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
633 sclp_init_req.status = SCLP_REQ_FILLED;
634 sclp_init_req.start_count = 0;
635 sclp_init_req.callback = NULL;
636 sclp_init_req.callback_data = NULL;
637 sclp_init_req.sccb = sccb;
638 sccb->header.length = sizeof(struct init_sccb);
639 sccb->mask_length = sizeof(sccb_mask_t);
640 sccb->receive_mask = receive_mask;
641 sccb->send_mask = send_mask;
642 sccb->sclp_receive_mask = 0;
643 sccb->sclp_send_mask = 0;
646 /* Start init mask request. If calculate is non-zero, calculate the mask as
647 * requested by registered listeners. Use zero mask otherwise. Return 0 on
648 * success, non-zero otherwise. */
649 static int
650 sclp_init_mask(int calculate)
652 unsigned long flags;
653 struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
654 sccb_mask_t receive_mask;
655 sccb_mask_t send_mask;
656 int retry;
657 int rc;
658 unsigned long wait;
660 spin_lock_irqsave(&sclp_lock, flags);
661 /* Check if interface is in appropriate state */
662 if (sclp_mask_state != sclp_mask_state_idle) {
663 spin_unlock_irqrestore(&sclp_lock, flags);
664 return -EBUSY;
666 if (sclp_activation_state == sclp_activation_state_inactive) {
667 spin_unlock_irqrestore(&sclp_lock, flags);
668 return -EINVAL;
670 sclp_mask_state = sclp_mask_state_initializing;
671 /* Determine mask */
672 if (calculate)
673 __sclp_get_mask(&receive_mask, &send_mask);
674 else {
675 receive_mask = 0;
676 send_mask = 0;
678 rc = -EIO;
679 for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
680 /* Prepare request */
681 __sclp_make_init_req(receive_mask, send_mask);
682 spin_unlock_irqrestore(&sclp_lock, flags);
683 if (sclp_add_request(&sclp_init_req)) {
684 /* Try again later */
685 wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
686 while (time_before(jiffies, wait))
687 sclp_sync_wait();
688 spin_lock_irqsave(&sclp_lock, flags);
689 continue;
691 while (sclp_init_req.status != SCLP_REQ_DONE &&
692 sclp_init_req.status != SCLP_REQ_FAILED)
693 sclp_sync_wait();
694 spin_lock_irqsave(&sclp_lock, flags);
695 if (sclp_init_req.status == SCLP_REQ_DONE &&
696 sccb->header.response_code == 0x20) {
697 /* Successful request */
698 if (calculate) {
699 sclp_receive_mask = sccb->sclp_receive_mask;
700 sclp_send_mask = sccb->sclp_send_mask;
701 } else {
702 sclp_receive_mask = 0;
703 sclp_send_mask = 0;
705 spin_unlock_irqrestore(&sclp_lock, flags);
706 sclp_dispatch_state_change();
707 spin_lock_irqsave(&sclp_lock, flags);
708 rc = 0;
709 break;
712 sclp_mask_state = sclp_mask_state_idle;
713 spin_unlock_irqrestore(&sclp_lock, flags);
714 return rc;
717 /* Deactivate SCLP interface. On success, new requests will be rejected,
718 * events will no longer be dispatched. Return 0 on success, non-zero
719 * otherwise. */
721 sclp_deactivate(void)
723 unsigned long flags;
724 int rc;
726 spin_lock_irqsave(&sclp_lock, flags);
727 /* Deactivate can only be called when active */
728 if (sclp_activation_state != sclp_activation_state_active) {
729 spin_unlock_irqrestore(&sclp_lock, flags);
730 return -EINVAL;
732 sclp_activation_state = sclp_activation_state_deactivating;
733 spin_unlock_irqrestore(&sclp_lock, flags);
734 rc = sclp_init_mask(0);
735 spin_lock_irqsave(&sclp_lock, flags);
736 if (rc == 0)
737 sclp_activation_state = sclp_activation_state_inactive;
738 else
739 sclp_activation_state = sclp_activation_state_active;
740 spin_unlock_irqrestore(&sclp_lock, flags);
741 return rc;
744 EXPORT_SYMBOL(sclp_deactivate);
746 /* Reactivate SCLP interface after sclp_deactivate. On success, new
747 * requests will be accepted, events will be dispatched again. Return 0 on
748 * success, non-zero otherwise. */
750 sclp_reactivate(void)
752 unsigned long flags;
753 int rc;
755 spin_lock_irqsave(&sclp_lock, flags);
756 /* Reactivate can only be called when inactive */
757 if (sclp_activation_state != sclp_activation_state_inactive) {
758 spin_unlock_irqrestore(&sclp_lock, flags);
759 return -EINVAL;
761 sclp_activation_state = sclp_activation_state_activating;
762 spin_unlock_irqrestore(&sclp_lock, flags);
763 rc = sclp_init_mask(1);
764 spin_lock_irqsave(&sclp_lock, flags);
765 if (rc == 0)
766 sclp_activation_state = sclp_activation_state_active;
767 else
768 sclp_activation_state = sclp_activation_state_inactive;
769 spin_unlock_irqrestore(&sclp_lock, flags);
770 return rc;
773 EXPORT_SYMBOL(sclp_reactivate);
775 /* Handler for external interruption used during initialization. Modify
776 * request state to done. */
777 static void
778 sclp_check_handler(__u16 code)
780 u32 finished_sccb;
782 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
783 /* Is this the interrupt we are waiting for? */
784 if (finished_sccb == 0)
785 return;
786 if (finished_sccb != (u32) (addr_t) sclp_init_sccb) {
787 printk(KERN_WARNING SCLP_HEADER "unsolicited interrupt "
788 "for buffer at 0x%x\n", finished_sccb);
789 return;
791 spin_lock(&sclp_lock);
792 if (sclp_running_state == sclp_running_state_running) {
793 sclp_init_req.status = SCLP_REQ_DONE;
794 sclp_running_state = sclp_running_state_idle;
796 spin_unlock(&sclp_lock);
799 /* Initial init mask request timed out. Modify request state to failed. */
800 static void
801 sclp_check_timeout(unsigned long data)
803 unsigned long flags;
805 spin_lock_irqsave(&sclp_lock, flags);
806 if (sclp_running_state == sclp_running_state_running) {
807 sclp_init_req.status = SCLP_REQ_FAILED;
808 sclp_running_state = sclp_running_state_idle;
810 spin_unlock_irqrestore(&sclp_lock, flags);
813 /* Perform a check of the SCLP interface. Return zero if the interface is
814 * available and there are no pending requests from a previous instance.
815 * Return non-zero otherwise. */
816 static int
817 sclp_check_interface(void)
819 struct init_sccb *sccb;
820 unsigned long flags;
821 int retry;
822 int rc;
824 spin_lock_irqsave(&sclp_lock, flags);
825 /* Prepare init mask command */
826 rc = register_early_external_interrupt(0x2401, sclp_check_handler,
827 &ext_int_info_hwc);
828 if (rc) {
829 spin_unlock_irqrestore(&sclp_lock, flags);
830 return rc;
832 for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
833 __sclp_make_init_req(0, 0);
834 sccb = (struct init_sccb *) sclp_init_req.sccb;
835 rc = sclp_service_call(sclp_init_req.command, sccb);
836 if (rc == -EIO)
837 break;
838 sclp_init_req.status = SCLP_REQ_RUNNING;
839 sclp_running_state = sclp_running_state_running;
840 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
841 sclp_check_timeout, 0);
842 spin_unlock_irqrestore(&sclp_lock, flags);
843 /* Enable service-signal interruption - needs to happen
844 * with IRQs enabled. */
845 ctl_set_bit(0, 9);
846 /* Wait for signal from interrupt or timeout */
847 sclp_sync_wait();
848 /* Disable service-signal interruption - needs to happen
849 * with IRQs enabled. */
850 ctl_clear_bit(0,9);
851 spin_lock_irqsave(&sclp_lock, flags);
852 del_timer(&sclp_request_timer);
853 if (sclp_init_req.status == SCLP_REQ_DONE &&
854 sccb->header.response_code == 0x20) {
855 rc = 0;
856 break;
857 } else
858 rc = -EBUSY;
860 unregister_early_external_interrupt(0x2401, sclp_check_handler,
861 &ext_int_info_hwc);
862 spin_unlock_irqrestore(&sclp_lock, flags);
863 return rc;
866 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
867 * events from interfering with rebooted system. */
868 static int
869 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
871 sclp_deactivate();
872 return NOTIFY_DONE;
875 static struct notifier_block sclp_reboot_notifier = {
876 .notifier_call = sclp_reboot_event
879 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
880 * otherwise. */
881 static int
882 sclp_init(void)
884 unsigned long flags;
885 int rc;
887 if (!MACHINE_HAS_SCLP)
888 return -ENODEV;
889 spin_lock_irqsave(&sclp_lock, flags);
890 /* Check for previous or running initialization */
891 if (sclp_init_state != sclp_init_state_uninitialized) {
892 spin_unlock_irqrestore(&sclp_lock, flags);
893 return 0;
895 sclp_init_state = sclp_init_state_initializing;
896 /* Set up variables */
897 INIT_LIST_HEAD(&sclp_req_queue);
898 INIT_LIST_HEAD(&sclp_reg_list);
899 list_add(&sclp_state_change_event.list, &sclp_reg_list);
900 init_timer(&sclp_request_timer);
901 /* Check interface */
902 spin_unlock_irqrestore(&sclp_lock, flags);
903 rc = sclp_check_interface();
904 spin_lock_irqsave(&sclp_lock, flags);
905 if (rc) {
906 sclp_init_state = sclp_init_state_uninitialized;
907 spin_unlock_irqrestore(&sclp_lock, flags);
908 return rc;
910 /* Register reboot handler */
911 rc = register_reboot_notifier(&sclp_reboot_notifier);
912 if (rc) {
913 sclp_init_state = sclp_init_state_uninitialized;
914 spin_unlock_irqrestore(&sclp_lock, flags);
915 return rc;
917 /* Register interrupt handler */
918 rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
919 &ext_int_info_hwc);
920 if (rc) {
921 unregister_reboot_notifier(&sclp_reboot_notifier);
922 sclp_init_state = sclp_init_state_uninitialized;
923 spin_unlock_irqrestore(&sclp_lock, flags);
924 return rc;
926 sclp_init_state = sclp_init_state_initialized;
927 spin_unlock_irqrestore(&sclp_lock, flags);
928 /* Enable service-signal external interruption - needs to happen with
929 * IRQs enabled. */
930 ctl_set_bit(0, 9);
931 sclp_init_mask(1);
932 return 0;
935 static __init int sclp_initcall(void)
937 return sclp_init();
940 arch_initcall(sclp_initcall);