qdio_main.c 45.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
J
Jan Glauber 已提交
2 3 4
/*
 * Linux for s390 qdio support, buffer handling, qdio API and module support.
 *
5
 * Copyright IBM Corp. 2000, 2008
J
Jan Glauber 已提交
6 7 8 9 10 11 12 13 14
 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
 *	      Jan Glauber <jang@linux.vnet.ibm.com>
 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/delay.h>
15
#include <linux/gfp.h>
16
#include <linux/io.h>
A
Arun Sharma 已提交
17
#include <linux/atomic.h>
J
Jan Glauber 已提交
18 19
#include <asm/debug.h>
#include <asm/qdio.h>
20
#include <asm/ipl.h>
J
Jan Glauber 已提交
21 22 23 24 25 26 27 28 29 30 31 32

#include "cio.h"
#include "css.h"
#include "device.h"
#include "qdio.h"
#include "qdio_debug.h"

MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
	"Jan Glauber <jang@linux.vnet.ibm.com>");
MODULE_DESCRIPTION("QDIO base support");
MODULE_LICENSE("GPL");

33 34 35
static inline int do_siga_sync(unsigned long schid,
			       unsigned int out_mask, unsigned int in_mask,
			       unsigned int fc)
J
Jan Glauber 已提交
36
{
37 38
	register unsigned long __fc asm ("0") = fc;
	register unsigned long __schid asm ("1") = schid;
J
Jan Glauber 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
	register unsigned long out asm ("2") = out_mask;
	register unsigned long in asm ("3") = in_mask;
	int cc;

	asm volatile(
		"	siga	0\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (cc)
		: "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
	return cc;
}

52 53
static inline int do_siga_input(unsigned long schid, unsigned int mask,
				unsigned int fc)
J
Jan Glauber 已提交
54
{
55 56
	register unsigned long __fc asm ("0") = fc;
	register unsigned long __schid asm ("1") = schid;
J
Jan Glauber 已提交
57 58 59 60 61 62 63 64
	register unsigned long __mask asm ("2") = mask;
	int cc;

	asm volatile(
		"	siga	0\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (cc)
65
		: "d" (__fc), "d" (__schid), "d" (__mask) : "cc");
J
Jan Glauber 已提交
66 67 68 69 70 71 72 73 74 75
	return cc;
}

/**
 * do_siga_output - perform SIGA-w/wt function
 * @schid: subchannel id or in case of QEBSM the subchannel token
 * @mask: which output queues to process
 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
 * @fc: function code to perform
 *
76
 * Returns condition code.
J
Jan Glauber 已提交
77 78 79
 * Note: For IQDC unicast queues only the highest priority queue is processed.
 */
static inline int do_siga_output(unsigned long schid, unsigned long mask,
80 81
				 unsigned int *bb, unsigned int fc,
				 unsigned long aob)
J
Jan Glauber 已提交
82 83 84 85
{
	register unsigned long __fc asm("0") = fc;
	register unsigned long __schid asm("1") = schid;
	register unsigned long __mask asm("2") = mask;
86
	register unsigned long __aob asm("3") = aob;
87
	int cc;
J
Jan Glauber 已提交
88 89 90

	asm volatile(
		"	siga	0\n"
91
		"	ipm	%0\n"
J
Jan Glauber 已提交
92
		"	srl	%0,28\n"
93 94 95 96
		: "=d" (cc), "+d" (__fc), "+d" (__aob)
		: "d" (__schid), "d" (__mask)
		: "cc");
	*bb = __fc >> 31;
J
Jan Glauber 已提交
97 98 99 100 101 102 103 104
	return cc;
}

static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
{
	/* all done or next buffer state different */
	if (ccq == 0 || ccq == 32)
		return 0;
J
Jan Glauber 已提交
105 106
	/* no buffer processed */
	if (ccq == 97)
J
Jan Glauber 已提交
107
		return 1;
J
Jan Glauber 已提交
108 109 110
	/* not all buffers processed */
	if (ccq == 96)
		return 2;
J
Jan Glauber 已提交
111
	/* notify devices immediately */
112
	DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
J
Jan Glauber 已提交
113 114 115 116 117 118 119 120 121
	return -EIO;
}

/**
 * qdio_do_eqbs - extract buffer states for QEBSM
 * @q: queue to manipulate
 * @state: state of the extracted buffers
 * @start: buffer number to start at
 * @count: count of buffers to examine
122
 * @auto_ack: automatically acknowledge buffers
J
Jan Glauber 已提交
123
 *
C
Coly Li 已提交
124
 * Returns the number of successfully extracted equal buffer states.
J
Jan Glauber 已提交
125 126 127
 * Stops processing if a state is different from the last buffers state.
 */
static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
128
			int start, int count, int auto_ack)
J
Jan Glauber 已提交
129
{
J
Jan Glauber 已提交
130
	int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
J
Jan Glauber 已提交
131 132
	unsigned int ccq = 0;

133
	qperf_inc(q, eqbs);
J
Jan Glauber 已提交
134 135 136 137

	if (!q->is_input_q)
		nr += q->irq_ptr->nr_input_qs;
again:
138 139
	ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
		      auto_ack);
J
Jan Glauber 已提交
140
	rc = qdio_check_ccq(q, ccq);
J
Jan Glauber 已提交
141 142
	if (!rc)
		return count - tmp_count;
143

J
Jan Glauber 已提交
144
	if (rc == 1) {
145
		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
J
Jan Glauber 已提交
146 147 148
		goto again;
	}

J
Jan Glauber 已提交
149 150 151 152 153 154 155 156 157 158 159 160
	if (rc == 2) {
		qperf_inc(q, eqbs_partial);
		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
			tmp_count);
		/*
		 * Retry once, if that fails bail out and process the
		 * extracted buffers before trying again.
		 */
		if (!retried++)
			goto again;
		else
			return count - tmp_count;
J
Jan Glauber 已提交
161
	}
J
Jan Glauber 已提交
162 163 164

	DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
	DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
165
	q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE,
166
		   q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
J
Jan Glauber 已提交
167
	return 0;
J
Jan Glauber 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

/**
 * qdio_do_sqbs - set buffer states for QEBSM
 * @q: queue to manipulate
 * @state: new state of the buffers
 * @start: first buffer number to change
 * @count: how many buffers to change
 *
 * Returns the number of successfully changed buffers.
 * Does retrying until the specified count of buffer states is set or an
 * error occurs.
 */
static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
			int count)
{
	unsigned int ccq = 0;
	int tmp_count = count, tmp_start = start;
	int nr = q->nr;
	int rc;

189 190
	if (!count)
		return 0;
191
	qperf_inc(q, sqbs);
J
Jan Glauber 已提交
192 193 194 195 196 197

	if (!q->is_input_q)
		nr += q->irq_ptr->nr_input_qs;
again:
	ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
	rc = qdio_check_ccq(q, ccq);
J
Jan Glauber 已提交
198
	if (!rc) {
199
		WARN_ON_ONCE(tmp_count);
J
Jan Glauber 已提交
200 201 202 203
		return count - tmp_count;
	}

	if (rc == 1 || rc == 2) {
204
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
205
		qperf_inc(q, sqbs_partial);
J
Jan Glauber 已提交
206 207
		goto again;
	}
J
Jan Glauber 已提交
208 209 210

	DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
	DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
211
	q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE,
212
		   q->nr, q->first_to_kick, count, q->irq_ptr->int_parm);
J
Jan Glauber 已提交
213
	return 0;
J
Jan Glauber 已提交
214 215 216 217
}

/* returns number of examined buffers and their common state in *state */
static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
218
				 unsigned char *state, unsigned int count,
219
				 int auto_ack, int merge_pending)
