qdio.c 103 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
/*
 *
 * linux/drivers/s390/cio/qdio.c
 *
 * Linux for S/390 QDIO base support, Hipersocket base support
 * version 2
 *
 * Copyright 2000,2002 IBM Corporation
 * Author(s):             Utz Bacher <utz.bacher@de.ibm.com>
10
 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
Linus Torvalds's avatar
Linus Torvalds committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 *
 * Restriction: only 63 iqdio subchannels would have its own indicator,
 * after that, subsequent subchannels share one indicator
 *
 *
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
35
#include <linux/delay.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
37
38
39
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/timer.h>
40
#include <linux/mempool.h>
41
#include <linux/semaphore.h>
Linus Torvalds's avatar
Linus Torvalds committed
42
43
44
45
46
47
48

#include <asm/ccwdev.h>
#include <asm/io.h>
#include <asm/atomic.h>
#include <asm/timex.h>

#include <asm/debug.h>
49
#include <asm/s390_rdev.h>
Linus Torvalds's avatar
Linus Torvalds committed
50
#include <asm/qdio.h>
51
#include <asm/airq.h>
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include "cio.h"
#include "css.h"
#include "device.h"
#include "qdio.h"
#include "ioasm.h"
#include "chsc.h"

/****************** MODULE PARAMETER VARIABLES ********************/
MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
MODULE_DESCRIPTION("QDIO base support version 2, " \
		   "Copyright 2000 IBM Corporation");
MODULE_LICENSE("GPL");

/******************** HERE WE GO ***********************************/

68
static const char version[] = "QDIO base support version 2";
Linus Torvalds's avatar
Linus Torvalds committed
69

70
static int qdio_performance_stats = 0;
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
static int proc_perf_file_registration;
static struct qdio_perf_stats perf_stats;

static int hydra_thinints;
75
static int is_passthrough = 0;
Linus Torvalds's avatar
Linus Torvalds committed
76
77
78
79
80
81
static int omit_svs;

static int indicator_used[INDICATORS_PER_CACHELINE];
static __u32 * volatile indicators;
static __u32 volatile spare_indicator;
static atomic_t spare_indicator_usecount;
82
83
#define QDIO_MEMPOOL_SCSSC_ELEMENTS 2
static mempool_t *qdio_mempool_scssc;
84
static struct kmem_cache *qdio_q_cache;
Linus Torvalds's avatar
Linus Torvalds committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98

static debug_info_t *qdio_dbf_setup;
static debug_info_t *qdio_dbf_sbal;
static debug_info_t *qdio_dbf_trace;
static debug_info_t *qdio_dbf_sense;
#ifdef CONFIG_QDIO_DEBUG
static debug_info_t *qdio_dbf_slsb_out;
static debug_info_t *qdio_dbf_slsb_in;
#endif /* CONFIG_QDIO_DEBUG */

/* iQDIO stuff: */
static volatile struct qdio_q *tiq_list=NULL; /* volatile as it could change
						 during a while loop */
static DEFINE_SPINLOCK(ttiq_list_lock);
99
static void *tiqdio_ind;
Linus Torvalds's avatar
Linus Torvalds committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
static void tiqdio_tl(unsigned long);
static DECLARE_TASKLET(tiqdio_tasklet,tiqdio_tl,0);

/* not a macro, as one of the arguments is atomic_read */
static inline int 
qdio_min(int a,int b)
{
	if (a<b)
		return a;
	else
		return b;
}

/***************** SCRUBBER HELPER ROUTINES **********************/
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifdef CONFIG_64BIT
static inline void qdio_perf_stat_inc(atomic64_t *count)
{
	if (qdio_performance_stats)
		atomic64_inc(count);
}

static inline void qdio_perf_stat_dec(atomic64_t *count)
{
	if (qdio_performance_stats)
		atomic64_dec(count);
}
#else /* CONFIG_64BIT */
static inline void qdio_perf_stat_inc(atomic_t *count)
{
	if (qdio_performance_stats)
		atomic_inc(count);
}

static inline void qdio_perf_stat_dec(atomic_t *count)
{
	if (qdio_performance_stats)
		atomic_dec(count);
}
#endif /* CONFIG_64BIT */
Linus Torvalds's avatar
Linus Torvalds committed
139

Al Viro's avatar
Al Viro committed
140
static inline __u64 
Linus Torvalds's avatar
Linus Torvalds committed
141
142
qdio_get_micros(void)
{
143
	return (get_clock() >> 12); /* time>>12 is microseconds */
Linus Torvalds's avatar
Linus Torvalds committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}

/* 
 * unfortunately, we can't just xchg the values; in do_QDIO we want to reserve
 * the q in any case, so that we'll not be interrupted when we are in
 * qdio_mark_tiq... shouldn't have a really bad impact, as reserving almost
 * ever works (last famous words) 
 */
static inline int 
qdio_reserve_q(struct qdio_q *q)
{
	return atomic_add_return(1,&q->use_count) - 1;
}

static inline void 
qdio_release_q(struct qdio_q *q)
{
	atomic_dec(&q->use_count);
}

164
/*check ccq  */
165
static int
166
qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
Linus Torvalds's avatar
Linus Torvalds committed
167
{
168
169
	char dbf_text[15];

170
	if (ccq == 0 || ccq == 32)
171
		return 0;
172
	if (ccq == 96 || ccq == 97)
173
174
175
176
177
		return 1;
	/*notify devices immediately*/
	sprintf(dbf_text,"%d", ccq);
	QDIO_DBF_TEXT2(1,trace,dbf_text);
	return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
178
}
179
/* EQBS: extract buffer states */
180
static int
181
182
183
184
185
186
187
qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
	     unsigned int *start, unsigned int *cnt)
{
	struct qdio_irq *irq;
	unsigned int tmp_cnt, q_no, ccq;
	int rc ;
	char dbf_text[15];
Linus Torvalds's avatar
Linus Torvalds committed
188

189
190
191
192
193
194
	ccq = 0;
	tmp_cnt = *cnt;
	irq = (struct qdio_irq*)q->irq_ptr;
	q_no = q->q_no;
	if(!q->is_input_q)
		q_no += irq->no_input_qs;
Frank Pavlic's avatar
Frank Pavlic committed
195
again:
196
197
	ccq = do_eqbs(irq->sch_token, state, q_no, start, cnt);
	rc = qdio_check_ccq(q, ccq);
198
199
	if ((ccq == 96) && (tmp_cnt != *cnt))
		rc = 0;
Frank Pavlic's avatar
Frank Pavlic committed
200
201
202
203
	if (rc == 1) {
		QDIO_DBF_TEXT5(1,trace,"eqAGAIN");
		goto again;
	}
204
205
206
207
208
209
210
211
212
213
214
215
216
	if (rc < 0) {
                QDIO_DBF_TEXT2(1,trace,"eqberr");
                sprintf(dbf_text,"%2x,%2x,%d,%d",tmp_cnt, *cnt, ccq, q_no);
                QDIO_DBF_TEXT2(1,trace,dbf_text);
		q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
				QDIO_STATUS_LOOK_FOR_ERROR,
				0, 0, 0, -1, -1, q->int_parm);
		return 0;
	}
	return (tmp_cnt - *cnt);
}

/* SQBS: set buffer states */
217
static int
218
219
220
221
222
223
224
225
226
227
228
229
230
231
qdio_do_sqbs(struct qdio_q *q, unsigned char state,
	     unsigned int *start, unsigned int *cnt)
{
	struct qdio_irq *irq;
	unsigned int tmp_cnt, q_no, ccq;
	int rc;
	char dbf_text[15];

	ccq = 0;
	tmp_cnt = *cnt;
	irq = (struct qdio_irq*)q->irq_ptr;
	q_no = q->q_no;
	if(!q->is_input_q)
		q_no += irq->no_input_qs;
Frank Pavlic's avatar
Frank Pavlic committed
232
again:
233
234
	ccq = do_sqbs(irq->sch_token, state, q_no, start, cnt);
	rc = qdio_check_ccq(q, ccq);
Frank Pavlic's avatar
Frank Pavlic committed
235
236
237
238
	if (rc == 1) {
		QDIO_DBF_TEXT5(1,trace,"sqAGAIN");
		goto again;
	}
239
	if (rc < 0) {
Ursula Braun's avatar
Ursula Braun committed
240
241
242
243
244
		QDIO_DBF_TEXT3(1,trace,"sqberr");
		sprintf(dbf_text,"%2x,%2x",tmp_cnt,*cnt);
		QDIO_DBF_TEXT3(1,trace,dbf_text);
		sprintf(dbf_text,"%d,%d",ccq,q_no);
		QDIO_DBF_TEXT3(1,trace,dbf_text);
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
		q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
				QDIO_STATUS_LOOK_FOR_ERROR,
				0, 0, 0, -1, -1, q->int_parm);
		return 0;
	}
	return (tmp_cnt - *cnt);
}

