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