J
Jan Glauber 已提交
220 221 222 223 224
{
	unsigned char __state = 0;
	int i;

	if (is_qebsm(q))
225
		return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
J
Jan Glauber 已提交
226 227

	for (i = 0; i < count; i++) {
228
		if (!__state) {
J
Jan Glauber 已提交
229
			__state = q->slsb.val[bufnr];
230 231 232 233 234 235
			if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
				__state = SLSB_P_OUTPUT_EMPTY;
		} else if (merge_pending) {
			if ((q->slsb.val[bufnr] & __state) != __state)
				break;
		} else if (q->slsb.val[bufnr] != __state)
J
Jan Glauber 已提交
236 237 238 239 240 241 242
			break;
		bufnr = next_buf(bufnr);
	}
	*state = __state;
	return i;
}

243 244
static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
				unsigned char *state, int auto_ack)
J
Jan Glauber 已提交
245
{
246
	return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
J
Jan Glauber 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

/* wrap-around safe setting of slsb states, returns number of changed buffers */
static inline int set_buf_states(struct qdio_q *q, int bufnr,
				 unsigned char state, int count)
{
	int i;

	if (is_qebsm(q))
		return qdio_do_sqbs(q, state, bufnr, count);

	for (i = 0; i < count; i++) {
		xchg(&q->slsb.val[bufnr], state);
		bufnr = next_buf(bufnr);
	}
	return count;
}

static inline int set_buf_state(struct qdio_q *q, int bufnr,
				unsigned char state)
{
	return set_buf_states(q, bufnr, state, 1);
}

/* set slsb states to initial state */
272
static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
J
Jan Glauber 已提交
273 274 275 276 277 278 279 280 281 282 283 284
{
	struct qdio_q *q;
	int i;

	for_each_input_queue(irq_ptr, q, i)
		set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
			       QDIO_MAX_BUFFERS_PER_Q);
	for_each_output_queue(irq_ptr, q, i)
		set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
			       QDIO_MAX_BUFFERS_PER_Q);
}

285
static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
J
Jan Glauber 已提交
286 287
			  unsigned int input)
{
288 289
	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
	unsigned int fc = QDIO_SIGA_SYNC;
J
Jan Glauber 已提交
290 291
	int cc;

292
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
293
	qperf_inc(q, siga_sync);
J
Jan Glauber 已提交
294

295 296 297 298 299 300
	if (is_qebsm(q)) {
		schid = q->irq_ptr->sch_token;
		fc |= QDIO_SIGA_QEBSM_FLAG;
	}

	cc = do_siga_sync(schid, output, input, fc);
J
Jan Glauber 已提交
301
	if (unlikely(cc))
302
		DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
303
	return (cc) ? -EIO : 0;
J
Jan Glauber 已提交
304 305
}

306
static inline int qdio_siga_sync_q(struct qdio_q *q)
J
Jan Glauber 已提交
307 308 309 310 311 312 313
{
	if (q->is_input_q)
		return qdio_siga_sync(q, 0, q->mask);
	else
		return qdio_siga_sync(q, q->mask, 0);
}

314 315
static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
	unsigned long aob)
J
Jan Glauber 已提交
316
{
317 318
	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
	unsigned int fc = QDIO_SIGA_WRITE;
319
	u64 start_time = 0;
320
	int retries = 0, cc;
321 322
	unsigned long laob = 0;

323 324
	WARN_ON_ONCE(aob && ((queue_type(q) != QDIO_IQDIO_QFMT) ||
			     !q->u.out.use_cq));
325 326 327 328
	if (q->u.out.use_cq && aob != 0) {
		fc = QDIO_SIGA_WRITEQ;
		laob = aob;
	}
J
Jan Glauber 已提交
329

330
	if (is_qebsm(q)) {
J
Jan Glauber 已提交
331
		schid = q->irq_ptr->sch_token;
332
		fc |= QDIO_SIGA_QEBSM_FLAG;
J
Jan Glauber 已提交
333 334
	}
again:
335
	cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
336 337

	/* hipersocket busy condition */
J
Jan Glauber 已提交
338
	if (unlikely(*busy_bit)) {
339
		retries++;
340

341
		if (!start_time) {
342
			start_time = get_tod_clock_fast();
343 344
			goto again;
		}
345
		if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
J
Jan Glauber 已提交
346 347
			goto again;
	}
348 349 350 351 352
	if (retries) {
		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
			      "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
		DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
	}
J
Jan Glauber 已提交
353 354 355 356 357
	return cc;
}

static inline int qdio_siga_input(struct qdio_q *q)
{
358 359
	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
	unsigned int fc = QDIO_SIGA_READ;
J
Jan Glauber 已提交
360 361
	int cc;

362
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
363
	qperf_inc(q, siga_read);
J
Jan Glauber 已提交
364

365 366 367 368 369 370
	if (is_qebsm(q)) {
		schid = q->irq_ptr->sch_token;
		fc |= QDIO_SIGA_QEBSM_FLAG;
	}

	cc = do_siga_input(schid, q->mask, fc);
J
Jan Glauber 已提交
371
	if (unlikely(cc))
372
		DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
373
	return (cc) ? -EIO : 0;
J
Jan Glauber 已提交
374 375
}

J
Jan Glauber 已提交
376 377 378 379
#define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
#define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)

static inline void qdio_sync_queues(struct qdio_q *q)
J
Jan Glauber 已提交
380
{
J
Jan Glauber 已提交
381 382 383 384
	/* PCI capable outbound queues will also be scanned so sync them too */
	if (pci_out_supported(q))
		qdio_siga_sync_all(q);
	else
J
Jan Glauber 已提交
385 386 387
		qdio_siga_sync_q(q);
}

388 389 390
int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
			unsigned char *state)
{
J
Jan Glauber 已提交
391 392
	if (need_siga_sync(q))
		qdio_siga_sync_q(q);
393
	return get_buf_states(q, bufnr, state, 1, 0, 0);
394 395 396
}

static inline void qdio_stop_polling(struct qdio_q *q)
J
Jan Glauber 已提交
397
{
398
	if (!q->u.in.polling)
J
Jan Glauber 已提交
399
		return;
400

J
Jan Glauber 已提交
401
	q->u.in.polling = 0;
402
	qperf_inc(q, stop_polling);
J
Jan Glauber 已提交
403 404

	/* show the card that we are not polling anymore */
405
	if (is_qebsm(q)) {
406
		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
407 408 409
			       q->u.in.ack_count);
		q->u.in.ack_count = 0;
	} else
410
		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
J
Jan Glauber 已提交
411 412
}

413
static inline void account_sbals(struct qdio_q *q, unsigned int count)
414
{
415
	int pos;
416 417 418 419 420 421

	q->q_stats.nr_sbal_total += count;
	if (count == QDIO_MAX_BUFFERS_MASK) {
		q->q_stats.nr_sbals[7]++;
		return;
	}
422
	pos = ilog2(count);
423 424 425
	q->q_stats.nr_sbals[pos]++;
}

426
static void process_buffer_error(struct qdio_q *q, int count)
J
Jan Glauber 已提交
427
{
428 429 430
	unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
					SLSB_P_OUTPUT_NOT_INIT;

431
	q->qdio_error = QDIO_ERROR_SLSB_STATE;
432 433

	/* special handling for no target buffer empty */
434 435
	if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
	    q->sbal[q->first_to_check]->element[15].sflags == 0x10) {
436
		qperf_inc(q, target_full);
437
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
438
			      q->first_to_check);
439
		goto set;
440 441
	}

442 443
	DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
	DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
444
	DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
445
	DBF_ERROR("F14:%2x F15:%2x",
446 447
		  q->sbal[q->first_to_check]->element[14].sflags,
		  q->sbal[q->first_to_check]->element[15].sflags);
