blob: 4f8f7431177867f3657c54c18fdce2e1127ce0d4 [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>
16#include <asm/atomic.h>
17#include <asm/debug.h>
18#include <asm/qdio.h>
19
20#include "cio.h"
21#include "css.h"
22#include "device.h"
23#include "qdio.h"
24#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020025
26MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
27 "Jan Glauber <jang@linux.vnet.ibm.com>");
28MODULE_DESCRIPTION("QDIO base support");
29MODULE_LICENSE("GPL");
30
31static inline int do_siga_sync(struct subchannel_id schid,
32 unsigned int out_mask, unsigned int in_mask)
33{
34 register unsigned long __fc asm ("0") = 2;
35 register struct subchannel_id __schid asm ("1") = schid;
36 register unsigned long out asm ("2") = out_mask;
37 register unsigned long in asm ("3") = in_mask;
38 int cc;
39
40 asm volatile(
41 " siga 0\n"
42 " ipm %0\n"
43 " srl %0,28\n"
44 : "=d" (cc)
45 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
46 return cc;
47}
48
49static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
50{
51 register unsigned long __fc asm ("0") = 1;
52 register struct subchannel_id __schid asm ("1") = schid;
53 register unsigned long __mask asm ("2") = mask;
54 int cc;
55
56 asm volatile(
57 " siga 0\n"
58 " ipm %0\n"
59 " srl %0,28\n"
60 : "=d" (cc)
61 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
62 return cc;
63}
64
65/**
66 * do_siga_output - perform SIGA-w/wt function
67 * @schid: subchannel id or in case of QEBSM the subchannel token
68 * @mask: which output queues to process
69 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
70 * @fc: function code to perform
71 *
72 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
73 * Note: For IQDC unicast queues only the highest priority queue is processed.
74 */
75static inline int do_siga_output(unsigned long schid, unsigned long mask,
Jan Glauber7a0b4cb2008-12-25 13:38:48 +010076 unsigned int *bb, unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020077{
78 register unsigned long __fc asm("0") = fc;
79 register unsigned long __schid asm("1") = schid;
80 register unsigned long __mask asm("2") = mask;
81 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
82
83 asm volatile(
84 " siga 0\n"
85 "0: ipm %0\n"
86 " srl %0,28\n"
87 "1:\n"
88 EX_TABLE(0b, 1b)
89 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
90 : : "cc", "memory");
91 *bb = ((unsigned int) __fc) >> 31;
92 return cc;
93}
94
95static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
96{
Jan Glauber779e6e12008-07-17 17:16:48 +020097 /* all done or next buffer state different */
98 if (ccq == 0 || ccq == 32)
99 return 0;
100 /* not all buffers processed */
101 if (ccq == 96 || ccq == 97)
102 return 1;
103 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100104 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200105 return -EIO;
106}
107
108/**
109 * qdio_do_eqbs - extract buffer states for QEBSM
110 * @q: queue to manipulate
111 * @state: state of the extracted buffers
112 * @start: buffer number to start at
113 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100114 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200115 *
Coly Li73ac36e2009-01-07 18:09:16 -0800116 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200117 * Stops processing if a state is different from the last buffers state.
118 */
119static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100120 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200121{
122 unsigned int ccq = 0;
123 int tmp_count = count, tmp_start = start;
124 int nr = q->nr;
125 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200126
127 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100128 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200129
130 if (!q->is_input_q)
131 nr += q->irq_ptr->nr_input_qs;
132again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100133 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
134 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200135 rc = qdio_check_ccq(q, ccq);
136
137 /* At least one buffer was processed, return and extract the remaining
138 * buffers later.
139 */
Jan Glauber23589d02008-12-25 13:38:44 +0100140 if ((ccq == 96) && (count != tmp_count)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100141 qperf_inc(q, eqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200142 return (count - tmp_count);
Jan Glauber23589d02008-12-25 13:38:44 +0100143 }
Jan Glauber22f99342008-12-25 13:38:46 +0100144
Jan Glauber779e6e12008-07-17 17:16:48 +0200145 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100146 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200147 goto again;
148 }
149
150 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100151 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
152 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200153 q->handler(q->irq_ptr->cdev,
154 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
155 0, -1, -1, q->irq_ptr->int_parm);
156 return 0;
157 }
158 return count - tmp_count;
159}
160
161/**
162 * qdio_do_sqbs - set buffer states for QEBSM
163 * @q: queue to manipulate
164 * @state: new state of the buffers
165 * @start: first buffer number to change
166 * @count: how many buffers to change
167 *
168 * Returns the number of successfully changed buffers.
169 * Does retrying until the specified count of buffer states is set or an
170 * error occurs.
171 */
172static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
173 int count)
174{
175 unsigned int ccq = 0;
176 int tmp_count = count, tmp_start = start;
177 int nr = q->nr;
178 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200179
Jan Glauber50f769d2008-12-25 13:38:47 +0100180 if (!count)
181 return 0;
182
Jan Glauber779e6e12008-07-17 17:16:48 +0200183 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100184 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200185
186 if (!q->is_input_q)
187 nr += q->irq_ptr->nr_input_qs;
188again:
189 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
190 rc = qdio_check_ccq(q, ccq);
191 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100192 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100193 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200194 goto again;
195 }
196 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100197 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
198 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200199 q->handler(q->irq_ptr->cdev,
200 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
201 0, -1, -1, q->irq_ptr->int_parm);
202 return 0;
203 }
204 WARN_ON(tmp_count);
205 return count - tmp_count;
206}
207
208/* returns number of examined buffers and their common state in *state */
209static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100210 unsigned char *state, unsigned int count,
211 int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200212{
213 unsigned char __state = 0;
214 int i;
215
216 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
217 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
218
219 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100220 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200221
222 for (i = 0; i < count; i++) {
223 if (!__state)
224 __state = q->slsb.val[bufnr];
225 else if (q->slsb.val[bufnr] != __state)
226 break;
227 bufnr = next_buf(bufnr);
228 }
229 *state = __state;
230 return i;
231}
232
Jan Glauber60b5df22009-06-22 12:08:10 +0200233static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
234 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200235{
Jan Glauber50f769d2008-12-25 13:38:47 +0100236 return get_buf_states(q, bufnr, state, 1, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200237}
238
239/* wrap-around safe setting of slsb states, returns number of changed buffers */
240static inline int set_buf_states(struct qdio_q *q, int bufnr,
241 unsigned char state, int count)
242{
243 int i;
244
245 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
246 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
247
248 if (is_qebsm(q))
249 return qdio_do_sqbs(q, state, bufnr, count);
250
251 for (i = 0; i < count; i++) {
252 xchg(&q->slsb.val[bufnr], state);
253 bufnr = next_buf(bufnr);
254 }
255 return count;
256}
257
258static inline int set_buf_state(struct qdio_q *q, int bufnr,
259 unsigned char state)
260{
261 return set_buf_states(q, bufnr, state, 1);
262}
263
264/* set slsb states to initial state */
265void qdio_init_buf_states(struct qdio_irq *irq_ptr)
266{
267 struct qdio_q *q;
268 int i;
269
270 for_each_input_queue(irq_ptr, q, i)
271 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
272 QDIO_MAX_BUFFERS_PER_Q);
273 for_each_output_queue(irq_ptr, q, i)
274 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
275 QDIO_MAX_BUFFERS_PER_Q);
276}
277
Jan Glauber60b5df22009-06-22 12:08:10 +0200278static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200279 unsigned int input)
280{
281 int cc;
282
283 if (!need_siga_sync(q))
284 return 0;
285
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100286 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100287 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200288
289 cc = do_siga_sync(q->irq_ptr->schid, output, input);
Jan Glauber22f99342008-12-25 13:38:46 +0100290 if (cc)
291 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200292 return cc;
293}
294
Jan Glauber60b5df22009-06-22 12:08:10 +0200295static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200296{
297 if (q->is_input_q)
298 return qdio_siga_sync(q, 0, q->mask);
299 else
300 return qdio_siga_sync(q, q->mask, 0);
301}
302
303static inline int qdio_siga_sync_out(struct qdio_q *q)
304{
305 return qdio_siga_sync(q, ~0U, 0);
306}
307
308static inline int qdio_siga_sync_all(struct qdio_q *q)
309{
310 return qdio_siga_sync(q, ~0U, ~0U);
311}
312
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100313static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
Jan Glauber779e6e12008-07-17 17:16:48 +0200314{
Jan Glauber779e6e12008-07-17 17:16:48 +0200315 unsigned long schid;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100316 unsigned int fc = 0;
317 u64 start_time = 0;
318 int cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200319
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100320 if (q->u.out.use_enh_siga)
Klaus-Dieter Wacker7a0f4752008-10-10 21:33:18 +0200321 fc = 3;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100322
323 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200324 schid = q->irq_ptr->sch_token;
325 fc |= 0x80;
326 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100327 else
328 schid = *((u32 *)&q->irq_ptr->schid);
Jan Glauber779e6e12008-07-17 17:16:48 +0200329
Jan Glauber779e6e12008-07-17 17:16:48 +0200330again:
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100331 cc = do_siga_output(schid, q->mask, busy_bit, fc);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200332
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100333 /* hipersocket busy condition */
334 if (*busy_bit) {
335 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
336
337 if (!start_time) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200338 start_time = get_usecs();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100339 goto again;
340 }
341 if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200342 goto again;
343 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200344 return cc;
345}
346
347static inline int qdio_siga_input(struct qdio_q *q)
348{
349 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
354 cc = do_siga_input(q->irq_ptr->schid, q->mask);
355 if (cc)
Jan Glauber22f99342008-12-25 13:38:46 +0100356 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200357 return cc;
358}
359
Jan Glauber60b5df22009-06-22 12:08:10 +0200360static inline void qdio_sync_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200361{
362 if (pci_out_supported(q)) {
363 if (need_siga_sync_thinint(q))
364 qdio_siga_sync_all(q);
365 else if (need_siga_sync_out_thinint(q))
366 qdio_siga_sync_out(q);
367 } else
368 qdio_siga_sync_q(q);
369}
370
Jan Glauber60b5df22009-06-22 12:08:10 +0200371int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
372 unsigned char *state)
373{
374 qdio_siga_sync_q(q);
375 return get_buf_states(q, bufnr, state, 1, 0);
376}
377
378static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200379{
Jan Glauber50f769d2008-12-25 13:38:47 +0100380 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200381 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100382
Jan Glauber779e6e12008-07-17 17:16:48 +0200383 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100384 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200385
386 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100387 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100388 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100389 q->u.in.ack_count);
390 q->u.in.ack_count = 0;
391 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100392 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200393}
394
Jan Glauberd3072972010-02-26 22:37:36 +0100395static inline void account_sbals(struct qdio_q *q, int count)
396{
397 int pos = 0;
398
399 q->q_stats.nr_sbal_total += count;
400 if (count == QDIO_MAX_BUFFERS_MASK) {
401 q->q_stats.nr_sbals[7]++;
402 return;
403 }
404 while (count >>= 1)
405 pos++;
406 q->q_stats.nr_sbals[pos]++;
407}
408
Jan Glauber50f769d2008-12-25 13:38:47 +0100409static void announce_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200410{
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100411 q->qdio_error |= QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100412
413 /* special handling for no target buffer empty */
414 if ((!q->is_input_q &&
415 (q->sbal[q->first_to_check]->element[15].flags & 0xff) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100416 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200417 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100418 q->first_to_check);
419 return;
420 }
421
Jan Glauber22f99342008-12-25 13:38:46 +0100422 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
423 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100424 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100425 DBF_ERROR("F14:%2x F15:%2x",
426 q->sbal[q->first_to_check]->element[14].flags & 0xff,
427 q->sbal[q->first_to_check]->element[15].flags & 0xff);
Jan Glauber50f769d2008-12-25 13:38:47 +0100428}
Jan Glauber779e6e12008-07-17 17:16:48 +0200429
Jan Glauber50f769d2008-12-25 13:38:47 +0100430static inline void inbound_primed(struct qdio_q *q, int count)
431{
432 int new;
433
Jan Glauber1d7e1502009-09-22 22:58:39 +0200434 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100435
436 /* for QEBSM the ACK was already set by EQBS */
437 if (is_qebsm(q)) {
438 if (!q->u.in.polling) {
439 q->u.in.polling = 1;
440 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100441 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100442 return;
443 }
444
445 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100446 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100447 q->u.in.ack_count);
448 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100449 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100450 return;
451 }
452
453 /*
454 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
455 * or by the next inbound run.
456 */
457 new = add_buf(q->first_to_check, count - 1);
458 if (q->u.in.polling) {
459 /* reset the previous ACK but first set the new one */
460 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100461 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100462 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100463 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100464 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100465 }
466
Jan Glaubere85dea02009-03-26 15:24:29 +0100467 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100468 count--;
469 if (!count)
470 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200471 /* need to change ALL buffers to get more interrupts */
472 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200473}
474
475static int get_inbound_buffer_frontier(struct qdio_q *q)
476{
477 int count, stop;
478 unsigned char state;
479
480 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200481 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
482 * would return 0.
483 */
484 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
485 stop = add_buf(q->first_to_check, count);
486
Jan Glauber779e6e12008-07-17 17:16:48 +0200487 if (q->first_to_check == stop)
488 goto out;
489
Jan Glauber36e3e722009-06-22 12:08:12 +0200490 /*
491 * No siga sync here, as a PCI or we after a thin interrupt
492 * already sync'ed the queues.
493 */
Jan Glauber50f769d2008-12-25 13:38:47 +0100494 count = get_buf_states(q, q->first_to_check, &state, count, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200495 if (!count)
496 goto out;
497
498 switch (state) {
499 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100500 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200501 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100502 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100503 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100504 if (q->irq_ptr->perf_stat_enabled)
505 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200506 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200507 case SLSB_P_INPUT_ERROR:
Jan Glauber50f769d2008-12-25 13:38:47 +0100508 announce_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200509 /* process the buffer, the upper layer will take care of it */
510 q->first_to_check = add_buf(q->first_to_check, count);
511 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100512 if (q->irq_ptr->perf_stat_enabled)
513 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200514 break;
515 case SLSB_CU_INPUT_EMPTY:
516 case SLSB_P_INPUT_NOT_INIT:
517 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100518 if (q->irq_ptr->perf_stat_enabled)
519 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100520 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200521 break;
522 default:
523 BUG();
524 }
525out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200526 return q->first_to_check;
527}
528
Jan Glauber60b5df22009-06-22 12:08:10 +0200529static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200530{
531 int bufnr;
532
533 bufnr = get_inbound_buffer_frontier(q);
534
Jan Glaubere85dea02009-03-26 15:24:29 +0100535 if ((bufnr != q->last_move) || q->qdio_error) {
536 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100537 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber779e6e12008-07-17 17:16:48 +0200538 q->u.in.timestamp = get_usecs();
Jan Glauber779e6e12008-07-17 17:16:48 +0200539 return 1;
540 } else
541 return 0;
542}
543
Jan Glauber9a2c1602009-06-22 12:08:11 +0200544static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200545{
546 unsigned char state = 0;
547
548 if (!atomic_read(&q->nr_buf_used))
549 return 1;
550
551 qdio_siga_sync_q(q);
552 get_buf_state(q, q->first_to_check, &state, 0);
553
Ursula Braun4c522282010-02-09 09:46:07 +0100554 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200555 /* more work coming */
556 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200557
558 if (is_thinint_irq(q->irq_ptr))
559 return 1;
560
561 /* don't poll under z/VM */
562 if (MACHINE_IS_VM)
563 return 1;
564
565 /*
566 * At this point we know, that inbound first_to_check
567 * has (probably) not moved (see qdio_inbound_processing).
568 */
569 if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200570 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200571 q->first_to_check);
572 return 1;
573 } else
574 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200575}
576
577static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200578{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100579 int start = q->first_to_kick;
580 int end = q->first_to_check;
581 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200582
583 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
584 return;
585
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100586 count = sub_buf(end, start);
587
588 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100589 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200590 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100591 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100592 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200593 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
594 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100595 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100596
597 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
598 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200599
600 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100601 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200602 q->qdio_error = 0;
603}
604
605static void __qdio_inbound_processing(struct qdio_q *q)
606{
Jan Glauber6486cda2010-01-04 09:05:42 +0100607 qperf_inc(q, tasklet_inbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200608again:
609 if (!qdio_inbound_q_moved(q))
610 return;
611
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100612 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200613
Jan Glauber6486cda2010-01-04 09:05:42 +0100614 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200615 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100616 qperf_inc(q, tasklet_inbound_resched);
Jan Glauber779e6e12008-07-17 17:16:48 +0200617 goto again;
Jan Glauber6486cda2010-01-04 09:05:42 +0100618 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200619
620 qdio_stop_polling(q);
621 /*
622 * We need to check again to not lose initiative after
623 * resetting the ACK state.
624 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100625 if (!qdio_inbound_q_done(q)) {
626 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber779e6e12008-07-17 17:16:48 +0200627 goto again;
Jan Glauber6486cda2010-01-04 09:05:42 +0100628 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200629}
630
Jan Glauber779e6e12008-07-17 17:16:48 +0200631void qdio_inbound_processing(unsigned long data)
632{
633 struct qdio_q *q = (struct qdio_q *)data;
634 __qdio_inbound_processing(q);
635}
636
637static int get_outbound_buffer_frontier(struct qdio_q *q)
638{
639 int count, stop;
640 unsigned char state;
641
642 if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
643 (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
644 qdio_siga_sync_q(q);
645
646 /*
647 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
648 * would return 0.
649 */
650 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
651 stop = add_buf(q->first_to_check, count);
652
Jan Glauber779e6e12008-07-17 17:16:48 +0200653 if (q->first_to_check == stop)
654 return q->first_to_check;
655
Jan Glauber50f769d2008-12-25 13:38:47 +0100656 count = get_buf_states(q, q->first_to_check, &state, count, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200657 if (!count)
658 return q->first_to_check;
659
660 switch (state) {
661 case SLSB_P_OUTPUT_EMPTY:
662 /* the adapter got it */
Jan Glauber1d7e1502009-09-22 22:58:39 +0200663 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200664
665 atomic_sub(count, &q->nr_buf_used);
666 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100667 if (q->irq_ptr->perf_stat_enabled)
668 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200669 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200670 case SLSB_P_OUTPUT_ERROR:
Jan Glauber50f769d2008-12-25 13:38:47 +0100671 announce_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200672 /* process the buffer, the upper layer will take care of it */
673 q->first_to_check = add_buf(q->first_to_check, count);
674 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100675 if (q->irq_ptr->perf_stat_enabled)
676 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200677 break;
678 case SLSB_CU_OUTPUT_PRIMED:
679 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100680 if (q->irq_ptr->perf_stat_enabled)
681 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100682 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200683 break;
684 case SLSB_P_OUTPUT_NOT_INIT:
685 case SLSB_P_OUTPUT_HALTED:
686 break;
687 default:
688 BUG();
689 }
690 return q->first_to_check;
691}
692
693/* all buffers processed? */
694static inline int qdio_outbound_q_done(struct qdio_q *q)
695{
696 return atomic_read(&q->nr_buf_used) == 0;
697}
698
699static inline int qdio_outbound_q_moved(struct qdio_q *q)
700{
701 int bufnr;
702
703 bufnr = get_outbound_buffer_frontier(q);
704
Jan Glaubere85dea02009-03-26 15:24:29 +0100705 if ((bufnr != q->last_move) || q->qdio_error) {
706 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100707 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200708 return 1;
709 } else
710 return 0;
711}
712
Jan Glauberd303b6f2009-03-26 15:24:31 +0100713static int qdio_kick_outbound_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200714{
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100715 unsigned int busy_bit;
716 int cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200717
718 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100719 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200720
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100721 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100722 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100723
724 cc = qdio_siga_output(q, &busy_bit);
725 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200726 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200727 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100728 case 2:
729 if (busy_bit) {
730 DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
Jan Glauberd303b6f2009-03-26 15:24:31 +0100731 cc |= QDIO_ERROR_SIGA_BUSY;
732 } else
733 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100734 break;
735 case 1:
736 case 3:
737 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100738 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200739 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100740 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200741}
742
Jan Glauber779e6e12008-07-17 17:16:48 +0200743static void __qdio_outbound_processing(struct qdio_q *q)
744{
Jan Glauber6486cda2010-01-04 09:05:42 +0100745 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200746 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
747
748 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100749 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200750
Jan Glauberc38f9602009-03-26 15:24:26 +0100751 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200752 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100753 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200754
755 /* bail out for HiperSockets unicast queues */
756 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
757 return;
758
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200759 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
Jan Glauberc38f9602009-03-26 15:24:26 +0100760 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
761 goto sched;
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200762
Jan Glauber779e6e12008-07-17 17:16:48 +0200763 if (q->u.out.pci_out_enabled)
764 return;
765
766 /*
767 * Now we know that queue type is either qeth without pci enabled
768 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
769 * EMPTY is noticed and outbound_handler is called after some time.
770 */
771 if (qdio_outbound_q_done(q))
772 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100773 else
774 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200775 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100776 return;
777
778sched:
779 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
780 return;
781 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200782}
783
784/* outbound tasklet */
785void qdio_outbound_processing(unsigned long data)
786{
787 struct qdio_q *q = (struct qdio_q *)data;
788 __qdio_outbound_processing(q);
789}
790
791void qdio_outbound_timer(unsigned long data)
792{
793 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100794
795 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
796 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200797 tasklet_schedule(&q->tasklet);
798}
799
Jan Glauber60b5df22009-06-22 12:08:10 +0200800static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200801{
802 struct qdio_q *out;
803 int i;
804
805 if (!pci_out_supported(q))
806 return;
807
808 for_each_output_queue(q->irq_ptr, out, i)
809 if (!qdio_outbound_q_done(out))
810 tasklet_schedule(&out->tasklet);
811}
812
Jan Glauber60b5df22009-06-22 12:08:10 +0200813static void __tiqdio_inbound_processing(struct qdio_q *q)
814{
Jan Glauber6486cda2010-01-04 09:05:42 +0100815 qperf_inc(q, tasklet_inbound);
Jan Glauber60b5df22009-06-22 12:08:10 +0200816 qdio_sync_after_thinint(q);
817
818 /*
819 * The interrupt could be caused by a PCI request. Check the
820 * PCI capable outbound queues.
821 */
822 qdio_check_outbound_after_thinint(q);
823
824 if (!qdio_inbound_q_moved(q))
825 return;
826
827 qdio_kick_handler(q);
828
Jan Glauber9a2c1602009-06-22 12:08:11 +0200829 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100830 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200831 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200832 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200833 return;
834 }
Jan Glauber60b5df22009-06-22 12:08:10 +0200835 }
836
837 qdio_stop_polling(q);
838 /*
839 * We need to check again to not lose initiative after
840 * resetting the ACK state.
841 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200842 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100843 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +0200844 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
845 tasklet_schedule(&q->tasklet);
846 }
847}
848
849void tiqdio_inbound_processing(unsigned long data)
850{
851 struct qdio_q *q = (struct qdio_q *)data;
852 __tiqdio_inbound_processing(q);
853}
854
Jan Glauber779e6e12008-07-17 17:16:48 +0200855static inline void qdio_set_state(struct qdio_irq *irq_ptr,
856 enum qdio_irq_states state)
857{
Jan Glauber22f99342008-12-25 13:38:46 +0100858 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200859
860 irq_ptr->state = state;
861 mb();
862}
863
Jan Glauber22f99342008-12-25 13:38:46 +0100864static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200865{
Jan Glauber779e6e12008-07-17 17:16:48 +0200866 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100867 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
868 DBF_ERROR_HEX(irb, 64);
869 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200870 }
871}
872
873/* PCI interrupt handler */
874static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
875{
876 int i;
877 struct qdio_q *q;
878
Jan Glauberc38f9602009-03-26 15:24:26 +0100879 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
880 return;
881
Jan Glauber779e6e12008-07-17 17:16:48 +0200882 for_each_input_queue(irq_ptr, q, i)
883 tasklet_schedule(&q->tasklet);
884
885 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
886 return;
887
888 for_each_output_queue(irq_ptr, q, i) {
889 if (qdio_outbound_q_done(q))
890 continue;
891
892 if (!siga_syncs_out_pci(q))
893 qdio_siga_sync_q(q);
894
895 tasklet_schedule(&q->tasklet);
896 }
897}
898
899static void qdio_handle_activate_check(struct ccw_device *cdev,
900 unsigned long intparm, int cstat, int dstat)
901{
902 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
903 struct qdio_q *q;
Jan Glauber779e6e12008-07-17 17:16:48 +0200904
Jan Glauber22f99342008-12-25 13:38:46 +0100905 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
906 DBF_ERROR("intp :%lx", intparm);
907 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +0200908
909 if (irq_ptr->nr_input_qs) {
910 q = irq_ptr->input_qs[0];
911 } else if (irq_ptr->nr_output_qs) {
912 q = irq_ptr->output_qs[0];
913 } else {
914 dump_stack();
915 goto no_handler;
916 }
917 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
918 0, -1, -1, irq_ptr->int_parm);
919no_handler:
920 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
921}
922
Jan Glauber779e6e12008-07-17 17:16:48 +0200923static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
924 int dstat)
925{
926 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +0200927
Jan Glauber22f99342008-12-25 13:38:46 +0100928 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +0200929
930 if (cstat)
931 goto error;
932 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
933 goto error;
934 if (!(dstat & DEV_STAT_DEV_END))
935 goto error;
936 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
937 return;
938
939error:
940 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
941 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
942 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +0200943}
944
945/* qdio interrupt handler */
946void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
947 struct irb *irb)
948{
949 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
950 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +0200951
Jan Glauber779e6e12008-07-17 17:16:48 +0200952 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +0100953 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +0200954 return;
955 }
956
957 if (IS_ERR(irb)) {
958 switch (PTR_ERR(irb)) {
959 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +0100960 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +0200961 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
962 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200963 return;
964 default:
965 WARN_ON(1);
966 return;
967 }
968 }
Jan Glauber22f99342008-12-25 13:38:46 +0100969 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +0200970 cstat = irb->scsw.cmd.cstat;
971 dstat = irb->scsw.cmd.dstat;
972
973 switch (irq_ptr->state) {
974 case QDIO_IRQ_STATE_INACTIVE:
975 qdio_establish_handle_irq(cdev, cstat, dstat);
976 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200977 case QDIO_IRQ_STATE_CLEANUP:
978 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
979 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200980 case QDIO_IRQ_STATE_ESTABLISHED:
981 case QDIO_IRQ_STATE_ACTIVE:
982 if (cstat & SCHN_STAT_PCI) {
983 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200984 return;
985 }
Jan Glauber4c575422009-06-12 10:26:28 +0200986 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +0200987 qdio_handle_activate_check(cdev, intparm, cstat,
988 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +0200989 break;
Jan Glauber959153d2010-02-09 09:46:08 +0100990 case QDIO_IRQ_STATE_STOPPED:
991 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200992 default:
993 WARN_ON(1);
994 }
995 wake_up(&cdev->private->wait_q);
996}
997
998/**
999 * qdio_get_ssqd_desc - get qdio subchannel description
1000 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001001 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001002 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001003 * Returns 0 or an error code. The results of the chsc are stored in the
1004 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001005 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001006int qdio_get_ssqd_desc(struct ccw_device *cdev,
1007 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001008{
Jan Glauber779e6e12008-07-17 17:16:48 +02001009
Jan Glauberbbd50e12008-12-25 13:38:43 +01001010 if (!cdev || !cdev->private)
1011 return -EINVAL;
1012
Jan Glauber22f99342008-12-25 13:38:46 +01001013 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001014 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001015}
1016EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1017
1018/**
1019 * qdio_cleanup - shutdown queues and free data structures
1020 * @cdev: associated ccw device
1021 * @how: use halt or clear to shutdown
1022 *
Jan Glauber700e9822009-03-26 15:24:27 +01001023 * This function calls qdio_shutdown() for @cdev with method @how.
1024 * and qdio_free(). The qdio_free() return value is ignored since
1025 * !irq_ptr is already checked.
Jan Glauber779e6e12008-07-17 17:16:48 +02001026 */
1027int qdio_cleanup(struct ccw_device *cdev, int how)
1028{
Jan Glauber22f99342008-12-25 13:38:46 +01001029 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001030 int rc;
1031
Jan Glauber779e6e12008-07-17 17:16:48 +02001032 if (!irq_ptr)
1033 return -ENODEV;
1034
Jan Glauber779e6e12008-07-17 17:16:48 +02001035 rc = qdio_shutdown(cdev, how);
Jan Glauber700e9822009-03-26 15:24:27 +01001036
1037 qdio_free(cdev);
Jan Glauber779e6e12008-07-17 17:16:48 +02001038 return rc;
1039}
1040EXPORT_SYMBOL_GPL(qdio_cleanup);
1041
1042static void qdio_shutdown_queues(struct ccw_device *cdev)
1043{
1044 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1045 struct qdio_q *q;
1046 int i;
1047
1048 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001049 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001050
1051 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001052 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001053 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001054 }
1055}
1056
1057/**
1058 * qdio_shutdown - shut down a qdio subchannel
1059 * @cdev: associated ccw device
1060 * @how: use halt or clear to shutdown
1061 */
1062int qdio_shutdown(struct ccw_device *cdev, int how)
1063{
Jan Glauber22f99342008-12-25 13:38:46 +01001064 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001065 int rc;
1066 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001067
Jan Glauber779e6e12008-07-17 17:16:48 +02001068 if (!irq_ptr)
1069 return -ENODEV;
1070
Jan Glauberb4547402009-03-26 15:24:24 +01001071 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001072 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1073
Jan Glauber779e6e12008-07-17 17:16:48 +02001074 mutex_lock(&irq_ptr->setup_mutex);
1075 /*
1076 * Subchannel was already shot down. We cannot prevent being called
1077 * twice since cio may trigger a shutdown asynchronously.
1078 */
1079 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1080 mutex_unlock(&irq_ptr->setup_mutex);
1081 return 0;
1082 }
1083
Jan Glauberc38f9602009-03-26 15:24:26 +01001084 /*
1085 * Indicate that the device is going down. Scheduling the queue
1086 * tasklets is forbidden from here on.
1087 */
1088 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1089
Jan Glauber779e6e12008-07-17 17:16:48 +02001090 tiqdio_remove_input_queues(irq_ptr);
1091 qdio_shutdown_queues(cdev);
1092 qdio_shutdown_debug_entries(irq_ptr, cdev);
1093
1094 /* cleanup subchannel */
1095 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1096
1097 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1098 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1099 else
1100 /* default behaviour is halt */
1101 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1102 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001103 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1104 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001105 goto no_cleanup;
1106 }
1107
1108 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1109 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1110 wait_event_interruptible_timeout(cdev->private->wait_q,
1111 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1112 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1113 10 * HZ);
1114 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1115
1116no_cleanup:
1117 qdio_shutdown_thinint(irq_ptr);
1118
1119 /* restore interrupt handler */
1120 if ((void *)cdev->handler == (void *)qdio_int_handler)
1121 cdev->handler = irq_ptr->orig_handler;
1122 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1123
1124 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1125 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001126 if (rc)
1127 return rc;
1128 return 0;
1129}
1130EXPORT_SYMBOL_GPL(qdio_shutdown);
1131
1132/**
1133 * qdio_free - free data structures for a qdio subchannel
1134 * @cdev: associated ccw device
1135 */
1136int qdio_free(struct ccw_device *cdev)
1137{
Jan Glauber22f99342008-12-25 13:38:46 +01001138 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001139
Jan Glauber779e6e12008-07-17 17:16:48 +02001140 if (!irq_ptr)
1141 return -ENODEV;
1142
Jan Glauber22f99342008-12-25 13:38:46 +01001143 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001144 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001145
1146 if (irq_ptr->debug_area != NULL) {
1147 debug_unregister(irq_ptr->debug_area);
1148 irq_ptr->debug_area = NULL;
1149 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001150 cdev->private->qdio_data = NULL;
1151 mutex_unlock(&irq_ptr->setup_mutex);
1152
1153 qdio_release_memory(irq_ptr);
1154 return 0;
1155}
1156EXPORT_SYMBOL_GPL(qdio_free);
1157
1158/**
1159 * qdio_initialize - allocate and establish queues for a qdio subchannel
1160 * @init_data: initialization data
1161 *
1162 * This function first allocates queues via qdio_allocate() and on success
1163 * establishes them via qdio_establish().
1164 */
1165int qdio_initialize(struct qdio_initialize *init_data)
1166{
1167 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001168
1169 rc = qdio_allocate(init_data);
1170 if (rc)
1171 return rc;
1172
1173 rc = qdio_establish(init_data);
1174 if (rc)
1175 qdio_free(init_data->cdev);
1176 return rc;
1177}
1178EXPORT_SYMBOL_GPL(qdio_initialize);
1179
1180/**
1181 * qdio_allocate - allocate qdio queues and associated data
1182 * @init_data: initialization data
1183 */
1184int qdio_allocate(struct qdio_initialize *init_data)
1185{
1186 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001187
Jan Glauber22f99342008-12-25 13:38:46 +01001188 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001189
1190 if ((init_data->no_input_qs && !init_data->input_handler) ||
1191 (init_data->no_output_qs && !init_data->output_handler))
1192 return -EINVAL;
1193
1194 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1195 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1196 return -EINVAL;
1197
1198 if ((!init_data->input_sbal_addr_array) ||
1199 (!init_data->output_sbal_addr_array))
1200 return -EINVAL;
1201
Jan Glauber779e6e12008-07-17 17:16:48 +02001202 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1203 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1204 if (!irq_ptr)
1205 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001206
1207 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001208 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001209
1210 /*
1211 * Allocate a page for the chsc calls in qdio_establish.
1212 * Must be pre-allocated since a zfcp recovery will call
1213 * qdio_establish. In case of low memory and swap on a zfcp disk
1214 * we may not be able to allocate memory otherwise.
1215 */
1216 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1217 if (!irq_ptr->chsc_page)
1218 goto out_rel;
1219
1220 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001221 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001222 if (!irq_ptr->qdr)
1223 goto out_rel;
1224 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1225
Jan Glauber779e6e12008-07-17 17:16:48 +02001226 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1227 init_data->no_output_qs))
1228 goto out_rel;
1229
1230 init_data->cdev->private->qdio_data = irq_ptr;
1231 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1232 return 0;
1233out_rel:
1234 qdio_release_memory(irq_ptr);
1235out_err:
1236 return -ENOMEM;
1237}
1238EXPORT_SYMBOL_GPL(qdio_allocate);
1239
1240/**
1241 * qdio_establish - establish queues on a qdio subchannel
1242 * @init_data: initialization data
1243 */
1244int qdio_establish(struct qdio_initialize *init_data)
1245{
Jan Glauber779e6e12008-07-17 17:16:48 +02001246 struct qdio_irq *irq_ptr;
1247 struct ccw_device *cdev = init_data->cdev;
1248 unsigned long saveflags;
1249 int rc;
1250
Jan Glauber22f99342008-12-25 13:38:46 +01001251 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001252
Jan Glauber779e6e12008-07-17 17:16:48 +02001253 irq_ptr = cdev->private->qdio_data;
1254 if (!irq_ptr)
1255 return -ENODEV;
1256
1257 if (cdev->private->state != DEV_STATE_ONLINE)
1258 return -EINVAL;
1259
Jan Glauber779e6e12008-07-17 17:16:48 +02001260 mutex_lock(&irq_ptr->setup_mutex);
1261 qdio_setup_irq(init_data);
1262
1263 rc = qdio_establish_thinint(irq_ptr);
1264 if (rc) {
1265 mutex_unlock(&irq_ptr->setup_mutex);
1266 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1267 return rc;
1268 }
1269
1270 /* establish q */
1271 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1272 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1273 irq_ptr->ccw.count = irq_ptr->equeue.count;
1274 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1275
1276 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1277 ccw_device_set_options_mask(cdev, 0);
1278
1279 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1280 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001281 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1282 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001283 }
1284 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1285
1286 if (rc) {
1287 mutex_unlock(&irq_ptr->setup_mutex);
1288 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1289 return rc;
1290 }
1291
1292 wait_event_interruptible_timeout(cdev->private->wait_q,
1293 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1294 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1295
1296 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1297 mutex_unlock(&irq_ptr->setup_mutex);
1298 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1299 return -EIO;
1300 }
1301
1302 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber22f99342008-12-25 13:38:46 +01001303 DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
1304 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
Jan Glauber779e6e12008-07-17 17:16:48 +02001305
1306 /* qebsm is now setup if available, initialize buffer states */
1307 qdio_init_buf_states(irq_ptr);
1308
1309 mutex_unlock(&irq_ptr->setup_mutex);
1310 qdio_print_subchannel_info(irq_ptr, cdev);
1311 qdio_setup_debug_entries(irq_ptr, cdev);
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(qdio_establish);
1315
1316/**
1317 * qdio_activate - activate queues on a qdio subchannel
1318 * @cdev: associated cdev
1319 */
1320int qdio_activate(struct ccw_device *cdev)
1321{
1322 struct qdio_irq *irq_ptr;
1323 int rc;
1324 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001325
Jan Glauber22f99342008-12-25 13:38:46 +01001326 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001327
Jan Glauber779e6e12008-07-17 17:16:48 +02001328 irq_ptr = cdev->private->qdio_data;
1329 if (!irq_ptr)
1330 return -ENODEV;
1331
1332 if (cdev->private->state != DEV_STATE_ONLINE)
1333 return -EINVAL;
1334
1335 mutex_lock(&irq_ptr->setup_mutex);
1336 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1337 rc = -EBUSY;
1338 goto out;
1339 }
1340
Jan Glauber779e6e12008-07-17 17:16:48 +02001341 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1342 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1343 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1344 irq_ptr->ccw.cda = 0;
1345
1346 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1347 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1348
1349 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1350 0, DOIO_DENY_PREFETCH);
1351 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001352 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1353 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001354 }
1355 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1356
1357 if (rc)
1358 goto out;
1359
1360 if (is_thinint_irq(irq_ptr))
1361 tiqdio_add_input_queues(irq_ptr);
1362
1363 /* wait for subchannel to become active */
1364 msleep(5);
1365
1366 switch (irq_ptr->state) {
1367 case QDIO_IRQ_STATE_STOPPED:
1368 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001369 rc = -EIO;
1370 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001371 default:
1372 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1373 rc = 0;
1374 }
1375out:
1376 mutex_unlock(&irq_ptr->setup_mutex);
1377 return rc;
1378}
1379EXPORT_SYMBOL_GPL(qdio_activate);
1380
1381static inline int buf_in_between(int bufnr, int start, int count)
1382{
1383 int end = add_buf(start, count);
1384
1385 if (end > start) {
1386 if (bufnr >= start && bufnr < end)
1387 return 1;
1388 else
1389 return 0;
1390 }
1391
1392 /* wrap-around case */
1393 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1394 (bufnr < end))
1395 return 1;
1396 else
1397 return 0;
1398}
1399
1400/**
1401 * handle_inbound - reset processed input buffers
1402 * @q: queue containing the buffers
1403 * @callflags: flags
1404 * @bufnr: first buffer to process
1405 * @count: how many buffers are emptied
1406 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001407static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1408 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001409{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001410 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001411
Jan Glauber6486cda2010-01-04 09:05:42 +01001412 qperf_inc(q, inbound_call);
1413
Jan Glauber50f769d2008-12-25 13:38:47 +01001414 if (!q->u.in.polling)
1415 goto set;
1416
1417 /* protect against stop polling setting an ACK for an emptied slsb */
1418 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1419 /* overwriting everything, just delete polling status */
1420 q->u.in.polling = 0;
1421 q->u.in.ack_count = 0;
1422 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001423 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001424 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001425 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001426 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001427 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001428 q->u.in.ack_count -= diff;
1429 if (q->u.in.ack_count <= 0) {
1430 q->u.in.polling = 0;
1431 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001432 goto set;
1433 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001434 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001435 }
1436 else
1437 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001438 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001439 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001440
Jan Glauber50f769d2008-12-25 13:38:47 +01001441set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001442 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001443
1444 used = atomic_add_return(count, &q->nr_buf_used) - count;
1445 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1446
1447 /* no need to signal as long as the adapter had free buffers */
1448 if (used)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001449 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001450
Jan Glauberd303b6f2009-03-26 15:24:31 +01001451 if (need_siga_in(q))
1452 return qdio_siga_input(q);
1453 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001454}
1455
1456/**
1457 * handle_outbound - process filled outbound buffers
1458 * @q: queue containing the buffers
1459 * @callflags: flags
1460 * @bufnr: first buffer to process
1461 * @count: how many buffers are filled
1462 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001463static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1464 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001465{
1466 unsigned char state;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001467 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001468
Jan Glauber6486cda2010-01-04 09:05:42 +01001469 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001470
1471 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1472 used = atomic_add_return(count, &q->nr_buf_used);
1473 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1474
Jan Glauber6486cda2010-01-04 09:05:42 +01001475 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001476 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001477 qperf_inc(q, pci_request_int);
1478 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001479 else
1480 q->u.out.pci_out_enabled = 0;
1481
1482 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1483 if (multicast_outbound(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +01001484 rc = qdio_kick_outbound_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001485 else
Klaus-Dieter Wacker7a0f4752008-10-10 21:33:18 +02001486 if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
1487 (count > 1) &&
1488 (count <= q->irq_ptr->ssqd_desc.mmwc)) {
1489 /* exploit enhanced SIGA */
1490 q->u.out.use_enh_siga = 1;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001491 rc = qdio_kick_outbound_q(q);
Klaus-Dieter Wacker7a0f4752008-10-10 21:33:18 +02001492 } else {
1493 /*
1494 * One siga-w per buffer required for unicast
1495 * HiperSockets.
1496 */
1497 q->u.out.use_enh_siga = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001498 while (count--) {
1499 rc = qdio_kick_outbound_q(q);
1500 if (rc)
1501 goto out;
1502 }
Klaus-Dieter Wacker7a0f4752008-10-10 21:33:18 +02001503 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001504 goto out;
1505 }
1506
1507 if (need_siga_sync(q)) {
1508 qdio_siga_sync_q(q);
1509 goto out;
1510 }
1511
1512 /* try to fast requeue buffers */
Jan Glauber50f769d2008-12-25 13:38:47 +01001513 get_buf_state(q, prev_buf(bufnr), &state, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001514 if (state != SLSB_CU_OUTPUT_PRIMED)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001515 rc = qdio_kick_outbound_q(q);
Jan Glauber1d7e1502009-09-22 22:58:39 +02001516 else
Jan Glauber6486cda2010-01-04 09:05:42 +01001517 qperf_inc(q, fast_requeue);
Jan Glauber1d7e1502009-09-22 22:58:39 +02001518
Jan Glauber779e6e12008-07-17 17:16:48 +02001519out:
Jan Glauber779e6e12008-07-17 17:16:48 +02001520 tasklet_schedule(&q->tasklet);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001521 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001522}
1523
1524/**
1525 * do_QDIO - process input or output buffers
1526 * @cdev: associated ccw_device for the qdio subchannel
1527 * @callflags: input or output and special flags from the program
1528 * @q_nr: queue number
1529 * @bufnr: buffer number
1530 * @count: how many buffers to process
1531 */
1532int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001533 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001534{
1535 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001536
Jan Glauber66182412009-06-22 12:08:15 +02001537 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001538 return -EINVAL;
1539
Jan Glauber779e6e12008-07-17 17:16:48 +02001540 irq_ptr = cdev->private->qdio_data;
1541 if (!irq_ptr)
1542 return -ENODEV;
1543
Jan Glauber1d7e1502009-09-22 22:58:39 +02001544 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1545 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001546
1547 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1548 return -EBUSY;
1549
1550 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001551 return handle_inbound(irq_ptr->input_qs[q_nr],
1552 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001553 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001554 return handle_outbound(irq_ptr->output_qs[q_nr],
1555 callflags, bufnr, count);
1556 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001557}
1558EXPORT_SYMBOL_GPL(do_QDIO);
1559
1560static int __init init_QDIO(void)
1561{
1562 int rc;
1563
1564 rc = qdio_setup_init();
1565 if (rc)
1566 return rc;
1567 rc = tiqdio_allocate_memory();
1568 if (rc)
1569 goto out_cache;
1570 rc = qdio_debug_init();
1571 if (rc)
1572 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001573 rc = tiqdio_register_thinints();
1574 if (rc)
Jan Glauber6486cda2010-01-04 09:05:42 +01001575 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001576 return 0;
1577
Jan Glauber779e6e12008-07-17 17:16:48 +02001578out_debug:
1579 qdio_debug_exit();
1580out_ti:
1581 tiqdio_free_memory();
1582out_cache:
1583 qdio_setup_exit();
1584 return rc;
1585}
1586
1587static void __exit exit_QDIO(void)
1588{
1589 tiqdio_unregister_thinints();
1590 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001591 qdio_debug_exit();
1592 qdio_setup_exit();
1593}
1594
1595module_init(init_QDIO);
1596module_exit(exit_QDIO);