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