xref: /openbmc/linux/drivers/s390/cio/qdio_main.c (revision a1b2f04ea527397fcacacd09e0d690927feef429)
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_WARN, 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_kick, 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_kick, 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 	for (i = 0; i < count; i++) {
258 		xchg(&q->slsb.val[bufnr], state);
259 		bufnr = next_buf(bufnr);
260 	}
261 	return count;
262 }
263 
264 static 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 */
271 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
272 {
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 
284 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
285 			  unsigned int input)
286 {
287 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
288 	unsigned int fc = QDIO_SIGA_SYNC;
289 	int cc;
290 
291 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
292 	qperf_inc(q, siga_sync);
293 
294 	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);
300 	if (unlikely(cc))
301 		DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
302 	return (cc) ? -EIO : 0;
303 }
304 
305 static inline int qdio_siga_sync_q(struct qdio_q *q)
306 {
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 
313 static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
314 	unsigned long aob)
315 {
316 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
317 	unsigned int fc = QDIO_SIGA_WRITE;
318 	u64 start_time = 0;
319 	int retries = 0, cc;
320 	unsigned long laob = 0;
321 
322 	WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
323 			     !q->u.out.use_cq));
324 	if (q->u.out.use_cq && aob != 0) {
325 		fc = QDIO_SIGA_WRITEQ;
326 		laob = aob;
327 	}
328 
329 	if (is_qebsm(q)) {
330 		schid = q->irq_ptr->sch_token;
331 		fc |= QDIO_SIGA_QEBSM_FLAG;
332 	}
333 again:
334 	cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
335 
336 	/* hipersocket busy condition */
337 	if (unlikely(*busy_bit)) {
338 		retries++;
339 
340 		if (!start_time) {
341 			start_time = get_tod_clock_fast();
342 			goto again;
343 		}
344 		if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
345 			goto again;
346 	}
347 	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 	}
352 	return cc;
353 }
354 
355 static inline int qdio_siga_input(struct qdio_q *q)
356 {
357 	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
358 	unsigned int fc = QDIO_SIGA_READ;
359 	int cc;
360 
361 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
362 	qperf_inc(q, siga_read);
363 
364 	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);
370 	if (unlikely(cc))
371 		DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
372 	return (cc) ? -EIO : 0;
373 }
374 
375 #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 
378 static inline void qdio_sync_queues(struct qdio_q *q)
379 {
380 	/* PCI capable outbound queues will also be scanned so sync them too */
381 	if (pci_out_supported(q->irq_ptr))
382 		qdio_siga_sync_all(q);
383 	else
384 		qdio_siga_sync_q(q);
385 }
386 
387 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
388 			unsigned char *state)
389 {
390 	if (need_siga_sync(q))
391 		qdio_siga_sync_q(q);
392 	return get_buf_state(q, bufnr, state, 0);
393 }
394 
395 static inline void qdio_stop_polling(struct qdio_q *q)
396 {
397 	if (!q->u.in.polling)
398 		return;
399 
400 	q->u.in.polling = 0;
401 	qperf_inc(q, stop_polling);
402 
403 	/* show the card that we are not polling anymore */
404 	if (is_qebsm(q)) {
405 		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
406 			       q->u.in.ack_count);
407 		q->u.in.ack_count = 0;
408 	} else
409 		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
410 }
411 
412 static inline void account_sbals(struct qdio_q *q, unsigned int count)
413 {
414 	int pos;
415 
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 	}
421 	pos = ilog2(count);
422 	q->q_stats.nr_sbals[pos]++;
423 }
424 
425 static void process_buffer_error(struct qdio_q *q, unsigned int start,
426 				 int count)
427 {
428 	unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
429 					SLSB_P_OUTPUT_NOT_INIT;
430 
431 	q->qdio_error = QDIO_ERROR_SLSB_STATE;
432 
433 	/* special handling for no target buffer empty */
434 	if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
435 	    q->sbal[start]->element[15].sflags == 0x10) {
436 		qperf_inc(q, target_full);
437 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
438 		goto set;
439 	}
440 
441 	DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
442 	DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
443 	DBF_ERROR("FTC:%3d C:%3d", start, count);
444 	DBF_ERROR("F14:%2x F15:%2x",
445 		  q->sbal[start]->element[14].sflags,
446 		  q->sbal[start]->element[15].sflags);
447 
448 set:
449 	/*
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, start, state, count);
454 }
455 
456 static inline void inbound_primed(struct qdio_q *q, unsigned int start,
457 				  int count)
458 {
459 	int new;
460 
461 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
462 
463 	/* for QEBSM the ACK was already set by EQBS */
464 	if (is_qebsm(q)) {
465 		if (!q->u.in.polling) {
466 			q->u.in.polling = 1;
467 			q->u.in.ack_count = count;
468 			q->u.in.ack_start = start;
469 			return;
470 		}
471 
472 		/* delete the previous ACK's */
473 		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
474 			       q->u.in.ack_count);
475 		q->u.in.ack_count = count;
476 		q->u.in.ack_start = start;
477 		return;
478 	}
479 
480 	/*
481 	 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
482 	 * or by the next inbound run.
483 	 */
484 	new = add_buf(start, count - 1);
485 	if (q->u.in.polling) {
486 		/* reset the previous ACK but first set the new one */
487 		set_buf_state(q, new, SLSB_P_INPUT_ACK);
488 		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
489 	} else {
490 		q->u.in.polling = 1;
491 		set_buf_state(q, new, SLSB_P_INPUT_ACK);
492 	}
493 
494 	q->u.in.ack_start = new;
495 	count--;
496 	if (!count)
497 		return;
498 	/* need to change ALL buffers to get more interrupts */
499 	set_buf_states(q, start, SLSB_P_INPUT_NOT_INIT, count);
500 }
501 
502 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
503 {
504 	unsigned char state = 0;
505 	int count;
506 
507 	q->timestamp = get_tod_clock_fast();
508 
509 	/*
510 	 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
511 	 * would return 0.
512 	 */
513 	count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
514 	if (!count)
515 		return 0;
516 
517 	/*
518 	 * No siga sync here, as a PCI or we after a thin interrupt
519 	 * already sync'ed the queues.
520 	 */
521 	count = get_buf_states(q, start, &state, count, 1, 0);
522 	if (!count)
523 		return 0;
524 
525 	switch (state) {
526 	case SLSB_P_INPUT_PRIMED:
527 		inbound_primed(q, start, count);
528 		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
529 			qperf_inc(q, inbound_queue_full);
530 		if (q->irq_ptr->perf_stat_enabled)
531 			account_sbals(q, count);
532 		return count;
533 	case SLSB_P_INPUT_ERROR:
534 		process_buffer_error(q, start, count);
535 		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
536 			qperf_inc(q, inbound_queue_full);
537 		if (q->irq_ptr->perf_stat_enabled)
538 			account_sbals_error(q, count);
539 		return count;
540 	case SLSB_CU_INPUT_EMPTY:
541 	case SLSB_P_INPUT_NOT_INIT:
542 	case SLSB_P_INPUT_ACK:
543 		if (q->irq_ptr->perf_stat_enabled)
544 			q->q_stats.nr_sbal_nop++;
545 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
546 			      q->nr, start);
547 		return 0;
548 	default:
549 		WARN_ON_ONCE(1);
550 		return 0;
551 	}
552 }
553 
554 static int qdio_inbound_q_moved(struct qdio_q *q, unsigned int start)
555 {
556 	int count;
557 
558 	count = get_inbound_buffer_frontier(q, start);
559 
560 	if (count && !is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
561 		q->u.in.timestamp = get_tod_clock();
562 
563 	return count;
564 }
565 
566 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
567 {
568 	unsigned char state = 0;
569 
570 	if (!atomic_read(&q->nr_buf_used))
571 		return 1;
572 
573 	if (need_siga_sync(q))
574 		qdio_siga_sync_q(q);
575 	get_buf_state(q, start, &state, 0);
576 
577 	if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
578 		/* more work coming */
579 		return 0;
580 
581 	if (is_thinint_irq(q->irq_ptr))
582 		return 1;
583 
584 	/* don't poll under z/VM */
585 	if (MACHINE_IS_VM)
586 		return 1;
587 
588 	/*
589 	 * At this point we know, that inbound first_to_check
590 	 * has (probably) not moved (see qdio_inbound_processing).
591 	 */
592 	if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
593 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x", start);
594 		return 1;
595 	} else
596 		return 0;
597 }
598 
599 static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
600 {
601 	unsigned char state = 0;
602 	int j, b = start;
603 
604 	for (j = 0; j < count; ++j) {
605 		get_buf_state(q, b, &state, 0);
606 		if (state == SLSB_P_OUTPUT_PENDING) {
607 			struct qaob *aob = q->u.out.aobs[b];
608 			if (aob == NULL)
609 				continue;
610 
611 			q->u.out.sbal_state[b].flags |=
612 				QDIO_OUTBUF_STATE_FLAG_PENDING;
613 			q->u.out.aobs[b] = NULL;
614 		}
615 		b = next_buf(b);
616 	}
617 }
618 
619 static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
620 					int bufnr)
621 {
622 	unsigned long phys_aob = 0;
623 
624 	if (!q->use_cq)
625 		return 0;
626 
627 	if (!q->aobs[bufnr]) {
628 		struct qaob *aob = qdio_allocate_aob();
629 		q->aobs[bufnr] = aob;
630 	}
631 	if (q->aobs[bufnr]) {
632 		q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
633 		phys_aob = virt_to_phys(q->aobs[bufnr]);
634 		WARN_ON_ONCE(phys_aob & 0xFF);
635 	}
636 
637 	q->sbal_state[bufnr].flags = 0;
638 	return phys_aob;
639 }
640 
641 static void qdio_kick_handler(struct qdio_q *q, unsigned int count)
642 {
643 	int start = q->first_to_kick;
644 
645 	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
646 		return;
647 
648 	if (q->is_input_q) {
649 		qperf_inc(q, inbound_handler);
650 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
651 	} else {
652 		qperf_inc(q, outbound_handler);
653 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
654 			      start, count);
655 		if (q->u.out.use_cq)
656 			qdio_handle_aobs(q, start, count);
657 	}
658 
659 	q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
660 		   q->irq_ptr->int_parm);
661 
662 	/* for the next time */
663 	q->first_to_kick = add_buf(start, count);
664 	q->qdio_error = 0;
665 }
666 
667 static inline int qdio_tasklet_schedule(struct qdio_q *q)
668 {
669 	if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
670 		tasklet_schedule(&q->tasklet);
671 		return 0;
672 	}
673 	return -EPERM;
674 }
675 
676 static void __qdio_inbound_processing(struct qdio_q *q)
677 {
678 	unsigned int start = q->first_to_check;
679 	int count;
680 
681 	qperf_inc(q, tasklet_inbound);
682 
683 	count = qdio_inbound_q_moved(q, start);
684 	if (count == 0)
685 		return;
686 
687 	start = add_buf(start, count);
688 	q->first_to_check = start;
689 	qdio_kick_handler(q, count);
690 
691 	if (!qdio_inbound_q_done(q, start)) {
692 		/* means poll time is not yet over */
693 		qperf_inc(q, tasklet_inbound_resched);
694 		if (!qdio_tasklet_schedule(q))
695 			return;
696 	}
697 
698 	qdio_stop_polling(q);
699 	/*
700 	 * We need to check again to not lose initiative after
701 	 * resetting the ACK state.
702 	 */
703 	if (!qdio_inbound_q_done(q, start)) {
704 		qperf_inc(q, tasklet_inbound_resched2);
705 		qdio_tasklet_schedule(q);
706 	}
707 }
708 
709 void qdio_inbound_processing(unsigned long data)
710 {
711 	struct qdio_q *q = (struct qdio_q *)data;
712 	__qdio_inbound_processing(q);
713 }
714 
715 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
716 {
717 	unsigned char state = 0;
718 	int count;
719 
720 	q->timestamp = get_tod_clock_fast();
721 
722 	if (need_siga_sync(q))
723 		if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
724 		    !pci_out_supported(q->irq_ptr)) ||
725 		    (queue_type(q) == QDIO_IQDIO_QFMT &&
726 		    multicast_outbound(q)))
727 			qdio_siga_sync_q(q);
728 
729 	count = atomic_read(&q->nr_buf_used);
730 	if (!count)
731 		return 0;
732 
733 	count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
734 	if (!count)
735 		return 0;
736 
737 	switch (state) {
738 	case SLSB_P_OUTPUT_EMPTY:
739 	case SLSB_P_OUTPUT_PENDING:
740 		/* the adapter got it */
741 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
742 			"out empty:%1d %02x", q->nr, count);
743 
744 		atomic_sub(count, &q->nr_buf_used);
745 		if (q->irq_ptr->perf_stat_enabled)
746 			account_sbals(q, count);
747 		return count;
748 	case SLSB_P_OUTPUT_ERROR:
749 		process_buffer_error(q, start, count);
750 		atomic_sub(count, &q->nr_buf_used);
751 		if (q->irq_ptr->perf_stat_enabled)
752 			account_sbals_error(q, count);
753 		return count;
754 	case SLSB_CU_OUTPUT_PRIMED:
755 		/* the adapter has not fetched the output yet */
756 		if (q->irq_ptr->perf_stat_enabled)
757 			q->q_stats.nr_sbal_nop++;
758 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
759 			      q->nr);
760 		return 0;
761 	case SLSB_P_OUTPUT_NOT_INIT:
762 	case SLSB_P_OUTPUT_HALTED:
763 		return 0;
764 	default:
765 		WARN_ON_ONCE(1);
766 		return 0;
767 	}
768 }
769 
770 /* all buffers processed? */
771 static inline int qdio_outbound_q_done(struct qdio_q *q)
772 {
773 	return atomic_read(&q->nr_buf_used) == 0;
774 }
775 
776 static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
777 {
778 	int count;
779 
780 	count = get_outbound_buffer_frontier(q, start);
781 
782 	if (count)
783 		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
784 
785 	return count;
786 }
787 
788 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
789 {
790 	int retries = 0, cc;
791 	unsigned int busy_bit;
792 
793 	if (!need_siga_out(q))
794 		return 0;
795 
796 	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
797 retry:
798 	qperf_inc(q, siga_write);
799 
800 	cc = qdio_siga_output(q, &busy_bit, aob);
801 	switch (cc) {
802 	case 0:
803 		break;
804 	case 2:
805 		if (busy_bit) {
806 			while (++retries < QDIO_BUSY_BIT_RETRIES) {
807 				mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
808 				goto retry;
809 			}
810 			DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
811 			cc = -EBUSY;
812 		} else {
813 			DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
814 			cc = -ENOBUFS;
815 		}
816 		break;
817 	case 1:
818 	case 3:
819 		DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
820 		cc = -EIO;
821 		break;
822 	}
823 	if (retries) {
824 		DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
825 		DBF_ERROR("count:%u", retries);
826 	}
827 	return cc;
828 }
829 
830 static void __qdio_outbound_processing(struct qdio_q *q)
831 {
832 	unsigned int start = q->first_to_check;
833 	int count;
834 
835 	qperf_inc(q, tasklet_outbound);
836 	WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
837 
838 	count = qdio_outbound_q_moved(q, start);
839 	if (count) {
840 		q->first_to_check = add_buf(start, count);
841 		qdio_kick_handler(q, count);
842 	}
843 
844 	if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
845 	    !qdio_outbound_q_done(q))
846 		goto sched;
847 
848 	if (q->u.out.pci_out_enabled)
849 		return;
850 
851 	/*
852 	 * Now we know that queue type is either qeth without pci enabled
853 	 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
854 	 * is noticed and outbound_handler is called after some time.
855 	 */
856 	if (qdio_outbound_q_done(q))
857 		del_timer_sync(&q->u.out.timer);
858 	else
859 		if (!timer_pending(&q->u.out.timer) &&
860 		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
861 			mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
862 	return;
863 
864 sched:
865 	qdio_tasklet_schedule(q);
866 }
867 
868 /* outbound tasklet */
869 void qdio_outbound_processing(unsigned long data)
870 {
871 	struct qdio_q *q = (struct qdio_q *)data;
872 	__qdio_outbound_processing(q);
873 }
874 
875 void qdio_outbound_timer(struct timer_list *t)
876 {
877 	struct qdio_q *q = from_timer(q, t, u.out.timer);
878 
879 	qdio_tasklet_schedule(q);
880 }
881 
882 static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
883 {
884 	struct qdio_q *out;
885 	int i;
886 
887 	if (!pci_out_supported(irq))
888 		return;
889 
890 	for_each_output_queue(irq, out, i)
891 		if (!qdio_outbound_q_done(out))
892 			qdio_tasklet_schedule(out);
893 }
894 
895 static void __tiqdio_inbound_processing(struct qdio_q *q)
896 {
897 	unsigned int start = q->first_to_check;
898 	int count;
899 
900 	qperf_inc(q, tasklet_inbound);
901 	if (need_siga_sync(q) && need_siga_sync_after_ai(q))
902 		qdio_sync_queues(q);
903 
904 	/* The interrupt could be caused by a PCI request: */
905 	qdio_check_outbound_pci_queues(q->irq_ptr);
906 
907 	count = qdio_inbound_q_moved(q, start);
908 	if (count == 0)
909 		return;
910 
911 	start = add_buf(start, count);
912 	q->first_to_check = start;
913 	qdio_kick_handler(q, count);
914 
915 	if (!qdio_inbound_q_done(q, start)) {
916 		qperf_inc(q, tasklet_inbound_resched);
917 		if (!qdio_tasklet_schedule(q))
918 			return;
919 	}
920 
921 	qdio_stop_polling(q);
922 	/*
923 	 * We need to check again to not lose initiative after
924 	 * resetting the ACK state.
925 	 */
926 	if (!qdio_inbound_q_done(q, start)) {
927 		qperf_inc(q, tasklet_inbound_resched2);
928 		qdio_tasklet_schedule(q);
929 	}
930 }
931 
932 void tiqdio_inbound_processing(unsigned long data)
933 {
934 	struct qdio_q *q = (struct qdio_q *)data;
935 	__tiqdio_inbound_processing(q);
936 }
937 
938 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
939 				  enum qdio_irq_states state)
940 {
941 	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
942 
943 	irq_ptr->state = state;
944 	mb();
945 }
946 
947 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
948 {
949 	if (irb->esw.esw0.erw.cons) {
950 		DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
951 		DBF_ERROR_HEX(irb, 64);
952 		DBF_ERROR_HEX(irb->ecw, 64);
953 	}
954 }
955 
956 /* PCI interrupt handler */
957 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
958 {
959 	int i;
960 	struct qdio_q *q;
961 
962 	if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
963 		return;
964 
965 	for_each_input_queue(irq_ptr, q, i) {
966 		if (q->u.in.queue_start_poll) {
967 			/* skip if polling is enabled or already in work */
968 			if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
969 				     &q->u.in.queue_irq_state)) {
970 				qperf_inc(q, int_discarded);
971 				continue;
972 			}
973 			q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
974 						 q->irq_ptr->int_parm);
975 		} else {
976 			tasklet_schedule(&q->tasklet);
977 		}
978 	}
979 
980 	if (!pci_out_supported(irq_ptr))
981 		return;
982 
983 	for_each_output_queue(irq_ptr, q, i) {
984 		if (qdio_outbound_q_done(q))
985 			continue;
986 		if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
987 			qdio_siga_sync_q(q);
988 		qdio_tasklet_schedule(q);
989 	}
990 }
991 
992 static void qdio_handle_activate_check(struct ccw_device *cdev,
993 				unsigned long intparm, int cstat, int dstat)
994 {
995 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
996 	struct qdio_q *q;
997 	int count;
998 
999 	DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1000 	DBF_ERROR("intp :%lx", intparm);
1001 	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1002 
1003 	if (irq_ptr->nr_input_qs) {
1004 		q = irq_ptr->input_qs[0];
1005 	} else if (irq_ptr->nr_output_qs) {
1006 		q = irq_ptr->output_qs[0];
1007 	} else {
1008 		dump_stack();
1009 		goto no_handler;
1010 	}
1011 
1012 	count = sub_buf(q->first_to_check, q->first_to_kick);
1013 	q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
1014 		   q->nr, q->first_to_kick, count, irq_ptr->int_parm);
1015 no_handler:
1016 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1017 	/*
1018 	 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1019 	 * Therefore we call the LGR detection function here.
1020 	 */
1021 	lgr_info_log();
1022 }
1023 
1024 static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1025 				      int dstat)
1026 {
1027 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1028 
1029 	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
1030 
1031 	if (cstat)
1032 		goto error;
1033 	if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1034 		goto error;
1035 	if (!(dstat & DEV_STAT_DEV_END))
1036 		goto error;
1037 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1038 	return;
1039 
1040 error:
1041 	DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1042 	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1043 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1044 }
1045 
1046 /* qdio interrupt handler */
1047 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1048 		      struct irb *irb)
1049 {
1050 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1051 	struct subchannel_id schid;
1052 	int cstat, dstat;
1053 
1054 	if (!intparm || !irq_ptr) {
1055 		ccw_device_get_schid(cdev, &schid);
1056 		DBF_ERROR("qint:%4x", schid.sch_no);
1057 		return;
1058 	}
1059 
1060 	if (irq_ptr->perf_stat_enabled)
1061 		irq_ptr->perf_stat.qdio_int++;
1062 
1063 	if (IS_ERR(irb)) {
1064 		DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1065 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1066 		wake_up(&cdev->private->wait_q);
1067 		return;
1068 	}
1069 	qdio_irq_check_sense(irq_ptr, irb);
1070 	cstat = irb->scsw.cmd.cstat;
1071 	dstat = irb->scsw.cmd.dstat;
1072 
1073 	switch (irq_ptr->state) {
1074 	case QDIO_IRQ_STATE_INACTIVE:
1075 		qdio_establish_handle_irq(cdev, cstat, dstat);
1076 		break;
1077 	case QDIO_IRQ_STATE_CLEANUP:
1078 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1079 		break;
1080 	case QDIO_IRQ_STATE_ESTABLISHED:
1081 	case QDIO_IRQ_STATE_ACTIVE:
1082 		if (cstat & SCHN_STAT_PCI) {
1083 			qdio_int_handler_pci(irq_ptr);
1084 			return;
1085 		}
1086 		if (cstat || dstat)
1087 			qdio_handle_activate_check(cdev, intparm, cstat,
1088 						   dstat);
1089 		break;
1090 	case QDIO_IRQ_STATE_STOPPED:
1091 		break;
1092 	default:
1093 		WARN_ON_ONCE(1);
1094 	}
1095 	wake_up(&cdev->private->wait_q);
1096 }
1097 
1098 /**
1099  * qdio_get_ssqd_desc - get qdio subchannel description
1100  * @cdev: ccw device to get description for
1101  * @data: where to store the ssqd
1102  *
1103  * Returns 0 or an error code. The results of the chsc are stored in the
1104  * specified structure.
1105  */
1106 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1107 		       struct qdio_ssqd_desc *data)
1108 {
1109 	struct subchannel_id schid;
1110 
1111 	if (!cdev || !cdev->private)
1112 		return -EINVAL;
1113 
1114 	ccw_device_get_schid(cdev, &schid);
1115 	DBF_EVENT("get ssqd:%4x", schid.sch_no);
1116 	return qdio_setup_get_ssqd(NULL, &schid, data);
1117 }
1118 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1119 
1120 static void qdio_shutdown_queues(struct ccw_device *cdev)
1121 {
1122 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1123 	struct qdio_q *q;
1124 	int i;
1125 
1126 	for_each_input_queue(irq_ptr, q, i)
1127 		tasklet_kill(&q->tasklet);
1128 
1129 	for_each_output_queue(irq_ptr, q, i) {
1130 		del_timer_sync(&q->u.out.timer);
1131 		tasklet_kill(&q->tasklet);
1132 	}
1133 }
1134 
1135 /**
1136  * qdio_shutdown - shut down a qdio subchannel
1137  * @cdev: associated ccw device
1138  * @how: use halt or clear to shutdown
1139  */
1140 int qdio_shutdown(struct ccw_device *cdev, int how)
1141 {
1142 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1143 	struct subchannel_id schid;
1144 	int rc;
1145 
1146 	if (!irq_ptr)
1147 		return -ENODEV;
1148 
1149 	WARN_ON_ONCE(irqs_disabled());
1150 	ccw_device_get_schid(cdev, &schid);
1151 	DBF_EVENT("qshutdown:%4x", schid.sch_no);
1152 
1153 	mutex_lock(&irq_ptr->setup_mutex);
1154 	/*
1155 	 * Subchannel was already shot down. We cannot prevent being called
1156 	 * twice since cio may trigger a shutdown asynchronously.
1157 	 */
1158 	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1159 		mutex_unlock(&irq_ptr->setup_mutex);
1160 		return 0;
1161 	}
1162 
1163 	/*
1164 	 * Indicate that the device is going down. Scheduling the queue
1165 	 * tasklets is forbidden from here on.
1166 	 */
1167 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1168 
1169 	tiqdio_remove_input_queues(irq_ptr);
1170 	qdio_shutdown_queues(cdev);
1171 	qdio_shutdown_debug_entries(irq_ptr);
1172 
1173 	/* cleanup subchannel */
1174 	spin_lock_irq(get_ccwdev_lock(cdev));
1175 
1176 	if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1177 		rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1178 	else
1179 		/* default behaviour is halt */
1180 		rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1181 	if (rc) {
1182 		DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1183 		DBF_ERROR("rc:%4d", rc);
1184 		goto no_cleanup;
1185 	}
1186 
1187 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1188 	spin_unlock_irq(get_ccwdev_lock(cdev));
1189 	wait_event_interruptible_timeout(cdev->private->wait_q,
1190 		irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1191 		irq_ptr->state == QDIO_IRQ_STATE_ERR,
1192 		10 * HZ);
1193 	spin_lock_irq(get_ccwdev_lock(cdev));
1194 
1195 no_cleanup:
1196 	qdio_shutdown_thinint(irq_ptr);
1197 
1198 	/* restore interrupt handler */
1199 	if ((void *)cdev->handler == (void *)qdio_int_handler) {
1200 		cdev->handler = irq_ptr->orig_handler;
1201 		cdev->private->intparm = 0;
1202 	}
1203 	spin_unlock_irq(get_ccwdev_lock(cdev));
1204 
1205 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1206 	mutex_unlock(&irq_ptr->setup_mutex);
1207 	if (rc)
1208 		return rc;
1209 	return 0;
1210 }
1211 EXPORT_SYMBOL_GPL(qdio_shutdown);
1212 
1213 /**
1214  * qdio_free - free data structures for a qdio subchannel
1215  * @cdev: associated ccw device
1216  */
1217 int qdio_free(struct ccw_device *cdev)
1218 {
1219 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1220 	struct subchannel_id schid;
1221 
1222 	if (!irq_ptr)
1223 		return -ENODEV;
1224 
1225 	ccw_device_get_schid(cdev, &schid);
1226 	DBF_EVENT("qfree:%4x", schid.sch_no);
1227 	DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
1228 	mutex_lock(&irq_ptr->setup_mutex);
1229 
1230 	irq_ptr->debug_area = NULL;
1231 	cdev->private->qdio_data = NULL;
1232 	mutex_unlock(&irq_ptr->setup_mutex);
1233 
1234 	qdio_release_memory(irq_ptr);
1235 	return 0;
1236 }
1237 EXPORT_SYMBOL_GPL(qdio_free);
1238 
1239 /**
1240  * qdio_allocate - allocate qdio queues and associated data
1241  * @init_data: initialization data
1242  */
1243 int qdio_allocate(struct qdio_initialize *init_data)
1244 {
1245 	struct subchannel_id schid;
1246 	struct qdio_irq *irq_ptr;
1247 
1248 	ccw_device_get_schid(init_data->cdev, &schid);
1249 	DBF_EVENT("qallocate:%4x", schid.sch_no);
1250 
1251 	if ((init_data->no_input_qs && !init_data->input_handler) ||
1252 	    (init_data->no_output_qs && !init_data->output_handler))
1253 		return -EINVAL;
1254 
1255 	if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1256 	    (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1257 		return -EINVAL;
1258 
1259 	if ((!init_data->input_sbal_addr_array) ||
1260 	    (!init_data->output_sbal_addr_array))
1261 		return -EINVAL;
1262 
1263 	/* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1264 	irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1265 	if (!irq_ptr)
1266 		goto out_err;
1267 
1268 	mutex_init(&irq_ptr->setup_mutex);
1269 	if (qdio_allocate_dbf(init_data, irq_ptr))
1270 		goto out_rel;
1271 
1272 	/*
1273 	 * Allocate a page for the chsc calls in qdio_establish.
1274 	 * Must be pre-allocated since a zfcp recovery will call
1275 	 * qdio_establish. In case of low memory and swap on a zfcp disk
1276 	 * we may not be able to allocate memory otherwise.
1277 	 */
1278 	irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1279 	if (!irq_ptr->chsc_page)
1280 		goto out_rel;
1281 
1282 	/* qdr is used in ccw1.cda which is u32 */
1283 	irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1284 	if (!irq_ptr->qdr)
1285 		goto out_rel;
1286 
1287 	if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1288 			     init_data->no_output_qs))
1289 		goto out_rel;
1290 
1291 	init_data->cdev->private->qdio_data = irq_ptr;
1292 	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1293 	return 0;
1294 out_rel:
1295 	qdio_release_memory(irq_ptr);
1296 out_err:
1297 	return -ENOMEM;
1298 }
1299 EXPORT_SYMBOL_GPL(qdio_allocate);
1300 
1301 static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1302 {
1303 	struct qdio_q *q = irq_ptr->input_qs[0];
1304 	int i, use_cq = 0;
1305 
1306 	if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1307 		use_cq = 1;
1308 
1309 	for_each_output_queue(irq_ptr, q, i) {
1310 		if (use_cq) {
1311 			if (qdio_enable_async_operation(&q->u.out) < 0) {
1312 				use_cq = 0;
1313 				continue;
1314 			}
1315 		} else
1316 			qdio_disable_async_operation(&q->u.out);
1317 	}
1318 	DBF_EVENT("use_cq:%d", use_cq);
1319 }
1320 
1321 /**
1322  * qdio_establish - establish queues on a qdio subchannel
1323  * @init_data: initialization data
1324  */
1325 int qdio_establish(struct qdio_initialize *init_data)
1326 {
1327 	struct ccw_device *cdev = init_data->cdev;
1328 	struct subchannel_id schid;
1329 	struct qdio_irq *irq_ptr;
1330 	int rc;
1331 
1332 	ccw_device_get_schid(cdev, &schid);
1333 	DBF_EVENT("qestablish:%4x", schid.sch_no);
1334 
1335 	irq_ptr = cdev->private->qdio_data;
1336 	if (!irq_ptr)
1337 		return -ENODEV;
1338 
1339 	mutex_lock(&irq_ptr->setup_mutex);
1340 	qdio_setup_irq(init_data);
1341 
1342 	rc = qdio_establish_thinint(irq_ptr);
1343 	if (rc) {
1344 		mutex_unlock(&irq_ptr->setup_mutex);
1345 		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1346 		return rc;
1347 	}
1348 
1349 	/* establish q */
1350 	irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1351 	irq_ptr->ccw.flags = CCW_FLAG_SLI;
1352 	irq_ptr->ccw.count = irq_ptr->equeue.count;
1353 	irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1354 
1355 	spin_lock_irq(get_ccwdev_lock(cdev));
1356 	ccw_device_set_options_mask(cdev, 0);
1357 
1358 	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1359 	spin_unlock_irq(get_ccwdev_lock(cdev));
1360 	if (rc) {
1361 		DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1362 		DBF_ERROR("rc:%4x", rc);
1363 		mutex_unlock(&irq_ptr->setup_mutex);
1364 		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1365 		return rc;
1366 	}
1367 
1368 	wait_event_interruptible_timeout(cdev->private->wait_q,
1369 		irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1370 		irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1371 
1372 	if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1373 		mutex_unlock(&irq_ptr->setup_mutex);
1374 		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1375 		return -EIO;
1376 	}
1377 
1378 	qdio_setup_ssqd_info(irq_ptr);
1379 
1380 	qdio_detect_hsicq(irq_ptr);
1381 
1382 	/* qebsm is now setup if available, initialize buffer states */
1383 	qdio_init_buf_states(irq_ptr);
1384 
1385 	mutex_unlock(&irq_ptr->setup_mutex);
1386 	qdio_print_subchannel_info(irq_ptr, cdev);
1387 	qdio_setup_debug_entries(irq_ptr, cdev);
1388 	return 0;
1389 }
1390 EXPORT_SYMBOL_GPL(qdio_establish);
1391 
1392 /**
1393  * qdio_activate - activate queues on a qdio subchannel
1394  * @cdev: associated cdev
1395  */
1396 int qdio_activate(struct ccw_device *cdev)
1397 {
1398 	struct subchannel_id schid;
1399 	struct qdio_irq *irq_ptr;
1400 	int rc;
1401 
1402 	ccw_device_get_schid(cdev, &schid);
1403 	DBF_EVENT("qactivate:%4x", schid.sch_no);
1404 
1405 	irq_ptr = cdev->private->qdio_data;
1406 	if (!irq_ptr)
1407 		return -ENODEV;
1408 
1409 	mutex_lock(&irq_ptr->setup_mutex);
1410 	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1411 		rc = -EBUSY;
1412 		goto out;
1413 	}
1414 
1415 	irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1416 	irq_ptr->ccw.flags = CCW_FLAG_SLI;
1417 	irq_ptr->ccw.count = irq_ptr->aqueue.count;
1418 	irq_ptr->ccw.cda = 0;
1419 
1420 	spin_lock_irq(get_ccwdev_lock(cdev));
1421 	ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1422 
1423 	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1424 			      0, DOIO_DENY_PREFETCH);
1425 	spin_unlock_irq(get_ccwdev_lock(cdev));
1426 	if (rc) {
1427 		DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1428 		DBF_ERROR("rc:%4x", rc);
1429 		goto out;
1430 	}
1431 
1432 	if (is_thinint_irq(irq_ptr))
1433 		tiqdio_add_input_queues(irq_ptr);
1434 
1435 	/* wait for subchannel to become active */
1436 	msleep(5);
1437 
1438 	switch (irq_ptr->state) {
1439 	case QDIO_IRQ_STATE_STOPPED:
1440 	case QDIO_IRQ_STATE_ERR:
1441 		rc = -EIO;
1442 		break;
1443 	default:
1444 		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1445 		rc = 0;
1446 	}
1447 out:
1448 	mutex_unlock(&irq_ptr->setup_mutex);
1449 	return rc;
1450 }
1451 EXPORT_SYMBOL_GPL(qdio_activate);
1452 
1453 static inline int buf_in_between(int bufnr, int start, int count)
1454 {
1455 	int end = add_buf(start, count);
1456 
1457 	if (end > start) {
1458 		if (bufnr >= start && bufnr < end)
1459 			return 1;
1460 		else
1461 			return 0;
1462 	}
1463 
1464 	/* wrap-around case */
1465 	if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1466 	    (bufnr < end))
1467 		return 1;
1468 	else
1469 		return 0;
1470 }
1471 
1472 /**
1473  * handle_inbound - reset processed input buffers
1474  * @q: queue containing the buffers
1475  * @callflags: flags
1476  * @bufnr: first buffer to process
1477  * @count: how many buffers are emptied
1478  */
1479 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1480 			  int bufnr, int count)
1481 {
1482 	int diff;
1483 
1484 	qperf_inc(q, inbound_call);
1485 
1486 	if (!q->u.in.polling)
1487 		goto set;
1488 
1489 	/* protect against stop polling setting an ACK for an emptied slsb */
1490 	if (count == QDIO_MAX_BUFFERS_PER_Q) {
1491 		/* overwriting everything, just delete polling status */
1492 		q->u.in.polling = 0;
1493 		q->u.in.ack_count = 0;
1494 		goto set;
1495 	} else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1496 		if (is_qebsm(q)) {
1497 			/* partial overwrite, just update ack_start */
1498 			diff = add_buf(bufnr, count);
1499 			diff = sub_buf(diff, q->u.in.ack_start);
1500 			q->u.in.ack_count -= diff;
1501 			if (q->u.in.ack_count <= 0) {
1502 				q->u.in.polling = 0;
1503 				q->u.in.ack_count = 0;
1504 				goto set;
1505 			}
1506 			q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1507 		}
1508 		else
1509 			/* the only ACK will be deleted, so stop polling */
1510 			q->u.in.polling = 0;
1511 	}
1512 
1513 set:
1514 	count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1515 	atomic_add(count, &q->nr_buf_used);
1516 
1517 	if (need_siga_in(q))
1518 		return qdio_siga_input(q);
1519 
1520 	return 0;
1521 }
1522 
1523 /**
1524  * handle_outbound - process filled outbound buffers
1525  * @q: queue containing the buffers
1526  * @callflags: flags
1527  * @bufnr: first buffer to process
1528  * @count: how many buffers are filled
1529  */
1530 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1531 			   int bufnr, int count)
1532 {
1533 	unsigned char state = 0;
1534 	int used, rc = 0;
1535 
1536 	qperf_inc(q, outbound_call);
1537 
1538 	count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1539 	used = atomic_add_return(count, &q->nr_buf_used);
1540 
1541 	if (used == QDIO_MAX_BUFFERS_PER_Q)
1542 		qperf_inc(q, outbound_queue_full);
1543 
1544 	if (callflags & QDIO_FLAG_PCI_OUT) {
1545 		q->u.out.pci_out_enabled = 1;
1546 		qperf_inc(q, pci_request_int);
1547 	} else
1548 		q->u.out.pci_out_enabled = 0;
1549 
1550 	if (queue_type(q) == QDIO_IQDIO_QFMT) {
1551 		unsigned long phys_aob = 0;
1552 
1553 		/* One SIGA-W per buffer required for unicast HSI */
1554 		WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1555 
1556 		phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1557 
1558 		rc = qdio_kick_outbound_q(q, phys_aob);
1559 	} else if (need_siga_sync(q)) {
1560 		rc = qdio_siga_sync_q(q);
1561 	} else {
1562 		/* try to fast requeue buffers */
1563 		get_buf_state(q, prev_buf(bufnr), &state, 0);
1564 		if (state != SLSB_CU_OUTPUT_PRIMED)
1565 			rc = qdio_kick_outbound_q(q, 0);
1566 		else
1567 			qperf_inc(q, fast_requeue);
1568 	}
1569 
1570 	/* in case of SIGA errors we must process the error immediately */
1571 	if (used >= q->u.out.scan_threshold || rc)
1572 		qdio_tasklet_schedule(q);
1573 	else
1574 		/* free the SBALs in case of no further traffic */
1575 		if (!timer_pending(&q->u.out.timer) &&
1576 		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
1577 			mod_timer(&q->u.out.timer, jiffies + HZ);
1578 	return rc;
1579 }
1580 
1581 /**
1582  * do_QDIO - process input or output buffers
1583  * @cdev: associated ccw_device for the qdio subchannel
1584  * @callflags: input or output and special flags from the program
1585  * @q_nr: queue number
1586  * @bufnr: buffer number
1587  * @count: how many buffers to process
1588  */
1589 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1590 	    int q_nr, unsigned int bufnr, unsigned int count)
1591 {
1592 	struct qdio_irq *irq_ptr;
1593 
1594 	if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1595 		return -EINVAL;
1596 
1597 	irq_ptr = cdev->private->qdio_data;
1598 	if (!irq_ptr)
1599 		return -ENODEV;
1600 
1601 	DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1602 		      "do%02x b:%02x c:%02x", callflags, bufnr, count);
1603 
1604 	if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1605 		return -EIO;
1606 	if (!count)
1607 		return 0;
1608 	if (callflags & QDIO_FLAG_SYNC_INPUT)
1609 		return handle_inbound(irq_ptr->input_qs[q_nr],
1610 				      callflags, bufnr, count);
1611 	else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1612 		return handle_outbound(irq_ptr->output_qs[q_nr],
1613 				       callflags, bufnr, count);
1614 	return -EINVAL;
1615 }
1616 EXPORT_SYMBOL_GPL(do_QDIO);
1617 
1618 /**
1619  * qdio_start_irq - process input buffers
1620  * @cdev: associated ccw_device for the qdio subchannel
1621  * @nr: input queue number
1622  *
1623  * Return codes
1624  *   0 - success
1625  *   1 - irqs not started since new data is available
1626  */
1627 int qdio_start_irq(struct ccw_device *cdev, int nr)
1628 {
1629 	struct qdio_q *q;
1630 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1631 
1632 	if (!irq_ptr)
1633 		return -ENODEV;
1634 	q = irq_ptr->input_qs[nr];
1635 
1636 	clear_nonshared_ind(irq_ptr);
1637 	qdio_stop_polling(q);
1638 	clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1639 
1640 	/*
1641 	 * We need to check again to not lose initiative after
1642 	 * resetting the ACK state.
1643 	 */
1644 	if (test_nonshared_ind(irq_ptr))
1645 		goto rescan;
1646 	if (!qdio_inbound_q_done(q, q->first_to_check))
1647 		goto rescan;
1648 	return 0;
1649 
1650 rescan:
1651 	if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1652 			     &q->u.in.queue_irq_state))
1653 		return 0;
1654 	else
1655 		return 1;
1656 
1657 }
1658 EXPORT_SYMBOL(qdio_start_irq);
1659 
1660 /**
1661  * qdio_get_next_buffers - process input buffers
1662  * @cdev: associated ccw_device for the qdio subchannel
1663  * @nr: input queue number
1664  * @bufnr: first filled buffer number
1665  * @error: buffers are in error state
1666  *
1667  * Return codes
1668  *   < 0 - error
1669  *   = 0 - no new buffers found
1670  *   > 0 - number of processed buffers
1671  */
1672 int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1673 			  int *error)
1674 {
1675 	struct qdio_q *q;
1676 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1677 	unsigned int start;
1678 	int count;
1679 
1680 	if (!irq_ptr)
1681 		return -ENODEV;
1682 	q = irq_ptr->input_qs[nr];
1683 	start = q->first_to_check;
1684 
1685 	/*
1686 	 * Cannot rely on automatic sync after interrupt since queues may
1687 	 * also be examined without interrupt.
1688 	 */
1689 	if (need_siga_sync(q))
1690 		qdio_sync_queues(q);
1691 
1692 	qdio_check_outbound_pci_queues(irq_ptr);
1693 
1694 	count = qdio_inbound_q_moved(q, start);
1695 	if (count == 0)
1696 		return 0;
1697 
1698 	start = add_buf(start, count);
1699 	q->first_to_check = start;
1700 
1701 	/* Note: upper-layer MUST stop processing immediately here ... */
1702 	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1703 		return -EIO;
1704 
1705 	*bufnr = q->first_to_kick;
1706 	*error = q->qdio_error;
1707 
1708 	/* for the next time */
1709 	q->first_to_kick = add_buf(q->first_to_kick, count);
1710 	q->qdio_error = 0;
1711 
1712 	return count;
1713 }
1714 EXPORT_SYMBOL(qdio_get_next_buffers);
1715 
1716 /**
1717  * qdio_stop_irq - disable interrupt processing for the device
1718  * @cdev: associated ccw_device for the qdio subchannel
1719  * @nr: input queue number
1720  *
1721  * Return codes
1722  *   0 - interrupts were already disabled
1723  *   1 - interrupts successfully disabled
1724  */
1725 int qdio_stop_irq(struct ccw_device *cdev, int nr)
1726 {
1727 	struct qdio_q *q;
1728 	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1729 
1730 	if (!irq_ptr)
1731 		return -ENODEV;
1732 	q = irq_ptr->input_qs[nr];
1733 
1734 	if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1735 			     &q->u.in.queue_irq_state))
1736 		return 0;
1737 	else
1738 		return 1;
1739 }
1740 EXPORT_SYMBOL(qdio_stop_irq);
1741 
1742 /**
1743  * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1744  * @schid:		Subchannel ID.
1745  * @cnc:		Boolean Change-Notification Control
1746  * @response:		Response code will be stored at this address
1747  * @cb: 		Callback function will be executed for each element
1748  *			of the address list
1749  * @priv:		Pointer to pass to the callback function.
1750  *
1751  * Performs "Store-network-bridging-information list" operation and calls
1752  * the callback function for every entry in the list. If "change-
1753  * notification-control" is set, further changes in the address list
1754  * will be reported via the IPA command.
1755  */
1756 int qdio_pnso_brinfo(struct subchannel_id schid,
1757 		int cnc, u16 *response,
1758 		void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1759 				void *entry),
1760 		void *priv)
1761 {
1762 	struct chsc_pnso_area *rr;
1763 	int rc;
1764 	u32 prev_instance = 0;
1765 	int isfirstblock = 1;
1766 	int i, size, elems;
1767 
1768 	rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1769 	if (rr == NULL)
1770 		return -ENOMEM;
1771 	do {
1772 		/* on the first iteration, naihdr.resume_token will be zero */
1773 		rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1774 		if (rc != 0 && rc != -EBUSY)
1775 			goto out;
1776 		if (rr->response.code != 1) {
1777 			rc = -EIO;
1778 			continue;
1779 		} else
1780 			rc = 0;
1781 
1782 		if (cb == NULL)
1783 			continue;
1784 
1785 		size = rr->naihdr.naids;
1786 		elems = (rr->response.length -
1787 				sizeof(struct chsc_header) -
1788 				sizeof(struct chsc_brinfo_naihdr)) /
1789 				size;
1790 
1791 		if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1792 			/* Inform the caller that they need to scrap */
1793 			/* the data that was already reported via cb */
1794 				rc = -EAGAIN;
1795 				break;
1796 		}
1797 		isfirstblock = 0;
1798 		prev_instance = rr->naihdr.instance;
1799 		for (i = 0; i < elems; i++)
1800 			switch (size) {
1801 			case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1802 				(*cb)(priv, l3_ipv6_addr,
1803 						&rr->entries.l3_ipv6[i]);
1804 				break;
1805 			case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1806 				(*cb)(priv, l3_ipv4_addr,
1807 						&rr->entries.l3_ipv4[i]);
1808 				break;
1809 			case sizeof(struct qdio_brinfo_entry_l2):
1810 				(*cb)(priv, l2_addr_lnid,
1811 						&rr->entries.l2[i]);
1812 				break;
1813 			default:
1814 				WARN_ON_ONCE(1);
1815 				rc = -EIO;
1816 				goto out;
1817 			}
1818 	} while (rr->response.code == 0x0107 ||  /* channel busy */
1819 		  (rr->response.code == 1 && /* list stored */
1820 		   /* resume token is non-zero => list incomplete */
1821 		   (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1822 	(*response) = rr->response.code;
1823 
1824 out:
1825 	free_page((unsigned long)rr);
1826 	return rc;
1827 }
1828 EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1829 
1830 static int __init init_QDIO(void)
1831 {
1832 	int rc;
1833 
1834 	rc = qdio_debug_init();
1835 	if (rc)
1836 		return rc;
1837 	rc = qdio_setup_init();
1838 	if (rc)
1839 		goto out_debug;
1840 	rc = tiqdio_allocate_memory();
1841 	if (rc)
1842 		goto out_cache;
1843 	rc = tiqdio_register_thinints();
1844 	if (rc)
1845 		goto out_ti;
1846 	return 0;
1847 
1848 out_ti:
1849 	tiqdio_free_memory();
1850 out_cache:
1851 	qdio_setup_exit();
1852 out_debug:
1853 	qdio_debug_exit();
1854 	return rc;
1855 }
1856 
1857 static void __exit exit_QDIO(void)
1858 {
1859 	tiqdio_unregister_thinints();
1860 	tiqdio_free_memory();
1861 	qdio_setup_exit();
1862 	qdio_debug_exit();
1863 }
1864 
1865 module_init(init_QDIO);
1866 module_exit(exit_QDIO);
1867