qdio_main.c 36.9 KB
Newer Older
J
Jan Glauber 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * linux/drivers/s390/cio/qdio_main.c
 *
 * Linux for s390 qdio support, buffer handling, qdio API and module support.
 *
 * Copyright 2000,2008 IBM Corp.
 * 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>
16
#include <linux/gfp.h>
J
Jan Glauber 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <asm/atomic.h>
#include <asm/debug.h>
#include <asm/qdio.h>

#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");

static inline int do_siga_sync(struct subchannel_id schid,
			       unsigned int out_mask, unsigned int in_mask)
{
	register unsigned long __fc asm ("0") = 2;
	register struct subchannel_id __schid asm ("1") = schid;
	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;
}

static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
{
	register unsigned long __fc asm ("0") = 1;
	register struct subchannel_id __schid asm ("1") = schid;
	register unsigned long __mask asm ("2") = mask;
	int cc;

	asm volatile(
		"	siga	0\n"
		"	ipm	%0\n"
		"	srl	%0,28\n"
		: "=d" (cc)
		: "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
	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
 *
 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
 * Note: For IQDC unicast queues only the highest priority queue is processed.
 */
static inline int do_siga_output(unsigned long schid, unsigned long mask,
77
				 unsigned int *bb, unsigned int fc)
J
Jan Glauber 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	register unsigned long __fc asm("0") = fc;
	register unsigned long __schid asm("1") = schid;
	register unsigned long __mask asm("2") = mask;
	int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;

	asm volatile(
		"	siga	0\n"
		"0:	ipm	%0\n"
		"	srl	%0,28\n"
		"1:\n"
		EX_TABLE(0b, 1b)
		: "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
		: : "cc", "memory");
	*bb = ((unsigned int) __fc) >> 31;
	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;
	/* not all buffers processed */
	if (ccq == 96 || ccq == 97)
		return 1;
	/* notify devices immediately */
105
	DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
J
Jan Glauber 已提交
106 107 108 109 110 111 112 113 114
	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
115
 * @auto_ack: automatically acknowledge buffers
J
Jan Glauber 已提交
116
 *
C
Coly Li 已提交
117
 * Returns the number of successfully extracted equal buffer states.
J
Jan Glauber 已提交
118 119 120
 * Stops processing if a state is different from the last buffers state.
 */
static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
121
			int start, int count, int auto_ack)
J
Jan Glauber 已提交
122 123 124 125 126 127 128
{
	unsigned int ccq = 0;
	int tmp_count = count, tmp_start = start;
	int nr = q->nr;
	int rc;

	BUG_ON(!q->irq_ptr->sch_token);
129
	qperf_inc(q, eqbs);
J
Jan Glauber 已提交
130 131 132 133

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

	/* At least one buffer was processed, return and extract the remaining
	 * buffers later.
	 */
141
	if ((ccq == 96) && (count != tmp_count)) {
142
		qperf_inc(q, eqbs_partial);
J
Jan Glauber 已提交
143
		return (count - tmp_count);
144
	}
145

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

	if (rc < 0) {
152 153
		DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
J
Jan Glauber 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		q->handler(q->irq_ptr->cdev,
			   QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
			   0, -1, -1, q->irq_ptr->int_parm);
		return 0;
	}
	return count - tmp_count;
}

/**
 * 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;

181 182 183
	if (!count)
		return 0;

J
Jan Glauber 已提交
184
	BUG_ON(!q->irq_ptr->sch_token);
185
	qperf_inc(q, sqbs);
J
Jan Glauber 已提交
186 187 188 189 190 191 192

	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);
	if (rc == 1) {
193
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
194
		qperf_inc(q, sqbs_partial);
J
Jan Glauber 已提交
195 196 197
		goto again;
	}
	if (rc < 0) {
198 199
		DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
J
Jan Glauber 已提交
200 201 202 203 204 205 206 207 208 209 210
		q->handler(q->irq_ptr->cdev,
			   QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
			   0, -1, -1, q->irq_ptr->int_parm);
		return 0;
	}
	WARN_ON(tmp_count);
	return count - tmp_count;
}

/* returns number of examined buffers and their common state in *state */
static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
211 212
				 unsigned char *state, unsigned int count,
				 int auto_ack)
