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/kernel_stat.h>
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 <linux/suspend.h>
20 #include <linux/completion.h>
21 #include <linux/platform_device.h>
22 #include <asm/s390_ext.h>
23 #include <asm/types.h>
28 #define SCLP_HEADER "sclp: "
30 /* Lock to protect internal data consistency. */
31 static DEFINE_SPINLOCK(sclp_lock
);
33 /* Mask of events that we can send to the sclp interface. */
34 static sccb_mask_t sclp_receive_mask
;
36 /* Mask of events that we can receive from the sclp interface. */
37 static sccb_mask_t sclp_send_mask
;
39 /* List of registered event listeners and senders. */
40 static struct list_head sclp_reg_list
;
42 /* List of queued requests. */
43 static struct list_head sclp_req_queue
;
45 /* Data for read and and init requests. */
46 static struct sclp_req sclp_read_req
;
47 static struct sclp_req sclp_init_req
;
48 static char sclp_read_sccb
[PAGE_SIZE
] __attribute__((__aligned__(PAGE_SIZE
)));
49 static char sclp_init_sccb
[PAGE_SIZE
] __attribute__((__aligned__(PAGE_SIZE
)));
52 static DECLARE_COMPLETION(sclp_request_queue_flushed
);
54 static void sclp_suspend_req_cb(struct sclp_req
*req
, void *data
)
56 complete(&sclp_request_queue_flushed
);
59 static struct sclp_req sclp_suspend_req
;
61 /* Timer for request retries. */
62 static struct timer_list sclp_request_timer
;
64 /* Internal state: is the driver initialized? */
65 static volatile enum sclp_init_state_t
{
66 sclp_init_state_uninitialized
,
67 sclp_init_state_initializing
,
68 sclp_init_state_initialized
69 } sclp_init_state
= sclp_init_state_uninitialized
;
71 /* Internal state: is a request active at the sclp? */
72 static volatile enum sclp_running_state_t
{
73 sclp_running_state_idle
,
74 sclp_running_state_running
,
75 sclp_running_state_reset_pending
76 } sclp_running_state
= sclp_running_state_idle
;
78 /* Internal state: is a read request pending? */
79 static volatile enum sclp_reading_state_t
{
80 sclp_reading_state_idle
,
81 sclp_reading_state_reading
82 } sclp_reading_state
= sclp_reading_state_idle
;
84 /* Internal state: is the driver currently serving requests? */
85 static volatile enum sclp_activation_state_t
{
86 sclp_activation_state_active
,
87 sclp_activation_state_deactivating
,
88 sclp_activation_state_inactive
,
89 sclp_activation_state_activating
90 } sclp_activation_state
= sclp_activation_state_active
;
92 /* Internal state: is an init mask request pending? */
93 static volatile enum sclp_mask_state_t
{
95 sclp_mask_state_initializing
96 } sclp_mask_state
= sclp_mask_state_idle
;
98 /* Internal state: is the driver suspended? */
99 static enum sclp_suspend_state_t
{
100 sclp_suspend_state_running
,
101 sclp_suspend_state_suspended
,
102 } sclp_suspend_state
= sclp_suspend_state_running
;
104 /* Maximum retry counts */
105 #define SCLP_INIT_RETRY 3
106 #define SCLP_MASK_RETRY 3
108 /* Timeout intervals in seconds.*/
109 #define SCLP_BUSY_INTERVAL 10
110 #define SCLP_RETRY_INTERVAL 30
112 static void sclp_process_queue(void);
113 static void __sclp_make_read_req(void);
114 static int sclp_init_mask(int calculate
);
115 static int sclp_init(void);
117 /* Perform service call. Return 0 on success, non-zero otherwise. */
119 sclp_service_call(sclp_cmdw_t command
, void *sccb
)
124 " .insn rre,0xb2200000,%1,%2\n" /* servc %1,%2 */
127 : "=&d" (cc
) : "d" (command
), "a" (__pa(sccb
))
138 __sclp_queue_read_req(void)
140 if (sclp_reading_state
== sclp_reading_state_idle
) {
141 sclp_reading_state
= sclp_reading_state_reading
;
142 __sclp_make_read_req();
143 /* Add request to head of queue */
144 list_add(&sclp_read_req
.list
, &sclp_req_queue
);
148 /* Set up request retry timer. Called while sclp_lock is locked. */
150 __sclp_set_request_timer(unsigned long time
, void (*function
)(unsigned long),
153 del_timer(&sclp_request_timer
);
154 sclp_request_timer
.function
= function
;
155 sclp_request_timer
.data
= data
;
156 sclp_request_timer
.expires
= jiffies
+ time
;
157 add_timer(&sclp_request_timer
);
160 /* Request timeout handler. Restart the request queue. If DATA is non-zero,
161 * force restart of running request. */
163 sclp_request_timeout(unsigned long data
)
167 spin_lock_irqsave(&sclp_lock
, flags
);
169 if (sclp_running_state
== sclp_running_state_running
) {
170 /* Break running state and queue NOP read event request
171 * to get a defined interface state. */
172 __sclp_queue_read_req();
173 sclp_running_state
= sclp_running_state_idle
;
176 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
177 sclp_request_timeout
, 0);
179 spin_unlock_irqrestore(&sclp_lock
, flags
);
180 sclp_process_queue();
183 /* Try to start a request. Return zero if the request was successfully
184 * started or if it will be started at a later time. Return non-zero otherwise.
185 * Called while sclp_lock is locked. */
187 __sclp_start_request(struct sclp_req
*req
)
191 if (sclp_running_state
!= sclp_running_state_idle
)
193 del_timer(&sclp_request_timer
);
194 rc
= sclp_service_call(req
->command
, req
->sccb
);
198 /* Successfully started request */
199 req
->status
= SCLP_REQ_RUNNING
;
200 sclp_running_state
= sclp_running_state_running
;
201 __sclp_set_request_timer(SCLP_RETRY_INTERVAL
* HZ
,
202 sclp_request_timeout
, 1);
204 } else if (rc
== -EBUSY
) {
205 /* Try again later */
206 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
207 sclp_request_timeout
, 0);
211 req
->status
= SCLP_REQ_FAILED
;
215 /* Try to start queued requests. */
217 sclp_process_queue(void)
219 struct sclp_req
*req
;
223 spin_lock_irqsave(&sclp_lock
, flags
);
224 if (sclp_running_state
!= sclp_running_state_idle
) {
225 spin_unlock_irqrestore(&sclp_lock
, flags
);
228 del_timer(&sclp_request_timer
);
229 while (!list_empty(&sclp_req_queue
)) {
230 req
= list_entry(sclp_req_queue
.next
, struct sclp_req
, list
);
233 rc
= __sclp_start_request(req
);
237 if (req
->start_count
> 1) {
238 /* Cannot abort already submitted request - could still
239 * be active at the SCLP */
240 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
241 sclp_request_timeout
, 0);
245 /* Post-processing for aborted request */
246 list_del(&req
->list
);
248 spin_unlock_irqrestore(&sclp_lock
, flags
);
249 req
->callback(req
, req
->callback_data
);
250 spin_lock_irqsave(&sclp_lock
, flags
);
253 spin_unlock_irqrestore(&sclp_lock
, flags
);
256 static int __sclp_can_add_request(struct sclp_req
*req
)
258 if (req
== &sclp_suspend_req
|| req
== &sclp_init_req
)
260 if (sclp_suspend_state
!= sclp_suspend_state_running
)
262 if (sclp_init_state
!= sclp_init_state_initialized
)
264 if (sclp_activation_state
!= sclp_activation_state_active
)
269 /* Queue a new request. Return zero on success, non-zero otherwise. */
271 sclp_add_request(struct sclp_req
*req
)
276 spin_lock_irqsave(&sclp_lock
, flags
);
277 if (!__sclp_can_add_request(req
)) {
278 spin_unlock_irqrestore(&sclp_lock
, flags
);
281 req
->status
= SCLP_REQ_QUEUED
;
282 req
->start_count
= 0;
283 list_add_tail(&req
->list
, &sclp_req_queue
);
285 /* Start if request is first in list */
286 if (sclp_running_state
== sclp_running_state_idle
&&
287 req
->list
.prev
== &sclp_req_queue
) {
289 list_del(&req
->list
);
293 rc
= __sclp_start_request(req
);
295 list_del(&req
->list
);
298 spin_unlock_irqrestore(&sclp_lock
, flags
);
302 EXPORT_SYMBOL(sclp_add_request
);
304 /* Dispatch events found in request buffer to registered listeners. Return 0
305 * if all events were dispatched, non-zero otherwise. */
307 sclp_dispatch_evbufs(struct sccb_header
*sccb
)
310 struct evbuf_header
*evbuf
;
312 struct sclp_register
*reg
;
316 spin_lock_irqsave(&sclp_lock
, flags
);
318 for (offset
= sizeof(struct sccb_header
); offset
< sccb
->length
;
319 offset
+= evbuf
->length
) {
320 evbuf
= (struct evbuf_header
*) ((addr_t
) sccb
+ offset
);
321 /* Check for malformed hardware response */
322 if (evbuf
->length
== 0)
324 /* Search for event handler */
326 list_for_each(l
, &sclp_reg_list
) {
327 reg
= list_entry(l
, struct sclp_register
, list
);
328 if (reg
->receive_mask
& (1 << (32 - evbuf
->type
)))
333 if (reg
&& reg
->receiver_fn
) {
334 spin_unlock_irqrestore(&sclp_lock
, flags
);
335 reg
->receiver_fn(evbuf
);
336 spin_lock_irqsave(&sclp_lock
, flags
);
337 } else if (reg
== NULL
)
340 spin_unlock_irqrestore(&sclp_lock
, flags
);
344 /* Read event data request callback. */
346 sclp_read_cb(struct sclp_req
*req
, void *data
)
349 struct sccb_header
*sccb
;
351 sccb
= (struct sccb_header
*) req
->sccb
;
352 if (req
->status
== SCLP_REQ_DONE
&& (sccb
->response_code
== 0x20 ||
353 sccb
->response_code
== 0x220))
354 sclp_dispatch_evbufs(sccb
);
355 spin_lock_irqsave(&sclp_lock
, flags
);
356 sclp_reading_state
= sclp_reading_state_idle
;
357 spin_unlock_irqrestore(&sclp_lock
, flags
);
360 /* Prepare read event data request. Called while sclp_lock is locked. */
361 static void __sclp_make_read_req(void)
363 struct sccb_header
*sccb
;
365 sccb
= (struct sccb_header
*) sclp_read_sccb
;
367 memset(&sclp_read_req
, 0, sizeof(struct sclp_req
));
368 sclp_read_req
.command
= SCLP_CMDW_READ_EVENT_DATA
;
369 sclp_read_req
.status
= SCLP_REQ_QUEUED
;
370 sclp_read_req
.start_count
= 0;
371 sclp_read_req
.callback
= sclp_read_cb
;
372 sclp_read_req
.sccb
= sccb
;
373 sccb
->length
= PAGE_SIZE
;
374 sccb
->function_code
= 0;
375 sccb
->control_mask
[2] = 0x80;
378 /* Search request list for request with matching sccb. Return request if found,
379 * NULL otherwise. Called while sclp_lock is locked. */
380 static inline struct sclp_req
*
381 __sclp_find_req(u32 sccb
)
384 struct sclp_req
*req
;
386 list_for_each(l
, &sclp_req_queue
) {
387 req
= list_entry(l
, struct sclp_req
, list
);
388 if (sccb
== (u32
) (addr_t
) req
->sccb
)
394 /* Handler for external interruption. Perform request post-processing.
395 * Prepare read event data request if necessary. Start processing of next
396 * request on queue. */
397 static void sclp_interrupt_handler(unsigned int ext_int_code
,
398 unsigned int param32
, unsigned long param64
)
400 struct sclp_req
*req
;
404 kstat_cpu(smp_processor_id()).irqs
[EXTINT_SCP
]++;
405 spin_lock(&sclp_lock
);
406 finished_sccb
= param32
& 0xfffffff8;
407 evbuf_pending
= param32
& 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 __arch_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. */
822 static void sclp_check_handler(unsigned int ext_int_code
,
823 unsigned int param32
, unsigned long param64
)
827 kstat_cpu(smp_processor_id()).irqs
[EXTINT_SCP
]++;
828 finished_sccb
= param32
& 0xfffffff8;
829 /* Is this the interrupt we are waiting for? */
830 if (finished_sccb
== 0)
832 if (finished_sccb
!= (u32
) (addr_t
) sclp_init_sccb
)
833 panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
835 spin_lock(&sclp_lock
);
836 if (sclp_running_state
== sclp_running_state_running
) {
837 sclp_init_req
.status
= SCLP_REQ_DONE
;
838 sclp_running_state
= sclp_running_state_idle
;
840 spin_unlock(&sclp_lock
);
843 /* Initial init mask request timed out. Modify request state to failed. */
845 sclp_check_timeout(unsigned long data
)
849 spin_lock_irqsave(&sclp_lock
, flags
);
850 if (sclp_running_state
== sclp_running_state_running
) {
851 sclp_init_req
.status
= SCLP_REQ_FAILED
;
852 sclp_running_state
= sclp_running_state_idle
;
854 spin_unlock_irqrestore(&sclp_lock
, flags
);
857 /* Perform a check of the SCLP interface. Return zero if the interface is
858 * available and there are no pending requests from a previous instance.
859 * Return non-zero otherwise. */
861 sclp_check_interface(void)
863 struct init_sccb
*sccb
;
868 spin_lock_irqsave(&sclp_lock
, flags
);
869 /* Prepare init mask command */
870 rc
= register_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_external_interrupt(0x2401, sclp_check_handler
);
904 spin_unlock_irqrestore(&sclp_lock
, flags
);
908 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
909 * events from interfering with rebooted system. */
911 sclp_reboot_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
917 static struct notifier_block sclp_reboot_notifier
= {
918 .notifier_call
= sclp_reboot_event
922 * Suspend/resume SCLP notifier implementation
925 static void sclp_pm_event(enum sclp_pm_event sclp_pm_event
, int rollback
)
927 struct sclp_register
*reg
;
931 spin_lock_irqsave(&sclp_lock
, flags
);
932 list_for_each_entry(reg
, &sclp_reg_list
, list
)
933 reg
->pm_event_posted
= 0;
934 spin_unlock_irqrestore(&sclp_lock
, flags
);
937 spin_lock_irqsave(&sclp_lock
, flags
);
938 list_for_each_entry(reg
, &sclp_reg_list
, list
) {
939 if (rollback
&& reg
->pm_event_posted
)
941 if (!rollback
&& !reg
->pm_event_posted
)
944 spin_unlock_irqrestore(&sclp_lock
, flags
);
947 spin_unlock_irqrestore(&sclp_lock
, flags
);
948 if (reg
->pm_event_fn
)
949 reg
->pm_event_fn(reg
, sclp_pm_event
);
950 reg
->pm_event_posted
= rollback
? 0 : 1;
955 * Susend/resume callbacks for platform device
958 static int sclp_freeze(struct device
*dev
)
963 sclp_pm_event(SCLP_PM_EVENT_FREEZE
, 0);
965 spin_lock_irqsave(&sclp_lock
, flags
);
966 sclp_suspend_state
= sclp_suspend_state_suspended
;
967 spin_unlock_irqrestore(&sclp_lock
, flags
);
969 /* Init supend data */
970 memset(&sclp_suspend_req
, 0, sizeof(sclp_suspend_req
));
971 sclp_suspend_req
.callback
= sclp_suspend_req_cb
;
972 sclp_suspend_req
.status
= SCLP_REQ_FILLED
;
973 init_completion(&sclp_request_queue_flushed
);
975 rc
= sclp_add_request(&sclp_suspend_req
);
977 wait_for_completion(&sclp_request_queue_flushed
);
978 else if (rc
!= -ENODATA
)
981 rc
= sclp_deactivate();
987 spin_lock_irqsave(&sclp_lock
, flags
);
988 sclp_suspend_state
= sclp_suspend_state_running
;
989 spin_unlock_irqrestore(&sclp_lock
, flags
);
990 sclp_pm_event(SCLP_PM_EVENT_THAW
, 1);
994 static int sclp_undo_suspend(enum sclp_pm_event event
)
999 rc
= sclp_reactivate();
1003 spin_lock_irqsave(&sclp_lock
, flags
);
1004 sclp_suspend_state
= sclp_suspend_state_running
;
1005 spin_unlock_irqrestore(&sclp_lock
, flags
);
1007 sclp_pm_event(event
, 0);
1011 static int sclp_thaw(struct device
*dev
)
1013 return sclp_undo_suspend(SCLP_PM_EVENT_THAW
);
1016 static int sclp_restore(struct device
*dev
)
1018 return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE
);
1021 static const struct dev_pm_ops sclp_pm_ops
= {
1022 .freeze
= sclp_freeze
,
1024 .restore
= sclp_restore
,
1027 static struct platform_driver sclp_pdrv
= {
1030 .owner
= THIS_MODULE
,
1035 static struct platform_device
*sclp_pdev
;
1037 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
1042 unsigned long flags
;
1045 spin_lock_irqsave(&sclp_lock
, flags
);
1046 /* Check for previous or running initialization */
1047 if (sclp_init_state
!= sclp_init_state_uninitialized
)
1049 sclp_init_state
= sclp_init_state_initializing
;
1050 /* Set up variables */
1051 INIT_LIST_HEAD(&sclp_req_queue
);
1052 INIT_LIST_HEAD(&sclp_reg_list
);
1053 list_add(&sclp_state_change_event
.list
, &sclp_reg_list
);
1054 init_timer(&sclp_request_timer
);
1055 /* Check interface */
1056 spin_unlock_irqrestore(&sclp_lock
, flags
);
1057 rc
= sclp_check_interface();
1058 spin_lock_irqsave(&sclp_lock
, flags
);
1060 goto fail_init_state_uninitialized
;
1061 /* Register reboot handler */
1062 rc
= register_reboot_notifier(&sclp_reboot_notifier
);
1064 goto fail_init_state_uninitialized
;
1065 /* Register interrupt handler */
1066 rc
= register_external_interrupt(0x2401, sclp_interrupt_handler
);
1068 goto fail_unregister_reboot_notifier
;
1069 sclp_init_state
= sclp_init_state_initialized
;
1070 spin_unlock_irqrestore(&sclp_lock
, flags
);
1071 /* Enable service-signal external interruption - needs to happen with
1077 fail_unregister_reboot_notifier
:
1078 unregister_reboot_notifier(&sclp_reboot_notifier
);
1079 fail_init_state_uninitialized
:
1080 sclp_init_state
= sclp_init_state_uninitialized
;
1082 spin_unlock_irqrestore(&sclp_lock
, flags
);
1087 * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
1088 * to print the panic message.
1090 static int sclp_panic_notify(struct notifier_block
*self
,
1091 unsigned long event
, void *data
)
1093 if (sclp_suspend_state
== sclp_suspend_state_suspended
)
1094 sclp_undo_suspend(SCLP_PM_EVENT_THAW
);
1098 static struct notifier_block sclp_on_panic_nb
= {
1099 .notifier_call
= sclp_panic_notify
,
1100 .priority
= SCLP_PANIC_PRIO
,
1103 static __init
int sclp_initcall(void)
1107 rc
= platform_driver_register(&sclp_pdrv
);
1110 sclp_pdev
= platform_device_register_simple("sclp", -1, NULL
, 0);
1111 rc
= IS_ERR(sclp_pdev
) ? PTR_ERR(sclp_pdev
) : 0;
1113 goto fail_platform_driver_unregister
;
1114 rc
= atomic_notifier_chain_register(&panic_notifier_list
,
1117 goto fail_platform_device_unregister
;
1121 fail_platform_device_unregister
:
1122 platform_device_unregister(sclp_pdev
);
1123 fail_platform_driver_unregister
:
1124 platform_driver_unregister(&sclp_pdrv
);
1128 arch_initcall(sclp_initcall
);