xref: /openbmc/linux/drivers/s390/char/sclp.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*
2  *  drivers/s390/char/sclp.c
3  *     core function to access sclp interface
4  *
5  *  S390 version
6  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *    Author(s): Martin Peschke <mpeschke@de.ibm.com>
8  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/spinlock.h>
14 #include <linux/interrupt.h>
15 #include <linux/timer.h>
16 #include <linux/reboot.h>
17 #include <linux/jiffies.h>
18 #include <linux/init.h>
19 #include <asm/types.h>
20 #include <asm/s390_ext.h>
21 
22 #include "sclp.h"
23 
24 #define SCLP_HEADER		"sclp: "
25 
26 /* Structure for register_early_external_interrupt. */
27 static ext_int_info_t ext_int_info_hwc;
28 
29 /* Lock to protect internal data consistency. */
30 static DEFINE_SPINLOCK(sclp_lock);
31 
32 /* Mask of events that we can send to the sclp interface. */
33 static sccb_mask_t sclp_receive_mask;
34 
35 /* Mask of events that we can receive from the sclp interface. */
36 static sccb_mask_t sclp_send_mask;
37 
38 /* List of registered event listeners and senders. */
39 static struct list_head sclp_reg_list;
40 
41 /* List of queued requests. */
42 static struct list_head sclp_req_queue;
43 
44 /* Data for read and and init requests. */
45 static struct sclp_req sclp_read_req;
46 static struct sclp_req sclp_init_req;
47 static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
48 static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
49 
50 /* Timer for request retries. */
51 static struct timer_list sclp_request_timer;
52 
53 /* Internal state: is the driver initialized? */
54 static volatile enum sclp_init_state_t {
55 	sclp_init_state_uninitialized,
56 	sclp_init_state_initializing,
57 	sclp_init_state_initialized
58 } sclp_init_state = sclp_init_state_uninitialized;
59 
60 /* Internal state: is a request active at the sclp? */
61 static volatile enum sclp_running_state_t {
62 	sclp_running_state_idle,
63 	sclp_running_state_running,
64 	sclp_running_state_reset_pending
65 } sclp_running_state = sclp_running_state_idle;
66 
67 /* Internal state: is a read request pending? */
68 static volatile enum sclp_reading_state_t {
69 	sclp_reading_state_idle,
70 	sclp_reading_state_reading
71 } sclp_reading_state = sclp_reading_state_idle;
72 
73 /* Internal state: is the driver currently serving requests? */
74 static volatile enum sclp_activation_state_t {
75 	sclp_activation_state_active,
76 	sclp_activation_state_deactivating,
77 	sclp_activation_state_inactive,
78 	sclp_activation_state_activating
79 } sclp_activation_state = sclp_activation_state_active;
80 
81 /* Internal state: is an init mask request pending? */
82 static volatile enum sclp_mask_state_t {
83 	sclp_mask_state_idle,
84 	sclp_mask_state_initializing
85 } sclp_mask_state = sclp_mask_state_idle;
86 
87 /* Maximum retry counts */
88 #define SCLP_INIT_RETRY		3
89 #define SCLP_MASK_RETRY		3
90 
91 /* Timeout intervals in seconds.*/
92 #define SCLP_BUSY_INTERVAL	10
93 #define SCLP_RETRY_INTERVAL	30
94 
95 static void sclp_process_queue(void);
96 static void __sclp_make_read_req(void);
97 static int sclp_init_mask(int calculate);
98 static int sclp_init(void);
99 
100 /* Perform service call. Return 0 on success, non-zero otherwise. */
101 int
102 sclp_service_call(sclp_cmdw_t command, void *sccb)
103 {
104 	int cc;
105 
106 	asm volatile(
107 		"	.insn	rre,0xb2200000,%1,%2\n"  /* servc %1,%2 */
108 		"	ipm	%0\n"
109 		"	srl	%0,28"
110 		: "=&d" (cc) : "d" (command), "a" (__pa(sccb))
111 		: "cc", "memory");
112 	if (cc == 3)
113 		return -EIO;
114 	if (cc == 2)
115 		return -EBUSY;
116 	return 0;
117 }
118 
119 
120 static void
121 __sclp_queue_read_req(void)
122 {
123 	if (sclp_reading_state == sclp_reading_state_idle) {
124 		sclp_reading_state = sclp_reading_state_reading;
125 		__sclp_make_read_req();
126 		/* Add request to head of queue */
127 		list_add(&sclp_read_req.list, &sclp_req_queue);
128 	}
129 }
130 
131 /* Set up request retry timer. Called while sclp_lock is locked. */
132 static inline void
133 __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
134 			 unsigned long data)
135 {
136 	del_timer(&sclp_request_timer);
137 	sclp_request_timer.function = function;
138 	sclp_request_timer.data = data;
139 	sclp_request_timer.expires = jiffies + time;
140 	add_timer(&sclp_request_timer);
141 }
142 
143 /* Request timeout handler. Restart the request queue. If DATA is non-zero,
144  * force restart of running request. */
145 static void
146 sclp_request_timeout(unsigned long data)
147 {
148 	unsigned long flags;
149 
150 	spin_lock_irqsave(&sclp_lock, flags);
151 	if (data) {
152 		if (sclp_running_state == sclp_running_state_running) {
153 			/* Break running state and queue NOP read event request
154 			 * to get a defined interface state. */
155 			__sclp_queue_read_req();
156 			sclp_running_state = sclp_running_state_idle;
157 		}
158 	} else {
159 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
160 					 sclp_request_timeout, 0);
161 	}
162 	spin_unlock_irqrestore(&sclp_lock, flags);
163 	sclp_process_queue();
164 }
165 
166 /* Try to start a request. Return zero if the request was successfully
167  * started or if it will be started at a later time. Return non-zero otherwise.
168  * Called while sclp_lock is locked. */
169 static int
170 __sclp_start_request(struct sclp_req *req)
171 {
172 	int rc;
173 
174 	if (sclp_running_state != sclp_running_state_idle)
175 		return 0;
176 	del_timer(&sclp_request_timer);
177 	rc = sclp_service_call(req->command, req->sccb);
178 	req->start_count++;
179 
180 	if (rc == 0) {
181 		/* Sucessfully started request */
182 		req->status = SCLP_REQ_RUNNING;
183 		sclp_running_state = sclp_running_state_running;
184 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
185 					 sclp_request_timeout, 1);
186 		return 0;
187 	} else if (rc == -EBUSY) {
188 		/* Try again later */
189 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
190 					 sclp_request_timeout, 0);
191 		return 0;
192 	}
193 	/* Request failed */
194 	req->status = SCLP_REQ_FAILED;
195 	return rc;
196 }
197 
198 /* Try to start queued requests. */
199 static void
200 sclp_process_queue(void)
201 {
202 	struct sclp_req *req;
203 	int rc;
204 	unsigned long flags;
205 
206 	spin_lock_irqsave(&sclp_lock, flags);
207 	if (sclp_running_state != sclp_running_state_idle) {
208 		spin_unlock_irqrestore(&sclp_lock, flags);
209 		return;
210 	}
211 	del_timer(&sclp_request_timer);
212 	while (!list_empty(&sclp_req_queue)) {
213 		req = list_entry(sclp_req_queue.next, struct sclp_req, list);
214 		rc = __sclp_start_request(req);
215 		if (rc == 0)
216 			break;
217 		/* Request failed */
218 		if (req->start_count > 1) {
219 			/* Cannot abort already submitted request - could still
220 			 * be active at the SCLP */
221 			__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
222 						 sclp_request_timeout, 0);
223 			break;
224 		}
225 		/* Post-processing for aborted request */
226 		list_del(&req->list);
227 		if (req->callback) {
228 			spin_unlock_irqrestore(&sclp_lock, flags);
229 			req->callback(req, req->callback_data);
230 			spin_lock_irqsave(&sclp_lock, flags);
231 		}
232 	}
233 	spin_unlock_irqrestore(&sclp_lock, flags);
234 }
235 
236 /* Queue a new request. Return zero on success, non-zero otherwise. */
237 int
238 sclp_add_request(struct sclp_req *req)
239 {
240 	unsigned long flags;
241 	int rc;
242 
243 	spin_lock_irqsave(&sclp_lock, flags);
244 	if ((sclp_init_state != sclp_init_state_initialized ||
245 	     sclp_activation_state != sclp_activation_state_active) &&
246 	    req != &sclp_init_req) {
247 		spin_unlock_irqrestore(&sclp_lock, flags);
248 		return -EIO;
249 	}
250 	req->status = SCLP_REQ_QUEUED;
251 	req->start_count = 0;
252 	list_add_tail(&req->list, &sclp_req_queue);
253 	rc = 0;
254 	/* Start if request is first in list */
255 	if (sclp_running_state == sclp_running_state_idle &&
256 	    req->list.prev == &sclp_req_queue) {
257 		rc = __sclp_start_request(req);
258 		if (rc)
259 			list_del(&req->list);
260 	}
261 	spin_unlock_irqrestore(&sclp_lock, flags);
262 	return rc;
263 }
264 
265 EXPORT_SYMBOL(sclp_add_request);
266 
267 /* Dispatch events found in request buffer to registered listeners. Return 0
268  * if all events were dispatched, non-zero otherwise. */
269 static int
270 sclp_dispatch_evbufs(struct sccb_header *sccb)
271 {
272 	unsigned long flags;
273 	struct evbuf_header *evbuf;
274 	struct list_head *l;
275 	struct sclp_register *reg;
276 	int offset;
277 	int rc;
278 
279 	spin_lock_irqsave(&sclp_lock, flags);
280 	rc = 0;
281 	for (offset = sizeof(struct sccb_header); offset < sccb->length;
282 	     offset += evbuf->length) {
283 		/* Search for event handler */
284 		evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
285 		reg = NULL;
286 		list_for_each(l, &sclp_reg_list) {
287 			reg = list_entry(l, struct sclp_register, list);
288 			if (reg->receive_mask & (1 << (32 - evbuf->type)))
289 				break;
290 			else
291 				reg = NULL;
292 		}
293 		if (reg && reg->receiver_fn) {
294 			spin_unlock_irqrestore(&sclp_lock, flags);
295 			reg->receiver_fn(evbuf);
296 			spin_lock_irqsave(&sclp_lock, flags);
297 		} else if (reg == NULL)
298 			rc = -ENOSYS;
299 	}
300 	spin_unlock_irqrestore(&sclp_lock, flags);
301 	return rc;
302 }
303 
304 /* Read event data request callback. */
305 static void
306 sclp_read_cb(struct sclp_req *req, void *data)
307 {
308 	unsigned long flags;
309 	struct sccb_header *sccb;
310 
311 	sccb = (struct sccb_header *) req->sccb;
312 	if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
313 	    sccb->response_code == 0x220))
314 		sclp_dispatch_evbufs(sccb);
315 	spin_lock_irqsave(&sclp_lock, flags);
316 	sclp_reading_state = sclp_reading_state_idle;
317 	spin_unlock_irqrestore(&sclp_lock, flags);
318 }
319 
320 /* Prepare read event data request. Called while sclp_lock is locked. */
321 static void __sclp_make_read_req(void)
322 {
323 	struct sccb_header *sccb;
324 
325 	sccb = (struct sccb_header *) sclp_read_sccb;
326 	clear_page(sccb);
327 	memset(&sclp_read_req, 0, sizeof(struct sclp_req));
328 	sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
329 	sclp_read_req.status = SCLP_REQ_QUEUED;
330 	sclp_read_req.start_count = 0;
331 	sclp_read_req.callback = sclp_read_cb;
332 	sclp_read_req.sccb = sccb;
333 	sccb->length = PAGE_SIZE;
334 	sccb->function_code = 0;
335 	sccb->control_mask[2] = 0x80;
336 }
337 
338 /* Search request list for request with matching sccb. Return request if found,
339  * NULL otherwise. Called while sclp_lock is locked. */
340 static inline struct sclp_req *
341 __sclp_find_req(u32 sccb)
342 {
343 	struct list_head *l;
344 	struct sclp_req *req;
345 
346 	list_for_each(l, &sclp_req_queue) {
347 		req = list_entry(l, struct sclp_req, list);
348 		if (sccb == (u32) (addr_t) req->sccb)
349 				return req;
350 	}
351 	return NULL;
352 }
353 
354 /* Handler for external interruption. Perform request post-processing.
355  * Prepare read event data request if necessary. Start processing of next
356  * request on queue. */
357 static void
358 sclp_interrupt_handler(__u16 code)
359 {
360 	struct sclp_req *req;
361 	u32 finished_sccb;
362 	u32 evbuf_pending;
363 
364 	spin_lock(&sclp_lock);
365 	finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
366 	evbuf_pending = S390_lowcore.ext_params & 0x3;
367 	if (finished_sccb) {
368 		del_timer(&sclp_request_timer);
369 		sclp_running_state = sclp_running_state_reset_pending;
370 		req = __sclp_find_req(finished_sccb);
371 		if (req) {
372 			/* Request post-processing */
373 			list_del(&req->list);
374 			req->status = SCLP_REQ_DONE;
375 			if (req->callback) {
376 				spin_unlock(&sclp_lock);
377 				req->callback(req, req->callback_data);
378 				spin_lock(&sclp_lock);
379 			}
380 		}
381 		sclp_running_state = sclp_running_state_idle;
382 	}
383 	if (evbuf_pending &&
384 	    sclp_activation_state == sclp_activation_state_active)
385 		__sclp_queue_read_req();
386 	spin_unlock(&sclp_lock);
387 	sclp_process_queue();
388 }
389 
390 /* Convert interval in jiffies to TOD ticks. */
391 static inline u64
392 sclp_tod_from_jiffies(unsigned long jiffies)
393 {
394 	return (u64) (jiffies / HZ) << 32;
395 }
396 
397 /* Wait until a currently running request finished. Note: while this function
398  * is running, no timers are served on the calling CPU. */
399 void
400 sclp_sync_wait(void)
401 {
402 	unsigned long long old_tick;
403 	unsigned long flags;
404 	unsigned long cr0, cr0_sync;
405 	u64 timeout;
406 	int irq_context;
407 
408 	/* We'll be disabling timer interrupts, so we need a custom timeout
409 	 * mechanism */
410 	timeout = 0;
411 	if (timer_pending(&sclp_request_timer)) {
412 		/* Get timeout TOD value */
413 		timeout = get_clock() +
414 			  sclp_tod_from_jiffies(sclp_request_timer.expires -
415 						jiffies);
416 	}
417 	local_irq_save(flags);
418 	/* Prevent bottom half from executing once we force interrupts open */
419 	irq_context = in_interrupt();
420 	if (!irq_context)
421 		local_bh_disable();
422 	/* Enable service-signal interruption, disable timer interrupts */
423 	old_tick = local_tick_disable();
424 	trace_hardirqs_on();
425 	__ctl_store(cr0, 0, 0);
426 	cr0_sync = cr0;
427 	cr0_sync &= 0xffff00a0;
428 	cr0_sync |= 0x00000200;
429 	__ctl_load(cr0_sync, 0, 0);
430 	__raw_local_irq_stosm(0x01);
431 	/* Loop until driver state indicates finished request */
432 	while (sclp_running_state != sclp_running_state_idle) {
433 		/* Check for expired request timer */
434 		if (timer_pending(&sclp_request_timer) &&
435 		    get_clock() > timeout &&
436 		    del_timer(&sclp_request_timer))
437 			sclp_request_timer.function(sclp_request_timer.data);
438 		cpu_relax();
439 	}
440 	local_irq_disable();
441 	__ctl_load(cr0, 0, 0);
442 	if (!irq_context)
443 		_local_bh_enable();
444 	local_tick_enable(old_tick);
445 	local_irq_restore(flags);
446 }
447 EXPORT_SYMBOL(sclp_sync_wait);
448 
449 /* Dispatch changes in send and receive mask to registered listeners. */
450 static void
451 sclp_dispatch_state_change(void)
452 {
453 	struct list_head *l;
454 	struct sclp_register *reg;
455 	unsigned long flags;
456 	sccb_mask_t receive_mask;
457 	sccb_mask_t send_mask;
458 
459 	do {
460 		spin_lock_irqsave(&sclp_lock, flags);
461 		reg = NULL;
462 		list_for_each(l, &sclp_reg_list) {
463 			reg = list_entry(l, struct sclp_register, list);
464 			receive_mask = reg->send_mask & sclp_receive_mask;
465 			send_mask = reg->receive_mask & sclp_send_mask;
466 			if (reg->sclp_receive_mask != receive_mask ||
467 			    reg->sclp_send_mask != send_mask) {
468 				reg->sclp_receive_mask = receive_mask;
469 				reg->sclp_send_mask = send_mask;
470 				break;
471 			} else
472 				reg = NULL;
473 		}
474 		spin_unlock_irqrestore(&sclp_lock, flags);
475 		if (reg && reg->state_change_fn)
476 			reg->state_change_fn(reg);
477 	} while (reg);
478 }
479 
480 struct sclp_statechangebuf {
481 	struct evbuf_header	header;
482 	u8		validity_sclp_active_facility_mask : 1;
483 	u8		validity_sclp_receive_mask : 1;
484 	u8		validity_sclp_send_mask : 1;
485 	u8		validity_read_data_function_mask : 1;
486 	u16		_zeros : 12;
487 	u16		mask_length;
488 	u64		sclp_active_facility_mask;
489 	sccb_mask_t	sclp_receive_mask;
490 	sccb_mask_t	sclp_send_mask;
491 	u32		read_data_function_mask;
492 } __attribute__((packed));
493 
494 
495 /* State change event callback. Inform listeners of changes. */
496 static void
497 sclp_state_change_cb(struct evbuf_header *evbuf)
498 {
499 	unsigned long flags;
500 	struct sclp_statechangebuf *scbuf;
501 
502 	scbuf = (struct sclp_statechangebuf *) evbuf;
503 	if (scbuf->mask_length != sizeof(sccb_mask_t))
504 		return;
505 	spin_lock_irqsave(&sclp_lock, flags);
506 	if (scbuf->validity_sclp_receive_mask)
507 		sclp_receive_mask = scbuf->sclp_receive_mask;
508 	if (scbuf->validity_sclp_send_mask)
509 		sclp_send_mask = scbuf->sclp_send_mask;
510 	spin_unlock_irqrestore(&sclp_lock, flags);
511 	if (scbuf->validity_sclp_active_facility_mask)
512 		sclp_facilities = scbuf->sclp_active_facility_mask;
513 	sclp_dispatch_state_change();
514 }
515 
516 static struct sclp_register sclp_state_change_event = {
517 	.receive_mask = EVTYP_STATECHANGE_MASK,
518 	.receiver_fn = sclp_state_change_cb
519 };
520 
521 /* Calculate receive and send mask of currently registered listeners.
522  * Called while sclp_lock is locked. */
523 static inline void
524 __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
525 {
526 	struct list_head *l;
527 	struct sclp_register *t;
528 
529 	*receive_mask = 0;
530 	*send_mask = 0;
531 	list_for_each(l, &sclp_reg_list) {
532 		t = list_entry(l, struct sclp_register, list);
533 		*receive_mask |= t->receive_mask;
534 		*send_mask |= t->send_mask;
535 	}
536 }
537 
538 /* Register event listener. Return 0 on success, non-zero otherwise. */
539 int
540 sclp_register(struct sclp_register *reg)
541 {
542 	unsigned long flags;
543 	sccb_mask_t receive_mask;
544 	sccb_mask_t send_mask;
545 	int rc;
546 
547 	rc = sclp_init();
548 	if (rc)
549 		return rc;
550 	spin_lock_irqsave(&sclp_lock, flags);
551 	/* Check event mask for collisions */
552 	__sclp_get_mask(&receive_mask, &send_mask);
553 	if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
554 		spin_unlock_irqrestore(&sclp_lock, flags);
555 		return -EBUSY;
556 	}
557 	/* Trigger initial state change callback */
558 	reg->sclp_receive_mask = 0;
559 	reg->sclp_send_mask = 0;
560 	list_add(&reg->list, &sclp_reg_list);
561 	spin_unlock_irqrestore(&sclp_lock, flags);
562 	rc = sclp_init_mask(1);
563 	if (rc) {
564 		spin_lock_irqsave(&sclp_lock, flags);
565 		list_del(&reg->list);
566 		spin_unlock_irqrestore(&sclp_lock, flags);
567 	}
568 	return rc;
569 }
570 
571 EXPORT_SYMBOL(sclp_register);
572 
573 /* Unregister event listener. */
574 void
575 sclp_unregister(struct sclp_register *reg)
576 {
577 	unsigned long flags;
578 
579 	spin_lock_irqsave(&sclp_lock, flags);
580 	list_del(&reg->list);
581 	spin_unlock_irqrestore(&sclp_lock, flags);
582 	sclp_init_mask(1);
583 }
584 
585 EXPORT_SYMBOL(sclp_unregister);
586 
587 /* Remove event buffers which are marked processed. Return the number of
588  * remaining event buffers. */
589 int
590 sclp_remove_processed(struct sccb_header *sccb)
591 {
592 	struct evbuf_header *evbuf;
593 	int unprocessed;
594 	u16 remaining;
595 
596 	evbuf = (struct evbuf_header *) (sccb + 1);
597 	unprocessed = 0;
598 	remaining = sccb->length - sizeof(struct sccb_header);
599 	while (remaining > 0) {
600 		remaining -= evbuf->length;
601 		if (evbuf->flags & 0x80) {
602 			sccb->length -= evbuf->length;
603 			memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
604 			       remaining);
605 		} else {
606 			unprocessed++;
607 			evbuf = (struct evbuf_header *)
608 					((addr_t) evbuf + evbuf->length);
609 		}
610 	}
611 	return unprocessed;
612 }
613 
614 EXPORT_SYMBOL(sclp_remove_processed);
615 
616 struct init_sccb {
617 	struct sccb_header header;
618 	u16 _reserved;
619 	u16 mask_length;
620 	sccb_mask_t receive_mask;
621 	sccb_mask_t send_mask;
622 	sccb_mask_t sclp_receive_mask;
623 	sccb_mask_t sclp_send_mask;
624 } __attribute__((packed));
625 
626 /* Prepare init mask request. Called while sclp_lock is locked. */
627 static inline void
628 __sclp_make_init_req(u32 receive_mask, u32 send_mask)
629 {
630 	struct init_sccb *sccb;
631 
632 	sccb = (struct init_sccb *) sclp_init_sccb;
633 	clear_page(sccb);
634 	memset(&sclp_init_req, 0, sizeof(struct sclp_req));
635 	sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
636 	sclp_init_req.status = SCLP_REQ_FILLED;
637 	sclp_init_req.start_count = 0;
638 	sclp_init_req.callback = NULL;
639 	sclp_init_req.callback_data = NULL;
640 	sclp_init_req.sccb = sccb;
641 	sccb->header.length = sizeof(struct init_sccb);
642 	sccb->mask_length = sizeof(sccb_mask_t);
643 	sccb->receive_mask = receive_mask;
644 	sccb->send_mask = send_mask;
645 	sccb->sclp_receive_mask = 0;
646 	sccb->sclp_send_mask = 0;
647 }
648 
649 /* Start init mask request. If calculate is non-zero, calculate the mask as
650  * requested by registered listeners. Use zero mask otherwise. Return 0 on
651  * success, non-zero otherwise. */
652 static int
653 sclp_init_mask(int calculate)
654 {
655 	unsigned long flags;
656 	struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
657 	sccb_mask_t receive_mask;
658 	sccb_mask_t send_mask;
659 	int retry;
660 	int rc;
661 	unsigned long wait;
662 
663 	spin_lock_irqsave(&sclp_lock, flags);
664 	/* Check if interface is in appropriate state */
665 	if (sclp_mask_state != sclp_mask_state_idle) {
666 		spin_unlock_irqrestore(&sclp_lock, flags);
667 		return -EBUSY;
668 	}
669 	if (sclp_activation_state == sclp_activation_state_inactive) {
670 		spin_unlock_irqrestore(&sclp_lock, flags);
671 		return -EINVAL;
672 	}
673 	sclp_mask_state = sclp_mask_state_initializing;
674 	/* Determine mask */
675 	if (calculate)
676 		__sclp_get_mask(&receive_mask, &send_mask);
677 	else {
678 		receive_mask = 0;
679 		send_mask = 0;
680 	}
681 	rc = -EIO;
682 	for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
683 		/* Prepare request */
684 		__sclp_make_init_req(receive_mask, send_mask);
685 		spin_unlock_irqrestore(&sclp_lock, flags);
686 		if (sclp_add_request(&sclp_init_req)) {
687 			/* Try again later */
688 			wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
689 			while (time_before(jiffies, wait))
690 				sclp_sync_wait();
691 			spin_lock_irqsave(&sclp_lock, flags);
692 			continue;
693 		}
694 		while (sclp_init_req.status != SCLP_REQ_DONE &&
695 		       sclp_init_req.status != SCLP_REQ_FAILED)
696 			sclp_sync_wait();
697 		spin_lock_irqsave(&sclp_lock, flags);
698 		if (sclp_init_req.status == SCLP_REQ_DONE &&
699 		    sccb->header.response_code == 0x20) {
700 			/* Successful request */
701 			if (calculate) {
702 				sclp_receive_mask = sccb->sclp_receive_mask;
703 				sclp_send_mask = sccb->sclp_send_mask;
704 			} else {
705 				sclp_receive_mask = 0;
706 				sclp_send_mask = 0;
707 			}
708 			spin_unlock_irqrestore(&sclp_lock, flags);
709 			sclp_dispatch_state_change();
710 			spin_lock_irqsave(&sclp_lock, flags);
711 			rc = 0;
712 			break;
713 		}
714 	}
715 	sclp_mask_state = sclp_mask_state_idle;
716 	spin_unlock_irqrestore(&sclp_lock, flags);
717 	return rc;
718 }
719 
720 /* Deactivate SCLP interface. On success, new requests will be rejected,
721  * events will no longer be dispatched. Return 0 on success, non-zero
722  * otherwise. */
723 int
724 sclp_deactivate(void)
725 {
726 	unsigned long flags;
727 	int rc;
728 
729 	spin_lock_irqsave(&sclp_lock, flags);
730 	/* Deactivate can only be called when active */
731 	if (sclp_activation_state != sclp_activation_state_active) {
732 		spin_unlock_irqrestore(&sclp_lock, flags);
733 		return -EINVAL;
734 	}
735 	sclp_activation_state = sclp_activation_state_deactivating;
736 	spin_unlock_irqrestore(&sclp_lock, flags);
737 	rc = sclp_init_mask(0);
738 	spin_lock_irqsave(&sclp_lock, flags);
739 	if (rc == 0)
740 		sclp_activation_state = sclp_activation_state_inactive;
741 	else
742 		sclp_activation_state = sclp_activation_state_active;
743 	spin_unlock_irqrestore(&sclp_lock, flags);
744 	return rc;
745 }
746 
747 EXPORT_SYMBOL(sclp_deactivate);
748 
749 /* Reactivate SCLP interface after sclp_deactivate. On success, new
750  * requests will be accepted, events will be dispatched again. Return 0 on
751  * success, non-zero otherwise. */
752 int
753 sclp_reactivate(void)
754 {
755 	unsigned long flags;
756 	int rc;
757 
758 	spin_lock_irqsave(&sclp_lock, flags);
759 	/* Reactivate can only be called when inactive */
760 	if (sclp_activation_state != sclp_activation_state_inactive) {
761 		spin_unlock_irqrestore(&sclp_lock, flags);
762 		return -EINVAL;
763 	}
764 	sclp_activation_state = sclp_activation_state_activating;
765 	spin_unlock_irqrestore(&sclp_lock, flags);
766 	rc = sclp_init_mask(1);
767 	spin_lock_irqsave(&sclp_lock, flags);
768 	if (rc == 0)
769 		sclp_activation_state = sclp_activation_state_active;
770 	else
771 		sclp_activation_state = sclp_activation_state_inactive;
772 	spin_unlock_irqrestore(&sclp_lock, flags);
773 	return rc;
774 }
775 
776 EXPORT_SYMBOL(sclp_reactivate);
777 
778 /* Handler for external interruption used during initialization. Modify
779  * request state to done. */
780 static void
781 sclp_check_handler(__u16 code)
782 {
783 	u32 finished_sccb;
784 
785 	finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
786 	/* Is this the interrupt we are waiting for? */
787 	if (finished_sccb == 0)
788 		return;
789 	if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
790 		panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
791 		      finished_sccb);
792 	spin_lock(&sclp_lock);
793 	if (sclp_running_state == sclp_running_state_running) {
794 		sclp_init_req.status = SCLP_REQ_DONE;
795 		sclp_running_state = sclp_running_state_idle;
796 	}
797 	spin_unlock(&sclp_lock);
798 }
799 
800 /* Initial init mask request timed out. Modify request state to failed. */
801 static void
802 sclp_check_timeout(unsigned long data)
803 {
804 	unsigned long flags;
805 
806 	spin_lock_irqsave(&sclp_lock, flags);
807 	if (sclp_running_state == sclp_running_state_running) {
808 		sclp_init_req.status = SCLP_REQ_FAILED;
809 		sclp_running_state = sclp_running_state_idle;
810 	}
811 	spin_unlock_irqrestore(&sclp_lock, flags);
812 }
813 
814 /* Perform a check of the SCLP interface. Return zero if the interface is
815  * available and there are no pending requests from a previous instance.
816  * Return non-zero otherwise. */
817 static int
818 sclp_check_interface(void)
819 {
820 	struct init_sccb *sccb;
821 	unsigned long flags;
822 	int retry;
823 	int rc;
824 
825 	spin_lock_irqsave(&sclp_lock, flags);
826 	/* Prepare init mask command */
827 	rc = register_early_external_interrupt(0x2401, sclp_check_handler,
828 					       &ext_int_info_hwc);
829 	if (rc) {
830 		spin_unlock_irqrestore(&sclp_lock, flags);
831 		return rc;
832 	}
833 	for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
834 		__sclp_make_init_req(0, 0);
835 		sccb = (struct init_sccb *) sclp_init_req.sccb;
836 		rc = sclp_service_call(sclp_init_req.command, sccb);
837 		if (rc == -EIO)
838 			break;
839 		sclp_init_req.status = SCLP_REQ_RUNNING;
840 		sclp_running_state = sclp_running_state_running;
841 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
842 					 sclp_check_timeout, 0);
843 		spin_unlock_irqrestore(&sclp_lock, flags);
844 		/* Enable service-signal interruption - needs to happen
845 		 * with IRQs enabled. */
846 		ctl_set_bit(0, 9);
847 		/* Wait for signal from interrupt or timeout */
848 		sclp_sync_wait();
849 		/* Disable service-signal interruption - needs to happen
850 		 * with IRQs enabled. */
851 		ctl_clear_bit(0,9);
852 		spin_lock_irqsave(&sclp_lock, flags);
853 		del_timer(&sclp_request_timer);
854 		if (sclp_init_req.status == SCLP_REQ_DONE &&
855 		    sccb->header.response_code == 0x20) {
856 			rc = 0;
857 			break;
858 		} else
859 			rc = -EBUSY;
860 	}
861 	unregister_early_external_interrupt(0x2401, sclp_check_handler,
862 					    &ext_int_info_hwc);
863 	spin_unlock_irqrestore(&sclp_lock, flags);
864 	return rc;
865 }
866 
867 /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
868  * events from interfering with rebooted system. */
869 static int
870 sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
871 {
872 	sclp_deactivate();
873 	return NOTIFY_DONE;
874 }
875 
876 static struct notifier_block sclp_reboot_notifier = {
877 	.notifier_call = sclp_reboot_event
878 };
879 
880 /* Initialize SCLP driver. Return zero if driver is operational, non-zero
881  * otherwise. */
882 static int
883 sclp_init(void)
884 {
885 	unsigned long flags;
886 	int rc;
887 
888 	spin_lock_irqsave(&sclp_lock, flags);
889 	/* Check for previous or running initialization */
890 	if (sclp_init_state != sclp_init_state_uninitialized) {
891 		spin_unlock_irqrestore(&sclp_lock, flags);
892 		return 0;
893 	}
894 	sclp_init_state = sclp_init_state_initializing;
895 	/* Set up variables */
896 	INIT_LIST_HEAD(&sclp_req_queue);
897 	INIT_LIST_HEAD(&sclp_reg_list);
898 	list_add(&sclp_state_change_event.list, &sclp_reg_list);
899 	init_timer(&sclp_request_timer);
900 	/* Check interface */
901 	spin_unlock_irqrestore(&sclp_lock, flags);
902 	rc = sclp_check_interface();
903 	spin_lock_irqsave(&sclp_lock, flags);
904 	if (rc) {
905 		sclp_init_state = sclp_init_state_uninitialized;
906 		spin_unlock_irqrestore(&sclp_lock, flags);
907 		return rc;
908 	}
909 	/* Register reboot handler */
910 	rc = register_reboot_notifier(&sclp_reboot_notifier);
911 	if (rc) {
912 		sclp_init_state = sclp_init_state_uninitialized;
913 		spin_unlock_irqrestore(&sclp_lock, flags);
914 		return rc;
915 	}
916 	/* Register interrupt handler */
917 	rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
918 					       &ext_int_info_hwc);
919 	if (rc) {
920 		unregister_reboot_notifier(&sclp_reboot_notifier);
921 		sclp_init_state = sclp_init_state_uninitialized;
922 		spin_unlock_irqrestore(&sclp_lock, flags);
923 		return rc;
924 	}
925 	sclp_init_state = sclp_init_state_initialized;
926 	spin_unlock_irqrestore(&sclp_lock, flags);
927 	/* Enable service-signal external interruption - needs to happen with
928 	 * IRQs enabled. */
929 	ctl_set_bit(0, 9);
930 	sclp_init_mask(1);
931 	return 0;
932 }
933 
934 static __init int sclp_initcall(void)
935 {
936 	return sclp_init();
937 }
938 
939 arch_initcall(sclp_initcall);
940