blob: 4b7cc8d425b1c64c6b27e4f3308d4152362a0e21 [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-2.0
Jan Glauber779e6e12008-07-17 17:16:48 +02002/*
Jan Glauber779e6e12008-07-17 17:16:48 +02003 * Linux for s390 qdio support, buffer handling, qdio API and module support.
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2000, 2008
Jan Glauber779e6e12008-07-17 17:16:48 +02006 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
7 * Jan Glauber <jang@linux.vnet.ibm.com>
8 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
9 */
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/timer.h>
14#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/gfp.h>
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000016#include <linux/io.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020018#include <asm/debug.h>
19#include <asm/qdio.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040020#include <asm/ipl.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020021
22#include "cio.h"
23#include "css.h"
24#include "device.h"
25#include "qdio.h"
26#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020027
28MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 "Jan Glauber <jang@linux.vnet.ibm.com>");
30MODULE_DESCRIPTION("QDIO base support");
31MODULE_LICENSE("GPL");
32
Jan Glauber958c0ba2011-01-05 12:47:52 +010033static inline int do_siga_sync(unsigned long schid,
34 unsigned int out_mask, unsigned int in_mask,
35 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020036{
Jan Glauber958c0ba2011-01-05 12:47:52 +010037 register unsigned long __fc asm ("0") = fc;
38 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020039 register unsigned long out asm ("2") = out_mask;
40 register unsigned long in asm ("3") = in_mask;
41 int cc;
42
43 asm volatile(
44 " siga 0\n"
45 " ipm %0\n"
46 " srl %0,28\n"
47 : "=d" (cc)
48 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
49 return cc;
50}
51
Jan Glauber958c0ba2011-01-05 12:47:52 +010052static inline int do_siga_input(unsigned long schid, unsigned int mask,
53 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020054{
Jan Glauber958c0ba2011-01-05 12:47:52 +010055 register unsigned long __fc asm ("0") = fc;
56 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020057 register unsigned long __mask asm ("2") = mask;
58 int cc;
59
60 asm volatile(
61 " siga 0\n"
62 " ipm %0\n"
63 " srl %0,28\n"
64 : "=d" (cc)
Jan Glauber1549d132012-05-09 16:27:34 +020065 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc");
Jan Glauber779e6e12008-07-17 17:16:48 +020066 return cc;
67}
68
69/**
70 * do_siga_output - perform SIGA-w/wt function
71 * @schid: subchannel id or in case of QEBSM the subchannel token
72 * @mask: which output queues to process
73 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
74 * @fc: function code to perform
Sebastian Ott364e3f92018-01-29 12:55:29 +010075 * @aob: asynchronous operation block
Jan Glauber779e6e12008-07-17 17:16:48 +020076 *
Jan Glauber1549d132012-05-09 16:27:34 +020077 * Returns condition code.
Jan Glauber779e6e12008-07-17 17:16:48 +020078 * Note: For IQDC unicast queues only the highest priority queue is processed.
79 */
80static inline int do_siga_output(unsigned long schid, unsigned long mask,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000081 unsigned int *bb, unsigned int fc,
82 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +020083{
84 register unsigned long __fc asm("0") = fc;
85 register unsigned long __schid asm("1") = schid;
86 register unsigned long __mask asm("2") = mask;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000087 register unsigned long __aob asm("3") = aob;
Jan Glauber1549d132012-05-09 16:27:34 +020088 int cc;
Jan Glauber779e6e12008-07-17 17:16:48 +020089
90 asm volatile(
91 " siga 0\n"
Jan Glauber1549d132012-05-09 16:27:34 +020092 " ipm %0\n"
Jan Glauber779e6e12008-07-17 17:16:48 +020093 " srl %0,28\n"
Jan Glauber1549d132012-05-09 16:27:34 +020094 : "=d" (cc), "+d" (__fc), "+d" (__aob)
95 : "d" (__schid), "d" (__mask)
96 : "cc");
97 *bb = __fc >> 31;
Jan Glauber779e6e12008-07-17 17:16:48 +020098 return cc;
99}
100
Jan Glauber779e6e12008-07-17 17:16:48 +0200101/**
102 * qdio_do_eqbs - extract buffer states for QEBSM
103 * @q: queue to manipulate
104 * @state: state of the extracted buffers
105 * @start: buffer number to start at
106 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100107 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200108 *
Coly Li73ac36e2009-01-07 18:09:16 -0800109 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200110 * Stops processing if a state is different from the last buffers state.
111 */
112static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100113 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200114{
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100115 int tmp_count = count, tmp_start = start, nr = q->nr;
Jan Glauber779e6e12008-07-17 17:16:48 +0200116 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200117
Jan Glauber6486cda2010-01-04 09:05:42 +0100118 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200119
120 if (!q->is_input_q)
121 nr += q->irq_ptr->nr_input_qs;
122again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100123 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
124 auto_ack);
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100125
126 switch (ccq) {
127 case 0:
128 case 32:
129 /* all done, or next buffer state different */
Jan Glauber25f269f2011-10-30 15:17:06 +0100130 return count - tmp_count;
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100131 case 96:
132 /* not all buffers processed */
Jan Glauber25f269f2011-10-30 15:17:06 +0100133 qperf_inc(q, eqbs_partial);
134 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
135 tmp_count);
Julian Wiedmanndae55b62018-03-05 09:39:38 +0100136 return count - tmp_count;
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100137 case 97:
138 /* no buffer processed */
139 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
140 goto again;
141 default:
142 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
143 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
144 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
145 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE, q->nr,
146 q->first_to_kick, count, q->irq_ptr->int_parm);
147 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200148 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200149}
150
151/**
152 * qdio_do_sqbs - set buffer states for QEBSM
153 * @q: queue to manipulate
154 * @state: new state of the buffers
155 * @start: first buffer number to change
156 * @count: how many buffers to change
157 *
158 * Returns the number of successfully changed buffers.
159 * Does retrying until the specified count of buffer states is set or an
160 * error occurs.
161 */
162static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
163 int count)
164{
165 unsigned int ccq = 0;
166 int tmp_count = count, tmp_start = start;
167 int nr = q->nr;
Jan Glauber779e6e12008-07-17 17:16:48 +0200168
Jan Glauber50f769d2008-12-25 13:38:47 +0100169 if (!count)
170 return 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100171 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200172
173 if (!q->is_input_q)
174 nr += q->irq_ptr->nr_input_qs;
175again:
176 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100177
178 switch (ccq) {
179 case 0:
180 case 32:
181 /* all done, or active buffer adapter-owned */
Jan Glauberce1d8012012-10-24 12:38:35 +0200182 WARN_ON_ONCE(tmp_count);
Jan Glauber25f269f2011-10-30 15:17:06 +0100183 return count - tmp_count;
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100184 case 96:
185 /* not all buffers processed */
Jan Glauber22f99342008-12-25 13:38:46 +0100186 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100187 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200188 goto again;
Julian Wiedmann88bf3192018-03-06 17:58:49 +0100189 default:
190 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
191 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
192 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
193 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE, q->nr,
194 q->first_to_kick, count, q->irq_ptr->int_parm);
195 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200196 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200197}
198
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100199/*
200 * Returns number of examined buffers and their common state in *state.
201 * Requested number of buffers-to-examine must be > 0.
202 */
Jan Glauber779e6e12008-07-17 17:16:48 +0200203static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100204 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000205 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200206{
207 unsigned char __state = 0;
208 int i;
209
Jan Glauber779e6e12008-07-17 17:16:48 +0200210 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100211 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200212
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100213 /* get initial state: */
214 __state = q->slsb.val[bufnr];
215 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
216 __state = SLSB_P_OUTPUT_EMPTY;
217
218 for (i = 1; i < count; i++) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200219 bufnr = next_buf(bufnr);
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100220
221 /* merge PENDING into EMPTY: */
222 if (merge_pending &&
223 q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
224 __state == SLSB_P_OUTPUT_EMPTY)
225 continue;
226
227 /* stop if next state differs from initial state: */
228 if (q->slsb.val[bufnr] != __state)
229 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200230 }
231 *state = __state;
232 return i;
233}
234
Jan Glauber60b5df22009-06-22 12:08:10 +0200235static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
236 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200237{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000238 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200239}
240
241/* wrap-around safe setting of slsb states, returns number of changed buffers */
242static inline int set_buf_states(struct qdio_q *q, int bufnr,
243 unsigned char state, int count)
244{
245 int i;
246
Jan Glauber779e6e12008-07-17 17:16:48 +0200247 if (is_qebsm(q))
248 return qdio_do_sqbs(q, state, bufnr, count);
249
250 for (i = 0; i < count; i++) {
251 xchg(&q->slsb.val[bufnr], state);
252 bufnr = next_buf(bufnr);
253 }
254 return count;
255}
256
257static inline int set_buf_state(struct qdio_q *q, int bufnr,
258 unsigned char state)
259{
260 return set_buf_states(q, bufnr, state, 1);
261}
262
263/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100264static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200265{
266 struct qdio_q *q;
267 int i;
268
269 for_each_input_queue(irq_ptr, q, i)
270 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
271 QDIO_MAX_BUFFERS_PER_Q);
272 for_each_output_queue(irq_ptr, q, i)
273 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
274 QDIO_MAX_BUFFERS_PER_Q);
275}
276
Jan Glauber60b5df22009-06-22 12:08:10 +0200277static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200278 unsigned int input)
279{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100280 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
281 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200282 int cc;
283
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100284 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100285 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200286
Jan Glauber958c0ba2011-01-05 12:47:52 +0100287 if (is_qebsm(q)) {
288 schid = q->irq_ptr->sch_token;
289 fc |= QDIO_SIGA_QEBSM_FLAG;
290 }
291
292 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100293 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100294 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200295 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200296}
297
Jan Glauber60b5df22009-06-22 12:08:10 +0200298static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200299{
300 if (q->is_input_q)
301 return qdio_siga_sync(q, 0, q->mask);
302 else
303 return qdio_siga_sync(q, q->mask, 0);
304}
305
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000306static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
307 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200308{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100309 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
310 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100311 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200312 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000313 unsigned long laob = 0;
314
Eugene Crosserec6674c2015-10-06 15:12:29 +0200315 WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
316 !q->u.out.use_cq));
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000317 if (q->u.out.use_cq && aob != 0) {
318 fc = QDIO_SIGA_WRITEQ;
319 laob = aob;
320 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200321
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100322 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200323 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100324 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200325 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200326again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000327 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200328
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100329 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100330 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200331 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100332
333 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200334 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100335 goto again;
336 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200337 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200338 goto again;
339 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200340 if (retries) {
341 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
342 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
343 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
344 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200345 return cc;
346}
347
348static inline int qdio_siga_input(struct qdio_q *q)
349{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100350 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
351 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200352 int cc;
353
Jan Glauber22f99342008-12-25 13:38:46 +0100354 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100355 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200356
Jan Glauber958c0ba2011-01-05 12:47:52 +0100357 if (is_qebsm(q)) {
358 schid = q->irq_ptr->sch_token;
359 fc |= QDIO_SIGA_QEBSM_FLAG;
360 }
361
362 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100363 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100364 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200365 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200366}
367
Jan Glauber90adac52011-01-05 12:47:54 +0100368#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
369#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
370
371static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200372{
Jan Glauber90adac52011-01-05 12:47:54 +0100373 /* PCI capable outbound queues will also be scanned so sync them too */
374 if (pci_out_supported(q))
375 qdio_siga_sync_all(q);
376 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200377 qdio_siga_sync_q(q);
378}
379
Jan Glauber60b5df22009-06-22 12:08:10 +0200380int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
381 unsigned char *state)
382{
Jan Glauber90adac52011-01-05 12:47:54 +0100383 if (need_siga_sync(q))
384 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000385 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200386}
387
388static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200389{
Jan Glauber50f769d2008-12-25 13:38:47 +0100390 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200391 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100392
Jan Glauber779e6e12008-07-17 17:16:48 +0200393 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100394 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200395
396 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100397 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100398 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100399 q->u.in.ack_count);
400 q->u.in.ack_count = 0;
401 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100402 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200403}
404
Fabian Frederick92bdae52014-06-03 21:55:15 +0200405static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100406{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200407 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100408
409 q->q_stats.nr_sbal_total += count;
410 if (count == QDIO_MAX_BUFFERS_MASK) {
411 q->q_stats.nr_sbals[7]++;
412 return;
413 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200414 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100415 q->q_stats.nr_sbals[pos]++;
416}
417
Jan Glauberbffbbd22011-04-20 10:15:33 +0200418static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200419{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200420 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
421 SLSB_P_OUTPUT_NOT_INIT;
422
Jan Glauber1549d132012-05-09 16:27:34 +0200423 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100424
425 /* special handling for no target buffer empty */
Julian Wiedmannb23481f2017-10-23 09:38:18 +0200426 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
427 q->sbal[q->first_to_check]->element[15].sflags == 0x10) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100428 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200429 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100430 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100431 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100432 }
433
Jan Glauber22f99342008-12-25 13:38:46 +0100434 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
435 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100436 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100437 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec908782011-06-06 14:14:40 +0200438 q->sbal[q->first_to_check]->element[14].sflags,
439 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200440
Jan Glauber2768b2d2011-10-30 15:17:07 +0100441set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200442 /*
443 * Interrupts may be avoided as long as the error is present
444 * so change the buffer state immediately to avoid starvation.
445 */
446 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100447}
Jan Glauber779e6e12008-07-17 17:16:48 +0200448
Jan Glauber50f769d2008-12-25 13:38:47 +0100449static inline void inbound_primed(struct qdio_q *q, int count)
450{
451 int new;
452
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100453 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100454
455 /* for QEBSM the ACK was already set by EQBS */
456 if (is_qebsm(q)) {
457 if (!q->u.in.polling) {
458 q->u.in.polling = 1;
459 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100460 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100461 return;
462 }
463
464 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100465 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100466 q->u.in.ack_count);
467 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100468 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100469 return;
470 }
471
472 /*
473 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
474 * or by the next inbound run.
475 */
476 new = add_buf(q->first_to_check, count - 1);
477 if (q->u.in.polling) {
478 /* reset the previous ACK but first set the new one */
479 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100480 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100481 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100482 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100483 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100484 }
485
Jan Glaubere85dea02009-03-26 15:24:29 +0100486 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100487 count--;
488 if (!count)
489 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200490 /* need to change ALL buffers to get more interrupts */
491 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200492}
493
494static int get_inbound_buffer_frontier(struct qdio_q *q)
495{
Jan Glauber6fa10982011-01-31 11:30:08 +0100496 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100497 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200498
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200499 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100500
Jan Glauber779e6e12008-07-17 17:16:48 +0200501 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200502 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
503 * would return 0.
504 */
505 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100506 if (!count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200507 goto out;
508
Jan Glauber36e3e722009-06-22 12:08:12 +0200509 /*
510 * No siga sync here, as a PCI or we after a thin interrupt
511 * already sync'ed the queues.
512 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000513 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200514 if (!count)
515 goto out;
516
517 switch (state) {
518 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100519 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200520 q->first_to_check = add_buf(q->first_to_check, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200521 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100522 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100523 if (q->irq_ptr->perf_stat_enabled)
524 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200525 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200526 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200527 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200528 q->first_to_check = add_buf(q->first_to_check, count);
Julian Wiedmann0b926ac2017-10-23 09:40:16 +0200529 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
530 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100531 if (q->irq_ptr->perf_stat_enabled)
532 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200533 break;
534 case SLSB_CU_INPUT_EMPTY:
535 case SLSB_P_INPUT_NOT_INIT:
536 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100537 if (q->irq_ptr->perf_stat_enabled)
538 q->q_stats.nr_sbal_nop++;
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100539 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
540 q->nr, q->first_to_check);
Jan Glauber779e6e12008-07-17 17:16:48 +0200541 break;
542 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200543 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200544 }
545out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200546 return q->first_to_check;
547}
548
Jan Glauber60b5df22009-06-22 12:08:10 +0200549static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200550{
551 int bufnr;
552
553 bufnr = get_inbound_buffer_frontier(q);
554
Jan Glauber1549d132012-05-09 16:27:34 +0200555 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100556 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100557 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Heiko Carstens1aae0562013-01-30 09:49:40 +0100558 q->u.in.timestamp = get_tod_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200559 return 1;
560 } else
561 return 0;
562}
563
Jan Glauber9a2c1602009-06-22 12:08:11 +0200564static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200565{
566 unsigned char state = 0;
567
568 if (!atomic_read(&q->nr_buf_used))
569 return 1;
570
Jan Glauber90adac52011-01-05 12:47:54 +0100571 if (need_siga_sync(q))
572 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200573 get_buf_state(q, q->first_to_check, &state, 0);
574
Ursula Braun4c522282010-02-09 09:46:07 +0100575 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200576 /* more work coming */
577 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200578
579 if (is_thinint_irq(q->irq_ptr))
580 return 1;
581
582 /* don't poll under z/VM */
583 if (MACHINE_IS_VM)
584 return 1;
585
586 /*
587 * At this point we know, that inbound first_to_check
588 * has (probably) not moved (see qdio_inbound_processing).
589 */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200590 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200591 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200592 q->first_to_check);
593 return 1;
594 } else
595 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200596}
597
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000598static inline int contains_aobs(struct qdio_q *q)
599{
600 return !q->is_input_q && q->u.out.use_cq;
601}
602
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000603static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
604{
605 unsigned char state = 0;
606 int j, b = start;
607
608 if (!contains_aobs(q))
609 return;
610
611 for (j = 0; j < count; ++j) {
612 get_buf_state(q, b, &state, 0);
613 if (state == SLSB_P_OUTPUT_PENDING) {
614 struct qaob *aob = q->u.out.aobs[b];
615 if (aob == NULL)
616 continue;
617
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000618 q->u.out.sbal_state[b].flags |=
619 QDIO_OUTBUF_STATE_FLAG_PENDING;
620 q->u.out.aobs[b] = NULL;
621 } else if (state == SLSB_P_OUTPUT_EMPTY) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000622 q->u.out.sbal_state[b].aob = NULL;
623 }
624 b = next_buf(b);
625 }
626}
627
628static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
629 int bufnr)
630{
631 unsigned long phys_aob = 0;
632
633 if (!q->use_cq)
Julian Wiedmann64e03ff2018-05-16 09:37:25 +0200634 return 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000635
636 if (!q->aobs[bufnr]) {
637 struct qaob *aob = qdio_allocate_aob();
638 q->aobs[bufnr] = aob;
639 }
640 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000641 q->sbal_state[bufnr].aob = q->aobs[bufnr];
642 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
643 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200644 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000645 }
646
Julian Wiedmann64e03ff2018-05-16 09:37:25 +0200647 q->sbal_state[bufnr].flags = 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000648 return phys_aob;
649}
650
Jan Glauber60b5df22009-06-22 12:08:10 +0200651static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200652{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100653 int start = q->first_to_kick;
654 int end = q->first_to_check;
655 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200656
657 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
658 return;
659
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100660 count = sub_buf(end, start);
661
662 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100663 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200664 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100665 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100666 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200667 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
668 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100669 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100670
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000671 qdio_handle_aobs(q, start, count);
672
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100673 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
674 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200675
676 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100677 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200678 q->qdio_error = 0;
679}
680
Ursula Braun9bce8b22016-08-05 12:33:10 +0200681static inline int qdio_tasklet_schedule(struct qdio_q *q)
682{
683 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
684 tasklet_schedule(&q->tasklet);
685 return 0;
686 }
687 return -EPERM;
688}
689
Jan Glauber779e6e12008-07-17 17:16:48 +0200690static void __qdio_inbound_processing(struct qdio_q *q)
691{
Jan Glauber6486cda2010-01-04 09:05:42 +0100692 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200693
Jan Glauber779e6e12008-07-17 17:16:48 +0200694 if (!qdio_inbound_q_moved(q))
695 return;
696
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100697 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200698
Jan Glauber6486cda2010-01-04 09:05:42 +0100699 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200700 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100701 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200702 if (!qdio_tasklet_schedule(q))
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200703 return;
Jan Glauber6486cda2010-01-04 09:05:42 +0100704 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200705
706 qdio_stop_polling(q);
707 /*
708 * We need to check again to not lose initiative after
709 * resetting the ACK state.
710 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100711 if (!qdio_inbound_q_done(q)) {
712 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200713 qdio_tasklet_schedule(q);
Jan Glauber6486cda2010-01-04 09:05:42 +0100714 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200715}
716
Jan Glauber779e6e12008-07-17 17:16:48 +0200717void qdio_inbound_processing(unsigned long data)
718{
719 struct qdio_q *q = (struct qdio_q *)data;
720 __qdio_inbound_processing(q);
721}
722
723static int get_outbound_buffer_frontier(struct qdio_q *q)
724{
Jan Glauber6fa10982011-01-31 11:30:08 +0100725 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100726 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200727
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200728 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100729
Jan Glauber90adac52011-01-05 12:47:54 +0100730 if (need_siga_sync(q))
731 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
732 !pci_out_supported(q)) ||
733 (queue_type(q) == QDIO_IQDIO_QFMT &&
734 multicast_outbound(q)))
735 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200736
737 /*
738 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
739 * would return 0.
740 */
741 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100742 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000743 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200744
Julian Wiedmannc11a3df2018-03-07 14:19:43 +0100745 count = get_buf_states(q, q->first_to_check, &state, count, 0,
746 q->u.out.use_cq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200747 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000748 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200749
750 switch (state) {
751 case SLSB_P_OUTPUT_EMPTY:
Julian Wiedmann99dcd702019-06-03 07:47:04 +0200752 case SLSB_P_OUTPUT_PENDING:
Jan Glauber779e6e12008-07-17 17:16:48 +0200753 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000754 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
755 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200756
757 atomic_sub(count, &q->nr_buf_used);
758 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100759 if (q->irq_ptr->perf_stat_enabled)
760 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000761
Jan Glauber36e3e722009-06-22 12:08:12 +0200762 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200763 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200764 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200765 q->first_to_check = add_buf(q->first_to_check, count);
766 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100767 if (q->irq_ptr->perf_stat_enabled)
768 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200769 break;
770 case SLSB_CU_OUTPUT_PRIMED:
771 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100772 if (q->irq_ptr->perf_stat_enabled)
773 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000774 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
775 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200776 break;
777 case SLSB_P_OUTPUT_NOT_INIT:
778 case SLSB_P_OUTPUT_HALTED:
779 break;
780 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200781 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200782 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000783
784out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200785 return q->first_to_check;
786}
787
788/* all buffers processed? */
789static inline int qdio_outbound_q_done(struct qdio_q *q)
790{
791 return atomic_read(&q->nr_buf_used) == 0;
792}
793
794static inline int qdio_outbound_q_moved(struct qdio_q *q)
795{
796 int bufnr;
797
798 bufnr = get_outbound_buffer_frontier(q);
799
Jan Glauber1549d132012-05-09 16:27:34 +0200800 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100801 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100802 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200803 return 1;
804 } else
805 return 0;
806}
807
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000808static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200809{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200810 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100811 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200812
813 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100814 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200815
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100816 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200817retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100818 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100819
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000820 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100821 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200822 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200823 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100824 case 2:
825 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200826 while (++retries < QDIO_BUSY_BIT_RETRIES) {
827 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
828 goto retry;
829 }
830 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200831 cc = -EBUSY;
832 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100833 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200834 cc = -ENOBUFS;
835 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100836 break;
837 case 1:
838 case 3:
839 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200840 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100841 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200842 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200843 if (retries) {
844 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
845 DBF_ERROR("count:%u", retries);
846 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100847 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200848}
849
Jan Glauber779e6e12008-07-17 17:16:48 +0200850static void __qdio_outbound_processing(struct qdio_q *q)
851{
Jan Glauber6486cda2010-01-04 09:05:42 +0100852 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200853 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200854
855 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100856 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200857
Jan Glauberc38f9602009-03-26 15:24:26 +0100858 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200859 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100860 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200861
Jan Glauber779e6e12008-07-17 17:16:48 +0200862 if (q->u.out.pci_out_enabled)
863 return;
864
865 /*
866 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100867 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
868 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200869 */
870 if (qdio_outbound_q_done(q))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200871 del_timer_sync(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100872 else
Ursula Braun9bce8b22016-08-05 12:33:10 +0200873 if (!timer_pending(&q->u.out.timer) &&
874 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber779e6e12008-07-17 17:16:48 +0200875 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100876 return;
877
878sched:
Ursula Braun9bce8b22016-08-05 12:33:10 +0200879 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200880}
881
882/* outbound tasklet */
883void qdio_outbound_processing(unsigned long data)
884{
885 struct qdio_q *q = (struct qdio_q *)data;
886 __qdio_outbound_processing(q);
887}
888
Kees Cookcb9f7802017-10-04 17:54:35 -0700889void qdio_outbound_timer(struct timer_list *t)
Jan Glauber779e6e12008-07-17 17:16:48 +0200890{
Kees Cookcb9f7802017-10-04 17:54:35 -0700891 struct qdio_q *q = from_timer(q, t, u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +0100892
Ursula Braun9bce8b22016-08-05 12:33:10 +0200893 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200894}
895
Jan Glauber60b5df22009-06-22 12:08:10 +0200896static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200897{
898 struct qdio_q *out;
899 int i;
900
901 if (!pci_out_supported(q))
902 return;
903
904 for_each_output_queue(q->irq_ptr, out, i)
905 if (!qdio_outbound_q_done(out))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200906 qdio_tasklet_schedule(out);
Jan Glauber779e6e12008-07-17 17:16:48 +0200907}
908
Jan Glauber60b5df22009-06-22 12:08:10 +0200909static void __tiqdio_inbound_processing(struct qdio_q *q)
910{
Jan Glauber6486cda2010-01-04 09:05:42 +0100911 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100912 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
913 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200914
915 /*
916 * The interrupt could be caused by a PCI request. Check the
917 * PCI capable outbound queues.
918 */
919 qdio_check_outbound_after_thinint(q);
920
921 if (!qdio_inbound_q_moved(q))
922 return;
923
924 qdio_kick_handler(q);
925
Jan Glauber9a2c1602009-06-22 12:08:11 +0200926 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100927 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200928 if (!qdio_tasklet_schedule(q))
Jan Glaubere2910bc2009-09-11 10:28:19 +0200929 return;
Jan Glauber60b5df22009-06-22 12:08:10 +0200930 }
931
932 qdio_stop_polling(q);
933 /*
934 * We need to check again to not lose initiative after
935 * resetting the ACK state.
936 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200937 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100938 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200939 qdio_tasklet_schedule(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200940 }
941}
942
943void tiqdio_inbound_processing(unsigned long data)
944{
945 struct qdio_q *q = (struct qdio_q *)data;
946 __tiqdio_inbound_processing(q);
947}
948
Jan Glauber779e6e12008-07-17 17:16:48 +0200949static inline void qdio_set_state(struct qdio_irq *irq_ptr,
950 enum qdio_irq_states state)
951{
Jan Glauber22f99342008-12-25 13:38:46 +0100952 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200953
954 irq_ptr->state = state;
955 mb();
956}
957
Jan Glauber22f99342008-12-25 13:38:46 +0100958static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200959{
Jan Glauber779e6e12008-07-17 17:16:48 +0200960 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100961 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
962 DBF_ERROR_HEX(irb, 64);
963 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200964 }
965}
966
967/* PCI interrupt handler */
968static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
969{
970 int i;
971 struct qdio_q *q;
972
Ursula Braun9bce8b22016-08-05 12:33:10 +0200973 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
Jan Glauberc38f9602009-03-26 15:24:26 +0100974 return;
975
Jan Glauberd36deae2010-09-07 21:14:39 +0000976 for_each_input_queue(irq_ptr, q, i) {
977 if (q->u.in.queue_start_poll) {
978 /* skip if polling is enabled or already in work */
979 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
980 &q->u.in.queue_irq_state)) {
981 qperf_inc(q, int_discarded);
982 continue;
983 }
984 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
985 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000986 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +0000987 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000988 }
Jan Glauberd36deae2010-09-07 21:14:39 +0000989 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200990
Ursula Braun0f308f42014-01-28 13:06:47 +0100991 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
Jan Glauber779e6e12008-07-17 17:16:48 +0200992 return;
993
994 for_each_output_queue(irq_ptr, q, i) {
995 if (qdio_outbound_q_done(q))
996 continue;
Jan Glauber90adac52011-01-05 12:47:54 +0100997 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +0200998 qdio_siga_sync_q(q);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200999 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001000 }
1001}
1002
1003static void qdio_handle_activate_check(struct ccw_device *cdev,
1004 unsigned long intparm, int cstat, int dstat)
1005{
1006 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1007 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001008 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001009
Jan Glauber22f99342008-12-25 13:38:46 +01001010 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1011 DBF_ERROR("intp :%lx", intparm);
1012 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001013
1014 if (irq_ptr->nr_input_qs) {
1015 q = irq_ptr->input_qs[0];
1016 } else if (irq_ptr->nr_output_qs) {
1017 q = irq_ptr->output_qs[0];
1018 } else {
1019 dump_stack();
1020 goto no_handler;
1021 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001022
1023 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001024 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001025 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001026no_handler:
1027 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001028 /*
1029 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1030 * Therefore we call the LGR detection function here.
1031 */
1032 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001033}
1034
Jan Glauber779e6e12008-07-17 17:16:48 +02001035static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1036 int dstat)
1037{
1038 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001039
Jan Glauber22f99342008-12-25 13:38:46 +01001040 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001041
1042 if (cstat)
1043 goto error;
1044 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1045 goto error;
1046 if (!(dstat & DEV_STAT_DEV_END))
1047 goto error;
1048 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1049 return;
1050
1051error:
1052 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1053 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1054 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001055}
1056
1057/* qdio interrupt handler */
1058void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1059 struct irb *irb)
1060{
1061 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001062 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001063 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001064
Jan Glauber779e6e12008-07-17 17:16:48 +02001065 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001066 ccw_device_get_schid(cdev, &schid);
1067 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001068 return;
1069 }
1070
Jan Glauber09a308f2010-05-17 10:00:14 +02001071 if (irq_ptr->perf_stat_enabled)
1072 irq_ptr->perf_stat.qdio_int++;
1073
Jan Glauber779e6e12008-07-17 17:16:48 +02001074 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001075 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1076 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1077 wake_up(&cdev->private->wait_q);
1078 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001079 }
Jan Glauber22f99342008-12-25 13:38:46 +01001080 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001081 cstat = irb->scsw.cmd.cstat;
1082 dstat = irb->scsw.cmd.dstat;
1083
1084 switch (irq_ptr->state) {
1085 case QDIO_IRQ_STATE_INACTIVE:
1086 qdio_establish_handle_irq(cdev, cstat, dstat);
1087 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001088 case QDIO_IRQ_STATE_CLEANUP:
1089 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1090 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001091 case QDIO_IRQ_STATE_ESTABLISHED:
1092 case QDIO_IRQ_STATE_ACTIVE:
1093 if (cstat & SCHN_STAT_PCI) {
1094 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001095 return;
1096 }
Jan Glauber4c575422009-06-12 10:26:28 +02001097 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001098 qdio_handle_activate_check(cdev, intparm, cstat,
1099 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001100 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001101 case QDIO_IRQ_STATE_STOPPED:
1102 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001103 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001104 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001105 }
1106 wake_up(&cdev->private->wait_q);
1107}
1108
1109/**
1110 * qdio_get_ssqd_desc - get qdio subchannel description
1111 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001112 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001113 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001114 * Returns 0 or an error code. The results of the chsc are stored in the
1115 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001116 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001117int qdio_get_ssqd_desc(struct ccw_device *cdev,
1118 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001119{
Sebastian Ott9080c922016-07-28 20:30:31 +02001120 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001121
Jan Glauberbbd50e12008-12-25 13:38:43 +01001122 if (!cdev || !cdev->private)
1123 return -EINVAL;
1124
Sebastian Ott9080c922016-07-28 20:30:31 +02001125 ccw_device_get_schid(cdev, &schid);
1126 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1127 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001128}
1129EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1130
Jan Glauber779e6e12008-07-17 17:16:48 +02001131static void qdio_shutdown_queues(struct ccw_device *cdev)
1132{
1133 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1134 struct qdio_q *q;
1135 int i;
1136
1137 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001138 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001139
1140 for_each_output_queue(irq_ptr, q, i) {
Ursula Braun9bce8b22016-08-05 12:33:10 +02001141 del_timer_sync(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001142 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001143 }
1144}
1145
1146/**
1147 * qdio_shutdown - shut down a qdio subchannel
1148 * @cdev: associated ccw device
1149 * @how: use halt or clear to shutdown
1150 */
1151int qdio_shutdown(struct ccw_device *cdev, int how)
1152{
Jan Glauber22f99342008-12-25 13:38:46 +01001153 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001154 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001155 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001156
Jan Glauber779e6e12008-07-17 17:16:48 +02001157 if (!irq_ptr)
1158 return -ENODEV;
1159
Jan Glauberce1d8012012-10-24 12:38:35 +02001160 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001161 ccw_device_get_schid(cdev, &schid);
1162 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001163
Jan Glauber779e6e12008-07-17 17:16:48 +02001164 mutex_lock(&irq_ptr->setup_mutex);
1165 /*
1166 * Subchannel was already shot down. We cannot prevent being called
1167 * twice since cio may trigger a shutdown asynchronously.
1168 */
1169 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1170 mutex_unlock(&irq_ptr->setup_mutex);
1171 return 0;
1172 }
1173
Jan Glauberc38f9602009-03-26 15:24:26 +01001174 /*
1175 * Indicate that the device is going down. Scheduling the queue
1176 * tasklets is forbidden from here on.
1177 */
1178 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1179
Jan Glauber779e6e12008-07-17 17:16:48 +02001180 tiqdio_remove_input_queues(irq_ptr);
1181 qdio_shutdown_queues(cdev);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001182 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001183
1184 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001185 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001186
1187 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1188 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1189 else
1190 /* default behaviour is halt */
1191 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1192 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001193 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1194 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001195 goto no_cleanup;
1196 }
1197
1198 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001199 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001200 wait_event_interruptible_timeout(cdev->private->wait_q,
1201 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1202 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1203 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001204 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001205
1206no_cleanup:
1207 qdio_shutdown_thinint(irq_ptr);
1208
1209 /* restore interrupt handler */
Julian Wiedmann89286322018-03-21 17:14:00 +01001210 if ((void *)cdev->handler == (void *)qdio_int_handler) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001211 cdev->handler = irq_ptr->orig_handler;
Julian Wiedmann89286322018-03-21 17:14:00 +01001212 cdev->private->intparm = 0;
1213 }
Sebastian Otta48ed862016-07-29 13:41:20 +02001214 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001215
1216 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1217 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001218 if (rc)
1219 return rc;
1220 return 0;
1221}
1222EXPORT_SYMBOL_GPL(qdio_shutdown);
1223
1224/**
1225 * qdio_free - free data structures for a qdio subchannel
1226 * @cdev: associated ccw device
1227 */
1228int qdio_free(struct ccw_device *cdev)
1229{
Jan Glauber22f99342008-12-25 13:38:46 +01001230 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001231 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001232
Jan Glauber779e6e12008-07-17 17:16:48 +02001233 if (!irq_ptr)
1234 return -ENODEV;
1235
Sebastian Ott9080c922016-07-28 20:30:31 +02001236 ccw_device_get_schid(cdev, &schid);
1237 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001238 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001239 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001240
Stefan Raspl613c4e02014-06-12 14:24:45 +02001241 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001242 cdev->private->qdio_data = NULL;
1243 mutex_unlock(&irq_ptr->setup_mutex);
1244
1245 qdio_release_memory(irq_ptr);
1246 return 0;
1247}
1248EXPORT_SYMBOL_GPL(qdio_free);
1249
1250/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001251 * qdio_allocate - allocate qdio queues and associated data
1252 * @init_data: initialization data
1253 */
1254int qdio_allocate(struct qdio_initialize *init_data)
1255{
Sebastian Ott9080c922016-07-28 20:30:31 +02001256 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001257 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001258
Sebastian Ott9080c922016-07-28 20:30:31 +02001259 ccw_device_get_schid(init_data->cdev, &schid);
1260 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001261
1262 if ((init_data->no_input_qs && !init_data->input_handler) ||
1263 (init_data->no_output_qs && !init_data->output_handler))
1264 return -EINVAL;
1265
1266 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1267 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1268 return -EINVAL;
1269
1270 if ((!init_data->input_sbal_addr_array) ||
1271 (!init_data->output_sbal_addr_array))
1272 return -EINVAL;
1273
Jan Glauber779e6e12008-07-17 17:16:48 +02001274 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1275 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1276 if (!irq_ptr)
1277 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001278
1279 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001280 if (qdio_allocate_dbf(init_data, irq_ptr))
1281 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001282
1283 /*
1284 * Allocate a page for the chsc calls in qdio_establish.
1285 * Must be pre-allocated since a zfcp recovery will call
1286 * qdio_establish. In case of low memory and swap on a zfcp disk
1287 * we may not be able to allocate memory otherwise.
1288 */
1289 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1290 if (!irq_ptr->chsc_page)
1291 goto out_rel;
1292
1293 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001294 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001295 if (!irq_ptr->qdr)
1296 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001297
Jan Glauber779e6e12008-07-17 17:16:48 +02001298 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1299 init_data->no_output_qs))
1300 goto out_rel;
1301
1302 init_data->cdev->private->qdio_data = irq_ptr;
1303 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1304 return 0;
1305out_rel:
1306 qdio_release_memory(irq_ptr);
1307out_err:
1308 return -ENOMEM;
1309}
1310EXPORT_SYMBOL_GPL(qdio_allocate);
1311
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001312static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1313{
1314 struct qdio_q *q = irq_ptr->input_qs[0];
1315 int i, use_cq = 0;
1316
1317 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1318 use_cq = 1;
1319
1320 for_each_output_queue(irq_ptr, q, i) {
1321 if (use_cq) {
1322 if (qdio_enable_async_operation(&q->u.out) < 0) {
1323 use_cq = 0;
1324 continue;
1325 }
1326 } else
1327 qdio_disable_async_operation(&q->u.out);
1328 }
1329 DBF_EVENT("use_cq:%d", use_cq);
1330}
1331
Jan Glauber779e6e12008-07-17 17:16:48 +02001332/**
1333 * qdio_establish - establish queues on a qdio subchannel
1334 * @init_data: initialization data
1335 */
1336int qdio_establish(struct qdio_initialize *init_data)
1337{
Jan Glauber779e6e12008-07-17 17:16:48 +02001338 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001339 struct subchannel_id schid;
1340 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001341 int rc;
1342
Sebastian Ott9080c922016-07-28 20:30:31 +02001343 ccw_device_get_schid(cdev, &schid);
1344 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001345
Jan Glauber779e6e12008-07-17 17:16:48 +02001346 irq_ptr = cdev->private->qdio_data;
1347 if (!irq_ptr)
1348 return -ENODEV;
1349
Jan Glauber779e6e12008-07-17 17:16:48 +02001350 mutex_lock(&irq_ptr->setup_mutex);
1351 qdio_setup_irq(init_data);
1352
1353 rc = qdio_establish_thinint(irq_ptr);
1354 if (rc) {
1355 mutex_unlock(&irq_ptr->setup_mutex);
1356 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1357 return rc;
1358 }
1359
1360 /* establish q */
1361 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1362 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1363 irq_ptr->ccw.count = irq_ptr->equeue.count;
1364 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1365
Sebastian Otta48ed862016-07-29 13:41:20 +02001366 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001367 ccw_device_set_options_mask(cdev, 0);
1368
1369 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001370 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001371 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001372 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1373 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001374 mutex_unlock(&irq_ptr->setup_mutex);
1375 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1376 return rc;
1377 }
1378
1379 wait_event_interruptible_timeout(cdev->private->wait_q,
1380 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1381 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1382
1383 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1384 mutex_unlock(&irq_ptr->setup_mutex);
1385 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1386 return -EIO;
1387 }
1388
1389 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001390
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001391 qdio_detect_hsicq(irq_ptr);
1392
Jan Glauber779e6e12008-07-17 17:16:48 +02001393 /* qebsm is now setup if available, initialize buffer states */
1394 qdio_init_buf_states(irq_ptr);
1395
1396 mutex_unlock(&irq_ptr->setup_mutex);
1397 qdio_print_subchannel_info(irq_ptr, cdev);
1398 qdio_setup_debug_entries(irq_ptr, cdev);
1399 return 0;
1400}
1401EXPORT_SYMBOL_GPL(qdio_establish);
1402
1403/**
1404 * qdio_activate - activate queues on a qdio subchannel
1405 * @cdev: associated cdev
1406 */
1407int qdio_activate(struct ccw_device *cdev)
1408{
Sebastian Ott9080c922016-07-28 20:30:31 +02001409 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001410 struct qdio_irq *irq_ptr;
1411 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001412
Sebastian Ott9080c922016-07-28 20:30:31 +02001413 ccw_device_get_schid(cdev, &schid);
1414 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001415
Jan Glauber779e6e12008-07-17 17:16:48 +02001416 irq_ptr = cdev->private->qdio_data;
1417 if (!irq_ptr)
1418 return -ENODEV;
1419
Jan Glauber779e6e12008-07-17 17:16:48 +02001420 mutex_lock(&irq_ptr->setup_mutex);
1421 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1422 rc = -EBUSY;
1423 goto out;
1424 }
1425
Jan Glauber779e6e12008-07-17 17:16:48 +02001426 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1427 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1428 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1429 irq_ptr->ccw.cda = 0;
1430
Sebastian Otta48ed862016-07-29 13:41:20 +02001431 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001432 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1433
1434 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1435 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001436 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001437 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001438 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1439 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001440 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001441 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001442
1443 if (is_thinint_irq(irq_ptr))
1444 tiqdio_add_input_queues(irq_ptr);
1445
1446 /* wait for subchannel to become active */
1447 msleep(5);
1448
1449 switch (irq_ptr->state) {
1450 case QDIO_IRQ_STATE_STOPPED:
1451 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001452 rc = -EIO;
1453 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001454 default:
1455 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1456 rc = 0;
1457 }
1458out:
1459 mutex_unlock(&irq_ptr->setup_mutex);
1460 return rc;
1461}
1462EXPORT_SYMBOL_GPL(qdio_activate);
1463
1464static inline int buf_in_between(int bufnr, int start, int count)
1465{
1466 int end = add_buf(start, count);
1467
1468 if (end > start) {
1469 if (bufnr >= start && bufnr < end)
1470 return 1;
1471 else
1472 return 0;
1473 }
1474
1475 /* wrap-around case */
1476 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1477 (bufnr < end))
1478 return 1;
1479 else
1480 return 0;
1481}
1482
1483/**
1484 * handle_inbound - reset processed input buffers
1485 * @q: queue containing the buffers
1486 * @callflags: flags
1487 * @bufnr: first buffer to process
1488 * @count: how many buffers are emptied
1489 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001490static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1491 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001492{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001493 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001494
Jan Glauber6486cda2010-01-04 09:05:42 +01001495 qperf_inc(q, inbound_call);
1496
Jan Glauber50f769d2008-12-25 13:38:47 +01001497 if (!q->u.in.polling)
1498 goto set;
1499
1500 /* protect against stop polling setting an ACK for an emptied slsb */
1501 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1502 /* overwriting everything, just delete polling status */
1503 q->u.in.polling = 0;
1504 q->u.in.ack_count = 0;
1505 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001506 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001507 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001508 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001509 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001510 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001511 q->u.in.ack_count -= diff;
1512 if (q->u.in.ack_count <= 0) {
1513 q->u.in.polling = 0;
1514 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001515 goto set;
1516 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001517 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001518 }
1519 else
1520 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001521 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001522 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001523
Jan Glauber50f769d2008-12-25 13:38:47 +01001524set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001525 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001526 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001527
Jan Glauberd303b6f2009-03-26 15:24:31 +01001528 if (need_siga_in(q))
1529 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001530
Jan Glauberd303b6f2009-03-26 15:24:31 +01001531 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001532}
1533
1534/**
1535 * handle_outbound - process filled outbound buffers
1536 * @q: queue containing the buffers
1537 * @callflags: flags
1538 * @bufnr: first buffer to process
1539 * @count: how many buffers are filled
1540 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001541static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1542 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001543{
Jan Glauberc26001d2011-05-23 10:24:38 +02001544 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001545 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001546
Jan Glauber6486cda2010-01-04 09:05:42 +01001547 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001548
1549 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1550 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001551
Jan Glauber01958432011-01-05 12:47:51 +01001552 if (used == QDIO_MAX_BUFFERS_PER_Q)
1553 qperf_inc(q, outbound_queue_full);
1554
Jan Glauber6486cda2010-01-04 09:05:42 +01001555 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001556 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001557 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001558 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001559 q->u.out.pci_out_enabled = 0;
1560
1561 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001562 unsigned long phys_aob = 0;
1563
1564 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001565 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1566
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001567 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1568
1569 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001570 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001571 rc = qdio_siga_sync_q(q);
Julian Wiedmann77868c02019-07-11 18:17:36 +02001572 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1573 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1574 state == SLSB_CU_OUTPUT_PRIMED) {
1575 /* The previous buffer is not processed yet, tack on. */
1576 qperf_inc(q, fast_requeue);
Jan Glauber110da312011-01-05 12:47:53 +01001577 } else {
Julian Wiedmann77868c02019-07-11 18:17:36 +02001578 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001579 }
1580
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001581 /* in case of SIGA errors we must process the error immediately */
1582 if (used >= q->u.out.scan_threshold || rc)
Ursula Braun9bce8b22016-08-05 12:33:10 +02001583 qdio_tasklet_schedule(q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001584 else
1585 /* free the SBALs in case of no further traffic */
Ursula Braun9bce8b22016-08-05 12:33:10 +02001586 if (!timer_pending(&q->u.out.timer) &&
1587 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001588 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001589 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001590}
1591
1592/**
1593 * do_QDIO - process input or output buffers
1594 * @cdev: associated ccw_device for the qdio subchannel
1595 * @callflags: input or output and special flags from the program
1596 * @q_nr: queue number
1597 * @bufnr: buffer number
1598 * @count: how many buffers to process
1599 */
1600int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001601 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001602{
1603 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001604
Jan Glauber66182412009-06-22 12:08:15 +02001605 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001606 return -EINVAL;
1607
Jan Glauber779e6e12008-07-17 17:16:48 +02001608 irq_ptr = cdev->private->qdio_data;
1609 if (!irq_ptr)
1610 return -ENODEV;
1611
Jan Glauber1d7e1502009-09-22 22:58:39 +02001612 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1613 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001614
1615 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001616 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001617 if (!count)
1618 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001619 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001620 return handle_inbound(irq_ptr->input_qs[q_nr],
1621 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001622 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001623 return handle_outbound(irq_ptr->output_qs[q_nr],
1624 callflags, bufnr, count);
1625 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001626}
1627EXPORT_SYMBOL_GPL(do_QDIO);
1628
Jan Glauberd36deae2010-09-07 21:14:39 +00001629/**
1630 * qdio_start_irq - process input buffers
1631 * @cdev: associated ccw_device for the qdio subchannel
1632 * @nr: input queue number
1633 *
1634 * Return codes
1635 * 0 - success
1636 * 1 - irqs not started since new data is available
1637 */
1638int qdio_start_irq(struct ccw_device *cdev, int nr)
1639{
1640 struct qdio_q *q;
1641 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1642
1643 if (!irq_ptr)
1644 return -ENODEV;
1645 q = irq_ptr->input_qs[nr];
1646
Jan Glauber5f4026f2011-10-30 15:17:20 +01001647 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001648 qdio_stop_polling(q);
1649 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1650
1651 /*
1652 * We need to check again to not lose initiative after
1653 * resetting the ACK state.
1654 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001655 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001656 goto rescan;
1657 if (!qdio_inbound_q_done(q))
1658 goto rescan;
1659 return 0;
1660
1661rescan:
1662 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1663 &q->u.in.queue_irq_state))
1664 return 0;
1665 else
1666 return 1;
1667
1668}
1669EXPORT_SYMBOL(qdio_start_irq);
1670
1671/**
1672 * qdio_get_next_buffers - process input buffers
1673 * @cdev: associated ccw_device for the qdio subchannel
1674 * @nr: input queue number
1675 * @bufnr: first filled buffer number
1676 * @error: buffers are in error state
1677 *
1678 * Return codes
1679 * < 0 - error
1680 * = 0 - no new buffers found
1681 * > 0 - number of processed buffers
1682 */
1683int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1684 int *error)
1685{
1686 struct qdio_q *q;
1687 int start, end;
1688 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1689
1690 if (!irq_ptr)
1691 return -ENODEV;
1692 q = irq_ptr->input_qs[nr];
Jan Glauberd36deae2010-09-07 21:14:39 +00001693
Jan Glauberd36deae2010-09-07 21:14:39 +00001694 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001695 * Cannot rely on automatic sync after interrupt since queues may
1696 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001697 */
Jan Glauber90adac52011-01-05 12:47:54 +01001698 if (need_siga_sync(q))
1699 qdio_sync_queues(q);
1700
1701 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001702 qdio_check_outbound_after_thinint(q);
1703
1704 if (!qdio_inbound_q_moved(q))
1705 return 0;
1706
1707 /* Note: upper-layer MUST stop processing immediately here ... */
1708 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1709 return -EIO;
1710
1711 start = q->first_to_kick;
1712 end = q->first_to_check;
1713 *bufnr = start;
1714 *error = q->qdio_error;
1715
1716 /* for the next time */
1717 q->first_to_kick = end;
1718 q->qdio_error = 0;
1719 return sub_buf(end, start);
1720}
1721EXPORT_SYMBOL(qdio_get_next_buffers);
1722
1723/**
1724 * qdio_stop_irq - disable interrupt processing for the device
1725 * @cdev: associated ccw_device for the qdio subchannel
1726 * @nr: input queue number
1727 *
1728 * Return codes
1729 * 0 - interrupts were already disabled
1730 * 1 - interrupts successfully disabled
1731 */
1732int qdio_stop_irq(struct ccw_device *cdev, int nr)
1733{
1734 struct qdio_q *q;
1735 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1736
1737 if (!irq_ptr)
1738 return -ENODEV;
1739 q = irq_ptr->input_qs[nr];
1740
1741 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1742 &q->u.in.queue_irq_state))
1743 return 0;
1744 else
1745 return 1;
1746}
1747EXPORT_SYMBOL(qdio_stop_irq);
1748
Eugene Crosser1c59a862013-04-24 12:00:23 +02001749/**
1750 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1751 * @schid: Subchannel ID.
1752 * @cnc: Boolean Change-Notification Control
1753 * @response: Response code will be stored at this address
1754 * @cb: Callback function will be executed for each element
1755 * of the address list
Eugene Crosser1c59a862013-04-24 12:00:23 +02001756 * @priv: Pointer to pass to the callback function.
1757 *
1758 * Performs "Store-network-bridging-information list" operation and calls
1759 * the callback function for every entry in the list. If "change-
1760 * notification-control" is set, further changes in the address list
1761 * will be reported via the IPA command.
1762 */
1763int qdio_pnso_brinfo(struct subchannel_id schid,
1764 int cnc, u16 *response,
1765 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1766 void *entry),
1767 void *priv)
1768{
1769 struct chsc_pnso_area *rr;
1770 int rc;
1771 u32 prev_instance = 0;
1772 int isfirstblock = 1;
1773 int i, size, elems;
1774
1775 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1776 if (rr == NULL)
1777 return -ENOMEM;
1778 do {
1779 /* on the first iteration, naihdr.resume_token will be zero */
1780 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1781 if (rc != 0 && rc != -EBUSY)
1782 goto out;
1783 if (rr->response.code != 1) {
1784 rc = -EIO;
1785 continue;
1786 } else
1787 rc = 0;
1788
1789 if (cb == NULL)
1790 continue;
1791
1792 size = rr->naihdr.naids;
1793 elems = (rr->response.length -
1794 sizeof(struct chsc_header) -
1795 sizeof(struct chsc_brinfo_naihdr)) /
1796 size;
1797
1798 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1799 /* Inform the caller that they need to scrap */
1800 /* the data that was already reported via cb */
1801 rc = -EAGAIN;
1802 break;
1803 }
1804 isfirstblock = 0;
1805 prev_instance = rr->naihdr.instance;
1806 for (i = 0; i < elems; i++)
1807 switch (size) {
1808 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1809 (*cb)(priv, l3_ipv6_addr,
1810 &rr->entries.l3_ipv6[i]);
1811 break;
1812 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1813 (*cb)(priv, l3_ipv4_addr,
1814 &rr->entries.l3_ipv4[i]);
1815 break;
1816 case sizeof(struct qdio_brinfo_entry_l2):
1817 (*cb)(priv, l2_addr_lnid,
1818 &rr->entries.l2[i]);
1819 break;
1820 default:
1821 WARN_ON_ONCE(1);
1822 rc = -EIO;
1823 goto out;
1824 }
1825 } while (rr->response.code == 0x0107 || /* channel busy */
1826 (rr->response.code == 1 && /* list stored */
1827 /* resume token is non-zero => list incomplete */
1828 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1829 (*response) = rr->response.code;
1830
1831out:
1832 free_page((unsigned long)rr);
1833 return rc;
1834}
1835EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1836
Jan Glauber779e6e12008-07-17 17:16:48 +02001837static int __init init_QDIO(void)
1838{
1839 int rc;
1840
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001841 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001842 if (rc)
1843 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001844 rc = qdio_setup_init();
1845 if (rc)
1846 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001847 rc = tiqdio_allocate_memory();
1848 if (rc)
1849 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001850 rc = tiqdio_register_thinints();
1851 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001852 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001853 return 0;
1854
Jan Glauber779e6e12008-07-17 17:16:48 +02001855out_ti:
1856 tiqdio_free_memory();
1857out_cache:
1858 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001859out_debug:
1860 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001861 return rc;
1862}
1863
1864static void __exit exit_QDIO(void)
1865{
1866 tiqdio_unregister_thinints();
1867 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001868 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001869 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001870}
1871
1872module_init(init_QDIO);
1873module_exit(exit_QDIO);