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