448

449
set:
450 451 452 453 454
	/*
	 * Interrupts may be avoided as long as the error is present
	 * so change the buffer state immediately to avoid starvation.
	 */
	set_buf_states(q, q->first_to_check, state, count);
455
}
J
Jan Glauber 已提交
456

457 458 459 460
static inline void inbound_primed(struct qdio_q *q, int count)
{
	int new;

461
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
462 463 464 465 466 467

	/* for QEBSM the ACK was already set by EQBS */
	if (is_qebsm(q)) {
		if (!q->u.in.polling) {
			q->u.in.polling = 1;
			q->u.in.ack_count = count;
468
			q->u.in.ack_start = q->first_to_check;
469 470 471 472
			return;
		}

		/* delete the previous ACK's */
473
		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
474 475
			       q->u.in.ack_count);
		q->u.in.ack_count = count;
476
		q->u.in.ack_start = q->first_to_check;
477 478 479 480 481 482 483 484 485 486 487
		return;
	}

	/*
	 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
	 * or by the next inbound run.
	 */
	new = add_buf(q->first_to_check, count - 1);
	if (q->u.in.polling) {
		/* reset the previous ACK but first set the new one */
		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 496 497
	count--;
	if (!count)
		return;
498 499
	/* need to change ALL buffers to get more interrupts */
	set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
J
Jan Glauber 已提交
500 501 502 503 504
}

static int get_inbound_buffer_frontier(struct qdio_q *q)
{
	int count, stop;
505
	unsigned char state = 0;
J
Jan Glauber 已提交
506

507
	q->timestamp = get_tod_clock_fast();
508

J
Jan Glauber 已提交
509 510 511 512 513 514 515 516 517 518
	/*
	 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
	 * would return 0.
	 */
	count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
	stop = add_buf(q->first_to_check, count);

	if (q->first_to_check == stop)
		goto out;

519 520 521 522
	/*
	 * No siga sync here, as a PCI or we after a thin interrupt
	 * already sync'ed the queues.
	 */
523
	count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
J
Jan Glauber 已提交
524 525 526 527 528
	if (!count)
		goto out;

	switch (state) {
	case SLSB_P_INPUT_PRIMED:
529
		inbound_primed(q, count);
J
Jan Glauber 已提交
530
		q->first_to_check = add_buf(q->first_to_check, count);
531
		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
532
			qperf_inc(q, inbound_queue_full);
533 534
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
535
		break;
J
Jan Glauber 已提交
536
	case SLSB_P_INPUT_ERROR:
537
		process_buffer_error(q, count);
J
Jan Glauber 已提交
538
		q->first_to_check = add_buf(q->first_to_check, count);
539 540
		if (atomic_sub_return(count, &q->nr_buf_used) == 0)
			qperf_inc(q, inbound_queue_full);
541 542
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
543 544 545 546
		break;
	case SLSB_CU_INPUT_EMPTY:
	case SLSB_P_INPUT_NOT_INIT:
	case SLSB_P_INPUT_ACK:
547 548
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
549 550
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
			q->nr, q->first_to_check);
J
Jan Glauber 已提交
551 552
		break;
	default:
553
		WARN_ON_ONCE(1);
J
Jan Glauber 已提交
554 555 556 557 558
	}
out:
	return q->first_to_check;
}

559
static int qdio_inbound_q_moved(struct qdio_q *q)
J
Jan Glauber 已提交
560 561 562 563 564
{
	int bufnr;

	bufnr = get_inbound_buffer_frontier(q);

565
	if (bufnr != q->last_move) {
566
		q->last_move = bufnr;
567
		if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
568
			q->u.in.timestamp = get_tod_clock();
J
Jan Glauber 已提交
569 570 571 572 573
		return 1;
	} else
		return 0;
}

574
static inline int qdio_inbound_q_done(struct qdio_q *q)
J
Jan Glauber 已提交
575
{
576
	unsigned char state = 0;
J
Jan Glauber 已提交
577 578 579 580

	if (!atomic_read(&q->nr_buf_used))
		return 1;

J
Jan Glauber 已提交
581 582
	if (need_siga_sync(q))
		qdio_siga_sync_q(q);
583
	get_buf_state(q, q->first_to_check, &state, 0);
584

585
	if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
586
		/* more work coming */
J
Jan Glauber 已提交
587 588
		return 0;

589 590 591 592 593
	if (is_thinint_irq(q->irq_ptr))
		return 1;

	/* don't poll under z/VM */
	if (MACHINE_IS_VM)
J
Jan Glauber 已提交
594 595 596 597 598 599
		return 1;

	/*
	 * At this point we know, that inbound first_to_check
	 * has (probably) not moved (see qdio_inbound_processing).
	 */
600
	if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
601
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
602
			      q->first_to_check);
J
Jan Glauber 已提交
603
		return 1;
604
	} else
605 606 607
		return 0;
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
static inline int contains_aobs(struct qdio_q *q)
{
	return !q->is_input_q && q->u.out.use_cq;
}

static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
{
	unsigned char state = 0;
	int j, b = start;

	if (!contains_aobs(q))
		return;

	for (j = 0; j < count; ++j) {
		get_buf_state(q, b, &state, 0);
		if (state == SLSB_P_OUTPUT_PENDING) {
			struct qaob *aob = q->u.out.aobs[b];
			if (aob == NULL)
				continue;

			q->u.out.sbal_state[b].flags |=
				QDIO_OUTBUF_STATE_FLAG_PENDING;
			q->u.out.aobs[b] = NULL;
		} else if (state == SLSB_P_OUTPUT_EMPTY) {
			q->u.out.sbal_state[b].aob = NULL;
		}
		b = next_buf(b);
	}
}

static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
					int bufnr)
{
	unsigned long phys_aob = 0;

	if (!q->use_cq)
		goto out;

	if (!q->aobs[bufnr]) {
		struct qaob *aob = qdio_allocate_aob();
		q->aobs[bufnr] = aob;
	}
	if (q->aobs[bufnr]) {
		q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
		q->sbal_state[bufnr].aob = q->aobs[bufnr];
		q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
		phys_aob = virt_to_phys(q->aobs[bufnr]);
655
		WARN_ON_ONCE(phys_aob & 0xFF);
656 657 658 659 660 661
	}

out:
	return phys_aob;
}

662
static void qdio_kick_handler(struct qdio_q *q)
J
Jan Glauber 已提交
663
{
664 665 666
	int start = q->first_to_kick;
	int end = q->first_to_check;
	int count;
J
Jan Glauber 已提交
667 668 669 670

	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
		return;

671 672 673
	count = sub_buf(end, start);

	if (q->is_input_q) {
674
		qperf_inc(q, inbound_handler);
675
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
U
Ursula Braun 已提交
676
	} else {
677
		qperf_inc(q, outbound_handler);
678 679
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
			      start, count);
U
Ursula Braun 已提交
680
	}
681

682 683
	qdio_handle_aobs(q, start, count);

684 685
	q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
		   q->irq_ptr->int_parm);
J
Jan Glauber 已提交
686 687

	/* for the next time */
688
	q->first_to_kick = end;
J
Jan Glauber 已提交
689 690 691
	q->qdio_error = 0;
}

692 693 694 695 696 697 698 699 700
static inline int qdio_tasklet_schedule(struct qdio_q *q)
{
	if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
		tasklet_schedule(&q->tasklet);
		return 0;
	}
	return -EPERM;
}

