blob: 18ab84e9c6b2a2cfe39facbf7fddcf08a98adfa1 [file] [log] [blame]
Jan Glauber779e6e12008-07-17 17:16:48 +02001/*
Jan Glauber779e6e12008-07-17 17:16:48 +02002 * Linux for s390 qdio support, buffer handling, qdio API and module support.
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2000, 2008
Jan Glauber779e6e12008-07-17 17:16:48 +02005 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/timer.h>
13#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000015#include <linux/io.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020017#include <asm/debug.h>
18#include <asm/qdio.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040019#include <asm/ipl.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020020
21#include "cio.h"
22#include "css.h"
23#include "device.h"
24#include "qdio.h"
25#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020026
27MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
28 "Jan Glauber <jang@linux.vnet.ibm.com>");
29MODULE_DESCRIPTION("QDIO base support");
30MODULE_LICENSE("GPL");
31
Jan Glauber958c0ba2011-01-05 12:47:52 +010032static inline int do_siga_sync(unsigned long schid,
33 unsigned int out_mask, unsigned int in_mask,
34 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020035{
Jan Glauber958c0ba2011-01-05 12:47:52 +010036 register unsigned long __fc asm ("0") = fc;
37 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020038 register unsigned long out asm ("2") = out_mask;
39 register unsigned long in asm ("3") = in_mask;
40 int cc;
41
42 asm volatile(
43 " siga 0\n"
44 " ipm %0\n"
45 " srl %0,28\n"
46 : "=d" (cc)
47 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
48 return cc;
49}
50
Jan Glauber958c0ba2011-01-05 12:47:52 +010051static inline int do_siga_input(unsigned long schid, unsigned int mask,
52 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020053{
Jan Glauber958c0ba2011-01-05 12:47:52 +010054 register unsigned long __fc asm ("0") = fc;
55 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020056 register unsigned long __mask asm ("2") = mask;
57 int cc;
58
59 asm volatile(
60 " siga 0\n"
61 " ipm %0\n"
62 " srl %0,28\n"
63 : "=d" (cc)
Jan Glauber1549d132012-05-09 16:27:34 +020064 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc");
Jan Glauber779e6e12008-07-17 17:16:48 +020065 return cc;
66}
67
68/**
69 * do_siga_output - perform SIGA-w/wt function
70 * @schid: subchannel id or in case of QEBSM the subchannel token
71 * @mask: which output queues to process
72 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
73 * @fc: function code to perform
74 *
Jan Glauber1549d132012-05-09 16:27:34 +020075 * Returns condition code.
Jan Glauber779e6e12008-07-17 17:16:48 +020076 * Note: For IQDC unicast queues only the highest priority queue is processed.
77 */
78static inline int do_siga_output(unsigned long schid, unsigned long mask,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000079 unsigned int *bb, unsigned int fc,
80 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +020081{
82 register unsigned long __fc asm("0") = fc;
83 register unsigned long __schid asm("1") = schid;
84 register unsigned long __mask asm("2") = mask;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000085 register unsigned long __aob asm("3") = aob;
Jan Glauber1549d132012-05-09 16:27:34 +020086 int cc;
Jan Glauber779e6e12008-07-17 17:16:48 +020087
88 asm volatile(
89 " siga 0\n"
Jan Glauber1549d132012-05-09 16:27:34 +020090 " ipm %0\n"
Jan Glauber779e6e12008-07-17 17:16:48 +020091 " srl %0,28\n"
Jan Glauber1549d132012-05-09 16:27:34 +020092 : "=d" (cc), "+d" (__fc), "+d" (__aob)
93 : "d" (__schid), "d" (__mask)
94 : "cc");
95 *bb = __fc >> 31;
Jan Glauber779e6e12008-07-17 17:16:48 +020096 return cc;
97}
98
99static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
100{
Jan Glauber779e6e12008-07-17 17:16:48 +0200101 /* all done or next buffer state different */
102 if (ccq == 0 || ccq == 32)
103 return 0;
Jan Glauber25f269f2011-10-30 15:17:06 +0100104 /* no buffer processed */
105 if (ccq == 97)
Jan Glauber779e6e12008-07-17 17:16:48 +0200106 return 1;
Jan Glauber25f269f2011-10-30 15:17:06 +0100107 /* not all buffers processed */
108 if (ccq == 96)
109 return 2;
Jan Glauber779e6e12008-07-17 17:16:48 +0200110 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100111 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200112 return -EIO;
113}
114
115/**
116 * qdio_do_eqbs - extract buffer states for QEBSM
117 * @q: queue to manipulate
118 * @state: state of the extracted buffers
119 * @start: buffer number to start at
120 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100121 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200122 *
Coly Li73ac36e2009-01-07 18:09:16 -0800123 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200124 * Stops processing if a state is different from the last buffers state.
125 */
126static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100127 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200128{
Julian Wiedmanndeda8e02018-03-05 09:39:38 +0100129 int rc, tmp_count = count, tmp_start = start, nr = q->nr;
Jan Glauber779e6e12008-07-17 17:16:48 +0200130 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200131
Jan Glauber6486cda2010-01-04 09:05:42 +0100132 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200133
134 if (!q->is_input_q)
135 nr += q->irq_ptr->nr_input_qs;
136again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100137 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
138 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200139 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100140 if (!rc)
141 return count - tmp_count;
Jan Glauber22f99342008-12-25 13:38:46 +0100142
Jan Glauber779e6e12008-07-17 17:16:48 +0200143 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100144 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200145 goto again;
146 }
147
Jan Glauber25f269f2011-10-30 15:17:06 +0100148 if (rc == 2) {
Jan Glauber25f269f2011-10-30 15:17:06 +0100149 qperf_inc(q, eqbs_partial);
150 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
151 tmp_count);
Julian Wiedmanndeda8e02018-03-05 09:39:38 +0100152 return count - tmp_count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200153 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100154
155 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
156 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200157 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100158 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100159 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200160}
161
162/**
163 * qdio_do_sqbs - set buffer states for QEBSM
164 * @q: queue to manipulate
165 * @state: new state of the buffers
166 * @start: first buffer number to change
167 * @count: how many buffers to change
168 *
169 * Returns the number of successfully changed buffers.
170 * Does retrying until the specified count of buffer states is set or an
171 * error occurs.
172 */
173static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
174 int count)
175{
176 unsigned int ccq = 0;
177 int tmp_count = count, tmp_start = start;
178 int nr = q->nr;
179 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200180
Jan Glauber50f769d2008-12-25 13:38:47 +0100181 if (!count)
182 return 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100183 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200184
185 if (!q->is_input_q)
186 nr += q->irq_ptr->nr_input_qs;
187again:
188 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
189 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100190 if (!rc) {
Jan Glauberce1d8012012-10-24 12:38:35 +0200191 WARN_ON_ONCE(tmp_count);
Jan Glauber25f269f2011-10-30 15:17:06 +0100192 return count - tmp_count;
193 }
194
195 if (rc == 1 || rc == 2) {
Jan Glauber22f99342008-12-25 13:38:46 +0100196 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100197 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200198 goto again;
199 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100200
201 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
202 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200203 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100204 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100205 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200206}
207
Julian Wiedmann5a768872018-03-07 14:01:01 +0100208/*
209 * Returns number of examined buffers and their common state in *state.
210 * Requested number of buffers-to-examine must be > 0.
211 */
Jan Glauber779e6e12008-07-17 17:16:48 +0200212static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100213 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000214 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200215{
216 unsigned char __state = 0;
217 int i;
218
Jan Glauber779e6e12008-07-17 17:16:48 +0200219 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100220 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200221
Julian Wiedmann5a768872018-03-07 14:01:01 +0100222 /* get initial state: */
223 __state = q->slsb.val[bufnr];
224 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
225 __state = SLSB_P_OUTPUT_EMPTY;
226
227 for (i = 1; i < count; i++) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200228 bufnr = next_buf(bufnr);
Julian Wiedmann5a768872018-03-07 14:01:01 +0100229
230 /* merge PENDING into EMPTY: */
231 if (merge_pending &&
232 q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
233 __state == SLSB_P_OUTPUT_EMPTY)
234 continue;
235
236 /* stop if next state differs from initial state: */
237 if (q->slsb.val[bufnr] != __state)
238 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200239 }
240 *state = __state;
241 return i;
242}
243
Jan Glauber60b5df22009-06-22 12:08:10 +0200244static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
245 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200246{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000247 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200248}
249
250/* wrap-around safe setting of slsb states, returns number of changed buffers */
251static inline int set_buf_states(struct qdio_q *q, int bufnr,
252 unsigned char state, int count)
253{
254 int i;
255
Jan Glauber779e6e12008-07-17 17:16:48 +0200256 if (is_qebsm(q))
257 return qdio_do_sqbs(q, state, bufnr, count);
258
259 for (i = 0; i < count; i++) {
260 xchg(&q->slsb.val[bufnr], state);
261 bufnr = next_buf(bufnr);
262 }
263 return count;
264}
265
266static inline int set_buf_state(struct qdio_q *q, int bufnr,
267 unsigned char state)
268{
269 return set_buf_states(q, bufnr, state, 1);
270}
271
272/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100273static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200274{
275 struct qdio_q *q;
276 int i;
277
278 for_each_input_queue(irq_ptr, q, i)
279 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
280 QDIO_MAX_BUFFERS_PER_Q);
281 for_each_output_queue(irq_ptr, q, i)
282 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
283 QDIO_MAX_BUFFERS_PER_Q);
284}
285
Jan Glauber60b5df22009-06-22 12:08:10 +0200286static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200287 unsigned int input)
288{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100289 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
290 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200291 int cc;
292
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100293 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100294 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200295
Jan Glauber958c0ba2011-01-05 12:47:52 +0100296 if (is_qebsm(q)) {
297 schid = q->irq_ptr->sch_token;
298 fc |= QDIO_SIGA_QEBSM_FLAG;
299 }
300
301 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100302 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100303 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200304 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200305}
306
Jan Glauber60b5df22009-06-22 12:08:10 +0200307static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200308{
309 if (q->is_input_q)
310 return qdio_siga_sync(q, 0, q->mask);
311 else
312 return qdio_siga_sync(q, q->mask, 0);
313}
314
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000315static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
316 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200317{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100318 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
319 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100320 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200321 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000322 unsigned long laob = 0;
323
Eugene Crosserec6674c2015-10-06 15:12:29 +0200324 WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
325 !q->u.out.use_cq));
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000326 if (q->u.out.use_cq && aob != 0) {
327 fc = QDIO_SIGA_WRITEQ;
328 laob = aob;
329 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200330
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100331 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200332 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100333 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200334 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200335again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000336 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200337
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100338 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100339 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200340 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100341
342 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200343 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100344 goto again;
345 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200346 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200347 goto again;
348 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200349 if (retries) {
350 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
351 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
352 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
353 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200354 return cc;
355}
356
357static inline int qdio_siga_input(struct qdio_q *q)
358{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100359 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
360 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200361 int cc;
362
Jan Glauber22f99342008-12-25 13:38:46 +0100363 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100364 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200365
Jan Glauber958c0ba2011-01-05 12:47:52 +0100366 if (is_qebsm(q)) {
367 schid = q->irq_ptr->sch_token;
368 fc |= QDIO_SIGA_QEBSM_FLAG;
369 }
370
371 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100372 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100373 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200374 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200375}
376
Jan Glauber90adac52011-01-05 12:47:54 +0100377#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
378#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
379
380static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200381{
Jan Glauber90adac52011-01-05 12:47:54 +0100382 /* PCI capable outbound queues will also be scanned so sync them too */
383 if (pci_out_supported(q))
384 qdio_siga_sync_all(q);
385 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200386 qdio_siga_sync_q(q);
387}
388
Jan Glauber60b5df22009-06-22 12:08:10 +0200389int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
390 unsigned char *state)
391{
Jan Glauber90adac52011-01-05 12:47:54 +0100392 if (need_siga_sync(q))
393 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000394 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200395}
396
397static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200398{
Jan Glauber50f769d2008-12-25 13:38:47 +0100399 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200400 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100401
Jan Glauber779e6e12008-07-17 17:16:48 +0200402 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100403 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200404
405 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100406 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100407 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100408 q->u.in.ack_count);
409 q->u.in.ack_count = 0;
410 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100411 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200412}
413
Fabian Frederick92bdae52014-06-03 21:55:15 +0200414static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100415{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200416 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100417
418 q->q_stats.nr_sbal_total += count;
419 if (count == QDIO_MAX_BUFFERS_MASK) {
420 q->q_stats.nr_sbals[7]++;
421 return;
422 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200423 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100424 q->q_stats.nr_sbals[pos]++;
425}
426
Jan Glauberbffbbd22011-04-20 10:15:33 +0200427static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200428{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200429 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
430 SLSB_P_OUTPUT_NOT_INIT;
431
Jan Glauber1549d132012-05-09 16:27:34 +0200432 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100433
434 /* special handling for no target buffer empty */
435 if ((!q->is_input_q &&
Jan Glauber3ec908782011-06-06 14:14:40 +0200436 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100437 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200438 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100439 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100440 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100441 }
442
Jan Glauber22f99342008-12-25 13:38:46 +0100443 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
444 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100445 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100446 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec908782011-06-06 14:14:40 +0200447 q->sbal[q->first_to_check]->element[14].sflags,
448 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200449
Jan Glauber2768b2d2011-10-30 15:17:07 +0100450set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200451 /*
452 * Interrupts may be avoided as long as the error is present
453 * so change the buffer state immediately to avoid starvation.
454 */
455 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100456}
Jan Glauber779e6e12008-07-17 17:16:48 +0200457
Jan Glauber50f769d2008-12-25 13:38:47 +0100458static inline void inbound_primed(struct qdio_q *q, int count)
459{
460 int new;
461
Jan Glauber1d7e1502009-09-22 22:58:39 +0200462 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100463
464 /* for QEBSM the ACK was already set by EQBS */
465 if (is_qebsm(q)) {
466 if (!q->u.in.polling) {
467 q->u.in.polling = 1;
468 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100469 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100470 return;
471 }
472
473 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100474 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100475 q->u.in.ack_count);
476 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100477 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100478 return;
479 }
480
481 /*
482 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
483 * or by the next inbound run.
484 */
485 new = add_buf(q->first_to_check, count - 1);
486 if (q->u.in.polling) {
487 /* reset the previous ACK but first set the new one */
488 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100489 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100490 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100491 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100492 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100493 }
494
Jan Glaubere85dea02009-03-26 15:24:29 +0100495 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100496 count--;
497 if (!count)
498 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200499 /* need to change ALL buffers to get more interrupts */
500 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200501}
502
503static int get_inbound_buffer_frontier(struct qdio_q *q)
504{
505 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100506 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200507
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200508 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100509
Jan Glauber779e6e12008-07-17 17:16:48 +0200510 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200511 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
512 * would return 0.
513 */
514 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
515 stop = add_buf(q->first_to_check, count);
516
Jan Glauber779e6e12008-07-17 17:16:48 +0200517 if (q->first_to_check == stop)
518 goto out;
519
Jan Glauber36e3e722009-06-22 12:08:12 +0200520 /*
521 * No siga sync here, as a PCI or we after a thin interrupt
522 * already sync'ed the queues.
523 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000524 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200525 if (!count)
526 goto out;
527
528 switch (state) {
529 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100530 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200531 q->first_to_check = add_buf(q->first_to_check, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200532 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100533 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100534 if (q->irq_ptr->perf_stat_enabled)
535 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200536 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200537 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200538 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200539 q->first_to_check = add_buf(q->first_to_check, count);
540 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100541 if (q->irq_ptr->perf_stat_enabled)
542 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200543 break;
544 case SLSB_CU_INPUT_EMPTY:
545 case SLSB_P_INPUT_NOT_INIT:
546 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100547 if (q->irq_ptr->perf_stat_enabled)
548 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100549 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200550 break;
551 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200552 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200553 }
554out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200555 return q->first_to_check;
556}
557
Jan Glauber60b5df22009-06-22 12:08:10 +0200558static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200559{
560 int bufnr;
561
562 bufnr = get_inbound_buffer_frontier(q);
563
Jan Glauber1549d132012-05-09 16:27:34 +0200564 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100565 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100566 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Heiko Carstens1aae0562013-01-30 09:49:40 +0100567 q->u.in.timestamp = get_tod_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200568 return 1;
569 } else
570 return 0;
571}
572
Jan Glauber9a2c1602009-06-22 12:08:11 +0200573static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200574{
575 unsigned char state = 0;
576
577 if (!atomic_read(&q->nr_buf_used))
578 return 1;
579
Jan Glauber90adac52011-01-05 12:47:54 +0100580 if (need_siga_sync(q))
581 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200582 get_buf_state(q, q->first_to_check, &state, 0);
583
Ursula Braun4c522282010-02-09 09:46:07 +0100584 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200585 /* more work coming */
586 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200587
588 if (is_thinint_irq(q->irq_ptr))
589 return 1;
590
591 /* don't poll under z/VM */
592 if (MACHINE_IS_VM)
593 return 1;
594
595 /*
596 * At this point we know, that inbound first_to_check
597 * has (probably) not moved (see qdio_inbound_processing).
598 */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200599 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200600 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200601 q->first_to_check);
602 return 1;
603 } else
604 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200605}
606
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000607static inline int contains_aobs(struct qdio_q *q)
608{
609 return !q->is_input_q && q->u.out.use_cq;
610}
611
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000612static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
613{
614 unsigned char state = 0;
615 int j, b = start;
616
617 if (!contains_aobs(q))
618 return;
619
620 for (j = 0; j < count; ++j) {
621 get_buf_state(q, b, &state, 0);
622 if (state == SLSB_P_OUTPUT_PENDING) {
623 struct qaob *aob = q->u.out.aobs[b];
624 if (aob == NULL)
625 continue;
626
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000627 q->u.out.sbal_state[b].flags |=
628 QDIO_OUTBUF_STATE_FLAG_PENDING;
629 q->u.out.aobs[b] = NULL;
630 } else if (state == SLSB_P_OUTPUT_EMPTY) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000631 q->u.out.sbal_state[b].aob = NULL;
632 }
633 b = next_buf(b);
634 }
635}
636
637static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
638 int bufnr)
639{
640 unsigned long phys_aob = 0;
641
642 if (!q->use_cq)
Julian Wiedmannb9f66a22018-05-16 09:37:25 +0200643 return 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000644
645 if (!q->aobs[bufnr]) {
646 struct qaob *aob = qdio_allocate_aob();
647 q->aobs[bufnr] = aob;
648 }
649 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000650 q->sbal_state[bufnr].aob = q->aobs[bufnr];
651 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
652 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200653 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000654 }
655
Julian Wiedmannb9f66a22018-05-16 09:37:25 +0200656 q->sbal_state[bufnr].flags = 0;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000657 return phys_aob;
658}
659
Jan Glauber60b5df22009-06-22 12:08:10 +0200660static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200661{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100662 int start = q->first_to_kick;
663 int end = q->first_to_check;
664 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200665
666 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
667 return;
668
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100669 count = sub_buf(end, start);
670
671 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100672 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200673 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100674 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100675 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200676 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
677 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100678 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100679
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000680 qdio_handle_aobs(q, start, count);
681
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100682 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
683 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200684
685 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100686 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200687 q->qdio_error = 0;
688}
689
Ursula Braun9bce8b22016-08-05 12:33:10 +0200690static inline int qdio_tasklet_schedule(struct qdio_q *q)
691{
692 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
693 tasklet_schedule(&q->tasklet);
694 return 0;
695 }
696 return -EPERM;
697}
698
Jan Glauber779e6e12008-07-17 17:16:48 +0200699static void __qdio_inbound_processing(struct qdio_q *q)
700{
Jan Glauber6486cda2010-01-04 09:05:42 +0100701 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200702
Jan Glauber779e6e12008-07-17 17:16:48 +0200703 if (!qdio_inbound_q_moved(q))
704 return;
705
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100706 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200707
Jan Glauber6486cda2010-01-04 09:05:42 +0100708 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200709 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100710 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200711 if (!qdio_tasklet_schedule(q))
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200712 return;
Jan Glauber6486cda2010-01-04 09:05:42 +0100713 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200714
715 qdio_stop_polling(q);
716 /*
717 * We need to check again to not lose initiative after
718 * resetting the ACK state.
719 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100720 if (!qdio_inbound_q_done(q)) {
721 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200722 qdio_tasklet_schedule(q);
Jan Glauber6486cda2010-01-04 09:05:42 +0100723 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200724}
725
Jan Glauber779e6e12008-07-17 17:16:48 +0200726void qdio_inbound_processing(unsigned long data)
727{
728 struct qdio_q *q = (struct qdio_q *)data;
729 __qdio_inbound_processing(q);
730}
731
732static int get_outbound_buffer_frontier(struct qdio_q *q)
733{
734 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100735 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200736
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200737 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100738
Jan Glauber90adac52011-01-05 12:47:54 +0100739 if (need_siga_sync(q))
740 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
741 !pci_out_supported(q)) ||
742 (queue_type(q) == QDIO_IQDIO_QFMT &&
743 multicast_outbound(q)))
744 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200745
746 /*
747 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
748 * would return 0.
749 */
750 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
751 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200752 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000753 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200754
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000755 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200756 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000757 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200758
759 switch (state) {
760 case SLSB_P_OUTPUT_EMPTY:
761 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000762 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
763 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200764
765 atomic_sub(count, &q->nr_buf_used);
766 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100767 if (q->irq_ptr->perf_stat_enabled)
768 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000769
Jan Glauber36e3e722009-06-22 12:08:12 +0200770 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200771 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200772 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200773 q->first_to_check = add_buf(q->first_to_check, count);
774 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100775 if (q->irq_ptr->perf_stat_enabled)
776 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200777 break;
778 case SLSB_CU_OUTPUT_PRIMED:
779 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100780 if (q->irq_ptr->perf_stat_enabled)
781 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000782 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
783 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200784 break;
785 case SLSB_P_OUTPUT_NOT_INIT:
786 case SLSB_P_OUTPUT_HALTED:
787 break;
788 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200789 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200790 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000791
792out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200793 return q->first_to_check;
794}
795
796/* all buffers processed? */
797static inline int qdio_outbound_q_done(struct qdio_q *q)
798{
799 return atomic_read(&q->nr_buf_used) == 0;
800}
801
802static inline int qdio_outbound_q_moved(struct qdio_q *q)
803{
804 int bufnr;
805
806 bufnr = get_outbound_buffer_frontier(q);
807
Jan Glauber1549d132012-05-09 16:27:34 +0200808 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100809 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100810 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200811 return 1;
812 } else
813 return 0;
814}
815
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000816static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200817{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200818 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100819 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200820
821 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100822 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200823
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100824 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200825retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100826 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100827
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000828 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100829 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200830 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200831 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100832 case 2:
833 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200834 while (++retries < QDIO_BUSY_BIT_RETRIES) {
835 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
836 goto retry;
837 }
838 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200839 cc = -EBUSY;
840 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100841 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200842 cc = -ENOBUFS;
843 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100844 break;
845 case 1:
846 case 3:
847 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200848 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100849 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200850 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200851 if (retries) {
852 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
853 DBF_ERROR("count:%u", retries);
854 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100855 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200856}
857
Jan Glauber779e6e12008-07-17 17:16:48 +0200858static void __qdio_outbound_processing(struct qdio_q *q)
859{
Jan Glauber6486cda2010-01-04 09:05:42 +0100860 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200861 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200862
863 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100864 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200865
Jan Glauberc38f9602009-03-26 15:24:26 +0100866 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200867 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100868 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200869
Jan Glauber779e6e12008-07-17 17:16:48 +0200870 if (q->u.out.pci_out_enabled)
871 return;
872
873 /*
874 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100875 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
876 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200877 */
878 if (qdio_outbound_q_done(q))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200879 del_timer_sync(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100880 else
Ursula Braun9bce8b22016-08-05 12:33:10 +0200881 if (!timer_pending(&q->u.out.timer) &&
882 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber779e6e12008-07-17 17:16:48 +0200883 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100884 return;
885
886sched:
Ursula Braun9bce8b22016-08-05 12:33:10 +0200887 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200888}
889
890/* outbound tasklet */
891void qdio_outbound_processing(unsigned long data)
892{
893 struct qdio_q *q = (struct qdio_q *)data;
894 __qdio_outbound_processing(q);
895}
896
897void qdio_outbound_timer(unsigned long data)
898{
899 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100900
Ursula Braun9bce8b22016-08-05 12:33:10 +0200901 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200902}
903
Jan Glauber60b5df22009-06-22 12:08:10 +0200904static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200905{
906 struct qdio_q *out;
907 int i;
908
909 if (!pci_out_supported(q))
910 return;
911
912 for_each_output_queue(q->irq_ptr, out, i)
913 if (!qdio_outbound_q_done(out))
Ursula Braun9bce8b22016-08-05 12:33:10 +0200914 qdio_tasklet_schedule(out);
Jan Glauber779e6e12008-07-17 17:16:48 +0200915}
916
Jan Glauber60b5df22009-06-22 12:08:10 +0200917static void __tiqdio_inbound_processing(struct qdio_q *q)
918{
Jan Glauber6486cda2010-01-04 09:05:42 +0100919 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100920 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
921 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200922
923 /*
924 * The interrupt could be caused by a PCI request. Check the
925 * PCI capable outbound queues.
926 */
927 qdio_check_outbound_after_thinint(q);
928
929 if (!qdio_inbound_q_moved(q))
930 return;
931
932 qdio_kick_handler(q);
933
Jan Glauber9a2c1602009-06-22 12:08:11 +0200934 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100935 qperf_inc(q, tasklet_inbound_resched);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200936 if (!qdio_tasklet_schedule(q))
Jan Glaubere2910bc2009-09-11 10:28:19 +0200937 return;
Jan Glauber60b5df22009-06-22 12:08:10 +0200938 }
939
940 qdio_stop_polling(q);
941 /*
942 * We need to check again to not lose initiative after
943 * resetting the ACK state.
944 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200945 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100946 qperf_inc(q, tasklet_inbound_resched2);
Ursula Braun9bce8b22016-08-05 12:33:10 +0200947 qdio_tasklet_schedule(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200948 }
949}
950
951void tiqdio_inbound_processing(unsigned long data)
952{
953 struct qdio_q *q = (struct qdio_q *)data;
954 __tiqdio_inbound_processing(q);
955}
956
Jan Glauber779e6e12008-07-17 17:16:48 +0200957static inline void qdio_set_state(struct qdio_irq *irq_ptr,
958 enum qdio_irq_states state)
959{
Jan Glauber22f99342008-12-25 13:38:46 +0100960 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200961
962 irq_ptr->state = state;
963 mb();
964}
965
Jan Glauber22f99342008-12-25 13:38:46 +0100966static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200967{
Jan Glauber779e6e12008-07-17 17:16:48 +0200968 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100969 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
970 DBF_ERROR_HEX(irb, 64);
971 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200972 }
973}
974
975/* PCI interrupt handler */
976static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
977{
978 int i;
979 struct qdio_q *q;
980
Ursula Braun9bce8b22016-08-05 12:33:10 +0200981 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
Jan Glauberc38f9602009-03-26 15:24:26 +0100982 return;
983
Jan Glauberd36deae2010-09-07 21:14:39 +0000984 for_each_input_queue(irq_ptr, q, i) {
985 if (q->u.in.queue_start_poll) {
986 /* skip if polling is enabled or already in work */
987 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
988 &q->u.in.queue_irq_state)) {
989 qperf_inc(q, int_discarded);
990 continue;
991 }
992 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
993 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000994 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +0000995 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000996 }
Jan Glauberd36deae2010-09-07 21:14:39 +0000997 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200998
Ursula Braun0f308f42014-01-28 13:06:47 +0100999 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
Jan Glauber779e6e12008-07-17 17:16:48 +02001000 return;
1001
1002 for_each_output_queue(irq_ptr, q, i) {
1003 if (qdio_outbound_q_done(q))
1004 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001005 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001006 qdio_siga_sync_q(q);
Ursula Braun9bce8b22016-08-05 12:33:10 +02001007 qdio_tasklet_schedule(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001008 }
1009}
1010
1011static void qdio_handle_activate_check(struct ccw_device *cdev,
1012 unsigned long intparm, int cstat, int dstat)
1013{
1014 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1015 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001016 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001017
Jan Glauber22f99342008-12-25 13:38:46 +01001018 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1019 DBF_ERROR("intp :%lx", intparm);
1020 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001021
1022 if (irq_ptr->nr_input_qs) {
1023 q = irq_ptr->input_qs[0];
1024 } else if (irq_ptr->nr_output_qs) {
1025 q = irq_ptr->output_qs[0];
1026 } else {
1027 dump_stack();
1028 goto no_handler;
1029 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001030
1031 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001032 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001033 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001034no_handler:
1035 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001036 /*
1037 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1038 * Therefore we call the LGR detection function here.
1039 */
1040 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001041}
1042
Jan Glauber779e6e12008-07-17 17:16:48 +02001043static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1044 int dstat)
1045{
1046 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001047
Jan Glauber22f99342008-12-25 13:38:46 +01001048 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001049
1050 if (cstat)
1051 goto error;
1052 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1053 goto error;
1054 if (!(dstat & DEV_STAT_DEV_END))
1055 goto error;
1056 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1057 return;
1058
1059error:
1060 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1061 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1062 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001063}
1064
1065/* qdio interrupt handler */
1066void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1067 struct irb *irb)
1068{
1069 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001070 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001071 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001072
Jan Glauber779e6e12008-07-17 17:16:48 +02001073 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001074 ccw_device_get_schid(cdev, &schid);
1075 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001076 return;
1077 }
1078
Jan Glauber09a308f2010-05-17 10:00:14 +02001079 if (irq_ptr->perf_stat_enabled)
1080 irq_ptr->perf_stat.qdio_int++;
1081
Jan Glauber779e6e12008-07-17 17:16:48 +02001082 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001083 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1084 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1085 wake_up(&cdev->private->wait_q);
1086 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001087 }
Jan Glauber22f99342008-12-25 13:38:46 +01001088 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001089 cstat = irb->scsw.cmd.cstat;
1090 dstat = irb->scsw.cmd.dstat;
1091
1092 switch (irq_ptr->state) {
1093 case QDIO_IRQ_STATE_INACTIVE:
1094 qdio_establish_handle_irq(cdev, cstat, dstat);
1095 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001096 case QDIO_IRQ_STATE_CLEANUP:
1097 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1098 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001099 case QDIO_IRQ_STATE_ESTABLISHED:
1100 case QDIO_IRQ_STATE_ACTIVE:
1101 if (cstat & SCHN_STAT_PCI) {
1102 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001103 return;
1104 }
Jan Glauber4c575422009-06-12 10:26:28 +02001105 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001106 qdio_handle_activate_check(cdev, intparm, cstat,
1107 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001108 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001109 case QDIO_IRQ_STATE_STOPPED:
1110 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001111 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001112 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001113 }
1114 wake_up(&cdev->private->wait_q);
1115}
1116
1117/**
1118 * qdio_get_ssqd_desc - get qdio subchannel description
1119 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001120 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001121 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001122 * Returns 0 or an error code. The results of the chsc are stored in the
1123 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001124 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001125int qdio_get_ssqd_desc(struct ccw_device *cdev,
1126 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001127{
Sebastian Ott9080c922016-07-28 20:30:31 +02001128 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001129
Jan Glauberbbd50e12008-12-25 13:38:43 +01001130 if (!cdev || !cdev->private)
1131 return -EINVAL;
1132
Sebastian Ott9080c922016-07-28 20:30:31 +02001133 ccw_device_get_schid(cdev, &schid);
1134 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1135 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001136}
1137EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1138
Jan Glauber779e6e12008-07-17 17:16:48 +02001139static void qdio_shutdown_queues(struct ccw_device *cdev)
1140{
1141 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1142 struct qdio_q *q;
1143 int i;
1144
1145 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001146 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001147
1148 for_each_output_queue(irq_ptr, q, i) {
Ursula Braun9bce8b22016-08-05 12:33:10 +02001149 del_timer_sync(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001150 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001151 }
1152}
1153
1154/**
1155 * qdio_shutdown - shut down a qdio subchannel
1156 * @cdev: associated ccw device
1157 * @how: use halt or clear to shutdown
1158 */
1159int qdio_shutdown(struct ccw_device *cdev, int how)
1160{
Jan Glauber22f99342008-12-25 13:38:46 +01001161 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001162 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001163 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001164
Jan Glauber779e6e12008-07-17 17:16:48 +02001165 if (!irq_ptr)
1166 return -ENODEV;
1167
Jan Glauberce1d8012012-10-24 12:38:35 +02001168 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001169 ccw_device_get_schid(cdev, &schid);
1170 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001171
Jan Glauber779e6e12008-07-17 17:16:48 +02001172 mutex_lock(&irq_ptr->setup_mutex);
1173 /*
1174 * Subchannel was already shot down. We cannot prevent being called
1175 * twice since cio may trigger a shutdown asynchronously.
1176 */
1177 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1178 mutex_unlock(&irq_ptr->setup_mutex);
1179 return 0;
1180 }
1181
Jan Glauberc38f9602009-03-26 15:24:26 +01001182 /*
1183 * Indicate that the device is going down. Scheduling the queue
1184 * tasklets is forbidden from here on.
1185 */
1186 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1187
Jan Glauber779e6e12008-07-17 17:16:48 +02001188 tiqdio_remove_input_queues(irq_ptr);
1189 qdio_shutdown_queues(cdev);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001190 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001191
1192 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001193 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001194
1195 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1196 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1197 else
1198 /* default behaviour is halt */
1199 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1200 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001201 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1202 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001203 goto no_cleanup;
1204 }
1205
1206 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001207 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001208 wait_event_interruptible_timeout(cdev->private->wait_q,
1209 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1210 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1211 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001212 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001213
1214no_cleanup:
1215 qdio_shutdown_thinint(irq_ptr);
1216
1217 /* restore interrupt handler */
1218 if ((void *)cdev->handler == (void *)qdio_int_handler)
1219 cdev->handler = irq_ptr->orig_handler;
Sebastian Otta48ed862016-07-29 13:41:20 +02001220 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001221
1222 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1223 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001224 if (rc)
1225 return rc;
1226 return 0;
1227}
1228EXPORT_SYMBOL_GPL(qdio_shutdown);
1229
1230/**
1231 * qdio_free - free data structures for a qdio subchannel
1232 * @cdev: associated ccw device
1233 */
1234int qdio_free(struct ccw_device *cdev)
1235{
Jan Glauber22f99342008-12-25 13:38:46 +01001236 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001237 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001238
Jan Glauber779e6e12008-07-17 17:16:48 +02001239 if (!irq_ptr)
1240 return -ENODEV;
1241
Sebastian Ott9080c922016-07-28 20:30:31 +02001242 ccw_device_get_schid(cdev, &schid);
1243 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001244 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001245 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001246
Stefan Raspl613c4e02014-06-12 14:24:45 +02001247 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001248 cdev->private->qdio_data = NULL;
1249 mutex_unlock(&irq_ptr->setup_mutex);
1250
1251 qdio_release_memory(irq_ptr);
1252 return 0;
1253}
1254EXPORT_SYMBOL_GPL(qdio_free);
1255
1256/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001257 * qdio_allocate - allocate qdio queues and associated data
1258 * @init_data: initialization data
1259 */
1260int qdio_allocate(struct qdio_initialize *init_data)
1261{
Sebastian Ott9080c922016-07-28 20:30:31 +02001262 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001263 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001264
Sebastian Ott9080c922016-07-28 20:30:31 +02001265 ccw_device_get_schid(init_data->cdev, &schid);
1266 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001267
1268 if ((init_data->no_input_qs && !init_data->input_handler) ||
1269 (init_data->no_output_qs && !init_data->output_handler))
1270 return -EINVAL;
1271
1272 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1273 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1274 return -EINVAL;
1275
1276 if ((!init_data->input_sbal_addr_array) ||
1277 (!init_data->output_sbal_addr_array))
1278 return -EINVAL;
1279
Jan Glauber779e6e12008-07-17 17:16:48 +02001280 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1281 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1282 if (!irq_ptr)
1283 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001284
1285 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001286 if (qdio_allocate_dbf(init_data, irq_ptr))
1287 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001288
1289 /*
1290 * Allocate a page for the chsc calls in qdio_establish.
1291 * Must be pre-allocated since a zfcp recovery will call
1292 * qdio_establish. In case of low memory and swap on a zfcp disk
1293 * we may not be able to allocate memory otherwise.
1294 */
1295 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1296 if (!irq_ptr->chsc_page)
1297 goto out_rel;
1298
1299 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001300 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001301 if (!irq_ptr->qdr)
1302 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001303
Jan Glauber779e6e12008-07-17 17:16:48 +02001304 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1305 init_data->no_output_qs))
1306 goto out_rel;
1307
1308 init_data->cdev->private->qdio_data = irq_ptr;
1309 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1310 return 0;
1311out_rel:
1312 qdio_release_memory(irq_ptr);
1313out_err:
1314 return -ENOMEM;
1315}
1316EXPORT_SYMBOL_GPL(qdio_allocate);
1317
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001318static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1319{
1320 struct qdio_q *q = irq_ptr->input_qs[0];
1321 int i, use_cq = 0;
1322
1323 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1324 use_cq = 1;
1325
1326 for_each_output_queue(irq_ptr, q, i) {
1327 if (use_cq) {
1328 if (qdio_enable_async_operation(&q->u.out) < 0) {
1329 use_cq = 0;
1330 continue;
1331 }
1332 } else
1333 qdio_disable_async_operation(&q->u.out);
1334 }
1335 DBF_EVENT("use_cq:%d", use_cq);
1336}
1337
Jan Glauber779e6e12008-07-17 17:16:48 +02001338/**
1339 * qdio_establish - establish queues on a qdio subchannel
1340 * @init_data: initialization data
1341 */
1342int qdio_establish(struct qdio_initialize *init_data)
1343{
Jan Glauber779e6e12008-07-17 17:16:48 +02001344 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001345 struct subchannel_id schid;
1346 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001347 int rc;
1348
Sebastian Ott9080c922016-07-28 20:30:31 +02001349 ccw_device_get_schid(cdev, &schid);
1350 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001351
Jan Glauber779e6e12008-07-17 17:16:48 +02001352 irq_ptr = cdev->private->qdio_data;
1353 if (!irq_ptr)
1354 return -ENODEV;
1355
Jan Glauber779e6e12008-07-17 17:16:48 +02001356 mutex_lock(&irq_ptr->setup_mutex);
1357 qdio_setup_irq(init_data);
1358
1359 rc = qdio_establish_thinint(irq_ptr);
1360 if (rc) {
1361 mutex_unlock(&irq_ptr->setup_mutex);
1362 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1363 return rc;
1364 }
1365
1366 /* establish q */
1367 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1368 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1369 irq_ptr->ccw.count = irq_ptr->equeue.count;
1370 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1371
Sebastian Otta48ed862016-07-29 13:41:20 +02001372 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001373 ccw_device_set_options_mask(cdev, 0);
1374
1375 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001376 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001377 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001378 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1379 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001380 mutex_unlock(&irq_ptr->setup_mutex);
1381 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1382 return rc;
1383 }
1384
1385 wait_event_interruptible_timeout(cdev->private->wait_q,
1386 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1387 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1388
1389 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1390 mutex_unlock(&irq_ptr->setup_mutex);
1391 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1392 return -EIO;
1393 }
1394
1395 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001396
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001397 qdio_detect_hsicq(irq_ptr);
1398
Jan Glauber779e6e12008-07-17 17:16:48 +02001399 /* qebsm is now setup if available, initialize buffer states */
1400 qdio_init_buf_states(irq_ptr);
1401
1402 mutex_unlock(&irq_ptr->setup_mutex);
1403 qdio_print_subchannel_info(irq_ptr, cdev);
1404 qdio_setup_debug_entries(irq_ptr, cdev);
1405 return 0;
1406}
1407EXPORT_SYMBOL_GPL(qdio_establish);
1408
1409/**
1410 * qdio_activate - activate queues on a qdio subchannel
1411 * @cdev: associated cdev
1412 */
1413int qdio_activate(struct ccw_device *cdev)
1414{
Sebastian Ott9080c922016-07-28 20:30:31 +02001415 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001416 struct qdio_irq *irq_ptr;
1417 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001418
Sebastian Ott9080c922016-07-28 20:30:31 +02001419 ccw_device_get_schid(cdev, &schid);
1420 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001421
Jan Glauber779e6e12008-07-17 17:16:48 +02001422 irq_ptr = cdev->private->qdio_data;
1423 if (!irq_ptr)
1424 return -ENODEV;
1425
Jan Glauber779e6e12008-07-17 17:16:48 +02001426 mutex_lock(&irq_ptr->setup_mutex);
1427 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1428 rc = -EBUSY;
1429 goto out;
1430 }
1431
Jan Glauber779e6e12008-07-17 17:16:48 +02001432 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1433 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1434 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1435 irq_ptr->ccw.cda = 0;
1436
Sebastian Otta48ed862016-07-29 13:41:20 +02001437 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001438 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1439
1440 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1441 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001442 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001443 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001444 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1445 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001446 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001447 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001448
1449 if (is_thinint_irq(irq_ptr))
1450 tiqdio_add_input_queues(irq_ptr);
1451
1452 /* wait for subchannel to become active */
1453 msleep(5);
1454
1455 switch (irq_ptr->state) {
1456 case QDIO_IRQ_STATE_STOPPED:
1457 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001458 rc = -EIO;
1459 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001460 default:
1461 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1462 rc = 0;
1463 }
1464out:
1465 mutex_unlock(&irq_ptr->setup_mutex);
1466 return rc;
1467}
1468EXPORT_SYMBOL_GPL(qdio_activate);
1469
1470static inline int buf_in_between(int bufnr, int start, int count)
1471{
1472 int end = add_buf(start, count);
1473
1474 if (end > start) {
1475 if (bufnr >= start && bufnr < end)
1476 return 1;
1477 else
1478 return 0;
1479 }
1480
1481 /* wrap-around case */
1482 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1483 (bufnr < end))
1484 return 1;
1485 else
1486 return 0;
1487}
1488
1489/**
1490 * handle_inbound - reset processed input buffers
1491 * @q: queue containing the buffers
1492 * @callflags: flags
1493 * @bufnr: first buffer to process
1494 * @count: how many buffers are emptied
1495 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001496static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1497 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001498{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001499 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001500
Jan Glauber6486cda2010-01-04 09:05:42 +01001501 qperf_inc(q, inbound_call);
1502
Jan Glauber50f769d2008-12-25 13:38:47 +01001503 if (!q->u.in.polling)
1504 goto set;
1505
1506 /* protect against stop polling setting an ACK for an emptied slsb */
1507 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1508 /* overwriting everything, just delete polling status */
1509 q->u.in.polling = 0;
1510 q->u.in.ack_count = 0;
1511 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001512 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001513 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001514 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001515 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001516 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001517 q->u.in.ack_count -= diff;
1518 if (q->u.in.ack_count <= 0) {
1519 q->u.in.polling = 0;
1520 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001521 goto set;
1522 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001523 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001524 }
1525 else
1526 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001527 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001528 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001529
Jan Glauber50f769d2008-12-25 13:38:47 +01001530set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001531 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001532 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001533
Jan Glauberd303b6f2009-03-26 15:24:31 +01001534 if (need_siga_in(q))
1535 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001536
Jan Glauberd303b6f2009-03-26 15:24:31 +01001537 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001538}
1539
1540/**
1541 * handle_outbound - process filled outbound buffers
1542 * @q: queue containing the buffers
1543 * @callflags: flags
1544 * @bufnr: first buffer to process
1545 * @count: how many buffers are filled
1546 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001547static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1548 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001549{
Jan Glauberc26001d2011-05-23 10:24:38 +02001550 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001551 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001552
Jan Glauber6486cda2010-01-04 09:05:42 +01001553 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001554
1555 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1556 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001557
Jan Glauber01958432011-01-05 12:47:51 +01001558 if (used == QDIO_MAX_BUFFERS_PER_Q)
1559 qperf_inc(q, outbound_queue_full);
1560
Jan Glauber6486cda2010-01-04 09:05:42 +01001561 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001562 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001563 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001564 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001565 q->u.out.pci_out_enabled = 0;
1566
1567 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001568 unsigned long phys_aob = 0;
1569
1570 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001571 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1572
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001573 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1574
1575 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001576 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001577 rc = qdio_siga_sync_q(q);
1578 } else {
1579 /* try to fast requeue buffers */
1580 get_buf_state(q, prev_buf(bufnr), &state, 0);
1581 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001582 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001583 else
Jan Glauber110da312011-01-05 12:47:53 +01001584 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001585 }
1586
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001587 /* in case of SIGA errors we must process the error immediately */
1588 if (used >= q->u.out.scan_threshold || rc)
Ursula Braun9bce8b22016-08-05 12:33:10 +02001589 qdio_tasklet_schedule(q);
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001590 else
1591 /* free the SBALs in case of no further traffic */
Ursula Braun9bce8b22016-08-05 12:33:10 +02001592 if (!timer_pending(&q->u.out.timer) &&
1593 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001594 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001595 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001596}
1597
1598/**
1599 * do_QDIO - process input or output buffers
1600 * @cdev: associated ccw_device for the qdio subchannel
1601 * @callflags: input or output and special flags from the program
1602 * @q_nr: queue number
1603 * @bufnr: buffer number
1604 * @count: how many buffers to process
1605 */
1606int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001607 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001608{
1609 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001610
Jan Glauber66182412009-06-22 12:08:15 +02001611 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001612 return -EINVAL;
1613
Jan Glauber779e6e12008-07-17 17:16:48 +02001614 irq_ptr = cdev->private->qdio_data;
1615 if (!irq_ptr)
1616 return -ENODEV;
1617
Jan Glauber1d7e1502009-09-22 22:58:39 +02001618 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1619 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001620
1621 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001622 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001623 if (!count)
1624 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001625 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001626 return handle_inbound(irq_ptr->input_qs[q_nr],
1627 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001628 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001629 return handle_outbound(irq_ptr->output_qs[q_nr],
1630 callflags, bufnr, count);
1631 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001632}
1633EXPORT_SYMBOL_GPL(do_QDIO);
1634
Jan Glauberd36deae2010-09-07 21:14:39 +00001635/**
1636 * qdio_start_irq - process input buffers
1637 * @cdev: associated ccw_device for the qdio subchannel
1638 * @nr: input queue number
1639 *
1640 * Return codes
1641 * 0 - success
1642 * 1 - irqs not started since new data is available
1643 */
1644int qdio_start_irq(struct ccw_device *cdev, int nr)
1645{
1646 struct qdio_q *q;
1647 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1648
1649 if (!irq_ptr)
1650 return -ENODEV;
1651 q = irq_ptr->input_qs[nr];
1652
Jan Glauber5f4026f2011-10-30 15:17:20 +01001653 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001654 qdio_stop_polling(q);
1655 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1656
1657 /*
1658 * We need to check again to not lose initiative after
1659 * resetting the ACK state.
1660 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001661 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001662 goto rescan;
1663 if (!qdio_inbound_q_done(q))
1664 goto rescan;
1665 return 0;
1666
1667rescan:
1668 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1669 &q->u.in.queue_irq_state))
1670 return 0;
1671 else
1672 return 1;
1673
1674}
1675EXPORT_SYMBOL(qdio_start_irq);
1676
1677/**
1678 * qdio_get_next_buffers - process input buffers
1679 * @cdev: associated ccw_device for the qdio subchannel
1680 * @nr: input queue number
1681 * @bufnr: first filled buffer number
1682 * @error: buffers are in error state
1683 *
1684 * Return codes
1685 * < 0 - error
1686 * = 0 - no new buffers found
1687 * > 0 - number of processed buffers
1688 */
1689int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1690 int *error)
1691{
1692 struct qdio_q *q;
1693 int start, end;
1694 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1695
1696 if (!irq_ptr)
1697 return -ENODEV;
1698 q = irq_ptr->input_qs[nr];
Jan Glauberd36deae2010-09-07 21:14:39 +00001699
Jan Glauberd36deae2010-09-07 21:14:39 +00001700 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001701 * Cannot rely on automatic sync after interrupt since queues may
1702 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001703 */
Jan Glauber90adac52011-01-05 12:47:54 +01001704 if (need_siga_sync(q))
1705 qdio_sync_queues(q);
1706
1707 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001708 qdio_check_outbound_after_thinint(q);
1709
1710 if (!qdio_inbound_q_moved(q))
1711 return 0;
1712
1713 /* Note: upper-layer MUST stop processing immediately here ... */
1714 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1715 return -EIO;
1716
1717 start = q->first_to_kick;
1718 end = q->first_to_check;
1719 *bufnr = start;
1720 *error = q->qdio_error;
1721
1722 /* for the next time */
1723 q->first_to_kick = end;
1724 q->qdio_error = 0;
1725 return sub_buf(end, start);
1726}
1727EXPORT_SYMBOL(qdio_get_next_buffers);
1728
1729/**
1730 * qdio_stop_irq - disable interrupt processing for the device
1731 * @cdev: associated ccw_device for the qdio subchannel
1732 * @nr: input queue number
1733 *
1734 * Return codes
1735 * 0 - interrupts were already disabled
1736 * 1 - interrupts successfully disabled
1737 */
1738int qdio_stop_irq(struct ccw_device *cdev, int nr)
1739{
1740 struct qdio_q *q;
1741 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1742
1743 if (!irq_ptr)
1744 return -ENODEV;
1745 q = irq_ptr->input_qs[nr];
1746
1747 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1748 &q->u.in.queue_irq_state))
1749 return 0;
1750 else
1751 return 1;
1752}
1753EXPORT_SYMBOL(qdio_stop_irq);
1754
Eugene Crosser1c59a862013-04-24 12:00:23 +02001755/**
1756 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1757 * @schid: Subchannel ID.
1758 * @cnc: Boolean Change-Notification Control
1759 * @response: Response code will be stored at this address
1760 * @cb: Callback function will be executed for each element
1761 * of the address list
1762 * @priv: Pointer passed from the caller to qdio_pnso_brinfo()
1763 * @type: Type of the address entry passed to the callback
1764 * @entry: Entry containg the address of the specified type
1765 * @priv: Pointer to pass to the callback function.
1766 *
1767 * Performs "Store-network-bridging-information list" operation and calls
1768 * the callback function for every entry in the list. If "change-
1769 * notification-control" is set, further changes in the address list
1770 * will be reported via the IPA command.
1771 */
1772int qdio_pnso_brinfo(struct subchannel_id schid,
1773 int cnc, u16 *response,
1774 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1775 void *entry),
1776 void *priv)
1777{
1778 struct chsc_pnso_area *rr;
1779 int rc;
1780 u32 prev_instance = 0;
1781 int isfirstblock = 1;
1782 int i, size, elems;
1783
1784 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1785 if (rr == NULL)
1786 return -ENOMEM;
1787 do {
1788 /* on the first iteration, naihdr.resume_token will be zero */
1789 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1790 if (rc != 0 && rc != -EBUSY)
1791 goto out;
1792 if (rr->response.code != 1) {
1793 rc = -EIO;
1794 continue;
1795 } else
1796 rc = 0;
1797
1798 if (cb == NULL)
1799 continue;
1800
1801 size = rr->naihdr.naids;
1802 elems = (rr->response.length -
1803 sizeof(struct chsc_header) -
1804 sizeof(struct chsc_brinfo_naihdr)) /
1805 size;
1806
1807 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1808 /* Inform the caller that they need to scrap */
1809 /* the data that was already reported via cb */
1810 rc = -EAGAIN;
1811 break;
1812 }
1813 isfirstblock = 0;
1814 prev_instance = rr->naihdr.instance;
1815 for (i = 0; i < elems; i++)
1816 switch (size) {
1817 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1818 (*cb)(priv, l3_ipv6_addr,
1819 &rr->entries.l3_ipv6[i]);
1820 break;
1821 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1822 (*cb)(priv, l3_ipv4_addr,
1823 &rr->entries.l3_ipv4[i]);
1824 break;
1825 case sizeof(struct qdio_brinfo_entry_l2):
1826 (*cb)(priv, l2_addr_lnid,
1827 &rr->entries.l2[i]);
1828 break;
1829 default:
1830 WARN_ON_ONCE(1);
1831 rc = -EIO;
1832 goto out;
1833 }
1834 } while (rr->response.code == 0x0107 || /* channel busy */
1835 (rr->response.code == 1 && /* list stored */
1836 /* resume token is non-zero => list incomplete */
1837 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1838 (*response) = rr->response.code;
1839
1840out:
1841 free_page((unsigned long)rr);
1842 return rc;
1843}
1844EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1845
Jan Glauber779e6e12008-07-17 17:16:48 +02001846static int __init init_QDIO(void)
1847{
1848 int rc;
1849
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001850 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001851 if (rc)
1852 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001853 rc = qdio_setup_init();
1854 if (rc)
1855 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001856 rc = tiqdio_allocate_memory();
1857 if (rc)
1858 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001859 rc = tiqdio_register_thinints();
1860 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001861 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001862 return 0;
1863
Jan Glauber779e6e12008-07-17 17:16:48 +02001864out_ti:
1865 tiqdio_free_memory();
1866out_cache:
1867 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001868out_debug:
1869 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001870 return rc;
1871}
1872
1873static void __exit exit_QDIO(void)
1874{
1875 tiqdio_unregister_thinints();
1876 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001877 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001878 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001879}
1880
1881module_init(init_QDIO);
1882module_exit(exit_QDIO);