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