J
Jan Glauber 已提交
701 702
static void __qdio_inbound_processing(struct qdio_q *q)
{
703
	qperf_inc(q, tasklet_inbound);
704

J
Jan Glauber 已提交
705 706 707
	if (!qdio_inbound_q_moved(q))
		return;

708
	qdio_kick_handler(q);
J
Jan Glauber 已提交
709

710
	if (!qdio_inbound_q_done(q)) {
J
Jan Glauber 已提交
711
		/* means poll time is not yet over */
712
		qperf_inc(q, tasklet_inbound_resched);
713
		if (!qdio_tasklet_schedule(q))
714
			return;
715
	}
J
Jan Glauber 已提交
716 717 718 719 720 721

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
722 723
	if (!qdio_inbound_q_done(q)) {
		qperf_inc(q, tasklet_inbound_resched2);
724
		qdio_tasklet_schedule(q);
725
	}
J
Jan Glauber 已提交
726 727 728 729 730 731 732 733 734 735 736
}

void qdio_inbound_processing(unsigned long data)
{
	struct qdio_q *q = (struct qdio_q *)data;
	__qdio_inbound_processing(q);
}

static int get_outbound_buffer_frontier(struct qdio_q *q)
{
	int count, stop;
737
	unsigned char state = 0;
J
Jan Glauber 已提交
738

739
	q->timestamp = get_tod_clock_fast();
740

J
Jan Glauber 已提交
741 742 743 744 745 746
	if (need_siga_sync(q))
		if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
		    !pci_out_supported(q)) ||
		    (queue_type(q) == QDIO_IQDIO_QFMT &&
		    multicast_outbound(q)))
			qdio_siga_sync_q(q);
J
Jan Glauber 已提交
747 748 749 750 751 752 753 754

	/*
	 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
	 * would return 0.
	 */
	count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
	stop = add_buf(q->first_to_check, count);
	if (q->first_to_check == stop)
755
		goto out;
J
Jan Glauber 已提交
756

757
	count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
J
Jan Glauber 已提交
758
	if (!count)
759
		goto out;
J
Jan Glauber 已提交
760 761 762 763

	switch (state) {
	case SLSB_P_OUTPUT_EMPTY:
		/* the adapter got it */
764 765
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
			"out empty:%1d %02x", q->nr, count);
J
Jan Glauber 已提交
766 767 768

		atomic_sub(count, &q->nr_buf_used);
		q->first_to_check = add_buf(q->first_to_check, count);
769 770
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
771

772
		break;
J
Jan Glauber 已提交
773
	case SLSB_P_OUTPUT_ERROR:
774
		process_buffer_error(q, count);
J
Jan Glauber 已提交
775 776
		q->first_to_check = add_buf(q->first_to_check, count);
		atomic_sub(count, &q->nr_buf_used);
777 778
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
779 780 781
		break;
	case SLSB_CU_OUTPUT_PRIMED:
		/* the adapter has not fetched the output yet */
782 783
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
784 785
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
			      q->nr);
J
Jan Glauber 已提交
786 787 788 789 790
		break;
	case SLSB_P_OUTPUT_NOT_INIT:
	case SLSB_P_OUTPUT_HALTED:
		break;
	default:
791
		WARN_ON_ONCE(1);
J
Jan Glauber 已提交
792
	}
793 794

out:
J
Jan Glauber 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
	return q->first_to_check;
}

/* all buffers processed? */
static inline int qdio_outbound_q_done(struct qdio_q *q)
{
	return atomic_read(&q->nr_buf_used) == 0;
}

static inline int qdio_outbound_q_moved(struct qdio_q *q)
{
	int bufnr;

	bufnr = get_outbound_buffer_frontier(q);

810
	if (bufnr != q->last_move) {
811
		q->last_move = bufnr;
812
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
J
Jan Glauber 已提交
813 814 815 816 817
		return 1;
	} else
		return 0;
}

818
static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
J
Jan Glauber 已提交
819
{
820
	int retries = 0, cc;
821
	unsigned int busy_bit;
J
Jan Glauber 已提交
822 823

	if (!need_siga_out(q))
824
		return 0;
J
Jan Glauber 已提交
825

826
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
827
retry:
828
	qperf_inc(q, siga_write);
829

830
	cc = qdio_siga_output(q, &busy_bit, aob);
831
	switch (cc) {
J
Jan Glauber 已提交
832 833
	case 0:
		break;
834 835
	case 2:
		if (busy_bit) {
836 837 838 839 840
			while (++retries < QDIO_BUSY_BIT_RETRIES) {
				mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
				goto retry;
			}
			DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
841 842
			cc = -EBUSY;
		} else {
843
			DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
844 845
			cc = -ENOBUFS;
		}
846 847 848 849
		break;
	case 1:
	case 3:
		DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
850
		cc = -EIO;
851
		break;
J
Jan Glauber 已提交
852
	}
853 854 855 856
	if (retries) {
		DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
		DBF_ERROR("count:%u", retries);
	}
857
	return cc;
J
Jan Glauber 已提交
858 859 860 861
}

static void __qdio_outbound_processing(struct qdio_q *q)
{
862
	qperf_inc(q, tasklet_outbound);
863
	WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
J
Jan Glauber 已提交
864 865

	if (qdio_outbound_q_moved(q))
866
		qdio_kick_handler(q);
J
Jan Glauber 已提交
867

868
	if (queue_type(q) == QDIO_ZFCP_QFMT)
J
Jan Glauber 已提交
869
		if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
870
			goto sched;
J
Jan Glauber 已提交
871 872 873 874 875 876

	if (q->u.out.pci_out_enabled)
		return;

	/*
	 * Now we know that queue type is either qeth without pci enabled
J
Jan Glauber 已提交
877 878
	 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
	 * is noticed and outbound_handler is called after some time.
J
Jan Glauber 已提交
879 880
	 */
	if (qdio_outbound_q_done(q))
881
		del_timer_sync(&q->u.out.timer);
882
	else
883 884
		if (!timer_pending(&q->u.out.timer) &&
		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
J
Jan Glauber 已提交
885
			mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
886 887 888
	return;

sched:
889
	qdio_tasklet_schedule(q);
J
Jan Glauber 已提交
890 891 892 893 894 895 896 897 898
}

/* outbound tasklet */
void qdio_outbound_processing(unsigned long data)
{
	struct qdio_q *q = (struct qdio_q *)data;
	__qdio_outbound_processing(q);
}

899
void qdio_outbound_timer(struct timer_list *t)
J
Jan Glauber 已提交
900
{
901
	struct qdio_q *q = from_timer(q, t, u.out.timer);
902

903
	qdio_tasklet_schedule(q);
J
Jan Glauber 已提交
904 905
}

906
static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
J
Jan Glauber 已提交
907 908 909 910 911 912 913 914 915
{
	struct qdio_q *out;
	int i;

	if (!pci_out_supported(q))
		return;

	for_each_output_queue(q->irq_ptr, out, i)
		if (!qdio_outbound_q_done(out))
916
			qdio_tasklet_schedule(out);
J
Jan Glauber 已提交
917 918
}

919 920
static void __tiqdio_inbound_processing(struct qdio_q *q)
{
921
	qperf_inc(q, tasklet_inbound);
J
Jan Glauber 已提交
922 923
	if (need_siga_sync(q) && need_siga_sync_after_ai(q))
		qdio_sync_queues(q);
924 925 926 927 928 929 930 931 932 933 934 935

	/*
	 * The interrupt could be caused by a PCI request. Check the
	 * PCI capable outbound queues.
	 */
	qdio_check_outbound_after_thinint(q);

	if (!qdio_inbound_q_moved(q))
		return;

	qdio_kick_handler(q);

936
	if (!qdio_inbound_q_done(q)) {
937
		qperf_inc(q, tasklet_inbound_resched);
938
		if (!qdio_tasklet_schedule(q))
939
			return;
940 941 942 943 944 945 946
	}

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
947
	if (!qdio_inbound_q_done(q)) {
948
		qperf_inc(q, tasklet_inbound_resched2);
949
		qdio_tasklet_schedule(q);
950 951 952 953 954 955 956 957 958
	}
}

void tiqdio_inbound_processing(unsigned long data)
{
	struct qdio_q *q = (struct qdio_q *)data;
	__tiqdio_inbound_processing(q);
}

J
Jan Glauber 已提交
959 960 961
static inline void qdio_set_state(struct qdio_irq *irq_ptr,
				  enum qdio_irq_states state)
{
962
	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
J
Jan Glauber 已提交
963 964 965 966 967

	irq_ptr->state = state;
	mb();
}

968
static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
J
Jan Glauber 已提交
969 970
{
	if (irb->esw.esw0.erw.cons) {
971 972 973
		DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
		DBF_ERROR_HEX(irb, 64);
		DBF_ERROR_HEX(irb->ecw, 64);
J
Jan Glauber 已提交
974 975 976 977 978 979 980 981 982
	}
}

/* PCI interrupt handler */
static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
{
	int i;
	struct qdio_q *q;

983
	if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
984 985
		return;

J
Jan Glauber 已提交
986 987 988 989 990 991 992 993 994 995
	for_each_input_queue(irq_ptr, q, i) {
		if (q->u.in.queue_start_poll) {
			/* skip if polling is enabled or already in work */
			if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
				     &q->u.in.queue_irq_state)) {
				qperf_inc(q, int_discarded);
				continue;
			}
			q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
						 q->irq_ptr->int_parm);
996
		} else {
J
Jan Glauber 已提交
997
			tasklet_schedule(&q->tasklet);
998
		}
