qdio_main.c 40.5 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>
17
#include <linux/kernel_stat.h>
J
Jan Glauber 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#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");

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	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,
80
				 unsigned int *bb, unsigned int fc)
J
Jan Glauber 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	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 */
108
	DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
J
Jan Glauber 已提交
109 110 111 112 113 114 115 116 117
	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
118
 * @auto_ack: automatically acknowledge buffers
J
Jan Glauber 已提交
119
 *
C
Coly Li 已提交
120
 * Returns the number of successfully extracted equal buffer states.
J
Jan Glauber 已提交
121 122 123
 * Stops processing if a state is different from the last buffers state.
 */
static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
124
			int start, int count, int auto_ack)
J
Jan Glauber 已提交
125 126 127 128 129 130 131
{
	unsigned int ccq = 0;
	int tmp_count = count, tmp_start = start;
	int nr = q->nr;
	int rc;

	BUG_ON(!q->irq_ptr->sch_token);
132
	qperf_inc(q, eqbs);
J
Jan Glauber 已提交
133 134 135 136

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

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

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

	if (rc < 0) {
155 156
		DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
J
Jan Glauber 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
		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;

184 185 186
	if (!count)
		return 0;

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

	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) {
196
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
197
		qperf_inc(q, sqbs_partial);
J
Jan Glauber 已提交
198 199 200
		goto again;
	}
	if (rc < 0) {
201 202
		DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
		DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
J
Jan Glauber 已提交
203 204 205 206 207 208 209 210 211 212 213
		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,
214 215
				 unsigned char *state, unsigned int count,
				 int auto_ack)
J
Jan Glauber 已提交
216 217 218 219 220 221 222 223
{
	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))
224
		return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
J
Jan Glauber 已提交
225 226 227 228 229 230 231 232 233 234 235 236

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

237 238
static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
				unsigned char *state, int auto_ack)
J
Jan Glauber 已提交
239
{
240
	return get_buf_states(q, bufnr, state, 1, auto_ack);
J
Jan Glauber 已提交
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 279 280 281
}

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

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

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

292 293 294 295 296 297
	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 已提交
298
	if (unlikely(cc))
299
		DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
J
Jan Glauber 已提交
300 301 302
	return cc;
}

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

311
static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
J
Jan Glauber 已提交
312
{
313 314
	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
	unsigned int fc = QDIO_SIGA_WRITE;
315 316
	u64 start_time = 0;
	int cc;
J
Jan Glauber 已提交
317

318
	if (is_qebsm(q)) {
J
Jan Glauber 已提交
319
		schid = q->irq_ptr->sch_token;
320
		fc |= QDIO_SIGA_QEBSM_FLAG;
J
Jan Glauber 已提交
321 322
	}
again:
323 324 325
	cc = do_siga_output(schid, q->mask, busy_bit, fc);

	/* hipersocket busy condition */
J
Jan Glauber 已提交
326
	if (unlikely(*busy_bit)) {
327
		WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
328

329
		if (!start_time) {
330
			start_time = get_clock();
331 332
			goto again;
		}
333
		if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
J
Jan Glauber 已提交
334 335 336 337 338 339 340
			goto again;
	}
	return cc;
}

static inline int qdio_siga_input(struct qdio_q *q)
{
341 342
	unsigned long schid = *((u32 *) &q->irq_ptr->schid);
	unsigned int fc = QDIO_SIGA_READ;
J
Jan Glauber 已提交
343 344
	int cc;

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

348 349 350 351 352 353
	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 已提交
354
	if (unlikely(cc))
355
		DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
J
Jan Glauber 已提交
356 357 358
	return cc;
}