static inline int
qdio_set_slsb(struct qdio_q *q, unsigned int *bufno,
	      unsigned char state, unsigned int *count)
{
	volatile char *slsb;
	struct qdio_irq *irq;

	irq = (struct qdio_irq*)q->irq_ptr;
	if (!irq->is_qebsm) {
		slsb = (char *)&q->slsb.acc.val[(*bufno)];
		xchg(slsb, state);
		return 1;
	}
	return qdio_do_sqbs(q, state, bufno, count);
}

#ifdef CONFIG_QDIO_DEBUG
static inline void
qdio_trace_slsb(struct qdio_q *q)
{
	if (q->queue_type==QDIO_TRACE_QTYPE) {
		if (q->is_input_q)
			QDIO_DBF_HEX2(0,slsb_in,&q->slsb,
				      QDIO_MAX_BUFFERS_PER_Q);
		else
			QDIO_DBF_HEX2(0,slsb_out,&q->slsb,
				      QDIO_MAX_BUFFERS_PER_Q);
	}
}
#endif

static inline int
set_slsb(struct qdio_q *q, unsigned int *bufno,
	 unsigned char state, unsigned int *count)
{
	int rc;
#ifdef CONFIG_QDIO_DEBUG
	qdio_trace_slsb(q);
#endif
	rc = qdio_set_slsb(q, bufno, state, count);
#ifdef CONFIG_QDIO_DEBUG
	qdio_trace_slsb(q);
#endif
	return rc;
}
Linus Torvalds's avatar
Linus Torvalds committed
298
299
300
301
302
303
304
305
306
static inline int 
qdio_siga_sync(struct qdio_q *q, unsigned int gpr2,
	       unsigned int gpr3)
{
	int cc;

	QDIO_DBF_TEXT4(0,trace,"sigasync");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

307
	qdio_perf_stat_inc(&perf_stats.siga_syncs);
Linus Torvalds's avatar
Linus Torvalds committed
308

309
	cc = do_siga_sync(q->schid, gpr2, gpr3);
Linus Torvalds's avatar
Linus Torvalds committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
	if (cc)
		QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));

	return cc;
}

static inline int
qdio_siga_sync_q(struct qdio_q *q)
{
	if (q->is_input_q)
		return qdio_siga_sync(q, 0, q->mask);
	return qdio_siga_sync(q, q->mask, 0);
}

324
325
326
327
328
static int
__do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
{
       struct qdio_irq *irq;
       unsigned int fc = 0;
329
       unsigned long schid;
330
331
332

       irq = (struct qdio_irq *) q->irq_ptr;
       if (!irq->is_qebsm)
333
334
335
336
337
338
	       schid = *((u32 *)&q->schid);
       else {
	       schid = irq->sch_token;
	       fc |= 0x80;
       }
       return do_siga_output(schid, q->mask, busy_bit, fc);
339
340
}

Linus Torvalds's avatar
Linus Torvalds committed
341
342
343
344
/* 
 * returns QDIO_SIGA_ERROR_ACCESS_EXCEPTION as cc, when SIGA returns
 * an access exception 
 */
345
static int
Linus Torvalds's avatar
Linus Torvalds committed
346
347
348
349
350
351
qdio_siga_output(struct qdio_q *q)
{
	int cc;
	__u32 busy_bit;
	__u64 start_time=0;

352
	qdio_perf_stat_inc(&perf_stats.siga_outs);
Linus Torvalds's avatar
Linus Torvalds committed
353
354
355
356
357

	QDIO_DBF_TEXT4(0,trace,"sigaout");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	for (;;) {
358
		cc = __do_siga_output(q, &busy_bit);
Linus Torvalds's avatar
Linus Torvalds committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//QDIO_PRINT_ERR("cc=%x, busy=%x\n",cc,busy_bit);
		if ((cc==2) && (busy_bit) && (q->is_iqdio_q)) {
			if (!start_time) 
				start_time=NOW;
			if ((NOW-start_time)>QDIO_BUSY_BIT_PATIENCE)
				break;
		} else
			break;
	}
	
	if ((cc==2) && (busy_bit)) 
		cc |= QDIO_SIGA_ERROR_B_BIT_SET;

	if (cc)
		QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));

	return cc;
}

378
static int
Linus Torvalds's avatar
Linus Torvalds committed
379
380
381
382
383
384
385
qdio_siga_input(struct qdio_q *q)
{
	int cc;

	QDIO_DBF_TEXT4(0,trace,"sigain");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

386
	qdio_perf_stat_inc(&perf_stats.siga_ins);
Linus Torvalds's avatar
Linus Torvalds committed
387

388
	cc = do_siga_input(q->schid, q->mask);
Linus Torvalds's avatar
Linus Torvalds committed
389
390
391
392
393
394
395
396
	
	if (cc)
		QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));

	return cc;
}

/* locked by the locks in qdio_activate and qdio_cleanup */
397
static __u32 *
Linus Torvalds's avatar
Linus Torvalds committed
398
399
400
401
qdio_get_indicator(void)
{
	int i;

402
	for (i = 0; i < INDICATORS_PER_CACHELINE; i++)
Linus Torvalds's avatar
Linus Torvalds committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
		if (!indicator_used[i]) {
			indicator_used[i]=1;
			return indicators+i;
		}
	atomic_inc(&spare_indicator_usecount);
	return (__u32 * volatile) &spare_indicator;
}

/* locked by the locks in qdio_activate and qdio_cleanup */
static void 
qdio_put_indicator(__u32 *addr)
{
	int i;

	if ( (addr) && (addr!=&spare_indicator) ) {
		i=addr-indicators;
		indicator_used[i]=0;
	}
	if (addr == &spare_indicator)
		atomic_dec(&spare_indicator_usecount);
}

425
static inline void
Linus Torvalds's avatar
Linus Torvalds committed
426
427
428
429
430
431
432
433
tiqdio_clear_summary_bit(__u32 *location)
{
	QDIO_DBF_TEXT5(0,trace,"clrsummb");
	QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));

	xchg(location,0);
}

434
static inline  void
Linus Torvalds's avatar
Linus Torvalds committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
tiqdio_set_summary_bit(__u32 *location)
{
	QDIO_DBF_TEXT5(0,trace,"setsummb");
	QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));

	xchg(location,-1);
}

static inline void 
tiqdio_sched_tl(void)
{
	tasklet_hi_schedule(&tiqdio_tasklet);
}

449
static void
Linus Torvalds's avatar
Linus Torvalds committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
qdio_mark_tiq(struct qdio_q *q)
{
	unsigned long flags;

	QDIO_DBF_TEXT4(0,trace,"mark iq");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	spin_lock_irqsave(&ttiq_list_lock,flags);
	if (unlikely(atomic_read(&q->is_in_shutdown)))
		goto out_unlock;

	if (!q->is_input_q)
		goto out_unlock;

	if ((q->list_prev) || (q->list_next)) 
		goto out_unlock;

	if (!tiq_list) {
		tiq_list=q;
		q->list_prev=q;
		q->list_next=q;
	} else {
		q->list_next=tiq_list;
		q->list_prev=tiq_list->list_prev;
		tiq_list->list_prev->list_next=q;
		tiq_list->list_prev=q;
	}
	spin_unlock_irqrestore(&ttiq_list_lock,flags);

	tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
	tiqdio_sched_tl();
	return;
out_unlock:
	spin_unlock_irqrestore(&ttiq_list_lock,flags);
	return;
}

static inline void
qdio_mark_q(struct qdio_q *q)
{
	QDIO_DBF_TEXT4(0,trace,"mark q");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	if (unlikely(atomic_read(&q->is_in_shutdown)))
		return;

	tasklet_schedule(&q->tasklet);
}