J
Jan Glauber 已提交
213 214 215 216 217 218 219 220
{
	unsigned char __state = 0;
	int i;

	BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
	BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);

	if (is_qebsm(q))
221
		return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
J
Jan Glauber 已提交
222 223 224 225 226 227 228 229 230 231 232 233

	for (i = 0; i < count; i++) {
		if (!__state)
			__state = q->slsb.val[bufnr];
		else if (q->slsb.val[bufnr] != __state)
			break;
		bufnr = next_buf(bufnr);
	}
	*state = __state;
	return i;
}

234 235
static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
				unsigned char *state, int auto_ack)
J
Jan Glauber 已提交
236
{
237
	return get_buf_states(q, bufnr, state, 1, auto_ack);
J
Jan Glauber 已提交
238 239 240 241 242 243 244 245 246 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 272 273 274 275 276 277 278
}

/* 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;

	BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
	BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);

	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 */
void qdio_init_buf_states(struct qdio_irq *irq_ptr)
{
	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);
}

279
static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
J
Jan Glauber 已提交
280 281 282 283 284 285 286
			  unsigned int input)
{
	int cc;

	if (!need_siga_sync(q))
		return 0;

287
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
288
	qperf_inc(q, siga_sync);
J
Jan Glauber 已提交
289 290

	cc = do_siga_sync(q->irq_ptr->schid, output, input);
291 292
	if (cc)
		DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
J
Jan Glauber 已提交
293 294 295
	return cc;
}

296
static inline int qdio_siga_sync_q(struct qdio_q *q)
J
Jan Glauber 已提交
297 298 299 300 301 302 303 304 305 306 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);
}

static inline int qdio_siga_sync_out(struct qdio_q *q)
{
	return qdio_siga_sync(q, ~0U, 0);
}

static inline int qdio_siga_sync_all(struct qdio_q *q)
{
	return qdio_siga_sync(q, ~0U, ~0U);
}

314
static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
J
Jan Glauber 已提交
315 316
{
	unsigned long schid;
317 318 319
	unsigned int fc = 0;
	u64 start_time = 0;
	int cc;
J
Jan Glauber 已提交
320

321
	if (q->u.out.use_enh_siga)
322
		fc = 3;
323 324

	if (is_qebsm(q)) {
J
Jan Glauber 已提交
325 326 327
		schid = q->irq_ptr->sch_token;
		fc |= 0x80;
	}
328 329
	else
		schid = *((u32 *)&q->irq_ptr->schid);
J
Jan Glauber 已提交
330 331

again:
332 333 334 335 336
	cc = do_siga_output(schid, q->mask, busy_bit, fc);

	/* hipersocket busy condition */
	if (*busy_bit) {
		WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
337

338
		if (!start_time) {
339
			start_time = get_clock();
340 341
			goto again;
		}
342
		if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
J
Jan Glauber 已提交
343 344 345 346 347 348 349 350 351
			goto again;
	}
	return cc;
}

static inline int qdio_siga_input(struct qdio_q *q)
{
	int cc;

352
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
353
	qperf_inc(q, siga_read);
J
Jan Glauber 已提交
354 355 356

	cc = do_siga_input(q->irq_ptr->schid, q->mask);
	if (cc)
357
		DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
J
Jan Glauber 已提交
358 359 360
	return cc;
}

361
static inline void qdio_sync_after_thinint(struct qdio_q *q)
J
Jan Glauber 已提交
362 363 364 365 366 367 368 369 370 371
{
	if (pci_out_supported(q)) {
		if (need_siga_sync_thinint(q))
			qdio_siga_sync_all(q);
		else if (need_siga_sync_out_thinint(q))
			qdio_siga_sync_out(q);
	} else
		qdio_siga_sync_q(q);
}

372 373 374 375 376 377 378 379
int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
			unsigned char *state)
{
	qdio_siga_sync_q(q);
	return get_buf_states(q, bufnr, state, 1, 0);
}

static inline void qdio_stop_polling(struct qdio_q *q)
J
Jan Glauber 已提交
380
{
381
	if (!q->u.in.polling)
J
Jan Glauber 已提交
382
		return;
383

J
Jan Glauber 已提交
384
	q->u.in.polling = 0;
385
	qperf_inc(q, stop_polling);
J
Jan Glauber 已提交
386 387

	/* show the card that we are not polling anymore */
388
	if (is_qebsm(q)) {
389
		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
390 391 392
			       q->u.in.ack_count);
		q->u.in.ack_count = 0;
	} else
