xref: /openbmc/linux/drivers/s390/cio/qdio_main.c (revision 400c2a45)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Linux for s390 qdio support, buffer handling, qdio API and module support.
4  *
5  * Copyright IBM Corp. 2000, 2008
6  * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
7  *	      Jan Glauber <jang@linux.vnet.ibm.com>
8  * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/gfp.h>
16 #include <linux/io.h>
17 #include <linux/atomic.h>
18 #include <asm/debug.h>
19 #include <asm/qdio.h>
20 #include <asm/ipl.h>
21 
22 #include "cio.h"
23 #include "css.h"
24 #include "device.h"
25 #include "qdio.h"
26 #include "qdio_debug.h"
27 
28 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 	"Jan Glauber <jang@linux.vnet.ibm.com>");
30 MODULE_DESCRIPTION("QDIO base support");
31 MODULE_LICENSE("GPL");
32 
33 static inline int do_siga_sync(unsigned long schid,
34 			       unsigned int out_mask, unsigned int in_mask,
35 			       unsigned int fc)
36 {
37 	register unsigned long __fc asm ("0") = fc;
38 	register unsigned long __schid asm ("1") = schid;
39 	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 
52 static inline int do_siga_input(unsigned long schid, unsigned int mask,
53 				unsigned int fc)
54 {
55 	register unsigned long __fc asm ("0") = fc;
56 	register unsigned long __schid asm ("1") = schid;
57 	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");
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  * @aob: asynchronous operation block
76  *
77  * Returns condition code.
78  * Note: For IQDC unicast queues only the highest priority queue is processed.
79  */
80 static inline int do_siga_output(unsigned long schid, unsigned long mask,
81 				 unsigned int *bb, unsigned int fc,
82 				 unsigned long aob)
83 {
84 	register unsigned long __fc asm("0") = fc;
85 	register unsigned long __schid asm("1") = schid;
86 	register unsigned long __mask asm("2") = mask;
87 	register unsigned long __aob asm("3") = aob;
88 	int cc;
89 
90 	asm volatile(
91 		"	siga	0\n"
92 		"	ipm	%0\n"
93 		"	srl	%0,28\n"
94 		: "=d" (cc), "+d" (__fc), "+d" (__aob)
95 		: "d" (__schid), "d" (__mask)
96 		: "cc");
97 	*bb = __fc >> 31;
98 	return cc;
99 }
100 
101 /**
102  * qdio_do_eqbs - extract buffer states for QEBSM
103  * @q: queue to manipulate
104  * @state: state of the extracted buffers
105  * @start: buffer number to start at
106  * @count: count of buffers to examine
107  * @auto_ack: automatically acknowledge buffers
108  *
109  * Returns the number of successfully extracted equal buffer states.
110  * Stops processing if a state is different from the last buffers state.
111  */
112 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
113 			int start, int count, int auto_ack)
114 {
115 	int tmp_count = count, tmp_start = start, nr = q->nr;
116 	unsigned int ccq = 0;
117 
118 	qperf_inc(q, eqbs);
119 
120 	if (!q->is_input_q)
121 		nr += q->irq_ptr->nr_input_qs;
122 again:
123 	ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
124 		      auto_ack);
125 
126 	switch (ccq) {
127 	case 0:
128 	case 32:
129 		/* all done, or next buffer state different */
130 		return count - tmp_count;
131 	case 96:
132 		/* not all buffers processed */
133 		qperf_inc(q, eqbs_partial);
134 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "EQBS part:%02x",
135 			tmp_count);
136 		return count - tmp_count;
137 	case 97:
138 		/* no buffer processed */
139 		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
140 		goto again;
141 	default:
142 		DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
143 		DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
144 		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
145 		q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE, q->nr,
146 			   q->first_to_check, count, q->irq_ptr->int_parm);
147 		return 0;
148 	}
149 }
150 
151 /**
152  * qdio_do_sqbs - set buffer states for QEBSM
153  * @q: queue to manipulate
154  * @state: new state of the buffers
155  * @start: first buffer number to change
156  * @count: how many buffers to change
157  *
158  * Returns the number of successfully changed buffers.
159  * Does retrying until the specified count of buffer states is set or an
160  * error occurs.
161  */
162 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
163 			int count)
164 {
165 	unsigned int ccq = 0;
166 	int tmp_count = count, tmp_start = start;
167 	int nr = q->nr;
168 
169 	if (!count)
170 		return 0;
171 	qperf_inc(q, sqbs);
172 
173 	if (!q->is_input_q)
174 		nr += q->irq_ptr->nr_input_qs;
175 again:
176 	ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
177 
178 	switch (ccq) {
179 	case 0:
180 	case 32:
181 		/* all done, or active buffer adapter-owned */
182 		WARN_ON_ONCE(tmp_count);
183 		return count - tmp_count;
184 	case 96:
185 		/* not all buffers processed */
186 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
187 		qperf_inc(q, sqbs_partial);
188 		goto again;
189 	default:
190 		DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
191 		DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
192 		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
193 		q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE, q->nr,
194 			   q->first_to_check, count, q->irq_ptr->int_parm);
195 		return 0;
196 	}
197 }
198 
199 /*
200  * Returns number of examined buffers and their common state in *state.
201  * Requested number of buffers-to-examine must be > 0.
202  */
203 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
204 				 unsigned char *state, unsigned int count,
205 				 int auto_ack, int merge_pending)
206 {
207 	unsigned char __state = 0;
208 	int i = 1;
209 
210 	if (is_qebsm(q))
211 		return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
212 
213 	/* get initial state: */
214 	__state = q->slsb.val[bufnr];
215 
216 	/* Bail out early if there is no work on the queue: */
217 	if (__state & SLSB_OWNER_CU)
218 		goto out;
219 
220 	if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
221 		__state = SLSB_P_OUTPUT_EMPTY;
222 
223 	for (; i < count; i++) {
224 		bufnr = next_buf(bufnr);
225 
226 		/* merge PENDING into EMPTY: */
227 		if (merge_pending &&
228 		    q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
229 		    __state == SLSB_P_OUTPUT_EMPTY)
230 			continue;
231 
232 		/* stop if next state differs from initial state: */
233 		if (q->slsb.val[bufnr] != __state)
234 			break;
235 	}
236 
237 out:
238 	*state = __state;
239 	return i;
240 }
241 
242 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
243 				unsigned char *state, int auto_ack)
244 {
245 	return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
246 }
247 
248 /* wrap-around safe setting of slsb states, returns number of changed buffers */
249 static inline int set_buf_states(struct qdio_q *q, int bufnr,
250 				 unsigned char state, int count)
251 {
252 	int i;
253 
254 	if (is_qebsm(q))
255 		return qdio_do_sqbs(q, state, bufnr, count);
256 
257 	/* Ensure that all preceding changes to the SBALs are visible: */
258 	mb();
259 
260 	for (i = 0; i < count; i++) {
261 		WRITE_ONCE(q->slsb.val[bufnr], state);
262 		bufnr = next_buf(bufnr);
263 	}
264 
265 	/* Make our SLSB changes visible: */
266 	mb();
267 
268 	return count;
269 }
270 
271 static inline int set_buf_state(struct qdio_q *q, int bufnr,
272 				unsigned char state)
273 {
274 	return set_buf_states(q, bufnr, state, 1);
275 }
276 
277 /* set slsb states to initial state */
278 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
279 {
280 	struct qdio_q *q;
281 	int i;
282 
283 	for_each_input_queue(irq_ptr, q, i)
284 		set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
285 			       QDIO_MAX_BUFFERS_PER_Q);
286 	for_each_output_queue(irq_ptr, q, i)
287 		set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
288 			       QDIO_MAX_BUFFERS_PER_Q);
289 }
290 
291 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
292 			  unsigned int input)
293 {
294 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
295 	unsigned int fc = QDIO_SIGA_SYNC;
296 	int cc;
297 
298 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
299 	qperf_inc(q, siga_sync);
300 
301 	if (is_qebsm(q)) {
302 		schid = q->irq_ptr->sch_token;
303 		fc |= QDIO_SIGA_QEBSM_FLAG;
304 	}
305 
306 	cc = do_siga_sync(schid, output, input, fc);
307 	if (unlikely(cc))
308 		DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
309 	return (cc) ? -EIO : 0;
310 }
311 
312 static inline int qdio_siga_sync_q(struct qdio_q *q)
313 {
314 	if (q->is_input_q)
315 		return qdio_siga_sync(q, 0, q->mask);
316 	else
317 		return qdio_siga_sync(q, q->mask, 0);
318 }
319 
320 static int qdio_siga_output(struct qdio_q *q, unsigned int count,
321 			    unsigned int *busy_bit, unsigned long aob)
322 {
323 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
324 	unsigned int fc = QDIO_SIGA_WRITE;
325 	u64 start_time = 0;
326 	int retries = 0, cc;
327 
328 	if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
329 		if (count > 1)
330 			fc = QDIO_SIGA_WRITEM;
331 		else if (aob)
332 			fc = QDIO_SIGA_WRITEQ;
333 	}
334 
335 	if (is_qebsm(q)) {
336 		schid = q->irq_ptr->sch_token;
337 		fc |= QDIO_SIGA_QEBSM_FLAG;
338 	}
339 again:
340 	cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
341 
342 	/* hipersocket busy condition */
343 	if (unlikely(*busy_bit)) {
344 		retries++;
345 
346 		if (!start_time) {
347 			start_time = get_tod_clock_fast();
348 			goto again;
349 		}
350 		if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
351 			goto again;
352 	}
353 	if (retries) {
354 		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
355 			      "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
356 		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
357 	}
358 	return cc;
359 }
360 
361 static inline int qdio_siga_input(struct qdio_q *q)
362 {
363 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
364 	unsigned int fc = QDIO_SIGA_READ;
365 	int cc;
366 
367 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
368 	qperf_inc(q, siga_read);
369 
370 	if (is_qebsm(q)) {
371 		schid = q->irq_ptr->sch_token;
372 		fc |= QDIO_SIGA_QEBSM_FLAG;
373 	}
374 
375 	cc = do_siga_input(schid, q->mask, fc);
376 	if (unlikely(cc))
377 		DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
378 	return (cc) ? -EIO : 0;
379 }
380 
381 #define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
382 #define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
383 
384 static inline void qdio_sync_queues(struct qdio_q *q)
385 {
386 	/* PCI capable outbound queues will also be scanned so sync them too */
387 	if (pci_out_supported(q->irq_ptr))
388 		qdio_siga_sync_all(q);
389 	else
390 		qdio_siga_sync_q(q);
391 }
392 
393 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
394 			unsigned char *state)
395 {
396 	if (need_siga_sync(q))
397 		qdio_siga_sync_q(q);
398 	return get_buf_state(q, bufnr, state, 0);
399 }
400 
401 static inline void qdio_stop_polling(struct qdio_q *q)
402 {
403 	if (!q->u.in.batch_count)
404 		return;
405 
406 	qperf_inc(q, stop_polling);
407 
408 	/* show the card that we are not polling anymore */
409 	set_buf_states(q, q->u.in.batch_start, SLSB_P_INPUT_NOT_INIT,
410 		       q->u.in.batch_count);
411 	q->u.in.batch_count = 0;
412 }
413 
414 static inline void account_sbals(struct qdio_q *q, unsigned int count)
415 {
416 	q->q_stats.nr_sbal_total += count;
417 	q->q_stats.nr_sbals[ilog2(count)]++;
418 }
419 
420 static void process_buffer_error(struct qdio_q *q, unsigned int start,
421 				 int count)
422 {
423 	q->qdio_error = QDIO_ERROR_SLSB_STATE;
424 
425 	/* special handling for no target buffer empty */
426 	if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
427 	    q->sbal[start]->element[15].sflags == 0x10) {
428 		qperf_inc(q, target_full);
429 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
430 		return;
431 	}
432 
433 	DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
434 	DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
435 	DBF_ERROR("FTC:%3d C:%3d", start, count);
436 	DBF_ERROR("F14:%2x F15:%2x",
437 		  q->sbal[start]->element[14].sflags,
438 		  q->sbal[start]->element[15].sflags);
439 }
440 
441 static inline void inbound_handle_work(struct qdio_q *q, unsigned int start,
442 				       int count, bool auto_ack)
443 {
444 	/* ACK the newest SBAL: */
445 	if (!auto_ack)
446 		set_buf_state(q, add_buf(start, count - 1), SLSB_P_INPUT_ACK);
447 
448 	if (!q->u.in.batch_count)
449 		q->u.in.batch_start = start;
450 	q->u.in.batch_count += count;
451 }
452 
453 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
454 {
455 	unsigned char state = 0;
456 	int count;
457 
458 	q->timestamp = get_tod_clock_fast();
459 
460 	count = atomic_read(&q->nr_buf_used);
461 	if (!count)
462 		return 0;
463 
464 	/*
465 	 * No siga sync here, as a PCI or we after a thin interrupt
466 	 * already sync'ed the queues.
467 	 */
468 	count = get_buf_states(q, start, &state, count, 1, 0);
469 	if (!count)
470 		return 0;
471 
472 	switch (state) {
473 	case SLSB_P_INPUT_PRIMED:
474 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr,
475 			      count);
476 
477 		inbound_handle_work(q, start, count, is_qebsm(q));
478 		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
479 			qperf_inc(q, inbound_queue_full);
480 		if (q->irq_ptr->perf_stat_enabled)
481 			account_sbals(q, count);
482 		return count;
483 	case SLSB_P_INPUT_ERROR:
484 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in err:%1d %02x", q->nr,
485 			      count);
486 
487 		process_buffer_error(q, start, count);
488 		inbound_handle_work(q, start, count, false);
489 		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
490 			qperf_inc(q, inbound_queue_full);
491 		if (q->irq_ptr->perf_stat_enabled)
492 			account_sbals_error(q, count);
493 		return count;
494 	case SLSB_CU_INPUT_EMPTY:
495 		if (q->irq_ptr->perf_stat_enabled)
496 			q->q_stats.nr_sbal_nop++;
497 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
498 			      q->nr, start);
499 		return 0;
500 	case SLSB_P_INPUT_NOT_INIT:
501 	case SLSB_P_INPUT_ACK:
502 		/* We should never see this state, throw a WARN: */
503 	default:
504 		dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
505 			      "found state %#x at index %u on queue %u\n",
506 			      state, start, q->nr);
507 		return 0;
508 	}
509 }
510 
511 static int qdio_inbound_q_moved(struct qdio_q *q, unsigned int start)
512 {
513 	return get_inbound_buffer_frontier(q, start);
514 }
515 
516 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
517 {
518 	unsigned char state = 0;
519 
520 	if (!atomic_read(&q->nr_buf_used))
521 		return 1;
522 
523 	if (need_siga_sync(q))
524 		qdio_siga_sync_q(q);
525 	get_buf_state(q, start, &state, 0);
526 
527 	if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
528 		/* more work coming */
529 		return 0;
530 
531 	return 1;
532 }
533 
534 static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
535 					int bufnr)
536 {
537 	unsigned long phys_aob = 0;
538 
539 	if (!q->aobs[bufnr]) {
540 		struct qaob *aob = qdio_allocate_aob();
541 		q->aobs[bufnr] = aob;
542 	}
543 	if (q->aobs[bufnr]) {
544 		q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
545 		phys_aob = virt_to_phys(q->aobs[bufnr]);
546 		WARN_ON_ONCE(phys_aob & 0xFF);
547 	}
548 
549 	q->sbal_state[bufnr].flags = 0;
550 	return phys_aob;
551 }
552 
553 static void qdio_kick_handler(struct qdio_q *q, unsigned int start,
554 			      unsigned int count)
555 {
556 	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
557 		return;
558 
559 	if (q->is_input_q) {
560 		qperf_inc(q, inbound_handler);
561 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
562 	} else {
563 		qperf_inc(q, outbound_handler);
564 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
565 			      start, count);
566 	}
567 
568 	q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
569 		   q->irq_ptr->int_parm);
570 
571 	/* for the next time */
572 	q->qdio_error = 0;
573 }
574 
575 static inline int qdio_tasklet_schedule(struct qdio_q *q)
576 {
577 	if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
578 		tasklet_schedule(&q->tasklet);
579 		return 0;
580 	}
581 	return -EPERM;
582 }
583 
584 static void __qdio_inbound_processing(struct qdio_q *q)
585 {
586 	unsigned int start = q->first_to_check;
587 	int count;
588 
589 	qperf_inc(q, tasklet_inbound);
590 
591 	count = qdio_inbound_q_moved(q, start);
592 	if (count == 0)
593 		return;
594 
595 	qdio_kick_handler(q, start, count);
596 	start = add_buf(start, count);
597 	q->first_to_check = start;
598 
599 	if (!qdio_inbound_q_done(q, start)) {
600 		/* means poll time is not yet over */
601 		qperf_inc(q, tasklet_inbound_resched);
602 		if (!qdio_tasklet_schedule(q))
603 			return;
604 	}
605 
606 	qdio_stop_polling(q);
607 	/*
608 	 * We need to check again to not lose initiative after
609 	 * resetting the ACK state.
610 	 */
611 	if (!qdio_inbound_q_done(q, start)) {
612 		qperf_inc(q, tasklet_inbound_resched2);
613 		qdio_tasklet_schedule(q);
614 	}
615 }
616 
617 void qdio_inbound_processing(unsigned long data)
618 {
619 	struct qdio_q *q = (struct qdio_q *)data;
620 	__qdio_inbound_processing(q);
621 }
622 
623 static void qdio_check_pending(struct qdio_q *q, unsigned int index)
624 {
625 	unsigned char state;
626 
627 	if (get_buf_state(q, index, &state, 0) > 0 &&
628 	    state == SLSB_P_OUTPUT_PENDING &&
629 	    q->u.out.aobs[index]) {
630 		q->u.out.sbal_state[index].flags |=
631 			QDIO_OUTBUF_STATE_FLAG_PENDING;
632 		q->u.out.aobs[index] = NULL;
633 	}
634 }
635 
636 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
637 {
638 	unsigned char state = 0;
639 	int count;
640 
641 	q->timestamp = get_tod_clock_fast();
642 
643 	if (need_siga_sync(q))
644 		if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
645 		    !pci_out_supported(q->irq_ptr)) ||
646 		    (queue_type(q) == QDIO_IQDIO_QFMT &&
647 		    multicast_outbound(q)))
648 			qdio_siga_sync_q(q);
649 
650 	count = atomic_read(&q->nr_buf_used);
651 	if (!count)
652 		return 0;
653 
654 	count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
655 	if (!count)
656 		return 0;
657 
658 	switch (state) {
659 	case SLSB_P_OUTPUT_EMPTY:
660 	case SLSB_P_OUTPUT_PENDING:
661 		/* the adapter got it */
662 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
663 			"out empty:%1d %02x", q->nr, count);
664 
665 		atomic_sub(count, &q->nr_buf_used);
666 		if (q->irq_ptr->perf_stat_enabled)
667 			account_sbals(q, count);
668 		return count;
669 	case SLSB_P_OUTPUT_ERROR:
670 		process_buffer_error(q, start, count);
671 		atomic_sub(count, &q->nr_buf_used);
672 		if (q->irq_ptr->perf_stat_enabled)
673 			account_sbals_error(q, count);
674 		return count;
675 	case SLSB_CU_OUTPUT_PRIMED:
676 		/* the adapter has not fetched the output yet */
677 		if (q->irq_ptr->perf_stat_enabled)
678 			q->q_stats.nr_sbal_nop++;
679 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
680 			      q->nr);
681 		return 0;
682 	case SLSB_P_OUTPUT_HALTED:
683 		return 0;
684 	case SLSB_P_OUTPUT_NOT_INIT:
685 		/* We should never see this state, throw a WARN: */
686 	default:
687 		dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
688 			      "found state %#x at index %u on queue %u\n",
689 			      state, start, q->nr);
690 		return 0;
691 	}
692 }
693 
694 /* all buffers processed? */
695 static inline int qdio_outbound_q_done(struct qdio_q *q)
696 {
697 	return atomic_read(&q->nr_buf_used) == 0;
698 }
699 
700 static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
701 {
702 	int count;
703 
704 	count = get_outbound_buffer_frontier(q, start);
705 
706 	if (count) {
707 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
708 
709 		if (q->u.out.use_cq) {
710 			unsigned int i;
711 
712 			for (i = 0; i < count; i++)
713 				qdio_check_pending(q, QDIO_BUFNR(start + i));
714 		}
715 	}
716 
717 	return count;
718 }
719 
720 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
721 				unsigned long aob)
722 {
723 	int retries = 0, cc;
724 	unsigned int busy_bit;
725 
726 	if (!need_siga_out(q))
727 		return 0;
728 
729 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
730 retry:
731 	qperf_inc(q, siga_write);
732 
733 	cc = qdio_siga_output(q, count, &busy_bit, aob);
734 	switch (cc) {
735 	case 0:
736 		break;
737 	case 2:
738 		if (busy_bit) {
739 			while (++retries < QDIO_BUSY_BIT_RETRIES) {
740 				mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
741 				goto retry;
742 			}
743 			DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
744 			cc = -EBUSY;
745 		} else {
746 			DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
747 			cc = -ENOBUFS;
748 		}
749 		break;
750 	case 1:
751 	case 3:
752 		DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
753 		cc = -EIO;
754 		break;
755 	}
756 	if (retries) {
757 		DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
758 		DBF_ERROR("count:%u", retries);
759 	}
760 	return cc;
761 }
762 
763 static void __qdio_outbound_processing(struct qdio_q *q)
764 {
765 	unsigned int start = q->first_to_check;
766 	int count;
767 
768 	qperf_inc(q, tasklet_outbound);
769 	WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
770 
771 	count = qdio_outbound_q_moved(q, start);
772 	if (count) {
773 		q->first_to_check = add_buf(start, count);
774 		qdio_kick_handler(q, start, count);
775 	}
776 
777 	if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
778 	    !qdio_outbound_q_done(q))
779 		goto sched;
780 
781 	if (q->u.out.pci_out_enabled)
782 		return;
783 
784 	/*
785 	 * Now we know that queue type is either qeth without pci enabled
786 	 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
787 	 * is noticed and outbound_handler is called after some time.
788 	 */
789 	if (qdio_outbound_q_done(q))
790 		del_timer_sync(&q->u.out.timer);
791 	else
792 		if (!timer_pending(&q->u.out.timer) &&
793 		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
794 			mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
795 	return;
796 
797 sched:
798 	qdio_tasklet_schedule(q);
799 }
800 
801 /* outbound tasklet */
802 void qdio_outbound_processing(unsigned long data)
803 {
804 	struct qdio_q *q = (struct qdio_q *)data;
805 	__qdio_outbound_processing(q);
806 }
807 
808 void qdio_outbound_timer(struct timer_list *t)
809 {
810 	struct qdio_q *q = from_timer(q, t, u.out.timer);
811 
812 	qdio_tasklet_schedule(q);
813 }
814 
815 static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
816 {
817 	struct qdio_q *out;
818 	int i;
819 
820 	if (!pci_out_supported(irq) || !irq->scan_threshold)
821 		return;
822 
823 	for_each_output_queue(irq, out, i)
824 		if (!qdio_outbound_q_done(out))
825 			qdio_tasklet_schedule(out);
826 }
827 
828 void tiqdio_inbound_processing(unsigned long data)
829 {
830 	struct qdio_q *q = (struct qdio_q *)data;
831 
832 	if (need_siga_sync(q) && need_siga_sync_after_ai(q))
833 		qdio_sync_queues(q);
834 
835 	/* The interrupt could be caused by a PCI request: */
836 	qdio_check_outbound_pci_queues(q->irq_ptr);
837 
838 	__qdio_inbound_processing(q);
839 }
840 
841 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
842 				  enum qdio_irq_states state)
843 {
844 	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
845 
846 	irq_ptr->state = state;
847 	mb();
848 }
849 
850 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
851 {
852 	if (irb->esw.esw0.erw.cons) {
853 		DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
854 		DBF_ERROR_HEX(irb, 64);
855 		DBF_ERROR_HEX(irb->ecw, 64);
856 	}
857 }
858 
859 /* PCI interrupt handler */
860 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
861 {
862 	int i;
863 	struct qdio_q *q;
864 
865 	if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
866 		return;
867 
868 	if (irq_ptr->irq_poll) {
869 		if (!test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
870 			irq_ptr->irq_poll(irq_ptr->cdev, irq_ptr->int_parm);
871 		else
872 			QDIO_PERF_STAT_INC(irq_ptr, int_discarded);
873 	} else {
874 		for_each_input_queue(irq_ptr, q, i)
875 			tasklet_schedule(&q->tasklet);
876 	}
877 
878 	if (!pci_out_supported(irq_ptr) || !irq_ptr->scan_threshold)
879 		return;
880 
881 	for_each_output_queue(irq_ptr, q, i) {
882 		if (qdio_outbound_q_done(q))
883 			continue;
884 		if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
885 			qdio_siga_sync_q(q);
886 		qdio_tasklet_schedule(q);
887 	}
888 }
889 
890 static void qdio_handle_activate_check(struct qdio_irq *irq_ptr,
891 				       unsigned long intparm, int cstat,
892 				       int dstat)
893 {
894 	struct qdio_q *q;
895 
896 	DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
897 	DBF_ERROR("intp :%lx", intparm);
898 	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
899 
900 	if (irq_ptr->nr_input_qs) {
901 		q = irq_ptr->input_qs[0];
902 	} else if (irq_ptr->nr_output_qs) {
903 		q = irq_ptr->output_qs[0];
904 	} else {
905 		dump_stack();
906 		goto no_handler;
907 	}
908 
909 	q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
910 		   q->nr, q->first_to_check, 0, irq_ptr->int_parm);
911 no_handler:
912 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
913 	/*
914 	 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
915 	 * Therefore we call the LGR detection function here.
916 	 */
917 	lgr_info_log();
918 }
919 
920 static void qdio_establish_handle_irq(struct qdio_irq *irq_ptr, int cstat,
921 				      int dstat)
922 {
923 	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
924 
925 	if (cstat)
926 		goto error;
927 	if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
928 		goto error;
929 	if (!(dstat & DEV_STAT_DEV_END))
930 		goto error;
931 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
932 	return;
933 
934 error:
935 	DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
936 	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
937 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
938 }
939 
940 /* qdio interrupt handler */
941 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
942 		      struct irb *irb)
943 {
944 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
945 	struct subchannel_id schid;
946 	int cstat, dstat;
947 
948 	if (!intparm || !irq_ptr) {
949 		ccw_device_get_schid(cdev, &schid);
950 		DBF_ERROR("qint:%4x", schid.sch_no);
951 		return;
952 	}
953 
954 	if (irq_ptr->perf_stat_enabled)
955 		irq_ptr->perf_stat.qdio_int++;
956 
957 	if (IS_ERR(irb)) {
958 		DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
959 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
960 		wake_up(&cdev->private->wait_q);
961 		return;
962 	}
963 	qdio_irq_check_sense(irq_ptr, irb);
964 	cstat = irb->scsw.cmd.cstat;
965 	dstat = irb->scsw.cmd.dstat;
966 
967 	switch (irq_ptr->state) {
968 	case QDIO_IRQ_STATE_INACTIVE:
969 		qdio_establish_handle_irq(irq_ptr, cstat, dstat);
970 		break;
971 	case QDIO_IRQ_STATE_CLEANUP:
972 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
973 		break;
974 	case QDIO_IRQ_STATE_ESTABLISHED:
975 	case QDIO_IRQ_STATE_ACTIVE:
976 		if (cstat & SCHN_STAT_PCI) {
977 			qdio_int_handler_pci(irq_ptr);
978 			return;
979 		}
980 		if (cstat || dstat)
981 			qdio_handle_activate_check(irq_ptr, intparm, cstat,
982 						   dstat);
983 		break;
984 	case QDIO_IRQ_STATE_STOPPED:
985 		break;
986 	default:
987 		WARN_ON_ONCE(1);
988 	}
989 	wake_up(&cdev->private->wait_q);
990 }
991 
992 /**
993  * qdio_get_ssqd_desc - get qdio subchannel description
994  * @cdev: ccw device to get description for
995  * @data: where to store the ssqd
996  *
997  * Returns 0 or an error code. The results of the chsc are stored in the
998  * specified structure.
999  */
1000 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1001 		       struct qdio_ssqd_desc *data)
1002 {
1003 	struct subchannel_id schid;
1004 
1005 	if (!cdev || !cdev->private)
1006 		return -EINVAL;
1007 
1008 	ccw_device_get_schid(cdev, &schid);
1009 	DBF_EVENT("get ssqd:%4x", schid.sch_no);
1010 	return qdio_setup_get_ssqd(NULL, &schid, data);
1011 }
1012 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1013 
1014 static void qdio_shutdown_queues(struct qdio_irq *irq_ptr)
1015 {
1016 	struct qdio_q *q;
1017 	int i;
1018 
1019 	for_each_input_queue(irq_ptr, q, i)
1020 		tasklet_kill(&q->tasklet);
1021 
1022 	for_each_output_queue(irq_ptr, q, i) {
1023 		del_timer_sync(&q->u.out.timer);
1024 		tasklet_kill(&q->tasklet);
1025 	}
1026 }
1027 
1028 /**
1029  * qdio_shutdown - shut down a qdio subchannel
1030  * @cdev: associated ccw device
1031  * @how: use halt or clear to shutdown
1032  */
1033 int qdio_shutdown(struct ccw_device *cdev, int how)
1034 {
1035 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1036 	struct subchannel_id schid;
1037 	int rc;
1038 
1039 	if (!irq_ptr)
1040 		return -ENODEV;
1041 
1042 	WARN_ON_ONCE(irqs_disabled());
1043 	ccw_device_get_schid(cdev, &schid);
1044 	DBF_EVENT("qshutdown:%4x", schid.sch_no);
1045 
1046 	mutex_lock(&irq_ptr->setup_mutex);
1047 	/*
1048 	 * Subchannel was already shot down. We cannot prevent being called
1049 	 * twice since cio may trigger a shutdown asynchronously.
1050 	 */
1051 	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1052 		mutex_unlock(&irq_ptr->setup_mutex);
1053 		return 0;
1054 	}
1055 
1056 	/*
1057 	 * Indicate that the device is going down. Scheduling the queue
1058 	 * tasklets is forbidden from here on.
1059 	 */
1060 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1061 
1062 	tiqdio_remove_device(irq_ptr);
1063 	qdio_shutdown_queues(irq_ptr);
1064 	qdio_shutdown_debug_entries(irq_ptr);
1065 
1066 	/* cleanup subchannel */
1067 	spin_lock_irq(get_ccwdev_lock(cdev));
1068 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1069 	if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1070 		rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1071 	else
1072 		/* default behaviour is halt */
1073 		rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1074 	spin_unlock_irq(get_ccwdev_lock(cdev));
1075 	if (rc) {
1076 		DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1077 		DBF_ERROR("rc:%4d", rc);
1078 		goto no_cleanup;
1079 	}
1080 
1081 	wait_event_interruptible_timeout(cdev->private->wait_q,
1082 		irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1083 		irq_ptr->state == QDIO_IRQ_STATE_ERR,
1084 		10 * HZ);
1085 
1086 no_cleanup:
1087 	qdio_shutdown_thinint(irq_ptr);
1088 	qdio_shutdown_irq(irq_ptr);
1089 
1090 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1091 	mutex_unlock(&irq_ptr->setup_mutex);
1092 	if (rc)
1093 		return rc;
1094 	return 0;
1095 }
1096 EXPORT_SYMBOL_GPL(qdio_shutdown);
1097 
1098 /**
1099  * qdio_free - free data structures for a qdio subchannel
1100  * @cdev: associated ccw device
1101  */
1102 int qdio_free(struct ccw_device *cdev)
1103 {
1104 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1105 	struct subchannel_id schid;
1106 
1107 	if (!irq_ptr)
1108 		return -ENODEV;
1109 
1110 	ccw_device_get_schid(cdev, &schid);
1111 	DBF_EVENT("qfree:%4x", schid.sch_no);
1112 	DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
1113 	mutex_lock(&irq_ptr->setup_mutex);
1114 
1115 	irq_ptr->debug_area = NULL;
1116 	cdev->private->qdio_data = NULL;
1117 	mutex_unlock(&irq_ptr->setup_mutex);
1118 
1119 	qdio_free_async_data(irq_ptr);
1120 	qdio_free_queues(irq_ptr);
1121 	free_page((unsigned long) irq_ptr->qdr);
1122 	free_page(irq_ptr->chsc_page);
1123 	free_page((unsigned long) irq_ptr);
1124 	return 0;
1125 }
1126 EXPORT_SYMBOL_GPL(qdio_free);
1127 
1128 /**
1129  * qdio_allocate - allocate qdio queues and associated data
1130  * @cdev: associated ccw device
1131  * @no_input_qs: allocate this number of Input Queues
1132  * @no_output_qs: allocate this number of Output Queues
1133  */
1134 int qdio_allocate(struct ccw_device *cdev, unsigned int no_input_qs,
1135 		  unsigned int no_output_qs)
1136 {
1137 	struct subchannel_id schid;
1138 	struct qdio_irq *irq_ptr;
1139 	int rc = -ENOMEM;
1140 
1141 	ccw_device_get_schid(cdev, &schid);
1142 	DBF_EVENT("qallocate:%4x", schid.sch_no);
1143 
1144 	if (no_input_qs > QDIO_MAX_QUEUES_PER_IRQ ||
1145 	    no_output_qs > QDIO_MAX_QUEUES_PER_IRQ)
1146 		return -EINVAL;
1147 
1148 	/* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1149 	irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1150 	if (!irq_ptr)
1151 		return -ENOMEM;
1152 
1153 	irq_ptr->cdev = cdev;
1154 	mutex_init(&irq_ptr->setup_mutex);
1155 	if (qdio_allocate_dbf(irq_ptr))
1156 		goto err_dbf;
1157 
1158 	DBF_DEV_EVENT(DBF_ERR, irq_ptr, "alloc niq:%1u noq:%1u", no_input_qs,
1159 		      no_output_qs);
1160 
1161 	/*
1162 	 * Allocate a page for the chsc calls in qdio_establish.
1163 	 * Must be pre-allocated since a zfcp recovery will call
1164 	 * qdio_establish. In case of low memory and swap on a zfcp disk
1165 	 * we may not be able to allocate memory otherwise.
1166 	 */
1167 	irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1168 	if (!irq_ptr->chsc_page)
1169 		goto err_chsc;
1170 
1171 	/* qdr is used in ccw1.cda which is u32 */
1172 	irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1173 	if (!irq_ptr->qdr)
1174 		goto err_qdr;
1175 
1176 	rc = qdio_allocate_qs(irq_ptr, no_input_qs, no_output_qs);
1177 	if (rc)
1178 		goto err_queues;
1179 
1180 	INIT_LIST_HEAD(&irq_ptr->entry);
1181 	cdev->private->qdio_data = irq_ptr;
1182 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1183 	return 0;
1184 
1185 err_queues:
1186 	free_page((unsigned long) irq_ptr->qdr);
1187 err_qdr:
1188 	free_page(irq_ptr->chsc_page);
1189 err_chsc:
1190 err_dbf:
1191 	free_page((unsigned long) irq_ptr);
1192 	return rc;
1193 }
1194 EXPORT_SYMBOL_GPL(qdio_allocate);
1195 
1196 static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1197 {
1198 	struct qdio_q *q = irq_ptr->input_qs[0];
1199 	int i, use_cq = 0;
1200 
1201 	if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1202 		use_cq = 1;
1203 
1204 	for_each_output_queue(irq_ptr, q, i) {
1205 		if (use_cq) {
1206 			if (multicast_outbound(q))
1207 				continue;
1208 			if (qdio_enable_async_operation(&q->u.out) < 0) {
1209 				use_cq = 0;
1210 				continue;
1211 			}
1212 		} else
1213 			qdio_disable_async_operation(&q->u.out);
1214 	}
1215 	DBF_EVENT("use_cq:%d", use_cq);
1216 }
1217 
1218 static void qdio_trace_init_data(struct qdio_irq *irq,
1219 				 struct qdio_initialize *data)
1220 {
1221 	DBF_DEV_EVENT(DBF_ERR, irq, "qfmt:%1u", data->q_format);
1222 	DBF_DEV_EVENT(DBF_ERR, irq, "qpff%4x", data->qib_param_field_format);
1223 	DBF_DEV_HEX(irq, &data->qib_param_field, sizeof(void *), DBF_ERR);
1224 	DBF_DEV_HEX(irq, &data->input_slib_elements, sizeof(void *), DBF_ERR);
1225 	DBF_DEV_HEX(irq, &data->output_slib_elements, sizeof(void *), DBF_ERR);
1226 	DBF_DEV_EVENT(DBF_ERR, irq, "niq:%1u noq:%1u", data->no_input_qs,
1227 		      data->no_output_qs);
1228 	DBF_DEV_HEX(irq, &data->input_handler, sizeof(void *), DBF_ERR);
1229 	DBF_DEV_HEX(irq, &data->output_handler, sizeof(void *), DBF_ERR);
1230 	DBF_DEV_HEX(irq, &data->int_parm, sizeof(long), DBF_ERR);
1231 	DBF_DEV_HEX(irq, &data->input_sbal_addr_array, sizeof(void *), DBF_ERR);
1232 	DBF_DEV_HEX(irq, &data->output_sbal_addr_array, sizeof(void *),
1233 		    DBF_ERR);
1234 }
1235 
1236 /**
1237  * qdio_establish - establish queues on a qdio subchannel
1238  * @cdev: associated ccw device
1239  * @init_data: initialization data
1240  */
1241 int qdio_establish(struct ccw_device *cdev,
1242 		   struct qdio_initialize *init_data)
1243 {
1244 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1245 	struct subchannel_id schid;
1246 	int rc;
1247 
1248 	ccw_device_get_schid(cdev, &schid);
1249 	DBF_EVENT("qestablish:%4x", schid.sch_no);
1250 
1251 	if (!irq_ptr)
1252 		return -ENODEV;
1253 
1254 	if (init_data->no_input_qs > irq_ptr->max_input_qs ||
1255 	    init_data->no_output_qs > irq_ptr->max_output_qs)
1256 		return -EINVAL;
1257 
1258 	if ((init_data->no_input_qs && !init_data->input_handler) ||
1259 	    (init_data->no_output_qs && !init_data->output_handler))
1260 		return -EINVAL;
1261 
1262 	if (!init_data->input_sbal_addr_array ||
1263 	    !init_data->output_sbal_addr_array)
1264 		return -EINVAL;
1265 
1266 	mutex_lock(&irq_ptr->setup_mutex);
1267 	qdio_trace_init_data(irq_ptr, init_data);
1268 	qdio_setup_irq(irq_ptr, init_data);
1269 
1270 	rc = qdio_establish_thinint(irq_ptr);
1271 	if (rc) {
1272 		qdio_shutdown_irq(irq_ptr);
1273 		mutex_unlock(&irq_ptr->setup_mutex);
1274 		return rc;
1275 	}
1276 
1277 	/* establish q */
1278 	irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1279 	irq_ptr->ccw.flags = CCW_FLAG_SLI;
1280 	irq_ptr->ccw.count = irq_ptr->equeue.count;
1281 	irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1282 
1283 	spin_lock_irq(get_ccwdev_lock(cdev));
1284 	ccw_device_set_options_mask(cdev, 0);
1285 
1286 	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1287 	spin_unlock_irq(get_ccwdev_lock(cdev));
1288 	if (rc) {
1289 		DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1290 		DBF_ERROR("rc:%4x", rc);
1291 		qdio_shutdown_thinint(irq_ptr);
1292 		qdio_shutdown_irq(irq_ptr);
1293 		mutex_unlock(&irq_ptr->setup_mutex);
1294 		return rc;
1295 	}
1296 
1297 	wait_event_interruptible_timeout(cdev->private->wait_q,
1298 		irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1299 		irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1300 
1301 	if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1302 		mutex_unlock(&irq_ptr->setup_mutex);
1303 		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1304 		return -EIO;
1305 	}
1306 
1307 	qdio_setup_ssqd_info(irq_ptr);
1308 
1309 	qdio_detect_hsicq(irq_ptr);
1310 
1311 	/* qebsm is now setup if available, initialize buffer states */
1312 	qdio_init_buf_states(irq_ptr);
1313 
1314 	mutex_unlock(&irq_ptr->setup_mutex);
1315 	qdio_print_subchannel_info(irq_ptr);
1316 	qdio_setup_debug_entries(irq_ptr);
1317 	return 0;
1318 }
1319 EXPORT_SYMBOL_GPL(qdio_establish);
1320 
1321 /**
1322  * qdio_activate - activate queues on a qdio subchannel
1323  * @cdev: associated cdev
1324  */
1325 int qdio_activate(struct ccw_device *cdev)
1326 {
1327 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1328 	struct subchannel_id schid;
1329 	int rc;
1330 
1331 	ccw_device_get_schid(cdev, &schid);
1332 	DBF_EVENT("qactivate:%4x", schid.sch_no);
1333 
1334 	if (!irq_ptr)
1335 		return -ENODEV;
1336 
1337 	mutex_lock(&irq_ptr->setup_mutex);
1338 	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1339 		rc = -EBUSY;
1340 		goto out;
1341 	}
1342 
1343 	irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1344 	irq_ptr->ccw.flags = CCW_FLAG_SLI;
1345 	irq_ptr->ccw.count = irq_ptr->aqueue.count;
1346 	irq_ptr->ccw.cda = 0;
1347 
1348 	spin_lock_irq(get_ccwdev_lock(cdev));
1349 	ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1350 
1351 	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1352 			      0, DOIO_DENY_PREFETCH);
1353 	spin_unlock_irq(get_ccwdev_lock(cdev));
1354 	if (rc) {
1355 		DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1356 		DBF_ERROR("rc:%4x", rc);
1357 		goto out;
1358 	}
1359 
1360 	if (is_thinint_irq(irq_ptr))
1361 		tiqdio_add_device(irq_ptr);
1362 
1363 	/* wait for subchannel to become active */
1364 	msleep(5);
1365 
1366 	switch (irq_ptr->state) {
1367 	case QDIO_IRQ_STATE_STOPPED:
1368 	case QDIO_IRQ_STATE_ERR:
1369 		rc = -EIO;
1370 		break;
1371 	default:
1372 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1373 		rc = 0;
1374 	}
1375 out:
1376 	mutex_unlock(&irq_ptr->setup_mutex);
1377 	return rc;
1378 }
1379 EXPORT_SYMBOL_GPL(qdio_activate);
1380 
1381 /**
1382  * handle_inbound - reset processed input buffers
1383  * @q: queue containing the buffers
1384  * @callflags: flags
1385  * @bufnr: first buffer to process
1386  * @count: how many buffers are emptied
1387  */
1388 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1389 			  int bufnr, int count)
1390 {
1391 	int overlap;
1392 
1393 	qperf_inc(q, inbound_call);
1394 
1395 	/* If any processed SBALs are returned to HW, adjust our tracking: */
1396 	overlap = min_t(int, count - sub_buf(q->u.in.batch_start, bufnr),
1397 			     q->u.in.batch_count);
1398 	if (overlap > 0) {
1399 		q->u.in.batch_start = add_buf(q->u.in.batch_start, overlap);
1400 		q->u.in.batch_count -= overlap;
1401 	}
1402 
1403 	count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1404 	atomic_add(count, &q->nr_buf_used);
1405 
1406 	if (need_siga_in(q))
1407 		return qdio_siga_input(q);
1408 
1409 	return 0;
1410 }
1411 
1412 /**
1413  * handle_outbound - process filled outbound buffers
1414  * @q: queue containing the buffers
1415  * @callflags: flags
1416  * @bufnr: first buffer to process
1417  * @count: how many buffers are filled
1418  */
1419 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1420 			   unsigned int bufnr, unsigned int count)
1421 {
1422 	const unsigned int scan_threshold = q->irq_ptr->scan_threshold;
1423 	unsigned char state = 0;
1424 	int used, rc = 0;
1425 
1426 	qperf_inc(q, outbound_call);
1427 
1428 	count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1429 	used = atomic_add_return(count, &q->nr_buf_used);
1430 
1431 	if (used == QDIO_MAX_BUFFERS_PER_Q)
1432 		qperf_inc(q, outbound_queue_full);
1433 
1434 	if (callflags & QDIO_FLAG_PCI_OUT) {
1435 		q->u.out.pci_out_enabled = 1;
1436 		qperf_inc(q, pci_request_int);
1437 	} else
1438 		q->u.out.pci_out_enabled = 0;
1439 
1440 	if (queue_type(q) == QDIO_IQDIO_QFMT) {
1441 		unsigned long phys_aob = 0;
1442 
1443 		if (q->u.out.use_cq && count == 1)
1444 			phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1445 
1446 		rc = qdio_kick_outbound_q(q, count, phys_aob);
1447 	} else if (need_siga_sync(q)) {
1448 		rc = qdio_siga_sync_q(q);
1449 	} else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1450 		   get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1451 		   state == SLSB_CU_OUTPUT_PRIMED) {
1452 		/* The previous buffer is not processed yet, tack on. */
1453 		qperf_inc(q, fast_requeue);
1454 	} else {
1455 		rc = qdio_kick_outbound_q(q, count, 0);
1456 	}
1457 
1458 	/* Let drivers implement their own completion scanning: */
1459 	if (!scan_threshold)
1460 		return rc;
1461 
1462 	/* in case of SIGA errors we must process the error immediately */
1463 	if (used >= scan_threshold || rc)
1464 		qdio_tasklet_schedule(q);
1465 	else
1466 		/* free the SBALs in case of no further traffic */
1467 		if (!timer_pending(&q->u.out.timer) &&
1468 		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
1469 			mod_timer(&q->u.out.timer, jiffies + HZ);
1470 	return rc;
1471 }
1472 
1473 /**
1474  * do_QDIO - process input or output buffers
1475  * @cdev: associated ccw_device for the qdio subchannel
1476  * @callflags: input or output and special flags from the program
1477  * @q_nr: queue number
1478  * @bufnr: buffer number
1479  * @count: how many buffers to process
1480  */
1481 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1482 	    int q_nr, unsigned int bufnr, unsigned int count)
1483 {
1484 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1485 
1486 	if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1487 		return -EINVAL;
1488 
1489 	if (!irq_ptr)
1490 		return -ENODEV;
1491 
1492 	DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1493 		      "do%02x b:%02x c:%02x", callflags, bufnr, count);
1494 
1495 	if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1496 		return -EIO;
1497 	if (!count)
1498 		return 0;
1499 	if (callflags & QDIO_FLAG_SYNC_INPUT)
1500 		return handle_inbound(irq_ptr->input_qs[q_nr],
1501 				      callflags, bufnr, count);
1502 	else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1503 		return handle_outbound(irq_ptr->output_qs[q_nr],
1504 				       callflags, bufnr, count);
1505 	return -EINVAL;
1506 }
1507 EXPORT_SYMBOL_GPL(do_QDIO);
1508 
1509 /**
1510  * qdio_start_irq - enable interrupt processing for the device
1511  * @cdev: associated ccw_device for the qdio subchannel
1512  *
1513  * Return codes
1514  *   0 - success
1515  *   1 - irqs not started since new data is available
1516  */
1517 int qdio_start_irq(struct ccw_device *cdev)
1518 {
1519 	struct qdio_q *q;
1520 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1521 	unsigned int i;
1522 
1523 	if (!irq_ptr)
1524 		return -ENODEV;
1525 
1526 	for_each_input_queue(irq_ptr, q, i)
1527 		qdio_stop_polling(q);
1528 
1529 	clear_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
1530 
1531 	/*
1532 	 * We need to check again to not lose initiative after
1533 	 * resetting the ACK state.
1534 	 */
1535 	if (test_nonshared_ind(irq_ptr))
1536 		goto rescan;
1537 
1538 	for_each_input_queue(irq_ptr, q, i) {
1539 		if (!qdio_inbound_q_done(q, q->first_to_check))
1540 			goto rescan;
1541 	}
1542 
1543 	return 0;
1544 
1545 rescan:
1546 	if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1547 		return 0;
1548 	else
1549 		return 1;
1550 
1551 }
1552 EXPORT_SYMBOL(qdio_start_irq);
1553 
1554 static int __qdio_inspect_queue(struct qdio_q *q, unsigned int *bufnr,
1555 				unsigned int *error)
1556 {
1557 	unsigned int start = q->first_to_check;
1558 	int count;
1559 
1560 	count = q->is_input_q ? qdio_inbound_q_moved(q, start) :
1561 				qdio_outbound_q_moved(q, start);
1562 	if (count == 0)
1563 		return 0;
1564 
1565 	*bufnr = start;
1566 	*error = q->qdio_error;
1567 
1568 	/* for the next time */
1569 	q->first_to_check = add_buf(start, count);
1570 	q->qdio_error = 0;
1571 
1572 	return count;
1573 }
1574 
1575 int qdio_inspect_queue(struct ccw_device *cdev, unsigned int nr, bool is_input,
1576 		       unsigned int *bufnr, unsigned int *error)
1577 {
1578 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1579 	struct qdio_q *q;
1580 
1581 	if (!irq_ptr)
1582 		return -ENODEV;
1583 	q = is_input ? irq_ptr->input_qs[nr] : irq_ptr->output_qs[nr];
1584 
1585 	if (need_siga_sync(q))
1586 		qdio_siga_sync_q(q);
1587 
1588 	return __qdio_inspect_queue(q, bufnr, error);
1589 }
1590 EXPORT_SYMBOL_GPL(qdio_inspect_queue);
1591 
1592 /**
1593  * qdio_get_next_buffers - process input buffers
1594  * @cdev: associated ccw_device for the qdio subchannel
1595  * @nr: input queue number
1596  * @bufnr: first filled buffer number
1597  * @error: buffers are in error state
1598  *
1599  * Return codes
1600  *   < 0 - error
1601  *   = 0 - no new buffers found
1602  *   > 0 - number of processed buffers
1603  */
1604 int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1605 			  int *error)
1606 {
1607 	struct qdio_q *q;
1608 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1609 
1610 	if (!irq_ptr)
1611 		return -ENODEV;
1612 	q = irq_ptr->input_qs[nr];
1613 
1614 	/*
1615 	 * Cannot rely on automatic sync after interrupt since queues may
1616 	 * also be examined without interrupt.
1617 	 */
1618 	if (need_siga_sync(q))
1619 		qdio_sync_queues(q);
1620 
1621 	qdio_check_outbound_pci_queues(irq_ptr);
1622 
1623 	/* Note: upper-layer MUST stop processing immediately here ... */
1624 	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1625 		return -EIO;
1626 
1627 	return __qdio_inspect_queue(q, bufnr, error);
1628 }
1629 EXPORT_SYMBOL(qdio_get_next_buffers);
1630 
1631 /**
1632  * qdio_stop_irq - disable interrupt processing for the device
1633  * @cdev: associated ccw_device for the qdio subchannel
1634  *
1635  * Return codes
1636  *   0 - interrupts were already disabled
1637  *   1 - interrupts successfully disabled
1638  */
1639 int qdio_stop_irq(struct ccw_device *cdev)
1640 {
1641 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1642 
1643 	if (!irq_ptr)
1644 		return -ENODEV;
1645 
1646 	if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1647 		return 0;
1648 	else
1649 		return 1;
1650 }
1651 EXPORT_SYMBOL(qdio_stop_irq);
1652 
1653 static int __init init_QDIO(void)
1654 {
1655 	int rc;
1656 
1657 	rc = qdio_debug_init();
1658 	if (rc)
1659 		return rc;
1660 	rc = qdio_setup_init();
1661 	if (rc)
1662 		goto out_debug;
1663 	rc = qdio_thinint_init();
1664 	if (rc)
1665 		goto out_cache;
1666 	return 0;
1667 
1668 out_cache:
1669 	qdio_setup_exit();
1670 out_debug:
1671 	qdio_debug_exit();
1672 	return rc;
1673 }
1674 
1675 static void __exit exit_QDIO(void)
1676 {
1677 	qdio_thinint_exit();
1678 	qdio_setup_exit();
1679 	qdio_debug_exit();
1680 }
1681 
1682 module_init(init_QDIO);
1683 module_exit(exit_QDIO);
1684