xref: /openbmc/linux/drivers/s390/char/sclp.c (revision b843563518c1e06521c446b9a043b7d352df02e0)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * core function to access sclp interface
41da177e4SLinus Torvalds  *
562b74942SMichael Holzheu  * Copyright IBM Corp. 1999, 2009
662b74942SMichael Holzheu  *
71da177e4SLinus Torvalds  * Author(s): Martin Peschke <mpeschke@de.ibm.com>
81da177e4SLinus Torvalds  *	      Martin Schwidefsky <schwidefsky@de.ibm.com>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
11052ff461SHeiko Carstens #include <linux/kernel_stat.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/err.h>
141da177e4SLinus Torvalds #include <linux/spinlock.h>
151da177e4SLinus Torvalds #include <linux/interrupt.h>
161da177e4SLinus Torvalds #include <linux/timer.h>
171da177e4SLinus Torvalds #include <linux/reboot.h>
181da177e4SLinus Torvalds #include <linux/jiffies.h>
19b3d00c3bSPeter Oberparleiter #include <linux/init.h>
2062b74942SMichael Holzheu #include <linux/suspend.h>
2162b74942SMichael Holzheu #include <linux/completion.h>
2262b74942SMichael Holzheu #include <linux/platform_device.h>
23052ff461SHeiko Carstens #include <asm/types.h>
24052ff461SHeiko Carstens #include <asm/irq.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include "sclp.h"
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #define SCLP_HEADER		"sclp: "
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /* Lock to protect internal data consistency. */
311da177e4SLinus Torvalds static DEFINE_SPINLOCK(sclp_lock);
321da177e4SLinus Torvalds 
33d082d3ceSPeter Oberparleiter /* Mask of events that we can send to the sclp interface. */
341da177e4SLinus Torvalds static sccb_mask_t sclp_receive_mask;
351da177e4SLinus Torvalds 
36d082d3ceSPeter Oberparleiter /* Mask of events that we can receive from the sclp interface. */
371da177e4SLinus Torvalds static sccb_mask_t sclp_send_mask;
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /* List of registered event listeners and senders. */
401da177e4SLinus Torvalds static struct list_head sclp_reg_list;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* List of queued requests. */
431da177e4SLinus Torvalds static struct list_head sclp_req_queue;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* Data for read and and init requests. */
461da177e4SLinus Torvalds static struct sclp_req sclp_read_req;
471da177e4SLinus Torvalds static struct sclp_req sclp_init_req;
481da177e4SLinus Torvalds static char sclp_read_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
491da177e4SLinus Torvalds static char sclp_init_sccb[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
501da177e4SLinus Torvalds 
5162b74942SMichael Holzheu /* Suspend request */
5262b74942SMichael Holzheu static DECLARE_COMPLETION(sclp_request_queue_flushed);
5362b74942SMichael Holzheu 
5425b41a7bSMartin Schwidefsky /* Number of console pages to allocate, used by sclp_con.c and sclp_vt220.c */
5525b41a7bSMartin Schwidefsky int sclp_console_pages = SCLP_CONSOLE_PAGES;
5625b41a7bSMartin Schwidefsky /* Flag to indicate if buffer pages are dropped on buffer full condition */
5700fd2cb0SPeter Oberparleiter int sclp_console_drop = 1;
5825b41a7bSMartin Schwidefsky /* Number of times the console dropped buffer pages */
5925b41a7bSMartin Schwidefsky unsigned long sclp_console_full;
6025b41a7bSMartin Schwidefsky 
6162b74942SMichael Holzheu static void sclp_suspend_req_cb(struct sclp_req *req, void *data)
6262b74942SMichael Holzheu {
6362b74942SMichael Holzheu 	complete(&sclp_request_queue_flushed);
6462b74942SMichael Holzheu }
6562b74942SMichael Holzheu 
6625b41a7bSMartin Schwidefsky static int __init sclp_setup_console_pages(char *str)
6725b41a7bSMartin Schwidefsky {
6825b41a7bSMartin Schwidefsky 	int pages, rc;
6925b41a7bSMartin Schwidefsky 
7025b41a7bSMartin Schwidefsky 	rc = kstrtoint(str, 0, &pages);
7125b41a7bSMartin Schwidefsky 	if (!rc && pages >= SCLP_CONSOLE_PAGES)
7225b41a7bSMartin Schwidefsky 		sclp_console_pages = pages;
7325b41a7bSMartin Schwidefsky 	return 1;
7425b41a7bSMartin Schwidefsky }
7525b41a7bSMartin Schwidefsky 
7625b41a7bSMartin Schwidefsky __setup("sclp_con_pages=", sclp_setup_console_pages);
7725b41a7bSMartin Schwidefsky 
7825b41a7bSMartin Schwidefsky static int __init sclp_setup_console_drop(char *str)
7925b41a7bSMartin Schwidefsky {
8025b41a7bSMartin Schwidefsky 	int drop, rc;
8125b41a7bSMartin Schwidefsky 
8225b41a7bSMartin Schwidefsky 	rc = kstrtoint(str, 0, &drop);
8300fd2cb0SPeter Oberparleiter 	if (!rc)
8400fd2cb0SPeter Oberparleiter 		sclp_console_drop = drop;
8525b41a7bSMartin Schwidefsky 	return 1;
8625b41a7bSMartin Schwidefsky }
8725b41a7bSMartin Schwidefsky 
8825b41a7bSMartin Schwidefsky __setup("sclp_con_drop=", sclp_setup_console_drop);
8925b41a7bSMartin Schwidefsky 
9062b74942SMichael Holzheu static struct sclp_req sclp_suspend_req;
9162b74942SMichael Holzheu 
921da177e4SLinus Torvalds /* Timer for request retries. */
931da177e4SLinus Torvalds static struct timer_list sclp_request_timer;
941da177e4SLinus Torvalds 
959f0128f9SGerald Schaefer /* Timer for queued requests. */
969f0128f9SGerald Schaefer static struct timer_list sclp_queue_timer;
979f0128f9SGerald Schaefer 
981da177e4SLinus Torvalds /* Internal state: is a request active at the sclp? */
991da177e4SLinus Torvalds static volatile enum sclp_running_state_t {
1001da177e4SLinus Torvalds 	sclp_running_state_idle,
101dbd8ae63SPeter Oberparleiter 	sclp_running_state_running,
102dbd8ae63SPeter Oberparleiter 	sclp_running_state_reset_pending
1031da177e4SLinus Torvalds } sclp_running_state = sclp_running_state_idle;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /* Internal state: is a read request pending? */
1061da177e4SLinus Torvalds static volatile enum sclp_reading_state_t {
1071da177e4SLinus Torvalds 	sclp_reading_state_idle,
1081da177e4SLinus Torvalds 	sclp_reading_state_reading
1091da177e4SLinus Torvalds } sclp_reading_state = sclp_reading_state_idle;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* Internal state: is the driver currently serving requests? */
1121da177e4SLinus Torvalds static volatile enum sclp_activation_state_t {
1131da177e4SLinus Torvalds 	sclp_activation_state_active,
1141da177e4SLinus Torvalds 	sclp_activation_state_deactivating,
1151da177e4SLinus Torvalds 	sclp_activation_state_inactive,
1161da177e4SLinus Torvalds 	sclp_activation_state_activating
1171da177e4SLinus Torvalds } sclp_activation_state = sclp_activation_state_active;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /* Internal state: is an init mask request pending? */
1201da177e4SLinus Torvalds static volatile enum sclp_mask_state_t {
1211da177e4SLinus Torvalds 	sclp_mask_state_idle,
1221da177e4SLinus Torvalds 	sclp_mask_state_initializing
1231da177e4SLinus Torvalds } sclp_mask_state = sclp_mask_state_idle;
1241da177e4SLinus Torvalds 
12562b74942SMichael Holzheu /* Internal state: is the driver suspended? */
12662b74942SMichael Holzheu static enum sclp_suspend_state_t {
12762b74942SMichael Holzheu 	sclp_suspend_state_running,
12862b74942SMichael Holzheu 	sclp_suspend_state_suspended,
12962b74942SMichael Holzheu } sclp_suspend_state = sclp_suspend_state_running;
13062b74942SMichael Holzheu 
1311da177e4SLinus Torvalds /* Maximum retry counts */
1321da177e4SLinus Torvalds #define SCLP_INIT_RETRY		3
1331da177e4SLinus Torvalds #define SCLP_MASK_RETRY		3
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /* Timeout intervals in seconds.*/
13625fab9ebSPeter Oberparleiter #define SCLP_BUSY_INTERVAL	10
137dbd8ae63SPeter Oberparleiter #define SCLP_RETRY_INTERVAL	30
1381da177e4SLinus Torvalds 
139c9602ee7SKees Cook static void sclp_request_timeout(bool force_restart);
1401da177e4SLinus Torvalds static void sclp_process_queue(void);
141364c8558SHeiko Carstens static void __sclp_make_read_req(void);
1421da177e4SLinus Torvalds static int sclp_init_mask(int calculate);
1431da177e4SLinus Torvalds static int sclp_init(void);
1441da177e4SLinus Torvalds 
145dbd8ae63SPeter Oberparleiter static void
146dbd8ae63SPeter Oberparleiter __sclp_queue_read_req(void)
147dbd8ae63SPeter Oberparleiter {
148dbd8ae63SPeter Oberparleiter 	if (sclp_reading_state == sclp_reading_state_idle) {
149dbd8ae63SPeter Oberparleiter 		sclp_reading_state = sclp_reading_state_reading;
150dbd8ae63SPeter Oberparleiter 		__sclp_make_read_req();
151dbd8ae63SPeter Oberparleiter 		/* Add request to head of queue */
152dbd8ae63SPeter Oberparleiter 		list_add(&sclp_read_req.list, &sclp_req_queue);
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /* Set up request retry timer. Called while sclp_lock is locked. */
1571da177e4SLinus Torvalds static inline void
158c9602ee7SKees Cook __sclp_set_request_timer(unsigned long time, void (*cb)(struct timer_list *))
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
161841b86f3SKees Cook 	sclp_request_timer.function = cb;
1621da177e4SLinus Torvalds 	sclp_request_timer.expires = jiffies + time;
1631da177e4SLinus Torvalds 	add_timer(&sclp_request_timer);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
166c9602ee7SKees Cook static void sclp_request_timeout_restart(struct timer_list *unused)
167c9602ee7SKees Cook {
168c9602ee7SKees Cook 	sclp_request_timeout(true);
169c9602ee7SKees Cook }
170c9602ee7SKees Cook 
171c9602ee7SKees Cook static void sclp_request_timeout_normal(struct timer_list *unused)
172c9602ee7SKees Cook {
173c9602ee7SKees Cook 	sclp_request_timeout(false);
174c9602ee7SKees Cook }
175c9602ee7SKees Cook 
176c9602ee7SKees Cook /* Request timeout handler. Restart the request queue. If force_restart,
177dbd8ae63SPeter Oberparleiter  * force restart of running request. */
178c9602ee7SKees Cook static void sclp_request_timeout(bool force_restart)
179dbd8ae63SPeter Oberparleiter {
180dbd8ae63SPeter Oberparleiter 	unsigned long flags;
181dbd8ae63SPeter Oberparleiter 
182dbd8ae63SPeter Oberparleiter 	spin_lock_irqsave(&sclp_lock, flags);
183c9602ee7SKees Cook 	if (force_restart) {
184dbd8ae63SPeter Oberparleiter 		if (sclp_running_state == sclp_running_state_running) {
185dbd8ae63SPeter Oberparleiter 			/* Break running state and queue NOP read event request
186dbd8ae63SPeter Oberparleiter 			 * to get a defined interface state. */
187dbd8ae63SPeter Oberparleiter 			__sclp_queue_read_req();
188dbd8ae63SPeter Oberparleiter 			sclp_running_state = sclp_running_state_idle;
189dbd8ae63SPeter Oberparleiter 		}
190dbd8ae63SPeter Oberparleiter 	} else {
191dbd8ae63SPeter Oberparleiter 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
192c9602ee7SKees Cook 					 sclp_request_timeout_normal);
193dbd8ae63SPeter Oberparleiter 	}
194dbd8ae63SPeter Oberparleiter 	spin_unlock_irqrestore(&sclp_lock, flags);
195dbd8ae63SPeter Oberparleiter 	sclp_process_queue();
196dbd8ae63SPeter Oberparleiter }
197dbd8ae63SPeter Oberparleiter 
1989f0128f9SGerald Schaefer /*
1999f0128f9SGerald Schaefer  * Returns the expire value in jiffies of the next pending request timeout,
2009f0128f9SGerald Schaefer  * if any. Needs to be called with sclp_lock.
2019f0128f9SGerald Schaefer  */
2029f0128f9SGerald Schaefer static unsigned long __sclp_req_queue_find_next_timeout(void)
2039f0128f9SGerald Schaefer {
2049f0128f9SGerald Schaefer 	unsigned long expires_next = 0;
2059f0128f9SGerald Schaefer 	struct sclp_req *req;
2069f0128f9SGerald Schaefer 
2079f0128f9SGerald Schaefer 	list_for_each_entry(req, &sclp_req_queue, list) {
2089f0128f9SGerald Schaefer 		if (!req->queue_expires)
2099f0128f9SGerald Schaefer 			continue;
2109f0128f9SGerald Schaefer 		if (!expires_next ||
2119f0128f9SGerald Schaefer 		   (time_before(req->queue_expires, expires_next)))
2129f0128f9SGerald Schaefer 				expires_next = req->queue_expires;
2139f0128f9SGerald Schaefer 	}
2149f0128f9SGerald Schaefer 	return expires_next;
2159f0128f9SGerald Schaefer }
2169f0128f9SGerald Schaefer 
2179f0128f9SGerald Schaefer /*
2189f0128f9SGerald Schaefer  * Returns expired request, if any, and removes it from the list.
2199f0128f9SGerald Schaefer  */
2209f0128f9SGerald Schaefer static struct sclp_req *__sclp_req_queue_remove_expired_req(void)
2219f0128f9SGerald Schaefer {
2229f0128f9SGerald Schaefer 	unsigned long flags, now;
2239f0128f9SGerald Schaefer 	struct sclp_req *req;
2249f0128f9SGerald Schaefer 
2259f0128f9SGerald Schaefer 	spin_lock_irqsave(&sclp_lock, flags);
2269f0128f9SGerald Schaefer 	now = jiffies;
2279f0128f9SGerald Schaefer 	/* Don't need list_for_each_safe because we break out after list_del */
2289f0128f9SGerald Schaefer 	list_for_each_entry(req, &sclp_req_queue, list) {
2299f0128f9SGerald Schaefer 		if (!req->queue_expires)
2309f0128f9SGerald Schaefer 			continue;
2319f0128f9SGerald Schaefer 		if (time_before_eq(req->queue_expires, now)) {
2329f0128f9SGerald Schaefer 			if (req->status == SCLP_REQ_QUEUED) {
2339f0128f9SGerald Schaefer 				req->status = SCLP_REQ_QUEUED_TIMEOUT;
2349f0128f9SGerald Schaefer 				list_del(&req->list);
2359f0128f9SGerald Schaefer 				goto out;
2369f0128f9SGerald Schaefer 			}
2379f0128f9SGerald Schaefer 		}
2389f0128f9SGerald Schaefer 	}
2399f0128f9SGerald Schaefer 	req = NULL;
2409f0128f9SGerald Schaefer out:
2419f0128f9SGerald Schaefer 	spin_unlock_irqrestore(&sclp_lock, flags);
2429f0128f9SGerald Schaefer 	return req;
2439f0128f9SGerald Schaefer }
2449f0128f9SGerald Schaefer 
2459f0128f9SGerald Schaefer /*
2469f0128f9SGerald Schaefer  * Timeout handler for queued requests. Removes request from list and
2479f0128f9SGerald Schaefer  * invokes callback. This timer can be set per request in situations where
2489f0128f9SGerald Schaefer  * waiting too long would be harmful to the system, e.g. during SE reboot.
2499f0128f9SGerald Schaefer  */
250c9602ee7SKees Cook static void sclp_req_queue_timeout(struct timer_list *unused)
2519f0128f9SGerald Schaefer {
2529f0128f9SGerald Schaefer 	unsigned long flags, expires_next;
2539f0128f9SGerald Schaefer 	struct sclp_req *req;
2549f0128f9SGerald Schaefer 
2559f0128f9SGerald Schaefer 	do {
2569f0128f9SGerald Schaefer 		req = __sclp_req_queue_remove_expired_req();
2579f0128f9SGerald Schaefer 		if (req && req->callback)
2589f0128f9SGerald Schaefer 			req->callback(req, req->callback_data);
2599f0128f9SGerald Schaefer 	} while (req);
2609f0128f9SGerald Schaefer 
2619f0128f9SGerald Schaefer 	spin_lock_irqsave(&sclp_lock, flags);
2629f0128f9SGerald Schaefer 	expires_next = __sclp_req_queue_find_next_timeout();
2639f0128f9SGerald Schaefer 	if (expires_next)
2649f0128f9SGerald Schaefer 		mod_timer(&sclp_queue_timer, expires_next);
2659f0128f9SGerald Schaefer 	spin_unlock_irqrestore(&sclp_lock, flags);
2669f0128f9SGerald Schaefer }
2679f0128f9SGerald Schaefer 
2681da177e4SLinus Torvalds /* Try to start a request. Return zero if the request was successfully
2691da177e4SLinus Torvalds  * started or if it will be started at a later time. Return non-zero otherwise.
2701da177e4SLinus Torvalds  * Called while sclp_lock is locked. */
2711da177e4SLinus Torvalds static int
2721da177e4SLinus Torvalds __sclp_start_request(struct sclp_req *req)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	int rc;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	if (sclp_running_state != sclp_running_state_idle)
2771da177e4SLinus Torvalds 		return 0;
2781da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
279ab14de6cSHeiko Carstens 	rc = sclp_service_call(req->command, req->sccb);
2801da177e4SLinus Torvalds 	req->start_count++;
28125fab9ebSPeter Oberparleiter 
2821da177e4SLinus Torvalds 	if (rc == 0) {
2833ad2f3fbSDaniel Mack 		/* Successfully started request */
2841da177e4SLinus Torvalds 		req->status = SCLP_REQ_RUNNING;
2851da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_running;
2861da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
287c9602ee7SKees Cook 					 sclp_request_timeout_restart);
2881da177e4SLinus Torvalds 		return 0;
2891da177e4SLinus Torvalds 	} else if (rc == -EBUSY) {
2901da177e4SLinus Torvalds 		/* Try again later */
2911da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
292c9602ee7SKees Cook 					 sclp_request_timeout_normal);
2931da177e4SLinus Torvalds 		return 0;
2941da177e4SLinus Torvalds 	}
2951da177e4SLinus Torvalds 	/* Request failed */
2961da177e4SLinus Torvalds 	req->status = SCLP_REQ_FAILED;
2971da177e4SLinus Torvalds 	return rc;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds /* Try to start queued requests. */
3011da177e4SLinus Torvalds static void
3021da177e4SLinus Torvalds sclp_process_queue(void)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct sclp_req *req;
3051da177e4SLinus Torvalds 	int rc;
3061da177e4SLinus Torvalds 	unsigned long flags;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
3091da177e4SLinus Torvalds 	if (sclp_running_state != sclp_running_state_idle) {
3101da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
3111da177e4SLinus Torvalds 		return;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	del_timer(&sclp_request_timer);
3141da177e4SLinus Torvalds 	while (!list_empty(&sclp_req_queue)) {
3151da177e4SLinus Torvalds 		req = list_entry(sclp_req_queue.next, struct sclp_req, list);
31662b74942SMichael Holzheu 		if (!req->sccb)
31762b74942SMichael Holzheu 			goto do_post;
3181da177e4SLinus Torvalds 		rc = __sclp_start_request(req);
3191da177e4SLinus Torvalds 		if (rc == 0)
3201da177e4SLinus Torvalds 			break;
321dbd8ae63SPeter Oberparleiter 		/* Request failed */
322dbd8ae63SPeter Oberparleiter 		if (req->start_count > 1) {
323dbd8ae63SPeter Oberparleiter 			/* Cannot abort already submitted request - could still
324dbd8ae63SPeter Oberparleiter 			 * be active at the SCLP */
325dbd8ae63SPeter Oberparleiter 			__sclp_set_request_timer(SCLP_BUSY_INTERVAL * HZ,
326c9602ee7SKees Cook 						 sclp_request_timeout_normal);
327dbd8ae63SPeter Oberparleiter 			break;
328dbd8ae63SPeter Oberparleiter 		}
32962b74942SMichael Holzheu do_post:
330dbd8ae63SPeter Oberparleiter 		/* Post-processing for aborted request */
3311da177e4SLinus Torvalds 		list_del(&req->list);
3321da177e4SLinus Torvalds 		if (req->callback) {
3331da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
3341da177e4SLinus Torvalds 			req->callback(req, req->callback_data);
3351da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
3361da177e4SLinus Torvalds 		}
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
34162b74942SMichael Holzheu static int __sclp_can_add_request(struct sclp_req *req)
34262b74942SMichael Holzheu {
34362b74942SMichael Holzheu 	if (req == &sclp_suspend_req || req == &sclp_init_req)
34462b74942SMichael Holzheu 		return 1;
34562b74942SMichael Holzheu 	if (sclp_suspend_state != sclp_suspend_state_running)
34662b74942SMichael Holzheu 		return 0;
34762b74942SMichael Holzheu 	if (sclp_init_state != sclp_init_state_initialized)
34862b74942SMichael Holzheu 		return 0;
34962b74942SMichael Holzheu 	if (sclp_activation_state != sclp_activation_state_active)
35062b74942SMichael Holzheu 		return 0;
35162b74942SMichael Holzheu 	return 1;
35262b74942SMichael Holzheu }
35362b74942SMichael Holzheu 
3541da177e4SLinus Torvalds /* Queue a new request. Return zero on success, non-zero otherwise. */
3551da177e4SLinus Torvalds int
3561da177e4SLinus Torvalds sclp_add_request(struct sclp_req *req)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	unsigned long flags;
3591da177e4SLinus Torvalds 	int rc;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
36262b74942SMichael Holzheu 	if (!__sclp_can_add_request(req)) {
3631da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
3641da177e4SLinus Torvalds 		return -EIO;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 	req->status = SCLP_REQ_QUEUED;
3671da177e4SLinus Torvalds 	req->start_count = 0;
3681da177e4SLinus Torvalds 	list_add_tail(&req->list, &sclp_req_queue);
3691da177e4SLinus Torvalds 	rc = 0;
3709f0128f9SGerald Schaefer 	if (req->queue_timeout) {
3719f0128f9SGerald Schaefer 		req->queue_expires = jiffies + req->queue_timeout * HZ;
3729f0128f9SGerald Schaefer 		if (!timer_pending(&sclp_queue_timer) ||
3739f0128f9SGerald Schaefer 		    time_after(sclp_queue_timer.expires, req->queue_expires))
3749f0128f9SGerald Schaefer 			mod_timer(&sclp_queue_timer, req->queue_expires);
3759f0128f9SGerald Schaefer 	} else
3769f0128f9SGerald Schaefer 		req->queue_expires = 0;
3771da177e4SLinus Torvalds 	/* Start if request is first in list */
378dbd8ae63SPeter Oberparleiter 	if (sclp_running_state == sclp_running_state_idle &&
379dbd8ae63SPeter Oberparleiter 	    req->list.prev == &sclp_req_queue) {
38062b74942SMichael Holzheu 		if (!req->sccb) {
38162b74942SMichael Holzheu 			list_del(&req->list);
38262b74942SMichael Holzheu 			rc = -ENODATA;
38362b74942SMichael Holzheu 			goto out;
38462b74942SMichael Holzheu 		}
3851da177e4SLinus Torvalds 		rc = __sclp_start_request(req);
3861da177e4SLinus Torvalds 		if (rc)
3871da177e4SLinus Torvalds 			list_del(&req->list);
3881da177e4SLinus Torvalds 	}
38962b74942SMichael Holzheu out:
3901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
3911da177e4SLinus Torvalds 	return rc;
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_add_request);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /* Dispatch events found in request buffer to registered listeners. Return 0
3971da177e4SLinus Torvalds  * if all events were dispatched, non-zero otherwise. */
3981da177e4SLinus Torvalds static int
3991da177e4SLinus Torvalds sclp_dispatch_evbufs(struct sccb_header *sccb)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	unsigned long flags;
4021da177e4SLinus Torvalds 	struct evbuf_header *evbuf;
4031da177e4SLinus Torvalds 	struct list_head *l;
4041da177e4SLinus Torvalds 	struct sclp_register *reg;
4051da177e4SLinus Torvalds 	int offset;
4061da177e4SLinus Torvalds 	int rc;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
4091da177e4SLinus Torvalds 	rc = 0;
4101da177e4SLinus Torvalds 	for (offset = sizeof(struct sccb_header); offset < sccb->length;
4111da177e4SLinus Torvalds 	     offset += evbuf->length) {
4121da177e4SLinus Torvalds 		evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
413e2e5a0f2SPeter Oberparleiter 		/* Check for malformed hardware response */
414e2e5a0f2SPeter Oberparleiter 		if (evbuf->length == 0)
415e2e5a0f2SPeter Oberparleiter 			break;
416e2e5a0f2SPeter Oberparleiter 		/* Search for event handler */
4171da177e4SLinus Torvalds 		reg = NULL;
4181da177e4SLinus Torvalds 		list_for_each(l, &sclp_reg_list) {
4191da177e4SLinus Torvalds 			reg = list_entry(l, struct sclp_register, list);
4200ee5f8dcSClaudio Imbrenda 			if (reg->receive_mask & SCLP_EVTYP_MASK(evbuf->type))
4211da177e4SLinus Torvalds 				break;
4221da177e4SLinus Torvalds 			else
4231da177e4SLinus Torvalds 				reg = NULL;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 		if (reg && reg->receiver_fn) {
4261da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
4271da177e4SLinus Torvalds 			reg->receiver_fn(evbuf);
4281da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
4291da177e4SLinus Torvalds 		} else if (reg == NULL)
430d06cbda6SHeiko Carstens 			rc = -EOPNOTSUPP;
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
4331da177e4SLinus Torvalds 	return rc;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds /* Read event data request callback. */
4371da177e4SLinus Torvalds static void
4381da177e4SLinus Torvalds sclp_read_cb(struct sclp_req *req, void *data)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	unsigned long flags;
4411da177e4SLinus Torvalds 	struct sccb_header *sccb;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	sccb = (struct sccb_header *) req->sccb;
4441da177e4SLinus Torvalds 	if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
4451da177e4SLinus Torvalds 	    sccb->response_code == 0x220))
4461da177e4SLinus Torvalds 		sclp_dispatch_evbufs(sccb);
4471da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
4481da177e4SLinus Torvalds 	sclp_reading_state = sclp_reading_state_idle;
4491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /* Prepare read event data request. Called while sclp_lock is locked. */
453364c8558SHeiko Carstens static void __sclp_make_read_req(void)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	struct sccb_header *sccb;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	sccb = (struct sccb_header *) sclp_read_sccb;
4581da177e4SLinus Torvalds 	clear_page(sccb);
4591da177e4SLinus Torvalds 	memset(&sclp_read_req, 0, sizeof(struct sclp_req));
460ab14de6cSHeiko Carstens 	sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
4611da177e4SLinus Torvalds 	sclp_read_req.status = SCLP_REQ_QUEUED;
4621da177e4SLinus Torvalds 	sclp_read_req.start_count = 0;
4631da177e4SLinus Torvalds 	sclp_read_req.callback = sclp_read_cb;
4641da177e4SLinus Torvalds 	sclp_read_req.sccb = sccb;
4651da177e4SLinus Torvalds 	sccb->length = PAGE_SIZE;
4661da177e4SLinus Torvalds 	sccb->function_code = 0;
4671da177e4SLinus Torvalds 	sccb->control_mask[2] = 0x80;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /* Search request list for request with matching sccb. Return request if found,
4711da177e4SLinus Torvalds  * NULL otherwise. Called while sclp_lock is locked. */
4721da177e4SLinus Torvalds static inline struct sclp_req *
4731da177e4SLinus Torvalds __sclp_find_req(u32 sccb)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	struct list_head *l;
4761da177e4SLinus Torvalds 	struct sclp_req *req;
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	list_for_each(l, &sclp_req_queue) {
4791da177e4SLinus Torvalds 		req = list_entry(l, struct sclp_req, list);
4801da177e4SLinus Torvalds 		if (sccb == (u32) (addr_t) req->sccb)
4811da177e4SLinus Torvalds 				return req;
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	return NULL;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /* Handler for external interruption. Perform request post-processing.
4871da177e4SLinus Torvalds  * Prepare read event data request if necessary. Start processing of next
4881da177e4SLinus Torvalds  * request on queue. */
489fde15c3aSHeiko Carstens static void sclp_interrupt_handler(struct ext_code ext_code,
490f6649a7eSMartin Schwidefsky 				   unsigned int param32, unsigned long param64)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	struct sclp_req *req;
4931da177e4SLinus Torvalds 	u32 finished_sccb;
4941da177e4SLinus Torvalds 	u32 evbuf_pending;
4951da177e4SLinus Torvalds 
496420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_SCP);
4971da177e4SLinus Torvalds 	spin_lock(&sclp_lock);
498f6649a7eSMartin Schwidefsky 	finished_sccb = param32 & 0xfffffff8;
499f6649a7eSMartin Schwidefsky 	evbuf_pending = param32 & 0x3;
5001da177e4SLinus Torvalds 	if (finished_sccb) {
501dbd8ae63SPeter Oberparleiter 		del_timer(&sclp_request_timer);
502dbd8ae63SPeter Oberparleiter 		sclp_running_state = sclp_running_state_reset_pending;
5031da177e4SLinus Torvalds 		req = __sclp_find_req(finished_sccb);
5041da177e4SLinus Torvalds 		if (req) {
5051da177e4SLinus Torvalds 			/* Request post-processing */
5061da177e4SLinus Torvalds 			list_del(&req->list);
5071da177e4SLinus Torvalds 			req->status = SCLP_REQ_DONE;
5081da177e4SLinus Torvalds 			if (req->callback) {
5091da177e4SLinus Torvalds 				spin_unlock(&sclp_lock);
5101da177e4SLinus Torvalds 				req->callback(req, req->callback_data);
5111da177e4SLinus Torvalds 				spin_lock(&sclp_lock);
5121da177e4SLinus Torvalds 			}
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
5151da177e4SLinus Torvalds 	}
516d082d3ceSPeter Oberparleiter 	if (evbuf_pending &&
517dbd8ae63SPeter Oberparleiter 	    sclp_activation_state == sclp_activation_state_active)
518dbd8ae63SPeter Oberparleiter 		__sclp_queue_read_req();
5191da177e4SLinus Torvalds 	spin_unlock(&sclp_lock);
5201da177e4SLinus Torvalds 	sclp_process_queue();
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /* Convert interval in jiffies to TOD ticks. */
5241da177e4SLinus Torvalds static inline u64
5251da177e4SLinus Torvalds sclp_tod_from_jiffies(unsigned long jiffies)
5261da177e4SLinus Torvalds {
5271da177e4SLinus Torvalds 	return (u64) (jiffies / HZ) << 32;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /* Wait until a currently running request finished. Note: while this function
5311da177e4SLinus Torvalds  * is running, no timers are served on the calling CPU. */
5321da177e4SLinus Torvalds void
5331da177e4SLinus Torvalds sclp_sync_wait(void)
5341da177e4SLinus Torvalds {
535934b2857SHeiko Carstens 	unsigned long long old_tick;
5361f194a4cSHeiko Carstens 	unsigned long flags;
5371da177e4SLinus Torvalds 	unsigned long cr0, cr0_sync;
5381da177e4SLinus Torvalds 	u64 timeout;
539c59d744bSHeiko Carstens 	int irq_context;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	/* We'll be disabling timer interrupts, so we need a custom timeout
5421da177e4SLinus Torvalds 	 * mechanism */
5431da177e4SLinus Torvalds 	timeout = 0;
5441da177e4SLinus Torvalds 	if (timer_pending(&sclp_request_timer)) {
5451da177e4SLinus Torvalds 		/* Get timeout TOD value */
5468c071b0fSMartin Schwidefsky 		timeout = get_tod_clock_fast() +
5471da177e4SLinus Torvalds 			  sclp_tod_from_jiffies(sclp_request_timer.expires -
5481da177e4SLinus Torvalds 						jiffies);
5491da177e4SLinus Torvalds 	}
5501f194a4cSHeiko Carstens 	local_irq_save(flags);
5511da177e4SLinus Torvalds 	/* Prevent bottom half from executing once we force interrupts open */
552c59d744bSHeiko Carstens 	irq_context = in_interrupt();
553c59d744bSHeiko Carstens 	if (!irq_context)
5541da177e4SLinus Torvalds 		local_bh_disable();
5551da177e4SLinus Torvalds 	/* Enable service-signal interruption, disable timer interrupts */
556934b2857SHeiko Carstens 	old_tick = local_tick_disable();
5571f194a4cSHeiko Carstens 	trace_hardirqs_on();
5581da177e4SLinus Torvalds 	__ctl_store(cr0, 0, 0);
559c2ab7282SHeiko Carstens 	cr0_sync = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
560c2ab7282SHeiko Carstens 	cr0_sync |= 1UL << (63 - 54);
5611da177e4SLinus Torvalds 	__ctl_load(cr0_sync, 0, 0);
562df9ee292SDavid Howells 	__arch_local_irq_stosm(0x01);
5631da177e4SLinus Torvalds 	/* Loop until driver state indicates finished request */
5641da177e4SLinus Torvalds 	while (sclp_running_state != sclp_running_state_idle) {
5651da177e4SLinus Torvalds 		/* Check for expired request timer */
5661da177e4SLinus Torvalds 		if (timer_pending(&sclp_request_timer) &&
5678c071b0fSMartin Schwidefsky 		    get_tod_clock_fast() > timeout &&
5681da177e4SLinus Torvalds 		    del_timer(&sclp_request_timer))
569841b86f3SKees Cook 			sclp_request_timer.function(&sclp_request_timer);
5701da177e4SLinus Torvalds 		cpu_relax();
5711da177e4SLinus Torvalds 	}
5721f194a4cSHeiko Carstens 	local_irq_disable();
5731da177e4SLinus Torvalds 	__ctl_load(cr0, 0, 0);
574c59d744bSHeiko Carstens 	if (!irq_context)
5751f194a4cSHeiko Carstens 		_local_bh_enable();
576934b2857SHeiko Carstens 	local_tick_enable(old_tick);
5771f194a4cSHeiko Carstens 	local_irq_restore(flags);
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_sync_wait);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /* Dispatch changes in send and receive mask to registered listeners. */
5824d284cacSHeiko Carstens static void
5831da177e4SLinus Torvalds sclp_dispatch_state_change(void)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	struct list_head *l;
5861da177e4SLinus Torvalds 	struct sclp_register *reg;
5871da177e4SLinus Torvalds 	unsigned long flags;
5881da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
5891da177e4SLinus Torvalds 	sccb_mask_t send_mask;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	do {
5921da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
5931da177e4SLinus Torvalds 		reg = NULL;
5941da177e4SLinus Torvalds 		list_for_each(l, &sclp_reg_list) {
5951da177e4SLinus Torvalds 			reg = list_entry(l, struct sclp_register, list);
596d082d3ceSPeter Oberparleiter 			receive_mask = reg->send_mask & sclp_receive_mask;
597d082d3ceSPeter Oberparleiter 			send_mask = reg->receive_mask & sclp_send_mask;
5981da177e4SLinus Torvalds 			if (reg->sclp_receive_mask != receive_mask ||
5991da177e4SLinus Torvalds 			    reg->sclp_send_mask != send_mask) {
6001da177e4SLinus Torvalds 				reg->sclp_receive_mask = receive_mask;
6011da177e4SLinus Torvalds 				reg->sclp_send_mask = send_mask;
6021da177e4SLinus Torvalds 				break;
6031da177e4SLinus Torvalds 			} else
6041da177e4SLinus Torvalds 				reg = NULL;
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
6071da177e4SLinus Torvalds 		if (reg && reg->state_change_fn)
6081da177e4SLinus Torvalds 			reg->state_change_fn(reg);
6091da177e4SLinus Torvalds 	} while (reg);
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds struct sclp_statechangebuf {
6131da177e4SLinus Torvalds 	struct evbuf_header	header;
6141da177e4SLinus Torvalds 	u8		validity_sclp_active_facility_mask : 1;
6151da177e4SLinus Torvalds 	u8		validity_sclp_receive_mask : 1;
6161da177e4SLinus Torvalds 	u8		validity_sclp_send_mask : 1;
6171da177e4SLinus Torvalds 	u8		validity_read_data_function_mask : 1;
6181da177e4SLinus Torvalds 	u16		_zeros : 12;
6191da177e4SLinus Torvalds 	u16		mask_length;
6201da177e4SLinus Torvalds 	u64		sclp_active_facility_mask;
621*b8435635SClaudio Imbrenda 	u8		masks[2 * 1021 + 4];	/* variable length */
622*b8435635SClaudio Imbrenda 	/*
623*b8435635SClaudio Imbrenda 	 * u8		sclp_receive_mask[mask_length];
624*b8435635SClaudio Imbrenda 	 * u8		sclp_send_mask[mask_length];
625*b8435635SClaudio Imbrenda 	 * u32		read_data_function_mask;
626*b8435635SClaudio Imbrenda 	 */
6271da177e4SLinus Torvalds } __attribute__((packed));
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /* State change event callback. Inform listeners of changes. */
6311da177e4SLinus Torvalds static void
6321da177e4SLinus Torvalds sclp_state_change_cb(struct evbuf_header *evbuf)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	unsigned long flags;
6351da177e4SLinus Torvalds 	struct sclp_statechangebuf *scbuf;
6361da177e4SLinus Torvalds 
637*b8435635SClaudio Imbrenda 	BUILD_BUG_ON(sizeof(struct sclp_statechangebuf) > PAGE_SIZE);
638*b8435635SClaudio Imbrenda 
6391da177e4SLinus Torvalds 	scbuf = (struct sclp_statechangebuf *) evbuf;
6401da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
6411da177e4SLinus Torvalds 	if (scbuf->validity_sclp_receive_mask)
642*b8435635SClaudio Imbrenda 		sclp_receive_mask = sccb_get_recv_mask(scbuf);
6431da177e4SLinus Torvalds 	if (scbuf->validity_sclp_send_mask)
644*b8435635SClaudio Imbrenda 		sclp_send_mask = sccb_get_send_mask(scbuf);
6451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
646887d935aSHeiko Carstens 	if (scbuf->validity_sclp_active_facility_mask)
64778335a30SDavid Hildenbrand 		sclp.facilities = scbuf->sclp_active_facility_mask;
6481da177e4SLinus Torvalds 	sclp_dispatch_state_change();
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds static struct sclp_register sclp_state_change_event = {
6526d4740c8SStefan Haberland 	.receive_mask = EVTYP_STATECHANGE_MASK,
6531da177e4SLinus Torvalds 	.receiver_fn = sclp_state_change_cb
6541da177e4SLinus Torvalds };
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds /* Calculate receive and send mask of currently registered listeners.
6571da177e4SLinus Torvalds  * Called while sclp_lock is locked. */
6581da177e4SLinus Torvalds static inline void
6591da177e4SLinus Torvalds __sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct list_head *l;
6621da177e4SLinus Torvalds 	struct sclp_register *t;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	*receive_mask = 0;
6651da177e4SLinus Torvalds 	*send_mask = 0;
6661da177e4SLinus Torvalds 	list_for_each(l, &sclp_reg_list) {
6671da177e4SLinus Torvalds 		t = list_entry(l, struct sclp_register, list);
6681da177e4SLinus Torvalds 		*receive_mask |= t->receive_mask;
6691da177e4SLinus Torvalds 		*send_mask |= t->send_mask;
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /* Register event listener. Return 0 on success, non-zero otherwise. */
6741da177e4SLinus Torvalds int
6751da177e4SLinus Torvalds sclp_register(struct sclp_register *reg)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	unsigned long flags;
6781da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
6791da177e4SLinus Torvalds 	sccb_mask_t send_mask;
6801da177e4SLinus Torvalds 	int rc;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 	rc = sclp_init();
6831da177e4SLinus Torvalds 	if (rc)
6841da177e4SLinus Torvalds 		return rc;
6851da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
6861da177e4SLinus Torvalds 	/* Check event mask for collisions */
6871da177e4SLinus Torvalds 	__sclp_get_mask(&receive_mask, &send_mask);
6881da177e4SLinus Torvalds 	if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
6891da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
6901da177e4SLinus Torvalds 		return -EBUSY;
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 	/* Trigger initial state change callback */
6931da177e4SLinus Torvalds 	reg->sclp_receive_mask = 0;
6941da177e4SLinus Torvalds 	reg->sclp_send_mask = 0;
69562b74942SMichael Holzheu 	reg->pm_event_posted = 0;
6961da177e4SLinus Torvalds 	list_add(&reg->list, &sclp_reg_list);
6971da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
6981da177e4SLinus Torvalds 	rc = sclp_init_mask(1);
6991da177e4SLinus Torvalds 	if (rc) {
7001da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
7011da177e4SLinus Torvalds 		list_del(&reg->list);
7021da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 	return rc;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_register);
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /* Unregister event listener. */
7101da177e4SLinus Torvalds void
7111da177e4SLinus Torvalds sclp_unregister(struct sclp_register *reg)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	unsigned long flags;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7161da177e4SLinus Torvalds 	list_del(&reg->list);
7171da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
7181da177e4SLinus Torvalds 	sclp_init_mask(1);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_unregister);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /* Remove event buffers which are marked processed. Return the number of
7241da177e4SLinus Torvalds  * remaining event buffers. */
7251da177e4SLinus Torvalds int
7261da177e4SLinus Torvalds sclp_remove_processed(struct sccb_header *sccb)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	struct evbuf_header *evbuf;
7291da177e4SLinus Torvalds 	int unprocessed;
7301da177e4SLinus Torvalds 	u16 remaining;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	evbuf = (struct evbuf_header *) (sccb + 1);
7331da177e4SLinus Torvalds 	unprocessed = 0;
7341da177e4SLinus Torvalds 	remaining = sccb->length - sizeof(struct sccb_header);
7351da177e4SLinus Torvalds 	while (remaining > 0) {
7361da177e4SLinus Torvalds 		remaining -= evbuf->length;
7371da177e4SLinus Torvalds 		if (evbuf->flags & 0x80) {
7381da177e4SLinus Torvalds 			sccb->length -= evbuf->length;
7391da177e4SLinus Torvalds 			memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
7401da177e4SLinus Torvalds 			       remaining);
7411da177e4SLinus Torvalds 		} else {
7421da177e4SLinus Torvalds 			unprocessed++;
7431da177e4SLinus Torvalds 			evbuf = (struct evbuf_header *)
7441da177e4SLinus Torvalds 					((addr_t) evbuf + evbuf->length);
7451da177e4SLinus Torvalds 		}
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 	return unprocessed;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_remove_processed);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* Prepare init mask request. Called while sclp_lock is locked. */
7531da177e4SLinus Torvalds static inline void
7540ee5f8dcSClaudio Imbrenda __sclp_make_init_req(sccb_mask_t receive_mask, sccb_mask_t send_mask)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	struct init_sccb *sccb;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	sccb = (struct init_sccb *) sclp_init_sccb;
7591da177e4SLinus Torvalds 	clear_page(sccb);
7601da177e4SLinus Torvalds 	memset(&sclp_init_req, 0, sizeof(struct sclp_req));
761ab14de6cSHeiko Carstens 	sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
7621da177e4SLinus Torvalds 	sclp_init_req.status = SCLP_REQ_FILLED;
7631da177e4SLinus Torvalds 	sclp_init_req.start_count = 0;
7641da177e4SLinus Torvalds 	sclp_init_req.callback = NULL;
7651da177e4SLinus Torvalds 	sclp_init_req.callback_data = NULL;
7661da177e4SLinus Torvalds 	sclp_init_req.sccb = sccb;
7670ee5f8dcSClaudio Imbrenda 	sccb->header.length = sizeof(*sccb);
7681da177e4SLinus Torvalds 	sccb->mask_length = sizeof(sccb_mask_t);
769*b8435635SClaudio Imbrenda 	sccb_set_recv_mask(sccb, receive_mask);
770*b8435635SClaudio Imbrenda 	sccb_set_send_mask(sccb, send_mask);
771*b8435635SClaudio Imbrenda 	sccb_set_sclp_recv_mask(sccb, 0);
772*b8435635SClaudio Imbrenda 	sccb_set_sclp_send_mask(sccb, 0);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /* Start init mask request. If calculate is non-zero, calculate the mask as
7761da177e4SLinus Torvalds  * requested by registered listeners. Use zero mask otherwise. Return 0 on
7771da177e4SLinus Torvalds  * success, non-zero otherwise. */
7781da177e4SLinus Torvalds static int
7791da177e4SLinus Torvalds sclp_init_mask(int calculate)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	unsigned long flags;
7821da177e4SLinus Torvalds 	struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
7831da177e4SLinus Torvalds 	sccb_mask_t receive_mask;
7841da177e4SLinus Torvalds 	sccb_mask_t send_mask;
7851da177e4SLinus Torvalds 	int retry;
7861da177e4SLinus Torvalds 	int rc;
7871da177e4SLinus Torvalds 	unsigned long wait;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
7901da177e4SLinus Torvalds 	/* Check if interface is in appropriate state */
7911da177e4SLinus Torvalds 	if (sclp_mask_state != sclp_mask_state_idle) {
7921da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
7931da177e4SLinus Torvalds 		return -EBUSY;
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 	if (sclp_activation_state == sclp_activation_state_inactive) {
7961da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
7971da177e4SLinus Torvalds 		return -EINVAL;
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 	sclp_mask_state = sclp_mask_state_initializing;
8001da177e4SLinus Torvalds 	/* Determine mask */
8011da177e4SLinus Torvalds 	if (calculate)
8021da177e4SLinus Torvalds 		__sclp_get_mask(&receive_mask, &send_mask);
8031da177e4SLinus Torvalds 	else {
8041da177e4SLinus Torvalds 		receive_mask = 0;
8051da177e4SLinus Torvalds 		send_mask = 0;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	rc = -EIO;
8081da177e4SLinus Torvalds 	for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
8091da177e4SLinus Torvalds 		/* Prepare request */
8101da177e4SLinus Torvalds 		__sclp_make_init_req(receive_mask, send_mask);
8111da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8121da177e4SLinus Torvalds 		if (sclp_add_request(&sclp_init_req)) {
8131da177e4SLinus Torvalds 			/* Try again later */
8141da177e4SLinus Torvalds 			wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
8151da177e4SLinus Torvalds 			while (time_before(jiffies, wait))
8161da177e4SLinus Torvalds 				sclp_sync_wait();
8171da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
8181da177e4SLinus Torvalds 			continue;
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		while (sclp_init_req.status != SCLP_REQ_DONE &&
8211da177e4SLinus Torvalds 		       sclp_init_req.status != SCLP_REQ_FAILED)
8221da177e4SLinus Torvalds 			sclp_sync_wait();
8231da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
8241da177e4SLinus Torvalds 		if (sclp_init_req.status == SCLP_REQ_DONE &&
8251da177e4SLinus Torvalds 		    sccb->header.response_code == 0x20) {
8261da177e4SLinus Torvalds 			/* Successful request */
8271da177e4SLinus Torvalds 			if (calculate) {
828*b8435635SClaudio Imbrenda 				sclp_receive_mask = sccb_get_sclp_recv_mask(sccb);
829*b8435635SClaudio Imbrenda 				sclp_send_mask = sccb_get_sclp_send_mask(sccb);
8301da177e4SLinus Torvalds 			} else {
8311da177e4SLinus Torvalds 				sclp_receive_mask = 0;
8321da177e4SLinus Torvalds 				sclp_send_mask = 0;
8331da177e4SLinus Torvalds 			}
8341da177e4SLinus Torvalds 			spin_unlock_irqrestore(&sclp_lock, flags);
8351da177e4SLinus Torvalds 			sclp_dispatch_state_change();
8361da177e4SLinus Torvalds 			spin_lock_irqsave(&sclp_lock, flags);
8371da177e4SLinus Torvalds 			rc = 0;
8381da177e4SLinus Torvalds 			break;
8391da177e4SLinus Torvalds 		}
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	sclp_mask_state = sclp_mask_state_idle;
8421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8431da177e4SLinus Torvalds 	return rc;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds /* Deactivate SCLP interface. On success, new requests will be rejected,
8471da177e4SLinus Torvalds  * events will no longer be dispatched. Return 0 on success, non-zero
8481da177e4SLinus Torvalds  * otherwise. */
8491da177e4SLinus Torvalds int
8501da177e4SLinus Torvalds sclp_deactivate(void)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	unsigned long flags;
8531da177e4SLinus Torvalds 	int rc;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8561da177e4SLinus Torvalds 	/* Deactivate can only be called when active */
8571da177e4SLinus Torvalds 	if (sclp_activation_state != sclp_activation_state_active) {
8581da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8591da177e4SLinus Torvalds 		return -EINVAL;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 	sclp_activation_state = sclp_activation_state_deactivating;
8621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8631da177e4SLinus Torvalds 	rc = sclp_init_mask(0);
8641da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8651da177e4SLinus Torvalds 	if (rc == 0)
8661da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_inactive;
8671da177e4SLinus Torvalds 	else
8681da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_active;
8691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8701da177e4SLinus Torvalds 	return rc;
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_deactivate);
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds /* Reactivate SCLP interface after sclp_deactivate. On success, new
8761da177e4SLinus Torvalds  * requests will be accepted, events will be dispatched again. Return 0 on
8771da177e4SLinus Torvalds  * success, non-zero otherwise. */
8781da177e4SLinus Torvalds int
8791da177e4SLinus Torvalds sclp_reactivate(void)
8801da177e4SLinus Torvalds {
8811da177e4SLinus Torvalds 	unsigned long flags;
8821da177e4SLinus Torvalds 	int rc;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8851da177e4SLinus Torvalds 	/* Reactivate can only be called when inactive */
8861da177e4SLinus Torvalds 	if (sclp_activation_state != sclp_activation_state_inactive) {
8871da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
8881da177e4SLinus Torvalds 		return -EINVAL;
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 	sclp_activation_state = sclp_activation_state_activating;
8911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8921da177e4SLinus Torvalds 	rc = sclp_init_mask(1);
8931da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
8941da177e4SLinus Torvalds 	if (rc == 0)
8951da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_active;
8961da177e4SLinus Torvalds 	else
8971da177e4SLinus Torvalds 		sclp_activation_state = sclp_activation_state_inactive;
8981da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
8991da177e4SLinus Torvalds 	return rc;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds EXPORT_SYMBOL(sclp_reactivate);
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds /* Handler for external interruption used during initialization. Modify
9051da177e4SLinus Torvalds  * request state to done. */
906fde15c3aSHeiko Carstens static void sclp_check_handler(struct ext_code ext_code,
907f6649a7eSMartin Schwidefsky 			       unsigned int param32, unsigned long param64)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	u32 finished_sccb;
9101da177e4SLinus Torvalds 
911420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_SCP);
912f6649a7eSMartin Schwidefsky 	finished_sccb = param32 & 0xfffffff8;
9131da177e4SLinus Torvalds 	/* Is this the interrupt we are waiting for? */
9141da177e4SLinus Torvalds 	if (finished_sccb == 0)
9151da177e4SLinus Torvalds 		return;
916a12c53f4SMartin Schwidefsky 	if (finished_sccb != (u32) (addr_t) sclp_init_sccb)
917a12c53f4SMartin Schwidefsky 		panic("sclp: unsolicited interrupt for buffer at 0x%x\n",
918a12c53f4SMartin Schwidefsky 		      finished_sccb);
9191da177e4SLinus Torvalds 	spin_lock(&sclp_lock);
9201da177e4SLinus Torvalds 	if (sclp_running_state == sclp_running_state_running) {
9211da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_DONE;
9221da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
9231da177e4SLinus Torvalds 	}
9241da177e4SLinus Torvalds 	spin_unlock(&sclp_lock);
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds /* Initial init mask request timed out. Modify request state to failed. */
9281da177e4SLinus Torvalds static void
929c9602ee7SKees Cook sclp_check_timeout(struct timer_list *unused)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	unsigned long flags;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
9341da177e4SLinus Torvalds 	if (sclp_running_state == sclp_running_state_running) {
9351da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_FAILED;
9361da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_idle;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds /* Perform a check of the SCLP interface. Return zero if the interface is
9421da177e4SLinus Torvalds  * available and there are no pending requests from a previous instance.
9431da177e4SLinus Torvalds  * Return non-zero otherwise. */
9441da177e4SLinus Torvalds static int
9451da177e4SLinus Torvalds sclp_check_interface(void)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	struct init_sccb *sccb;
9481da177e4SLinus Torvalds 	unsigned long flags;
9491da177e4SLinus Torvalds 	int retry;
9501da177e4SLinus Torvalds 	int rc;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
9531da177e4SLinus Torvalds 	/* Prepare init mask command */
9541dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler);
9551da177e4SLinus Torvalds 	if (rc) {
9561da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
9571da177e4SLinus Torvalds 		return rc;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 	for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
9601da177e4SLinus Torvalds 		__sclp_make_init_req(0, 0);
9611da177e4SLinus Torvalds 		sccb = (struct init_sccb *) sclp_init_req.sccb;
962ab14de6cSHeiko Carstens 		rc = sclp_service_call(sclp_init_req.command, sccb);
9631da177e4SLinus Torvalds 		if (rc == -EIO)
9641da177e4SLinus Torvalds 			break;
9651da177e4SLinus Torvalds 		sclp_init_req.status = SCLP_REQ_RUNNING;
9661da177e4SLinus Torvalds 		sclp_running_state = sclp_running_state_running;
9671da177e4SLinus Torvalds 		__sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
968c9602ee7SKees Cook 					 sclp_check_timeout);
9691da177e4SLinus Torvalds 		spin_unlock_irqrestore(&sclp_lock, flags);
9701da177e4SLinus Torvalds 		/* Enable service-signal interruption - needs to happen
9711da177e4SLinus Torvalds 		 * with IRQs enabled. */
97282003c3eSHeiko Carstens 		irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
9731da177e4SLinus Torvalds 		/* Wait for signal from interrupt or timeout */
9741da177e4SLinus Torvalds 		sclp_sync_wait();
9751da177e4SLinus Torvalds 		/* Disable service-signal interruption - needs to happen
9761da177e4SLinus Torvalds 		 * with IRQs enabled. */
97782003c3eSHeiko Carstens 		irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
9781da177e4SLinus Torvalds 		spin_lock_irqsave(&sclp_lock, flags);
9791da177e4SLinus Torvalds 		del_timer(&sclp_request_timer);
9801da177e4SLinus Torvalds 		if (sclp_init_req.status == SCLP_REQ_DONE &&
9811da177e4SLinus Torvalds 		    sccb->header.response_code == 0x20) {
9821da177e4SLinus Torvalds 			rc = 0;
9831da177e4SLinus Torvalds 			break;
9841da177e4SLinus Torvalds 		} else
9851da177e4SLinus Torvalds 			rc = -EBUSY;
9861da177e4SLinus Torvalds 	}
9871dad093bSThomas Huth 	unregister_external_irq(EXT_IRQ_SERVICE_SIG, sclp_check_handler);
9881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
9891da177e4SLinus Torvalds 	return rc;
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds /* Reboot event handler. Reset send and receive mask to prevent pending SCLP
9931da177e4SLinus Torvalds  * events from interfering with rebooted system. */
9941da177e4SLinus Torvalds static int
9951da177e4SLinus Torvalds sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
9961da177e4SLinus Torvalds {
9971da177e4SLinus Torvalds 	sclp_deactivate();
9981da177e4SLinus Torvalds 	return NOTIFY_DONE;
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds static struct notifier_block sclp_reboot_notifier = {
10021da177e4SLinus Torvalds 	.notifier_call = sclp_reboot_event
10031da177e4SLinus Torvalds };
10041da177e4SLinus Torvalds 
100562b74942SMichael Holzheu /*
100662b74942SMichael Holzheu  * Suspend/resume SCLP notifier implementation
100762b74942SMichael Holzheu  */
100862b74942SMichael Holzheu 
100962b74942SMichael Holzheu static void sclp_pm_event(enum sclp_pm_event sclp_pm_event, int rollback)
101062b74942SMichael Holzheu {
101162b74942SMichael Holzheu 	struct sclp_register *reg;
101262b74942SMichael Holzheu 	unsigned long flags;
101362b74942SMichael Holzheu 
101462b74942SMichael Holzheu 	if (!rollback) {
101562b74942SMichael Holzheu 		spin_lock_irqsave(&sclp_lock, flags);
101662b74942SMichael Holzheu 		list_for_each_entry(reg, &sclp_reg_list, list)
101762b74942SMichael Holzheu 			reg->pm_event_posted = 0;
101862b74942SMichael Holzheu 		spin_unlock_irqrestore(&sclp_lock, flags);
101962b74942SMichael Holzheu 	}
102062b74942SMichael Holzheu 	do {
102162b74942SMichael Holzheu 		spin_lock_irqsave(&sclp_lock, flags);
102262b74942SMichael Holzheu 		list_for_each_entry(reg, &sclp_reg_list, list) {
102362b74942SMichael Holzheu 			if (rollback && reg->pm_event_posted)
102462b74942SMichael Holzheu 				goto found;
102562b74942SMichael Holzheu 			if (!rollback && !reg->pm_event_posted)
102662b74942SMichael Holzheu 				goto found;
102762b74942SMichael Holzheu 		}
102862b74942SMichael Holzheu 		spin_unlock_irqrestore(&sclp_lock, flags);
102962b74942SMichael Holzheu 		return;
103062b74942SMichael Holzheu found:
103162b74942SMichael Holzheu 		spin_unlock_irqrestore(&sclp_lock, flags);
103262b74942SMichael Holzheu 		if (reg->pm_event_fn)
103362b74942SMichael Holzheu 			reg->pm_event_fn(reg, sclp_pm_event);
103462b74942SMichael Holzheu 		reg->pm_event_posted = rollback ? 0 : 1;
103562b74942SMichael Holzheu 	} while (1);
103662b74942SMichael Holzheu }
103762b74942SMichael Holzheu 
103862b74942SMichael Holzheu /*
103962b74942SMichael Holzheu  * Susend/resume callbacks for platform device
104062b74942SMichael Holzheu  */
104162b74942SMichael Holzheu 
104262b74942SMichael Holzheu static int sclp_freeze(struct device *dev)
104362b74942SMichael Holzheu {
104462b74942SMichael Holzheu 	unsigned long flags;
104562b74942SMichael Holzheu 	int rc;
104662b74942SMichael Holzheu 
104762b74942SMichael Holzheu 	sclp_pm_event(SCLP_PM_EVENT_FREEZE, 0);
104862b74942SMichael Holzheu 
104962b74942SMichael Holzheu 	spin_lock_irqsave(&sclp_lock, flags);
105062b74942SMichael Holzheu 	sclp_suspend_state = sclp_suspend_state_suspended;
105162b74942SMichael Holzheu 	spin_unlock_irqrestore(&sclp_lock, flags);
105262b74942SMichael Holzheu 
105362b74942SMichael Holzheu 	/* Init supend data */
105462b74942SMichael Holzheu 	memset(&sclp_suspend_req, 0, sizeof(sclp_suspend_req));
105562b74942SMichael Holzheu 	sclp_suspend_req.callback = sclp_suspend_req_cb;
105662b74942SMichael Holzheu 	sclp_suspend_req.status = SCLP_REQ_FILLED;
105762b74942SMichael Holzheu 	init_completion(&sclp_request_queue_flushed);
105862b74942SMichael Holzheu 
105962b74942SMichael Holzheu 	rc = sclp_add_request(&sclp_suspend_req);
106062b74942SMichael Holzheu 	if (rc == 0)
106162b74942SMichael Holzheu 		wait_for_completion(&sclp_request_queue_flushed);
106262b74942SMichael Holzheu 	else if (rc != -ENODATA)
106362b74942SMichael Holzheu 		goto fail_thaw;
106462b74942SMichael Holzheu 
106562b74942SMichael Holzheu 	rc = sclp_deactivate();
106662b74942SMichael Holzheu 	if (rc)
106762b74942SMichael Holzheu 		goto fail_thaw;
106862b74942SMichael Holzheu 	return 0;
106962b74942SMichael Holzheu 
107062b74942SMichael Holzheu fail_thaw:
107162b74942SMichael Holzheu 	spin_lock_irqsave(&sclp_lock, flags);
107262b74942SMichael Holzheu 	sclp_suspend_state = sclp_suspend_state_running;
107362b74942SMichael Holzheu 	spin_unlock_irqrestore(&sclp_lock, flags);
107462b74942SMichael Holzheu 	sclp_pm_event(SCLP_PM_EVENT_THAW, 1);
107562b74942SMichael Holzheu 	return rc;
107662b74942SMichael Holzheu }
107762b74942SMichael Holzheu 
107862b74942SMichael Holzheu static int sclp_undo_suspend(enum sclp_pm_event event)
107962b74942SMichael Holzheu {
108062b74942SMichael Holzheu 	unsigned long flags;
108162b74942SMichael Holzheu 	int rc;
108262b74942SMichael Holzheu 
108362b74942SMichael Holzheu 	rc = sclp_reactivate();
108462b74942SMichael Holzheu 	if (rc)
108562b74942SMichael Holzheu 		return rc;
108662b74942SMichael Holzheu 
108762b74942SMichael Holzheu 	spin_lock_irqsave(&sclp_lock, flags);
108862b74942SMichael Holzheu 	sclp_suspend_state = sclp_suspend_state_running;
108962b74942SMichael Holzheu 	spin_unlock_irqrestore(&sclp_lock, flags);
109062b74942SMichael Holzheu 
109162b74942SMichael Holzheu 	sclp_pm_event(event, 0);
109262b74942SMichael Holzheu 	return 0;
109362b74942SMichael Holzheu }
109462b74942SMichael Holzheu 
109562b74942SMichael Holzheu static int sclp_thaw(struct device *dev)
109662b74942SMichael Holzheu {
109762b74942SMichael Holzheu 	return sclp_undo_suspend(SCLP_PM_EVENT_THAW);
109862b74942SMichael Holzheu }
109962b74942SMichael Holzheu 
110062b74942SMichael Holzheu static int sclp_restore(struct device *dev)
110162b74942SMichael Holzheu {
110262b74942SMichael Holzheu 	return sclp_undo_suspend(SCLP_PM_EVENT_RESTORE);
110362b74942SMichael Holzheu }
110462b74942SMichael Holzheu 
110547145210SAlexey Dobriyan static const struct dev_pm_ops sclp_pm_ops = {
110662b74942SMichael Holzheu 	.freeze		= sclp_freeze,
110762b74942SMichael Holzheu 	.thaw		= sclp_thaw,
110862b74942SMichael Holzheu 	.restore	= sclp_restore,
110962b74942SMichael Holzheu };
111062b74942SMichael Holzheu 
111136369569SGreg Kroah-Hartman static ssize_t con_pages_show(struct device_driver *dev, char *buf)
111225b41a7bSMartin Schwidefsky {
111325b41a7bSMartin Schwidefsky 	return sprintf(buf, "%i\n", sclp_console_pages);
111425b41a7bSMartin Schwidefsky }
111525b41a7bSMartin Schwidefsky 
111636369569SGreg Kroah-Hartman static DRIVER_ATTR_RO(con_pages);
111725b41a7bSMartin Schwidefsky 
111836369569SGreg Kroah-Hartman static ssize_t con_drop_show(struct device_driver *dev, char *buf)
111925b41a7bSMartin Schwidefsky {
112025b41a7bSMartin Schwidefsky 	return sprintf(buf, "%i\n", sclp_console_drop);
112125b41a7bSMartin Schwidefsky }
112225b41a7bSMartin Schwidefsky 
112336369569SGreg Kroah-Hartman static DRIVER_ATTR_RO(con_drop);
112425b41a7bSMartin Schwidefsky 
112536369569SGreg Kroah-Hartman static ssize_t con_full_show(struct device_driver *dev, char *buf)
112625b41a7bSMartin Schwidefsky {
112725b41a7bSMartin Schwidefsky 	return sprintf(buf, "%lu\n", sclp_console_full);
112825b41a7bSMartin Schwidefsky }
112925b41a7bSMartin Schwidefsky 
113036369569SGreg Kroah-Hartman static DRIVER_ATTR_RO(con_full);
113125b41a7bSMartin Schwidefsky 
113225b41a7bSMartin Schwidefsky static struct attribute *sclp_drv_attrs[] = {
113325b41a7bSMartin Schwidefsky 	&driver_attr_con_pages.attr,
113425b41a7bSMartin Schwidefsky 	&driver_attr_con_drop.attr,
113525b41a7bSMartin Schwidefsky 	&driver_attr_con_full.attr,
113625b41a7bSMartin Schwidefsky 	NULL,
113725b41a7bSMartin Schwidefsky };
113825b41a7bSMartin Schwidefsky static struct attribute_group sclp_drv_attr_group = {
113925b41a7bSMartin Schwidefsky 	.attrs = sclp_drv_attrs,
114025b41a7bSMartin Schwidefsky };
114125b41a7bSMartin Schwidefsky static const struct attribute_group *sclp_drv_attr_groups[] = {
114225b41a7bSMartin Schwidefsky 	&sclp_drv_attr_group,
114325b41a7bSMartin Schwidefsky 	NULL,
114425b41a7bSMartin Schwidefsky };
114525b41a7bSMartin Schwidefsky 
114662b74942SMichael Holzheu static struct platform_driver sclp_pdrv = {
114762b74942SMichael Holzheu 	.driver = {
114862b74942SMichael Holzheu 		.name	= "sclp",
114962b74942SMichael Holzheu 		.pm	= &sclp_pm_ops,
115025b41a7bSMartin Schwidefsky 		.groups = sclp_drv_attr_groups,
115162b74942SMichael Holzheu 	},
115262b74942SMichael Holzheu };
115362b74942SMichael Holzheu 
115462b74942SMichael Holzheu static struct platform_device *sclp_pdev;
115562b74942SMichael Holzheu 
11561da177e4SLinus Torvalds /* Initialize SCLP driver. Return zero if driver is operational, non-zero
11571da177e4SLinus Torvalds  * otherwise. */
11581da177e4SLinus Torvalds static int
11591da177e4SLinus Torvalds sclp_init(void)
11601da177e4SLinus Torvalds {
11611da177e4SLinus Torvalds 	unsigned long flags;
116262b74942SMichael Holzheu 	int rc = 0;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
11651da177e4SLinus Torvalds 	/* Check for previous or running initialization */
116662b74942SMichael Holzheu 	if (sclp_init_state != sclp_init_state_uninitialized)
116762b74942SMichael Holzheu 		goto fail_unlock;
11681da177e4SLinus Torvalds 	sclp_init_state = sclp_init_state_initializing;
11691da177e4SLinus Torvalds 	/* Set up variables */
11701da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sclp_req_queue);
11711da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sclp_reg_list);
11721da177e4SLinus Torvalds 	list_add(&sclp_state_change_event.list, &sclp_reg_list);
1173c9602ee7SKees Cook 	timer_setup(&sclp_request_timer, NULL, 0);
1174c9602ee7SKees Cook 	timer_setup(&sclp_queue_timer, sclp_req_queue_timeout, 0);
11751da177e4SLinus Torvalds 	/* Check interface */
11761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
11771da177e4SLinus Torvalds 	rc = sclp_check_interface();
11781da177e4SLinus Torvalds 	spin_lock_irqsave(&sclp_lock, flags);
117962b74942SMichael Holzheu 	if (rc)
118062b74942SMichael Holzheu 		goto fail_init_state_uninitialized;
11811da177e4SLinus Torvalds 	/* Register reboot handler */
11821da177e4SLinus Torvalds 	rc = register_reboot_notifier(&sclp_reboot_notifier);
118362b74942SMichael Holzheu 	if (rc)
118462b74942SMichael Holzheu 		goto fail_init_state_uninitialized;
11851da177e4SLinus Torvalds 	/* Register interrupt handler */
11861dad093bSThomas Huth 	rc = register_external_irq(EXT_IRQ_SERVICE_SIG, sclp_interrupt_handler);
118762b74942SMichael Holzheu 	if (rc)
118862b74942SMichael Holzheu 		goto fail_unregister_reboot_notifier;
11891da177e4SLinus Torvalds 	sclp_init_state = sclp_init_state_initialized;
11901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&sclp_lock, flags);
11911da177e4SLinus Torvalds 	/* Enable service-signal external interruption - needs to happen with
11921da177e4SLinus Torvalds 	 * IRQs enabled. */
119382003c3eSHeiko Carstens 	irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
11941da177e4SLinus Torvalds 	sclp_init_mask(1);
11951da177e4SLinus Torvalds 	return 0;
119662b74942SMichael Holzheu 
119762b74942SMichael Holzheu fail_unregister_reboot_notifier:
119862b74942SMichael Holzheu 	unregister_reboot_notifier(&sclp_reboot_notifier);
119962b74942SMichael Holzheu fail_init_state_uninitialized:
120062b74942SMichael Holzheu 	sclp_init_state = sclp_init_state_uninitialized;
120162b74942SMichael Holzheu fail_unlock:
120262b74942SMichael Holzheu 	spin_unlock_irqrestore(&sclp_lock, flags);
120362b74942SMichael Holzheu 	return rc;
12041da177e4SLinus Torvalds }
1205b3d00c3bSPeter Oberparleiter 
120662b74942SMichael Holzheu /*
120762b74942SMichael Holzheu  * SCLP panic notifier: If we are suspended, we thaw SCLP in order to be able
120862b74942SMichael Holzheu  * to print the panic message.
120962b74942SMichael Holzheu  */
121062b74942SMichael Holzheu static int sclp_panic_notify(struct notifier_block *self,
121162b74942SMichael Holzheu 			     unsigned long event, void *data)
121262b74942SMichael Holzheu {
121362b74942SMichael Holzheu 	if (sclp_suspend_state == sclp_suspend_state_suspended)
121462b74942SMichael Holzheu 		sclp_undo_suspend(SCLP_PM_EVENT_THAW);
121562b74942SMichael Holzheu 	return NOTIFY_OK;
121662b74942SMichael Holzheu }
121762b74942SMichael Holzheu 
121862b74942SMichael Holzheu static struct notifier_block sclp_on_panic_nb = {
121962b74942SMichael Holzheu 	.notifier_call = sclp_panic_notify,
122062b74942SMichael Holzheu 	.priority = SCLP_PANIC_PRIO,
122162b74942SMichael Holzheu };
122262b74942SMichael Holzheu 
1223b3d00c3bSPeter Oberparleiter static __init int sclp_initcall(void)
1224b3d00c3bSPeter Oberparleiter {
122562b74942SMichael Holzheu 	int rc;
122662b74942SMichael Holzheu 
122762b74942SMichael Holzheu 	rc = platform_driver_register(&sclp_pdrv);
122862b74942SMichael Holzheu 	if (rc)
122962b74942SMichael Holzheu 		return rc;
123025b41a7bSMartin Schwidefsky 
123162b74942SMichael Holzheu 	sclp_pdev = platform_device_register_simple("sclp", -1, NULL, 0);
123283d8e252SDuan Jiong 	rc = PTR_ERR_OR_ZERO(sclp_pdev);
123362b74942SMichael Holzheu 	if (rc)
123462b74942SMichael Holzheu 		goto fail_platform_driver_unregister;
123525b41a7bSMartin Schwidefsky 
123662b74942SMichael Holzheu 	rc = atomic_notifier_chain_register(&panic_notifier_list,
123762b74942SMichael Holzheu 					    &sclp_on_panic_nb);
123862b74942SMichael Holzheu 	if (rc)
123962b74942SMichael Holzheu 		goto fail_platform_device_unregister;
124062b74942SMichael Holzheu 
1241b3d00c3bSPeter Oberparleiter 	return sclp_init();
124262b74942SMichael Holzheu 
124362b74942SMichael Holzheu fail_platform_device_unregister:
124462b74942SMichael Holzheu 	platform_device_unregister(sclp_pdev);
124562b74942SMichael Holzheu fail_platform_driver_unregister:
124662b74942SMichael Holzheu 	platform_driver_unregister(&sclp_pdrv);
124762b74942SMichael Holzheu 	return rc;
1248b3d00c3bSPeter Oberparleiter }
1249b3d00c3bSPeter Oberparleiter 
1250b3d00c3bSPeter Oberparleiter arch_initcall(sclp_initcall);
1251