blob: bd26df85f55945a31ae7d4743606154a8bf316aa [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
101static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
102{
Jan Glauber779e6e12008-07-17 17:16:48 +0200103 /* all done or next buffer state different */
104 if (ccq == 0 || ccq == 32)
105 return 0;
Jan Glauber25f269f2011-10-30 15:17:06 +0100106 /* no buffer processed */
107 if (ccq == 97)
Jan Glauber779e6e12008-07-17 17:16:48 +0200108 return 1;
Jan Glauber25f269f2011-10-30 15:17:06 +0100109 /* not all buffers processed */
110 if (ccq == 96)
111 return 2;
Jan Glauber779e6e12008-07-17 17:16:48 +0200112 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100113 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200114 return -EIO;
115}
116
117/**
118 * qdio_do_eqbs - extract buffer states for QEBSM
119 * @q: queue to manipulate
120 * @state: state of the extracted buffers
121 * @start: buffer number to start at
122 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100123 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200124 *
Coly Li73ac36e2009-01-07 18:09:16 -0800125 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200126 * Stops processing if a state is different from the last buffers state.
127 */
128static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100129 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200130{
Jan Glauber25f269f2011-10-30 15:17:06 +0100131 int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200132 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200133
Jan Glauber6486cda2010-01-04 09:05:42 +0100134 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200135
136 if (!q->is_input_q)
137 nr += q->irq_ptr->nr_input_qs;
138again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100139 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
140 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200141 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100142 if (!rc)
143 return count - tmp_count;
Jan Glauber22f99342008-12-25 13:38:46 +0100144
Jan Glauber779e6e12008-07-17 17:16:48 +0200145 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100146 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200147 goto again;
148 }
149
Jan Glauber25f269f2011-10-30 15:17:06 +0100150 if (rc == 2) {
Jan Glauber25f269f2011-10-30 15:17:06 +0100151 qperf_inc(q, eqbs_partial);
152 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
153 tmp_count);
154 /*
155 * Retry once, if that fails bail out and process the
156 * extracted buffers before trying again.
157 */
158 if (!retried++)
159 goto again;
160 else
161 return count - tmp_count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200162 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100163
164 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
165 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200166 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100167 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100168 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200169}
170
171/**
172 * qdio_do_sqbs - set buffer states for QEBSM
173 * @q: queue to manipulate
174 * @state: new state of the buffers
175 * @start: first buffer number to change
176 * @count: how many buffers to change
177 *
178 * Returns the number of successfully changed buffers.
179 * Does retrying until the specified count of buffer states is set or an
180 * error occurs.
181 */
182static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
183 int count)
184{
185 unsigned int ccq = 0;
186 int tmp_count = count, tmp_start = start;
187 int nr = q->nr;
188 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200189
Jan Glauber50f769d2008-12-25 13:38:47 +0100190 if (!count)
191 return 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100192 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200193
194 if (!q->is_input_q)
195 nr += q->irq_ptr->nr_input_qs;
196again:
197 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
198 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100199 if (!rc) {
Jan Glauberce1d8012012-10-24 12:38:35 +0200200 WARN_ON_ONCE(tmp_count);
Jan Glauber25f269f2011-10-30 15:17:06 +0100201 return count - tmp_count;
202 }
203
204 if (rc == 1 || rc == 2) {
Jan Glauber22f99342008-12-25 13:38:46 +0100205 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100206 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200207 goto again;
208 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100209
210 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
211 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200212 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100213 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100214 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200215}
216
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100217/*
218 * Returns number of examined buffers and their common state in *state.
219 * Requested number of buffers-to-examine must be > 0.
220 */
Jan Glauber779e6e12008-07-17 17:16:48 +0200221static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100222 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000223 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200224{
225 unsigned char __state = 0;
226 int i;
227
Jan Glauber779e6e12008-07-17 17:16:48 +0200228 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100229 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200230
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100231 /* get initial state: */
232 __state = q->slsb.val[bufnr];
233 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
234 __state = SLSB_P_OUTPUT_EMPTY;
235
236 for (i = 1; i < count; i++) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200237 bufnr = next_buf(bufnr);
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100238
239 /* merge PENDING into EMPTY: */
240 if (merge_pending &&
241 q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
242 __state == SLSB_P_OUTPUT_EMPTY)
243 continue;
244
245 /* stop if next state differs from initial state: */
246 if (q->slsb.val[bufnr] != __state)
247 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200248 }
249 *state = __state;
250 return i;
251}
252
Jan Glauber60b5df22009-06-22 12:08:10 +0200253static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
254 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200255{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000256 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200257}
258
259/* wrap-around safe setting of slsb states, returns number of changed buffers */
260static inline int set_buf_states(struct qdio_q *q, int bufnr,
261 unsigned char state, int count)
262{
263 int i;
264
Jan Glauber779e6e12008-07-17 17:16:48 +0200265 if (is_qebsm(q))
266 return qdio_do_sqbs(q, state, bufnr, count);
267
268 for (i = 0; i < count; i++) {
269 xchg(&q->slsb.val[bufnr], state);
270 bufnr = next_buf(bufnr);
271 }
272 return count;
273}
274
275static inline int set_buf_state(struct qdio_q *q, int bufnr,
276 unsigned char state)
277{
278 return set_buf_states(q, bufnr, state, 1);
279}
280
281/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100282static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200283{
284 struct qdio_q *q;
285 int i;
286
287 for_each_input_queue(irq_ptr, q, i)
288 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
289 QDIO_MAX_BUFFERS_PER_Q);
290 for_each_output_queue(irq_ptr, q, i)
291 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
292 QDIO_MAX_BUFFERS_PER_Q);
293}
294
Jan Glauber60b5df22009-06-22 12:08:10 +0200295static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200296 unsigned int input)
297{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100298 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
299 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200300 int cc;
301
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100302 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100303 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200304
Jan Glauber958c0ba2011-01-05 12:47:52 +0100305 if (is_qebsm(q)) {
306 schid = q->irq_ptr->sch_token;
307 fc |= QDIO_SIGA_QEBSM_FLAG;
308 }
309
310 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100311 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100312 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200313 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200314}
315
Jan Glauber60b5df22009-06-22 12:08:10 +0200316static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200317{
318 if (q->is_input_q)
319 return qdio_siga_sync(q, 0, q->mask);
320 else
321 return qdio_siga_sync(q, q->mask, 0);
322}
323
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000324static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
325 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200326{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100327 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
328 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100329 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200330 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000331 unsigned long laob = 0;
332
Eugene Crosserec6674c2015-10-06 15:12:29 +0200333 WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
334 !q->u.out.use_cq));
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000335 if (q->u.out.use_cq && aob != 0) {
336 fc = QDIO_SIGA_WRITEQ;
337 laob = aob;
338 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200339
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100340 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200341 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100342 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200343 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200344again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000345 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200346
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100347 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100348 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200349 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100350
351 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200352 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100353 goto again;
354 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200355 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200356 goto again;
357 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200358 if (retries) {
359 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
360 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
361 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
362 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200363 return cc;
364}
365
366static inline int qdio_siga_input(struct qdio_q *q)
367{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100368 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
369 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200370 int cc;
371
Jan Glauber22f99342008-12-25 13:38:46 +0100372 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100373 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200374
Jan Glauber958c0ba2011-01-05 12:47:52 +0100375 if (is_qebsm(q)) {
376 schid = q->irq_ptr->sch_token;
377 fc |= QDIO_SIGA_QEBSM_FLAG;
378 }
379
380 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100381 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100382 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200383 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200384}
385
Jan Glauber90adac52011-01-05 12:47:54 +0100386#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
387#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
388
389static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200390{
Jan Glauber90adac52011-01-05 12:47:54 +0100391 /* PCI capable outbound queues will also be scanned so sync them too */
392 if (pci_out_supported(q))
393 qdio_siga_sync_all(q);
394 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200395 qdio_siga_sync_q(q);
396}
397
Jan Glauber60b5df22009-06-22 12:08:10 +0200398int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
399 unsigned char *state)
400{
Jan Glauber90adac52011-01-05 12:47:54 +0100401 if (need_siga_sync(q))
402 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000403 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200404}
405
406static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200407{
Jan Glauber50f769d2008-12-25 13:38:47 +0100408 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200409 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100410
Jan Glauber779e6e12008-07-17 17:16:48 +0200411 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100412 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200413
414 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100415 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100416 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100417 q->u.in.ack_count);
418 q->u.in.ack_count = 0;
419 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100420 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200421}
422
Fabian Frederick92bdae52014-06-03 21:55:15 +0200423static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100424{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200425 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100426
427 q->q_stats.nr_sbal_total += count;
428 if (count == QDIO_MAX_BUFFERS_MASK) {
429 q->q_stats.nr_sbals[7]++;
430 return;
431 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200432 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100433 q->q_stats.nr_sbals[pos]++;
434}
435
Jan Glauberbffbbd22011-04-20 10:15:33 +0200436static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200437{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200438 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
439 SLSB_P_OUTPUT_NOT_INIT;
440
Jan Glauber1549d132012-05-09 16:27:34 +0200441 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100442
443 /* special handling for no target buffer empty */
Julian Wiedmannb23481f2017-10-23 09:38:18 +0200444 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
445 q->sbal[q->first_to_check]->element[15].sflags == 0x10) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100446 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200447 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100448 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100449 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100450 }
451
Jan Glauber22f99342008-12-25 13:38:46 +0100452 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
453 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100454 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100455 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec908782011-06-06 14:14:40 +0200456 q->sbal[q->first_to_check]->element[14].sflags,
457 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200458
Jan Glauber2768b2d2011-10-30 15:17:07 +0100459set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200460 /*
461 * Interrupts may be avoided as long as the error is present
462 * so change the buffer state immediately to avoid starvation.
463 */
464 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100465}
Jan Glauber779e6e12008-07-17 17:16:48 +0200466
Jan Glauber50f769d2008-12-25 13:38:47 +0100467static inline void inbound_primed(struct qdio_q *q, int count)
468{
469 int new;
470
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100471 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100472
473 /* for QEBSM the ACK was already set by EQBS */
474 if (is_qebsm(q)) {
475 if (!q->u.in.polling) {
476 q->u.in.polling = 1;
477 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100478 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100479 return;
480 }
481
482 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100483 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100484 q->u.in.ack_count);
485 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100486 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100487 return;
488 }
489
490 /*
491 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
492 * or by the next inbound run.
493 */
494 new = add_buf(q->first_to_check, count - 1);
495 if (q->u.in.polling) {
496 /* reset the previous ACK but first set the new one */
497 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100498 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100499 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100500 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100501 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100502 }
503
Jan Glaubere85dea02009-03-26 15:24:29 +0100504 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100505 count--;
506 if (!count)
507 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200508 /* need to change ALL buffers to get more interrupts */
509 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200510}
511
512static int get_inbound_buffer_frontier(struct qdio_q *q)
513{
Jan Glauber6fa10982011-01-31 11:30:08 +0100514 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100515 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200516
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200517 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100518
Jan Glauber779e6e12008-07-17 17:16:48 +0200519 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200520 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
521 * would return 0.
522 */
523 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100524 if (!count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200525 goto out;
526
Jan Glauber36e3e722009-06-22 12:08:12 +0200527 /*
528 * No siga sync here, as a PCI or we after a thin interrupt
529 * already sync'ed the queues.
530 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000531 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200532 if (!count)
533 goto out;
534
535 switch (state) {
536 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100537 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200538 q->first_to_check = add_buf(q->first_to_check, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200539 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100540 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100541 if (q->irq_ptr->perf_stat_enabled)
542 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200543 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200544 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200545 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200546 q->first_to_check = add_buf(q->first_to_check, count);
Julian Wiedmann0b926ac2017-10-23 09:40:16 +0200547 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
548 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100549 if (q->irq_ptr->perf_stat_enabled)
550 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200551 break;
552 case SLSB_CU_INPUT_EMPTY:
553 case SLSB_P_INPUT_NOT_INIT:
554 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100555 if (q->irq_ptr->perf_stat_enabled)
556 q->q_stats.nr_sbal_nop++;
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100557 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
558 q->nr, q->first_to_check);
Jan Glauber779e6e12008-07-17 17:16:48 +0200559 break;
560 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200561 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200562 }
563out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200564 return q->first_to_check;
565}
566
Jan Glauber60b5df22009-06-22 12:08:10 +0200567static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200568{
569 int bufnr;
570
571 bufnr = get_inbound_buffer_frontier(q);
572
Jan Glauber1549d132012-05-09 16:27:34 +0200573 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100574 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100575 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Heiko Carstens1aae0562013-01-30 09:49:40 +0100576 q->u.in.timestamp = get_tod_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200577 return 1;
578 } else
579 return 0;
580}
581
Jan Glauber9a2c1602009-06-22 12:08:11 +0200582static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200583{
584 unsigned char state = 0;
585
586 if (!atomic_read(&q->nr_buf_used))
587 return 1;
588
Jan Glauber90adac52011-01-05 12:47:54 +0100589 if (need_siga_sync(q))
590 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200591 get_buf_state(q, q->first_to_check, &state, 0);
592
Ursula Braun4c522282010-02-09 09:46:07 +0100593 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200594 /* more work coming */
595 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200596
597 if (is_thinint_irq(q->irq_ptr))
598 return 1;
599
600 /* don't poll under z/VM */
601 if (MACHINE_IS_VM)
602 return 1;
603
604 /*
605 * At this point we know, that inbound first_to_check
606 * has (probably) not moved (see qdio_inbound_processing).
607 */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200608 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200609 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200610 q->first_to_check);
611 return 1;
612 } else
613 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200614}
615
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000616static inline int contains_aobs(struct qdio_q *q)
617{
618 return !q->is_input_q && q->u.out.use_cq;
619}
620
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000621static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
622{
623 unsigned char state = 0;
624 int j, b = start;
625
626 if (!contains_aobs(q))
627 return;
628
629 for (j = 0; j < count; ++j) {
630 get_buf_state(q, b, &state, 0);
631 if (state == SLSB_P_OUTPUT_PENDING) {
632 struct qaob *aob = q->u.out.aobs[b];
633 if (aob == NULL)
634 continue;
635
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000636 q->u.out.sbal_state[b].flags |=
637 QDIO_OUTBUF_STATE_FLAG_PENDING;
638 q->u.out.aobs[b] = NULL;
639 } else if (state == SLSB_P_OUTPUT_EMPTY) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000640 q->u.out.sbal_state[b].aob = NULL;
641 }
642 b = next_buf(b);
643 }
644}
645
646static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
647 int bufnr)
648{
649 unsigned long phys_aob = 0;
650
651 if (!q->use_cq)
652 goto out;
653
654 if (!q->aobs[bufnr]) {
655 struct qaob *aob = qdio_allocate_aob();
656 q->aobs[bufnr] = aob;
657 }
658 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000659 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
660 q->sbal_state[bufnr].aob = q->aobs[bufnr];
661 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
662 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200663 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000664 }
665
666out:
667 return phys_aob;
668}
669
Jan Glauber60b5df22009-06-22 12:08:10 +0200670static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200671{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100672 int start = q->first_to_kick;
673 int end = q->first_to_check;
674 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200675
676 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
677 return;
678
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100679 count = sub_buf(end, start);
680
681 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100682 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200683 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100684 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100685 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200686 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
687 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100688 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100689
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000690 qdio_handle_aobs(q, start, count);
691
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100692 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
693 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200694
695 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100696 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200697 q->qdio_error = 0;
698}
699
Ursula Braun9bce8b22016-08-05 12:33:10 +0200700static inline int qdio_tasklet_schedule(struct qdio_q *q)
701{
702 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
703 tasklet_schedule(&q->tasklet);
704 return 0;
705 }
706 return -EPERM;
707}
708
Jan Glauber779e6e12008-07-17 17:16:48 +0200709static void __qdio_inbound_processing(struct qdio_q *q)
710{
Jan Glauber6486cda2010-01-04 09:05:42 +0100711 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200712
Jan Glauber779e6e12008-07-17 17:16:48 +0200713 if (!qdio_inbound_q_moved(q))
714 return;
715
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100716 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200717
Jan Glauber6486cda2010-01-04 09:05:42 +0100718 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200719 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100720 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200721 if (!qdio_tasklet_schedule(q))
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200722 return;
Jan Glauber6486cda2010-01-04 09:05:42 +0100723 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200724
725 qdio_stop_polling(q);
726 /*
727 * We need to check again to not lose initiative after
728 * resetting the ACK state.
729 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100730 if (!qdio_inbound_q_done(q)) {
731 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200732 qdio_tasklet_schedule(q);
Jan Glauber6486cda2010-01-04 09:05:42 +0100733 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200734}
735
Jan Glauber779e6e12008-07-17 17:16:48 +0200736void qdio_inbound_processing(unsigned long data)
737{
738 struct qdio_q *q = (struct qdio_q *)data;
739 __qdio_inbound_processing(q);
740}
741
742static int get_outbound_buffer_frontier(struct qdio_q *q)
743{
Jan Glauber6fa10982011-01-31 11:30:08 +0100744 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100745 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200746
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200747 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100748
Jan Glauber90adac52011-01-05 12:47:54 +0100749 if (need_siga_sync(q))
750 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
751 !pci_out_supported(q)) ||
752 (queue_type(q) == QDIO_IQDIO_QFMT &&
753 multicast_outbound(q)))
754 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200755
756 /*
757 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
758 * would return 0.
759 */
760 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100761 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000762 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200763
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000764 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200765 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000766 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200767
768 switch (state) {
769 case SLSB_P_OUTPUT_EMPTY:
770 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000771 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
772 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200773
774 atomic_sub(count, &q->nr_buf_used);
775 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100776 if (q->irq_ptr->perf_stat_enabled)
777 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000778
Jan Glauber36e3e722009-06-22 12:08:12 +0200779 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200780 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200781 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200782 q->first_to_check = add_buf(q->first_to_check, count);
783 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100784 if (q->irq_ptr->perf_stat_enabled)
785 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200786 break;
787 case SLSB_CU_OUTPUT_PRIMED:
788 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100789 if (q->irq_ptr->perf_stat_enabled)
790 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000791 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
792 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200793 break;
794 case SLSB_P_OUTPUT_NOT_INIT:
795 case SLSB_P_OUTPUT_HALTED:
796 break;
797 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200798 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200799 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000800
801out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200802 return q->first_to_check;
803}
804
805/* all buffers processed? */
806static inline int qdio_outbound_q_done(struct qdio_q *q)
807{
808 return atomic_read(&q->nr_buf_used) == 0;
809}
810
811static inline int qdio_outbound_q_moved(struct qdio_q *q)
812{
813 int bufnr;
814
815 bufnr = get_outbound_buffer_frontier(q);
816
Jan Glauber1549d132012-05-09 16:27:34 +0200817 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100818 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100819 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200820 return 1;
821 } else
822 return 0;
823}
824
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000825static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200826{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200827 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100828 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200829
830 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100831 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200832
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100833 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200834retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100835 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100836
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000837 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100838 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200839 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200840 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100841 case 2:
842 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200843 while (++retries < QDIO_BUSY_BIT_RETRIES) {
844 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
845 goto retry;
846 }
847 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200848 cc = -EBUSY;
849 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100850 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200851 cc = -ENOBUFS;
852 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100853 break;
854 case 1:
855 case 3:
856 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200857 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100858 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200859 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200860 if (retries) {
861 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
862 DBF_ERROR("count:%u", retries);
863 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100864 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200865}
866
Jan Glauber779e6e12008-07-17 17:16:48 +0200867static void __qdio_outbound_processing(struct qdio_q *q)
868{
Jan Glauber6486cda2010-01-04 09:05:42 +0100869 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200870 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200871
872 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100873 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200874
Jan Glauberc38f9602009-03-26 15:24:26 +0100875 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200876 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100877 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200878
Jan Glauber779e6e12008-07-17 17:16:48 +0200879 if (q->u.out.pci_out_enabled)
880 return;
881
882 /*
883 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100884 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
885 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200886 */
887 if (qdio_outbound_q_done(q))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200888 del_timer_sync(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100889 else
Ursula Braun9bce8b22016-08-05 12:33:10 +0200890 if (!timer_pending(&q->u.out.timer) &&
891 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber779e6e12008-07-17 17:16:48 +0200892 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100893 return;
894
895sched:
Ursula Braun9bce8b22016-08-05 12:33:10 +0200896 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200897}
898
899/* outbound tasklet */
900void qdio_outbound_processing(unsigned long data)
901{
902 struct qdio_q *q = (struct qdio_q *)data;
903 __qdio_outbound_processing(q);
904}
905
Kees Cookcb9f7802017-10-04 17:54:35 -0700906void qdio_outbound_timer(struct timer_list *t)
Jan Glauber779e6e12008-07-17 17:16:48 +0200907{
Kees Cookcb9f7802017-10-04 17:54:35 -0700908 struct qdio_q *q = from_timer(q, t, u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +0100909
Ursula Braun9bce8b22016-08-05 12:33:10 +0200910 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200911}
912
Jan Glauber60b5df22009-06-22 12:08:10 +0200913static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200914{
915 struct qdio_q *out;
916 int i;
917
918 if (!pci_out_supported(q))
919 return;
920
921 for_each_output_queue(q->irq_ptr, out, i)
922 if (!qdio_outbound_q_done(out))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200923 qdio_tasklet_schedule(out);
Jan Glauber779e6e12008-07-17 17:16:48 +0200924}
925
Jan Glauber60b5df22009-06-22 12:08:10 +0200926static void __tiqdio_inbound_processing(struct qdio_q *q)
927{
Jan Glauber6486cda2010-01-04 09:05:42 +0100928 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100929 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
930 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200931
932 /*
933 * The interrupt could be caused by a PCI request. Check the
934 * PCI capable outbound queues.
935 */
936 qdio_check_outbound_after_thinint(q);
937
938 if (!qdio_inbound_q_moved(q))
939 return;
940
941 qdio_kick_handler(q);
942
Jan Glauber9a2c1602009-06-22 12:08:11 +0200943 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100944 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200945 if (!qdio_tasklet_schedule(q))
Jan Glaubere2910bc2009-09-11 10:28:19 +0200946 return;
Jan Glauber60b5df22009-06-22 12:08:10 +0200947 }
948
949 qdio_stop_polling(q);
950 /*
951 * We need to check again to not lose initiative after
952 * resetting the ACK state.
953 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200954 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100955 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200956 qdio_tasklet_schedule(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200957 }
958}
959
960void tiqdio_inbound_processing(unsigned long data)
961{
962 struct qdio_q *q = (struct qdio_q *)data;
963 __tiqdio_inbound_processing(q);
964}
965
Jan Glauber779e6e12008-07-17 17:16:48 +0200966static inline void qdio_set_state(struct qdio_irq *irq_ptr,
967 enum qdio_irq_states state)
968{
Jan Glauber22f99342008-12-25 13:38:46 +0100969 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200970
971 irq_ptr->state = state;
972 mb();
973}
974
Jan Glauber22f99342008-12-25 13:38:46 +0100975static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200976{
Jan Glauber779e6e12008-07-17 17:16:48 +0200977 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100978 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
979 DBF_ERROR_HEX(irb, 64);
980 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200981 }
982}
983
984/* PCI interrupt handler */
985static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
986{
987 int i;
988 struct qdio_q *q;
989
Ursula Braun9bce8b22016-08-05 12:33:10 +0200990 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
Jan Glauberc38f9602009-03-26 15:24:26 +0100991 return;
992
Jan Glauberd36deae2010-09-07 21:14:39 +0000993 for_each_input_queue(irq_ptr, q, i) {
994 if (q->u.in.queue_start_poll) {
995 /* skip if polling is enabled or already in work */
996 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
997 &q->u.in.queue_irq_state)) {
998 qperf_inc(q, int_discarded);
999 continue;
1000 }
1001 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1002 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001003 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +00001004 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001005 }
Jan Glauberd36deae2010-09-07 21:14:39 +00001006 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001007
Ursula Braun0f308f42014-01-28 13:06:47 +01001008 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
Jan Glauber779e6e12008-07-17 17:16:48 +02001009 return;
1010
1011 for_each_output_queue(irq_ptr, q, i) {
1012 if (qdio_outbound_q_done(q))
1013 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001014 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001015 qdio_siga_sync_q(q);
Ursula Braun9bce8b22016-08-05 12:33:10 +02001016 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001017 }
1018}
1019
1020static void qdio_handle_activate_check(struct ccw_device *cdev,
1021 unsigned long intparm, int cstat, int dstat)
1022{
1023 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1024 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001025 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001026
Jan Glauber22f99342008-12-25 13:38:46 +01001027 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1028 DBF_ERROR("intp :%lx", intparm);
1029 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001030
1031 if (irq_ptr->nr_input_qs) {
1032 q = irq_ptr->input_qs[0];
1033 } else if (irq_ptr->nr_output_qs) {
1034 q = irq_ptr->output_qs[0];
1035 } else {
1036 dump_stack();
1037 goto no_handler;
1038 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001039
1040 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001041 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001042 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001043no_handler:
1044 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001045 /*
1046 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1047 * Therefore we call the LGR detection function here.
1048 */
1049 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001050}
1051
Jan Glauber779e6e12008-07-17 17:16:48 +02001052static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1053 int dstat)
1054{
1055 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001056
Jan Glauber22f99342008-12-25 13:38:46 +01001057 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001058
1059 if (cstat)
1060 goto error;
1061 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1062 goto error;
1063 if (!(dstat & DEV_STAT_DEV_END))
1064 goto error;
1065 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1066 return;
1067
1068error:
1069 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1070 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1071 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001072}
1073
1074/* qdio interrupt handler */
1075void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1076 struct irb *irb)
1077{
1078 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001079 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001080 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001081
Jan Glauber779e6e12008-07-17 17:16:48 +02001082 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001083 ccw_device_get_schid(cdev, &schid);
1084 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001085 return;
1086 }
1087
Jan Glauber09a308f2010-05-17 10:00:14 +02001088 if (irq_ptr->perf_stat_enabled)
1089 irq_ptr->perf_stat.qdio_int++;
1090
Jan Glauber779e6e12008-07-17 17:16:48 +02001091 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001092 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1093 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1094 wake_up(&cdev->private->wait_q);
1095 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001096 }
Jan Glauber22f99342008-12-25 13:38:46 +01001097 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001098 cstat = irb->scsw.cmd.cstat;
1099 dstat = irb->scsw.cmd.dstat;
1100
1101 switch (irq_ptr->state) {
1102 case QDIO_IRQ_STATE_INACTIVE:
1103 qdio_establish_handle_irq(cdev, cstat, dstat);
1104 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001105 case QDIO_IRQ_STATE_CLEANUP:
1106 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1107 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001108 case QDIO_IRQ_STATE_ESTABLISHED:
1109 case QDIO_IRQ_STATE_ACTIVE:
1110 if (cstat & SCHN_STAT_PCI) {
1111 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001112 return;
1113 }
Jan Glauber4c575422009-06-12 10:26:28 +02001114 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001115 qdio_handle_activate_check(cdev, intparm, cstat,
1116 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001117 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001118 case QDIO_IRQ_STATE_STOPPED:
1119 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001120 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001121 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001122 }
1123 wake_up(&cdev->private->wait_q);
1124}
1125
1126/**
1127 * qdio_get_ssqd_desc - get qdio subchannel description
1128 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001129 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001130 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001131 * Returns 0 or an error code. The results of the chsc are stored in the
1132 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001133 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001134int qdio_get_ssqd_desc(struct ccw_device *cdev,
1135 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001136{
Sebastian Ott9080c922016-07-28 20:30:31 +02001137 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001138
Jan Glauberbbd50e12008-12-25 13:38:43 +01001139 if (!cdev || !cdev->private)
1140 return -EINVAL;
1141
Sebastian Ott9080c922016-07-28 20:30:31 +02001142 ccw_device_get_schid(cdev, &schid);
1143 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1144 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001145}
1146EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1147
Jan Glauber779e6e12008-07-17 17:16:48 +02001148static void qdio_shutdown_queues(struct ccw_device *cdev)
1149{
1150 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1151 struct qdio_q *q;
1152 int i;
1153
1154 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001155 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001156
1157 for_each_output_queue(irq_ptr, q, i) {
Ursula Braun9bce8b22016-08-05 12:33:10 +02001158 del_timer_sync(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001159 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001160 }
1161}
1162
1163/**
1164 * qdio_shutdown - shut down a qdio subchannel
1165 * @cdev: associated ccw device
1166 * @how: use halt or clear to shutdown
1167 */
1168int qdio_shutdown(struct ccw_device *cdev, int how)
1169{
Jan Glauber22f99342008-12-25 13:38:46 +01001170 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001171 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001172 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001173
Jan Glauber779e6e12008-07-17 17:16:48 +02001174 if (!irq_ptr)
1175 return -ENODEV;
1176
Jan Glauberce1d8012012-10-24 12:38:35 +02001177 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001178 ccw_device_get_schid(cdev, &schid);
1179 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001180
Jan Glauber779e6e12008-07-17 17:16:48 +02001181 mutex_lock(&irq_ptr->setup_mutex);
1182 /*
1183 * Subchannel was already shot down. We cannot prevent being called
1184 * twice since cio may trigger a shutdown asynchronously.
1185 */
1186 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1187 mutex_unlock(&irq_ptr->setup_mutex);
1188 return 0;
1189 }
1190
Jan Glauberc38f9602009-03-26 15:24:26 +01001191 /*
1192 * Indicate that the device is going down. Scheduling the queue
1193 * tasklets is forbidden from here on.
1194 */
1195 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1196
Jan Glauber779e6e12008-07-17 17:16:48 +02001197 tiqdio_remove_input_queues(irq_ptr);
1198 qdio_shutdown_queues(cdev);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001199 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001200
1201 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001202 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001203
1204 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1205 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1206 else
1207 /* default behaviour is halt */
1208 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1209 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001210 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1211 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001212 goto no_cleanup;
1213 }
1214
1215 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001216 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001217 wait_event_interruptible_timeout(cdev->private->wait_q,
1218 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1219 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1220 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001221 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001222
1223no_cleanup:
1224 qdio_shutdown_thinint(irq_ptr);
1225
1226 /* restore interrupt handler */
1227 if ((void *)cdev->handler == (void *)qdio_int_handler)
1228 cdev->handler = irq_ptr->orig_handler;
Sebastian Otta48ed862016-07-29 13:41:20 +02001229 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001230
1231 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1232 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001233 if (rc)
1234 return rc;
1235 return 0;
1236}
1237EXPORT_SYMBOL_GPL(qdio_shutdown);
1238
1239/**
1240 * qdio_free - free data structures for a qdio subchannel
1241 * @cdev: associated ccw device
1242 */
1243int qdio_free(struct ccw_device *cdev)
1244{
Jan Glauber22f99342008-12-25 13:38:46 +01001245 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001246 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001247
Jan Glauber779e6e12008-07-17 17:16:48 +02001248 if (!irq_ptr)
1249 return -ENODEV;
1250
Sebastian Ott9080c922016-07-28 20:30:31 +02001251 ccw_device_get_schid(cdev, &schid);
1252 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001253 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001254 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001255
Stefan Raspl613c4e02014-06-12 14:24:45 +02001256 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001257 cdev->private->qdio_data = NULL;
1258 mutex_unlock(&irq_ptr->setup_mutex);
1259
1260 qdio_release_memory(irq_ptr);
1261 return 0;
1262}
1263EXPORT_SYMBOL_GPL(qdio_free);
1264
1265/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001266 * qdio_allocate - allocate qdio queues and associated data
1267 * @init_data: initialization data
1268 */
1269int qdio_allocate(struct qdio_initialize *init_data)
1270{
Sebastian Ott9080c922016-07-28 20:30:31 +02001271 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001272 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001273
Sebastian Ott9080c922016-07-28 20:30:31 +02001274 ccw_device_get_schid(init_data->cdev, &schid);
1275 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001276
1277 if ((init_data->no_input_qs && !init_data->input_handler) ||
1278 (init_data->no_output_qs && !init_data->output_handler))
1279 return -EINVAL;
1280
1281 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1282 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1283 return -EINVAL;
1284
1285 if ((!init_data->input_sbal_addr_array) ||
1286 (!init_data->output_sbal_addr_array))
1287 return -EINVAL;
1288
Jan Glauber779e6e12008-07-17 17:16:48 +02001289 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1290 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1291 if (!irq_ptr)
1292 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001293
1294 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001295 if (qdio_allocate_dbf(init_data, irq_ptr))
1296 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001297
1298 /*
1299 * Allocate a page for the chsc calls in qdio_establish.
1300 * Must be pre-allocated since a zfcp recovery will call
1301 * qdio_establish. In case of low memory and swap on a zfcp disk
1302 * we may not be able to allocate memory otherwise.
1303 */
1304 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1305 if (!irq_ptr->chsc_page)
1306 goto out_rel;
1307
1308 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001309 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001310 if (!irq_ptr->qdr)
1311 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001312
Jan Glauber779e6e12008-07-17 17:16:48 +02001313 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1314 init_data->no_output_qs))
1315 goto out_rel;
1316
1317 init_data->cdev->private->qdio_data = irq_ptr;
1318 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1319 return 0;
1320out_rel:
1321 qdio_release_memory(irq_ptr);
1322out_err:
1323 return -ENOMEM;
1324}
1325EXPORT_SYMBOL_GPL(qdio_allocate);
1326
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001327static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1328{
1329 struct qdio_q *q = irq_ptr->input_qs[0];
1330 int i, use_cq = 0;
1331
1332 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1333 use_cq = 1;
1334
1335 for_each_output_queue(irq_ptr, q, i) {
1336 if (use_cq) {
1337 if (qdio_enable_async_operation(&q->u.out) < 0) {
1338 use_cq = 0;
1339 continue;
1340 }
1341 } else
1342 qdio_disable_async_operation(&q->u.out);
1343 }
1344 DBF_EVENT("use_cq:%d", use_cq);
1345}
1346
Jan Glauber779e6e12008-07-17 17:16:48 +02001347/**
1348 * qdio_establish - establish queues on a qdio subchannel
1349 * @init_data: initialization data
1350 */
1351int qdio_establish(struct qdio_initialize *init_data)
1352{
Jan Glauber779e6e12008-07-17 17:16:48 +02001353 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001354 struct subchannel_id schid;
1355 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001356 int rc;
1357
Sebastian Ott9080c922016-07-28 20:30:31 +02001358 ccw_device_get_schid(cdev, &schid);
1359 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001360
Jan Glauber779e6e12008-07-17 17:16:48 +02001361 irq_ptr = cdev->private->qdio_data;
1362 if (!irq_ptr)
1363 return -ENODEV;
1364
Jan Glauber779e6e12008-07-17 17:16:48 +02001365 mutex_lock(&irq_ptr->setup_mutex);
1366 qdio_setup_irq(init_data);
1367
1368 rc = qdio_establish_thinint(irq_ptr);
1369 if (rc) {
1370 mutex_unlock(&irq_ptr->setup_mutex);
1371 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1372 return rc;
1373 }
1374
1375 /* establish q */
1376 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1377 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1378 irq_ptr->ccw.count = irq_ptr->equeue.count;
1379 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1380
Sebastian Otta48ed862016-07-29 13:41:20 +02001381 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001382 ccw_device_set_options_mask(cdev, 0);
1383
1384 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001385 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001386 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001387 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1388 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001389 mutex_unlock(&irq_ptr->setup_mutex);
1390 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1391 return rc;
1392 }
1393
1394 wait_event_interruptible_timeout(cdev->private->wait_q,
1395 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1396 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1397
1398 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1399 mutex_unlock(&irq_ptr->setup_mutex);
1400 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1401 return -EIO;
1402 }
1403
1404 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001405
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001406 qdio_detect_hsicq(irq_ptr);
1407
Jan Glauber779e6e12008-07-17 17:16:48 +02001408 /* qebsm is now setup if available, initialize buffer states */
1409 qdio_init_buf_states(irq_ptr);
1410
1411 mutex_unlock(&irq_ptr->setup_mutex);
1412 qdio_print_subchannel_info(irq_ptr, cdev);
1413 qdio_setup_debug_entries(irq_ptr, cdev);
1414 return 0;
1415}
1416EXPORT_SYMBOL_GPL(qdio_establish);
1417
1418/**
1419 * qdio_activate - activate queues on a qdio subchannel
1420 * @cdev: associated cdev
1421 */
1422int qdio_activate(struct ccw_device *cdev)
1423{
Sebastian Ott9080c922016-07-28 20:30:31 +02001424 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001425 struct qdio_irq *irq_ptr;
1426 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001427
Sebastian Ott9080c922016-07-28 20:30:31 +02001428 ccw_device_get_schid(cdev, &schid);
1429 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001430
Jan Glauber779e6e12008-07-17 17:16:48 +02001431 irq_ptr = cdev->private->qdio_data;
1432 if (!irq_ptr)
1433 return -ENODEV;
1434
Jan Glauber779e6e12008-07-17 17:16:48 +02001435 mutex_lock(&irq_ptr->setup_mutex);
1436 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1437 rc = -EBUSY;
1438 goto out;
1439 }
1440
Jan Glauber779e6e12008-07-17 17:16:48 +02001441 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1442 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1443 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1444 irq_ptr->ccw.cda = 0;
1445
Sebastian Otta48ed862016-07-29 13:41:20 +02001446 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001447 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1448
1449 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1450 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001451 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001452 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001453 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1454 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001455 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001456 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001457
1458 if (is_thinint_irq(irq_ptr))
1459 tiqdio_add_input_queues(irq_ptr);
1460
1461 /* wait for subchannel to become active */
1462 msleep(5);
1463
1464 switch (irq_ptr->state) {
1465 case QDIO_IRQ_STATE_STOPPED:
1466 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001467 rc = -EIO;
1468 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001469 default:
1470 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1471 rc = 0;
1472 }
1473out:
1474 mutex_unlock(&irq_ptr->setup_mutex);
1475 return rc;
1476}
1477EXPORT_SYMBOL_GPL(qdio_activate);
1478
1479static inline int buf_in_between(int bufnr, int start, int count)
1480{
1481 int end = add_buf(start, count);
1482
1483 if (end > start) {
1484 if (bufnr >= start && bufnr < end)
1485 return 1;
1486 else
1487 return 0;
1488 }
1489
1490 /* wrap-around case */
1491 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1492 (bufnr < end))
1493 return 1;
1494 else
1495 return 0;
1496}
1497
1498/**
1499 * handle_inbound - reset processed input buffers
1500 * @q: queue containing the buffers
1501 * @callflags: flags
1502 * @bufnr: first buffer to process
1503 * @count: how many buffers are emptied
1504 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001505static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1506 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001507{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001508 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001509
Jan Glauber6486cda2010-01-04 09:05:42 +01001510 qperf_inc(q, inbound_call);
1511
Jan Glauber50f769d2008-12-25 13:38:47 +01001512 if (!q->u.in.polling)
1513 goto set;
1514
1515 /* protect against stop polling setting an ACK for an emptied slsb */
1516 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1517 /* overwriting everything, just delete polling status */
1518 q->u.in.polling = 0;
1519 q->u.in.ack_count = 0;
1520 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001521 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001522 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001523 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001524 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001525 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001526 q->u.in.ack_count -= diff;
1527 if (q->u.in.ack_count <= 0) {
1528 q->u.in.polling = 0;
1529 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001530 goto set;
1531 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001532 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001533 }
1534 else
1535 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001536 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001537 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001538
Jan Glauber50f769d2008-12-25 13:38:47 +01001539set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001540 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001541 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001542
Jan Glauberd303b6f2009-03-26 15:24:31 +01001543 if (need_siga_in(q))
1544 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001545
Jan Glauberd303b6f2009-03-26 15:24:31 +01001546 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001547}
1548
1549/**
1550 * handle_outbound - process filled outbound buffers
1551 * @q: queue containing the buffers
1552 * @callflags: flags
1553 * @bufnr: first buffer to process
1554 * @count: how many buffers are filled
1555 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001556static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1557 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001558{
Jan Glauberc26001d2011-05-23 10:24:38 +02001559 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001560 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001561
Jan Glauber6486cda2010-01-04 09:05:42 +01001562 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001563
1564 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1565 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001566
Jan Glauber01958432011-01-05 12:47:51 +01001567 if (used == QDIO_MAX_BUFFERS_PER_Q)
1568 qperf_inc(q, outbound_queue_full);
1569
Jan Glauber6486cda2010-01-04 09:05:42 +01001570 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001571 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001572 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001573 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001574 q->u.out.pci_out_enabled = 0;
1575
1576 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001577 unsigned long phys_aob = 0;
1578
1579 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001580 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1581
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001582 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1583
1584 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001585 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001586 rc = qdio_siga_sync_q(q);
1587 } else {
1588 /* try to fast requeue buffers */
1589 get_buf_state(q, prev_buf(bufnr), &state, 0);
1590 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001591 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001592 else
Jan Glauber110da312011-01-05 12:47:53 +01001593 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001594 }
1595
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001596 /* in case of SIGA errors we must process the error immediately */
1597 if (used >= q->u.out.scan_threshold || rc)
Ursula Braun9bce8b22016-08-05 12:33:10 +02001598 qdio_tasklet_schedule(q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001599 else
1600 /* free the SBALs in case of no further traffic */
Ursula Braun9bce8b22016-08-05 12:33:10 +02001601 if (!timer_pending(&q->u.out.timer) &&
1602 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001603 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001604 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001605}
1606
1607/**
1608 * do_QDIO - process input or output buffers
1609 * @cdev: associated ccw_device for the qdio subchannel
1610 * @callflags: input or output and special flags from the program
1611 * @q_nr: queue number
1612 * @bufnr: buffer number
1613 * @count: how many buffers to process
1614 */
1615int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001616 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001617{
1618 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001619
Jan Glauber66182412009-06-22 12:08:15 +02001620 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001621 return -EINVAL;
1622
Jan Glauber779e6e12008-07-17 17:16:48 +02001623 irq_ptr = cdev->private->qdio_data;
1624 if (!irq_ptr)
1625 return -ENODEV;
1626
Jan Glauber1d7e1502009-09-22 22:58:39 +02001627 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1628 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001629
1630 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001631 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001632 if (!count)
1633 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001634 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001635 return handle_inbound(irq_ptr->input_qs[q_nr],
1636 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001637 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001638 return handle_outbound(irq_ptr->output_qs[q_nr],
1639 callflags, bufnr, count);
1640 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001641}
1642EXPORT_SYMBOL_GPL(do_QDIO);
1643
Jan Glauberd36deae2010-09-07 21:14:39 +00001644/**
1645 * qdio_start_irq - process input buffers
1646 * @cdev: associated ccw_device for the qdio subchannel
1647 * @nr: input queue number
1648 *
1649 * Return codes
1650 * 0 - success
1651 * 1 - irqs not started since new data is available
1652 */
1653int qdio_start_irq(struct ccw_device *cdev, int nr)
1654{
1655 struct qdio_q *q;
1656 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1657
1658 if (!irq_ptr)
1659 return -ENODEV;
1660 q = irq_ptr->input_qs[nr];
1661
Jan Glauber5f4026f2011-10-30 15:17:20 +01001662 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001663 qdio_stop_polling(q);
1664 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1665
1666 /*
1667 * We need to check again to not lose initiative after
1668 * resetting the ACK state.
1669 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001670 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001671 goto rescan;
1672 if (!qdio_inbound_q_done(q))
1673 goto rescan;
1674 return 0;
1675
1676rescan:
1677 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1678 &q->u.in.queue_irq_state))
1679 return 0;
1680 else
1681 return 1;
1682
1683}
1684EXPORT_SYMBOL(qdio_start_irq);
1685
1686/**
1687 * qdio_get_next_buffers - process input buffers
1688 * @cdev: associated ccw_device for the qdio subchannel
1689 * @nr: input queue number
1690 * @bufnr: first filled buffer number
1691 * @error: buffers are in error state
1692 *
1693 * Return codes
1694 * < 0 - error
1695 * = 0 - no new buffers found
1696 * > 0 - number of processed buffers
1697 */
1698int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1699 int *error)
1700{
1701 struct qdio_q *q;
1702 int start, end;
1703 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1704
1705 if (!irq_ptr)
1706 return -ENODEV;
1707 q = irq_ptr->input_qs[nr];
Jan Glauberd36deae2010-09-07 21:14:39 +00001708
Jan Glauberd36deae2010-09-07 21:14:39 +00001709 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001710 * Cannot rely on automatic sync after interrupt since queues may
1711 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001712 */
Jan Glauber90adac52011-01-05 12:47:54 +01001713 if (need_siga_sync(q))
1714 qdio_sync_queues(q);
1715
1716 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001717 qdio_check_outbound_after_thinint(q);
1718
1719 if (!qdio_inbound_q_moved(q))
1720 return 0;
1721
1722 /* Note: upper-layer MUST stop processing immediately here ... */
1723 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1724 return -EIO;
1725
1726 start = q->first_to_kick;
1727 end = q->first_to_check;
1728 *bufnr = start;
1729 *error = q->qdio_error;
1730
1731 /* for the next time */
1732 q->first_to_kick = end;
1733 q->qdio_error = 0;
1734 return sub_buf(end, start);
1735}
1736EXPORT_SYMBOL(qdio_get_next_buffers);
1737
1738/**
1739 * qdio_stop_irq - disable interrupt processing for the device
1740 * @cdev: associated ccw_device for the qdio subchannel
1741 * @nr: input queue number
1742 *
1743 * Return codes
1744 * 0 - interrupts were already disabled
1745 * 1 - interrupts successfully disabled
1746 */
1747int qdio_stop_irq(struct ccw_device *cdev, int nr)
1748{
1749 struct qdio_q *q;
1750 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1751
1752 if (!irq_ptr)
1753 return -ENODEV;
1754 q = irq_ptr->input_qs[nr];
1755
1756 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1757 &q->u.in.queue_irq_state))
1758 return 0;
1759 else
1760 return 1;
1761}
1762EXPORT_SYMBOL(qdio_stop_irq);
1763
Eugene Crosser1c59a862013-04-24 12:00:23 +02001764/**
1765 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1766 * @schid: Subchannel ID.
1767 * @cnc: Boolean Change-Notification Control
1768 * @response: Response code will be stored at this address
1769 * @cb: Callback function will be executed for each element
1770 * of the address list
Eugene Crosser1c59a862013-04-24 12:00:23 +02001771 * @priv: Pointer to pass to the callback function.
1772 *
1773 * Performs "Store-network-bridging-information list" operation and calls
1774 * the callback function for every entry in the list. If "change-
1775 * notification-control" is set, further changes in the address list
1776 * will be reported via the IPA command.
1777 */
1778int qdio_pnso_brinfo(struct subchannel_id schid,
1779 int cnc, u16 *response,
1780 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1781 void *entry),
1782 void *priv)
1783{
1784 struct chsc_pnso_area *rr;
1785 int rc;
1786 u32 prev_instance = 0;
1787 int isfirstblock = 1;
1788 int i, size, elems;
1789
1790 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1791 if (rr == NULL)
1792 return -ENOMEM;
1793 do {
1794 /* on the first iteration, naihdr.resume_token will be zero */
1795 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1796 if (rc != 0 && rc != -EBUSY)
1797 goto out;
1798 if (rr->response.code != 1) {
1799 rc = -EIO;
1800 continue;
1801 } else
1802 rc = 0;
1803
1804 if (cb == NULL)
1805 continue;
1806
1807 size = rr->naihdr.naids;
1808 elems = (rr->response.length -
1809 sizeof(struct chsc_header) -
1810 sizeof(struct chsc_brinfo_naihdr)) /
1811 size;
1812
1813 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1814 /* Inform the caller that they need to scrap */
1815 /* the data that was already reported via cb */
1816 rc = -EAGAIN;
1817 break;
1818 }
1819 isfirstblock = 0;
1820 prev_instance = rr->naihdr.instance;
1821 for (i = 0; i < elems; i++)
1822 switch (size) {
1823 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1824 (*cb)(priv, l3_ipv6_addr,
1825 &rr->entries.l3_ipv6[i]);
1826 break;
1827 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1828 (*cb)(priv, l3_ipv4_addr,
1829 &rr->entries.l3_ipv4[i]);
1830 break;
1831 case sizeof(struct qdio_brinfo_entry_l2):
1832 (*cb)(priv, l2_addr_lnid,
1833 &rr->entries.l2[i]);
1834 break;
1835 default:
1836 WARN_ON_ONCE(1);
1837 rc = -EIO;
1838 goto out;
1839 }
1840 } while (rr->response.code == 0x0107 || /* channel busy */
1841 (rr->response.code == 1 && /* list stored */
1842 /* resume token is non-zero => list incomplete */
1843 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1844 (*response) = rr->response.code;
1845
1846out:
1847 free_page((unsigned long)rr);
1848 return rc;
1849}
1850EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1851
Jan Glauber779e6e12008-07-17 17:16:48 +02001852static int __init init_QDIO(void)
1853{
1854 int rc;
1855
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001856 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001857 if (rc)
1858 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001859 rc = qdio_setup_init();
1860 if (rc)
1861 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001862 rc = tiqdio_allocate_memory();
1863 if (rc)
1864 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001865 rc = tiqdio_register_thinints();
1866 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001867 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001868 return 0;
1869
Jan Glauber779e6e12008-07-17 17:16:48 +02001870out_ti:
1871 tiqdio_free_memory();
1872out_cache:
1873 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001874out_debug:
1875 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001876 return rc;
1877}
1878
1879static void __exit exit_QDIO(void)
1880{
1881 tiqdio_unregister_thinints();
1882 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001883 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001884 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001885}
1886
1887module_init(init_QDIO);
1888module_exit(exit_QDIO);