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>
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>
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
)));
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
{
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
)
125 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
128 : "=&d" (cc
) : "d" (command
), "a" (__pa(sccb
))
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. */
151 __sclp_set_request_timer(unsigned long time
, void (*function
)(unsigned long),
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. */
164 sclp_request_timeout(unsigned long data
)
168 spin_lock_irqsave(&sclp_lock
, flags
);
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
;
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. */
188 __sclp_start_request(struct sclp_req
*req
)
192 if (sclp_running_state
!= sclp_running_state_idle
)
194 del_timer(&sclp_request_timer
);
195 rc
= sclp_service_call(req
->command
, req
->sccb
);
199 /* Sucessfully 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);
205 } else if (rc
== -EBUSY
) {
206 /* Try again later */
207 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
208 sclp_request_timeout
, 0);
212 req
->status
= SCLP_REQ_FAILED
;
216 /* Try to start queued requests. */
218 sclp_process_queue(void)
220 struct sclp_req
*req
;
224 spin_lock_irqsave(&sclp_lock
, flags
);
225 if (sclp_running_state
!= sclp_running_state_idle
) {
226 spin_unlock_irqrestore(&sclp_lock
, flags
);
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
);
234 rc
= __sclp_start_request(req
);
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);
246 /* Post-processing for aborted request */
247 list_del(&req
->list
);
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
)
261 if (sclp_suspend_state
!= sclp_suspend_state_running
)
263 if (sclp_init_state
!= sclp_init_state_initialized
)
265 if (sclp_activation_state
!= sclp_activation_state_active
)
270 /* Queue a new request. Return zero on success, non-zero otherwise. */
272 sclp_add_request(struct sclp_req
*req
)
277 spin_lock_irqsave(&sclp_lock
, flags
);
278 if (!__sclp_can_add_request(req
)) {
279 spin_unlock_irqrestore(&sclp_lock
, flags
);
282 req
->status
= SCLP_REQ_QUEUED
;
283 req
->start_count
= 0;
284 list_add_tail(&req
->list
, &sclp_req_queue
);
286 /* Start if request is first in list */
287 if (sclp_running_state
== sclp_running_state_idle
&&
288 req
->list
.prev
== &sclp_req_queue
) {
290 list_del(&req
->list
);
294 rc
= __sclp_start_request(req
);
296 list_del(&req
->list
);
299 spin_unlock_irqrestore(&sclp_lock
, flags
);
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. */
308 sclp_dispatch_evbufs(struct sccb_header
*sccb
)
311 struct evbuf_header
*evbuf
;
313 struct sclp_register
*reg
;
317 spin_lock_irqsave(&sclp_lock
, flags
);
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)
325 /* Search for event handler */
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
)))
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
)
341 spin_unlock_irqrestore(&sclp_lock
, flags
);
345 /* Read event data request callback. */
347 sclp_read_cb(struct sclp_req
*req
, void *data
)
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
;
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
)
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
)
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. */
399 sclp_interrupt_handler(__u16 code
)
401 struct sclp_req
*req
;
405 spin_lock(&sclp_lock
);
406 finished_sccb
= S390_lowcore
.ext_params
& 0xfffffff8;
407 evbuf_pending
= S390_lowcore
.ext_params
& 0x3;
409 del_timer(&sclp_request_timer
);
410 sclp_running_state
= sclp_running_state_reset_pending
;
411 req
= __sclp_find_req(finished_sccb
);
413 /* Request post-processing */
414 list_del(&req
->list
);
415 req
->status
= SCLP_REQ_DONE
;
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
;
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. */
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. */
443 unsigned long long old_tick
;
445 unsigned long cr0
, cr0_sync
;
449 /* We'll be disabling timer interrupts, so we need a custom timeout
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
-
458 local_irq_save(flags
);
459 /* Prevent bottom half from executing once we force interrupts open */
460 irq_context
= in_interrupt();
463 /* Enable service-signal interruption, disable timer interrupts */
464 old_tick
= local_tick_disable();
466 __ctl_store(cr0
, 0, 0);
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
);
482 __ctl_load(cr0
, 0, 0);
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. */
492 sclp_dispatch_state_change(void)
495 struct sclp_register
*reg
;
497 sccb_mask_t receive_mask
;
498 sccb_mask_t send_mask
;
501 spin_lock_irqsave(&sclp_lock
, flags
);
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
;
515 spin_unlock_irqrestore(&sclp_lock
, flags
);
516 if (reg
&& reg
->state_change_fn
)
517 reg
->state_change_fn(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;
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. */
538 sclp_state_change_cb(struct evbuf_header
*evbuf
)
541 struct sclp_statechangebuf
*scbuf
;
543 scbuf
= (struct sclp_statechangebuf
*) evbuf
;
544 if (scbuf
->mask_length
!= sizeof(sccb_mask_t
))
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. */
565 __sclp_get_mask(sccb_mask_t
*receive_mask
, sccb_mask_t
*send_mask
)
568 struct sclp_register
*t
;
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
)
584 sccb_mask_t receive_mask
;
585 sccb_mask_t send_mask
;
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
);
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(®
->list
, &sclp_reg_list
);
603 spin_unlock_irqrestore(&sclp_lock
, flags
);
604 rc
= sclp_init_mask(1);
606 spin_lock_irqsave(&sclp_lock
, flags
);
607 list_del(®
->list
);
608 spin_unlock_irqrestore(&sclp_lock
, flags
);
613 EXPORT_SYMBOL(sclp_register
);
615 /* Unregister event listener. */
617 sclp_unregister(struct sclp_register
*reg
)
621 spin_lock_irqsave(&sclp_lock
, flags
);
622 list_del(®
->list
);
623 spin_unlock_irqrestore(&sclp_lock
, flags
);
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
;
638 evbuf
= (struct evbuf_header
*) (sccb
+ 1);
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
),
649 evbuf
= (struct evbuf_header
*)
650 ((addr_t
) evbuf
+ evbuf
->length
);
656 EXPORT_SYMBOL(sclp_remove_processed
);
659 struct sccb_header header
;
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. */
670 __sclp_make_init_req(u32 receive_mask
, u32 send_mask
)
672 struct init_sccb
*sccb
;
674 sccb
= (struct init_sccb
*) sclp_init_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. */
695 sclp_init_mask(int calculate
)
698 struct init_sccb
*sccb
= (struct init_sccb
*) sclp_init_sccb
;
699 sccb_mask_t receive_mask
;
700 sccb_mask_t send_mask
;
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
);
711 if (sclp_activation_state
== sclp_activation_state_inactive
) {
712 spin_unlock_irqrestore(&sclp_lock
, flags
);
715 sclp_mask_state
= sclp_mask_state_initializing
;
718 __sclp_get_mask(&receive_mask
, &send_mask
);
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
))
733 spin_lock_irqsave(&sclp_lock
, flags
);
736 while (sclp_init_req
.status
!= SCLP_REQ_DONE
&&
737 sclp_init_req
.status
!= SCLP_REQ_FAILED
)
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 */
744 sclp_receive_mask
= sccb
->sclp_receive_mask
;
745 sclp_send_mask
= sccb
->sclp_send_mask
;
747 sclp_receive_mask
= 0;
750 spin_unlock_irqrestore(&sclp_lock
, flags
);
751 sclp_dispatch_state_change();
752 spin_lock_irqsave(&sclp_lock
, flags
);
757 sclp_mask_state
= sclp_mask_state_idle
;
758 spin_unlock_irqrestore(&sclp_lock
, flags
);
762 /* Deactivate SCLP interface. On success, new requests will be rejected,
763 * events will no longer be dispatched. Return 0 on success, non-zero
766 sclp_deactivate(void)
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
);
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
);
782 sclp_activation_state
= sclp_activation_state_inactive
;
784 sclp_activation_state
= sclp_activation_state_active
;
785 spin_unlock_irqrestore(&sclp_lock
, flags
);
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)
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
);
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
);
811 sclp_activation_state
= sclp_activation_state_active
;
813 sclp_activation_state
= sclp_activation_state_inactive
;
814 spin_unlock_irqrestore(&sclp_lock
, flags
);
818 EXPORT_SYMBOL(sclp_reactivate
);
820 /* Handler for external interruption used during initialization. Modify
821 * request state to done. */
823 sclp_check_handler(__u16 code
)
827 finished_sccb
= S390_lowcore
.ext_params
& 0xfffffff8;
828 /* Is this the interrupt we are waiting for? */
829 if (finished_sccb
== 0)
831 if (finished_sccb
!= (u32
) (addr_t
) sclp_init_sccb
)
832 panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
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. */
844 sclp_check_timeout(unsigned long data
)
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. */
860 sclp_check_interface(void)
862 struct init_sccb
*sccb
;
867 spin_lock_irqsave(&sclp_lock
, flags
);
868 /* Prepare init mask command */
869 rc
= register_early_external_interrupt(0x2401, sclp_check_handler
,
872 spin_unlock_irqrestore(&sclp_lock
, flags
);
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
);
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. */
889 /* Wait for signal from interrupt or timeout */
891 /* Disable service-signal interruption - needs to happen
892 * with IRQs enabled. */
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) {
903 unregister_early_external_interrupt(0x2401, sclp_check_handler
,
905 spin_unlock_irqrestore(&sclp_lock
, flags
);
909 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
910 * events from interfering with rebooted system. */
912 sclp_reboot_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
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
;
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
);
938 spin_lock_irqsave(&sclp_lock
, flags
);
939 list_for_each_entry(reg
, &sclp_reg_list
, list
) {
940 if (rollback
&& reg
->pm_event_posted
)
942 if (!rollback
&& !reg
->pm_event_posted
)
945 spin_unlock_irqrestore(&sclp_lock
, flags
);
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;
956 * Susend/resume callbacks for platform device
959 static int sclp_freeze(struct device
*dev
)
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
);
978 wait_for_completion(&sclp_request_queue_flushed
);
979 else if (rc
!= -ENODATA
)
982 rc
= sclp_deactivate();
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);
995 static int sclp_undo_suspend(enum sclp_pm_event event
)
1000 rc
= sclp_reactivate();
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);
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 struct dev_pm_ops sclp_pm_ops
= {
1023 .freeze
= sclp_freeze
,
1025 .restore
= sclp_restore
,
1028 static struct platform_driver sclp_pdrv
= {
1031 .owner
= THIS_MODULE
,
1036 static struct platform_device
*sclp_pdev
;
1038 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
1043 unsigned long flags
;
1046 spin_lock_irqsave(&sclp_lock
, flags
);
1047 /* Check for previous or running initialization */
1048 if (sclp_init_state
!= sclp_init_state_uninitialized
)
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
);
1061 goto fail_init_state_uninitialized
;
1062 /* Register reboot handler */
1063 rc
= register_reboot_notifier(&sclp_reboot_notifier
);
1065 goto fail_init_state_uninitialized
;
1066 /* Register interrupt handler */
1067 rc
= register_early_external_interrupt(0x2401, sclp_interrupt_handler
,
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
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
;
1084 spin_unlock_irqrestore(&sclp_lock
, flags
);
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
);
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)
1109 rc
= platform_driver_register(&sclp_pdrv
);
1112 sclp_pdev
= platform_device_register_simple("sclp", -1, NULL
, 0);
1113 rc
= IS_ERR(sclp_pdev
) ? PTR_ERR(sclp_pdev
) : 0;
1115 goto fail_platform_driver_unregister
;
1116 rc
= atomic_notifier_chain_register(&panic_notifier_list
,
1119 goto fail_platform_device_unregister
;
1123 fail_platform_device_unregister
:
1124 platform_device_unregister(sclp_pdev
);
1125 fail_platform_driver_unregister
:
1126 platform_driver_unregister(&sclp_pdrv
);
1130 arch_initcall(sclp_initcall
);