J
Jan Glauber 已提交
359 360 361 362
#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 已提交
363
{
J
Jan Glauber 已提交
364 365 366 367
	/* 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 已提交
368 369 370
		qdio_siga_sync_q(q);
}

371 372 373
int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
			unsigned char *state)
{
J
Jan Glauber 已提交
374 375
	if (need_siga_sync(q))
		qdio_siga_sync_q(q);
376 377 378 379
	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 process_buffer_error(struct qdio_q *q, int count)
J
Jan Glauber 已提交
411
{
412 413 414
	unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
					SLSB_P_OUTPUT_NOT_INIT;

415
	q->qdio_error |= QDIO_ERROR_SLSB_STATE;
416 417 418

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

426 427
	DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
	DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
428
	DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
429
	DBF_ERROR("F14:%2x F15:%2x",
430 431
		  q->sbal[q->first_to_check]->element[14].sflags,
		  q->sbal[q->first_to_check]->element[15].sflags);
432 433 434 435 436 437

	/*
	 * 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);
438
}
J
Jan Glauber 已提交
439

440 441 442 443
static inline void inbound_primed(struct qdio_q *q, int count)
{
	int new;

444
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
445 446 447 448 449 450

	/* 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;
451
			q->u.in.ack_start = q->first_to_check;
452 453 454 455
			return;
		}

		/* delete the previous ACK's */
456
		set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
457 458
			       q->u.in.ack_count);
		q->u.in.ack_count = count;
459
		q->u.in.ack_start = q->first_to_check;
460 461 462 463 464 465 466 467 468 469 470
		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);
471
		set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
472
	} else {
473
		q->u.in.polling = 1;
474
		set_buf_state(q, new, SLSB_P_INPUT_ACK);
475 476
	}

477
	q->u.in.ack_start = new;
478 479 480
	count--;
	if (!count)
		return;
481 482
	/* 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 已提交
483 484 485 486 487
}

static int get_inbound_buffer_frontier(struct qdio_q *q)
{
	int count, stop;
488
	unsigned char state = 0;
J
Jan Glauber 已提交
489 490 491 492 493 494 495 496 497 498 499

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

500 501 502 503
	/*
	 * No siga sync here, as a PCI or we after a thin interrupt
	 * already sync'ed the queues.
	 */
504
	count = get_buf_states(q, q->first_to_check, &state, count, 1);
J
Jan Glauber 已提交
505 506 507 508 509
	if (!count)
		goto out;

	switch (state) {
	case SLSB_P_INPUT_PRIMED:
510
		inbound_primed(q, count);
J
Jan Glauber 已提交
511
		q->first_to_check = add_buf(q->first_to_check, count);
512
		if (atomic_sub(count, &q->nr_buf_used) == 0)
513
			qperf_inc(q, inbound_queue_full);
514 515
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
516
		break;
J
Jan Glauber 已提交
517
	case SLSB_P_INPUT_ERROR:
518
		process_buffer_error(q, count);
J
Jan Glauber 已提交
519 520
		q->first_to_check = add_buf(q->first_to_check, count);
		atomic_sub(count, &q->nr_buf_used);
521 522
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
523 524 525 526
		break;
	case SLSB_CU_INPUT_EMPTY:
	case SLSB_P_INPUT_NOT_INIT:
	case SLSB_P_INPUT_ACK:
527 528
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
529
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
J
Jan Glauber 已提交
530 531 532 533 534 535 536 537
		break;
	default:
		BUG();
	}
out:
	return q->first_to_check;
}

538
static int qdio_inbound_q_moved(struct qdio_q *q)
J
Jan Glauber 已提交
539 540 541 542 543
{
	int bufnr;

	bufnr = get_inbound_buffer_frontier(q);

544 545
	if ((bufnr != q->last_move) || q->qdio_error) {
		q->last_move = bufnr;
546
		if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
547
			q->u.in.timestamp = get_clock();
J
Jan Glauber 已提交
548 549 550 551 552
		return 1;
	} else
		return 0;
}

