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