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>
18*d2197485SMatthew Rosato #include <asm/tpi.h>
19779e6e1cSJan Glauber
20779e6e1cSJan Glauber #include "cio.h"
21779e6e1cSJan Glauber #include "ioasm.h"
22779e6e1cSJan Glauber #include "qdio.h"
23779e6e1cSJan Glauber #include "qdio_debug.h"
24779e6e1cSJan Glauber
25779e6e1cSJan Glauber /*
26779e6e1cSJan Glauber * Restriction: only 63 iqdio subchannels would have its own indicator,
27779e6e1cSJan Glauber * after that, subsequent subchannels share one indicator
28779e6e1cSJan Glauber */
29779e6e1cSJan Glauber #define TIQDIO_NR_NONSHARED_IND 63
30779e6e1cSJan Glauber #define TIQDIO_NR_INDICATORS (TIQDIO_NR_NONSHARED_IND + 1)
315f4026f8SJan Glauber #define TIQDIO_SHARED_IND 63
325f4026f8SJan Glauber
335f4026f8SJan Glauber /* device state change indicators */
345f4026f8SJan Glauber struct indicator_t {
355f4026f8SJan Glauber u32 ind; /* u32 because of compare-and-swap performance */
365f4026f8SJan Glauber atomic_t count; /* use count, 0 or 1 for non-shared indicators */
375f4026f8SJan Glauber };
38779e6e1cSJan Glauber
39779e6e1cSJan Glauber /* list of thin interrupt input queues */
40779e6e1cSJan Glauber static LIST_HEAD(tiq_list);
41c4736d96SMartin Schwidefsky static DEFINE_MUTEX(tiq_list_lock);
42779e6e1cSJan Glauber
435f4026f8SJan Glauber static struct indicator_t *q_indicators;
44779e6e1cSJan Glauber
45a2b86019SJan Glauber u64 last_ai_time;
46d36deae7SJan Glauber
47779e6e1cSJan Glauber /* returns addr for the device state change indicator */
get_indicator(void)48779e6e1cSJan Glauber static u32 *get_indicator(void)
49779e6e1cSJan Glauber {
50779e6e1cSJan Glauber int i;
51779e6e1cSJan Glauber
52779e6e1cSJan Glauber for (i = 0; i < TIQDIO_NR_NONSHARED_IND; i++)
53648a6f44SSebastian Ott if (!atomic_cmpxchg(&q_indicators[i].count, 0, 1))
54779e6e1cSJan Glauber return &q_indicators[i].ind;
55779e6e1cSJan Glauber
56779e6e1cSJan Glauber /* use the shared indicator */
57779e6e1cSJan Glauber atomic_inc(&q_indicators[TIQDIO_SHARED_IND].count);
58779e6e1cSJan Glauber return &q_indicators[TIQDIO_SHARED_IND].ind;
59779e6e1cSJan Glauber }
60779e6e1cSJan Glauber
put_indicator(u32 * addr)61779e6e1cSJan Glauber static void put_indicator(u32 *addr)
62779e6e1cSJan Glauber {
6330e8eb86SSebastian Ott struct indicator_t *ind = container_of(addr, struct indicator_t, ind);
64779e6e1cSJan Glauber
65779e6e1cSJan Glauber if (!addr)
66779e6e1cSJan Glauber return;
6730e8eb86SSebastian Ott atomic_dec(&ind->count);
68779e6e1cSJan Glauber }
69779e6e1cSJan Glauber
references_shared_dsci(struct qdio_irq * irq_ptr)705f4026f8SJan Glauber static inline int references_shared_dsci(struct qdio_irq *irq_ptr)
715f4026f8SJan Glauber {
725f4026f8SJan Glauber return irq_ptr->dsci == &q_indicators[TIQDIO_SHARED_IND].ind;
735f4026f8SJan Glauber }
745f4026f8SJan Glauber
test_nonshared_ind(struct qdio_irq * irq_ptr)755f4026f8SJan Glauber int test_nonshared_ind(struct qdio_irq *irq_ptr)
765f4026f8SJan Glauber {
775f4026f8SJan Glauber if (!is_thinint_irq(irq_ptr))
785f4026f8SJan Glauber return 0;
799c159bbcSJulian Wiedmann if (references_shared_dsci(irq_ptr))
805f4026f8SJan Glauber return 0;
815f4026f8SJan Glauber if (*irq_ptr->dsci)
825f4026f8SJan Glauber return 1;
835f4026f8SJan Glauber else
845f4026f8SJan Glauber return 0;
855f4026f8SJan Glauber }
865f4026f8SJan Glauber
clear_shared_ind(void)87b02f0c2eSJan Glauber static inline u32 clear_shared_ind(void)
88779e6e1cSJan Glauber {
89b02f0c2eSJan Glauber if (!atomic_read(&q_indicators[TIQDIO_SHARED_IND].count))
90b02f0c2eSJan Glauber return 0;
91b02f0c2eSJan Glauber return xchg(&q_indicators[TIQDIO_SHARED_IND].ind, 0);
92779e6e1cSJan Glauber }
93779e6e1cSJan Glauber
94cf9a031cSJan Glauber /**
95cf9a031cSJan Glauber * tiqdio_thinint_handler - thin interrupt handler for qdio
9672a01d0bSJulian Wiedmann * @airq: pointer to adapter interrupt descriptor
97*d2197485SMatthew Rosato * @tpi_info: interrupt information (e.g. floating vs directed -- unused)
98cf9a031cSJan Glauber */
tiqdio_thinint_handler(struct airq_struct * airq,struct tpi_info * tpi_info)99*d2197485SMatthew Rosato static void tiqdio_thinint_handler(struct airq_struct *airq,
100*d2197485SMatthew Rosato struct tpi_info *tpi_info)
101779e6e1cSJan Glauber {
102bd839171SJulian Wiedmann u64 irq_time = S390_lowcore.int_clock;
103b02f0c2eSJan Glauber u32 si_used = clear_shared_ind();
10494c43bdaSJulian Wiedmann struct qdio_irq *irq;
105779e6e1cSJan Glauber
106bd839171SJulian Wiedmann last_ai_time = irq_time;
107420f42ecSHeiko Carstens inc_irq_stat(IRQIO_QAI);
108d36deae7SJan Glauber
109779e6e1cSJan Glauber /* protect tiq_list entries, only changed in activate or shutdown */
110779e6e1cSJan Glauber rcu_read_lock();
111779e6e1cSJan Glauber
11294c43bdaSJulian Wiedmann list_for_each_entry_rcu(irq, &tiq_list, entry) {
113d36deae7SJan Glauber /* only process queues from changed sets */
114104ea556Sfrank.blaschka@de.ibm.com if (unlikely(references_shared_dsci(irq))) {
1154f325184SJan Glauber if (!si_used)
1164f325184SJan Glauber continue;
1171ecbcfd5SJulian Wiedmann } else {
1181ecbcfd5SJulian Wiedmann if (!*irq->dsci)
119d36deae7SJan Glauber continue;
120d36deae7SJan Glauber
1211ecbcfd5SJulian Wiedmann xchg(irq->dsci, 0);
1221ecbcfd5SJulian Wiedmann }
1231ecbcfd5SJulian Wiedmann
1241ecbcfd5SJulian Wiedmann qdio_deliver_irq(irq);
125bd839171SJulian Wiedmann irq->last_data_irq_time = irq_time;
126d36deae7SJan Glauber
12746112810SJulian Wiedmann QDIO_PERF_STAT_INC(irq, adapter_int);
128d36deae7SJan Glauber }
129779e6e1cSJan Glauber rcu_read_unlock();
130779e6e1cSJan Glauber }
131779e6e1cSJan Glauber
132d86f71fdSJulian Wiedmann static struct airq_struct tiqdio_airq = {
133d86f71fdSJulian Wiedmann .handler = tiqdio_thinint_handler,
134d86f71fdSJulian Wiedmann .isc = QDIO_AIRQ_ISC,
135d86f71fdSJulian Wiedmann };
136d86f71fdSJulian Wiedmann
set_subchannel_ind(struct qdio_irq * irq_ptr,int reset)137779e6e1cSJan Glauber static int set_subchannel_ind(struct qdio_irq *irq_ptr, int reset)
138779e6e1cSJan Glauber {
139ca4ba153SSebastian Ott struct chsc_scssc_area *scssc = (void *)irq_ptr->chsc_page;
140ca4ba153SSebastian Ott u64 summary_indicator_addr, subchannel_indicator_addr;
141779e6e1cSJan Glauber int rc;
142779e6e1cSJan Glauber
143779e6e1cSJan Glauber if (reset) {
144ca4ba153SSebastian Ott summary_indicator_addr = 0;
145ca4ba153SSebastian Ott subchannel_indicator_addr = 0;
146779e6e1cSJan Glauber } else {
147f4eae94fSMartin Schwidefsky summary_indicator_addr = virt_to_phys(tiqdio_airq.lsi_ptr);
148ca4ba153SSebastian Ott subchannel_indicator_addr = virt_to_phys(irq_ptr->dsci);
149779e6e1cSJan Glauber }
150779e6e1cSJan Glauber
151ca4ba153SSebastian Ott rc = chsc_sadc(irq_ptr->schid, scssc, summary_indicator_addr,
15292892240SJulian Wiedmann subchannel_indicator_addr, tiqdio_airq.isc);
153779e6e1cSJan Glauber if (rc) {
15422f99347SJan Glauber DBF_ERROR("%4x SSI r:%4x", irq_ptr->schid.sch_no,
155ca4ba153SSebastian Ott scssc->response.code);
156ca4ba153SSebastian Ott goto out;
157779e6e1cSJan Glauber }
158779e6e1cSJan Glauber
15922f99347SJan Glauber DBF_EVENT("setscind");
160ca4ba153SSebastian Ott DBF_HEX(&summary_indicator_addr, sizeof(summary_indicator_addr));
161ca4ba153SSebastian Ott DBF_HEX(&subchannel_indicator_addr, sizeof(subchannel_indicator_addr));
162ca4ba153SSebastian Ott out:
163ca4ba153SSebastian Ott return rc;
164779e6e1cSJan Glauber }
165779e6e1cSJan Glauber
qdio_establish_thinint(struct qdio_irq * irq_ptr)166779e6e1cSJan Glauber int qdio_establish_thinint(struct qdio_irq *irq_ptr)
167779e6e1cSJan Glauber {
16875e82becSJulian Wiedmann int rc;
16975e82becSJulian Wiedmann
170779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr))
171779e6e1cSJan Glauber return 0;
172779e6e1cSJan Glauber
173779e6e1cSJan Glauber irq_ptr->dsci = get_indicator();
17422f99347SJan Glauber DBF_HEX(&irq_ptr->dsci, sizeof(void *));
17575e82becSJulian Wiedmann
17675e82becSJulian Wiedmann rc = set_subchannel_ind(irq_ptr, 0);
177954d6235SJulian Wiedmann if (rc) {
17875e82becSJulian Wiedmann put_indicator(irq_ptr->dsci);
17975e82becSJulian Wiedmann return rc;
180779e6e1cSJan Glauber }
181779e6e1cSJan Glauber
182954d6235SJulian Wiedmann mutex_lock(&tiq_list_lock);
183954d6235SJulian Wiedmann list_add_rcu(&irq_ptr->entry, &tiq_list);
184954d6235SJulian Wiedmann mutex_unlock(&tiq_list_lock);
185954d6235SJulian Wiedmann return 0;
186954d6235SJulian Wiedmann }
187954d6235SJulian Wiedmann
qdio_shutdown_thinint(struct qdio_irq * irq_ptr)188779e6e1cSJan Glauber void qdio_shutdown_thinint(struct qdio_irq *irq_ptr)
189779e6e1cSJan Glauber {
190779e6e1cSJan Glauber if (!is_thinint_irq(irq_ptr))
191779e6e1cSJan Glauber return;
192779e6e1cSJan Glauber
193954d6235SJulian Wiedmann mutex_lock(&tiq_list_lock);
194954d6235SJulian Wiedmann list_del_rcu(&irq_ptr->entry);
195954d6235SJulian Wiedmann mutex_unlock(&tiq_list_lock);
196954d6235SJulian Wiedmann synchronize_rcu();
197954d6235SJulian Wiedmann
198779e6e1cSJan Glauber /* reset adapter interrupt indicators */
199779e6e1cSJan Glauber set_subchannel_ind(irq_ptr, 1);
2004814a2b3SJan Glauber put_indicator(irq_ptr->dsci);
201779e6e1cSJan Glauber }
202779e6e1cSJan Glauber
qdio_thinint_init(void)2033050f022SJulian Wiedmann int __init qdio_thinint_init(void)
2043050f022SJulian Wiedmann {
2053050f022SJulian Wiedmann int rc;
2063050f022SJulian Wiedmann
2073050f022SJulian Wiedmann q_indicators = kcalloc(TIQDIO_NR_INDICATORS, sizeof(struct indicator_t),
2083050f022SJulian Wiedmann GFP_KERNEL);
2093050f022SJulian Wiedmann if (!q_indicators)
2103050f022SJulian Wiedmann return -ENOMEM;
2113050f022SJulian Wiedmann
2123050f022SJulian Wiedmann rc = register_adapter_interrupt(&tiqdio_airq);
2133050f022SJulian Wiedmann if (rc) {
2143050f022SJulian Wiedmann DBF_EVENT("RTI:%x", rc);
2153050f022SJulian Wiedmann kfree(q_indicators);
2163050f022SJulian Wiedmann return rc;
2173050f022SJulian Wiedmann }
2183050f022SJulian Wiedmann return 0;
2193050f022SJulian Wiedmann }
2203050f022SJulian Wiedmann
qdio_thinint_exit(void)2213050f022SJulian Wiedmann void __exit qdio_thinint_exit(void)
222779e6e1cSJan Glauber {
2239e890ad8SJan Glauber WARN_ON(!list_empty(&tiq_list));
224f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&tiqdio_airq);
2253050f022SJulian Wiedmann kfree(q_indicators);
226779e6e1cSJan Glauber }
227