blob: c890848064fe69a55dbfd380c43214c7c0da9a8b [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);
Julian Wiedmann4e79a5d2019-08-19 17:20:05 +0200134 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "EQBS part:%02x",
Jan Glauber25f269f2011-10-30 15:17:06 +0100135 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
Julian Wiedmannb7f143d2019-10-31 13:42:14 +0100313static int qdio_siga_output(struct qdio_q *q, unsigned int count,
314 unsigned int *busy_bit, 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
Julian Wiedmannb7f143d2019-10-31 13:42:14 +0100321 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
322 if (count > 1)
323 fc = QDIO_SIGA_WRITEM;
324 else if (aob)
325 fc = QDIO_SIGA_WRITEQ;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000326 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200327
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100328 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200329 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100330 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200331 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200332again:
Julian Wiedmannb7f143d2019-10-31 13:42:14 +0100333 cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200334
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100335 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100336 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200337 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100338
339 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200340 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100341 goto again;
342 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200343 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200344 goto again;
345 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200346 if (retries) {
347 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
348 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
349 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
350 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200351 return cc;
352}
353
354static inline int qdio_siga_input(struct qdio_q *q)
355{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100356 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
357 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200358 int cc;
359
Jan Glauber22f99342008-12-25 13:38:46 +0100360 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100361 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200362
Jan Glauber958c0ba2011-01-05 12:47:52 +0100363 if (is_qebsm(q)) {
364 schid = q->irq_ptr->sch_token;
365 fc |= QDIO_SIGA_QEBSM_FLAG;
366 }
367
368 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100369 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100370 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200371 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200372}
373
Jan Glauber90adac52011-01-05 12:47:54 +0100374#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
375#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
376
377static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200378{
Jan Glauber90adac52011-01-05 12:47:54 +0100379 /* PCI capable outbound queues will also be scanned so sync them too */
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100380 if (pci_out_supported(q->irq_ptr))
Jan Glauber90adac52011-01-05 12:47:54 +0100381 qdio_siga_sync_all(q);
382 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200383 qdio_siga_sync_q(q);
384}
385
Jan Glauber60b5df22009-06-22 12:08:10 +0200386int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
387 unsigned char *state)
388{
Jan Glauber90adac52011-01-05 12:47:54 +0100389 if (need_siga_sync(q))
390 qdio_siga_sync_q(q);
Julian Wiedmann5a19d672019-04-26 09:30:11 +0200391 return get_buf_state(q, bufnr, state, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200392}
393
394static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200395{
Julian Wiedmann0b6f4992019-11-25 14:18:29 +0100396 if (!q->u.in.ack_count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200397 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100398
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 */
Julian Wiedmann0b6f4992019-11-25 14:18:29 +0100402 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
403 q->u.in.ack_count);
404 q->u.in.ack_count = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200405}
406
Fabian Frederick92bdae52014-06-03 21:55:15 +0200407static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100408{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200409 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100410
411 q->q_stats.nr_sbal_total += count;
412 if (count == QDIO_MAX_BUFFERS_MASK) {
413 q->q_stats.nr_sbals[7]++;
414 return;
415 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200416 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100417 q->q_stats.nr_sbals[pos]++;
418}
419
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200420static void process_buffer_error(struct qdio_q *q, unsigned int start,
421 int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200422{
Jan Glauber1549d132012-05-09 16:27:34 +0200423 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100424
425 /* special handling for no target buffer empty */
Julian Wiedmannb23481f2017-10-23 09:38:18 +0200426 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200427 q->sbal[start]->element[15].sflags == 0x10) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100428 qperf_inc(q, target_full);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200429 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
Julian Wiedmann6d76c8982019-10-04 11:07:13 +0200430 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100431 }
432
Jan Glauber22f99342008-12-25 13:38:46 +0100433 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
434 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200435 DBF_ERROR("FTC:%3d C:%3d", start, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100436 DBF_ERROR("F14:%2x F15:%2x",
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200437 q->sbal[start]->element[14].sflags,
438 q->sbal[start]->element[15].sflags);
Jan Glauber50f769d2008-12-25 13:38:47 +0100439}
Jan Glauber779e6e12008-07-17 17:16:48 +0200440
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200441static inline void inbound_primed(struct qdio_q *q, unsigned int start,
442 int count)
Jan Glauber50f769d2008-12-25 13:38:47 +0100443{
444 int new;
445
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100446 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100447
448 /* for QEBSM the ACK was already set by EQBS */
449 if (is_qebsm(q)) {
Julian Wiedmann0b6f4992019-11-25 14:18:29 +0100450 if (!q->u.in.ack_count) {
Jan Glauber50f769d2008-12-25 13:38:47 +0100451 q->u.in.ack_count = count;
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200452 q->u.in.ack_start = start;
Jan Glauber50f769d2008-12-25 13:38:47 +0100453 return;
454 }
455
456 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100457 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100458 q->u.in.ack_count);
459 q->u.in.ack_count = count;
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200460 q->u.in.ack_start = start;
Jan Glauber50f769d2008-12-25 13:38:47 +0100461 return;
462 }
463
464 /*
465 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
466 * or by the next inbound run.
467 */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200468 new = add_buf(start, count - 1);
Julian Wiedmann0b6f4992019-11-25 14:18:29 +0100469 if (q->u.in.ack_count) {
Jan Glauber50f769d2008-12-25 13:38:47 +0100470 /* reset the previous ACK but first set the new one */
471 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100472 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100473 } else {
Julian Wiedmann0b6f4992019-11-25 14:18:29 +0100474 q->u.in.ack_count = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100475 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100476 }
477
Jan Glaubere85dea02009-03-26 15:24:29 +0100478 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100479 count--;
480 if (!count)
481 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200482 /* need to change ALL buffers to get more interrupts */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200483 set_buf_states(q, start, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200484}
485
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200486static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200487{
Jan Glauber6fa10982011-01-31 11:30:08 +0100488 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100489 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200490
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200491 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100492
Jan Glauber779e6e12008-07-17 17:16:48 +0200493 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200494 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
495 * would return 0.
496 */
497 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100498 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100499 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200500
Jan Glauber36e3e722009-06-22 12:08:12 +0200501 /*
502 * No siga sync here, as a PCI or we after a thin interrupt
503 * already sync'ed the queues.
504 */
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200505 count = get_buf_states(q, start, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200506 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100507 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200508
509 switch (state) {
510 case SLSB_P_INPUT_PRIMED:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200511 inbound_primed(q, start, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200512 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100513 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100514 if (q->irq_ptr->perf_stat_enabled)
515 account_sbals(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100516 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200517 case SLSB_P_INPUT_ERROR:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200518 process_buffer_error(q, start, count);
Julian Wiedmann6d76c8982019-10-04 11:07:13 +0200519 /*
520 * Interrupts may be avoided as long as the error is present
521 * so change the buffer state immediately to avoid starvation.
522 */
523 set_buf_states(q, start, SLSB_P_INPUT_NOT_INIT, count);
Julian Wiedmann0b926ac2017-10-23 09:40:16 +0200524 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
525 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100526 if (q->irq_ptr->perf_stat_enabled)
527 account_sbals_error(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100528 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200529 case SLSB_CU_INPUT_EMPTY:
530 case SLSB_P_INPUT_NOT_INIT:
531 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100532 if (q->irq_ptr->perf_stat_enabled)
533 q->q_stats.nr_sbal_nop++;
Julian Wiedmannf83435c2016-11-21 11:34:25 +0100534 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200535 q->nr, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100536 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200537 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200538 WARN_ON_ONCE(1);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100539 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200540 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200541}
542
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200543static int qdio_inbound_q_moved(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200544{
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100545 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200546
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200547 count = get_inbound_buffer_frontier(q, start);
Jan Glauber779e6e12008-07-17 17:16:48 +0200548
Julian Wiedmanndccbbaf2019-03-28 10:45:11 +0100549 if (count && !is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
550 q->u.in.timestamp = get_tod_clock();
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100551
552 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200553}
554
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200555static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
Jan Glauber60b5df22009-06-22 12:08:10 +0200556{
557 unsigned char state = 0;
558
559 if (!atomic_read(&q->nr_buf_used))
560 return 1;
561
Jan Glauber90adac52011-01-05 12:47:54 +0100562 if (need_siga_sync(q))
563 qdio_siga_sync_q(q);
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200564 get_buf_state(q, start, &state, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200565
Ursula Braun4c522282010-02-09 09:46:07 +0100566 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200567 /* more work coming */
568 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200569
570 if (is_thinint_irq(q->irq_ptr))
571 return 1;
572
573 /* don't poll under z/VM */
574 if (MACHINE_IS_VM)
575 return 1;
576
577 /*
578 * At this point we know, that inbound first_to_check
579 * has (probably) not moved (see qdio_inbound_processing).
580 */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200581 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200582 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x", start);
Jan Glauber9a2c1602009-06-22 12:08:11 +0200583 return 1;
584 } else
585 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200586}
587
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000588static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
589{
590 unsigned char state = 0;
591 int j, b = start;
592
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000593 for (j = 0; j < count; ++j) {
594 get_buf_state(q, b, &state, 0);
595 if (state == SLSB_P_OUTPUT_PENDING) {
596 struct qaob *aob = q->u.out.aobs[b];
597 if (aob == NULL)
598 continue;
599
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000600 q->u.out.sbal_state[b].flags |=
601 QDIO_OUTBUF_STATE_FLAG_PENDING;
602 q->u.out.aobs[b] = NULL;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000603 }
604 b = next_buf(b);
605 }
606}
607
608static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
609 int bufnr)
610{
611 unsigned long phys_aob = 0;
612
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000613 if (!q->aobs[bufnr]) {
614 struct qaob *aob = qdio_allocate_aob();
615 q->aobs[bufnr] = aob;
616 }
617 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000618 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
619 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200620 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000621 }
622
Julian Wiedmann64e03ff2018-05-16 09:37:25 +0200623 q->sbal_state[bufnr].flags = 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000624 return phys_aob;
625}
626
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100627static void qdio_kick_handler(struct qdio_q *q, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200628{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100629 int start = q->first_to_kick;
Jan Glauber779e6e12008-07-17 17:16:48 +0200630
631 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
632 return;
633
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100634 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100635 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200636 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100637 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100638 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200639 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
640 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100641 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100642
643 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
644 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200645
646 /* for the next time */
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100647 q->first_to_kick = add_buf(start, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200648 q->qdio_error = 0;
649}
650
Ursula Braun9bce8b22016-08-05 12:33:10 +0200651static inline int qdio_tasklet_schedule(struct qdio_q *q)
652{
653 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
654 tasklet_schedule(&q->tasklet);
655 return 0;
656 }
657 return -EPERM;
658}
659
Jan Glauber779e6e12008-07-17 17:16:48 +0200660static void __qdio_inbound_processing(struct qdio_q *q)
661{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200662 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100663 int count;
664
Jan Glauber6486cda2010-01-04 09:05:42 +0100665 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200666
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200667 count = qdio_inbound_q_moved(q, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100668 if (count == 0)
Jan Glauber779e6e12008-07-17 17:16:48 +0200669 return;
670
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200671 start = add_buf(start, count);
672 q->first_to_check = start;
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100673 qdio_kick_handler(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200674
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200675 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200676 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100677 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200678 if (!qdio_tasklet_schedule(q))
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200679 return;
Jan Glauber6486cda2010-01-04 09:05:42 +0100680 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200681
682 qdio_stop_polling(q);
683 /*
684 * We need to check again to not lose initiative after
685 * resetting the ACK state.
686 */
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200687 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100688 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200689 qdio_tasklet_schedule(q);
Jan Glauber6486cda2010-01-04 09:05:42 +0100690 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200691}
692
Jan Glauber779e6e12008-07-17 17:16:48 +0200693void qdio_inbound_processing(unsigned long data)
694{
695 struct qdio_q *q = (struct qdio_q *)data;
696 __qdio_inbound_processing(q);
697}
698
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200699static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200700{
Jan Glauber6fa10982011-01-31 11:30:08 +0100701 unsigned char state = 0;
Julian Wiedmann152485b2017-12-06 08:53:33 +0100702 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200703
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200704 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100705
Jan Glauber90adac52011-01-05 12:47:54 +0100706 if (need_siga_sync(q))
707 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100708 !pci_out_supported(q->irq_ptr)) ||
Jan Glauber90adac52011-01-05 12:47:54 +0100709 (queue_type(q) == QDIO_IQDIO_QFMT &&
710 multicast_outbound(q)))
711 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200712
Julian Wiedmann30607812019-04-01 10:08:08 +0200713 count = atomic_read(&q->nr_buf_used);
Julian Wiedmann152485b2017-12-06 08:53:33 +0100714 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100715 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200716
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200717 count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200718 if (!count)
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100719 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200720
721 switch (state) {
722 case SLSB_P_OUTPUT_EMPTY:
Julian Wiedmann04310322019-06-03 07:47:04 +0200723 case SLSB_P_OUTPUT_PENDING:
Jan Glauber779e6e12008-07-17 17:16:48 +0200724 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000725 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
726 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200727
728 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100729 if (q->irq_ptr->perf_stat_enabled)
730 account_sbals(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100731 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200732 case SLSB_P_OUTPUT_ERROR:
Julian Wiedmann5b2ad272019-04-08 11:19:54 +0200733 process_buffer_error(q, start, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200734 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100735 if (q->irq_ptr->perf_stat_enabled)
736 account_sbals_error(q, count);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100737 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200738 case SLSB_CU_OUTPUT_PRIMED:
739 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100740 if (q->irq_ptr->perf_stat_enabled)
741 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000742 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
743 q->nr);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100744 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200745 case SLSB_P_OUTPUT_NOT_INIT:
746 case SLSB_P_OUTPUT_HALTED:
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100747 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200748 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200749 WARN_ON_ONCE(1);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100750 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200751 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200752}
753
754/* all buffers processed? */
755static inline int qdio_outbound_q_done(struct qdio_q *q)
756{
757 return atomic_read(&q->nr_buf_used) == 0;
758}
759
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200760static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
Jan Glauber779e6e12008-07-17 17:16:48 +0200761{
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100762 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200763
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200764 count = get_outbound_buffer_frontier(q, start);
Jan Glauber779e6e12008-07-17 17:16:48 +0200765
Julian Wiedmann7c47f5a2019-08-23 11:48:47 +0200766 if (count) {
Jan Glauber22f99342008-12-25 13:38:46 +0100767 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Julian Wiedmann7c47f5a2019-08-23 11:48:47 +0200768 if (q->u.out.use_cq)
769 qdio_handle_aobs(q, start, count);
770 }
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100771
772 return count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200773}
774
Julian Wiedmannb7f143d2019-10-31 13:42:14 +0100775static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
776 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200777{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200778 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100779 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200780
781 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100782 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200783
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100784 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200785retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100786 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100787
Julian Wiedmannb7f143d2019-10-31 13:42:14 +0100788 cc = qdio_siga_output(q, count, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100789 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200790 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200791 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100792 case 2:
793 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200794 while (++retries < QDIO_BUSY_BIT_RETRIES) {
795 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
796 goto retry;
797 }
798 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200799 cc = -EBUSY;
800 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100801 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200802 cc = -ENOBUFS;
803 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100804 break;
805 case 1:
806 case 3:
807 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200808 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100809 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200810 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200811 if (retries) {
812 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
813 DBF_ERROR("count:%u", retries);
814 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100815 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200816}
817
Jan Glauber779e6e12008-07-17 17:16:48 +0200818static void __qdio_outbound_processing(struct qdio_q *q)
819{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200820 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100821 int count;
822
Jan Glauber6486cda2010-01-04 09:05:42 +0100823 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200824 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200825
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200826 count = qdio_outbound_q_moved(q, start);
827 if (count) {
828 q->first_to_check = add_buf(start, count);
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100829 qdio_kick_handler(q, count);
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200830 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200831
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100832 if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
833 !qdio_outbound_q_done(q))
834 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200835
Jan Glauber779e6e12008-07-17 17:16:48 +0200836 if (q->u.out.pci_out_enabled)
837 return;
838
839 /*
840 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100841 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
842 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200843 */
844 if (qdio_outbound_q_done(q))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200845 del_timer_sync(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100846 else
Ursula Braun9bce8b22016-08-05 12:33:10 +0200847 if (!timer_pending(&q->u.out.timer) &&
848 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber779e6e12008-07-17 17:16:48 +0200849 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100850 return;
851
852sched:
Ursula Braun9bce8b22016-08-05 12:33:10 +0200853 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200854}
855
856/* outbound tasklet */
857void qdio_outbound_processing(unsigned long data)
858{
859 struct qdio_q *q = (struct qdio_q *)data;
860 __qdio_outbound_processing(q);
861}
862
Kees Cookcb9f7802017-10-04 17:54:35 -0700863void qdio_outbound_timer(struct timer_list *t)
Jan Glauber779e6e12008-07-17 17:16:48 +0200864{
Kees Cookcb9f7802017-10-04 17:54:35 -0700865 struct qdio_q *q = from_timer(q, t, u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +0100866
Ursula Braun9bce8b22016-08-05 12:33:10 +0200867 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200868}
869
Julian Wiedmann2f2f3832018-10-30 08:21:27 +0100870static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
Jan Glauber779e6e12008-07-17 17:16:48 +0200871{
872 struct qdio_q *out;
873 int i;
874
Julian Wiedmann313dc682019-08-23 11:48:48 +0200875 if (!pci_out_supported(irq) || !irq->scan_threshold)
Jan Glauber779e6e12008-07-17 17:16:48 +0200876 return;
877
Julian Wiedmannf85b2b22018-10-30 08:19:54 +0100878 for_each_output_queue(irq, out, i)
Jan Glauber779e6e12008-07-17 17:16:48 +0200879 if (!qdio_outbound_q_done(out))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200880 qdio_tasklet_schedule(out);
Jan Glauber779e6e12008-07-17 17:16:48 +0200881}
882
Jan Glauber60b5df22009-06-22 12:08:10 +0200883static void __tiqdio_inbound_processing(struct qdio_q *q)
884{
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200885 unsigned int start = q->first_to_check;
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100886 int count;
887
Jan Glauber6486cda2010-01-04 09:05:42 +0100888 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100889 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
890 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200891
Julian Wiedmann2f2f3832018-10-30 08:21:27 +0100892 /* The interrupt could be caused by a PCI request: */
893 qdio_check_outbound_pci_queues(q->irq_ptr);
Jan Glauber60b5df22009-06-22 12:08:10 +0200894
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200895 count = qdio_inbound_q_moved(q, start);
Julian Wiedmannb39544c2019-03-28 10:39:25 +0100896 if (count == 0)
Jan Glauber60b5df22009-06-22 12:08:10 +0200897 return;
898
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200899 start = add_buf(start, count);
900 q->first_to_check = start;
Julian Wiedmann65e4f772019-03-28 10:43:46 +0100901 qdio_kick_handler(q, count);
Jan Glauber60b5df22009-06-22 12:08:10 +0200902
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200903 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100904 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200905 if (!qdio_tasklet_schedule(q))
Jan Glaubere2910bc2009-09-11 10:28:19 +0200906 return;
Jan Glauber60b5df22009-06-22 12:08:10 +0200907 }
908
909 qdio_stop_polling(q);
910 /*
911 * We need to check again to not lose initiative after
912 * resetting the ACK state.
913 */
Julian Wiedmann6bcf74e2019-04-08 13:32:12 +0200914 if (!qdio_inbound_q_done(q, start)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100915 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200916 qdio_tasklet_schedule(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200917 }
918}
919
920void tiqdio_inbound_processing(unsigned long data)
921{
922 struct qdio_q *q = (struct qdio_q *)data;
923 __tiqdio_inbound_processing(q);
924}
925
Jan Glauber779e6e12008-07-17 17:16:48 +0200926static inline void qdio_set_state(struct qdio_irq *irq_ptr,
927 enum qdio_irq_states state)
928{
Jan Glauber22f99342008-12-25 13:38:46 +0100929 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200930
931 irq_ptr->state = state;
932 mb();
933}
934
Jan Glauber22f99342008-12-25 13:38:46 +0100935static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200936{
Jan Glauber779e6e12008-07-17 17:16:48 +0200937 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100938 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
939 DBF_ERROR_HEX(irb, 64);
940 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200941 }
942}
943
944/* PCI interrupt handler */
945static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
946{
947 int i;
948 struct qdio_q *q;
949
Ursula Braun9bce8b22016-08-05 12:33:10 +0200950 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
Jan Glauberc38f9602009-03-26 15:24:26 +0100951 return;
952
Julian Wiedmann0a6e6342020-03-25 10:35:00 +0100953 if (irq_ptr->irq_poll) {
954 if (!test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
955 irq_ptr->irq_poll(irq_ptr->cdev, irq_ptr->int_parm);
956 else
957 QDIO_PERF_STAT_INC(irq_ptr, int_discarded);
958 } else {
959 for_each_input_queue(irq_ptr, q, i)
Jan Glauberd36deae2010-09-07 21:14:39 +0000960 tasklet_schedule(&q->tasklet);
961 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200962
Julian Wiedmann313dc682019-08-23 11:48:48 +0200963 if (!pci_out_supported(irq_ptr) || !irq_ptr->scan_threshold)
Jan Glauber779e6e12008-07-17 17:16:48 +0200964 return;
965
966 for_each_output_queue(irq_ptr, q, i) {
967 if (qdio_outbound_q_done(q))
968 continue;
Jan Glauber90adac52011-01-05 12:47:54 +0100969 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +0200970 qdio_siga_sync_q(q);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200971 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200972 }
973}
974
975static void qdio_handle_activate_check(struct ccw_device *cdev,
976 unsigned long intparm, int cstat, int dstat)
977{
978 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
979 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200980 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200981
Jan Glauber22f99342008-12-25 13:38:46 +0100982 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
983 DBF_ERROR("intp :%lx", intparm);
984 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +0200985
986 if (irq_ptr->nr_input_qs) {
987 q = irq_ptr->input_qs[0];
988 } else if (irq_ptr->nr_output_qs) {
989 q = irq_ptr->output_qs[0];
990 } else {
991 dump_stack();
992 goto no_handler;
993 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200994
995 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +0200996 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200997 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200998no_handler:
999 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001000 /*
1001 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1002 * Therefore we call the LGR detection function here.
1003 */
1004 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001005}
1006
Jan Glauber779e6e12008-07-17 17:16:48 +02001007static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1008 int dstat)
1009{
1010 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001011
Jan Glauber22f99342008-12-25 13:38:46 +01001012 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001013
1014 if (cstat)
1015 goto error;
1016 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1017 goto error;
1018 if (!(dstat & DEV_STAT_DEV_END))
1019 goto error;
1020 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1021 return;
1022
1023error:
1024 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1025 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1026 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001027}
1028
1029/* qdio interrupt handler */
1030void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1031 struct irb *irb)
1032{
1033 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001034 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001035 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001036
Jan Glauber779e6e12008-07-17 17:16:48 +02001037 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001038 ccw_device_get_schid(cdev, &schid);
1039 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001040 return;
1041 }
1042
Jan Glauber09a308f2010-05-17 10:00:14 +02001043 if (irq_ptr->perf_stat_enabled)
1044 irq_ptr->perf_stat.qdio_int++;
1045
Jan Glauber779e6e12008-07-17 17:16:48 +02001046 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001047 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1048 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1049 wake_up(&cdev->private->wait_q);
1050 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001051 }
Jan Glauber22f99342008-12-25 13:38:46 +01001052 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001053 cstat = irb->scsw.cmd.cstat;
1054 dstat = irb->scsw.cmd.dstat;
1055
1056 switch (irq_ptr->state) {
1057 case QDIO_IRQ_STATE_INACTIVE:
1058 qdio_establish_handle_irq(cdev, cstat, dstat);
1059 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001060 case QDIO_IRQ_STATE_CLEANUP:
1061 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1062 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001063 case QDIO_IRQ_STATE_ESTABLISHED:
1064 case QDIO_IRQ_STATE_ACTIVE:
1065 if (cstat & SCHN_STAT_PCI) {
1066 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001067 return;
1068 }
Jan Glauber4c575422009-06-12 10:26:28 +02001069 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001070 qdio_handle_activate_check(cdev, intparm, cstat,
1071 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001072 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001073 case QDIO_IRQ_STATE_STOPPED:
1074 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001075 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001076 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001077 }
1078 wake_up(&cdev->private->wait_q);
1079}
1080
1081/**
1082 * qdio_get_ssqd_desc - get qdio subchannel description
1083 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001084 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001085 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001086 * Returns 0 or an error code. The results of the chsc are stored in the
1087 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001088 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001089int qdio_get_ssqd_desc(struct ccw_device *cdev,
1090 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001091{
Sebastian Ott9080c922016-07-28 20:30:31 +02001092 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001093
Jan Glauberbbd50e12008-12-25 13:38:43 +01001094 if (!cdev || !cdev->private)
1095 return -EINVAL;
1096
Sebastian Ott9080c922016-07-28 20:30:31 +02001097 ccw_device_get_schid(cdev, &schid);
1098 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1099 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001100}
1101EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1102
Julian Wiedmann014816b2020-02-10 14:58:07 +01001103static void qdio_shutdown_queues(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +02001104{
Jan Glauber779e6e12008-07-17 17:16:48 +02001105 struct qdio_q *q;
1106 int i;
1107
1108 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001109 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001110
1111 for_each_output_queue(irq_ptr, q, i) {
Ursula Braun9bce8b22016-08-05 12:33:10 +02001112 del_timer_sync(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001113 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001114 }
1115}
1116
1117/**
1118 * qdio_shutdown - shut down a qdio subchannel
1119 * @cdev: associated ccw device
1120 * @how: use halt or clear to shutdown
1121 */
1122int qdio_shutdown(struct ccw_device *cdev, int how)
1123{
Jan Glauber22f99342008-12-25 13:38:46 +01001124 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001125 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001126 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001127
Jan Glauber779e6e12008-07-17 17:16:48 +02001128 if (!irq_ptr)
1129 return -ENODEV;
1130
Jan Glauberce1d8012012-10-24 12:38:35 +02001131 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001132 ccw_device_get_schid(cdev, &schid);
1133 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001134
Jan Glauber779e6e12008-07-17 17:16:48 +02001135 mutex_lock(&irq_ptr->setup_mutex);
1136 /*
1137 * Subchannel was already shot down. We cannot prevent being called
1138 * twice since cio may trigger a shutdown asynchronously.
1139 */
1140 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1141 mutex_unlock(&irq_ptr->setup_mutex);
1142 return 0;
1143 }
1144
Jan Glauberc38f9602009-03-26 15:24:26 +01001145 /*
1146 * Indicate that the device is going down. Scheduling the queue
1147 * tasklets is forbidden from here on.
1148 */
1149 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1150
Julian Wiedmann94c43bd2019-07-23 11:55:27 +02001151 tiqdio_remove_device(irq_ptr);
Julian Wiedmann014816b2020-02-10 14:58:07 +01001152 qdio_shutdown_queues(irq_ptr);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001153 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001154
1155 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001156 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001157
1158 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1159 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1160 else
1161 /* default behaviour is halt */
1162 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1163 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001164 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1165 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001166 goto no_cleanup;
1167 }
1168
1169 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001170 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001171 wait_event_interruptible_timeout(cdev->private->wait_q,
1172 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1173 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1174 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001175 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001176
1177no_cleanup:
1178 qdio_shutdown_thinint(irq_ptr);
1179
1180 /* restore interrupt handler */
Julian Wiedmann89286322018-03-21 17:14:00 +01001181 if ((void *)cdev->handler == (void *)qdio_int_handler) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001182 cdev->handler = irq_ptr->orig_handler;
Julian Wiedmann89286322018-03-21 17:14:00 +01001183 cdev->private->intparm = 0;
1184 }
Sebastian Otta48ed862016-07-29 13:41:20 +02001185 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001186
1187 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1188 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001189 if (rc)
1190 return rc;
1191 return 0;
1192}
1193EXPORT_SYMBOL_GPL(qdio_shutdown);
1194
1195/**
1196 * qdio_free - free data structures for a qdio subchannel
1197 * @cdev: associated ccw device
1198 */
1199int qdio_free(struct ccw_device *cdev)
1200{
Jan Glauber22f99342008-12-25 13:38:46 +01001201 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001202 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001203
Jan Glauber779e6e12008-07-17 17:16:48 +02001204 if (!irq_ptr)
1205 return -ENODEV;
1206
Sebastian Ott9080c922016-07-28 20:30:31 +02001207 ccw_device_get_schid(cdev, &schid);
1208 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001209 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001210 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001211
Stefan Raspl613c4e02014-06-12 14:24:45 +02001212 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001213 cdev->private->qdio_data = NULL;
1214 mutex_unlock(&irq_ptr->setup_mutex);
1215
1216 qdio_release_memory(irq_ptr);
1217 return 0;
1218}
1219EXPORT_SYMBOL_GPL(qdio_free);
1220
1221/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001222 * qdio_allocate - allocate qdio queues and associated data
1223 * @init_data: initialization data
1224 */
1225int qdio_allocate(struct qdio_initialize *init_data)
1226{
Julian Wiedmannb2745652020-03-20 14:00:00 +01001227 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001228 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001229 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001230
Julian Wiedmannb2745652020-03-20 14:00:00 +01001231 ccw_device_get_schid(cdev, &schid);
Sebastian Ott9080c922016-07-28 20:30:31 +02001232 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001233
1234 if ((init_data->no_input_qs && !init_data->input_handler) ||
1235 (init_data->no_output_qs && !init_data->output_handler))
1236 return -EINVAL;
1237
1238 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1239 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1240 return -EINVAL;
1241
1242 if ((!init_data->input_sbal_addr_array) ||
1243 (!init_data->output_sbal_addr_array))
1244 return -EINVAL;
1245
Jan Glauber779e6e12008-07-17 17:16:48 +02001246 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1247 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1248 if (!irq_ptr)
1249 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001250
Julian Wiedmannb2745652020-03-20 14:00:00 +01001251 irq_ptr->cdev = cdev;
Jan Glauber779e6e12008-07-17 17:16:48 +02001252 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001253 if (qdio_allocate_dbf(init_data, irq_ptr))
1254 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001255
1256 /*
1257 * Allocate a page for the chsc calls in qdio_establish.
1258 * Must be pre-allocated since a zfcp recovery will call
1259 * qdio_establish. In case of low memory and swap on a zfcp disk
1260 * we may not be able to allocate memory otherwise.
1261 */
1262 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1263 if (!irq_ptr->chsc_page)
1264 goto out_rel;
1265
1266 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001267 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001268 if (!irq_ptr->qdr)
1269 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001270
Jan Glauber779e6e12008-07-17 17:16:48 +02001271 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1272 init_data->no_output_qs))
1273 goto out_rel;
1274
Julian Wiedmann94c43bd2019-07-23 11:55:27 +02001275 INIT_LIST_HEAD(&irq_ptr->entry);
Julian Wiedmannb2745652020-03-20 14:00:00 +01001276 cdev->private->qdio_data = irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001277 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1278 return 0;
1279out_rel:
1280 qdio_release_memory(irq_ptr);
1281out_err:
1282 return -ENOMEM;
1283}
1284EXPORT_SYMBOL_GPL(qdio_allocate);
1285
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001286static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1287{
1288 struct qdio_q *q = irq_ptr->input_qs[0];
1289 int i, use_cq = 0;
1290
1291 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1292 use_cq = 1;
1293
1294 for_each_output_queue(irq_ptr, q, i) {
1295 if (use_cq) {
Julian Wiedmann69e96202019-07-01 14:19:29 +02001296 if (multicast_outbound(q))
1297 continue;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001298 if (qdio_enable_async_operation(&q->u.out) < 0) {
1299 use_cq = 0;
1300 continue;
1301 }
1302 } else
1303 qdio_disable_async_operation(&q->u.out);
1304 }
1305 DBF_EVENT("use_cq:%d", use_cq);
1306}
1307
Jan Glauber779e6e12008-07-17 17:16:48 +02001308/**
1309 * qdio_establish - establish queues on a qdio subchannel
1310 * @init_data: initialization data
1311 */
1312int qdio_establish(struct qdio_initialize *init_data)
1313{
Jan Glauber779e6e12008-07-17 17:16:48 +02001314 struct ccw_device *cdev = init_data->cdev;
Julian Wiedmann014816b2020-02-10 14:58:07 +01001315 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001316 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001317 int rc;
1318
Sebastian Ott9080c922016-07-28 20:30:31 +02001319 ccw_device_get_schid(cdev, &schid);
1320 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001321
Jan Glauber779e6e12008-07-17 17:16:48 +02001322 if (!irq_ptr)
1323 return -ENODEV;
1324
Jan Glauber779e6e12008-07-17 17:16:48 +02001325 mutex_lock(&irq_ptr->setup_mutex);
Julian Wiedmann014816b2020-02-10 14:58:07 +01001326 qdio_setup_irq(irq_ptr, init_data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001327
1328 rc = qdio_establish_thinint(irq_ptr);
1329 if (rc) {
1330 mutex_unlock(&irq_ptr->setup_mutex);
1331 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1332 return rc;
1333 }
1334
1335 /* establish q */
1336 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1337 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1338 irq_ptr->ccw.count = irq_ptr->equeue.count;
1339 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1340
Sebastian Otta48ed862016-07-29 13:41:20 +02001341 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001342 ccw_device_set_options_mask(cdev, 0);
1343
1344 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001345 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001346 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001347 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1348 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001349 mutex_unlock(&irq_ptr->setup_mutex);
1350 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1351 return rc;
1352 }
1353
1354 wait_event_interruptible_timeout(cdev->private->wait_q,
1355 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1356 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1357
1358 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1359 mutex_unlock(&irq_ptr->setup_mutex);
1360 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1361 return -EIO;
1362 }
1363
1364 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001365
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001366 qdio_detect_hsicq(irq_ptr);
1367
Jan Glauber779e6e12008-07-17 17:16:48 +02001368 /* qebsm is now setup if available, initialize buffer states */
1369 qdio_init_buf_states(irq_ptr);
1370
1371 mutex_unlock(&irq_ptr->setup_mutex);
Julian Wiedmannb2745652020-03-20 14:00:00 +01001372 qdio_print_subchannel_info(irq_ptr);
1373 qdio_setup_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001374 return 0;
1375}
1376EXPORT_SYMBOL_GPL(qdio_establish);
1377
1378/**
1379 * qdio_activate - activate queues on a qdio subchannel
1380 * @cdev: associated cdev
1381 */
1382int qdio_activate(struct ccw_device *cdev)
1383{
Julian Wiedmann014816b2020-02-10 14:58:07 +01001384 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001385 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001386 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001387
Sebastian Ott9080c922016-07-28 20:30:31 +02001388 ccw_device_get_schid(cdev, &schid);
1389 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001390
Jan Glauber779e6e12008-07-17 17:16:48 +02001391 if (!irq_ptr)
1392 return -ENODEV;
1393
Jan Glauber779e6e12008-07-17 17:16:48 +02001394 mutex_lock(&irq_ptr->setup_mutex);
1395 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1396 rc = -EBUSY;
1397 goto out;
1398 }
1399
Jan Glauber779e6e12008-07-17 17:16:48 +02001400 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1401 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1402 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1403 irq_ptr->ccw.cda = 0;
1404
Sebastian Otta48ed862016-07-29 13:41:20 +02001405 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001406 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1407
1408 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1409 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001410 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001411 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001412 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1413 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001414 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001415 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001416
1417 if (is_thinint_irq(irq_ptr))
Julian Wiedmann94c43bd2019-07-23 11:55:27 +02001418 tiqdio_add_device(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001419
1420 /* wait for subchannel to become active */
1421 msleep(5);
1422
1423 switch (irq_ptr->state) {
1424 case QDIO_IRQ_STATE_STOPPED:
1425 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001426 rc = -EIO;
1427 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001428 default:
1429 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1430 rc = 0;
1431 }
1432out:
1433 mutex_unlock(&irq_ptr->setup_mutex);
1434 return rc;
1435}
1436EXPORT_SYMBOL_GPL(qdio_activate);
1437
1438static inline int buf_in_between(int bufnr, int start, int count)
1439{
1440 int end = add_buf(start, count);
1441
1442 if (end > start) {
1443 if (bufnr >= start && bufnr < end)
1444 return 1;
1445 else
1446 return 0;
1447 }
1448
1449 /* wrap-around case */
1450 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1451 (bufnr < end))
1452 return 1;
1453 else
1454 return 0;
1455}
1456
1457/**
1458 * handle_inbound - reset processed input buffers
1459 * @q: queue containing the buffers
1460 * @callflags: flags
1461 * @bufnr: first buffer to process
1462 * @count: how many buffers are emptied
1463 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001464static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1465 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001466{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001467 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001468
Jan Glauber6486cda2010-01-04 09:05:42 +01001469 qperf_inc(q, inbound_call);
1470
Julian Wiedmann0b6f4992019-11-25 14:18:29 +01001471 if (!q->u.in.ack_count)
Jan Glauber50f769d2008-12-25 13:38:47 +01001472 goto set;
1473
1474 /* protect against stop polling setting an ACK for an emptied slsb */
1475 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1476 /* overwriting everything, just delete polling status */
Jan Glauber50f769d2008-12-25 13:38:47 +01001477 q->u.in.ack_count = 0;
1478 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001479 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001480 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001481 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001482 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001483 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001484 q->u.in.ack_count -= diff;
1485 if (q->u.in.ack_count <= 0) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001486 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001487 goto set;
1488 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001489 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Julian Wiedmann0b6f4992019-11-25 14:18:29 +01001490 } else {
1491 /* the only ACK will be deleted */
1492 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001493 }
Jan Glauber50f769d2008-12-25 13:38:47 +01001494 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001495
Jan Glauber50f769d2008-12-25 13:38:47 +01001496set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001497 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001498 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001499
Jan Glauberd303b6f2009-03-26 15:24:31 +01001500 if (need_siga_in(q))
1501 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001502
Jan Glauberd303b6f2009-03-26 15:24:31 +01001503 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001504}
1505
1506/**
1507 * handle_outbound - process filled outbound buffers
1508 * @q: queue containing the buffers
1509 * @callflags: flags
1510 * @bufnr: first buffer to process
1511 * @count: how many buffers are filled
1512 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001513static int handle_outbound(struct qdio_q *q, unsigned int callflags,
Julian Wiedmannb7f143d2019-10-31 13:42:14 +01001514 unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001515{
Julian Wiedmann313dc682019-08-23 11:48:48 +02001516 const unsigned int scan_threshold = q->irq_ptr->scan_threshold;
Jan Glauberc26001d2011-05-23 10:24:38 +02001517 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001518 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001519
Jan Glauber6486cda2010-01-04 09:05:42 +01001520 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001521
1522 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1523 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001524
Jan Glauber01958432011-01-05 12:47:51 +01001525 if (used == QDIO_MAX_BUFFERS_PER_Q)
1526 qperf_inc(q, outbound_queue_full);
1527
Jan Glauber6486cda2010-01-04 09:05:42 +01001528 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001529 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001530 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001531 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001532 q->u.out.pci_out_enabled = 0;
1533
1534 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001535 unsigned long phys_aob = 0;
1536
Julian Wiedmannb7f143d2019-10-31 13:42:14 +01001537 if (q->u.out.use_cq && count == 1)
Julian Wiedmann69e96202019-07-01 14:19:29 +02001538 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001539
Julian Wiedmannb7f143d2019-10-31 13:42:14 +01001540 rc = qdio_kick_outbound_q(q, count, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001541 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001542 rc = qdio_siga_sync_q(q);
Julian Wiedmanna6ec4142019-07-11 18:17:36 +02001543 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1544 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1545 state == SLSB_CU_OUTPUT_PRIMED) {
1546 /* The previous buffer is not processed yet, tack on. */
1547 qperf_inc(q, fast_requeue);
Jan Glauber110da312011-01-05 12:47:53 +01001548 } else {
Julian Wiedmannb7f143d2019-10-31 13:42:14 +01001549 rc = qdio_kick_outbound_q(q, count, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001550 }
1551
Julian Wiedmann313dc682019-08-23 11:48:48 +02001552 /* Let drivers implement their own completion scanning: */
1553 if (!scan_threshold)
1554 return rc;
1555
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001556 /* in case of SIGA errors we must process the error immediately */
Julian Wiedmann313dc682019-08-23 11:48:48 +02001557 if (used >= scan_threshold || rc)
Ursula Braun9bce8b22016-08-05 12:33:10 +02001558 qdio_tasklet_schedule(q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001559 else
1560 /* free the SBALs in case of no further traffic */
Ursula Braun9bce8b22016-08-05 12:33:10 +02001561 if (!timer_pending(&q->u.out.timer) &&
1562 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001563 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001564 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001565}
1566
1567/**
1568 * do_QDIO - process input or output buffers
1569 * @cdev: associated ccw_device for the qdio subchannel
1570 * @callflags: input or output and special flags from the program
1571 * @q_nr: queue number
1572 * @bufnr: buffer number
1573 * @count: how many buffers to process
1574 */
1575int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001576 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001577{
1578 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001579
Jan Glauber66182412009-06-22 12:08:15 +02001580 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001581 return -EINVAL;
1582
Jan Glauber779e6e12008-07-17 17:16:48 +02001583 irq_ptr = cdev->private->qdio_data;
1584 if (!irq_ptr)
1585 return -ENODEV;
1586
Jan Glauber1d7e1502009-09-22 22:58:39 +02001587 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1588 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001589
1590 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001591 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001592 if (!count)
1593 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001594 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001595 return handle_inbound(irq_ptr->input_qs[q_nr],
1596 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001597 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001598 return handle_outbound(irq_ptr->output_qs[q_nr],
1599 callflags, bufnr, count);
1600 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001601}
1602EXPORT_SYMBOL_GPL(do_QDIO);
1603
Jan Glauberd36deae2010-09-07 21:14:39 +00001604/**
1605 * qdio_start_irq - process input buffers
1606 * @cdev: associated ccw_device for the qdio subchannel
Jan Glauberd36deae2010-09-07 21:14:39 +00001607 *
1608 * Return codes
1609 * 0 - success
1610 * 1 - irqs not started since new data is available
1611 */
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001612int qdio_start_irq(struct ccw_device *cdev)
Jan Glauberd36deae2010-09-07 21:14:39 +00001613{
1614 struct qdio_q *q;
1615 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001616 unsigned int i;
Jan Glauberd36deae2010-09-07 21:14:39 +00001617
1618 if (!irq_ptr)
1619 return -ENODEV;
Jan Glauberd36deae2010-09-07 21:14:39 +00001620
Jan Glauber5f4026f2011-10-30 15:17:20 +01001621 clear_nonshared_ind(irq_ptr);
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001622
1623 for_each_input_queue(irq_ptr, q, i)
1624 qdio_stop_polling(q);
1625
1626 clear_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
Jan Glauberd36deae2010-09-07 21:14:39 +00001627
1628 /*
1629 * We need to check again to not lose initiative after
1630 * resetting the ACK state.
1631 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001632 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001633 goto rescan;
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001634
1635 for_each_input_queue(irq_ptr, q, i) {
1636 if (!qdio_inbound_q_done(q, q->first_to_check))
1637 goto rescan;
1638 }
1639
Jan Glauberd36deae2010-09-07 21:14:39 +00001640 return 0;
1641
1642rescan:
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001643 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
Jan Glauberd36deae2010-09-07 21:14:39 +00001644 return 0;
1645 else
1646 return 1;
1647
1648}
1649EXPORT_SYMBOL(qdio_start_irq);
1650
Julian Wiedmann7c47f5a2019-08-23 11:48:47 +02001651static int __qdio_inspect_queue(struct qdio_q *q, unsigned int *bufnr,
1652 unsigned int *error)
1653{
1654 unsigned int start = q->first_to_check;
1655 int count;
1656
1657 count = q->is_input_q ? qdio_inbound_q_moved(q, start) :
1658 qdio_outbound_q_moved(q, start);
1659 if (count == 0)
1660 return 0;
1661
1662 *bufnr = start;
1663 *error = q->qdio_error;
1664
1665 /* for the next time */
1666 q->first_to_check = add_buf(start, count);
1667 q->qdio_error = 0;
1668
1669 return count;
1670}
1671
1672int qdio_inspect_queue(struct ccw_device *cdev, unsigned int nr, bool is_input,
1673 unsigned int *bufnr, unsigned int *error)
1674{
1675 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1676 struct qdio_q *q;
1677
1678 if (!irq_ptr)
1679 return -ENODEV;
1680 q = is_input ? irq_ptr->input_qs[nr] : irq_ptr->output_qs[nr];
1681
1682 if (need_siga_sync(q))
1683 qdio_siga_sync_q(q);
1684
1685 return __qdio_inspect_queue(q, bufnr, error);
1686}
1687EXPORT_SYMBOL_GPL(qdio_inspect_queue);
1688
Jan Glauberd36deae2010-09-07 21:14:39 +00001689/**
1690 * qdio_get_next_buffers - process input buffers
1691 * @cdev: associated ccw_device for the qdio subchannel
1692 * @nr: input queue number
1693 * @bufnr: first filled buffer number
1694 * @error: buffers are in error state
1695 *
1696 * Return codes
1697 * < 0 - error
1698 * = 0 - no new buffers found
1699 * > 0 - number of processed buffers
1700 */
1701int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1702 int *error)
1703{
1704 struct qdio_q *q;
Jan Glauberd36deae2010-09-07 21:14:39 +00001705 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1706
1707 if (!irq_ptr)
1708 return -ENODEV;
1709 q = irq_ptr->input_qs[nr];
Jan Glauberd36deae2010-09-07 21:14:39 +00001710
Jan Glauberd36deae2010-09-07 21:14:39 +00001711 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001712 * Cannot rely on automatic sync after interrupt since queues may
1713 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001714 */
Jan Glauber90adac52011-01-05 12:47:54 +01001715 if (need_siga_sync(q))
1716 qdio_sync_queues(q);
1717
Julian Wiedmann2f2f3832018-10-30 08:21:27 +01001718 qdio_check_outbound_pci_queues(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001719
Jan Glauberd36deae2010-09-07 21:14:39 +00001720 /* Note: upper-layer MUST stop processing immediately here ... */
1721 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1722 return -EIO;
1723
Julian Wiedmann7c47f5a2019-08-23 11:48:47 +02001724 return __qdio_inspect_queue(q, bufnr, error);
Jan Glauberd36deae2010-09-07 21:14:39 +00001725}
1726EXPORT_SYMBOL(qdio_get_next_buffers);
1727
1728/**
1729 * qdio_stop_irq - disable interrupt processing for the device
1730 * @cdev: associated ccw_device for the qdio subchannel
Jan Glauberd36deae2010-09-07 21:14:39 +00001731 *
1732 * Return codes
1733 * 0 - interrupts were already disabled
1734 * 1 - interrupts successfully disabled
1735 */
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001736int qdio_stop_irq(struct ccw_device *cdev)
Jan Glauberd36deae2010-09-07 21:14:39 +00001737{
Jan Glauberd36deae2010-09-07 21:14:39 +00001738 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1739
1740 if (!irq_ptr)
1741 return -ENODEV;
Jan Glauberd36deae2010-09-07 21:14:39 +00001742
Julian Wiedmann0a6e6342020-03-25 10:35:00 +01001743 if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
Jan Glauberd36deae2010-09-07 21:14:39 +00001744 return 0;
1745 else
1746 return 1;
1747}
1748EXPORT_SYMBOL(qdio_stop_irq);
1749
Eugene Crosser1c59a862013-04-24 12:00:23 +02001750/**
1751 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1752 * @schid: Subchannel ID.
1753 * @cnc: Boolean Change-Notification Control
1754 * @response: Response code will be stored at this address
1755 * @cb: Callback function will be executed for each element
1756 * of the address list
Eugene Crosser1c59a862013-04-24 12:00:23 +02001757 * @priv: Pointer to pass to the callback function.
1758 *
1759 * Performs "Store-network-bridging-information list" operation and calls
1760 * the callback function for every entry in the list. If "change-
1761 * notification-control" is set, further changes in the address list
1762 * will be reported via the IPA command.
1763 */
1764int qdio_pnso_brinfo(struct subchannel_id schid,
1765 int cnc, u16 *response,
1766 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1767 void *entry),
1768 void *priv)
1769{
1770 struct chsc_pnso_area *rr;
1771 int rc;
1772 u32 prev_instance = 0;
1773 int isfirstblock = 1;
1774 int i, size, elems;
1775
1776 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1777 if (rr == NULL)
1778 return -ENOMEM;
1779 do {
1780 /* on the first iteration, naihdr.resume_token will be zero */
1781 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1782 if (rc != 0 && rc != -EBUSY)
1783 goto out;
1784 if (rr->response.code != 1) {
1785 rc = -EIO;
1786 continue;
1787 } else
1788 rc = 0;
1789
1790 if (cb == NULL)
1791 continue;
1792
1793 size = rr->naihdr.naids;
1794 elems = (rr->response.length -
1795 sizeof(struct chsc_header) -
1796 sizeof(struct chsc_brinfo_naihdr)) /
1797 size;
1798
1799 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1800 /* Inform the caller that they need to scrap */
1801 /* the data that was already reported via cb */
1802 rc = -EAGAIN;
1803 break;
1804 }
1805 isfirstblock = 0;
1806 prev_instance = rr->naihdr.instance;
1807 for (i = 0; i < elems; i++)
1808 switch (size) {
1809 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1810 (*cb)(priv, l3_ipv6_addr,
1811 &rr->entries.l3_ipv6[i]);
1812 break;
1813 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1814 (*cb)(priv, l3_ipv4_addr,
1815 &rr->entries.l3_ipv4[i]);
1816 break;
1817 case sizeof(struct qdio_brinfo_entry_l2):
1818 (*cb)(priv, l2_addr_lnid,
1819 &rr->entries.l2[i]);
1820 break;
1821 default:
1822 WARN_ON_ONCE(1);
1823 rc = -EIO;
1824 goto out;
1825 }
1826 } while (rr->response.code == 0x0107 || /* channel busy */
1827 (rr->response.code == 1 && /* list stored */
1828 /* resume token is non-zero => list incomplete */
1829 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1830 (*response) = rr->response.code;
1831
1832out:
1833 free_page((unsigned long)rr);
1834 return rc;
1835}
1836EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1837
Jan Glauber779e6e12008-07-17 17:16:48 +02001838static int __init init_QDIO(void)
1839{
1840 int rc;
1841
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001842 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001843 if (rc)
1844 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001845 rc = qdio_setup_init();
1846 if (rc)
1847 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001848 rc = tiqdio_allocate_memory();
1849 if (rc)
1850 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001851 rc = tiqdio_register_thinints();
1852 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001853 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001854 return 0;
1855
Jan Glauber779e6e12008-07-17 17:16:48 +02001856out_ti:
1857 tiqdio_free_memory();
1858out_cache:
1859 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001860out_debug:
1861 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001862 return rc;
1863}
1864
1865static void __exit exit_QDIO(void)
1866{
1867 tiqdio_unregister_thinints();
1868 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001869 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001870 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001871}
1872
1873module_init(init_QDIO);
1874module_exit(exit_QDIO);