J
Jan Glauber 已提交
999
	}
J
Jan Glauber 已提交
1000

1001
	if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
J
Jan Glauber 已提交
1002 1003 1004 1005 1006
		return;

	for_each_output_queue(irq_ptr, q, i) {
		if (qdio_outbound_q_done(q))
			continue;
J
Jan Glauber 已提交
1007
		if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
J
Jan Glauber 已提交
1008
			qdio_siga_sync_q(q);
1009
		qdio_tasklet_schedule(q);
J
Jan Glauber 已提交
1010 1011 1012 1013 1014 1015 1016 1017
	}
}

static void qdio_handle_activate_check(struct ccw_device *cdev,
				unsigned long intparm, int cstat, int dstat)
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
	struct qdio_q *q;
1018
	int count;
J
Jan Glauber 已提交
1019

1020 1021 1022
	DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
	DBF_ERROR("intp :%lx", intparm);
	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
J
Jan Glauber 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031

	if (irq_ptr->nr_input_qs) {
		q = irq_ptr->input_qs[0];
	} else if (irq_ptr->nr_output_qs) {
		q = irq_ptr->output_qs[0];
	} else {
		dump_stack();
		goto no_handler;
	}
1032 1033

	count = sub_buf(q->first_to_check, q->first_to_kick);
1034
	q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
1035
		   q->nr, q->first_to_kick, count, irq_ptr->int_parm);
J
Jan Glauber 已提交
1036 1037
no_handler:
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1038 1039 1040 1041 1042
	/*
	 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
	 * Therefore we call the LGR detection function here.
	 */
	lgr_info_log();
J
Jan Glauber 已提交
1043 1044
}

1045 1046
static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
				      int dstat)
J
Jan Glauber 已提交
1047 1048 1049
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

1050
	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
J
Jan Glauber 已提交
1051

1052
	if (cstat)
J
Jan Glauber 已提交
1053
		goto error;
1054
	if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
J
Jan Glauber 已提交
1055
		goto error;
1056 1057 1058 1059 1060
	if (!(dstat & DEV_STAT_DEV_END))
		goto error;
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
	return;

J
Jan Glauber 已提交
1061
error:
1062 1063
	DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
J
Jan Glauber 已提交
1064 1065 1066 1067 1068 1069 1070 1071
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
}

/* qdio interrupt handler */
void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
		      struct irb *irb)
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1072
	struct subchannel_id schid;
J
Jan Glauber 已提交
1073 1074 1075
	int cstat, dstat;

	if (!intparm || !irq_ptr) {
1076 1077
		ccw_device_get_schid(cdev, &schid);
		DBF_ERROR("qint:%4x", schid.sch_no);
J
Jan Glauber 已提交
1078 1079 1080
		return;
	}

1081 1082 1083
	if (irq_ptr->perf_stat_enabled)
		irq_ptr->perf_stat.qdio_int++;

J
Jan Glauber 已提交
1084
	if (IS_ERR(irb)) {
1085 1086 1087 1088
		DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
		wake_up(&cdev->private->wait_q);
		return;
J
Jan Glauber 已提交
1089
	}
1090
	qdio_irq_check_sense(irq_ptr, irb);
J
Jan Glauber 已提交
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	cstat = irb->scsw.cmd.cstat;
	dstat = irb->scsw.cmd.dstat;

	switch (irq_ptr->state) {
	case QDIO_IRQ_STATE_INACTIVE:
		qdio_establish_handle_irq(cdev, cstat, dstat);
		break;
	case QDIO_IRQ_STATE_CLEANUP:
		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
		break;
	case QDIO_IRQ_STATE_ESTABLISHED:
	case QDIO_IRQ_STATE_ACTIVE:
		if (cstat & SCHN_STAT_PCI) {
			qdio_int_handler_pci(irq_ptr);
			return;
		}
1107
		if (cstat || dstat)
J
Jan Glauber 已提交
1108 1109
			qdio_handle_activate_check(cdev, intparm, cstat,
						   dstat);
1110
		break;
1111 1112
	case QDIO_IRQ_STATE_STOPPED:
		break;
J
Jan Glauber 已提交
1113
	default:
1114
		WARN_ON_ONCE(1);
J
Jan Glauber 已提交
1115 1116 1117 1118 1119 1120 1121
	}
	wake_up(&cdev->private->wait_q);
}

/**
 * qdio_get_ssqd_desc - get qdio subchannel description
 * @cdev: ccw device to get description for
1122
 * @data: where to store the ssqd
J
Jan Glauber 已提交
1123
 *
1124 1125
 * Returns 0 or an error code. The results of the chsc are stored in the
 * specified structure.
J
Jan Glauber 已提交
1126
 */
1127 1128
int qdio_get_ssqd_desc(struct ccw_device *cdev,
		       struct qdio_ssqd_desc *data)
J
Jan Glauber 已提交
1129
{
1130
	struct subchannel_id schid;
J
Jan Glauber 已提交
1131

1132 1133 1134
	if (!cdev || !cdev->private)
		return -EINVAL;

1135 1136 1137
	ccw_device_get_schid(cdev, &schid);
	DBF_EVENT("get ssqd:%4x", schid.sch_no);
	return qdio_setup_get_ssqd(NULL, &schid, data);
J
Jan Glauber 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
}
EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);

static void qdio_shutdown_queues(struct ccw_device *cdev)
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
	struct qdio_q *q;
	int i;

	for_each_input_queue(irq_ptr, q, i)
1148
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1149 1150

	for_each_output_queue(irq_ptr, q, i) {
1151
		del_timer_sync(&q->u.out.timer);
1152
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	}
}

/**
 * qdio_shutdown - shut down a qdio subchannel
 * @cdev: associated ccw device
 * @how: use halt or clear to shutdown
 */
