xref: /openbmc/linux/drivers/s390/char/sclp.c (revision 65a0d3c1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * core function to access sclp interface
4  *
5  * Copyright IBM Corp. 1999, 2009
6  *
7  * Author(s): Martin Peschke <mpeschke@de.ibm.com>
8  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
11 #include <linux/kernel_stat.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/panic_notifier.h>
15 #include <linux/spinlock.h>
16 #include <linux/interrupt.h>
17 #include <linux/timer.h>
18 #include <linux/reboot.h>
19 #include <linux/jiffies.h>
20 #include <linux/init.h>
21 #include <linux/suspend.h>
22 #include <linux/completion.h>
23 #include <linux/platform_device.h>
24 #include <asm/types.h>
25 #include <asm/irq.h>
26 
27 #include "sclp.h"
28 
29 #define SCLP_HEADER		"sclp: "
30 
31 /* Lock to protect internal data consistency. */
32 static DEFINE_SPINLOCK(sclp_lock);
33 
34 /* Mask of events that we can send to the sclp interface. */
35 static sccb_mask_t sclp_receive_mask;
36 
37 /* Mask of events that we can receive from the sclp interface. */
38 static sccb_mask_t sclp_send_mask;
39 
40 /* List of registered event listeners and senders. */
41 static LIST_HEAD(sclp_reg_list);
42 
43 /* List of queued requests. */
44 static LIST_HEAD(sclp_req_queue);
45 
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 void *sclp_read_sccb;
50 static struct init_sccb *sclp_init_sccb;
51 
52 /* Suspend request */
53 static DECLARE_COMPLETION(sclp_request_queue_flushed);
54 
55 /* Number of console pages to allocate, used by sclp_con.c and sclp_vt220.c */
56 int sclp_console_pages = SCLP_CONSOLE_PAGES;
57 /* Flag to indicate if buffer pages are dropped on buffer full condition */
58 int sclp_console_drop = 1;
59 /* Number of times the console dropped buffer pages */
60 unsigned long sclp_console_full;
61 
62 static void sclp_suspend_req_cb(struct sclp_req *req, void *data)
63 {
64 	complete(&sclp_request_queue_flushed);
65 }
66 
67 static int __init sclp_setup_console_pages(char *str)
68 {
69 	int pages, rc;
70 
71 	rc = kstrtoint(str, 0, &pages);
72 	if (!rc && pages >= SCLP_CONSOLE_PAGES)
73 		sclp_console_pages = pages;
74 	return 1;
75 }
76 
77 __setup("sclp_con_pages=", sclp_setup_console_pages);
78 
79 static int __init sclp_setup_console_drop(char *str)
80 {
81 	int drop, rc;
82 
83 	rc = kstrtoint(str, 0, &drop);
84 	if (!rc)
85 		sclp_console_drop = drop;
86 	return 1;
87 }
88 
89 __setup("sclp_con_drop=", sclp_setup_console_drop);
90 
91 static struct sclp_req sclp_suspend_req;
92 
93 /* Timer for request retries. */
94 static struct timer_list sclp_request_timer;
95 
96 /* Timer for queued requests. */
97 static struct timer_list sclp_queue_timer;
98 
99 /* Internal state: is a request active at the sclp? */
100 static volatile enum sclp_running_state_t {
101 	sclp_running_state_idle,
102 	sclp_running_state_running,
103 	sclp_running_state_reset_pending
104 } sclp_running_state = sclp_running_state_idle;
105 
106 /* Internal state: is a read request pending? */
107 static volatile enum sclp_reading_state_t {
108 	sclp_reading_state_idle,
109 	sclp_reading_state_reading
110 } sclp_reading_state = sclp_reading_state_idle;
111 
112 /* Internal state: is the driver currently serving requests? */
113 static volatile enum sclp_activation_state_t {
114 	sclp_activation_state_active,
115 	sclp_activation_state_deactivating,
116 	sclp_activation_state_inactive,
117 	sclp_activation_state_activating
118 } sclp_activation_state = sclp_activation_state_active;
119 
120 /* Internal state: is an init mask request pending? */
121 static volatile enum sclp_mask_state_t {
122 	sclp_mask_state_idle,
123 	sclp_mask_state_initializing
124 } sclp_mask_state = sclp_mask_state_idle;
125 
126 /* Internal state: is the driver suspended? */
127 static enum sclp_suspend_state_t {
128 	sclp_suspend_state_running,
129 	sclp_suspend_state_suspended,
130 } sclp_suspend_state = sclp_suspend_state_running;
131 
132 /* Maximum retry counts */
133 #define SCLP_INIT_RETRY		3
134 #define SCLP_MASK_RETRY		3
135 
136 /* Timeout intervals in seconds.*/
137 #define SCLP_BUSY_INTERVAL	10
138 #define SCLP_RETRY_INTERVAL	30
139 
140 static void sclp_request_timeout(bool force_restart);
141 static void sclp_process_queue(void);
142 static void __sclp_make_read_req(void);
143 static int sclp_init_mask(int calculate);
144 static int sclp_init(void);
145 
146 static void
147 __sclp_queue_read_req(void)
148 {
149 	if (sclp_reading_state == sclp_reading_state_idle) {
150 		sclp_reading_state = sclp_reading_state_reading;
151 		__sclp_make_read_req();
152 		/* Add request to head of queue */
153 		list_add(&sclp_read_req.list, &sclp_req_queue);
154 	}
155 }
156 
157 /* Set up request retry timer. Called while sclp_lock is locked. */
158 static inline void
159 __sclp_set_request_timer(unsigned long time, void (*cb)(struct timer_list *))
160 {
161 	del_timer(&sclp_request_timer);
162 	sclp_request_timer.function = cb;
163 	sclp_request_timer.expires = jiffies + time;
164 	add_timer(&sclp_request_timer);
165 }
166 
167 static void sclp_request_timeout_restart(struct timer_list *unused)
168 {
169 	sclp_request_timeout(true);
170 }
171 
172 static void sclp_request_timeout_normal(struct timer_list *unused)
173 {
174 	sclp_request_timeout(false);
175 }
176 
177 /* Request timeout handler. Restart the request queue. If force_restart,
178  * force restart of running request. */
179 static void sclp_request_timeout(bool force_restart)
180 {
181 	unsigned long flags;
182 
183 	spin_lock_irqsave(&sclp_lock, flags);
184 	if (force_restart) {
185 		if (sclp_running_state == sclp_running_state_running) {
186 			/* Break running state and queue NOP read event request
187 			 * to get a defined interface state. */
188 			__sclp_queue_read_req();
189 			sclp_running_state = sclp_running_state_idle;
190 		}
191 	} else {
192 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
193 					 sclp_request_timeout_normal);
194 	}
195 	spin_unlock_irqrestore(&sclp_lock, flags);
196 	sclp_process_queue();
197 }
198 
199 /*
200  * Returns the expire value in jiffies of the next pending request timeout,
201  * if any. Needs to be called with sclp_lock.
202  */
203 static unsigned long __sclp_req_queue_find_next_timeout(void)
204 {
205 	unsigned long expires_next = 0;
206 	struct sclp_req *req;
207 
208 	list_for_each_entry(req, &sclp_req_queue, list) {
209 		if (!req->queue_expires)
210 			continue;
211 		if (!expires_next ||
212 		   (time_before(req->queue_expires, expires_next)))
213 				expires_next = req->queue_expires;
214 	}
215 	return expires_next;
216 }
217 
218 /*
219  * Returns expired request, if any, and removes it from the list.
220  */
221 static struct sclp_req *__sclp_req_queue_remove_expired_req(void)
222 {
223 	unsigned long flags, now;
224 	struct sclp_req *req;
225 
226 	spin_lock_irqsave(&sclp_lock, flags);
227 	now = jiffies;
228 	/* Don't need list_for_each_safe because we break out after list_del */
229 	list_for_each_entry(req, &sclp_req_queue, list) {
230 		if (!req->queue_expires)
231 			continue;
232 		if (time_before_eq(req->queue_expires, now)) {
233 			if (req->status == SCLP_REQ_QUEUED) {
234 				req->status = SCLP_REQ_QUEUED_TIMEOUT;
235 				list_del(&req->list);
236 				goto out;
237 			}
238 		}
239 	}
240 	req = NULL;
241 out:
242 	spin_unlock_irqrestore(&sclp_lock, flags);
243 	return req;
244 }
245 
246 /*
247  * Timeout handler for queued requests. Removes request from list and
248  * invokes callback. This timer can be set per request in situations where
249  * waiting too long would be harmful to the system, e.g. during SE reboot.
250  */
251 static void sclp_req_queue_timeout(struct timer_list *unused)
252 {
253 	unsigned long flags, expires_next;
254 	struct sclp_req *req;
255 
256 	do {
257 		req = __sclp_req_queue_remove_expired_req();
258 		if (req && req->callback)
259 			req->callback(req, req->callback_data);
260 	} while (req);
261 
262 	spin_lock_irqsave(&sclp_lock, flags);
263 	expires_next = __sclp_req_queue_find_next_timeout();
264 	if (expires_next)
265 		mod_timer(&sclp_queue_timer, expires_next);
266 	spin_unlock_irqrestore(&sclp_lock, flags);
267 }
268 
269 /* Try to start a request. Return zero if the request was successfully
270  * started or if it will be started at a later time. Return non-zero otherwise.
271  * Called while sclp_lock is locked. */
272 static int
273 __sclp_start_request(struct sclp_req *req)
274 {
275 	int rc;
276 
277 	if (sclp_running_state != sclp_running_state_idle)
278 		return 0;
279 	del_timer(&sclp_request_timer);
280 	rc = sclp_service_call(req->command, req->sccb);
281 	req->start_count++;
282 
283 	if (rc == 0) {
284 		/* Successfully started request */
285 		req->status = SCLP_REQ_RUNNING;
286 		sclp_running_state = sclp_running_state_running;
287 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
288 					 sclp_request_timeout_restart);
289 		return 0;
290 	} else if (rc == -EBUSY) {
291 		/* Try again later */
292 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
293 					 sclp_request_timeout_normal);
294 		return 0;
295 	}
296 	/* Request failed */
297 	req->status = SCLP_REQ_FAILED;
298 	return rc;
299 }
300 
301 /* Try to start queued requests. */
302 static void
303 sclp_process_queue(void)
304 {
305 	struct sclp_req *req;
306 	int rc;
307 	unsigned long flags;
308 
309 	spin_lock_irqsave(&sclp_lock, flags);
310 	if (sclp_running_state != sclp_running_state_idle) {
311 		spin_unlock_irqrestore(&sclp_lock, flags);
312 		return;
313 	}
314 	del_timer(&sclp_request_timer);
315 	while (!list_empty(&sclp_req_queue)) {
316 		req = list_entry(sclp_req_queue.next, struct sclp_req, list);
317 		if (!req->sccb)
318 			goto do_post;
319 		rc = __sclp_start_request(req);
320 		if (rc == 0)
321 			break;
322 		/* Request failed */
323 		if (req->start_count > 1) {
324 			/* Cannot abort already submitted request - could still
325 			 * be active at the SCLP */
326 			__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
327 						 sclp_request_timeout_normal);
328 			break;
329 		}
330 do_post:
331 		/* Post-processing for aborted request */
332 		list_del(&req->list);
333 		if (req->callback) {
334 			spin_unlock_irqrestore(&sclp_lock, flags);
335 			req->callback(req, req->callback_data);
336 			spin_lock_irqsave(&sclp_lock, flags);
337 		}
338 	}
339 	spin_unlock_irqrestore(&sclp_lock, flags);
340 }
341 
342 static int __sclp_can_add_request(struct sclp_req *req)
343 {
344 	if (req == &sclp_suspend_req || req == &sclp_init_req)
345 		return 1;
346 	if (sclp_suspend_state != sclp_suspend_state_running)
347 		return 0;
348 	if (sclp_init_state != sclp_init_state_initialized)
349 		return 0;
350 	if (sclp_activation_state != sclp_activation_state_active)
351 		return 0;
352 	return 1;
353 }
354 
355 /* Queue a new request. Return zero on success, non-zero otherwise. */
356 int
357 sclp_add_request(struct sclp_req *req)
358 {
359 	unsigned long flags;
360 	int rc;
361 
362 	spin_lock_irqsave(&sclp_lock, flags);
363 	if (!__sclp_can_add_request(req)) {
364 		spin_unlock_irqrestore(&sclp_lock, flags);
365 		return -EIO;
366 	}
367 	req->status = SCLP_REQ_QUEUED;
368 	req->start_count = 0;
369 	list_add_tail(&req->list, &sclp_req_queue);
370 	rc = 0;
371 	if (req->queue_timeout) {
372 		req->queue_expires = jiffies + req->queue_timeout * HZ;
373 		if (!timer_pending(&sclp_queue_timer) ||
374 		    time_after(sclp_queue_timer.expires, req->queue_expires))
375 			mod_timer(&sclp_queue_timer, req->queue_expires);
376 	} else
377 		req->queue_expires = 0;
378 	/* Start if request is first in list */
379 	if (sclp_running_state == sclp_running_state_idle &&
380 	    req->list.prev == &sclp_req_queue) {
381 		if (!req->sccb) {
382 			list_del(&req->list);
383 			rc = -ENODATA;
384 			goto out;
385 		}
386 		rc = __sclp_start_request(req);
387 		if (rc)
388 			list_del(&req->list);
389 	}
390 out:
391 	spin_unlock_irqrestore(&sclp_lock, flags);
392 	return rc;
393 }
394 
395 EXPORT_SYMBOL(sclp_add_request);
396 
397 /* Dispatch events found in request buffer to registered listeners. Return 0
398  * if all events were dispatched, non-zero otherwise. */
399 static int
400 sclp_dispatch_evbufs(struct sccb_header *sccb)
401 {
402 	unsigned long flags;
403 	struct evbuf_header *evbuf;
404 	struct list_head *l;
405 	struct sclp_register *reg;
406 	int offset;
407 	int rc;
408 
409 	spin_lock_irqsave(&sclp_lock, flags);
410 	rc = 0;
411 	for (offset = sizeof(struct sccb_header); offset < sccb->length;
412 	     offset += evbuf->length) {
413 		evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
414 		/* Check for malformed hardware response */
415 		if (evbuf->length == 0)
416 			break;
417 		/* Search for event handler */
418 		reg = NULL;
419 		list_for_each(l, &sclp_reg_list) {
420 			reg = list_entry(l, struct sclp_register, list);
421 			if (reg->receive_mask & SCLP_EVTYP_MASK(evbuf->type))
422 				break;
423 			else
424 				reg = NULL;
425 		}
426 		if (reg && reg->receiver_fn) {
427 			spin_unlock_irqrestore(&sclp_lock, flags);
428 			reg->receiver_fn(evbuf);
429 			spin_lock_irqsave(&sclp_lock, flags);
430 		} else if (reg == NULL)
431 			rc = -EOPNOTSUPP;
432 	}
433 	spin_unlock_irqrestore(&sclp_lock, flags);
434 	return rc;
435 }
436 
437 /* Read event data request callback. */
438 static void
439 sclp_read_cb(struct sclp_req *req, void *data)
440 {
441 	unsigned long flags;
442 	struct sccb_header *sccb;
443 
444 	sccb = (struct sccb_header *) req->sccb;
445 	if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
446 	    sccb->response_code == 0x220))
447 		sclp_dispatch_evbufs(sccb);
448 	spin_lock_irqsave(&sclp_lock, flags);
449 	sclp_reading_state = sclp_reading_state_idle;
450 	spin_unlock_irqrestore(&sclp_lock, flags);
451 }
452 
453 /* Prepare read event data request. Called while sclp_lock is locked. */
454 static void __sclp_make_read_req(void)
455 {
456 	struct sccb_header *sccb;
457 
458 	sccb = (struct sccb_header *) sclp_read_sccb;
459 	clear_page(sccb);
460 	memset(&sclp_read_req, 0, sizeof(struct sclp_req));
461 	sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
462 	sclp_read_req.status = SCLP_REQ_QUEUED;
463 	sclp_read_req.start_count = 0;
464 	sclp_read_req.callback = sclp_read_cb;
465 	sclp_read_req.sccb = sccb;
466 	sccb->length = PAGE_SIZE;
467 	sccb->function_code = 0;
468 	sccb->control_mask[2] = 0x80;
469 }
470 
471 /* Search request list for request with matching sccb. Return request if found,
472  * NULL otherwise. Called while sclp_lock is locked. */
473 static inline struct sclp_req *
474 __sclp_find_req(u32 sccb)
475 {
476 	struct list_head *l;
477 	struct sclp_req *req;
478 
479 	list_for_each(l, &sclp_req_queue) {
480 		req = list_entry(l, struct sclp_req, list);
481 		if (sccb == (u32) (addr_t) req->sccb)
482 				return req;
483 	}
484 	return NULL;
485 }
486 
487 /* Handler for external interruption. Perform request post-processing.
488  * Prepare read event data request if necessary. Start processing of next
489  * request on queue. */
490 static void sclp_interrupt_handler(struct ext_code ext_code,
491 				   unsigned int param32, unsigned long param64)
492 {
493 	struct sclp_req *req;
494 	u32 finished_sccb;
495 	u32 evbuf_pending;
496 
497 	inc_irq_stat(IRQEXT_SCP);
498 	spin_lock(&sclp_lock);
499 	finished_sccb = param32 & 0xfffffff8;
500 	evbuf_pending = param32 & 0x3;
501 	if (finished_sccb) {
502 		del_timer(&sclp_request_timer);
503 		sclp_running_state = sclp_running_state_reset_pending;
504 		req = __sclp_find_req(finished_sccb);
505 		if (req) {
506 			/* Request post-processing */
507 			list_del(&req->list);
508 			req->status = SCLP_REQ_DONE;
509 			if (req->callback) {
510 				spin_unlock(&sclp_lock);
511 				req->callback(req, req->callback_data);
512 				spin_lock(&sclp_lock);
513 			}
514 		}
515 		sclp_running_state = sclp_running_state_idle;
516 	}
517 	if (evbuf_pending &&
518 	    sclp_activation_state == sclp_activation_state_active)
519 		__sclp_queue_read_req();
520 	spin_unlock(&sclp_lock);
521 	sclp_process_queue();
522 }
523 
524 /* Convert interval in jiffies to TOD ticks. */
525 static inline u64
526 sclp_tod_from_jiffies(unsigned long jiffies)
527 {
528 	return (u64) (jiffies / HZ) << 32;
529 }
530 
531 /* Wait until a currently running request finished. Note: while this function
532  * is running, no timers are served on the calling CPU. */
533 void
534 sclp_sync_wait(void)
535 {
536 	unsigned long long old_tick;
537 	unsigned long flags;
538 	unsigned long cr0, cr0_sync;
539 	u64 timeout;
540 	int irq_context;
541 
542 	/* We'll be disabling timer interrupts, so we need a custom timeout
543 	 * mechanism */
544 	timeout = 0;
545 	if (timer_pending(&sclp_request_timer)) {
546 		/* Get timeout TOD value */
547 		timeout = get_tod_clock_fast() +
548 			  sclp_tod_from_jiffies(sclp_request_timer.expires -
549 						jiffies);
550 	}
551 	local_irq_save(flags);
552 	/* Prevent bottom half from executing once we force interrupts open */
553 	irq_context = in_interrupt();
554 	if (!irq_context)
555 		local_bh_disable();
556 	/* Enable service-signal interruption, disable timer interrupts */
557 	old_tick = local_tick_disable();
558 	trace_hardirqs_on();
559 	__ctl_store(cr0, 0, 0);
560 	cr0_sync = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
561 	cr0_sync |= 1UL << (63 - 54);
562 	__ctl_load(cr0_sync, 0, 0);
563 	__arch_local_irq_stosm(0x01);
564 	/* Loop until driver state indicates finished request */
565 	while (sclp_running_state != sclp_running_state_idle) {
566 		/* Check for expired request timer */
567 		if (timer_pending(&sclp_request_timer) &&
568 		    get_tod_clock_fast() > timeout &&
569 		    del_timer(&sclp_request_timer))
570 			sclp_request_timer.function(&sclp_request_timer);
571 		cpu_relax();
572 	}
573 	local_irq_disable();
574 	__ctl_load(cr0, 0, 0);
575 	if (!irq_context)
576 		_local_bh_enable();
577 	local_tick_enable(old_tick);
578 	local_irq_restore(flags);
579 }
580 EXPORT_SYMBOL(sclp_sync_wait);
581 
582 /* Dispatch changes in send and receive mask to registered listeners. */
583 static void
584 sclp_dispatch_state_change(void)
585 {
586 	struct list_head *l;
587 	struct sclp_register *reg;
588 	unsigned long flags;
589 	sccb_mask_t receive_mask;
590 	sccb_mask_t send_mask;
591 
592 	do {
593 		spin_lock_irqsave(&sclp_lock, flags);
594 		reg = NULL;
595 		list_for_each(l, &sclp_reg_list) {
596 			reg = list_entry(l, struct sclp_register, list);
597 			receive_mask = reg->send_mask & sclp_receive_mask;
598 			send_mask = reg->receive_mask & sclp_send_mask;
599 			if (reg->sclp_receive_mask != receive_mask ||
600 			    reg->sclp_send_mask != send_mask) {
601 				reg->sclp_receive_mask = receive_mask;
602 				reg->sclp_send_mask = send_mask;
603 				break;
604 			} else
605 				reg = NULL;
606 		}
607 		spin_unlock_irqrestore(&sclp_lock, flags);
608 		if (reg && reg->state_change_fn)
609 			reg->state_change_fn(reg);
610 	} while (reg);
611 }
612 
613 struct sclp_statechangebuf {
614 	struct evbuf_header	header;
615 	u8		validity_sclp_active_facility_mask : 1;
616 	u8		validity_sclp_receive_mask : 1;
617 	u8		validity_sclp_send_mask : 1;
618 	u8		validity_read_data_function_mask : 1;
619 	u16		_zeros : 12;
620 	u16		mask_length;
621 	u64		sclp_active_facility_mask;
622 	u8		masks[2 * 1021 + 4];	/* variable length */
623 	/*
624 	 * u8		sclp_receive_mask[mask_length];
625 	 * u8		sclp_send_mask[mask_length];
626 	 * u32		read_data_function_mask;
627 	 */
628 } __attribute__((packed));
629 
630 
631 /* State change event callback. Inform listeners of changes. */
632 static void
633 sclp_state_change_cb(struct evbuf_header *evbuf)
634 {
635 	unsigned long flags;
636 	struct sclp_statechangebuf *scbuf;
637 
638 	BUILD_BUG_ON(sizeof(struct sclp_statechangebuf) > PAGE_SIZE);
639 
640 	scbuf = (struct sclp_statechangebuf *) evbuf;
641 	spin_lock_irqsave(&sclp_lock, flags);
642 	if (scbuf->validity_sclp_receive_mask)
643 		sclp_receive_mask = sccb_get_recv_mask(scbuf);
644 	if (scbuf->validity_sclp_send_mask)
645 		sclp_send_mask = sccb_get_send_mask(scbuf);
646 	spin_unlock_irqrestore(&sclp_lock, flags);
647 	if (scbuf->validity_sclp_active_facility_mask)
648 		sclp.facilities = scbuf->sclp_active_facility_mask;
649 	sclp_dispatch_state_change();
650 }
651 
652 static struct sclp_register sclp_state_change_event = {
653 	.receive_mask = EVTYP_STATECHANGE_MASK,
654 	.receiver_fn = sclp_state_change_cb
655 };
656 
657 /* Calculate receive and send mask of currently registered listeners.
658  * Called while sclp_lock is locked. */
659 static inline void
660 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
661 {
662 	struct list_head *l;
663 	struct sclp_register *t;
664 
665 	*receive_mask = 0;
666 	*send_mask = 0;
667 	list_for_each(l, &sclp_reg_list) {
668 		t = list_entry(l, struct sclp_register, list);
669 		*receive_mask |= t->receive_mask;
670 		*send_mask |= t->send_mask;
671 	}
672 }
673 
674 /* Register event listener. Return 0 on success, non-zero otherwise. */
675 int
676 sclp_register(struct sclp_register *reg)
677 {
678 	unsigned long flags;
679 	sccb_mask_t receive_mask;
680 	sccb_mask_t send_mask;
681 	int rc;
682 
683 	rc = sclp_init();
684 	if (rc)
685 		return rc;
686 	spin_lock_irqsave(&sclp_lock, flags);
687 	/* Check event mask for collisions */
688 	__sclp_get_mask(&receive_mask, &send_mask);
689 	if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
690 		spin_unlock_irqrestore(&sclp_lock, flags);
691 		return -EBUSY;
692 	}
693 	/* Trigger initial state change callback */
694 	reg->sclp_receive_mask = 0;
695 	reg->sclp_send_mask = 0;
696 	reg->pm_event_posted = 0;
697 	list_add(&reg->list, &sclp_reg_list);
698 	spin_unlock_irqrestore(&sclp_lock, flags);
699 	rc = sclp_init_mask(1);
700 	if (rc) {
701 		spin_lock_irqsave(&sclp_lock, flags);
702 		list_del(&reg->list);
703 		spin_unlock_irqrestore(&sclp_lock, flags);
704 	}
705 	return rc;
706 }
707 
708 EXPORT_SYMBOL(sclp_register);
709 
710 /* Unregister event listener. */
711 void
712 sclp_unregister(struct sclp_register *reg)
713 {
714 	unsigned long flags;
715 
716 	spin_lock_irqsave(&sclp_lock, flags);
717 	list_del(&reg->list);
718 	spin_unlock_irqrestore(&sclp_lock, flags);
719 	sclp_init_mask(1);
720 }
721 
722 EXPORT_SYMBOL(sclp_unregister);
723 
724 /* Remove event buffers which are marked processed. Return the number of
725  * remaining event buffers. */
726 int
727 sclp_remove_processed(struct sccb_header *sccb)
728 {
729 	struct evbuf_header *evbuf;
730 	int unprocessed;
731 	u16 remaining;
732 
733 	evbuf = (struct evbuf_header *) (sccb + 1);
734 	unprocessed = 0;
735 	remaining = sccb->length - sizeof(struct sccb_header);
736 	while (remaining > 0) {
737 		remaining -= evbuf->length;
738 		if (evbuf->flags & 0x80) {
739 			sccb->length -= evbuf->length;
740 			memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
741 			       remaining);
742 		} else {
743 			unprocessed++;
744 			evbuf = (struct evbuf_header *)
745 					((addr_t) evbuf + evbuf->length);
746 		}
747 	}
748 	return unprocessed;
749 }
750 
751 EXPORT_SYMBOL(sclp_remove_processed);
752 
753 /* Prepare init mask request. Called while sclp_lock is locked. */
754 static inline void
755 __sclp_make_init_req(sccb_mask_t receive_mask, sccb_mask_t send_mask)
756 {
757 	struct init_sccb *sccb = sclp_init_sccb;
758 
759 	clear_page(sccb);
760 	memset(&sclp_init_req, 0, sizeof(struct sclp_req));
761 	sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
762 	sclp_init_req.status = SCLP_REQ_FILLED;
763 	sclp_init_req.start_count = 0;
764 	sclp_init_req.callback = NULL;
765 	sclp_init_req.callback_data = NULL;
766 	sclp_init_req.sccb = sccb;
767 	sccb->header.length = sizeof(*sccb);
768 	if (sclp_mask_compat_mode)
769 		sccb->mask_length = SCLP_MASK_SIZE_COMPAT;
770 	else
771 		sccb->mask_length = sizeof(sccb_mask_t);
772 	sccb_set_recv_mask(sccb, receive_mask);
773 	sccb_set_send_mask(sccb, send_mask);
774 	sccb_set_sclp_recv_mask(sccb, 0);
775 	sccb_set_sclp_send_mask(sccb, 0);
776 }
777 
778 /* Start init mask request. If calculate is non-zero, calculate the mask as
779  * requested by registered listeners. Use zero mask otherwise. Return 0 on
780  * success, non-zero otherwise. */
781 static int
782 sclp_init_mask(int calculate)
783 {
784 	unsigned long flags;
785 	struct init_sccb *sccb = sclp_init_sccb;
786 	sccb_mask_t receive_mask;
787 	sccb_mask_t send_mask;
788 	int retry;
789 	int rc;
790 	unsigned long wait;
791 
792 	spin_lock_irqsave(&sclp_lock, flags);
793 	/* Check if interface is in appropriate state */
794 	if (sclp_mask_state != sclp_mask_state_idle) {
795 		spin_unlock_irqrestore(&sclp_lock, flags);
796 		return -EBUSY;
797 	}
798 	if (sclp_activation_state == sclp_activation_state_inactive) {
799 		spin_unlock_irqrestore(&sclp_lock, flags);
800 		return -EINVAL;
801 	}
802 	sclp_mask_state = sclp_mask_state_initializing;
803 	/* Determine mask */
804 	if (calculate)
805 		__sclp_get_mask(&receive_mask, &send_mask);
806 	else {
807 		receive_mask = 0;
808 		send_mask = 0;
809 	}
810 	rc = -EIO;
811 	for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
812 		/* Prepare request */
813 		__sclp_make_init_req(receive_mask, send_mask);
814 		spin_unlock_irqrestore(&sclp_lock, flags);
815 		if (sclp_add_request(&sclp_init_req)) {
816 			/* Try again later */
817 			wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
818 			while (time_before(jiffies, wait))
819 				sclp_sync_wait();
820 			spin_lock_irqsave(&sclp_lock, flags);
821 			continue;
822 		}
823 		while (sclp_init_req.status != SCLP_REQ_DONE &&
824 		       sclp_init_req.status != SCLP_REQ_FAILED)
825 			sclp_sync_wait();
826 		spin_lock_irqsave(&sclp_lock, flags);
827 		if (sclp_init_req.status == SCLP_REQ_DONE &&
828 		    sccb->header.response_code == 0x20) {
829 			/* Successful request */
830 			if (calculate) {
831 				sclp_receive_mask = sccb_get_sclp_recv_mask(sccb);
832 				sclp_send_mask = sccb_get_sclp_send_mask(sccb);
833 			} else {
834 				sclp_receive_mask = 0;
835 				sclp_send_mask = 0;
836 			}
837 			spin_unlock_irqrestore(&sclp_lock, flags);
838 			sclp_dispatch_state_change();
839 			spin_lock_irqsave(&sclp_lock, flags);
840 			rc = 0;
841 			break;
842 		}
843 	}
844 	sclp_mask_state = sclp_mask_state_idle;
845 	spin_unlock_irqrestore(&sclp_lock, flags);
846 	return rc;
847 }
848 
849 /* Deactivate SCLP interface. On success, new requests will be rejected,
850  * events will no longer be dispatched. Return 0 on success, non-zero
851  * otherwise. */
852 int
853 sclp_deactivate(void)
854 {
855 	unsigned long flags;
856 	int rc;
857 
858 	spin_lock_irqsave(&sclp_lock, flags);
859 	/* Deactivate can only be called when active */
860 	if (sclp_activation_state != sclp_activation_state_active) {
861 		spin_unlock_irqrestore(&sclp_lock, flags);
862 		return -EINVAL;
863 	}
864 	sclp_activation_state = sclp_activation_state_deactivating;
865 	spin_unlock_irqrestore(&sclp_lock, flags);
866 	rc = sclp_init_mask(0);
867 	spin_lock_irqsave(&sclp_lock, flags);
868 	if (rc == 0)
869 		sclp_activation_state = sclp_activation_state_inactive;
870 	else
871 		sclp_activation_state = sclp_activation_state_active;
872 	spin_unlock_irqrestore(&sclp_lock, flags);
873 	return rc;
874 }
875 
876 EXPORT_SYMBOL(sclp_deactivate);
877 
878 /* Reactivate SCLP interface after sclp_deactivate. On success, new
879  * requests will be accepted, events will be dispatched again. Return 0 on
880  * success, non-zero otherwise. */
881 int
882 sclp_reactivate(void)
883 {
884 	unsigned long flags;
885 	int rc;
886 
887 	spin_lock_irqsave(&sclp_lock, flags);
888 	/* Reactivate can only be called when inactive */
889 	if (sclp_activation_state != sclp_activation_state_inactive) {
890 		spin_unlock_irqrestore(&sclp_lock, flags);
891 		return -EINVAL;
892 	}
893 	sclp_activation_state = sclp_activation_state_activating;
894 	spin_unlock_irqrestore(&sclp_lock, flags);
895 	rc = sclp_init_mask(1);
896 	spin_lock_irqsave(&sclp_lock, flags);
897 	if (rc == 0)
898 		sclp_activation_state = sclp_activation_state_active;
899 	else
900 		sclp_activation_state = sclp_activation_state_inactive;
901 	spin_unlock_irqrestore(&sclp_lock, flags);
902 	return rc;
903 }
904 
905 EXPORT_SYMBOL(sclp_reactivate);
906 
907 /* Handler for external interruption used during initialization. Modify
908  * request state to done. */
909 static void sclp_check_handler(struct ext_code ext_code,
910 			       unsigned int param32, unsigned long param64)
911 {
912 	u32 finished_sccb;
913 
914 	inc_irq_stat(IRQEXT_SCP);
915 	finished_sccb = param32 & 0xfffffff8;
916 	/* Is this the interrupt we are waiting for? */
917 	if (finished_sccb == 0)
918 		return;
919 	if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
920 		panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
921 		      finished_sccb);
922 	spin_lock(&sclp_lock);
923 	if (sclp_running_state == sclp_running_state_running) {
924 		sclp_init_req.status = SCLP_REQ_DONE;
925 		sclp_running_state = sclp_running_state_idle;
926 	}
927 	spin_unlock(&sclp_lock);
928 }
929 
930 /* Initial init mask request timed out. Modify request state to failed. */
931 static void
932 sclp_check_timeout(struct timer_list *unused)
933 {
934 	unsigned long flags;
935 
936 	spin_lock_irqsave(&sclp_lock, flags);
937 	if (sclp_running_state == sclp_running_state_running) {
938 		sclp_init_req.status = SCLP_REQ_FAILED;
939 		sclp_running_state = sclp_running_state_idle;
940 	}
941 	spin_unlock_irqrestore(&sclp_lock, flags);
942 }
943 
944 /* Perform a check of the SCLP interface. Return zero if the interface is
945  * available and there are no pending requests from a previous instance.
946  * Return non-zero otherwise. */
947 static int
948 sclp_check_interface(void)
949 {
950 	struct init_sccb *sccb;
951 	unsigned long flags;
952 	int retry;
953 	int rc;
954 
955 	spin_lock_irqsave(&sclp_lock, flags);
956 	/* Prepare init mask command */
957 	rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler);
958 	if (rc) {
959 		spin_unlock_irqrestore(&sclp_lock, flags);
960 		return rc;
961 	}
962 	for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
963 		__sclp_make_init_req(0, 0);
964 		sccb = (struct init_sccb *) sclp_init_req.sccb;
965 		rc = sclp_service_call(sclp_init_req.command, sccb);
966 		if (rc == -EIO)
967 			break;
968 		sclp_init_req.status = SCLP_REQ_RUNNING;
969 		sclp_running_state = sclp_running_state_running;
970 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
971 					 sclp_check_timeout);
972 		spin_unlock_irqrestore(&sclp_lock, flags);
973 		/* Enable service-signal interruption - needs to happen
974 		 * with IRQs enabled. */
975 		irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
976 		/* Wait for signal from interrupt or timeout */
977 		sclp_sync_wait();
978 		/* Disable service-signal interruption - needs to happen
979 		 * with IRQs enabled. */
980 		irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
981 		spin_lock_irqsave(&sclp_lock, flags);
982 		del_timer(&sclp_request_timer);
983 		rc = -EBUSY;
984 		if (sclp_init_req.status == SCLP_REQ_DONE) {
985 			if (sccb->header.response_code == 0x20) {
986 				rc = 0;
987 				break;
988 			} else if (sccb->header.response_code == 0x74f0) {
989 				if (!sclp_mask_compat_mode) {
990 					sclp_mask_compat_mode = true;
991 					retry = 0;
992 				}
993 			}
994 		}
995 	}
996 	unregister_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler);
997 	spin_unlock_irqrestore(&sclp_lock, flags);
998 	return rc;
999 }
1000 
1001 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
1002  * events from interfering with rebooted system. */
1003 static int
1004 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
1005 {
1006 	sclp_deactivate();
1007 	return NOTIFY_DONE;
1008 }
1009 
1010 static struct notifier_block sclp_reboot_notifier = {
1011 	.notifier_call = sclp_reboot_event
1012 };
1013 
1014 /*
1015  * Suspend/resume SCLP notifier implementation
1016  */
1017 
1018 static void sclp_pm_event(enum sclp_pm_event sclp_pm_event, int rollback)
1019 {
1020 	struct sclp_register *reg;
1021 	unsigned long flags;
1022 
1023 	if (!rollback) {
1024 		spin_lock_irqsave(&sclp_lock, flags);
1025 		list_for_each_entry(reg, &sclp_reg_list, list)
1026 			reg->pm_event_posted = 0;
1027 		spin_unlock_irqrestore(&sclp_lock, flags);
1028 	}
1029 	do {
1030 		spin_lock_irqsave(&sclp_lock, flags);
1031 		list_for_each_entry(reg, &sclp_reg_list, list) {
1032 			if (rollback && reg->pm_event_posted)
1033 				goto found;
1034 			if (!rollback && !reg->pm_event_posted)
1035 				goto found;
1036 		}
1037 		spin_unlock_irqrestore(&sclp_lock, flags);
1038 		return;
1039 found:
1040 		spin_unlock_irqrestore(&sclp_lock, flags);
1041 		if (reg->pm_event_fn)
1042 			reg->pm_event_fn(reg, sclp_pm_event);
1043 		reg->pm_event_posted = rollback ? 0 : 1;
1044 	} while (1);
1045 }
1046 
1047 /*
1048  * Susend/resume callbacks for platform device
1049  */
1050 
1051 static int sclp_freeze(struct device *dev)
1052 {
1053 	unsigned long flags;
1054 	int rc;
1055 
1056 	sclp_pm_event(SCLP_PM_EVENT_FREEZE, 0);
1057 
1058 	spin_lock_irqsave(&sclp_lock, flags);
1059 	sclp_suspend_state = sclp_suspend_state_suspended;
1060 	spin_unlock_irqrestore(&sclp_lock, flags);
1061 
1062 	/* Init supend data */
1063 	memset(&sclp_suspend_req, 0, sizeof(sclp_suspend_req));
1064 	sclp_suspend_req.callback = sclp_suspend_req_cb;
1065 	sclp_suspend_req.status = SCLP_REQ_FILLED;
1066 	init_completion(&sclp_request_queue_flushed);
1067 
1068 	rc = sclp_add_request(&sclp_suspend_req);
1069 	if (rc == 0)
1070 		wait_for_completion(&sclp_request_queue_flushed);
1071 	else if (rc != -ENODATA)
1072 		goto fail_thaw;
1073 
1074 	rc = sclp_deactivate();
1075 	if (rc)
1076 		goto fail_thaw;
1077 	return 0;
1078 
1079 fail_thaw:
1080 	spin_lock_irqsave(&sclp_lock, flags);
1081 	sclp_suspend_state = sclp_suspend_state_running;
1082 	spin_unlock_irqrestore(&sclp_lock, flags);
1083 	sclp_pm_event(SCLP_PM_EVENT_THAW, 1);
1084 	return rc;
1085 }
1086 
1087 static int sclp_undo_suspend(enum sclp_pm_event event)
1088 {
1089 	unsigned long flags;
1090 	int rc;
1091 
1092 	rc = sclp_reactivate();
1093 	if (rc)
1094 		return rc;
1095 
1096 	spin_lock_irqsave(&sclp_lock, flags);
1097 	sclp_suspend_state = sclp_suspend_state_running;
1098 	spin_unlock_irqrestore(&sclp_lock, flags);
1099 
1100 	sclp_pm_event(event, 0);
1101 	return 0;
1102 }
1103 
1104 static int sclp_thaw(struct device *dev)
1105 {
1106 	return sclp_undo_suspend(SCLP_PM_EVENT_THAW);
1107 }
1108 
1109 static int sclp_restore(struct device *dev)
1110 {
1111 	return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE);
1112 }
1113 
1114 static const struct dev_pm_ops sclp_pm_ops = {
1115 	.freeze		= sclp_freeze,
1116 	.thaw		= sclp_thaw,
1117 	.restore	= sclp_restore,
1118 };
1119 
1120 static ssize_t con_pages_show(struct device_driver *dev, char *buf)
1121 {
1122 	return sprintf(buf, "%i\n", sclp_console_pages);
1123 }
1124 
1125 static DRIVER_ATTR_RO(con_pages);
1126 
1127 static ssize_t con_drop_show(struct device_driver *dev, char *buf)
1128 {
1129 	return sprintf(buf, "%i\n", sclp_console_drop);
1130 }
1131 
1132 static DRIVER_ATTR_RO(con_drop);
1133 
1134 static ssize_t con_full_show(struct device_driver *dev, char *buf)
1135 {
1136 	return sprintf(buf, "%lu\n", sclp_console_full);
1137 }
1138 
1139 static DRIVER_ATTR_RO(con_full);
1140 
1141 static struct attribute *sclp_drv_attrs[] = {
1142 	&driver_attr_con_pages.attr,
1143 	&driver_attr_con_drop.attr,
1144 	&driver_attr_con_full.attr,
1145 	NULL,
1146 };
1147 static struct attribute_group sclp_drv_attr_group = {
1148 	.attrs = sclp_drv_attrs,
1149 };
1150 static const struct attribute_group *sclp_drv_attr_groups[] = {
1151 	&sclp_drv_attr_group,
1152 	NULL,
1153 };
1154 
1155 static struct platform_driver sclp_pdrv = {
1156 	.driver = {
1157 		.name	= "sclp",
1158 		.pm	= &sclp_pm_ops,
1159 		.groups = sclp_drv_attr_groups,
1160 	},
1161 };
1162 
1163 static struct platform_device *sclp_pdev;
1164 
1165 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
1166  * otherwise. */
1167 static int
1168 sclp_init(void)
1169 {
1170 	unsigned long flags;
1171 	int rc = 0;
1172 
1173 	spin_lock_irqsave(&sclp_lock, flags);
1174 	/* Check for previous or running initialization */
1175 	if (sclp_init_state != sclp_init_state_uninitialized)
1176 		goto fail_unlock;
1177 	sclp_init_state = sclp_init_state_initializing;
1178 	sclp_read_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA);
1179 	sclp_init_sccb = (void *) __get_free_page(GFP_ATOMIC | GFP_DMA);
1180 	BUG_ON(!sclp_read_sccb || !sclp_init_sccb);
1181 	/* Set up variables */
1182 	list_add(&sclp_state_change_event.list, &sclp_reg_list);
1183 	timer_setup(&sclp_request_timer, NULL, 0);
1184 	timer_setup(&sclp_queue_timer, sclp_req_queue_timeout, 0);
1185 	/* Check interface */
1186 	spin_unlock_irqrestore(&sclp_lock, flags);
1187 	rc = sclp_check_interface();
1188 	spin_lock_irqsave(&sclp_lock, flags);
1189 	if (rc)
1190 		goto fail_init_state_uninitialized;
1191 	/* Register reboot handler */
1192 	rc = register_reboot_notifier(&sclp_reboot_notifier);
1193 	if (rc)
1194 		goto fail_init_state_uninitialized;
1195 	/* Register interrupt handler */
1196 	rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_interrupt_handler);
1197 	if (rc)
1198 		goto fail_unregister_reboot_notifier;
1199 	sclp_init_state = sclp_init_state_initialized;
1200 	spin_unlock_irqrestore(&sclp_lock, flags);
1201 	/* Enable service-signal external interruption - needs to happen with
1202 	 * IRQs enabled. */
1203 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
1204 	sclp_init_mask(1);
1205 	return 0;
1206 
1207 fail_unregister_reboot_notifier:
1208 	unregister_reboot_notifier(&sclp_reboot_notifier);
1209 fail_init_state_uninitialized:
1210 	sclp_init_state = sclp_init_state_uninitialized;
1211 	free_page((unsigned long) sclp_read_sccb);
1212 	free_page((unsigned long) sclp_init_sccb);
1213 fail_unlock:
1214 	spin_unlock_irqrestore(&sclp_lock, flags);
1215 	return rc;
1216 }
1217 
1218 /*
1219  * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
1220  * to print the panic message.
1221  */
1222 static int sclp_panic_notify(struct notifier_block *self,
1223 			     unsigned long event, void *data)
1224 {
1225 	if (sclp_suspend_state == sclp_suspend_state_suspended)
1226 		sclp_undo_suspend(SCLP_PM_EVENT_THAW);
1227 	return NOTIFY_OK;
1228 }
1229 
1230 static struct notifier_block sclp_on_panic_nb = {
1231 	.notifier_call = sclp_panic_notify,
1232 	.priority = SCLP_PANIC_PRIO,
1233 };
1234 
1235 static __init int sclp_initcall(void)
1236 {
1237 	int rc;
1238 
1239 	rc = platform_driver_register(&sclp_pdrv);
1240 	if (rc)
1241 		return rc;
1242 
1243 	sclp_pdev = platform_device_register_simple("sclp", -1, NULL, 0);
1244 	rc = PTR_ERR_OR_ZERO(sclp_pdev);
1245 	if (rc)
1246 		goto fail_platform_driver_unregister;
1247 
1248 	rc = atomic_notifier_chain_register(&panic_notifier_list,
1249 					    &sclp_on_panic_nb);
1250 	if (rc)
1251 		goto fail_platform_device_unregister;
1252 
1253 	return sclp_init();
1254 
1255 fail_platform_device_unregister:
1256 	platform_device_unregister(sclp_pdev);
1257 fail_platform_driver_unregister:
1258 	platform_driver_unregister(&sclp_pdrv);
1259 	return rc;
1260 }
1261 
1262 arch_initcall(sclp_initcall);
1263