1 // SPDX-License-Identifier: GPL-2.0
3 * core function to access sclp interface
5 * Copyright IBM Corp. 1999, 2009
7 * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8 * Martin Schwidefsky <schwidefsky@de.ibm.com>
11 #include <linux/kernel_stat.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/spinlock.h>
15 #include <linux/interrupt.h>
16 #include <linux/timer.h>
17 #include <linux/reboot.h>
18 #include <linux/jiffies.h>
19 #include <linux/init.h>
20 #include <linux/suspend.h>
21 #include <linux/completion.h>
22 #include <linux/platform_device.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 /* Number of console pages to allocate, used by sclp_con.c and sclp_vt220.c */
55 int sclp_console_pages
= SCLP_CONSOLE_PAGES
;
56 /* Flag to indicate if buffer pages are dropped on buffer full condition */
57 int sclp_console_drop
= 1;
58 /* Number of times the console dropped buffer pages */
59 unsigned long sclp_console_full
;
61 static void sclp_suspend_req_cb(struct sclp_req
*req
, void *data
)
63 complete(&sclp_request_queue_flushed
);
66 static int __init
sclp_setup_console_pages(char *str
)
70 rc
= kstrtoint(str
, 0, &pages
);
71 if (!rc
&& pages
>= SCLP_CONSOLE_PAGES
)
72 sclp_console_pages
= pages
;
76 __setup("sclp_con_pages=", sclp_setup_console_pages
);
78 static int __init
sclp_setup_console_drop(char *str
)
82 rc
= kstrtoint(str
, 0, &drop
);
84 sclp_console_drop
= drop
;
88 __setup("sclp_con_drop=", sclp_setup_console_drop
);
90 static struct sclp_req sclp_suspend_req
;
92 /* Timer for request retries. */
93 static struct timer_list sclp_request_timer
;
95 /* Timer for queued requests. */
96 static struct timer_list sclp_queue_timer
;
98 /* Internal state: is a request active at the sclp? */
99 static volatile enum sclp_running_state_t
{
100 sclp_running_state_idle
,
101 sclp_running_state_running
,
102 sclp_running_state_reset_pending
103 } sclp_running_state
= sclp_running_state_idle
;
105 /* Internal state: is a read request pending? */
106 static volatile enum sclp_reading_state_t
{
107 sclp_reading_state_idle
,
108 sclp_reading_state_reading
109 } sclp_reading_state
= sclp_reading_state_idle
;
111 /* Internal state: is the driver currently serving requests? */
112 static volatile enum sclp_activation_state_t
{
113 sclp_activation_state_active
,
114 sclp_activation_state_deactivating
,
115 sclp_activation_state_inactive
,
116 sclp_activation_state_activating
117 } sclp_activation_state
= sclp_activation_state_active
;
119 /* Internal state: is an init mask request pending? */
120 static volatile enum sclp_mask_state_t
{
121 sclp_mask_state_idle
,
122 sclp_mask_state_initializing
123 } sclp_mask_state
= sclp_mask_state_idle
;
125 /* Internal state: is the driver suspended? */
126 static enum sclp_suspend_state_t
{
127 sclp_suspend_state_running
,
128 sclp_suspend_state_suspended
,
129 } sclp_suspend_state
= sclp_suspend_state_running
;
131 /* Maximum retry counts */
132 #define SCLP_INIT_RETRY 3
133 #define SCLP_MASK_RETRY 3
135 /* Timeout intervals in seconds.*/
136 #define SCLP_BUSY_INTERVAL 10
137 #define SCLP_RETRY_INTERVAL 30
139 static void sclp_request_timeout(bool force_restart
);
140 static void sclp_process_queue(void);
141 static void __sclp_make_read_req(void);
142 static int sclp_init_mask(int calculate
);
143 static int sclp_init(void);
146 __sclp_queue_read_req(void)
148 if (sclp_reading_state
== sclp_reading_state_idle
) {
149 sclp_reading_state
= sclp_reading_state_reading
;
150 __sclp_make_read_req();
151 /* Add request to head of queue */
152 list_add(&sclp_read_req
.list
, &sclp_req_queue
);
156 /* Set up request retry timer. Called while sclp_lock is locked. */
158 __sclp_set_request_timer(unsigned long time
, void (*cb
)(struct timer_list
*))
160 del_timer(&sclp_request_timer
);
161 sclp_request_timer
.function
= cb
;
162 sclp_request_timer
.expires
= jiffies
+ time
;
163 add_timer(&sclp_request_timer
);
166 static void sclp_request_timeout_restart(struct timer_list
*unused
)
168 sclp_request_timeout(true);
171 static void sclp_request_timeout_normal(struct timer_list
*unused
)
173 sclp_request_timeout(false);
176 /* Request timeout handler. Restart the request queue. If force_restart,
177 * force restart of running request. */
178 static void sclp_request_timeout(bool force_restart
)
182 spin_lock_irqsave(&sclp_lock
, flags
);
184 if (sclp_running_state
== sclp_running_state_running
) {
185 /* Break running state and queue NOP read event request
186 * to get a defined interface state. */
187 __sclp_queue_read_req();
188 sclp_running_state
= sclp_running_state_idle
;
191 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
192 sclp_request_timeout_normal
);
194 spin_unlock_irqrestore(&sclp_lock
, flags
);
195 sclp_process_queue();
199 * Returns the expire value in jiffies of the next pending request timeout,
200 * if any. Needs to be called with sclp_lock.
202 static unsigned long __sclp_req_queue_find_next_timeout(void)
204 unsigned long expires_next
= 0;
205 struct sclp_req
*req
;
207 list_for_each_entry(req
, &sclp_req_queue
, list
) {
208 if (!req
->queue_expires
)
211 (time_before(req
->queue_expires
, expires_next
)))
212 expires_next
= req
->queue_expires
;
218 * Returns expired request, if any, and removes it from the list.
220 static struct sclp_req
*__sclp_req_queue_remove_expired_req(void)
222 unsigned long flags
, now
;
223 struct sclp_req
*req
;
225 spin_lock_irqsave(&sclp_lock
, flags
);
227 /* Don't need list_for_each_safe because we break out after list_del */
228 list_for_each_entry(req
, &sclp_req_queue
, list
) {
229 if (!req
->queue_expires
)
231 if (time_before_eq(req
->queue_expires
, now
)) {
232 if (req
->status
== SCLP_REQ_QUEUED
) {
233 req
->status
= SCLP_REQ_QUEUED_TIMEOUT
;
234 list_del(&req
->list
);
241 spin_unlock_irqrestore(&sclp_lock
, flags
);
246 * Timeout handler for queued requests. Removes request from list and
247 * invokes callback. This timer can be set per request in situations where
248 * waiting too long would be harmful to the system, e.g. during SE reboot.
250 static void sclp_req_queue_timeout(struct timer_list
*unused
)
252 unsigned long flags
, expires_next
;
253 struct sclp_req
*req
;
256 req
= __sclp_req_queue_remove_expired_req();
257 if (req
&& req
->callback
)
258 req
->callback(req
, req
->callback_data
);
261 spin_lock_irqsave(&sclp_lock
, flags
);
262 expires_next
= __sclp_req_queue_find_next_timeout();
264 mod_timer(&sclp_queue_timer
, expires_next
);
265 spin_unlock_irqrestore(&sclp_lock
, flags
);
268 /* Try to start a request. Return zero if the request was successfully
269 * started or if it will be started at a later time. Return non-zero otherwise.
270 * Called while sclp_lock is locked. */
272 __sclp_start_request(struct sclp_req
*req
)
276 if (sclp_running_state
!= sclp_running_state_idle
)
278 del_timer(&sclp_request_timer
);
279 rc
= sclp_service_call(req
->command
, req
->sccb
);
283 /* Successfully started request */
284 req
->status
= SCLP_REQ_RUNNING
;
285 sclp_running_state
= sclp_running_state_running
;
286 __sclp_set_request_timer(SCLP_RETRY_INTERVAL
* HZ
,
287 sclp_request_timeout_restart
);
289 } else if (rc
== -EBUSY
) {
290 /* Try again later */
291 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
292 sclp_request_timeout_normal
);
296 req
->status
= SCLP_REQ_FAILED
;
300 /* Try to start queued requests. */
302 sclp_process_queue(void)
304 struct sclp_req
*req
;
308 spin_lock_irqsave(&sclp_lock
, flags
);
309 if (sclp_running_state
!= sclp_running_state_idle
) {
310 spin_unlock_irqrestore(&sclp_lock
, flags
);
313 del_timer(&sclp_request_timer
);
314 while (!list_empty(&sclp_req_queue
)) {
315 req
= list_entry(sclp_req_queue
.next
, struct sclp_req
, list
);
318 rc
= __sclp_start_request(req
);
322 if (req
->start_count
> 1) {
323 /* Cannot abort already submitted request - could still
324 * be active at the SCLP */
325 __sclp_set_request_timer(SCLP_BUSY_INTERVAL
* HZ
,
326 sclp_request_timeout_normal
);
330 /* Post-processing for aborted request */
331 list_del(&req
->list
);
333 spin_unlock_irqrestore(&sclp_lock
, flags
);
334 req
->callback(req
, req
->callback_data
);
335 spin_lock_irqsave(&sclp_lock
, flags
);
338 spin_unlock_irqrestore(&sclp_lock
, flags
);
341 static int __sclp_can_add_request(struct sclp_req
*req
)
343 if (req
== &sclp_suspend_req
|| req
== &sclp_init_req
)
345 if (sclp_suspend_state
!= sclp_suspend_state_running
)
347 if (sclp_init_state
!= sclp_init_state_initialized
)
349 if (sclp_activation_state
!= sclp_activation_state_active
)
354 /* Queue a new request. Return zero on success, non-zero otherwise. */
356 sclp_add_request(struct sclp_req
*req
)
361 spin_lock_irqsave(&sclp_lock
, flags
);
362 if (!__sclp_can_add_request(req
)) {
363 spin_unlock_irqrestore(&sclp_lock
, flags
);
366 req
->status
= SCLP_REQ_QUEUED
;
367 req
->start_count
= 0;
368 list_add_tail(&req
->list
, &sclp_req_queue
);
370 if (req
->queue_timeout
) {
371 req
->queue_expires
= jiffies
+ req
->queue_timeout
* HZ
;
372 if (!timer_pending(&sclp_queue_timer
) ||
373 time_after(sclp_queue_timer
.expires
, req
->queue_expires
))
374 mod_timer(&sclp_queue_timer
, req
->queue_expires
);
376 req
->queue_expires
= 0;
377 /* Start if request is first in list */
378 if (sclp_running_state
== sclp_running_state_idle
&&
379 req
->list
.prev
== &sclp_req_queue
) {
381 list_del(&req
->list
);
385 rc
= __sclp_start_request(req
);
387 list_del(&req
->list
);
390 spin_unlock_irqrestore(&sclp_lock
, flags
);
394 EXPORT_SYMBOL(sclp_add_request
);
396 /* Dispatch events found in request buffer to registered listeners. Return 0
397 * if all events were dispatched, non-zero otherwise. */
399 sclp_dispatch_evbufs(struct sccb_header
*sccb
)
402 struct evbuf_header
*evbuf
;
404 struct sclp_register
*reg
;
408 spin_lock_irqsave(&sclp_lock
, flags
);
410 for (offset
= sizeof(struct sccb_header
); offset
< sccb
->length
;
411 offset
+= evbuf
->length
) {
412 evbuf
= (struct evbuf_header
*) ((addr_t
) sccb
+ offset
);
413 /* Check for malformed hardware response */
414 if (evbuf
->length
== 0)
416 /* Search for event handler */
418 list_for_each(l
, &sclp_reg_list
) {
419 reg
= list_entry(l
, struct sclp_register
, list
);
420 if (reg
->receive_mask
& (1 << (32 - evbuf
->type
)))
425 if (reg
&& reg
->receiver_fn
) {
426 spin_unlock_irqrestore(&sclp_lock
, flags
);
427 reg
->receiver_fn(evbuf
);
428 spin_lock_irqsave(&sclp_lock
, flags
);
429 } else if (reg
== NULL
)
432 spin_unlock_irqrestore(&sclp_lock
, flags
);
436 /* Read event data request callback. */
438 sclp_read_cb(struct sclp_req
*req
, void *data
)
441 struct sccb_header
*sccb
;
443 sccb
= (struct sccb_header
*) req
->sccb
;
444 if (req
->status
== SCLP_REQ_DONE
&& (sccb
->response_code
== 0x20 ||
445 sccb
->response_code
== 0x220))
446 sclp_dispatch_evbufs(sccb
);
447 spin_lock_irqsave(&sclp_lock
, flags
);
448 sclp_reading_state
= sclp_reading_state_idle
;
449 spin_unlock_irqrestore(&sclp_lock
, flags
);
452 /* Prepare read event data request. Called while sclp_lock is locked. */
453 static void __sclp_make_read_req(void)
455 struct sccb_header
*sccb
;
457 sccb
= (struct sccb_header
*) sclp_read_sccb
;
459 memset(&sclp_read_req
, 0, sizeof(struct sclp_req
));
460 sclp_read_req
.command
= SCLP_CMDW_READ_EVENT_DATA
;
461 sclp_read_req
.status
= SCLP_REQ_QUEUED
;
462 sclp_read_req
.start_count
= 0;
463 sclp_read_req
.callback
= sclp_read_cb
;
464 sclp_read_req
.sccb
= sccb
;
465 sccb
->length
= PAGE_SIZE
;
466 sccb
->function_code
= 0;
467 sccb
->control_mask
[2] = 0x80;
470 /* Search request list for request with matching sccb. Return request if found,
471 * NULL otherwise. Called while sclp_lock is locked. */
472 static inline struct sclp_req
*
473 __sclp_find_req(u32 sccb
)
476 struct sclp_req
*req
;
478 list_for_each(l
, &sclp_req_queue
) {
479 req
= list_entry(l
, struct sclp_req
, list
);
480 if (sccb
== (u32
) (addr_t
) req
->sccb
)
486 /* Handler for external interruption. Perform request post-processing.
487 * Prepare read event data request if necessary. Start processing of next
488 * request on queue. */
489 static void sclp_interrupt_handler(struct ext_code ext_code
,
490 unsigned int param32
, unsigned long param64
)
492 struct sclp_req
*req
;
496 inc_irq_stat(IRQEXT_SCP
);
497 spin_lock(&sclp_lock
);
498 finished_sccb
= param32
& 0xfffffff8;
499 evbuf_pending
= param32
& 0x3;
501 del_timer(&sclp_request_timer
);
502 sclp_running_state
= sclp_running_state_reset_pending
;
503 req
= __sclp_find_req(finished_sccb
);
505 /* Request post-processing */
506 list_del(&req
->list
);
507 req
->status
= SCLP_REQ_DONE
;
509 spin_unlock(&sclp_lock
);
510 req
->callback(req
, req
->callback_data
);
511 spin_lock(&sclp_lock
);
514 sclp_running_state
= sclp_running_state_idle
;
517 sclp_activation_state
== sclp_activation_state_active
)
518 __sclp_queue_read_req();
519 spin_unlock(&sclp_lock
);
520 sclp_process_queue();
523 /* Convert interval in jiffies to TOD ticks. */
525 sclp_tod_from_jiffies(unsigned long jiffies
)
527 return (u64
) (jiffies
/ HZ
) << 32;
530 /* Wait until a currently running request finished. Note: while this function
531 * is running, no timers are served on the calling CPU. */
535 unsigned long long old_tick
;
537 unsigned long cr0
, cr0_sync
;
541 /* We'll be disabling timer interrupts, so we need a custom timeout
544 if (timer_pending(&sclp_request_timer
)) {
545 /* Get timeout TOD value */
546 timeout
= get_tod_clock_fast() +
547 sclp_tod_from_jiffies(sclp_request_timer
.expires
-
550 local_irq_save(flags
);
551 /* Prevent bottom half from executing once we force interrupts open */
552 irq_context
= in_interrupt();
555 /* Enable service-signal interruption, disable timer interrupts */
556 old_tick
= local_tick_disable();
558 __ctl_store(cr0
, 0, 0);
559 cr0_sync
= cr0
& ~CR0_IRQ_SUBCLASS_MASK
;
560 cr0_sync
|= 1UL << (63 - 54);
561 __ctl_load(cr0_sync
, 0, 0);
562 __arch_local_irq_stosm(0x01);
563 /* Loop until driver state indicates finished request */
564 while (sclp_running_state
!= sclp_running_state_idle
) {
565 /* Check for expired request timer */
566 if (timer_pending(&sclp_request_timer
) &&
567 get_tod_clock_fast() > timeout
&&
568 del_timer(&sclp_request_timer
))
569 sclp_request_timer
.function(&sclp_request_timer
);
573 __ctl_load(cr0
, 0, 0);
576 local_tick_enable(old_tick
);
577 local_irq_restore(flags
);
579 EXPORT_SYMBOL(sclp_sync_wait
);
581 /* Dispatch changes in send and receive mask to registered listeners. */
583 sclp_dispatch_state_change(void)
586 struct sclp_register
*reg
;
588 sccb_mask_t receive_mask
;
589 sccb_mask_t send_mask
;
592 spin_lock_irqsave(&sclp_lock
, flags
);
594 list_for_each(l
, &sclp_reg_list
) {
595 reg
= list_entry(l
, struct sclp_register
, list
);
596 receive_mask
= reg
->send_mask
& sclp_receive_mask
;
597 send_mask
= reg
->receive_mask
& sclp_send_mask
;
598 if (reg
->sclp_receive_mask
!= receive_mask
||
599 reg
->sclp_send_mask
!= send_mask
) {
600 reg
->sclp_receive_mask
= receive_mask
;
601 reg
->sclp_send_mask
= send_mask
;
606 spin_unlock_irqrestore(&sclp_lock
, flags
);
607 if (reg
&& reg
->state_change_fn
)
608 reg
->state_change_fn(reg
);
612 struct sclp_statechangebuf
{
613 struct evbuf_header header
;
614 u8 validity_sclp_active_facility_mask
: 1;
615 u8 validity_sclp_receive_mask
: 1;
616 u8 validity_sclp_send_mask
: 1;
617 u8 validity_read_data_function_mask
: 1;
620 u64 sclp_active_facility_mask
;
621 sccb_mask_t sclp_receive_mask
;
622 sccb_mask_t sclp_send_mask
;
623 u32 read_data_function_mask
;
624 } __attribute__((packed
));
627 /* State change event callback. Inform listeners of changes. */
629 sclp_state_change_cb(struct evbuf_header
*evbuf
)
632 struct sclp_statechangebuf
*scbuf
;
634 scbuf
= (struct sclp_statechangebuf
*) evbuf
;
635 if (scbuf
->mask_length
!= sizeof(sccb_mask_t
))
637 spin_lock_irqsave(&sclp_lock
, flags
);
638 if (scbuf
->validity_sclp_receive_mask
)
639 sclp_receive_mask
= scbuf
->sclp_receive_mask
;
640 if (scbuf
->validity_sclp_send_mask
)
641 sclp_send_mask
= scbuf
->sclp_send_mask
;
642 spin_unlock_irqrestore(&sclp_lock
, flags
);
643 if (scbuf
->validity_sclp_active_facility_mask
)
644 sclp
.facilities
= scbuf
->sclp_active_facility_mask
;
645 sclp_dispatch_state_change();
648 static struct sclp_register sclp_state_change_event
= {
649 .receive_mask
= EVTYP_STATECHANGE_MASK
,
650 .receiver_fn
= sclp_state_change_cb
653 /* Calculate receive and send mask of currently registered listeners.
654 * Called while sclp_lock is locked. */
656 __sclp_get_mask(sccb_mask_t
*receive_mask
, sccb_mask_t
*send_mask
)
659 struct sclp_register
*t
;
663 list_for_each(l
, &sclp_reg_list
) {
664 t
= list_entry(l
, struct sclp_register
, list
);
665 *receive_mask
|= t
->receive_mask
;
666 *send_mask
|= t
->send_mask
;
670 /* Register event listener. Return 0 on success, non-zero otherwise. */
672 sclp_register(struct sclp_register
*reg
)
675 sccb_mask_t receive_mask
;
676 sccb_mask_t send_mask
;
682 spin_lock_irqsave(&sclp_lock
, flags
);
683 /* Check event mask for collisions */
684 __sclp_get_mask(&receive_mask
, &send_mask
);
685 if (reg
->receive_mask
& receive_mask
|| reg
->send_mask
& send_mask
) {
686 spin_unlock_irqrestore(&sclp_lock
, flags
);
689 /* Trigger initial state change callback */
690 reg
->sclp_receive_mask
= 0;
691 reg
->sclp_send_mask
= 0;
692 reg
->pm_event_posted
= 0;
693 list_add(®
->list
, &sclp_reg_list
);
694 spin_unlock_irqrestore(&sclp_lock
, flags
);
695 rc
= sclp_init_mask(1);
697 spin_lock_irqsave(&sclp_lock
, flags
);
698 list_del(®
->list
);
699 spin_unlock_irqrestore(&sclp_lock
, flags
);
704 EXPORT_SYMBOL(sclp_register
);
706 /* Unregister event listener. */
708 sclp_unregister(struct sclp_register
*reg
)
712 spin_lock_irqsave(&sclp_lock
, flags
);
713 list_del(®
->list
);
714 spin_unlock_irqrestore(&sclp_lock
, flags
);
718 EXPORT_SYMBOL(sclp_unregister
);
720 /* Remove event buffers which are marked processed. Return the number of
721 * remaining event buffers. */
723 sclp_remove_processed(struct sccb_header
*sccb
)
725 struct evbuf_header
*evbuf
;
729 evbuf
= (struct evbuf_header
*) (sccb
+ 1);
731 remaining
= sccb
->length
- sizeof(struct sccb_header
);
732 while (remaining
> 0) {
733 remaining
-= evbuf
->length
;
734 if (evbuf
->flags
& 0x80) {
735 sccb
->length
-= evbuf
->length
;
736 memcpy(evbuf
, (void *) ((addr_t
) evbuf
+ evbuf
->length
),
740 evbuf
= (struct evbuf_header
*)
741 ((addr_t
) evbuf
+ evbuf
->length
);
747 EXPORT_SYMBOL(sclp_remove_processed
);
749 /* Prepare init mask request. Called while sclp_lock is locked. */
751 __sclp_make_init_req(u32 receive_mask
, u32 send_mask
)
753 struct init_sccb
*sccb
;
755 sccb
= (struct init_sccb
*) sclp_init_sccb
;
757 memset(&sclp_init_req
, 0, sizeof(struct sclp_req
));
758 sclp_init_req
.command
= SCLP_CMDW_WRITE_EVENT_MASK
;
759 sclp_init_req
.status
= SCLP_REQ_FILLED
;
760 sclp_init_req
.start_count
= 0;
761 sclp_init_req
.callback
= NULL
;
762 sclp_init_req
.callback_data
= NULL
;
763 sclp_init_req
.sccb
= sccb
;
764 sccb
->header
.length
= sizeof(struct init_sccb
);
765 sccb
->mask_length
= sizeof(sccb_mask_t
);
766 sccb
->receive_mask
= receive_mask
;
767 sccb
->send_mask
= send_mask
;
768 sccb
->sclp_receive_mask
= 0;
769 sccb
->sclp_send_mask
= 0;
772 /* Start init mask request. If calculate is non-zero, calculate the mask as
773 * requested by registered listeners. Use zero mask otherwise. Return 0 on
774 * success, non-zero otherwise. */
776 sclp_init_mask(int calculate
)
779 struct init_sccb
*sccb
= (struct init_sccb
*) sclp_init_sccb
;
780 sccb_mask_t receive_mask
;
781 sccb_mask_t send_mask
;
786 spin_lock_irqsave(&sclp_lock
, flags
);
787 /* Check if interface is in appropriate state */
788 if (sclp_mask_state
!= sclp_mask_state_idle
) {
789 spin_unlock_irqrestore(&sclp_lock
, flags
);
792 if (sclp_activation_state
== sclp_activation_state_inactive
) {
793 spin_unlock_irqrestore(&sclp_lock
, flags
);
796 sclp_mask_state
= sclp_mask_state_initializing
;
799 __sclp_get_mask(&receive_mask
, &send_mask
);
805 for (retry
= 0; retry
<= SCLP_MASK_RETRY
; retry
++) {
806 /* Prepare request */
807 __sclp_make_init_req(receive_mask
, send_mask
);
808 spin_unlock_irqrestore(&sclp_lock
, flags
);
809 if (sclp_add_request(&sclp_init_req
)) {
810 /* Try again later */
811 wait
= jiffies
+ SCLP_BUSY_INTERVAL
* HZ
;
812 while (time_before(jiffies
, wait
))
814 spin_lock_irqsave(&sclp_lock
, flags
);
817 while (sclp_init_req
.status
!= SCLP_REQ_DONE
&&
818 sclp_init_req
.status
!= SCLP_REQ_FAILED
)
820 spin_lock_irqsave(&sclp_lock
, flags
);
821 if (sclp_init_req
.status
== SCLP_REQ_DONE
&&
822 sccb
->header
.response_code
== 0x20) {
823 /* Successful request */
825 sclp_receive_mask
= sccb
->sclp_receive_mask
;
826 sclp_send_mask
= sccb
->sclp_send_mask
;
828 sclp_receive_mask
= 0;
831 spin_unlock_irqrestore(&sclp_lock
, flags
);
832 sclp_dispatch_state_change();
833 spin_lock_irqsave(&sclp_lock
, flags
);
838 sclp_mask_state
= sclp_mask_state_idle
;
839 spin_unlock_irqrestore(&sclp_lock
, flags
);
843 /* Deactivate SCLP interface. On success, new requests will be rejected,
844 * events will no longer be dispatched. Return 0 on success, non-zero
847 sclp_deactivate(void)
852 spin_lock_irqsave(&sclp_lock
, flags
);
853 /* Deactivate can only be called when active */
854 if (sclp_activation_state
!= sclp_activation_state_active
) {
855 spin_unlock_irqrestore(&sclp_lock
, flags
);
858 sclp_activation_state
= sclp_activation_state_deactivating
;
859 spin_unlock_irqrestore(&sclp_lock
, flags
);
860 rc
= sclp_init_mask(0);
861 spin_lock_irqsave(&sclp_lock
, flags
);
863 sclp_activation_state
= sclp_activation_state_inactive
;
865 sclp_activation_state
= sclp_activation_state_active
;
866 spin_unlock_irqrestore(&sclp_lock
, flags
);
870 EXPORT_SYMBOL(sclp_deactivate
);
872 /* Reactivate SCLP interface after sclp_deactivate. On success, new
873 * requests will be accepted, events will be dispatched again. Return 0 on
874 * success, non-zero otherwise. */
876 sclp_reactivate(void)
881 spin_lock_irqsave(&sclp_lock
, flags
);
882 /* Reactivate can only be called when inactive */
883 if (sclp_activation_state
!= sclp_activation_state_inactive
) {
884 spin_unlock_irqrestore(&sclp_lock
, flags
);
887 sclp_activation_state
= sclp_activation_state_activating
;
888 spin_unlock_irqrestore(&sclp_lock
, flags
);
889 rc
= sclp_init_mask(1);
890 spin_lock_irqsave(&sclp_lock
, flags
);
892 sclp_activation_state
= sclp_activation_state_active
;
894 sclp_activation_state
= sclp_activation_state_inactive
;
895 spin_unlock_irqrestore(&sclp_lock
, flags
);
899 EXPORT_SYMBOL(sclp_reactivate
);
901 /* Handler for external interruption used during initialization. Modify
902 * request state to done. */
903 static void sclp_check_handler(struct ext_code ext_code
,
904 unsigned int param32
, unsigned long param64
)
908 inc_irq_stat(IRQEXT_SCP
);
909 finished_sccb
= param32
& 0xfffffff8;
910 /* Is this the interrupt we are waiting for? */
911 if (finished_sccb
== 0)
913 if (finished_sccb
!= (u32
) (addr_t
) sclp_init_sccb
)
914 panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
916 spin_lock(&sclp_lock
);
917 if (sclp_running_state
== sclp_running_state_running
) {
918 sclp_init_req
.status
= SCLP_REQ_DONE
;
919 sclp_running_state
= sclp_running_state_idle
;
921 spin_unlock(&sclp_lock
);
924 /* Initial init mask request timed out. Modify request state to failed. */
926 sclp_check_timeout(struct timer_list
*unused
)
930 spin_lock_irqsave(&sclp_lock
, flags
);
931 if (sclp_running_state
== sclp_running_state_running
) {
932 sclp_init_req
.status
= SCLP_REQ_FAILED
;
933 sclp_running_state
= sclp_running_state_idle
;
935 spin_unlock_irqrestore(&sclp_lock
, flags
);
938 /* Perform a check of the SCLP interface. Return zero if the interface is
939 * available and there are no pending requests from a previous instance.
940 * Return non-zero otherwise. */
942 sclp_check_interface(void)
944 struct init_sccb
*sccb
;
949 spin_lock_irqsave(&sclp_lock
, flags
);
950 /* Prepare init mask command */
951 rc
= register_external_irq(EXT_IRQ_SERVICE_SIG
, sclp_check_handler
);
953 spin_unlock_irqrestore(&sclp_lock
, flags
);
956 for (retry
= 0; retry
<= SCLP_INIT_RETRY
; retry
++) {
957 __sclp_make_init_req(0, 0);
958 sccb
= (struct init_sccb
*) sclp_init_req
.sccb
;
959 rc
= sclp_service_call(sclp_init_req
.command
, sccb
);
962 sclp_init_req
.status
= SCLP_REQ_RUNNING
;
963 sclp_running_state
= sclp_running_state_running
;
964 __sclp_set_request_timer(SCLP_RETRY_INTERVAL
* HZ
,
966 spin_unlock_irqrestore(&sclp_lock
, flags
);
967 /* Enable service-signal interruption - needs to happen
968 * with IRQs enabled. */
969 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL
);
970 /* Wait for signal from interrupt or timeout */
972 /* Disable service-signal interruption - needs to happen
973 * with IRQs enabled. */
974 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL
);
975 spin_lock_irqsave(&sclp_lock
, flags
);
976 del_timer(&sclp_request_timer
);
977 if (sclp_init_req
.status
== SCLP_REQ_DONE
&&
978 sccb
->header
.response_code
== 0x20) {
984 unregister_external_irq(EXT_IRQ_SERVICE_SIG
, sclp_check_handler
);
985 spin_unlock_irqrestore(&sclp_lock
, flags
);
989 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
990 * events from interfering with rebooted system. */
992 sclp_reboot_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
998 static struct notifier_block sclp_reboot_notifier
= {
999 .notifier_call
= sclp_reboot_event
1003 * Suspend/resume SCLP notifier implementation
1006 static void sclp_pm_event(enum sclp_pm_event sclp_pm_event
, int rollback
)
1008 struct sclp_register
*reg
;
1009 unsigned long flags
;
1012 spin_lock_irqsave(&sclp_lock
, flags
);
1013 list_for_each_entry(reg
, &sclp_reg_list
, list
)
1014 reg
->pm_event_posted
= 0;
1015 spin_unlock_irqrestore(&sclp_lock
, flags
);
1018 spin_lock_irqsave(&sclp_lock
, flags
);
1019 list_for_each_entry(reg
, &sclp_reg_list
, list
) {
1020 if (rollback
&& reg
->pm_event_posted
)
1022 if (!rollback
&& !reg
->pm_event_posted
)
1025 spin_unlock_irqrestore(&sclp_lock
, flags
);
1028 spin_unlock_irqrestore(&sclp_lock
, flags
);
1029 if (reg
->pm_event_fn
)
1030 reg
->pm_event_fn(reg
, sclp_pm_event
);
1031 reg
->pm_event_posted
= rollback
? 0 : 1;
1036 * Susend/resume callbacks for platform device
1039 static int sclp_freeze(struct device
*dev
)
1041 unsigned long flags
;
1044 sclp_pm_event(SCLP_PM_EVENT_FREEZE
, 0);
1046 spin_lock_irqsave(&sclp_lock
, flags
);
1047 sclp_suspend_state
= sclp_suspend_state_suspended
;
1048 spin_unlock_irqrestore(&sclp_lock
, flags
);
1050 /* Init supend data */
1051 memset(&sclp_suspend_req
, 0, sizeof(sclp_suspend_req
));
1052 sclp_suspend_req
.callback
= sclp_suspend_req_cb
;
1053 sclp_suspend_req
.status
= SCLP_REQ_FILLED
;
1054 init_completion(&sclp_request_queue_flushed
);
1056 rc
= sclp_add_request(&sclp_suspend_req
);
1058 wait_for_completion(&sclp_request_queue_flushed
);
1059 else if (rc
!= -ENODATA
)
1062 rc
= sclp_deactivate();
1068 spin_lock_irqsave(&sclp_lock
, flags
);
1069 sclp_suspend_state
= sclp_suspend_state_running
;
1070 spin_unlock_irqrestore(&sclp_lock
, flags
);
1071 sclp_pm_event(SCLP_PM_EVENT_THAW
, 1);
1075 static int sclp_undo_suspend(enum sclp_pm_event event
)
1077 unsigned long flags
;
1080 rc
= sclp_reactivate();
1084 spin_lock_irqsave(&sclp_lock
, flags
);
1085 sclp_suspend_state
= sclp_suspend_state_running
;
1086 spin_unlock_irqrestore(&sclp_lock
, flags
);
1088 sclp_pm_event(event
, 0);
1092 static int sclp_thaw(struct device
*dev
)
1094 return sclp_undo_suspend(SCLP_PM_EVENT_THAW
);
1097 static int sclp_restore(struct device
*dev
)
1099 return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE
);
1102 static const struct dev_pm_ops sclp_pm_ops
= {
1103 .freeze
= sclp_freeze
,
1105 .restore
= sclp_restore
,
1108 static ssize_t
con_pages_show(struct device_driver
*dev
, char *buf
)
1110 return sprintf(buf
, "%i\n", sclp_console_pages
);
1113 static DRIVER_ATTR_RO(con_pages
);
1115 static ssize_t
con_drop_show(struct device_driver
*dev
, char *buf
)
1117 return sprintf(buf
, "%i\n", sclp_console_drop
);
1120 static DRIVER_ATTR_RO(con_drop
);
1122 static ssize_t
con_full_show(struct device_driver
*dev
, char *buf
)
1124 return sprintf(buf
, "%lu\n", sclp_console_full
);
1127 static DRIVER_ATTR_RO(con_full
);
1129 static struct attribute
*sclp_drv_attrs
[] = {
1130 &driver_attr_con_pages
.attr
,
1131 &driver_attr_con_drop
.attr
,
1132 &driver_attr_con_full
.attr
,
1135 static struct attribute_group sclp_drv_attr_group
= {
1136 .attrs
= sclp_drv_attrs
,
1138 static const struct attribute_group
*sclp_drv_attr_groups
[] = {
1139 &sclp_drv_attr_group
,
1143 static struct platform_driver sclp_pdrv
= {
1147 .groups
= sclp_drv_attr_groups
,
1151 static struct platform_device
*sclp_pdev
;
1153 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
1158 unsigned long flags
;
1161 spin_lock_irqsave(&sclp_lock
, flags
);
1162 /* Check for previous or running initialization */
1163 if (sclp_init_state
!= sclp_init_state_uninitialized
)
1165 sclp_init_state
= sclp_init_state_initializing
;
1166 /* Set up variables */
1167 INIT_LIST_HEAD(&sclp_req_queue
);
1168 INIT_LIST_HEAD(&sclp_reg_list
);
1169 list_add(&sclp_state_change_event
.list
, &sclp_reg_list
);
1170 timer_setup(&sclp_request_timer
, NULL
, 0);
1171 timer_setup(&sclp_queue_timer
, sclp_req_queue_timeout
, 0);
1172 /* Check interface */
1173 spin_unlock_irqrestore(&sclp_lock
, flags
);
1174 rc
= sclp_check_interface();
1175 spin_lock_irqsave(&sclp_lock
, flags
);
1177 goto fail_init_state_uninitialized
;
1178 /* Register reboot handler */
1179 rc
= register_reboot_notifier(&sclp_reboot_notifier
);
1181 goto fail_init_state_uninitialized
;
1182 /* Register interrupt handler */
1183 rc
= register_external_irq(EXT_IRQ_SERVICE_SIG
, sclp_interrupt_handler
);
1185 goto fail_unregister_reboot_notifier
;
1186 sclp_init_state
= sclp_init_state_initialized
;
1187 spin_unlock_irqrestore(&sclp_lock
, flags
);
1188 /* Enable service-signal external interruption - needs to happen with
1190 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL
);
1194 fail_unregister_reboot_notifier
:
1195 unregister_reboot_notifier(&sclp_reboot_notifier
);
1196 fail_init_state_uninitialized
:
1197 sclp_init_state
= sclp_init_state_uninitialized
;
1199 spin_unlock_irqrestore(&sclp_lock
, flags
);
1204 * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
1205 * to print the panic message.
1207 static int sclp_panic_notify(struct notifier_block
*self
,
1208 unsigned long event
, void *data
)
1210 if (sclp_suspend_state
== sclp_suspend_state_suspended
)
1211 sclp_undo_suspend(SCLP_PM_EVENT_THAW
);
1215 static struct notifier_block sclp_on_panic_nb
= {
1216 .notifier_call
= sclp_panic_notify
,
1217 .priority
= SCLP_PANIC_PRIO
,
1220 static __init
int sclp_initcall(void)
1224 rc
= platform_driver_register(&sclp_pdrv
);
1228 sclp_pdev
= platform_device_register_simple("sclp", -1, NULL
, 0);
1229 rc
= PTR_ERR_OR_ZERO(sclp_pdev
);
1231 goto fail_platform_driver_unregister
;
1233 rc
= atomic_notifier_chain_register(&panic_notifier_list
,
1236 goto fail_platform_device_unregister
;
1240 fail_platform_device_unregister
:
1241 platform_device_unregister(sclp_pdev
);
1242 fail_platform_driver_unregister
:
1243 platform_driver_unregister(&sclp_pdrv
);
1247 arch_initcall(sclp_initcall
);