int qdio_shutdown(struct ccw_device *cdev, int how)
{
1163
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1164
	struct subchannel_id schid;
J
Jan Glauber 已提交
1165 1166 1167 1168 1169
	int rc;

	if (!irq_ptr)
		return -ENODEV;

1170
	WARN_ON_ONCE(irqs_disabled());
1171 1172
	ccw_device_get_schid(cdev, &schid);
	DBF_EVENT("qshutdown:%4x", schid.sch_no);
1173

J
Jan Glauber 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	mutex_lock(&irq_ptr->setup_mutex);
	/*
	 * Subchannel was already shot down. We cannot prevent being called
	 * twice since cio may trigger a shutdown asynchronously.
	 */
	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
		mutex_unlock(&irq_ptr->setup_mutex);
		return 0;
	}

1184 1185 1186 1187 1188 1189
	/*
	 * Indicate that the device is going down. Scheduling the queue
	 * tasklets is forbidden from here on.
	 */
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);

J
Jan Glauber 已提交
1190 1191
	tiqdio_remove_input_queues(irq_ptr);
	qdio_shutdown_queues(cdev);
S
Stefan Raspl 已提交
1192
	qdio_shutdown_debug_entries(irq_ptr);
J
Jan Glauber 已提交
1193 1194

	/* cleanup subchannel */
1195
	spin_lock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1196 1197 1198 1199 1200 1201 1202

	if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
		rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
	else
		/* default behaviour is halt */
		rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
	if (rc) {
1203 1204
		DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4d", rc);
J
Jan Glauber 已提交
1205 1206 1207 1208
		goto no_cleanup;
	}

	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1209
	spin_unlock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1210 1211 1212 1213
	wait_event_interruptible_timeout(cdev->private->wait_q,
		irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
		irq_ptr->state == QDIO_IRQ_STATE_ERR,
		10 * HZ);
1214
	spin_lock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1215 1216 1217 1218 1219 1220 1221

no_cleanup:
	qdio_shutdown_thinint(irq_ptr);

	/* restore interrupt handler */
	if ((void *)cdev->handler == (void *)qdio_int_handler)
		cdev->handler = irq_ptr->orig_handler;
1222
	spin_unlock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
	mutex_unlock(&irq_ptr->setup_mutex);
	if (rc)
		return rc;
	return 0;
}
EXPORT_SYMBOL_GPL(qdio_shutdown);

/**
 * qdio_free - free data structures for a qdio subchannel
 * @cdev: associated ccw device
 */
int qdio_free(struct ccw_device *cdev)
{
1238
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1239
	struct subchannel_id schid;
1240

J
Jan Glauber 已提交
1241 1242 1243
	if (!irq_ptr)
		return -ENODEV;

1244 1245
	ccw_device_get_schid(cdev, &schid);
	DBF_EVENT("qfree:%4x", schid.sch_no);
1246
	DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
J
Jan Glauber 已提交
1247
	mutex_lock(&irq_ptr->setup_mutex);
1248

1249
	irq_ptr->debug_area = NULL;
J
Jan Glauber 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	cdev->private->qdio_data = NULL;
	mutex_unlock(&irq_ptr->setup_mutex);

	qdio_release_memory(irq_ptr);
	return 0;
}
EXPORT_SYMBOL_GPL(qdio_free);

/**
 * qdio_allocate - allocate qdio queues and associated data
 * @init_data: initialization data
 */
int qdio_allocate(struct qdio_initialize *init_data)
{
1264
	struct subchannel_id schid;
J
Jan Glauber 已提交
1265 1266
	struct qdio_irq *irq_ptr;

1267 1268
	ccw_device_get_schid(init_data->cdev, &schid);
	DBF_EVENT("qallocate:%4x", schid.sch_no);
J
Jan Glauber 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

	if ((init_data->no_input_qs && !init_data->input_handler) ||
	    (init_data->no_output_qs && !init_data->output_handler))
		return -EINVAL;

	if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
	    (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
		return -EINVAL;

	if ((!init_data->input_sbal_addr_array) ||
	    (!init_data->output_sbal_addr_array))
		return -EINVAL;

	/* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
	irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
	if (!irq_ptr)
		goto out_err;

	mutex_init(&irq_ptr->setup_mutex);
1288 1289
	if (qdio_allocate_dbf(init_data, irq_ptr))
		goto out_rel;
J
Jan Glauber 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301

	/*
	 * Allocate a page for the chsc calls in qdio_establish.
	 * Must be pre-allocated since a zfcp recovery will call
	 * qdio_establish. In case of low memory and swap on a zfcp disk
	 * we may not be able to allocate memory otherwise.
	 */
	irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
	if (!irq_ptr->chsc_page)
		goto out_rel;

	/* qdr is used in ccw1.cda which is u32 */
1302
	irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
J
Jan Glauber 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	if (!irq_ptr->qdr)
		goto out_rel;

	if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
			     init_data->no_output_qs))
		goto out_rel;

	init_data->cdev->private->qdio_data = irq_ptr;
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
	return 0;
out_rel:
	qdio_release_memory(irq_ptr);
out_err:
	return -ENOMEM;
}
EXPORT_SYMBOL_GPL(qdio_allocate);

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
{
	struct qdio_q *q = irq_ptr->input_qs[0];
	int i, use_cq = 0;

	if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
		use_cq = 1;

	for_each_output_queue(irq_ptr, q, i) {
		if (use_cq) {
			if (qdio_enable_async_operation(&q->u.out) < 0) {
				use_cq = 0;
				continue;
			}
		} else
			qdio_disable_async_operation(&q->u.out);
	}
	DBF_EVENT("use_cq:%d", use_cq);
}

J
Jan Glauber 已提交
1340 1341 1342 1343 1344 1345 1346
/**
 * qdio_establish - establish queues on a qdio subchannel
 * @init_data: initialization data
 */
int qdio_establish(struct qdio_initialize *init_data)
{
	struct ccw_device *cdev = init_data->cdev;
1347 1348
	struct subchannel_id schid;
	struct qdio_irq *irq_ptr;
J
Jan Glauber 已提交
1349 1350
	int rc;

1351 1352
	ccw_device_get_schid(cdev, &schid);
	DBF_EVENT("qestablish:%4x", schid.sch_no);
1353

J
Jan Glauber 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
	irq_ptr = cdev->private->qdio_data;
	if (!irq_ptr)
		return -ENODEV;

	mutex_lock(&irq_ptr->setup_mutex);
	qdio_setup_irq(init_data);

	rc = qdio_establish_thinint(irq_ptr);
	if (rc) {
		mutex_unlock(&irq_ptr->setup_mutex);
		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
		return rc;
	}

	/* establish q */
	irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
	irq_ptr->ccw.flags = CCW_FLAG_SLI;
	irq_ptr->ccw.count = irq_ptr->equeue.count;
	irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);

1374
	spin_lock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1375 1376 1377
	ccw_device_set_options_mask(cdev, 0);

	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1378
	spin_unlock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1379
	if (rc) {
1380 1381
		DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		mutex_unlock(&irq_ptr->setup_mutex);
		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
		return rc;
	}

	wait_event_interruptible_timeout(cdev->private->wait_q,
		irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
		irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);

	if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
		mutex_unlock(&irq_ptr->setup_mutex);
		qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
		return -EIO;
	}

	qdio_setup_ssqd_info(irq_ptr);

1399 1400
	qdio_detect_hsicq(irq_ptr);

J
Jan Glauber 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
	/* qebsm is now setup if available, initialize buffer states */
	qdio_init_buf_states(irq_ptr);

	mutex_unlock(&irq_ptr->setup_mutex);
	qdio_print_subchannel_info(irq_ptr, cdev);
	qdio_setup_debug_entries(irq_ptr, cdev);
	return 0;
}
EXPORT_SYMBOL_GPL(qdio_establish);

/**
 * qdio_activate - activate queues on a qdio subchannel
 * @cdev: associated cdev
 */
