Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / s390 / char / sclp.c
blobf6d72e1f2a3897a324398f70767324bafb7056e0
1 /*
2 * core function to access sclp interface
4 * Copyright IBM Corp. 1999, 2009
6 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
7 * Martin Schwidefsky <schwidefsky@de.ibm.com>
8 */
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/spinlock.h>
13 #include <linux/interrupt.h>
14 #include <linux/timer.h>
15 #include <linux/reboot.h>
16 #include <linux/jiffies.h>
17 #include <linux/init.h>
18 #include <linux/suspend.h>
19 #include <linux/completion.h>
20 #include <linux/platform_device.h>
21 #include <asm/types.h>
22 #include <asm/s390_ext.h>
24 #include "sclp.h"
26 #define SCLP_HEADER "sclp: "
28 /* Structure for register_early_external_interrupt. */
29 static ext_int_info_t ext_int_info_hwc;
31 /* Lock to protect internal data consistency. */
32 static DEFINE_SPINLOCK(sclp_lock);
34 /* Mask of events that we can send to the sclp interface. */
35 static sccb_mask_t sclp_receive_mask;
37 /* Mask of events that we can receive from the sclp interface. */
38 static sccb_mask_t sclp_send_mask;
40 /* List of registered event listeners and senders. */
41 static struct list_head sclp_reg_list;
43 /* List of queued requests. */
44 static struct list_head sclp_req_queue;
46 /* Data for read and and init requests. */
47 static struct sclp_req sclp_read_req;
48 static struct sclp_req sclp_init_req;
49 static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
50 static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
52 /* Suspend request */
53 static DECLARE_COMPLETION(sclp_request_queue_flushed);
55 static void sclp_suspend_req_cb(struct sclp_req *req, void *data)
57 complete(&sclp_request_queue_flushed);
60 static struct sclp_req sclp_suspend_req;
62 /* Timer for request retries. */
63 static struct timer_list sclp_request_timer;
65 /* Internal state: is the driver initialized? */
66 static volatile enum sclp_init_state_t {
67 sclp_init_state_uninitialized,
68 sclp_init_state_initializing,
69 sclp_init_state_initialized
70 } sclp_init_state = sclp_init_state_uninitialized;
72 /* Internal state: is a request active at the sclp? */
73 static volatile enum sclp_running_state_t {
74 sclp_running_state_idle,
75 sclp_running_state_running,
76 sclp_running_state_reset_pending
77 } sclp_running_state = sclp_running_state_idle;
79 /* Internal state: is a read request pending? */
80 static volatile enum sclp_reading_state_t {
81 sclp_reading_state_idle,
82 sclp_reading_state_reading
83 } sclp_reading_state = sclp_reading_state_idle;
85 /* Internal state: is the driver currently serving requests? */
86 static volatile enum sclp_activation_state_t {
87 sclp_activation_state_active,
88 sclp_activation_state_deactivating,
89 sclp_activation_state_inactive,
90 sclp_activation_state_activating
91 } sclp_activation_state = sclp_activation_state_active;
93 /* Internal state: is an init mask request pending? */
94 static volatile enum sclp_mask_state_t {
95 sclp_mask_state_idle,
96 sclp_mask_state_initializing
97 } sclp_mask_state = sclp_mask_state_idle;
99 /* Internal state: is the driver suspended? */
100 static enum sclp_suspend_state_t {
101 sclp_suspend_state_running,
102 sclp_suspend_state_suspended,
103 } sclp_suspend_state = sclp_suspend_state_running;
105 /* Maximum retry counts */
106 #define SCLP_INIT_RETRY 3
107 #define SCLP_MASK_RETRY 3
109 /* Timeout intervals in seconds.*/
110 #define SCLP_BUSY_INTERVAL 10
111 #define SCLP_RETRY_INTERVAL 30
113 static void sclp_process_queue(void);
114 static void __sclp_make_read_req(void);
115 static int sclp_init_mask(int calculate);
116 static int sclp_init(void);
118 /* Perform service call. Return 0 on success, non-zero otherwise. */
120 sclp_service_call(sclp_cmdw_t command, void *sccb)
122 int cc;
124 asm volatile(
125 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
126 " ipm %0\n"
127 " srl %0,28"
128 : "=&d" (cc) : "d" (command), "a" (__pa(sccb))
129 : "cc", "memory");
130 if (cc == 3)
131 return -EIO;
132 if (cc == 2)
133 return -EBUSY;
134 return 0;
138 static void
139 __sclp_queue_read_req(void)
141 if (sclp_reading_state == sclp_reading_state_idle) {
142 sclp_reading_state = sclp_reading_state_reading;
143 __sclp_make_read_req();
144 /* Add request to head of queue */
145 list_add(&sclp_read_req.list, &sclp_req_queue);
149 /* Set up request retry timer. Called while sclp_lock is locked. */
150 static inline void
151 __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
152 unsigned long data)
154 del_timer(&sclp_request_timer);
155 sclp_request_timer.function = function;
156 sclp_request_timer.data = data;
157 sclp_request_timer.expires = jiffies + time;
158 add_timer(&sclp_request_timer);
161 /* Request timeout handler. Restart the request queue. If DATA is non-zero,
162 * force restart of running request. */
163 static void
164 sclp_request_timeout(unsigned long data)
166 unsigned long flags;
168 spin_lock_irqsave(&sclp_lock, flags);
169 if (data) {
170 if (sclp_running_state == sclp_running_state_running) {
171 /* Break running state and queue NOP read event request
172 * to get a defined interface state. */
173 __sclp_queue_read_req();
174 sclp_running_state = sclp_running_state_idle;
176 } else {
177 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
178 sclp_request_timeout, 0);
180 spin_unlock_irqrestore(&sclp_lock, flags);
181 sclp_process_queue();
184 /* Try to start a request. Return zero if the request was successfully
185 * started or if it will be started at a later time. Return non-zero otherwise.
186 * Called while sclp_lock is locked. */
187 static int
188 __sclp_start_request(struct sclp_req *req)
190 int rc;
192 if (sclp_running_state != sclp_running_state_idle)
193 return 0;
194 del_timer(&sclp_request_timer);
195 rc = sclp_service_call(req->command, req->sccb);
196 req->start_count++;
198 if (rc == 0) {
199 /* Successfully started request */
200 req->status = SCLP_REQ_RUNNING;
201 sclp_running_state = sclp_running_state_running;
202 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
203 sclp_request_timeout, 1);
204 return 0;
205 } else if (rc == -EBUSY) {
206 /* Try again later */
207 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
208 sclp_request_timeout, 0);
209 return 0;
211 /* Request failed */
212 req->status = SCLP_REQ_FAILED;
213 return rc;
216 /* Try to start queued requests. */
217 static void
218 sclp_process_queue(void)
220 struct sclp_req *req;
221 int rc;
222 unsigned long flags;
224 spin_lock_irqsave(&sclp_lock, flags);
225 if (sclp_running_state != sclp_running_state_idle) {
226 spin_unlock_irqrestore(&sclp_lock, flags);
227 return;
229 del_timer(&sclp_request_timer);
230 while (!list_empty(&sclp_req_queue)) {
231 req = list_entry(sclp_req_queue.next, struct sclp_req, list);
232 if (!req->sccb)
233 goto do_post;
234 rc = __sclp_start_request(req);
235 if (rc == 0)
236 break;
237 /* Request failed */
238 if (req->start_count > 1) {
239 /* Cannot abort already submitted request - could still
240 * be active at the SCLP */
241 __sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
242 sclp_request_timeout, 0);
243 break;
245 do_post:
246 /* Post-processing for aborted request */
247 list_del(&req->list);
248 if (req->callback) {
249 spin_unlock_irqrestore(&sclp_lock, flags);
250 req->callback(req, req->callback_data);
251 spin_lock_irqsave(&sclp_lock, flags);
254 spin_unlock_irqrestore(&sclp_lock, flags);
257 static int __sclp_can_add_request(struct sclp_req *req)
259 if (req == &sclp_suspend_req || req == &sclp_init_req)
260 return 1;
261 if (sclp_suspend_state != sclp_suspend_state_running)
262 return 0;
263 if (sclp_init_state != sclp_init_state_initialized)
264 return 0;
265 if (sclp_activation_state != sclp_activation_state_active)
266 return 0;
267 return 1;
270 /* Queue a new request. Return zero on success, non-zero otherwise. */
272 sclp_add_request(struct sclp_req *req)
274 unsigned long flags;
275 int rc;
277 spin_lock_irqsave(&sclp_lock, flags);
278 if (!__sclp_can_add_request(req)) {
279 spin_unlock_irqrestore(&sclp_lock, flags);
280 return -EIO;
282 req->status = SCLP_REQ_QUEUED;
283 req->start_count = 0;
284 list_add_tail(&req->list, &sclp_req_queue);
285 rc = 0;
286 /* Start if request is first in list */
287 if (sclp_running_state == sclp_running_state_idle &&
288 req->list.prev == &sclp_req_queue) {
289 if (!req->sccb) {
290 list_del(&req->list);
291 rc = -ENODATA;
292 goto out;
294 rc = __sclp_start_request(req);
295 if (rc)
296 list_del(&req->list);
298 out:
299 spin_unlock_irqrestore(&sclp_lock, flags);
300 return rc;
303 EXPORT_SYMBOL(sclp_add_request);
305 /* Dispatch events found in request buffer to registered listeners. Return 0
306 * if all events were dispatched, non-zero otherwise. */
307 static int
308 sclp_dispatch_evbufs(struct sccb_header *sccb)
310 unsigned long flags;
311 struct evbuf_header *evbuf;
312 struct list_head *l;
313 struct sclp_register *reg;
314 int offset;
315 int rc;
317 spin_lock_irqsave(&sclp_lock, flags);
318 rc = 0;
319 for (offset = sizeof(struct sccb_header); offset < sccb->length;
320 offset += evbuf->length) {
321 evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
322 /* Check for malformed hardware response */
323 if (evbuf->length == 0)
324 break;
325 /* Search for event handler */
326 reg = NULL;
327 list_for_each(l, &sclp_reg_list) {
328 reg = list_entry(l, struct sclp_register, list);
329 if (reg->receive_mask & (1 << (32 - evbuf->type)))
330 break;
331 else
332 reg = NULL;
334 if (reg && reg->receiver_fn) {
335 spin_unlock_irqrestore(&sclp_lock, flags);
336 reg->receiver_fn(evbuf);
337 spin_lock_irqsave(&sclp_lock, flags);
338 } else if (reg == NULL)
339 rc = -ENOSYS;
341 spin_unlock_irqrestore(&sclp_lock, flags);
342 return rc;
345 /* Read event data request callback. */
346 static void
347 sclp_read_cb(struct sclp_req *req, void *data)
349 unsigned long flags;
350 struct sccb_header *sccb;
352 sccb = (struct sccb_header *) req->sccb;
353 if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
354 sccb->response_code == 0x220))
355 sclp_dispatch_evbufs(sccb);
356 spin_lock_irqsave(&sclp_lock, flags);
357 sclp_reading_state = sclp_reading_state_idle;
358 spin_unlock_irqrestore(&sclp_lock, flags);
361 /* Prepare read event data request. Called while sclp_lock is locked. */
362 static void __sclp_make_read_req(void)
364 struct sccb_header *sccb;
366 sccb = (struct sccb_header *) sclp_read_sccb;
367 clear_page(sccb);
368 memset(&sclp_read_req, 0, sizeof(struct sclp_req));
369 sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
370 sclp_read_req.status = SCLP_REQ_QUEUED;
371 sclp_read_req.start_count = 0;
372 sclp_read_req.callback = sclp_read_cb;
373 sclp_read_req.sccb = sccb;
374 sccb->length = PAGE_SIZE;
375 sccb->function_code = 0;
376 sccb->control_mask[2] = 0x80;
379 /* Search request list for request with matching sccb. Return request if found,
380 * NULL otherwise. Called while sclp_lock is locked. */
381 static inline struct sclp_req *
382 __sclp_find_req(u32 sccb)
384 struct list_head *l;
385 struct sclp_req *req;
387 list_for_each(l, &sclp_req_queue) {
388 req = list_entry(l, struct sclp_req, list);
389 if (sccb == (u32) (addr_t) req->sccb)
390 return req;
392 return NULL;
395 /* Handler for external interruption. Perform request post-processing.
396 * Prepare read event data request if necessary. Start processing of next
397 * request on queue. */
398 static void
399 sclp_interrupt_handler(__u16 code)
401 struct sclp_req *req;
402 u32 finished_sccb;
403 u32 evbuf_pending;
405 spin_lock(&sclp_lock);
406 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
407 evbuf_pending = S390_lowcore.ext_params & 0x3;
408 if (finished_sccb) {
409 del_timer(&sclp_request_timer);
410 sclp_running_state = sclp_running_state_reset_pending;
411 req = __sclp_find_req(finished_sccb);
412 if (req) {
413 /* Request post-processing */
414 list_del(&req->list);
415 req->status = SCLP_REQ_DONE;
416 if (req->callback) {
417 spin_unlock(&sclp_lock);
418 req->callback(req, req->callback_data);
419 spin_lock(&sclp_lock);
422 sclp_running_state = sclp_running_state_idle;
424 if (evbuf_pending &&
425 sclp_activation_state == sclp_activation_state_active)
426 __sclp_queue_read_req();
427 spin_unlock(&sclp_lock);
428 sclp_process_queue();
431 /* Convert interval in jiffies to TOD ticks. */
432 static inline u64
433 sclp_tod_from_jiffies(unsigned long jiffies)
435 return (u64) (jiffies / HZ) << 32;
438 /* Wait until a currently running request finished. Note: while this function
439 * is running, no timers are served on the calling CPU. */
440 void
441 sclp_sync_wait(void)
443 unsigned long long old_tick;
444 unsigned long flags;
445 unsigned long cr0, cr0_sync;
446 u64 timeout;
447 int irq_context;
449 /* We'll be disabling timer interrupts, so we need a custom timeout
450 * mechanism */
451 timeout = 0;
452 if (timer_pending(&sclp_request_timer)) {
453 /* Get timeout TOD value */
454 timeout = get_clock() +
455 sclp_tod_from_jiffies(sclp_request_timer.expires -
456 jiffies);
458 local_irq_save(flags);
459 /* Prevent bottom half from executing once we force interrupts open */
460 irq_context = in_interrupt();
461 if (!irq_context)
462 local_bh_disable();
463 /* Enable service-signal interruption, disable timer interrupts */
464 old_tick = local_tick_disable();
465 trace_hardirqs_on();
466 __ctl_store(cr0, 0, 0);
467 cr0_sync = cr0;
468 cr0_sync &= 0xffff00a0;
469 cr0_sync |= 0x00000200;
470 __ctl_load(cr0_sync, 0, 0);
471 __raw_local_irq_stosm(0x01);
472 /* Loop until driver state indicates finished request */
473 while (sclp_running_state != sclp_running_state_idle) {
474 /* Check for expired request timer */
475 if (timer_pending(&sclp_request_timer) &&
476 get_clock() > timeout &&
477 del_timer(&sclp_request_timer))
478 sclp_request_timer.function(sclp_request_timer.data);
479 cpu_relax();
481 local_irq_disable();
482 __ctl_load(cr0, 0, 0);
483 if (!irq_context)
484 _local_bh_enable();
485 local_tick_enable(old_tick);
486 local_irq_restore(flags);
488 EXPORT_SYMBOL(sclp_sync_wait);
490 /* Dispatch changes in send and receive mask to registered listeners. */
491 static void
492 sclp_dispatch_state_change(void)
494 struct list_head *l;
495 struct sclp_register *reg;
496 unsigned long flags;
497 sccb_mask_t receive_mask;
498 sccb_mask_t send_mask;
500 do {
501 spin_lock_irqsave(&sclp_lock, flags);
502 reg = NULL;
503 list_for_each(l, &sclp_reg_list) {
504 reg = list_entry(l, struct sclp_register, list);
505 receive_mask = reg->send_mask & sclp_receive_mask;
506 send_mask = reg->receive_mask & sclp_send_mask;
507 if (reg->sclp_receive_mask != receive_mask ||
508 reg->sclp_send_mask != send_mask) {
509 reg->sclp_receive_mask = receive_mask;
510 reg->sclp_send_mask = send_mask;
511 break;
512 } else
513 reg = NULL;
515 spin_unlock_irqrestore(&sclp_lock, flags);
516 if (reg && reg->state_change_fn)
517 reg->state_change_fn(reg);
518 } while (reg);
521 struct sclp_statechangebuf {
522 struct evbuf_header header;
523 u8 validity_sclp_active_facility_mask : 1;
524 u8 validity_sclp_receive_mask : 1;
525 u8 validity_sclp_send_mask : 1;
526 u8 validity_read_data_function_mask : 1;
527 u16 _zeros : 12;
528 u16 mask_length;
529 u64 sclp_active_facility_mask;
530 sccb_mask_t sclp_receive_mask;
531 sccb_mask_t sclp_send_mask;
532 u32 read_data_function_mask;
533 } __attribute__((packed));
536 /* State change event callback. Inform listeners of changes. */
537 static void
538 sclp_state_change_cb(struct evbuf_header *evbuf)
540 unsigned long flags;
541 struct sclp_statechangebuf *scbuf;
543 scbuf = (struct sclp_statechangebuf *) evbuf;
544 if (scbuf->mask_length != sizeof(sccb_mask_t))
545 return;
546 spin_lock_irqsave(&sclp_lock, flags);
547 if (scbuf->validity_sclp_receive_mask)
548 sclp_receive_mask = scbuf->sclp_receive_mask;
549 if (scbuf->validity_sclp_send_mask)
550 sclp_send_mask = scbuf->sclp_send_mask;
551 spin_unlock_irqrestore(&sclp_lock, flags);
552 if (scbuf->validity_sclp_active_facility_mask)
553 sclp_facilities = scbuf->sclp_active_facility_mask;
554 sclp_dispatch_state_change();
557 static struct sclp_register sclp_state_change_event = {
558 .receive_mask = EVTYP_STATECHANGE_MASK,
559 .receiver_fn = sclp_state_change_cb
562 /* Calculate receive and send mask of currently registered listeners.
563 * Called while sclp_lock is locked. */
564 static inline void
565 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
567 struct list_head *l;
568 struct sclp_register *t;
570 *receive_mask = 0;
571 *send_mask = 0;
572 list_for_each(l, &sclp_reg_list) {
573 t = list_entry(l, struct sclp_register, list);
574 *receive_mask |= t->receive_mask;
575 *send_mask |= t->send_mask;
579 /* Register event listener. Return 0 on success, non-zero otherwise. */
581 sclp_register(struct sclp_register *reg)
583 unsigned long flags;
584 sccb_mask_t receive_mask;
585 sccb_mask_t send_mask;
586 int rc;
588 rc = sclp_init();
589 if (rc)
590 return rc;
591 spin_lock_irqsave(&sclp_lock, flags);
592 /* Check event mask for collisions */
593 __sclp_get_mask(&receive_mask, &send_mask);
594 if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
595 spin_unlock_irqrestore(&sclp_lock, flags);
596 return -EBUSY;
598 /* Trigger initial state change callback */
599 reg->sclp_receive_mask = 0;
600 reg->sclp_send_mask = 0;
601 reg->pm_event_posted = 0;
602 list_add(&reg->list, &sclp_reg_list);
603 spin_unlock_irqrestore(&sclp_lock, flags);
604 rc = sclp_init_mask(1);
605 if (rc) {
606 spin_lock_irqsave(&sclp_lock, flags);
607 list_del(&reg->list);
608 spin_unlock_irqrestore(&sclp_lock, flags);
610 return rc;
613 EXPORT_SYMBOL(sclp_register);
615 /* Unregister event listener. */
616 void
617 sclp_unregister(struct sclp_register *reg)
619 unsigned long flags;
621 spin_lock_irqsave(&sclp_lock, flags);
622 list_del(&reg->list);
623 spin_unlock_irqrestore(&sclp_lock, flags);
624 sclp_init_mask(1);
627 EXPORT_SYMBOL(sclp_unregister);
629 /* Remove event buffers which are marked processed. Return the number of
630 * remaining event buffers. */
632 sclp_remove_processed(struct sccb_header *sccb)
634 struct evbuf_header *evbuf;
635 int unprocessed;
636 u16 remaining;
638 evbuf = (struct evbuf_header *) (sccb + 1);
639 unprocessed = 0;
640 remaining = sccb->length - sizeof(struct sccb_header);
641 while (remaining > 0) {
642 remaining -= evbuf->length;
643 if (evbuf->flags & 0x80) {
644 sccb->length -= evbuf->length;
645 memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
646 remaining);
647 } else {
648 unprocessed++;
649 evbuf = (struct evbuf_header *)
650 ((addr_t) evbuf + evbuf->length);
653 return unprocessed;
656 EXPORT_SYMBOL(sclp_remove_processed);
658 struct init_sccb {
659 struct sccb_header header;
660 u16 _reserved;
661 u16 mask_length;
662 sccb_mask_t receive_mask;
663 sccb_mask_t send_mask;
664 sccb_mask_t sclp_receive_mask;
665 sccb_mask_t sclp_send_mask;
666 } __attribute__((packed));
668 /* Prepare init mask request. Called while sclp_lock is locked. */
669 static inline void
670 __sclp_make_init_req(u32 receive_mask, u32 send_mask)
672 struct init_sccb *sccb;
674 sccb = (struct init_sccb *) sclp_init_sccb;
675 clear_page(sccb);
676 memset(&sclp_init_req, 0, sizeof(struct sclp_req));
677 sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
678 sclp_init_req.status = SCLP_REQ_FILLED;
679 sclp_init_req.start_count = 0;
680 sclp_init_req.callback = NULL;
681 sclp_init_req.callback_data = NULL;
682 sclp_init_req.sccb = sccb;
683 sccb->header.length = sizeof(struct init_sccb);
684 sccb->mask_length = sizeof(sccb_mask_t);
685 sccb->receive_mask = receive_mask;
686 sccb->send_mask = send_mask;
687 sccb->sclp_receive_mask = 0;
688 sccb->sclp_send_mask = 0;
691 /* Start init mask request. If calculate is non-zero, calculate the mask as
692 * requested by registered listeners. Use zero mask otherwise. Return 0 on
693 * success, non-zero otherwise. */
694 static int
695 sclp_init_mask(int calculate)
697 unsigned long flags;
698 struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
699 sccb_mask_t receive_mask;
700 sccb_mask_t send_mask;
701 int retry;
702 int rc;
703 unsigned long wait;
705 spin_lock_irqsave(&sclp_lock, flags);
706 /* Check if interface is in appropriate state */
707 if (sclp_mask_state != sclp_mask_state_idle) {
708 spin_unlock_irqrestore(&sclp_lock, flags);
709 return -EBUSY;
711 if (sclp_activation_state == sclp_activation_state_inactive) {
712 spin_unlock_irqrestore(&sclp_lock, flags);
713 return -EINVAL;
715 sclp_mask_state = sclp_mask_state_initializing;
716 /* Determine mask */
717 if (calculate)
718 __sclp_get_mask(&receive_mask, &send_mask);
719 else {
720 receive_mask = 0;
721 send_mask = 0;
723 rc = -EIO;
724 for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
725 /* Prepare request */
726 __sclp_make_init_req(receive_mask, send_mask);
727 spin_unlock_irqrestore(&sclp_lock, flags);
728 if (sclp_add_request(&sclp_init_req)) {
729 /* Try again later */
730 wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
731 while (time_before(jiffies, wait))
732 sclp_sync_wait();
733 spin_lock_irqsave(&sclp_lock, flags);
734 continue;
736 while (sclp_init_req.status != SCLP_REQ_DONE &&
737 sclp_init_req.status != SCLP_REQ_FAILED)
738 sclp_sync_wait();
739 spin_lock_irqsave(&sclp_lock, flags);
740 if (sclp_init_req.status == SCLP_REQ_DONE &&
741 sccb->header.response_code == 0x20) {
742 /* Successful request */
743 if (calculate) {
744 sclp_receive_mask = sccb->sclp_receive_mask;
745 sclp_send_mask = sccb->sclp_send_mask;
746 } else {
747 sclp_receive_mask = 0;
748 sclp_send_mask = 0;
750 spin_unlock_irqrestore(&sclp_lock, flags);
751 sclp_dispatch_state_change();
752 spin_lock_irqsave(&sclp_lock, flags);
753 rc = 0;
754 break;
757 sclp_mask_state = sclp_mask_state_idle;
758 spin_unlock_irqrestore(&sclp_lock, flags);
759 return rc;
762 /* Deactivate SCLP interface. On success, new requests will be rejected,
763 * events will no longer be dispatched. Return 0 on success, non-zero
764 * otherwise. */
766 sclp_deactivate(void)
768 unsigned long flags;
769 int rc;
771 spin_lock_irqsave(&sclp_lock, flags);
772 /* Deactivate can only be called when active */
773 if (sclp_activation_state != sclp_activation_state_active) {
774 spin_unlock_irqrestore(&sclp_lock, flags);
775 return -EINVAL;
777 sclp_activation_state = sclp_activation_state_deactivating;
778 spin_unlock_irqrestore(&sclp_lock, flags);
779 rc = sclp_init_mask(0);
780 spin_lock_irqsave(&sclp_lock, flags);
781 if (rc == 0)
782 sclp_activation_state = sclp_activation_state_inactive;
783 else
784 sclp_activation_state = sclp_activation_state_active;
785 spin_unlock_irqrestore(&sclp_lock, flags);
786 return rc;
789 EXPORT_SYMBOL(sclp_deactivate);
791 /* Reactivate SCLP interface after sclp_deactivate. On success, new
792 * requests will be accepted, events will be dispatched again. Return 0 on
793 * success, non-zero otherwise. */
795 sclp_reactivate(void)
797 unsigned long flags;
798 int rc;
800 spin_lock_irqsave(&sclp_lock, flags);
801 /* Reactivate can only be called when inactive */
802 if (sclp_activation_state != sclp_activation_state_inactive) {
803 spin_unlock_irqrestore(&sclp_lock, flags);
804 return -EINVAL;
806 sclp_activation_state = sclp_activation_state_activating;
807 spin_unlock_irqrestore(&sclp_lock, flags);
808 rc = sclp_init_mask(1);
809 spin_lock_irqsave(&sclp_lock, flags);
810 if (rc == 0)
811 sclp_activation_state = sclp_activation_state_active;
812 else
813 sclp_activation_state = sclp_activation_state_inactive;
814 spin_unlock_irqrestore(&sclp_lock, flags);
815 return rc;
818 EXPORT_SYMBOL(sclp_reactivate);
820 /* Handler for external interruption used during initialization. Modify
821 * request state to done. */
822 static void
823 sclp_check_handler(__u16 code)
825 u32 finished_sccb;
827 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
828 /* Is this the interrupt we are waiting for? */
829 if (finished_sccb == 0)
830 return;
831 if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
832 panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
833 finished_sccb);
834 spin_lock(&sclp_lock);
835 if (sclp_running_state == sclp_running_state_running) {
836 sclp_init_req.status = SCLP_REQ_DONE;
837 sclp_running_state = sclp_running_state_idle;
839 spin_unlock(&sclp_lock);
842 /* Initial init mask request timed out. Modify request state to failed. */
843 static void
844 sclp_check_timeout(unsigned long data)
846 unsigned long flags;
848 spin_lock_irqsave(&sclp_lock, flags);
849 if (sclp_running_state == sclp_running_state_running) {
850 sclp_init_req.status = SCLP_REQ_FAILED;
851 sclp_running_state = sclp_running_state_idle;
853 spin_unlock_irqrestore(&sclp_lock, flags);
856 /* Perform a check of the SCLP interface. Return zero if the interface is
857 * available and there are no pending requests from a previous instance.
858 * Return non-zero otherwise. */
859 static int
860 sclp_check_interface(void)
862 struct init_sccb *sccb;
863 unsigned long flags;
864 int retry;
865 int rc;
867 spin_lock_irqsave(&sclp_lock, flags);
868 /* Prepare init mask command */
869 rc = register_early_external_interrupt(0x2401, sclp_check_handler,
870 &ext_int_info_hwc);
871 if (rc) {
872 spin_unlock_irqrestore(&sclp_lock, flags);
873 return rc;
875 for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
876 __sclp_make_init_req(0, 0);
877 sccb = (struct init_sccb *) sclp_init_req.sccb;
878 rc = sclp_service_call(sclp_init_req.command, sccb);
879 if (rc == -EIO)
880 break;
881 sclp_init_req.status = SCLP_REQ_RUNNING;
882 sclp_running_state = sclp_running_state_running;
883 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
884 sclp_check_timeout, 0);
885 spin_unlock_irqrestore(&sclp_lock, flags);
886 /* Enable service-signal interruption - needs to happen
887 * with IRQs enabled. */
888 ctl_set_bit(0, 9);
889 /* Wait for signal from interrupt or timeout */
890 sclp_sync_wait();
891 /* Disable service-signal interruption - needs to happen
892 * with IRQs enabled. */
893 ctl_clear_bit(0,9);
894 spin_lock_irqsave(&sclp_lock, flags);
895 del_timer(&sclp_request_timer);
896 if (sclp_init_req.status == SCLP_REQ_DONE &&
897 sccb->header.response_code == 0x20) {
898 rc = 0;
899 break;
900 } else
901 rc = -EBUSY;
903 unregister_early_external_interrupt(0x2401, sclp_check_handler,
904 &ext_int_info_hwc);
905 spin_unlock_irqrestore(&sclp_lock, flags);
906 return rc;
909 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
910 * events from interfering with rebooted system. */
911 static int
912 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
914 sclp_deactivate();
915 return NOTIFY_DONE;
918 static struct notifier_block sclp_reboot_notifier = {
919 .notifier_call = sclp_reboot_event
923 * Suspend/resume SCLP notifier implementation
926 static void sclp_pm_event(enum sclp_pm_event sclp_pm_event, int rollback)
928 struct sclp_register *reg;
929 unsigned long flags;
931 if (!rollback) {
932 spin_lock_irqsave(&sclp_lock, flags);
933 list_for_each_entry(reg, &sclp_reg_list, list)
934 reg->pm_event_posted = 0;
935 spin_unlock_irqrestore(&sclp_lock, flags);
937 do {
938 spin_lock_irqsave(&sclp_lock, flags);
939 list_for_each_entry(reg, &sclp_reg_list, list) {
940 if (rollback && reg->pm_event_posted)
941 goto found;
942 if (!rollback && !reg->pm_event_posted)
943 goto found;
945 spin_unlock_irqrestore(&sclp_lock, flags);
946 return;
947 found:
948 spin_unlock_irqrestore(&sclp_lock, flags);
949 if (reg->pm_event_fn)
950 reg->pm_event_fn(reg, sclp_pm_event);
951 reg->pm_event_posted = rollback ? 0 : 1;
952 } while (1);
956 * Susend/resume callbacks for platform device
959 static int sclp_freeze(struct device *dev)
961 unsigned long flags;
962 int rc;
964 sclp_pm_event(SCLP_PM_EVENT_FREEZE, 0);
966 spin_lock_irqsave(&sclp_lock, flags);
967 sclp_suspend_state = sclp_suspend_state_suspended;
968 spin_unlock_irqrestore(&sclp_lock, flags);
970 /* Init supend data */
971 memset(&sclp_suspend_req, 0, sizeof(sclp_suspend_req));
972 sclp_suspend_req.callback = sclp_suspend_req_cb;
973 sclp_suspend_req.status = SCLP_REQ_FILLED;
974 init_completion(&sclp_request_queue_flushed);
976 rc = sclp_add_request(&sclp_suspend_req);
977 if (rc == 0)
978 wait_for_completion(&sclp_request_queue_flushed);
979 else if (rc != -ENODATA)
980 goto fail_thaw;
982 rc = sclp_deactivate();
983 if (rc)
984 goto fail_thaw;
985 return 0;
987 fail_thaw:
988 spin_lock_irqsave(&sclp_lock, flags);
989 sclp_suspend_state = sclp_suspend_state_running;
990 spin_unlock_irqrestore(&sclp_lock, flags);
991 sclp_pm_event(SCLP_PM_EVENT_THAW, 1);
992 return rc;
995 static int sclp_undo_suspend(enum sclp_pm_event event)
997 unsigned long flags;
998 int rc;
1000 rc = sclp_reactivate();
1001 if (rc)
1002 return rc;
1004 spin_lock_irqsave(&sclp_lock, flags);
1005 sclp_suspend_state = sclp_suspend_state_running;
1006 spin_unlock_irqrestore(&sclp_lock, flags);
1008 sclp_pm_event(event, 0);
1009 return 0;
1012 static int sclp_thaw(struct device *dev)
1014 return sclp_undo_suspend(SCLP_PM_EVENT_THAW);
1017 static int sclp_restore(struct device *dev)
1019 return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE);
1022 static const struct dev_pm_ops sclp_pm_ops = {
1023 .freeze = sclp_freeze,
1024 .thaw = sclp_thaw,
1025 .restore = sclp_restore,
1028 static struct platform_driver sclp_pdrv = {
1029 .driver = {
1030 .name = "sclp",
1031 .owner = THIS_MODULE,
1032 .pm = &sclp_pm_ops,
1036 static struct platform_device *sclp_pdev;
1038 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
1039 * otherwise. */
1040 static int
1041 sclp_init(void)
1043 unsigned long flags;
1044 int rc = 0;
1046 spin_lock_irqsave(&sclp_lock, flags);
1047 /* Check for previous or running initialization */
1048 if (sclp_init_state != sclp_init_state_uninitialized)
1049 goto fail_unlock;
1050 sclp_init_state = sclp_init_state_initializing;
1051 /* Set up variables */
1052 INIT_LIST_HEAD(&sclp_req_queue);
1053 INIT_LIST_HEAD(&sclp_reg_list);
1054 list_add(&sclp_state_change_event.list, &sclp_reg_list);
1055 init_timer(&sclp_request_timer);
1056 /* Check interface */
1057 spin_unlock_irqrestore(&sclp_lock, flags);
1058 rc = sclp_check_interface();
1059 spin_lock_irqsave(&sclp_lock, flags);
1060 if (rc)
1061 goto fail_init_state_uninitialized;
1062 /* Register reboot handler */
1063 rc = register_reboot_notifier(&sclp_reboot_notifier);
1064 if (rc)
1065 goto fail_init_state_uninitialized;
1066 /* Register interrupt handler */
1067 rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
1068 &ext_int_info_hwc);
1069 if (rc)
1070 goto fail_unregister_reboot_notifier;
1071 sclp_init_state = sclp_init_state_initialized;
1072 spin_unlock_irqrestore(&sclp_lock, flags);
1073 /* Enable service-signal external interruption - needs to happen with
1074 * IRQs enabled. */
1075 ctl_set_bit(0, 9);
1076 sclp_init_mask(1);
1077 return 0;
1079 fail_unregister_reboot_notifier:
1080 unregister_reboot_notifier(&sclp_reboot_notifier);
1081 fail_init_state_uninitialized:
1082 sclp_init_state = sclp_init_state_uninitialized;
1083 fail_unlock:
1084 spin_unlock_irqrestore(&sclp_lock, flags);
1085 return rc;
1089 * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
1090 * to print the panic message.
1092 static int sclp_panic_notify(struct notifier_block *self,
1093 unsigned long event, void *data)
1095 if (sclp_suspend_state == sclp_suspend_state_suspended)
1096 sclp_undo_suspend(SCLP_PM_EVENT_THAW);
1097 return NOTIFY_OK;
1100 static struct notifier_block sclp_on_panic_nb = {
1101 .notifier_call = sclp_panic_notify,
1102 .priority = SCLP_PANIC_PRIO,
1105 static __init int sclp_initcall(void)
1107 int rc;
1109 rc = platform_driver_register(&sclp_pdrv);
1110 if (rc)
1111 return rc;
1112 sclp_pdev = platform_device_register_simple("sclp", -1, NULL, 0);
1113 rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
1114 if (rc)
1115 goto fail_platform_driver_unregister;
1116 rc = atomic_notifier_chain_register(&panic_notifier_list,
1117 &sclp_on_panic_nb);
1118 if (rc)
1119 goto fail_platform_device_unregister;
1121 return sclp_init();
1123 fail_platform_device_unregister:
1124 platform_device_unregister(sclp_pdev);
1125 fail_platform_driver_unregister:
1126 platform_driver_unregister(&sclp_pdrv);
1127 return rc;
1130 arch_initcall(sclp_initcall);