499
static int
Linus Torvalds's avatar
Linus Torvalds committed
500
501
502
qdio_stop_polling(struct qdio_q *q)
{
#ifdef QDIO_USE_PROCESSING_STATE
503
504
505
       unsigned int tmp, gsf, count = 1;
       unsigned char state = 0;
       struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
Linus Torvalds's avatar
Linus Torvalds committed
506

507
	if (!atomic_xchg(&q->polling,0))
Linus Torvalds's avatar
Linus Torvalds committed
508
509
510
511
512
513
514
515
516
		return 1;

	QDIO_DBF_TEXT4(0,trace,"stoppoll");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	/* show the card that we are not polling anymore */
	if (!q->is_input_q)
		return 1;

517
518
519
520
       tmp = gsf = GET_SAVED_FRONTIER(q);
       tmp = ((tmp + QDIO_MAX_BUFFERS_PER_Q-1) & (QDIO_MAX_BUFFERS_PER_Q-1) );
       set_slsb(q, &tmp, SLSB_P_INPUT_NOT_INIT, &count);

Linus Torvalds's avatar
Linus Torvalds committed
521
522
523
524
525
526
	/* 
	 * we don't issue this SYNC_MEMORY, as we trust Rick T and
	 * moreover will not use the PROCESSING state under VM, so
	 * q->polling was 0 anyway
	 */
	/*SYNC_MEMORY;*/
527
528
529
530
531
532
       if (irq->is_qebsm) {
               count = 1;
               qdio_do_eqbs(q, &state, &gsf, &count);
       } else
               state = q->slsb.acc.val[gsf];
       if (state != SLSB_P_INPUT_PRIMED)
Linus Torvalds's avatar
Linus Torvalds committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
		return 1;
	/* 
	 * set our summary bit again, as otherwise there is a
	 * small window we can miss between resetting it and
	 * checking for PRIMED state 
	 */
	if (q->is_thinint_q)
		tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
	return 0;

#else /* QDIO_USE_PROCESSING_STATE */
	return 1;
#endif /* QDIO_USE_PROCESSING_STATE */
}

/* 
 * see the comment in do_QDIO and before qdio_reserve_q about the
 * sophisticated locking outside of unmark_q, so that we don't need to
 * disable the interrupts :-) 
*/
553
static void
Linus Torvalds's avatar
Linus Torvalds committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
qdio_unmark_q(struct qdio_q *q)
{
	unsigned long flags;

	QDIO_DBF_TEXT4(0,trace,"unmark q");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	if ((!q->list_prev)||(!q->list_next))
		return;

	if ((q->is_thinint_q)&&(q->is_input_q)) {
		/* iQDIO */
		spin_lock_irqsave(&ttiq_list_lock,flags);
		/* in case cleanup has done this already and simultanously
		 * qdio_unmark_q is called from the interrupt handler, we've
		 * got to check this in this specific case again */
		if ((!q->list_prev)||(!q->list_next))
			goto out;
		if (q->list_next==q) {
			/* q was the only interesting q */
			tiq_list=NULL;
			q->list_next=NULL;
			q->list_prev=NULL;
		} else {
			q->list_next->list_prev=q->list_prev;
			q->list_prev->list_next=q->list_next;
			tiq_list=q->list_next;
			q->list_next=NULL;
			q->list_prev=NULL;
		}
out:
		spin_unlock_irqrestore(&ttiq_list_lock,flags);
	}
}

static inline unsigned long 
tiqdio_clear_global_summary(void)
{
	unsigned long time;

	QDIO_DBF_TEXT5(0,trace,"clrglobl");
	
	time = do_clear_global_summary();

	QDIO_DBF_HEX5(0,trace,&time,sizeof(unsigned long));

	return time;
}


/************************* OUTBOUND ROUTINES *******************************/
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
static int
qdio_qebsm_get_outbound_buffer_frontier(struct qdio_q *q)
{
        struct qdio_irq *irq;
        unsigned char state;
        unsigned int cnt, count, ftc;

        irq = (struct qdio_irq *) q->irq_ptr;
        if ((!q->is_iqdio_q) && (!q->hydra_gives_outbound_pcis))
                SYNC_MEMORY;

        ftc = q->first_to_check;
        count = qdio_min(atomic_read(&q->number_of_buffers_used),
                        (QDIO_MAX_BUFFERS_PER_Q-1));
        if (count == 0)
                return q->first_to_check;
        cnt = qdio_do_eqbs(q, &state, &ftc, &count);
        if (cnt == 0)
                return q->first_to_check;
        switch (state) {
        case SLSB_P_OUTPUT_ERROR:
                QDIO_DBF_TEXT3(0,trace,"outperr");
                atomic_sub(cnt , &q->number_of_buffers_used);
                if (q->qdio_error)
                        q->error_status_flags |=
                                QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
                q->qdio_error = SLSB_P_OUTPUT_ERROR;
                q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
                q->first_to_check = ftc;
                break;
        case SLSB_P_OUTPUT_EMPTY:
                QDIO_DBF_TEXT5(0,trace,"outpempt");
                atomic_sub(cnt, &q->number_of_buffers_used);
                q->first_to_check = ftc;
                break;
        case SLSB_CU_OUTPUT_PRIMED:
                /* all buffers primed */
                QDIO_DBF_TEXT5(0,trace,"outpprim");
                break;
        default:
                break;
        }
        QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
        return q->first_to_check;
}

static int
qdio_qebsm_get_inbound_buffer_frontier(struct qdio_q *q)
{
        struct qdio_irq *irq;
        unsigned char state;
        int tmp, ftc, count, cnt;
        char dbf_text[15];


        irq = (struct qdio_irq *) q->irq_ptr;
        ftc = q->first_to_check;
        count = qdio_min(atomic_read(&q->number_of_buffers_used),
                        (QDIO_MAX_BUFFERS_PER_Q-1));
        if (count == 0)
                 return q->first_to_check;
        cnt = qdio_do_eqbs(q, &state, &ftc, &count);
        if (cnt == 0)
                 return q->first_to_check;
        switch (state) {
        case SLSB_P_INPUT_ERROR :
#ifdef CONFIG_QDIO_DEBUG
                QDIO_DBF_TEXT3(1,trace,"inperr");
                sprintf(dbf_text,"%2x,%2x",ftc,count);
                QDIO_DBF_TEXT3(1,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */
                if (q->qdio_error)
                        q->error_status_flags |=
                                QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
                q->qdio_error = SLSB_P_INPUT_ERROR;
                q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
                atomic_sub(cnt, &q->number_of_buffers_used);
                q->first_to_check = ftc;
                break;
        case SLSB_P_INPUT_PRIMED :
                QDIO_DBF_TEXT3(0,trace,"inptprim");
                sprintf(dbf_text,"%2x,%2x",ftc,count);
                QDIO_DBF_TEXT3(1,trace,dbf_text);
                tmp = 0;
                ftc = q->first_to_check;
#ifdef QDIO_USE_PROCESSING_STATE
		if (cnt > 1) {
			cnt -= 1;
			tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
			if (!tmp)
				break;
		}
		cnt = 1;
		tmp += set_slsb(q, &ftc,
			       SLSB_P_INPUT_PROCESSING, &cnt);
		atomic_set(&q->polling, 1);
#else
                tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
#endif
                atomic_sub(tmp, &q->number_of_buffers_used);
                q->first_to_check = ftc;
                break;
        case SLSB_CU_INPUT_EMPTY:
        case SLSB_P_INPUT_NOT_INIT:
        case SLSB_P_INPUT_PROCESSING:
                QDIO_DBF_TEXT5(0,trace,"inpnipro");
                break;
        default:
                break;
        }
        QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
        return q->first_to_check;
}
Linus Torvalds's avatar
Linus Torvalds committed
718

719
static int
Linus Torvalds's avatar
Linus Torvalds committed
720
721
qdio_get_outbound_buffer_frontier(struct qdio_q *q)
{
722
723
724
725
	struct qdio_irq *irq;
        volatile char *slsb;
        unsigned int count = 1;
        int first_not_to_check, f, f_mod_no;
Linus Torvalds's avatar
Linus Torvalds committed
726
727
728
729
730
	char dbf_text[15];

	QDIO_DBF_TEXT4(0,trace,"getobfro");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

731
732
733
734
	irq = (struct qdio_irq *) q->irq_ptr;
	if (irq->is_qebsm)
		return qdio_qebsm_get_outbound_buffer_frontier(q);

Linus Torvalds's avatar
Linus Torvalds committed
735
736
737
738
739
740
741
742
743
744
	slsb=&q->slsb.acc.val[0];
	f_mod_no=f=q->first_to_check;
	/* 
	 * f points to already processed elements, so f+no_used is correct...
	 * ... but: we don't check 128 buffers, as otherwise
	 * qdio_has_outbound_q_moved would return 0 
	 */
	first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
				      (QDIO_MAX_BUFFERS_PER_Q-1));

745
746
	if (((!q->is_iqdio_q) && (!q->hydra_gives_outbound_pcis)) ||
		 (q->queue_type == QDIO_IQDIO_QFMT_ASYNCH))
Linus Torvalds's avatar
Linus Torvalds committed
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
		SYNC_MEMORY;

check_next:
	if (f==first_not_to_check) 
		goto out;

	switch(slsb[f_mod_no]) {

        /* the adapter has not fetched the output yet */
	case SLSB_CU_OUTPUT_PRIMED:
		QDIO_DBF_TEXT5(0,trace,"outpprim");
		break;

	/* the adapter got it */
	case SLSB_P_OUTPUT_EMPTY:
		atomic_dec(&q->number_of_buffers_used);
		f++;
		f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
		QDIO_DBF_TEXT5(0,trace,"outpempt");
		goto check_next;

	case SLSB_P_OUTPUT_ERROR:
		QDIO_DBF_TEXT3(0,trace,"outperr");
		sprintf(dbf_text,"%x-%x-%x",f_mod_no,
			q->sbal[f_mod_no]->element[14].sbalf.value,
			q->sbal[f_mod_no]->element[15].sbalf.value);
		QDIO_DBF_TEXT3(1,trace,dbf_text);
		QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);

		/* kind of process the buffer */
777
		set_slsb(q, &f_mod_no, SLSB_P_OUTPUT_NOT_INIT, &count);
Linus Torvalds's avatar
Linus Torvalds committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802

		/* 
		 * we increment the frontier, as this buffer
		 * was processed obviously 
		 */
		atomic_dec(&q->number_of_buffers_used);
		f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);

		if (q->qdio_error)
			q->error_status_flags|=
				QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
		q->qdio_error=SLSB_P_OUTPUT_ERROR;
		q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;

		break;

	/* no new buffers */
	default:
		QDIO_DBF_TEXT5(0,trace,"outpni");
	}