553
static inline int qdio_inbound_q_done(struct qdio_q *q)
J
Jan Glauber 已提交
554
{
555
	unsigned char state = 0;
J
Jan Glauber 已提交
556 557 558 559

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

J
Jan Glauber 已提交
560 561
	if (need_siga_sync(q))
		qdio_siga_sync_q(q);
562
	get_buf_state(q, q->first_to_check, &state, 0);
563

564
	if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
565
		/* more work coming */
J
Jan Glauber 已提交
566 567
		return 0;

568 569 570 571 572
	if (is_thinint_irq(q->irq_ptr))
		return 1;

	/* don't poll under z/VM */
	if (MACHINE_IS_VM)
J
Jan Glauber 已提交
573 574 575 576 577 578
		return 1;

	/*
	 * At this point we know, that inbound first_to_check
	 * has (probably) not moved (see qdio_inbound_processing).
	 */
579
	if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
580
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
581
			      q->first_to_check);
J
Jan Glauber 已提交
582
		return 1;
583
	} else
584 585 586 587
		return 0;
}

static void qdio_kick_handler(struct qdio_q *q)
J
Jan Glauber 已提交
588
{
589 590 591
	int start = q->first_to_kick;
	int end = q->first_to_check;
	int count;
J
Jan Glauber 已提交
592 593 594 595

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

596 597 598
	count = sub_buf(end, start);

	if (q->is_input_q) {
599
		qperf_inc(q, inbound_handler);
600
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
U
Ursula Braun 已提交
601
	} else {
602
		qperf_inc(q, outbound_handler);
603 604
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
			      start, count);
U
Ursula Braun 已提交
605
	}
606 607 608

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

	/* for the next time */
611
	q->first_to_kick = end;
J
Jan Glauber 已提交
612 613 614 615 616
	q->qdio_error = 0;
}

static void __qdio_inbound_processing(struct qdio_q *q)
{
617
	qperf_inc(q, tasklet_inbound);
618

J
Jan Glauber 已提交
619 620 621
	if (!qdio_inbound_q_moved(q))
		return;

622
	qdio_kick_handler(q);
J
Jan Glauber 已提交
623

624
	if (!qdio_inbound_q_done(q)) {
J
Jan Glauber 已提交
625
		/* means poll time is not yet over */
626
		qperf_inc(q, tasklet_inbound_resched);
627 628 629 630
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
			tasklet_schedule(&q->tasklet);
			return;
		}
631
	}
J
Jan Glauber 已提交
632 633 634 635 636 637

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
638 639
	if (!qdio_inbound_q_done(q)) {
		qperf_inc(q, tasklet_inbound_resched2);
640 641
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
			tasklet_schedule(&q->tasklet);
642
	}
J
Jan Glauber 已提交
643 644 645 646 647 648 649 650 651 652 653
}

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;
654
	unsigned char state = 0;
J
Jan Glauber 已提交
655

J
Jan Glauber 已提交
656 657 658 659 660 661
	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 已提交
662 663 664 665 666 667 668 669 670 671 672

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

673
	count = get_buf_states(q, q->first_to_check, &state, count, 0);
J
Jan Glauber 已提交
674 675 676 677 678 679
	if (!count)
		return q->first_to_check;

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

		atomic_sub(count, &q->nr_buf_used);
		q->first_to_check = add_buf(q->first_to_check, count);
684 685
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals(q, count);
686
		break;
J
Jan Glauber 已提交
687
	case SLSB_P_OUTPUT_ERROR:
688
		process_buffer_error(q, count);
J
Jan Glauber 已提交
689 690
		q->first_to_check = add_buf(q->first_to_check, count);
		atomic_sub(count, &q->nr_buf_used);
691 692
		if (q->irq_ptr->perf_stat_enabled)
			account_sbals_error(q, count);
J
Jan Glauber 已提交
693 694 695
		break;
	case SLSB_CU_OUTPUT_PRIMED:
		/* the adapter has not fetched the output yet */
696 697
		if (q->irq_ptr->perf_stat_enabled)
			q->q_stats.nr_sbal_nop++;
698
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
J
Jan Glauber 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
		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);

721 722
	if ((bufnr != q->last_move) || q->qdio_error) {
		q->last_move = bufnr;
723
		DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
J
Jan Glauber 已提交
724 725 726 727 728
		return 1;
	} else
		return 0;
}

