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