• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 interrupt definitions */
40 static void tiqdio_thinint_handler(struct airq_struct *airq);
41 
42 static struct airq_struct tiqdio_airq = {
43 	.handler = tiqdio_thinint_handler,
44 	.isc = QDIO_AIRQ_ISC,
45 };
46 
47 static struct indicator_t *q_indicators;
48 
49 u64 last_ai_time;
50 
51 /* returns addr for the device state change indicator */
get_indicator(void)52 static u32 *get_indicator(void)
53 {
54 	int i;
55 
56 	for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
57 		if (!atomic_read(&q_indicators[i].count)) {
58 			atomic_set(&q_indicators[i].count, 1);
59 			return &q_indicators[i].ind;
60 		}
61 
62 	/* use the shared indicator */
63 	atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
64 	return &q_indicators[TIQDIO_SHARED_IND].ind;
65 }
66 
put_indicator(u32 * addr)67 static void put_indicator(u32 *addr)
68 {
69 	int i;
70 
71 	if (!addr)
72 		return;
73 	i = ((unsigned long)addr - (unsigned long)q_indicators) /
74 		sizeof(struct indicator_t);
75 	atomic_dec(&q_indicators[i].count);
76 }
77 
tiqdio_add_input_queues(struct qdio_irq * irq_ptr)78 void tiqdio_add_input_queues(struct qdio_irq *irq_ptr)
79 {
80 	mutex_lock(&tiq_list_lock);
81 	list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list);
82 	mutex_unlock(&tiq_list_lock);
83 }
84 
tiqdio_remove_input_queues(struct qdio_irq * irq_ptr)85 void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr)
86 {
87 	struct qdio_q *q;
88 
89 	q = irq_ptr->input_qs[0];
90 	if (!q)
91 		return;
92 
93 	mutex_lock(&tiq_list_lock);
94 	list_del_rcu(&q->entry);
95 	mutex_unlock(&tiq_list_lock);
96 	synchronize_rcu();
97 	INIT_LIST_HEAD(&q->entry);
98 }
99 
has_multiple_inq_on_dsci(struct qdio_irq * irq_ptr)100 static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr)
101 {
102 	return irq_ptr->nr_input_qs > 1;
103 }
104 
references_shared_dsci(struct qdio_irq * irq_ptr)105 static inline int references_shared_dsci(struct qdio_irq *irq_ptr)
106 {
107 	return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
108 }
109 
shared_ind(struct qdio_irq * irq_ptr)110 static inline int shared_ind(struct qdio_irq *irq_ptr)
111 {
112 	return references_shared_dsci(irq_ptr) ||
113 		has_multiple_inq_on_dsci(irq_ptr);
114 }
115 
clear_nonshared_ind(struct qdio_irq * irq_ptr)116 void clear_nonshared_ind(struct qdio_irq *irq_ptr)
117 {
118 	if (!is_thinint_irq(irq_ptr))
119 		return;
120 	if (shared_ind(irq_ptr))
121 		return;
122 	xchg(irq_ptr->dsci, 0);
123 }
124 
test_nonshared_ind(struct qdio_irq * irq_ptr)125 int test_nonshared_ind(struct qdio_irq *irq_ptr)
126 {
127 	if (!is_thinint_irq(irq_ptr))
128 		return 0;
129 	if (shared_ind(irq_ptr))
130 		return 0;
131 	if (*irq_ptr->dsci)
132 		return 1;
133 	else
134 		return 0;
135 }
136 
clear_shared_ind(void)137 static inline u32 clear_shared_ind(void)
138 {
139 	if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count))
140 		return 0;
141 	return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
142 }
143 
tiqdio_call_inq_handlers(struct qdio_irq * irq)144 static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq)
145 {
146 	struct qdio_q *q;
147 	int i;
148 
149 	if (!references_shared_dsci(irq) &&
150 	    has_multiple_inq_on_dsci(irq))
151 		xchg(irq->dsci, 0);
152 
153 	for_each_input_queue(irq, q, i) {
154 		if (q->u.in.queue_start_poll) {
155 			/* skip if polling is enabled or already in work */
156 			if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
157 					     &q->u.in.queue_irq_state)) {
158 				qperf_inc(q, int_discarded);
159 				continue;
160 			}
161 
162 			/* avoid dsci clear here, done after processing */
163 			q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
164 						 q->irq_ptr->int_parm);
165 		} else {
166 			if (!shared_ind(q->irq_ptr))
167 				xchg(q->irq_ptr->dsci, 0);
168 
169 			/*
170 			 * Call inbound processing but not directly
171 			 * since that could starve other thinint queues.
172 			 */
173 			tasklet_schedule(&q->tasklet);
174 		}
175 	}
176 }
177 
178 /**
179  * tiqdio_thinint_handler - thin interrupt handler for qdio
180  * @alsi: pointer to adapter local summary indicator
181  * @data: NULL
182  */
tiqdio_thinint_handler(struct airq_struct * airq)183 static void tiqdio_thinint_handler(struct airq_struct *airq)
184 {
185 	u32 si_used = clear_shared_ind();
186 	struct qdio_q *q;
187 
188 	last_ai_time = S390_lowcore.int_clock;
189 	inc_irq_stat(IRQIO_QAI);
190 
191 	/* protect tiq_list entries, only changed in activate or shutdown */
192 	rcu_read_lock();
193 
194 	/* check for work on all inbound thinint queues */
195 	list_for_each_entry_rcu(q, &tiq_list, entry) {
196 		struct qdio_irq *irq;
197 
198 		/* only process queues from changed sets */
199 		irq = q->irq_ptr;
200 		if (unlikely(references_shared_dsci(irq))) {
201 			if (!si_used)
202 				continue;
203 		} else if (!*irq->dsci)
204 			continue;
205 
206 		tiqdio_call_inq_handlers(irq);
207 
208 		qperf_inc(q, adapter_int);
209 	}
210 	rcu_read_unlock();
211 }
212 
set_subchannel_ind(struct qdio_irq * irq_ptr,int reset)213 static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
214 {
215 	struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page;
216 	u64 summary_indicator_addr, subchannel_indicator_addr;
217 	int rc;
218 
219 	if (reset) {
220 		summary_indicator_addr = 0;
221 		subchannel_indicator_addr = 0;
222 	} else {
223 		summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr);
224 		subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci);
225 	}
226 
227 	rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr,
228 		       subchannel_indicator_addr);
229 	if (rc) {
230 		DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no,
231 			  scssc->response.code);
232 		goto out;
233 	}
234 
235 	DBF_EVENT("setscind");
236 	DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr));
237 	DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr));
238 out:
239 	return rc;
240 }
241 
242 /* allocate non-shared indicators and shared indicator */
tiqdio_allocate_memory(void)243 int __init tiqdio_allocate_memory(void)
244 {
245 	q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS,
246 			     GFP_KERNEL);
247 	if (!q_indicators)
248 		return -ENOMEM;
249 	return 0;
250 }
251 
tiqdio_free_memory(void)252 void tiqdio_free_memory(void)
253 {
254 	kfree(q_indicators);
255 }
256 
tiqdio_register_thinints(void)257 int __init tiqdio_register_thinints(void)
258 {
259 	int rc;
260 
261 	rc = register_adapter_interrupt(&tiqdio_airq);
262 	if (rc) {
263 		DBF_EVENT("RTI:%x", rc);
264 		return rc;
265 	}
266 	return 0;
267 }
268 
qdio_establish_thinint(struct qdio_irq * irq_ptr)269 int qdio_establish_thinint(struct qdio_irq *irq_ptr)
270 {
271 	int rc;
272 
273 	if (!is_thinint_irq(irq_ptr))
274 		return 0;
275 
276 	irq_ptr->dsci = get_indicator();
277 	DBF_HEX(&irq_ptr->dsci, sizeof(void *));
278 
279 	rc = set_subchannel_ind(irq_ptr, 0);
280 	if (rc)
281 		put_indicator(irq_ptr->dsci);
282 
283 	return rc;
284 }
285 
qdio_shutdown_thinint(struct qdio_irq * irq_ptr)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 
tiqdio_unregister_thinints(void)296 void __exit tiqdio_unregister_thinints(void)
297 {
298 	WARN_ON(!list_empty(&tiq_list));
299 	unregister_adapter_interrupt(&tiqdio_airq);
300 }
301