729
static int qdio_kick_outbound_q(struct qdio_q *q)
J
Jan Glauber 已提交
730
{
731 732
	unsigned int busy_bit;
	int cc;
J
Jan Glauber 已提交
733 734

	if (!need_siga_out(q))
735
		return 0;
J
Jan Glauber 已提交
736

737
	DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
738
	qperf_inc(q, siga_write);
739 740 741

	cc = qdio_siga_output(q, &busy_bit);
	switch (cc) {
J
Jan Glauber 已提交
742 743
	case 0:
		break;
744 745 746
	case 2:
		if (busy_bit) {
			DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
747 748 749
			cc |= QDIO_ERROR_SIGA_BUSY;
		} else
			DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
750 751 752 753 754
		break;
	case 1:
	case 3:
		DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
		break;
J
Jan Glauber 已提交
755
	}
756
	return cc;
J
Jan Glauber 已提交
757 758 759 760
}

static void __qdio_outbound_processing(struct qdio_q *q)
{
761
	qperf_inc(q, tasklet_outbound);
J
Jan Glauber 已提交
762 763 764
	BUG_ON(atomic_read(&q->nr_buf_used) < 0);

	if (qdio_outbound_q_moved(q))
765
		qdio_kick_handler(q);
J
Jan Glauber 已提交
766

767
	if (queue_type(q) == QDIO_ZFCP_QFMT)
J
Jan Glauber 已提交
768
		if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
769
			goto sched;
J
Jan Glauber 已提交
770 771 772 773 774

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

775
	if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
776 777
	    (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
		goto sched;
778

J
Jan Glauber 已提交
779 780 781 782 783 784 785 786 787 788
	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);
789 790
	else
		if (!timer_pending(&q->u.out.timer))
J
Jan Glauber 已提交
791
			mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
792 793 794 795 796 797
	return;

sched:
	if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;
	tasklet_schedule(&q->tasklet);
J
Jan Glauber 已提交
798 799 800 801 802 803 804 805 806 807 808 809
}

/* 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;
810 811 812

	if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;
J
Jan Glauber 已提交
813 814 815
	tasklet_schedule(&q->tasklet);
}

816
static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
J
Jan Glauber 已提交
817 818 819 820 821 822 823 824 825 826 827 828
{
	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);
}

829 830
static void __tiqdio_inbound_processing(struct qdio_q *q)
{
831
	qperf_inc(q, tasklet_inbound);
J
Jan Glauber 已提交
832 833
	if (need_siga_sync(q) && need_siga_sync_after_ai(q))
		qdio_sync_queues(q);
834 835 836 837 838 839 840 841 842 843 844 845

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

846
	if (!qdio_inbound_q_done(q)) {
847
		qperf_inc(q, tasklet_inbound_resched);
848
		if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
849
			tasklet_schedule(&q->tasklet);
850 851
			return;
		}
852 853 854 855 856 857 858
	}

	qdio_stop_polling(q);
	/*
	 * We need to check again to not lose initiative after
	 * resetting the ACK state.
	 */
859
	if (!qdio_inbound_q_done(q)) {
860
		qperf_inc(q, tasklet_inbound_resched2);
861 862 863 864 865 866 867 868 869 870 871
		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 已提交
872 873 874
static inline void qdio_set_state(struct qdio_irq *irq_ptr,
				  enum qdio_irq_states state)
{
875
	DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
J
Jan Glauber 已提交
876 877 878 879 880

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

881
static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
J
Jan Glauber 已提交
882 883
{
	if (irb->esw.esw0.erw.cons) {
884 885 886
		DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
		DBF_ERROR_HEX(irb, 64);
		DBF_ERROR_HEX(irb->ecw, 64);
J
Jan Glauber 已提交
887 888 889 890 891 892 893 894 895
	}
}

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

896 897 898
	if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
		return;

J
Jan Glauber 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911
	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);
		} else
			tasklet_schedule(&q->tasklet);
	}
