xref: /linux/drivers/s390/cio/qdio_thinint.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  * Copyright IBM Corp. 2000, 2009
3  * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
4  *	      Cornelia Huck <cornelia.huck@de.ibm.com>
5  *	      Jan Glauber <jang@linux.vnet.ibm.com>
6  */
7 #include <linux/io.h>
8 #include <linux/slab.h>
9 #include <linux/kernel_stat.h>
10 #include <linux/atomic.h>
11 #include <linux/rculist.h>
12 
13 #include <asm/debug.h>
14 #include <asm/qdio.h>
15 #include <asm/airq.h>
16 #include <asm/isc.h>
17 
18 #include "cio.h"
19 #include "ioasm.h"
20 #include "qdio.h"
21 #include "qdio_debug.h"
22 
23 /*
24  * Restriction: only 63 iqdio subchannels would have its own indicator,
25  * after that, subsequent subchannels share one indicator
26  */
27 #define TIQDIO_NR_NONSHARED_IND		63
28 #define TIQDIO_NR_INDICATORS		(TIQDIO_NR_NONSHARED_IND + 1)
29 #define TIQDIO_SHARED_IND		63
30 
31 /* device state change indicators */
32 struct indicator_t {
33 	u32 ind;	/* u32 because of compare-and-swap performance */
34 	atomic_t count; /* use count, 0 or 1 for non-shared indicators */
35 };
36 
37 /* list of thin interrupt input queues */
38 static LIST_HEAD(tiq_list);
39 static DEFINE_MUTEX(tiq_list_lock);
40 
41 /* Adapter interrupt definitions */
42 static void tiqdio_thinint_handler(struct airq_struct *airq);
43 
44 static struct airq_struct tiqdio_airq = {
45 	.handler = tiqdio_thinint_handler,
46 	.isc = QDIO_AIRQ_ISC,
47 };
48 
49 static struct indicator_t *q_indicators;
50 
51 u64 last_ai_time;
52 
53 /* returns addr for the device state change indicator */
54 static u32 *get_indicator(void)
55 {
56 	int i;
57 
58 	for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
59 		if (!atomic_read(&q_indicators[i].count)) {
60 			atomic_set(&q_indicators[i].count, 1);
61 			return &q_indicators[i].ind;
62 		}
63 
64 	/* use the shared indicator */
65 	atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
66 	return &q_indicators[TIQDIO_SHARED_IND].ind;
67 }
68 
69 static void put_indicator(u32 *addr)
70 {
71 	int i;
72 
73 	if (!addr)
74 		return;
75 	i = ((unsigned long)addr - (unsigned long)q_indicators) /
76 		sizeof(struct indicator_t);
77 	atomic_dec(&q_indicators[i].count);
78 }
79 
80 void tiqdio_add_input_queues(struct qdio_irq *irq_ptr)
81 {
82 	mutex_lock(&tiq_list_lock);
83 	list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list);
84 	mutex_unlock(&tiq_list_lock);
85 	xchg(irq_ptr->dsci, 1 << 7);
86 }
87 
88 void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr)
89 {
90 	struct qdio_q *q;
91 
92 	q = irq_ptr->input_qs[0];
93 	/* if establish triggered an error */
94 	if (!q || !q->entry.prev || !q->entry.next)
95 		return;
96 
97 	mutex_lock(&tiq_list_lock);
98 	list_del_rcu(&q->entry);
99 	mutex_unlock(&tiq_list_lock);
100 	synchronize_rcu();
101 }
102 
103 static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr)
104 {
105 	return irq_ptr->nr_input_qs > 1;
106 }
107 
108 static inline int references_shared_dsci(struct qdio_irq *irq_ptr)
109 {
110 	return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
111 }
112 
113 static inline int shared_ind(struct qdio_irq *irq_ptr)
114 {
115 	return references_shared_dsci(irq_ptr) ||
116 		has_multiple_inq_on_dsci(irq_ptr);
117 }
118 
119 void clear_nonshared_ind(struct qdio_irq *irq_ptr)
120 {
121 	if (!is_thinint_irq(irq_ptr))
122 		return;
123 	if (shared_ind(irq_ptr))
124 		return;
125 	xchg(irq_ptr->dsci, 0);
126 }
127 
128 int test_nonshared_ind(struct qdio_irq *irq_ptr)
129 {
130 	if (!is_thinint_irq(irq_ptr))
131 		return 0;
132 	if (shared_ind(irq_ptr))
133 		return 0;
134 	if (*irq_ptr->dsci)
135 		return 1;
136 	else
137 		return 0;
138 }
139 
140 static inline u32 clear_shared_ind(void)
141 {
142 	if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count))
143 		return 0;
144 	return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
145 }
146 
147 static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq)
148 {
149 	struct qdio_q *q;
150 	int i;
151 
152 	if (!references_shared_dsci(irq) &&
153 	    has_multiple_inq_on_dsci(irq))
154 		xchg(irq->dsci, 0);
155 
156 	for_each_input_queue(irq, q, i) {
157 		if (q->u.in.queue_start_poll) {
158 			/* skip if polling is enabled or already in work */
159 			if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
160 					     &q->u.in.queue_irq_state)) {
161 				qperf_inc(q, int_discarded);
162 				continue;
163 			}
164 
165 			/* avoid dsci clear here, done after processing */
166 			q->u.in.queue_start_poll(irq->cdev, q->nr,
167 						 irq->int_parm);
168 		} else {
169 			if (!shared_ind(irq))
170 				xchg(irq->dsci, 0);
171 
172 			/*
173 			 * Call inbound processing but not directly
174 			 * since that could starve other thinint queues.
175 			 */
176 			tasklet_schedule(&q->tasklet);
177 		}
178 	}
179 }
180 
181 /**
182  * tiqdio_thinint_handler - thin interrupt handler for qdio
183  * @airq: pointer to adapter interrupt descriptor
184  */
185 static void tiqdio_thinint_handler(struct airq_struct *airq)
186 {
187 	u32 si_used = clear_shared_ind();
188 	struct qdio_q *q;
189 
190 	last_ai_time = S390_lowcore.int_clock;
191 	inc_irq_stat(IRQIO_QAI);
192 
193 	/* protect tiq_list entries, only changed in activate or shutdown */
194 	rcu_read_lock();
195 
196 	/* check for work on all inbound thinint queues */
197 	list_for_each_entry_rcu(q, &tiq_list, entry) {
198 		struct qdio_irq *irq;
199 
200 		/* only process queues from changed sets */
201 		irq = q->irq_ptr;
202 		if (unlikely(references_shared_dsci(irq))) {
203 			if (!si_used)
204 				continue;
205 		} else if (!*irq->dsci)
206 			continue;
207 
208 		tiqdio_call_inq_handlers(irq);
209 
210 		qperf_inc(q, adapter_int);
211 	}
212 	rcu_read_unlock();
213 }
214 
215 static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
216 {
217 	struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page;
218 	u64 summary_indicator_addr, subchannel_indicator_addr;
219 	int rc;
220 
221 	if (reset) {
222 		summary_indicator_addr = 0;
223 		subchannel_indicator_addr = 0;
224 	} else {
225 		summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr);
226 		subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci);
227 	}
228 
229 	rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr,
230 		       subchannel_indicator_addr);
231 	if (rc) {
232 		DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no,
233 			  scssc->response.code);
234 		goto out;
235 	}
236 
237 	DBF_EVENT("setscind");
238 	DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr));
239 	DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr));
240 out:
241 	return rc;
242 }
243 
244 /* allocate non-shared indicators and shared indicator */
245 int __init tiqdio_allocate_memory(void)
246 {
247 	q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS,
248 			     GFP_KERNEL);
249 	if (!q_indicators)
250 		return -ENOMEM;
251 	return 0;
252 }
253 
254 void tiqdio_free_memory(void)
255 {
256 	kfree(q_indicators);
257 }
258 
259 int __init tiqdio_register_thinints(void)
260 {
261 	int rc;
262 
263 	rc = register_adapter_interrupt(&tiqdio_airq);
264 	if (rc) {
265 		DBF_EVENT("RTI:%x", rc);
266 		return rc;
267 	}
268 	return 0;
269 }
270 
271 int qdio_establish_thinint(struct qdio_irq *irq_ptr)
272 {
273 	if (!is_thinint_irq(irq_ptr))
274 		return 0;
275 	return set_subchannel_ind(irq_ptr, 0);
276 }
277 
278 void qdio_setup_thinint(struct qdio_irq *irq_ptr)
279 {
280 	if (!is_thinint_irq(irq_ptr))
281 		return;
282 	irq_ptr->dsci = get_indicator();
283 	DBF_HEX(&irq_ptr->dsci, sizeof(void *));
284 }
285 
286 void qdio_shutdown_thinint(struct qdio_irq *irq_ptr)
287 {
288 	if (!is_thinint_irq(irq_ptr))
289 		return;
290 
291 	/* reset adapter interrupt indicators */
292 	set_subchannel_ind(irq_ptr, 1);
293 	put_indicator(irq_ptr->dsci);
294 }
295 
296 void __exit tiqdio_unregister_thinints(void)
297 {
298 	WARN_ON(!list_empty(&tiq_list));
299 	unregister_adapter_interrupt(&tiqdio_airq);
300 }
301