1779e6e1cSJan Glauber /* 2a53c8fabSHeiko Carstens * Copyright IBM Corp. 2000, 2009 3779e6e1cSJan Glauber * Author(s): Utz Bacher <utz.bacher@de.ibm.com> 4779e6e1cSJan Glauber * Cornelia Huck <cornelia.huck@de.ibm.com> 5779e6e1cSJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 6779e6e1cSJan Glauber */ 7779e6e1cSJan Glauber #include <linux/io.h> 85a0e3ad6STejun Heo #include <linux/slab.h> 930d77c3eSJan Glauber #include <linux/kernel_stat.h> 1060063497SArun Sharma #include <linux/atomic.h> 11779e6e1cSJan Glauber #include <asm/debug.h> 12779e6e1cSJan Glauber #include <asm/qdio.h> 13779e6e1cSJan Glauber #include <asm/airq.h> 14779e6e1cSJan Glauber #include <asm/isc.h> 15779e6e1cSJan Glauber 16779e6e1cSJan Glauber #include "cio.h" 17779e6e1cSJan Glauber #include "ioasm.h" 18779e6e1cSJan Glauber #include "qdio.h" 19779e6e1cSJan Glauber #include "qdio_debug.h" 20779e6e1cSJan Glauber 21779e6e1cSJan Glauber /* 22779e6e1cSJan Glauber * Restriction: only 63 iqdio subchannels would have its own indicator, 23779e6e1cSJan Glauber * after that, subsequent subchannels share one indicator 24779e6e1cSJan Glauber */ 25779e6e1cSJan Glauber #define TIQDIO_NR_NONSHARED_IND 63 26779e6e1cSJan Glauber #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1) 275f4026f8SJan Glauber #define TIQDIO_SHARED_IND 63 285f4026f8SJan Glauber 295f4026f8SJan Glauber /* device state change indicators */ 305f4026f8SJan Glauber struct indicator_t { 315f4026f8SJan Glauber u32 ind; /* u32 because of compare-and-swap performance */ 325f4026f8SJan Glauber atomic_t count; /* use count, 0 or 1 for non-shared indicators */ 335f4026f8SJan Glauber }; 34779e6e1cSJan Glauber 35779e6e1cSJan Glauber /* list of thin interrupt input queues */ 36779e6e1cSJan Glauber static LIST_HEAD(tiq_list); 37c4736d96SMartin Schwidefsky static DEFINE_MUTEX(tiq_list_lock); 38779e6e1cSJan Glauber 39f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 40f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq); 41f4eae94fSMartin Schwidefsky 42f4eae94fSMartin Schwidefsky static struct airq_struct tiqdio_airq = { 43f4eae94fSMartin Schwidefsky .handler = tiqdio_thinint_handler, 44f4eae94fSMartin Schwidefsky .isc = QDIO_AIRQ_ISC, 45f4eae94fSMartin Schwidefsky }; 46779e6e1cSJan Glauber 475f4026f8SJan Glauber static struct indicator_t *q_indicators; 48779e6e1cSJan Glauber 49a2b86019SJan Glauber u64 last_ai_time; 50d36deae7SJan Glauber 51779e6e1cSJan Glauber /* returns addr for the device state change indicator */ 52779e6e1cSJan Glauber static u32 *get_indicator(void) 53779e6e1cSJan Glauber { 54779e6e1cSJan Glauber int i; 55779e6e1cSJan Glauber 56779e6e1cSJan Glauber for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++) 57779e6e1cSJan Glauber if (!atomic_read(&q_indicators[i].count)) { 58779e6e1cSJan Glauber atomic_set(&q_indicators[i].count, 1); 59779e6e1cSJan Glauber return &q_indicators[i].ind; 60779e6e1cSJan Glauber } 61779e6e1cSJan Glauber 62779e6e1cSJan Glauber /* use the shared indicator */ 63779e6e1cSJan Glauber atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count); 64779e6e1cSJan Glauber return &q_indicators[TIQDIO_SHARED_IND].ind; 65779e6e1cSJan Glauber } 66779e6e1cSJan Glauber 67779e6e1cSJan Glauber static void put_indicator(u32 *addr) 68779e6e1cSJan Glauber { 69779e6e1cSJan Glauber int i; 70779e6e1cSJan Glauber 71779e6e1cSJan Glauber if (!addr) 72779e6e1cSJan Glauber return; 73779e6e1cSJan Glauber i = ((unsigned long)addr - (unsigned long)q_indicators) / 74779e6e1cSJan Glauber sizeof(struct indicator_t); 75779e6e1cSJan Glauber atomic_dec(&q_indicators[i].count); 76779e6e1cSJan Glauber } 77779e6e1cSJan Glauber 78779e6e1cSJan Glauber void tiqdio_add_input_queues(struct qdio_irq *irq_ptr) 79779e6e1cSJan Glauber { 80b4547402SJan Glauber mutex_lock(&tiq_list_lock); 81104ea556Sfrank.blaschka@de.ibm.com list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list); 82b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 83d0c9d4a8SJan Glauber xchg(irq_ptr->dsci, 1 << 7); 84779e6e1cSJan Glauber } 85779e6e1cSJan Glauber 86779e6e1cSJan Glauber void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr) 87779e6e1cSJan Glauber { 88779e6e1cSJan Glauber struct qdio_q *q; 89779e6e1cSJan Glauber 90104ea556Sfrank.blaschka@de.ibm.com q = irq_ptr->input_qs[0]; 9153b41ba7SJan Glauber /* if establish triggered an error */ 9253b41ba7SJan Glauber if (!q || !q->entry.prev || !q->entry.next) 93104ea556Sfrank.blaschka@de.ibm.com return; 94b4547402SJan Glauber 95b4547402SJan Glauber mutex_lock(&tiq_list_lock); 96779e6e1cSJan Glauber list_del_rcu(&q->entry); 97b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 98779e6e1cSJan Glauber synchronize_rcu(); 99779e6e1cSJan Glauber } 100779e6e1cSJan Glauber 1015f4026f8SJan Glauber static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr) 1025f4026f8SJan Glauber { 1035f4026f8SJan Glauber return irq_ptr->nr_input_qs > 1; 1045f4026f8SJan Glauber } 1055f4026f8SJan Glauber 1065f4026f8SJan Glauber static inline int references_shared_dsci(struct qdio_irq *irq_ptr) 1075f4026f8SJan Glauber { 1085f4026f8SJan Glauber return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind; 1095f4026f8SJan Glauber } 1105f4026f8SJan Glauber 1115f4026f8SJan Glauber static inline int shared_ind(struct qdio_irq *irq_ptr) 1125f4026f8SJan Glauber { 1135f4026f8SJan Glauber return references_shared_dsci(irq_ptr) || 1145f4026f8SJan Glauber has_multiple_inq_on_dsci(irq_ptr); 1155f4026f8SJan Glauber } 1165f4026f8SJan Glauber 1175f4026f8SJan Glauber void clear_nonshared_ind(struct qdio_irq *irq_ptr) 1185f4026f8SJan Glauber { 1195f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1205f4026f8SJan Glauber return; 1215f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1225f4026f8SJan Glauber return; 1235f4026f8SJan Glauber xchg(irq_ptr->dsci, 0); 1245f4026f8SJan Glauber } 1255f4026f8SJan Glauber 1265f4026f8SJan Glauber int test_nonshared_ind(struct qdio_irq *irq_ptr) 1275f4026f8SJan Glauber { 1285f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1295f4026f8SJan Glauber return 0; 1305f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1315f4026f8SJan Glauber return 0; 1325f4026f8SJan Glauber if (*irq_ptr->dsci) 1335f4026f8SJan Glauber return 1; 1345f4026f8SJan Glauber else 1355f4026f8SJan Glauber return 0; 1365f4026f8SJan Glauber } 1375f4026f8SJan Glauber 138b02f0c2eSJan Glauber static inline u32 clear_shared_ind(void) 139779e6e1cSJan Glauber { 140b02f0c2eSJan Glauber if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count)) 141b02f0c2eSJan Glauber return 0; 142b02f0c2eSJan Glauber return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0); 143779e6e1cSJan Glauber } 144779e6e1cSJan Glauber 145104ea556Sfrank.blaschka@de.ibm.com static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq) 146104ea556Sfrank.blaschka@de.ibm.com { 147104ea556Sfrank.blaschka@de.ibm.com struct qdio_q *q; 148104ea556Sfrank.blaschka@de.ibm.com int i; 149104ea556Sfrank.blaschka@de.ibm.com 150104ea556Sfrank.blaschka@de.ibm.com if (!references_shared_dsci(irq) && 151104ea556Sfrank.blaschka@de.ibm.com has_multiple_inq_on_dsci(irq)) 152*1e4a382fSJulian Wiedmann xchg(irq->dsci, 0); 153104ea556Sfrank.blaschka@de.ibm.com 154*1e4a382fSJulian Wiedmann for_each_input_queue(irq, q, i) { 155104ea556Sfrank.blaschka@de.ibm.com if (q->u.in.queue_start_poll) { 156104ea556Sfrank.blaschka@de.ibm.com /* skip if polling is enabled or already in work */ 157104ea556Sfrank.blaschka@de.ibm.com if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED, 158104ea556Sfrank.blaschka@de.ibm.com &q->u.in.queue_irq_state)) { 159104ea556Sfrank.blaschka@de.ibm.com qperf_inc(q, int_discarded); 160104ea556Sfrank.blaschka@de.ibm.com continue; 161104ea556Sfrank.blaschka@de.ibm.com } 162104ea556Sfrank.blaschka@de.ibm.com 163104ea556Sfrank.blaschka@de.ibm.com /* avoid dsci clear here, done after processing */ 164104ea556Sfrank.blaschka@de.ibm.com q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr, 165104ea556Sfrank.blaschka@de.ibm.com q->irq_ptr->int_parm); 166104ea556Sfrank.blaschka@de.ibm.com } else { 1675f4026f8SJan Glauber if (!shared_ind(q->irq_ptr)) 168104ea556Sfrank.blaschka@de.ibm.com xchg(q->irq_ptr->dsci, 0); 169104ea556Sfrank.blaschka@de.ibm.com 170104ea556Sfrank.blaschka@de.ibm.com /* 171104ea556Sfrank.blaschka@de.ibm.com * Call inbound processing but not directly 172104ea556Sfrank.blaschka@de.ibm.com * since that could starve other thinint queues. 173104ea556Sfrank.blaschka@de.ibm.com */ 174104ea556Sfrank.blaschka@de.ibm.com tasklet_schedule(&q->tasklet); 175104ea556Sfrank.blaschka@de.ibm.com } 176104ea556Sfrank.blaschka@de.ibm.com } 177104ea556Sfrank.blaschka@de.ibm.com } 178104ea556Sfrank.blaschka@de.ibm.com 179cf9a031cSJan Glauber /** 180cf9a031cSJan Glauber * tiqdio_thinint_handler - thin interrupt handler for qdio 181d36deae7SJan Glauber * @alsi: pointer to adapter local summary indicator 182d36deae7SJan Glauber * @data: NULL 183cf9a031cSJan Glauber */ 184f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq) 185779e6e1cSJan Glauber { 186b02f0c2eSJan Glauber u32 si_used = clear_shared_ind(); 187779e6e1cSJan Glauber struct qdio_q *q; 188779e6e1cSJan Glauber 189d36deae7SJan Glauber last_ai_time = S390_lowcore.int_clock; 190420f42ecSHeiko Carstens inc_irq_stat(IRQIO_QAI); 191d36deae7SJan Glauber 192779e6e1cSJan Glauber /* protect tiq_list entries, only changed in activate or shutdown */ 193779e6e1cSJan Glauber rcu_read_lock(); 194779e6e1cSJan Glauber 195cf9a031cSJan Glauber /* check for work on all inbound thinint queues */ 196d36deae7SJan Glauber list_for_each_entry_rcu(q, &tiq_list, entry) { 197104ea556Sfrank.blaschka@de.ibm.com struct qdio_irq *irq; 198779e6e1cSJan Glauber 199d36deae7SJan Glauber /* only process queues from changed sets */ 200104ea556Sfrank.blaschka@de.ibm.com irq = q->irq_ptr; 201104ea556Sfrank.blaschka@de.ibm.com if (unlikely(references_shared_dsci(irq))) { 2024f325184SJan Glauber if (!si_used) 2034f325184SJan Glauber continue; 204104ea556Sfrank.blaschka@de.ibm.com } else if (!*irq->dsci) 205d36deae7SJan Glauber continue; 206d36deae7SJan Glauber 207104ea556Sfrank.blaschka@de.ibm.com tiqdio_call_inq_handlers(irq); 208d36deae7SJan Glauber 209d36deae7SJan Glauber qperf_inc(q, adapter_int); 210d36deae7SJan Glauber } 211779e6e1cSJan Glauber rcu_read_unlock(); 212779e6e1cSJan Glauber } 213779e6e1cSJan Glauber 214779e6e1cSJan Glauber static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset) 215779e6e1cSJan Glauber { 216ca4ba153SSebastian Ott struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page; 217ca4ba153SSebastian Ott u64 summary_indicator_addr, subchannel_indicator_addr; 218779e6e1cSJan Glauber int rc; 219779e6e1cSJan Glauber 220779e6e1cSJan Glauber if (reset) { 221ca4ba153SSebastian Ott summary_indicator_addr = 0; 222ca4ba153SSebastian Ott subchannel_indicator_addr = 0; 223779e6e1cSJan Glauber } else { 224f4eae94fSMartin Schwidefsky summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr); 225ca4ba153SSebastian Ott subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci); 226779e6e1cSJan Glauber } 227779e6e1cSJan Glauber 228ca4ba153SSebastian Ott rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr, 229ca4ba153SSebastian Ott subchannel_indicator_addr); 230779e6e1cSJan Glauber if (rc) { 23122f99347SJan Glauber DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no, 232ca4ba153SSebastian Ott scssc->response.code); 233ca4ba153SSebastian Ott goto out; 234779e6e1cSJan Glauber } 235779e6e1cSJan Glauber 23622f99347SJan Glauber DBF_EVENT("setscind"); 237ca4ba153SSebastian Ott DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr)); 238ca4ba153SSebastian Ott DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr)); 239ca4ba153SSebastian Ott out: 240ca4ba153SSebastian Ott return rc; 241779e6e1cSJan Glauber } 242779e6e1cSJan Glauber 243779e6e1cSJan Glauber /* allocate non-shared indicators and shared indicator */ 244779e6e1cSJan Glauber int __init tiqdio_allocate_memory(void) 245779e6e1cSJan Glauber { 246779e6e1cSJan Glauber q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS, 247779e6e1cSJan Glauber GFP_KERNEL); 248779e6e1cSJan Glauber if (!q_indicators) 249779e6e1cSJan Glauber return -ENOMEM; 250779e6e1cSJan Glauber return 0; 251779e6e1cSJan Glauber } 252779e6e1cSJan Glauber 253779e6e1cSJan Glauber void tiqdio_free_memory(void) 254779e6e1cSJan Glauber { 255779e6e1cSJan Glauber kfree(q_indicators); 256779e6e1cSJan Glauber } 257779e6e1cSJan Glauber 258779e6e1cSJan Glauber int __init tiqdio_register_thinints(void) 259779e6e1cSJan Glauber { 260f4eae94fSMartin Schwidefsky int rc; 261f4eae94fSMartin Schwidefsky 262f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&tiqdio_airq); 263f4eae94fSMartin Schwidefsky if (rc) { 264f4eae94fSMartin Schwidefsky DBF_EVENT("RTI:%x", rc); 265f4eae94fSMartin Schwidefsky return rc; 266779e6e1cSJan Glauber } 267779e6e1cSJan Glauber return 0; 268779e6e1cSJan Glauber } 269779e6e1cSJan Glauber 270779e6e1cSJan Glauber int qdio_establish_thinint(struct qdio_irq *irq_ptr) 271779e6e1cSJan Glauber { 272779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 273779e6e1cSJan Glauber return 0; 274779e6e1cSJan Glauber return set_subchannel_ind(irq_ptr, 0); 275779e6e1cSJan Glauber } 276779e6e1cSJan Glauber 277779e6e1cSJan Glauber void qdio_setup_thinint(struct qdio_irq *irq_ptr) 278779e6e1cSJan Glauber { 279779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 280779e6e1cSJan Glauber return; 281779e6e1cSJan Glauber irq_ptr->dsci = get_indicator(); 28222f99347SJan Glauber DBF_HEX(&irq_ptr->dsci, sizeof(void *)); 283779e6e1cSJan Glauber } 284779e6e1cSJan Glauber 285779e6e1cSJan Glauber void qdio_shutdown_thinint(struct qdio_irq *irq_ptr) 286779e6e1cSJan Glauber { 287779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 288779e6e1cSJan Glauber return; 289779e6e1cSJan Glauber 290779e6e1cSJan Glauber /* reset adapter interrupt indicators */ 291779e6e1cSJan Glauber set_subchannel_ind(irq_ptr, 1); 2924814a2b3SJan Glauber put_indicator(irq_ptr->dsci); 293779e6e1cSJan Glauber } 294779e6e1cSJan Glauber 295779e6e1cSJan Glauber void __exit tiqdio_unregister_thinints(void) 296779e6e1cSJan Glauber { 2979e890ad8SJan Glauber WARN_ON(!list_empty(&tiq_list)); 298f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&tiqdio_airq); 299779e6e1cSJan Glauber } 300