J
Jan Glauber 已提交
912

J
Jan Glauber 已提交
913
	if (!pci_out_supported(q))
J
Jan Glauber 已提交
914 915 916 917 918
		return;

	for_each_output_queue(irq_ptr, q, i) {
		if (qdio_outbound_q_done(q))
			continue;
J
Jan Glauber 已提交
919
		if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
J
Jan Glauber 已提交
920 921 922 923 924 925 926 927 928 929 930
			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;

931 932 933
	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 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

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

949 950
static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
				      int dstat)
J
Jan Glauber 已提交
951 952 953
{
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;

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

956
	if (cstat)
J
Jan Glauber 已提交
957
		goto error;
958
	if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
J
Jan Glauber 已提交
959
		goto error;
960 961 962 963 964
	if (!(dstat & DEV_STAT_DEV_END))
		goto error;
	qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
	return;

J
Jan Glauber 已提交
965
error:
966 967
	DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
	DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
J
Jan Glauber 已提交
968 969 970 971 972 973 974 975 976 977 978
	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) {
979
		DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
J
Jan Glauber 已提交
980 981 982
		return;
	}

983
	kstat_cpu(smp_processor_id()).irqs[IOINT_QDI]++;
984 985 986
	if (irq_ptr->perf_stat_enabled)
		irq_ptr->perf_stat.qdio_int++;

J
Jan Glauber 已提交
987 988 989
	if (IS_ERR(irb)) {
		switch (PTR_ERR(irb)) {
		case -EIO:
990
			DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
991 992
			qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
			wake_up(&cdev->private->wait_q);
J
Jan Glauber 已提交
993 994 995 996 997 998
			return;
		default:
			WARN_ON(1);
			return;
		}
	}
999
	qdio_irq_check_sense(irq_ptr, irb);
J
Jan Glauber 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	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;
		}
1016
		if (cstat || dstat)
J
Jan Glauber 已提交
1017 1018
			qdio_handle_activate_check(cdev, intparm, cstat,
						   dstat);
1019
		break;
1020 1021
	case QDIO_IRQ_STATE_STOPPED:
		break;
J
Jan Glauber 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030
	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
1031
 * @data: where to store the ssqd
J
Jan Glauber 已提交
1032
 *
1033 1034
 * Returns 0 or an error code. The results of the chsc are stored in the
 * specified structure.
J
Jan Glauber 已提交
1035
 */
1036 1037
int qdio_get_ssqd_desc(struct ccw_device *cdev,
		       struct qdio_ssqd_desc *data)
J
Jan Glauber 已提交
1038 1039
{

1040 1041 1042
	if (!cdev || !cdev->private)
		return -EINVAL;

1043
	DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
1044
	return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
J
Jan Glauber 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
}
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)
1055
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1056 1057 1058

	for_each_output_queue(irq_ptr, q, i) {
		del_timer(&q->u.out.timer);
1059
		tasklet_kill(&q->tasklet);
J
Jan Glauber 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
	}
}

/**
 * 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)
{
1070
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
J
Jan Glauber 已提交
1071 1072 1073 1074 1075 1076
	int rc;
	unsigned long flags;

	if (!irq_ptr)
		return -ENODEV;

1077
	BUG_ON(irqs_disabled());
1078 1079
	DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);

J
Jan Glauber 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	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;
	}

1090 1091 1092 1093 1094 1095
	/*
	 * 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 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	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) {
1109 1110
		DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4d", rc);
J
Jan Glauber 已提交
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
		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)
{
1144
	struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1145

J
Jan Glauber 已提交
1146 1147 1148
	if (!irq_ptr)
		return -ENODEV;

1149
	DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
J
Jan Glauber 已提交
1150
	mutex_lock(&irq_ptr->setup_mutex);
1151 1152 1153 1154 1155

	if (irq_ptr->debug_area != NULL) {
		debug_unregister(irq_ptr->debug_area);
		irq_ptr->debug_area = NULL;
	}
J
Jan Glauber 已提交
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	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;

1172
	DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
J
Jan Glauber 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

	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);
1192
	qdio_allocate_dbf(init_data, irq_ptr);
J
Jan Glauber 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

	/*
	 * 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 */
