blob: 288c9140290e9a08b899b234c7e514c9bcc6838a [file] [log] [blame]
Jan Glauber779e6e12008-07-17 17:16:48 +02001/*
2 * linux/drivers/s390/cio/qdio_main.c
3 *
4 * Linux for s390 qdio support, buffer handling, qdio API and module support.
5 *
6 * Copyright 2000,2008 IBM Corp.
7 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
8 * Jan Glauber <jang@linux.vnet.ibm.com>
9 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
10 */
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/timer.h>
15#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Jan Glauber30d77c32011-01-05 12:47:29 +010017#include <linux/kernel_stat.h>
Arun Sharma60063492011-07-26 16:09:06 -070018#include <linux/atomic.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020019#include <asm/debug.h>
20#include <asm/qdio.h>
21
22#include "cio.h"
23#include "css.h"
24#include "device.h"
25#include "qdio.h"
26#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020027
28MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 "Jan Glauber <jang@linux.vnet.ibm.com>");
30MODULE_DESCRIPTION("QDIO base support");
31MODULE_LICENSE("GPL");
32
Jan Glauber958c0ba2011-01-05 12:47:52 +010033static inline int do_siga_sync(unsigned long schid,
34 unsigned int out_mask, unsigned int in_mask,
35 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020036{
Jan Glauber958c0ba2011-01-05 12:47:52 +010037 register unsigned long __fc asm ("0") = fc;
38 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020039 register unsigned long out asm ("2") = out_mask;
40 register unsigned long in asm ("3") = in_mask;
41 int cc;
42
43 asm volatile(
44 " siga 0\n"
45 " ipm %0\n"
46 " srl %0,28\n"
47 : "=d" (cc)
48 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
49 return cc;
50}
51
Jan Glauber958c0ba2011-01-05 12:47:52 +010052static inline int do_siga_input(unsigned long schid, unsigned int mask,
53 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020054{
Jan Glauber958c0ba2011-01-05 12:47:52 +010055 register unsigned long __fc asm ("0") = fc;
56 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020057 register unsigned long __mask asm ("2") = mask;
58 int cc;
59
60 asm volatile(
61 " siga 0\n"
62 " ipm %0\n"
63 " srl %0,28\n"
64 : "=d" (cc)
65 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
66 return cc;
67}
68
69/**
70 * do_siga_output - perform SIGA-w/wt function
71 * @schid: subchannel id or in case of QEBSM the subchannel token
72 * @mask: which output queues to process
73 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
74 * @fc: function code to perform
75 *
76 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
77 * Note: For IQDC unicast queues only the highest priority queue is processed.
78 */
79static inline int do_siga_output(unsigned long schid, unsigned long mask,
Jan Glauber7a0b4cb2008-12-25 13:38:48 +010080 unsigned int *bb, unsigned int fc)
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;
85 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
86
87 asm volatile(
88 " siga 0\n"
89 "0: ipm %0\n"
90 " srl %0,28\n"
91 "1:\n"
92 EX_TABLE(0b, 1b)
93 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
94 : : "cc", "memory");
95 *bb = ((unsigned int) __fc) >> 31;
96 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;
104 /* not all buffers processed */
105 if (ccq == 96 || ccq == 97)
106 return 1;
107 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100108 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200109 return -EIO;
110}
111
112/**
113 * qdio_do_eqbs - extract buffer states for QEBSM
114 * @q: queue to manipulate
115 * @state: state of the extracted buffers
116 * @start: buffer number to start at
117 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100118 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200119 *
Coly Li73ac36e2009-01-07 18:09:16 -0800120 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200121 * Stops processing if a state is different from the last buffers state.
122 */
123static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100124 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200125{
126 unsigned int ccq = 0;
127 int tmp_count = count, tmp_start = start;
128 int nr = q->nr;
129 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200130
131 BUG_ON(!q->irq_ptr->sch_token);
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);
140
141 /* At least one buffer was processed, return and extract the remaining
142 * buffers later.
143 */
Jan Glauber23589d02008-12-25 13:38:44 +0100144 if ((ccq == 96) && (count != tmp_count)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100145 qperf_inc(q, eqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200146 return (count - tmp_count);
Jan Glauber23589d02008-12-25 13:38:44 +0100147 }
Jan Glauber22f99342008-12-25 13:38:46 +0100148
Jan Glauber779e6e12008-07-17 17:16:48 +0200149 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100150 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200151 goto again;
152 }
153
154 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100155 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
156 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200157 q->handler(q->irq_ptr->cdev,
158 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
159 0, -1, -1, q->irq_ptr->int_parm);
160 return 0;
161 }
162 return count - tmp_count;
163}
164
165/**
166 * qdio_do_sqbs - set buffer states for QEBSM
167 * @q: queue to manipulate
168 * @state: new state of the buffers
169 * @start: first buffer number to change
170 * @count: how many buffers to change
171 *
172 * Returns the number of successfully changed buffers.
173 * Does retrying until the specified count of buffer states is set or an
174 * error occurs.
175 */
176static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
177 int count)
178{
179 unsigned int ccq = 0;
180 int tmp_count = count, tmp_start = start;
181 int nr = q->nr;
182 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200183
Jan Glauber50f769d2008-12-25 13:38:47 +0100184 if (!count)
185 return 0;
186
Jan Glauber779e6e12008-07-17 17:16:48 +0200187 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100188 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200189
190 if (!q->is_input_q)
191 nr += q->irq_ptr->nr_input_qs;
192again:
193 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
194 rc = qdio_check_ccq(q, ccq);
195 if (rc == 1) {
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 }
200 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100201 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
202 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200203 q->handler(q->irq_ptr->cdev,
204 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
205 0, -1, -1, q->irq_ptr->int_parm);
206 return 0;
207 }
208 WARN_ON(tmp_count);
209 return count - tmp_count;
210}
211
212/* returns number of examined buffers and their common state in *state */
213static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100214 unsigned char *state, unsigned int count,
215 int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200216{
217 unsigned char __state = 0;
218 int i;
219
220 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
221 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
222
223 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100224 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200225
226 for (i = 0; i < count; i++) {
227 if (!__state)
228 __state = q->slsb.val[bufnr];
229 else if (q->slsb.val[bufnr] != __state)
230 break;
231 bufnr = next_buf(bufnr);
232 }
233 *state = __state;
234 return i;
235}
236
Jan Glauber60b5df22009-06-22 12:08:10 +0200237static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
238 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200239{
Jan Glauber50f769d2008-12-25 13:38:47 +0100240 return get_buf_states(q, bufnr, state, 1, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200241}
242
243/* wrap-around safe setting of slsb states, returns number of changed buffers */
244static inline int set_buf_states(struct qdio_q *q, int bufnr,
245 unsigned char state, int count)
246{
247 int i;
248
249 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
250 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
251
252 if (is_qebsm(q))
253 return qdio_do_sqbs(q, state, bufnr, count);
254
255 for (i = 0; i < count; i++) {
256 xchg(&q->slsb.val[bufnr], state);
257 bufnr = next_buf(bufnr);
258 }
259 return count;
260}
261
262static inline int set_buf_state(struct qdio_q *q, int bufnr,
263 unsigned char state)
264{
265 return set_buf_states(q, bufnr, state, 1);
266}
267
268/* set slsb states to initial state */
269void qdio_init_buf_states(struct qdio_irq *irq_ptr)
270{
271 struct qdio_q *q;
272 int i;
273
274 for_each_input_queue(irq_ptr, q, i)
275 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
276 QDIO_MAX_BUFFERS_PER_Q);
277 for_each_output_queue(irq_ptr, q, i)
278 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
279 QDIO_MAX_BUFFERS_PER_Q);
280}
281
Jan Glauber60b5df22009-06-22 12:08:10 +0200282static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200283 unsigned int input)
284{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100285 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
286 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200287 int cc;
288
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100289 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100290 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200291
Jan Glauber958c0ba2011-01-05 12:47:52 +0100292 if (is_qebsm(q)) {
293 schid = q->irq_ptr->sch_token;
294 fc |= QDIO_SIGA_QEBSM_FLAG;
295 }
296
297 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100298 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100299 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200300 return cc;
301}
302
Jan Glauber60b5df22009-06-22 12:08:10 +0200303static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200304{
305 if (q->is_input_q)
306 return qdio_siga_sync(q, 0, q->mask);
307 else
308 return qdio_siga_sync(q, q->mask, 0);
309}
310
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100311static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
Jan Glauber779e6e12008-07-17 17:16:48 +0200312{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100313 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
314 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100315 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200316 int retries = 0, cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200317
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100318 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200319 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100320 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200321 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200322again:
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100323 cc = do_siga_output(schid, q->mask, busy_bit, fc);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200324
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100325 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100326 if (unlikely(*busy_bit)) {
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100327 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200328 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100329
330 if (!start_time) {
Jan Glauber3a601bf2010-05-17 10:00:17 +0200331 start_time = get_clock();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100332 goto again;
333 }
Jan Glauber3a601bf2010-05-17 10:00:17 +0200334 if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200335 goto again;
336 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200337 if (retries) {
338 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
339 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
340 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
341 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200342 return cc;
343}
344
345static inline int qdio_siga_input(struct qdio_q *q)
346{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100347 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
348 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200349 int cc;
350
Jan Glauber22f99342008-12-25 13:38:46 +0100351 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100352 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200353
Jan Glauber958c0ba2011-01-05 12:47:52 +0100354 if (is_qebsm(q)) {
355 schid = q->irq_ptr->sch_token;
356 fc |= QDIO_SIGA_QEBSM_FLAG;
357 }
358
359 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100360 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100361 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200362 return cc;
363}
364
Jan Glauber90adac52011-01-05 12:47:54 +0100365#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
366#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
367
368static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200369{
Jan Glauber90adac52011-01-05 12:47:54 +0100370 /* PCI capable outbound queues will also be scanned so sync them too */
371 if (pci_out_supported(q))
372 qdio_siga_sync_all(q);
373 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200374 qdio_siga_sync_q(q);
375}
376
Jan Glauber60b5df22009-06-22 12:08:10 +0200377int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
378 unsigned char *state)
379{
Jan Glauber90adac52011-01-05 12:47:54 +0100380 if (need_siga_sync(q))
381 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200382 return get_buf_states(q, bufnr, state, 1, 0);
383}
384
385static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200386{
Jan Glauber50f769d2008-12-25 13:38:47 +0100387 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200388 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100389
Jan Glauber779e6e12008-07-17 17:16:48 +0200390 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100391 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200392
393 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100394 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100395 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100396 q->u.in.ack_count);
397 q->u.in.ack_count = 0;
398 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100399 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200400}
401
Jan Glauberd3072972010-02-26 22:37:36 +0100402static inline void account_sbals(struct qdio_q *q, int count)
403{
404 int pos = 0;
405
406 q->q_stats.nr_sbal_total += count;
407 if (count == QDIO_MAX_BUFFERS_MASK) {
408 q->q_stats.nr_sbals[7]++;
409 return;
410 }
411 while (count >>= 1)
412 pos++;
413 q->q_stats.nr_sbals[pos]++;
414}
415
Jan Glauberbffbbd22011-04-20 10:15:33 +0200416static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200417{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200418 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
419 SLSB_P_OUTPUT_NOT_INIT;
420
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100421 q->qdio_error |= QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100422
423 /* special handling for no target buffer empty */
424 if ((!q->is_input_q &&
Jan Glauber3ec90872011-06-06 14:14:40 +0200425 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100426 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200427 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100428 q->first_to_check);
429 return;
430 }
431
Jan Glauber22f99342008-12-25 13:38:46 +0100432 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
433 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100434 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100435 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec90872011-06-06 14:14:40 +0200436 q->sbal[q->first_to_check]->element[14].sflags,
437 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200438
439 /*
440 * Interrupts may be avoided as long as the error is present
441 * so change the buffer state immediately to avoid starvation.
442 */
443 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100444}
Jan Glauber779e6e12008-07-17 17:16:48 +0200445
Jan Glauber50f769d2008-12-25 13:38:47 +0100446static inline void inbound_primed(struct qdio_q *q, int count)
447{
448 int new;
449
Jan Glauber1d7e1502009-09-22 22:58:39 +0200450 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100451
452 /* for QEBSM the ACK was already set by EQBS */
453 if (is_qebsm(q)) {
454 if (!q->u.in.polling) {
455 q->u.in.polling = 1;
456 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100457 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100458 return;
459 }
460
461 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100462 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100463 q->u.in.ack_count);
464 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100465 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100466 return;
467 }
468
469 /*
470 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
471 * or by the next inbound run.
472 */
473 new = add_buf(q->first_to_check, count - 1);
474 if (q->u.in.polling) {
475 /* reset the previous ACK but first set the new one */
476 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100477 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100478 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100479 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100480 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100481 }
482
Jan Glaubere85dea02009-03-26 15:24:29 +0100483 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100484 count--;
485 if (!count)
486 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200487 /* need to change ALL buffers to get more interrupts */
488 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200489}
490
491static int get_inbound_buffer_frontier(struct qdio_q *q)
492{
493 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100494 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200495
496 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200497 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
498 * would return 0.
499 */
500 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
501 stop = add_buf(q->first_to_check, count);
502
Jan Glauber779e6e12008-07-17 17:16:48 +0200503 if (q->first_to_check == stop)
504 goto out;
505
Jan Glauber36e3e722009-06-22 12:08:12 +0200506 /*
507 * No siga sync here, as a PCI or we after a thin interrupt
508 * already sync'ed the queues.
509 */
Jan Glauber50f769d2008-12-25 13:38:47 +0100510 count = get_buf_states(q, q->first_to_check, &state, count, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200511 if (!count)
512 goto out;
513
514 switch (state) {
515 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100516 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200517 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100518 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100519 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100520 if (q->irq_ptr->perf_stat_enabled)
521 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200522 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200523 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200524 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200525 q->first_to_check = add_buf(q->first_to_check, count);
526 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100527 if (q->irq_ptr->perf_stat_enabled)
528 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200529 break;
530 case SLSB_CU_INPUT_EMPTY:
531 case SLSB_P_INPUT_NOT_INIT:
532 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100533 if (q->irq_ptr->perf_stat_enabled)
534 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100535 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200536 break;
537 default:
538 BUG();
539 }
540out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200541 return q->first_to_check;
542}
543
Jan Glauber60b5df22009-06-22 12:08:10 +0200544static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200545{
546 int bufnr;
547
548 bufnr = get_inbound_buffer_frontier(q);
549
Jan Glaubere85dea02009-03-26 15:24:29 +0100550 if ((bufnr != q->last_move) || q->qdio_error) {
551 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100552 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber3a601bf2010-05-17 10:00:17 +0200553 q->u.in.timestamp = get_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200554 return 1;
555 } else
556 return 0;
557}
558
Jan Glauber9a2c1602009-06-22 12:08:11 +0200559static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200560{
561 unsigned char state = 0;
562
563 if (!atomic_read(&q->nr_buf_used))
564 return 1;
565
Jan Glauber90adac52011-01-05 12:47:54 +0100566 if (need_siga_sync(q))
567 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200568 get_buf_state(q, q->first_to_check, &state, 0);
569
Ursula Braun4c522282010-02-09 09:46:07 +0100570 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200571 /* more work coming */
572 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200573
574 if (is_thinint_irq(q->irq_ptr))
575 return 1;
576
577 /* don't poll under z/VM */
578 if (MACHINE_IS_VM)
579 return 1;
580
581 /*
582 * At this point we know, that inbound first_to_check
583 * has (probably) not moved (see qdio_inbound_processing).
584 */
Jan Glauber3a601bf2010-05-17 10:00:17 +0200585 if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200586 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200587 q->first_to_check);
588 return 1;
589 } else
590 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200591}
592
593static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200594{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100595 int start = q->first_to_kick;
596 int end = q->first_to_check;
597 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200598
599 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
600 return;
601
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100602 count = sub_buf(end, start);
603
604 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100605 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200606 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100607 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100608 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200609 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
610 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100611 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100612
613 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
614 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200615
616 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100617 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200618 q->qdio_error = 0;
619}
620
621static void __qdio_inbound_processing(struct qdio_q *q)
622{
Jan Glauber6486cda2010-01-04 09:05:42 +0100623 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200624
Jan Glauber779e6e12008-07-17 17:16:48 +0200625 if (!qdio_inbound_q_moved(q))
626 return;
627
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100628 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200629
Jan Glauber6486cda2010-01-04 09:05:42 +0100630 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200631 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100632 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200633 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
634 tasklet_schedule(&q->tasklet);
635 return;
636 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100637 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200638
639 qdio_stop_polling(q);
640 /*
641 * We need to check again to not lose initiative after
642 * resetting the ACK state.
643 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100644 if (!qdio_inbound_q_done(q)) {
645 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200646 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
647 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100648 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200649}
650
Jan Glauber779e6e12008-07-17 17:16:48 +0200651void qdio_inbound_processing(unsigned long data)
652{
653 struct qdio_q *q = (struct qdio_q *)data;
654 __qdio_inbound_processing(q);
655}
656
657static int get_outbound_buffer_frontier(struct qdio_q *q)
658{
659 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100660 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200661
Jan Glauber90adac52011-01-05 12:47:54 +0100662 if (need_siga_sync(q))
663 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
664 !pci_out_supported(q)) ||
665 (queue_type(q) == QDIO_IQDIO_QFMT &&
666 multicast_outbound(q)))
667 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200668
669 /*
670 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
671 * would return 0.
672 */
673 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
674 stop = add_buf(q->first_to_check, count);
675
Jan Glauber779e6e12008-07-17 17:16:48 +0200676 if (q->first_to_check == stop)
677 return q->first_to_check;
678
Jan Glauber50f769d2008-12-25 13:38:47 +0100679 count = get_buf_states(q, q->first_to_check, &state, count, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200680 if (!count)
681 return q->first_to_check;
682
683 switch (state) {
684 case SLSB_P_OUTPUT_EMPTY:
685 /* the adapter got it */
Jan Glauber1d7e1502009-09-22 22:58:39 +0200686 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200687
688 atomic_sub(count, &q->nr_buf_used);
689 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100690 if (q->irq_ptr->perf_stat_enabled)
691 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200692 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200693 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200694 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200695 q->first_to_check = add_buf(q->first_to_check, count);
696 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100697 if (q->irq_ptr->perf_stat_enabled)
698 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200699 break;
700 case SLSB_CU_OUTPUT_PRIMED:
701 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100702 if (q->irq_ptr->perf_stat_enabled)
703 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100704 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200705 break;
706 case SLSB_P_OUTPUT_NOT_INIT:
707 case SLSB_P_OUTPUT_HALTED:
708 break;
709 default:
710 BUG();
711 }
712 return q->first_to_check;
713}
714
715/* all buffers processed? */
716static inline int qdio_outbound_q_done(struct qdio_q *q)
717{
718 return atomic_read(&q->nr_buf_used) == 0;
719}
720
721static inline int qdio_outbound_q_moved(struct qdio_q *q)
722{
723 int bufnr;
724
725 bufnr = get_outbound_buffer_frontier(q);
726
Jan Glaubere85dea02009-03-26 15:24:29 +0100727 if ((bufnr != q->last_move) || q->qdio_error) {
728 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100729 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200730 return 1;
731 } else
732 return 0;
733}
734
Jan Glauberd303b6f2009-03-26 15:24:31 +0100735static int qdio_kick_outbound_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200736{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200737 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100738 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200739
740 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100741 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200742
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100743 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200744retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100745 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100746
747 cc = qdio_siga_output(q, &busy_bit);
748 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200749 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200750 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100751 case 2:
752 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200753 while (++retries < QDIO_BUSY_BIT_RETRIES) {
754 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
755 goto retry;
756 }
757 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauberd303b6f2009-03-26 15:24:31 +0100758 cc |= QDIO_ERROR_SIGA_BUSY;
759 } else
760 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100761 break;
762 case 1:
763 case 3:
764 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100765 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200766 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200767 if (retries) {
768 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
769 DBF_ERROR("count:%u", retries);
770 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100771 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200772}
773
Jan Glauber779e6e12008-07-17 17:16:48 +0200774static void __qdio_outbound_processing(struct qdio_q *q)
775{
Jan Glauber6486cda2010-01-04 09:05:42 +0100776 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200777 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
778
779 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100780 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200781
Jan Glauberc38f9602009-03-26 15:24:26 +0100782 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200783 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100784 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200785
786 /* bail out for HiperSockets unicast queues */
787 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
788 return;
789
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200790 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
Jan Glauberc38f9602009-03-26 15:24:26 +0100791 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
792 goto sched;
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200793
Jan Glauber779e6e12008-07-17 17:16:48 +0200794 if (q->u.out.pci_out_enabled)
795 return;
796
797 /*
798 * Now we know that queue type is either qeth without pci enabled
799 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
800 * EMPTY is noticed and outbound_handler is called after some time.
801 */
802 if (qdio_outbound_q_done(q))
803 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100804 else
805 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200806 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100807 return;
808
809sched:
810 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
811 return;
812 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200813}
814
815/* outbound tasklet */
816void qdio_outbound_processing(unsigned long data)
817{
818 struct qdio_q *q = (struct qdio_q *)data;
819 __qdio_outbound_processing(q);
820}
821
822void qdio_outbound_timer(unsigned long data)
823{
824 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100825
826 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
827 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200828 tasklet_schedule(&q->tasklet);
829}
830
Jan Glauber60b5df22009-06-22 12:08:10 +0200831static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200832{
833 struct qdio_q *out;
834 int i;
835
836 if (!pci_out_supported(q))
837 return;
838
839 for_each_output_queue(q->irq_ptr, out, i)
840 if (!qdio_outbound_q_done(out))
841 tasklet_schedule(&out->tasklet);
842}
843
Jan Glauber60b5df22009-06-22 12:08:10 +0200844static void __tiqdio_inbound_processing(struct qdio_q *q)
845{
Jan Glauber6486cda2010-01-04 09:05:42 +0100846 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100847 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
848 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200849
850 /*
851 * The interrupt could be caused by a PCI request. Check the
852 * PCI capable outbound queues.
853 */
854 qdio_check_outbound_after_thinint(q);
855
856 if (!qdio_inbound_q_moved(q))
857 return;
858
859 qdio_kick_handler(q);
860
Jan Glauber9a2c1602009-06-22 12:08:11 +0200861 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100862 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200863 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200864 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200865 return;
866 }
Jan Glauber60b5df22009-06-22 12:08:10 +0200867 }
868
869 qdio_stop_polling(q);
870 /*
871 * We need to check again to not lose initiative after
872 * resetting the ACK state.
873 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200874 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100875 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +0200876 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
877 tasklet_schedule(&q->tasklet);
878 }
879}
880
881void tiqdio_inbound_processing(unsigned long data)
882{
883 struct qdio_q *q = (struct qdio_q *)data;
884 __tiqdio_inbound_processing(q);
885}
886
Jan Glauber779e6e12008-07-17 17:16:48 +0200887static inline void qdio_set_state(struct qdio_irq *irq_ptr,
888 enum qdio_irq_states state)
889{
Jan Glauber22f99342008-12-25 13:38:46 +0100890 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200891
892 irq_ptr->state = state;
893 mb();
894}
895
Jan Glauber22f99342008-12-25 13:38:46 +0100896static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200897{
Jan Glauber779e6e12008-07-17 17:16:48 +0200898 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100899 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
900 DBF_ERROR_HEX(irb, 64);
901 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200902 }
903}
904
905/* PCI interrupt handler */
906static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
907{
908 int i;
909 struct qdio_q *q;
910
Jan Glauberc38f9602009-03-26 15:24:26 +0100911 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
912 return;
913
Jan Glauberd36deae2010-09-07 21:14:39 +0000914 for_each_input_queue(irq_ptr, q, i) {
915 if (q->u.in.queue_start_poll) {
916 /* skip if polling is enabled or already in work */
917 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
918 &q->u.in.queue_irq_state)) {
919 qperf_inc(q, int_discarded);
920 continue;
921 }
922 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
923 q->irq_ptr->int_parm);
924 } else
925 tasklet_schedule(&q->tasklet);
926 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200927
Jan Glauber90adac52011-01-05 12:47:54 +0100928 if (!pci_out_supported(q))
Jan Glauber779e6e12008-07-17 17:16:48 +0200929 return;
930
931 for_each_output_queue(irq_ptr, q, i) {
932 if (qdio_outbound_q_done(q))
933 continue;
Jan Glauber90adac52011-01-05 12:47:54 +0100934 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +0200935 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200936 tasklet_schedule(&q->tasklet);
937 }
938}
939
940static void qdio_handle_activate_check(struct ccw_device *cdev,
941 unsigned long intparm, int cstat, int dstat)
942{
943 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
944 struct qdio_q *q;
Jan Glauber779e6e12008-07-17 17:16:48 +0200945
Jan Glauber22f99342008-12-25 13:38:46 +0100946 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
947 DBF_ERROR("intp :%lx", intparm);
948 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +0200949
950 if (irq_ptr->nr_input_qs) {
951 q = irq_ptr->input_qs[0];
952 } else if (irq_ptr->nr_output_qs) {
953 q = irq_ptr->output_qs[0];
954 } else {
955 dump_stack();
956 goto no_handler;
957 }
958 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
959 0, -1, -1, irq_ptr->int_parm);
960no_handler:
961 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
962}
963
Jan Glauber779e6e12008-07-17 17:16:48 +0200964static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
965 int dstat)
966{
967 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +0200968
Jan Glauber22f99342008-12-25 13:38:46 +0100969 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +0200970
971 if (cstat)
972 goto error;
973 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
974 goto error;
975 if (!(dstat & DEV_STAT_DEV_END))
976 goto error;
977 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
978 return;
979
980error:
981 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
982 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
983 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +0200984}
985
986/* qdio interrupt handler */
987void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
988 struct irb *irb)
989{
990 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
991 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +0200992
Jan Glauber779e6e12008-07-17 17:16:48 +0200993 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +0100994 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +0200995 return;
996 }
997
Jan Glauber30d77c32011-01-05 12:47:29 +0100998 kstat_cpu(smp_processor_id()).irqs[IOINT_QDI]++;
Jan Glauber09a308f2010-05-17 10:00:14 +0200999 if (irq_ptr->perf_stat_enabled)
1000 irq_ptr->perf_stat.qdio_int++;
1001
Jan Glauber779e6e12008-07-17 17:16:48 +02001002 if (IS_ERR(irb)) {
1003 switch (PTR_ERR(irb)) {
1004 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +01001005 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +02001006 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1007 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001008 return;
1009 default:
1010 WARN_ON(1);
1011 return;
1012 }
1013 }
Jan Glauber22f99342008-12-25 13:38:46 +01001014 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001015 cstat = irb->scsw.cmd.cstat;
1016 dstat = irb->scsw.cmd.dstat;
1017
1018 switch (irq_ptr->state) {
1019 case QDIO_IRQ_STATE_INACTIVE:
1020 qdio_establish_handle_irq(cdev, cstat, dstat);
1021 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001022 case QDIO_IRQ_STATE_CLEANUP:
1023 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1024 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001025 case QDIO_IRQ_STATE_ESTABLISHED:
1026 case QDIO_IRQ_STATE_ACTIVE:
1027 if (cstat & SCHN_STAT_PCI) {
1028 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001029 return;
1030 }
Jan Glauber4c575422009-06-12 10:26:28 +02001031 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001032 qdio_handle_activate_check(cdev, intparm, cstat,
1033 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001034 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001035 case QDIO_IRQ_STATE_STOPPED:
1036 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001037 default:
1038 WARN_ON(1);
1039 }
1040 wake_up(&cdev->private->wait_q);
1041}
1042
1043/**
1044 * qdio_get_ssqd_desc - get qdio subchannel description
1045 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001046 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001047 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001048 * Returns 0 or an error code. The results of the chsc are stored in the
1049 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001050 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001051int qdio_get_ssqd_desc(struct ccw_device *cdev,
1052 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001053{
Jan Glauber779e6e12008-07-17 17:16:48 +02001054
Jan Glauberbbd50e12008-12-25 13:38:43 +01001055 if (!cdev || !cdev->private)
1056 return -EINVAL;
1057
Jan Glauber22f99342008-12-25 13:38:46 +01001058 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001059 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001060}
1061EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1062
Jan Glauber779e6e12008-07-17 17:16:48 +02001063static void qdio_shutdown_queues(struct ccw_device *cdev)
1064{
1065 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1066 struct qdio_q *q;
1067 int i;
1068
1069 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001070 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001071
1072 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001073 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001074 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001075 }
1076}
1077
1078/**
1079 * qdio_shutdown - shut down a qdio subchannel
1080 * @cdev: associated ccw device
1081 * @how: use halt or clear to shutdown
1082 */
1083int qdio_shutdown(struct ccw_device *cdev, int how)
1084{
Jan Glauber22f99342008-12-25 13:38:46 +01001085 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001086 int rc;
1087 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001088
Jan Glauber779e6e12008-07-17 17:16:48 +02001089 if (!irq_ptr)
1090 return -ENODEV;
1091
Jan Glauberb4547402009-03-26 15:24:24 +01001092 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001093 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1094
Jan Glauber779e6e12008-07-17 17:16:48 +02001095 mutex_lock(&irq_ptr->setup_mutex);
1096 /*
1097 * Subchannel was already shot down. We cannot prevent being called
1098 * twice since cio may trigger a shutdown asynchronously.
1099 */
1100 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1101 mutex_unlock(&irq_ptr->setup_mutex);
1102 return 0;
1103 }
1104
Jan Glauberc38f9602009-03-26 15:24:26 +01001105 /*
1106 * Indicate that the device is going down. Scheduling the queue
1107 * tasklets is forbidden from here on.
1108 */
1109 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1110
Jan Glauber779e6e12008-07-17 17:16:48 +02001111 tiqdio_remove_input_queues(irq_ptr);
1112 qdio_shutdown_queues(cdev);
1113 qdio_shutdown_debug_entries(irq_ptr, cdev);
1114
1115 /* cleanup subchannel */
1116 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1117
1118 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1119 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1120 else
1121 /* default behaviour is halt */
1122 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1123 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001124 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1125 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001126 goto no_cleanup;
1127 }
1128
1129 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1130 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1131 wait_event_interruptible_timeout(cdev->private->wait_q,
1132 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1133 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1134 10 * HZ);
1135 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1136
1137no_cleanup:
1138 qdio_shutdown_thinint(irq_ptr);
1139
1140 /* restore interrupt handler */
1141 if ((void *)cdev->handler == (void *)qdio_int_handler)
1142 cdev->handler = irq_ptr->orig_handler;
1143 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1144
1145 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1146 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001147 if (rc)
1148 return rc;
1149 return 0;
1150}
1151EXPORT_SYMBOL_GPL(qdio_shutdown);
1152
1153/**
1154 * qdio_free - free data structures for a qdio subchannel
1155 * @cdev: associated ccw device
1156 */
1157int qdio_free(struct ccw_device *cdev)
1158{
Jan Glauber22f99342008-12-25 13:38:46 +01001159 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001160
Jan Glauber779e6e12008-07-17 17:16:48 +02001161 if (!irq_ptr)
1162 return -ENODEV;
1163
Jan Glauber22f99342008-12-25 13:38:46 +01001164 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001165 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001166
1167 if (irq_ptr->debug_area != NULL) {
1168 debug_unregister(irq_ptr->debug_area);
1169 irq_ptr->debug_area = NULL;
1170 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001171 cdev->private->qdio_data = NULL;
1172 mutex_unlock(&irq_ptr->setup_mutex);
1173
1174 qdio_release_memory(irq_ptr);
1175 return 0;
1176}
1177EXPORT_SYMBOL_GPL(qdio_free);
1178
1179/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001180 * qdio_allocate - allocate qdio queues and associated data
1181 * @init_data: initialization data
1182 */
1183int qdio_allocate(struct qdio_initialize *init_data)
1184{
1185 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001186
Jan Glauber22f99342008-12-25 13:38:46 +01001187 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001188
1189 if ((init_data->no_input_qs && !init_data->input_handler) ||
1190 (init_data->no_output_qs && !init_data->output_handler))
1191 return -EINVAL;
1192
1193 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1194 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1195 return -EINVAL;
1196
1197 if ((!init_data->input_sbal_addr_array) ||
1198 (!init_data->output_sbal_addr_array))
1199 return -EINVAL;
1200
Jan Glauber779e6e12008-07-17 17:16:48 +02001201 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1202 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1203 if (!irq_ptr)
1204 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001205
1206 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001207 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001208
1209 /*
1210 * Allocate a page for the chsc calls in qdio_establish.
1211 * Must be pre-allocated since a zfcp recovery will call
1212 * qdio_establish. In case of low memory and swap on a zfcp disk
1213 * we may not be able to allocate memory otherwise.
1214 */
1215 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1216 if (!irq_ptr->chsc_page)
1217 goto out_rel;
1218
1219 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001220 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001221 if (!irq_ptr->qdr)
1222 goto out_rel;
1223 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1224
Jan Glauber779e6e12008-07-17 17:16:48 +02001225 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1226 init_data->no_output_qs))
1227 goto out_rel;
1228
1229 init_data->cdev->private->qdio_data = irq_ptr;
1230 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1231 return 0;
1232out_rel:
1233 qdio_release_memory(irq_ptr);
1234out_err:
1235 return -ENOMEM;
1236}
1237EXPORT_SYMBOL_GPL(qdio_allocate);
1238
1239/**
1240 * qdio_establish - establish queues on a qdio subchannel
1241 * @init_data: initialization data
1242 */
1243int qdio_establish(struct qdio_initialize *init_data)
1244{
Jan Glauber779e6e12008-07-17 17:16:48 +02001245 struct qdio_irq *irq_ptr;
1246 struct ccw_device *cdev = init_data->cdev;
1247 unsigned long saveflags;
1248 int rc;
1249
Jan Glauber22f99342008-12-25 13:38:46 +01001250 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001251
Jan Glauber779e6e12008-07-17 17:16:48 +02001252 irq_ptr = cdev->private->qdio_data;
1253 if (!irq_ptr)
1254 return -ENODEV;
1255
1256 if (cdev->private->state != DEV_STATE_ONLINE)
1257 return -EINVAL;
1258
Jan Glauber779e6e12008-07-17 17:16:48 +02001259 mutex_lock(&irq_ptr->setup_mutex);
1260 qdio_setup_irq(init_data);
1261
1262 rc = qdio_establish_thinint(irq_ptr);
1263 if (rc) {
1264 mutex_unlock(&irq_ptr->setup_mutex);
1265 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1266 return rc;
1267 }
1268
1269 /* establish q */
1270 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1271 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1272 irq_ptr->ccw.count = irq_ptr->equeue.count;
1273 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1274
1275 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1276 ccw_device_set_options_mask(cdev, 0);
1277
1278 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1279 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001280 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1281 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001282 }
1283 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1284
1285 if (rc) {
1286 mutex_unlock(&irq_ptr->setup_mutex);
1287 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1288 return rc;
1289 }
1290
1291 wait_event_interruptible_timeout(cdev->private->wait_q,
1292 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1293 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1294
1295 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1296 mutex_unlock(&irq_ptr->setup_mutex);
1297 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1298 return -EIO;
1299 }
1300
1301 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber22f99342008-12-25 13:38:46 +01001302 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
Jan Glauber779e6e12008-07-17 17:16:48 +02001303
1304 /* qebsm is now setup if available, initialize buffer states */
1305 qdio_init_buf_states(irq_ptr);
1306
1307 mutex_unlock(&irq_ptr->setup_mutex);
1308 qdio_print_subchannel_info(irq_ptr, cdev);
1309 qdio_setup_debug_entries(irq_ptr, cdev);
1310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(qdio_establish);
1313
1314/**
1315 * qdio_activate - activate queues on a qdio subchannel
1316 * @cdev: associated cdev
1317 */
1318int qdio_activate(struct ccw_device *cdev)
1319{
1320 struct qdio_irq *irq_ptr;
1321 int rc;
1322 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001323
Jan Glauber22f99342008-12-25 13:38:46 +01001324 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001325
Jan Glauber779e6e12008-07-17 17:16:48 +02001326 irq_ptr = cdev->private->qdio_data;
1327 if (!irq_ptr)
1328 return -ENODEV;
1329
1330 if (cdev->private->state != DEV_STATE_ONLINE)
1331 return -EINVAL;
1332
1333 mutex_lock(&irq_ptr->setup_mutex);
1334 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1335 rc = -EBUSY;
1336 goto out;
1337 }
1338
Jan Glauber779e6e12008-07-17 17:16:48 +02001339 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1340 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1341 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1342 irq_ptr->ccw.cda = 0;
1343
1344 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1345 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1346
1347 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1348 0, DOIO_DENY_PREFETCH);
1349 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001350 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1351 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001352 }
1353 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1354
1355 if (rc)
1356 goto out;
1357
1358 if (is_thinint_irq(irq_ptr))
1359 tiqdio_add_input_queues(irq_ptr);
1360
1361 /* wait for subchannel to become active */
1362 msleep(5);
1363
1364 switch (irq_ptr->state) {
1365 case QDIO_IRQ_STATE_STOPPED:
1366 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001367 rc = -EIO;
1368 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001369 default:
1370 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1371 rc = 0;
1372 }
1373out:
1374 mutex_unlock(&irq_ptr->setup_mutex);
1375 return rc;
1376}
1377EXPORT_SYMBOL_GPL(qdio_activate);
1378
1379static inline int buf_in_between(int bufnr, int start, int count)
1380{
1381 int end = add_buf(start, count);
1382
1383 if (end > start) {
1384 if (bufnr >= start && bufnr < end)
1385 return 1;
1386 else
1387 return 0;
1388 }
1389
1390 /* wrap-around case */
1391 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1392 (bufnr < end))
1393 return 1;
1394 else
1395 return 0;
1396}
1397
1398/**
1399 * handle_inbound - reset processed input buffers
1400 * @q: queue containing the buffers
1401 * @callflags: flags
1402 * @bufnr: first buffer to process
1403 * @count: how many buffers are emptied
1404 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001405static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1406 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001407{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001408 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001409
Jan Glauber6486cda2010-01-04 09:05:42 +01001410 qperf_inc(q, inbound_call);
1411
Jan Glauber50f769d2008-12-25 13:38:47 +01001412 if (!q->u.in.polling)
1413 goto set;
1414
1415 /* protect against stop polling setting an ACK for an emptied slsb */
1416 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1417 /* overwriting everything, just delete polling status */
1418 q->u.in.polling = 0;
1419 q->u.in.ack_count = 0;
1420 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001421 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001422 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001423 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001424 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001425 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001426 q->u.in.ack_count -= diff;
1427 if (q->u.in.ack_count <= 0) {
1428 q->u.in.polling = 0;
1429 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001430 goto set;
1431 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001432 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001433 }
1434 else
1435 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001436 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001437 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001438
Jan Glauber50f769d2008-12-25 13:38:47 +01001439set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001440 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001441
1442 used = atomic_add_return(count, &q->nr_buf_used) - count;
1443 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1444
1445 /* no need to signal as long as the adapter had free buffers */
1446 if (used)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001447 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001448
Jan Glauberd303b6f2009-03-26 15:24:31 +01001449 if (need_siga_in(q))
1450 return qdio_siga_input(q);
1451 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001452}
1453
1454/**
1455 * handle_outbound - process filled outbound buffers
1456 * @q: queue containing the buffers
1457 * @callflags: flags
1458 * @bufnr: first buffer to process
1459 * @count: how many buffers are filled
1460 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001461static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1462 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001463{
Jan Glauberc26001d2011-05-23 10:24:38 +02001464 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001465 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001466
Jan Glauber6486cda2010-01-04 09:05:42 +01001467 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001468
1469 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1470 used = atomic_add_return(count, &q->nr_buf_used);
1471 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1472
Jan Glauber01958432011-01-05 12:47:51 +01001473 if (used == QDIO_MAX_BUFFERS_PER_Q)
1474 qperf_inc(q, outbound_queue_full);
1475
Jan Glauber6486cda2010-01-04 09:05:42 +01001476 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001477 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001478 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001479 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001480 q->u.out.pci_out_enabled = 0;
1481
1482 if (queue_type(q) == QDIO_IQDIO_QFMT) {
Jan Glauber110da312011-01-05 12:47:53 +01001483 /* One SIGA-W per buffer required for unicast HiperSockets. */
1484 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1485
1486 rc = qdio_kick_outbound_q(q);
Jan Glauber90adac52011-01-05 12:47:54 +01001487 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001488 rc = qdio_siga_sync_q(q);
1489 } else {
1490 /* try to fast requeue buffers */
1491 get_buf_state(q, prev_buf(bufnr), &state, 0);
1492 if (state != SLSB_CU_OUTPUT_PRIMED)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001493 rc = qdio_kick_outbound_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001494 else
Jan Glauber110da312011-01-05 12:47:53 +01001495 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001496 }
1497
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001498 /* in case of SIGA errors we must process the error immediately */
1499 if (used >= q->u.out.scan_threshold || rc)
1500 tasklet_schedule(&q->tasklet);
1501 else
1502 /* free the SBALs in case of no further traffic */
1503 if (!timer_pending(&q->u.out.timer))
1504 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001505 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001506}
1507
1508/**
1509 * do_QDIO - process input or output buffers
1510 * @cdev: associated ccw_device for the qdio subchannel
1511 * @callflags: input or output and special flags from the program
1512 * @q_nr: queue number
1513 * @bufnr: buffer number
1514 * @count: how many buffers to process
1515 */
1516int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001517 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001518{
1519 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001520
Jan Glauber66182412009-06-22 12:08:15 +02001521 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001522 return -EINVAL;
1523
Jan Glauber779e6e12008-07-17 17:16:48 +02001524 irq_ptr = cdev->private->qdio_data;
1525 if (!irq_ptr)
1526 return -ENODEV;
1527
Jan Glauber1d7e1502009-09-22 22:58:39 +02001528 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1529 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001530
1531 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1532 return -EBUSY;
Jan Glauber9a265132011-03-23 10:16:01 +01001533 if (!count)
1534 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001535 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001536 return handle_inbound(irq_ptr->input_qs[q_nr],
1537 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001538 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001539 return handle_outbound(irq_ptr->output_qs[q_nr],
1540 callflags, bufnr, count);
1541 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001542}
1543EXPORT_SYMBOL_GPL(do_QDIO);
1544
Jan Glauberd36deae2010-09-07 21:14:39 +00001545/**
1546 * qdio_start_irq - process input buffers
1547 * @cdev: associated ccw_device for the qdio subchannel
1548 * @nr: input queue number
1549 *
1550 * Return codes
1551 * 0 - success
1552 * 1 - irqs not started since new data is available
1553 */
1554int qdio_start_irq(struct ccw_device *cdev, int nr)
1555{
1556 struct qdio_q *q;
1557 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1558
1559 if (!irq_ptr)
1560 return -ENODEV;
1561 q = irq_ptr->input_qs[nr];
1562
1563 WARN_ON(queue_irqs_enabled(q));
1564
Jan Glauber4f325182011-01-05 12:47:49 +01001565 if (!shared_ind(q->irq_ptr->dsci))
Jan Glauberd36deae2010-09-07 21:14:39 +00001566 xchg(q->irq_ptr->dsci, 0);
1567
1568 qdio_stop_polling(q);
1569 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1570
1571 /*
1572 * We need to check again to not lose initiative after
1573 * resetting the ACK state.
1574 */
Jan Glauber4f325182011-01-05 12:47:49 +01001575 if (!shared_ind(q->irq_ptr->dsci) && *q->irq_ptr->dsci)
Jan Glauberd36deae2010-09-07 21:14:39 +00001576 goto rescan;
1577 if (!qdio_inbound_q_done(q))
1578 goto rescan;
1579 return 0;
1580
1581rescan:
1582 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1583 &q->u.in.queue_irq_state))
1584 return 0;
1585 else
1586 return 1;
1587
1588}
1589EXPORT_SYMBOL(qdio_start_irq);
1590
1591/**
1592 * qdio_get_next_buffers - process input buffers
1593 * @cdev: associated ccw_device for the qdio subchannel
1594 * @nr: input queue number
1595 * @bufnr: first filled buffer number
1596 * @error: buffers are in error state
1597 *
1598 * Return codes
1599 * < 0 - error
1600 * = 0 - no new buffers found
1601 * > 0 - number of processed buffers
1602 */
1603int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1604 int *error)
1605{
1606 struct qdio_q *q;
1607 int start, end;
1608 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1609
1610 if (!irq_ptr)
1611 return -ENODEV;
1612 q = irq_ptr->input_qs[nr];
1613 WARN_ON(queue_irqs_enabled(q));
1614
Jan Glauberd36deae2010-09-07 21:14:39 +00001615 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001616 * Cannot rely on automatic sync after interrupt since queues may
1617 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001618 */
Jan Glauber90adac52011-01-05 12:47:54 +01001619 if (need_siga_sync(q))
1620 qdio_sync_queues(q);
1621
1622 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001623 qdio_check_outbound_after_thinint(q);
1624
1625 if (!qdio_inbound_q_moved(q))
1626 return 0;
1627
1628 /* Note: upper-layer MUST stop processing immediately here ... */
1629 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1630 return -EIO;
1631
1632 start = q->first_to_kick;
1633 end = q->first_to_check;
1634 *bufnr = start;
1635 *error = q->qdio_error;
1636
1637 /* for the next time */
1638 q->first_to_kick = end;
1639 q->qdio_error = 0;
1640 return sub_buf(end, start);
1641}
1642EXPORT_SYMBOL(qdio_get_next_buffers);
1643
1644/**
1645 * qdio_stop_irq - disable interrupt processing for the device
1646 * @cdev: associated ccw_device for the qdio subchannel
1647 * @nr: input queue number
1648 *
1649 * Return codes
1650 * 0 - interrupts were already disabled
1651 * 1 - interrupts successfully disabled
1652 */
1653int qdio_stop_irq(struct ccw_device *cdev, int nr)
1654{
1655 struct qdio_q *q;
1656 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1657
1658 if (!irq_ptr)
1659 return -ENODEV;
1660 q = irq_ptr->input_qs[nr];
1661
1662 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1663 &q->u.in.queue_irq_state))
1664 return 0;
1665 else
1666 return 1;
1667}
1668EXPORT_SYMBOL(qdio_stop_irq);
1669
Jan Glauber779e6e12008-07-17 17:16:48 +02001670static int __init init_QDIO(void)
1671{
1672 int rc;
1673
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001674 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001675 if (rc)
1676 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001677 rc = qdio_setup_init();
1678 if (rc)
1679 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001680 rc = tiqdio_allocate_memory();
1681 if (rc)
1682 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001683 rc = tiqdio_register_thinints();
1684 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001685 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001686 return 0;
1687
Jan Glauber779e6e12008-07-17 17:16:48 +02001688out_ti:
1689 tiqdio_free_memory();
1690out_cache:
1691 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001692out_debug:
1693 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001694 return rc;
1695}
1696
1697static void __exit exit_QDIO(void)
1698{
1699 tiqdio_unregister_thinints();
1700 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001701 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001702 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001703}
1704
1705module_init(init_QDIO);
1706module_exit(exit_QDIO);