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> 11*b2d09103SIngo Molnar #include <linux/rculist.h> 12*b2d09103SIngo Molnar 13779e6e1cSJan Glauber #include <asm/debug.h> 14779e6e1cSJan Glauber #include <asm/qdio.h> 15779e6e1cSJan Glauber #include <asm/airq.h> 16779e6e1cSJan Glauber #include <asm/isc.h> 17779e6e1cSJan Glauber 18779e6e1cSJan Glauber #include "cio.h" 19779e6e1cSJan Glauber #include "ioasm.h" 20779e6e1cSJan Glauber #include "qdio.h" 21779e6e1cSJan Glauber #include "qdio_debug.h" 22779e6e1cSJan Glauber 23779e6e1cSJan Glauber /* 24779e6e1cSJan Glauber * Restriction: only 63 iqdio subchannels would have its own indicator, 25779e6e1cSJan Glauber * after that, subsequent subchannels share one indicator 26779e6e1cSJan Glauber */ 27779e6e1cSJan Glauber #define TIQDIO_NR_NONSHARED_IND 63 28779e6e1cSJan Glauber #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1) 295f4026f8SJan Glauber #define TIQDIO_SHARED_IND 63 305f4026f8SJan Glauber 315f4026f8SJan Glauber /* device state change indicators */ 325f4026f8SJan Glauber struct indicator_t { 335f4026f8SJan Glauber u32 ind; /* u32 because of compare-and-swap performance */ 345f4026f8SJan Glauber atomic_t count; /* use count, 0 or 1 for non-shared indicators */ 355f4026f8SJan Glauber }; 36779e6e1cSJan Glauber 37779e6e1cSJan Glauber /* list of thin interrupt input queues */ 38779e6e1cSJan Glauber static LIST_HEAD(tiq_list); 39c4736d96SMartin Schwidefsky static DEFINE_MUTEX(tiq_list_lock); 40779e6e1cSJan Glauber 41f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 42f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq); 43f4eae94fSMartin Schwidefsky 44f4eae94fSMartin Schwidefsky static struct airq_struct tiqdio_airq = { 45f4eae94fSMartin Schwidefsky .handler = tiqdio_thinint_handler, 46f4eae94fSMartin Schwidefsky .isc = QDIO_AIRQ_ISC, 47f4eae94fSMartin Schwidefsky }; 48779e6e1cSJan Glauber 495f4026f8SJan Glauber static struct indicator_t *q_indicators; 50779e6e1cSJan Glauber 51a2b86019SJan Glauber u64 last_ai_time; 52d36deae7SJan Glauber 53779e6e1cSJan Glauber /* returns addr for the device state change indicator */ 54779e6e1cSJan Glauber static u32 *get_indicator(void) 55779e6e1cSJan Glauber { 56779e6e1cSJan Glauber int i; 57779e6e1cSJan Glauber 58779e6e1cSJan Glauber for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++) 59779e6e1cSJan Glauber if (!atomic_read(&q_indicators[i].count)) { 60779e6e1cSJan Glauber atomic_set(&q_indicators[i].count, 1); 61779e6e1cSJan Glauber return &q_indicators[i].ind; 62779e6e1cSJan Glauber } 63779e6e1cSJan Glauber 64779e6e1cSJan Glauber /* use the shared indicator */ 65779e6e1cSJan Glauber atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count); 66779e6e1cSJan Glauber return &q_indicators[TIQDIO_SHARED_IND].ind; 67779e6e1cSJan Glauber } 68779e6e1cSJan Glauber 69779e6e1cSJan Glauber static void put_indicator(u32 *addr) 70779e6e1cSJan Glauber { 71779e6e1cSJan Glauber int i; 72779e6e1cSJan Glauber 73779e6e1cSJan Glauber if (!addr) 74779e6e1cSJan Glauber return; 75779e6e1cSJan Glauber i = ((unsigned long)addr - (unsigned long)q_indicators) / 76779e6e1cSJan Glauber sizeof(struct indicator_t); 77779e6e1cSJan Glauber atomic_dec(&q_indicators[i].count); 78779e6e1cSJan Glauber } 79779e6e1cSJan Glauber 80779e6e1cSJan Glauber void tiqdio_add_input_queues(struct qdio_irq *irq_ptr) 81779e6e1cSJan Glauber { 82b4547402SJan Glauber mutex_lock(&tiq_list_lock); 83104ea556Sfrank.blaschka@de.ibm.com list_add_rcu(&irq_ptr->input_qs[0]->entry, &tiq_list); 84b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 85d0c9d4a8SJan Glauber xchg(irq_ptr->dsci, 1 << 7); 86779e6e1cSJan Glauber } 87779e6e1cSJan Glauber 88779e6e1cSJan Glauber void tiqdio_remove_input_queues(struct qdio_irq *irq_ptr) 89779e6e1cSJan Glauber { 90779e6e1cSJan Glauber struct qdio_q *q; 91779e6e1cSJan Glauber 92104ea556Sfrank.blaschka@de.ibm.com q = irq_ptr->input_qs[0]; 9353b41ba7SJan Glauber /* if establish triggered an error */ 9453b41ba7SJan Glauber if (!q || !q->entry.prev || !q->entry.next) 95104ea556Sfrank.blaschka@de.ibm.com return; 96b4547402SJan Glauber 97b4547402SJan Glauber mutex_lock(&tiq_list_lock); 98779e6e1cSJan Glauber list_del_rcu(&q->entry); 99b4547402SJan Glauber mutex_unlock(&tiq_list_lock); 100779e6e1cSJan Glauber synchronize_rcu(); 101779e6e1cSJan Glauber } 102779e6e1cSJan Glauber 1035f4026f8SJan Glauber static inline int has_multiple_inq_on_dsci(struct qdio_irq *irq_ptr) 1045f4026f8SJan Glauber { 1055f4026f8SJan Glauber return irq_ptr->nr_input_qs > 1; 1065f4026f8SJan Glauber } 1075f4026f8SJan Glauber 1085f4026f8SJan Glauber static inline int references_shared_dsci(struct qdio_irq *irq_ptr) 1095f4026f8SJan Glauber { 1105f4026f8SJan Glauber return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind; 1115f4026f8SJan Glauber } 1125f4026f8SJan Glauber 1135f4026f8SJan Glauber static inline int shared_ind(struct qdio_irq *irq_ptr) 1145f4026f8SJan Glauber { 1155f4026f8SJan Glauber return references_shared_dsci(irq_ptr) || 1165f4026f8SJan Glauber has_multiple_inq_on_dsci(irq_ptr); 1175f4026f8SJan Glauber } 1185f4026f8SJan Glauber 1195f4026f8SJan Glauber void clear_nonshared_ind(struct qdio_irq *irq_ptr) 1205f4026f8SJan Glauber { 1215f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1225f4026f8SJan Glauber return; 1235f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1245f4026f8SJan Glauber return; 1255f4026f8SJan Glauber xchg(irq_ptr->dsci, 0); 1265f4026f8SJan Glauber } 1275f4026f8SJan Glauber 1285f4026f8SJan Glauber int test_nonshared_ind(struct qdio_irq *irq_ptr) 1295f4026f8SJan Glauber { 1305f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr)) 1315f4026f8SJan Glauber return 0; 1325f4026f8SJan Glauber if (shared_ind(irq_ptr)) 1335f4026f8SJan Glauber return 0; 1345f4026f8SJan Glauber if (*irq_ptr->dsci) 1355f4026f8SJan Glauber return 1; 1365f4026f8SJan Glauber else 1375f4026f8SJan Glauber return 0; 1385f4026f8SJan Glauber } 1395f4026f8SJan Glauber 140b02f0c2eSJan Glauber static inline u32 clear_shared_ind(void) 141779e6e1cSJan Glauber { 142b02f0c2eSJan Glauber if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count)) 143b02f0c2eSJan Glauber return 0; 144b02f0c2eSJan Glauber return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0); 145779e6e1cSJan Glauber } 146779e6e1cSJan Glauber 147104ea556Sfrank.blaschka@de.ibm.com static inline void tiqdio_call_inq_handlers(struct qdio_irq *irq) 148104ea556Sfrank.blaschka@de.ibm.com { 149104ea556Sfrank.blaschka@de.ibm.com struct qdio_q *q; 150104ea556Sfrank.blaschka@de.ibm.com int i; 151104ea556Sfrank.blaschka@de.ibm.com 152104ea556Sfrank.blaschka@de.ibm.com if (!references_shared_dsci(irq) && 153104ea556Sfrank.blaschka@de.ibm.com has_multiple_inq_on_dsci(irq)) 1541e4a382fSJulian Wiedmann xchg(irq->dsci, 0); 155104ea556Sfrank.blaschka@de.ibm.com 1561e4a382fSJulian Wiedmann for_each_input_queue(irq, q, i) { 157104ea556Sfrank.blaschka@de.ibm.com if (q->u.in.queue_start_poll) { 158104ea556Sfrank.blaschka@de.ibm.com /* skip if polling is enabled or already in work */ 159104ea556Sfrank.blaschka@de.ibm.com if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED, 160104ea556Sfrank.blaschka@de.ibm.com &q->u.in.queue_irq_state)) { 161104ea556Sfrank.blaschka@de.ibm.com qperf_inc(q, int_discarded); 162104ea556Sfrank.blaschka@de.ibm.com continue; 163104ea556Sfrank.blaschka@de.ibm.com } 164104ea556Sfrank.blaschka@de.ibm.com 165104ea556Sfrank.blaschka@de.ibm.com /* avoid dsci clear here, done after processing */ 16667bb323bSJulian Wiedmann q->u.in.queue_start_poll(irq->cdev, q->nr, 16767bb323bSJulian Wiedmann irq->int_parm); 168104ea556Sfrank.blaschka@de.ibm.com } else { 16967bb323bSJulian Wiedmann if (!shared_ind(irq)) 17067bb323bSJulian Wiedmann xchg(irq->dsci, 0); 171104ea556Sfrank.blaschka@de.ibm.com 172104ea556Sfrank.blaschka@de.ibm.com /* 173104ea556Sfrank.blaschka@de.ibm.com * Call inbound processing but not directly 174104ea556Sfrank.blaschka@de.ibm.com * since that could starve other thinint queues. 175104ea556Sfrank.blaschka@de.ibm.com */ 176104ea556Sfrank.blaschka@de.ibm.com tasklet_schedule(&q->tasklet); 177104ea556Sfrank.blaschka@de.ibm.com } 178104ea556Sfrank.blaschka@de.ibm.com } 179104ea556Sfrank.blaschka@de.ibm.com } 180104ea556Sfrank.blaschka@de.ibm.com 181cf9a031cSJan Glauber /** 182cf9a031cSJan Glauber * tiqdio_thinint_handler - thin interrupt handler for qdio 18372a01d0bSJulian Wiedmann * @airq: pointer to adapter interrupt descriptor 184cf9a031cSJan Glauber */ 185f4eae94fSMartin Schwidefsky static void tiqdio_thinint_handler(struct airq_struct *airq) 186779e6e1cSJan Glauber { 187b02f0c2eSJan Glauber u32 si_used = clear_shared_ind(); 188779e6e1cSJan Glauber struct qdio_q *q; 189779e6e1cSJan Glauber 190d36deae7SJan Glauber last_ai_time = S390_lowcore.int_clock; 191420f42ecSHeiko Carstens inc_irq_stat(IRQIO_QAI); 192d36deae7SJan Glauber 193779e6e1cSJan Glauber /* protect tiq_list entries, only changed in activate or shutdown */ 194779e6e1cSJan Glauber rcu_read_lock(); 195779e6e1cSJan Glauber 196cf9a031cSJan Glauber /* check for work on all inbound thinint queues */ 197d36deae7SJan Glauber list_for_each_entry_rcu(q, &tiq_list, entry) { 198104ea556Sfrank.blaschka@de.ibm.com struct qdio_irq *irq; 199779e6e1cSJan Glauber 200d36deae7SJan Glauber /* only process queues from changed sets */ 201104ea556Sfrank.blaschka@de.ibm.com irq = q->irq_ptr; 202104ea556Sfrank.blaschka@de.ibm.com if (unlikely(references_shared_dsci(irq))) { 2034f325184SJan Glauber if (!si_used) 2044f325184SJan Glauber continue; 205104ea556Sfrank.blaschka@de.ibm.com } else if (!*irq->dsci) 206d36deae7SJan Glauber continue; 207d36deae7SJan Glauber 208104ea556Sfrank.blaschka@de.ibm.com tiqdio_call_inq_handlers(irq); 209d36deae7SJan Glauber 210d36deae7SJan Glauber qperf_inc(q, adapter_int); 211d36deae7SJan Glauber } 212779e6e1cSJan Glauber rcu_read_unlock(); 213779e6e1cSJan Glauber } 214779e6e1cSJan Glauber 215779e6e1cSJan Glauber static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset) 216779e6e1cSJan Glauber { 217ca4ba153SSebastian Ott struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page; 218ca4ba153SSebastian Ott u64 summary_indicator_addr, subchannel_indicator_addr; 219779e6e1cSJan Glauber int rc; 220779e6e1cSJan Glauber 221779e6e1cSJan Glauber if (reset) { 222ca4ba153SSebastian Ott summary_indicator_addr = 0; 223ca4ba153SSebastian Ott subchannel_indicator_addr = 0; 224779e6e1cSJan Glauber } else { 225f4eae94fSMartin Schwidefsky summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr); 226ca4ba153SSebastian Ott subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci); 227779e6e1cSJan Glauber } 228779e6e1cSJan Glauber 229ca4ba153SSebastian Ott rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr, 230ca4ba153SSebastian Ott subchannel_indicator_addr); 231779e6e1cSJan Glauber if (rc) { 23222f99347SJan Glauber DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no, 233ca4ba153SSebastian Ott scssc->response.code); 234ca4ba153SSebastian Ott goto out; 235779e6e1cSJan Glauber } 236779e6e1cSJan Glauber 23722f99347SJan Glauber DBF_EVENT("setscind"); 238ca4ba153SSebastian Ott DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr)); 239ca4ba153SSebastian Ott DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr)); 240ca4ba153SSebastian Ott out: 241ca4ba153SSebastian Ott return rc; 242779e6e1cSJan Glauber } 243779e6e1cSJan Glauber 244779e6e1cSJan Glauber /* allocate non-shared indicators and shared indicator */ 245779e6e1cSJan Glauber int __init tiqdio_allocate_memory(void) 246779e6e1cSJan Glauber { 247779e6e1cSJan Glauber q_indicators = kzalloc(sizeof(struct indicator_t) * TIQDIO_NR_INDICATORS, 248779e6e1cSJan Glauber GFP_KERNEL); 249779e6e1cSJan Glauber if (!q_indicators) 250779e6e1cSJan Glauber return -ENOMEM; 251779e6e1cSJan Glauber return 0; 252779e6e1cSJan Glauber } 253779e6e1cSJan Glauber 254779e6e1cSJan Glauber void tiqdio_free_memory(void) 255779e6e1cSJan Glauber { 256779e6e1cSJan Glauber kfree(q_indicators); 257779e6e1cSJan Glauber } 258779e6e1cSJan Glauber 259779e6e1cSJan Glauber int __init tiqdio_register_thinints(void) 260779e6e1cSJan Glauber { 261f4eae94fSMartin Schwidefsky int rc; 262f4eae94fSMartin Schwidefsky 263f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&tiqdio_airq); 264f4eae94fSMartin Schwidefsky if (rc) { 265f4eae94fSMartin Schwidefsky DBF_EVENT("RTI:%x", rc); 266f4eae94fSMartin Schwidefsky return rc; 267779e6e1cSJan Glauber } 268779e6e1cSJan Glauber return 0; 269779e6e1cSJan Glauber } 270779e6e1cSJan Glauber 271779e6e1cSJan Glauber int qdio_establish_thinint(struct qdio_irq *irq_ptr) 272779e6e1cSJan Glauber { 273779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 274779e6e1cSJan Glauber return 0; 275779e6e1cSJan Glauber return set_subchannel_ind(irq_ptr, 0); 276779e6e1cSJan Glauber } 277779e6e1cSJan Glauber 278779e6e1cSJan Glauber void qdio_setup_thinint(struct qdio_irq *irq_ptr) 279779e6e1cSJan Glauber { 280779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 281779e6e1cSJan Glauber return; 282779e6e1cSJan Glauber irq_ptr->dsci = get_indicator(); 28322f99347SJan Glauber DBF_HEX(&irq_ptr->dsci, sizeof(void *)); 284779e6e1cSJan Glauber } 285779e6e1cSJan Glauber 286779e6e1cSJan Glauber void qdio_shutdown_thinint(struct qdio_irq *irq_ptr) 287779e6e1cSJan Glauber { 288779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr)) 289779e6e1cSJan Glauber return; 290779e6e1cSJan Glauber 291779e6e1cSJan Glauber /* reset adapter interrupt indicators */ 292779e6e1cSJan Glauber set_subchannel_ind(irq_ptr, 1); 2934814a2b3SJan Glauber put_indicator(irq_ptr->dsci); 294779e6e1cSJan Glauber } 295779e6e1cSJan Glauber 296779e6e1cSJan Glauber void __exit tiqdio_unregister_thinints(void) 297779e6e1cSJan Glauber { 2989e890ad8SJan Glauber WARN_ON(!list_empty(&tiq_list)); 299f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&tiqdio_airq); 300779e6e1cSJan Glauber } 301