1205
	irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
J
Jan Glauber 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	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;

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

J
Jan Glauber 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	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) {
1265 1266
		DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	}
	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);
1287
	DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
J
Jan Glauber 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

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

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

J
Jan Glauber 已提交
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
	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) {
1335 1336
		DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
		DBF_ERROR("rc:%4x", rc);
J
Jan Glauber 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
	}
	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:
1352 1353
		rc = -EIO;
		break;
J
Jan Glauber 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	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
 */
1390 1391
static int handle_inbound(struct qdio_q *q, unsigned int callflags,
			  int bufnr, int count)
J
Jan Glauber 已提交
1392
{
1393
	int used, diff;
J
Jan Glauber 已提交
1394

1395 1396
	qperf_inc(q, inbound_call);

1397 1398 1399 1400 1401 1402 1403 1404 1405
	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;
1406
	} else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1407
		if (is_qebsm(q)) {
1408
			/* partial overwrite, just update ack_start */
1409
			diff = add_buf(bufnr, count);
1410
			diff = sub_buf(diff, q->u.in.ack_start);
1411 1412 1413 1414 1415 1416
			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;
			}
1417
			q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1418 1419 1420
		}
		else
			/* the only ACK will be deleted, so stop polling */
J
Jan Glauber 已提交
1421
			q->u.in.polling = 0;
1422
	}
J
Jan Glauber 已提交
1423

1424
set:
J
Jan Glauber 已提交
1425 1426 1427 1428 1429 1430 1431
	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)
1432
		return 0;
J
Jan Glauber 已提交
1433

1434 1435 1436
	if (need_siga_in(q))
		return qdio_siga_input(q);
	return 0;
J
Jan Glauber 已提交
1437 1438 1439 1440 1441 1442 1443 1444 1445
}

/**
 * handle_outbound - process filled outbound buffers
 * @q: queue containing the buffers
 * @callflags: flags
 * @bufnr: first buffer to process
 * @count: how many buffers are filled
 */
1446 1447
static int handle_outbound(struct qdio_q *q, unsigned int callflags,
			   int bufnr, int count)
J
Jan Glauber 已提交
1448
{
1449
	unsigned char state = 0;
1450
	int used, rc = 0;
J
Jan Glauber 已提交
1451

1452
	qperf_inc(q, outbound_call);
J
Jan Glauber 已提交
1453 1454 1455 1456 1457

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

1458 1459 1460
	if (used == QDIO_MAX_BUFFERS_PER_Q)
		qperf_inc(q, outbound_queue_full);

1461
	if (callflags & QDIO_FLAG_PCI_OUT) {
J
Jan Glauber 已提交
1462
		q->u.out.pci_out_enabled = 1;
1463
		qperf_inc(q, pci_request_int);
J
Jan Glauber 已提交
1464
	} else
J
Jan Glauber 已提交
1465 1466 1467
		q->u.out.pci_out_enabled = 0;

	if (queue_type(q) == QDIO_IQDIO_QFMT) {
J
Jan Glauber 已提交
1468 1469 1470 1471
		/* One SIGA-W per buffer required for unicast HiperSockets. */
		WARN_ON_ONCE(count > 1 && !multicast_outbound(q));

		rc = qdio_kick_outbound_q(q);
J
Jan Glauber 已提交
1472
	} else if (need_siga_sync(q)) {
J
Jan Glauber 已提交
1473 1474 1475 1476 1477
		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)
1478
			rc = qdio_kick_outbound_q(q);
J
Jan Glauber 已提交
1479
		else
J
Jan Glauber 已提交
1480
			qperf_inc(q, fast_requeue);
J
Jan Glauber 已提交
1481 1482
	}