int qdio_activate(struct ccw_device *cdev)
{
1417
	struct subchannel_id schid;
J
Jan Glauber 已提交
1418 1419 1420
	struct qdio_irq *irq_ptr;
	int rc;

1421 1422
	ccw_device_get_schid(cdev, &schid);
	DBF_EVENT("qactivate:%4x", schid.sch_no);
1423

J
Jan Glauber 已提交
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	irq_ptr = cdev->private->qdio_data;
	if (!irq_ptr)
		return -ENODEV;

	mutex_lock(&irq_ptr->setup_mutex);
	if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
		rc = -EBUSY;
		goto out;
	}

	irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
	irq_ptr->ccw.flags = CCW_FLAG_SLI;
	irq_ptr->ccw.count = irq_ptr->aqueue.count;
	irq_ptr->ccw.cda = 0;

1439
	spin_lock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1440 1441 1442 1443
	ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);

	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
			      0, DOIO_DENY_PREFETCH);
1444
	spin_unlock_irq(get_ccwdev_lock(cdev));
J
Jan Glauber 已提交
1445
	if (rc) {
1446 1447
		DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1448
		goto out;
1449
	}
J
Jan Glauber 已提交
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459

	if (is_thinint_irq(irq_ptr))
		tiqdio_add_input_queues(irq_ptr);

	/* wait for subchannel to become active */
	msleep(5);

	switch (irq_ptr->state) {
	case QDIO_IRQ_STATE_STOPPED:
	case QDIO_IRQ_STATE_ERR:
1460 1461
		rc = -EIO;
		break;
J
Jan Glauber 已提交
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	default:
		qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
		rc = 0;
	}
out:
	mutex_unlock(&irq_ptr->setup_mutex);
	return rc;
}
EXPORT_SYMBOL_GPL(qdio_activate);

static inline int buf_in_between(int bufnr, int start, int count)
{
	int end = add_buf(start, count);

	if (end > start) {
		if (bufnr >= start && bufnr < end)
			return 1;
		else
			return 0;
	}

	/* wrap-around case */
	if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
	    (bufnr < end))
		return 1;
	else
		return 0;
}

/**
 * handle_inbound - reset processed input buffers
 * @q: queue containing the buffers
 * @callflags: flags
 * @bufnr: first buffer to process
 * @count: how many buffers are emptied
 */
1498 1499
static int handle_inbound(struct qdio_q *q, unsigned int callflags,
			  int bufnr, int count)
J
Jan Glauber 已提交
1500
{
1501
	int diff;
J
Jan Glauber 已提交
1502

1503 1504
	qperf_inc(q, inbound_call);

1505 1506 1507 1508 1509 1510 1511 1512 1513
	if (!q->u.in.polling)
		goto set;

	/* protect against stop polling setting an ACK for an emptied slsb */
	if (count == QDIO_MAX_BUFFERS_PER_Q) {
		/* overwriting everything, just delete polling status */
		q->u.in.polling = 0;
		q->u.in.ack_count = 0;
		goto set;
1514
	} else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1515
		if (is_qebsm(q)) {
1516
			/* partial overwrite, just update ack_start */
1517
			diff = add_buf(bufnr, count);
1518
			diff = sub_buf(diff, q->u.in.ack_start);
1519 1520 1521 1522 1523 1524
			q->u.in.ack_count -= diff;
			if (q->u.in.ack_count <= 0) {
				q->u.in.polling = 0;
				q->u.in.ack_count = 0;
				goto set;
			}
1525
			q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1526 1527 1528
		}
		else
			/* the only ACK will be deleted, so stop polling */
J
Jan Glauber 已提交
1529
			q->u.in.polling = 0;
1530
	}
J
Jan Glauber 已提交
1531

1532
set:
J
Jan Glauber 已提交
1533
	count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1534
	atomic_add(count, &q->nr_buf_used);
J
Jan Glauber 已提交
1535

1536 1537
	if (need_siga_in(q))
		return qdio_siga_input(q);
1538

1539
	return 0;
J
Jan Glauber 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548
}

/**
 * handle_outbound - process filled outbound buffers
 * @q: queue containing the buffers
 * @callflags: flags
 * @bufnr: first buffer to process
 * @count: how many buffers are filled
 */
1549 1550
static int handle_outbound(struct qdio_q *q, unsigned int callflags,
			   int bufnr, int count)
J
Jan Glauber 已提交
1551
{
1552
	unsigned char state = 0;
1553
	int used, rc = 0;
J
Jan Glauber 已提交
1554

1555
	qperf_inc(q, outbound_call);
J
Jan Glauber 已提交
1556 1557 1558 1559

	count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
	used = atomic_add_return(count, &q->nr_buf_used);

1560 1561 1562
	if (used == QDIO_MAX_BUFFERS_PER_Q)
		qperf_inc(q, outbound_queue_full);

1563
	if (callflags & QDIO_FLAG_PCI_OUT) {
J
Jan Glauber 已提交
1564
		q->u.out.pci_out_enabled = 1;
1565
		qperf_inc(q, pci_request_int);
J
Jan Glauber 已提交
1566
	} else
J
Jan Glauber 已提交
1567 1568 1569
		q->u.out.pci_out_enabled = 0;

	if (queue_type(q) == QDIO_IQDIO_QFMT) {
1570 1571 1572
		unsigned long phys_aob = 0;

		/* One SIGA-W per buffer required for unicast HSI */
J
Jan Glauber 已提交
1573 1574
		WARN_ON_ONCE(count > 1 && !multicast_outbound(q));

1575 1576 1577
		phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);

		rc = qdio_kick_outbound_q(q, phys_aob);
J
Jan Glauber 已提交
1578
	} else if (need_siga_sync(q)) {
J
Jan Glauber 已提交
1579 1580 1581 1582 1583
		rc = qdio_siga_sync_q(q);
	} else {
		/* try to fast requeue buffers */
		get_buf_state(q, prev_buf(bufnr), &state, 0);
		if (state != SLSB_CU_OUTPUT_PRIMED)
1584
			rc = qdio_kick_outbound_q(q, 0);
J
Jan Glauber 已提交
1585
		else
J
Jan Glauber 已提交
1586
			qperf_inc(q, fast_requeue);
J
Jan Glauber 已提交
1587 1588
	}

1589 1590
	/* in case of SIGA errors we must process the error immediately */
	if (used >= q->u.out.scan_threshold || rc)
1591
		qdio_tasklet_schedule(q);
1592 1593
	else
		/* free the SBALs in case of no further traffic */
1594 1595
		if (!timer_pending(&q->u.out.timer) &&
		    likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
1596
			mod_timer(&q->u.out.timer, jiffies + HZ);
1597
	return rc;
J
Jan Glauber 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
}

/**
 * do_QDIO - process input or output buffers
 * @cdev: associated ccw_device for the qdio subchannel
 * @callflags: input or output and special flags from the program
 * @q_nr: queue number
 * @bufnr: buffer number
 * @count: how many buffers to process
 */
int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1609
	    int q_nr, unsigned int bufnr, unsigned int count)
J
Jan Glauber 已提交
1610 1611 1612
{
	struct qdio_irq *irq_ptr;

1613
	if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
J
Jan Glauber 已提交
1614 1615 1616 1617 1618 1619
		return -EINVAL;

	irq_ptr = cdev->private->qdio_data;
	if (!irq_ptr)
		return -ENODEV;

1620 1621
	DBF_DEV_EVENT(DBF_INFO, irq_ptr,
		      "do%02x b:%02x c:%02x", callflags, bufnr, count);
J
Jan Glauber 已提交
1622 1623

	if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1624
		return -EIO;
1625 1626
	if (!count)
		return 0;
J
Jan Glauber 已提交
1627
	if (callflags & QDIO_FLAG_SYNC_INPUT)
1628 1629
		return handle_inbound(irq_ptr->input_qs[q_nr],
				      callflags, bufnr, count);
J
Jan Glauber 已提交
1630
	else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1631 1632 1633
		return handle_outbound(irq_ptr->output_qs[q_nr],
				       callflags, bufnr, count);
	return -EINVAL;
J
Jan Glauber 已提交
1634 1635 1636
}
EXPORT_SYMBOL_GPL(do_QDIO);

