blob: 4142c85e77d8baa37d7def40b88c35c48e82bd70 [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;
Julian Wiedmanna698e132019-04-26 09:37:41 +0200208 int i = 1;
Jan Glauber779e6e12008-07-17 17:16:48 +0200209
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];
Julian Wiedmanna698e132019-04-26 09:37:41 +0200215
216 /* Bail out early if there is no work on the queue: */
217 if (__state & SLSB_OWNER_CU)
218 goto out;
219
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100220 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
221 __state = SLSB_P_OUTPUT_EMPTY;
222
Julian Wiedmanna698e132019-04-26 09:37:41 +0200223 for (; i < count; i++) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200224 bufnr = next_buf(bufnr);
Julian Wiedmann0cf1e052018-03-07 14:01:01 +0100225
226 /* merge PENDING into EMPTY: */
227 if (merge_pending &&
228 q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
229 __state == SLSB_P_OUTPUT_EMPTY)
230 continue;
231
232 /* stop if next state differs from initial state: */
233 if (q->slsb.val[bufnr] != __state)
234 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200235 }
Julian Wiedmanna698e132019-04-26 09:37:41 +0200236
237out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200238 *state = __state;
239 return i;
240}
241
Jan Glauber60b5df22009-06-22 12:08:10 +0200242static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
243 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200244{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000245 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200246}
247
248/* wrap-around safe setting of slsb states, returns number of changed buffers */
249static inline int set_buf_states(struct qdio_q *q, int bufnr,
250 unsigned char state, int count)
251{
252 int i;
253
Jan Glauber779e6e12008-07-17 17:16:48 +0200254 if (is_qebsm(q))
255 return qdio_do_sqbs(q, state, bufnr, count);
256
257 for (i = 0; i < count; i++) {
258 xchg(&q->slsb.val[bufnr], state);
259 bufnr = next_buf(bufnr);
260 }
261 return count;
262}
263
264static inline int set_buf_state(struct qdio_q *q, int bufnr,
265 unsigned char state)
266{
267 return set_buf_states(q, bufnr, state, 1);
268}
269
270/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100271static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200272{
273 struct qdio_q *q;
274 int i;
275
276 for_each_input_queue(irq_ptr, q, i)
277 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
278 QDIO_MAX_BUFFERS_PER_Q);
279 for_each_output_queue(irq_ptr, q, i)
280 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
281 QDIO_MAX_BUFFERS_PER_Q);
282}
283
Jan Glauber60b5df22009-06-22 12:08:10 +0200284static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200285 unsigned int input)
286{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100287 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
288 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200289 int cc;
290
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100291 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100292 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200293
Jan Glauber958c0ba2011-01-05 12:47:52 +0100294 if (is_qebsm(q)) {
295 schid = q->irq_ptr->sch_token;
296 fc |= QDIO_SIGA_QEBSM_FLAG;
297 }
298
299 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100300 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100301 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200302 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200303}
304
Jan Glauber60b5df22009-06-22 12:08:10 +0200305static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200306{
307 if (q->is_input_q)
308 return qdio_siga_sync(q, 0, q->mask);
309 else
310 return qdio_siga_sync(q, q->mask, 0);
311}
312
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000313static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
314 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200315{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100316 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
317 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100318 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200319 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000320 unsigned long laob = 0;
321
Julian Wiedmann69e96202019-07-01 14:19:29 +0200322 if (aob) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000323 fc = QDIO_SIGA_WRITEQ;
324 laob = aob;
325 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200326
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100327 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200328 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100329 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200330 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200331again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000332 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200333
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100334 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100335 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200336 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100337
338 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200339 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100340 goto again;
341 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200342 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200343 goto again;
344 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200345 if (retries) {
346 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
347 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
348 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
349 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200350 return cc;
351}
352
353static inline int qdio_siga_input(struct qdio_q *q)
354{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100355 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
356 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200357 int cc;
358
Jan Glauber22f99342008-12-25 13:38:46 +0100359 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100360 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200361
Jan Glauber958c0ba2011-01-05 12:47:52 +0100362 if (is_qebsm(q)) {
363 schid = q->irq_ptr->sch_token;
364 fc |= QDIO_SIGA_QEBSM_FLAG;
365 }
366
367 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100368 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100369 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200370 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200371}
372
Jan Glauber90adac52011-01-05 12:47:54 +0100373#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
374#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
375
376static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200377{
Jan Glauber90adac52011-01-05 12:47:54 +0100378 /* PCI capable outbound queues will also be scanned so sync them too */
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100379 if (pci_out_supported(q->irq_ptr))
Jan Glauber90adac52011-01-05 12:47:54 +0100380 qdio_siga_sync_all(q);
381 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200382 qdio_siga_sync_q(q);
383}
384
Jan Glauber60b5df22009-06-22 12:08:10 +0200385int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
386 unsigned char *state)
387{
Jan Glauber90adac52011-01-05 12:47:54 +0100388 if (need_siga_sync(q))
389 qdio_siga_sync_q(q);
Julian Wiedmann5a19d672019-04-26 09:30:11 +0200390 return get_buf_state(q, bufnr, state, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200391}
392
393static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200394{
Jan Glauber50f769d2008-12-25 13:38:47 +0100395 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200396 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100397
Jan Glauber779e6e12008-07-17 17:16:48 +0200398 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100399 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200400
401 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100402 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100403 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100404 q->u.in.ack_count);
405 q->u.in.ack_count = 0;
406 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100407 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200408}
409
Fabian Frederick92bdae52014-06-03 21:55:15 +0200410static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100411{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200412 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100413
414 q->q_stats.nr_sbal_total += count;
415 if (count == QDIO_MAX_BUFFERS_MASK) {
416 q->q_stats.nr_sbals[7]++;
417 return;
418 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200419 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100420 q->q_stats.nr_sbals[pos]++;
421}
422
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200423static void process_buffer_error(struct qdio_q *q, unsigned int start,
424 int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200425{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200426 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
427 SLSB_P_OUTPUT_NOT_INIT;
428
Jan Glauber1549d132012-05-09 16:27:34 +0200429 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100430
431 /* special handling for no target buffer empty */
Julian Wiedmannb23481f2017-10-23 09:38:18 +0200432 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200433 q->sbal[start]->element[15].sflags == 0x10) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100434 qperf_inc(q, target_full);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200435 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100436 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100437 }
438
Jan Glauber22f99342008-12-25 13:38:46 +0100439 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
440 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200441 DBF_ERROR("FTC:%3d C:%3d", start, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100442 DBF_ERROR("F14:%2x F15:%2x",
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200443 q->sbal[start]->element[14].sflags,
444 q->sbal[start]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200445
Jan Glauber2768b2d2011-10-30 15:17:07 +0100446set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200447 /*
448 * Interrupts may be avoided as long as the error is present
449 * so change the buffer state immediately to avoid starvation.
450 */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200451 set_buf_states(q, start, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100452}
Jan Glauber779e6e12008-07-17 17:16:48 +0200453
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200454static inline void inbound_primed(struct qdio_q *q, unsigned int start,
455 int count)
Jan Glauber50f769d2008-12-25 13:38:47 +0100456{
457 int new;
458
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100459 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100460
461 /* for QEBSM the ACK was already set by EQBS */
462 if (is_qebsm(q)) {
463 if (!q->u.in.polling) {
464 q->u.in.polling = 1;
465 q->u.in.ack_count = count;
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200466 q->u.in.ack_start = start;
Jan Glauber50f769d2008-12-25 13:38:47 +0100467 return;
468 }
469
470 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100471 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100472 q->u.in.ack_count);
473 q->u.in.ack_count = count;
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200474 q->u.in.ack_start = start;
Jan Glauber50f769d2008-12-25 13:38:47 +0100475 return;
476 }
477
478 /*
479 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
480 * or by the next inbound run.
481 */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200482 new = add_buf(start, count - 1);
Jan Glauber50f769d2008-12-25 13:38:47 +0100483 if (q->u.in.polling) {
484 /* reset the previous ACK but first set the new one */
485 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100486 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100487 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100488 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100489 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100490 }
491
Jan Glaubere85dea02009-03-26 15:24:29 +0100492 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100493 count--;
494 if (!count)
495 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200496 /* need to change ALL buffers to get more interrupts */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200497 set_buf_states(q, start, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200498}
499
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200500static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200501{
Jan Glauber6fa10982011-01-31 11:30:08 +0100502 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100503 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200504
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200505 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100506
Jan Glauber779e6e12008-07-17 17:16:48 +0200507 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200508 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
509 * would return 0.
510 */
511 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100512 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100513 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200514
Jan Glauber36e3e722009-06-22 12:08:12 +0200515 /*
516 * No siga sync here, as a PCI or we after a thin interrupt
517 * already sync'ed the queues.
518 */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200519 count = get_buf_states(q, start, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200520 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100521 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200522
523 switch (state) {
524 case SLSB_P_INPUT_PRIMED:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200525 inbound_primed(q, start, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200526 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100527 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100528 if (q->irq_ptr->perf_stat_enabled)
529 account_sbals(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100530 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200531 case SLSB_P_INPUT_ERROR:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200532 process_buffer_error(q, start, count);
Julian Wiedmann0b926ac2017-10-23 09:40:16 +0200533 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
534 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100535 if (q->irq_ptr->perf_stat_enabled)
536 account_sbals_error(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100537 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200538 case SLSB_CU_INPUT_EMPTY:
539 case SLSB_P_INPUT_NOT_INIT:
540 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100541 if (q->irq_ptr->perf_stat_enabled)
542 q->q_stats.nr_sbal_nop++;
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100543 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200544 q->nr, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100545 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200546 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200547 WARN_ON_ONCE(1);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100548 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200549 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200550}
551
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200552static int qdio_inbound_q_moved(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200553{
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100554 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200555
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200556 count = get_inbound_buffer_frontier(q, start);
Jan Glauber779e6e12008-07-17 17:16:48 +0200557
Julian Wiedmanndccbbaf2019-03-28 10:45:11 +0100558 if (count && !is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
559 q->u.in.timestamp = get_tod_clock();
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100560
561 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200562}
563
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200564static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
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);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200573 get_buf_state(q, start, &state, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200574
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) {
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200591 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x", start);
Jan Glauber9a2c1602009-06-22 12:08:11 +0200592 return 1;
593 } else
594 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200595}
596
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000597static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
598{
599 unsigned char state = 0;
600 int j, b = start;
601
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000602 for (j = 0; j < count; ++j) {
603 get_buf_state(q, b, &state, 0);
604 if (state == SLSB_P_OUTPUT_PENDING) {
605 struct qaob *aob = q->u.out.aobs[b];
606 if (aob == NULL)
607 continue;
608
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000609 q->u.out.sbal_state[b].flags |=
610 QDIO_OUTBUF_STATE_FLAG_PENDING;
611 q->u.out.aobs[b] = NULL;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000612 }
613 b = next_buf(b);
614 }
615}
616
617static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
618 int bufnr)
619{
620 unsigned long phys_aob = 0;
621
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000622 if (!q->aobs[bufnr]) {
623 struct qaob *aob = qdio_allocate_aob();
624 q->aobs[bufnr] = aob;
625 }
626 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000627 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
628 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200629 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000630 }
631
Julian Wiedmann64e03ff2018-05-16 09:37:25 +0200632 q->sbal_state[bufnr].flags = 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000633 return phys_aob;
634}
635
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100636static void qdio_kick_handler(struct qdio_q *q, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200637{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100638 int start = q->first_to_kick;
Jan Glauber779e6e12008-07-17 17:16:48 +0200639
640 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
641 return;
642
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100643 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100644 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200645 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100646 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100647 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200648 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
649 start, count);
Julian Wiedmannccc413f2018-05-15 21:17:38 +0200650 if (q->u.out.use_cq)
651 qdio_handle_aobs(q, start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100652 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100653
654 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
655 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200656
657 /* for the next time */
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100658 q->first_to_kick = add_buf(start, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200659 q->qdio_error = 0;
660}
661
Ursula Braun9bce8b22016-08-05 12:33:10 +0200662static inline int qdio_tasklet_schedule(struct qdio_q *q)
663{
664 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
665 tasklet_schedule(&q->tasklet);
666 return 0;
667 }
668 return -EPERM;
669}
670
Jan Glauber779e6e12008-07-17 17:16:48 +0200671static void __qdio_inbound_processing(struct qdio_q *q)
672{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200673 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100674 int count;
675
Jan Glauber6486cda2010-01-04 09:05:42 +0100676 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200677
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200678 count = qdio_inbound_q_moved(q, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100679 if (count == 0)
Jan Glauber779e6e12008-07-17 17:16:48 +0200680 return;
681
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200682 start = add_buf(start, count);
683 q->first_to_check = start;
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100684 qdio_kick_handler(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200685
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200686 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200687 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100688 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200689 if (!qdio_tasklet_schedule(q))
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200690 return;
Jan Glauber6486cda2010-01-04 09:05:42 +0100691 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200692
693 qdio_stop_polling(q);
694 /*
695 * We need to check again to not lose initiative after
696 * resetting the ACK state.
697 */
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200698 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100699 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200700 qdio_tasklet_schedule(q);
Jan Glauber6486cda2010-01-04 09:05:42 +0100701 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200702}
703
Jan Glauber779e6e12008-07-17 17:16:48 +0200704void qdio_inbound_processing(unsigned long data)
705{
706 struct qdio_q *q = (struct qdio_q *)data;
707 __qdio_inbound_processing(q);
708}
709
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200710static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200711{
Jan Glauber6fa10982011-01-31 11:30:08 +0100712 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100713 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200714
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200715 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100716
Jan Glauber90adac52011-01-05 12:47:54 +0100717 if (need_siga_sync(q))
718 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100719 !pci_out_supported(q->irq_ptr)) ||
Jan Glauber90adac52011-01-05 12:47:54 +0100720 (queue_type(q) == QDIO_IQDIO_QFMT &&
721 multicast_outbound(q)))
722 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200723
Julian Wiedmann30607812019-04-01 10:08:08 +0200724 count = atomic_read(&q->nr_buf_used);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100725 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100726 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200727
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200728 count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200729 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100730 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200731
732 switch (state) {
733 case SLSB_P_OUTPUT_EMPTY:
Julian Wiedmann04310322019-06-03 07:47:04 +0200734 case SLSB_P_OUTPUT_PENDING:
Jan Glauber779e6e12008-07-17 17:16:48 +0200735 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000736 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
737 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200738
739 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100740 if (q->irq_ptr->perf_stat_enabled)
741 account_sbals(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100742 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200743 case SLSB_P_OUTPUT_ERROR:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200744 process_buffer_error(q, start, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200745 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100746 if (q->irq_ptr->perf_stat_enabled)
747 account_sbals_error(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100748 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200749 case SLSB_CU_OUTPUT_PRIMED:
750 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100751 if (q->irq_ptr->perf_stat_enabled)
752 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000753 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
754 q->nr);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100755 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200756 case SLSB_P_OUTPUT_NOT_INIT:
757 case SLSB_P_OUTPUT_HALTED:
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100758 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200759 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200760 WARN_ON_ONCE(1);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100761 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200762 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200763}
764
765/* all buffers processed? */
766static inline int qdio_outbound_q_done(struct qdio_q *q)
767{
768 return atomic_read(&q->nr_buf_used) == 0;
769}
770
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200771static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200772{
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100773 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200774
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200775 count = get_outbound_buffer_frontier(q, start);
Jan Glauber779e6e12008-07-17 17:16:48 +0200776
Julian Wiedmanndccbbaf2019-03-28 10:45:11 +0100777 if (count)
Jan Glauber22f99342008-12-25 13:38:46 +0100778 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100779
780 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200781}
782
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000783static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200784{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200785 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100786 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200787
788 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100789 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200790
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100791 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200792retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100793 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100794
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000795 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100796 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200797 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200798 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100799 case 2:
800 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200801 while (++retries < QDIO_BUSY_BIT_RETRIES) {
802 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
803 goto retry;
804 }
805 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200806 cc = -EBUSY;
807 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100808 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200809 cc = -ENOBUFS;
810 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100811 break;
812 case 1:
813 case 3:
814 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200815 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100816 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200817 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200818 if (retries) {
819 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
820 DBF_ERROR("count:%u", retries);
821 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100822 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200823}
824
Jan Glauber779e6e12008-07-17 17:16:48 +0200825static void __qdio_outbound_processing(struct qdio_q *q)
826{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200827 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100828 int count;
829
Jan Glauber6486cda2010-01-04 09:05:42 +0100830 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200831 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200832
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200833 count = qdio_outbound_q_moved(q, start);
834 if (count) {
835 q->first_to_check = add_buf(start, count);
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100836 qdio_kick_handler(q, count);
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200837 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200838
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100839 if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
840 !qdio_outbound_q_done(q))
841 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200842
Jan Glauber779e6e12008-07-17 17:16:48 +0200843 if (q->u.out.pci_out_enabled)
844 return;
845
846 /*
847 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100848 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
849 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200850 */
851 if (qdio_outbound_q_done(q))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200852 del_timer_sync(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100853 else
Ursula Braun9bce8b22016-08-05 12:33:10 +0200854 if (!timer_pending(&q->u.out.timer) &&
855 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber779e6e12008-07-17 17:16:48 +0200856 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100857 return;
858
859sched:
Ursula Braun9bce8b22016-08-05 12:33:10 +0200860 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200861}
862
863/* outbound tasklet */
864void qdio_outbound_processing(unsigned long data)
865{
866 struct qdio_q *q = (struct qdio_q *)data;
867 __qdio_outbound_processing(q);
868}
869
Kees Cookcb9f7802017-10-04 17:54:35 -0700870void qdio_outbound_timer(struct timer_list *t)
Jan Glauber779e6e12008-07-17 17:16:48 +0200871{
Kees Cookcb9f7802017-10-04 17:54:35 -0700872 struct qdio_q *q = from_timer(q, t, u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +0100873
Ursula Braun9bce8b22016-08-05 12:33:10 +0200874 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200875}
876
Julian Wiedmann2f2f3832018-10-30 08:21:27 +0100877static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
Jan Glauber779e6e12008-07-17 17:16:48 +0200878{
879 struct qdio_q *out;
880 int i;
881
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100882 if (!pci_out_supported(irq))
Jan Glauber779e6e12008-07-17 17:16:48 +0200883 return;
884
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100885 for_each_output_queue(irq, out, i)
Jan Glauber779e6e12008-07-17 17:16:48 +0200886 if (!qdio_outbound_q_done(out))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200887 qdio_tasklet_schedule(out);
Jan Glauber779e6e12008-07-17 17:16:48 +0200888}
889
Jan Glauber60b5df22009-06-22 12:08:10 +0200890static void __tiqdio_inbound_processing(struct qdio_q *q)
891{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200892 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100893 int count;
894
Jan Glauber6486cda2010-01-04 09:05:42 +0100895 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100896 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
897 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200898
Julian Wiedmann2f2f3832018-10-30 08:21:27 +0100899 /* The interrupt could be caused by a PCI request: */
900 qdio_check_outbound_pci_queues(q->irq_ptr);
Jan Glauber60b5df22009-06-22 12:08:10 +0200901
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200902 count = qdio_inbound_q_moved(q, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100903 if (count == 0)
Jan Glauber60b5df22009-06-22 12:08:10 +0200904 return;
905
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200906 start = add_buf(start, count);
907 q->first_to_check = start;
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100908 qdio_kick_handler(q, count);
Jan Glauber60b5df22009-06-22 12:08:10 +0200909
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200910 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100911 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200912 if (!qdio_tasklet_schedule(q))
Jan Glaubere2910bc2009-09-11 10:28:19 +0200913 return;
Jan Glauber60b5df22009-06-22 12:08:10 +0200914 }
915
916 qdio_stop_polling(q);
917 /*
918 * We need to check again to not lose initiative after
919 * resetting the ACK state.
920 */
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200921 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100922 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200923 qdio_tasklet_schedule(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200924 }
925}
926
927void tiqdio_inbound_processing(unsigned long data)
928{
929 struct qdio_q *q = (struct qdio_q *)data;
930 __tiqdio_inbound_processing(q);
931}
932
Jan Glauber779e6e12008-07-17 17:16:48 +0200933static inline void qdio_set_state(struct qdio_irq *irq_ptr,
934 enum qdio_irq_states state)
935{
Jan Glauber22f99342008-12-25 13:38:46 +0100936 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200937
938 irq_ptr->state = state;
939 mb();
940}
941
Jan Glauber22f99342008-12-25 13:38:46 +0100942static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200943{
Jan Glauber779e6e12008-07-17 17:16:48 +0200944 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100945 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
946 DBF_ERROR_HEX(irb, 64);
947 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200948 }
949}
950
951/* PCI interrupt handler */
952static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
953{
954 int i;
955 struct qdio_q *q;
956
Ursula Braun9bce8b22016-08-05 12:33:10 +0200957 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
Jan Glauberc38f9602009-03-26 15:24:26 +0100958 return;
959
Jan Glauberd36deae2010-09-07 21:14:39 +0000960 for_each_input_queue(irq_ptr, q, i) {
961 if (q->u.in.queue_start_poll) {
962 /* skip if polling is enabled or already in work */
963 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
964 &q->u.in.queue_irq_state)) {
965 qperf_inc(q, int_discarded);
966 continue;
967 }
968 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
969 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000970 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +0000971 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000972 }
Jan Glauberd36deae2010-09-07 21:14:39 +0000973 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200974
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100975 if (!pci_out_supported(irq_ptr))
Jan Glauber779e6e12008-07-17 17:16:48 +0200976 return;
977
978 for_each_output_queue(irq_ptr, q, i) {
979 if (qdio_outbound_q_done(q))
980 continue;
Jan Glauber90adac52011-01-05 12:47:54 +0100981 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +0200982 qdio_siga_sync_q(q);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200983 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200984 }
985}
986
987static void qdio_handle_activate_check(struct ccw_device *cdev,
988 unsigned long intparm, int cstat, int dstat)
989{
990 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
991 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200992 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200993
Jan Glauber22f99342008-12-25 13:38:46 +0100994 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
995 DBF_ERROR("intp :%lx", intparm);
996 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +0200997
998 if (irq_ptr->nr_input_qs) {
999 q = irq_ptr->input_qs[0];
1000 } else if (irq_ptr->nr_output_qs) {
1001 q = irq_ptr->output_qs[0];
1002 } else {
1003 dump_stack();
1004 goto no_handler;
1005 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001006
1007 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001008 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001009 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001010no_handler:
1011 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001012 /*
1013 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1014 * Therefore we call the LGR detection function here.
1015 */
1016 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001017}
1018
Jan Glauber779e6e12008-07-17 17:16:48 +02001019static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1020 int dstat)
1021{
1022 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001023
Jan Glauber22f99342008-12-25 13:38:46 +01001024 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001025
1026 if (cstat)
1027 goto error;
1028 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1029 goto error;
1030 if (!(dstat & DEV_STAT_DEV_END))
1031 goto error;
1032 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1033 return;
1034
1035error:
1036 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1037 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1038 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001039}
1040
1041/* qdio interrupt handler */
1042void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1043 struct irb *irb)
1044{
1045 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001046 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001047 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001048
Jan Glauber779e6e12008-07-17 17:16:48 +02001049 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001050 ccw_device_get_schid(cdev, &schid);
1051 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001052 return;
1053 }
1054
Jan Glauber09a308f2010-05-17 10:00:14 +02001055 if (irq_ptr->perf_stat_enabled)
1056 irq_ptr->perf_stat.qdio_int++;
1057
Jan Glauber779e6e12008-07-17 17:16:48 +02001058 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001059 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1060 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1061 wake_up(&cdev->private->wait_q);
1062 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001063 }
Jan Glauber22f99342008-12-25 13:38:46 +01001064 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001065 cstat = irb->scsw.cmd.cstat;
1066 dstat = irb->scsw.cmd.dstat;
1067
1068 switch (irq_ptr->state) {
1069 case QDIO_IRQ_STATE_INACTIVE:
1070 qdio_establish_handle_irq(cdev, cstat, dstat);
1071 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001072 case QDIO_IRQ_STATE_CLEANUP:
1073 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1074 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001075 case QDIO_IRQ_STATE_ESTABLISHED:
1076 case QDIO_IRQ_STATE_ACTIVE:
1077 if (cstat & SCHN_STAT_PCI) {
1078 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001079 return;
1080 }
Jan Glauber4c575422009-06-12 10:26:28 +02001081 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001082 qdio_handle_activate_check(cdev, intparm, cstat,
1083 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001084 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001085 case QDIO_IRQ_STATE_STOPPED:
1086 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001087 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001088 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001089 }
1090 wake_up(&cdev->private->wait_q);
1091}
1092
1093/**
1094 * qdio_get_ssqd_desc - get qdio subchannel description
1095 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001096 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001097 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001098 * Returns 0 or an error code. The results of the chsc are stored in the
1099 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001100 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001101int qdio_get_ssqd_desc(struct ccw_device *cdev,
1102 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001103{
Sebastian Ott9080c922016-07-28 20:30:31 +02001104 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001105
Jan Glauberbbd50e12008-12-25 13:38:43 +01001106 if (!cdev || !cdev->private)
1107 return -EINVAL;
1108
Sebastian Ott9080c922016-07-28 20:30:31 +02001109 ccw_device_get_schid(cdev, &schid);
1110 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1111 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001112}
1113EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1114
Jan Glauber779e6e12008-07-17 17:16:48 +02001115static void qdio_shutdown_queues(struct ccw_device *cdev)
1116{
1117 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1118 struct qdio_q *q;
1119 int i;
1120
1121 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001122 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001123
1124 for_each_output_queue(irq_ptr, q, i) {
Ursula Braun9bce8b22016-08-05 12:33:10 +02001125 del_timer_sync(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001126 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001127 }
1128}
1129
1130/**
1131 * qdio_shutdown - shut down a qdio subchannel
1132 * @cdev: associated ccw device
1133 * @how: use halt or clear to shutdown
1134 */
1135int qdio_shutdown(struct ccw_device *cdev, int how)
1136{
Jan Glauber22f99342008-12-25 13:38:46 +01001137 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001138 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001139 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001140
Jan Glauber779e6e12008-07-17 17:16:48 +02001141 if (!irq_ptr)
1142 return -ENODEV;
1143
Jan Glauberce1d8012012-10-24 12:38:35 +02001144 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001145 ccw_device_get_schid(cdev, &schid);
1146 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001147
Jan Glauber779e6e12008-07-17 17:16:48 +02001148 mutex_lock(&irq_ptr->setup_mutex);
1149 /*
1150 * Subchannel was already shot down. We cannot prevent being called
1151 * twice since cio may trigger a shutdown asynchronously.
1152 */
1153 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1154 mutex_unlock(&irq_ptr->setup_mutex);
1155 return 0;
1156 }
1157
Jan Glauberc38f9602009-03-26 15:24:26 +01001158 /*
1159 * Indicate that the device is going down. Scheduling the queue
1160 * tasklets is forbidden from here on.
1161 */
1162 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1163
Jan Glauber779e6e12008-07-17 17:16:48 +02001164 tiqdio_remove_input_queues(irq_ptr);
1165 qdio_shutdown_queues(cdev);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001166 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001167
1168 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001169 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001170
1171 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1172 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1173 else
1174 /* default behaviour is halt */
1175 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1176 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001177 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1178 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001179 goto no_cleanup;
1180 }
1181
1182 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001183 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001184 wait_event_interruptible_timeout(cdev->private->wait_q,
1185 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1186 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1187 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001188 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001189
1190no_cleanup:
1191 qdio_shutdown_thinint(irq_ptr);
1192
1193 /* restore interrupt handler */
Julian Wiedmann89286322018-03-21 17:14:00 +01001194 if ((void *)cdev->handler == (void *)qdio_int_handler) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001195 cdev->handler = irq_ptr->orig_handler;
Julian Wiedmann89286322018-03-21 17:14:00 +01001196 cdev->private->intparm = 0;
1197 }
Sebastian Otta48ed862016-07-29 13:41:20 +02001198 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001199
1200 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1201 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001202 if (rc)
1203 return rc;
1204 return 0;
1205}
1206EXPORT_SYMBOL_GPL(qdio_shutdown);
1207
1208/**
1209 * qdio_free - free data structures for a qdio subchannel
1210 * @cdev: associated ccw device
1211 */
1212int qdio_free(struct ccw_device *cdev)
1213{
Jan Glauber22f99342008-12-25 13:38:46 +01001214 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001215 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001216
Jan Glauber779e6e12008-07-17 17:16:48 +02001217 if (!irq_ptr)
1218 return -ENODEV;
1219
Sebastian Ott9080c922016-07-28 20:30:31 +02001220 ccw_device_get_schid(cdev, &schid);
1221 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001222 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001223 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001224
Stefan Raspl613c4e02014-06-12 14:24:45 +02001225 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001226 cdev->private->qdio_data = NULL;
1227 mutex_unlock(&irq_ptr->setup_mutex);
1228
1229 qdio_release_memory(irq_ptr);
1230 return 0;
1231}
1232EXPORT_SYMBOL_GPL(qdio_free);
1233
1234/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001235 * qdio_allocate - allocate qdio queues and associated data
1236 * @init_data: initialization data
1237 */
1238int qdio_allocate(struct qdio_initialize *init_data)
1239{
Sebastian Ott9080c922016-07-28 20:30:31 +02001240 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001241 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001242
Sebastian Ott9080c922016-07-28 20:30:31 +02001243 ccw_device_get_schid(init_data->cdev, &schid);
1244 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001245
1246 if ((init_data->no_input_qs && !init_data->input_handler) ||
1247 (init_data->no_output_qs && !init_data->output_handler))
1248 return -EINVAL;
1249
1250 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1251 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1252 return -EINVAL;
1253
1254 if ((!init_data->input_sbal_addr_array) ||
1255 (!init_data->output_sbal_addr_array))
1256 return -EINVAL;
1257
Jan Glauber779e6e12008-07-17 17:16:48 +02001258 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1259 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1260 if (!irq_ptr)
1261 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001262
1263 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001264 if (qdio_allocate_dbf(init_data, irq_ptr))
1265 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001266
1267 /*
1268 * Allocate a page for the chsc calls in qdio_establish.
1269 * Must be pre-allocated since a zfcp recovery will call
1270 * qdio_establish. In case of low memory and swap on a zfcp disk
1271 * we may not be able to allocate memory otherwise.
1272 */
1273 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1274 if (!irq_ptr->chsc_page)
1275 goto out_rel;
1276
1277 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001278 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001279 if (!irq_ptr->qdr)
1280 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001281
Jan Glauber779e6e12008-07-17 17:16:48 +02001282 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1283 init_data->no_output_qs))
1284 goto out_rel;
1285
1286 init_data->cdev->private->qdio_data = irq_ptr;
1287 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1288 return 0;
1289out_rel:
1290 qdio_release_memory(irq_ptr);
1291out_err:
1292 return -ENOMEM;
1293}
1294EXPORT_SYMBOL_GPL(qdio_allocate);
1295
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001296static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1297{
1298 struct qdio_q *q = irq_ptr->input_qs[0];
1299 int i, use_cq = 0;
1300
1301 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1302 use_cq = 1;
1303
1304 for_each_output_queue(irq_ptr, q, i) {
1305 if (use_cq) {
Julian Wiedmann69e96202019-07-01 14:19:29 +02001306 if (multicast_outbound(q))
1307 continue;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001308 if (qdio_enable_async_operation(&q->u.out) < 0) {
1309 use_cq = 0;
1310 continue;
1311 }
1312 } else
1313 qdio_disable_async_operation(&q->u.out);
1314 }
1315 DBF_EVENT("use_cq:%d", use_cq);
1316}
1317
Jan Glauber779e6e12008-07-17 17:16:48 +02001318/**
1319 * qdio_establish - establish queues on a qdio subchannel
1320 * @init_data: initialization data
1321 */
1322int qdio_establish(struct qdio_initialize *init_data)
1323{
Jan Glauber779e6e12008-07-17 17:16:48 +02001324 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001325 struct subchannel_id schid;
1326 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001327 int rc;
1328
Sebastian Ott9080c922016-07-28 20:30:31 +02001329 ccw_device_get_schid(cdev, &schid);
1330 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001331
Jan Glauber779e6e12008-07-17 17:16:48 +02001332 irq_ptr = cdev->private->qdio_data;
1333 if (!irq_ptr)
1334 return -ENODEV;
1335
Jan Glauber779e6e12008-07-17 17:16:48 +02001336 mutex_lock(&irq_ptr->setup_mutex);
1337 qdio_setup_irq(init_data);
1338
1339 rc = qdio_establish_thinint(irq_ptr);
1340 if (rc) {
1341 mutex_unlock(&irq_ptr->setup_mutex);
1342 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1343 return rc;
1344 }
1345
1346 /* establish q */
1347 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1348 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1349 irq_ptr->ccw.count = irq_ptr->equeue.count;
1350 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1351
Sebastian Otta48ed862016-07-29 13:41:20 +02001352 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001353 ccw_device_set_options_mask(cdev, 0);
1354
1355 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001356 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001357 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001358 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1359 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001360 mutex_unlock(&irq_ptr->setup_mutex);
1361 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1362 return rc;
1363 }
1364
1365 wait_event_interruptible_timeout(cdev->private->wait_q,
1366 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1367 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1368
1369 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1370 mutex_unlock(&irq_ptr->setup_mutex);
1371 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1372 return -EIO;
1373 }
1374
1375 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001376
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001377 qdio_detect_hsicq(irq_ptr);
1378
Jan Glauber779e6e12008-07-17 17:16:48 +02001379 /* qebsm is now setup if available, initialize buffer states */
1380 qdio_init_buf_states(irq_ptr);
1381
1382 mutex_unlock(&irq_ptr->setup_mutex);
1383 qdio_print_subchannel_info(irq_ptr, cdev);
1384 qdio_setup_debug_entries(irq_ptr, cdev);
1385 return 0;
1386}
1387EXPORT_SYMBOL_GPL(qdio_establish);
1388
1389/**
1390 * qdio_activate - activate queues on a qdio subchannel
1391 * @cdev: associated cdev
1392 */
1393int qdio_activate(struct ccw_device *cdev)
1394{
Sebastian Ott9080c922016-07-28 20:30:31 +02001395 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001396 struct qdio_irq *irq_ptr;
1397 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001398
Sebastian Ott9080c922016-07-28 20:30:31 +02001399 ccw_device_get_schid(cdev, &schid);
1400 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001401
Jan Glauber779e6e12008-07-17 17:16:48 +02001402 irq_ptr = cdev->private->qdio_data;
1403 if (!irq_ptr)
1404 return -ENODEV;
1405
Jan Glauber779e6e12008-07-17 17:16:48 +02001406 mutex_lock(&irq_ptr->setup_mutex);
1407 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1408 rc = -EBUSY;
1409 goto out;
1410 }
1411
Jan Glauber779e6e12008-07-17 17:16:48 +02001412 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1413 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1414 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1415 irq_ptr->ccw.cda = 0;
1416
Sebastian Otta48ed862016-07-29 13:41:20 +02001417 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001418 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1419
1420 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1421 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001422 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001423 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001424 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1425 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001426 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001427 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001428
1429 if (is_thinint_irq(irq_ptr))
1430 tiqdio_add_input_queues(irq_ptr);
1431
1432 /* wait for subchannel to become active */
1433 msleep(5);
1434
1435 switch (irq_ptr->state) {
1436 case QDIO_IRQ_STATE_STOPPED:
1437 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001438 rc = -EIO;
1439 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001440 default:
1441 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1442 rc = 0;
1443 }
1444out:
1445 mutex_unlock(&irq_ptr->setup_mutex);
1446 return rc;
1447}
1448EXPORT_SYMBOL_GPL(qdio_activate);
1449
1450static inline int buf_in_between(int bufnr, int start, int count)
1451{
1452 int end = add_buf(start, count);
1453
1454 if (end > start) {
1455 if (bufnr >= start && bufnr < end)
1456 return 1;
1457 else
1458 return 0;
1459 }
1460
1461 /* wrap-around case */
1462 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1463 (bufnr < end))
1464 return 1;
1465 else
1466 return 0;
1467}
1468
1469/**
1470 * handle_inbound - reset processed input buffers
1471 * @q: queue containing the buffers
1472 * @callflags: flags
1473 * @bufnr: first buffer to process
1474 * @count: how many buffers are emptied
1475 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001476static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1477 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001478{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001479 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001480
Jan Glauber6486cda2010-01-04 09:05:42 +01001481 qperf_inc(q, inbound_call);
1482
Jan Glauber50f769d2008-12-25 13:38:47 +01001483 if (!q->u.in.polling)
1484 goto set;
1485
1486 /* protect against stop polling setting an ACK for an emptied slsb */
1487 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1488 /* overwriting everything, just delete polling status */
1489 q->u.in.polling = 0;
1490 q->u.in.ack_count = 0;
1491 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001492 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001493 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001494 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001495 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001496 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001497 q->u.in.ack_count -= diff;
1498 if (q->u.in.ack_count <= 0) {
1499 q->u.in.polling = 0;
1500 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001501 goto set;
1502 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001503 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001504 }
1505 else
1506 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001507 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001508 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001509
Jan Glauber50f769d2008-12-25 13:38:47 +01001510set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001511 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001512 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001513
Jan Glauberd303b6f2009-03-26 15:24:31 +01001514 if (need_siga_in(q))
1515 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001516
Jan Glauberd303b6f2009-03-26 15:24:31 +01001517 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001518}
1519
1520/**
1521 * handle_outbound - process filled outbound buffers
1522 * @q: queue containing the buffers
1523 * @callflags: flags
1524 * @bufnr: first buffer to process
1525 * @count: how many buffers are filled
1526 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001527static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1528 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001529{
Jan Glauberc26001d2011-05-23 10:24:38 +02001530 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001531 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001532
Jan Glauber6486cda2010-01-04 09:05:42 +01001533 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001534
1535 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1536 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001537
Jan Glauber01958432011-01-05 12:47:51 +01001538 if (used == QDIO_MAX_BUFFERS_PER_Q)
1539 qperf_inc(q, outbound_queue_full);
1540
Jan Glauber6486cda2010-01-04 09:05:42 +01001541 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001542 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001543 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001544 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001545 q->u.out.pci_out_enabled = 0;
1546
1547 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001548 unsigned long phys_aob = 0;
1549
1550 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001551 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1552
Julian Wiedmann69e96202019-07-01 14:19:29 +02001553 if (q->u.out.use_cq)
1554 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001555
1556 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001557 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001558 rc = qdio_siga_sync_q(q);
Julian Wiedmanna6ec4142019-07-11 18:17:36 +02001559 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1560 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1561 state == SLSB_CU_OUTPUT_PRIMED) {
1562 /* The previous buffer is not processed yet, tack on. */
1563 qperf_inc(q, fast_requeue);
Jan Glauber110da312011-01-05 12:47:53 +01001564 } else {
Julian Wiedmanna6ec4142019-07-11 18:17:36 +02001565 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001566 }
1567
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001568 /* in case of SIGA errors we must process the error immediately */
1569 if (used >= q->u.out.scan_threshold || rc)
Ursula Braun9bce8b22016-08-05 12:33:10 +02001570 qdio_tasklet_schedule(q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001571 else
1572 /* free the SBALs in case of no further traffic */
Ursula Braun9bce8b22016-08-05 12:33:10 +02001573 if (!timer_pending(&q->u.out.timer) &&
1574 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001575 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001576 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001577}
1578
1579/**
1580 * do_QDIO - process input or output buffers
1581 * @cdev: associated ccw_device for the qdio subchannel
1582 * @callflags: input or output and special flags from the program
1583 * @q_nr: queue number
1584 * @bufnr: buffer number
1585 * @count: how many buffers to process
1586 */
1587int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001588 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001589{
1590 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001591
Jan Glauber66182412009-06-22 12:08:15 +02001592 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001593 return -EINVAL;
1594
Jan Glauber779e6e12008-07-17 17:16:48 +02001595 irq_ptr = cdev->private->qdio_data;
1596 if (!irq_ptr)
1597 return -ENODEV;
1598
Jan Glauber1d7e1502009-09-22 22:58:39 +02001599 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1600 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001601
1602 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001603 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001604 if (!count)
1605 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001606 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001607 return handle_inbound(irq_ptr->input_qs[q_nr],
1608 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001609 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001610 return handle_outbound(irq_ptr->output_qs[q_nr],
1611 callflags, bufnr, count);
1612 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001613}
1614EXPORT_SYMBOL_GPL(do_QDIO);
1615
Jan Glauberd36deae2010-09-07 21:14:39 +00001616/**
1617 * qdio_start_irq - process input buffers
1618 * @cdev: associated ccw_device for the qdio subchannel
1619 * @nr: input queue number
1620 *
1621 * Return codes
1622 * 0 - success
1623 * 1 - irqs not started since new data is available
1624 */
1625int qdio_start_irq(struct ccw_device *cdev, int nr)
1626{
1627 struct qdio_q *q;
1628 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1629
1630 if (!irq_ptr)
1631 return -ENODEV;
1632 q = irq_ptr->input_qs[nr];
1633
Jan Glauber5f4026f2011-10-30 15:17:20 +01001634 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001635 qdio_stop_polling(q);
1636 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1637
1638 /*
1639 * We need to check again to not lose initiative after
1640 * resetting the ACK state.
1641 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001642 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001643 goto rescan;
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +02001644 if (!qdio_inbound_q_done(q, q->first_to_check))
Jan Glauberd36deae2010-09-07 21:14:39 +00001645 goto rescan;
1646 return 0;
1647
1648rescan:
1649 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1650 &q->u.in.queue_irq_state))
1651 return 0;
1652 else
1653 return 1;
1654
1655}
1656EXPORT_SYMBOL(qdio_start_irq);
1657
1658/**
1659 * qdio_get_next_buffers - process input buffers
1660 * @cdev: associated ccw_device for the qdio subchannel
1661 * @nr: input queue number
1662 * @bufnr: first filled buffer number
1663 * @error: buffers are in error state
1664 *
1665 * Return codes
1666 * < 0 - error
1667 * = 0 - no new buffers found
1668 * > 0 - number of processed buffers
1669 */
1670int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1671 int *error)
1672{
1673 struct qdio_q *q;
Jan Glauberd36deae2010-09-07 21:14:39 +00001674 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +02001675 unsigned int start;
Julian Wiedmannb39544c2019-03-28 10:39:25 +01001676 int count;
Jan Glauberd36deae2010-09-07 21:14:39 +00001677
1678 if (!irq_ptr)
1679 return -ENODEV;
1680 q = irq_ptr->input_qs[nr];
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +02001681 start = q->first_to_check;
Jan Glauberd36deae2010-09-07 21:14:39 +00001682
Jan Glauberd36deae2010-09-07 21:14:39 +00001683 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001684 * Cannot rely on automatic sync after interrupt since queues may
1685 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001686 */
Jan Glauber90adac52011-01-05 12:47:54 +01001687 if (need_siga_sync(q))
1688 qdio_sync_queues(q);
1689
Julian Wiedmann2f2f3832018-10-30 08:21:27 +01001690 qdio_check_outbound_pci_queues(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001691
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +02001692 count = qdio_inbound_q_moved(q, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +01001693 if (count == 0)
Jan Glauberd36deae2010-09-07 21:14:39 +00001694 return 0;
1695
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +02001696 start = add_buf(start, count);
1697 q->first_to_check = start;
1698
Jan Glauberd36deae2010-09-07 21:14:39 +00001699 /* Note: upper-layer MUST stop processing immediately here ... */
1700 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1701 return -EIO;
1702
Julian Wiedmann65e4f772019-03-28 10:43:46 +01001703 *bufnr = q->first_to_kick;
Jan Glauberd36deae2010-09-07 21:14:39 +00001704 *error = q->qdio_error;
1705
1706 /* for the next time */
Julian Wiedmann65e4f772019-03-28 10:43:46 +01001707 q->first_to_kick = add_buf(q->first_to_kick, count);
Jan Glauberd36deae2010-09-07 21:14:39 +00001708 q->qdio_error = 0;
Julian Wiedmann65e4f772019-03-28 10:43:46 +01001709
1710 return count;
Jan Glauberd36deae2010-09-07 21:14:39 +00001711}
1712EXPORT_SYMBOL(qdio_get_next_buffers);
1713
1714/**
1715 * qdio_stop_irq - disable interrupt processing for the device
1716 * @cdev: associated ccw_device for the qdio subchannel
1717 * @nr: input queue number
1718 *
1719 * Return codes
1720 * 0 - interrupts were already disabled
1721 * 1 - interrupts successfully disabled
1722 */
1723int qdio_stop_irq(struct ccw_device *cdev, int nr)
1724{
1725 struct qdio_q *q;
1726 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1727
1728 if (!irq_ptr)
1729 return -ENODEV;
1730 q = irq_ptr->input_qs[nr];
1731
1732 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1733 &q->u.in.queue_irq_state))
1734 return 0;
1735 else
1736 return 1;
1737}
1738EXPORT_SYMBOL(qdio_stop_irq);
1739
Eugene Crosser1c59a862013-04-24 12:00:23 +02001740/**
1741 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1742 * @schid: Subchannel ID.
1743 * @cnc: Boolean Change-Notification Control
1744 * @response: Response code will be stored at this address
1745 * @cb: Callback function will be executed for each element
1746 * of the address list
Eugene Crosser1c59a862013-04-24 12:00:23 +02001747 * @priv: Pointer to pass to the callback function.
1748 *
1749 * Performs "Store-network-bridging-information list" operation and calls
1750 * the callback function for every entry in the list. If "change-
1751 * notification-control" is set, further changes in the address list
1752 * will be reported via the IPA command.
1753 */
1754int qdio_pnso_brinfo(struct subchannel_id schid,
1755 int cnc, u16 *response,
1756 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1757 void *entry),
1758 void *priv)
1759{
1760 struct chsc_pnso_area *rr;
1761 int rc;
1762 u32 prev_instance = 0;
1763 int isfirstblock = 1;
1764 int i, size, elems;
1765
1766 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1767 if (rr == NULL)
1768 return -ENOMEM;
1769 do {
1770 /* on the first iteration, naihdr.resume_token will be zero */
1771 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1772 if (rc != 0 && rc != -EBUSY)
1773 goto out;
1774 if (rr->response.code != 1) {
1775 rc = -EIO;
1776 continue;
1777 } else
1778 rc = 0;
1779
1780 if (cb == NULL)
1781 continue;
1782
1783 size = rr->naihdr.naids;
1784 elems = (rr->response.length -
1785 sizeof(struct chsc_header) -
1786 sizeof(struct chsc_brinfo_naihdr)) /
1787 size;
1788
1789 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1790 /* Inform the caller that they need to scrap */
1791 /* the data that was already reported via cb */
1792 rc = -EAGAIN;
1793 break;
1794 }
1795 isfirstblock = 0;
1796 prev_instance = rr->naihdr.instance;
1797 for (i = 0; i < elems; i++)
1798 switch (size) {
1799 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1800 (*cb)(priv, l3_ipv6_addr,
1801 &rr->entries.l3_ipv6[i]);
1802 break;
1803 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1804 (*cb)(priv, l3_ipv4_addr,
1805 &rr->entries.l3_ipv4[i]);
1806 break;
1807 case sizeof(struct qdio_brinfo_entry_l2):
1808 (*cb)(priv, l2_addr_lnid,
1809 &rr->entries.l2[i]);
1810 break;
1811 default:
1812 WARN_ON_ONCE(1);
1813 rc = -EIO;
1814 goto out;
1815 }
1816 } while (rr->response.code == 0x0107 || /* channel busy */
1817 (rr->response.code == 1 && /* list stored */
1818 /* resume token is non-zero => list incomplete */
1819 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1820 (*response) = rr->response.code;
1821
1822out:
1823 free_page((unsigned long)rr);
1824 return rc;
1825}
1826EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1827
Jan Glauber779e6e12008-07-17 17:16:48 +02001828static int __init init_QDIO(void)
1829{
1830 int rc;
1831
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001832 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001833 if (rc)
1834 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001835 rc = qdio_setup_init();
1836 if (rc)
1837 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001838 rc = tiqdio_allocate_memory();
1839 if (rc)
1840 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001841 rc = tiqdio_register_thinints();
1842 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001843 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001844 return 0;
1845
Jan Glauber779e6e12008-07-17 17:16:48 +02001846out_ti:
1847 tiqdio_free_memory();
1848out_cache:
1849 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001850out_debug:
1851 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001852 return rc;
1853}
1854
1855static void __exit exit_QDIO(void)
1856{
1857 tiqdio_unregister_thinints();
1858 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001859 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001860 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001861}
1862
1863module_init(init_QDIO);
1864module_exit(exit_QDIO);