1483 1484 1485 1486 1487 1488 1489
	/* in case of SIGA errors we must process the error immediately */
	if (used >= q->u.out.scan_threshold || rc)
		tasklet_schedule(&q->tasklet);
	else
		/* free the SBALs in case of no further traffic */
		if (!timer_pending(&q->u.out.timer))
			mod_timer(&q->u.out.timer, jiffies + HZ);
1490
	return rc;
J
Jan Glauber 已提交
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
}

/**
 * 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,
1502
	    int q_nr, unsigned int bufnr, unsigned int count)
J
Jan Glauber 已提交
1503 1504 1505
{
	struct qdio_irq *irq_ptr;

1506
	if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
J
Jan Glauber 已提交
1507 1508 1509 1510 1511 1512
		return -EINVAL;

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

1513 1514
	DBF_DEV_EVENT(DBF_INFO, irq_ptr,
		      "do%02x b:%02x c:%02x", callflags, bufnr, count);
J
Jan Glauber 已提交
1515 1516 1517

	if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
		return -EBUSY;
1518 1519
	if (!count)
		return 0;
J
Jan Glauber 已提交
1520
	if (callflags & QDIO_FLAG_SYNC_INPUT)
1521 1522
		return handle_inbound(irq_ptr->input_qs[q_nr],
				      callflags, bufnr, count);
J
Jan Glauber 已提交
1523
	else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1524 1525 1526
		return handle_outbound(irq_ptr->output_qs[q_nr],
				       callflags, bufnr, count);
	return -EINVAL;
J
Jan Glauber 已提交
1527 1528 1529
}
EXPORT_SYMBOL_GPL(do_QDIO);

J
Jan Glauber 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
/**
 * 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];

	WARN_ON(queue_irqs_enabled(q));

1550
	if (!shared_ind(q->irq_ptr->dsci))
J
Jan Glauber 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
		xchg(q->irq_ptr->dsci, 0);

	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.
	 */
1560
	if (!shared_ind(q->irq_ptr->dsci) && *q->irq_ptr->dsci)
J
Jan Glauber 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
		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];
	WARN_ON(queue_irqs_enabled(q));

	/*
J
Jan Glauber 已提交
1601 1602
	 * Cannot rely on automatic sync after interrupt since queues may
	 * also be examined without interrupt.
J
Jan Glauber 已提交
1603
	 */
J
Jan Glauber 已提交
1604 1605 1606 1607
	if (need_siga_sync(q))
		qdio_sync_queues(q);

	/* check the PCI capable outbound queues. */
J
Jan Glauber 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	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);

J
Jan Glauber 已提交
1655 1656 1657 1658
static int __init init_QDIO(void)
{
	int rc;

S
Sebastian Ott 已提交
1659
	rc = qdio_debug_init();
J
Jan Glauber 已提交
1660 1661
	if (rc)
		return rc;
S
Sebastian Ott 已提交
1662 1663 1664
	rc = qdio_setup_init();
	if (rc)
		goto out_debug;
J
Jan Glauber 已提交
1665 1666 1667 1668 1669
	rc = tiqdio_allocate_memory();
	if (rc)
		goto out_cache;
	rc = tiqdio_register_thinints();
	if (rc)
S
Sebastian Ott 已提交
1670
		goto out_ti;
J
Jan Glauber 已提交
1671 1672 1673 1674 1675 1676
	return 0;

out_ti:
	tiqdio_free_memory();
out_cache:
	qdio_setup_exit();
S
Sebastian Ott 已提交
1677 1678
out_debug:
	qdio_debug_exit();
J
Jan Glauber 已提交
1679 1680 1681 1682 1683 1684 1685 1686
	return rc;
}

static void __exit exit_QDIO(void)
{
	tiqdio_unregister_thinints();
	tiqdio_free_memory();
	qdio_setup_exit();
S
Sebastian Ott 已提交
1687
	qdio_debug_exit();
J
Jan Glauber 已提交
1688 1689 1690 1691
}

module_init(init_QDIO);
module_exit(exit_QDIO);