1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2779e6e1cSJan Glauber /* 3a53c8fabSHeiko Carstens * Copyright IBM Corp. 2000, 2009 4779e6e1cSJan Glauber * Author(s): Utz Bacher <utz.bacher@de.ibm.com> 5779e6e1cSJan Glauber * Cornelia Huck <cornelia.huck@de.ibm.com> 6779e6e1cSJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 7779e6e1cSJan Glauber */ 8779e6e1cSJan Glauber #include <linux/io.h> 95a0e3ad6STejun Heo #include <linux/slab.h> 1030d77c3eSJan Glauber #include <linux/kernel_stat.h> 1160063497SArun Sharma #include <linux/atomic.h> 12b2d09103SIngo Molnar #include <linux/rculist.h> 13b2d09103SIngo Molnar 14779e6e1cSJan Glauber #include <asm/debug.h> 15779e6e1cSJan Glauber #include <asm/qdio.h> 16779e6e1cSJan Glauber #include <asm/airq.h> 17779e6e1cSJan Glauber #include <asm/isc.h> 18779e6e1cSJan Glauber 19779e6e1cSJan Glauber #include "cio.h" 20779e6e1cSJan Glauber #include "ioasm.h" 21779e6e1cSJan Glauber #include "qdio.h" 22779e6e1cSJan Glauber #include "qdio_debug.h" 23779e6e1cSJan Glauber 24779e6e1cSJan Glauber /* 25779e6e1cSJan Glauber * Restriction: only 63 iqdio subchannels would have its own indicator, 26779e6e1cSJan Glauber * after that, subsequent subchannels share one indicator 27779e6e1cSJan Glauber */ 28779e6e1cSJan Glauber #define TIQDIO_NR_NONSHARED_IND 63 29779e6e1cSJan Glauber #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1) 305f4026f8SJan Glauber #define TIQDIO_SHARED_IND 63 315f4026f8SJan Glauber 325f4026f8SJan Glauber /* device state change indicators */ 335f4026f8SJan Glauber struct indicator_t { 345f4026f8SJan Glauber u32 ind; /* u32 because of compare-and-swap performance */ 355f4026f8SJan Glauber atomic_t count; /* use count, 0 or 1 for non-shared indicators */ 365f4026f8SJan Glauber }; 37779e6e1cSJan Glauber 38779e6e1cSJan Glauber /* list of thin interrupt input queues */ 39779e6e1cSJan Glauber static LIST_HEAD(tiq_list); 40c4736d96SMartin Schwidefsky static DEFINE_MUTEX(tiq_list_lock); 41779e6e1cSJan Glauber 42f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 43f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq); 44f4eae94fSMartin Schwidefsky 45f4eae94fSMartin Schwidefsky static struct airq_struct tiqdio_airq = { 46f4eae94fSMartin Schwidefsky .handler = tiqdio_thinint_handler, 47f4eae94fSMartin Schwidefsky .isc = QDIO_AIRQ_ISC, 48f4eae94fSMartin Schwidefsky }; 49779e6e1cSJan Glauber 505f4026f8SJan Glauber static struct indicator_t *q_indicators; 51779e6e1cSJan Glauber 52a2b86019SJan Glauber u64 last_ai_time; 53d36deae7SJan Glauber 54779e6e1cSJan Glauber /* returns addr for the device state change indicator */ 55779e6e1cSJan Glauber static u32 *get_indicator(void) 56779e6e1cSJan Glauber { 57779e6e1cSJan Glauber int i; 58779e6e1cSJan Glauber 59779e6e1cSJan Glauber for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++) 60648a6f44SSebastian Ott if (!atomic_cmpxchg(&q_indicators[i].count, 0, 1)) 61779e6e1cSJan Glauber return &q_indicators[i].ind; 62779e6e1cSJan Glauber 63779e6e1cSJan Glauber /* use the shared indicator */ 64779e6e1cSJan Glauber atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count); 65779e6e1cSJan Glauber return &q_indicators[TIQDIO_SHARED_IND].ind; 66779e6e1cSJan Glauber } 67779e6e1cSJan Glauber 68779e6e1cSJan Glauber static void put_indicator(u32 *addr) 69779e6e1cSJan Glauber { 7030e8eb86SSebastian Ott struct indicator_t *ind = container_of(addr, struct indicator_t, ind); 71779e6e1cSJan Glauber 72779e6e1cSJan Glauber if (!addr) 73779e6e1cSJan Glauber return; 7430e8eb86SSebastian Ott atomic_dec(&ind->count); 75779e6e1cSJan Glauber } 76779e6e1cSJan Glauber 77779e6e1cSJan Glauber void tiqdio_add_input_queues(struct qdio_irq *irq_ptr) 78779e6e1cSJan Glauber { 79b4547402SJan Glauber mutex_lock(&tiq_list_lock); 80104ea556Sfrank.blaschka@de.ibm.com list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list); 81b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 82d0c9d4a8SJan Glauber xchg(irq_ptr->dsci, 1 << 7); 83779e6e1cSJan Glauber } 84779e6e1cSJan Glauber 85779e6e1cSJan Glauber void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr) 86779e6e1cSJan Glauber { 87779e6e1cSJan Glauber struct qdio_q *q; 88779e6e1cSJan Glauber 89104ea556Sfrank.blaschka@de.ibm.com q = irq_ptr->input_qs[0]; 9053b41ba7SJan Glauber /* if establish triggered an error */ 9153b41ba7SJan Glauber if (!q || !q->entry.prev || !q->entry.next) 92104ea556Sfrank.blaschka@de.ibm.com return; 93b4547402SJan Glauber 94b4547402SJan Glauber mutex_lock(&tiq_list_lock); 95779e6e1cSJan Glauber list_del_rcu(&q->entry); 96b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 97779e6e1cSJan Glauber synchronize_rcu(); 98779e6e1cSJan Glauber } 99779e6e1cSJan Glauber 1005f4026f8SJan Glauber static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr) 1015f4026f8SJan Glauber { 1025f4026f8SJan Glauber return irq_ptr->nr_input_qs > 1; 1035f4026f8SJan Glauber } 1045f4026f8SJan Glauber 1055f4026f8SJan Glauber static inline int references_shared_dsci(struct qdio_irq *irq_ptr) 1065f4026f8SJan Glauber { 1075f4026f8SJan Glauber return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind; 1085f4026f8SJan Glauber } 1095f4026f8SJan Glauber 1105f4026f8SJan Glauber static inline int shared_ind(struct qdio_irq *irq_ptr) 1115f4026f8SJan Glauber { 1125f4026f8SJan Glauber return references_shared_dsci(irq_ptr) || 1135f4026f8SJan Glauber has_multiple_inq_on_dsci(irq_ptr); 1145f4026f8SJan Glauber } 1155f4026f8SJan Glauber 1165f4026f8SJan Glauber void clear_nonshared_ind(struct qdio_irq *irq_ptr) 1175f4026f8SJan Glauber { 1185f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1195f4026f8SJan Glauber return; 1205f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1215f4026f8SJan Glauber return; 1225f4026f8SJan Glauber xchg(irq_ptr->dsci, 0); 1235f4026f8SJan Glauber } 1245f4026f8SJan Glauber 1255f4026f8SJan Glauber int test_nonshared_ind(struct qdio_irq *irq_ptr) 1265f4026f8SJan Glauber { 1275f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1285f4026f8SJan Glauber return 0; 1295f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1305f4026f8SJan Glauber return 0; 1315f4026f8SJan Glauber if (*irq_ptr->dsci) 1325f4026f8SJan Glauber return 1; 1335f4026f8SJan Glauber else 1345f4026f8SJan Glauber return 0; 1355f4026f8SJan Glauber } 1365f4026f8SJan Glauber 137b02f0c2eSJan Glauber static inline u32 clear_shared_ind(void) 138779e6e1cSJan Glauber { 139b02f0c2eSJan Glauber if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count)) 140b02f0c2eSJan Glauber return 0; 141b02f0c2eSJan Glauber return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0); 142779e6e1cSJan Glauber } 143779e6e1cSJan Glauber 144104ea556Sfrank.blaschka@de.ibm.com static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq) 145104ea556Sfrank.blaschka@de.ibm.com { 146104ea556Sfrank.blaschka@de.ibm.com struct qdio_q *q; 147104ea556Sfrank.blaschka@de.ibm.com int i; 148104ea556Sfrank.blaschka@de.ibm.com 149104ea556Sfrank.blaschka@de.ibm.com if (!references_shared_dsci(irq) && 150104ea556Sfrank.blaschka@de.ibm.com has_multiple_inq_on_dsci(irq)) 1511e4a382fSJulian Wiedmann xchg(irq->dsci, 0); 152104ea556Sfrank.blaschka@de.ibm.com 1531e4a382fSJulian Wiedmann for_each_input_queue(irq, q, i) { 154104ea556Sfrank.blaschka@de.ibm.com if (q->u.in.queue_start_poll) { 155104ea556Sfrank.blaschka@de.ibm.com /* skip if polling is enabled or already in work */ 156104ea556Sfrank.blaschka@de.ibm.com if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED, 157104ea556Sfrank.blaschka@de.ibm.com &q->u.in.queue_irq_state)) { 158104ea556Sfrank.blaschka@de.ibm.com qperf_inc(q, int_discarded); 159104ea556Sfrank.blaschka@de.ibm.com continue; 160104ea556Sfrank.blaschka@de.ibm.com } 161104ea556Sfrank.blaschka@de.ibm.com 162104ea556Sfrank.blaschka@de.ibm.com /* avoid dsci clear here, done after processing */ 16367bb323bSJulian Wiedmann q->u.in.queue_start_poll(irq->cdev, q->nr, 16467bb323bSJulian Wiedmann irq->int_parm); 165104ea556Sfrank.blaschka@de.ibm.com } else { 16667bb323bSJulian Wiedmann if (!shared_ind(irq)) 16767bb323bSJulian Wiedmann xchg(irq->dsci, 0); 168104ea556Sfrank.blaschka@de.ibm.com 169104ea556Sfrank.blaschka@de.ibm.com /* 170104ea556Sfrank.blaschka@de.ibm.com * Call inbound processing but not directly 171104ea556Sfrank.blaschka@de.ibm.com * since that could starve other thinint queues. 172104ea556Sfrank.blaschka@de.ibm.com */ 173104ea556Sfrank.blaschka@de.ibm.com tasklet_schedule(&q->tasklet); 174104ea556Sfrank.blaschka@de.ibm.com } 175104ea556Sfrank.blaschka@de.ibm.com } 176104ea556Sfrank.blaschka@de.ibm.com } 177104ea556Sfrank.blaschka@de.ibm.com 178cf9a031cSJan Glauber /** 179cf9a031cSJan Glauber * tiqdio_thinint_handler - thin interrupt handler for qdio 18072a01d0bSJulian Wiedmann * @airq: pointer to adapter interrupt descriptor 181cf9a031cSJan Glauber */ 182f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq) 183779e6e1cSJan Glauber { 184b02f0c2eSJan Glauber u32 si_used = clear_shared_ind(); 185779e6e1cSJan Glauber struct qdio_q *q; 186779e6e1cSJan Glauber 187d36deae7SJan Glauber last_ai_time = S390_lowcore.int_clock; 188420f42ecSHeiko Carstens inc_irq_stat(IRQIO_QAI); 189d36deae7SJan Glauber 190779e6e1cSJan Glauber /* protect tiq_list entries, only changed in activate or shutdown */ 191779e6e1cSJan Glauber rcu_read_lock(); 192779e6e1cSJan Glauber 193cf9a031cSJan Glauber /* check for work on all inbound thinint queues */ 194d36deae7SJan Glauber list_for_each_entry_rcu(q, &tiq_list, entry) { 195104ea556Sfrank.blaschka@de.ibm.com struct qdio_irq *irq; 196779e6e1cSJan Glauber 197d36deae7SJan Glauber /* only process queues from changed sets */ 198104ea556Sfrank.blaschka@de.ibm.com irq = q->irq_ptr; 199104ea556Sfrank.blaschka@de.ibm.com if (unlikely(references_shared_dsci(irq))) { 2004f325184SJan Glauber if (!si_used) 2014f325184SJan Glauber continue; 202104ea556Sfrank.blaschka@de.ibm.com } else if (!*irq->dsci) 203d36deae7SJan Glauber continue; 204d36deae7SJan Glauber 205104ea556Sfrank.blaschka@de.ibm.com tiqdio_call_inq_handlers(irq); 206d36deae7SJan Glauber 207d36deae7SJan Glauber qperf_inc(q, adapter_int); 208d36deae7SJan Glauber } 209779e6e1cSJan Glauber rcu_read_unlock(); 210779e6e1cSJan Glauber } 211779e6e1cSJan Glauber 212779e6e1cSJan Glauber static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset) 213779e6e1cSJan Glauber { 214ca4ba153SSebastian Ott struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page; 215ca4ba153SSebastian Ott u64 summary_indicator_addr, subchannel_indicator_addr; 216779e6e1cSJan Glauber int rc; 217779e6e1cSJan Glauber 218779e6e1cSJan Glauber if (reset) { 219ca4ba153SSebastian Ott summary_indicator_addr = 0; 220ca4ba153SSebastian Ott subchannel_indicator_addr = 0; 221779e6e1cSJan Glauber } else { 222f4eae94fSMartin Schwidefsky summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr); 223ca4ba153SSebastian Ott subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci); 224779e6e1cSJan Glauber } 225779e6e1cSJan Glauber 226ca4ba153SSebastian Ott rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr, 227ca4ba153SSebastian Ott subchannel_indicator_addr); 228779e6e1cSJan Glauber if (rc) { 22922f99347SJan Glauber DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no, 230ca4ba153SSebastian Ott scssc->response.code); 231ca4ba153SSebastian Ott goto out; 232779e6e1cSJan Glauber } 233779e6e1cSJan Glauber 23422f99347SJan Glauber DBF_EVENT("setscind"); 235ca4ba153SSebastian Ott DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr)); 236ca4ba153SSebastian Ott DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr)); 237ca4ba153SSebastian Ott out: 238ca4ba153SSebastian Ott return rc; 239779e6e1cSJan Glauber } 240779e6e1cSJan Glauber 241779e6e1cSJan Glauber /* allocate non-shared indicators and shared indicator */ 242779e6e1cSJan Glauber int __init tiqdio_allocate_memory(void) 243779e6e1cSJan Glauber { 244*6396bb22SKees Cook q_indicators = kcalloc(TIQDIO_NR_INDICATORS, 245*6396bb22SKees Cook sizeof(struct indicator_t), 246779e6e1cSJan Glauber GFP_KERNEL); 247779e6e1cSJan Glauber if (!q_indicators) 248779e6e1cSJan Glauber return -ENOMEM; 249779e6e1cSJan Glauber return 0; 250779e6e1cSJan Glauber } 251779e6e1cSJan Glauber 252779e6e1cSJan Glauber void tiqdio_free_memory(void) 253779e6e1cSJan Glauber { 254779e6e1cSJan Glauber kfree(q_indicators); 255779e6e1cSJan Glauber } 256779e6e1cSJan Glauber 257779e6e1cSJan Glauber int __init tiqdio_register_thinints(void) 258779e6e1cSJan Glauber { 259f4eae94fSMartin Schwidefsky int rc; 260f4eae94fSMartin Schwidefsky 261f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&tiqdio_airq); 262f4eae94fSMartin Schwidefsky if (rc) { 263f4eae94fSMartin Schwidefsky DBF_EVENT("RTI:%x", rc); 264f4eae94fSMartin Schwidefsky return rc; 265779e6e1cSJan Glauber } 266779e6e1cSJan Glauber return 0; 267779e6e1cSJan Glauber } 268779e6e1cSJan Glauber 269779e6e1cSJan Glauber int qdio_establish_thinint(struct qdio_irq *irq_ptr) 270779e6e1cSJan Glauber { 271779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 272779e6e1cSJan Glauber return 0; 273779e6e1cSJan Glauber return set_subchannel_ind(irq_ptr, 0); 274779e6e1cSJan Glauber } 275779e6e1cSJan Glauber 276779e6e1cSJan Glauber void qdio_setup_thinint(struct qdio_irq *irq_ptr) 277779e6e1cSJan Glauber { 278779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 279779e6e1cSJan Glauber return; 280779e6e1cSJan Glauber irq_ptr->dsci = get_indicator(); 28122f99347SJan Glauber DBF_HEX(&irq_ptr->dsci, sizeof(void *)); 282779e6e1cSJan Glauber } 283779e6e1cSJan Glauber 284779e6e1cSJan Glauber void qdio_shutdown_thinint(struct qdio_irq *irq_ptr) 285779e6e1cSJan Glauber { 286779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 287779e6e1cSJan Glauber return; 288779e6e1cSJan Glauber 289779e6e1cSJan Glauber /* reset adapter interrupt indicators */ 290779e6e1cSJan Glauber set_subchannel_ind(irq_ptr, 1); 2914814a2b3SJan Glauber put_indicator(irq_ptr->dsci); 292779e6e1cSJan Glauber } 293779e6e1cSJan Glauber 294779e6e1cSJan Glauber void __exit tiqdio_unregister_thinints(void) 295779e6e1cSJan Glauber { 2969e890ad8SJan Glauber WARN_ON(!list_empty(&tiq_list)); 297f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&tiqdio_airq); 298779e6e1cSJan Glauber } 299