393
		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
J
Jan Glauber 已提交
394 395
}

396 397 398 399 400 401 402 403 404 405 406 407 408 409
static inline void account_sbals(struct qdio_q *q, int count)
{
	int pos = 0;

	q->q_stats.nr_sbal_total += count;
	if (count == QDIO_MAX_BUFFERS_MASK) {
		q->q_stats.nr_sbals[7]++;
		return;
	}
	while (count >>= 1)
		pos++;
	q->q_stats.nr_sbals[pos]++;
}

410
static void announce_buffer_error(struct qdio_q *q, int count)
J
Jan Glauber 已提交
411
{
412
	q->qdio_error |= QDIO_ERROR_SLSB_STATE;
413 414 415 416

	/* special handling for no target buffer empty */
	if ((!q->is_input_q &&
	    (q->sbal[q->first_to_check]->element[15].flags & 0xff) == 0x10)) {
417
		qperf_inc(q, target_full);
418
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
419 420 421 422
			      q->first_to_check);
		return;
	}

423 424
	DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
	DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
425
	DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
426 427 428
	DBF_ERROR("F14:%2x F15:%2x",
		  q->sbal[q->first_to_check]->element[14].flags & 0xff,
		  q->sbal[q->first_to_check]->element[15].flags & 0xff);
429
}
J
Jan Glauber 已提交
430

431 432 433 434
static inline void inbound_primed(struct qdio_q *q, int count)
{
	int new;

435
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
436 437 438 439 440 441

	/* 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;
442
			q->u.in.ack_start = q->first_to_check;
443 444 445 446
			return;
		}

		/* delete the previous ACK's */
447
		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
448 449
			       q->u.in.ack_count);
		q->u.in.ack_count = count;
450
		q->u.in.ack_start = q->first_to_check;
451 452 453 454 455 456 457 458 459 460 461
		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);
462
		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
463
	} else {
464
		q->u.in.polling = 1;
465
		set_buf_state(q, new, SLSB_P_INPUT_ACK);
466 467
	}

468
	q->u.in.ack_start = new;
469 470 471
	count--;
	if (!count)
		return;
472 473
	/* 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 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
}

static int get_inbound_buffer_frontier(struct qdio_q *q)
{
	int count, stop;
	unsigned char state;

	/*
	 * 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;

491 492 493 494
	/*
	 * No siga sync here, as a PCI or we after a thin interrupt
	 * already sync'ed the queues.
	 */
495
	count = get_buf_states(q, q->first_to_check, &state, count, 1);
J
Jan Glauber 已提交
496 497 498 499 500
	if (!count)
		goto out;

	switch (state) {
	case SLSB_P_INPUT_PRIMED:
501
		inbound_primed(q, count);
J
Jan Glauber 已提交
502
		q->first_to_check = add_buf(q->first_to_check, count);
503
		if (atomic_sub(count, &q->nr_buf_used) == 0)
504
			qperf_inc(q, inbound_queue_full);
505 506
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
507
		break;
J
Jan Glauber 已提交
508
	case SLSB_P_INPUT_ERROR:
509
		announce_buffer_error(q, count);
J
Jan Glauber 已提交
510 511 512
		/* process the buffer, the upper layer will take care of it */
		q->first_to_check = add_buf(q->first_to_check, count);
		atomic_sub(count, &q->nr_buf_used);
513 514
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
515 516 517 518
		break;
	case SLSB_CU_INPUT_EMPTY:
	case SLSB_P_INPUT_NOT_INIT:
	case SLSB_P_INPUT_ACK:
519 520
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
521
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
J
Jan Glauber 已提交
522 523 524 525 526 527 528 529
		break;
	default:
		BUG();
	}
out:
	return q->first_to_check;
}

530
static int qdio_inbound_q_moved(struct qdio_q *q)
J
Jan Glauber 已提交
531 532 533 534 535
{
	int bufnr;

	bufnr = get_inbound_buffer_frontier(q);

536 537
	if ((bufnr != q->last_move) || q->qdio_error) {
		q->last_move = bufnr;
538
		if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
539
			q->u.in.timestamp = get_clock();
J
Jan Glauber 已提交
540 541 542 543 544
		return 1;
	} else
		return 0;
}

