blob: 65d2e769dfa16123ff0a50593ef0797ff670e8f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/airq.c
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +01003 * Support for adapter interruptions
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +01005 * Copyright IBM Corp. 1999,2007
6 * Author(s): Ingo Adlung <adlung@de.ibm.com>
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 * Arnd Bergmann <arndb@de.ibm.com>
9 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/rcupdate.h>
16
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010017#include <asm/airq.h>
Cornelia Huckda7c5af2008-07-14 09:58:59 +020018#include <asm/isc.h>
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010019
20#include "cio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010023#define NR_AIRQS 32
24#define NR_AIRQS_PER_WORD sizeof(unsigned long)
25#define NR_AIRQ_WORDS (NR_AIRQS / NR_AIRQS_PER_WORD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010027union indicator_t {
28 unsigned long word[NR_AIRQ_WORDS];
29 unsigned char byte[NR_AIRQS];
30} __attribute__((packed));
31
32struct airq_t {
33 adapter_int_handler_t handler;
34 void *drv_data;
35};
36
Sebastian Otta1f64072009-03-26 15:24:12 +010037static union indicator_t indicators[MAX_ISC+1];
38static struct airq_t *airqs[MAX_ISC+1][NR_AIRQS];
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010039
Cornelia Huckda7c5af2008-07-14 09:58:59 +020040static int register_airq(struct airq_t *airq, u8 isc)
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010041{
42 int i;
43
44 for (i = 0; i < NR_AIRQS; i++)
Cornelia Huckda7c5af2008-07-14 09:58:59 +020045 if (!cmpxchg(&airqs[isc][i], NULL, airq))
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010046 return i;
47 return -ENOMEM;
48}
49
50/**
51 * s390_register_adapter_interrupt() - register adapter interrupt handler
52 * @handler: adapter handler to be registered
53 * @drv_data: driver data passed with each call to the handler
Cornelia Huckda7c5af2008-07-14 09:58:59 +020054 * @isc: isc for which the handler should be called
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010056 * Returns:
57 * Pointer to the indicator to be used on success
58 * ERR_PTR() if registration failed
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010060void *s390_register_adapter_interrupt(adapter_int_handler_t handler,
Cornelia Huckda7c5af2008-07-14 09:58:59 +020061 void *drv_data, u8 isc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010063 struct airq_t *airq;
64 char dbf_txt[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Cornelia Huckda7c5af2008-07-14 09:58:59 +020067 if (isc > MAX_ISC)
68 return ERR_PTR(-EINVAL);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010069 airq = kmalloc(sizeof(struct airq_t), GFP_KERNEL);
70 if (!airq) {
71 ret = -ENOMEM;
72 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010074 airq->handler = handler;
75 airq->drv_data = drv_data;
Cornelia Huckda7c5af2008-07-14 09:58:59 +020076
77 ret = register_airq(airq, isc);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010078out:
79 snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%d", ret);
80 CIO_TRACE_EVENT(4, dbf_txt);
Cornelia Huckda7c5af2008-07-14 09:58:59 +020081 if (ret < 0) {
82 kfree(airq);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010083 return ERR_PTR(ret);
Cornelia Huckda7c5af2008-07-14 09:58:59 +020084 } else
85 return &indicators[isc].byte[ret];
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010087EXPORT_SYMBOL(s390_register_adapter_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010089/**
90 * s390_unregister_adapter_interrupt - unregister adapter interrupt handler
91 * @ind: indicator for which the handler is to be unregistered
Cornelia Huckda7c5af2008-07-14 09:58:59 +020092 * @isc: interruption subclass
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010093 */
Cornelia Huckda7c5af2008-07-14 09:58:59 +020094void s390_unregister_adapter_interrupt(void *ind, u8 isc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010096 struct airq_t *airq;
97 char dbf_txt[16];
98 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200100 i = (int) ((addr_t) ind) - ((addr_t) &indicators[isc].byte[0]);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100101 snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%d", i);
102 CIO_TRACE_EVENT(4, dbf_txt);
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200103 indicators[isc].byte[i] = 0;
104 airq = xchg(&airqs[isc][i], NULL);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100105 /*
106 * Allow interrupts to complete. This will ensure that the airq handle
107 * is no longer referenced by any interrupt handler.
108 */
109 synchronize_sched();
110 kfree(airq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100112EXPORT_SYMBOL(s390_unregister_adapter_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100114#define INDICATOR_MASK (0xffUL << ((NR_AIRQS_PER_WORD - 1) * 8))
115
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200116void do_adapter_IO(u8 isc)
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100117{
118 int w;
119 int i;
120 unsigned long word;
121 struct airq_t *airq;
122
123 /*
124 * Access indicator array in word-sized chunks to minimize storage
125 * fetch operations.
126 */
127 for (w = 0; w < NR_AIRQ_WORDS; w++) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200128 word = indicators[isc].word[w];
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100129 i = w * NR_AIRQS_PER_WORD;
130 /*
131 * Check bytes within word for active indicators.
132 */
133 while (word) {
134 if (word & INDICATOR_MASK) {
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200135 airq = airqs[isc][i];
Heiko Carstens87fa5af2009-03-26 15:24:10 +0100136 /* Make sure gcc reads from airqs only once. */
137 barrier();
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100138 if (likely(airq))
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200139 airq->handler(&indicators[isc].byte[i],
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100140 airq->drv_data);
141 else
142 /*
143 * Reset ill-behaved indicator.
144 */
Cornelia Huckda7c5af2008-07-14 09:58:59 +0200145 indicators[isc].byte[i] = 0;
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100146 }
147 word <<= 8;
148 i++;
149 }
150 }
151}