out:
	return (q->first_to_check=f_mod_no);
}

/* all buffers are processed */
803
static int
Linus Torvalds's avatar
Linus Torvalds committed
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
qdio_is_outbound_q_done(struct qdio_q *q)
{
	int no_used;
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif

	no_used=atomic_read(&q->number_of_buffers_used);

#ifdef CONFIG_QDIO_DEBUG
	if (no_used) {
		sprintf(dbf_text,"oqisnt%02x",no_used);
		QDIO_DBF_TEXT4(0,trace,dbf_text);
	} else {
		QDIO_DBF_TEXT4(0,trace,"oqisdone");
	}
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
#endif /* CONFIG_QDIO_DEBUG */
	return (no_used==0);
}

825
static int
Linus Torvalds's avatar
Linus Torvalds committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
qdio_has_outbound_q_moved(struct qdio_q *q)
{
	int i;

	i=qdio_get_outbound_buffer_frontier(q);

	if ( (i!=GET_SAVED_FRONTIER(q)) ||
	     (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
		SAVE_FRONTIER(q,i);
		QDIO_DBF_TEXT4(0,trace,"oqhasmvd");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 1;
	} else {
		QDIO_DBF_TEXT4(0,trace,"oqhsntmv");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 0;
	}
}

845
static void
Linus Torvalds's avatar
Linus Torvalds committed
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
qdio_kick_outbound_q(struct qdio_q *q)
{
	int result;
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];

	QDIO_DBF_TEXT4(0,trace,"kickoutq");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
#endif /* CONFIG_QDIO_DEBUG */

	if (!q->siga_out)
		return;

	/* here's the story with cc=2 and busy bit set (thanks, Rick):
	 * VM's CP could present us cc=2 and busy bit set on SIGA-write
	 * during reconfiguration of their Guest LAN (only in HIPERS mode,
	 * QDIO mode is asynchronous -- cc=2 and busy bit there will take
	 * the queues down immediately; and not being under VM we have a
	 * problem on cc=2 and busy bit set right away).
	 *
	 * Therefore qdio_siga_output will try for a short time constantly,
	 * if such a condition occurs. If it doesn't change, it will
	 * increase the busy_siga_counter and save the timestamp, and
	 * schedule the queue for later processing (via mark_q, using the
	 * queue tasklet). __qdio_outbound_processing will check out the
	 * counter. If non-zero, it will call qdio_kick_outbound_q as often
	 * as the value of the counter. This will attempt further SIGA
	 * instructions. For each successful SIGA, the counter is
	 * decreased, for failing SIGAs the counter remains the same, after
	 * all.
	 * After some time of no movement, qdio_kick_outbound_q will
	 * finally fail and reflect corresponding error codes to call
	 * the upper layer module and have it take the queues down.
	 *
	 * Note that this is a change from the original HiperSockets design
	 * (saying cc=2 and busy bit means take the queues down), but in
	 * these days Guest LAN didn't exist... excessive cc=2 with busy bit
	 * conditions will still take the queues down, but the threshold is
	 * higher due to the Guest LAN environment.
	 */


	result=qdio_siga_output(q);

890
891
892
	switch (result) {
	case 0:
		/* went smooth this time, reset timestamp */
Linus Torvalds's avatar
Linus Torvalds committed
893
#ifdef CONFIG_QDIO_DEBUG
894
		QDIO_DBF_TEXT3(0,trace,"cc2reslv");
895
		sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
896
897
			atomic_read(&q->busy_siga_counter));
		QDIO_DBF_TEXT3(0,trace,dbf_text);
Linus Torvalds's avatar
Linus Torvalds committed
898
#endif /* CONFIG_QDIO_DEBUG */
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
		q->timing.busy_start=0;
		break;
	case (2|QDIO_SIGA_ERROR_B_BIT_SET):
		/* cc=2 and busy bit: */
		atomic_inc(&q->busy_siga_counter);

		/* if the last siga was successful, save
		 * timestamp here */
		if (!q->timing.busy_start)
			q->timing.busy_start=NOW;

		/* if we're in time, don't touch error_status_flags
		 * and siga_error */
		if (NOW-q->timing.busy_start<QDIO_BUSY_BIT_GIVE_UP) {
			qdio_mark_q(q);
Linus Torvalds's avatar
Linus Torvalds committed
914
			break;
915
916
		}
		QDIO_DBF_TEXT2(0,trace,"cc2REPRT");
Linus Torvalds's avatar
Linus Torvalds committed
917
#ifdef CONFIG_QDIO_DEBUG
918
		sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
919
920
			atomic_read(&q->busy_siga_counter));
		QDIO_DBF_TEXT3(0,trace,dbf_text);
Linus Torvalds's avatar
Linus Torvalds committed
921
#endif /* CONFIG_QDIO_DEBUG */
922
923
924
925
		/* else fallthrough and report error */
	default:
		/* for plain cc=1, 2 or 3: */
		if (q->siga_error)
Linus Torvalds's avatar
Linus Torvalds committed
926
			q->error_status_flags|=
927
928
929
930
931
				QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR;
		q->error_status_flags|=
			QDIO_STATUS_LOOK_FOR_ERROR;
		q->siga_error=result;
	}
Linus Torvalds's avatar
Linus Torvalds committed
932
933
}