545
static inline int qdio_inbound_q_done(struct qdio_q *q)
J
Jan Glauber 已提交
546
{
547
	unsigned char state = 0;
J
Jan Glauber 已提交
548 549 550 551 552

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

	qdio_siga_sync_q(q);
553
	get_buf_state(q, q->first_to_check, &state, 0);
554

555
	if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
556
		/* more work coming */
J
Jan Glauber 已提交
557 558
		return 0;

559 560 561 562 563
	if (is_thinint_irq(q->irq_ptr))
		return 1;

	/* don't poll under z/VM */
	if (MACHINE_IS_VM)
J
Jan Glauber 已提交
564 565 566 567 568 569
		return 1;

	/*
	 * At this point we know, that inbound first_to_check
	 * has (probably) not moved (see qdio_inbound_processing).
	 */
570
	if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
571
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
572
			      q->first_to_check);
J
Jan Glauber 已提交
573
		return 1;
574
	} else
575 576 577 578
		return 0;
}

static void qdio_kick_handler(struct qdio_q *q)
J
Jan Glauber 已提交
579
{
580 581 582
	int start = q->first_to_kick;
	int end = q->first_to_check;
	int count;
J
Jan Glauber 已提交
583 584 585 586

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

587 588 589
	count = sub_buf(end, start);

	if (q->is_input_q) {
590
		qperf_inc(q, inbound_handler);
591
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
U
Ursula Braun 已提交
592
	} else {
593
		qperf_inc(q, outbound_handler);
594 595
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
			      start, count);
U
Ursula Braun 已提交
596
	}
597 598 599

	q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
		   q->irq_ptr->int_parm);
J
Jan Glauber 已提交
600 601

	/* for the next time */
602
	q->first_to_kick = end;
J
Jan Glauber 已提交
603 604 605 606 607
	q->qdio_error = 0;
}

static void __qdio_inbound_processing(struct qdio_q *q)
{
608
	qperf_inc(q, tasklet_inbound);
609

J
Jan Glauber 已提交
610 611 612
	if (!qdio_inbound_q_moved(q))
		return;

613
	qdio_kick_handler(q);
J
Jan Glauber 已提交
614

615
	if (!qdio_inbound_q_done(q)) {
J
Jan Glauber 已提交
616
		/* means poll time is not yet over */
617
		qperf_inc(q, tasklet_inbound_resched);
618 619 620 621
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
			tasklet_schedule(&q->tasklet);
			return;
		}
622
	}
J
Jan Glauber 已提交
623 624 625 626 627 628

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
629 630
	if (!qdio_inbound_q_done(q)) {
		qperf_inc(q, tasklet_inbound_resched2);
631 632
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
			tasklet_schedule(&q->tasklet);
633
	}
J
Jan Glauber 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
}

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;
	unsigned char state;

	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);

	/*
	 * 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)
		return q->first_to_check;

661
	count = get_buf_states(q, q->first_to_check, &state, count, 0);
J
Jan Glauber 已提交
662 663 664 665 666 667
	if (!count)
		return q->first_to_check;

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

		atomic_sub(count, &q->nr_buf_used);
		q->first_to_check = add_buf(q->first_to_check, count);
672 673
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
674
		break;
J
Jan Glauber 已提交
675
	case SLSB_P_OUTPUT_ERROR:
676
		announce_buffer_error(q, count);
J
Jan Glauber 已提交
677 678 679
		/* process the buffer, the upper layer will take care of it */
		q->first_to_check = add_buf(q->first_to_check, count);
		atomic_sub(count, &q->nr_buf_used);
680 681
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
682 683 684
		break;
	case SLSB_CU_OUTPUT_PRIMED:
		/* the adapter has not fetched the output yet */
685 686
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
687
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
J
Jan Glauber 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
		break;
	case SLSB_P_OUTPUT_NOT_INIT:
	case SLSB_P_OUTPUT_HALTED:
		break;
	default:
		BUG();
	}
	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);