J
Jan Glauber 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
/**
 * qdio_start_irq - process input buffers
 * @cdev: associated ccw_device for the qdio subchannel
 * @nr: input queue number
 *
 * Return codes
 *   0 - success
 *   1 - irqs not started since new data is available
 */
int qdio_start_irq(struct ccw_device *cdev, int nr)
{
	struct qdio_q *q;
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

	if (!irq_ptr)
		return -ENODEV;
	q = irq_ptr->input_qs[nr];

1655
	clear_nonshared_ind(irq_ptr);
J
Jan Glauber 已提交
1656 1657 1658 1659 1660 1661 1662
	qdio_stop_polling(q);
	clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);

	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
1663
	if (test_nonshared_ind(irq_ptr))
J
Jan Glauber 已提交
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
		goto rescan;
	if (!qdio_inbound_q_done(q))
		goto rescan;
	return 0;

rescan:
	if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
			     &q->u.in.queue_irq_state))
		return 0;
	else
		return 1;

}
EXPORT_SYMBOL(qdio_start_irq);

/**
 * qdio_get_next_buffers - process input buffers
 * @cdev: associated ccw_device for the qdio subchannel
 * @nr: input queue number
 * @bufnr: first filled buffer number
 * @error: buffers are in error state
 *
 * Return codes
 *   < 0 - error
 *   = 0 - no new buffers found
 *   > 0 - number of processed buffers
 */
int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
			  int *error)
{
	struct qdio_q *q;
	int start, end;
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

	if (!irq_ptr)
		return -ENODEV;
	q = irq_ptr->input_qs[nr];

	/*
J
Jan Glauber 已提交
1703 1704
	 * Cannot rely on automatic sync after interrupt since queues may
	 * also be examined without interrupt.
J
Jan Glauber 已提交
1705
	 */
J
Jan Glauber 已提交
1706 1707 1708 1709
	if (need_siga_sync(q))
		qdio_sync_queues(q);

	/* check the PCI capable outbound queues. */
J
Jan Glauber 已提交
1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
	qdio_check_outbound_after_thinint(q);

	if (!qdio_inbound_q_moved(q))
		return 0;

	/* Note: upper-layer MUST stop processing immediately here ... */
	if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
		return -EIO;

	start = q->first_to_kick;
	end = q->first_to_check;
	*bufnr = start;
	*error = q->qdio_error;

	/* for the next time */
	q->first_to_kick = end;
	q->qdio_error = 0;
	return sub_buf(end, start);
}
EXPORT_SYMBOL(qdio_get_next_buffers);

/**
 * qdio_stop_irq - disable interrupt processing for the device
 * @cdev: associated ccw_device for the qdio subchannel
 * @nr: input queue number
 *
 * Return codes
 *   0 - interrupts were already disabled
 *   1 - interrupts successfully disabled
 */
int qdio_stop_irq(struct ccw_device *cdev, int nr)
{
	struct qdio_q *q;
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

	if (!irq_ptr)
		return -ENODEV;
	q = irq_ptr->input_qs[nr];

	if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
			     &q->u.in.queue_irq_state))
		return 0;
	else
		return 1;
}
EXPORT_SYMBOL(qdio_stop_irq);

1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847
/**
 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
 * @schid:		Subchannel ID.
 * @cnc:		Boolean Change-Notification Control
 * @response:		Response code will be stored at this address
 * @cb: 		Callback function will be executed for each element
 *			of the address list
 * @priv:		Pointer passed from the caller to qdio_pnso_brinfo()
 * @type:		Type of the address entry passed to the callback
 * @entry:		Entry containg the address of the specified type
 * @priv:		Pointer to pass to the callback function.
 *
 * Performs "Store-network-bridging-information list" operation and calls
 * the callback function for every entry in the list. If "change-
 * notification-control" is set, further changes in the address list
 * will be reported via the IPA command.
 */
int qdio_pnso_brinfo(struct subchannel_id schid,
		int cnc, u16 *response,
		void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
				void *entry),
		void *priv)
{
	struct chsc_pnso_area *rr;
	int rc;
	u32 prev_instance = 0;
	int isfirstblock = 1;
	int i, size, elems;

	rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
	if (rr == NULL)
		return -ENOMEM;
	do {
		/* on the first iteration, naihdr.resume_token will be zero */
		rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
		if (rc != 0 && rc != -EBUSY)
			goto out;
		if (rr->response.code != 1) {
			rc = -EIO;
			continue;
		} else
			rc = 0;

		if (cb == NULL)
			continue;

		size = rr->naihdr.naids;
		elems = (rr->response.length -
				sizeof(struct chsc_header) -
				sizeof(struct chsc_brinfo_naihdr)) /
				size;

		if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
			/* Inform the caller that they need to scrap */
			/* the data that was already reported via cb */
				rc = -EAGAIN;
				break;
		}
		isfirstblock = 0;
		prev_instance = rr->naihdr.instance;
		for (i = 0; i < elems; i++)
			switch (size) {
			case sizeof(struct qdio_brinfo_entry_l3_ipv6):
				(*cb)(priv, l3_ipv6_addr,
						&rr->entries.l3_ipv6[i]);
				break;
			case sizeof(struct qdio_brinfo_entry_l3_ipv4):
				(*cb)(priv, l3_ipv4_addr,
						&rr->entries.l3_ipv4[i]);
				break;
			case sizeof(struct qdio_brinfo_entry_l2):
				(*cb)(priv, l2_addr_lnid,
						&rr->entries.l2[i]);
				break;
			default:
				WARN_ON_ONCE(1);
				rc = -EIO;
				goto out;
			}
	} while (rr->response.code == 0x0107 ||  /* channel busy */
		  (rr->response.code == 1 && /* list stored */
		   /* resume token is non-zero => list incomplete */
		   (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
	(*response) = rr->response.code;

out:
	free_page((unsigned long)rr);
	return rc;
}
EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);

J
Jan Glauber 已提交
1848 1849 1850 1851
static int __init init_QDIO(void)
{
	int rc;

S
Sebastian Ott 已提交
1852
	rc = qdio_debug_init();
J
Jan Glauber 已提交
1853 1854
	if (rc)
		return rc;
S
Sebastian Ott 已提交
1855 1856 1857
	rc = qdio_setup_init();
	if (rc)
		goto out_debug;
J
Jan Glauber 已提交
1858 1859 1860 1861 1862
	rc = tiqdio_allocate_memory();
	if (rc)
		goto out_cache;
	rc = tiqdio_register_thinints();
	if (rc)
S
Sebastian Ott 已提交
1863
		goto out_ti;
J
Jan Glauber 已提交
1864 1865 1866 1867 1868 1869
	return 0;

out_ti:
	tiqdio_free_memory();
out_cache:
	qdio_setup_exit();
S
Sebastian Ott 已提交
1870 1871
out_debug:
	qdio_debug_exit();
J
Jan Glauber 已提交
1872 1873 1874 1875 1876 1877 1878 1879
	return rc;
}

static void __exit exit_QDIO(void)
{
	tiqdio_unregister_thinints();
	tiqdio_free_memory();
	qdio_setup_exit();
S
Sebastian Ott 已提交
1880
	qdio_debug_exit();
J
Jan Glauber 已提交
1881 1882 1883 1884
}

module_init(init_QDIO);
module_exit(exit_QDIO);