934
static void
Linus Torvalds's avatar
Linus Torvalds committed
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
qdio_kick_outbound_handler(struct qdio_q *q)
{
	int start, end, real_end, count;
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif

	start = q->first_element_to_kick;
	/* last_move_ftc was just updated */
	real_end = GET_SAVED_FRONTIER(q);
	end = (real_end+QDIO_MAX_BUFFERS_PER_Q-1)&
		(QDIO_MAX_BUFFERS_PER_Q-1);
	count = (end+QDIO_MAX_BUFFERS_PER_Q+1-start)&
		(QDIO_MAX_BUFFERS_PER_Q-1);

#ifdef CONFIG_QDIO_DEBUG
	QDIO_DBF_TEXT4(0,trace,"kickouth");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	sprintf(dbf_text,"s=%2xc=%2x",start,count);
	QDIO_DBF_TEXT4(0,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */

	if (q->state==QDIO_IRQ_STATE_ACTIVE)
		q->handler(q->cdev,QDIO_STATUS_OUTBOUND_INT|
			   q->error_status_flags,
			   q->qdio_error,q->siga_error,q->q_no,start,count,
			   q->int_parm);

	/* for the next time: */
	q->first_element_to_kick=real_end;
	q->qdio_error=0;
	q->siga_error=0;
	q->error_status_flags=0;
}

971
static void
Linus Torvalds's avatar
Linus Torvalds committed
972
973
974
975
976
977
978
979
980
__qdio_outbound_processing(struct qdio_q *q)
{
	int siga_attempts;

	QDIO_DBF_TEXT4(0,trace,"qoutproc");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	if (unlikely(qdio_reserve_q(q))) {
		qdio_release_q(q);
981
		qdio_perf_stat_inc(&perf_stats.outbound_tl_runs_resched);
Linus Torvalds's avatar
Linus Torvalds committed
982
983
984
985
986
987
988
		/* as we're sissies, we'll check next time */
		if (likely(!atomic_read(&q->is_in_shutdown))) {
			qdio_mark_q(q);
			QDIO_DBF_TEXT4(0,trace,"busy,agn");
		}
		return;
	}
989
990
	qdio_perf_stat_inc(&perf_stats.outbound_tl_runs);
	qdio_perf_stat_inc(&perf_stats.tl_runs);
Linus Torvalds's avatar
Linus Torvalds committed
991
992
993
994
995
996
997
998
999
1000
1001
1002

	/* see comment in qdio_kick_outbound_q */
	siga_attempts=atomic_read(&q->busy_siga_counter);
	while (siga_attempts) {
		atomic_dec(&q->busy_siga_counter);
		qdio_kick_outbound_q(q);
		siga_attempts--;
	}

	if (qdio_has_outbound_q_moved(q))
		qdio_kick_outbound_handler(q);

1003
1004
1005
1006
1007
1008
1009
	if (q->queue_type == QDIO_ZFCP_QFMT) {
		if ((!q->hydra_gives_outbound_pcis) &&
		    (!qdio_is_outbound_q_done(q)))
			qdio_mark_q(q);
	}
	else if (((!q->is_iqdio_q) && (!q->is_pci_out)) ||
		 (q->queue_type == QDIO_IQDIO_QFMT_ASYNCH)) {
Linus Torvalds's avatar
Linus Torvalds committed
1010
		/* 
1011
1012
		 * make sure buffer switch from PRIMED to EMPTY is noticed
		 * and outbound_handler is called
Linus Torvalds's avatar
Linus Torvalds committed
1013
		 */
1014
1015
1016
1017
1018
1019
1020
1021
		if (qdio_is_outbound_q_done(q)) {
			del_timer(&q->timer);
		} else {
			if (!timer_pending(&q->timer))
				mod_timer(&q->timer, jiffies +
					  QDIO_FORCE_CHECK_TIMEOUT);
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1022
1023
1024
1025
1026

	qdio_release_q(q);
}

static void
1027
qdio_outbound_processing(unsigned long q)
Linus Torvalds's avatar
Linus Torvalds committed
1028
{
1029
	__qdio_outbound_processing((struct qdio_q *) q);
Linus Torvalds's avatar
Linus Torvalds committed
1030
1031
1032
1033
1034
}

/************************* INBOUND ROUTINES *******************************/


1035
static int
Linus Torvalds's avatar
Linus Torvalds committed
1036
1037
qdio_get_inbound_buffer_frontier(struct qdio_q *q)
{
1038
	struct qdio_irq *irq;
Linus Torvalds's avatar
Linus Torvalds committed
1039
1040
	int f,f_mod_no;
	volatile char *slsb;
1041
	unsigned int count = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
	int first_not_to_check;
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif /* CONFIG_QDIO_DEBUG */
#ifdef QDIO_USE_PROCESSING_STATE
	int last_position=-1;
#endif /* QDIO_USE_PROCESSING_STATE */

	QDIO_DBF_TEXT4(0,trace,"getibfro");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

1053
1054
1055
1056
	irq = (struct qdio_irq *) q->irq_ptr;
	if (irq->is_qebsm)
		return qdio_qebsm_get_inbound_buffer_frontier(q);

Linus Torvalds's avatar
Linus Torvalds committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
	slsb=&q->slsb.acc.val[0];
	f_mod_no=f=q->first_to_check;
	/* 
	 * we don't check 128 buffers, as otherwise qdio_has_inbound_q_moved
	 * would return 0 
	 */
	first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
				      (QDIO_MAX_BUFFERS_PER_Q-1));

	/* 
	 * we don't use this one, as a PCI or we after a thin interrupt
	 * will sync the queues
	 */
	/* SYNC_MEMORY;*/

check_next:
	f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
	if (f==first_not_to_check) 
		goto out;
	switch (slsb[f_mod_no]) {

	/* CU_EMPTY means frontier is reached */
	case SLSB_CU_INPUT_EMPTY:
		QDIO_DBF_TEXT5(0,trace,"inptempt");
		break;

	/* P_PRIMED means set slsb to P_PROCESSING and move on */
	case SLSB_P_INPUT_PRIMED:
		QDIO_DBF_TEXT5(0,trace,"inptprim");

#ifdef QDIO_USE_PROCESSING_STATE
		/* 
		 * as soon as running under VM, polling the input queues will
		 * kill VM in terms of CP overhead 
		 */
		if (q->siga_sync) {
1093
			set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1094
1095
1096
1097
1098
		} else {
			/* set the previous buffer to NOT_INIT. The current
			 * buffer will be set to PROCESSING at the end of
			 * this function to avoid further interrupts. */
			if (last_position>=0)
1099
1100
				set_slsb(q, &last_position,
					 SLSB_P_INPUT_NOT_INIT, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1101
1102
1103
1104
			atomic_set(&q->polling,1);
			last_position=f_mod_no;
		}
#else /* QDIO_USE_PROCESSING_STATE */
1105
		set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
#endif /* QDIO_USE_PROCESSING_STATE */
		/* 
		 * not needed, as the inbound queue will be synced on the next
		 * siga-r, resp. tiqdio_is_inbound_q_done will do the siga-s
		 */
		/*SYNC_MEMORY;*/
		f++;
		atomic_dec(&q->number_of_buffers_used);
		goto check_next;

	case SLSB_P_INPUT_NOT_INIT:
	case SLSB_P_INPUT_PROCESSING:
		QDIO_DBF_TEXT5(0,trace,"inpnipro");
		break;

	/* P_ERROR means frontier is reached, break and report error */
	case SLSB_P_INPUT_ERROR:
#ifdef CONFIG_QDIO_DEBUG
		sprintf(dbf_text,"inperr%2x",f_mod_no);
		QDIO_DBF_TEXT3(1,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */
		QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);

		/* kind of process the buffer */
1130
		set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157

		if (q->qdio_error)
			q->error_status_flags|=
				QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
		q->qdio_error=SLSB_P_INPUT_ERROR;
		q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;

		/* we increment the frontier, as this buffer
		 * was processed obviously */
		f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
		atomic_dec(&q->number_of_buffers_used);

#ifdef QDIO_USE_PROCESSING_STATE
		last_position=-1;
#endif /* QDIO_USE_PROCESSING_STATE */

		break;

	/* everything else means frontier not changed (HALTED or so) */
	default: 
		break;
	}
out:
	q->first_to_check=f_mod_no;

#ifdef QDIO_USE_PROCESSING_STATE
	if (last_position>=0)
1158
		set_slsb(q, &last_position, SLSB_P_INPUT_PROCESSING, &count);
Linus Torvalds's avatar
Linus Torvalds committed
1159
1160
1161
1162
1163
1164
1165
#endif /* QDIO_USE_PROCESSING_STATE */

	QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));

	return q->first_to_check;
}

1166
static int
Linus Torvalds's avatar
Linus Torvalds committed
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
qdio_has_inbound_q_moved(struct qdio_q *q)
{
	int i;

	i=qdio_get_inbound_buffer_frontier(q);
	if ( (i!=GET_SAVED_FRONTIER(q)) ||
	     (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
		SAVE_FRONTIER(q,i);
		if ((!q->siga_sync)&&(!q->hydra_gives_outbound_pcis))
			SAVE_TIMESTAMP(q);

		QDIO_DBF_TEXT4(0,trace,"inhasmvd");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 1;
	} else {
		QDIO_DBF_TEXT4(0,trace,"inhsntmv");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 0;
	}
}

/* means, no more buffers to be filled */
1189
static int
Linus Torvalds's avatar
Linus Torvalds committed
1190
1191
1192
tiqdio_is_inbound_q_done(struct qdio_q *q)
{
	int no_used;
1193
1194
1195
1196
	unsigned int start_buf, count;
	unsigned char state = 0;
	struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;

Linus Torvalds's avatar
Linus Torvalds committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif

	no_used=atomic_read(&q->number_of_buffers_used);

	/* propagate the change from 82 to 80 through VM */
	SYNC_MEMORY;

#ifdef CONFIG_QDIO_DEBUG
	if (no_used) {
		sprintf(dbf_text,"iqisnt%02x",no_used);
		QDIO_DBF_TEXT4(0,trace,dbf_text);
	} else {
		QDIO_DBF_TEXT4(0,trace,"iniqisdo");
	}
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
#endif /* CONFIG_QDIO_DEBUG */

	if (!no_used)
		return 1;
1218
1219
1220
1221
1222
1223
1224
	if (irq->is_qebsm) {
		count = 1;
		start_buf = q->first_to_check;
		qdio_do_eqbs(q, &state, &start_buf, &count);
	} else
		state = q->slsb.acc.val[q->first_to_check];
	if (state != SLSB_P_INPUT_PRIMED)
Linus Torvalds's avatar
Linus Torvalds committed
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
		/* 
		 * nothing more to do, if next buffer is not PRIMED.
		 * note that we did a SYNC_MEMORY before, that there
		 * has been a sychnronization.
		 * we will return 0 below, as there is nothing to do
		 * (stop_polling not necessary, as we have not been
		 * using the PROCESSING state 
		 */
		return 0;

	/* 
	 * ok, the next input buffer is primed. that means, that device state 
	 * change indicator and adapter local summary are set, so we will find
	 * it next time.
	 * we will return 0 below, as there is nothing to do, except scheduling
	 * ourselves for the next time. 
	 */
	tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
	tiqdio_sched_tl();
	return 0;
}

1247
static int
Linus Torvalds's avatar
Linus Torvalds committed
1248
1249
1250
qdio_is_inbound_q_done(struct qdio_q *q)
{
	int no_used;
1251
1252
1253
1254
	unsigned int start_buf, count;
	unsigned char state = 0;
	struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;

Linus Torvalds's avatar
Linus Torvalds committed
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif

	no_used=atomic_read(&q->number_of_buffers_used);

	/* 
	 * we need that one for synchronization with the adapter, as it
	 * does a kind of PCI avoidance 
	 */
	SYNC_MEMORY;

	if (!no_used) {
		QDIO_DBF_TEXT4(0,trace,"inqisdnA");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 1;
	}
1272
1273
1274
1275
1276
1277
1278
	if (irq->is_qebsm) {
		count = 1;
		start_buf = q->first_to_check;
		qdio_do_eqbs(q, &state, &start_buf, &count);
	} else
		state = q->slsb.acc.val[q->first_to_check];
	if (state == SLSB_P_INPUT_PRIMED) {
Linus Torvalds's avatar
Linus Torvalds committed
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
		/* we got something to do */
		QDIO_DBF_TEXT4(0,trace,"inqisntA");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		return 0;
	}

	/* on VM, we don't poll, so the q is always done here */
	if (q->siga_sync)
		return 1;
	if (q->hydra_gives_outbound_pcis)
		return 1;

	/* 
	 * at this point we know, that inbound first_to_check
	 * has (probably) not moved (see qdio_inbound_processing) 
	 */
	if (NOW>GET_SAVED_TIMESTAMP(q)+q->timing.threshold) {
#ifdef CONFIG_QDIO_DEBUG
		QDIO_DBF_TEXT4(0,trace,"inqisdon");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
		QDIO_DBF_TEXT4(0,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */
		return 1;
	} else {
#ifdef CONFIG_QDIO_DEBUG
		QDIO_DBF_TEXT4(0,trace,"inqisntd");
		QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
		sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
		QDIO_DBF_TEXT4(0,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */
		return 0;
	}
}

1314
static void
Linus Torvalds's avatar
Linus Torvalds committed
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
qdio_kick_inbound_handler(struct qdio_q *q)
{
	int count, start, end, real_end, i;
#ifdef CONFIG_QDIO_DEBUG
	char dbf_text[15];
#endif

	QDIO_DBF_TEXT4(0,trace,"kickinh");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

  	start=q->first_element_to_kick;
 	real_end=q->first_to_check;
 	end=(real_end+QDIO_MAX_BUFFERS_PER_Q-1)&(QDIO_MAX_BUFFERS_PER_Q-1);
 
 	i=start;
	count=0;
 	while (1) {
 		count++;
 		if (i==end)
			break;
 		i=(i+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
 	}

#ifdef CONFIG_QDIO_DEBUG
	sprintf(dbf_text,"s=%2xc=%2x",start,count);
	QDIO_DBF_TEXT4(0,trace,dbf_text);
#endif /* CONFIG_QDIO_DEBUG */

	if (likely(q->state==QDIO_IRQ_STATE_ACTIVE))
		q->handler(q->cdev,
			   QDIO_STATUS_INBOUND_INT|q->error_status_flags,
			   q->qdio_error,q->siga_error,q->q_no,start,count,
			   q->int_parm);

	/* for the next time: */
	q->first_element_to_kick=real_end;
	q->qdio_error=0;
	q->siga_error=0;
	q->error_status_flags=0;

1355
	qdio_perf_stat_inc(&perf_stats.inbound_cnt);
Linus Torvalds's avatar
Linus Torvalds committed
1356
1357
}

1358
static void
Linus Torvalds's avatar
Linus Torvalds committed
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
__tiqdio_inbound_processing(struct qdio_q *q, int spare_ind_was_set)
{
	struct qdio_irq *irq_ptr;
	struct qdio_q *oq;
	int i;

	QDIO_DBF_TEXT4(0,trace,"iqinproc");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	/* 
	 * we first want to reserve the q, so that we know, that we don't
	 * interrupt ourselves and call qdio_unmark_q, as is_in_shutdown might
	 * be set 
	 */
	if (unlikely(qdio_reserve_q(q))) {
		qdio_release_q(q);
1375
		qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
Linus Torvalds's avatar
Linus Torvalds committed
1376
1377
1378
1379
1380
1381
1382
		/* 
		 * as we might just be about to stop polling, we make
		 * sure that we check again at least once more 
		 */
		tiqdio_sched_tl();
		return;
	}
1383
	qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs);
Linus Torvalds's avatar
Linus Torvalds committed
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
	if (unlikely(atomic_read(&q->is_in_shutdown))) {
		qdio_unmark_q(q);
		goto out;
	}

	/* 
	 * we reset spare_ind_was_set, when the queue does not use the
	 * spare indicator
	 */
	if (spare_ind_was_set)
		spare_ind_was_set = (q->dev_st_chg_ind == &spare_indicator);

	if (!(*(q->dev_st_chg_ind)) && !spare_ind_was_set)
		goto out;
	/*
	 * q->dev_st_chg_ind is the indicator, be it shared or not.
	 * only clear it, if indicator is non-shared
	 */
1402
	if (q->dev_st_chg_ind != &spare_indicator)
Linus Torvalds's avatar
Linus Torvalds committed
1403
1404
1405
1406
1407
		tiqdio_clear_summary_bit((__u32*)q->dev_st_chg_ind);

	if (q->hydra_gives_outbound_pcis) {
		if (!q->siga_sync_done_on_thinints) {
			SYNC_MEMORY_ALL;
1408
		} else if (!q->siga_sync_done_on_outb_tis) {
Linus Torvalds's avatar
Linus Torvalds committed
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
			SYNC_MEMORY_ALL_OUTB;
		}
	} else {
		SYNC_MEMORY;
	}
	/*
	 * maybe we have to do work on our outbound queues... at least
	 * we have to check the outbound-int-capable thinint-capable
	 * queues
	 */
	if (q->hydra_gives_outbound_pcis) {
		irq_ptr = (struct qdio_irq*)q->irq_ptr;
		for (i=0;i<irq_ptr->no_output_qs;i++) {
			oq = irq_ptr->output_qs[i];
1423
			if (!qdio_is_outbound_q_done(oq)) {
1424
				qdio_perf_stat_dec(&perf_stats.tl_runs);
Linus Torvalds's avatar
Linus Torvalds committed
1425
				__qdio_outbound_processing(oq);
1426
			}
Linus Torvalds's avatar
Linus Torvalds committed
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
		}
	}

	if (!qdio_has_inbound_q_moved(q))
		goto out;

	qdio_kick_inbound_handler(q);
	if (tiqdio_is_inbound_q_done(q))
		if (!qdio_stop_polling(q)) {
			/* 
			 * we set the flags to get into the stuff next time,
			 * see also comment in qdio_stop_polling 
			 */
			tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
			tiqdio_sched_tl();
		}
out:
	qdio_release_q(q);
}

static void
1448
tiqdio_inbound_processing(unsigned long q)
Linus Torvalds's avatar
Linus Torvalds committed
1449
{
1450
1451
	__tiqdio_inbound_processing((struct qdio_q *) q,
				    atomic_read(&spare_indicator_usecount));
Linus Torvalds's avatar
Linus Torvalds committed
1452
1453
}

1454
static void
Linus Torvalds's avatar
Linus Torvalds committed
1455
1456
1457
1458
1459
1460
1461
1462
1463
__qdio_inbound_processing(struct qdio_q *q)
{
	int q_laps=0;

	QDIO_DBF_TEXT4(0,trace,"qinproc");
	QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));

	if (unlikely(qdio_reserve_q(q))) {
		qdio_release_q(q);
1464
		qdio_perf_stat_inc(&perf_stats.inbound_tl_runs_resched);
Linus Torvalds's avatar
Linus Torvalds committed
1465
1466
1467
1468
1469
1470
1471
		/* as we're sissies, we'll check next time */
		if (likely(!atomic_read(&q->is_in_shutdown))) {
			qdio_mark_q(q);
			QDIO_DBF_TEXT4(0,trace,"busy,agn");
		}
		return;
	}
1472
1473
	qdio_perf_stat_inc(&perf_stats.inbound_tl_runs);
	qdio_perf_stat_inc(&perf_stats.tl_runs);
Linus Torvalds's avatar
Linus Torvalds committed
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493

again:
	if (qdio_has_inbound_q_moved(q)) {
		qdio_kick_inbound_handler(q);
		if (!qdio_stop_polling(q)) {
			q_laps++;
			if (q_laps<QDIO_Q_LAPS) 
				goto again;
		}
		qdio_mark_q(q);
	} else {
		if (!qdio_is_inbound_q_done(q)) 
                        /* means poll time is not yet over */
			qdio_mark_q(q);
	}

	qdio_release_q(q);
}

static void
1494
qdio_inbound_processing(unsigned long q)
Linus Torvalds's avatar
Linus Torvalds committed
1495
{
1496
	__qdio_inbound_processing((struct qdio_q *) q);
Linus Torvalds's avatar
Linus Torvalds committed
1497
1498
1499
1500
1501
}

/************************* MAIN ROUTINES *******************************/

#ifdef QDIO_USE_PROCESSING_STATE
1502
static int
Linus Torvalds's avatar
Linus Torvalds committed
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
tiqdio_reset_processing_state(struct qdio_q *q, int q_laps)
{
	if (!q) {
		tiqdio_sched_tl();
		return 0;
	}

	/* 
	 * under VM, we have not used the PROCESSING state, so no
	 * need to stop polling 
	 */
	if (q->siga_sync)
		return 2;

	if (unlikely(qdio_reserve_q(q))) {
		qdio_release_q(q);
1519
		qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
Linus Torvalds's avatar
Linus Torvalds committed
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
		/* 
		 * as we might just be about to stop polling, we make
		 * sure that we check again at least once more 
		 */
		
		/* 
		 * sanity -- we'd get here without setting the
		 * dev st chg ind 
		 */
		tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
		tiqdio_sched_tl();
		return 0;
	}
	if (qdio_stop_polling(q)) {
		qdio_release_q(q);
		return 2;
	}		
	if (q_laps<QDIO_Q_LAPS-1) {
		qdio_release_q(q);
		return 3;
	}
	/* 
	 * we set the flags to get into the stuff
	 * next time, see also comment in qdio_stop_polling 
	 */
	tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
	tiqdio_sched_tl();
	qdio_release_q(q);
	return 1;
	
}
#endif /* QDIO_USE_PROCESSING_STATE */

1553
static void
Linus Torvalds's avatar
Linus Torvalds committed
1554
1555
1556
1557
1558
1559
1560
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
1601
1602
1603
1604
1605
1606
1607
1608
1609
tiqdio_inbound_checks(void)
{
	struct qdio_q *q;
	int spare_ind_was_set=0;
#ifdef QDIO_USE_PROCESSING_STATE
	int q_laps=0;
#endif /* QDIO_USE_PROCESSING_STATE */

	QDIO_DBF_TEXT4(0,trace,"iqdinbck");
	QDIO_DBF_TEXT5(0,trace,"iqlocsum");

#ifdef QDIO_USE_PROCESSING_STATE
again:
#endif /* QDIO_USE_PROCESSING_STATE */

	/* when the spare indicator is used and set, save that and clear it */
	if ((atomic_read(&spare_indicator_usecount)) && spare_indicator) {
		spare_ind_was_set = 1;
		tiqdio_clear_summary_bit((__u32*)&spare_indicator);
	}

	q=(struct qdio_q*)tiq_list;
	do {
		if (!q)
			break;
		__tiqdio_inbound_processing(q, spare_ind_was_set);
		q=(struct qdio_q*)q->list_next;
	} while (q!=(struct qdio_q*)tiq_list);

#ifdef QDIO_USE_PROCESSING_STATE
	q=(struct qdio_q*)tiq_list;
	do {
		int ret;

		ret = tiqdio_reset_processing_state(q, q_laps);
		switch (ret) {
		case 0:
			return;
		case 1:
			q_laps++;
		case 2:
			q = (struct qdio_q*)q->list_next;
			break;
		default:
			q_laps++;
			goto again;
		}
	} while (q!=(struct qdio_q*)tiq_list);
#endif /* QDIO_USE_PROCESSING_STATE */
}

static void
tiqdio_tl(unsigned long data)
{
	QDIO_DBF_TEXT4(0,trace,"iqdio_tl");

1610
	qdio_perf_stat_inc(&perf_stats.tl_runs);
Linus Torvalds's avatar
Linus Torvalds committed
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620

	tiqdio_inbound_checks();
}

/********************* GENERAL HELPER_ROUTINES ***********************/

static void
qdio_release_irq_memory(struct qdio_irq *irq_ptr)
{
	int i;
1621
	struct qdio_q *q;
Linus Torvalds's avatar
Linus Torvalds committed
1622

1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
	for (i = 0; i < QDIO_MAX_QUEUES_PER_IRQ; i++) {
		q = irq_ptr->input_qs[i];
		if (q) {
			free_page((unsigned long) q->slib);
			kmem_cache_free(qdio_q_cache, q);
		}
		q = irq_ptr->output_qs[i];
		if (q) {
			free_page((unsigned long) q->slib);
			kmem_cache_free(qdio_q_cache, q);
		}
Linus Torvalds's avatar
Linus Torvalds committed
1634
	}
1635
	free_page((unsigned long) irq_ptr->qdr);
1636
	free_page((unsigned long) irq_ptr);
Linus Torvalds's avatar
Linus Torvalds committed
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
}

static void
qdio_set_impl_params(struct qdio_irq *irq_ptr,
		     unsigned int qib_param_field_format,
		     /* pointer to 128 bytes or NULL, if no param field */
		     unsigned char *qib_param_field,
		     /* pointer to no_queues*128 words of data or NULL */
		     unsigned int no_input_qs,
		     unsigned int no_output_qs,
		     unsigned long *input_slib_elements,
		     unsigned long *output_slib_elements)
{
	int i,j;

	if (!irq_ptr)
		return;

	irq_ptr->qib.pfmt=qib_param_field_format;
	if (qib_param_field)
		memcpy(irq_ptr->qib.parm,qib_param_field,
		       QDIO_MAX_BUFFERS_PER_Q);

	if (input_slib_elements)
		for (i=0;i<no_input_qs;i++) {
			for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
				irq_ptr->input_qs[i]->slib->slibe[j].parms=
					input_slib_elements[
						i*QDIO_MAX_BUFFERS_PER_Q+j];
		}
	if (output_slib_elements)
		for (i=0;i<no_output_qs;i++) {
			for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
				irq_ptr->output_qs[i]->slib->slibe[j].parms=
					output_slib_elements[
						i*QDIO_MAX_BUFFERS_PER_Q+j];
		}
}

static int
qdio_alloc_qs(struct qdio_irq *irq_ptr,
	      int no_input_qs, int no_output_qs)
{
	int i;
	struct qdio_q *q;

1683
1684
1685
1686
1687
	for (i = 0; i < no_input_qs; i++) {
		q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
		if (!q)
			return -ENOMEM;
		memset(q, 0, sizeof(*q));
Linus Torvalds's avatar
Linus Torvalds committed
1688

1689
		q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1690
		if (!q->slib) {
1691
1692
			kmem_cache_free(qdio_q_cache, q);
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1693
1694
1695
1696
		}
		irq_ptr->input_qs[i]=q;
	}

1697
1698
1699
1700
1701
	for (i = 0; i < no_output_qs; i++) {
		q = kmem_cache_alloc(qdio_q_cache, GFP_KERNEL);
		if (!q)
			return -ENOMEM;
		memset(q, 0, sizeof(*q));
Linus Torvalds's avatar
Linus Torvalds committed
1702

1703
		q->slib = (struct slib *) __get_free_page(GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
1704
		if (!q->slib) {
1705
1706
			kmem_cache_free(qdio_q_cache, q);
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1707
1708
1709
		}
		irq_ptr->output_qs[i]=q;
	}
1710
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
}

static void
qdio_fill_qs(struct qdio_irq *irq_ptr, struct ccw_device *cdev,
       	     int no_input_qs, int no_output_qs,
	     qdio_handler_t *input_handler,
	     qdio_handler_t *output_handler,
	     unsigned long int_parm,int q_format,
	     unsigned long flags,
	     void **inbound_sbals_array,
	     void **outbound_sbals_array)
{
	struct qdio_q *q;
	int i,j;
	char dbf_text[20]; /* see qdio_initialize */
	void *ptr;
	int available;

1729
	sprintf(dbf_text,"qfqs%4x",cdev->private->schid.sch_no);
Linus Torvalds's avatar
Linus Torvalds committed
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
	QDIO_DBF_TEXT0(0,setup,dbf_text);
	for (i=0;i<no_input_qs;i++) {
		q=irq_ptr->input_qs[i];

		memset(q,0,((char*)&q->slib)-((char*)q));
		sprintf(dbf_text,"in-q%4x",i);
		QDIO_DBF_TEXT0(0,setup,dbf_text);
		QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));

		memset(q->slib,0,PAGE_SIZE);
		q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);

		available=0;

		for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
			q->sbal[j]=*(inbound_sbals_array++);

                q->queue_type=q_format;
		q->int_parm=int_parm;
1749
		q->schid = irq_ptr->schid;
Linus Torvalds's avatar
Linus Torvalds committed
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
		q->irq_ptr = irq_ptr;
		q->cdev = cdev;
		q->mask=1<<(31-i);
		q->q_no=i;
		q->is_input_q=1;
		q->first_to_check=0;
		q->last_move_ftc=0;
		q->handler=input_handler;
		q->dev_st_chg_ind=irq_ptr->dev_st_chg_ind;

		/* q->is_thinint_q isn't valid at this time, but
1761
1762
1763
1764
1765
1766
1767
1768
		 * irq_ptr->is_thinint_irq is
		 */
		if (irq_ptr->is_thinint_irq)
			tasklet_init(&q->tasklet, tiqdio_inbound_processing,
				     (unsigned long) q);
		else
			tasklet_init(&q->tasklet, qdio_inbound_processing,
				     (unsigned long) q);
Linus Torvalds's avatar
Linus Torvalds committed
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798

		/* actually this is not used for inbound queues. yet. */
		atomic_set(&q->busy_siga_counter,0);
		q->timing.busy_start=0;

/*		for (j=0;j<QDIO_STATS_NUMBER;j++)
			q->timing.last_transfer_times[j]=(qdio_get_micros()/
							  QDIO_STATS_NUMBER)*j;
		q->timing.last_transfer_index=QDIO_STATS_NUMBER-1;
*/

		/* fill in slib */
		if (i>0) irq_ptr->input_qs[i-1]->slib->nsliba=
				 (unsigned long)(q->slib);
		q->slib->sla=(unsigned long)(q->sl);
		q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);

		/* fill in sl */
		for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
			q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);

		QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
		ptr=(void*)q->sl;
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
		ptr=(void*)&q->slsb;
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
		ptr=(void*)q->sbal[0];
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));

		/* fill in slsb */