710 711
	if ((bufnr != q->last_move) || q->qdio_error) {
		q->last_move = bufnr;
712
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
J
Jan Glauber 已提交
713 714 715 716 717
		return 1;
	} else
		return 0;
}

718
static int qdio_kick_outbound_q(struct qdio_q *q)
J
Jan Glauber 已提交
719
{
720 721
	unsigned int busy_bit;
	int cc;
J
Jan Glauber 已提交
722 723

	if (!need_siga_out(q))
724
		return 0;
J
Jan Glauber 已提交
725

726
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
727
	qperf_inc(q, siga_write);
728 729 730

	cc = qdio_siga_output(q, &busy_bit);
	switch (cc) {
J
Jan Glauber 已提交
731 732
	case 0:
		break;
733 734 735
	case 2:
		if (busy_bit) {
			DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
736 737 738
			cc |= QDIO_ERROR_SIGA_BUSY;
		} else
			DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
739 740 741 742 743
		break;
	case 1:
	case 3:
		DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
		break;
J
Jan Glauber 已提交
744
	}
745
	return cc;
J
Jan Glauber 已提交
746 747 748 749
}

static void __qdio_outbound_processing(struct qdio_q *q)
{
750
	qperf_inc(q, tasklet_outbound);
J
Jan Glauber 已提交
751 752 753
	BUG_ON(atomic_read(&q->nr_buf_used) < 0);

	if (qdio_outbound_q_moved(q))
754
		qdio_kick_handler(q);
J
Jan Glauber 已提交
755

756
	if (queue_type(q) == QDIO_ZFCP_QFMT)
J
Jan Glauber 已提交
757
		if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
758
			goto sched;
J
Jan Glauber 已提交
759 760 761 762 763

	/* bail out for HiperSockets unicast queues */
	if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
		return;

764
	if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
765 766
	    (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
		goto sched;
767

J
Jan Glauber 已提交
768 769 770 771 772 773 774 775 776 777
	if (q->u.out.pci_out_enabled)
		return;

	/*
	 * Now we know that queue type is either qeth without pci enabled
	 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
	 * EMPTY is noticed and outbound_handler is called after some time.
	 */
	if (qdio_outbound_q_done(q))
		del_timer(&q->u.out.timer);
778 779
	else
		if (!timer_pending(&q->u.out.timer))
J
Jan Glauber 已提交
780
			mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
781 782 783 784 785 786
	return;

sched:
	if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;
	tasklet_schedule(&q->tasklet);
J
Jan Glauber 已提交
787 788 789 790 791 792 793 794 795 796 797 798
}

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

void qdio_outbound_timer(unsigned long data)
{
	struct qdio_q *q = (struct qdio_q *)data;
799 800 801

	if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;
J
Jan Glauber 已提交
802 803 804
	tasklet_schedule(&q->tasklet);
}

805
static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
J
Jan Glauber 已提交
806 807 808 809 810 811 812 813 814 815 816 817
{
	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))
			tasklet_schedule(&out->tasklet);
}

