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 */ 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 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 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 xchg(irq_ptr->dsci, 1 << 7); 83 } 84 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 establish triggered an error */ 91 if (!q || !q->entry.prev || !q->entry.next) 92 return; 93 94 mutex_lock(&tiq_list_lock); 95 list_del_rcu(&q->entry); 96 mutex_unlock(&tiq_list_lock); 97 synchronize_rcu(); 98 } 99 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 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 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 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 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 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 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(irq->cdev, q->nr, 164 irq->int_parm); 165 } else { 166 if (!shared_ind(irq)) 167 xchg(irq->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 * @airq: pointer to adapter interrupt descriptor 181 */ 182 static void tiqdio_thinint_handler(struct airq_struct *airq) 183 { 184 u32 si_used = clear_shared_ind(); 185 struct qdio_q *q; 186 187 last_ai_time = S390_lowcore.int_clock; 188 inc_irq_stat(IRQIO_QAI); 189 190 /* protect tiq_list entries, only changed in activate or shutdown */ 191 rcu_read_lock(); 192 193 /* check for work on all inbound thinint queues */ 194 list_for_each_entry_rcu(q, &tiq_list, entry) { 195 struct qdio_irq *irq; 196 197 /* only process queues from changed sets */ 198 irq = q->irq_ptr; 199 if (unlikely(references_shared_dsci(irq))) { 200 if (!si_used) 201 continue; 202 } else if (!*irq->dsci) 203 continue; 204 205 tiqdio_call_inq_handlers(irq); 206 207 qperf_inc(q, adapter_int); 208 } 209 rcu_read_unlock(); 210 } 211 212 static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset) 213 { 214 struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page; 215 u64 summary_indicator_addr, subchannel_indicator_addr; 216 int rc; 217 218 if (reset) { 219 summary_indicator_addr = 0; 220 subchannel_indicator_addr = 0; 221 } else { 222 summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr); 223 subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci); 224 } 225 226 rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr, 227 subchannel_indicator_addr); 228 if (rc) { 229 DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no, 230 scssc->response.code); 231 goto out; 232 } 233 234 DBF_EVENT("setscind"); 235 DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr)); 236 DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr)); 237 out: 238 return rc; 239 } 240 241 /* allocate non-shared indicators and shared indicator */ 242 int __init tiqdio_allocate_memory(void) 243 { 244 q_indicators = kcalloc(TIQDIO_NR_INDICATORS, 245 sizeof(struct indicator_t), 246 GFP_KERNEL); 247 if (!q_indicators) 248 return -ENOMEM; 249 return 0; 250 } 251 252 void tiqdio_free_memory(void) 253 { 254 kfree(q_indicators); 255 } 256 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 269 int qdio_establish_thinint(struct qdio_irq *irq_ptr) 270 { 271 if (!is_thinint_irq(irq_ptr)) 272 return 0; 273 return set_subchannel_ind(irq_ptr, 0); 274 } 275 276 void qdio_setup_thinint(struct qdio_irq *irq_ptr) 277 { 278 if (!is_thinint_irq(irq_ptr)) 279 return; 280 irq_ptr->dsci = get_indicator(); 281 DBF_HEX(&irq_ptr->dsci, sizeof(void *)); 282 } 283 284 void qdio_shutdown_thinint(struct qdio_irq *irq_ptr) 285 { 286 if (!is_thinint_irq(irq_ptr)) 287 return; 288 289 /* reset adapter interrupt indicators */ 290 set_subchannel_ind(irq_ptr, 1); 291 put_indicator(irq_ptr->dsci); 292 } 293 294 void __exit tiqdio_unregister_thinints(void) 295 { 296 WARN_ON(!list_empty(&tiq_list)); 297 unregister_adapter_interrupt(&tiqdio_airq); 298 } 299