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