818 819
static void __tiqdio_inbound_processing(struct qdio_q *q)
{
820
	qperf_inc(q, tasklet_inbound);
821 822 823 824 825 826 827 828 829 830 831 832 833
	qdio_sync_after_thinint(q);

	/*
	 * 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);

834
	if (!qdio_inbound_q_done(q)) {
835
		qperf_inc(q, tasklet_inbound_resched);
836
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
837
			tasklet_schedule(&q->tasklet);
838 839
			return;
		}
840 841 842 843 844 845 846
	}

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
847
	if (!qdio_inbound_q_done(q)) {
848
		qperf_inc(q, tasklet_inbound_resched2);
849 850 851 852 853 854 855 856 857 858 859
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
			tasklet_schedule(&q->tasklet);
	}
}

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

J
Jan Glauber 已提交
860 861 862
static inline void qdio_set_state(struct qdio_irq *irq_ptr,
				  enum qdio_irq_states state)
{
863
	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
J
Jan Glauber 已提交
864 865 866 867 868

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

869
static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
J
Jan Glauber 已提交
870 871
{
	if (irb->esw.esw0.erw.cons) {
872 873 874
		DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
		DBF_ERROR_HEX(irb, 64);
		DBF_ERROR_HEX(irb->ecw, 64);
J
Jan Glauber 已提交
875 876 877 878 879 880 881 882 883
	}
}

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

884 885 886
	if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;

J
Jan Glauber 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	for_each_input_queue(irq_ptr, q, i)
		tasklet_schedule(&q->tasklet);

	if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
		return;

	for_each_output_queue(irq_ptr, q, i) {
		if (qdio_outbound_q_done(q))
			continue;

		if (!siga_syncs_out_pci(q))
			qdio_siga_sync_q(q);

		tasklet_schedule(&q->tasklet);
	}
}

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;

910 911 912
	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 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927

	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;
	}
	q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
		   0, -1, -1, irq_ptr->int_parm);
no_handler:
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
}

928 929
static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
				      int dstat)
J
Jan Glauber 已提交
930 931 932
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

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

935
	if (cstat)
J
Jan Glauber 已提交
936
		goto error;
937
	if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
J
Jan Glauber 已提交
938
		goto error;
939 940 941 942 943
	if (!(dstat & DEV_STAT_DEV_END))
		goto error;
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
	return;

J
Jan Glauber 已提交
944
error:
945 946
	DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
J
Jan Glauber 已提交
947 948 949 950 951 952 953 954 955 956 957
	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;
	int cstat, dstat;

	if (!intparm || !irq_ptr) {
958
		DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
J
Jan Glauber 已提交
959 960 961
		return;
	}

962 963 964
	if (irq_ptr->perf_stat_enabled)
		irq_ptr->perf_stat.qdio_int++;

J
Jan Glauber 已提交
965 966 967
	if (IS_ERR(irb)) {
		switch (PTR_ERR(irb)) {
		case -EIO:
968
			DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
969 970
			qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
			wake_up(&cdev->private->wait_q);
J
Jan Glauber 已提交
971 972 973 974 975 976
			return;
		default:
			WARN_ON(1);
			return;
		}
	}
977
	qdio_irq_check_sense(irq_ptr, irb);
J
Jan Glauber 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	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;
		}
994
		if (cstat || dstat)
J
Jan Glauber 已提交
995 996
			qdio_handle_activate_check(cdev, intparm, cstat,
						   dstat);
997
		break;
998 999
	case QDIO_IRQ_STATE_STOPPED:
		break;
J
Jan Glauber 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008
	default:
		WARN_ON(1);
	}
	wake_up(&cdev->private->wait_q);
}

/**
 * qdio_get_ssqd_desc - get qdio subchannel description
 * @cdev: ccw device to get description for
1009
 * @data: where to store the ssqd
J
Jan Glauber 已提交
1010
 *
1011 1012
 * Returns 0 or an error code. The results of the chsc are stored in the
 * specified structure.
J
Jan Glauber 已提交
1013
 */
1014 1015
int qdio_get_ssqd_desc(struct ccw_device *cdev,
		       struct qdio_ssqd_desc *data)
J
Jan Glauber 已提交
1016 1017
{

1018 1019 1020
	if (!cdev || !cdev->private)
		return -EINVAL;

1021
	DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
1022
	return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
J
Jan Glauber 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
}
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)
1033
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1034 1035 1036

	for_each_output_queue(irq_ptr, q, i) {
		del_timer(&q->u.out.timer);
1037
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	}
}

/**
 * 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)
{
1048
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
J
Jan Glauber 已提交
1049 1050 1051 1052 1053 1054
	int rc;
	unsigned long flags;

	if (!irq_ptr)
		return -ENODEV;

1055
	BUG_ON(irqs_disabled());
1056 1057
	DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);

J
Jan Glauber 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	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;
	}

1068 1069 1070 1071 1072 1073
	/*
	 * 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 已提交
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	tiqdio_remove_input_queues(irq_ptr);
	qdio_shutdown_queues(cdev);
	qdio_shutdown_debug_entries(irq_ptr, cdev);

	/* cleanup subchannel */
	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);

	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) {
1087 1088
		DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4d", rc);
J
Jan Glauber 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
		goto no_cleanup;
	}

	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
	wait_event_interruptible_timeout(cdev->private->wait_q,
		irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
		irq_ptr->state == QDIO_IRQ_STATE_ERR,
		10 * HZ);
	spin_lock_irqsave(get_ccwdev_lock(cdev), flags);

