blob: e06fa03ea1e456bac69ba5f84e634ebbd9f645ce [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{
Jan Glauber25f269f2011-10-30 15:17:06 +0100129 int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200130 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200131
132 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100133 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200134
135 if (!q->is_input_q)
136 nr += q->irq_ptr->nr_input_qs;
137again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100138 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
139 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200140 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100141 if (!rc)
142 return count - tmp_count;
Jan Glauber22f99342008-12-25 13:38:46 +0100143
Jan Glauber779e6e12008-07-17 17:16:48 +0200144 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100145 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200146 goto again;
147 }
148
Jan Glauber25f269f2011-10-30 15:17:06 +0100149 if (rc == 2) {
150 BUG_ON(tmp_count == count);
151 qperf_inc(q, eqbs_partial);
152 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
153 tmp_count);
154 /*
155 * Retry once, if that fails bail out and process the
156 * extracted buffers before trying again.
157 */
158 if (!retried++)
159 goto again;
160 else
161 return count - tmp_count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200162 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100163
164 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
165 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200166 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100167 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100168 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200169}
170
171/**
172 * qdio_do_sqbs - set buffer states for QEBSM
173 * @q: queue to manipulate
174 * @state: new state of the buffers
175 * @start: first buffer number to change
176 * @count: how many buffers to change
177 *
178 * Returns the number of successfully changed buffers.
179 * Does retrying until the specified count of buffer states is set or an
180 * error occurs.
181 */
182static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
183 int count)
184{
185 unsigned int ccq = 0;
186 int tmp_count = count, tmp_start = start;
187 int nr = q->nr;
188 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200189
Jan Glauber50f769d2008-12-25 13:38:47 +0100190 if (!count)
191 return 0;
192
Jan Glauber779e6e12008-07-17 17:16:48 +0200193 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100194 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200195
196 if (!q->is_input_q)
197 nr += q->irq_ptr->nr_input_qs;
198again:
199 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
200 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100201 if (!rc) {
202 WARN_ON(tmp_count);
203 return count - tmp_count;
204 }
205
206 if (rc == 1 || rc == 2) {
Jan Glauber22f99342008-12-25 13:38:46 +0100207 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100208 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200209 goto again;
210 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100211
212 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
213 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200214 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100215 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100216 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200217}
218
219/* returns number of examined buffers and their common state in *state */
220static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100221 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000222 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200223{
224 unsigned char __state = 0;
225 int i;
226
227 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
228 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
229
230 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100231 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200232
233 for (i = 0; i < count; i++) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000234 if (!__state) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200235 __state = q->slsb.val[bufnr];
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000236 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
237 __state = SLSB_P_OUTPUT_EMPTY;
238 } else if (merge_pending) {
239 if ((q->slsb.val[bufnr] & __state) != __state)
240 break;
241 } else if (q->slsb.val[bufnr] != __state)
Jan Glauber779e6e12008-07-17 17:16:48 +0200242 break;
243 bufnr = next_buf(bufnr);
244 }
245 *state = __state;
246 return i;
247}
248
Jan Glauber60b5df22009-06-22 12:08:10 +0200249static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
250 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200251{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000252 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200253}
254
255/* wrap-around safe setting of slsb states, returns number of changed buffers */
256static inline int set_buf_states(struct qdio_q *q, int bufnr,
257 unsigned char state, int count)
258{
259 int i;
260
261 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
262 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
263
264 if (is_qebsm(q))
265 return qdio_do_sqbs(q, state, bufnr, count);
266
267 for (i = 0; i < count; i++) {
268 xchg(&q->slsb.val[bufnr], state);
269 bufnr = next_buf(bufnr);
270 }
271 return count;
272}
273
274static inline int set_buf_state(struct qdio_q *q, int bufnr,
275 unsigned char state)
276{
277 return set_buf_states(q, bufnr, state, 1);
278}
279
280/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100281static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200282{
283 struct qdio_q *q;
284 int i;
285
286 for_each_input_queue(irq_ptr, q, i)
287 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
288 QDIO_MAX_BUFFERS_PER_Q);
289 for_each_output_queue(irq_ptr, q, i)
290 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
291 QDIO_MAX_BUFFERS_PER_Q);
292}
293
Jan Glauber60b5df22009-06-22 12:08:10 +0200294static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200295 unsigned int input)
296{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100297 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
298 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200299 int cc;
300
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100301 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100302 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200303
Jan Glauber958c0ba2011-01-05 12:47:52 +0100304 if (is_qebsm(q)) {
305 schid = q->irq_ptr->sch_token;
306 fc |= QDIO_SIGA_QEBSM_FLAG;
307 }
308
309 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100310 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100311 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200312 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200313}
314
Jan Glauber60b5df22009-06-22 12:08:10 +0200315static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200316{
317 if (q->is_input_q)
318 return qdio_siga_sync(q, 0, q->mask);
319 else
320 return qdio_siga_sync(q, q->mask, 0);
321}
322
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000323static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
324 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200325{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100326 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
327 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100328 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200329 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000330 unsigned long laob = 0;
331
332 if (q->u.out.use_cq && aob != 0) {
333 fc = QDIO_SIGA_WRITEQ;
334 laob = aob;
335 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200336
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100337 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200338 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100339 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200340 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200341again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000342 WARN_ON_ONCE((aob && queue_type(q) != QDIO_IQDIO_QFMT) ||
343 (aob && fc != QDIO_SIGA_WRITEQ));
344 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200345
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100346 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100347 if (unlikely(*busy_bit)) {
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100348 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200349 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100350
351 if (!start_time) {
Jan Glauber3a601bf2010-05-17 10:00:17 +0200352 start_time = get_clock();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100353 goto again;
354 }
Jan Glauber3a601bf2010-05-17 10:00:17 +0200355 if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200356 goto again;
357 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200358 if (retries) {
359 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
360 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
361 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
362 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200363 return cc;
364}
365
366static inline int qdio_siga_input(struct qdio_q *q)
367{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100368 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
369 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200370 int cc;
371
Jan Glauber22f99342008-12-25 13:38:46 +0100372 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100373 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200374
Jan Glauber958c0ba2011-01-05 12:47:52 +0100375 if (is_qebsm(q)) {
376 schid = q->irq_ptr->sch_token;
377 fc |= QDIO_SIGA_QEBSM_FLAG;
378 }
379
380 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100381 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100382 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200383 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200384}
385
Jan Glauber90adac52011-01-05 12:47:54 +0100386#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
387#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
388
389static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200390{
Jan Glauber90adac52011-01-05 12:47:54 +0100391 /* PCI capable outbound queues will also be scanned so sync them too */
392 if (pci_out_supported(q))
393 qdio_siga_sync_all(q);
394 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200395 qdio_siga_sync_q(q);
396}
397
Jan Glauber60b5df22009-06-22 12:08:10 +0200398int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
399 unsigned char *state)
400{
Jan Glauber90adac52011-01-05 12:47:54 +0100401 if (need_siga_sync(q))
402 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000403 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200404}
405
406static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200407{
Jan Glauber50f769d2008-12-25 13:38:47 +0100408 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200409 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100410
Jan Glauber779e6e12008-07-17 17:16:48 +0200411 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100412 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200413
414 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100415 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100416 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100417 q->u.in.ack_count);
418 q->u.in.ack_count = 0;
419 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100420 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200421}
422
Jan Glauberd3072972010-02-26 22:37:36 +0100423static inline void account_sbals(struct qdio_q *q, int count)
424{
425 int pos = 0;
426
427 q->q_stats.nr_sbal_total += count;
428 if (count == QDIO_MAX_BUFFERS_MASK) {
429 q->q_stats.nr_sbals[7]++;
430 return;
431 }
432 while (count >>= 1)
433 pos++;
434 q->q_stats.nr_sbals[pos]++;
435}
436
Jan Glauberbffbbd22011-04-20 10:15:33 +0200437static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200438{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200439 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
440 SLSB_P_OUTPUT_NOT_INIT;
441
Jan Glauber1549d132012-05-09 16:27:34 +0200442 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100443
444 /* special handling for no target buffer empty */
445 if ((!q->is_input_q &&
Jan Glauber3ec90872011-06-06 14:14:40 +0200446 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100447 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200448 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100449 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100450 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100451 }
452
Jan Glauber22f99342008-12-25 13:38:46 +0100453 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
454 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100455 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100456 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec90872011-06-06 14:14:40 +0200457 q->sbal[q->first_to_check]->element[14].sflags,
458 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200459
Jan Glauber2768b2d2011-10-30 15:17:07 +0100460set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200461 /*
462 * Interrupts may be avoided as long as the error is present
463 * so change the buffer state immediately to avoid starvation.
464 */
465 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100466}
Jan Glauber779e6e12008-07-17 17:16:48 +0200467
Jan Glauber50f769d2008-12-25 13:38:47 +0100468static inline void inbound_primed(struct qdio_q *q, int count)
469{
470 int new;
471
Jan Glauber1d7e1502009-09-22 22:58:39 +0200472 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100473
474 /* for QEBSM the ACK was already set by EQBS */
475 if (is_qebsm(q)) {
476 if (!q->u.in.polling) {
477 q->u.in.polling = 1;
478 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100479 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100480 return;
481 }
482
483 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100484 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100485 q->u.in.ack_count);
486 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100487 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100488 return;
489 }
490
491 /*
492 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
493 * or by the next inbound run.
494 */
495 new = add_buf(q->first_to_check, count - 1);
496 if (q->u.in.polling) {
497 /* reset the previous ACK but first set the new one */
498 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100499 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100500 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100501 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100502 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100503 }
504
Jan Glaubere85dea02009-03-26 15:24:29 +0100505 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100506 count--;
507 if (!count)
508 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200509 /* need to change ALL buffers to get more interrupts */
510 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200511}
512
513static int get_inbound_buffer_frontier(struct qdio_q *q)
514{
515 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100516 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200517
Heiko Carstens473e66b2012-05-09 16:27:39 +0200518 q->timestamp = get_clock();
Jan Glaubera2b86012011-10-30 15:17:05 +0100519
Jan Glauber779e6e12008-07-17 17:16:48 +0200520 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200521 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
522 * would return 0.
523 */
524 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
525 stop = add_buf(q->first_to_check, count);
526
Jan Glauber779e6e12008-07-17 17:16:48 +0200527 if (q->first_to_check == stop)
528 goto out;
529
Jan Glauber36e3e722009-06-22 12:08:12 +0200530 /*
531 * No siga sync here, as a PCI or we after a thin interrupt
532 * already sync'ed the queues.
533 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000534 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200535 if (!count)
536 goto out;
537
538 switch (state) {
539 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100540 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200541 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100542 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100543 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100544 if (q->irq_ptr->perf_stat_enabled)
545 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200546 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200547 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200548 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200549 q->first_to_check = add_buf(q->first_to_check, count);
550 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100551 if (q->irq_ptr->perf_stat_enabled)
552 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200553 break;
554 case SLSB_CU_INPUT_EMPTY:
555 case SLSB_P_INPUT_NOT_INIT:
556 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100557 if (q->irq_ptr->perf_stat_enabled)
558 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100559 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200560 break;
561 default:
562 BUG();
563 }
564out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200565 return q->first_to_check;
566}
567
Jan Glauber60b5df22009-06-22 12:08:10 +0200568static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200569{
570 int bufnr;
571
572 bufnr = get_inbound_buffer_frontier(q);
573
Jan Glauber1549d132012-05-09 16:27:34 +0200574 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100575 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100576 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber3a601bf2010-05-17 10:00:17 +0200577 q->u.in.timestamp = get_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200578 return 1;
579 } else
580 return 0;
581}
582
Jan Glauber9a2c1602009-06-22 12:08:11 +0200583static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200584{
585 unsigned char state = 0;
586
587 if (!atomic_read(&q->nr_buf_used))
588 return 1;
589
Jan Glauber90adac52011-01-05 12:47:54 +0100590 if (need_siga_sync(q))
591 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200592 get_buf_state(q, q->first_to_check, &state, 0);
593
Ursula Braun4c522282010-02-09 09:46:07 +0100594 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200595 /* more work coming */
596 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200597
598 if (is_thinint_irq(q->irq_ptr))
599 return 1;
600
601 /* don't poll under z/VM */
602 if (MACHINE_IS_VM)
603 return 1;
604
605 /*
606 * At this point we know, that inbound first_to_check
607 * has (probably) not moved (see qdio_inbound_processing).
608 */
Jan Glauber3a601bf2010-05-17 10:00:17 +0200609 if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200610 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200611 q->first_to_check);
612 return 1;
613 } else
614 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200615}
616
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000617static inline int contains_aobs(struct qdio_q *q)
618{
619 return !q->is_input_q && q->u.out.use_cq;
620}
621
622static inline void qdio_trace_aob(struct qdio_irq *irq, struct qdio_q *q,
623 int i, struct qaob *aob)
624{
625 int tmp;
626
627 DBF_DEV_EVENT(DBF_INFO, irq, "AOB%d:%lx", i,
628 (unsigned long) virt_to_phys(aob));
629 DBF_DEV_EVENT(DBF_INFO, irq, "RES00:%lx",
630 (unsigned long) aob->res0[0]);
631 DBF_DEV_EVENT(DBF_INFO, irq, "RES01:%lx",
632 (unsigned long) aob->res0[1]);
633 DBF_DEV_EVENT(DBF_INFO, irq, "RES02:%lx",
634 (unsigned long) aob->res0[2]);
635 DBF_DEV_EVENT(DBF_INFO, irq, "RES03:%lx",
636 (unsigned long) aob->res0[3]);
637 DBF_DEV_EVENT(DBF_INFO, irq, "RES04:%lx",
638 (unsigned long) aob->res0[4]);
639 DBF_DEV_EVENT(DBF_INFO, irq, "RES05:%lx",
640 (unsigned long) aob->res0[5]);
641 DBF_DEV_EVENT(DBF_INFO, irq, "RES1:%x", aob->res1);
642 DBF_DEV_EVENT(DBF_INFO, irq, "RES2:%x", aob->res2);
643 DBF_DEV_EVENT(DBF_INFO, irq, "RES3:%x", aob->res3);
644 DBF_DEV_EVENT(DBF_INFO, irq, "AORC:%u", aob->aorc);
645 DBF_DEV_EVENT(DBF_INFO, irq, "FLAGS:%u", aob->flags);
646 DBF_DEV_EVENT(DBF_INFO, irq, "CBTBS:%u", aob->cbtbs);
647 DBF_DEV_EVENT(DBF_INFO, irq, "SBC:%u", aob->sb_count);
648 for (tmp = 0; tmp < QDIO_MAX_ELEMENTS_PER_BUFFER; ++tmp) {
649 DBF_DEV_EVENT(DBF_INFO, irq, "SBA%d:%lx", tmp,
650 (unsigned long) aob->sba[tmp]);
651 DBF_DEV_EVENT(DBF_INFO, irq, "rSBA%d:%lx", tmp,
652 (unsigned long) q->sbal[i]->element[tmp].addr);
653 DBF_DEV_EVENT(DBF_INFO, irq, "DC%d:%u", tmp, aob->dcount[tmp]);
654 DBF_DEV_EVENT(DBF_INFO, irq, "rDC%d:%u", tmp,
655 q->sbal[i]->element[tmp].length);
656 }
657 DBF_DEV_EVENT(DBF_INFO, irq, "USER0:%lx", (unsigned long) aob->user0);
658 for (tmp = 0; tmp < 2; ++tmp) {
659 DBF_DEV_EVENT(DBF_INFO, irq, "RES4%d:%lx", tmp,
660 (unsigned long) aob->res4[tmp]);
661 }
662 DBF_DEV_EVENT(DBF_INFO, irq, "USER1:%lx", (unsigned long) aob->user1);
663 DBF_DEV_EVENT(DBF_INFO, irq, "USER2:%lx", (unsigned long) aob->user2);
664}
665
666static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
667{
668 unsigned char state = 0;
669 int j, b = start;
670
671 if (!contains_aobs(q))
672 return;
673
674 for (j = 0; j < count; ++j) {
675 get_buf_state(q, b, &state, 0);
676 if (state == SLSB_P_OUTPUT_PENDING) {
677 struct qaob *aob = q->u.out.aobs[b];
678 if (aob == NULL)
679 continue;
680
681 BUG_ON(q->u.out.sbal_state == NULL);
682 q->u.out.sbal_state[b].flags |=
683 QDIO_OUTBUF_STATE_FLAG_PENDING;
684 q->u.out.aobs[b] = NULL;
685 } else if (state == SLSB_P_OUTPUT_EMPTY) {
686 BUG_ON(q->u.out.sbal_state == NULL);
687 q->u.out.sbal_state[b].aob = NULL;
688 }
689 b = next_buf(b);
690 }
691}
692
693static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
694 int bufnr)
695{
696 unsigned long phys_aob = 0;
697
698 if (!q->use_cq)
699 goto out;
700
701 if (!q->aobs[bufnr]) {
702 struct qaob *aob = qdio_allocate_aob();
703 q->aobs[bufnr] = aob;
704 }
705 if (q->aobs[bufnr]) {
706 BUG_ON(q->sbal_state == NULL);
707 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
708 q->sbal_state[bufnr].aob = q->aobs[bufnr];
709 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
710 phys_aob = virt_to_phys(q->aobs[bufnr]);
711 BUG_ON(phys_aob & 0xFF);
712 }
713
714out:
715 return phys_aob;
716}
717
Jan Glauber60b5df22009-06-22 12:08:10 +0200718static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200719{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100720 int start = q->first_to_kick;
721 int end = q->first_to_check;
722 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200723
724 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
725 return;
726
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100727 count = sub_buf(end, start);
728
729 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100730 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200731 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100732 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100733 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200734 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
735 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100736 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100737
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000738 qdio_handle_aobs(q, start, count);
739
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100740 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
741 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200742
743 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100744 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200745 q->qdio_error = 0;
746}
747
748static void __qdio_inbound_processing(struct qdio_q *q)
749{
Jan Glauber6486cda2010-01-04 09:05:42 +0100750 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200751
Jan Glauber779e6e12008-07-17 17:16:48 +0200752 if (!qdio_inbound_q_moved(q))
753 return;
754
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100755 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200756
Jan Glauber6486cda2010-01-04 09:05:42 +0100757 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200758 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100759 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200760 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
761 tasklet_schedule(&q->tasklet);
762 return;
763 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100764 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200765
766 qdio_stop_polling(q);
767 /*
768 * We need to check again to not lose initiative after
769 * resetting the ACK state.
770 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100771 if (!qdio_inbound_q_done(q)) {
772 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200773 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
774 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100775 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200776}
777
Jan Glauber779e6e12008-07-17 17:16:48 +0200778void qdio_inbound_processing(unsigned long data)
779{
780 struct qdio_q *q = (struct qdio_q *)data;
781 __qdio_inbound_processing(q);
782}
783
784static int get_outbound_buffer_frontier(struct qdio_q *q)
785{
786 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100787 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200788
Heiko Carstens473e66b2012-05-09 16:27:39 +0200789 q->timestamp = get_clock();
Jan Glaubera2b86012011-10-30 15:17:05 +0100790
Jan Glauber90adac52011-01-05 12:47:54 +0100791 if (need_siga_sync(q))
792 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
793 !pci_out_supported(q)) ||
794 (queue_type(q) == QDIO_IQDIO_QFMT &&
795 multicast_outbound(q)))
796 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200797
798 /*
799 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
800 * would return 0.
801 */
802 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
803 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200804 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000805 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200806
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000807 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200808 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000809 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200810
811 switch (state) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000812 case SLSB_P_OUTPUT_PENDING:
813 BUG();
Jan Glauber779e6e12008-07-17 17:16:48 +0200814 case SLSB_P_OUTPUT_EMPTY:
815 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000816 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
817 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200818
819 atomic_sub(count, &q->nr_buf_used);
820 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100821 if (q->irq_ptr->perf_stat_enabled)
822 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000823
Jan Glauber36e3e722009-06-22 12:08:12 +0200824 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200825 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200826 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200827 q->first_to_check = add_buf(q->first_to_check, count);
828 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100829 if (q->irq_ptr->perf_stat_enabled)
830 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200831 break;
832 case SLSB_CU_OUTPUT_PRIMED:
833 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100834 if (q->irq_ptr->perf_stat_enabled)
835 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000836 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
837 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200838 break;
839 case SLSB_P_OUTPUT_NOT_INIT:
840 case SLSB_P_OUTPUT_HALTED:
841 break;
842 default:
843 BUG();
844 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000845
846out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200847 return q->first_to_check;
848}
849
850/* all buffers processed? */
851static inline int qdio_outbound_q_done(struct qdio_q *q)
852{
853 return atomic_read(&q->nr_buf_used) == 0;
854}
855
856static inline int qdio_outbound_q_moved(struct qdio_q *q)
857{
858 int bufnr;
859
860 bufnr = get_outbound_buffer_frontier(q);
861
Jan Glauber1549d132012-05-09 16:27:34 +0200862 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100863 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100864 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200865 return 1;
866 } else
867 return 0;
868}
869
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000870static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200871{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200872 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100873 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200874
875 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100876 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200877
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100878 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200879retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100880 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100881
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000882 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100883 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200884 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200885 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100886 case 2:
887 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200888 while (++retries < QDIO_BUSY_BIT_RETRIES) {
889 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
890 goto retry;
891 }
892 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200893 cc = -EBUSY;
894 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100895 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200896 cc = -ENOBUFS;
897 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100898 break;
899 case 1:
900 case 3:
901 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200902 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100903 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200904 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200905 if (retries) {
906 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
907 DBF_ERROR("count:%u", retries);
908 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100909 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200910}
911
Jan Glauber779e6e12008-07-17 17:16:48 +0200912static void __qdio_outbound_processing(struct qdio_q *q)
913{
Jan Glauber6486cda2010-01-04 09:05:42 +0100914 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200915 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
916
917 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100918 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200919
Jan Glauberc38f9602009-03-26 15:24:26 +0100920 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200921 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100922 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200923
Jan Glauber779e6e12008-07-17 17:16:48 +0200924 if (q->u.out.pci_out_enabled)
925 return;
926
927 /*
928 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100929 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
930 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200931 */
932 if (qdio_outbound_q_done(q))
933 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100934 else
935 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200936 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100937 return;
938
939sched:
940 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
941 return;
942 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200943}
944
945/* outbound tasklet */
946void qdio_outbound_processing(unsigned long data)
947{
948 struct qdio_q *q = (struct qdio_q *)data;
949 __qdio_outbound_processing(q);
950}
951
952void qdio_outbound_timer(unsigned long data)
953{
954 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100955
956 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
957 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200958 tasklet_schedule(&q->tasklet);
959}
960
Jan Glauber60b5df22009-06-22 12:08:10 +0200961static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200962{
963 struct qdio_q *out;
964 int i;
965
966 if (!pci_out_supported(q))
967 return;
968
969 for_each_output_queue(q->irq_ptr, out, i)
970 if (!qdio_outbound_q_done(out))
971 tasklet_schedule(&out->tasklet);
972}
973
Jan Glauber60b5df22009-06-22 12:08:10 +0200974static void __tiqdio_inbound_processing(struct qdio_q *q)
975{
Jan Glauber6486cda2010-01-04 09:05:42 +0100976 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100977 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
978 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200979
980 /*
981 * The interrupt could be caused by a PCI request. Check the
982 * PCI capable outbound queues.
983 */
984 qdio_check_outbound_after_thinint(q);
985
986 if (!qdio_inbound_q_moved(q))
987 return;
988
989 qdio_kick_handler(q);
990
Jan Glauber9a2c1602009-06-22 12:08:11 +0200991 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100992 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200993 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200994 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200995 return;
996 }
Jan Glauber60b5df22009-06-22 12:08:10 +0200997 }
998
999 qdio_stop_polling(q);
1000 /*
1001 * We need to check again to not lose initiative after
1002 * resetting the ACK state.
1003 */
Jan Glauber9a2c1602009-06-22 12:08:11 +02001004 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +01001005 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +02001006 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
1007 tasklet_schedule(&q->tasklet);
1008 }
1009}
1010
1011void tiqdio_inbound_processing(unsigned long data)
1012{
1013 struct qdio_q *q = (struct qdio_q *)data;
1014 __tiqdio_inbound_processing(q);
1015}
1016
Jan Glauber779e6e12008-07-17 17:16:48 +02001017static inline void qdio_set_state(struct qdio_irq *irq_ptr,
1018 enum qdio_irq_states state)
1019{
Jan Glauber22f99342008-12-25 13:38:46 +01001020 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +02001021
1022 irq_ptr->state = state;
1023 mb();
1024}
1025
Jan Glauber22f99342008-12-25 13:38:46 +01001026static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +02001027{
Jan Glauber779e6e12008-07-17 17:16:48 +02001028 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +01001029 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
1030 DBF_ERROR_HEX(irb, 64);
1031 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +02001032 }
1033}
1034
1035/* PCI interrupt handler */
1036static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
1037{
1038 int i;
1039 struct qdio_q *q;
1040
Jan Glauberc38f9602009-03-26 15:24:26 +01001041 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
1042 return;
1043
Jan Glauberd36deae2010-09-07 21:14:39 +00001044 for_each_input_queue(irq_ptr, q, i) {
1045 if (q->u.in.queue_start_poll) {
1046 /* skip if polling is enabled or already in work */
1047 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1048 &q->u.in.queue_irq_state)) {
1049 qperf_inc(q, int_discarded);
1050 continue;
1051 }
1052 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1053 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001054 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +00001055 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001056 }
Jan Glauberd36deae2010-09-07 21:14:39 +00001057 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001058
Jan Glauber90adac52011-01-05 12:47:54 +01001059 if (!pci_out_supported(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001060 return;
1061
1062 for_each_output_queue(irq_ptr, q, i) {
1063 if (qdio_outbound_q_done(q))
1064 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001065 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001066 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001067 tasklet_schedule(&q->tasklet);
1068 }
1069}
1070
1071static void qdio_handle_activate_check(struct ccw_device *cdev,
1072 unsigned long intparm, int cstat, int dstat)
1073{
1074 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1075 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001076 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001077
Jan Glauber22f99342008-12-25 13:38:46 +01001078 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1079 DBF_ERROR("intp :%lx", intparm);
1080 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001081
1082 if (irq_ptr->nr_input_qs) {
1083 q = irq_ptr->input_qs[0];
1084 } else if (irq_ptr->nr_output_qs) {
1085 q = irq_ptr->output_qs[0];
1086 } else {
1087 dump_stack();
1088 goto no_handler;
1089 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001090
1091 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001092 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001093 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001094no_handler:
1095 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001096 /*
1097 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1098 * Therefore we call the LGR detection function here.
1099 */
1100 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001101}
1102
Jan Glauber779e6e12008-07-17 17:16:48 +02001103static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1104 int dstat)
1105{
1106 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001107
Jan Glauber22f99342008-12-25 13:38:46 +01001108 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001109
1110 if (cstat)
1111 goto error;
1112 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1113 goto error;
1114 if (!(dstat & DEV_STAT_DEV_END))
1115 goto error;
1116 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1117 return;
1118
1119error:
1120 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1121 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1122 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001123}
1124
1125/* qdio interrupt handler */
1126void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1127 struct irb *irb)
1128{
1129 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1130 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001131
Jan Glauber779e6e12008-07-17 17:16:48 +02001132 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +01001133 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001134 return;
1135 }
1136
Jan Glauber09a308f2010-05-17 10:00:14 +02001137 if (irq_ptr->perf_stat_enabled)
1138 irq_ptr->perf_stat.qdio_int++;
1139
Jan Glauber779e6e12008-07-17 17:16:48 +02001140 if (IS_ERR(irb)) {
1141 switch (PTR_ERR(irb)) {
1142 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +01001143 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +02001144 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1145 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001146 return;
1147 default:
1148 WARN_ON(1);
1149 return;
1150 }
1151 }
Jan Glauber22f99342008-12-25 13:38:46 +01001152 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001153 cstat = irb->scsw.cmd.cstat;
1154 dstat = irb->scsw.cmd.dstat;
1155
1156 switch (irq_ptr->state) {
1157 case QDIO_IRQ_STATE_INACTIVE:
1158 qdio_establish_handle_irq(cdev, cstat, dstat);
1159 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001160 case QDIO_IRQ_STATE_CLEANUP:
1161 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1162 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001163 case QDIO_IRQ_STATE_ESTABLISHED:
1164 case QDIO_IRQ_STATE_ACTIVE:
1165 if (cstat & SCHN_STAT_PCI) {
1166 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001167 return;
1168 }
Jan Glauber4c575422009-06-12 10:26:28 +02001169 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001170 qdio_handle_activate_check(cdev, intparm, cstat,
1171 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001172 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001173 case QDIO_IRQ_STATE_STOPPED:
1174 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001175 default:
1176 WARN_ON(1);
1177 }
1178 wake_up(&cdev->private->wait_q);
1179}
1180
1181/**
1182 * qdio_get_ssqd_desc - get qdio subchannel description
1183 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001184 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001185 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001186 * Returns 0 or an error code. The results of the chsc are stored in the
1187 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001188 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001189int qdio_get_ssqd_desc(struct ccw_device *cdev,
1190 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001191{
Jan Glauber779e6e12008-07-17 17:16:48 +02001192
Jan Glauberbbd50e12008-12-25 13:38:43 +01001193 if (!cdev || !cdev->private)
1194 return -EINVAL;
1195
Jan Glauber22f99342008-12-25 13:38:46 +01001196 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001197 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001198}
1199EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1200
Jan Glauber779e6e12008-07-17 17:16:48 +02001201static void qdio_shutdown_queues(struct ccw_device *cdev)
1202{
1203 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1204 struct qdio_q *q;
1205 int i;
1206
1207 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001208 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001209
1210 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001211 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001212 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001213 }
1214}
1215
1216/**
1217 * qdio_shutdown - shut down a qdio subchannel
1218 * @cdev: associated ccw device
1219 * @how: use halt or clear to shutdown
1220 */
1221int qdio_shutdown(struct ccw_device *cdev, int how)
1222{
Jan Glauber22f99342008-12-25 13:38:46 +01001223 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001224 int rc;
1225 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001226
Jan Glauber779e6e12008-07-17 17:16:48 +02001227 if (!irq_ptr)
1228 return -ENODEV;
1229
Jan Glauberb4547402009-03-26 15:24:24 +01001230 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001231 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1232
Jan Glauber779e6e12008-07-17 17:16:48 +02001233 mutex_lock(&irq_ptr->setup_mutex);
1234 /*
1235 * Subchannel was already shot down. We cannot prevent being called
1236 * twice since cio may trigger a shutdown asynchronously.
1237 */
1238 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1239 mutex_unlock(&irq_ptr->setup_mutex);
1240 return 0;
1241 }
1242
Jan Glauberc38f9602009-03-26 15:24:26 +01001243 /*
1244 * Indicate that the device is going down. Scheduling the queue
1245 * tasklets is forbidden from here on.
1246 */
1247 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1248
Jan Glauber779e6e12008-07-17 17:16:48 +02001249 tiqdio_remove_input_queues(irq_ptr);
1250 qdio_shutdown_queues(cdev);
1251 qdio_shutdown_debug_entries(irq_ptr, cdev);
1252
1253 /* cleanup subchannel */
1254 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1255
1256 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1257 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1258 else
1259 /* default behaviour is halt */
1260 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1261 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001262 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1263 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001264 goto no_cleanup;
1265 }
1266
1267 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1268 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1269 wait_event_interruptible_timeout(cdev->private->wait_q,
1270 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1271 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1272 10 * HZ);
1273 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1274
1275no_cleanup:
1276 qdio_shutdown_thinint(irq_ptr);
1277
1278 /* restore interrupt handler */
1279 if ((void *)cdev->handler == (void *)qdio_int_handler)
1280 cdev->handler = irq_ptr->orig_handler;
1281 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1282
1283 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1284 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001285 if (rc)
1286 return rc;
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(qdio_shutdown);
1290
1291/**
1292 * qdio_free - free data structures for a qdio subchannel
1293 * @cdev: associated ccw device
1294 */
1295int qdio_free(struct ccw_device *cdev)
1296{
Jan Glauber22f99342008-12-25 13:38:46 +01001297 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001298
Jan Glauber779e6e12008-07-17 17:16:48 +02001299 if (!irq_ptr)
1300 return -ENODEV;
1301
Jan Glauber22f99342008-12-25 13:38:46 +01001302 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001303 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001304
1305 if (irq_ptr->debug_area != NULL) {
1306 debug_unregister(irq_ptr->debug_area);
1307 irq_ptr->debug_area = NULL;
1308 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001309 cdev->private->qdio_data = NULL;
1310 mutex_unlock(&irq_ptr->setup_mutex);
1311
1312 qdio_release_memory(irq_ptr);
1313 return 0;
1314}
1315EXPORT_SYMBOL_GPL(qdio_free);
1316
1317/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001318 * qdio_allocate - allocate qdio queues and associated data
1319 * @init_data: initialization data
1320 */
1321int qdio_allocate(struct qdio_initialize *init_data)
1322{
1323 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001324
Jan Glauber22f99342008-12-25 13:38:46 +01001325 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001326
1327 if ((init_data->no_input_qs && !init_data->input_handler) ||
1328 (init_data->no_output_qs && !init_data->output_handler))
1329 return -EINVAL;
1330
1331 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1332 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1333 return -EINVAL;
1334
1335 if ((!init_data->input_sbal_addr_array) ||
1336 (!init_data->output_sbal_addr_array))
1337 return -EINVAL;
1338
Jan Glauber779e6e12008-07-17 17:16:48 +02001339 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1340 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1341 if (!irq_ptr)
1342 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001343
1344 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001345 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001346
1347 /*
1348 * Allocate a page for the chsc calls in qdio_establish.
1349 * Must be pre-allocated since a zfcp recovery will call
1350 * qdio_establish. In case of low memory and swap on a zfcp disk
1351 * we may not be able to allocate memory otherwise.
1352 */
1353 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1354 if (!irq_ptr->chsc_page)
1355 goto out_rel;
1356
1357 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001358 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001359 if (!irq_ptr->qdr)
1360 goto out_rel;
1361 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1362
Jan Glauber779e6e12008-07-17 17:16:48 +02001363 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1364 init_data->no_output_qs))
1365 goto out_rel;
1366
1367 init_data->cdev->private->qdio_data = irq_ptr;
1368 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1369 return 0;
1370out_rel:
1371 qdio_release_memory(irq_ptr);
1372out_err:
1373 return -ENOMEM;
1374}
1375EXPORT_SYMBOL_GPL(qdio_allocate);
1376
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001377static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1378{
1379 struct qdio_q *q = irq_ptr->input_qs[0];
1380 int i, use_cq = 0;
1381
1382 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1383 use_cq = 1;
1384
1385 for_each_output_queue(irq_ptr, q, i) {
1386 if (use_cq) {
1387 if (qdio_enable_async_operation(&q->u.out) < 0) {
1388 use_cq = 0;
1389 continue;
1390 }
1391 } else
1392 qdio_disable_async_operation(&q->u.out);
1393 }
1394 DBF_EVENT("use_cq:%d", use_cq);
1395}
1396
Jan Glauber779e6e12008-07-17 17:16:48 +02001397/**
1398 * qdio_establish - establish queues on a qdio subchannel
1399 * @init_data: initialization data
1400 */
1401int qdio_establish(struct qdio_initialize *init_data)
1402{
Jan Glauber779e6e12008-07-17 17:16:48 +02001403 struct qdio_irq *irq_ptr;
1404 struct ccw_device *cdev = init_data->cdev;
1405 unsigned long saveflags;
1406 int rc;
1407
Jan Glauber22f99342008-12-25 13:38:46 +01001408 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001409
Jan Glauber779e6e12008-07-17 17:16:48 +02001410 irq_ptr = cdev->private->qdio_data;
1411 if (!irq_ptr)
1412 return -ENODEV;
1413
1414 if (cdev->private->state != DEV_STATE_ONLINE)
1415 return -EINVAL;
1416
Jan Glauber779e6e12008-07-17 17:16:48 +02001417 mutex_lock(&irq_ptr->setup_mutex);
1418 qdio_setup_irq(init_data);
1419
1420 rc = qdio_establish_thinint(irq_ptr);
1421 if (rc) {
1422 mutex_unlock(&irq_ptr->setup_mutex);
1423 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1424 return rc;
1425 }
1426
1427 /* establish q */
1428 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1429 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1430 irq_ptr->ccw.count = irq_ptr->equeue.count;
1431 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1432
1433 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1434 ccw_device_set_options_mask(cdev, 0);
1435
1436 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1437 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001438 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1439 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001440 }
1441 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1442
1443 if (rc) {
1444 mutex_unlock(&irq_ptr->setup_mutex);
1445 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1446 return rc;
1447 }
1448
1449 wait_event_interruptible_timeout(cdev->private->wait_q,
1450 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1451 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1452
1453 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1454 mutex_unlock(&irq_ptr->setup_mutex);
1455 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1456 return -EIO;
1457 }
1458
1459 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001460
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001461 qdio_detect_hsicq(irq_ptr);
1462
Jan Glauber779e6e12008-07-17 17:16:48 +02001463 /* qebsm is now setup if available, initialize buffer states */
1464 qdio_init_buf_states(irq_ptr);
1465
1466 mutex_unlock(&irq_ptr->setup_mutex);
1467 qdio_print_subchannel_info(irq_ptr, cdev);
1468 qdio_setup_debug_entries(irq_ptr, cdev);
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(qdio_establish);
1472
1473/**
1474 * qdio_activate - activate queues on a qdio subchannel
1475 * @cdev: associated cdev
1476 */
1477int qdio_activate(struct ccw_device *cdev)
1478{
1479 struct qdio_irq *irq_ptr;
1480 int rc;
1481 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001482
Jan Glauber22f99342008-12-25 13:38:46 +01001483 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001484
Jan Glauber779e6e12008-07-17 17:16:48 +02001485 irq_ptr = cdev->private->qdio_data;
1486 if (!irq_ptr)
1487 return -ENODEV;
1488
1489 if (cdev->private->state != DEV_STATE_ONLINE)
1490 return -EINVAL;
1491
1492 mutex_lock(&irq_ptr->setup_mutex);
1493 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1494 rc = -EBUSY;
1495 goto out;
1496 }
1497
Jan Glauber779e6e12008-07-17 17:16:48 +02001498 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1499 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1500 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1501 irq_ptr->ccw.cda = 0;
1502
1503 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1504 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1505
1506 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1507 0, DOIO_DENY_PREFETCH);
1508 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001509 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1510 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001511 }
1512 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1513
1514 if (rc)
1515 goto out;
1516
1517 if (is_thinint_irq(irq_ptr))
1518 tiqdio_add_input_queues(irq_ptr);
1519
1520 /* wait for subchannel to become active */
1521 msleep(5);
1522
1523 switch (irq_ptr->state) {
1524 case QDIO_IRQ_STATE_STOPPED:
1525 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001526 rc = -EIO;
1527 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001528 default:
1529 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1530 rc = 0;
1531 }
1532out:
1533 mutex_unlock(&irq_ptr->setup_mutex);
1534 return rc;
1535}
1536EXPORT_SYMBOL_GPL(qdio_activate);
1537
1538static inline int buf_in_between(int bufnr, int start, int count)
1539{
1540 int end = add_buf(start, count);
1541
1542 if (end > start) {
1543 if (bufnr >= start && bufnr < end)
1544 return 1;
1545 else
1546 return 0;
1547 }
1548
1549 /* wrap-around case */
1550 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1551 (bufnr < end))
1552 return 1;
1553 else
1554 return 0;
1555}
1556
1557/**
1558 * handle_inbound - reset processed input buffers
1559 * @q: queue containing the buffers
1560 * @callflags: flags
1561 * @bufnr: first buffer to process
1562 * @count: how many buffers are emptied
1563 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001564static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1565 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001566{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001567 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001568
Jan Glauber6486cda2010-01-04 09:05:42 +01001569 qperf_inc(q, inbound_call);
1570
Jan Glauber50f769d2008-12-25 13:38:47 +01001571 if (!q->u.in.polling)
1572 goto set;
1573
1574 /* protect against stop polling setting an ACK for an emptied slsb */
1575 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1576 /* overwriting everything, just delete polling status */
1577 q->u.in.polling = 0;
1578 q->u.in.ack_count = 0;
1579 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001580 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001581 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001582 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001583 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001584 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001585 q->u.in.ack_count -= diff;
1586 if (q->u.in.ack_count <= 0) {
1587 q->u.in.polling = 0;
1588 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001589 goto set;
1590 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001591 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001592 }
1593 else
1594 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001595 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001596 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001597
Jan Glauber50f769d2008-12-25 13:38:47 +01001598set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001599 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001600
1601 used = atomic_add_return(count, &q->nr_buf_used) - count;
1602 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1603
Jan Glauberd303b6f2009-03-26 15:24:31 +01001604 if (need_siga_in(q))
1605 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001606
Jan Glauberd303b6f2009-03-26 15:24:31 +01001607 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001608}
1609
1610/**
1611 * handle_outbound - process filled outbound buffers
1612 * @q: queue containing the buffers
1613 * @callflags: flags
1614 * @bufnr: first buffer to process
1615 * @count: how many buffers are filled
1616 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001617static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1618 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001619{
Jan Glauberc26001d2011-05-23 10:24:38 +02001620 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001621 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001622
Jan Glauber6486cda2010-01-04 09:05:42 +01001623 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001624
1625 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1626 used = atomic_add_return(count, &q->nr_buf_used);
1627 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1628
Jan Glauber01958432011-01-05 12:47:51 +01001629 if (used == QDIO_MAX_BUFFERS_PER_Q)
1630 qperf_inc(q, outbound_queue_full);
1631
Jan Glauber6486cda2010-01-04 09:05:42 +01001632 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001633 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001634 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001635 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001636 q->u.out.pci_out_enabled = 0;
1637
1638 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001639 unsigned long phys_aob = 0;
1640
1641 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001642 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1643
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001644 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1645
1646 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001647 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001648 rc = qdio_siga_sync_q(q);
1649 } else {
1650 /* try to fast requeue buffers */
1651 get_buf_state(q, prev_buf(bufnr), &state, 0);
1652 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001653 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001654 else
Jan Glauber110da312011-01-05 12:47:53 +01001655 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001656 }
1657
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001658 /* in case of SIGA errors we must process the error immediately */
1659 if (used >= q->u.out.scan_threshold || rc)
1660 tasklet_schedule(&q->tasklet);
1661 else
1662 /* free the SBALs in case of no further traffic */
1663 if (!timer_pending(&q->u.out.timer))
1664 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001665 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001666}
1667
1668/**
1669 * do_QDIO - process input or output buffers
1670 * @cdev: associated ccw_device for the qdio subchannel
1671 * @callflags: input or output and special flags from the program
1672 * @q_nr: queue number
1673 * @bufnr: buffer number
1674 * @count: how many buffers to process
1675 */
1676int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001677 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001678{
1679 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001680
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001681
Jan Glauber66182412009-06-22 12:08:15 +02001682 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001683 return -EINVAL;
1684
Jan Glauber779e6e12008-07-17 17:16:48 +02001685 irq_ptr = cdev->private->qdio_data;
1686 if (!irq_ptr)
1687 return -ENODEV;
1688
Jan Glauber1d7e1502009-09-22 22:58:39 +02001689 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1690 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001691
1692 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001693 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001694 if (!count)
1695 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001696 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001697 return handle_inbound(irq_ptr->input_qs[q_nr],
1698 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001699 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001700 return handle_outbound(irq_ptr->output_qs[q_nr],
1701 callflags, bufnr, count);
1702 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001703}
1704EXPORT_SYMBOL_GPL(do_QDIO);
1705
Jan Glauberd36deae2010-09-07 21:14:39 +00001706/**
1707 * qdio_start_irq - process input buffers
1708 * @cdev: associated ccw_device for the qdio subchannel
1709 * @nr: input queue number
1710 *
1711 * Return codes
1712 * 0 - success
1713 * 1 - irqs not started since new data is available
1714 */
1715int qdio_start_irq(struct ccw_device *cdev, int nr)
1716{
1717 struct qdio_q *q;
1718 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1719
1720 if (!irq_ptr)
1721 return -ENODEV;
1722 q = irq_ptr->input_qs[nr];
1723
1724 WARN_ON(queue_irqs_enabled(q));
1725
Jan Glauber5f4026f2011-10-30 15:17:20 +01001726 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001727 qdio_stop_polling(q);
1728 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1729
1730 /*
1731 * We need to check again to not lose initiative after
1732 * resetting the ACK state.
1733 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001734 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001735 goto rescan;
1736 if (!qdio_inbound_q_done(q))
1737 goto rescan;
1738 return 0;
1739
1740rescan:
1741 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1742 &q->u.in.queue_irq_state))
1743 return 0;
1744 else
1745 return 1;
1746
1747}
1748EXPORT_SYMBOL(qdio_start_irq);
1749
1750/**
1751 * qdio_get_next_buffers - process input buffers
1752 * @cdev: associated ccw_device for the qdio subchannel
1753 * @nr: input queue number
1754 * @bufnr: first filled buffer number
1755 * @error: buffers are in error state
1756 *
1757 * Return codes
1758 * < 0 - error
1759 * = 0 - no new buffers found
1760 * > 0 - number of processed buffers
1761 */
1762int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1763 int *error)
1764{
1765 struct qdio_q *q;
1766 int start, end;
1767 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1768
1769 if (!irq_ptr)
1770 return -ENODEV;
1771 q = irq_ptr->input_qs[nr];
1772 WARN_ON(queue_irqs_enabled(q));
1773
Jan Glauberd36deae2010-09-07 21:14:39 +00001774 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001775 * Cannot rely on automatic sync after interrupt since queues may
1776 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001777 */
Jan Glauber90adac52011-01-05 12:47:54 +01001778 if (need_siga_sync(q))
1779 qdio_sync_queues(q);
1780
1781 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001782 qdio_check_outbound_after_thinint(q);
1783
1784 if (!qdio_inbound_q_moved(q))
1785 return 0;
1786
1787 /* Note: upper-layer MUST stop processing immediately here ... */
1788 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1789 return -EIO;
1790
1791 start = q->first_to_kick;
1792 end = q->first_to_check;
1793 *bufnr = start;
1794 *error = q->qdio_error;
1795
1796 /* for the next time */
1797 q->first_to_kick = end;
1798 q->qdio_error = 0;
1799 return sub_buf(end, start);
1800}
1801EXPORT_SYMBOL(qdio_get_next_buffers);
1802
1803/**
1804 * qdio_stop_irq - disable interrupt processing for the device
1805 * @cdev: associated ccw_device for the qdio subchannel
1806 * @nr: input queue number
1807 *
1808 * Return codes
1809 * 0 - interrupts were already disabled
1810 * 1 - interrupts successfully disabled
1811 */
1812int qdio_stop_irq(struct ccw_device *cdev, int nr)
1813{
1814 struct qdio_q *q;
1815 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1816
1817 if (!irq_ptr)
1818 return -ENODEV;
1819 q = irq_ptr->input_qs[nr];
1820
1821 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1822 &q->u.in.queue_irq_state))
1823 return 0;
1824 else
1825 return 1;
1826}
1827EXPORT_SYMBOL(qdio_stop_irq);
1828
Jan Glauber779e6e12008-07-17 17:16:48 +02001829static int __init init_QDIO(void)
1830{
1831 int rc;
1832
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001833 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001834 if (rc)
1835 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001836 rc = qdio_setup_init();
1837 if (rc)
1838 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001839 rc = tiqdio_allocate_memory();
1840 if (rc)
1841 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001842 rc = tiqdio_register_thinints();
1843 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001844 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001845 return 0;
1846
Jan Glauber779e6e12008-07-17 17:16:48 +02001847out_ti:
1848 tiqdio_free_memory();
1849out_cache:
1850 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001851out_debug:
1852 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001853 return rc;
1854}
1855
1856static void __exit exit_QDIO(void)
1857{
1858 tiqdio_unregister_thinints();
1859 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001860 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001861 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001862}
1863
1864module_init(init_QDIO);
1865module_exit(exit_QDIO);