blob: 277f9ccfbb7c2e94867f29018ed6a13e0634a7cb [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) {
283 /* Search for event handler */
284 evbuf = (struct evbuf_header *) ((addr_t) sccb + offset);
285 reg = NULL;
286 list_for_each(l, &sclp_reg_list) {
287 reg = list_entry(l, struct sclp_register, list);
288 if (reg->receive_mask & (1 << (32 - evbuf->type)))
289 break;
290 else
291 reg = NULL;
292 }
293 if (reg && reg->receiver_fn) {
294 spin_unlock_irqrestore(&sclp_lock, flags);
295 reg->receiver_fn(evbuf);
296 spin_lock_irqsave(&sclp_lock, flags);
297 } else if (reg == NULL)
298 rc = -ENOSYS;
299 }
300 spin_unlock_irqrestore(&sclp_lock, flags);
301 return rc;
302}
303
304/* Read event data request callback. */
305static void
306sclp_read_cb(struct sclp_req *req, void *data)
307{
308 unsigned long flags;
309 struct sccb_header *sccb;
310
311 sccb = (struct sccb_header *) req->sccb;
312 if (req->status == SCLP_REQ_DONE && (sccb->response_code == 0x20 ||
313 sccb->response_code == 0x220))
314 sclp_dispatch_evbufs(sccb);
315 spin_lock_irqsave(&sclp_lock, flags);
316 sclp_reading_state = sclp_reading_state_idle;
317 spin_unlock_irqrestore(&sclp_lock, flags);
318}
319
320/* Prepare read event data request. Called while sclp_lock is locked. */
Heiko Carstens364c8552007-10-12 16:11:35 +0200321static void __sclp_make_read_req(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
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));
Heiko Carstensab14de62007-02-05 21:18:37 +0100328 sclp_read_req.command = SCLP_CMDW_READ_EVENT_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 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 Carstens5a489b982006-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 }
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100383 if (evbuf_pending &&
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 cpu_relax();
437 }
Heiko Carstens1f194a42006-07-03 00:24:46 -0700438 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 __ctl_load(cr0, 0, 0);
Heiko Carstensc59d7442007-02-05 21:17:16 +0100440 if (!irq_context)
441 _local_bh_enable();
Heiko Carstens1f194a42006-07-03 00:24:46 -0700442 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445EXPORT_SYMBOL(sclp_sync_wait);
446
447/* Dispatch changes in send and receive mask to registered listeners. */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100448static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449sclp_dispatch_state_change(void)
450{
451 struct list_head *l;
452 struct sclp_register *reg;
453 unsigned long flags;
454 sccb_mask_t receive_mask;
455 sccb_mask_t send_mask;
456
457 do {
458 spin_lock_irqsave(&sclp_lock, flags);
459 reg = NULL;
460 list_for_each(l, &sclp_reg_list) {
461 reg = list_entry(l, struct sclp_register, list);
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100462 receive_mask = reg->send_mask & sclp_receive_mask;
463 send_mask = reg->receive_mask & sclp_send_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (reg->sclp_receive_mask != receive_mask ||
465 reg->sclp_send_mask != send_mask) {
466 reg->sclp_receive_mask = receive_mask;
467 reg->sclp_send_mask = send_mask;
468 break;
469 } else
470 reg = NULL;
471 }
472 spin_unlock_irqrestore(&sclp_lock, flags);
473 if (reg && reg->state_change_fn)
474 reg->state_change_fn(reg);
475 } while (reg);
476}
477
478struct sclp_statechangebuf {
479 struct evbuf_header header;
480 u8 validity_sclp_active_facility_mask : 1;
481 u8 validity_sclp_receive_mask : 1;
482 u8 validity_sclp_send_mask : 1;
483 u8 validity_read_data_function_mask : 1;
484 u16 _zeros : 12;
485 u16 mask_length;
486 u64 sclp_active_facility_mask;
487 sccb_mask_t sclp_receive_mask;
488 sccb_mask_t sclp_send_mask;
489 u32 read_data_function_mask;
490} __attribute__((packed));
491
492
493/* State change event callback. Inform listeners of changes. */
494static void
495sclp_state_change_cb(struct evbuf_header *evbuf)
496{
497 unsigned long flags;
498 struct sclp_statechangebuf *scbuf;
499
500 scbuf = (struct sclp_statechangebuf *) evbuf;
501 if (scbuf->mask_length != sizeof(sccb_mask_t))
502 return;
503 spin_lock_irqsave(&sclp_lock, flags);
504 if (scbuf->validity_sclp_receive_mask)
505 sclp_receive_mask = scbuf->sclp_receive_mask;
506 if (scbuf->validity_sclp_send_mask)
507 sclp_send_mask = scbuf->sclp_send_mask;
508 spin_unlock_irqrestore(&sclp_lock, flags);
Heiko Carstens887d9352008-07-14 09:57:26 +0200509 if (scbuf->validity_sclp_active_facility_mask)
510 sclp_facilities = scbuf->sclp_active_facility_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 sclp_dispatch_state_change();
512}
513
514static struct sclp_register sclp_state_change_event = {
Stefan Haberland6d4740c2007-04-27 16:01:53 +0200515 .receive_mask = EVTYP_STATECHANGE_MASK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 .receiver_fn = sclp_state_change_cb
517};
518
519/* Calculate receive and send mask of currently registered listeners.
520 * Called while sclp_lock is locked. */
521static inline void
522__sclp_get_mask(sccb_mask_t *receive_mask, sccb_mask_t *send_mask)
523{
524 struct list_head *l;
525 struct sclp_register *t;
526
527 *receive_mask = 0;
528 *send_mask = 0;
529 list_for_each(l, &sclp_reg_list) {
530 t = list_entry(l, struct sclp_register, list);
531 *receive_mask |= t->receive_mask;
532 *send_mask |= t->send_mask;
533 }
534}
535
536/* Register event listener. Return 0 on success, non-zero otherwise. */
537int
538sclp_register(struct sclp_register *reg)
539{
540 unsigned long flags;
541 sccb_mask_t receive_mask;
542 sccb_mask_t send_mask;
543 int rc;
544
545 rc = sclp_init();
546 if (rc)
547 return rc;
548 spin_lock_irqsave(&sclp_lock, flags);
549 /* Check event mask for collisions */
550 __sclp_get_mask(&receive_mask, &send_mask);
551 if (reg->receive_mask & receive_mask || reg->send_mask & send_mask) {
552 spin_unlock_irqrestore(&sclp_lock, flags);
553 return -EBUSY;
554 }
555 /* Trigger initial state change callback */
556 reg->sclp_receive_mask = 0;
557 reg->sclp_send_mask = 0;
558 list_add(&reg->list, &sclp_reg_list);
559 spin_unlock_irqrestore(&sclp_lock, flags);
560 rc = sclp_init_mask(1);
561 if (rc) {
562 spin_lock_irqsave(&sclp_lock, flags);
563 list_del(&reg->list);
564 spin_unlock_irqrestore(&sclp_lock, flags);
565 }
566 return rc;
567}
568
569EXPORT_SYMBOL(sclp_register);
570
571/* Unregister event listener. */
572void
573sclp_unregister(struct sclp_register *reg)
574{
575 unsigned long flags;
576
577 spin_lock_irqsave(&sclp_lock, flags);
578 list_del(&reg->list);
579 spin_unlock_irqrestore(&sclp_lock, flags);
580 sclp_init_mask(1);
581}
582
583EXPORT_SYMBOL(sclp_unregister);
584
585/* Remove event buffers which are marked processed. Return the number of
586 * remaining event buffers. */
587int
588sclp_remove_processed(struct sccb_header *sccb)
589{
590 struct evbuf_header *evbuf;
591 int unprocessed;
592 u16 remaining;
593
594 evbuf = (struct evbuf_header *) (sccb + 1);
595 unprocessed = 0;
596 remaining = sccb->length - sizeof(struct sccb_header);
597 while (remaining > 0) {
598 remaining -= evbuf->length;
599 if (evbuf->flags & 0x80) {
600 sccb->length -= evbuf->length;
601 memcpy(evbuf, (void *) ((addr_t) evbuf + evbuf->length),
602 remaining);
603 } else {
604 unprocessed++;
605 evbuf = (struct evbuf_header *)
606 ((addr_t) evbuf + evbuf->length);
607 }
608 }
609 return unprocessed;
610}
611
612EXPORT_SYMBOL(sclp_remove_processed);
613
614struct init_sccb {
615 struct sccb_header header;
616 u16 _reserved;
617 u16 mask_length;
618 sccb_mask_t receive_mask;
619 sccb_mask_t send_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 sccb_mask_t sclp_receive_mask;
Peter Oberparleiterd082d3c2008-02-19 15:29:32 +0100621 sccb_mask_t sclp_send_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622} __attribute__((packed));
623
624/* Prepare init mask request. Called while sclp_lock is locked. */
625static inline void
626__sclp_make_init_req(u32 receive_mask, u32 send_mask)
627{
628 struct init_sccb *sccb;
629
630 sccb = (struct init_sccb *) sclp_init_sccb;
631 clear_page(sccb);
632 memset(&sclp_init_req, 0, sizeof(struct sclp_req));
Heiko Carstensab14de62007-02-05 21:18:37 +0100633 sclp_init_req.command = SCLP_CMDW_WRITE_EVENT_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 sclp_init_req.status = SCLP_REQ_FILLED;
635 sclp_init_req.start_count = 0;
636 sclp_init_req.callback = NULL;
637 sclp_init_req.callback_data = NULL;
638 sclp_init_req.sccb = sccb;
639 sccb->header.length = sizeof(struct init_sccb);
640 sccb->mask_length = sizeof(sccb_mask_t);
641 sccb->receive_mask = receive_mask;
642 sccb->send_mask = send_mask;
643 sccb->sclp_receive_mask = 0;
644 sccb->sclp_send_mask = 0;
645}
646
647/* Start init mask request. If calculate is non-zero, calculate the mask as
648 * requested by registered listeners. Use zero mask otherwise. Return 0 on
649 * success, non-zero otherwise. */
650static int
651sclp_init_mask(int calculate)
652{
653 unsigned long flags;
654 struct init_sccb *sccb = (struct init_sccb *) sclp_init_sccb;
655 sccb_mask_t receive_mask;
656 sccb_mask_t send_mask;
657 int retry;
658 int rc;
659 unsigned long wait;
660
661 spin_lock_irqsave(&sclp_lock, flags);
662 /* Check if interface is in appropriate state */
663 if (sclp_mask_state != sclp_mask_state_idle) {
664 spin_unlock_irqrestore(&sclp_lock, flags);
665 return -EBUSY;
666 }
667 if (sclp_activation_state == sclp_activation_state_inactive) {
668 spin_unlock_irqrestore(&sclp_lock, flags);
669 return -EINVAL;
670 }
671 sclp_mask_state = sclp_mask_state_initializing;
672 /* Determine mask */
673 if (calculate)
674 __sclp_get_mask(&receive_mask, &send_mask);
675 else {
676 receive_mask = 0;
677 send_mask = 0;
678 }
679 rc = -EIO;
680 for (retry = 0; retry <= SCLP_MASK_RETRY; retry++) {
681 /* Prepare request */
682 __sclp_make_init_req(receive_mask, send_mask);
683 spin_unlock_irqrestore(&sclp_lock, flags);
684 if (sclp_add_request(&sclp_init_req)) {
685 /* Try again later */
686 wait = jiffies + SCLP_BUSY_INTERVAL * HZ;
687 while (time_before(jiffies, wait))
688 sclp_sync_wait();
689 spin_lock_irqsave(&sclp_lock, flags);
690 continue;
691 }
692 while (sclp_init_req.status != SCLP_REQ_DONE &&
693 sclp_init_req.status != SCLP_REQ_FAILED)
694 sclp_sync_wait();
695 spin_lock_irqsave(&sclp_lock, flags);
696 if (sclp_init_req.status == SCLP_REQ_DONE &&
697 sccb->header.response_code == 0x20) {
698 /* Successful request */
699 if (calculate) {
700 sclp_receive_mask = sccb->sclp_receive_mask;
701 sclp_send_mask = sccb->sclp_send_mask;
702 } else {
703 sclp_receive_mask = 0;
704 sclp_send_mask = 0;
705 }
706 spin_unlock_irqrestore(&sclp_lock, flags);
707 sclp_dispatch_state_change();
708 spin_lock_irqsave(&sclp_lock, flags);
709 rc = 0;
710 break;
711 }
712 }
713 sclp_mask_state = sclp_mask_state_idle;
714 spin_unlock_irqrestore(&sclp_lock, flags);
715 return rc;
716}
717
718/* Deactivate SCLP interface. On success, new requests will be rejected,
719 * events will no longer be dispatched. Return 0 on success, non-zero
720 * otherwise. */
721int
722sclp_deactivate(void)
723{
724 unsigned long flags;
725 int rc;
726
727 spin_lock_irqsave(&sclp_lock, flags);
728 /* Deactivate can only be called when active */
729 if (sclp_activation_state != sclp_activation_state_active) {
730 spin_unlock_irqrestore(&sclp_lock, flags);
731 return -EINVAL;
732 }
733 sclp_activation_state = sclp_activation_state_deactivating;
734 spin_unlock_irqrestore(&sclp_lock, flags);
735 rc = sclp_init_mask(0);
736 spin_lock_irqsave(&sclp_lock, flags);
737 if (rc == 0)
738 sclp_activation_state = sclp_activation_state_inactive;
739 else
740 sclp_activation_state = sclp_activation_state_active;
741 spin_unlock_irqrestore(&sclp_lock, flags);
742 return rc;
743}
744
745EXPORT_SYMBOL(sclp_deactivate);
746
747/* Reactivate SCLP interface after sclp_deactivate. On success, new
748 * requests will be accepted, events will be dispatched again. Return 0 on
749 * success, non-zero otherwise. */
750int
751sclp_reactivate(void)
752{
753 unsigned long flags;
754 int rc;
755
756 spin_lock_irqsave(&sclp_lock, flags);
757 /* Reactivate can only be called when inactive */
758 if (sclp_activation_state != sclp_activation_state_inactive) {
759 spin_unlock_irqrestore(&sclp_lock, flags);
760 return -EINVAL;
761 }
762 sclp_activation_state = sclp_activation_state_activating;
763 spin_unlock_irqrestore(&sclp_lock, flags);
764 rc = sclp_init_mask(1);
765 spin_lock_irqsave(&sclp_lock, flags);
766 if (rc == 0)
767 sclp_activation_state = sclp_activation_state_active;
768 else
769 sclp_activation_state = sclp_activation_state_inactive;
770 spin_unlock_irqrestore(&sclp_lock, flags);
771 return rc;
772}
773
774EXPORT_SYMBOL(sclp_reactivate);
775
776/* Handler for external interruption used during initialization. Modify
777 * request state to done. */
778static void
Heiko Carstens5a489b982006-10-06 16:38:35 +0200779sclp_check_handler(__u16 code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 u32 finished_sccb;
782
783 finished_sccb = S390_lowcore.ext_params & 0xfffffff8;
784 /* Is this the interrupt we are waiting for? */
785 if (finished_sccb == 0)
786 return;
787 if (finished_sccb != (u32) (addr_t) sclp_init_sccb) {
788 printk(KERN_WARNING SCLP_HEADER "unsolicited interrupt "
789 "for buffer at 0x%x\n", finished_sccb);
790 return;
791 }
792 spin_lock(&sclp_lock);
793 if (sclp_running_state == sclp_running_state_running) {
794 sclp_init_req.status = SCLP_REQ_DONE;
795 sclp_running_state = sclp_running_state_idle;
796 }
797 spin_unlock(&sclp_lock);
798}
799
800/* Initial init mask request timed out. Modify request state to failed. */
801static void
802sclp_check_timeout(unsigned long data)
803{
804 unsigned long flags;
805
806 spin_lock_irqsave(&sclp_lock, flags);
807 if (sclp_running_state == sclp_running_state_running) {
808 sclp_init_req.status = SCLP_REQ_FAILED;
809 sclp_running_state = sclp_running_state_idle;
810 }
811 spin_unlock_irqrestore(&sclp_lock, flags);
812}
813
814/* Perform a check of the SCLP interface. Return zero if the interface is
815 * available and there are no pending requests from a previous instance.
816 * Return non-zero otherwise. */
817static int
818sclp_check_interface(void)
819{
820 struct init_sccb *sccb;
821 unsigned long flags;
822 int retry;
823 int rc;
824
825 spin_lock_irqsave(&sclp_lock, flags);
826 /* Prepare init mask command */
827 rc = register_early_external_interrupt(0x2401, sclp_check_handler,
828 &ext_int_info_hwc);
829 if (rc) {
830 spin_unlock_irqrestore(&sclp_lock, flags);
831 return rc;
832 }
833 for (retry = 0; retry <= SCLP_INIT_RETRY; retry++) {
834 __sclp_make_init_req(0, 0);
835 sccb = (struct init_sccb *) sclp_init_req.sccb;
Heiko Carstensab14de62007-02-05 21:18:37 +0100836 rc = sclp_service_call(sclp_init_req.command, sccb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (rc == -EIO)
838 break;
839 sclp_init_req.status = SCLP_REQ_RUNNING;
840 sclp_running_state = sclp_running_state_running;
841 __sclp_set_request_timer(SCLP_RETRY_INTERVAL * HZ,
842 sclp_check_timeout, 0);
843 spin_unlock_irqrestore(&sclp_lock, flags);
844 /* Enable service-signal interruption - needs to happen
845 * with IRQs enabled. */
846 ctl_set_bit(0, 9);
847 /* Wait for signal from interrupt or timeout */
848 sclp_sync_wait();
849 /* Disable service-signal interruption - needs to happen
850 * with IRQs enabled. */
851 ctl_clear_bit(0,9);
852 spin_lock_irqsave(&sclp_lock, flags);
853 del_timer(&sclp_request_timer);
854 if (sclp_init_req.status == SCLP_REQ_DONE &&
855 sccb->header.response_code == 0x20) {
856 rc = 0;
857 break;
858 } else
859 rc = -EBUSY;
860 }
861 unregister_early_external_interrupt(0x2401, sclp_check_handler,
862 &ext_int_info_hwc);
863 spin_unlock_irqrestore(&sclp_lock, flags);
864 return rc;
865}
866
867/* Reboot event handler. Reset send and receive mask to prevent pending SCLP
868 * events from interfering with rebooted system. */
869static int
870sclp_reboot_event(struct notifier_block *this, unsigned long event, void *ptr)
871{
872 sclp_deactivate();
873 return NOTIFY_DONE;
874}
875
876static struct notifier_block sclp_reboot_notifier = {
877 .notifier_call = sclp_reboot_event
878};
879
880/* Initialize SCLP driver. Return zero if driver is operational, non-zero
881 * otherwise. */
882static int
883sclp_init(void)
884{
885 unsigned long flags;
886 int rc;
887
888 if (!MACHINE_HAS_SCLP)
889 return -ENODEV;
890 spin_lock_irqsave(&sclp_lock, flags);
891 /* Check for previous or running initialization */
892 if (sclp_init_state != sclp_init_state_uninitialized) {
893 spin_unlock_irqrestore(&sclp_lock, flags);
894 return 0;
895 }
896 sclp_init_state = sclp_init_state_initializing;
897 /* Set up variables */
898 INIT_LIST_HEAD(&sclp_req_queue);
899 INIT_LIST_HEAD(&sclp_reg_list);
900 list_add(&sclp_state_change_event.list, &sclp_reg_list);
901 init_timer(&sclp_request_timer);
902 /* Check interface */
903 spin_unlock_irqrestore(&sclp_lock, flags);
904 rc = sclp_check_interface();
905 spin_lock_irqsave(&sclp_lock, flags);
906 if (rc) {
907 sclp_init_state = sclp_init_state_uninitialized;
908 spin_unlock_irqrestore(&sclp_lock, flags);
909 return rc;
910 }
911 /* Register reboot handler */
912 rc = register_reboot_notifier(&sclp_reboot_notifier);
913 if (rc) {
914 sclp_init_state = sclp_init_state_uninitialized;
915 spin_unlock_irqrestore(&sclp_lock, flags);
916 return rc;
917 }
918 /* Register interrupt handler */
919 rc = register_early_external_interrupt(0x2401, sclp_interrupt_handler,
920 &ext_int_info_hwc);
921 if (rc) {
922 unregister_reboot_notifier(&sclp_reboot_notifier);
923 sclp_init_state = sclp_init_state_uninitialized;
924 spin_unlock_irqrestore(&sclp_lock, flags);
925 return rc;
926 }
927 sclp_init_state = sclp_init_state_initialized;
928 spin_unlock_irqrestore(&sclp_lock, flags);
929 /* Enable service-signal external interruption - needs to happen with
930 * IRQs enabled. */
931 ctl_set_bit(0, 9);
932 sclp_init_mask(1);
933 return 0;
934}
Peter Oberparleiterb3d00c32007-04-27 16:01:51 +0200935
936static __init int sclp_initcall(void)
937{
938 return sclp_init();
939}
940
941arch_initcall(sclp_initcall);