no_cleanup:
	qdio_shutdown_thinint(irq_ptr);

	/* restore interrupt handler */
	if ((void *)cdev->handler == (void *)qdio_int_handler)
		cdev->handler = irq_ptr->orig_handler;
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);

	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)
{
1122
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1123

J
Jan Glauber 已提交
1124 1125 1126
	if (!irq_ptr)
		return -ENODEV;

1127
	DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
J
Jan Glauber 已提交
1128
	mutex_lock(&irq_ptr->setup_mutex);
1129 1130 1131 1132 1133

	if (irq_ptr->debug_area != NULL) {
		debug_unregister(irq_ptr->debug_area);
		irq_ptr->debug_area = NULL;
	}
J
Jan Glauber 已提交
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	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)
{
	struct qdio_irq *irq_ptr;

1150
	DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
J
Jan Glauber 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

	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);
1170
	qdio_allocate_dbf(init_data, irq_ptr);
J
Jan Glauber 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

	/*
	 * 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 */
1183
	irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
J
Jan Glauber 已提交
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	if (!irq_ptr->qdr)
		goto out_rel;
	WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);

	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);

/**
 * qdio_establish - establish queues on a qdio subchannel
 * @init_data: initialization data
 */
int qdio_establish(struct qdio_initialize *init_data)
{
	struct qdio_irq *irq_ptr;
	struct ccw_device *cdev = init_data->cdev;
	unsigned long saveflags;
	int rc;

1213
	DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
1214

J
Jan Glauber 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
	irq_ptr = cdev->private->qdio_data;
	if (!irq_ptr)
		return -ENODEV;

	if (cdev->private->state != DEV_STATE_ONLINE)
		return -EINVAL;

	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);

	spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
	ccw_device_set_options_mask(cdev, 0);

	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
	if (rc) {
1243 1244
		DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	}
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);

	if (rc) {
		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);
1265 1266
	DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
	DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
J
Jan Glauber 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

	/* 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)
{
	struct qdio_irq *irq_ptr;
	int rc;
	unsigned long saveflags;

1288
	DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
1289

J
Jan Glauber 已提交
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	irq_ptr = cdev->private->qdio_data;
	if (!irq_ptr)
		return -ENODEV;

	if (cdev->private->state != DEV_STATE_ONLINE)
		return -EINVAL;

	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;

	spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
	ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);

	rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
			      0, DOIO_DENY_PREFETCH);
	if (rc) {
1314 1315
		DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	}
	spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);

	if (rc)
		goto out;

	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:
1331 1332
		rc = -EIO;
		break;
J
Jan Glauber 已提交
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
	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
 */
1369 1370
static int handle_inbound(struct qdio_q *q, unsigned int callflags,
			  int bufnr, int count)
J
Jan Glauber 已提交
1371
{
1372
	int used, diff;
J
Jan Glauber 已提交
1373

1374 1375
	qperf_inc(q, inbound_call);

1376 1377 1378 1379 1380 1381 1382 1383 1384
	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;
1385
	} else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1386
		if (is_qebsm(q)) {
1387
			/* partial overwrite, just update ack_start */
1388
			diff = add_buf(bufnr, count);
1389
			diff = sub_buf(diff, q->u.in.ack_start);
1390 1391 1392 1393 1394 1395
			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;
			}
1396
			q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1397 1398 1399
		}
		else
			/* the only ACK will be deleted, so stop polling */
J
Jan Glauber 已提交
1400
			q->u.in.polling = 0;
1401
	}
J
Jan Glauber 已提交
1402

1403
set:
J
Jan Glauber 已提交
1404 1405 1406 1407 1408 1409 1410
	count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);

	used = atomic_add_return(count, &q->nr_buf_used) - count;
	BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);

	/* no need to signal as long as the adapter had free buffers */
	if (used)
1411
		return 0;
J
Jan Glauber 已提交
1412

1413 1414 1415
	if (need_siga_in(q))
		return qdio_siga_input(q);
	return 0;
J
Jan Glauber 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424
}

/**
 * handle_outbound - process filled outbound buffers
 * @q: queue containing the buffers
 * @callflags: flags
 * @bufnr: first buffer to process
 * @count: how many buffers are filled
 */