1799
1800
1801
1802
1803
		if (!irq_ptr->is_qebsm) {
                        unsigned int count = 1;
                        for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
                                set_slsb(q, &j, SLSB_P_INPUT_NOT_INIT, &count);
                }
Linus Torvalds's avatar
Linus Torvalds committed
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
	}

	for (i=0;i<no_output_qs;i++) {
		q=irq_ptr->output_qs[i];
		memset(q,0,((char*)&q->slib)-((char*)q));

		sprintf(dbf_text,"outq%4x",i);
		QDIO_DBF_TEXT0(0,setup,dbf_text);
		QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));

		memset(q->slib,0,PAGE_SIZE);
		q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);

		available=0;
		
		for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
			q->sbal[j]=*(outbound_sbals_array++);

                q->queue_type=q_format;
1823
1824
1825
1826
		if ((q->queue_type == QDIO_IQDIO_QFMT) &&
		    (no_output_qs > 1) &&
		    (i == no_output_qs-1))
			q->queue_type = QDIO_IQDIO_QFMT_ASYNCH;
Linus Torvalds's avatar
Linus Torvalds committed
1827
1828
		q->int_parm=int_parm;
		q->is_input_q=0;
1829
		q->is_pci_out = 0;
1830
		q->schid = irq_ptr->schid;
