blob: 5ec5317f7903948bc7669b323f09b1558ba7438c [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>
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000017#include <linux/io.h>
Arun Sharma600634972011-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,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000080 unsigned int *bb, unsigned int fc,
81 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +020082{
83 register unsigned long __fc asm("0") = fc;
84 register unsigned long __schid asm("1") = schid;
85 register unsigned long __mask asm("2") = mask;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000086 register unsigned long __aob asm("3") = aob;
Jan Glauber779e6e12008-07-17 17:16:48 +020087 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
88
89 asm volatile(
90 " siga 0\n"
91 "0: ipm %0\n"
92 " srl %0,28\n"
93 "1:\n"
94 EX_TABLE(0b, 1b)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000095 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask),
96 "+d" (__aob)
Jan Glauber779e6e12008-07-17 17:16:48 +020097 : : "cc", "memory");
98 *bb = ((unsigned int) __fc) >> 31;
99 return cc;
100}
101
102static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
103{
Jan Glauber779e6e12008-07-17 17:16:48 +0200104 /* all done or next buffer state different */
105 if (ccq == 0 || ccq == 32)
106 return 0;
Jan Glauber25f269f2011-10-30 15:17:06 +0100107 /* no buffer processed */
108 if (ccq == 97)
Jan Glauber779e6e12008-07-17 17:16:48 +0200109 return 1;
Jan Glauber25f269f2011-10-30 15:17:06 +0100110 /* not all buffers processed */
111 if (ccq == 96)
112 return 2;
Jan Glauber779e6e12008-07-17 17:16:48 +0200113 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100114 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200115 return -EIO;
116}
117
118/**
119 * qdio_do_eqbs - extract buffer states for QEBSM
120 * @q: queue to manipulate
121 * @state: state of the extracted buffers
122 * @start: buffer number to start at
123 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100124 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200125 *
Coly Li73ac36e2009-01-07 18:09:16 -0800126 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200127 * Stops processing if a state is different from the last buffers state.
128 */
129static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100130 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200131{
Jan Glauber25f269f2011-10-30 15:17:06 +0100132 int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200133 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200134
135 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100136 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200137
138 if (!q->is_input_q)
139 nr += q->irq_ptr->nr_input_qs;
140again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100141 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
142 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200143 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100144 if (!rc)
145 return count - tmp_count;
Jan Glauber22f99342008-12-25 13:38:46 +0100146
Jan Glauber779e6e12008-07-17 17:16:48 +0200147 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100148 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200149 goto again;
150 }
151
Jan Glauber25f269f2011-10-30 15:17:06 +0100152 if (rc == 2) {
153 BUG_ON(tmp_count == count);
154 qperf_inc(q, eqbs_partial);
155 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
156 tmp_count);
157 /*
158 * Retry once, if that fails bail out and process the
159 * extracted buffers before trying again.
160 */
161 if (!retried++)
162 goto again;
163 else
164 return count - tmp_count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200165 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100166
167 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
168 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
169 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
170 0, -1, -1, q->irq_ptr->int_parm);
171 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200172}
173
174/**
175 * qdio_do_sqbs - set buffer states for QEBSM
176 * @q: queue to manipulate
177 * @state: new state of the buffers
178 * @start: first buffer number to change
179 * @count: how many buffers to change
180 *
181 * Returns the number of successfully changed buffers.
182 * Does retrying until the specified count of buffer states is set or an
183 * error occurs.
184 */
185static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
186 int count)
187{
188 unsigned int ccq = 0;
189 int tmp_count = count, tmp_start = start;
190 int nr = q->nr;
191 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200192
Jan Glauber50f769d2008-12-25 13:38:47 +0100193 if (!count)
194 return 0;
195
Jan Glauber779e6e12008-07-17 17:16:48 +0200196 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100197 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200198
199 if (!q->is_input_q)
200 nr += q->irq_ptr->nr_input_qs;
201again:
202 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
203 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100204 if (!rc) {
205 WARN_ON(tmp_count);
206 return count - tmp_count;
207 }
208
209 if (rc == 1 || rc == 2) {
Jan Glauber22f99342008-12-25 13:38:46 +0100210 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100211 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200212 goto again;
213 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100214
215 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
216 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
217 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
218 0, -1, -1, q->irq_ptr->int_parm);
219 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200220}
221
222/* returns number of examined buffers and their common state in *state */
223static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100224 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000225 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200226{
227 unsigned char __state = 0;
228 int i;
229
230 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
231 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
232
233 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100234 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200235
236 for (i = 0; i < count; i++) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000237 if (!__state) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200238 __state = q->slsb.val[bufnr];
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000239 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
240 __state = SLSB_P_OUTPUT_EMPTY;
241 } else if (merge_pending) {
242 if ((q->slsb.val[bufnr] & __state) != __state)
243 break;
244 } else if (q->slsb.val[bufnr] != __state)
Jan Glauber779e6e12008-07-17 17:16:48 +0200245 break;
246 bufnr = next_buf(bufnr);
247 }
248 *state = __state;
249 return i;
250}
251
Jan Glauber60b5df22009-06-22 12:08:10 +0200252static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
253 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200254{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000255 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200256}
257
258/* wrap-around safe setting of slsb states, returns number of changed buffers */
259static inline int set_buf_states(struct qdio_q *q, int bufnr,
260 unsigned char state, int count)
261{
262 int i;
263
264 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
265 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
266
267 if (is_qebsm(q))
268 return qdio_do_sqbs(q, state, bufnr, count);
269
270 for (i = 0; i < count; i++) {
271 xchg(&q->slsb.val[bufnr], state);
272 bufnr = next_buf(bufnr);
273 }
274 return count;
275}
276
277static inline int set_buf_state(struct qdio_q *q, int bufnr,
278 unsigned char state)
279{
280 return set_buf_states(q, bufnr, state, 1);
281}
282
283/* set slsb states to initial state */
284void qdio_init_buf_states(struct qdio_irq *irq_ptr)
285{
286 struct qdio_q *q;
287 int i;
288
289 for_each_input_queue(irq_ptr, q, i)
290 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
291 QDIO_MAX_BUFFERS_PER_Q);
292 for_each_output_queue(irq_ptr, q, i)
293 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
294 QDIO_MAX_BUFFERS_PER_Q);
295}
296
Jan Glauber60b5df22009-06-22 12:08:10 +0200297static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200298 unsigned int input)
299{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100300 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
301 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200302 int cc;
303
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100304 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100305 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200306
Jan Glauber958c0ba2011-01-05 12:47:52 +0100307 if (is_qebsm(q)) {
308 schid = q->irq_ptr->sch_token;
309 fc |= QDIO_SIGA_QEBSM_FLAG;
310 }
311
312 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100313 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100314 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200315 return cc;
316}
317
Jan Glauber60b5df22009-06-22 12:08:10 +0200318static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200319{
320 if (q->is_input_q)
321 return qdio_siga_sync(q, 0, q->mask);
322 else
323 return qdio_siga_sync(q, q->mask, 0);
324}
325
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000326static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
327 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200328{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100329 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
330 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100331 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200332 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000333 unsigned long laob = 0;
334
335 if (q->u.out.use_cq && aob != 0) {
336 fc = QDIO_SIGA_WRITEQ;
337 laob = aob;
338 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200339
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100340 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200341 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100342 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200343 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200344again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000345 WARN_ON_ONCE((aob && queue_type(q) != QDIO_IQDIO_QFMT) ||
346 (aob && fc != QDIO_SIGA_WRITEQ));
347 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200348
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100349 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100350 if (unlikely(*busy_bit)) {
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100351 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200352 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100353
354 if (!start_time) {
Jan Glauber3a601bf2010-05-17 10:00:17 +0200355 start_time = get_clock();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100356 goto again;
357 }
Jan Glauber3a601bf2010-05-17 10:00:17 +0200358 if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200359 goto again;
360 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200361 if (retries) {
362 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
363 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
364 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
365 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200366 return cc;
367}
368
369static inline int qdio_siga_input(struct qdio_q *q)
370{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100371 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
372 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200373 int cc;
374
Jan Glauber22f99342008-12-25 13:38:46 +0100375 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100376 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200377
Jan Glauber958c0ba2011-01-05 12:47:52 +0100378 if (is_qebsm(q)) {
379 schid = q->irq_ptr->sch_token;
380 fc |= QDIO_SIGA_QEBSM_FLAG;
381 }
382
383 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100384 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100385 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200386 return cc;
387}
388
Jan Glauber90adac52011-01-05 12:47:54 +0100389#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
390#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
391
392static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200393{
Jan Glauber90adac52011-01-05 12:47:54 +0100394 /* PCI capable outbound queues will also be scanned so sync them too */
395 if (pci_out_supported(q))
396 qdio_siga_sync_all(q);
397 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200398 qdio_siga_sync_q(q);
399}
400
Jan Glauber60b5df22009-06-22 12:08:10 +0200401int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
402 unsigned char *state)
403{
Jan Glauber90adac52011-01-05 12:47:54 +0100404 if (need_siga_sync(q))
405 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000406 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200407}
408
409static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200410{
Jan Glauber50f769d2008-12-25 13:38:47 +0100411 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200412 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100413
Jan Glauber779e6e12008-07-17 17:16:48 +0200414 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100415 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200416
417 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100418 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100419 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100420 q->u.in.ack_count);
421 q->u.in.ack_count = 0;
422 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100423 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200424}
425
Jan Glauberd3072972010-02-26 22:37:36 +0100426static inline void account_sbals(struct qdio_q *q, int count)
427{
428 int pos = 0;
429
430 q->q_stats.nr_sbal_total += count;
431 if (count == QDIO_MAX_BUFFERS_MASK) {
432 q->q_stats.nr_sbals[7]++;
433 return;
434 }
435 while (count >>= 1)
436 pos++;
437 q->q_stats.nr_sbals[pos]++;
438}
439
Jan Glauberbffbbd22011-04-20 10:15:33 +0200440static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200441{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200442 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
443 SLSB_P_OUTPUT_NOT_INIT;
444
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100445 q->qdio_error |= QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100446
447 /* special handling for no target buffer empty */
448 if ((!q->is_input_q &&
Jan Glauber3ec908782011-06-06 14:14:40 +0200449 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100450 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200451 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100452 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100453 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100454 }
455
Jan Glauber22f99342008-12-25 13:38:46 +0100456 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
457 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100458 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100459 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec908782011-06-06 14:14:40 +0200460 q->sbal[q->first_to_check]->element[14].sflags,
461 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200462
Jan Glauber2768b2d2011-10-30 15:17:07 +0100463set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200464 /*
465 * Interrupts may be avoided as long as the error is present
466 * so change the buffer state immediately to avoid starvation.
467 */
468 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100469}
Jan Glauber779e6e12008-07-17 17:16:48 +0200470
Jan Glauber50f769d2008-12-25 13:38:47 +0100471static inline void inbound_primed(struct qdio_q *q, int count)
472{
473 int new;
474
Jan Glauber1d7e1502009-09-22 22:58:39 +0200475 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100476
477 /* for QEBSM the ACK was already set by EQBS */
478 if (is_qebsm(q)) {
479 if (!q->u.in.polling) {
480 q->u.in.polling = 1;
481 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100482 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100483 return;
484 }
485
486 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100487 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100488 q->u.in.ack_count);
489 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100490 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100491 return;
492 }
493
494 /*
495 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
496 * or by the next inbound run.
497 */
498 new = add_buf(q->first_to_check, count - 1);
499 if (q->u.in.polling) {
500 /* reset the previous ACK but first set the new one */
501 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100502 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100503 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100504 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100505 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100506 }
507
Jan Glaubere85dea02009-03-26 15:24:29 +0100508 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100509 count--;
510 if (!count)
511 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200512 /* need to change ALL buffers to get more interrupts */
513 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200514}
515
516static int get_inbound_buffer_frontier(struct qdio_q *q)
517{
518 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100519 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200520
Jan Glaubera2b86012011-10-30 15:17:05 +0100521 q->timestamp = get_clock_fast();
522
Jan Glauber779e6e12008-07-17 17:16:48 +0200523 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200524 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
525 * would return 0.
526 */
527 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
528 stop = add_buf(q->first_to_check, count);
529
Jan Glauber779e6e12008-07-17 17:16:48 +0200530 if (q->first_to_check == stop)
531 goto out;
532
Jan Glauber36e3e722009-06-22 12:08:12 +0200533 /*
534 * No siga sync here, as a PCI or we after a thin interrupt
535 * already sync'ed the queues.
536 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000537 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200538 if (!count)
539 goto out;
540
541 switch (state) {
542 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100543 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200544 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100545 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100546 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100547 if (q->irq_ptr->perf_stat_enabled)
548 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200549 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200550 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200551 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200552 q->first_to_check = add_buf(q->first_to_check, count);
553 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100554 if (q->irq_ptr->perf_stat_enabled)
555 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200556 break;
557 case SLSB_CU_INPUT_EMPTY:
558 case SLSB_P_INPUT_NOT_INIT:
559 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100560 if (q->irq_ptr->perf_stat_enabled)
561 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100562 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200563 break;
564 default:
565 BUG();
566 }
567out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200568 return q->first_to_check;
569}
570
Jan Glauber60b5df22009-06-22 12:08:10 +0200571static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200572{
573 int bufnr;
574
575 bufnr = get_inbound_buffer_frontier(q);
576
Jan Glaubere85dea02009-03-26 15:24:29 +0100577 if ((bufnr != q->last_move) || q->qdio_error) {
578 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100579 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber3a601bf2010-05-17 10:00:17 +0200580 q->u.in.timestamp = get_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200581 return 1;
582 } else
583 return 0;
584}
585
Jan Glauber9a2c1602009-06-22 12:08:11 +0200586static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200587{
588 unsigned char state = 0;
589
590 if (!atomic_read(&q->nr_buf_used))
591 return 1;
592
Jan Glauber90adac52011-01-05 12:47:54 +0100593 if (need_siga_sync(q))
594 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200595 get_buf_state(q, q->first_to_check, &state, 0);
596
Ursula Braun4c522282010-02-09 09:46:07 +0100597 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200598 /* more work coming */
599 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200600
601 if (is_thinint_irq(q->irq_ptr))
602 return 1;
603
604 /* don't poll under z/VM */
605 if (MACHINE_IS_VM)
606 return 1;
607
608 /*
609 * At this point we know, that inbound first_to_check
610 * has (probably) not moved (see qdio_inbound_processing).
611 */
Jan Glauber3a601bf2010-05-17 10:00:17 +0200612 if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200613 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200614 q->first_to_check);
615 return 1;
616 } else
617 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200618}
619
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000620static inline int contains_aobs(struct qdio_q *q)
621{
622 return !q->is_input_q && q->u.out.use_cq;
623}
624
625static inline void qdio_trace_aob(struct qdio_irq *irq, struct qdio_q *q,
626 int i, struct qaob *aob)
627{
628 int tmp;
629
630 DBF_DEV_EVENT(DBF_INFO, irq, "AOB%d:%lx", i,
631 (unsigned long) virt_to_phys(aob));
632 DBF_DEV_EVENT(DBF_INFO, irq, "RES00:%lx",
633 (unsigned long) aob->res0[0]);
634 DBF_DEV_EVENT(DBF_INFO, irq, "RES01:%lx",
635 (unsigned long) aob->res0[1]);
636 DBF_DEV_EVENT(DBF_INFO, irq, "RES02:%lx",
637 (unsigned long) aob->res0[2]);
638 DBF_DEV_EVENT(DBF_INFO, irq, "RES03:%lx",
639 (unsigned long) aob->res0[3]);
640 DBF_DEV_EVENT(DBF_INFO, irq, "RES04:%lx",
641 (unsigned long) aob->res0[4]);
642 DBF_DEV_EVENT(DBF_INFO, irq, "RES05:%lx",
643 (unsigned long) aob->res0[5]);
644 DBF_DEV_EVENT(DBF_INFO, irq, "RES1:%x", aob->res1);
645 DBF_DEV_EVENT(DBF_INFO, irq, "RES2:%x", aob->res2);
646 DBF_DEV_EVENT(DBF_INFO, irq, "RES3:%x", aob->res3);
647 DBF_DEV_EVENT(DBF_INFO, irq, "AORC:%u", aob->aorc);
648 DBF_DEV_EVENT(DBF_INFO, irq, "FLAGS:%u", aob->flags);
649 DBF_DEV_EVENT(DBF_INFO, irq, "CBTBS:%u", aob->cbtbs);
650 DBF_DEV_EVENT(DBF_INFO, irq, "SBC:%u", aob->sb_count);
651 for (tmp = 0; tmp < QDIO_MAX_ELEMENTS_PER_BUFFER; ++tmp) {
652 DBF_DEV_EVENT(DBF_INFO, irq, "SBA%d:%lx", tmp,
653 (unsigned long) aob->sba[tmp]);
654 DBF_DEV_EVENT(DBF_INFO, irq, "rSBA%d:%lx", tmp,
655 (unsigned long) q->sbal[i]->element[tmp].addr);
656 DBF_DEV_EVENT(DBF_INFO, irq, "DC%d:%u", tmp, aob->dcount[tmp]);
657 DBF_DEV_EVENT(DBF_INFO, irq, "rDC%d:%u", tmp,
658 q->sbal[i]->element[tmp].length);
659 }
660 DBF_DEV_EVENT(DBF_INFO, irq, "USER0:%lx", (unsigned long) aob->user0);
661 for (tmp = 0; tmp < 2; ++tmp) {
662 DBF_DEV_EVENT(DBF_INFO, irq, "RES4%d:%lx", tmp,
663 (unsigned long) aob->res4[tmp]);
664 }
665 DBF_DEV_EVENT(DBF_INFO, irq, "USER1:%lx", (unsigned long) aob->user1);
666 DBF_DEV_EVENT(DBF_INFO, irq, "USER2:%lx", (unsigned long) aob->user2);
667}
668
669static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
670{
671 unsigned char state = 0;
672 int j, b = start;
673
674 if (!contains_aobs(q))
675 return;
676
677 for (j = 0; j < count; ++j) {
678 get_buf_state(q, b, &state, 0);
679 if (state == SLSB_P_OUTPUT_PENDING) {
680 struct qaob *aob = q->u.out.aobs[b];
681 if (aob == NULL)
682 continue;
683
684 BUG_ON(q->u.out.sbal_state == NULL);
685 q->u.out.sbal_state[b].flags |=
686 QDIO_OUTBUF_STATE_FLAG_PENDING;
687 q->u.out.aobs[b] = NULL;
688 } else if (state == SLSB_P_OUTPUT_EMPTY) {
689 BUG_ON(q->u.out.sbal_state == NULL);
690 q->u.out.sbal_state[b].aob = NULL;
691 }
692 b = next_buf(b);
693 }
694}
695
696static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
697 int bufnr)
698{
699 unsigned long phys_aob = 0;
700
701 if (!q->use_cq)
702 goto out;
703
704 if (!q->aobs[bufnr]) {
705 struct qaob *aob = qdio_allocate_aob();
706 q->aobs[bufnr] = aob;
707 }
708 if (q->aobs[bufnr]) {
709 BUG_ON(q->sbal_state == NULL);
710 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
711 q->sbal_state[bufnr].aob = q->aobs[bufnr];
712 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
713 phys_aob = virt_to_phys(q->aobs[bufnr]);
714 BUG_ON(phys_aob & 0xFF);
715 }
716
717out:
718 return phys_aob;
719}
720
Jan Glauber60b5df22009-06-22 12:08:10 +0200721static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200722{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100723 int start = q->first_to_kick;
724 int end = q->first_to_check;
725 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200726
727 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
728 return;
729
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100730 count = sub_buf(end, start);
731
732 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100733 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200734 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100735 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100736 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200737 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
738 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100739 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100740
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000741 qdio_handle_aobs(q, start, count);
742
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100743 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
744 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200745
746 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100747 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200748 q->qdio_error = 0;
749}
750
751static void __qdio_inbound_processing(struct qdio_q *q)
752{
Jan Glauber6486cda2010-01-04 09:05:42 +0100753 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200754
Jan Glauber779e6e12008-07-17 17:16:48 +0200755 if (!qdio_inbound_q_moved(q))
756 return;
757
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100758 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200759
Jan Glauber6486cda2010-01-04 09:05:42 +0100760 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200761 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100762 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200763 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
764 tasklet_schedule(&q->tasklet);
765 return;
766 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100767 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200768
769 qdio_stop_polling(q);
770 /*
771 * We need to check again to not lose initiative after
772 * resetting the ACK state.
773 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100774 if (!qdio_inbound_q_done(q)) {
775 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200776 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
777 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100778 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200779}
780
Jan Glauber779e6e12008-07-17 17:16:48 +0200781void qdio_inbound_processing(unsigned long data)
782{
783 struct qdio_q *q = (struct qdio_q *)data;
784 __qdio_inbound_processing(q);
785}
786
787static int get_outbound_buffer_frontier(struct qdio_q *q)
788{
789 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100790 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200791
Jan Glaubera2b86012011-10-30 15:17:05 +0100792 q->timestamp = get_clock_fast();
793
Jan Glauber90adac52011-01-05 12:47:54 +0100794 if (need_siga_sync(q))
795 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
796 !pci_out_supported(q)) ||
797 (queue_type(q) == QDIO_IQDIO_QFMT &&
798 multicast_outbound(q)))
799 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200800
801 /*
802 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
803 * would return 0.
804 */
805 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
806 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200807 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000808 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200809
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000810 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200811 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000812 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200813
814 switch (state) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000815 case SLSB_P_OUTPUT_PENDING:
816 BUG();
Jan Glauber779e6e12008-07-17 17:16:48 +0200817 case SLSB_P_OUTPUT_EMPTY:
818 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000819 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
820 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200821
822 atomic_sub(count, &q->nr_buf_used);
823 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100824 if (q->irq_ptr->perf_stat_enabled)
825 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000826
Jan Glauber36e3e722009-06-22 12:08:12 +0200827 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200828 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200829 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200830 q->first_to_check = add_buf(q->first_to_check, count);
831 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100832 if (q->irq_ptr->perf_stat_enabled)
833 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200834 break;
835 case SLSB_CU_OUTPUT_PRIMED:
836 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100837 if (q->irq_ptr->perf_stat_enabled)
838 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000839 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
840 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200841 break;
842 case SLSB_P_OUTPUT_NOT_INIT:
843 case SLSB_P_OUTPUT_HALTED:
844 break;
845 default:
846 BUG();
847 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000848
849out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200850 return q->first_to_check;
851}
852
853/* all buffers processed? */
854static inline int qdio_outbound_q_done(struct qdio_q *q)
855{
856 return atomic_read(&q->nr_buf_used) == 0;
857}
858
859static inline int qdio_outbound_q_moved(struct qdio_q *q)
860{
861 int bufnr;
862
863 bufnr = get_outbound_buffer_frontier(q);
864
Jan Glaubere85dea02009-03-26 15:24:29 +0100865 if ((bufnr != q->last_move) || q->qdio_error) {
866 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100867 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200868 return 1;
869 } else
870 return 0;
871}
872
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000873static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200874{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200875 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100876 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200877
878 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100879 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200880
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100881 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200882retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100883 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100884
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000885 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100886 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200887 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200888 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100889 case 2:
890 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200891 while (++retries < QDIO_BUSY_BIT_RETRIES) {
892 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
893 goto retry;
894 }
895 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauberd303b6f2009-03-26 15:24:31 +0100896 cc |= QDIO_ERROR_SIGA_BUSY;
897 } else
898 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100899 break;
900 case 1:
901 case 3:
902 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100903 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200904 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200905 if (retries) {
906 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
907 DBF_ERROR("count:%u", retries);
908 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100909 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200910}
911
Jan Glauber779e6e12008-07-17 17:16:48 +0200912static void __qdio_outbound_processing(struct qdio_q *q)
913{
Jan Glauber6486cda2010-01-04 09:05:42 +0100914 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200915 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
916
917 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100918 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200919
Jan Glauberc38f9602009-03-26 15:24:26 +0100920 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200921 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100922 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200923
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200924 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
Jan Glauberc38f9602009-03-26 15:24:26 +0100925 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
926 goto sched;
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200927
Jan Glauber779e6e12008-07-17 17:16:48 +0200928 if (q->u.out.pci_out_enabled)
929 return;
930
931 /*
932 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100933 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
934 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200935 */
936 if (qdio_outbound_q_done(q))
937 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100938 else
939 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200940 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100941 return;
942
943sched:
944 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
945 return;
946 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200947}
948
949/* outbound tasklet */
950void qdio_outbound_processing(unsigned long data)
951{
952 struct qdio_q *q = (struct qdio_q *)data;
953 __qdio_outbound_processing(q);
954}
955
956void qdio_outbound_timer(unsigned long data)
957{
958 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100959
960 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
961 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200962 tasklet_schedule(&q->tasklet);
963}
964
Jan Glauber60b5df22009-06-22 12:08:10 +0200965static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200966{
967 struct qdio_q *out;
968 int i;
969
970 if (!pci_out_supported(q))
971 return;
972
973 for_each_output_queue(q->irq_ptr, out, i)
974 if (!qdio_outbound_q_done(out))
975 tasklet_schedule(&out->tasklet);
976}
977
Jan Glauber60b5df22009-06-22 12:08:10 +0200978static void __tiqdio_inbound_processing(struct qdio_q *q)
979{
Jan Glauber6486cda2010-01-04 09:05:42 +0100980 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100981 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
982 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200983
984 /*
985 * The interrupt could be caused by a PCI request. Check the
986 * PCI capable outbound queues.
987 */
988 qdio_check_outbound_after_thinint(q);
989
990 if (!qdio_inbound_q_moved(q))
991 return;
992
993 qdio_kick_handler(q);
994
Jan Glauber9a2c1602009-06-22 12:08:11 +0200995 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100996 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200997 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200998 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200999 return;
1000 }
Jan Glauber60b5df22009-06-22 12:08:10 +02001001 }
1002
1003 qdio_stop_polling(q);
1004 /*
1005 * We need to check again to not lose initiative after
1006 * resetting the ACK state.
1007 */
Jan Glauber9a2c1602009-06-22 12:08:11 +02001008 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +01001009 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +02001010 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
1011 tasklet_schedule(&q->tasklet);
1012 }
1013}
1014
1015void tiqdio_inbound_processing(unsigned long data)
1016{
1017 struct qdio_q *q = (struct qdio_q *)data;
1018 __tiqdio_inbound_processing(q);
1019}
1020
Jan Glauber779e6e12008-07-17 17:16:48 +02001021static inline void qdio_set_state(struct qdio_irq *irq_ptr,
1022 enum qdio_irq_states state)
1023{
Jan Glauber22f99342008-12-25 13:38:46 +01001024 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +02001025
1026 irq_ptr->state = state;
1027 mb();
1028}
1029
Jan Glauber22f99342008-12-25 13:38:46 +01001030static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +02001031{
Jan Glauber779e6e12008-07-17 17:16:48 +02001032 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +01001033 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
1034 DBF_ERROR_HEX(irb, 64);
1035 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +02001036 }
1037}
1038
1039/* PCI interrupt handler */
1040static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
1041{
1042 int i;
1043 struct qdio_q *q;
1044
Jan Glauberc38f9602009-03-26 15:24:26 +01001045 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
1046 return;
1047
Jan Glauberd36deae2010-09-07 21:14:39 +00001048 for_each_input_queue(irq_ptr, q, i) {
1049 if (q->u.in.queue_start_poll) {
1050 /* skip if polling is enabled or already in work */
1051 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1052 &q->u.in.queue_irq_state)) {
1053 qperf_inc(q, int_discarded);
1054 continue;
1055 }
1056 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1057 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001058 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +00001059 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001060 }
Jan Glauberd36deae2010-09-07 21:14:39 +00001061 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001062
Jan Glauber90adac52011-01-05 12:47:54 +01001063 if (!pci_out_supported(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001064 return;
1065
1066 for_each_output_queue(irq_ptr, q, i) {
1067 if (qdio_outbound_q_done(q))
1068 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001069 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001070 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001071 tasklet_schedule(&q->tasklet);
1072 }
1073}
1074
1075static void qdio_handle_activate_check(struct ccw_device *cdev,
1076 unsigned long intparm, int cstat, int dstat)
1077{
1078 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1079 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001080 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001081
Jan Glauber22f99342008-12-25 13:38:46 +01001082 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1083 DBF_ERROR("intp :%lx", intparm);
1084 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001085
1086 if (irq_ptr->nr_input_qs) {
1087 q = irq_ptr->input_qs[0];
1088 } else if (irq_ptr->nr_output_qs) {
1089 q = irq_ptr->output_qs[0];
1090 } else {
1091 dump_stack();
1092 goto no_handler;
1093 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001094
1095 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber779e6e12008-07-17 17:16:48 +02001096 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001097 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001098no_handler:
1099 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1100}
1101
Jan Glauber779e6e12008-07-17 17:16:48 +02001102static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1103 int dstat)
1104{
1105 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001106
Jan Glauber22f99342008-12-25 13:38:46 +01001107 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001108
1109 if (cstat)
1110 goto error;
1111 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1112 goto error;
1113 if (!(dstat & DEV_STAT_DEV_END))
1114 goto error;
1115 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1116 return;
1117
1118error:
1119 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1120 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1121 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001122}
1123
1124/* qdio interrupt handler */
1125void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1126 struct irb *irb)
1127{
1128 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1129 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001130
Jan Glauber779e6e12008-07-17 17:16:48 +02001131 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +01001132 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001133 return;
1134 }
1135
Jan Glauber09a308f2010-05-17 10:00:14 +02001136 if (irq_ptr->perf_stat_enabled)
1137 irq_ptr->perf_stat.qdio_int++;
1138
Jan Glauber779e6e12008-07-17 17:16:48 +02001139 if (IS_ERR(irb)) {
1140 switch (PTR_ERR(irb)) {
1141 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +01001142 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +02001143 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1144 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001145 return;
1146 default:
1147 WARN_ON(1);
1148 return;
1149 }
1150 }
Jan Glauber22f99342008-12-25 13:38:46 +01001151 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001152 cstat = irb->scsw.cmd.cstat;
1153 dstat = irb->scsw.cmd.dstat;
1154
1155 switch (irq_ptr->state) {
1156 case QDIO_IRQ_STATE_INACTIVE:
1157 qdio_establish_handle_irq(cdev, cstat, dstat);
1158 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001159 case QDIO_IRQ_STATE_CLEANUP:
1160 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1161 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001162 case QDIO_IRQ_STATE_ESTABLISHED:
1163 case QDIO_IRQ_STATE_ACTIVE:
1164 if (cstat & SCHN_STAT_PCI) {
1165 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001166 return;
1167 }
Jan Glauber4c575422009-06-12 10:26:28 +02001168 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001169 qdio_handle_activate_check(cdev, intparm, cstat,
1170 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001171 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001172 case QDIO_IRQ_STATE_STOPPED:
1173 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001174 default:
1175 WARN_ON(1);
1176 }
1177 wake_up(&cdev->private->wait_q);
1178}
1179
1180/**
1181 * qdio_get_ssqd_desc - get qdio subchannel description
1182 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001183 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001184 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001185 * Returns 0 or an error code. The results of the chsc are stored in the
1186 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001187 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001188int qdio_get_ssqd_desc(struct ccw_device *cdev,
1189 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001190{
Jan Glauber779e6e12008-07-17 17:16:48 +02001191
Jan Glauberbbd50e12008-12-25 13:38:43 +01001192 if (!cdev || !cdev->private)
1193 return -EINVAL;
1194
Jan Glauber22f99342008-12-25 13:38:46 +01001195 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001196 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001197}
1198EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1199
Jan Glauber779e6e12008-07-17 17:16:48 +02001200static void qdio_shutdown_queues(struct ccw_device *cdev)
1201{
1202 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1203 struct qdio_q *q;
1204 int i;
1205
1206 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001207 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001208
1209 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001210 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001211 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001212 }
1213}
1214
1215/**
1216 * qdio_shutdown - shut down a qdio subchannel
1217 * @cdev: associated ccw device
1218 * @how: use halt or clear to shutdown
1219 */
1220int qdio_shutdown(struct ccw_device *cdev, int how)
1221{
Jan Glauber22f99342008-12-25 13:38:46 +01001222 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001223 int rc;
1224 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001225
Jan Glauber779e6e12008-07-17 17:16:48 +02001226 if (!irq_ptr)
1227 return -ENODEV;
1228
Jan Glauberb4547402009-03-26 15:24:24 +01001229 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001230 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1231
Jan Glauber779e6e12008-07-17 17:16:48 +02001232 mutex_lock(&irq_ptr->setup_mutex);
1233 /*
1234 * Subchannel was already shot down. We cannot prevent being called
1235 * twice since cio may trigger a shutdown asynchronously.
1236 */
1237 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1238 mutex_unlock(&irq_ptr->setup_mutex);
1239 return 0;
1240 }
1241
Jan Glauberc38f9602009-03-26 15:24:26 +01001242 /*
1243 * Indicate that the device is going down. Scheduling the queue
1244 * tasklets is forbidden from here on.
1245 */
1246 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1247
Jan Glauber779e6e12008-07-17 17:16:48 +02001248 tiqdio_remove_input_queues(irq_ptr);
1249 qdio_shutdown_queues(cdev);
1250 qdio_shutdown_debug_entries(irq_ptr, cdev);
1251
1252 /* cleanup subchannel */
1253 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1254
1255 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1256 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1257 else
1258 /* default behaviour is halt */
1259 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1260 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001261 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1262 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001263 goto no_cleanup;
1264 }
1265
1266 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1267 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1268 wait_event_interruptible_timeout(cdev->private->wait_q,
1269 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1270 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1271 10 * HZ);
1272 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1273
1274no_cleanup:
1275 qdio_shutdown_thinint(irq_ptr);
1276
1277 /* restore interrupt handler */
1278 if ((void *)cdev->handler == (void *)qdio_int_handler)
1279 cdev->handler = irq_ptr->orig_handler;
1280 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1281
1282 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1283 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001284 if (rc)
1285 return rc;
1286 return 0;
1287}
1288EXPORT_SYMBOL_GPL(qdio_shutdown);
1289
1290/**
1291 * qdio_free - free data structures for a qdio subchannel
1292 * @cdev: associated ccw device
1293 */
1294int qdio_free(struct ccw_device *cdev)
1295{
Jan Glauber22f99342008-12-25 13:38:46 +01001296 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001297
Jan Glauber779e6e12008-07-17 17:16:48 +02001298 if (!irq_ptr)
1299 return -ENODEV;
1300
Jan Glauber22f99342008-12-25 13:38:46 +01001301 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001302 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001303
1304 if (irq_ptr->debug_area != NULL) {
1305 debug_unregister(irq_ptr->debug_area);
1306 irq_ptr->debug_area = NULL;
1307 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001308 cdev->private->qdio_data = NULL;
1309 mutex_unlock(&irq_ptr->setup_mutex);
1310
1311 qdio_release_memory(irq_ptr);
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(qdio_free);
1315
1316/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001317 * qdio_allocate - allocate qdio queues and associated data
1318 * @init_data: initialization data
1319 */
1320int qdio_allocate(struct qdio_initialize *init_data)
1321{
1322 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001323
Jan Glauber22f99342008-12-25 13:38:46 +01001324 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001325
1326 if ((init_data->no_input_qs && !init_data->input_handler) ||
1327 (init_data->no_output_qs && !init_data->output_handler))
1328 return -EINVAL;
1329
1330 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1331 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1332 return -EINVAL;
1333
1334 if ((!init_data->input_sbal_addr_array) ||
1335 (!init_data->output_sbal_addr_array))
1336 return -EINVAL;
1337
Jan Glauber779e6e12008-07-17 17:16:48 +02001338 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1339 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1340 if (!irq_ptr)
1341 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001342
1343 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001344 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001345
1346 /*
1347 * Allocate a page for the chsc calls in qdio_establish.
1348 * Must be pre-allocated since a zfcp recovery will call
1349 * qdio_establish. In case of low memory and swap on a zfcp disk
1350 * we may not be able to allocate memory otherwise.
1351 */
1352 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1353 if (!irq_ptr->chsc_page)
1354 goto out_rel;
1355
1356 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001357 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001358 if (!irq_ptr->qdr)
1359 goto out_rel;
1360 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1361
Jan Glauber779e6e12008-07-17 17:16:48 +02001362 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1363 init_data->no_output_qs))
1364 goto out_rel;
1365
1366 init_data->cdev->private->qdio_data = irq_ptr;
1367 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1368 return 0;
1369out_rel:
1370 qdio_release_memory(irq_ptr);
1371out_err:
1372 return -ENOMEM;
1373}
1374EXPORT_SYMBOL_GPL(qdio_allocate);
1375
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001376static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1377{
1378 struct qdio_q *q = irq_ptr->input_qs[0];
1379 int i, use_cq = 0;
1380
1381 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1382 use_cq = 1;
1383
1384 for_each_output_queue(irq_ptr, q, i) {
1385 if (use_cq) {
1386 if (qdio_enable_async_operation(&q->u.out) < 0) {
1387 use_cq = 0;
1388 continue;
1389 }
1390 } else
1391 qdio_disable_async_operation(&q->u.out);
1392 }
1393 DBF_EVENT("use_cq:%d", use_cq);
1394}
1395
Jan Glauber779e6e12008-07-17 17:16:48 +02001396/**
1397 * qdio_establish - establish queues on a qdio subchannel
1398 * @init_data: initialization data
1399 */
1400int qdio_establish(struct qdio_initialize *init_data)
1401{
Jan Glauber779e6e12008-07-17 17:16:48 +02001402 struct qdio_irq *irq_ptr;
1403 struct ccw_device *cdev = init_data->cdev;
1404 unsigned long saveflags;
1405 int rc;
1406
Jan Glauber22f99342008-12-25 13:38:46 +01001407 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001408
Jan Glauber779e6e12008-07-17 17:16:48 +02001409 irq_ptr = cdev->private->qdio_data;
1410 if (!irq_ptr)
1411 return -ENODEV;
1412
1413 if (cdev->private->state != DEV_STATE_ONLINE)
1414 return -EINVAL;
1415
Jan Glauber779e6e12008-07-17 17:16:48 +02001416 mutex_lock(&irq_ptr->setup_mutex);
1417 qdio_setup_irq(init_data);
1418
1419 rc = qdio_establish_thinint(irq_ptr);
1420 if (rc) {
1421 mutex_unlock(&irq_ptr->setup_mutex);
1422 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1423 return rc;
1424 }
1425
1426 /* establish q */
1427 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1428 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1429 irq_ptr->ccw.count = irq_ptr->equeue.count;
1430 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1431
1432 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1433 ccw_device_set_options_mask(cdev, 0);
1434
1435 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1436 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001437 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1438 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001439 }
1440 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1441
1442 if (rc) {
1443 mutex_unlock(&irq_ptr->setup_mutex);
1444 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1445 return rc;
1446 }
1447
1448 wait_event_interruptible_timeout(cdev->private->wait_q,
1449 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1450 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1451
1452 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1453 mutex_unlock(&irq_ptr->setup_mutex);
1454 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1455 return -EIO;
1456 }
1457
1458 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber22f99342008-12-25 13:38:46 +01001459 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
Jan Glauber779e6e12008-07-17 17:16:48 +02001460
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001461 qdio_detect_hsicq(irq_ptr);
1462
Jan Glauber779e6e12008-07-17 17:16:48 +02001463 /* qebsm is now setup if available, initialize buffer states */
1464 qdio_init_buf_states(irq_ptr);
1465
1466 mutex_unlock(&irq_ptr->setup_mutex);
1467 qdio_print_subchannel_info(irq_ptr, cdev);
1468 qdio_setup_debug_entries(irq_ptr, cdev);
1469 return 0;
1470}
1471EXPORT_SYMBOL_GPL(qdio_establish);
1472
1473/**
1474 * qdio_activate - activate queues on a qdio subchannel
1475 * @cdev: associated cdev
1476 */
1477int qdio_activate(struct ccw_device *cdev)
1478{
1479 struct qdio_irq *irq_ptr;
1480 int rc;
1481 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001482
Jan Glauber22f99342008-12-25 13:38:46 +01001483 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001484
Jan Glauber779e6e12008-07-17 17:16:48 +02001485 irq_ptr = cdev->private->qdio_data;
1486 if (!irq_ptr)
1487 return -ENODEV;
1488
1489 if (cdev->private->state != DEV_STATE_ONLINE)
1490 return -EINVAL;
1491
1492 mutex_lock(&irq_ptr->setup_mutex);
1493 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1494 rc = -EBUSY;
1495 goto out;
1496 }
1497
Jan Glauber779e6e12008-07-17 17:16:48 +02001498 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1499 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1500 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1501 irq_ptr->ccw.cda = 0;
1502
1503 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1504 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1505
1506 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1507 0, DOIO_DENY_PREFETCH);
1508 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001509 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1510 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001511 }
1512 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1513
1514 if (rc)
1515 goto out;
1516
1517 if (is_thinint_irq(irq_ptr))
1518 tiqdio_add_input_queues(irq_ptr);
1519
1520 /* wait for subchannel to become active */
1521 msleep(5);
1522
1523 switch (irq_ptr->state) {
1524 case QDIO_IRQ_STATE_STOPPED:
1525 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001526 rc = -EIO;
1527 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001528 default:
1529 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1530 rc = 0;
1531 }
1532out:
1533 mutex_unlock(&irq_ptr->setup_mutex);
1534 return rc;
1535}
1536EXPORT_SYMBOL_GPL(qdio_activate);
1537
1538static inline int buf_in_between(int bufnr, int start, int count)
1539{
1540 int end = add_buf(start, count);
1541
1542 if (end > start) {
1543 if (bufnr >= start && bufnr < end)
1544 return 1;
1545 else
1546 return 0;
1547 }
1548
1549 /* wrap-around case */
1550 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1551 (bufnr < end))
1552 return 1;
1553 else
1554 return 0;
1555}
1556
1557/**
1558 * handle_inbound - reset processed input buffers
1559 * @q: queue containing the buffers
1560 * @callflags: flags
1561 * @bufnr: first buffer to process
1562 * @count: how many buffers are emptied
1563 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001564static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1565 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001566{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001567 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001568
Jan Glauber6486cda2010-01-04 09:05:42 +01001569 qperf_inc(q, inbound_call);
1570
Jan Glauber50f769d2008-12-25 13:38:47 +01001571 if (!q->u.in.polling)
1572 goto set;
1573
1574 /* protect against stop polling setting an ACK for an emptied slsb */
1575 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1576 /* overwriting everything, just delete polling status */
1577 q->u.in.polling = 0;
1578 q->u.in.ack_count = 0;
1579 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001580 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001581 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001582 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001583 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001584 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001585 q->u.in.ack_count -= diff;
1586 if (q->u.in.ack_count <= 0) {
1587 q->u.in.polling = 0;
1588 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001589 goto set;
1590 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001591 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001592 }
1593 else
1594 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001595 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001596 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001597
Jan Glauber50f769d2008-12-25 13:38:47 +01001598set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001599 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001600
1601 used = atomic_add_return(count, &q->nr_buf_used) - count;
1602 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1603
Jan Glauberd303b6f2009-03-26 15:24:31 +01001604 if (need_siga_in(q))
1605 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001606
Jan Glauberd303b6f2009-03-26 15:24:31 +01001607 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001608}
1609
1610/**
1611 * handle_outbound - process filled outbound buffers
1612 * @q: queue containing the buffers
1613 * @callflags: flags
1614 * @bufnr: first buffer to process
1615 * @count: how many buffers are filled
1616 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001617static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1618 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001619{
Jan Glauberc26001d2011-05-23 10:24:38 +02001620 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001621 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001622
Jan Glauber6486cda2010-01-04 09:05:42 +01001623 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001624
1625 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1626 used = atomic_add_return(count, &q->nr_buf_used);
1627 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1628
Jan Glauber01958432011-01-05 12:47:51 +01001629 if (used == QDIO_MAX_BUFFERS_PER_Q)
1630 qperf_inc(q, outbound_queue_full);
1631
Jan Glauber6486cda2010-01-04 09:05:42 +01001632 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001633 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001634 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001635 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001636 q->u.out.pci_out_enabled = 0;
1637
1638 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001639 unsigned long phys_aob = 0;
1640
1641 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001642 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1643
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001644 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1645
1646 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001647 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001648 rc = qdio_siga_sync_q(q);
1649 } else {
1650 /* try to fast requeue buffers */
1651 get_buf_state(q, prev_buf(bufnr), &state, 0);
1652 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001653 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001654 else
Jan Glauber110da312011-01-05 12:47:53 +01001655 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001656 }
1657
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001658 /* in case of SIGA errors we must process the error immediately */
1659 if (used >= q->u.out.scan_threshold || rc)
1660 tasklet_schedule(&q->tasklet);
1661 else
1662 /* free the SBALs in case of no further traffic */
1663 if (!timer_pending(&q->u.out.timer))
1664 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001665 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001666}
1667
1668/**
1669 * do_QDIO - process input or output buffers
1670 * @cdev: associated ccw_device for the qdio subchannel
1671 * @callflags: input or output and special flags from the program
1672 * @q_nr: queue number
1673 * @bufnr: buffer number
1674 * @count: how many buffers to process
1675 */
1676int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001677 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001678{
1679 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001680
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001681
Jan Glauber66182412009-06-22 12:08:15 +02001682 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001683 return -EINVAL;
1684
Jan Glauber779e6e12008-07-17 17:16:48 +02001685 irq_ptr = cdev->private->qdio_data;
1686 if (!irq_ptr)
1687 return -ENODEV;
1688
Jan Glauber1d7e1502009-09-22 22:58:39 +02001689 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1690 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001691
1692 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1693 return -EBUSY;
Jan Glauber9a265132011-03-23 10:16:01 +01001694 if (!count)
1695 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001696 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001697 return handle_inbound(irq_ptr->input_qs[q_nr],
1698 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001699 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001700 return handle_outbound(irq_ptr->output_qs[q_nr],
1701 callflags, bufnr, count);
1702 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001703}
1704EXPORT_SYMBOL_GPL(do_QDIO);
1705
Jan Glauberd36deae2010-09-07 21:14:39 +00001706/**
1707 * qdio_start_irq - process input buffers
1708 * @cdev: associated ccw_device for the qdio subchannel
1709 * @nr: input queue number
1710 *
1711 * Return codes
1712 * 0 - success
1713 * 1 - irqs not started since new data is available
1714 */
1715int qdio_start_irq(struct ccw_device *cdev, int nr)
1716{
1717 struct qdio_q *q;
1718 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1719
1720 if (!irq_ptr)
1721 return -ENODEV;
1722 q = irq_ptr->input_qs[nr];
1723
1724 WARN_ON(queue_irqs_enabled(q));
1725
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001726 if (!shared_ind(q))
Jan Glauberd36deae2010-09-07 21:14:39 +00001727 xchg(q->irq_ptr->dsci, 0);
1728
1729 qdio_stop_polling(q);
1730 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1731
1732 /*
1733 * We need to check again to not lose initiative after
1734 * resetting the ACK state.
1735 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001736 if (!shared_ind(q) && *q->irq_ptr->dsci)
Jan Glauberd36deae2010-09-07 21:14:39 +00001737 goto rescan;
1738 if (!qdio_inbound_q_done(q))
1739 goto rescan;
1740 return 0;
1741
1742rescan:
1743 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1744 &q->u.in.queue_irq_state))
1745 return 0;
1746 else
1747 return 1;
1748
1749}
1750EXPORT_SYMBOL(qdio_start_irq);
1751
1752/**
1753 * qdio_get_next_buffers - process input buffers
1754 * @cdev: associated ccw_device for the qdio subchannel
1755 * @nr: input queue number
1756 * @bufnr: first filled buffer number
1757 * @error: buffers are in error state
1758 *
1759 * Return codes
1760 * < 0 - error
1761 * = 0 - no new buffers found
1762 * > 0 - number of processed buffers
1763 */
1764int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1765 int *error)
1766{
1767 struct qdio_q *q;
1768 int start, end;
1769 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1770
1771 if (!irq_ptr)
1772 return -ENODEV;
1773 q = irq_ptr->input_qs[nr];
1774 WARN_ON(queue_irqs_enabled(q));
1775
Jan Glauberd36deae2010-09-07 21:14:39 +00001776 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001777 * Cannot rely on automatic sync after interrupt since queues may
1778 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001779 */
Jan Glauber90adac52011-01-05 12:47:54 +01001780 if (need_siga_sync(q))
1781 qdio_sync_queues(q);
1782
1783 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001784 qdio_check_outbound_after_thinint(q);
1785
1786 if (!qdio_inbound_q_moved(q))
1787 return 0;
1788
1789 /* Note: upper-layer MUST stop processing immediately here ... */
1790 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1791 return -EIO;
1792
1793 start = q->first_to_kick;
1794 end = q->first_to_check;
1795 *bufnr = start;
1796 *error = q->qdio_error;
1797
1798 /* for the next time */
1799 q->first_to_kick = end;
1800 q->qdio_error = 0;
1801 return sub_buf(end, start);
1802}
1803EXPORT_SYMBOL(qdio_get_next_buffers);
1804
1805/**
1806 * qdio_stop_irq - disable interrupt processing for the device
1807 * @cdev: associated ccw_device for the qdio subchannel
1808 * @nr: input queue number
1809 *
1810 * Return codes
1811 * 0 - interrupts were already disabled
1812 * 1 - interrupts successfully disabled
1813 */
1814int qdio_stop_irq(struct ccw_device *cdev, int nr)
1815{
1816 struct qdio_q *q;
1817 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1818
1819 if (!irq_ptr)
1820 return -ENODEV;
1821 q = irq_ptr->input_qs[nr];
1822
1823 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1824 &q->u.in.queue_irq_state))
1825 return 0;
1826 else
1827 return 1;
1828}
1829EXPORT_SYMBOL(qdio_stop_irq);
1830
Jan Glauber779e6e12008-07-17 17:16:48 +02001831static int __init init_QDIO(void)
1832{
1833 int rc;
1834
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001835 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001836 if (rc)
1837 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001838 rc = qdio_setup_init();
1839 if (rc)
1840 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001841 rc = tiqdio_allocate_memory();
1842 if (rc)
1843 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001844 rc = tiqdio_register_thinints();
1845 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001846 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001847 return 0;
1848
Jan Glauber779e6e12008-07-17 17:16:48 +02001849out_ti:
1850 tiqdio_free_memory();
1851out_cache:
1852 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001853out_debug:
1854 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001855 return rc;
1856}
1857
1858static void __exit exit_QDIO(void)
1859{
1860 tiqdio_unregister_thinints();
1861 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001862 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001863 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001864}
1865
1866module_init(init_QDIO);
1867module_exit(exit_QDIO);