blob: da969789936d5d993450d8b85fff80f37d8ff4c5 [file] [log] [blame]
Jan Glauber779e6e12008-07-17 17:16:48 +02001/*
Jan Glauber779e6e12008-07-17 17:16:48 +02002 * Linux for s390 qdio support, buffer handling, qdio API and module support.
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2000, 2008
Jan Glauber779e6e12008-07-17 17:16:48 +02005 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
8 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/timer.h>
13#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000015#include <linux/io.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020017#include <asm/debug.h>
18#include <asm/qdio.h>
Michael Holzheu3ab121a2012-03-11 11:59:32 -040019#include <asm/ipl.h>
Jan Glauber779e6e12008-07-17 17:16:48 +020020
21#include "cio.h"
22#include "css.h"
23#include "device.h"
24#include "qdio.h"
25#include "qdio_debug.h"
Jan Glauber779e6e12008-07-17 17:16:48 +020026
27MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
28 "Jan Glauber <jang@linux.vnet.ibm.com>");
29MODULE_DESCRIPTION("QDIO base support");
30MODULE_LICENSE("GPL");
31
Jan Glauber958c0ba2011-01-05 12:47:52 +010032static inline int do_siga_sync(unsigned long schid,
33 unsigned int out_mask, unsigned int in_mask,
34 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020035{
Jan Glauber958c0ba2011-01-05 12:47:52 +010036 register unsigned long __fc asm ("0") = fc;
37 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020038 register unsigned long out asm ("2") = out_mask;
39 register unsigned long in asm ("3") = in_mask;
40 int cc;
41
42 asm volatile(
43 " siga 0\n"
44 " ipm %0\n"
45 " srl %0,28\n"
46 : "=d" (cc)
47 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
48 return cc;
49}
50
Jan Glauber958c0ba2011-01-05 12:47:52 +010051static inline int do_siga_input(unsigned long schid, unsigned int mask,
52 unsigned int fc)
Jan Glauber779e6e12008-07-17 17:16:48 +020053{
Jan Glauber958c0ba2011-01-05 12:47:52 +010054 register unsigned long __fc asm ("0") = fc;
55 register unsigned long __schid asm ("1") = schid;
Jan Glauber779e6e12008-07-17 17:16:48 +020056 register unsigned long __mask asm ("2") = mask;
57 int cc;
58
59 asm volatile(
60 " siga 0\n"
61 " ipm %0\n"
62 " srl %0,28\n"
63 : "=d" (cc)
Jan Glauber1549d132012-05-09 16:27:34 +020064 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc");
Jan Glauber779e6e12008-07-17 17:16:48 +020065 return cc;
66}
67
68/**
69 * do_siga_output - perform SIGA-w/wt function
70 * @schid: subchannel id or in case of QEBSM the subchannel token
71 * @mask: which output queues to process
72 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
73 * @fc: function code to perform
74 *
Jan Glauber1549d132012-05-09 16:27:34 +020075 * Returns condition code.
Jan Glauber779e6e12008-07-17 17:16:48 +020076 * Note: For IQDC unicast queues only the highest priority queue is processed.
77 */
78static inline int do_siga_output(unsigned long schid, unsigned long mask,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000079 unsigned int *bb, unsigned int fc,
80 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +020081{
82 register unsigned long __fc asm("0") = fc;
83 register unsigned long __schid asm("1") = schid;
84 register unsigned long __mask asm("2") = mask;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +000085 register unsigned long __aob asm("3") = aob;
Jan Glauber1549d132012-05-09 16:27:34 +020086 int cc;
Jan Glauber779e6e12008-07-17 17:16:48 +020087
88 asm volatile(
89 " siga 0\n"
Jan Glauber1549d132012-05-09 16:27:34 +020090 " ipm %0\n"
Jan Glauber779e6e12008-07-17 17:16:48 +020091 " srl %0,28\n"
Jan Glauber1549d132012-05-09 16:27:34 +020092 : "=d" (cc), "+d" (__fc), "+d" (__aob)
93 : "d" (__schid), "d" (__mask)
94 : "cc");
95 *bb = __fc >> 31;
Jan Glauber779e6e12008-07-17 17:16:48 +020096 return cc;
97}
98
99static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
100{
Jan Glauber779e6e12008-07-17 17:16:48 +0200101 /* all done or next buffer state different */
102 if (ccq == 0 || ccq == 32)
103 return 0;
Jan Glauber25f269f2011-10-30 15:17:06 +0100104 /* no buffer processed */
105 if (ccq == 97)
Jan Glauber779e6e12008-07-17 17:16:48 +0200106 return 1;
Jan Glauber25f269f2011-10-30 15:17:06 +0100107 /* not all buffers processed */
108 if (ccq == 96)
109 return 2;
Jan Glauber779e6e12008-07-17 17:16:48 +0200110 /* 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{
Jan Glauber25f269f2011-10-30 15:17:06 +0100129 int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200130 unsigned int ccq = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200131
Jan Glauber6486cda2010-01-04 09:05:42 +0100132 qperf_inc(q, eqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200133
134 if (!q->is_input_q)
135 nr += q->irq_ptr->nr_input_qs;
136again:
Jan Glauber50f769d2008-12-25 13:38:47 +0100137 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
138 auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200139 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100140 if (!rc)
141 return count - tmp_count;
Jan Glauber22f99342008-12-25 13:38:46 +0100142
Jan Glauber779e6e12008-07-17 17:16:48 +0200143 if (rc == 1) {
Jan Glauber22f99342008-12-25 13:38:46 +0100144 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
Jan Glauber779e6e12008-07-17 17:16:48 +0200145 goto again;
146 }
147
Jan Glauber25f269f2011-10-30 15:17:06 +0100148 if (rc == 2) {
Jan Glauber25f269f2011-10-30 15:17:06 +0100149 qperf_inc(q, eqbs_partial);
150 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
151 tmp_count);
152 /*
153 * Retry once, if that fails bail out and process the
154 * extracted buffers before trying again.
155 */
156 if (!retried++)
157 goto again;
158 else
159 return count - tmp_count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200160 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100161
162 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
163 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200164 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100165 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100166 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200167}
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;
Jan Glauber6486cda2010-01-04 09:05:42 +0100190 qperf_inc(q, sqbs);
Jan Glauber779e6e12008-07-17 17:16:48 +0200191
192 if (!q->is_input_q)
193 nr += q->irq_ptr->nr_input_qs;
194again:
195 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
196 rc = qdio_check_ccq(q, ccq);
Jan Glauber25f269f2011-10-30 15:17:06 +0100197 if (!rc) {
Jan Glauberce1d8012012-10-24 12:38:35 +0200198 WARN_ON_ONCE(tmp_count);
Jan Glauber25f269f2011-10-30 15:17:06 +0100199 return count - tmp_count;
200 }
201
202 if (rc == 1 || rc == 2) {
Jan Glauber22f99342008-12-25 13:38:46 +0100203 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
Jan Glauber6486cda2010-01-04 09:05:42 +0100204 qperf_inc(q, sqbs_partial);
Jan Glauber779e6e12008-07-17 17:16:48 +0200205 goto again;
206 }
Jan Glauber25f269f2011-10-30 15:17:06 +0100207
208 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
209 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200210 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE,
Steffen Maier7b3cc672012-03-02 17:32:58 +0100211 q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
Jan Glauber25f269f2011-10-30 15:17:06 +0100212 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200213}
214
215/* returns number of examined buffers and their common state in *state */
216static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
Jan Glauber50f769d2008-12-25 13:38:47 +0100217 unsigned char *state, unsigned int count,
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000218 int auto_ack, int merge_pending)
Jan Glauber779e6e12008-07-17 17:16:48 +0200219{
220 unsigned char __state = 0;
221 int i;
222
Jan Glauber779e6e12008-07-17 17:16:48 +0200223 if (is_qebsm(q))
Jan Glauber50f769d2008-12-25 13:38:47 +0100224 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
Jan Glauber779e6e12008-07-17 17:16:48 +0200225
226 for (i = 0; i < count; i++) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000227 if (!__state) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200228 __state = q->slsb.val[bufnr];
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000229 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
230 __state = SLSB_P_OUTPUT_EMPTY;
231 } else if (merge_pending) {
232 if ((q->slsb.val[bufnr] & __state) != __state)
233 break;
234 } else if (q->slsb.val[bufnr] != __state)
Jan Glauber779e6e12008-07-17 17:16:48 +0200235 break;
236 bufnr = next_buf(bufnr);
237 }
238 *state = __state;
239 return i;
240}
241
Jan Glauber60b5df22009-06-22 12:08:10 +0200242static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
243 unsigned char *state, int auto_ack)
Jan Glauber779e6e12008-07-17 17:16:48 +0200244{
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000245 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200246}
247
248/* wrap-around safe setting of slsb states, returns number of changed buffers */
249static inline int set_buf_states(struct qdio_q *q, int bufnr,
250 unsigned char state, int count)
251{
252 int i;
253
Jan Glauber779e6e12008-07-17 17:16:48 +0200254 if (is_qebsm(q))
255 return qdio_do_sqbs(q, state, bufnr, count);
256
257 for (i = 0; i < count; i++) {
258 xchg(&q->slsb.val[bufnr], state);
259 bufnr = next_buf(bufnr);
260 }
261 return count;
262}
263
264static inline int set_buf_state(struct qdio_q *q, int bufnr,
265 unsigned char state)
266{
267 return set_buf_states(q, bufnr, state, 1);
268}
269
270/* set slsb states to initial state */
Martin Schwidefskyc4736d92011-10-30 15:17:11 +0100271static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
Jan Glauber779e6e12008-07-17 17:16:48 +0200272{
273 struct qdio_q *q;
274 int i;
275
276 for_each_input_queue(irq_ptr, q, i)
277 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
278 QDIO_MAX_BUFFERS_PER_Q);
279 for_each_output_queue(irq_ptr, q, i)
280 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
281 QDIO_MAX_BUFFERS_PER_Q);
282}
283
Jan Glauber60b5df22009-06-22 12:08:10 +0200284static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
Jan Glauber779e6e12008-07-17 17:16:48 +0200285 unsigned int input)
286{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100287 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
288 unsigned int fc = QDIO_SIGA_SYNC;
Jan Glauber779e6e12008-07-17 17:16:48 +0200289 int cc;
290
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100291 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100292 qperf_inc(q, siga_sync);
Jan Glauber779e6e12008-07-17 17:16:48 +0200293
Jan Glauber958c0ba2011-01-05 12:47:52 +0100294 if (is_qebsm(q)) {
295 schid = q->irq_ptr->sch_token;
296 fc |= QDIO_SIGA_QEBSM_FLAG;
297 }
298
299 cc = do_siga_sync(schid, output, input, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100300 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100301 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200302 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200303}
304
Jan Glauber60b5df22009-06-22 12:08:10 +0200305static inline int qdio_siga_sync_q(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200306{
307 if (q->is_input_q)
308 return qdio_siga_sync(q, 0, q->mask);
309 else
310 return qdio_siga_sync(q, q->mask, 0);
311}
312
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000313static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
314 unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200315{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100316 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
317 unsigned int fc = QDIO_SIGA_WRITE;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100318 u64 start_time = 0;
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200319 int retries = 0, cc;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000320 unsigned long laob = 0;
321
Eugene Crosserec6674c2015-10-06 15:12:29 +0200322 WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
323 !q->u.out.use_cq));
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000324 if (q->u.out.use_cq && aob != 0) {
325 fc = QDIO_SIGA_WRITEQ;
326 laob = aob;
327 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200328
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100329 if (is_qebsm(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200330 schid = q->irq_ptr->sch_token;
Jan Glauber958c0ba2011-01-05 12:47:52 +0100331 fc |= QDIO_SIGA_QEBSM_FLAG;
Jan Glauber779e6e12008-07-17 17:16:48 +0200332 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200333again:
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000334 cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
Jan Glauber58eb27c2008-08-21 19:46:34 +0200335
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100336 /* hipersocket busy condition */
Jan Glauber110da312011-01-05 12:47:53 +0100337 if (unlikely(*busy_bit)) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200338 retries++;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100339
340 if (!start_time) {
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200341 start_time = get_tod_clock_fast();
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100342 goto again;
343 }
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200344 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
Jan Glauber779e6e12008-07-17 17:16:48 +0200345 goto again;
346 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200347 if (retries) {
348 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
349 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
350 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
351 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200352 return cc;
353}
354
355static inline int qdio_siga_input(struct qdio_q *q)
356{
Jan Glauber958c0ba2011-01-05 12:47:52 +0100357 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
358 unsigned int fc = QDIO_SIGA_READ;
Jan Glauber779e6e12008-07-17 17:16:48 +0200359 int cc;
360
Jan Glauber22f99342008-12-25 13:38:46 +0100361 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
Jan Glauber6486cda2010-01-04 09:05:42 +0100362 qperf_inc(q, siga_read);
Jan Glauber779e6e12008-07-17 17:16:48 +0200363
Jan Glauber958c0ba2011-01-05 12:47:52 +0100364 if (is_qebsm(q)) {
365 schid = q->irq_ptr->sch_token;
366 fc |= QDIO_SIGA_QEBSM_FLAG;
367 }
368
369 cc = do_siga_input(schid, q->mask, fc);
Jan Glauber110da312011-01-05 12:47:53 +0100370 if (unlikely(cc))
Jan Glauber22f99342008-12-25 13:38:46 +0100371 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200372 return (cc) ? -EIO : 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200373}
374
Jan Glauber90adac52011-01-05 12:47:54 +0100375#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
376#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
377
378static inline void qdio_sync_queues(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200379{
Jan Glauber90adac52011-01-05 12:47:54 +0100380 /* PCI capable outbound queues will also be scanned so sync them too */
381 if (pci_out_supported(q))
382 qdio_siga_sync_all(q);
383 else
Jan Glauber779e6e12008-07-17 17:16:48 +0200384 qdio_siga_sync_q(q);
385}
386
Jan Glauber60b5df22009-06-22 12:08:10 +0200387int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
388 unsigned char *state)
389{
Jan Glauber90adac52011-01-05 12:47:54 +0100390 if (need_siga_sync(q))
391 qdio_siga_sync_q(q);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000392 return get_buf_states(q, bufnr, state, 1, 0, 0);
Jan Glauber60b5df22009-06-22 12:08:10 +0200393}
394
395static inline void qdio_stop_polling(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200396{
Jan Glauber50f769d2008-12-25 13:38:47 +0100397 if (!q->u.in.polling)
Jan Glauber779e6e12008-07-17 17:16:48 +0200398 return;
Jan Glauber50f769d2008-12-25 13:38:47 +0100399
Jan Glauber779e6e12008-07-17 17:16:48 +0200400 q->u.in.polling = 0;
Jan Glauber6486cda2010-01-04 09:05:42 +0100401 qperf_inc(q, stop_polling);
Jan Glauber779e6e12008-07-17 17:16:48 +0200402
403 /* show the card that we are not polling anymore */
Jan Glauber50f769d2008-12-25 13:38:47 +0100404 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100405 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100406 q->u.in.ack_count);
407 q->u.in.ack_count = 0;
408 } else
Jan Glaubere85dea02009-03-26 15:24:29 +0100409 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber779e6e12008-07-17 17:16:48 +0200410}
411
Fabian Frederick92bdae52014-06-03 21:55:15 +0200412static inline void account_sbals(struct qdio_q *q, unsigned int count)
Jan Glauberd3072972010-02-26 22:37:36 +0100413{
Fabian Frederick92bdae52014-06-03 21:55:15 +0200414 int pos;
Jan Glauberd3072972010-02-26 22:37:36 +0100415
416 q->q_stats.nr_sbal_total += count;
417 if (count == QDIO_MAX_BUFFERS_MASK) {
418 q->q_stats.nr_sbals[7]++;
419 return;
420 }
Fabian Frederick92bdae52014-06-03 21:55:15 +0200421 pos = ilog2(count);
Jan Glauberd3072972010-02-26 22:37:36 +0100422 q->q_stats.nr_sbals[pos]++;
423}
424
Jan Glauberbffbbd22011-04-20 10:15:33 +0200425static void process_buffer_error(struct qdio_q *q, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +0200426{
Jan Glauberbffbbd22011-04-20 10:15:33 +0200427 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
428 SLSB_P_OUTPUT_NOT_INIT;
429
Jan Glauber1549d132012-05-09 16:27:34 +0200430 q->qdio_error = QDIO_ERROR_SLSB_STATE;
Jan Glauber50f769d2008-12-25 13:38:47 +0100431
432 /* special handling for no target buffer empty */
433 if ((!q->is_input_q &&
Jan Glauber3ec908782011-06-06 14:14:40 +0200434 (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100435 qperf_inc(q, target_full);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200436 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
Jan Glauber50f769d2008-12-25 13:38:47 +0100437 q->first_to_check);
Jan Glauber2768b2d2011-10-30 15:17:07 +0100438 goto set;
Jan Glauber50f769d2008-12-25 13:38:47 +0100439 }
440
Jan Glauber22f99342008-12-25 13:38:46 +0100441 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
442 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
Jan Glauber50f769d2008-12-25 13:38:47 +0100443 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
Jan Glauber22f99342008-12-25 13:38:46 +0100444 DBF_ERROR("F14:%2x F15:%2x",
Jan Glauber3ec908782011-06-06 14:14:40 +0200445 q->sbal[q->first_to_check]->element[14].sflags,
446 q->sbal[q->first_to_check]->element[15].sflags);
Jan Glauberbffbbd22011-04-20 10:15:33 +0200447
Jan Glauber2768b2d2011-10-30 15:17:07 +0100448set:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200449 /*
450 * Interrupts may be avoided as long as the error is present
451 * so change the buffer state immediately to avoid starvation.
452 */
453 set_buf_states(q, q->first_to_check, state, count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100454}
Jan Glauber779e6e12008-07-17 17:16:48 +0200455
Jan Glauber50f769d2008-12-25 13:38:47 +0100456static inline void inbound_primed(struct qdio_q *q, int count)
457{
458 int new;
459
Jan Glauber1d7e1502009-09-22 22:58:39 +0200460 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
Jan Glauber50f769d2008-12-25 13:38:47 +0100461
462 /* for QEBSM the ACK was already set by EQBS */
463 if (is_qebsm(q)) {
464 if (!q->u.in.polling) {
465 q->u.in.polling = 1;
466 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100467 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100468 return;
469 }
470
471 /* delete the previous ACK's */
Jan Glaubere85dea02009-03-26 15:24:29 +0100472 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
Jan Glauber50f769d2008-12-25 13:38:47 +0100473 q->u.in.ack_count);
474 q->u.in.ack_count = count;
Jan Glaubere85dea02009-03-26 15:24:29 +0100475 q->u.in.ack_start = q->first_to_check;
Jan Glauber50f769d2008-12-25 13:38:47 +0100476 return;
477 }
478
479 /*
480 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
481 * or by the next inbound run.
482 */
483 new = add_buf(q->first_to_check, count - 1);
484 if (q->u.in.polling) {
485 /* reset the previous ACK but first set the new one */
486 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glaubere85dea02009-03-26 15:24:29 +0100487 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100488 } else {
Jan Glauber50f769d2008-12-25 13:38:47 +0100489 q->u.in.polling = 1;
Jan Glauber3fdf1e12009-03-26 15:24:28 +0100490 set_buf_state(q, new, SLSB_P_INPUT_ACK);
Jan Glauber50f769d2008-12-25 13:38:47 +0100491 }
492
Jan Glaubere85dea02009-03-26 15:24:29 +0100493 q->u.in.ack_start = new;
Jan Glauber50f769d2008-12-25 13:38:47 +0100494 count--;
495 if (!count)
496 return;
Jan Glauber6541f7b2009-09-22 22:58:40 +0200497 /* need to change ALL buffers to get more interrupts */
498 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200499}
500
501static int get_inbound_buffer_frontier(struct qdio_q *q)
502{
503 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100504 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200505
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200506 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100507
Jan Glauber779e6e12008-07-17 17:16:48 +0200508 /*
Jan Glauber779e6e12008-07-17 17:16:48 +0200509 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
510 * would return 0.
511 */
512 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
513 stop = add_buf(q->first_to_check, count);
514
Jan Glauber779e6e12008-07-17 17:16:48 +0200515 if (q->first_to_check == stop)
516 goto out;
517
Jan Glauber36e3e722009-06-22 12:08:12 +0200518 /*
519 * No siga sync here, as a PCI or we after a thin interrupt
520 * already sync'ed the queues.
521 */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000522 count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200523 if (!count)
524 goto out;
525
526 switch (state) {
527 case SLSB_P_INPUT_PRIMED:
Jan Glauber50f769d2008-12-25 13:38:47 +0100528 inbound_primed(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200529 q->first_to_check = add_buf(q->first_to_check, count);
Heiko Carstenseddf0d52013-09-16 06:59:50 +0200530 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
Jan Glauber6486cda2010-01-04 09:05:42 +0100531 qperf_inc(q, inbound_queue_full);
Jan Glauberd3072972010-02-26 22:37:36 +0100532 if (q->irq_ptr->perf_stat_enabled)
533 account_sbals(q, count);
Jan Glauber36e3e722009-06-22 12:08:12 +0200534 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200535 case SLSB_P_INPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200536 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200537 q->first_to_check = add_buf(q->first_to_check, count);
538 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100539 if (q->irq_ptr->perf_stat_enabled)
540 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200541 break;
542 case SLSB_CU_INPUT_EMPTY:
543 case SLSB_P_INPUT_NOT_INIT:
544 case SLSB_P_INPUT_ACK:
Jan Glauberd3072972010-02-26 22:37:36 +0100545 if (q->irq_ptr->perf_stat_enabled)
546 q->q_stats.nr_sbal_nop++;
Jan Glauber22f99342008-12-25 13:38:46 +0100547 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
Jan Glauber779e6e12008-07-17 17:16:48 +0200548 break;
549 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200550 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200551 }
552out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200553 return q->first_to_check;
554}
555
Jan Glauber60b5df22009-06-22 12:08:10 +0200556static int qdio_inbound_q_moved(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200557{
558 int bufnr;
559
560 bufnr = get_inbound_buffer_frontier(q);
561
Jan Glauber1549d132012-05-09 16:27:34 +0200562 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100563 q->last_move = bufnr;
Martin Schwidefsky27d71602010-02-26 22:37:38 +0100564 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
Heiko Carstens1aae0562013-01-30 09:49:40 +0100565 q->u.in.timestamp = get_tod_clock();
Jan Glauber779e6e12008-07-17 17:16:48 +0200566 return 1;
567 } else
568 return 0;
569}
570
Jan Glauber9a2c1602009-06-22 12:08:11 +0200571static inline int qdio_inbound_q_done(struct qdio_q *q)
Jan Glauber60b5df22009-06-22 12:08:10 +0200572{
573 unsigned char state = 0;
574
575 if (!atomic_read(&q->nr_buf_used))
576 return 1;
577
Jan Glauber90adac52011-01-05 12:47:54 +0100578 if (need_siga_sync(q))
579 qdio_siga_sync_q(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200580 get_buf_state(q, q->first_to_check, &state, 0);
581
Ursula Braun4c522282010-02-09 09:46:07 +0100582 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
Jan Glauber60b5df22009-06-22 12:08:10 +0200583 /* more work coming */
584 return 0;
Jan Glauber9a2c1602009-06-22 12:08:11 +0200585
586 if (is_thinint_irq(q->irq_ptr))
587 return 1;
588
589 /* don't poll under z/VM */
590 if (MACHINE_IS_VM)
591 return 1;
592
593 /*
594 * At this point we know, that inbound first_to_check
595 * has (probably) not moved (see qdio_inbound_processing).
596 */
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200597 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
Jan Glauber1d7e1502009-09-22 22:58:39 +0200598 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
Jan Glauber9a2c1602009-06-22 12:08:11 +0200599 q->first_to_check);
600 return 1;
601 } else
602 return 0;
Jan Glauber60b5df22009-06-22 12:08:10 +0200603}
604
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000605static inline int contains_aobs(struct qdio_q *q)
606{
607 return !q->is_input_q && q->u.out.use_cq;
608}
609
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000610static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
611{
612 unsigned char state = 0;
613 int j, b = start;
614
615 if (!contains_aobs(q))
616 return;
617
618 for (j = 0; j < count; ++j) {
619 get_buf_state(q, b, &state, 0);
620 if (state == SLSB_P_OUTPUT_PENDING) {
621 struct qaob *aob = q->u.out.aobs[b];
622 if (aob == NULL)
623 continue;
624
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000625 q->u.out.sbal_state[b].flags |=
626 QDIO_OUTBUF_STATE_FLAG_PENDING;
627 q->u.out.aobs[b] = NULL;
628 } else if (state == SLSB_P_OUTPUT_EMPTY) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000629 q->u.out.sbal_state[b].aob = NULL;
630 }
631 b = next_buf(b);
632 }
633}
634
635static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
636 int bufnr)
637{
638 unsigned long phys_aob = 0;
639
640 if (!q->use_cq)
641 goto out;
642
643 if (!q->aobs[bufnr]) {
644 struct qaob *aob = qdio_allocate_aob();
645 q->aobs[bufnr] = aob;
646 }
647 if (q->aobs[bufnr]) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000648 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
649 q->sbal_state[bufnr].aob = q->aobs[bufnr];
650 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
651 phys_aob = virt_to_phys(q->aobs[bufnr]);
Jan Glauberce1d8012012-10-24 12:38:35 +0200652 WARN_ON_ONCE(phys_aob & 0xFF);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000653 }
654
655out:
656 return phys_aob;
657}
658
Jan Glauber60b5df22009-06-22 12:08:10 +0200659static void qdio_kick_handler(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200660{
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100661 int start = q->first_to_kick;
662 int end = q->first_to_check;
663 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +0200664
665 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
666 return;
667
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100668 count = sub_buf(end, start);
669
670 if (q->is_input_q) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100671 qperf_inc(q, inbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200672 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100673 } else {
Jan Glauber6486cda2010-01-04 09:05:42 +0100674 qperf_inc(q, outbound_handler);
Jan Glauber1d7e1502009-09-22 22:58:39 +0200675 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
676 start, count);
Ursula Braunbd6e8a12010-03-08 12:25:18 +0100677 }
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100678
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000679 qdio_handle_aobs(q, start, count);
680
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100681 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
682 q->irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +0200683
684 /* for the next time */
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100685 q->first_to_kick = end;
Jan Glauber779e6e12008-07-17 17:16:48 +0200686 q->qdio_error = 0;
687}
688
689static void __qdio_inbound_processing(struct qdio_q *q)
690{
Jan Glauber6486cda2010-01-04 09:05:42 +0100691 qperf_inc(q, tasklet_inbound);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200692
Jan Glauber779e6e12008-07-17 17:16:48 +0200693 if (!qdio_inbound_q_moved(q))
694 return;
695
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100696 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200697
Jan Glauber6486cda2010-01-04 09:05:42 +0100698 if (!qdio_inbound_q_done(q)) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200699 /* means poll time is not yet over */
Jan Glauber6486cda2010-01-04 09:05:42 +0100700 qperf_inc(q, tasklet_inbound_resched);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200701 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
702 tasklet_schedule(&q->tasklet);
703 return;
704 }
Jan Glauber6486cda2010-01-04 09:05:42 +0100705 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200706
707 qdio_stop_polling(q);
708 /*
709 * We need to check again to not lose initiative after
710 * resetting the ACK state.
711 */
Jan Glauber6486cda2010-01-04 09:05:42 +0100712 if (!qdio_inbound_q_done(q)) {
713 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauberf3eb20f2010-05-17 10:00:15 +0200714 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
715 tasklet_schedule(&q->tasklet);
Jan Glauber6486cda2010-01-04 09:05:42 +0100716 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200717}
718
Jan Glauber779e6e12008-07-17 17:16:48 +0200719void qdio_inbound_processing(unsigned long data)
720{
721 struct qdio_q *q = (struct qdio_q *)data;
722 __qdio_inbound_processing(q);
723}
724
725static int get_outbound_buffer_frontier(struct qdio_q *q)
726{
727 int count, stop;
Jan Glauber6fa10982011-01-31 11:30:08 +0100728 unsigned char state = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200729
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200730 q->timestamp = get_tod_clock_fast();
Jan Glaubera2b86012011-10-30 15:17:05 +0100731
Jan Glauber90adac52011-01-05 12:47:54 +0100732 if (need_siga_sync(q))
733 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
734 !pci_out_supported(q)) ||
735 (queue_type(q) == QDIO_IQDIO_QFMT &&
736 multicast_outbound(q)))
737 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200738
739 /*
740 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
741 * would return 0.
742 */
743 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
744 stop = add_buf(q->first_to_check, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200745 if (q->first_to_check == stop)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000746 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200747
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000748 count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200749 if (!count)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000750 goto out;
Jan Glauber779e6e12008-07-17 17:16:48 +0200751
752 switch (state) {
753 case SLSB_P_OUTPUT_EMPTY:
754 /* the adapter got it */
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000755 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
756 "out empty:%1d %02x", q->nr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200757
758 atomic_sub(count, &q->nr_buf_used);
759 q->first_to_check = add_buf(q->first_to_check, count);
Jan Glauberd3072972010-02-26 22:37:36 +0100760 if (q->irq_ptr->perf_stat_enabled)
761 account_sbals(q, count);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000762
Jan Glauber36e3e722009-06-22 12:08:12 +0200763 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200764 case SLSB_P_OUTPUT_ERROR:
Jan Glauberbffbbd22011-04-20 10:15:33 +0200765 process_buffer_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200766 q->first_to_check = add_buf(q->first_to_check, count);
767 atomic_sub(count, &q->nr_buf_used);
Jan Glauberd3072972010-02-26 22:37:36 +0100768 if (q->irq_ptr->perf_stat_enabled)
769 account_sbals_error(q, count);
Jan Glauber779e6e12008-07-17 17:16:48 +0200770 break;
771 case SLSB_CU_OUTPUT_PRIMED:
772 /* the adapter has not fetched the output yet */
Jan Glauberd3072972010-02-26 22:37:36 +0100773 if (q->irq_ptr->perf_stat_enabled)
774 q->q_stats.nr_sbal_nop++;
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000775 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
776 q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200777 break;
778 case SLSB_P_OUTPUT_NOT_INIT:
779 case SLSB_P_OUTPUT_HALTED:
780 break;
781 default:
Jan Glauberce1d8012012-10-24 12:38:35 +0200782 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +0200783 }
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000784
785out:
Jan Glauber779e6e12008-07-17 17:16:48 +0200786 return q->first_to_check;
787}
788
789/* all buffers processed? */
790static inline int qdio_outbound_q_done(struct qdio_q *q)
791{
792 return atomic_read(&q->nr_buf_used) == 0;
793}
794
795static inline int qdio_outbound_q_moved(struct qdio_q *q)
796{
797 int bufnr;
798
799 bufnr = get_outbound_buffer_frontier(q);
800
Jan Glauber1549d132012-05-09 16:27:34 +0200801 if (bufnr != q->last_move) {
Jan Glaubere85dea02009-03-26 15:24:29 +0100802 q->last_move = bufnr;
Jan Glauber22f99342008-12-25 13:38:46 +0100803 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
Jan Glauber779e6e12008-07-17 17:16:48 +0200804 return 1;
805 } else
806 return 0;
807}
808
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000809static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
Jan Glauber779e6e12008-07-17 17:16:48 +0200810{
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200811 int retries = 0, cc;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100812 unsigned int busy_bit;
Jan Glauber779e6e12008-07-17 17:16:48 +0200813
814 if (!need_siga_out(q))
Jan Glauberd303b6f2009-03-26 15:24:31 +0100815 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200816
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100817 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200818retry:
Jan Glauber6486cda2010-01-04 09:05:42 +0100819 qperf_inc(q, siga_write);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100820
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000821 cc = qdio_siga_output(q, &busy_bit, aob);
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100822 switch (cc) {
Jan Glauber779e6e12008-07-17 17:16:48 +0200823 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200824 break;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100825 case 2:
826 if (busy_bit) {
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200827 while (++retries < QDIO_BUSY_BIT_RETRIES) {
828 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
829 goto retry;
830 }
831 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200832 cc = -EBUSY;
833 } else {
Jan Glauberd303b6f2009-03-26 15:24:31 +0100834 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
Jan Glauber1549d132012-05-09 16:27:34 +0200835 cc = -ENOBUFS;
836 }
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100837 break;
838 case 1:
839 case 3:
840 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
Jan Glauber1549d132012-05-09 16:27:34 +0200841 cc = -EIO;
Jan Glauber7a0b4cb2008-12-25 13:38:48 +0100842 break;
Jan Glauber779e6e12008-07-17 17:16:48 +0200843 }
Jan Glauberbe8d97a2011-08-03 16:44:17 +0200844 if (retries) {
845 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
846 DBF_ERROR("count:%u", retries);
847 }
Jan Glauberd303b6f2009-03-26 15:24:31 +0100848 return cc;
Jan Glauber779e6e12008-07-17 17:16:48 +0200849}
850
Jan Glauber779e6e12008-07-17 17:16:48 +0200851static void __qdio_outbound_processing(struct qdio_q *q)
852{
Jan Glauber6486cda2010-01-04 09:05:42 +0100853 qperf_inc(q, tasklet_outbound);
Jan Glauberce1d8012012-10-24 12:38:35 +0200854 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
Jan Glauber779e6e12008-07-17 17:16:48 +0200855
856 if (qdio_outbound_q_moved(q))
Jan Glauber9c8a08d2009-03-26 15:24:32 +0100857 qdio_kick_handler(q);
Jan Glauber779e6e12008-07-17 17:16:48 +0200858
Jan Glauberc38f9602009-03-26 15:24:26 +0100859 if (queue_type(q) == QDIO_ZFCP_QFMT)
Jan Glauber779e6e12008-07-17 17:16:48 +0200860 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
Jan Glauberc38f9602009-03-26 15:24:26 +0100861 goto sched;
Jan Glauber779e6e12008-07-17 17:16:48 +0200862
Jan Glauber779e6e12008-07-17 17:16:48 +0200863 if (q->u.out.pci_out_enabled)
864 return;
865
866 /*
867 * Now we know that queue type is either qeth without pci enabled
Jan Glauber25f269f2011-10-30 15:17:06 +0100868 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
869 * is noticed and outbound_handler is called after some time.
Jan Glauber779e6e12008-07-17 17:16:48 +0200870 */
871 if (qdio_outbound_q_done(q))
872 del_timer(&q->u.out.timer);
Jan Glauber6486cda2010-01-04 09:05:42 +0100873 else
874 if (!timer_pending(&q->u.out.timer))
Jan Glauber779e6e12008-07-17 17:16:48 +0200875 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
Jan Glauberc38f9602009-03-26 15:24:26 +0100876 return;
877
878sched:
879 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
880 return;
881 tasklet_schedule(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +0200882}
883
884/* outbound tasklet */
885void qdio_outbound_processing(unsigned long data)
886{
887 struct qdio_q *q = (struct qdio_q *)data;
888 __qdio_outbound_processing(q);
889}
890
891void qdio_outbound_timer(unsigned long data)
892{
893 struct qdio_q *q = (struct qdio_q *)data;
Jan Glauberc38f9602009-03-26 15:24:26 +0100894
895 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
896 return;
Jan Glauber779e6e12008-07-17 17:16:48 +0200897 tasklet_schedule(&q->tasklet);
898}
899
Jan Glauber60b5df22009-06-22 12:08:10 +0200900static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
Jan Glauber779e6e12008-07-17 17:16:48 +0200901{
902 struct qdio_q *out;
903 int i;
904
905 if (!pci_out_supported(q))
906 return;
907
908 for_each_output_queue(q->irq_ptr, out, i)
909 if (!qdio_outbound_q_done(out))
910 tasklet_schedule(&out->tasklet);
911}
912
Jan Glauber60b5df22009-06-22 12:08:10 +0200913static void __tiqdio_inbound_processing(struct qdio_q *q)
914{
Jan Glauber6486cda2010-01-04 09:05:42 +0100915 qperf_inc(q, tasklet_inbound);
Jan Glauber90adac52011-01-05 12:47:54 +0100916 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
917 qdio_sync_queues(q);
Jan Glauber60b5df22009-06-22 12:08:10 +0200918
919 /*
920 * The interrupt could be caused by a PCI request. Check the
921 * PCI capable outbound queues.
922 */
923 qdio_check_outbound_after_thinint(q);
924
925 if (!qdio_inbound_q_moved(q))
926 return;
927
928 qdio_kick_handler(q);
929
Jan Glauber9a2c1602009-06-22 12:08:11 +0200930 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100931 qperf_inc(q, tasklet_inbound_resched);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200932 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
Jan Glauber60b5df22009-06-22 12:08:10 +0200933 tasklet_schedule(&q->tasklet);
Jan Glaubere2910bc2009-09-11 10:28:19 +0200934 return;
935 }
Jan Glauber60b5df22009-06-22 12:08:10 +0200936 }
937
938 qdio_stop_polling(q);
939 /*
940 * We need to check again to not lose initiative after
941 * resetting the ACK state.
942 */
Jan Glauber9a2c1602009-06-22 12:08:11 +0200943 if (!qdio_inbound_q_done(q)) {
Jan Glauber6486cda2010-01-04 09:05:42 +0100944 qperf_inc(q, tasklet_inbound_resched2);
Jan Glauber60b5df22009-06-22 12:08:10 +0200945 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
946 tasklet_schedule(&q->tasklet);
947 }
948}
949
950void tiqdio_inbound_processing(unsigned long data)
951{
952 struct qdio_q *q = (struct qdio_q *)data;
953 __tiqdio_inbound_processing(q);
954}
955
Jan Glauber779e6e12008-07-17 17:16:48 +0200956static inline void qdio_set_state(struct qdio_irq *irq_ptr,
957 enum qdio_irq_states state)
958{
Jan Glauber22f99342008-12-25 13:38:46 +0100959 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
Jan Glauber779e6e12008-07-17 17:16:48 +0200960
961 irq_ptr->state = state;
962 mb();
963}
964
Jan Glauber22f99342008-12-25 13:38:46 +0100965static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
Jan Glauber779e6e12008-07-17 17:16:48 +0200966{
Jan Glauber779e6e12008-07-17 17:16:48 +0200967 if (irb->esw.esw0.erw.cons) {
Jan Glauber22f99342008-12-25 13:38:46 +0100968 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
969 DBF_ERROR_HEX(irb, 64);
970 DBF_ERROR_HEX(irb->ecw, 64);
Jan Glauber779e6e12008-07-17 17:16:48 +0200971 }
972}
973
974/* PCI interrupt handler */
975static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
976{
977 int i;
978 struct qdio_q *q;
979
Jan Glauberc38f9602009-03-26 15:24:26 +0100980 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
981 return;
982
Jan Glauberd36deae2010-09-07 21:14:39 +0000983 for_each_input_queue(irq_ptr, q, i) {
984 if (q->u.in.queue_start_poll) {
985 /* skip if polling is enabled or already in work */
986 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
987 &q->u.in.queue_irq_state)) {
988 qperf_inc(q, int_discarded);
989 continue;
990 }
991 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
992 q->irq_ptr->int_parm);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000993 } else {
Jan Glauberd36deae2010-09-07 21:14:39 +0000994 tasklet_schedule(&q->tasklet);
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +0000995 }
Jan Glauberd36deae2010-09-07 21:14:39 +0000996 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200997
Ursula Braun0f308f42014-01-28 13:06:47 +0100998 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
Jan Glauber779e6e12008-07-17 17:16:48 +0200999 return;
1000
1001 for_each_output_queue(irq_ptr, q, i) {
1002 if (qdio_outbound_q_done(q))
1003 continue;
Jan Glauber90adac52011-01-05 12:47:54 +01001004 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
Jan Glauber779e6e12008-07-17 17:16:48 +02001005 qdio_siga_sync_q(q);
Jan Glauber779e6e12008-07-17 17:16:48 +02001006 tasklet_schedule(&q->tasklet);
1007 }
1008}
1009
1010static void qdio_handle_activate_check(struct ccw_device *cdev,
1011 unsigned long intparm, int cstat, int dstat)
1012{
1013 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1014 struct qdio_q *q;
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001015 int count;
Jan Glauber779e6e12008-07-17 17:16:48 +02001016
Jan Glauber22f99342008-12-25 13:38:46 +01001017 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1018 DBF_ERROR("intp :%lx", intparm);
1019 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
Jan Glauber779e6e12008-07-17 17:16:48 +02001020
1021 if (irq_ptr->nr_input_qs) {
1022 q = irq_ptr->input_qs[0];
1023 } else if (irq_ptr->nr_output_qs) {
1024 q = irq_ptr->output_qs[0];
1025 } else {
1026 dump_stack();
1027 goto no_handler;
1028 }
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001029
1030 count = sub_buf(q->first_to_check, q->first_to_kick);
Jan Glauber1549d132012-05-09 16:27:34 +02001031 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
Swen Schilligdfe5bb52011-08-15 14:40:31 +02001032 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
Jan Glauber779e6e12008-07-17 17:16:48 +02001033no_handler:
1034 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
Michael Holzheu3ab121a2012-03-11 11:59:32 -04001035 /*
1036 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1037 * Therefore we call the LGR detection function here.
1038 */
1039 lgr_info_log();
Jan Glauber779e6e12008-07-17 17:16:48 +02001040}
1041
Jan Glauber779e6e12008-07-17 17:16:48 +02001042static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1043 int dstat)
1044{
1045 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Jan Glauber779e6e12008-07-17 17:16:48 +02001046
Jan Glauber22f99342008-12-25 13:38:46 +01001047 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
Jan Glauber4c575422009-06-12 10:26:28 +02001048
1049 if (cstat)
1050 goto error;
1051 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1052 goto error;
1053 if (!(dstat & DEV_STAT_DEV_END))
1054 goto error;
1055 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1056 return;
1057
1058error:
1059 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1060 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1061 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
Jan Glauber779e6e12008-07-17 17:16:48 +02001062}
1063
1064/* qdio interrupt handler */
1065void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1066 struct irb *irb)
1067{
1068 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001069 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001070 int cstat, dstat;
Jan Glauber779e6e12008-07-17 17:16:48 +02001071
Jan Glauber779e6e12008-07-17 17:16:48 +02001072 if (!intparm || !irq_ptr) {
Sebastian Ott9080c922016-07-28 20:30:31 +02001073 ccw_device_get_schid(cdev, &schid);
1074 DBF_ERROR("qint:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001075 return;
1076 }
1077
Jan Glauber09a308f2010-05-17 10:00:14 +02001078 if (irq_ptr->perf_stat_enabled)
1079 irq_ptr->perf_stat.qdio_int++;
1080
Jan Glauber779e6e12008-07-17 17:16:48 +02001081 if (IS_ERR(irb)) {
Jan Glauberce1d8012012-10-24 12:38:35 +02001082 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1083 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1084 wake_up(&cdev->private->wait_q);
1085 return;
Jan Glauber779e6e12008-07-17 17:16:48 +02001086 }
Jan Glauber22f99342008-12-25 13:38:46 +01001087 qdio_irq_check_sense(irq_ptr, irb);
Jan Glauber779e6e12008-07-17 17:16:48 +02001088 cstat = irb->scsw.cmd.cstat;
1089 dstat = irb->scsw.cmd.dstat;
1090
1091 switch (irq_ptr->state) {
1092 case QDIO_IRQ_STATE_INACTIVE:
1093 qdio_establish_handle_irq(cdev, cstat, dstat);
1094 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001095 case QDIO_IRQ_STATE_CLEANUP:
1096 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1097 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001098 case QDIO_IRQ_STATE_ESTABLISHED:
1099 case QDIO_IRQ_STATE_ACTIVE:
1100 if (cstat & SCHN_STAT_PCI) {
1101 qdio_int_handler_pci(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001102 return;
1103 }
Jan Glauber4c575422009-06-12 10:26:28 +02001104 if (cstat || dstat)
Jan Glauber779e6e12008-07-17 17:16:48 +02001105 qdio_handle_activate_check(cdev, intparm, cstat,
1106 dstat);
Jan Glauber4c575422009-06-12 10:26:28 +02001107 break;
Jan Glauber959153d2010-02-09 09:46:08 +01001108 case QDIO_IRQ_STATE_STOPPED:
1109 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001110 default:
Jan Glauberce1d8012012-10-24 12:38:35 +02001111 WARN_ON_ONCE(1);
Jan Glauber779e6e12008-07-17 17:16:48 +02001112 }
1113 wake_up(&cdev->private->wait_q);
1114}
1115
1116/**
1117 * qdio_get_ssqd_desc - get qdio subchannel description
1118 * @cdev: ccw device to get description for
Jan Glauberbbd50e12008-12-25 13:38:43 +01001119 * @data: where to store the ssqd
Jan Glauber779e6e12008-07-17 17:16:48 +02001120 *
Jan Glauberbbd50e12008-12-25 13:38:43 +01001121 * Returns 0 or an error code. The results of the chsc are stored in the
1122 * specified structure.
Jan Glauber779e6e12008-07-17 17:16:48 +02001123 */
Jan Glauberbbd50e12008-12-25 13:38:43 +01001124int qdio_get_ssqd_desc(struct ccw_device *cdev,
1125 struct qdio_ssqd_desc *data)
Jan Glauber779e6e12008-07-17 17:16:48 +02001126{
Sebastian Ott9080c922016-07-28 20:30:31 +02001127 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001128
Jan Glauberbbd50e12008-12-25 13:38:43 +01001129 if (!cdev || !cdev->private)
1130 return -EINVAL;
1131
Sebastian Ott9080c922016-07-28 20:30:31 +02001132 ccw_device_get_schid(cdev, &schid);
1133 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1134 return qdio_setup_get_ssqd(NULL, &schid, data);
Jan Glauber779e6e12008-07-17 17:16:48 +02001135}
1136EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1137
Jan Glauber779e6e12008-07-17 17:16:48 +02001138static void qdio_shutdown_queues(struct ccw_device *cdev)
1139{
1140 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1141 struct qdio_q *q;
1142 int i;
1143
1144 for_each_input_queue(irq_ptr, q, i)
Jan Glauberc38f9602009-03-26 15:24:26 +01001145 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001146
1147 for_each_output_queue(irq_ptr, q, i) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001148 del_timer(&q->u.out.timer);
Jan Glauberc38f9602009-03-26 15:24:26 +01001149 tasklet_kill(&q->tasklet);
Jan Glauber779e6e12008-07-17 17:16:48 +02001150 }
1151}
1152
1153/**
1154 * qdio_shutdown - shut down a qdio subchannel
1155 * @cdev: associated ccw device
1156 * @how: use halt or clear to shutdown
1157 */
1158int qdio_shutdown(struct ccw_device *cdev, int how)
1159{
Jan Glauber22f99342008-12-25 13:38:46 +01001160 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001161 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001162 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001163
Jan Glauber779e6e12008-07-17 17:16:48 +02001164 if (!irq_ptr)
1165 return -ENODEV;
1166
Jan Glauberce1d8012012-10-24 12:38:35 +02001167 WARN_ON_ONCE(irqs_disabled());
Sebastian Ott9080c922016-07-28 20:30:31 +02001168 ccw_device_get_schid(cdev, &schid);
1169 DBF_EVENT("qshutdown:%4x", schid.sch_no);
Jan Glauber22f99342008-12-25 13:38:46 +01001170
Jan Glauber779e6e12008-07-17 17:16:48 +02001171 mutex_lock(&irq_ptr->setup_mutex);
1172 /*
1173 * Subchannel was already shot down. We cannot prevent being called
1174 * twice since cio may trigger a shutdown asynchronously.
1175 */
1176 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1177 mutex_unlock(&irq_ptr->setup_mutex);
1178 return 0;
1179 }
1180
Jan Glauberc38f9602009-03-26 15:24:26 +01001181 /*
1182 * Indicate that the device is going down. Scheduling the queue
1183 * tasklets is forbidden from here on.
1184 */
1185 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1186
Jan Glauber779e6e12008-07-17 17:16:48 +02001187 tiqdio_remove_input_queues(irq_ptr);
1188 qdio_shutdown_queues(cdev);
Stefan Rasplaa2383f2013-02-26 13:08:34 +01001189 qdio_shutdown_debug_entries(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001190
1191 /* cleanup subchannel */
Sebastian Otta48ed862016-07-29 13:41:20 +02001192 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001193
1194 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1195 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1196 else
1197 /* default behaviour is halt */
1198 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1199 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001200 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1201 DBF_ERROR("rc:%4d", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001202 goto no_cleanup;
1203 }
1204
1205 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
Sebastian Otta48ed862016-07-29 13:41:20 +02001206 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001207 wait_event_interruptible_timeout(cdev->private->wait_q,
1208 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1209 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1210 10 * HZ);
Sebastian Otta48ed862016-07-29 13:41:20 +02001211 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001212
1213no_cleanup:
1214 qdio_shutdown_thinint(irq_ptr);
1215
1216 /* restore interrupt handler */
1217 if ((void *)cdev->handler == (void *)qdio_int_handler)
1218 cdev->handler = irq_ptr->orig_handler;
Sebastian Otta48ed862016-07-29 13:41:20 +02001219 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001220
1221 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1222 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001223 if (rc)
1224 return rc;
1225 return 0;
1226}
1227EXPORT_SYMBOL_GPL(qdio_shutdown);
1228
1229/**
1230 * qdio_free - free data structures for a qdio subchannel
1231 * @cdev: associated ccw device
1232 */
1233int qdio_free(struct ccw_device *cdev)
1234{
Jan Glauber22f99342008-12-25 13:38:46 +01001235 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
Sebastian Ott9080c922016-07-28 20:30:31 +02001236 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001237
Jan Glauber779e6e12008-07-17 17:16:48 +02001238 if (!irq_ptr)
1239 return -ENODEV;
1240
Sebastian Ott9080c922016-07-28 20:30:31 +02001241 ccw_device_get_schid(cdev, &schid);
1242 DBF_EVENT("qfree:%4x", schid.sch_no);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001243 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
Jan Glauber779e6e12008-07-17 17:16:48 +02001244 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber22f99342008-12-25 13:38:46 +01001245
Stefan Raspl613c4e02014-06-12 14:24:45 +02001246 irq_ptr->debug_area = NULL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001247 cdev->private->qdio_data = NULL;
1248 mutex_unlock(&irq_ptr->setup_mutex);
1249
1250 qdio_release_memory(irq_ptr);
1251 return 0;
1252}
1253EXPORT_SYMBOL_GPL(qdio_free);
1254
1255/**
Jan Glauber779e6e12008-07-17 17:16:48 +02001256 * qdio_allocate - allocate qdio queues and associated data
1257 * @init_data: initialization data
1258 */
1259int qdio_allocate(struct qdio_initialize *init_data)
1260{
Sebastian Ott9080c922016-07-28 20:30:31 +02001261 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001262 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001263
Sebastian Ott9080c922016-07-28 20:30:31 +02001264 ccw_device_get_schid(init_data->cdev, &schid);
1265 DBF_EVENT("qallocate:%4x", schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001266
1267 if ((init_data->no_input_qs && !init_data->input_handler) ||
1268 (init_data->no_output_qs && !init_data->output_handler))
1269 return -EINVAL;
1270
1271 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1272 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1273 return -EINVAL;
1274
1275 if ((!init_data->input_sbal_addr_array) ||
1276 (!init_data->output_sbal_addr_array))
1277 return -EINVAL;
1278
Jan Glauber779e6e12008-07-17 17:16:48 +02001279 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1280 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1281 if (!irq_ptr)
1282 goto out_err;
Jan Glauber779e6e12008-07-17 17:16:48 +02001283
1284 mutex_init(&irq_ptr->setup_mutex);
Stefan Raspl613c4e02014-06-12 14:24:45 +02001285 if (qdio_allocate_dbf(init_data, irq_ptr))
1286 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001287
1288 /*
1289 * Allocate a page for the chsc calls in qdio_establish.
1290 * Must be pre-allocated since a zfcp recovery will call
1291 * qdio_establish. In case of low memory and swap on a zfcp disk
1292 * we may not be able to allocate memory otherwise.
1293 */
1294 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1295 if (!irq_ptr->chsc_page)
1296 goto out_rel;
1297
1298 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001299 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001300 if (!irq_ptr->qdr)
1301 goto out_rel;
Jan Glauber779e6e12008-07-17 17:16:48 +02001302
Jan Glauber779e6e12008-07-17 17:16:48 +02001303 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1304 init_data->no_output_qs))
1305 goto out_rel;
1306
1307 init_data->cdev->private->qdio_data = irq_ptr;
1308 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1309 return 0;
1310out_rel:
1311 qdio_release_memory(irq_ptr);
1312out_err:
1313 return -ENOMEM;
1314}
1315EXPORT_SYMBOL_GPL(qdio_allocate);
1316
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001317static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1318{
1319 struct qdio_q *q = irq_ptr->input_qs[0];
1320 int i, use_cq = 0;
1321
1322 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1323 use_cq = 1;
1324
1325 for_each_output_queue(irq_ptr, q, i) {
1326 if (use_cq) {
1327 if (qdio_enable_async_operation(&q->u.out) < 0) {
1328 use_cq = 0;
1329 continue;
1330 }
1331 } else
1332 qdio_disable_async_operation(&q->u.out);
1333 }
1334 DBF_EVENT("use_cq:%d", use_cq);
1335}
1336
Jan Glauber779e6e12008-07-17 17:16:48 +02001337/**
1338 * qdio_establish - establish queues on a qdio subchannel
1339 * @init_data: initialization data
1340 */
1341int qdio_establish(struct qdio_initialize *init_data)
1342{
Jan Glauber779e6e12008-07-17 17:16:48 +02001343 struct ccw_device *cdev = init_data->cdev;
Sebastian Ott9080c922016-07-28 20:30:31 +02001344 struct subchannel_id schid;
1345 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001346 int rc;
1347
Sebastian Ott9080c922016-07-28 20:30:31 +02001348 ccw_device_get_schid(cdev, &schid);
1349 DBF_EVENT("qestablish:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001350
Jan Glauber779e6e12008-07-17 17:16:48 +02001351 irq_ptr = cdev->private->qdio_data;
1352 if (!irq_ptr)
1353 return -ENODEV;
1354
1355 if (cdev->private->state != DEV_STATE_ONLINE)
1356 return -EINVAL;
1357
Jan Glauber779e6e12008-07-17 17:16:48 +02001358 mutex_lock(&irq_ptr->setup_mutex);
1359 qdio_setup_irq(init_data);
1360
1361 rc = qdio_establish_thinint(irq_ptr);
1362 if (rc) {
1363 mutex_unlock(&irq_ptr->setup_mutex);
1364 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1365 return rc;
1366 }
1367
1368 /* establish q */
1369 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1370 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1371 irq_ptr->ccw.count = irq_ptr->equeue.count;
1372 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1373
Sebastian Otta48ed862016-07-29 13:41:20 +02001374 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001375 ccw_device_set_options_mask(cdev, 0);
1376
1377 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
Sebastian Ottddebf662016-07-29 14:00:27 +02001378 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001379 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001380 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1381 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001382 mutex_unlock(&irq_ptr->setup_mutex);
1383 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1384 return rc;
1385 }
1386
1387 wait_event_interruptible_timeout(cdev->private->wait_q,
1388 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1389 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1390
1391 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1392 mutex_unlock(&irq_ptr->setup_mutex);
1393 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1394 return -EIO;
1395 }
1396
1397 qdio_setup_ssqd_info(irq_ptr);
Jan Glauber779e6e12008-07-17 17:16:48 +02001398
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001399 qdio_detect_hsicq(irq_ptr);
1400
Jan Glauber779e6e12008-07-17 17:16:48 +02001401 /* qebsm is now setup if available, initialize buffer states */
1402 qdio_init_buf_states(irq_ptr);
1403
1404 mutex_unlock(&irq_ptr->setup_mutex);
1405 qdio_print_subchannel_info(irq_ptr, cdev);
1406 qdio_setup_debug_entries(irq_ptr, cdev);
1407 return 0;
1408}
1409EXPORT_SYMBOL_GPL(qdio_establish);
1410
1411/**
1412 * qdio_activate - activate queues on a qdio subchannel
1413 * @cdev: associated cdev
1414 */
1415int qdio_activate(struct ccw_device *cdev)
1416{
Sebastian Ott9080c922016-07-28 20:30:31 +02001417 struct subchannel_id schid;
Jan Glauber779e6e12008-07-17 17:16:48 +02001418 struct qdio_irq *irq_ptr;
1419 int rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001420
Sebastian Ott9080c922016-07-28 20:30:31 +02001421 ccw_device_get_schid(cdev, &schid);
1422 DBF_EVENT("qactivate:%4x", schid.sch_no);
Jan Glauber58eb27c2008-08-21 19:46:34 +02001423
Jan Glauber779e6e12008-07-17 17:16:48 +02001424 irq_ptr = cdev->private->qdio_data;
1425 if (!irq_ptr)
1426 return -ENODEV;
1427
1428 if (cdev->private->state != DEV_STATE_ONLINE)
1429 return -EINVAL;
1430
1431 mutex_lock(&irq_ptr->setup_mutex);
1432 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1433 rc = -EBUSY;
1434 goto out;
1435 }
1436
Jan Glauber779e6e12008-07-17 17:16:48 +02001437 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1438 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1439 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1440 irq_ptr->ccw.cda = 0;
1441
Sebastian Otta48ed862016-07-29 13:41:20 +02001442 spin_lock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001443 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1444
1445 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1446 0, DOIO_DENY_PREFETCH);
Sebastian Ottddebf662016-07-29 14:00:27 +02001447 spin_unlock_irq(get_ccwdev_lock(cdev));
Jan Glauber779e6e12008-07-17 17:16:48 +02001448 if (rc) {
Jan Glauber22f99342008-12-25 13:38:46 +01001449 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1450 DBF_ERROR("rc:%4x", rc);
Jan Glauber779e6e12008-07-17 17:16:48 +02001451 goto out;
Sebastian Ottddebf662016-07-29 14:00:27 +02001452 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001453
1454 if (is_thinint_irq(irq_ptr))
1455 tiqdio_add_input_queues(irq_ptr);
1456
1457 /* wait for subchannel to become active */
1458 msleep(5);
1459
1460 switch (irq_ptr->state) {
1461 case QDIO_IRQ_STATE_STOPPED:
1462 case QDIO_IRQ_STATE_ERR:
Jan Glaubere4c14e22009-03-26 15:24:25 +01001463 rc = -EIO;
1464 break;
Jan Glauber779e6e12008-07-17 17:16:48 +02001465 default:
1466 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1467 rc = 0;
1468 }
1469out:
1470 mutex_unlock(&irq_ptr->setup_mutex);
1471 return rc;
1472}
1473EXPORT_SYMBOL_GPL(qdio_activate);
1474
1475static inline int buf_in_between(int bufnr, int start, int count)
1476{
1477 int end = add_buf(start, count);
1478
1479 if (end > start) {
1480 if (bufnr >= start && bufnr < end)
1481 return 1;
1482 else
1483 return 0;
1484 }
1485
1486 /* wrap-around case */
1487 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1488 (bufnr < end))
1489 return 1;
1490 else
1491 return 0;
1492}
1493
1494/**
1495 * handle_inbound - reset processed input buffers
1496 * @q: queue containing the buffers
1497 * @callflags: flags
1498 * @bufnr: first buffer to process
1499 * @count: how many buffers are emptied
1500 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001501static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1502 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001503{
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001504 int diff;
Jan Glauber779e6e12008-07-17 17:16:48 +02001505
Jan Glauber6486cda2010-01-04 09:05:42 +01001506 qperf_inc(q, inbound_call);
1507
Jan Glauber50f769d2008-12-25 13:38:47 +01001508 if (!q->u.in.polling)
1509 goto set;
1510
1511 /* protect against stop polling setting an ACK for an emptied slsb */
1512 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1513 /* overwriting everything, just delete polling status */
1514 q->u.in.polling = 0;
1515 q->u.in.ack_count = 0;
1516 goto set;
Jan Glaubere85dea02009-03-26 15:24:29 +01001517 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
Jan Glauber50f769d2008-12-25 13:38:47 +01001518 if (is_qebsm(q)) {
Jan Glaubere85dea02009-03-26 15:24:29 +01001519 /* partial overwrite, just update ack_start */
Jan Glauber50f769d2008-12-25 13:38:47 +01001520 diff = add_buf(bufnr, count);
Jan Glaubere85dea02009-03-26 15:24:29 +01001521 diff = sub_buf(diff, q->u.in.ack_start);
Jan Glauber50f769d2008-12-25 13:38:47 +01001522 q->u.in.ack_count -= diff;
1523 if (q->u.in.ack_count <= 0) {
1524 q->u.in.polling = 0;
1525 q->u.in.ack_count = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001526 goto set;
1527 }
Jan Glaubere85dea02009-03-26 15:24:29 +01001528 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
Jan Glauber50f769d2008-12-25 13:38:47 +01001529 }
1530 else
1531 /* the only ACK will be deleted, so stop polling */
Jan Glauber779e6e12008-07-17 17:16:48 +02001532 q->u.in.polling = 0;
Jan Glauber50f769d2008-12-25 13:38:47 +01001533 }
Jan Glauber779e6e12008-07-17 17:16:48 +02001534
Jan Glauber50f769d2008-12-25 13:38:47 +01001535set:
Jan Glauber779e6e12008-07-17 17:16:48 +02001536 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
Sebastian Ottdae7fd42013-07-05 09:22:11 +02001537 atomic_add(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001538
Jan Glauberd303b6f2009-03-26 15:24:31 +01001539 if (need_siga_in(q))
1540 return qdio_siga_input(q);
frank.blaschka@de.ibm.com9cb72842011-08-08 01:33:56 +00001541
Jan Glauberd303b6f2009-03-26 15:24:31 +01001542 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001543}
1544
1545/**
1546 * handle_outbound - process filled outbound buffers
1547 * @q: queue containing the buffers
1548 * @callflags: flags
1549 * @bufnr: first buffer to process
1550 * @count: how many buffers are filled
1551 */
Jan Glauberd303b6f2009-03-26 15:24:31 +01001552static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1553 int bufnr, int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001554{
Jan Glauberc26001d2011-05-23 10:24:38 +02001555 unsigned char state = 0;
Jan Glauberd303b6f2009-03-26 15:24:31 +01001556 int used, rc = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001557
Jan Glauber6486cda2010-01-04 09:05:42 +01001558 qperf_inc(q, outbound_call);
Jan Glauber779e6e12008-07-17 17:16:48 +02001559
1560 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1561 used = atomic_add_return(count, &q->nr_buf_used);
Jan Glauber779e6e12008-07-17 17:16:48 +02001562
Jan Glauber01958432011-01-05 12:47:51 +01001563 if (used == QDIO_MAX_BUFFERS_PER_Q)
1564 qperf_inc(q, outbound_queue_full);
1565
Jan Glauber6486cda2010-01-04 09:05:42 +01001566 if (callflags & QDIO_FLAG_PCI_OUT) {
Jan Glauber779e6e12008-07-17 17:16:48 +02001567 q->u.out.pci_out_enabled = 1;
Jan Glauber6486cda2010-01-04 09:05:42 +01001568 qperf_inc(q, pci_request_int);
Jan Glauber110da312011-01-05 12:47:53 +01001569 } else
Jan Glauber779e6e12008-07-17 17:16:48 +02001570 q->u.out.pci_out_enabled = 0;
1571
1572 if (queue_type(q) == QDIO_IQDIO_QFMT) {
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001573 unsigned long phys_aob = 0;
1574
1575 /* One SIGA-W per buffer required for unicast HSI */
Jan Glauber110da312011-01-05 12:47:53 +01001576 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1577
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001578 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1579
1580 rc = qdio_kick_outbound_q(q, phys_aob);
Jan Glauber90adac52011-01-05 12:47:54 +01001581 } else if (need_siga_sync(q)) {
Jan Glauber110da312011-01-05 12:47:53 +01001582 rc = qdio_siga_sync_q(q);
1583 } else {
1584 /* try to fast requeue buffers */
1585 get_buf_state(q, prev_buf(bufnr), &state, 0);
1586 if (state != SLSB_CU_OUTPUT_PRIMED)
frank.blaschka@de.ibm.com104ea552011-08-08 01:33:55 +00001587 rc = qdio_kick_outbound_q(q, 0);
Jan Glauber779e6e12008-07-17 17:16:48 +02001588 else
Jan Glauber110da312011-01-05 12:47:53 +01001589 qperf_inc(q, fast_requeue);
Jan Glauber779e6e12008-07-17 17:16:48 +02001590 }
1591
Jan Glauber3d6c76f2011-01-05 12:47:50 +01001592 /* in case of SIGA errors we must process the error immediately */
1593 if (used >= q->u.out.scan_threshold || rc)
1594 tasklet_schedule(&q->tasklet);
1595 else
1596 /* free the SBALs in case of no further traffic */
1597 if (!timer_pending(&q->u.out.timer))
1598 mod_timer(&q->u.out.timer, jiffies + HZ);
Jan Glauberd303b6f2009-03-26 15:24:31 +01001599 return rc;
Jan Glauber779e6e12008-07-17 17:16:48 +02001600}
1601
1602/**
1603 * do_QDIO - process input or output buffers
1604 * @cdev: associated ccw_device for the qdio subchannel
1605 * @callflags: input or output and special flags from the program
1606 * @q_nr: queue number
1607 * @bufnr: buffer number
1608 * @count: how many buffers to process
1609 */
1610int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
Jan Glauber66182412009-06-22 12:08:15 +02001611 int q_nr, unsigned int bufnr, unsigned int count)
Jan Glauber779e6e12008-07-17 17:16:48 +02001612{
1613 struct qdio_irq *irq_ptr;
Jan Glauber779e6e12008-07-17 17:16:48 +02001614
Jan Glauber66182412009-06-22 12:08:15 +02001615 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
Jan Glauber779e6e12008-07-17 17:16:48 +02001616 return -EINVAL;
1617
Jan Glauber779e6e12008-07-17 17:16:48 +02001618 irq_ptr = cdev->private->qdio_data;
1619 if (!irq_ptr)
1620 return -ENODEV;
1621
Jan Glauber1d7e1502009-09-22 22:58:39 +02001622 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1623 "do%02x b:%02x c:%02x", callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001624
1625 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
Jan Glauber1549d132012-05-09 16:27:34 +02001626 return -EIO;
Jan Glauber9a265132011-03-23 10:16:01 +01001627 if (!count)
1628 return 0;
Jan Glauber779e6e12008-07-17 17:16:48 +02001629 if (callflags & QDIO_FLAG_SYNC_INPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001630 return handle_inbound(irq_ptr->input_qs[q_nr],
1631 callflags, bufnr, count);
Jan Glauber779e6e12008-07-17 17:16:48 +02001632 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
Jan Glauberd303b6f2009-03-26 15:24:31 +01001633 return handle_outbound(irq_ptr->output_qs[q_nr],
1634 callflags, bufnr, count);
1635 return -EINVAL;
Jan Glauber779e6e12008-07-17 17:16:48 +02001636}
1637EXPORT_SYMBOL_GPL(do_QDIO);
1638
Jan Glauberd36deae2010-09-07 21:14:39 +00001639/**
1640 * qdio_start_irq - process input buffers
1641 * @cdev: associated ccw_device for the qdio subchannel
1642 * @nr: input queue number
1643 *
1644 * Return codes
1645 * 0 - success
1646 * 1 - irqs not started since new data is available
1647 */
1648int qdio_start_irq(struct ccw_device *cdev, int nr)
1649{
1650 struct qdio_q *q;
1651 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1652
1653 if (!irq_ptr)
1654 return -ENODEV;
1655 q = irq_ptr->input_qs[nr];
1656
Jan Glauber5f4026f2011-10-30 15:17:20 +01001657 clear_nonshared_ind(irq_ptr);
Jan Glauberd36deae2010-09-07 21:14:39 +00001658 qdio_stop_polling(q);
1659 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1660
1661 /*
1662 * We need to check again to not lose initiative after
1663 * resetting the ACK state.
1664 */
Jan Glauber5f4026f2011-10-30 15:17:20 +01001665 if (test_nonshared_ind(irq_ptr))
Jan Glauberd36deae2010-09-07 21:14:39 +00001666 goto rescan;
1667 if (!qdio_inbound_q_done(q))
1668 goto rescan;
1669 return 0;
1670
1671rescan:
1672 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1673 &q->u.in.queue_irq_state))
1674 return 0;
1675 else
1676 return 1;
1677
1678}
1679EXPORT_SYMBOL(qdio_start_irq);
1680
1681/**
1682 * qdio_get_next_buffers - process input buffers
1683 * @cdev: associated ccw_device for the qdio subchannel
1684 * @nr: input queue number
1685 * @bufnr: first filled buffer number
1686 * @error: buffers are in error state
1687 *
1688 * Return codes
1689 * < 0 - error
1690 * = 0 - no new buffers found
1691 * > 0 - number of processed buffers
1692 */
1693int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1694 int *error)
1695{
1696 struct qdio_q *q;
1697 int start, end;
1698 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1699
1700 if (!irq_ptr)
1701 return -ENODEV;
1702 q = irq_ptr->input_qs[nr];
Jan Glauberd36deae2010-09-07 21:14:39 +00001703
Jan Glauberd36deae2010-09-07 21:14:39 +00001704 /*
Jan Glauber90adac52011-01-05 12:47:54 +01001705 * Cannot rely on automatic sync after interrupt since queues may
1706 * also be examined without interrupt.
Jan Glauberd36deae2010-09-07 21:14:39 +00001707 */
Jan Glauber90adac52011-01-05 12:47:54 +01001708 if (need_siga_sync(q))
1709 qdio_sync_queues(q);
1710
1711 /* check the PCI capable outbound queues. */
Jan Glauberd36deae2010-09-07 21:14:39 +00001712 qdio_check_outbound_after_thinint(q);
1713
1714 if (!qdio_inbound_q_moved(q))
1715 return 0;
1716
1717 /* Note: upper-layer MUST stop processing immediately here ... */
1718 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1719 return -EIO;
1720
1721 start = q->first_to_kick;
1722 end = q->first_to_check;
1723 *bufnr = start;
1724 *error = q->qdio_error;
1725
1726 /* for the next time */
1727 q->first_to_kick = end;
1728 q->qdio_error = 0;
1729 return sub_buf(end, start);
1730}
1731EXPORT_SYMBOL(qdio_get_next_buffers);
1732
1733/**
1734 * qdio_stop_irq - disable interrupt processing for the device
1735 * @cdev: associated ccw_device for the qdio subchannel
1736 * @nr: input queue number
1737 *
1738 * Return codes
1739 * 0 - interrupts were already disabled
1740 * 1 - interrupts successfully disabled
1741 */
1742int qdio_stop_irq(struct ccw_device *cdev, int nr)
1743{
1744 struct qdio_q *q;
1745 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1746
1747 if (!irq_ptr)
1748 return -ENODEV;
1749 q = irq_ptr->input_qs[nr];
1750
1751 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1752 &q->u.in.queue_irq_state))
1753 return 0;
1754 else
1755 return 1;
1756}
1757EXPORT_SYMBOL(qdio_stop_irq);
1758
Eugene Crosser1c59a862013-04-24 12:00:23 +02001759/**
1760 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1761 * @schid: Subchannel ID.
1762 * @cnc: Boolean Change-Notification Control
1763 * @response: Response code will be stored at this address
1764 * @cb: Callback function will be executed for each element
1765 * of the address list
1766 * @priv: Pointer passed from the caller to qdio_pnso_brinfo()
1767 * @type: Type of the address entry passed to the callback
1768 * @entry: Entry containg the address of the specified type
1769 * @priv: Pointer to pass to the callback function.
1770 *
1771 * Performs "Store-network-bridging-information list" operation and calls
1772 * the callback function for every entry in the list. If "change-
1773 * notification-control" is set, further changes in the address list
1774 * will be reported via the IPA command.
1775 */
1776int qdio_pnso_brinfo(struct subchannel_id schid,
1777 int cnc, u16 *response,
1778 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1779 void *entry),
1780 void *priv)
1781{
1782 struct chsc_pnso_area *rr;
1783 int rc;
1784 u32 prev_instance = 0;
1785 int isfirstblock = 1;
1786 int i, size, elems;
1787
1788 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1789 if (rr == NULL)
1790 return -ENOMEM;
1791 do {
1792 /* on the first iteration, naihdr.resume_token will be zero */
1793 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1794 if (rc != 0 && rc != -EBUSY)
1795 goto out;
1796 if (rr->response.code != 1) {
1797 rc = -EIO;
1798 continue;
1799 } else
1800 rc = 0;
1801
1802 if (cb == NULL)
1803 continue;
1804
1805 size = rr->naihdr.naids;
1806 elems = (rr->response.length -
1807 sizeof(struct chsc_header) -
1808 sizeof(struct chsc_brinfo_naihdr)) /
1809 size;
1810
1811 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1812 /* Inform the caller that they need to scrap */
1813 /* the data that was already reported via cb */
1814 rc = -EAGAIN;
1815 break;
1816 }
1817 isfirstblock = 0;
1818 prev_instance = rr->naihdr.instance;
1819 for (i = 0; i < elems; i++)
1820 switch (size) {
1821 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1822 (*cb)(priv, l3_ipv6_addr,
1823 &rr->entries.l3_ipv6[i]);
1824 break;
1825 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1826 (*cb)(priv, l3_ipv4_addr,
1827 &rr->entries.l3_ipv4[i]);
1828 break;
1829 case sizeof(struct qdio_brinfo_entry_l2):
1830 (*cb)(priv, l2_addr_lnid,
1831 &rr->entries.l2[i]);
1832 break;
1833 default:
1834 WARN_ON_ONCE(1);
1835 rc = -EIO;
1836 goto out;
1837 }
1838 } while (rr->response.code == 0x0107 || /* channel busy */
1839 (rr->response.code == 1 && /* list stored */
1840 /* resume token is non-zero => list incomplete */
1841 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1842 (*response) = rr->response.code;
1843
1844out:
1845 free_page((unsigned long)rr);
1846 return rc;
1847}
1848EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1849
Jan Glauber779e6e12008-07-17 17:16:48 +02001850static int __init init_QDIO(void)
1851{
1852 int rc;
1853
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001854 rc = qdio_debug_init();
Jan Glauber779e6e12008-07-17 17:16:48 +02001855 if (rc)
1856 return rc;
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001857 rc = qdio_setup_init();
1858 if (rc)
1859 goto out_debug;
Jan Glauber779e6e12008-07-17 17:16:48 +02001860 rc = tiqdio_allocate_memory();
1861 if (rc)
1862 goto out_cache;
Jan Glauber779e6e12008-07-17 17:16:48 +02001863 rc = tiqdio_register_thinints();
1864 if (rc)
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001865 goto out_ti;
Jan Glauber779e6e12008-07-17 17:16:48 +02001866 return 0;
1867
Jan Glauber779e6e12008-07-17 17:16:48 +02001868out_ti:
1869 tiqdio_free_memory();
1870out_cache:
1871 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001872out_debug:
1873 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001874 return rc;
1875}
1876
1877static void __exit exit_QDIO(void)
1878{
1879 tiqdio_unregister_thinints();
1880 tiqdio_free_memory();
Jan Glauber779e6e12008-07-17 17:16:48 +02001881 qdio_setup_exit();
Sebastian Ottaa5c8df2011-04-04 09:43:31 +02001882 qdio_debug_exit();
Jan Glauber779e6e12008-07-17 17:16:48 +02001883}
1884
1885module_init(init_QDIO);
1886module_exit(exit_QDIO);