xref: /openbmc/linux/drivers/s390/char/sclp.c (revision e2e5a0f2b100a5204d27def8bbf73333d1710be2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  drivers/s390/char/sclp.c
31da177e4SLinus Torvalds  *     core function to access sclp interface
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  S390 version
61da177e4SLinus Torvalds  *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
71da177e4SLinus Torvalds  *    Author(s): Martin Peschke <mpeschke@de.ibm.com>
81da177e4SLinus Torvalds  *		 Martin Schwidefsky <schwidefsky@de.ibm.com>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/err.h>
131da177e4SLinus Torvalds #include <linux/spinlock.h>
141da177e4SLinus Torvalds #include <linux/interrupt.h>
151da177e4SLinus Torvalds #include <linux/timer.h>
161da177e4SLinus Torvalds #include <linux/reboot.h>
171da177e4SLinus Torvalds #include <linux/jiffies.h>
18b3d00c3bSPeter Oberparleiter #include <linux/init.h>
191da177e4SLinus Torvalds #include <asm/types.h>
201da177e4SLinus Torvalds #include <asm/s390_ext.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "sclp.h"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define SCLP_HEADER		"sclp: "
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /* Structure for register_early_external_interrupt. */
271da177e4SLinus Torvalds static ext_int_info_t ext_int_info_hwc;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /* Lock to protect internal data consistency. */
301da177e4SLinus Torvalds static DEFINE_SPINLOCK(sclp_lock);
311da177e4SLinus Torvalds 
32d082d3ceSPeter Oberparleiter /* Mask of events that we can send to the sclp interface. */
331da177e4SLinus Torvalds static sccb_mask_t sclp_receive_mask;
341da177e4SLinus Torvalds 
35d082d3ceSPeter Oberparleiter /* Mask of events that we can receive from the sclp interface. */
361da177e4SLinus Torvalds static sccb_mask_t sclp_send_mask;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /* List of registered event listeners and senders. */
391da177e4SLinus Torvalds static struct list_head sclp_reg_list;
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* List of queued requests. */
421da177e4SLinus Torvalds static struct list_head sclp_req_queue;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* Data for read and and init requests. */
451da177e4SLinus Torvalds static struct sclp_req sclp_read_req;
461da177e4SLinus Torvalds static struct sclp_req sclp_init_req;
471da177e4SLinus Torvalds static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
481da177e4SLinus Torvalds static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* Timer for request retries. */
511da177e4SLinus Torvalds static struct timer_list sclp_request_timer;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* Internal state: is the driver initialized? */
541da177e4SLinus Torvalds static volatile enum sclp_init_state_t {
551da177e4SLinus Torvalds 	sclp_init_state_uninitialized,
561da177e4SLinus Torvalds 	sclp_init_state_initializing,
571da177e4SLinus Torvalds 	sclp_init_state_initialized
581da177e4SLinus Torvalds } sclp_init_state = sclp_init_state_uninitialized;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* Internal state: is a request active at the sclp? */
611da177e4SLinus Torvalds static volatile enum sclp_running_state_t {
621da177e4SLinus Torvalds 	sclp_running_state_idle,
63dbd8ae63SPeter Oberparleiter 	sclp_running_state_running,
64dbd8ae63SPeter Oberparleiter 	sclp_running_state_reset_pending
651da177e4SLinus Torvalds } sclp_running_state = sclp_running_state_idle;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Internal state: is a read request pending? */
681da177e4SLinus Torvalds static volatile enum sclp_reading_state_t {
691da177e4SLinus Torvalds 	sclp_reading_state_idle,
701da177e4SLinus Torvalds 	sclp_reading_state_reading
711da177e4SLinus Torvalds } sclp_reading_state = sclp_reading_state_idle;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Internal state: is the driver currently serving requests? */
741da177e4SLinus Torvalds static volatile enum sclp_activation_state_t {
751da177e4SLinus Torvalds 	sclp_activation_state_active,
761da177e4SLinus Torvalds 	sclp_activation_state_deactivating,
771da177e4SLinus Torvalds 	sclp_activation_state_inactive,
781da177e4SLinus Torvalds 	sclp_activation_state_activating
791da177e4SLinus Torvalds } sclp_activation_state = sclp_activation_state_active;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /* Internal state: is an init mask request pending? */
821da177e4SLinus Torvalds static volatile enum sclp_mask_state_t {
831da177e4SLinus Torvalds 	sclp_mask_state_idle,
841da177e4SLinus Torvalds 	sclp_mask_state_initializing
851da177e4SLinus Torvalds } sclp_mask_state = sclp_mask_state_idle;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* Maximum retry counts */
881da177e4SLinus Torvalds #define SCLP_INIT_RETRY		3
891da177e4SLinus Torvalds #define SCLP_MASK_RETRY		3
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* Timeout intervals in seconds.*/
9225fab9ebSPeter Oberparleiter #define SCLP_BUSY_INTERVAL	10
93dbd8ae63SPeter Oberparleiter #define SCLP_RETRY_INTERVAL	30
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds static void sclp_process_queue(void);
96364c8558SHeiko Carstens static void __sclp_make_read_req(void);
971da177e4SLinus Torvalds static int sclp_init_mask(int calculate);
981da177e4SLinus Torvalds static int sclp_init(void);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Perform service call. Return 0 on success, non-zero otherwise. */
101ab14de6cSHeiko Carstens int
102ab14de6cSHeiko Carstens sclp_service_call(sclp_cmdw_t command, void *sccb)
1031da177e4SLinus Torvalds {
1041da177e4SLinus Torvalds 	int cc;
1051da177e4SLinus Torvalds 
10694c12cc7SMartin Schwidefsky 	asm volatile(
1071da177e4SLinus Torvalds 		"	.insn	rre,0xb2200000,%1,%2\n"  /* servc %1,%2 */
1081da177e4SLinus Torvalds 		"	ipm	%0\n"
1091da177e4SLinus Torvalds 		"	srl	%0,28"
11094c12cc7SMartin Schwidefsky 		: "=&d" (cc) : "d" (command), "a" (__pa(sccb))
1111da177e4SLinus Torvalds 		: "cc", "memory");
1121da177e4SLinus Torvalds 	if (cc == 3)
1131da177e4SLinus Torvalds 		return -EIO;
1141da177e4SLinus Torvalds 	if (cc == 2)
1151da177e4SLinus Torvalds 		return -EBUSY;
1161da177e4SLinus Torvalds 	return 0;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 
120dbd8ae63SPeter Oberparleiter static void
121dbd8ae63SPeter Oberparleiter __sclp_queue_read_req(void)
122dbd8ae63SPeter Oberparleiter {
123dbd8ae63SPeter Oberparleiter 	if (sclp_reading_state == sclp_reading_state_idle) {
124dbd8ae63SPeter Oberparleiter 		sclp_reading_state = sclp_reading_state_reading;
125dbd8ae63SPeter Oberparleiter 		__sclp_make_read_req();
126dbd8ae63SPeter Oberparleiter 		/* Add request to head of queue */
127dbd8ae63SPeter Oberparleiter 		list_add(&sclp_read_req.list, &sclp_req_queue);
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* Set up request retry timer. Called while sclp_lock is locked. */
1321da177e4SLinus Torvalds static inline void
1331da177e4SLinus Torvalds __sclp_set_request_timer(unsigned long time, void (*function)(unsigned long),
1341da177e4SLinus Torvalds 			 unsigned long data)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
1371da177e4SLinus Torvalds 	sclp_request_timer.function = function;
1381da177e4SLinus Torvalds 	sclp_request_timer.data = data;
1391da177e4SLinus Torvalds 	sclp_request_timer.expires = jiffies + time;
1401da177e4SLinus Torvalds 	add_timer(&sclp_request_timer);
1411da177e4SLinus Torvalds }
1421da177e4SLinus Torvalds 
143dbd8ae63SPeter Oberparleiter /* Request timeout handler. Restart the request queue. If DATA is non-zero,
144dbd8ae63SPeter Oberparleiter  * force restart of running request. */
145dbd8ae63SPeter Oberparleiter static void
146dbd8ae63SPeter Oberparleiter sclp_request_timeout(unsigned long data)
147dbd8ae63SPeter Oberparleiter {
148dbd8ae63SPeter Oberparleiter 	unsigned long flags;
149dbd8ae63SPeter Oberparleiter 
150dbd8ae63SPeter Oberparleiter 	spin_lock_irqsave(&sclp_lock, flags);
151dbd8ae63SPeter Oberparleiter 	if (data) {
152dbd8ae63SPeter Oberparleiter 		if (sclp_running_state == sclp_running_state_running) {
153dbd8ae63SPeter Oberparleiter 			/* Break running state and queue NOP read event request
154dbd8ae63SPeter Oberparleiter 			 * to get a defined interface state. */
155dbd8ae63SPeter Oberparleiter 			__sclp_queue_read_req();
156dbd8ae63SPeter Oberparleiter 			sclp_running_state = sclp_running_state_idle;
157dbd8ae63SPeter Oberparleiter 		}
158dbd8ae63SPeter Oberparleiter 	} else {
159dbd8ae63SPeter Oberparleiter 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
160dbd8ae63SPeter Oberparleiter 					 sclp_request_timeout, 0);
161dbd8ae63SPeter Oberparleiter 	}
162dbd8ae63SPeter Oberparleiter 	spin_unlock_irqrestore(&sclp_lock, flags);
163dbd8ae63SPeter Oberparleiter 	sclp_process_queue();
164dbd8ae63SPeter Oberparleiter }
165dbd8ae63SPeter Oberparleiter 
1661da177e4SLinus Torvalds /* Try to start a request. Return zero if the request was successfully
1671da177e4SLinus Torvalds  * started or if it will be started at a later time. Return non-zero otherwise.
1681da177e4SLinus Torvalds  * Called while sclp_lock is locked. */
1691da177e4SLinus Torvalds static int
1701da177e4SLinus Torvalds __sclp_start_request(struct sclp_req *req)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	int rc;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	if (sclp_running_state != sclp_running_state_idle)
1751da177e4SLinus Torvalds 		return 0;
1761da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
177ab14de6cSHeiko Carstens 	rc = sclp_service_call(req->command, req->sccb);
1781da177e4SLinus Torvalds 	req->start_count++;
17925fab9ebSPeter Oberparleiter 
1801da177e4SLinus Torvalds 	if (rc == 0) {
1811da177e4SLinus Torvalds 		/* Sucessfully started request */
1821da177e4SLinus Torvalds 		req->status = SCLP_REQ_RUNNING;
1831da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_running;
1841da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
1851da177e4SLinus Torvalds 					 sclp_request_timeout, 1);
1861da177e4SLinus Torvalds 		return 0;
1871da177e4SLinus Torvalds 	} else if (rc == -EBUSY) {
1881da177e4SLinus Torvalds 		/* Try again later */
1891da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
1901da177e4SLinus Torvalds 					 sclp_request_timeout, 0);
1911da177e4SLinus Torvalds 		return 0;
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 	/* Request failed */
1941da177e4SLinus Torvalds 	req->status = SCLP_REQ_FAILED;
1951da177e4SLinus Torvalds 	return rc;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /* Try to start queued requests. */
1991da177e4SLinus Torvalds static void
2001da177e4SLinus Torvalds sclp_process_queue(void)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct sclp_req *req;
2031da177e4SLinus Torvalds 	int rc;
2041da177e4SLinus Torvalds 	unsigned long flags;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
2071da177e4SLinus Torvalds 	if (sclp_running_state != sclp_running_state_idle) {
2081da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
2091da177e4SLinus Torvalds 		return;
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
2121da177e4SLinus Torvalds 	while (!list_empty(&sclp_req_queue)) {
2131da177e4SLinus Torvalds 		req = list_entry(sclp_req_queue.next, struct sclp_req, list);
2141da177e4SLinus Torvalds 		rc = __sclp_start_request(req);
2151da177e4SLinus Torvalds 		if (rc == 0)
2161da177e4SLinus Torvalds 			break;
217dbd8ae63SPeter Oberparleiter 		/* Request failed */
218dbd8ae63SPeter Oberparleiter 		if (req->start_count > 1) {
219dbd8ae63SPeter Oberparleiter 			/* Cannot abort already submitted request - could still
220dbd8ae63SPeter Oberparleiter 			 * be active at the SCLP */
221dbd8ae63SPeter Oberparleiter 			__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
222dbd8ae63SPeter Oberparleiter 						 sclp_request_timeout, 0);
223dbd8ae63SPeter Oberparleiter 			break;
224dbd8ae63SPeter Oberparleiter 		}
225dbd8ae63SPeter Oberparleiter 		/* Post-processing for aborted request */
2261da177e4SLinus Torvalds 		list_del(&req->list);
2271da177e4SLinus Torvalds 		if (req->callback) {
2281da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
2291da177e4SLinus Torvalds 			req->callback(req, req->callback_data);
2301da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
2311da177e4SLinus Torvalds 		}
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /* Queue a new request. Return zero on success, non-zero otherwise. */
2371da177e4SLinus Torvalds int
2381da177e4SLinus Torvalds sclp_add_request(struct sclp_req *req)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	unsigned long flags;
2411da177e4SLinus Torvalds 	int rc;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
2441da177e4SLinus Torvalds 	if ((sclp_init_state != sclp_init_state_initialized ||
2451da177e4SLinus Torvalds 	     sclp_activation_state != sclp_activation_state_active) &&
2461da177e4SLinus Torvalds 	    req != &sclp_init_req) {
2471da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
2481da177e4SLinus Torvalds 		return -EIO;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 	req->status = SCLP_REQ_QUEUED;
2511da177e4SLinus Torvalds 	req->start_count = 0;
2521da177e4SLinus Torvalds 	list_add_tail(&req->list, &sclp_req_queue);
2531da177e4SLinus Torvalds 	rc = 0;
2541da177e4SLinus Torvalds 	/* Start if request is first in list */
255dbd8ae63SPeter Oberparleiter 	if (sclp_running_state == sclp_running_state_idle &&
256dbd8ae63SPeter Oberparleiter 	    req->list.prev == &sclp_req_queue) {
2571da177e4SLinus Torvalds 		rc = __sclp_start_request(req);
2581da177e4SLinus Torvalds 		if (rc)
2591da177e4SLinus Torvalds 			list_del(&req->list);
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
2621da177e4SLinus Torvalds 	return rc;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_add_request);
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds /* Dispatch events found in request buffer to registered listeners. Return 0
2681da177e4SLinus Torvalds  * if all events were dispatched, non-zero otherwise. */
2691da177e4SLinus Torvalds static int
2701da177e4SLinus Torvalds sclp_dispatch_evbufs(struct sccb_header *sccb)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	unsigned long flags;
2731da177e4SLinus Torvalds 	struct evbuf_header *evbuf;
2741da177e4SLinus Torvalds 	struct list_head *l;
2751da177e4SLinus Torvalds 	struct sclp_register *reg;
2761da177e4SLinus Torvalds 	int offset;
2771da177e4SLinus Torvalds 	int rc;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
2801da177e4SLinus Torvalds 	rc = 0;
2811da177e4SLinus Torvalds 	for (offset = sizeof(struct sccb_header); offset < sccb->length;
2821da177e4SLinus Torvalds 	     offset += evbuf->length) {
2831da177e4SLinus Torvalds 		evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
284*e2e5a0f2SPeter Oberparleiter 		/* Check for malformed hardware response */
285*e2e5a0f2SPeter Oberparleiter 		if (evbuf->length == 0)
286*e2e5a0f2SPeter Oberparleiter 			break;
287*e2e5a0f2SPeter Oberparleiter 		/* Search for event handler */
2881da177e4SLinus Torvalds 		reg = NULL;
2891da177e4SLinus Torvalds 		list_for_each(l, &sclp_reg_list) {
2901da177e4SLinus Torvalds 			reg = list_entry(l, struct sclp_register, list);
2911da177e4SLinus Torvalds 			if (reg->receive_mask & (1 << (32 - evbuf->type)))
2921da177e4SLinus Torvalds 				break;
2931da177e4SLinus Torvalds 			else
2941da177e4SLinus Torvalds 				reg = NULL;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 		if (reg && reg->receiver_fn) {
2971da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
2981da177e4SLinus Torvalds 			reg->receiver_fn(evbuf);
2991da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
3001da177e4SLinus Torvalds 		} else if (reg == NULL)
3011da177e4SLinus Torvalds 			rc = -ENOSYS;
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
3041da177e4SLinus Torvalds 	return rc;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /* Read event data request callback. */
3081da177e4SLinus Torvalds static void
3091da177e4SLinus Torvalds sclp_read_cb(struct sclp_req *req, void *data)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	unsigned long flags;
3121da177e4SLinus Torvalds 	struct sccb_header *sccb;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	sccb = (struct sccb_header *) req->sccb;
3151da177e4SLinus Torvalds 	if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
3161da177e4SLinus Torvalds 	    sccb->response_code == 0x220))
3171da177e4SLinus Torvalds 		sclp_dispatch_evbufs(sccb);
3181da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
3191da177e4SLinus Torvalds 	sclp_reading_state = sclp_reading_state_idle;
3201da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /* Prepare read event data request. Called while sclp_lock is locked. */
324364c8558SHeiko Carstens static void __sclp_make_read_req(void)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct sccb_header *sccb;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	sccb = (struct sccb_header *) sclp_read_sccb;
3291da177e4SLinus Torvalds 	clear_page(sccb);
3301da177e4SLinus Torvalds 	memset(&sclp_read_req, 0, sizeof(struct sclp_req));
331ab14de6cSHeiko Carstens 	sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
3321da177e4SLinus Torvalds 	sclp_read_req.status = SCLP_REQ_QUEUED;
3331da177e4SLinus Torvalds 	sclp_read_req.start_count = 0;
3341da177e4SLinus Torvalds 	sclp_read_req.callback = sclp_read_cb;
3351da177e4SLinus Torvalds 	sclp_read_req.sccb = sccb;
3361da177e4SLinus Torvalds 	sccb->length = PAGE_SIZE;
3371da177e4SLinus Torvalds 	sccb->function_code = 0;
3381da177e4SLinus Torvalds 	sccb->control_mask[2] = 0x80;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* Search request list for request with matching sccb. Return request if found,
3421da177e4SLinus Torvalds  * NULL otherwise. Called while sclp_lock is locked. */
3431da177e4SLinus Torvalds static inline struct sclp_req *
3441da177e4SLinus Torvalds __sclp_find_req(u32 sccb)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	struct list_head *l;
3471da177e4SLinus Torvalds 	struct sclp_req *req;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	list_for_each(l, &sclp_req_queue) {
3501da177e4SLinus Torvalds 		req = list_entry(l, struct sclp_req, list);
3511da177e4SLinus Torvalds 		if (sccb == (u32) (addr_t) req->sccb)
3521da177e4SLinus Torvalds 				return req;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 	return NULL;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds /* Handler for external interruption. Perform request post-processing.
3581da177e4SLinus Torvalds  * Prepare read event data request if necessary. Start processing of next
3591da177e4SLinus Torvalds  * request on queue. */
3601da177e4SLinus Torvalds static void
3615a489b98SHeiko Carstens sclp_interrupt_handler(__u16 code)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	struct sclp_req *req;
3641da177e4SLinus Torvalds 	u32 finished_sccb;
3651da177e4SLinus Torvalds 	u32 evbuf_pending;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	spin_lock(&sclp_lock);
3681da177e4SLinus Torvalds 	finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
3691da177e4SLinus Torvalds 	evbuf_pending = S390_lowcore.ext_params & 0x3;
3701da177e4SLinus Torvalds 	if (finished_sccb) {
371dbd8ae63SPeter Oberparleiter 		del_timer(&sclp_request_timer);
372dbd8ae63SPeter Oberparleiter 		sclp_running_state = sclp_running_state_reset_pending;
3731da177e4SLinus Torvalds 		req = __sclp_find_req(finished_sccb);
3741da177e4SLinus Torvalds 		if (req) {
3751da177e4SLinus Torvalds 			/* Request post-processing */
3761da177e4SLinus Torvalds 			list_del(&req->list);
3771da177e4SLinus Torvalds 			req->status = SCLP_REQ_DONE;
3781da177e4SLinus Torvalds 			if (req->callback) {
3791da177e4SLinus Torvalds 				spin_unlock(&sclp_lock);
3801da177e4SLinus Torvalds 				req->callback(req, req->callback_data);
3811da177e4SLinus Torvalds 				spin_lock(&sclp_lock);
3821da177e4SLinus Torvalds 			}
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
3851da177e4SLinus Torvalds 	}
386d082d3ceSPeter Oberparleiter 	if (evbuf_pending &&
387dbd8ae63SPeter Oberparleiter 	    sclp_activation_state == sclp_activation_state_active)
388dbd8ae63SPeter Oberparleiter 		__sclp_queue_read_req();
3891da177e4SLinus Torvalds 	spin_unlock(&sclp_lock);
3901da177e4SLinus Torvalds 	sclp_process_queue();
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds /* Convert interval in jiffies to TOD ticks. */
3941da177e4SLinus Torvalds static inline u64
3951da177e4SLinus Torvalds sclp_tod_from_jiffies(unsigned long jiffies)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	return (u64) (jiffies / HZ) << 32;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds /* Wait until a currently running request finished. Note: while this function
4011da177e4SLinus Torvalds  * is running, no timers are served on the calling CPU. */
4021da177e4SLinus Torvalds void
4031da177e4SLinus Torvalds sclp_sync_wait(void)
4041da177e4SLinus Torvalds {
405934b2857SHeiko Carstens 	unsigned long long old_tick;
4061f194a4cSHeiko Carstens 	unsigned long flags;
4071da177e4SLinus Torvalds 	unsigned long cr0, cr0_sync;
4081da177e4SLinus Torvalds 	u64 timeout;
409c59d744bSHeiko Carstens 	int irq_context;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	/* We'll be disabling timer interrupts, so we need a custom timeout
4121da177e4SLinus Torvalds 	 * mechanism */
4131da177e4SLinus Torvalds 	timeout = 0;
4141da177e4SLinus Torvalds 	if (timer_pending(&sclp_request_timer)) {
4151da177e4SLinus Torvalds 		/* Get timeout TOD value */
41694c12cc7SMartin Schwidefsky 		timeout = get_clock() +
4171da177e4SLinus Torvalds 			  sclp_tod_from_jiffies(sclp_request_timer.expires -
4181da177e4SLinus Torvalds 						jiffies);
4191da177e4SLinus Torvalds 	}
4201f194a4cSHeiko Carstens 	local_irq_save(flags);
4211da177e4SLinus Torvalds 	/* Prevent bottom half from executing once we force interrupts open */
422c59d744bSHeiko Carstens 	irq_context = in_interrupt();
423c59d744bSHeiko Carstens 	if (!irq_context)
4241da177e4SLinus Torvalds 		local_bh_disable();
4251da177e4SLinus Torvalds 	/* Enable service-signal interruption, disable timer interrupts */
426934b2857SHeiko Carstens 	old_tick = local_tick_disable();
4271f194a4cSHeiko Carstens 	trace_hardirqs_on();
4281da177e4SLinus Torvalds 	__ctl_store(cr0, 0, 0);
4291da177e4SLinus Torvalds 	cr0_sync = cr0;
430934b2857SHeiko Carstens 	cr0_sync &= 0xffff00a0;
4311da177e4SLinus Torvalds 	cr0_sync |= 0x00000200;
4321da177e4SLinus Torvalds 	__ctl_load(cr0_sync, 0, 0);
43394c12cc7SMartin Schwidefsky 	__raw_local_irq_stosm(0x01);
4341da177e4SLinus Torvalds 	/* Loop until driver state indicates finished request */
4351da177e4SLinus Torvalds 	while (sclp_running_state != sclp_running_state_idle) {
4361da177e4SLinus Torvalds 		/* Check for expired request timer */
4371da177e4SLinus Torvalds 		if (timer_pending(&sclp_request_timer) &&
43894c12cc7SMartin Schwidefsky 		    get_clock() > timeout &&
4391da177e4SLinus Torvalds 		    del_timer(&sclp_request_timer))
4401da177e4SLinus Torvalds 			sclp_request_timer.function(sclp_request_timer.data);
4411da177e4SLinus Torvalds 		cpu_relax();
4421da177e4SLinus Torvalds 	}
4431f194a4cSHeiko Carstens 	local_irq_disable();
4441da177e4SLinus Torvalds 	__ctl_load(cr0, 0, 0);
445c59d744bSHeiko Carstens 	if (!irq_context)
4461f194a4cSHeiko Carstens 		_local_bh_enable();
447934b2857SHeiko Carstens 	local_tick_enable(old_tick);
4481f194a4cSHeiko Carstens 	local_irq_restore(flags);
4491da177e4SLinus Torvalds }
4501da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_sync_wait);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /* Dispatch changes in send and receive mask to registered listeners. */
4534d284cacSHeiko Carstens static void
4541da177e4SLinus Torvalds sclp_dispatch_state_change(void)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	struct list_head *l;
4571da177e4SLinus Torvalds 	struct sclp_register *reg;
4581da177e4SLinus Torvalds 	unsigned long flags;
4591da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
4601da177e4SLinus Torvalds 	sccb_mask_t send_mask;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	do {
4631da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
4641da177e4SLinus Torvalds 		reg = NULL;
4651da177e4SLinus Torvalds 		list_for_each(l, &sclp_reg_list) {
4661da177e4SLinus Torvalds 			reg = list_entry(l, struct sclp_register, list);
467d082d3ceSPeter Oberparleiter 			receive_mask = reg->send_mask & sclp_receive_mask;
468d082d3ceSPeter Oberparleiter 			send_mask = reg->receive_mask & sclp_send_mask;
4691da177e4SLinus Torvalds 			if (reg->sclp_receive_mask != receive_mask ||
4701da177e4SLinus Torvalds 			    reg->sclp_send_mask != send_mask) {
4711da177e4SLinus Torvalds 				reg->sclp_receive_mask = receive_mask;
4721da177e4SLinus Torvalds 				reg->sclp_send_mask = send_mask;
4731da177e4SLinus Torvalds 				break;
4741da177e4SLinus Torvalds 			} else
4751da177e4SLinus Torvalds 				reg = NULL;
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
4781da177e4SLinus Torvalds 		if (reg && reg->state_change_fn)
4791da177e4SLinus Torvalds 			reg->state_change_fn(reg);
4801da177e4SLinus Torvalds 	} while (reg);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds struct sclp_statechangebuf {
4841da177e4SLinus Torvalds 	struct evbuf_header	header;
4851da177e4SLinus Torvalds 	u8		validity_sclp_active_facility_mask : 1;
4861da177e4SLinus Torvalds 	u8		validity_sclp_receive_mask : 1;
4871da177e4SLinus Torvalds 	u8		validity_sclp_send_mask : 1;
4881da177e4SLinus Torvalds 	u8		validity_read_data_function_mask : 1;
4891da177e4SLinus Torvalds 	u16		_zeros : 12;
4901da177e4SLinus Torvalds 	u16		mask_length;
4911da177e4SLinus Torvalds 	u64		sclp_active_facility_mask;
4921da177e4SLinus Torvalds 	sccb_mask_t	sclp_receive_mask;
4931da177e4SLinus Torvalds 	sccb_mask_t	sclp_send_mask;
4941da177e4SLinus Torvalds 	u32		read_data_function_mask;
4951da177e4SLinus Torvalds } __attribute__((packed));
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /* State change event callback. Inform listeners of changes. */
4991da177e4SLinus Torvalds static void
5001da177e4SLinus Torvalds sclp_state_change_cb(struct evbuf_header *evbuf)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	unsigned long flags;
5031da177e4SLinus Torvalds 	struct sclp_statechangebuf *scbuf;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	scbuf = (struct sclp_statechangebuf *) evbuf;
5061da177e4SLinus Torvalds 	if (scbuf->mask_length != sizeof(sccb_mask_t))
5071da177e4SLinus Torvalds 		return;
5081da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
5091da177e4SLinus Torvalds 	if (scbuf->validity_sclp_receive_mask)
5101da177e4SLinus Torvalds 		sclp_receive_mask = scbuf->sclp_receive_mask;
5111da177e4SLinus Torvalds 	if (scbuf->validity_sclp_send_mask)
5121da177e4SLinus Torvalds 		sclp_send_mask = scbuf->sclp_send_mask;
5131da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
514887d935aSHeiko Carstens 	if (scbuf->validity_sclp_active_facility_mask)
515887d935aSHeiko Carstens 		sclp_facilities = scbuf->sclp_active_facility_mask;
5161da177e4SLinus Torvalds 	sclp_dispatch_state_change();
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds static struct sclp_register sclp_state_change_event = {
5206d4740c8SStefan Haberland 	.receive_mask = EVTYP_STATECHANGE_MASK,
5211da177e4SLinus Torvalds 	.receiver_fn = sclp_state_change_cb
5221da177e4SLinus Torvalds };
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds /* Calculate receive and send mask of currently registered listeners.
5251da177e4SLinus Torvalds  * Called while sclp_lock is locked. */
5261da177e4SLinus Torvalds static inline void
5271da177e4SLinus Torvalds __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct list_head *l;
5301da177e4SLinus Torvalds 	struct sclp_register *t;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	*receive_mask = 0;
5331da177e4SLinus Torvalds 	*send_mask = 0;
5341da177e4SLinus Torvalds 	list_for_each(l, &sclp_reg_list) {
5351da177e4SLinus Torvalds 		t = list_entry(l, struct sclp_register, list);
5361da177e4SLinus Torvalds 		*receive_mask |= t->receive_mask;
5371da177e4SLinus Torvalds 		*send_mask |= t->send_mask;
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /* Register event listener. Return 0 on success, non-zero otherwise. */
5421da177e4SLinus Torvalds int
5431da177e4SLinus Torvalds sclp_register(struct sclp_register *reg)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	unsigned long flags;
5461da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
5471da177e4SLinus Torvalds 	sccb_mask_t send_mask;
5481da177e4SLinus Torvalds 	int rc;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	rc = sclp_init();
5511da177e4SLinus Torvalds 	if (rc)
5521da177e4SLinus Torvalds 		return rc;
5531da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
5541da177e4SLinus Torvalds 	/* Check event mask for collisions */
5551da177e4SLinus Torvalds 	__sclp_get_mask(&receive_mask, &send_mask);
5561da177e4SLinus Torvalds 	if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
5571da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
5581da177e4SLinus Torvalds 		return -EBUSY;
5591da177e4SLinus Torvalds 	}
5601da177e4SLinus Torvalds 	/* Trigger initial state change callback */
5611da177e4SLinus Torvalds 	reg->sclp_receive_mask = 0;
5621da177e4SLinus Torvalds 	reg->sclp_send_mask = 0;
5631da177e4SLinus Torvalds 	list_add(&reg->list, &sclp_reg_list);
5641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
5651da177e4SLinus Torvalds 	rc = sclp_init_mask(1);
5661da177e4SLinus Torvalds 	if (rc) {
5671da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
5681da177e4SLinus Torvalds 		list_del(&reg->list);
5691da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	return rc;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_register);
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds /* Unregister event listener. */
5771da177e4SLinus Torvalds void
5781da177e4SLinus Torvalds sclp_unregister(struct sclp_register *reg)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	unsigned long flags;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
5831da177e4SLinus Torvalds 	list_del(&reg->list);
5841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
5851da177e4SLinus Torvalds 	sclp_init_mask(1);
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_unregister);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /* Remove event buffers which are marked processed. Return the number of
5911da177e4SLinus Torvalds  * remaining event buffers. */
5921da177e4SLinus Torvalds int
5931da177e4SLinus Torvalds sclp_remove_processed(struct sccb_header *sccb)
5941da177e4SLinus Torvalds {
5951da177e4SLinus Torvalds 	struct evbuf_header *evbuf;
5961da177e4SLinus Torvalds 	int unprocessed;
5971da177e4SLinus Torvalds 	u16 remaining;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	evbuf = (struct evbuf_header *) (sccb + 1);
6001da177e4SLinus Torvalds 	unprocessed = 0;
6011da177e4SLinus Torvalds 	remaining = sccb->length - sizeof(struct sccb_header);
6021da177e4SLinus Torvalds 	while (remaining > 0) {
6031da177e4SLinus Torvalds 		remaining -= evbuf->length;
6041da177e4SLinus Torvalds 		if (evbuf->flags & 0x80) {
6051da177e4SLinus Torvalds 			sccb->length -= evbuf->length;
6061da177e4SLinus Torvalds 			memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
6071da177e4SLinus Torvalds 			       remaining);
6081da177e4SLinus Torvalds 		} else {
6091da177e4SLinus Torvalds 			unprocessed++;
6101da177e4SLinus Torvalds 			evbuf = (struct evbuf_header *)
6111da177e4SLinus Torvalds 					((addr_t) evbuf + evbuf->length);
6121da177e4SLinus Torvalds 		}
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 	return unprocessed;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_remove_processed);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds struct init_sccb {
6201da177e4SLinus Torvalds 	struct sccb_header header;
6211da177e4SLinus Torvalds 	u16 _reserved;
6221da177e4SLinus Torvalds 	u16 mask_length;
6231da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
6241da177e4SLinus Torvalds 	sccb_mask_t send_mask;
6251da177e4SLinus Torvalds 	sccb_mask_t sclp_receive_mask;
626d082d3ceSPeter Oberparleiter 	sccb_mask_t sclp_send_mask;
6271da177e4SLinus Torvalds } __attribute__((packed));
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds /* Prepare init mask request. Called while sclp_lock is locked. */
6301da177e4SLinus Torvalds static inline void
6311da177e4SLinus Torvalds __sclp_make_init_req(u32 receive_mask, u32 send_mask)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	struct init_sccb *sccb;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	sccb = (struct init_sccb *) sclp_init_sccb;
6361da177e4SLinus Torvalds 	clear_page(sccb);
6371da177e4SLinus Torvalds 	memset(&sclp_init_req, 0, sizeof(struct sclp_req));
638ab14de6cSHeiko Carstens 	sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
6391da177e4SLinus Torvalds 	sclp_init_req.status = SCLP_REQ_FILLED;
6401da177e4SLinus Torvalds 	sclp_init_req.start_count = 0;
6411da177e4SLinus Torvalds 	sclp_init_req.callback = NULL;
6421da177e4SLinus Torvalds 	sclp_init_req.callback_data = NULL;
6431da177e4SLinus Torvalds 	sclp_init_req.sccb = sccb;
6441da177e4SLinus Torvalds 	sccb->header.length = sizeof(struct init_sccb);
6451da177e4SLinus Torvalds 	sccb->mask_length = sizeof(sccb_mask_t);
6461da177e4SLinus Torvalds 	sccb->receive_mask = receive_mask;
6471da177e4SLinus Torvalds 	sccb->send_mask = send_mask;
6481da177e4SLinus Torvalds 	sccb->sclp_receive_mask = 0;
6491da177e4SLinus Torvalds 	sccb->sclp_send_mask = 0;
6501da177e4SLinus Torvalds }
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds /* Start init mask request. If calculate is non-zero, calculate the mask as
6531da177e4SLinus Torvalds  * requested by registered listeners. Use zero mask otherwise. Return 0 on
6541da177e4SLinus Torvalds  * success, non-zero otherwise. */
6551da177e4SLinus Torvalds static int
6561da177e4SLinus Torvalds sclp_init_mask(int calculate)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	unsigned long flags;
6591da177e4SLinus Torvalds 	struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
6601da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
6611da177e4SLinus Torvalds 	sccb_mask_t send_mask;
6621da177e4SLinus Torvalds 	int retry;
6631da177e4SLinus Torvalds 	int rc;
6641da177e4SLinus Torvalds 	unsigned long wait;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
6671da177e4SLinus Torvalds 	/* Check if interface is in appropriate state */
6681da177e4SLinus Torvalds 	if (sclp_mask_state != sclp_mask_state_idle) {
6691da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
6701da177e4SLinus Torvalds 		return -EBUSY;
6711da177e4SLinus Torvalds 	}
6721da177e4SLinus Torvalds 	if (sclp_activation_state == sclp_activation_state_inactive) {
6731da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
6741da177e4SLinus Torvalds 		return -EINVAL;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds 	sclp_mask_state = sclp_mask_state_initializing;
6771da177e4SLinus Torvalds 	/* Determine mask */
6781da177e4SLinus Torvalds 	if (calculate)
6791da177e4SLinus Torvalds 		__sclp_get_mask(&receive_mask, &send_mask);
6801da177e4SLinus Torvalds 	else {
6811da177e4SLinus Torvalds 		receive_mask = 0;
6821da177e4SLinus Torvalds 		send_mask = 0;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	rc = -EIO;
6851da177e4SLinus Torvalds 	for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
6861da177e4SLinus Torvalds 		/* Prepare request */
6871da177e4SLinus Torvalds 		__sclp_make_init_req(receive_mask, send_mask);
6881da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
6891da177e4SLinus Torvalds 		if (sclp_add_request(&sclp_init_req)) {
6901da177e4SLinus Torvalds 			/* Try again later */
6911da177e4SLinus Torvalds 			wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
6921da177e4SLinus Torvalds 			while (time_before(jiffies, wait))
6931da177e4SLinus Torvalds 				sclp_sync_wait();
6941da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
6951da177e4SLinus Torvalds 			continue;
6961da177e4SLinus Torvalds 		}
6971da177e4SLinus Torvalds 		while (sclp_init_req.status != SCLP_REQ_DONE &&
6981da177e4SLinus Torvalds 		       sclp_init_req.status != SCLP_REQ_FAILED)
6991da177e4SLinus Torvalds 			sclp_sync_wait();
7001da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
7011da177e4SLinus Torvalds 		if (sclp_init_req.status == SCLP_REQ_DONE &&
7021da177e4SLinus Torvalds 		    sccb->header.response_code == 0x20) {
7031da177e4SLinus Torvalds 			/* Successful request */
7041da177e4SLinus Torvalds 			if (calculate) {
7051da177e4SLinus Torvalds 				sclp_receive_mask = sccb->sclp_receive_mask;
7061da177e4SLinus Torvalds 				sclp_send_mask = sccb->sclp_send_mask;
7071da177e4SLinus Torvalds 			} else {
7081da177e4SLinus Torvalds 				sclp_receive_mask = 0;
7091da177e4SLinus Torvalds 				sclp_send_mask = 0;
7101da177e4SLinus Torvalds 			}
7111da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
7121da177e4SLinus Torvalds 			sclp_dispatch_state_change();
7131da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
7141da177e4SLinus Torvalds 			rc = 0;
7151da177e4SLinus Torvalds 			break;
7161da177e4SLinus Torvalds 		}
7171da177e4SLinus Torvalds 	}
7181da177e4SLinus Torvalds 	sclp_mask_state = sclp_mask_state_idle;
7191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7201da177e4SLinus Torvalds 	return rc;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /* Deactivate SCLP interface. On success, new requests will be rejected,
7241da177e4SLinus Torvalds  * events will no longer be dispatched. Return 0 on success, non-zero
7251da177e4SLinus Torvalds  * otherwise. */
7261da177e4SLinus Torvalds int
7271da177e4SLinus Torvalds sclp_deactivate(void)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	unsigned long flags;
7301da177e4SLinus Torvalds 	int rc;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7331da177e4SLinus Torvalds 	/* Deactivate can only be called when active */
7341da177e4SLinus Torvalds 	if (sclp_activation_state != sclp_activation_state_active) {
7351da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
7361da177e4SLinus Torvalds 		return -EINVAL;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 	sclp_activation_state = sclp_activation_state_deactivating;
7391da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7401da177e4SLinus Torvalds 	rc = sclp_init_mask(0);
7411da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7421da177e4SLinus Torvalds 	if (rc == 0)
7431da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_inactive;
7441da177e4SLinus Torvalds 	else
7451da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_active;
7461da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7471da177e4SLinus Torvalds 	return rc;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_deactivate);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* Reactivate SCLP interface after sclp_deactivate. On success, new
7531da177e4SLinus Torvalds  * requests will be accepted, events will be dispatched again. Return 0 on
7541da177e4SLinus Torvalds  * success, non-zero otherwise. */
7551da177e4SLinus Torvalds int
7561da177e4SLinus Torvalds sclp_reactivate(void)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	unsigned long flags;
7591da177e4SLinus Torvalds 	int rc;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7621da177e4SLinus Torvalds 	/* Reactivate can only be called when inactive */
7631da177e4SLinus Torvalds 	if (sclp_activation_state != sclp_activation_state_inactive) {
7641da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
7651da177e4SLinus Torvalds 		return -EINVAL;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 	sclp_activation_state = sclp_activation_state_activating;
7681da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7691da177e4SLinus Torvalds 	rc = sclp_init_mask(1);
7701da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7711da177e4SLinus Torvalds 	if (rc == 0)
7721da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_active;
7731da177e4SLinus Torvalds 	else
7741da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_inactive;
7751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7761da177e4SLinus Torvalds 	return rc;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_reactivate);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds /* Handler for external interruption used during initialization. Modify
7821da177e4SLinus Torvalds  * request state to done. */
7831da177e4SLinus Torvalds static void
7845a489b98SHeiko Carstens sclp_check_handler(__u16 code)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	u32 finished_sccb;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
7891da177e4SLinus Torvalds 	/* Is this the interrupt we are waiting for? */
7901da177e4SLinus Torvalds 	if (finished_sccb == 0)
7911da177e4SLinus Torvalds 		return;
792a12c53f4SMartin Schwidefsky 	if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
793a12c53f4SMartin Schwidefsky 		panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
794a12c53f4SMartin Schwidefsky 		      finished_sccb);
7951da177e4SLinus Torvalds 	spin_lock(&sclp_lock);
7961da177e4SLinus Torvalds 	if (sclp_running_state == sclp_running_state_running) {
7971da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_DONE;
7981da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 	spin_unlock(&sclp_lock);
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds /* Initial init mask request timed out. Modify request state to failed. */
8041da177e4SLinus Torvalds static void
8051da177e4SLinus Torvalds sclp_check_timeout(unsigned long data)
8061da177e4SLinus Torvalds {
8071da177e4SLinus Torvalds 	unsigned long flags;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8101da177e4SLinus Torvalds 	if (sclp_running_state == sclp_running_state_running) {
8111da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_FAILED;
8121da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds /* Perform a check of the SCLP interface. Return zero if the interface is
8181da177e4SLinus Torvalds  * available and there are no pending requests from a previous instance.
8191da177e4SLinus Torvalds  * Return non-zero otherwise. */
8201da177e4SLinus Torvalds static int
8211da177e4SLinus Torvalds sclp_check_interface(void)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct init_sccb *sccb;
8241da177e4SLinus Torvalds 	unsigned long flags;
8251da177e4SLinus Torvalds 	int retry;
8261da177e4SLinus Torvalds 	int rc;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8291da177e4SLinus Torvalds 	/* Prepare init mask command */
8301da177e4SLinus Torvalds 	rc = register_early_external_interrupt(0x2401, sclp_check_handler,
8311da177e4SLinus Torvalds 					       &ext_int_info_hwc);
8321da177e4SLinus Torvalds 	if (rc) {
8331da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8341da177e4SLinus Torvalds 		return rc;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 	for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
8371da177e4SLinus Torvalds 		__sclp_make_init_req(0, 0);
8381da177e4SLinus Torvalds 		sccb = (struct init_sccb *) sclp_init_req.sccb;
839ab14de6cSHeiko Carstens 		rc = sclp_service_call(sclp_init_req.command, sccb);
8401da177e4SLinus Torvalds 		if (rc == -EIO)
8411da177e4SLinus Torvalds 			break;
8421da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_RUNNING;
8431da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_running;
8441da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
8451da177e4SLinus Torvalds 					 sclp_check_timeout, 0);
8461da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8471da177e4SLinus Torvalds 		/* Enable service-signal interruption - needs to happen
8481da177e4SLinus Torvalds 		 * with IRQs enabled. */
8491da177e4SLinus Torvalds 		ctl_set_bit(0, 9);
8501da177e4SLinus Torvalds 		/* Wait for signal from interrupt or timeout */
8511da177e4SLinus Torvalds 		sclp_sync_wait();
8521da177e4SLinus Torvalds 		/* Disable service-signal interruption - needs to happen
8531da177e4SLinus Torvalds 		 * with IRQs enabled. */
8541da177e4SLinus Torvalds 		ctl_clear_bit(0,9);
8551da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
8561da177e4SLinus Torvalds 		del_timer(&sclp_request_timer);
8571da177e4SLinus Torvalds 		if (sclp_init_req.status == SCLP_REQ_DONE &&
8581da177e4SLinus Torvalds 		    sccb->header.response_code == 0x20) {
8591da177e4SLinus Torvalds 			rc = 0;
8601da177e4SLinus Torvalds 			break;
8611da177e4SLinus Torvalds 		} else
8621da177e4SLinus Torvalds 			rc = -EBUSY;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 	unregister_early_external_interrupt(0x2401, sclp_check_handler,
8651da177e4SLinus Torvalds 					    &ext_int_info_hwc);
8661da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8671da177e4SLinus Torvalds 	return rc;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
8711da177e4SLinus Torvalds  * events from interfering with rebooted system. */
8721da177e4SLinus Torvalds static int
8731da177e4SLinus Torvalds sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	sclp_deactivate();
8761da177e4SLinus Torvalds 	return NOTIFY_DONE;
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds static struct notifier_block sclp_reboot_notifier = {
8801da177e4SLinus Torvalds 	.notifier_call = sclp_reboot_event
8811da177e4SLinus Torvalds };
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds /* Initialize SCLP driver. Return zero if driver is operational, non-zero
8841da177e4SLinus Torvalds  * otherwise. */
8851da177e4SLinus Torvalds static int
8861da177e4SLinus Torvalds sclp_init(void)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	unsigned long flags;
8891da177e4SLinus Torvalds 	int rc;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8921da177e4SLinus Torvalds 	/* Check for previous or running initialization */
8931da177e4SLinus Torvalds 	if (sclp_init_state != sclp_init_state_uninitialized) {
8941da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8951da177e4SLinus Torvalds 		return 0;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 	sclp_init_state = sclp_init_state_initializing;
8981da177e4SLinus Torvalds 	/* Set up variables */
8991da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sclp_req_queue);
9001da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sclp_reg_list);
9011da177e4SLinus Torvalds 	list_add(&sclp_state_change_event.list, &sclp_reg_list);
9021da177e4SLinus Torvalds 	init_timer(&sclp_request_timer);
9031da177e4SLinus Torvalds 	/* Check interface */
9041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
9051da177e4SLinus Torvalds 	rc = sclp_check_interface();
9061da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
9071da177e4SLinus Torvalds 	if (rc) {
9081da177e4SLinus Torvalds 		sclp_init_state = sclp_init_state_uninitialized;
9091da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
9101da177e4SLinus Torvalds 		return rc;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 	/* Register reboot handler */
9131da177e4SLinus Torvalds 	rc = register_reboot_notifier(&sclp_reboot_notifier);
9141da177e4SLinus Torvalds 	if (rc) {
9151da177e4SLinus Torvalds 		sclp_init_state = sclp_init_state_uninitialized;
9161da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
9171da177e4SLinus Torvalds 		return rc;
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 	/* Register interrupt handler */
9201da177e4SLinus Torvalds 	rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
9211da177e4SLinus Torvalds 					       &ext_int_info_hwc);
9221da177e4SLinus Torvalds 	if (rc) {
9231da177e4SLinus Torvalds 		unregister_reboot_notifier(&sclp_reboot_notifier);
9241da177e4SLinus Torvalds 		sclp_init_state = sclp_init_state_uninitialized;
9251da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
9261da177e4SLinus Torvalds 		return rc;
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds 	sclp_init_state = sclp_init_state_initialized;
9291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
9301da177e4SLinus Torvalds 	/* Enable service-signal external interruption - needs to happen with
9311da177e4SLinus Torvalds 	 * IRQs enabled. */
9321da177e4SLinus Torvalds 	ctl_set_bit(0, 9);
9331da177e4SLinus Torvalds 	sclp_init_mask(1);
9341da177e4SLinus Torvalds 	return 0;
9351da177e4SLinus Torvalds }
936b3d00c3bSPeter Oberparleiter 
937b3d00c3bSPeter Oberparleiter static __init int sclp_initcall(void)
938b3d00c3bSPeter Oberparleiter {
939b3d00c3bSPeter Oberparleiter 	return sclp_init();
940b3d00c3bSPeter Oberparleiter }
941b3d00c3bSPeter Oberparleiter 
942b3d00c3bSPeter Oberparleiter arch_initcall(sclp_initcall);
943