blob: 7c567b2268a769544429ffb7e5ee57d44ce9d6e5 [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);
453 return;
454 }
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
463 /*
464 * Interrupts may be avoided as long as the error is present
465 * so change the buffer state immediately to avoid starvation.
466 */
467 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100468}
Jan Glauber779e6e12008-07-17 17:16:48 +0200469
Jan Glauber50f769d2008-12-25 13:38:47 +0100470static inline void inbound_primed(struct qdio_q *q, int count)
471{
472 int new;
473
Jan Glauber1d7e1502009-09-22 22:58:39 +0200474 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100475
476 /* for QEBSM the ACK was already set by EQBS */
477 if (is_qebsm(q)) {
478 if (!q->u.in.polling) {
479 q->u.in.polling = 1;
480 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100481 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100482 return;
483 }
484
485 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100486 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100487 q->u.in.ack_count);
488 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100489 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100490 return;
491 }
492
493 /*
494 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
495 * or by the next inbound run.
496 */
497 new = add_buf(q->first_to_check, count - 1);
498 if (q->u.in.polling) {
499 /* reset the previous ACK but first set the new one */
500 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100501 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100502 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100503 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100504 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100505 }
506
Jan Glaubere85dea02009-03-26 15:24:29 +0100507 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100508 count--;
509 if (!count)
510 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200511 /* need to change ALL buffers to get more interrupts */
512 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200513}
514
515static int get_inbound_buffer_frontier(struct qdio_q *q)
516{
517 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100518 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200519
Jan Glaubera2b86012011-10-30 15:17:05 +0100520 q->timestamp = get_clock_fast();
521
Jan Glauber779e6e12008-07-17 17:16:48 +0200522 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200523 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
524 * would return 0.
525 */
526 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
527 stop = add_buf(q->first_to_check, count);
528
Jan Glauber779e6e12008-07-17 17:16:48 +0200529 if (q->first_to_check == stop)
530 goto out;
531
Jan Glauber36e3e722009-06-22 12:08:12 +0200532 /*
533 * No siga sync here, as a PCI or we after a thin interrupt
534 * already sync'ed the queues.
535 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000536 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200537 if (!count)
538 goto out;
539
540 switch (state) {
541 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100542 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200543 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100544 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100545 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100546 if (q->irq_ptr->perf_stat_enabled)
547 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200548 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200549 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200550 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200551 q->first_to_check = add_buf(q->first_to_check, count);
552 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100553 if (q->irq_ptr->perf_stat_enabled)
554 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200555 break;
556 case SLSB_CU_INPUT_EMPTY:
557 case SLSB_P_INPUT_NOT_INIT:
558 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100559 if (q->irq_ptr->perf_stat_enabled)
560 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100561 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200562 break;
563 default:
564 BUG();
565 }
566out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200567 return q->first_to_check;
568}
569
Jan Glauber60b5df22009-06-22 12:08:10 +0200570static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200571{
572 int bufnr;
573
574 bufnr = get_inbound_buffer_frontier(q);
575
Jan Glaubere85dea02009-03-26 15:24:29 +0100576 if ((bufnr != q->last_move) || q->qdio_error) {
577 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100578 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber3a601bf2010-05-17 10:00:17 +0200579 q->u.in.timestamp = get_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200580 return 1;
581 } else
582 return 0;
583}
584
Jan Glauber9a2c1602009-06-22 12:08:11 +0200585static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200586{
587 unsigned char state = 0;
588
589 if (!atomic_read(&q->nr_buf_used))
590 return 1;
591
Jan Glauber90adac52011-01-05 12:47:54 +0100592 if (need_siga_sync(q))
593 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200594 get_buf_state(q, q->first_to_check, &state, 0);
595
Ursula Braun4c522282010-02-09 09:46:07 +0100596 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200597 /* more work coming */
598 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200599
600 if (is_thinint_irq(q->irq_ptr))
601 return 1;
602
603 /* don't poll under z/VM */
604 if (MACHINE_IS_VM)
605 return 1;
606
607 /*
608 * At this point we know, that inbound first_to_check
609 * has (probably) not moved (see qdio_inbound_processing).
610 */
Jan Glauber3a601bf2010-05-17 10:00:17 +0200611 if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200612 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200613 q->first_to_check);
614 return 1;
615 } else
616 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200617}
618
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000619static inline int contains_aobs(struct qdio_q *q)
620{
621 return !q->is_input_q && q->u.out.use_cq;
622}
623
624static inline void qdio_trace_aob(struct qdio_irq *irq, struct qdio_q *q,
625 int i, struct qaob *aob)
626{
627 int tmp;
628
629 DBF_DEV_EVENT(DBF_INFO, irq, "AOB%d:%lx", i,
630 (unsigned long) virt_to_phys(aob));
631 DBF_DEV_EVENT(DBF_INFO, irq, "RES00:%lx",
632 (unsigned long) aob->res0[0]);
633 DBF_DEV_EVENT(DBF_INFO, irq, "RES01:%lx",
634 (unsigned long) aob->res0[1]);
635 DBF_DEV_EVENT(DBF_INFO, irq, "RES02:%lx",
636 (unsigned long) aob->res0[2]);
637 DBF_DEV_EVENT(DBF_INFO, irq, "RES03:%lx",
638 (unsigned long) aob->res0[3]);
639 DBF_DEV_EVENT(DBF_INFO, irq, "RES04:%lx",
640 (unsigned long) aob->res0[4]);
641 DBF_DEV_EVENT(DBF_INFO, irq, "RES05:%lx",
642 (unsigned long) aob->res0[5]);
643 DBF_DEV_EVENT(DBF_INFO, irq, "RES1:%x", aob->res1);
644 DBF_DEV_EVENT(DBF_INFO, irq, "RES2:%x", aob->res2);
645 DBF_DEV_EVENT(DBF_INFO, irq, "RES3:%x", aob->res3);
646 DBF_DEV_EVENT(DBF_INFO, irq, "AORC:%u", aob->aorc);
647 DBF_DEV_EVENT(DBF_INFO, irq, "FLAGS:%u", aob->flags);
648 DBF_DEV_EVENT(DBF_INFO, irq, "CBTBS:%u", aob->cbtbs);
649 DBF_DEV_EVENT(DBF_INFO, irq, "SBC:%u", aob->sb_count);
650 for (tmp = 0; tmp < QDIO_MAX_ELEMENTS_PER_BUFFER; ++tmp) {
651 DBF_DEV_EVENT(DBF_INFO, irq, "SBA%d:%lx", tmp,
652 (unsigned long) aob->sba[tmp]);
653 DBF_DEV_EVENT(DBF_INFO, irq, "rSBA%d:%lx", tmp,
654 (unsigned long) q->sbal[i]->element[tmp].addr);
655 DBF_DEV_EVENT(DBF_INFO, irq, "DC%d:%u", tmp, aob->dcount[tmp]);
656 DBF_DEV_EVENT(DBF_INFO, irq, "rDC%d:%u", tmp,
657 q->sbal[i]->element[tmp].length);
658 }
659 DBF_DEV_EVENT(DBF_INFO, irq, "USER0:%lx", (unsigned long) aob->user0);
660 for (tmp = 0; tmp < 2; ++tmp) {
661 DBF_DEV_EVENT(DBF_INFO, irq, "RES4%d:%lx", tmp,
662 (unsigned long) aob->res4[tmp]);
663 }
664 DBF_DEV_EVENT(DBF_INFO, irq, "USER1:%lx", (unsigned long) aob->user1);
665 DBF_DEV_EVENT(DBF_INFO, irq, "USER2:%lx", (unsigned long) aob->user2);
666}
667
668static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
669{
670 unsigned char state = 0;
671 int j, b = start;
672
673 if (!contains_aobs(q))
674 return;
675
676 for (j = 0; j < count; ++j) {
677 get_buf_state(q, b, &state, 0);
678 if (state == SLSB_P_OUTPUT_PENDING) {
679 struct qaob *aob = q->u.out.aobs[b];
680 if (aob == NULL)
681 continue;
682
683 BUG_ON(q->u.out.sbal_state == NULL);
684 q->u.out.sbal_state[b].flags |=
685 QDIO_OUTBUF_STATE_FLAG_PENDING;
686 q->u.out.aobs[b] = NULL;
687 } else if (state == SLSB_P_OUTPUT_EMPTY) {
688 BUG_ON(q->u.out.sbal_state == NULL);
689 q->u.out.sbal_state[b].aob = NULL;
690 }
691 b = next_buf(b);
692 }
693}
694
695static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
696 int bufnr)
697{
698 unsigned long phys_aob = 0;
699
700 if (!q->use_cq)
701 goto out;
702
703 if (!q->aobs[bufnr]) {
704 struct qaob *aob = qdio_allocate_aob();
705 q->aobs[bufnr] = aob;
706 }
707 if (q->aobs[bufnr]) {
708 BUG_ON(q->sbal_state == NULL);
709 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
710 q->sbal_state[bufnr].aob = q->aobs[bufnr];
711 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
712 phys_aob = virt_to_phys(q->aobs[bufnr]);
713 BUG_ON(phys_aob & 0xFF);
714 }
715
716out:
717 return phys_aob;
718}
719
Jan Glauber60b5df22009-06-22 12:08:10 +0200720static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200721{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100722 int start = q->first_to_kick;
723 int end = q->first_to_check;
724 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200725
726 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
727 return;
728
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100729 count = sub_buf(end, start);
730
731 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100732 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200733 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100734 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100735 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200736 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
737 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100738 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100739
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000740 qdio_handle_aobs(q, start, count);
741
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100742 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
743 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200744
745 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100746 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200747 q->qdio_error = 0;
748}
749
750static void __qdio_inbound_processing(struct qdio_q *q)
751{
Jan Glauber6486cda2010-01-04 09:05:42 +0100752 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200753
Jan Glauber779e6e12008-07-17 17:16:48 +0200754 if (!qdio_inbound_q_moved(q))
755 return;
756
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100757 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200758
Jan Glauber6486cda2010-01-04 09:05:42 +0100759 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200760 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100761 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200762 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
763 tasklet_schedule(&q->tasklet);
764 return;
765 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100766 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200767
768 qdio_stop_polling(q);
769 /*
770 * We need to check again to not lose initiative after
771 * resetting the ACK state.
772 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100773 if (!qdio_inbound_q_done(q)) {
774 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200775 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
776 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100777 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200778}
779
Jan Glauber779e6e12008-07-17 17:16:48 +0200780void qdio_inbound_processing(unsigned long data)
781{
782 struct qdio_q *q = (struct qdio_q *)data;
783 __qdio_inbound_processing(q);
784}
785
786static int get_outbound_buffer_frontier(struct qdio_q *q)
787{
788 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100789 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200790
Jan Glaubera2b86012011-10-30 15:17:05 +0100791 q->timestamp = get_clock_fast();
792
Jan Glauber90adac52011-01-05 12:47:54 +0100793 if (need_siga_sync(q))
794 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
795 !pci_out_supported(q)) ||
796 (queue_type(q) == QDIO_IQDIO_QFMT &&
797 multicast_outbound(q)))
798 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200799
800 /*
801 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
802 * would return 0.
803 */
804 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
805 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200806 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000807 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200808
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000809 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200810 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000811 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200812
813 switch (state) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000814 case SLSB_P_OUTPUT_PENDING:
815 BUG();
Jan Glauber779e6e12008-07-17 17:16:48 +0200816 case SLSB_P_OUTPUT_EMPTY:
817 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000818 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
819 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200820
821 atomic_sub(count, &q->nr_buf_used);
822 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100823 if (q->irq_ptr->perf_stat_enabled)
824 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000825
Jan Glauber36e3e722009-06-22 12:08:12 +0200826 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200827 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200828 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200829 q->first_to_check = add_buf(q->first_to_check, count);
830 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100831 if (q->irq_ptr->perf_stat_enabled)
832 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200833 break;
834 case SLSB_CU_OUTPUT_PRIMED:
835 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100836 if (q->irq_ptr->perf_stat_enabled)
837 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000838 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
839 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200840 break;
841 case SLSB_P_OUTPUT_NOT_INIT:
842 case SLSB_P_OUTPUT_HALTED:
843 break;
844 default:
845 BUG();
846 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000847
848out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200849 return q->first_to_check;
850}
851
852/* all buffers processed? */
853static inline int qdio_outbound_q_done(struct qdio_q *q)
854{
855 return atomic_read(&q->nr_buf_used) == 0;
856}
857
858static inline int qdio_outbound_q_moved(struct qdio_q *q)
859{
860 int bufnr;
861
862 bufnr = get_outbound_buffer_frontier(q);
863
Jan Glaubere85dea02009-03-26 15:24:29 +0100864 if ((bufnr != q->last_move) || q->qdio_error) {
865 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100866 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200867 return 1;
868 } else
869 return 0;
870}
871
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000872static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200873{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200874 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100875 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200876
877 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100878 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200879
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100880 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200881retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100882 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100883
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000884 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100885 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200886 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200887 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100888 case 2:
889 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200890 while (++retries < QDIO_BUSY_BIT_RETRIES) {
891 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
892 goto retry;
893 }
894 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauberd303b6f2009-03-26 15:24:31 +0100895 cc |= QDIO_ERROR_SIGA_BUSY;
896 } else
897 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100898 break;
899 case 1:
900 case 3:
901 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100902 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200903 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200904 if (retries) {
905 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
906 DBF_ERROR("count:%u", retries);
907 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100908 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200909}
910
Jan Glauber779e6e12008-07-17 17:16:48 +0200911static void __qdio_outbound_processing(struct qdio_q *q)
912{
Jan Glauber6486cda2010-01-04 09:05:42 +0100913 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200914 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
915
916 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100917 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200918
Jan Glauberc38f9602009-03-26 15:24:26 +0100919 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200920 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100921 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200922
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200923 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
Jan Glauberc38f9602009-03-26 15:24:26 +0100924 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
925 goto sched;
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200926
Jan Glauber779e6e12008-07-17 17:16:48 +0200927 if (q->u.out.pci_out_enabled)
928 return;
929
930 /*
931 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100932 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
933 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200934 */
935 if (qdio_outbound_q_done(q))
936 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100937 else
938 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200939 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100940 return;
941
942sched:
943 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
944 return;
945 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200946}
947
948/* outbound tasklet */
949void qdio_outbound_processing(unsigned long data)
950{
951 struct qdio_q *q = (struct qdio_q *)data;
952 __qdio_outbound_processing(q);
953}
954
955void qdio_outbound_timer(unsigned long data)
956{
957 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100958
959 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
960 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200961 tasklet_schedule(&q->tasklet);
962}
963
Jan Glauber60b5df22009-06-22 12:08:10 +0200964static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200965{
966 struct qdio_q *out;
967 int i;
968
969 if (!pci_out_supported(q))
970 return;
971
972 for_each_output_queue(q->irq_ptr, out, i)
973 if (!qdio_outbound_q_done(out))
974 tasklet_schedule(&out->tasklet);
975}
976
Jan Glauber60b5df22009-06-22 12:08:10 +0200977static void __tiqdio_inbound_processing(struct qdio_q *q)
978{
Jan Glauber6486cda2010-01-04 09:05:42 +0100979 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100980 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
981 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200982
983 /*
984 * The interrupt could be caused by a PCI request. Check the
985 * PCI capable outbound queues.
986 */
987 qdio_check_outbound_after_thinint(q);
988
989 if (!qdio_inbound_q_moved(q))
990 return;
991
992 qdio_kick_handler(q);
993
Jan Glauber9a2c1602009-06-22 12:08:11 +0200994 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100995 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200996 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200997 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200998 return;
999 }
Jan Glauber60b5df22009-06-22 12:08:10 +02001000 }
1001
1002 qdio_stop_polling(q);
1003 /*
1004 * We need to check again to not lose initiative after
1005 * resetting the ACK state.
1006 */
Jan Glauber9a2c1602009-06-22 12:08:11 +02001007 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +01001008 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +02001009 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
1010 tasklet_schedule(&q->tasklet);
1011 }
1012}
1013
1014void tiqdio_inbound_processing(unsigned long data)
1015{
1016 struct qdio_q *q = (struct qdio_q *)data;
1017 __tiqdio_inbound_processing(q);
1018}
1019
Jan Glauber779e6e12008-07-17 17:16:48 +02001020static inline void qdio_set_state(struct qdio_irq *irq_ptr,
1021 enum qdio_irq_states state)
1022{
Jan Glauber22f99342008-12-25 13:38:46 +01001023 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +02001024
1025 irq_ptr->state = state;
1026 mb();
1027}
1028
Jan Glauber22f99342008-12-25 13:38:46 +01001029static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +02001030{
Jan Glauber779e6e12008-07-17 17:16:48 +02001031 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +01001032 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
1033 DBF_ERROR_HEX(irb, 64);
1034 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +02001035 }
1036}
1037
1038/* PCI interrupt handler */
1039static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
1040{
1041 int i;
1042 struct qdio_q *q;
1043
Jan Glauberc38f9602009-03-26 15:24:26 +01001044 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
1045 return;
1046
Jan Glauberd36deae2010-09-07 21:14:39 +00001047 for_each_input_queue(irq_ptr, q, i) {
1048 if (q->u.in.queue_start_poll) {
1049 /* skip if polling is enabled or already in work */
1050 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1051 &q->u.in.queue_irq_state)) {
1052 qperf_inc(q, int_discarded);
1053 continue;
1054 }
1055 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1056 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001057 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +00001058 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001059 }
Jan Glauberd36deae2010-09-07 21:14:39 +00001060 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001061
Jan Glauber90adac52011-01-05 12:47:54 +01001062 if (!pci_out_supported(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001063 return;
1064
1065 for_each_output_queue(irq_ptr, q, i) {
1066 if (qdio_outbound_q_done(q))
1067 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001068 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001069 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001070 tasklet_schedule(&q->tasklet);
1071 }
1072}
1073
1074static void qdio_handle_activate_check(struct ccw_device *cdev,
1075 unsigned long intparm, int cstat, int dstat)
1076{
1077 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1078 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001079 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001080
Jan Glauber22f99342008-12-25 13:38:46 +01001081 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1082 DBF_ERROR("intp :%lx", intparm);
1083 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001084
1085 if (irq_ptr->nr_input_qs) {
1086 q = irq_ptr->input_qs[0];
1087 } else if (irq_ptr->nr_output_qs) {
1088 q = irq_ptr->output_qs[0];
1089 } else {
1090 dump_stack();
1091 goto no_handler;
1092 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001093
1094 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber779e6e12008-07-17 17:16:48 +02001095 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001096 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001097no_handler:
1098 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1099}
1100
Jan Glauber779e6e12008-07-17 17:16:48 +02001101static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1102 int dstat)
1103{
1104 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001105
Jan Glauber22f99342008-12-25 13:38:46 +01001106 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001107
1108 if (cstat)
1109 goto error;
1110 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1111 goto error;
1112 if (!(dstat & DEV_STAT_DEV_END))
1113 goto error;
1114 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1115 return;
1116
1117error:
1118 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1119 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1120 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001121}
1122
1123/* qdio interrupt handler */
1124void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1125 struct irb *irb)
1126{
1127 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1128 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001129
Jan Glauber779e6e12008-07-17 17:16:48 +02001130 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +01001131 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001132 return;
1133 }
1134
Jan Glauber09a308f2010-05-17 10:00:14 +02001135 if (irq_ptr->perf_stat_enabled)
1136 irq_ptr->perf_stat.qdio_int++;
1137
Jan Glauber779e6e12008-07-17 17:16:48 +02001138 if (IS_ERR(irb)) {
1139 switch (PTR_ERR(irb)) {
1140 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +01001141 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +02001142 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1143 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001144 return;
1145 default:
1146 WARN_ON(1);
1147 return;
1148 }
1149 }
Jan Glauber22f99342008-12-25 13:38:46 +01001150 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001151 cstat = irb->scsw.cmd.cstat;
1152 dstat = irb->scsw.cmd.dstat;
1153
1154 switch (irq_ptr->state) {
1155 case QDIO_IRQ_STATE_INACTIVE:
1156 qdio_establish_handle_irq(cdev, cstat, dstat);
1157 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001158 case QDIO_IRQ_STATE_CLEANUP:
1159 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1160 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001161 case QDIO_IRQ_STATE_ESTABLISHED:
1162 case QDIO_IRQ_STATE_ACTIVE:
1163 if (cstat & SCHN_STAT_PCI) {
1164 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001165 return;
1166 }
Jan Glauber4c575422009-06-12 10:26:28 +02001167 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001168 qdio_handle_activate_check(cdev, intparm, cstat,
1169 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001170 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001171 case QDIO_IRQ_STATE_STOPPED:
1172 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001173 default:
1174 WARN_ON(1);
1175 }
1176 wake_up(&cdev->private->wait_q);
1177}
1178
1179/**
1180 * qdio_get_ssqd_desc - get qdio subchannel description
1181 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001182 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001183 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001184 * Returns 0 or an error code. The results of the chsc are stored in the
1185 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001186 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001187int qdio_get_ssqd_desc(struct ccw_device *cdev,
1188 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001189{
Jan Glauber779e6e12008-07-17 17:16:48 +02001190
Jan Glauberbbd50e12008-12-25 13:38:43 +01001191 if (!cdev || !cdev->private)
1192 return -EINVAL;
1193
Jan Glauber22f99342008-12-25 13:38:46 +01001194 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001195 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001196}
1197EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1198
Jan Glauber779e6e12008-07-17 17:16:48 +02001199static void qdio_shutdown_queues(struct ccw_device *cdev)
1200{
1201 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1202 struct qdio_q *q;
1203 int i;
1204
1205 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001206 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001207
1208 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001209 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001210 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001211 }
1212}
1213
1214/**
1215 * qdio_shutdown - shut down a qdio subchannel
1216 * @cdev: associated ccw device
1217 * @how: use halt or clear to shutdown
1218 */
1219int qdio_shutdown(struct ccw_device *cdev, int how)
1220{
Jan Glauber22f99342008-12-25 13:38:46 +01001221 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001222 int rc;
1223 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001224
Jan Glauber779e6e12008-07-17 17:16:48 +02001225 if (!irq_ptr)
1226 return -ENODEV;
1227
Jan Glauberb4547402009-03-26 15:24:24 +01001228 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001229 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1230
Jan Glauber779e6e12008-07-17 17:16:48 +02001231 mutex_lock(&irq_ptr->setup_mutex);
1232 /*
1233 * Subchannel was already shot down. We cannot prevent being called
1234 * twice since cio may trigger a shutdown asynchronously.
1235 */
1236 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1237 mutex_unlock(&irq_ptr->setup_mutex);
1238 return 0;
1239 }
1240
Jan Glauberc38f9602009-03-26 15:24:26 +01001241 /*
1242 * Indicate that the device is going down. Scheduling the queue
1243 * tasklets is forbidden from here on.
1244 */
1245 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1246
Jan Glauber779e6e12008-07-17 17:16:48 +02001247 tiqdio_remove_input_queues(irq_ptr);
1248 qdio_shutdown_queues(cdev);
1249 qdio_shutdown_debug_entries(irq_ptr, cdev);
1250
1251 /* cleanup subchannel */
1252 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1253
1254 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1255 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1256 else
1257 /* default behaviour is halt */
1258 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1259 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001260 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1261 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001262 goto no_cleanup;
1263 }
1264
1265 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1266 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1267 wait_event_interruptible_timeout(cdev->private->wait_q,
1268 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1269 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1270 10 * HZ);
1271 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1272
1273no_cleanup:
1274 qdio_shutdown_thinint(irq_ptr);
1275
1276 /* restore interrupt handler */
1277 if ((void *)cdev->handler == (void *)qdio_int_handler)
1278 cdev->handler = irq_ptr->orig_handler;
1279 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1280
1281 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1282 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001283 if (rc)
1284 return rc;
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(qdio_shutdown);
1288
1289/**
1290 * qdio_free - free data structures for a qdio subchannel
1291 * @cdev: associated ccw device
1292 */
1293int qdio_free(struct ccw_device *cdev)
1294{
Jan Glauber22f99342008-12-25 13:38:46 +01001295 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001296
Jan Glauber779e6e12008-07-17 17:16:48 +02001297 if (!irq_ptr)
1298 return -ENODEV;
1299
Jan Glauber22f99342008-12-25 13:38:46 +01001300 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001301 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001302
1303 if (irq_ptr->debug_area != NULL) {
1304 debug_unregister(irq_ptr->debug_area);
1305 irq_ptr->debug_area = NULL;
1306 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001307 cdev->private->qdio_data = NULL;
1308 mutex_unlock(&irq_ptr->setup_mutex);
1309
1310 qdio_release_memory(irq_ptr);
1311 return 0;
1312}
1313EXPORT_SYMBOL_GPL(qdio_free);
1314
1315/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001316 * qdio_allocate - allocate qdio queues and associated data
1317 * @init_data: initialization data
1318 */
1319int qdio_allocate(struct qdio_initialize *init_data)
1320{
1321 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001322
Jan Glauber22f99342008-12-25 13:38:46 +01001323 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001324
1325 if ((init_data->no_input_qs && !init_data->input_handler) ||
1326 (init_data->no_output_qs && !init_data->output_handler))
1327 return -EINVAL;
1328
1329 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1330 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1331 return -EINVAL;
1332
1333 if ((!init_data->input_sbal_addr_array) ||
1334 (!init_data->output_sbal_addr_array))
1335 return -EINVAL;
1336
Jan Glauber779e6e12008-07-17 17:16:48 +02001337 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1338 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1339 if (!irq_ptr)
1340 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001341
1342 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001343 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001344
1345 /*
1346 * Allocate a page for the chsc calls in qdio_establish.
1347 * Must be pre-allocated since a zfcp recovery will call
1348 * qdio_establish. In case of low memory and swap on a zfcp disk
1349 * we may not be able to allocate memory otherwise.
1350 */
1351 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1352 if (!irq_ptr->chsc_page)
1353 goto out_rel;
1354
1355 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001356 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001357 if (!irq_ptr->qdr)
1358 goto out_rel;
1359 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1360
Jan Glauber779e6e12008-07-17 17:16:48 +02001361 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1362 init_data->no_output_qs))
1363 goto out_rel;
1364
1365 init_data->cdev->private->qdio_data = irq_ptr;
1366 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1367 return 0;
1368out_rel:
1369 qdio_release_memory(irq_ptr);
1370out_err:
1371 return -ENOMEM;
1372}
1373EXPORT_SYMBOL_GPL(qdio_allocate);
1374
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001375static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1376{
1377 struct qdio_q *q = irq_ptr->input_qs[0];
1378 int i, use_cq = 0;
1379
1380 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1381 use_cq = 1;
1382
1383 for_each_output_queue(irq_ptr, q, i) {
1384 if (use_cq) {
1385 if (qdio_enable_async_operation(&q->u.out) < 0) {
1386 use_cq = 0;
1387 continue;
1388 }
1389 } else
1390 qdio_disable_async_operation(&q->u.out);
1391 }
1392 DBF_EVENT("use_cq:%d", use_cq);
1393}
1394
Jan Glauber779e6e12008-07-17 17:16:48 +02001395/**
1396 * qdio_establish - establish queues on a qdio subchannel
1397 * @init_data: initialization data
1398 */
1399int qdio_establish(struct qdio_initialize *init_data)
1400{
Jan Glauber779e6e12008-07-17 17:16:48 +02001401 struct qdio_irq *irq_ptr;
1402 struct ccw_device *cdev = init_data->cdev;
1403 unsigned long saveflags;
1404 int rc;
1405
Jan Glauber22f99342008-12-25 13:38:46 +01001406 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001407
Jan Glauber779e6e12008-07-17 17:16:48 +02001408 irq_ptr = cdev->private->qdio_data;
1409 if (!irq_ptr)
1410 return -ENODEV;
1411
1412 if (cdev->private->state != DEV_STATE_ONLINE)
1413 return -EINVAL;
1414
Jan Glauber779e6e12008-07-17 17:16:48 +02001415 mutex_lock(&irq_ptr->setup_mutex);
1416 qdio_setup_irq(init_data);
1417
1418 rc = qdio_establish_thinint(irq_ptr);
1419 if (rc) {
1420 mutex_unlock(&irq_ptr->setup_mutex);
1421 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1422 return rc;
1423 }
1424
1425 /* establish q */
1426 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1427 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1428 irq_ptr->ccw.count = irq_ptr->equeue.count;
1429 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1430
1431 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1432 ccw_device_set_options_mask(cdev, 0);
1433
1434 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1435 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001436 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1437 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001438 }
1439 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1440
1441 if (rc) {
1442 mutex_unlock(&irq_ptr->setup_mutex);
1443 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1444 return rc;
1445 }
1446
1447 wait_event_interruptible_timeout(cdev->private->wait_q,
1448 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1449 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1450
1451 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1452 mutex_unlock(&irq_ptr->setup_mutex);
1453 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1454 return -EIO;
1455 }
1456
1457 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber22f99342008-12-25 13:38:46 +01001458 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
Jan Glauber779e6e12008-07-17 17:16:48 +02001459
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001460 qdio_detect_hsicq(irq_ptr);
1461
Jan Glauber779e6e12008-07-17 17:16:48 +02001462 /* qebsm is now setup if available, initialize buffer states */
1463 qdio_init_buf_states(irq_ptr);
1464
1465 mutex_unlock(&irq_ptr->setup_mutex);
1466 qdio_print_subchannel_info(irq_ptr, cdev);
1467 qdio_setup_debug_entries(irq_ptr, cdev);
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(qdio_establish);
1471
1472/**
1473 * qdio_activate - activate queues on a qdio subchannel
1474 * @cdev: associated cdev
1475 */
1476int qdio_activate(struct ccw_device *cdev)
1477{
1478 struct qdio_irq *irq_ptr;
1479 int rc;
1480 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001481
Jan Glauber22f99342008-12-25 13:38:46 +01001482 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001483
Jan Glauber779e6e12008-07-17 17:16:48 +02001484 irq_ptr = cdev->private->qdio_data;
1485 if (!irq_ptr)
1486 return -ENODEV;
1487
1488 if (cdev->private->state != DEV_STATE_ONLINE)
1489 return -EINVAL;
1490
1491 mutex_lock(&irq_ptr->setup_mutex);
1492 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1493 rc = -EBUSY;
1494 goto out;
1495 }
1496
Jan Glauber779e6e12008-07-17 17:16:48 +02001497 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1498 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1499 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1500 irq_ptr->ccw.cda = 0;
1501
1502 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1503 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1504
1505 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1506 0, DOIO_DENY_PREFETCH);
1507 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001508 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1509 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001510 }
1511 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1512
1513 if (rc)
1514 goto out;
1515
1516 if (is_thinint_irq(irq_ptr))
1517 tiqdio_add_input_queues(irq_ptr);
1518
1519 /* wait for subchannel to become active */
1520 msleep(5);
1521
1522 switch (irq_ptr->state) {
1523 case QDIO_IRQ_STATE_STOPPED:
1524 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001525 rc = -EIO;
1526 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001527 default:
1528 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1529 rc = 0;
1530 }
1531out:
1532 mutex_unlock(&irq_ptr->setup_mutex);
1533 return rc;
1534}
1535EXPORT_SYMBOL_GPL(qdio_activate);
1536
1537static inline int buf_in_between(int bufnr, int start, int count)
1538{
1539 int end = add_buf(start, count);
1540
1541 if (end > start) {
1542 if (bufnr >= start && bufnr < end)
1543 return 1;
1544 else
1545 return 0;
1546 }
1547
1548 /* wrap-around case */
1549 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1550 (bufnr < end))
1551 return 1;
1552 else
1553 return 0;
1554}
1555
1556/**
1557 * handle_inbound - reset processed input buffers
1558 * @q: queue containing the buffers
1559 * @callflags: flags
1560 * @bufnr: first buffer to process
1561 * @count: how many buffers are emptied
1562 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001563static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1564 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001565{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001566 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001567
Jan Glauber6486cda2010-01-04 09:05:42 +01001568 qperf_inc(q, inbound_call);
1569
Jan Glauber50f769d2008-12-25 13:38:47 +01001570 if (!q->u.in.polling)
1571 goto set;
1572
1573 /* protect against stop polling setting an ACK for an emptied slsb */
1574 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1575 /* overwriting everything, just delete polling status */
1576 q->u.in.polling = 0;
1577 q->u.in.ack_count = 0;
1578 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001579 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001580 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001581 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001582 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001583 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001584 q->u.in.ack_count -= diff;
1585 if (q->u.in.ack_count <= 0) {
1586 q->u.in.polling = 0;
1587 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001588 goto set;
1589 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001590 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001591 }
1592 else
1593 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001594 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001595 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001596
Jan Glauber50f769d2008-12-25 13:38:47 +01001597set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001598 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001599
1600 used = atomic_add_return(count, &q->nr_buf_used) - count;
1601 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1602
Jan Glauberd303b6f2009-03-26 15:24:31 +01001603 if (need_siga_in(q))
1604 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001605
Jan Glauberd303b6f2009-03-26 15:24:31 +01001606 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001607}
1608
1609/**
1610 * handle_outbound - process filled outbound buffers
1611 * @q: queue containing the buffers
1612 * @callflags: flags
1613 * @bufnr: first buffer to process
1614 * @count: how many buffers are filled
1615 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001616static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1617 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001618{
Jan Glauberc26001d2011-05-23 10:24:38 +02001619 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001620 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001621
Jan Glauber6486cda2010-01-04 09:05:42 +01001622 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001623
1624 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1625 used = atomic_add_return(count, &q->nr_buf_used);
1626 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1627
Jan Glauber01958432011-01-05 12:47:51 +01001628 if (used == QDIO_MAX_BUFFERS_PER_Q)
1629 qperf_inc(q, outbound_queue_full);
1630
Jan Glauber6486cda2010-01-04 09:05:42 +01001631 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001632 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001633 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001634 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001635 q->u.out.pci_out_enabled = 0;
1636
1637 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001638 unsigned long phys_aob = 0;
1639
1640 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001641 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1642
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001643 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1644
1645 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001646 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001647 rc = qdio_siga_sync_q(q);
1648 } else {
1649 /* try to fast requeue buffers */
1650 get_buf_state(q, prev_buf(bufnr), &state, 0);
1651 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001652 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001653 else
Jan Glauber110da312011-01-05 12:47:53 +01001654 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001655 }
1656
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001657 /* in case of SIGA errors we must process the error immediately */
1658 if (used >= q->u.out.scan_threshold || rc)
1659 tasklet_schedule(&q->tasklet);
1660 else
1661 /* free the SBALs in case of no further traffic */
1662 if (!timer_pending(&q->u.out.timer))
1663 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001664 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001665}
1666
1667/**
1668 * do_QDIO - process input or output buffers
1669 * @cdev: associated ccw_device for the qdio subchannel
1670 * @callflags: input or output and special flags from the program
1671 * @q_nr: queue number
1672 * @bufnr: buffer number
1673 * @count: how many buffers to process
1674 */
1675int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001676 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001677{
1678 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001679
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001680
Jan Glauber66182412009-06-22 12:08:15 +02001681 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001682 return -EINVAL;
1683
Jan Glauber779e6e12008-07-17 17:16:48 +02001684 irq_ptr = cdev->private->qdio_data;
1685 if (!irq_ptr)
1686 return -ENODEV;
1687
Jan Glauber1d7e1502009-09-22 22:58:39 +02001688 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1689 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001690
1691 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1692 return -EBUSY;
Jan Glauber9a265132011-03-23 10:16:01 +01001693 if (!count)
1694 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001695 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001696 return handle_inbound(irq_ptr->input_qs[q_nr],
1697 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001698 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001699 return handle_outbound(irq_ptr->output_qs[q_nr],
1700 callflags, bufnr, count);
1701 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001702}
1703EXPORT_SYMBOL_GPL(do_QDIO);
1704
Jan Glauberd36deae2010-09-07 21:14:39 +00001705/**
1706 * qdio_start_irq - process input buffers
1707 * @cdev: associated ccw_device for the qdio subchannel
1708 * @nr: input queue number
1709 *
1710 * Return codes
1711 * 0 - success
1712 * 1 - irqs not started since new data is available
1713 */
1714int qdio_start_irq(struct ccw_device *cdev, int nr)
1715{
1716 struct qdio_q *q;
1717 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1718
1719 if (!irq_ptr)
1720 return -ENODEV;
1721 q = irq_ptr->input_qs[nr];
1722
1723 WARN_ON(queue_irqs_enabled(q));
1724
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001725 if (!shared_ind(q))
Jan Glauberd36deae2010-09-07 21:14:39 +00001726 xchg(q->irq_ptr->dsci, 0);
1727
1728 qdio_stop_polling(q);
1729 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1730
1731 /*
1732 * We need to check again to not lose initiative after
1733 * resetting the ACK state.
1734 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001735 if (!shared_ind(q) && *q->irq_ptr->dsci)
Jan Glauberd36deae2010-09-07 21:14:39 +00001736 goto rescan;
1737 if (!qdio_inbound_q_done(q))
1738 goto rescan;
1739 return 0;
1740
1741rescan:
1742 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1743 &q->u.in.queue_irq_state))
1744 return 0;
1745 else
1746 return 1;
1747
1748}
1749EXPORT_SYMBOL(qdio_start_irq);
1750
1751/**
1752 * qdio_get_next_buffers - process input buffers
1753 * @cdev: associated ccw_device for the qdio subchannel
1754 * @nr: input queue number
1755 * @bufnr: first filled buffer number
1756 * @error: buffers are in error state
1757 *
1758 * Return codes
1759 * < 0 - error
1760 * = 0 - no new buffers found
1761 * > 0 - number of processed buffers
1762 */
1763int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1764 int *error)
1765{
1766 struct qdio_q *q;
1767 int start, end;
1768 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1769
1770 if (!irq_ptr)
1771 return -ENODEV;
1772 q = irq_ptr->input_qs[nr];
1773 WARN_ON(queue_irqs_enabled(q));
1774
Jan Glauberd36deae2010-09-07 21:14:39 +00001775 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001776 * Cannot rely on automatic sync after interrupt since queues may
1777 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001778 */
Jan Glauber90adac52011-01-05 12:47:54 +01001779 if (need_siga_sync(q))
1780 qdio_sync_queues(q);
1781
1782 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001783 qdio_check_outbound_after_thinint(q);
1784
1785 if (!qdio_inbound_q_moved(q))
1786 return 0;
1787
1788 /* Note: upper-layer MUST stop processing immediately here ... */
1789 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1790 return -EIO;
1791
1792 start = q->first_to_kick;
1793 end = q->first_to_check;
1794 *bufnr = start;
1795 *error = q->qdio_error;
1796
1797 /* for the next time */
1798 q->first_to_kick = end;
1799 q->qdio_error = 0;
1800 return sub_buf(end, start);
1801}
1802EXPORT_SYMBOL(qdio_get_next_buffers);
1803
1804/**
1805 * qdio_stop_irq - disable interrupt processing for the device
1806 * @cdev: associated ccw_device for the qdio subchannel
1807 * @nr: input queue number
1808 *
1809 * Return codes
1810 * 0 - interrupts were already disabled
1811 * 1 - interrupts successfully disabled
1812 */
1813int qdio_stop_irq(struct ccw_device *cdev, int nr)
1814{
1815 struct qdio_q *q;
1816 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1817
1818 if (!irq_ptr)
1819 return -ENODEV;
1820 q = irq_ptr->input_qs[nr];
1821
1822 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1823 &q->u.in.queue_irq_state))
1824 return 0;
1825 else
1826 return 1;
1827}
1828EXPORT_SYMBOL(qdio_stop_irq);
1829
Jan Glauber779e6e12008-07-17 17:16:48 +02001830static int __init init_QDIO(void)
1831{
1832 int rc;
1833
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001834 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001835 if (rc)
1836 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001837 rc = qdio_setup_init();
1838 if (rc)
1839 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001840 rc = tiqdio_allocate_memory();
1841 if (rc)
1842 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001843 rc = tiqdio_register_thinints();
1844 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001845 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001846 return 0;
1847
Jan Glauber779e6e12008-07-17 17:16:48 +02001848out_ti:
1849 tiqdio_free_memory();
1850out_cache:
1851 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001852out_debug:
1853 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001854 return rc;
1855}
1856
1857static void __exit exit_QDIO(void)
1858{
1859 tiqdio_unregister_thinints();
1860 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001861 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001862 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001863}
1864
1865module_init(init_QDIO);
1866module_exit(exit_QDIO);