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