1 // SPDX-License-Identifier: GPL-2.0
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>
9 #include <linux/slab.h>
10 #include <linux/kernel_stat.h>
11 #include <linux/atomic.h>
12 #include <linux/rculist.h>
14 #include <asm/debug.h>
22 #include "qdio_debug.h"
25 * Restriction: only 63 iqdio subchannels would have its own indicator,
26 * after that, subsequent subchannels share one indicator
28 #define TIQDIO_NR_NONSHARED_IND 63
29 #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1)
30 #define TIQDIO_SHARED_IND 63
32 /* device state change indicators */
34 u32 ind
; /* u32 because of compare-and-swap performance */
35 atomic_t count
; /* use count, 0 or 1 for non-shared indicators */
38 /* list of thin interrupt input queues */
39 static LIST_HEAD(tiq_list
);
40 static DEFINE_MUTEX(tiq_list_lock
);
42 static struct indicator_t
*q_indicators
;
46 /* returns addr for the device state change indicator */
47 static u32
*get_indicator(void)
51 for (i
= 0; i
< TIQDIO_NR_NONSHARED_IND
; i
++)
52 if (!atomic_cmpxchg(&q_indicators
[i
].count
, 0, 1))
53 return &q_indicators
[i
].ind
;
55 /* use the shared indicator */
56 atomic_inc(&q_indicators
[TIQDIO_SHARED_IND
].count
);
57 return &q_indicators
[TIQDIO_SHARED_IND
].ind
;
60 static void put_indicator(u32
*addr
)
62 struct indicator_t
*ind
= container_of(addr
, struct indicator_t
, ind
);
66 atomic_dec(&ind
->count
);
69 void tiqdio_add_device(struct qdio_irq
*irq_ptr
)
71 mutex_lock(&tiq_list_lock
);
72 list_add_rcu(&irq_ptr
->entry
, &tiq_list
);
73 mutex_unlock(&tiq_list_lock
);
76 void tiqdio_remove_device(struct qdio_irq
*irq_ptr
)
78 mutex_lock(&tiq_list_lock
);
79 list_del_rcu(&irq_ptr
->entry
);
80 mutex_unlock(&tiq_list_lock
);
82 INIT_LIST_HEAD(&irq_ptr
->entry
);
85 static inline int references_shared_dsci(struct qdio_irq
*irq_ptr
)
87 return irq_ptr
->dsci
== &q_indicators
[TIQDIO_SHARED_IND
].ind
;
90 int test_nonshared_ind(struct qdio_irq
*irq_ptr
)
92 if (!is_thinint_irq(irq_ptr
))
94 if (references_shared_dsci(irq_ptr
))
102 static inline u32
clear_shared_ind(void)
104 if (!atomic_read(&q_indicators
[TIQDIO_SHARED_IND
].count
))
106 return xchg(&q_indicators
[TIQDIO_SHARED_IND
].ind
, 0);
109 static inline void tiqdio_call_inq_handlers(struct qdio_irq
*irq
)
114 if (!references_shared_dsci(irq
))
118 if (!test_and_set_bit(QDIO_IRQ_DISABLED
, &irq
->poll_state
))
119 irq
->irq_poll(irq
->cdev
, irq
->int_parm
);
121 QDIO_PERF_STAT_INC(irq
, int_discarded
);
126 for_each_input_queue(irq
, q
, i
) {
128 * Call inbound processing but not directly
129 * since that could starve other thinint queues.
131 tasklet_schedule(&q
->tasklet
);
136 * tiqdio_thinint_handler - thin interrupt handler for qdio
137 * @airq: pointer to adapter interrupt descriptor
138 * @floating: flag to recognize floating vs. directed interrupts (unused)
140 static void tiqdio_thinint_handler(struct airq_struct
*airq
, bool floating
)
142 u32 si_used
= clear_shared_ind();
143 struct qdio_irq
*irq
;
145 last_ai_time
= S390_lowcore
.int_clock
;
146 inc_irq_stat(IRQIO_QAI
);
148 /* protect tiq_list entries, only changed in activate or shutdown */
151 list_for_each_entry_rcu(irq
, &tiq_list
, entry
) {
152 /* only process queues from changed sets */
153 if (unlikely(references_shared_dsci(irq
))) {
156 } else if (!*irq
->dsci
)
159 tiqdio_call_inq_handlers(irq
);
161 QDIO_PERF_STAT_INC(irq
, adapter_int
);
166 static struct airq_struct tiqdio_airq
= {
167 .handler
= tiqdio_thinint_handler
,
168 .isc
= QDIO_AIRQ_ISC
,
171 static int set_subchannel_ind(struct qdio_irq
*irq_ptr
, int reset
)
173 struct chsc_scssc_area
*scssc
= (void *)irq_ptr
->chsc_page
;
174 u64 summary_indicator_addr
, subchannel_indicator_addr
;
178 summary_indicator_addr
= 0;
179 subchannel_indicator_addr
= 0;
181 summary_indicator_addr
= virt_to_phys(tiqdio_airq
.lsi_ptr
);
182 subchannel_indicator_addr
= virt_to_phys(irq_ptr
->dsci
);
185 rc
= chsc_sadc(irq_ptr
->schid
, scssc
, summary_indicator_addr
,
186 subchannel_indicator_addr
, tiqdio_airq
.isc
);
188 DBF_ERROR("%4x SSI r:%4x", irq_ptr
->schid
.sch_no
,
189 scssc
->response
.code
);
193 DBF_EVENT("setscind");
194 DBF_HEX(&summary_indicator_addr
, sizeof(summary_indicator_addr
));
195 DBF_HEX(&subchannel_indicator_addr
, sizeof(subchannel_indicator_addr
));
200 /* allocate non-shared indicators and shared indicator */
201 int __init
tiqdio_allocate_memory(void)
203 q_indicators
= kcalloc(TIQDIO_NR_INDICATORS
,
204 sizeof(struct indicator_t
),
211 void tiqdio_free_memory(void)
216 int __init
tiqdio_register_thinints(void)
220 rc
= register_adapter_interrupt(&tiqdio_airq
);
222 DBF_EVENT("RTI:%x", rc
);
228 int qdio_establish_thinint(struct qdio_irq
*irq_ptr
)
230 if (!is_thinint_irq(irq_ptr
))
232 return set_subchannel_ind(irq_ptr
, 0);
235 void qdio_setup_thinint(struct qdio_irq
*irq_ptr
)
237 if (!is_thinint_irq(irq_ptr
))
239 irq_ptr
->dsci
= get_indicator();
240 DBF_HEX(&irq_ptr
->dsci
, sizeof(void *));
243 void qdio_shutdown_thinint(struct qdio_irq
*irq_ptr
)
245 if (!is_thinint_irq(irq_ptr
))
248 /* reset adapter interrupt indicators */
249 set_subchannel_ind(irq_ptr
, 1);
250 put_indicator(irq_ptr
->dsci
);
253 void __exit
tiqdio_unregister_thinints(void)
255 WARN_ON(!list_empty(&tiq_list
));
256 unregister_adapter_interrupt(&tiqdio_airq
);