airq.c 3.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  drivers/s390/cio/airq.c
3
 *    Support for adapter interruptions
L
Linus Torvalds 已提交
4
 *
5 6 7 8 9
 *    Copyright IBM Corp. 1999,2007
 *    Author(s): Ingo Adlung <adlung@de.ibm.com>
 *		 Cornelia Huck <cornelia.huck@de.ibm.com>
 *		 Arnd Bergmann <arndb@de.ibm.com>
 *		 Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/rcupdate.h>

17
#include <asm/airq.h>
18
#include <asm/isc.h>
19 20

#include "cio.h"
L
Linus Torvalds 已提交
21 22
#include "cio_debug.h"

23 24 25
#define NR_AIRQS		32
#define NR_AIRQS_PER_WORD	sizeof(unsigned long)
#define NR_AIRQ_WORDS		(NR_AIRQS / NR_AIRQS_PER_WORD)
L
Linus Torvalds 已提交
26

27 28 29 30
union indicator_t {
	unsigned long word[NR_AIRQ_WORDS];
	unsigned char byte[NR_AIRQS];
} __attribute__((packed));
L
Linus Torvalds 已提交
31

32 33 34 35
struct airq_t {
	adapter_int_handler_t handler;
	void *drv_data;
};
L
Linus Torvalds 已提交
36

37 38
static union indicator_t indicators[MAX_ISC];
static struct airq_t *airqs[MAX_ISC][NR_AIRQS];
L
Linus Torvalds 已提交
39

40
static int register_airq(struct airq_t *airq, u8 isc)
41 42
{
	int i;
L
Linus Torvalds 已提交
43

44
	for (i = 0; i < NR_AIRQS; i++)
45
		if (!cmpxchg(&airqs[isc][i], NULL, airq))
46 47
			return i;
	return -ENOMEM;
L
Linus Torvalds 已提交
48 49
}

50 51 52 53
/**
 * s390_register_adapter_interrupt() - register adapter interrupt handler
 * @handler: adapter handler to be registered
 * @drv_data: driver data passed with each call to the handler
54
 * @isc: isc for which the handler should be called
55 56 57 58 59 60
 *
 * Returns:
 *  Pointer to the indicator to be used on success
 *  ERR_PTR() if registration failed
 */
void *s390_register_adapter_interrupt(adapter_int_handler_t handler,
61
				      void *drv_data, u8 isc)
L
Linus Torvalds 已提交
62
{
63 64
	struct airq_t *airq;
	char dbf_txt[16];
L
Linus Torvalds 已提交
65 66
	int ret;

67 68
	if (isc > MAX_ISC)
		return ERR_PTR(-EINVAL);
69 70 71 72
	airq = kmalloc(sizeof(struct airq_t), GFP_KERNEL);
	if (!airq) {
		ret = -ENOMEM;
		goto out;
L
Linus Torvalds 已提交
73
	}
74 75
	airq->handler = handler;
	airq->drv_data = drv_data;
76 77

	ret = register_airq(airq, isc);
78 79 80
out:
	snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%d", ret);
	CIO_TRACE_EVENT(4, dbf_txt);
81 82
	if (ret < 0) {
		kfree(airq);
83
		return ERR_PTR(ret);
84 85
	} else
		return &indicators[isc].byte[ret];
L
Linus Torvalds 已提交
86
}
87
EXPORT_SYMBOL(s390_register_adapter_interrupt);
L
Linus Torvalds 已提交
88

89 90 91
/**
 * s390_unregister_adapter_interrupt - unregister adapter interrupt handler
 * @ind: indicator for which the handler is to be unregistered
92
 * @isc: interruption subclass
93
 */
94
void s390_unregister_adapter_interrupt(void *ind, u8 isc)
L
Linus Torvalds 已提交
95
{
96 97 98
	struct airq_t *airq;
	char dbf_txt[16];
	int i;
L
Linus Torvalds 已提交
99

100
	i = (int) ((addr_t) ind) - ((addr_t) &indicators[isc].byte[0]);
101 102
	snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%d", i);
	CIO_TRACE_EVENT(4, dbf_txt);
103 104
	indicators[isc].byte[i] = 0;
	airq = xchg(&airqs[isc][i], NULL);
105 106 107 108 109 110
	/*
	 * Allow interrupts to complete. This will ensure that the airq handle
	 * is no longer referenced by any interrupt handler.
	 */
	synchronize_sched();
	kfree(airq);
L
Linus Torvalds 已提交
111
}
112 113 114
EXPORT_SYMBOL(s390_unregister_adapter_interrupt);

#define INDICATOR_MASK	(0xffUL << ((NR_AIRQS_PER_WORD - 1) * 8))
L
Linus Torvalds 已提交
115

116
void do_adapter_IO(u8 isc)
117 118 119 120 121 122 123 124 125 126 127
{
	int w;
	int i;
	unsigned long word;
	struct airq_t *airq;

	/*
	 * Access indicator array in word-sized chunks to minimize storage
	 * fetch operations.
	 */
	for (w = 0; w < NR_AIRQ_WORDS; w++) {
128
		word = indicators[isc].word[w];
129 130 131 132 133 134
		i = w * NR_AIRQS_PER_WORD;
		/*
		 * Check bytes within word for active indicators.
		 */
		while (word) {
			if (word & INDICATOR_MASK) {
135
				airq = airqs[isc][i];
136 137
				/* Make sure gcc reads from airqs only once. */
				barrier();
138
				if (likely(airq))
139
					airq->handler(&indicators[isc].byte[i],
140 141 142 143 144
						      airq->drv_data);
				else
					/*
					 * Reset ill-behaved indicator.
					 */
145
					indicators[isc].byte[i] = 0;
146 147 148 149 150 151
			}
			word <<= 8;
			i++;
		}
	}
}