Linus Torvalds's avatar
Linus Torvalds committed
1831
1832
1833
1834
1835
1836
1837
1838
		q->cdev = cdev;
		q->irq_ptr = irq_ptr;
		q->mask=1<<(31-i);
		q->q_no=i;
		q->first_to_check=0;
		q->last_move_ftc=0;
		q->handler=output_handler;

1839
1840
1841
1842
		tasklet_init(&q->tasklet, qdio_outbound_processing,
			     (unsigned long) q);
		setup_timer(&q->timer, qdio_outbound_processing,
			    (unsigned long) q);
Linus Torvalds's avatar
Linus Torvalds committed
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865

		atomic_set(&q->busy_siga_counter,0);
		q->timing.busy_start=0;

		/* fill in slib */
		if (i>0) irq_ptr->output_qs[i-1]->slib->nsliba=
				 (unsigned long)(q->slib);
		q->slib->sla=(unsigned long)(q->sl);
		q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);

		/* fill in sl */
		for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
			q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);

		QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
		ptr=(void*)q->sl;
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
		ptr=(void*)&q->slsb;
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
		ptr=(void*)q->sbal[0];
		QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));

		/* fill in slsb */
1866
1867
1868
1869
1870
                if (!irq_ptr->is_qebsm) {
                        unsigned int count = 1;
                        for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
                                set_slsb(q, &j, SLSB_P_OUTPUT_NOT_INIT, &count);
                }
Linus Torvalds's avatar
Linus Torvalds committed
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
	}
}

static void
qdio_fill_thresholds(struct qdio_irq *irq_ptr,
		     unsigned int no_input_qs,
		     unsigned int no_output_qs,
		     unsigned int min_input_threshold,
		     unsigned int max_input_threshold,
		     unsigned int min_output_threshold,
		     unsigned int max_output_threshold)
{
	int i;
	struct qdio_q *q;

	for (i=0;i<no_input_qs;i++) {
		q=irq_ptr->input_qs[i];
		q->timing.threshold=max_input_threshold;
/*		for (j=0;j<QDIO_STATS_CLASSES;j++) {
			q->threshold_classes[j].threshold=
				min_input_threshold+
				(max_input_threshold-min_input_threshold)/
				QDIO_STATS_CLASSES;
		}
		qdio_use_thresholds(q,QDIO_STATS_CLASSES/2);*/