1425 1426
static int handle_outbound(struct qdio_q *q, unsigned int callflags,
			   int bufnr, int count)
J
Jan Glauber 已提交
1427 1428
{
	unsigned char state;
1429
	int used, rc = 0;
J
Jan Glauber 已提交
1430

1431
	qperf_inc(q, outbound_call);
J
Jan Glauber 已提交
1432 1433 1434 1435 1436

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

1437
	if (callflags & QDIO_FLAG_PCI_OUT) {
J
Jan Glauber 已提交
1438
		q->u.out.pci_out_enabled = 1;
1439 1440
		qperf_inc(q, pci_request_int);
	}
J
Jan Glauber 已提交
1441 1442 1443 1444 1445
	else
		q->u.out.pci_out_enabled = 0;

	if (queue_type(q) == QDIO_IQDIO_QFMT) {
		if (multicast_outbound(q))
1446
			rc = qdio_kick_outbound_q(q);
J
Jan Glauber 已提交
1447
		else
1448 1449 1450 1451 1452
			if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
			    (count > 1) &&
			    (count <= q->irq_ptr->ssqd_desc.mmwc)) {
				/* exploit enhanced SIGA */
				q->u.out.use_enh_siga = 1;
1453
				rc = qdio_kick_outbound_q(q);
1454 1455 1456 1457 1458 1459
			} else {
				/*
				* One siga-w per buffer required for unicast
				* HiperSockets.
				*/
				q->u.out.use_enh_siga = 0;
1460 1461 1462 1463 1464
				while (count--) {
					rc = qdio_kick_outbound_q(q);
					if (rc)
						goto out;
				}
1465
			}
J
Jan Glauber 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474
		goto out;
	}

	if (need_siga_sync(q)) {
		qdio_siga_sync_q(q);
		goto out;
	}

	/* try to fast requeue buffers */
1475
	get_buf_state(q, prev_buf(bufnr), &state, 0);
J
Jan Glauber 已提交
1476
	if (state != SLSB_CU_OUTPUT_PRIMED)
1477
		rc = qdio_kick_outbound_q(q);
1478
	else
1479
		qperf_inc(q, fast_requeue);
1480

J
Jan Glauber 已提交
1481 1482
out:
	tasklet_schedule(&q->tasklet);
1483
	return rc;
J
Jan Glauber 已提交
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
}

/**
 * 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,
1495
	    int q_nr, unsigned int bufnr, unsigned int count)
J
Jan Glauber 已提交
1496 1497 1498
{
	struct qdio_irq *irq_ptr;

1499
	if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
J
Jan Glauber 已提交
1500 1501 1502 1503 1504 1505
		return -EINVAL;

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

1506 1507
	DBF_DEV_EVENT(DBF_INFO, irq_ptr,
		      "do%02x b:%02x c:%02x", callflags, bufnr, count);
J
Jan Glauber 已提交
1508 1509 1510 1511 1512

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

	if (callflags & QDIO_FLAG_SYNC_INPUT)
1513 1514
		return handle_inbound(irq_ptr->input_qs[q_nr],
				      callflags, bufnr, count);
J
Jan Glauber 已提交
1515
	else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1516 1517 1518
		return handle_outbound(irq_ptr->output_qs[q_nr],
				       callflags, bufnr, count);
	return -EINVAL;
J
Jan Glauber 已提交
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
}
EXPORT_SYMBOL_GPL(do_QDIO);

static int __init init_QDIO(void)
{
	int rc;

	rc = qdio_setup_init();
	if (rc)
		return rc;
	rc = tiqdio_allocate_memory();
	if (rc)
		goto out_cache;
	rc = qdio_debug_init();
	if (rc)
		goto out_ti;
	rc = tiqdio_register_thinints();
	if (rc)
1537
		goto out_debug;
J
Jan Glauber 已提交
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	return 0;

out_debug:
	qdio_debug_exit();
out_ti:
	tiqdio_free_memory();
out_cache:
	qdio_setup_exit();
	return rc;
}

static void __exit exit_QDIO(void)
{
	tiqdio_unregister_thinints();
	tiqdio_free_memory();
	qdio_debug_exit();
	qdio_setup_exit();
}

module_init(init_QDIO);
module_exit(exit_QDIO);