blob: a76d6764ce670b74beba3f3f83dda16491c3bff0 [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 Sharma60063492011-07-26 16:09:06 -070018#include <linux/atomic.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020019#include <asm/debug.h>
20#include <asm/qdio.h>
21
22#include "cio.h"
23#include "css.h"
24#include "device.h"
25#include "qdio.h"
26#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020027
28MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 "Jan Glauber <jang@linux.vnet.ibm.com>");
30MODULE_DESCRIPTION("QDIO base support");
31MODULE_LICENSE("GPL");
32
Jan Glauber958c0ba2011-01-05 12:47:52 +010033static inline int do_siga_sync(unsigned long schid,
34 unsigned int out_mask, unsigned int in_mask,
35 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020036{
Jan Glauber958c0ba2011-01-05 12:47:52 +010037 register unsigned long __fc asm ("0") = fc;
38 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020039 register unsigned long out asm ("2") = out_mask;
40 register unsigned long in asm ("3") = in_mask;
41 int cc;
42
43 asm volatile(
44 " siga 0\n"
45 " ipm %0\n"
46 " srl %0,28\n"
47 : "=d" (cc)
48 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
49 return cc;
50}
51
Jan Glauber958c0ba2011-01-05 12:47:52 +010052static inline int do_siga_input(unsigned long schid, unsigned int mask,
53 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020054{
Jan Glauber958c0ba2011-01-05 12:47:52 +010055 register unsigned long __fc asm ("0") = fc;
56 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020057 register unsigned long __mask asm ("2") = mask;
58 int cc;
59
60 asm volatile(
61 " siga 0\n"
62 " ipm %0\n"
63 " srl %0,28\n"
64 : "=d" (cc)
65 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
66 return cc;
67}
68
69/**
70 * do_siga_output - perform SIGA-w/wt function
71 * @schid: subchannel id or in case of QEBSM the subchannel token
72 * @mask: which output queues to process
73 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
74 * @fc: function code to perform
75 *
76 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
77 * Note: For IQDC unicast queues only the highest priority queue is processed.
78 */
79static inline int do_siga_output(unsigned long schid, unsigned long mask,
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;
107 /* not all buffers processed */
108 if (ccq == 96 || ccq == 97)
109 return 1;
110 /* notify devices immediately */
Jan Glauber22f99342008-12-25 13:38:46 +0100111 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200112 return -EIO;
113}
114
115/**
116 * qdio_do_eqbs - extract buffer states for QEBSM
117 * @q: queue to manipulate
118 * @state: state of the extracted buffers
119 * @start: buffer number to start at
120 * @count: count of buffers to examine
Jan Glauber50f769d2008-12-25 13:38:47 +0100121 * @auto_ack: automatically acknowledge buffers
Jan Glauber779e6e12008-07-17 17:16:48 +0200122 *
Coly Li73ac36e2009-01-07 18:09:16 -0800123 * Returns the number of successfully extracted equal buffer states.
Jan Glauber779e6e12008-07-17 17:16:48 +0200124 * Stops processing if a state is different from the last buffers state.
125 */
126static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
Jan Glauber50f769d2008-12-25 13:38:47 +0100127 int start, int count, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200128{
129 unsigned int ccq = 0;
130 int tmp_count = count, tmp_start = start;
131 int nr = q->nr;
132 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200133
134 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100135 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200136
137 if (!q->is_input_q)
138 nr += q->irq_ptr->nr_input_qs;
139again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100140 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
141 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200142 rc = qdio_check_ccq(q, ccq);
143
144 /* At least one buffer was processed, return and extract the remaining
145 * buffers later.
146 */
Jan Glauber23589d02008-12-25 13:38:44 +0100147 if ((ccq == 96) && (count != tmp_count)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100148 qperf_inc(q, eqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200149 return (count - tmp_count);
Jan Glauber23589d02008-12-25 13:38:44 +0100150 }
Jan Glauber22f99342008-12-25 13:38:46 +0100151
Jan Glauber779e6e12008-07-17 17:16:48 +0200152 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100153 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200154 goto again;
155 }
156
157 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100158 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
159 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200160 q->handler(q->irq_ptr->cdev,
161 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200162 q->nr, q->first_to_kick, count,
163 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200164 return 0;
165 }
166 return count - tmp_count;
167}
168
169/**
170 * qdio_do_sqbs - set buffer states for QEBSM
171 * @q: queue to manipulate
172 * @state: new state of the buffers
173 * @start: first buffer number to change
174 * @count: how many buffers to change
175 *
176 * Returns the number of successfully changed buffers.
177 * Does retrying until the specified count of buffer states is set or an
178 * error occurs.
179 */
180static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
181 int count)
182{
183 unsigned int ccq = 0;
184 int tmp_count = count, tmp_start = start;
185 int nr = q->nr;
186 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200187
Jan Glauber50f769d2008-12-25 13:38:47 +0100188 if (!count)
189 return 0;
190
Jan Glauber779e6e12008-07-17 17:16:48 +0200191 BUG_ON(!q->irq_ptr->sch_token);
Jan Glauber6486cda2010-01-04 09:05:42 +0100192 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200193
194 if (!q->is_input_q)
195 nr += q->irq_ptr->nr_input_qs;
196again:
197 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
198 rc = qdio_check_ccq(q, ccq);
199 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100200 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100201 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200202 goto again;
203 }
204 if (rc < 0) {
Jan Glauber22f99342008-12-25 13:38:46 +0100205 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
206 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200207 q->handler(q->irq_ptr->cdev,
208 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
Swen Schilligdfe5bb52011-08-15 14:40:31 +0200209 q->nr, q->first_to_kick, count,
210 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200211 return 0;
212 }
213 WARN_ON(tmp_count);
214 return count - tmp_count;
215}
216
217/* returns number of examined buffers and their common state in *state */
218static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100219 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000220 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200221{
222 unsigned char __state = 0;
223 int i;
224
225 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
226 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
227
228 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100229 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200230
231 for (i = 0; i < count; i++) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000232 if (!__state) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200233 __state = q->slsb.val[bufnr];
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000234 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
235 __state = SLSB_P_OUTPUT_EMPTY;
236 } else if (merge_pending) {
237 if ((q->slsb.val[bufnr] & __state) != __state)
238 break;
239 } else if (q->slsb.val[bufnr] != __state)
Jan Glauber779e6e12008-07-17 17:16:48 +0200240 break;
241 bufnr = next_buf(bufnr);
242 }
243 *state = __state;
244 return i;
245}
246
Jan Glauber60b5df22009-06-22 12:08:10 +0200247static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
248 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200249{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000250 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200251}
252
253/* wrap-around safe setting of slsb states, returns number of changed buffers */
254static inline int set_buf_states(struct qdio_q *q, int bufnr,
255 unsigned char state, int count)
256{
257 int i;
258
259 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
260 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
261
262 if (is_qebsm(q))
263 return qdio_do_sqbs(q, state, bufnr, count);
264
265 for (i = 0; i < count; i++) {
266 xchg(&q->slsb.val[bufnr], state);
267 bufnr = next_buf(bufnr);
268 }
269 return count;
270}
271
272static inline int set_buf_state(struct qdio_q *q, int bufnr,
273 unsigned char state)
274{
275 return set_buf_states(q, bufnr, state, 1);
276}
277
278/* set slsb states to initial state */
279void qdio_init_buf_states(struct qdio_irq *irq_ptr)
280{
281 struct qdio_q *q;
282 int i;
283
284 for_each_input_queue(irq_ptr, q, i)
285 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
286 QDIO_MAX_BUFFERS_PER_Q);
287 for_each_output_queue(irq_ptr, q, i)
288 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
289 QDIO_MAX_BUFFERS_PER_Q);
290}
291
Jan Glauber60b5df22009-06-22 12:08:10 +0200292static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200293 unsigned int input)
294{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100295 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
296 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200297 int cc;
298
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100299 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100300 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200301
Jan Glauber958c0ba2011-01-05 12:47:52 +0100302 if (is_qebsm(q)) {
303 schid = q->irq_ptr->sch_token;
304 fc |= QDIO_SIGA_QEBSM_FLAG;
305 }
306
307 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100308 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100309 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200310 return cc;
311}
312
Jan Glauber60b5df22009-06-22 12:08:10 +0200313static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200314{
315 if (q->is_input_q)
316 return qdio_siga_sync(q, 0, q->mask);
317 else
318 return qdio_siga_sync(q, q->mask, 0);
319}
320
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000321static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
322 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200323{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100324 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
325 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100326 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200327 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000328 unsigned long laob = 0;
329
330 if (q->u.out.use_cq && aob != 0) {
331 fc = QDIO_SIGA_WRITEQ;
332 laob = aob;
333 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200334
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100335 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200336 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100337 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200338 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200339again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000340 WARN_ON_ONCE((aob && queue_type(q) != QDIO_IQDIO_QFMT) ||
341 (aob && fc != QDIO_SIGA_WRITEQ));
342 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200343
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100344 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100345 if (unlikely(*busy_bit)) {
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100346 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200347 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100348
349 if (!start_time) {
Jan Glauber3a601bf2010-05-17 10:00:17 +0200350 start_time = get_clock();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100351 goto again;
352 }
Jan Glauber3a601bf2010-05-17 10:00:17 +0200353 if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200354 goto again;
355 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200356 if (retries) {
357 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
358 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
359 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
360 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200361 return cc;
362}
363
364static inline int qdio_siga_input(struct qdio_q *q)
365{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100366 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
367 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200368 int cc;
369
Jan Glauber22f99342008-12-25 13:38:46 +0100370 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100371 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200372
Jan Glauber958c0ba2011-01-05 12:47:52 +0100373 if (is_qebsm(q)) {
374 schid = q->irq_ptr->sch_token;
375 fc |= QDIO_SIGA_QEBSM_FLAG;
376 }
377
378 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100379 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100380 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber779e6e12008-07-17 17:16:48 +0200381 return cc;
382}
383
Jan Glauber90adac52011-01-05 12:47:54 +0100384#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
385#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
386
387static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200388{
Jan Glauber90adac52011-01-05 12:47:54 +0100389 /* PCI capable outbound queues will also be scanned so sync them too */
390 if (pci_out_supported(q))
391 qdio_siga_sync_all(q);
392 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200393 qdio_siga_sync_q(q);
394}
395
Jan Glauber60b5df22009-06-22 12:08:10 +0200396int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
397 unsigned char *state)
398{
Jan Glauber90adac52011-01-05 12:47:54 +0100399 if (need_siga_sync(q))
400 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000401 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200402}
403
404static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200405{
Jan Glauber50f769d2008-12-25 13:38:47 +0100406 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200407 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100408
Jan Glauber779e6e12008-07-17 17:16:48 +0200409 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100410 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200411
412 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100413 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100414 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100415 q->u.in.ack_count);
416 q->u.in.ack_count = 0;
417 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100418 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200419}
420
Jan Glauberd3072972010-02-26 22:37:36 +0100421static inline void account_sbals(struct qdio_q *q, int count)
422{
423 int pos = 0;
424
425 q->q_stats.nr_sbal_total += count;
426 if (count == QDIO_MAX_BUFFERS_MASK) {
427 q->q_stats.nr_sbals[7]++;
428 return;
429 }
430 while (count >>= 1)
431 pos++;
432 q->q_stats.nr_sbals[pos]++;
433}
434
Jan Glauberbffbbd22011-04-20 10:15:33 +0200435static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200436{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200437 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
438 SLSB_P_OUTPUT_NOT_INIT;
439
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100440 q->qdio_error |= QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100441
442 /* special handling for no target buffer empty */
443 if ((!q->is_input_q &&
Jan Glauber3ec90872011-06-06 14:14:40 +0200444 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100445 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200446 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100447 q->first_to_check);
448 return;
449 }
450
Jan Glauber22f99342008-12-25 13:38:46 +0100451 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
452 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100453 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100454 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec90872011-06-06 14:14:40 +0200455 q->sbal[q->first_to_check]->element[14].sflags,
456 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200457
458 /*
459 * Interrupts may be avoided as long as the error is present
460 * so change the buffer state immediately to avoid starvation.
461 */
462 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100463}
Jan Glauber779e6e12008-07-17 17:16:48 +0200464
Jan Glauber50f769d2008-12-25 13:38:47 +0100465static inline void inbound_primed(struct qdio_q *q, int count)
466{
467 int new;
468
Jan Glauber1d7e1502009-09-22 22:58:39 +0200469 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100470
471 /* for QEBSM the ACK was already set by EQBS */
472 if (is_qebsm(q)) {
473 if (!q->u.in.polling) {
474 q->u.in.polling = 1;
475 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100476 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100477 return;
478 }
479
480 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100481 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100482 q->u.in.ack_count);
483 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100484 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100485 return;
486 }
487
488 /*
489 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
490 * or by the next inbound run.
491 */
492 new = add_buf(q->first_to_check, count - 1);
493 if (q->u.in.polling) {
494 /* reset the previous ACK but first set the new one */
495 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100496 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100497 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100498 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100499 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100500 }
501
Jan Glaubere85dea02009-03-26 15:24:29 +0100502 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100503 count--;
504 if (!count)
505 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200506 /* need to change ALL buffers to get more interrupts */
507 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200508}
509
510static int get_inbound_buffer_frontier(struct qdio_q *q)
511{
512 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100513 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200514
Jan Glaubera2b86012011-10-30 15:17:05 +0100515 q->timestamp = get_clock_fast();
516
Jan Glauber779e6e12008-07-17 17:16:48 +0200517 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200518 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
519 * would return 0.
520 */
521 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
522 stop = add_buf(q->first_to_check, count);
523
Jan Glauber779e6e12008-07-17 17:16:48 +0200524 if (q->first_to_check == stop)
525 goto out;
526
Jan Glauber36e3e722009-06-22 12:08:12 +0200527 /*
528 * No siga sync here, as a PCI or we after a thin interrupt
529 * already sync'ed the queues.
530 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000531 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200532 if (!count)
533 goto out;
534
535 switch (state) {
536 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100537 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200538 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauber8bcd9b02009-12-18 17:43:26 +0100539 if (atomic_sub(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100540 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100541 if (q->irq_ptr->perf_stat_enabled)
542 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200543 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200544 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200545 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200546 q->first_to_check = add_buf(q->first_to_check, count);
547 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100548 if (q->irq_ptr->perf_stat_enabled)
549 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200550 break;
551 case SLSB_CU_INPUT_EMPTY:
552 case SLSB_P_INPUT_NOT_INIT:
553 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100554 if (q->irq_ptr->perf_stat_enabled)
555 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100556 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200557 break;
558 default:
559 BUG();
560 }
561out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200562 return q->first_to_check;
563}
564
Jan Glauber60b5df22009-06-22 12:08:10 +0200565static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200566{
567 int bufnr;
568
569 bufnr = get_inbound_buffer_frontier(q);
570
Jan Glaubere85dea02009-03-26 15:24:29 +0100571 if ((bufnr != q->last_move) || q->qdio_error) {
572 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100573 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Jan Glauber3a601bf2010-05-17 10:00:17 +0200574 q->u.in.timestamp = get_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200575 return 1;
576 } else
577 return 0;
578}
579
Jan Glauber9a2c1602009-06-22 12:08:11 +0200580static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200581{
582 unsigned char state = 0;
583
584 if (!atomic_read(&q->nr_buf_used))
585 return 1;
586
Jan Glauber90adac52011-01-05 12:47:54 +0100587 if (need_siga_sync(q))
588 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200589 get_buf_state(q, q->first_to_check, &state, 0);
590
Ursula Braun4c522282010-02-09 09:46:07 +0100591 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200592 /* more work coming */
593 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200594
595 if (is_thinint_irq(q->irq_ptr))
596 return 1;
597
598 /* don't poll under z/VM */
599 if (MACHINE_IS_VM)
600 return 1;
601
602 /*
603 * At this point we know, that inbound first_to_check
604 * has (probably) not moved (see qdio_inbound_processing).
605 */
Jan Glauber3a601bf2010-05-17 10:00:17 +0200606 if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200607 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200608 q->first_to_check);
609 return 1;
610 } else
611 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200612}
613
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000614static inline int contains_aobs(struct qdio_q *q)
615{
616 return !q->is_input_q && q->u.out.use_cq;
617}
618
619static inline void qdio_trace_aob(struct qdio_irq *irq, struct qdio_q *q,
620 int i, struct qaob *aob)
621{
622 int tmp;
623
624 DBF_DEV_EVENT(DBF_INFO, irq, "AOB%d:%lx", i,
625 (unsigned long) virt_to_phys(aob));
626 DBF_DEV_EVENT(DBF_INFO, irq, "RES00:%lx",
627 (unsigned long) aob->res0[0]);
628 DBF_DEV_EVENT(DBF_INFO, irq, "RES01:%lx",
629 (unsigned long) aob->res0[1]);
630 DBF_DEV_EVENT(DBF_INFO, irq, "RES02:%lx",
631 (unsigned long) aob->res0[2]);
632 DBF_DEV_EVENT(DBF_INFO, irq, "RES03:%lx",
633 (unsigned long) aob->res0[3]);
634 DBF_DEV_EVENT(DBF_INFO, irq, "RES04:%lx",
635 (unsigned long) aob->res0[4]);
636 DBF_DEV_EVENT(DBF_INFO, irq, "RES05:%lx",
637 (unsigned long) aob->res0[5]);
638 DBF_DEV_EVENT(DBF_INFO, irq, "RES1:%x", aob->res1);
639 DBF_DEV_EVENT(DBF_INFO, irq, "RES2:%x", aob->res2);
640 DBF_DEV_EVENT(DBF_INFO, irq, "RES3:%x", aob->res3);
641 DBF_DEV_EVENT(DBF_INFO, irq, "AORC:%u", aob->aorc);
642 DBF_DEV_EVENT(DBF_INFO, irq, "FLAGS:%u", aob->flags);
643 DBF_DEV_EVENT(DBF_INFO, irq, "CBTBS:%u", aob->cbtbs);
644 DBF_DEV_EVENT(DBF_INFO, irq, "SBC:%u", aob->sb_count);
645 for (tmp = 0; tmp < QDIO_MAX_ELEMENTS_PER_BUFFER; ++tmp) {
646 DBF_DEV_EVENT(DBF_INFO, irq, "SBA%d:%lx", tmp,
647 (unsigned long) aob->sba[tmp]);
648 DBF_DEV_EVENT(DBF_INFO, irq, "rSBA%d:%lx", tmp,
649 (unsigned long) q->sbal[i]->element[tmp].addr);
650 DBF_DEV_EVENT(DBF_INFO, irq, "DC%d:%u", tmp, aob->dcount[tmp]);
651 DBF_DEV_EVENT(DBF_INFO, irq, "rDC%d:%u", tmp,
652 q->sbal[i]->element[tmp].length);
653 }
654 DBF_DEV_EVENT(DBF_INFO, irq, "USER0:%lx", (unsigned long) aob->user0);
655 for (tmp = 0; tmp < 2; ++tmp) {
656 DBF_DEV_EVENT(DBF_INFO, irq, "RES4%d:%lx", tmp,
657 (unsigned long) aob->res4[tmp]);
658 }
659 DBF_DEV_EVENT(DBF_INFO, irq, "USER1:%lx", (unsigned long) aob->user1);
660 DBF_DEV_EVENT(DBF_INFO, irq, "USER2:%lx", (unsigned long) aob->user2);
661}
662
663static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
664{
665 unsigned char state = 0;
666 int j, b = start;
667
668 if (!contains_aobs(q))
669 return;
670
671 for (j = 0; j < count; ++j) {
672 get_buf_state(q, b, &state, 0);
673 if (state == SLSB_P_OUTPUT_PENDING) {
674 struct qaob *aob = q->u.out.aobs[b];
675 if (aob == NULL)
676 continue;
677
678 BUG_ON(q->u.out.sbal_state == NULL);
679 q->u.out.sbal_state[b].flags |=
680 QDIO_OUTBUF_STATE_FLAG_PENDING;
681 q->u.out.aobs[b] = NULL;
682 } else if (state == SLSB_P_OUTPUT_EMPTY) {
683 BUG_ON(q->u.out.sbal_state == NULL);
684 q->u.out.sbal_state[b].aob = NULL;
685 }
686 b = next_buf(b);
687 }
688}
689
690static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
691 int bufnr)
692{
693 unsigned long phys_aob = 0;
694
695 if (!q->use_cq)
696 goto out;
697
698 if (!q->aobs[bufnr]) {
699 struct qaob *aob = qdio_allocate_aob();
700 q->aobs[bufnr] = aob;
701 }
702 if (q->aobs[bufnr]) {
703 BUG_ON(q->sbal_state == NULL);
704 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
705 q->sbal_state[bufnr].aob = q->aobs[bufnr];
706 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
707 phys_aob = virt_to_phys(q->aobs[bufnr]);
708 BUG_ON(phys_aob & 0xFF);
709 }
710
711out:
712 return phys_aob;
713}
714
Jan Glauber60b5df22009-06-22 12:08:10 +0200715static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200716{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100717 int start = q->first_to_kick;
718 int end = q->first_to_check;
719 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200720
721 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
722 return;
723
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100724 count = sub_buf(end, start);
725
726 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100727 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200728 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100729 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100730 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200731 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
732 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100733 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100734
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000735 qdio_handle_aobs(q, start, count);
736
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100737 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
738 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200739
740 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100741 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200742 q->qdio_error = 0;
743}
744
745static void __qdio_inbound_processing(struct qdio_q *q)
746{
Jan Glauber6486cda2010-01-04 09:05:42 +0100747 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200748
Jan Glauber779e6e12008-07-17 17:16:48 +0200749 if (!qdio_inbound_q_moved(q))
750 return;
751
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100752 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200753
Jan Glauber6486cda2010-01-04 09:05:42 +0100754 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200755 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100756 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200757 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
758 tasklet_schedule(&q->tasklet);
759 return;
760 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100761 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200762
763 qdio_stop_polling(q);
764 /*
765 * We need to check again to not lose initiative after
766 * resetting the ACK state.
767 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100768 if (!qdio_inbound_q_done(q)) {
769 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200770 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
771 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100772 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200773}
774
Jan Glauber779e6e12008-07-17 17:16:48 +0200775void qdio_inbound_processing(unsigned long data)
776{
777 struct qdio_q *q = (struct qdio_q *)data;
778 __qdio_inbound_processing(q);
779}
780
781static int get_outbound_buffer_frontier(struct qdio_q *q)
782{
783 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100784 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200785
Jan Glaubera2b86012011-10-30 15:17:05 +0100786 q->timestamp = get_clock_fast();
787
Jan Glauber90adac52011-01-05 12:47:54 +0100788 if (need_siga_sync(q))
789 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
790 !pci_out_supported(q)) ||
791 (queue_type(q) == QDIO_IQDIO_QFMT &&
792 multicast_outbound(q)))
793 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200794
795 /*
796 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
797 * would return 0.
798 */
799 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
800 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200801 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000802 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200803
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000804 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200805 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000806 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200807
808 switch (state) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000809 case SLSB_P_OUTPUT_PENDING:
810 BUG();
Jan Glauber779e6e12008-07-17 17:16:48 +0200811 case SLSB_P_OUTPUT_EMPTY:
812 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000813 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
814 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200815
816 atomic_sub(count, &q->nr_buf_used);
817 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100818 if (q->irq_ptr->perf_stat_enabled)
819 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000820
Jan Glauber36e3e722009-06-22 12:08:12 +0200821 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200822 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200823 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200824 q->first_to_check = add_buf(q->first_to_check, count);
825 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100826 if (q->irq_ptr->perf_stat_enabled)
827 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200828 break;
829 case SLSB_CU_OUTPUT_PRIMED:
830 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100831 if (q->irq_ptr->perf_stat_enabled)
832 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000833 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
834 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200835 break;
836 case SLSB_P_OUTPUT_NOT_INIT:
837 case SLSB_P_OUTPUT_HALTED:
838 break;
839 default:
840 BUG();
841 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000842
843out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200844 return q->first_to_check;
845}
846
847/* all buffers processed? */
848static inline int qdio_outbound_q_done(struct qdio_q *q)
849{
850 return atomic_read(&q->nr_buf_used) == 0;
851}
852
853static inline int qdio_outbound_q_moved(struct qdio_q *q)
854{
855 int bufnr;
856
857 bufnr = get_outbound_buffer_frontier(q);
858
Jan Glaubere85dea02009-03-26 15:24:29 +0100859 if ((bufnr != q->last_move) || q->qdio_error) {
860 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100861 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200862 return 1;
863 } else
864 return 0;
865}
866
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000867static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200868{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200869 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100870 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200871
872 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100873 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200874
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100875 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200876retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100877 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100878
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000879 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100880 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200881 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200882 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100883 case 2:
884 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200885 while (++retries < QDIO_BUSY_BIT_RETRIES) {
886 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
887 goto retry;
888 }
889 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauberd303b6f2009-03-26 15:24:31 +0100890 cc |= QDIO_ERROR_SIGA_BUSY;
891 } else
892 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100893 break;
894 case 1:
895 case 3:
896 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100897 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200898 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200899 if (retries) {
900 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
901 DBF_ERROR("count:%u", retries);
902 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100903 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200904}
905
Jan Glauber779e6e12008-07-17 17:16:48 +0200906static void __qdio_outbound_processing(struct qdio_q *q)
907{
Jan Glauber6486cda2010-01-04 09:05:42 +0100908 qperf_inc(q, tasklet_outbound);
Jan Glauber779e6e12008-07-17 17:16:48 +0200909 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
910
911 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100912 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200913
Jan Glauberc38f9602009-03-26 15:24:26 +0100914 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200915 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100916 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200917
918 /* bail out for HiperSockets unicast queues */
919 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
920 return;
921
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200922 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
Jan Glauberc38f9602009-03-26 15:24:26 +0100923 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
924 goto sched;
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200925
Jan Glauber779e6e12008-07-17 17:16:48 +0200926 if (q->u.out.pci_out_enabled)
927 return;
928
929 /*
930 * Now we know that queue type is either qeth without pci enabled
931 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
932 * EMPTY is noticed and outbound_handler is called after some time.
933 */
934 if (qdio_outbound_q_done(q))
935 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100936 else
937 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200938 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100939 return;
940
941sched:
942 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
943 return;
944 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200945}
946
947/* outbound tasklet */
948void qdio_outbound_processing(unsigned long data)
949{
950 struct qdio_q *q = (struct qdio_q *)data;
951 __qdio_outbound_processing(q);
952}
953
954void qdio_outbound_timer(unsigned long data)
955{
956 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100957
958 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
959 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200960 tasklet_schedule(&q->tasklet);
961}
962
Jan Glauber60b5df22009-06-22 12:08:10 +0200963static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200964{
965 struct qdio_q *out;
966 int i;
967
968 if (!pci_out_supported(q))
969 return;
970
971 for_each_output_queue(q->irq_ptr, out, i)
972 if (!qdio_outbound_q_done(out))
973 tasklet_schedule(&out->tasklet);
974}
975
Jan Glauber60b5df22009-06-22 12:08:10 +0200976static void __tiqdio_inbound_processing(struct qdio_q *q)
977{
Jan Glauber6486cda2010-01-04 09:05:42 +0100978 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100979 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
980 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200981
982 /*
983 * The interrupt could be caused by a PCI request. Check the
984 * PCI capable outbound queues.
985 */
986 qdio_check_outbound_after_thinint(q);
987
988 if (!qdio_inbound_q_moved(q))
989 return;
990
991 qdio_kick_handler(q);
992
Jan Glauber9a2c1602009-06-22 12:08:11 +0200993 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100994 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200995 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200996 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200997 return;
998 }
Jan Glauber60b5df22009-06-22 12:08:10 +0200999 }
1000
1001 qdio_stop_polling(q);
1002 /*
1003 * We need to check again to not lose initiative after
1004 * resetting the ACK state.
1005 */
Jan Glauber9a2c1602009-06-22 12:08:11 +02001006 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +01001007 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +02001008 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
1009 tasklet_schedule(&q->tasklet);
1010 }
1011}
1012
1013void tiqdio_inbound_processing(unsigned long data)
1014{
1015 struct qdio_q *q = (struct qdio_q *)data;
1016 __tiqdio_inbound_processing(q);
1017}
1018
Jan Glauber779e6e12008-07-17 17:16:48 +02001019static inline void qdio_set_state(struct qdio_irq *irq_ptr,
1020 enum qdio_irq_states state)
1021{
Jan Glauber22f99342008-12-25 13:38:46 +01001022 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +02001023
1024 irq_ptr->state = state;
1025 mb();
1026}
1027
Jan Glauber22f99342008-12-25 13:38:46 +01001028static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +02001029{
Jan Glauber779e6e12008-07-17 17:16:48 +02001030 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +01001031 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
1032 DBF_ERROR_HEX(irb, 64);
1033 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +02001034 }
1035}
1036
1037/* PCI interrupt handler */
1038static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
1039{
1040 int i;
1041 struct qdio_q *q;
1042
Jan Glauberc38f9602009-03-26 15:24:26 +01001043 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
1044 return;
1045
Jan Glauberd36deae2010-09-07 21:14:39 +00001046 for_each_input_queue(irq_ptr, q, i) {
1047 if (q->u.in.queue_start_poll) {
1048 /* skip if polling is enabled or already in work */
1049 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1050 &q->u.in.queue_irq_state)) {
1051 qperf_inc(q, int_discarded);
1052 continue;
1053 }
1054 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1055 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001056 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +00001057 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001058 }
Jan Glauberd36deae2010-09-07 21:14:39 +00001059 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001060
Jan Glauber90adac52011-01-05 12:47:54 +01001061 if (!pci_out_supported(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001062 return;
1063
1064 for_each_output_queue(irq_ptr, q, i) {
1065 if (qdio_outbound_q_done(q))
1066 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001067 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001068 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001069 tasklet_schedule(&q->tasklet);
1070 }
1071}
1072
1073static void qdio_handle_activate_check(struct ccw_device *cdev,
1074 unsigned long intparm, int cstat, int dstat)
1075{
1076 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1077 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001078 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001079
Jan Glauber22f99342008-12-25 13:38:46 +01001080 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1081 DBF_ERROR("intp :%lx", intparm);
1082 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001083
1084 if (irq_ptr->nr_input_qs) {
1085 q = irq_ptr->input_qs[0];
1086 } else if (irq_ptr->nr_output_qs) {
1087 q = irq_ptr->output_qs[0];
1088 } else {
1089 dump_stack();
1090 goto no_handler;
1091 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001092
1093 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber779e6e12008-07-17 17:16:48 +02001094 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001095 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001096no_handler:
1097 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1098}
1099
Jan Glauber779e6e12008-07-17 17:16:48 +02001100static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1101 int dstat)
1102{
1103 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001104
Jan Glauber22f99342008-12-25 13:38:46 +01001105 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001106
1107 if (cstat)
1108 goto error;
1109 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1110 goto error;
1111 if (!(dstat & DEV_STAT_DEV_END))
1112 goto error;
1113 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1114 return;
1115
1116error:
1117 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1118 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1119 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001120}
1121
1122/* qdio interrupt handler */
1123void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1124 struct irb *irb)
1125{
1126 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1127 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001128
Jan Glauber779e6e12008-07-17 17:16:48 +02001129 if (!intparm || !irq_ptr) {
Jan Glauber22f99342008-12-25 13:38:46 +01001130 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001131 return;
1132 }
1133
Jan Glauber09a308f2010-05-17 10:00:14 +02001134 if (irq_ptr->perf_stat_enabled)
1135 irq_ptr->perf_stat.qdio_int++;
1136
Jan Glauber779e6e12008-07-17 17:16:48 +02001137 if (IS_ERR(irb)) {
1138 switch (PTR_ERR(irb)) {
1139 case -EIO:
Jan Glauber22f99342008-12-25 13:38:46 +01001140 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
Jan Glauber75cb71f2009-04-14 15:36:22 +02001141 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1142 wake_up(&cdev->private->wait_q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001143 return;
1144 default:
1145 WARN_ON(1);
1146 return;
1147 }
1148 }
Jan Glauber22f99342008-12-25 13:38:46 +01001149 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001150 cstat = irb->scsw.cmd.cstat;
1151 dstat = irb->scsw.cmd.dstat;
1152
1153 switch (irq_ptr->state) {
1154 case QDIO_IRQ_STATE_INACTIVE:
1155 qdio_establish_handle_irq(cdev, cstat, dstat);
1156 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001157 case QDIO_IRQ_STATE_CLEANUP:
1158 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1159 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001160 case QDIO_IRQ_STATE_ESTABLISHED:
1161 case QDIO_IRQ_STATE_ACTIVE:
1162 if (cstat & SCHN_STAT_PCI) {
1163 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001164 return;
1165 }
Jan Glauber4c575422009-06-12 10:26:28 +02001166 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001167 qdio_handle_activate_check(cdev, intparm, cstat,
1168 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001169 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001170 case QDIO_IRQ_STATE_STOPPED:
1171 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001172 default:
1173 WARN_ON(1);
1174 }
1175 wake_up(&cdev->private->wait_q);
1176}
1177
1178/**
1179 * qdio_get_ssqd_desc - get qdio subchannel description
1180 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001181 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001182 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001183 * Returns 0 or an error code. The results of the chsc are stored in the
1184 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001185 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001186int qdio_get_ssqd_desc(struct ccw_device *cdev,
1187 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001188{
Jan Glauber779e6e12008-07-17 17:16:48 +02001189
Jan Glauberbbd50e12008-12-25 13:38:43 +01001190 if (!cdev || !cdev->private)
1191 return -EINVAL;
1192
Jan Glauber22f99342008-12-25 13:38:46 +01001193 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
Jan Glauberbbd50e12008-12-25 13:38:43 +01001194 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001195}
1196EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1197
Jan Glauber779e6e12008-07-17 17:16:48 +02001198static void qdio_shutdown_queues(struct ccw_device *cdev)
1199{
1200 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1201 struct qdio_q *q;
1202 int i;
1203
1204 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001205 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001206
1207 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001208 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001209 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001210 }
1211}
1212
1213/**
1214 * qdio_shutdown - shut down a qdio subchannel
1215 * @cdev: associated ccw device
1216 * @how: use halt or clear to shutdown
1217 */
1218int qdio_shutdown(struct ccw_device *cdev, int how)
1219{
Jan Glauber22f99342008-12-25 13:38:46 +01001220 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001221 int rc;
1222 unsigned long flags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001223
Jan Glauber779e6e12008-07-17 17:16:48 +02001224 if (!irq_ptr)
1225 return -ENODEV;
1226
Jan Glauberb4547402009-03-26 15:24:24 +01001227 BUG_ON(irqs_disabled());
Jan Glauber22f99342008-12-25 13:38:46 +01001228 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1229
Jan Glauber779e6e12008-07-17 17:16:48 +02001230 mutex_lock(&irq_ptr->setup_mutex);
1231 /*
1232 * Subchannel was already shot down. We cannot prevent being called
1233 * twice since cio may trigger a shutdown asynchronously.
1234 */
1235 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1236 mutex_unlock(&irq_ptr->setup_mutex);
1237 return 0;
1238 }
1239
Jan Glauberc38f9602009-03-26 15:24:26 +01001240 /*
1241 * Indicate that the device is going down. Scheduling the queue
1242 * tasklets is forbidden from here on.
1243 */
1244 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1245
Jan Glauber779e6e12008-07-17 17:16:48 +02001246 tiqdio_remove_input_queues(irq_ptr);
1247 qdio_shutdown_queues(cdev);
1248 qdio_shutdown_debug_entries(irq_ptr, cdev);
1249
1250 /* cleanup subchannel */
1251 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1252
1253 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1254 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1255 else
1256 /* default behaviour is halt */
1257 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1258 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001259 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1260 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001261 goto no_cleanup;
1262 }
1263
1264 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1265 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1266 wait_event_interruptible_timeout(cdev->private->wait_q,
1267 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1268 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1269 10 * HZ);
1270 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1271
1272no_cleanup:
1273 qdio_shutdown_thinint(irq_ptr);
1274
1275 /* restore interrupt handler */
1276 if ((void *)cdev->handler == (void *)qdio_int_handler)
1277 cdev->handler = irq_ptr->orig_handler;
1278 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1279
1280 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1281 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001282 if (rc)
1283 return rc;
1284 return 0;
1285}
1286EXPORT_SYMBOL_GPL(qdio_shutdown);
1287
1288/**
1289 * qdio_free - free data structures for a qdio subchannel
1290 * @cdev: associated ccw device
1291 */
1292int qdio_free(struct ccw_device *cdev)
1293{
Jan Glauber22f99342008-12-25 13:38:46 +01001294 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001295
Jan Glauber779e6e12008-07-17 17:16:48 +02001296 if (!irq_ptr)
1297 return -ENODEV;
1298
Jan Glauber22f99342008-12-25 13:38:46 +01001299 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001300 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001301
1302 if (irq_ptr->debug_area != NULL) {
1303 debug_unregister(irq_ptr->debug_area);
1304 irq_ptr->debug_area = NULL;
1305 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001306 cdev->private->qdio_data = NULL;
1307 mutex_unlock(&irq_ptr->setup_mutex);
1308
1309 qdio_release_memory(irq_ptr);
1310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(qdio_free);
1313
1314/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001315 * qdio_allocate - allocate qdio queues and associated data
1316 * @init_data: initialization data
1317 */
1318int qdio_allocate(struct qdio_initialize *init_data)
1319{
1320 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001321
Jan Glauber22f99342008-12-25 13:38:46 +01001322 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001323
1324 if ((init_data->no_input_qs && !init_data->input_handler) ||
1325 (init_data->no_output_qs && !init_data->output_handler))
1326 return -EINVAL;
1327
1328 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1329 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1330 return -EINVAL;
1331
1332 if ((!init_data->input_sbal_addr_array) ||
1333 (!init_data->output_sbal_addr_array))
1334 return -EINVAL;
1335
Jan Glauber779e6e12008-07-17 17:16:48 +02001336 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1337 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1338 if (!irq_ptr)
1339 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001340
1341 mutex_init(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001342 qdio_allocate_dbf(init_data, irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001343
1344 /*
1345 * Allocate a page for the chsc calls in qdio_establish.
1346 * Must be pre-allocated since a zfcp recovery will call
1347 * qdio_establish. In case of low memory and swap on a zfcp disk
1348 * we may not be able to allocate memory otherwise.
1349 */
1350 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1351 if (!irq_ptr->chsc_page)
1352 goto out_rel;
1353
1354 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001355 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001356 if (!irq_ptr->qdr)
1357 goto out_rel;
1358 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1359
Jan Glauber779e6e12008-07-17 17:16:48 +02001360 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1361 init_data->no_output_qs))
1362 goto out_rel;
1363
1364 init_data->cdev->private->qdio_data = irq_ptr;
1365 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1366 return 0;
1367out_rel:
1368 qdio_release_memory(irq_ptr);
1369out_err:
1370 return -ENOMEM;
1371}
1372EXPORT_SYMBOL_GPL(qdio_allocate);
1373
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001374static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1375{
1376 struct qdio_q *q = irq_ptr->input_qs[0];
1377 int i, use_cq = 0;
1378
1379 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1380 use_cq = 1;
1381
1382 for_each_output_queue(irq_ptr, q, i) {
1383 if (use_cq) {
1384 if (qdio_enable_async_operation(&q->u.out) < 0) {
1385 use_cq = 0;
1386 continue;
1387 }
1388 } else
1389 qdio_disable_async_operation(&q->u.out);
1390 }
1391 DBF_EVENT("use_cq:%d", use_cq);
1392}
1393
Jan Glauber779e6e12008-07-17 17:16:48 +02001394/**
1395 * qdio_establish - establish queues on a qdio subchannel
1396 * @init_data: initialization data
1397 */
1398int qdio_establish(struct qdio_initialize *init_data)
1399{
Jan Glauber779e6e12008-07-17 17:16:48 +02001400 struct qdio_irq *irq_ptr;
1401 struct ccw_device *cdev = init_data->cdev;
1402 unsigned long saveflags;
1403 int rc;
1404
Jan Glauber22f99342008-12-25 13:38:46 +01001405 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001406
Jan Glauber779e6e12008-07-17 17:16:48 +02001407 irq_ptr = cdev->private->qdio_data;
1408 if (!irq_ptr)
1409 return -ENODEV;
1410
1411 if (cdev->private->state != DEV_STATE_ONLINE)
1412 return -EINVAL;
1413
Jan Glauber779e6e12008-07-17 17:16:48 +02001414 mutex_lock(&irq_ptr->setup_mutex);
1415 qdio_setup_irq(init_data);
1416
1417 rc = qdio_establish_thinint(irq_ptr);
1418 if (rc) {
1419 mutex_unlock(&irq_ptr->setup_mutex);
1420 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1421 return rc;
1422 }
1423
1424 /* establish q */
1425 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1426 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1427 irq_ptr->ccw.count = irq_ptr->equeue.count;
1428 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1429
1430 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1431 ccw_device_set_options_mask(cdev, 0);
1432
1433 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1434 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001435 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1436 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001437 }
1438 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1439
1440 if (rc) {
1441 mutex_unlock(&irq_ptr->setup_mutex);
1442 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1443 return rc;
1444 }
1445
1446 wait_event_interruptible_timeout(cdev->private->wait_q,
1447 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1448 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1449
1450 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1451 mutex_unlock(&irq_ptr->setup_mutex);
1452 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1453 return -EIO;
1454 }
1455
1456 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber22f99342008-12-25 13:38:46 +01001457 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
Jan Glauber779e6e12008-07-17 17:16:48 +02001458
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001459 qdio_detect_hsicq(irq_ptr);
1460
Jan Glauber779e6e12008-07-17 17:16:48 +02001461 /* qebsm is now setup if available, initialize buffer states */
1462 qdio_init_buf_states(irq_ptr);
1463
1464 mutex_unlock(&irq_ptr->setup_mutex);
1465 qdio_print_subchannel_info(irq_ptr, cdev);
1466 qdio_setup_debug_entries(irq_ptr, cdev);
1467 return 0;
1468}
1469EXPORT_SYMBOL_GPL(qdio_establish);
1470
1471/**
1472 * qdio_activate - activate queues on a qdio subchannel
1473 * @cdev: associated cdev
1474 */
1475int qdio_activate(struct ccw_device *cdev)
1476{
1477 struct qdio_irq *irq_ptr;
1478 int rc;
1479 unsigned long saveflags;
Jan Glauber779e6e12008-07-17 17:16:48 +02001480
Jan Glauber22f99342008-12-25 13:38:46 +01001481 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001482
Jan Glauber779e6e12008-07-17 17:16:48 +02001483 irq_ptr = cdev->private->qdio_data;
1484 if (!irq_ptr)
1485 return -ENODEV;
1486
1487 if (cdev->private->state != DEV_STATE_ONLINE)
1488 return -EINVAL;
1489
1490 mutex_lock(&irq_ptr->setup_mutex);
1491 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1492 rc = -EBUSY;
1493 goto out;
1494 }
1495
Jan Glauber779e6e12008-07-17 17:16:48 +02001496 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1497 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1498 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1499 irq_ptr->ccw.cda = 0;
1500
1501 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1502 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1503
1504 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1505 0, DOIO_DENY_PREFETCH);
1506 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001507 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1508 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001509 }
1510 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1511
1512 if (rc)
1513 goto out;
1514
1515 if (is_thinint_irq(irq_ptr))
1516 tiqdio_add_input_queues(irq_ptr);
1517
1518 /* wait for subchannel to become active */
1519 msleep(5);
1520
1521 switch (irq_ptr->state) {
1522 case QDIO_IRQ_STATE_STOPPED:
1523 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001524 rc = -EIO;
1525 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001526 default:
1527 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1528 rc = 0;
1529 }
1530out:
1531 mutex_unlock(&irq_ptr->setup_mutex);
1532 return rc;
1533}
1534EXPORT_SYMBOL_GPL(qdio_activate);
1535
1536static inline int buf_in_between(int bufnr, int start, int count)
1537{
1538 int end = add_buf(start, count);
1539
1540 if (end > start) {
1541 if (bufnr >= start && bufnr < end)
1542 return 1;
1543 else
1544 return 0;
1545 }
1546
1547 /* wrap-around case */
1548 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1549 (bufnr < end))
1550 return 1;
1551 else
1552 return 0;
1553}
1554
1555/**
1556 * handle_inbound - reset processed input buffers
1557 * @q: queue containing the buffers
1558 * @callflags: flags
1559 * @bufnr: first buffer to process
1560 * @count: how many buffers are emptied
1561 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001562static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1563 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001564{
Jan Glauberd303b6f2009-03-26 15:24:31 +01001565 int used, diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001566
Jan Glauber6486cda2010-01-04 09:05:42 +01001567 qperf_inc(q, inbound_call);
1568
Jan Glauber50f769d2008-12-25 13:38:47 +01001569 if (!q->u.in.polling)
1570 goto set;
1571
1572 /* protect against stop polling setting an ACK for an emptied slsb */
1573 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1574 /* overwriting everything, just delete polling status */
1575 q->u.in.polling = 0;
1576 q->u.in.ack_count = 0;
1577 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001578 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001579 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001580 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001581 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001582 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001583 q->u.in.ack_count -= diff;
1584 if (q->u.in.ack_count <= 0) {
1585 q->u.in.polling = 0;
1586 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001587 goto set;
1588 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001589 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001590 }
1591 else
1592 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001593 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001594 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001595
Jan Glauber50f769d2008-12-25 13:38:47 +01001596set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001597 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001598
1599 used = atomic_add_return(count, &q->nr_buf_used) - count;
1600 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1601
Jan Glauberd303b6f2009-03-26 15:24:31 +01001602 if (need_siga_in(q))
1603 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001604
Jan Glauberd303b6f2009-03-26 15:24:31 +01001605 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001606}
1607
1608/**
1609 * handle_outbound - process filled outbound buffers
1610 * @q: queue containing the buffers
1611 * @callflags: flags
1612 * @bufnr: first buffer to process
1613 * @count: how many buffers are filled
1614 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001615static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1616 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001617{
Jan Glauberc26001d2011-05-23 10:24:38 +02001618 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001619 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001620
Jan Glauber6486cda2010-01-04 09:05:42 +01001621 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001622
1623 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1624 used = atomic_add_return(count, &q->nr_buf_used);
1625 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1626
Jan Glauber01958432011-01-05 12:47:51 +01001627 if (used == QDIO_MAX_BUFFERS_PER_Q)
1628 qperf_inc(q, outbound_queue_full);
1629
Jan Glauber6486cda2010-01-04 09:05:42 +01001630 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001631 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001632 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001633 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001634 q->u.out.pci_out_enabled = 0;
1635
1636 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001637 unsigned long phys_aob = 0;
1638
1639 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001640 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1641
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001642 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1643
1644 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001645 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001646 rc = qdio_siga_sync_q(q);
1647 } else {
1648 /* try to fast requeue buffers */
1649 get_buf_state(q, prev_buf(bufnr), &state, 0);
1650 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001651 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001652 else
Jan Glauber110da312011-01-05 12:47:53 +01001653 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001654 }
1655
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001656 /* in case of SIGA errors we must process the error immediately */
1657 if (used >= q->u.out.scan_threshold || rc)
1658 tasklet_schedule(&q->tasklet);
1659 else
1660 /* free the SBALs in case of no further traffic */
1661 if (!timer_pending(&q->u.out.timer))
1662 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001663 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001664}
1665
1666/**
1667 * do_QDIO - process input or output buffers
1668 * @cdev: associated ccw_device for the qdio subchannel
1669 * @callflags: input or output and special flags from the program
1670 * @q_nr: queue number
1671 * @bufnr: buffer number
1672 * @count: how many buffers to process
1673 */
1674int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001675 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001676{
1677 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001678
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001679
Jan Glauber66182412009-06-22 12:08:15 +02001680 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001681 return -EINVAL;
1682
Jan Glauber779e6e12008-07-17 17:16:48 +02001683 irq_ptr = cdev->private->qdio_data;
1684 if (!irq_ptr)
1685 return -ENODEV;
1686
Jan Glauber1d7e1502009-09-22 22:58:39 +02001687 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1688 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001689
1690 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1691 return -EBUSY;
Jan Glauber9a265132011-03-23 10:16:01 +01001692 if (!count)
1693 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001694 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001695 return handle_inbound(irq_ptr->input_qs[q_nr],
1696 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001697 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001698 return handle_outbound(irq_ptr->output_qs[q_nr],
1699 callflags, bufnr, count);
1700 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001701}
1702EXPORT_SYMBOL_GPL(do_QDIO);
1703
Jan Glauberd36deae2010-09-07 21:14:39 +00001704/**
1705 * qdio_start_irq - process input buffers
1706 * @cdev: associated ccw_device for the qdio subchannel
1707 * @nr: input queue number
1708 *
1709 * Return codes
1710 * 0 - success
1711 * 1 - irqs not started since new data is available
1712 */
1713int qdio_start_irq(struct ccw_device *cdev, int nr)
1714{
1715 struct qdio_q *q;
1716 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1717
1718 if (!irq_ptr)
1719 return -ENODEV;
1720 q = irq_ptr->input_qs[nr];
1721
1722 WARN_ON(queue_irqs_enabled(q));
1723
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001724 if (!shared_ind(q))
Jan Glauberd36deae2010-09-07 21:14:39 +00001725 xchg(q->irq_ptr->dsci, 0);
1726
1727 qdio_stop_polling(q);
1728 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1729
1730 /*
1731 * We need to check again to not lose initiative after
1732 * resetting the ACK state.
1733 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001734 if (!shared_ind(q) && *q->irq_ptr->dsci)
Jan Glauberd36deae2010-09-07 21:14:39 +00001735 goto rescan;
1736 if (!qdio_inbound_q_done(q))
1737 goto rescan;
1738 return 0;
1739
1740rescan:
1741 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1742 &q->u.in.queue_irq_state))
1743 return 0;
1744 else
1745 return 1;
1746
1747}
1748EXPORT_SYMBOL(qdio_start_irq);
1749
1750/**
1751 * qdio_get_next_buffers - process input buffers
1752 * @cdev: associated ccw_device for the qdio subchannel
1753 * @nr: input queue number
1754 * @bufnr: first filled buffer number
1755 * @error: buffers are in error state
1756 *
1757 * Return codes
1758 * < 0 - error
1759 * = 0 - no new buffers found
1760 * > 0 - number of processed buffers
1761 */
1762int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1763 int *error)
1764{
1765 struct qdio_q *q;
1766 int start, end;
1767 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1768
1769 if (!irq_ptr)
1770 return -ENODEV;
1771 q = irq_ptr->input_qs[nr];
1772 WARN_ON(queue_irqs_enabled(q));
1773
Jan Glauberd36deae2010-09-07 21:14:39 +00001774 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001775 * Cannot rely on automatic sync after interrupt since queues may
1776 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001777 */
Jan Glauber90adac52011-01-05 12:47:54 +01001778 if (need_siga_sync(q))
1779 qdio_sync_queues(q);
1780
1781 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001782 qdio_check_outbound_after_thinint(q);
1783
1784 if (!qdio_inbound_q_moved(q))
1785 return 0;
1786
1787 /* Note: upper-layer MUST stop processing immediately here ... */
1788 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1789 return -EIO;
1790
1791 start = q->first_to_kick;
1792 end = q->first_to_check;
1793 *bufnr = start;
1794 *error = q->qdio_error;
1795
1796 /* for the next time */
1797 q->first_to_kick = end;
1798 q->qdio_error = 0;
1799 return sub_buf(end, start);
1800}
1801EXPORT_SYMBOL(qdio_get_next_buffers);
1802
1803/**
1804 * qdio_stop_irq - disable interrupt processing for the device
1805 * @cdev: associated ccw_device for the qdio subchannel
1806 * @nr: input queue number
1807 *
1808 * Return codes
1809 * 0 - interrupts were already disabled
1810 * 1 - interrupts successfully disabled
1811 */
1812int qdio_stop_irq(struct ccw_device *cdev, int nr)
1813{
1814 struct qdio_q *q;
1815 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1816
1817 if (!irq_ptr)
1818 return -ENODEV;
1819 q = irq_ptr->input_qs[nr];
1820
1821 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1822 &q->u.in.queue_irq_state))
1823 return 0;
1824 else
1825 return 1;
1826}
1827EXPORT_SYMBOL(qdio_stop_irq);
1828
Jan Glauber779e6e12008-07-17 17:16:48 +02001829static int __init init_QDIO(void)
1830{
1831 int rc;
1832
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001833 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001834 if (rc)
1835 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001836 rc = qdio_setup_init();
1837 if (rc)
1838 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001839 rc = tiqdio_allocate_memory();
1840 if (rc)
1841 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001842 rc = tiqdio_register_thinints();
1843 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001844 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001845 return 0;
1846
Jan Glauber779e6e12008-07-17 17:16:48 +02001847out_ti:
1848 tiqdio_free_memory();
1849out_cache:
1850 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001851out_debug:
1852 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001853 return rc;
1854}
1855
1856static void __exit exit_QDIO(void)
1857{
1858 tiqdio_unregister_thinints();
1859 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001860 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001861 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001862}
1863
1864module_init(init_QDIO);
1865module_exit(exit_QDIO);