Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[linux-2.6] / drivers / s390 / cio / qdio.c
1 /*
2  *
3  * linux/drivers/s390/cio/qdio.c
4  *
5  * Linux for S/390 QDIO base support, Hipersocket base support
6  * version 2
7  *
8  * Copyright 2000,2002 IBM Corporation
9  * Author(s):             Utz Bacher <utz.bacher@de.ibm.com>
10  * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
11  *
12  * Restriction: only 63 iqdio subchannels would have its own indicator,
13  * after that, subsequent subchannels share one indicator
14  *
15  *
16  *
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2, or (at your option)
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31  */
32
33 #include <linux/module.h>
34 #include <linux/init.h>
35
36 #include <linux/slab.h>
37 #include <linux/kernel.h>
38 #include <linux/proc_fs.h>
39 #include <linux/timer.h>
40 #include <linux/mempool.h>
41
42 #include <asm/ccwdev.h>
43 #include <asm/io.h>
44 #include <asm/atomic.h>
45 #include <asm/semaphore.h>
46 #include <asm/timex.h>
47
48 #include <asm/debug.h>
49 #include <asm/s390_rdev.h>
50 #include <asm/qdio.h>
51
52 #include "cio.h"
53 #include "css.h"
54 #include "device.h"
55 #include "airq.h"
56 #include "qdio.h"
57 #include "ioasm.h"
58 #include "chsc.h"
59
60 /****************** MODULE PARAMETER VARIABLES ********************/
61 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
62 MODULE_DESCRIPTION("QDIO base support version 2, " \
63                    "Copyright 2000 IBM Corporation");
64 MODULE_LICENSE("GPL");
65
66 /******************** HERE WE GO ***********************************/
67
68 static const char version[] = "QDIO base support version 2";
69
70 static int qdio_performance_stats = 0;
71 static int proc_perf_file_registration;
72 static struct qdio_perf_stats perf_stats;
73
74 static int hydra_thinints;
75 static int is_passthrough = 0;
76 static int omit_svs;
77
78 static int indicator_used[INDICATORS_PER_CACHELINE];
79 static __u32 * volatile indicators;
80 static __u32 volatile spare_indicator;
81 static atomic_t spare_indicator_usecount;
82 #define QDIO_MEMPOOL_SCSSC_ELEMENTS 2
83 static mempool_t *qdio_mempool_scssc;
84
85 static debug_info_t *qdio_dbf_setup;
86 static debug_info_t *qdio_dbf_sbal;
87 static debug_info_t *qdio_dbf_trace;
88 static debug_info_t *qdio_dbf_sense;
89 #ifdef CONFIG_QDIO_DEBUG
90 static debug_info_t *qdio_dbf_slsb_out;
91 static debug_info_t *qdio_dbf_slsb_in;
92 #endif /* CONFIG_QDIO_DEBUG */
93
94 /* iQDIO stuff: */
95 static volatile struct qdio_q *tiq_list=NULL; /* volatile as it could change
96                                                  during a while loop */
97 static DEFINE_SPINLOCK(ttiq_list_lock);
98 static int register_thinint_result;
99 static void tiqdio_tl(unsigned long);
100 static DECLARE_TASKLET(tiqdio_tasklet,tiqdio_tl,0);
101
102 /* not a macro, as one of the arguments is atomic_read */
103 static inline int 
104 qdio_min(int a,int b)
105 {
106         if (a<b)
107                 return a;
108         else
109                 return b;
110 }
111
112 /***************** SCRUBBER HELPER ROUTINES **********************/
113 #ifdef CONFIG_64BIT
114 static inline void qdio_perf_stat_inc(atomic64_t *count)
115 {
116         if (qdio_performance_stats)
117                 atomic64_inc(count);
118 }
119
120 static inline void qdio_perf_stat_dec(atomic64_t *count)
121 {
122         if (qdio_performance_stats)
123                 atomic64_dec(count);
124 }
125 #else /* CONFIG_64BIT */
126 static inline void qdio_perf_stat_inc(atomic_t *count)
127 {
128         if (qdio_performance_stats)
129                 atomic_inc(count);
130 }
131
132 static inline void qdio_perf_stat_dec(atomic_t *count)
133 {
134         if (qdio_performance_stats)
135                 atomic_dec(count);
136 }
137 #endif /* CONFIG_64BIT */
138
139 static inline __u64 
140 qdio_get_micros(void)
141 {
142         return (get_clock() >> 12); /* time>>12 is microseconds */
143 }
144
145 /* 
146  * unfortunately, we can't just xchg the values; in do_QDIO we want to reserve
147  * the q in any case, so that we'll not be interrupted when we are in
148  * qdio_mark_tiq... shouldn't have a really bad impact, as reserving almost
149  * ever works (last famous words) 
150  */
151 static inline int 
152 qdio_reserve_q(struct qdio_q *q)
153 {
154         return atomic_add_return(1,&q->use_count) - 1;
155 }
156
157 static inline void 
158 qdio_release_q(struct qdio_q *q)
159 {
160         atomic_dec(&q->use_count);
161 }
162
163 /*check ccq  */
164 static int
165 qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
166 {
167         char dbf_text[15];
168
169         if (ccq == 0 || ccq == 32 || ccq == 96)
170                 return 0;
171         if (ccq == 97)
172                 return 1;
173         /*notify devices immediately*/
174         sprintf(dbf_text,"%d", ccq);
175         QDIO_DBF_TEXT2(1,trace,dbf_text);
176         return -EIO;
177 }
178 /* EQBS: extract buffer states */
179 static int
180 qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
181              unsigned int *start, unsigned int *cnt)
182 {
183         struct qdio_irq *irq;
184         unsigned int tmp_cnt, q_no, ccq;
185         int rc ;
186         char dbf_text[15];
187
188         ccq = 0;
189         tmp_cnt = *cnt;
190         irq = (struct qdio_irq*)q->irq_ptr;
191         q_no = q->q_no;
192         if(!q->is_input_q)
193                 q_no += irq->no_input_qs;
194 again:
195         ccq = do_eqbs(irq->sch_token, state, q_no, start, cnt);
196         rc = qdio_check_ccq(q, ccq);
197         if (rc == 1) {
198                 QDIO_DBF_TEXT5(1,trace,"eqAGAIN");
199                 goto again;
200         }
201         if (rc < 0) {
202                 QDIO_DBF_TEXT2(1,trace,"eqberr");
203                 sprintf(dbf_text,"%2x,%2x,%d,%d",tmp_cnt, *cnt, ccq, q_no);
204                 QDIO_DBF_TEXT2(1,trace,dbf_text);
205                 q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
206                                 QDIO_STATUS_LOOK_FOR_ERROR,
207                                 0, 0, 0, -1, -1, q->int_parm);
208                 return 0;
209         }
210         return (tmp_cnt - *cnt);
211 }
212
213 /* SQBS: set buffer states */
214 static int
215 qdio_do_sqbs(struct qdio_q *q, unsigned char state,
216              unsigned int *start, unsigned int *cnt)
217 {
218         struct qdio_irq *irq;
219         unsigned int tmp_cnt, q_no, ccq;
220         int rc;
221         char dbf_text[15];
222
223         ccq = 0;
224         tmp_cnt = *cnt;
225         irq = (struct qdio_irq*)q->irq_ptr;
226         q_no = q->q_no;
227         if(!q->is_input_q)
228                 q_no += irq->no_input_qs;
229 again:
230         ccq = do_sqbs(irq->sch_token, state, q_no, start, cnt);
231         rc = qdio_check_ccq(q, ccq);
232         if (rc == 1) {
233                 QDIO_DBF_TEXT5(1,trace,"sqAGAIN");
234                 goto again;
235         }
236         if (rc < 0) {
237                 QDIO_DBF_TEXT3(1,trace,"sqberr");
238                 sprintf(dbf_text,"%2x,%2x",tmp_cnt,*cnt);
239                 QDIO_DBF_TEXT3(1,trace,dbf_text);
240                 sprintf(dbf_text,"%d,%d",ccq,q_no);
241                 QDIO_DBF_TEXT3(1,trace,dbf_text);
242                 q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
243                                 QDIO_STATUS_LOOK_FOR_ERROR,
244                                 0, 0, 0, -1, -1, q->int_parm);
245                 return 0;
246         }
247         return (tmp_cnt - *cnt);
248 }
249
250 static inline int
251 qdio_set_slsb(struct qdio_q *q, unsigned int *bufno,
252               unsigned char state, unsigned int *count)
253 {
254         volatile char *slsb;
255         struct qdio_irq *irq;
256
257         irq = (struct qdio_irq*)q->irq_ptr;
258         if (!irq->is_qebsm) {
259                 slsb = (char *)&q->slsb.acc.val[(*bufno)];
260                 xchg(slsb, state);
261                 return 1;
262         }
263         return qdio_do_sqbs(q, state, bufno, count);
264 }
265
266 #ifdef CONFIG_QDIO_DEBUG
267 static inline void
268 qdio_trace_slsb(struct qdio_q *q)
269 {
270         if (q->queue_type==QDIO_TRACE_QTYPE) {
271                 if (q->is_input_q)
272                         QDIO_DBF_HEX2(0,slsb_in,&q->slsb,
273                                       QDIO_MAX_BUFFERS_PER_Q);
274                 else
275                         QDIO_DBF_HEX2(0,slsb_out,&q->slsb,
276                                       QDIO_MAX_BUFFERS_PER_Q);
277         }
278 }
279 #endif
280
281 static inline int
282 set_slsb(struct qdio_q *q, unsigned int *bufno,
283          unsigned char state, unsigned int *count)
284 {
285         int rc;
286 #ifdef CONFIG_QDIO_DEBUG
287         qdio_trace_slsb(q);
288 #endif
289         rc = qdio_set_slsb(q, bufno, state, count);
290 #ifdef CONFIG_QDIO_DEBUG
291         qdio_trace_slsb(q);
292 #endif
293         return rc;
294 }
295 static inline int 
296 qdio_siga_sync(struct qdio_q *q, unsigned int gpr2,
297                unsigned int gpr3)
298 {
299         int cc;
300
301         QDIO_DBF_TEXT4(0,trace,"sigasync");
302         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
303
304         qdio_perf_stat_inc(&perf_stats.siga_syncs);
305
306         cc = do_siga_sync(q->schid, gpr2, gpr3);
307         if (cc)
308                 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
309
310         return cc;
311 }
312
313 static inline int
314 qdio_siga_sync_q(struct qdio_q *q)
315 {
316         if (q->is_input_q)
317                 return qdio_siga_sync(q, 0, q->mask);
318         return qdio_siga_sync(q, q->mask, 0);
319 }
320
321 static int
322 __do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
323 {
324        struct qdio_irq *irq;
325        unsigned int fc = 0;
326        unsigned long schid;
327
328        irq = (struct qdio_irq *) q->irq_ptr;
329        if (!irq->is_qebsm)
330                schid = *((u32 *)&q->schid);
331        else {
332                schid = irq->sch_token;
333                fc |= 0x80;
334        }
335        return do_siga_output(schid, q->mask, busy_bit, fc);
336 }
337
338 /* 
339  * returns QDIO_SIGA_ERROR_ACCESS_EXCEPTION as cc, when SIGA returns
340  * an access exception 
341  */
342 static int
343 qdio_siga_output(struct qdio_q *q)
344 {
345         int cc;
346         __u32 busy_bit;
347         __u64 start_time=0;
348
349         qdio_perf_stat_inc(&perf_stats.siga_outs);
350
351         QDIO_DBF_TEXT4(0,trace,"sigaout");
352         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
353
354         for (;;) {
355                 cc = __do_siga_output(q, &busy_bit);
356 //QDIO_PRINT_ERR("cc=%x, busy=%x\n",cc,busy_bit);
357                 if ((cc==2) && (busy_bit) && (q->is_iqdio_q)) {
358                         if (!start_time) 
359                                 start_time=NOW;
360                         if ((NOW-start_time)>QDIO_BUSY_BIT_PATIENCE)
361                                 break;
362                 } else
363                         break;
364         }
365         
366         if ((cc==2) && (busy_bit)) 
367                 cc |= QDIO_SIGA_ERROR_B_BIT_SET;
368
369         if (cc)
370                 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
371
372         return cc;
373 }
374
375 static int
376 qdio_siga_input(struct qdio_q *q)
377 {
378         int cc;
379
380         QDIO_DBF_TEXT4(0,trace,"sigain");
381         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
382
383         qdio_perf_stat_inc(&perf_stats.siga_ins);
384
385         cc = do_siga_input(q->schid, q->mask);
386         
387         if (cc)
388                 QDIO_DBF_HEX3(0,trace,&cc,sizeof(int*));
389
390         return cc;
391 }
392
393 /* locked by the locks in qdio_activate and qdio_cleanup */
394 static __u32 *
395 qdio_get_indicator(void)
396 {
397         int i;
398
399         for (i=1;i<INDICATORS_PER_CACHELINE;i++)
400                 if (!indicator_used[i]) {
401                         indicator_used[i]=1;
402                         return indicators+i;
403                 }
404         atomic_inc(&spare_indicator_usecount);
405         return (__u32 * volatile) &spare_indicator;
406 }
407
408 /* locked by the locks in qdio_activate and qdio_cleanup */
409 static void 
410 qdio_put_indicator(__u32 *addr)
411 {
412         int i;
413
414         if ( (addr) && (addr!=&spare_indicator) ) {
415                 i=addr-indicators;
416                 indicator_used[i]=0;
417         }
418         if (addr == &spare_indicator)
419                 atomic_dec(&spare_indicator_usecount);
420 }
421
422 static inline void
423 tiqdio_clear_summary_bit(__u32 *location)
424 {
425         QDIO_DBF_TEXT5(0,trace,"clrsummb");
426         QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));
427
428         xchg(location,0);
429 }
430
431 static inline  void
432 tiqdio_set_summary_bit(__u32 *location)
433 {
434         QDIO_DBF_TEXT5(0,trace,"setsummb");
435         QDIO_DBF_HEX5(0,trace,&location,sizeof(void*));
436
437         xchg(location,-1);
438 }
439
440 static inline void 
441 tiqdio_sched_tl(void)
442 {
443         tasklet_hi_schedule(&tiqdio_tasklet);
444 }
445
446 static void
447 qdio_mark_tiq(struct qdio_q *q)
448 {
449         unsigned long flags;
450
451         QDIO_DBF_TEXT4(0,trace,"mark iq");
452         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
453
454         spin_lock_irqsave(&ttiq_list_lock,flags);
455         if (unlikely(atomic_read(&q->is_in_shutdown)))
456                 goto out_unlock;
457
458         if (!q->is_input_q)
459                 goto out_unlock;
460
461         if ((q->list_prev) || (q->list_next)) 
462                 goto out_unlock;
463
464         if (!tiq_list) {
465                 tiq_list=q;
466                 q->list_prev=q;
467                 q->list_next=q;
468         } else {
469                 q->list_next=tiq_list;
470                 q->list_prev=tiq_list->list_prev;
471                 tiq_list->list_prev->list_next=q;
472                 tiq_list->list_prev=q;
473         }
474         spin_unlock_irqrestore(&ttiq_list_lock,flags);
475
476         tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
477         tiqdio_sched_tl();
478         return;
479 out_unlock:
480         spin_unlock_irqrestore(&ttiq_list_lock,flags);
481         return;
482 }
483
484 static inline void
485 qdio_mark_q(struct qdio_q *q)
486 {
487         QDIO_DBF_TEXT4(0,trace,"mark q");
488         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
489
490         if (unlikely(atomic_read(&q->is_in_shutdown)))
491                 return;
492
493         tasklet_schedule(&q->tasklet);
494 }
495
496 static int
497 qdio_stop_polling(struct qdio_q *q)
498 {
499 #ifdef QDIO_USE_PROCESSING_STATE
500        unsigned int tmp, gsf, count = 1;
501        unsigned char state = 0;
502        struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
503
504         if (!atomic_xchg(&q->polling,0))
505                 return 1;
506
507         QDIO_DBF_TEXT4(0,trace,"stoppoll");
508         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
509
510         /* show the card that we are not polling anymore */
511         if (!q->is_input_q)
512                 return 1;
513
514        tmp = gsf = GET_SAVED_FRONTIER(q);
515        tmp = ((tmp + QDIO_MAX_BUFFERS_PER_Q-1) & (QDIO_MAX_BUFFERS_PER_Q-1) );
516        set_slsb(q, &tmp, SLSB_P_INPUT_NOT_INIT, &count);
517
518         /* 
519          * we don't issue this SYNC_MEMORY, as we trust Rick T and
520          * moreover will not use the PROCESSING state under VM, so
521          * q->polling was 0 anyway
522          */
523         /*SYNC_MEMORY;*/
524        if (irq->is_qebsm) {
525                count = 1;
526                qdio_do_eqbs(q, &state, &gsf, &count);
527        } else
528                state = q->slsb.acc.val[gsf];
529        if (state != SLSB_P_INPUT_PRIMED)
530                 return 1;
531         /* 
532          * set our summary bit again, as otherwise there is a
533          * small window we can miss between resetting it and
534          * checking for PRIMED state 
535          */
536         if (q->is_thinint_q)
537                 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
538         return 0;
539
540 #else /* QDIO_USE_PROCESSING_STATE */
541         return 1;
542 #endif /* QDIO_USE_PROCESSING_STATE */
543 }
544
545 /* 
546  * see the comment in do_QDIO and before qdio_reserve_q about the
547  * sophisticated locking outside of unmark_q, so that we don't need to
548  * disable the interrupts :-) 
549 */
550 static void
551 qdio_unmark_q(struct qdio_q *q)
552 {
553         unsigned long flags;
554
555         QDIO_DBF_TEXT4(0,trace,"unmark q");
556         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
557
558         if ((!q->list_prev)||(!q->list_next))
559                 return;
560
561         if ((q->is_thinint_q)&&(q->is_input_q)) {
562                 /* iQDIO */
563                 spin_lock_irqsave(&ttiq_list_lock,flags);
564                 /* in case cleanup has done this already and simultanously
565                  * qdio_unmark_q is called from the interrupt handler, we've
566                  * got to check this in this specific case again */
567                 if ((!q->list_prev)||(!q->list_next))
568                         goto out;
569                 if (q->list_next==q) {
570                         /* q was the only interesting q */
571                         tiq_list=NULL;
572                         q->list_next=NULL;
573                         q->list_prev=NULL;
574                 } else {
575                         q->list_next->list_prev=q->list_prev;
576                         q->list_prev->list_next=q->list_next;
577                         tiq_list=q->list_next;
578                         q->list_next=NULL;
579                         q->list_prev=NULL;
580                 }
581 out:
582                 spin_unlock_irqrestore(&ttiq_list_lock,flags);
583         }
584 }
585
586 static inline unsigned long 
587 tiqdio_clear_global_summary(void)
588 {
589         unsigned long time;
590
591         QDIO_DBF_TEXT5(0,trace,"clrglobl");
592         
593         time = do_clear_global_summary();
594
595         QDIO_DBF_HEX5(0,trace,&time,sizeof(unsigned long));
596
597         return time;
598 }
599
600
601 /************************* OUTBOUND ROUTINES *******************************/
602 static int
603 qdio_qebsm_get_outbound_buffer_frontier(struct qdio_q *q)
604 {
605         struct qdio_irq *irq;
606         unsigned char state;
607         unsigned int cnt, count, ftc;
608
609         irq = (struct qdio_irq *) q->irq_ptr;
610         if ((!q->is_iqdio_q) && (!q->hydra_gives_outbound_pcis))
611                 SYNC_MEMORY;
612
613         ftc = q->first_to_check;
614         count = qdio_min(atomic_read(&q->number_of_buffers_used),
615                         (QDIO_MAX_BUFFERS_PER_Q-1));
616         if (count == 0)
617                 return q->first_to_check;
618         cnt = qdio_do_eqbs(q, &state, &ftc, &count);
619         if (cnt == 0)
620                 return q->first_to_check;
621         switch (state) {
622         case SLSB_P_OUTPUT_ERROR:
623                 QDIO_DBF_TEXT3(0,trace,"outperr");
624                 atomic_sub(cnt , &q->number_of_buffers_used);
625                 if (q->qdio_error)
626                         q->error_status_flags |=
627                                 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
628                 q->qdio_error = SLSB_P_OUTPUT_ERROR;
629                 q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
630                 q->first_to_check = ftc;
631                 break;
632         case SLSB_P_OUTPUT_EMPTY:
633                 QDIO_DBF_TEXT5(0,trace,"outpempt");
634                 atomic_sub(cnt, &q->number_of_buffers_used);
635                 q->first_to_check = ftc;
636                 break;
637         case SLSB_CU_OUTPUT_PRIMED:
638                 /* all buffers primed */
639                 QDIO_DBF_TEXT5(0,trace,"outpprim");
640                 break;
641         default:
642                 break;
643         }
644         QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
645         return q->first_to_check;
646 }
647
648 static int
649 qdio_qebsm_get_inbound_buffer_frontier(struct qdio_q *q)
650 {
651         struct qdio_irq *irq;
652         unsigned char state;
653         int tmp, ftc, count, cnt;
654         char dbf_text[15];
655
656
657         irq = (struct qdio_irq *) q->irq_ptr;
658         ftc = q->first_to_check;
659         count = qdio_min(atomic_read(&q->number_of_buffers_used),
660                         (QDIO_MAX_BUFFERS_PER_Q-1));
661         if (count == 0)
662                  return q->first_to_check;
663         cnt = qdio_do_eqbs(q, &state, &ftc, &count);
664         if (cnt == 0)
665                  return q->first_to_check;
666         switch (state) {
667         case SLSB_P_INPUT_ERROR :
668 #ifdef CONFIG_QDIO_DEBUG
669                 QDIO_DBF_TEXT3(1,trace,"inperr");
670                 sprintf(dbf_text,"%2x,%2x",ftc,count);
671                 QDIO_DBF_TEXT3(1,trace,dbf_text);
672 #endif /* CONFIG_QDIO_DEBUG */
673                 if (q->qdio_error)
674                         q->error_status_flags |=
675                                 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
676                 q->qdio_error = SLSB_P_INPUT_ERROR;
677                 q->error_status_flags |= QDIO_STATUS_LOOK_FOR_ERROR;
678                 atomic_sub(cnt, &q->number_of_buffers_used);
679                 q->first_to_check = ftc;
680                 break;
681         case SLSB_P_INPUT_PRIMED :
682                 QDIO_DBF_TEXT3(0,trace,"inptprim");
683                 sprintf(dbf_text,"%2x,%2x",ftc,count);
684                 QDIO_DBF_TEXT3(1,trace,dbf_text);
685                 tmp = 0;
686                 ftc = q->first_to_check;
687 #ifdef QDIO_USE_PROCESSING_STATE
688                 if (cnt > 1) {
689                         cnt -= 1;
690                         tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
691                         if (!tmp)
692                                 break;
693                 }
694                 cnt = 1;
695                 tmp += set_slsb(q, &ftc,
696                                SLSB_P_INPUT_PROCESSING, &cnt);
697                 atomic_set(&q->polling, 1);
698 #else
699                 tmp = set_slsb(q, &ftc, SLSB_P_INPUT_NOT_INIT, &cnt);
700 #endif
701                 atomic_sub(tmp, &q->number_of_buffers_used);
702                 q->first_to_check = ftc;
703                 break;
704         case SLSB_CU_INPUT_EMPTY:
705         case SLSB_P_INPUT_NOT_INIT:
706         case SLSB_P_INPUT_PROCESSING:
707                 QDIO_DBF_TEXT5(0,trace,"inpnipro");
708                 break;
709         default:
710                 break;
711         }
712         QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
713         return q->first_to_check;
714 }
715
716 static int
717 qdio_get_outbound_buffer_frontier(struct qdio_q *q)
718 {
719         struct qdio_irq *irq;
720         volatile char *slsb;
721         unsigned int count = 1;
722         int first_not_to_check, f, f_mod_no;
723         char dbf_text[15];
724
725         QDIO_DBF_TEXT4(0,trace,"getobfro");
726         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
727
728         irq = (struct qdio_irq *) q->irq_ptr;
729         if (irq->is_qebsm)
730                 return qdio_qebsm_get_outbound_buffer_frontier(q);
731
732         slsb=&q->slsb.acc.val[0];
733         f_mod_no=f=q->first_to_check;
734         /* 
735          * f points to already processed elements, so f+no_used is correct...
736          * ... but: we don't check 128 buffers, as otherwise
737          * qdio_has_outbound_q_moved would return 0 
738          */
739         first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
740                                       (QDIO_MAX_BUFFERS_PER_Q-1));
741
742         if ((!q->is_iqdio_q)&&(!q->hydra_gives_outbound_pcis))
743                 SYNC_MEMORY;
744
745 check_next:
746         if (f==first_not_to_check) 
747                 goto out;
748
749         switch(slsb[f_mod_no]) {
750
751         /* the adapter has not fetched the output yet */
752         case SLSB_CU_OUTPUT_PRIMED:
753                 QDIO_DBF_TEXT5(0,trace,"outpprim");
754                 break;
755
756         /* the adapter got it */
757         case SLSB_P_OUTPUT_EMPTY:
758                 atomic_dec(&q->number_of_buffers_used);
759                 f++;
760                 f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
761                 QDIO_DBF_TEXT5(0,trace,"outpempt");
762                 goto check_next;
763
764         case SLSB_P_OUTPUT_ERROR:
765                 QDIO_DBF_TEXT3(0,trace,"outperr");
766                 sprintf(dbf_text,"%x-%x-%x",f_mod_no,
767                         q->sbal[f_mod_no]->element[14].sbalf.value,
768                         q->sbal[f_mod_no]->element[15].sbalf.value);
769                 QDIO_DBF_TEXT3(1,trace,dbf_text);
770                 QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);
771
772                 /* kind of process the buffer */
773                 set_slsb(q, &f_mod_no, SLSB_P_OUTPUT_NOT_INIT, &count);
774
775                 /* 
776                  * we increment the frontier, as this buffer
777                  * was processed obviously 
778                  */
779                 atomic_dec(&q->number_of_buffers_used);
780                 f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
781
782                 if (q->qdio_error)
783                         q->error_status_flags|=
784                                 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
785                 q->qdio_error=SLSB_P_OUTPUT_ERROR;
786                 q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
787
788                 break;
789
790         /* no new buffers */
791         default:
792                 QDIO_DBF_TEXT5(0,trace,"outpni");
793         }
794 out:
795         return (q->first_to_check=f_mod_no);
796 }
797
798 /* all buffers are processed */
799 static int
800 qdio_is_outbound_q_done(struct qdio_q *q)
801 {
802         int no_used;
803 #ifdef CONFIG_QDIO_DEBUG
804         char dbf_text[15];
805 #endif
806
807         no_used=atomic_read(&q->number_of_buffers_used);
808
809 #ifdef CONFIG_QDIO_DEBUG
810         if (no_used) {
811                 sprintf(dbf_text,"oqisnt%02x",no_used);
812                 QDIO_DBF_TEXT4(0,trace,dbf_text);
813         } else {
814                 QDIO_DBF_TEXT4(0,trace,"oqisdone");
815         }
816         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
817 #endif /* CONFIG_QDIO_DEBUG */
818         return (no_used==0);
819 }
820
821 static int
822 qdio_has_outbound_q_moved(struct qdio_q *q)
823 {
824         int i;
825
826         i=qdio_get_outbound_buffer_frontier(q);
827
828         if ( (i!=GET_SAVED_FRONTIER(q)) ||
829              (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
830                 SAVE_FRONTIER(q,i);
831                 QDIO_DBF_TEXT4(0,trace,"oqhasmvd");
832                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
833                 return 1;
834         } else {
835                 QDIO_DBF_TEXT4(0,trace,"oqhsntmv");
836                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
837                 return 0;
838         }
839 }
840
841 static void
842 qdio_kick_outbound_q(struct qdio_q *q)
843 {
844         int result;
845 #ifdef CONFIG_QDIO_DEBUG
846         char dbf_text[15];
847
848         QDIO_DBF_TEXT4(0,trace,"kickoutq");
849         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
850 #endif /* CONFIG_QDIO_DEBUG */
851
852         if (!q->siga_out)
853                 return;
854
855         /* here's the story with cc=2 and busy bit set (thanks, Rick):
856          * VM's CP could present us cc=2 and busy bit set on SIGA-write
857          * during reconfiguration of their Guest LAN (only in HIPERS mode,
858          * QDIO mode is asynchronous -- cc=2 and busy bit there will take
859          * the queues down immediately; and not being under VM we have a
860          * problem on cc=2 and busy bit set right away).
861          *
862          * Therefore qdio_siga_output will try for a short time constantly,
863          * if such a condition occurs. If it doesn't change, it will
864          * increase the busy_siga_counter and save the timestamp, and
865          * schedule the queue for later processing (via mark_q, using the
866          * queue tasklet). __qdio_outbound_processing will check out the
867          * counter. If non-zero, it will call qdio_kick_outbound_q as often
868          * as the value of the counter. This will attempt further SIGA
869          * instructions. For each successful SIGA, the counter is
870          * decreased, for failing SIGAs the counter remains the same, after
871          * all.
872          * After some time of no movement, qdio_kick_outbound_q will
873          * finally fail and reflect corresponding error codes to call
874          * the upper layer module and have it take the queues down.
875          *
876          * Note that this is a change from the original HiperSockets design
877          * (saying cc=2 and busy bit means take the queues down), but in
878          * these days Guest LAN didn't exist... excessive cc=2 with busy bit
879          * conditions will still take the queues down, but the threshold is
880          * higher due to the Guest LAN environment.
881          */
882
883
884         result=qdio_siga_output(q);
885
886         switch (result) {
887         case 0:
888                 /* went smooth this time, reset timestamp */
889 #ifdef CONFIG_QDIO_DEBUG
890                 QDIO_DBF_TEXT3(0,trace,"cc2reslv");
891                 sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
892                         atomic_read(&q->busy_siga_counter));
893                 QDIO_DBF_TEXT3(0,trace,dbf_text);
894 #endif /* CONFIG_QDIO_DEBUG */
895                 q->timing.busy_start=0;
896                 break;
897         case (2|QDIO_SIGA_ERROR_B_BIT_SET):
898                 /* cc=2 and busy bit: */
899                 atomic_inc(&q->busy_siga_counter);
900
901                 /* if the last siga was successful, save
902                  * timestamp here */
903                 if (!q->timing.busy_start)
904                         q->timing.busy_start=NOW;
905
906                 /* if we're in time, don't touch error_status_flags
907                  * and siga_error */
908                 if (NOW-q->timing.busy_start<QDIO_BUSY_BIT_GIVE_UP) {
909                         qdio_mark_q(q);
910                         break;
911                 }
912                 QDIO_DBF_TEXT2(0,trace,"cc2REPRT");
913 #ifdef CONFIG_QDIO_DEBUG
914                 sprintf(dbf_text,"%4x%2x%2x",q->schid.sch_no,q->q_no,
915                         atomic_read(&q->busy_siga_counter));
916                 QDIO_DBF_TEXT3(0,trace,dbf_text);
917 #endif /* CONFIG_QDIO_DEBUG */
918                 /* else fallthrough and report error */
919         default:
920                 /* for plain cc=1, 2 or 3: */
921                 if (q->siga_error)
922                         q->error_status_flags|=
923                                 QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR;
924                 q->error_status_flags|=
925                         QDIO_STATUS_LOOK_FOR_ERROR;
926                 q->siga_error=result;
927         }
928 }
929
930 static void
931 qdio_kick_outbound_handler(struct qdio_q *q)
932 {
933         int start, end, real_end, count;
934 #ifdef CONFIG_QDIO_DEBUG
935         char dbf_text[15];
936 #endif
937
938         start = q->first_element_to_kick;
939         /* last_move_ftc was just updated */
940         real_end = GET_SAVED_FRONTIER(q);
941         end = (real_end+QDIO_MAX_BUFFERS_PER_Q-1)&
942                 (QDIO_MAX_BUFFERS_PER_Q-1);
943         count = (end+QDIO_MAX_BUFFERS_PER_Q+1-start)&
944                 (QDIO_MAX_BUFFERS_PER_Q-1);
945
946 #ifdef CONFIG_QDIO_DEBUG
947         QDIO_DBF_TEXT4(0,trace,"kickouth");
948         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
949
950         sprintf(dbf_text,"s=%2xc=%2x",start,count);
951         QDIO_DBF_TEXT4(0,trace,dbf_text);
952 #endif /* CONFIG_QDIO_DEBUG */
953
954         if (q->state==QDIO_IRQ_STATE_ACTIVE)
955                 q->handler(q->cdev,QDIO_STATUS_OUTBOUND_INT|
956                            q->error_status_flags,
957                            q->qdio_error,q->siga_error,q->q_no,start,count,
958                            q->int_parm);
959
960         /* for the next time: */
961         q->first_element_to_kick=real_end;
962         q->qdio_error=0;
963         q->siga_error=0;
964         q->error_status_flags=0;
965 }
966
967 static void
968 __qdio_outbound_processing(struct qdio_q *q)
969 {
970         int siga_attempts;
971
972         QDIO_DBF_TEXT4(0,trace,"qoutproc");
973         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
974
975         if (unlikely(qdio_reserve_q(q))) {
976                 qdio_release_q(q);
977                 qdio_perf_stat_inc(&perf_stats.outbound_tl_runs_resched);
978                 /* as we're sissies, we'll check next time */
979                 if (likely(!atomic_read(&q->is_in_shutdown))) {
980                         qdio_mark_q(q);
981                         QDIO_DBF_TEXT4(0,trace,"busy,agn");
982                 }
983                 return;
984         }
985         qdio_perf_stat_inc(&perf_stats.outbound_tl_runs);
986         qdio_perf_stat_inc(&perf_stats.tl_runs);
987
988         /* see comment in qdio_kick_outbound_q */
989         siga_attempts=atomic_read(&q->busy_siga_counter);
990         while (siga_attempts) {
991                 atomic_dec(&q->busy_siga_counter);
992                 qdio_kick_outbound_q(q);
993                 siga_attempts--;
994         }
995
996         if (qdio_has_outbound_q_moved(q))
997                 qdio_kick_outbound_handler(q);
998
999         if (q->queue_type == QDIO_ZFCP_QFMT) {
1000                 if ((!q->hydra_gives_outbound_pcis) &&
1001                     (!qdio_is_outbound_q_done(q)))
1002                         qdio_mark_q(q);
1003         }
1004         else if (((!q->is_iqdio_q) && (!q->is_pci_out)) ||
1005                  (q->queue_type == QDIO_IQDIO_QFMT_ASYNCH)) {
1006                 /* 
1007                  * make sure buffer switch from PRIMED to EMPTY is noticed
1008                  * and outbound_handler is called
1009                  */
1010                 if (qdio_is_outbound_q_done(q)) {
1011                         del_timer(&q->timer);
1012                 } else {
1013                         if (!timer_pending(&q->timer))
1014                                 mod_timer(&q->timer, jiffies +
1015                                           QDIO_FORCE_CHECK_TIMEOUT);
1016                 }
1017         }
1018
1019         qdio_release_q(q);
1020 }
1021
1022 static void
1023 qdio_outbound_processing(struct qdio_q *q)
1024 {
1025         __qdio_outbound_processing(q);
1026 }
1027
1028 /************************* INBOUND ROUTINES *******************************/
1029
1030
1031 static int
1032 qdio_get_inbound_buffer_frontier(struct qdio_q *q)
1033 {
1034         struct qdio_irq *irq;
1035         int f,f_mod_no;
1036         volatile char *slsb;
1037         unsigned int count = 1;
1038         int first_not_to_check;
1039 #ifdef CONFIG_QDIO_DEBUG
1040         char dbf_text[15];
1041 #endif /* CONFIG_QDIO_DEBUG */
1042 #ifdef QDIO_USE_PROCESSING_STATE
1043         int last_position=-1;
1044 #endif /* QDIO_USE_PROCESSING_STATE */
1045
1046         QDIO_DBF_TEXT4(0,trace,"getibfro");
1047         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1048
1049         irq = (struct qdio_irq *) q->irq_ptr;
1050         if (irq->is_qebsm)
1051                 return qdio_qebsm_get_inbound_buffer_frontier(q);
1052
1053         slsb=&q->slsb.acc.val[0];
1054         f_mod_no=f=q->first_to_check;
1055         /* 
1056          * we don't check 128 buffers, as otherwise qdio_has_inbound_q_moved
1057          * would return 0 
1058          */
1059         first_not_to_check=f+qdio_min(atomic_read(&q->number_of_buffers_used),
1060                                       (QDIO_MAX_BUFFERS_PER_Q-1));
1061
1062         /* 
1063          * we don't use this one, as a PCI or we after a thin interrupt
1064          * will sync the queues
1065          */
1066         /* SYNC_MEMORY;*/
1067
1068 check_next:
1069         f_mod_no=f&(QDIO_MAX_BUFFERS_PER_Q-1);
1070         if (f==first_not_to_check) 
1071                 goto out;
1072         switch (slsb[f_mod_no]) {
1073
1074         /* CU_EMPTY means frontier is reached */
1075         case SLSB_CU_INPUT_EMPTY:
1076                 QDIO_DBF_TEXT5(0,trace,"inptempt");
1077                 break;
1078
1079         /* P_PRIMED means set slsb to P_PROCESSING and move on */
1080         case SLSB_P_INPUT_PRIMED:
1081                 QDIO_DBF_TEXT5(0,trace,"inptprim");
1082
1083 #ifdef QDIO_USE_PROCESSING_STATE
1084                 /* 
1085                  * as soon as running under VM, polling the input queues will
1086                  * kill VM in terms of CP overhead 
1087                  */
1088                 if (q->siga_sync) {
1089                         set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1090                 } else {
1091                         /* set the previous buffer to NOT_INIT. The current
1092                          * buffer will be set to PROCESSING at the end of
1093                          * this function to avoid further interrupts. */
1094                         if (last_position>=0)
1095                                 set_slsb(q, &last_position,
1096                                          SLSB_P_INPUT_NOT_INIT, &count);
1097                         atomic_set(&q->polling,1);
1098                         last_position=f_mod_no;
1099                 }
1100 #else /* QDIO_USE_PROCESSING_STATE */
1101                 set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1102 #endif /* QDIO_USE_PROCESSING_STATE */
1103                 /* 
1104                  * not needed, as the inbound queue will be synced on the next
1105                  * siga-r, resp. tiqdio_is_inbound_q_done will do the siga-s
1106                  */
1107                 /*SYNC_MEMORY;*/
1108                 f++;
1109                 atomic_dec(&q->number_of_buffers_used);
1110                 goto check_next;
1111
1112         case SLSB_P_INPUT_NOT_INIT:
1113         case SLSB_P_INPUT_PROCESSING:
1114                 QDIO_DBF_TEXT5(0,trace,"inpnipro");
1115                 break;
1116
1117         /* P_ERROR means frontier is reached, break and report error */
1118         case SLSB_P_INPUT_ERROR:
1119 #ifdef CONFIG_QDIO_DEBUG
1120                 sprintf(dbf_text,"inperr%2x",f_mod_no);
1121                 QDIO_DBF_TEXT3(1,trace,dbf_text);
1122 #endif /* CONFIG_QDIO_DEBUG */
1123                 QDIO_DBF_HEX2(1,sbal,q->sbal[f_mod_no],256);
1124
1125                 /* kind of process the buffer */
1126                 set_slsb(q, &f_mod_no, SLSB_P_INPUT_NOT_INIT, &count);
1127
1128                 if (q->qdio_error)
1129                         q->error_status_flags|=
1130                                 QDIO_STATUS_MORE_THAN_ONE_QDIO_ERROR;
1131                 q->qdio_error=SLSB_P_INPUT_ERROR;
1132                 q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
1133
1134                 /* we increment the frontier, as this buffer
1135                  * was processed obviously */
1136                 f_mod_no=(f_mod_no+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1137                 atomic_dec(&q->number_of_buffers_used);
1138
1139 #ifdef QDIO_USE_PROCESSING_STATE
1140                 last_position=-1;
1141 #endif /* QDIO_USE_PROCESSING_STATE */
1142
1143                 break;
1144
1145         /* everything else means frontier not changed (HALTED or so) */
1146         default: 
1147                 break;
1148         }
1149 out:
1150         q->first_to_check=f_mod_no;
1151
1152 #ifdef QDIO_USE_PROCESSING_STATE
1153         if (last_position>=0)
1154                 set_slsb(q, &last_position, SLSB_P_INPUT_PROCESSING, &count);
1155 #endif /* QDIO_USE_PROCESSING_STATE */
1156
1157         QDIO_DBF_HEX4(0,trace,&q->first_to_check,sizeof(int));
1158
1159         return q->first_to_check;
1160 }
1161
1162 static int
1163 qdio_has_inbound_q_moved(struct qdio_q *q)
1164 {
1165         int i;
1166
1167         i=qdio_get_inbound_buffer_frontier(q);
1168         if ( (i!=GET_SAVED_FRONTIER(q)) ||
1169              (q->error_status_flags&QDIO_STATUS_LOOK_FOR_ERROR) ) {
1170                 SAVE_FRONTIER(q,i);
1171                 if ((!q->siga_sync)&&(!q->hydra_gives_outbound_pcis))
1172                         SAVE_TIMESTAMP(q);
1173
1174                 QDIO_DBF_TEXT4(0,trace,"inhasmvd");
1175                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1176                 return 1;
1177         } else {
1178                 QDIO_DBF_TEXT4(0,trace,"inhsntmv");
1179                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1180                 return 0;
1181         }
1182 }
1183
1184 /* means, no more buffers to be filled */
1185 static int
1186 tiqdio_is_inbound_q_done(struct qdio_q *q)
1187 {
1188         int no_used;
1189         unsigned int start_buf, count;
1190         unsigned char state = 0;
1191         struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
1192
1193 #ifdef CONFIG_QDIO_DEBUG
1194         char dbf_text[15];
1195 #endif
1196
1197         no_used=atomic_read(&q->number_of_buffers_used);
1198
1199         /* propagate the change from 82 to 80 through VM */
1200         SYNC_MEMORY;
1201
1202 #ifdef CONFIG_QDIO_DEBUG
1203         if (no_used) {
1204                 sprintf(dbf_text,"iqisnt%02x",no_used);
1205                 QDIO_DBF_TEXT4(0,trace,dbf_text);
1206         } else {
1207                 QDIO_DBF_TEXT4(0,trace,"iniqisdo");
1208         }
1209         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1210 #endif /* CONFIG_QDIO_DEBUG */
1211
1212         if (!no_used)
1213                 return 1;
1214         if (!q->siga_sync && !irq->is_qebsm)
1215                 /* we'll check for more primed buffers in qeth_stop_polling */
1216                 return 0;
1217         if (irq->is_qebsm) {
1218                 count = 1;
1219                 start_buf = q->first_to_check;
1220                 qdio_do_eqbs(q, &state, &start_buf, &count);
1221         } else
1222                 state = q->slsb.acc.val[q->first_to_check];
1223         if (state != SLSB_P_INPUT_PRIMED)
1224                 /* 
1225                  * nothing more to do, if next buffer is not PRIMED.
1226                  * note that we did a SYNC_MEMORY before, that there
1227                  * has been a sychnronization.
1228                  * we will return 0 below, as there is nothing to do
1229                  * (stop_polling not necessary, as we have not been
1230                  * using the PROCESSING state 
1231                  */
1232                 return 0;
1233
1234         /* 
1235          * ok, the next input buffer is primed. that means, that device state 
1236          * change indicator and adapter local summary are set, so we will find
1237          * it next time.
1238          * we will return 0 below, as there is nothing to do, except scheduling
1239          * ourselves for the next time. 
1240          */
1241         tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1242         tiqdio_sched_tl();
1243         return 0;
1244 }
1245
1246 static int
1247 qdio_is_inbound_q_done(struct qdio_q *q)
1248 {
1249         int no_used;
1250         unsigned int start_buf, count;
1251         unsigned char state = 0;
1252         struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
1253
1254 #ifdef CONFIG_QDIO_DEBUG
1255         char dbf_text[15];
1256 #endif
1257
1258         no_used=atomic_read(&q->number_of_buffers_used);
1259
1260         /* 
1261          * we need that one for synchronization with the adapter, as it
1262          * does a kind of PCI avoidance 
1263          */
1264         SYNC_MEMORY;
1265
1266         if (!no_used) {
1267                 QDIO_DBF_TEXT4(0,trace,"inqisdnA");
1268                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1269                 return 1;
1270         }
1271         if (irq->is_qebsm) {
1272                 count = 1;
1273                 start_buf = q->first_to_check;
1274                 qdio_do_eqbs(q, &state, &start_buf, &count);
1275         } else
1276                 state = q->slsb.acc.val[q->first_to_check];
1277         if (state == SLSB_P_INPUT_PRIMED) {
1278                 /* we got something to do */
1279                 QDIO_DBF_TEXT4(0,trace,"inqisntA");
1280                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1281                 return 0;
1282         }
1283
1284         /* on VM, we don't poll, so the q is always done here */
1285         if (q->siga_sync)
1286                 return 1;
1287         if (q->hydra_gives_outbound_pcis)
1288                 return 1;
1289
1290         /* 
1291          * at this point we know, that inbound first_to_check
1292          * has (probably) not moved (see qdio_inbound_processing) 
1293          */
1294         if (NOW>GET_SAVED_TIMESTAMP(q)+q->timing.threshold) {
1295 #ifdef CONFIG_QDIO_DEBUG
1296                 QDIO_DBF_TEXT4(0,trace,"inqisdon");
1297                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1298                 sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
1299                 QDIO_DBF_TEXT4(0,trace,dbf_text);
1300 #endif /* CONFIG_QDIO_DEBUG */
1301                 return 1;
1302         } else {
1303 #ifdef CONFIG_QDIO_DEBUG
1304                 QDIO_DBF_TEXT4(0,trace,"inqisntd");
1305                 QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1306                 sprintf(dbf_text,"pf%02xcn%02x",q->first_to_check,no_used);
1307                 QDIO_DBF_TEXT4(0,trace,dbf_text);
1308 #endif /* CONFIG_QDIO_DEBUG */
1309                 return 0;
1310         }
1311 }
1312
1313 static void
1314 qdio_kick_inbound_handler(struct qdio_q *q)
1315 {
1316         int count, start, end, real_end, i;
1317 #ifdef CONFIG_QDIO_DEBUG
1318         char dbf_text[15];
1319 #endif
1320
1321         QDIO_DBF_TEXT4(0,trace,"kickinh");
1322         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1323
1324         start=q->first_element_to_kick;
1325         real_end=q->first_to_check;
1326         end=(real_end+QDIO_MAX_BUFFERS_PER_Q-1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1327  
1328         i=start;
1329         count=0;
1330         while (1) {
1331                 count++;
1332                 if (i==end)
1333                         break;
1334                 i=(i+1)&(QDIO_MAX_BUFFERS_PER_Q-1);
1335         }
1336
1337 #ifdef CONFIG_QDIO_DEBUG
1338         sprintf(dbf_text,"s=%2xc=%2x",start,count);
1339         QDIO_DBF_TEXT4(0,trace,dbf_text);
1340 #endif /* CONFIG_QDIO_DEBUG */
1341
1342         if (likely(q->state==QDIO_IRQ_STATE_ACTIVE))
1343                 q->handler(q->cdev,
1344                            QDIO_STATUS_INBOUND_INT|q->error_status_flags,
1345                            q->qdio_error,q->siga_error,q->q_no,start,count,
1346                            q->int_parm);
1347
1348         /* for the next time: */
1349         q->first_element_to_kick=real_end;
1350         q->qdio_error=0;
1351         q->siga_error=0;
1352         q->error_status_flags=0;
1353
1354         qdio_perf_stat_inc(&perf_stats.inbound_cnt);
1355 }
1356
1357 static void
1358 __tiqdio_inbound_processing(struct qdio_q *q, int spare_ind_was_set)
1359 {
1360         struct qdio_irq *irq_ptr;
1361         struct qdio_q *oq;
1362         int i;
1363
1364         QDIO_DBF_TEXT4(0,trace,"iqinproc");
1365         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1366
1367         /* 
1368          * we first want to reserve the q, so that we know, that we don't
1369          * interrupt ourselves and call qdio_unmark_q, as is_in_shutdown might
1370          * be set 
1371          */
1372         if (unlikely(qdio_reserve_q(q))) {
1373                 qdio_release_q(q);
1374                 qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
1375                 /* 
1376                  * as we might just be about to stop polling, we make
1377                  * sure that we check again at least once more 
1378                  */
1379                 tiqdio_sched_tl();
1380                 return;
1381         }
1382         qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs);
1383         if (unlikely(atomic_read(&q->is_in_shutdown))) {
1384                 qdio_unmark_q(q);
1385                 goto out;
1386         }
1387
1388         /* 
1389          * we reset spare_ind_was_set, when the queue does not use the
1390          * spare indicator
1391          */
1392         if (spare_ind_was_set)
1393                 spare_ind_was_set = (q->dev_st_chg_ind == &spare_indicator);
1394
1395         if (!(*(q->dev_st_chg_ind)) && !spare_ind_was_set)
1396                 goto out;
1397         /*
1398          * q->dev_st_chg_ind is the indicator, be it shared or not.
1399          * only clear it, if indicator is non-shared
1400          */
1401         if (!spare_ind_was_set)
1402                 tiqdio_clear_summary_bit((__u32*)q->dev_st_chg_ind);
1403
1404         if (q->hydra_gives_outbound_pcis) {
1405                 if (!q->siga_sync_done_on_thinints) {
1406                         SYNC_MEMORY_ALL;
1407                 } else if ((!q->siga_sync_done_on_outb_tis)&&
1408                          (q->hydra_gives_outbound_pcis)) {
1409                         SYNC_MEMORY_ALL_OUTB;
1410                 }
1411         } else {
1412                 SYNC_MEMORY;
1413         }
1414         /*
1415          * maybe we have to do work on our outbound queues... at least
1416          * we have to check the outbound-int-capable thinint-capable
1417          * queues
1418          */
1419         if (q->hydra_gives_outbound_pcis) {
1420                 irq_ptr = (struct qdio_irq*)q->irq_ptr;
1421                 for (i=0;i<irq_ptr->no_output_qs;i++) {
1422                         oq = irq_ptr->output_qs[i];
1423                         if (!qdio_is_outbound_q_done(oq)) {
1424                                 qdio_perf_stat_dec(&perf_stats.tl_runs);
1425                                 __qdio_outbound_processing(oq);
1426                         }
1427                 }
1428         }
1429
1430         if (!qdio_has_inbound_q_moved(q))
1431                 goto out;
1432
1433         qdio_kick_inbound_handler(q);
1434         if (tiqdio_is_inbound_q_done(q))
1435                 if (!qdio_stop_polling(q)) {
1436                         /* 
1437                          * we set the flags to get into the stuff next time,
1438                          * see also comment in qdio_stop_polling 
1439                          */
1440                         tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1441                         tiqdio_sched_tl();
1442                 }
1443 out:
1444         qdio_release_q(q);
1445 }
1446
1447 static void
1448 tiqdio_inbound_processing(struct qdio_q *q)
1449 {
1450         __tiqdio_inbound_processing(q, atomic_read(&spare_indicator_usecount));
1451 }
1452
1453 static void
1454 __qdio_inbound_processing(struct qdio_q *q)
1455 {
1456         int q_laps=0;
1457
1458         QDIO_DBF_TEXT4(0,trace,"qinproc");
1459         QDIO_DBF_HEX4(0,trace,&q,sizeof(void*));
1460
1461         if (unlikely(qdio_reserve_q(q))) {
1462                 qdio_release_q(q);
1463                 qdio_perf_stat_inc(&perf_stats.inbound_tl_runs_resched);
1464                 /* as we're sissies, we'll check next time */
1465                 if (likely(!atomic_read(&q->is_in_shutdown))) {
1466                         qdio_mark_q(q);
1467                         QDIO_DBF_TEXT4(0,trace,"busy,agn");
1468                 }
1469                 return;
1470         }
1471         qdio_perf_stat_inc(&perf_stats.inbound_tl_runs);
1472         qdio_perf_stat_inc(&perf_stats.tl_runs);
1473
1474 again:
1475         if (qdio_has_inbound_q_moved(q)) {
1476                 qdio_kick_inbound_handler(q);
1477                 if (!qdio_stop_polling(q)) {
1478                         q_laps++;
1479                         if (q_laps<QDIO_Q_LAPS) 
1480                                 goto again;
1481                 }
1482                 qdio_mark_q(q);
1483         } else {
1484                 if (!qdio_is_inbound_q_done(q)) 
1485                         /* means poll time is not yet over */
1486                         qdio_mark_q(q);
1487         }
1488
1489         qdio_release_q(q);
1490 }
1491
1492 static void
1493 qdio_inbound_processing(struct qdio_q *q)
1494 {
1495         __qdio_inbound_processing(q);
1496 }
1497
1498 /************************* MAIN ROUTINES *******************************/
1499
1500 #ifdef QDIO_USE_PROCESSING_STATE
1501 static int
1502 tiqdio_reset_processing_state(struct qdio_q *q, int q_laps)
1503 {
1504         if (!q) {
1505                 tiqdio_sched_tl();
1506                 return 0;
1507         }
1508
1509         /* 
1510          * under VM, we have not used the PROCESSING state, so no
1511          * need to stop polling 
1512          */
1513         if (q->siga_sync)
1514                 return 2;
1515
1516         if (unlikely(qdio_reserve_q(q))) {
1517                 qdio_release_q(q);
1518                 qdio_perf_stat_inc(&perf_stats.inbound_thin_tl_runs_resched);
1519                 /* 
1520                  * as we might just be about to stop polling, we make
1521                  * sure that we check again at least once more 
1522                  */
1523                 
1524                 /* 
1525                  * sanity -- we'd get here without setting the
1526                  * dev st chg ind 
1527                  */
1528                 tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1529                 tiqdio_sched_tl();
1530                 return 0;
1531         }
1532         if (qdio_stop_polling(q)) {
1533                 qdio_release_q(q);
1534                 return 2;
1535         }               
1536         if (q_laps<QDIO_Q_LAPS-1) {
1537                 qdio_release_q(q);
1538                 return 3;
1539         }
1540         /* 
1541          * we set the flags to get into the stuff
1542          * next time, see also comment in qdio_stop_polling 
1543          */
1544         tiqdio_set_summary_bit((__u32*)q->dev_st_chg_ind);
1545         tiqdio_sched_tl();
1546         qdio_release_q(q);
1547         return 1;
1548         
1549 }
1550 #endif /* QDIO_USE_PROCESSING_STATE */
1551
1552 static void
1553 tiqdio_inbound_checks(void)
1554 {
1555         struct qdio_q *q;
1556         int spare_ind_was_set=0;
1557 #ifdef QDIO_USE_PROCESSING_STATE
1558         int q_laps=0;
1559 #endif /* QDIO_USE_PROCESSING_STATE */
1560
1561         QDIO_DBF_TEXT4(0,trace,"iqdinbck");
1562         QDIO_DBF_TEXT5(0,trace,"iqlocsum");
1563
1564 #ifdef QDIO_USE_PROCESSING_STATE
1565 again:
1566 #endif /* QDIO_USE_PROCESSING_STATE */
1567
1568         /* when the spare indicator is used and set, save that and clear it */
1569         if ((atomic_read(&spare_indicator_usecount)) && spare_indicator) {
1570                 spare_ind_was_set = 1;
1571                 tiqdio_clear_summary_bit((__u32*)&spare_indicator);
1572         }
1573
1574         q=(struct qdio_q*)tiq_list;
1575         do {
1576                 if (!q)
1577                         break;
1578                 __tiqdio_inbound_processing(q, spare_ind_was_set);
1579                 q=(struct qdio_q*)q->list_next;
1580         } while (q!=(struct qdio_q*)tiq_list);
1581
1582 #ifdef QDIO_USE_PROCESSING_STATE
1583         q=(struct qdio_q*)tiq_list;
1584         do {
1585                 int ret;
1586
1587                 ret = tiqdio_reset_processing_state(q, q_laps);
1588                 switch (ret) {
1589                 case 0:
1590                         return;
1591                 case 1:
1592                         q_laps++;
1593                 case 2:
1594                         q = (struct qdio_q*)q->list_next;
1595                         break;
1596                 default:
1597                         q_laps++;
1598                         goto again;
1599                 }
1600         } while (q!=(struct qdio_q*)tiq_list);
1601 #endif /* QDIO_USE_PROCESSING_STATE */
1602 }
1603
1604 static void
1605 tiqdio_tl(unsigned long data)
1606 {
1607         QDIO_DBF_TEXT4(0,trace,"iqdio_tl");
1608
1609         qdio_perf_stat_inc(&perf_stats.tl_runs);
1610
1611         tiqdio_inbound_checks();
1612 }
1613
1614 /********************* GENERAL HELPER_ROUTINES ***********************/
1615
1616 static void
1617 qdio_release_irq_memory(struct qdio_irq *irq_ptr)
1618 {
1619         int i;
1620
1621         for (i=0;i<QDIO_MAX_QUEUES_PER_IRQ;i++) {
1622                 if (!irq_ptr->input_qs[i])
1623                         goto next;
1624
1625                 kfree(irq_ptr->input_qs[i]->slib);
1626                 kfree(irq_ptr->input_qs[i]);
1627
1628 next:
1629                 if (!irq_ptr->output_qs[i])
1630                         continue;
1631
1632                 kfree(irq_ptr->output_qs[i]->slib);
1633                 kfree(irq_ptr->output_qs[i]);
1634
1635         }
1636         kfree(irq_ptr->qdr);
1637         free_page((unsigned long) irq_ptr);
1638 }
1639
1640 static void
1641 qdio_set_impl_params(struct qdio_irq *irq_ptr,
1642                      unsigned int qib_param_field_format,
1643                      /* pointer to 128 bytes or NULL, if no param field */
1644                      unsigned char *qib_param_field,
1645                      /* pointer to no_queues*128 words of data or NULL */
1646                      unsigned int no_input_qs,
1647                      unsigned int no_output_qs,
1648                      unsigned long *input_slib_elements,
1649                      unsigned long *output_slib_elements)
1650 {
1651         int i,j;
1652
1653         if (!irq_ptr)
1654                 return;
1655
1656         irq_ptr->qib.pfmt=qib_param_field_format;
1657         if (qib_param_field)
1658                 memcpy(irq_ptr->qib.parm,qib_param_field,
1659                        QDIO_MAX_BUFFERS_PER_Q);
1660
1661         if (input_slib_elements)
1662                 for (i=0;i<no_input_qs;i++) {
1663                         for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1664                                 irq_ptr->input_qs[i]->slib->slibe[j].parms=
1665                                         input_slib_elements[
1666                                                 i*QDIO_MAX_BUFFERS_PER_Q+j];
1667                 }
1668         if (output_slib_elements)
1669                 for (i=0;i<no_output_qs;i++) {
1670                         for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1671                                 irq_ptr->output_qs[i]->slib->slibe[j].parms=
1672                                         output_slib_elements[
1673                                                 i*QDIO_MAX_BUFFERS_PER_Q+j];
1674                 }
1675 }
1676
1677 static int
1678 qdio_alloc_qs(struct qdio_irq *irq_ptr,
1679               int no_input_qs, int no_output_qs)
1680 {
1681         int i;
1682         struct qdio_q *q;
1683         int result=-ENOMEM;
1684
1685         for (i=0;i<no_input_qs;i++) {
1686                 q = kzalloc(sizeof(struct qdio_q), GFP_KERNEL);
1687
1688                 if (!q) {
1689                         QDIO_PRINT_ERR("kmalloc of q failed!\n");
1690                         goto out;
1691                 }
1692
1693                 q->slib = kmalloc(PAGE_SIZE, GFP_KERNEL);
1694                 if (!q->slib) {
1695                         QDIO_PRINT_ERR("kmalloc of slib failed!\n");
1696                         goto out;
1697                 }
1698
1699                 irq_ptr->input_qs[i]=q;
1700         }
1701
1702         for (i=0;i<no_output_qs;i++) {
1703                 q = kzalloc(sizeof(struct qdio_q), GFP_KERNEL);
1704
1705                 if (!q) {
1706                         goto out;
1707                 }
1708
1709                 q->slib=kmalloc(PAGE_SIZE,GFP_KERNEL);
1710                 if (!q->slib) {
1711                         QDIO_PRINT_ERR("kmalloc of slib failed!\n");
1712                         goto out;
1713                 }
1714
1715                 irq_ptr->output_qs[i]=q;
1716         }
1717
1718         result=0;
1719 out:
1720         return result;
1721 }
1722
1723 static void
1724 qdio_fill_qs(struct qdio_irq *irq_ptr, struct ccw_device *cdev,
1725              int no_input_qs, int no_output_qs,
1726              qdio_handler_t *input_handler,
1727              qdio_handler_t *output_handler,
1728              unsigned long int_parm,int q_format,
1729              unsigned long flags,
1730              void **inbound_sbals_array,
1731              void **outbound_sbals_array)
1732 {
1733         struct qdio_q *q;
1734         int i,j;
1735         char dbf_text[20]; /* see qdio_initialize */
1736         void *ptr;
1737         int available;
1738
1739         sprintf(dbf_text,"qfqs%4x",cdev->private->schid.sch_no);
1740         QDIO_DBF_TEXT0(0,setup,dbf_text);
1741         for (i=0;i<no_input_qs;i++) {
1742                 q=irq_ptr->input_qs[i];
1743
1744                 memset(q,0,((char*)&q->slib)-((char*)q));
1745                 sprintf(dbf_text,"in-q%4x",i);
1746                 QDIO_DBF_TEXT0(0,setup,dbf_text);
1747                 QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));
1748
1749                 memset(q->slib,0,PAGE_SIZE);
1750                 q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);
1751
1752                 available=0;
1753
1754                 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1755                         q->sbal[j]=*(inbound_sbals_array++);
1756
1757                 q->queue_type=q_format;
1758                 q->int_parm=int_parm;
1759                 q->schid = irq_ptr->schid;
1760                 q->irq_ptr = irq_ptr;
1761                 q->cdev = cdev;
1762                 q->mask=1<<(31-i);
1763                 q->q_no=i;
1764                 q->is_input_q=1;
1765                 q->first_to_check=0;
1766                 q->last_move_ftc=0;
1767                 q->handler=input_handler;
1768                 q->dev_st_chg_ind=irq_ptr->dev_st_chg_ind;
1769
1770                 q->tasklet.data=(unsigned long)q;
1771                 /* q->is_thinint_q isn't valid at this time, but
1772                  * irq_ptr->is_thinint_irq is */
1773                 q->tasklet.func=(void(*)(unsigned long))
1774                         ((irq_ptr->is_thinint_irq)?&tiqdio_inbound_processing:
1775                          &qdio_inbound_processing);
1776
1777                 /* actually this is not used for inbound queues. yet. */
1778                 atomic_set(&q->busy_siga_counter,0);
1779                 q->timing.busy_start=0;
1780
1781 /*              for (j=0;j<QDIO_STATS_NUMBER;j++)
1782                         q->timing.last_transfer_times[j]=(qdio_get_micros()/
1783                                                           QDIO_STATS_NUMBER)*j;
1784                 q->timing.last_transfer_index=QDIO_STATS_NUMBER-1;
1785 */
1786
1787                 /* fill in slib */
1788                 if (i>0) irq_ptr->input_qs[i-1]->slib->nsliba=
1789                                  (unsigned long)(q->slib);
1790                 q->slib->sla=(unsigned long)(q->sl);
1791                 q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);
1792
1793                 /* fill in sl */
1794                 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1795                         q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);
1796
1797                 QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
1798                 ptr=(void*)q->sl;
1799                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1800                 ptr=(void*)&q->slsb;
1801                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1802                 ptr=(void*)q->sbal[0];
1803                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1804
1805                 /* fill in slsb */
1806                 if (!irq_ptr->is_qebsm) {
1807                         unsigned int count = 1;
1808                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
1809                                 set_slsb(q, &j, SLSB_P_INPUT_NOT_INIT, &count);
1810                 }
1811         }
1812
1813         for (i=0;i<no_output_qs;i++) {
1814                 q=irq_ptr->output_qs[i];
1815                 memset(q,0,((char*)&q->slib)-((char*)q));
1816
1817                 sprintf(dbf_text,"outq%4x",i);
1818                 QDIO_DBF_TEXT0(0,setup,dbf_text);
1819                 QDIO_DBF_HEX0(0,setup,&q,sizeof(void*));
1820
1821                 memset(q->slib,0,PAGE_SIZE);
1822                 q->sl=(struct sl*)(((char*)q->slib)+PAGE_SIZE/2);
1823
1824                 available=0;
1825                 
1826                 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1827                         q->sbal[j]=*(outbound_sbals_array++);
1828
1829                 q->queue_type=q_format;
1830                 if ((q->queue_type == QDIO_IQDIO_QFMT) &&
1831                     (no_output_qs > 1) &&
1832                     (i == no_output_qs-1))
1833                         q->queue_type = QDIO_IQDIO_QFMT_ASYNCH;
1834                 q->int_parm=int_parm;
1835                 q->is_input_q=0;
1836                 q->is_pci_out = 0;
1837                 q->schid = irq_ptr->schid;
1838                 q->cdev = cdev;
1839                 q->irq_ptr = irq_ptr;
1840                 q->mask=1<<(31-i);
1841                 q->q_no=i;
1842                 q->first_to_check=0;
1843                 q->last_move_ftc=0;
1844                 q->handler=output_handler;
1845
1846                 q->tasklet.data=(unsigned long)q;
1847                 q->tasklet.func=(void(*)(unsigned long))
1848                         &qdio_outbound_processing;
1849                 q->timer.function=(void(*)(unsigned long))
1850                         &qdio_outbound_processing;
1851                 q->timer.data = (long)q;
1852                 init_timer(&q->timer);
1853
1854                 atomic_set(&q->busy_siga_counter,0);
1855                 q->timing.busy_start=0;
1856
1857                 /* fill in slib */
1858                 if (i>0) irq_ptr->output_qs[i-1]->slib->nsliba=
1859                                  (unsigned long)(q->slib);
1860                 q->slib->sla=(unsigned long)(q->sl);
1861                 q->slib->slsba=(unsigned long)(&q->slsb.acc.val[0]);
1862
1863                 /* fill in sl */
1864                 for (j=0;j<QDIO_MAX_BUFFERS_PER_Q;j++)
1865                         q->sl->element[j].sbal=(unsigned long)(q->sbal[j]);
1866
1867                 QDIO_DBF_TEXT2(0,setup,"sl-sb-b0");
1868                 ptr=(void*)q->sl;
1869                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1870                 ptr=(void*)&q->slsb;
1871                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1872                 ptr=(void*)q->sbal[0];
1873                 QDIO_DBF_HEX2(0,setup,&ptr,sizeof(void*));
1874
1875                 /* fill in slsb */
1876                 if (!irq_ptr->is_qebsm) {
1877                         unsigned int count = 1;
1878                         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++)
1879                                 set_slsb(q, &j, SLSB_P_OUTPUT_NOT_INIT, &count);
1880                 }
1881         }
1882 }
1883
1884 static void
1885 qdio_fill_thresholds(struct qdio_irq *irq_ptr,
1886                      unsigned int no_input_qs,
1887                      unsigned int no_output_qs,
1888                      unsigned int min_input_threshold,
1889                      unsigned int max_input_threshold,
1890                      unsigned int min_output_threshold,
1891                      unsigned int max_output_threshold)
1892 {
1893         int i;
1894         struct qdio_q *q;
1895
1896         for (i=0;i<no_input_qs;i++) {
1897                 q=irq_ptr->input_qs[i];
1898                 q->timing.threshold=max_input_threshold;
1899 /*              for (j=0;j<QDIO_STATS_CLASSES;j++) {
1900                         q->threshold_classes[j].threshold=
1901                                 min_input_threshold+
1902                                 (max_input_threshold-min_input_threshold)/
1903                                 QDIO_STATS_CLASSES;
1904                 }
1905                 qdio_use_thresholds(q,QDIO_STATS_CLASSES/2);*/
1906         }
1907         for (i=0;i<no_output_qs;i++) {
1908                 q=irq_ptr->output_qs[i];
1909                 q->timing.threshold=max_output_threshold;
1910 /*              for (j=0;j<QDIO_STATS_CLASSES;j++) {
1911                         q->threshold_classes[j].threshold=
1912                                 min_output_threshold+
1913                                 (max_output_threshold-min_output_threshold)/
1914                                 QDIO_STATS_CLASSES;
1915                 }
1916                 qdio_use_thresholds(q,QDIO_STATS_CLASSES/2);*/
1917         }
1918 }
1919
1920 static int
1921 tiqdio_thinint_handler(void)
1922 {
1923         QDIO_DBF_TEXT4(0,trace,"thin_int");
1924
1925         qdio_perf_stat_inc(&perf_stats.thinints);
1926
1927         /* SVS only when needed:
1928          * issue SVS to benefit from iqdio interrupt avoidance
1929          * (SVS clears AISOI)*/
1930         if (!omit_svs)
1931                 tiqdio_clear_global_summary();
1932
1933         tiqdio_inbound_checks();
1934         return 0;
1935 }
1936
1937 static void
1938 qdio_set_state(struct qdio_irq *irq_ptr, enum qdio_irq_states state)
1939 {
1940         int i;
1941 #ifdef CONFIG_QDIO_DEBUG
1942         char dbf_text[15];
1943
1944         QDIO_DBF_TEXT5(0,trace,"newstate");
1945         sprintf(dbf_text,"%4x%4x",irq_ptr->schid.sch_no,state);
1946         QDIO_DBF_TEXT5(0,trace,dbf_text);
1947 #endif /* CONFIG_QDIO_DEBUG */
1948
1949         irq_ptr->state=state;
1950         for (i=0;i<irq_ptr->no_input_qs;i++)
1951                 irq_ptr->input_qs[i]->state=state;
1952         for (i=0;i<irq_ptr->no_output_qs;i++)
1953                 irq_ptr->output_qs[i]->state=state;
1954         mb();
1955 }
1956
1957 static void
1958 qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
1959 {
1960         char dbf_text[15];
1961
1962         if (irb->esw.esw0.erw.cons) {
1963                 sprintf(dbf_text,"sens%4x",schid.sch_no);
1964                 QDIO_DBF_TEXT2(1,trace,dbf_text);
1965                 QDIO_DBF_HEX0(0,sense,irb,QDIO_DBF_SENSE_LEN);
1966
1967                 QDIO_PRINT_WARN("sense data available on qdio channel.\n");
1968                 QDIO_HEXDUMP16(WARN,"irb: ",irb);
1969                 QDIO_HEXDUMP16(WARN,"sense data: ",irb->ecw);
1970         }
1971                 
1972 }
1973
1974 static void
1975 qdio_handle_pci(struct qdio_irq *irq_ptr)
1976 {
1977         int i;
1978         struct qdio_q *q;
1979
1980         qdio_perf_stat_inc(&perf_stats.pcis);
1981         for (i=0;i<irq_ptr->no_input_qs;i++) {
1982                 q=irq_ptr->input_qs[i];
1983                 if (q->is_input_q&QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT)
1984                         qdio_mark_q(q);
1985                 else {
1986                         __qdio_inbound_processing(q);
1987                 }
1988         }
1989         if (!irq_ptr->hydra_gives_outbound_pcis)
1990                 return;
1991         for (i=0;i<irq_ptr->no_output_qs;i++) {
1992                 q=irq_ptr->output_qs[i];
1993                 if (qdio_is_outbound_q_done(q))
1994                         continue;
1995                 qdio_perf_stat_dec(&perf_stats.tl_runs);
1996                 if (!irq_ptr->sync_done_on_outb_pcis)
1997                         SYNC_MEMORY;
1998                 __qdio_outbound_processing(q);
1999         }
2000 }
2001
2002 static void qdio_establish_handle_irq(struct ccw_device*, int, int);
2003
2004 static void
2005 qdio_handle_activate_check(struct ccw_device *cdev, unsigned long intparm,
2006                            int cstat, int dstat)
2007 {
2008         struct qdio_irq *irq_ptr;
2009         struct qdio_q *q;
2010         char dbf_text[15];
2011
2012         irq_ptr = cdev->private->qdio_data;
2013
2014         QDIO_DBF_TEXT2(1, trace, "ick2");
2015         sprintf(dbf_text,"%s", cdev->dev.bus_id);
2016         QDIO_DBF_TEXT2(1,trace,dbf_text);
2017         QDIO_DBF_HEX2(0,trace,&intparm,sizeof(int));
2018         QDIO_DBF_HEX2(0,trace,&dstat,sizeof(int));
2019         QDIO_DBF_HEX2(0,trace,&cstat,sizeof(int));
2020         QDIO_PRINT_ERR("received check condition on activate " \
2021                        "queues on device %s (cs=x%x, ds=x%x).\n",
2022                        cdev->dev.bus_id, cstat, dstat);
2023         if (irq_ptr->no_input_qs) {
2024                 q=irq_ptr->input_qs[0];
2025         } else if (irq_ptr->no_output_qs) {
2026                 q=irq_ptr->output_qs[0];
2027         } else {
2028                 QDIO_PRINT_ERR("oops... no queue registered for device %s!?\n",
2029                                cdev->dev.bus_id);
2030                 goto omit_handler_call;
2031         }
2032         q->handler(q->cdev,QDIO_STATUS_ACTIVATE_CHECK_CONDITION|
2033                    QDIO_STATUS_LOOK_FOR_ERROR,
2034                    0,0,0,-1,-1,q->int_parm);
2035 omit_handler_call:
2036         qdio_set_state(irq_ptr,QDIO_IRQ_STATE_STOPPED);
2037
2038 }
2039
2040 static void
2041 qdio_call_shutdown(struct work_struct *work)
2042 {
2043         struct ccw_device_private *priv;
2044         struct ccw_device *cdev;
2045
2046         priv = container_of(work, struct ccw_device_private, kick_work);
2047         cdev = priv->cdev;
2048         qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
2049         put_device(&cdev->dev);
2050 }
2051
2052 static void
2053 qdio_timeout_handler(struct ccw_device *cdev)
2054 {
2055         struct qdio_irq *irq_ptr;
2056         char dbf_text[15];
2057
2058         QDIO_DBF_TEXT2(0, trace, "qtoh");
2059         sprintf(dbf_text, "%s", cdev->dev.bus_id);
2060         QDIO_DBF_TEXT2(0, trace, dbf_text);
2061
2062         irq_ptr = cdev->private->qdio_data;
2063         sprintf(dbf_text, "state:%d", irq_ptr->state);
2064         QDIO_DBF_TEXT2(0, trace, dbf_text);
2065
2066         switch (irq_ptr->state) {
2067         case QDIO_IRQ_STATE_INACTIVE:
2068                 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: timed out\n",
2069                                irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2070                 QDIO_DBF_TEXT2(1,setup,"eq:timeo");
2071                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2072                 break;
2073         case QDIO_IRQ_STATE_CLEANUP:
2074                 QDIO_PRINT_INFO("Did not get interrupt on cleanup, "
2075                                 "irq=0.%x.%x.\n",
2076                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2077                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2078                 break;
2079         case QDIO_IRQ_STATE_ESTABLISHED:
2080         case QDIO_IRQ_STATE_ACTIVE:
2081                 /* I/O has been terminated by common I/O layer. */
2082                 QDIO_PRINT_INFO("Queues on irq 0.%x.%04x killed by cio.\n",
2083                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2084                 QDIO_DBF_TEXT2(1, trace, "cio:term");
2085                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
2086                 if (get_device(&cdev->dev)) {
2087                         /* Can't call shutdown from interrupt context. */
2088                         PREPARE_WORK(&cdev->private->kick_work,
2089                                      qdio_call_shutdown);
2090                         queue_work(ccw_device_work, &cdev->private->kick_work);
2091                 }
2092                 break;
2093         default:
2094                 BUG();
2095         }
2096         ccw_device_set_timeout(cdev, 0);
2097         wake_up(&cdev->private->wait_q);
2098 }
2099
2100 static void
2101 qdio_handler(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
2102 {
2103         struct qdio_irq *irq_ptr;
2104         int cstat,dstat;
2105         char dbf_text[15];
2106
2107 #ifdef CONFIG_QDIO_DEBUG
2108         QDIO_DBF_TEXT4(0, trace, "qint");
2109         sprintf(dbf_text, "%s", cdev->dev.bus_id);
2110         QDIO_DBF_TEXT4(0, trace, dbf_text);
2111 #endif /* CONFIG_QDIO_DEBUG */
2112         
2113         if (!intparm) {
2114                 QDIO_PRINT_ERR("got unsolicited interrupt in qdio " \
2115                                   "handler, device %s\n", cdev->dev.bus_id);
2116                 return;
2117         }
2118
2119         irq_ptr = cdev->private->qdio_data;
2120         if (!irq_ptr) {
2121                 QDIO_DBF_TEXT2(1, trace, "uint");
2122                 sprintf(dbf_text,"%s", cdev->dev.bus_id);
2123                 QDIO_DBF_TEXT2(1,trace,dbf_text);
2124                 QDIO_PRINT_ERR("received interrupt on unused device %s!\n",
2125                                cdev->dev.bus_id);
2126                 return;
2127         }
2128
2129         if (IS_ERR(irb)) {
2130                 /* Currently running i/o is in error. */
2131                 switch (PTR_ERR(irb)) {
2132                 case -EIO:
2133                         QDIO_PRINT_ERR("i/o error on device %s\n",
2134                                        cdev->dev.bus_id);
2135                         return;
2136                 case -ETIMEDOUT:
2137                         qdio_timeout_handler(cdev);
2138                         return;
2139                 default:
2140                         QDIO_PRINT_ERR("unknown error state %ld on device %s\n",
2141                                        PTR_ERR(irb), cdev->dev.bus_id);
2142                         return;
2143                 }
2144         }
2145
2146         qdio_irq_check_sense(irq_ptr->schid, irb);
2147
2148 #ifdef CONFIG_QDIO_DEBUG
2149         sprintf(dbf_text, "state:%d", irq_ptr->state);
2150         QDIO_DBF_TEXT4(0, trace, dbf_text);
2151 #endif /* CONFIG_QDIO_DEBUG */
2152
2153         cstat = irb->scsw.cstat;
2154         dstat = irb->scsw.dstat;
2155
2156         switch (irq_ptr->state) {
2157         case QDIO_IRQ_STATE_INACTIVE:
2158                 qdio_establish_handle_irq(cdev, cstat, dstat);
2159                 break;
2160
2161         case QDIO_IRQ_STATE_CLEANUP:
2162                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2163                 break;
2164
2165         case QDIO_IRQ_STATE_ESTABLISHED:
2166         case QDIO_IRQ_STATE_ACTIVE:
2167                 if (cstat & SCHN_STAT_PCI) {
2168                         qdio_handle_pci(irq_ptr);
2169                         break;
2170                 }
2171
2172                 if ((cstat&~SCHN_STAT_PCI)||dstat) {
2173                         qdio_handle_activate_check(cdev, intparm, cstat, dstat);
2174                         break;
2175                 }
2176         default:
2177                 QDIO_PRINT_ERR("got interrupt for queues in state %d on " \
2178                                "device %s?!\n",
2179                                irq_ptr->state, cdev->dev.bus_id);
2180         }
2181         wake_up(&cdev->private->wait_q);
2182
2183 }
2184
2185 int
2186 qdio_synchronize(struct ccw_device *cdev, unsigned int flags,
2187                  unsigned int queue_number)
2188 {
2189         int cc = 0;
2190         struct qdio_q *q;
2191         struct qdio_irq *irq_ptr;
2192         void *ptr;
2193 #ifdef CONFIG_QDIO_DEBUG
2194         char dbf_text[15]="SyncXXXX";
2195 #endif
2196
2197         irq_ptr = cdev->private->qdio_data;
2198         if (!irq_ptr)
2199                 return -ENODEV;
2200
2201 #ifdef CONFIG_QDIO_DEBUG
2202         *((int*)(&dbf_text[4])) = irq_ptr->schid.sch_no;
2203         QDIO_DBF_HEX4(0,trace,dbf_text,QDIO_DBF_TRACE_LEN);
2204         *((int*)(&dbf_text[0]))=flags;
2205         *((int*)(&dbf_text[4]))=queue_number;
2206         QDIO_DBF_HEX4(0,trace,dbf_text,QDIO_DBF_TRACE_LEN);
2207 #endif /* CONFIG_QDIO_DEBUG */
2208
2209         if (flags&QDIO_FLAG_SYNC_INPUT) {
2210                 q=irq_ptr->input_qs[queue_number];
2211                 if (!q)
2212                         return -EINVAL;
2213                 if (!(irq_ptr->is_qebsm))
2214                         cc = do_siga_sync(q->schid, 0, q->mask);
2215         } else if (flags&QDIO_FLAG_SYNC_OUTPUT) {
2216                 q=irq_ptr->output_qs[queue_number];
2217                 if (!q)
2218                         return -EINVAL;
2219                 if (!(irq_ptr->is_qebsm))
2220                         cc = do_siga_sync(q->schid, q->mask, 0);
2221         } else 
2222                 return -EINVAL;
2223
2224         ptr=&cc;
2225         if (cc)
2226                 QDIO_DBF_HEX3(0,trace,&ptr,sizeof(int));
2227
2228         return cc;
2229 }
2230
2231 static void
2232 qdio_check_subchannel_qebsm(struct qdio_irq *irq_ptr, unsigned char qdioac,
2233                             unsigned long token)
2234 {
2235         struct qdio_q *q;
2236         int i;
2237         unsigned int count, start_buf;
2238         char dbf_text[15];
2239
2240         /*check if QEBSM is disabled */
2241         if (!(irq_ptr->is_qebsm) || !(qdioac & 0x01)) {
2242                 irq_ptr->is_qebsm  = 0;
2243                 irq_ptr->sch_token = 0;
2244                 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
2245                 QDIO_DBF_TEXT0(0,setup,"noV=V");
2246                 return;
2247         }
2248         irq_ptr->sch_token = token;
2249         /*input queue*/
2250         for (i = 0; i < irq_ptr->no_input_qs;i++) {
2251                 q = irq_ptr->input_qs[i];
2252                 count = QDIO_MAX_BUFFERS_PER_Q;
2253                 start_buf = 0;
2254                 set_slsb(q, &start_buf, SLSB_P_INPUT_NOT_INIT, &count);
2255         }
2256         sprintf(dbf_text,"V=V:%2x",irq_ptr->is_qebsm);
2257         QDIO_DBF_TEXT0(0,setup,dbf_text);
2258         sprintf(dbf_text,"%8lx",irq_ptr->sch_token);
2259         QDIO_DBF_TEXT0(0,setup,dbf_text);
2260         /*output queue*/
2261         for (i = 0; i < irq_ptr->no_output_qs; i++) {
2262                 q = irq_ptr->output_qs[i];
2263                 count = QDIO_MAX_BUFFERS_PER_Q;
2264                 start_buf = 0;
2265                 set_slsb(q, &start_buf, SLSB_P_OUTPUT_NOT_INIT, &count);
2266         }
2267 }
2268
2269 static void
2270 qdio_get_ssqd_information(struct qdio_irq *irq_ptr)
2271 {
2272         int result;
2273         unsigned char qdioac;
2274         struct {
2275                 struct chsc_header request;
2276                 u16 reserved1:10;
2277                 u16 ssid:2;
2278                 u16 fmt:4;
2279                 u16 first_sch;
2280                 u16 reserved2;
2281                 u16 last_sch;
2282                 u32 reserved3;
2283                 struct chsc_header response;
2284                 u32 reserved4;
2285                 u8  flags;
2286                 u8  reserved5;
2287                 u16 sch;
2288                 u8  qfmt;
2289                 u8  parm;
2290                 u8  qdioac1;
2291                 u8  sch_class;
2292                 u8  reserved7;
2293                 u8  icnt;
2294                 u8  reserved8;
2295                 u8  ocnt;
2296                 u8 reserved9;
2297                 u8 mbccnt;
2298                 u16 qdioac2;
2299                 u64 sch_token;
2300         } *ssqd_area;
2301
2302         QDIO_DBF_TEXT0(0,setup,"getssqd");
2303         qdioac = 0;
2304         ssqd_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2305         if (!ssqd_area) {
2306                 QDIO_PRINT_WARN("Could not get memory for chsc. Using all " \
2307                                 "SIGAs for sch x%x.\n", irq_ptr->schid.sch_no);
2308                 irq_ptr->qdioac = CHSC_FLAG_SIGA_INPUT_NECESSARY ||
2309                                   CHSC_FLAG_SIGA_OUTPUT_NECESSARY ||
2310                                   CHSC_FLAG_SIGA_SYNC_NECESSARY; /* all flags set */
2311                 irq_ptr->is_qebsm = 0;
2312                 irq_ptr->sch_token = 0;
2313                 irq_ptr->qib.rflags &= ~QIB_RFLAGS_ENABLE_QEBSM;
2314                 return;
2315         }
2316
2317         ssqd_area->request = (struct chsc_header) {
2318                 .length = 0x0010,
2319                 .code   = 0x0024,
2320         };
2321         ssqd_area->first_sch = irq_ptr->schid.sch_no;
2322         ssqd_area->last_sch = irq_ptr->schid.sch_no;
2323         ssqd_area->ssid = irq_ptr->schid.ssid;
2324         result = chsc(ssqd_area);
2325
2326         if (result) {
2327                 QDIO_PRINT_WARN("CHSC returned cc %i. Using all " \
2328                                 "SIGAs for sch 0.%x.%x.\n", result,
2329                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2330                 qdioac = CHSC_FLAG_SIGA_INPUT_NECESSARY ||
2331                         CHSC_FLAG_SIGA_OUTPUT_NECESSARY ||
2332                         CHSC_FLAG_SIGA_SYNC_NECESSARY; /* all flags set */
2333                 irq_ptr->is_qebsm  = 0;
2334                 goto out;
2335         }
2336
2337         if (ssqd_area->response.code != QDIO_CHSC_RESPONSE_CODE_OK) {
2338                 QDIO_PRINT_WARN("response upon checking SIGA needs " \
2339                                 "is 0x%x. Using all SIGAs for sch 0.%x.%x.\n",
2340                                 ssqd_area->response.code,
2341                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2342                 qdioac = CHSC_FLAG_SIGA_INPUT_NECESSARY ||
2343                         CHSC_FLAG_SIGA_OUTPUT_NECESSARY ||
2344                         CHSC_FLAG_SIGA_SYNC_NECESSARY; /* all flags set */
2345                 irq_ptr->is_qebsm  = 0;
2346                 goto out;
2347         }
2348         if (!(ssqd_area->flags & CHSC_FLAG_QDIO_CAPABILITY) ||
2349             !(ssqd_area->flags & CHSC_FLAG_VALIDITY) ||
2350             (ssqd_area->sch != irq_ptr->schid.sch_no)) {
2351                 QDIO_PRINT_WARN("huh? problems checking out sch 0.%x.%x... " \
2352                                 "using all SIGAs.\n",
2353                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2354                 qdioac = CHSC_FLAG_SIGA_INPUT_NECESSARY |
2355                         CHSC_FLAG_SIGA_OUTPUT_NECESSARY |
2356                         CHSC_FLAG_SIGA_SYNC_NECESSARY; /* worst case */
2357                 irq_ptr->is_qebsm  = 0;
2358                 goto out;
2359         }
2360         qdioac = ssqd_area->qdioac1;
2361 out:
2362         qdio_check_subchannel_qebsm(irq_ptr, qdioac,
2363                                     ssqd_area->sch_token);
2364         mempool_free(ssqd_area, qdio_mempool_scssc);
2365         irq_ptr->qdioac = qdioac;
2366 }
2367
2368 static unsigned int
2369 tiqdio_check_chsc_availability(void)
2370 {
2371         char dbf_text[15];
2372
2373         if (!css_characteristics_avail)
2374                 return -EIO;
2375
2376         /* Check for bit 41. */
2377         if (!css_general_characteristics.aif) {
2378                 QDIO_PRINT_WARN("Adapter interruption facility not " \
2379                                 "installed.\n");
2380                 return -ENOENT;
2381         }
2382
2383         /* Check for bits 107 and 108. */
2384         if (!css_chsc_characteristics.scssc ||
2385             !css_chsc_characteristics.scsscf) {
2386                 QDIO_PRINT_WARN("Set Chan Subsys. Char. & Fast-CHSCs " \
2387                                 "not available.\n");
2388                 return -ENOENT;
2389         }
2390
2391         /* Check for OSA/FCP thin interrupts (bit 67). */
2392         hydra_thinints = css_general_characteristics.aif_osa;
2393         sprintf(dbf_text,"hydrati%1x", hydra_thinints);
2394         QDIO_DBF_TEXT0(0,setup,dbf_text);
2395
2396 #ifdef CONFIG_64BIT
2397         /* Check for QEBSM support in general (bit 58). */
2398         is_passthrough = css_general_characteristics.qebsm;
2399 #endif
2400         sprintf(dbf_text,"cssQBS:%1x", is_passthrough);
2401         QDIO_DBF_TEXT0(0,setup,dbf_text);
2402
2403         /* Check for aif time delay disablement fac (bit 56). If installed,
2404          * omit svs even under lpar (good point by rick again) */
2405         omit_svs = css_general_characteristics.aif_tdd;
2406         sprintf(dbf_text,"omitsvs%1x", omit_svs);
2407         QDIO_DBF_TEXT0(0,setup,dbf_text);
2408         return 0;
2409 }
2410
2411
2412 static unsigned int
2413 tiqdio_set_subchannel_ind(struct qdio_irq *irq_ptr, int reset_to_zero)
2414 {
2415         unsigned long real_addr_local_summary_bit;
2416         unsigned long real_addr_dev_st_chg_ind;
2417         void *ptr;
2418         char dbf_text[15];
2419
2420         unsigned int resp_code;
2421         int result;
2422
2423         struct {
2424                 struct chsc_header request;
2425                 u16 operation_code;
2426                 u16 reserved1;
2427                 u32 reserved2;
2428                 u32 reserved3;
2429                 u64 summary_indicator_addr;
2430                 u64 subchannel_indicator_addr;
2431                 u32 ks:4;
2432                 u32 kc:4;
2433                 u32 reserved4:21;
2434                 u32 isc:3;
2435                 u32 word_with_d_bit;
2436                 /* set to 0x10000000 to enable
2437                  * time delay disablement facility */
2438                 u32 reserved5;
2439                 struct subchannel_id schid;
2440                 u32 reserved6[1004];
2441                 struct chsc_header response;
2442                 u32 reserved7;
2443         } *scssc_area;
2444
2445         if (!irq_ptr->is_thinint_irq)
2446                 return -ENODEV;
2447
2448         if (reset_to_zero) {
2449                 real_addr_local_summary_bit=0;
2450                 real_addr_dev_st_chg_ind=0;
2451         } else {
2452                 real_addr_local_summary_bit=
2453                         virt_to_phys((volatile void *)indicators);
2454                 real_addr_dev_st_chg_ind=
2455                         virt_to_phys((volatile void *)irq_ptr->dev_st_chg_ind);
2456         }
2457
2458         scssc_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2459         if (!scssc_area) {
2460                 QDIO_PRINT_WARN("No memory for setting indicators on " \
2461                                 "subchannel 0.%x.%x.\n",
2462                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2463                 return -ENOMEM;
2464         }
2465         scssc_area->request = (struct chsc_header) {
2466                 .length = 0x0fe0,
2467                 .code   = 0x0021,
2468         };
2469         scssc_area->operation_code = 0;
2470
2471         scssc_area->summary_indicator_addr = real_addr_local_summary_bit;
2472         scssc_area->subchannel_indicator_addr = real_addr_dev_st_chg_ind;
2473         scssc_area->ks = QDIO_STORAGE_KEY;
2474         scssc_area->kc = QDIO_STORAGE_KEY;
2475         scssc_area->isc = TIQDIO_THININT_ISC;
2476         scssc_area->schid = irq_ptr->schid;
2477         /* enables the time delay disablement facility. Don't care
2478          * whether it is really there (i.e. we haven't checked for
2479          * it) */
2480         if (css_general_characteristics.aif_tdd)
2481                 scssc_area->word_with_d_bit = 0x10000000;
2482         else
2483                 QDIO_PRINT_WARN("Time delay disablement facility " \
2484                                 "not available\n");
2485
2486         result = chsc(scssc_area);
2487         if (result) {
2488                 QDIO_PRINT_WARN("could not set indicators on irq 0.%x.%x, " \
2489                                 "cc=%i.\n",
2490                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,result);
2491                 result = -EIO;
2492                 goto out;
2493         }
2494
2495         resp_code = scssc_area->response.code;
2496         if (resp_code!=QDIO_CHSC_RESPONSE_CODE_OK) {
2497                 QDIO_PRINT_WARN("response upon setting indicators " \
2498                                 "is 0x%x.\n",resp_code);
2499                 sprintf(dbf_text,"sidR%4x",resp_code);
2500                 QDIO_DBF_TEXT1(0,trace,dbf_text);
2501                 QDIO_DBF_TEXT1(0,setup,dbf_text);
2502                 ptr=&scssc_area->response;
2503                 QDIO_DBF_HEX2(1,setup,&ptr,QDIO_DBF_SETUP_LEN);
2504                 result = -EIO;
2505                 goto out;
2506         }
2507
2508         QDIO_DBF_TEXT2(0,setup,"setscind");
2509         QDIO_DBF_HEX2(0,setup,&real_addr_local_summary_bit,
2510                       sizeof(unsigned long));
2511         QDIO_DBF_HEX2(0,setup,&real_addr_dev_st_chg_ind,sizeof(unsigned long));
2512         result = 0;
2513 out:
2514         mempool_free(scssc_area, qdio_mempool_scssc);
2515         return result;
2516
2517 }
2518
2519 static unsigned int
2520 tiqdio_set_delay_target(struct qdio_irq *irq_ptr, unsigned long delay_target)
2521 {
2522         unsigned int resp_code;
2523         int result;
2524         void *ptr;
2525         char dbf_text[15];
2526
2527         struct {
2528                 struct chsc_header request;
2529                 u16 operation_code;
2530                 u16 reserved1;
2531                 u32 reserved2;
2532                 u32 reserved3;
2533                 u32 reserved4[2];
2534                 u32 delay_target;
2535                 u32 reserved5[1009];
2536                 struct chsc_header response;
2537                 u32 reserved6;
2538         } *scsscf_area;
2539
2540         if (!irq_ptr->is_thinint_irq)
2541                 return -ENODEV;
2542
2543         scsscf_area = mempool_alloc(qdio_mempool_scssc, GFP_ATOMIC);
2544         if (!scsscf_area) {
2545                 QDIO_PRINT_WARN("No memory for setting delay target on " \
2546                                 "subchannel 0.%x.%x.\n",
2547                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
2548                 return -ENOMEM;
2549         }
2550         scsscf_area->request = (struct chsc_header) {
2551                 .length = 0x0fe0,
2552                 .code   = 0x1027,
2553         };
2554
2555         scsscf_area->delay_target = delay_target<<16;
2556
2557         result=chsc(scsscf_area);
2558         if (result) {
2559                 QDIO_PRINT_WARN("could not set delay target on irq 0.%x.%x, " \
2560                                 "cc=%i. Continuing.\n",
2561                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2562                                 result);
2563                 result = -EIO;
2564                 goto out;
2565         }
2566
2567         resp_code = scsscf_area->response.code;
2568         if (resp_code!=QDIO_CHSC_RESPONSE_CODE_OK) {
2569                 QDIO_PRINT_WARN("response upon setting delay target " \
2570                                 "is 0x%x. Continuing.\n",resp_code);
2571                 sprintf(dbf_text,"sdtR%4x",resp_code);
2572                 QDIO_DBF_TEXT1(0,trace,dbf_text);
2573                 QDIO_DBF_TEXT1(0,setup,dbf_text);
2574                 ptr=&scsscf_area->response;
2575                 QDIO_DBF_HEX2(1,trace,&ptr,QDIO_DBF_TRACE_LEN);
2576         }
2577         QDIO_DBF_TEXT2(0,trace,"delytrgt");
2578         QDIO_DBF_HEX2(0,trace,&delay_target,sizeof(unsigned long));
2579         result = 0; /* not critical */
2580 out:
2581         mempool_free(scsscf_area, qdio_mempool_scssc);
2582         return result;
2583 }
2584
2585 int
2586 qdio_cleanup(struct ccw_device *cdev, int how)
2587 {
2588         struct qdio_irq *irq_ptr;
2589         char dbf_text[15];
2590         int rc;
2591
2592         irq_ptr = cdev->private->qdio_data;
2593         if (!irq_ptr)
2594                 return -ENODEV;
2595
2596         sprintf(dbf_text,"qcln%4x",irq_ptr->schid.sch_no);
2597         QDIO_DBF_TEXT1(0,trace,dbf_text);
2598         QDIO_DBF_TEXT0(0,setup,dbf_text);
2599
2600         rc = qdio_shutdown(cdev, how);
2601         if ((rc == 0) || (rc == -EINPROGRESS))
2602                 rc = qdio_free(cdev);
2603         return rc;
2604 }
2605
2606 int
2607 qdio_shutdown(struct ccw_device *cdev, int how)
2608 {
2609         struct qdio_irq *irq_ptr;
2610         int i;
2611         int result = 0;
2612         int rc;
2613         unsigned long flags;
2614         int timeout;
2615         char dbf_text[15];
2616
2617         irq_ptr = cdev->private->qdio_data;
2618         if (!irq_ptr)
2619                 return -ENODEV;
2620
2621         down(&irq_ptr->setting_up_sema);
2622
2623         sprintf(dbf_text,"qsqs%4x",irq_ptr->schid.sch_no);
2624         QDIO_DBF_TEXT1(0,trace,dbf_text);
2625         QDIO_DBF_TEXT0(0,setup,dbf_text);
2626
2627         /* mark all qs as uninteresting */
2628         for (i=0;i<irq_ptr->no_input_qs;i++)
2629                 atomic_set(&irq_ptr->input_qs[i]->is_in_shutdown,1);
2630
2631         for (i=0;i<irq_ptr->no_output_qs;i++)
2632                 atomic_set(&irq_ptr->output_qs[i]->is_in_shutdown,1);
2633
2634         tasklet_kill(&tiqdio_tasklet);
2635
2636         for (i=0;i<irq_ptr->no_input_qs;i++) {
2637                 qdio_unmark_q(irq_ptr->input_qs[i]);
2638                 tasklet_kill(&irq_ptr->input_qs[i]->tasklet);
2639                 wait_event_interruptible_timeout(cdev->private->wait_q,
2640                                                  !atomic_read(&irq_ptr->
2641                                                               input_qs[i]->
2642                                                               use_count),
2643                                                  QDIO_NO_USE_COUNT_TIMEOUT);
2644                 if (atomic_read(&irq_ptr->input_qs[i]->use_count))
2645                         result=-EINPROGRESS;
2646         }
2647
2648         for (i=0;i<irq_ptr->no_output_qs;i++) {
2649                 tasklet_kill(&irq_ptr->output_qs[i]->tasklet);
2650                 del_timer(&irq_ptr->output_qs[i]->timer);
2651                 wait_event_interruptible_timeout(cdev->private->wait_q,
2652                                                  !atomic_read(&irq_ptr->
2653                                                               output_qs[i]->
2654                                                               use_count),
2655                                                  QDIO_NO_USE_COUNT_TIMEOUT);
2656                 if (atomic_read(&irq_ptr->output_qs[i]->use_count))
2657                         result=-EINPROGRESS;
2658         }
2659
2660         /* cleanup subchannel */
2661         spin_lock_irqsave(get_ccwdev_lock(cdev),flags);
2662         if (how&QDIO_FLAG_CLEANUP_USING_CLEAR) {
2663                 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
2664                 timeout=QDIO_CLEANUP_CLEAR_TIMEOUT;
2665         } else if (how&QDIO_FLAG_CLEANUP_USING_HALT) {
2666                 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
2667                 timeout=QDIO_CLEANUP_HALT_TIMEOUT;
2668         } else { /* default behaviour */
2669                 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
2670                 timeout=QDIO_CLEANUP_HALT_TIMEOUT;
2671         }
2672         if (rc == -ENODEV) {
2673                 /* No need to wait for device no longer present. */
2674                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2675                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2676         } else if (((void *)cdev->handler != (void *)qdio_handler) && rc == 0) {
2677                 /*
2678                  * Whoever put another handler there, has to cope with the
2679                  * interrupt theirself. Might happen if qdio_shutdown was
2680                  * called on already shutdown queues, but this shouldn't have
2681                  * bad side effects.
2682                  */
2683                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2684                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2685         } else if (rc == 0) {
2686                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
2687                 ccw_device_set_timeout(cdev, timeout);
2688                 spin_unlock_irqrestore(get_ccwdev_lock(cdev),flags);
2689
2690                 wait_event(cdev->private->wait_q,
2691                            irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
2692                            irq_ptr->state == QDIO_IRQ_STATE_ERR);
2693         } else {
2694                 QDIO_PRINT_INFO("ccw_device_{halt,clear} returned %d for "
2695                                 "device %s\n", result, cdev->dev.bus_id);
2696                 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
2697                 result = rc;
2698                 goto out;
2699         }
2700         if (irq_ptr->is_thinint_irq) {
2701                 qdio_put_indicator((__u32*)irq_ptr->dev_st_chg_ind);
2702                 tiqdio_set_subchannel_ind(irq_ptr,1); 
2703                 /* reset adapter interrupt indicators */
2704         }
2705
2706         /* exchange int handlers, if necessary */
2707         if ((void*)cdev->handler == (void*)qdio_handler)
2708                 cdev->handler=irq_ptr->original_int_handler;
2709
2710         /* Ignore errors. */
2711         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
2712         ccw_device_set_timeout(cdev, 0);
2713 out:
2714         up(&irq_ptr->setting_up_sema);
2715         return result;
2716 }
2717
2718 int
2719 qdio_free(struct ccw_device *cdev)
2720 {
2721         struct qdio_irq *irq_ptr;
2722         char dbf_text[15];
2723
2724         irq_ptr = cdev->private->qdio_data;
2725         if (!irq_ptr)
2726                 return -ENODEV;
2727
2728         down(&irq_ptr->setting_up_sema);
2729
2730         sprintf(dbf_text,"qfqs%4x",irq_ptr->schid.sch_no);
2731         QDIO_DBF_TEXT1(0,trace,dbf_text);
2732         QDIO_DBF_TEXT0(0,setup,dbf_text);
2733
2734         cdev->private->qdio_data = NULL;
2735
2736         up(&irq_ptr->setting_up_sema);
2737
2738         qdio_release_irq_memory(irq_ptr);
2739         module_put(THIS_MODULE);
2740         return 0;
2741 }
2742
2743 static void
2744 qdio_allocate_do_dbf(struct qdio_initialize *init_data)
2745 {
2746         char dbf_text[20]; /* if a printf printed out more than 8 chars */
2747
2748         sprintf(dbf_text,"qfmt:%x",init_data->q_format);
2749         QDIO_DBF_TEXT0(0,setup,dbf_text);
2750         QDIO_DBF_HEX0(0,setup,init_data->adapter_name,8);
2751         sprintf(dbf_text,"qpff%4x",init_data->qib_param_field_format);
2752         QDIO_DBF_TEXT0(0,setup,dbf_text);
2753         QDIO_DBF_HEX0(0,setup,&init_data->qib_param_field,sizeof(char*));
2754         QDIO_DBF_HEX0(0,setup,&init_data->input_slib_elements,sizeof(long*));
2755         QDIO_DBF_HEX0(0,setup,&init_data->output_slib_elements,sizeof(long*));
2756         sprintf(dbf_text,"miit%4x",init_data->min_input_threshold);
2757         QDIO_DBF_TEXT0(0,setup,dbf_text);
2758         sprintf(dbf_text,"mait%4x",init_data->max_input_threshold);
2759         QDIO_DBF_TEXT0(0,setup,dbf_text);
2760         sprintf(dbf_text,"miot%4x",init_data->min_output_threshold);
2761         QDIO_DBF_TEXT0(0,setup,dbf_text);
2762         sprintf(dbf_text,"maot%4x",init_data->max_output_threshold);
2763         QDIO_DBF_TEXT0(0,setup,dbf_text);
2764         sprintf(dbf_text,"niq:%4x",init_data->no_input_qs);
2765         QDIO_DBF_TEXT0(0,setup,dbf_text);
2766         sprintf(dbf_text,"noq:%4x",init_data->no_output_qs);
2767         QDIO_DBF_TEXT0(0,setup,dbf_text);
2768         QDIO_DBF_HEX0(0,setup,&init_data->input_handler,sizeof(void*));
2769         QDIO_DBF_HEX0(0,setup,&init_data->output_handler,sizeof(void*));
2770         QDIO_DBF_HEX0(0,setup,&init_data->int_parm,sizeof(long));
2771         QDIO_DBF_HEX0(0,setup,&init_data->flags,sizeof(long));
2772         QDIO_DBF_HEX0(0,setup,&init_data->input_sbal_addr_array,sizeof(void*));
2773         QDIO_DBF_HEX0(0,setup,&init_data->output_sbal_addr_array,sizeof(void*));
2774 }
2775
2776 static void
2777 qdio_allocate_fill_input_desc(struct qdio_irq *irq_ptr, int i, int iqfmt)
2778 {
2779         irq_ptr->input_qs[i]->is_iqdio_q = iqfmt;
2780         irq_ptr->input_qs[i]->is_thinint_q = irq_ptr->is_thinint_irq;
2781
2782         irq_ptr->qdr->qdf0[i].sliba=(unsigned long)(irq_ptr->input_qs[i]->slib);
2783
2784         irq_ptr->qdr->qdf0[i].sla=(unsigned long)(irq_ptr->input_qs[i]->sl);
2785
2786         irq_ptr->qdr->qdf0[i].slsba=
2787                 (unsigned long)(&irq_ptr->input_qs[i]->slsb.acc.val[0]);
2788
2789         irq_ptr->qdr->qdf0[i].akey=QDIO_STORAGE_KEY;
2790         irq_ptr->qdr->qdf0[i].bkey=QDIO_STORAGE_KEY;
2791         irq_ptr->qdr->qdf0[i].ckey=QDIO_STORAGE_KEY;
2792         irq_ptr->qdr->qdf0[i].dkey=QDIO_STORAGE_KEY;
2793 }
2794
2795 static void
2796 qdio_allocate_fill_output_desc(struct qdio_irq *irq_ptr, int i,
2797                                int j, int iqfmt)
2798 {
2799         irq_ptr->output_qs[i]->is_iqdio_q = iqfmt;
2800         irq_ptr->output_qs[i]->is_thinint_q = irq_ptr->is_thinint_irq;
2801
2802         irq_ptr->qdr->qdf0[i+j].sliba=(unsigned long)(irq_ptr->output_qs[i]->slib);
2803
2804         irq_ptr->qdr->qdf0[i+j].sla=(unsigned long)(irq_ptr->output_qs[i]->sl);
2805
2806         irq_ptr->qdr->qdf0[i+j].slsba=
2807                 (unsigned long)(&irq_ptr->output_qs[i]->slsb.acc.val[0]);
2808
2809         irq_ptr->qdr->qdf0[i+j].akey=QDIO_STORAGE_KEY;
2810         irq_ptr->qdr->qdf0[i+j].bkey=QDIO_STORAGE_KEY;
2811         irq_ptr->qdr->qdf0[i+j].ckey=QDIO_STORAGE_KEY;
2812         irq_ptr->qdr->qdf0[i+j].dkey=QDIO_STORAGE_KEY;
2813 }
2814
2815
2816 static void
2817 qdio_initialize_set_siga_flags_input(struct qdio_irq *irq_ptr)
2818 {
2819         int i;
2820
2821         for (i=0;i<irq_ptr->no_input_qs;i++) {
2822                 irq_ptr->input_qs[i]->siga_sync=
2823                         irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY;
2824                 irq_ptr->input_qs[i]->siga_in=
2825                         irq_ptr->qdioac&CHSC_FLAG_SIGA_INPUT_NECESSARY;
2826                 irq_ptr->input_qs[i]->siga_out=
2827                         irq_ptr->qdioac&CHSC_FLAG_SIGA_OUTPUT_NECESSARY;
2828                 irq_ptr->input_qs[i]->siga_sync_done_on_thinints=
2829                         irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS;
2830                 irq_ptr->input_qs[i]->hydra_gives_outbound_pcis=
2831                         irq_ptr->hydra_gives_outbound_pcis;
2832                 irq_ptr->input_qs[i]->siga_sync_done_on_outb_tis=
2833                         ((irq_ptr->qdioac&
2834                           (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2835                            CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS))==
2836                          (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2837                           CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS));
2838
2839         }
2840 }
2841
2842 static void
2843 qdio_initialize_set_siga_flags_output(struct qdio_irq *irq_ptr)
2844 {
2845         int i;
2846
2847         for (i=0;i<irq_ptr->no_output_qs;i++) {
2848                 irq_ptr->output_qs[i]->siga_sync=
2849                         irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY;
2850                 irq_ptr->output_qs[i]->siga_in=
2851                         irq_ptr->qdioac&CHSC_FLAG_SIGA_INPUT_NECESSARY;
2852                 irq_ptr->output_qs[i]->siga_out=
2853                         irq_ptr->qdioac&CHSC_FLAG_SIGA_OUTPUT_NECESSARY;
2854                 irq_ptr->output_qs[i]->siga_sync_done_on_thinints=
2855                         irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS;
2856                 irq_ptr->output_qs[i]->hydra_gives_outbound_pcis=
2857                         irq_ptr->hydra_gives_outbound_pcis;
2858                 irq_ptr->output_qs[i]->siga_sync_done_on_outb_tis=
2859                         ((irq_ptr->qdioac&
2860                           (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2861                            CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS))==
2862                          (CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS|
2863                           CHSC_FLAG_SIGA_SYNC_DONE_ON_THININTS));
2864
2865         }
2866 }
2867
2868 static int
2869 qdio_establish_irq_check_for_errors(struct ccw_device *cdev, int cstat,
2870                                     int dstat)
2871 {
2872         char dbf_text[15];
2873         struct qdio_irq *irq_ptr;
2874
2875         irq_ptr = cdev->private->qdio_data;
2876
2877         if (cstat || (dstat & ~(DEV_STAT_CHN_END|DEV_STAT_DEV_END))) {
2878                 sprintf(dbf_text,"ick1%4x",irq_ptr->schid.sch_no);
2879                 QDIO_DBF_TEXT2(1,trace,dbf_text);
2880                 QDIO_DBF_HEX2(0,trace,&dstat,sizeof(int));
2881                 QDIO_DBF_HEX2(0,trace,&cstat,sizeof(int));
2882                 QDIO_PRINT_ERR("received check condition on establish " \
2883                                "queues on irq 0.%x.%x (cs=x%x, ds=x%x).\n",
2884                                irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2885                                cstat,dstat);
2886                 qdio_set_state(irq_ptr,QDIO_IRQ_STATE_ERR);
2887         }
2888         
2889         if (!(dstat & DEV_STAT_DEV_END)) {
2890                 QDIO_DBF_TEXT2(1,setup,"eq:no de");
2891                 QDIO_DBF_HEX2(0,setup,&dstat, sizeof(dstat));
2892                 QDIO_DBF_HEX2(0,setup,&cstat, sizeof(cstat));
2893                 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: didn't get "
2894                                "device end: dstat=%02x, cstat=%02x\n",
2895                                irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
2896                                dstat, cstat);
2897                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2898                 return 1;
2899         }
2900
2901         if (dstat & ~(DEV_STAT_CHN_END|DEV_STAT_DEV_END)) {
2902                 QDIO_DBF_TEXT2(1,setup,"eq:badio");
2903                 QDIO_DBF_HEX2(0,setup,&dstat, sizeof(dstat));
2904                 QDIO_DBF_HEX2(0,setup,&cstat, sizeof(cstat));
2905                 QDIO_PRINT_ERR("establish queues on irq 0.%x.%04x: got "
2906                                "the following devstat: dstat=%02x, "
2907                                "cstat=%02x\n", irq_ptr->schid.ssid,
2908                                irq_ptr->schid.sch_no, dstat, cstat);
2909                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
2910                 return 1;
2911         }
2912         return 0;
2913 }
2914
2915 static void
2916 qdio_establish_handle_irq(struct ccw_device *cdev, int cstat, int dstat)
2917 {
2918         struct qdio_irq *irq_ptr;
2919         char dbf_text[15];
2920
2921         irq_ptr = cdev->private->qdio_data;
2922
2923         sprintf(dbf_text,"qehi%4x",cdev->private->schid.sch_no);
2924         QDIO_DBF_TEXT0(0,setup,dbf_text);
2925         QDIO_DBF_TEXT0(0,trace,dbf_text);
2926
2927         if (qdio_establish_irq_check_for_errors(cdev, cstat, dstat)) {
2928                 ccw_device_set_timeout(cdev, 0);
2929                 return;
2930         }
2931
2932         qdio_set_state(irq_ptr,QDIO_IRQ_STATE_ESTABLISHED);
2933         ccw_device_set_timeout(cdev, 0);
2934 }
2935
2936 int
2937 qdio_initialize(struct qdio_initialize *init_data)
2938 {
2939         int rc;
2940         char dbf_text[15];
2941
2942         sprintf(dbf_text,"qini%4x",init_data->cdev->private->schid.sch_no);
2943         QDIO_DBF_TEXT0(0,setup,dbf_text);
2944         QDIO_DBF_TEXT0(0,trace,dbf_text);
2945
2946         rc = qdio_allocate(init_data);
2947         if (rc == 0) {
2948                 rc = qdio_establish(init_data);
2949                 if (rc != 0)
2950                         qdio_free(init_data->cdev);
2951         }
2952
2953         return rc;
2954 }
2955
2956
2957 int
2958 qdio_allocate(struct qdio_initialize *init_data)
2959 {
2960         struct qdio_irq *irq_ptr;
2961         char dbf_text[15];
2962
2963         sprintf(dbf_text,"qalc%4x",init_data->cdev->private->schid.sch_no);
2964         QDIO_DBF_TEXT0(0,setup,dbf_text);
2965         QDIO_DBF_TEXT0(0,trace,dbf_text);
2966         if ( (init_data->no_input_qs>QDIO_MAX_QUEUES_PER_IRQ) ||
2967              (init_data->no_output_qs>QDIO_MAX_QUEUES_PER_IRQ) ||
2968              ((init_data->no_input_qs) && (!init_data->input_handler)) ||
2969              ((init_data->no_output_qs) && (!init_data->output_handler)) )
2970                 return -EINVAL;
2971
2972         if (!init_data->input_sbal_addr_array)
2973                 return -EINVAL;
2974
2975         if (!init_data->output_sbal_addr_array)
2976                 return -EINVAL;
2977
2978         qdio_allocate_do_dbf(init_data);
2979
2980         /* create irq */
2981         irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2982
2983         QDIO_DBF_TEXT0(0,setup,"irq_ptr:");
2984         QDIO_DBF_HEX0(0,setup,&irq_ptr,sizeof(void*));
2985
2986         if (!irq_ptr) {
2987                 QDIO_PRINT_ERR("kmalloc of irq_ptr failed!\n");
2988                 return -ENOMEM;
2989         }
2990
2991         init_MUTEX(&irq_ptr->setting_up_sema);
2992
2993         /* QDR must be in DMA area since CCW data address is only 32 bit */
2994         irq_ptr->qdr=kmalloc(sizeof(struct qdr), GFP_KERNEL | GFP_DMA);
2995         if (!(irq_ptr->qdr)) {
2996                 free_page((unsigned long) irq_ptr);
2997                 QDIO_PRINT_ERR("kmalloc of irq_ptr->qdr failed!\n");
2998                 return -ENOMEM;
2999         }
3000         QDIO_DBF_TEXT0(0,setup,"qdr:");
3001         QDIO_DBF_HEX0(0,setup,&irq_ptr->qdr,sizeof(void*));
3002
3003         if (qdio_alloc_qs(irq_ptr,
3004                           init_data->no_input_qs,
3005                           init_data->no_output_qs)) {
3006                 qdio_release_irq_memory(irq_ptr);
3007                 return -ENOMEM;
3008         }
3009
3010         init_data->cdev->private->qdio_data = irq_ptr;
3011
3012         qdio_set_state(irq_ptr,QDIO_IRQ_STATE_INACTIVE);
3013
3014         return 0;
3015 }
3016
3017 static int qdio_fill_irq(struct qdio_initialize *init_data)
3018 {
3019         int i;
3020         char dbf_text[15];
3021         struct ciw *ciw;
3022         int is_iqdio;
3023         struct qdio_irq *irq_ptr;
3024
3025         irq_ptr = init_data->cdev->private->qdio_data;
3026
3027         memset(irq_ptr,0,((char*)&irq_ptr->qdr)-((char*)irq_ptr));
3028
3029         /* wipes qib.ac, required by ar7063 */
3030         memset(irq_ptr->qdr,0,sizeof(struct qdr));
3031
3032         irq_ptr->int_parm=init_data->int_parm;
3033
3034         irq_ptr->schid = ccw_device_get_subchannel_id(init_data->cdev);
3035         irq_ptr->no_input_qs=init_data->no_input_qs;
3036         irq_ptr->no_output_qs=init_data->no_output_qs;
3037
3038         if (init_data->q_format==QDIO_IQDIO_QFMT) {
3039                 irq_ptr->is_iqdio_irq=1;
3040                 irq_ptr->is_thinint_irq=1;
3041         } else {
3042                 irq_ptr->is_iqdio_irq=0;
3043                 irq_ptr->is_thinint_irq=hydra_thinints;
3044         }
3045         sprintf(dbf_text,"is_i_t%1x%1x",
3046                 irq_ptr->is_iqdio_irq,irq_ptr->is_thinint_irq);
3047         QDIO_DBF_TEXT2(0,setup,dbf_text);
3048
3049         if (irq_ptr->is_thinint_irq) {
3050                 irq_ptr->dev_st_chg_ind = qdio_get_indicator();
3051                 QDIO_DBF_HEX1(0,setup,&irq_ptr->dev_st_chg_ind,sizeof(void*));
3052                 if (!irq_ptr->dev_st_chg_ind) {
3053                         QDIO_PRINT_WARN("no indicator location available " \
3054                                         "for irq 0.%x.%x\n",
3055                                         irq_ptr->schid.ssid, irq_ptr->schid.sch_no);
3056                         qdio_release_irq_memory(irq_ptr);
3057                         return -ENOBUFS;
3058                 }
3059         }
3060
3061         /* defaults */
3062         irq_ptr->equeue.cmd=DEFAULT_ESTABLISH_QS_CMD;
3063         irq_ptr->equeue.count=DEFAULT_ESTABLISH_QS_COUNT;
3064         irq_ptr->aqueue.cmd=DEFAULT_ACTIVATE_QS_CMD;
3065         irq_ptr->aqueue.count=DEFAULT_ACTIVATE_QS_COUNT;
3066
3067         qdio_fill_qs(irq_ptr, init_data->cdev,
3068                      init_data->no_input_qs,
3069                      init_data->no_output_qs,
3070                      init_data->input_handler,
3071                      init_data->output_handler,init_data->int_parm,
3072                      init_data->q_format,init_data->flags,
3073                      init_data->input_sbal_addr_array,
3074                      init_data->output_sbal_addr_array);
3075
3076         if (!try_module_get(THIS_MODULE)) {
3077                 QDIO_PRINT_CRIT("try_module_get() failed!\n");
3078                 qdio_release_irq_memory(irq_ptr);
3079                 return -EINVAL;
3080         }
3081
3082         qdio_fill_thresholds(irq_ptr,init_data->no_input_qs,
3083                              init_data->no_output_qs,
3084                              init_data->min_input_threshold,
3085                              init_data->max_input_threshold,
3086                              init_data->min_output_threshold,
3087                              init_data->max_output_threshold);
3088
3089         /* fill in qdr */
3090         irq_ptr->qdr->qfmt=init_data->q_format;
3091         irq_ptr->qdr->iqdcnt=init_data->no_input_qs;
3092         irq_ptr->qdr->oqdcnt=init_data->no_output_qs;
3093         irq_ptr->qdr->iqdsz=sizeof(struct qdesfmt0)/4; /* size in words */
3094         irq_ptr->qdr->oqdsz=sizeof(struct qdesfmt0)/4;
3095
3096         irq_ptr->qdr->qiba=(unsigned long)&irq_ptr->qib;
3097         irq_ptr->qdr->qkey=QDIO_STORAGE_KEY;
3098
3099         /* fill in qib */
3100         irq_ptr->is_qebsm = is_passthrough;
3101         if (irq_ptr->is_qebsm)
3102                 irq_ptr->qib.rflags |= QIB_RFLAGS_ENABLE_QEBSM;
3103
3104         irq_ptr->qib.qfmt=init_data->q_format;
3105         if (init_data->no_input_qs)
3106                 irq_ptr->qib.isliba=(unsigned long)(irq_ptr->input_qs[0]->slib);
3107         if (init_data->no_output_qs)
3108                 irq_ptr->qib.osliba=(unsigned long)(irq_ptr->output_qs[0]->slib);
3109         memcpy(irq_ptr->qib.ebcnam,init_data->adapter_name,8);
3110
3111         qdio_set_impl_params(irq_ptr,init_data->qib_param_field_format,
3112                              init_data->qib_param_field,
3113                              init_data->no_input_qs,
3114                              init_data->no_output_qs,
3115                              init_data->input_slib_elements,
3116                              init_data->output_slib_elements);
3117
3118         /* first input descriptors, then output descriptors */
3119         is_iqdio = (init_data->q_format == QDIO_IQDIO_QFMT) ? 1 : 0;
3120         for (i=0;i<init_data->no_input_qs;i++)
3121                 qdio_allocate_fill_input_desc(irq_ptr, i, is_iqdio);
3122
3123         for (i=0;i<init_data->no_output_qs;i++)
3124                 qdio_allocate_fill_output_desc(irq_ptr, i,
3125                                                init_data->no_input_qs,
3126                                                is_iqdio);
3127
3128         /* qdr, qib, sls, slsbs, slibs, sbales filled. */
3129
3130         /* get qdio commands */
3131         ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_EQUEUE);
3132         if (!ciw) {
3133                 QDIO_DBF_TEXT2(1,setup,"no eq");
3134                 QDIO_PRINT_INFO("No equeue CIW found for QDIO commands. "
3135                                 "Trying to use default.\n");
3136         } else
3137                 irq_ptr->equeue = *ciw;
3138         ciw = ccw_device_get_ciw(init_data->cdev, CIW_TYPE_AQUEUE);
3139         if (!ciw) {
3140                 QDIO_DBF_TEXT2(1,setup,"no aq");
3141                 QDIO_PRINT_INFO("No aqueue CIW found for QDIO commands. "
3142                                 "Trying to use default.\n");
3143         } else
3144                 irq_ptr->aqueue = *ciw;
3145
3146         /* Set new interrupt handler. */
3147         irq_ptr->original_int_handler = init_data->cdev->handler;
3148         init_data->cdev->handler = qdio_handler;
3149
3150         return 0;
3151 }
3152
3153 int
3154 qdio_establish(struct qdio_initialize *init_data)
3155 {
3156         struct qdio_irq *irq_ptr;
3157         unsigned long saveflags;
3158         int result, result2;
3159         struct ccw_device *cdev;
3160         char dbf_text[20];
3161
3162         cdev=init_data->cdev;
3163         irq_ptr = cdev->private->qdio_data;
3164         if (!irq_ptr)
3165                 return -EINVAL;
3166
3167         if (cdev->private->state != DEV_STATE_ONLINE)
3168                 return -EINVAL;
3169         
3170         down(&irq_ptr->setting_up_sema);
3171
3172         qdio_fill_irq(init_data);
3173
3174         /* the thinint CHSC stuff */
3175         if (irq_ptr->is_thinint_irq) {
3176
3177                 result = tiqdio_set_subchannel_ind(irq_ptr,0);
3178                 if (result) {
3179                         up(&irq_ptr->setting_up_sema);
3180                         qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3181                         return result;
3182                 }
3183                 tiqdio_set_delay_target(irq_ptr,TIQDIO_DELAY_TARGET);
3184         }
3185
3186         sprintf(dbf_text,"qest%4x",cdev->private->schid.sch_no);
3187         QDIO_DBF_TEXT0(0,setup,dbf_text);
3188         QDIO_DBF_TEXT0(0,trace,dbf_text);
3189
3190         /* establish q */
3191         irq_ptr->ccw.cmd_code=irq_ptr->equeue.cmd;
3192         irq_ptr->ccw.flags=CCW_FLAG_SLI;
3193         irq_ptr->ccw.count=irq_ptr->equeue.count;
3194         irq_ptr->ccw.cda=QDIO_GET_ADDR(irq_ptr->qdr);
3195
3196         spin_lock_irqsave(get_ccwdev_lock(cdev),saveflags);
3197
3198         ccw_device_set_options_mask(cdev, 0);
3199         result=ccw_device_start_timeout(cdev,&irq_ptr->ccw,
3200                                         QDIO_DOING_ESTABLISH,0, 0,
3201                                         QDIO_ESTABLISH_TIMEOUT);
3202         if (result) {
3203                 result2=ccw_device_start_timeout(cdev,&irq_ptr->ccw,
3204                                                  QDIO_DOING_ESTABLISH,0,0,
3205                                                  QDIO_ESTABLISH_TIMEOUT);
3206                 sprintf(dbf_text,"eq:io%4x",result);
3207                 QDIO_DBF_TEXT2(1,setup,dbf_text);
3208                 if (result2) {
3209                         sprintf(dbf_text,"eq:io%4x",result);
3210                         QDIO_DBF_TEXT2(1,setup,dbf_text);
3211                 }
3212                 QDIO_PRINT_WARN("establish queues on irq 0.%x.%04x: do_IO " \
3213                                 "returned %i, next try returned %i\n",
3214                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
3215                                 result, result2);
3216                 result=result2;
3217                 if (result)
3218                         ccw_device_set_timeout(cdev, 0);
3219         }
3220
3221         spin_unlock_irqrestore(get_ccwdev_lock(cdev),saveflags);
3222
3223         if (result) {
3224                 up(&irq_ptr->setting_up_sema);
3225                 qdio_shutdown(cdev,QDIO_FLAG_CLEANUP_USING_CLEAR);
3226                 return result;
3227         }
3228         
3229         /* Timeout is cared for already by using ccw_device_start_timeout(). */
3230         wait_event_interruptible(cdev->private->wait_q,
3231                  irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
3232                  irq_ptr->state == QDIO_IRQ_STATE_ERR);
3233
3234         if (irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED)
3235                 result = 0;
3236         else {
3237                 up(&irq_ptr->setting_up_sema);
3238                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3239                 return -EIO;
3240         }
3241
3242         qdio_get_ssqd_information(irq_ptr);
3243         /* if this gets set once, we're running under VM and can omit SVSes */
3244         if (irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_NECESSARY)
3245                 omit_svs=1;
3246
3247         sprintf(dbf_text,"qdioac%2x",irq_ptr->qdioac);
3248         QDIO_DBF_TEXT2(0,setup,dbf_text);
3249
3250         sprintf(dbf_text,"qib ac%2x",irq_ptr->qib.ac);
3251         QDIO_DBF_TEXT2(0,setup,dbf_text);
3252
3253         irq_ptr->hydra_gives_outbound_pcis=
3254                 irq_ptr->qib.ac&QIB_AC_OUTBOUND_PCI_SUPPORTED;
3255         irq_ptr->sync_done_on_outb_pcis=
3256                 irq_ptr->qdioac&CHSC_FLAG_SIGA_SYNC_DONE_ON_OUTB_PCIS;
3257
3258         qdio_initialize_set_siga_flags_input(irq_ptr);
3259         qdio_initialize_set_siga_flags_output(irq_ptr);
3260
3261         up(&irq_ptr->setting_up_sema);
3262
3263         return result;
3264         
3265 }
3266
3267 int
3268 qdio_activate(struct ccw_device *cdev, int flags)
3269 {
3270         struct qdio_irq *irq_ptr;
3271         int i,result=0,result2;
3272         unsigned long saveflags;
3273         char dbf_text[20]; /* see qdio_initialize */
3274
3275         irq_ptr = cdev->private->qdio_data;
3276         if (!irq_ptr)
3277                 return -ENODEV;
3278
3279         if (cdev->private->state != DEV_STATE_ONLINE)
3280                 return -EINVAL;
3281
3282         down(&irq_ptr->setting_up_sema);
3283         if (irq_ptr->state==QDIO_IRQ_STATE_INACTIVE) {
3284                 result=-EBUSY;
3285                 goto out;
3286         }
3287
3288         sprintf(dbf_text,"qact%4x", irq_ptr->schid.sch_no);
3289         QDIO_DBF_TEXT2(0,setup,dbf_text);
3290         QDIO_DBF_TEXT2(0,trace,dbf_text);
3291
3292         /* activate q */
3293         irq_ptr->ccw.cmd_code=irq_ptr->aqueue.cmd;
3294         irq_ptr->ccw.flags=CCW_FLAG_SLI;
3295         irq_ptr->ccw.count=irq_ptr->aqueue.count;
3296         irq_ptr->ccw.cda=QDIO_GET_ADDR(0);
3297
3298         spin_lock_irqsave(get_ccwdev_lock(cdev),saveflags);
3299
3300         ccw_device_set_timeout(cdev, 0);
3301         ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
3302         result=ccw_device_start(cdev,&irq_ptr->ccw,QDIO_DOING_ACTIVATE,
3303                                 0, DOIO_DENY_PREFETCH);
3304         if (result) {
3305                 result2=ccw_device_start(cdev,&irq_ptr->ccw,
3306                                          QDIO_DOING_ACTIVATE,0,0);
3307                 sprintf(dbf_text,"aq:io%4x",result);
3308                 QDIO_DBF_TEXT2(1,setup,dbf_text);
3309                 if (result2) {
3310                         sprintf(dbf_text,"aq:io%4x",result);
3311                         QDIO_DBF_TEXT2(1,setup,dbf_text);
3312                 }
3313                 QDIO_PRINT_WARN("activate queues on irq 0.%x.%04x: do_IO " \
3314                                 "returned %i, next try returned %i\n",
3315                                 irq_ptr->schid.ssid, irq_ptr->schid.sch_no,
3316                                 result, result2);
3317                 result=result2;
3318         }
3319
3320         spin_unlock_irqrestore(get_ccwdev_lock(cdev),saveflags);
3321         if (result)
3322                 goto out;
3323
3324         for (i=0;i<irq_ptr->no_input_qs;i++) {
3325                 if (irq_ptr->is_thinint_irq) {
3326                         /* 
3327                          * that way we know, that, if we will get interrupted
3328                          * by tiqdio_inbound_processing, qdio_unmark_q will
3329                          * not be called 
3330                          */
3331                         qdio_reserve_q(irq_ptr->input_qs[i]);
3332                         qdio_mark_tiq(irq_ptr->input_qs[i]);
3333                         qdio_release_q(irq_ptr->input_qs[i]);
3334                 }
3335         }
3336
3337         if (flags&QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT) {
3338                 for (i=0;i<irq_ptr->no_input_qs;i++) {
3339                         irq_ptr->input_qs[i]->is_input_q|=
3340                                 QDIO_FLAG_NO_INPUT_INTERRUPT_CONTEXT;
3341                 }
3342         }
3343
3344         wait_event_interruptible_timeout(cdev->private->wait_q,
3345                                          ((irq_ptr->state ==
3346                                           QDIO_IRQ_STATE_STOPPED) ||
3347                                           (irq_ptr->state ==
3348                                            QDIO_IRQ_STATE_ERR)),
3349                                          QDIO_ACTIVATE_TIMEOUT);
3350
3351         switch (irq_ptr->state) {
3352         case QDIO_IRQ_STATE_STOPPED:
3353         case QDIO_IRQ_STATE_ERR:
3354                 up(&irq_ptr->setting_up_sema);
3355                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
3356                 down(&irq_ptr->setting_up_sema);
3357                 result = -EIO;
3358                 break;
3359         default:
3360                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
3361                 result = 0;
3362         }
3363  out:
3364         up(&irq_ptr->setting_up_sema);
3365
3366         return result;
3367 }
3368
3369 /* buffers filled forwards again to make Rick happy */
3370 static void
3371 qdio_do_qdio_fill_input(struct qdio_q *q, unsigned int qidx,
3372                         unsigned int count, struct qdio_buffer *buffers)
3373 {
3374         struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3375         int tmp = 0;
3376
3377         qidx &= (QDIO_MAX_BUFFERS_PER_Q - 1);
3378         if (irq->is_qebsm) {
3379                 while (count) {
3380                         tmp = set_slsb(q, &qidx, SLSB_CU_INPUT_EMPTY, &count);
3381                         if (!tmp)
3382                                 return;
3383                 }
3384                 return;
3385         }
3386         for (;;) {
3387                 set_slsb(q, &qidx, SLSB_CU_INPUT_EMPTY, &count);
3388                 count--;
3389                 if (!count) break;
3390                 qidx = (qidx + 1) & (QDIO_MAX_BUFFERS_PER_Q - 1);
3391         }
3392 }
3393
3394 static void
3395 qdio_do_qdio_fill_output(struct qdio_q *q, unsigned int qidx,
3396                          unsigned int count, struct qdio_buffer *buffers)
3397 {
3398         struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3399         int tmp = 0;
3400
3401         qidx &= (QDIO_MAX_BUFFERS_PER_Q - 1);
3402         if (irq->is_qebsm) {
3403                 while (count) {
3404                         tmp = set_slsb(q, &qidx, SLSB_CU_OUTPUT_PRIMED, &count);
3405                         if (!tmp)
3406                                 return;
3407                 }
3408                 return;
3409         }
3410
3411         for (;;) {
3412                 set_slsb(q, &qidx, SLSB_CU_OUTPUT_PRIMED, &count);
3413                 count--;
3414                 if (!count) break;
3415                 qidx = (qidx + 1) & (QDIO_MAX_BUFFERS_PER_Q - 1);
3416         }
3417 }
3418
3419 static void
3420 do_qdio_handle_inbound(struct qdio_q *q, unsigned int callflags,
3421                        unsigned int qidx, unsigned int count,
3422                        struct qdio_buffer *buffers)
3423 {
3424         int used_elements;
3425
3426         /* This is the inbound handling of queues */
3427         used_elements=atomic_add_return(count, &q->number_of_buffers_used) - count;
3428         
3429         qdio_do_qdio_fill_input(q,qidx,count,buffers);
3430         
3431         if ((used_elements+count==QDIO_MAX_BUFFERS_PER_Q)&&
3432             (callflags&QDIO_FLAG_UNDER_INTERRUPT))
3433                 atomic_xchg(&q->polling,0);
3434         
3435         if (used_elements) 
3436                 return;
3437         if (callflags&QDIO_FLAG_DONT_SIGA)
3438                 return;
3439         if (q->siga_in) {
3440                 int result;
3441                 
3442                 result=qdio_siga_input(q);
3443                 if (result) {
3444                         if (q->siga_error)
3445                                 q->error_status_flags|=
3446                                         QDIO_STATUS_MORE_THAN_ONE_SIGA_ERROR;
3447                         q->error_status_flags|=QDIO_STATUS_LOOK_FOR_ERROR;
3448                         q->siga_error=result;
3449                 }
3450         }
3451                 
3452         qdio_mark_q(q);
3453 }
3454
3455 static void
3456 do_qdio_handle_outbound(struct qdio_q *q, unsigned int callflags,
3457                         unsigned int qidx, unsigned int count,
3458                         struct qdio_buffer *buffers)
3459 {
3460         int used_elements;
3461         unsigned int cnt, start_buf;
3462         unsigned char state = 0;
3463         struct qdio_irq *irq = (struct qdio_irq *) q->irq_ptr;
3464
3465         /* This is the outbound handling of queues */
3466         qdio_do_qdio_fill_output(q,qidx,count,buffers);
3467
3468         used_elements=atomic_add_return(count, &q->number_of_buffers_used) - count;
3469
3470         if (callflags&QDIO_FLAG_DONT_SIGA) {
3471                 qdio_perf_stat_inc(&perf_stats.outbound_cnt);
3472                 return;
3473         }
3474         if (callflags & QDIO_FLAG_PCI_OUT)
3475                 q->is_pci_out = 1;
3476         else
3477                 q->is_pci_out = 0;
3478         if (q->is_iqdio_q) {
3479                 /* one siga for every sbal */
3480                 while (count--)
3481                         qdio_kick_outbound_q(q);
3482                         
3483                 __qdio_outbound_processing(q);
3484         } else {
3485                 /* under VM, we do a SIGA sync unconditionally */
3486                 SYNC_MEMORY;
3487                 else {
3488                         /* 
3489                          * w/o shadow queues (else branch of
3490                          * SYNC_MEMORY :-/ ), we try to
3491                          * fast-requeue buffers 
3492                          */
3493                         if (irq->is_qebsm) {
3494                                 cnt = 1;
3495                                 start_buf = ((qidx+QDIO_MAX_BUFFERS_PER_Q-1) &
3496                                              (QDIO_MAX_BUFFERS_PER_Q-1));
3497                                 qdio_do_eqbs(q, &state, &start_buf, &cnt);
3498                         } else
3499                                 state = q->slsb.acc.val[(qidx+QDIO_MAX_BUFFERS_PER_Q-1)
3500                                         &(QDIO_MAX_BUFFERS_PER_Q-1) ];
3501                          if (state != SLSB_CU_OUTPUT_PRIMED) {
3502                                 qdio_kick_outbound_q(q);
3503                         } else {
3504                                 QDIO_DBF_TEXT3(0,trace, "fast-req");
3505                                 qdio_perf_stat_inc(&perf_stats.fast_reqs);
3506                         }
3507                 }
3508                 /* 
3509                  * only marking the q could take too long,
3510                  * the upper layer module could do a lot of
3511                  * traffic in that time 
3512                  */
3513                 __qdio_outbound_processing(q);
3514         }
3515
3516         qdio_perf_stat_inc(&perf_stats.outbound_cnt);
3517 }
3518
3519 /* count must be 1 in iqdio */
3520 int
3521 do_QDIO(struct ccw_device *cdev,unsigned int callflags,
3522         unsigned int queue_number, unsigned int qidx,
3523         unsigned int count,struct qdio_buffer *buffers)
3524 {
3525         struct qdio_irq *irq_ptr;
3526 #ifdef CONFIG_QDIO_DEBUG
3527         char dbf_text[20];
3528
3529         sprintf(dbf_text,"doQD%04x",cdev->private->schid.sch_no);
3530         QDIO_DBF_TEXT3(0,trace,dbf_text);
3531 #endif /* CONFIG_QDIO_DEBUG */
3532
3533         if ( (qidx>QDIO_MAX_BUFFERS_PER_Q) ||
3534              (count>QDIO_MAX_BUFFERS_PER_Q) ||
3535              (queue_number>QDIO_MAX_QUEUES_PER_IRQ) )
3536                 return -EINVAL;
3537
3538         if (count==0)
3539                 return 0;
3540
3541         irq_ptr = cdev->private->qdio_data;
3542         if (!irq_ptr)
3543                 return -ENODEV;
3544
3545 #ifdef CONFIG_QDIO_DEBUG
3546         if (callflags&QDIO_FLAG_SYNC_INPUT)
3547                 QDIO_DBF_HEX3(0,trace,&irq_ptr->input_qs[queue_number],
3548                               sizeof(void*));
3549         else
3550                 QDIO_DBF_HEX3(0,trace,&irq_ptr->output_qs[queue_number],
3551                               sizeof(void*));
3552         sprintf(dbf_text,"flag%04x",callflags);
3553         QDIO_DBF_TEXT3(0,trace,dbf_text);
3554         sprintf(dbf_text,"qi%02xct%02x",qidx,count);
3555         QDIO_DBF_TEXT3(0,trace,dbf_text);
3556 #endif /* CONFIG_QDIO_DEBUG */
3557
3558         if (irq_ptr->state!=QDIO_IRQ_STATE_ACTIVE)
3559                 return -EBUSY;
3560
3561         if (callflags&QDIO_FLAG_SYNC_INPUT)
3562                 do_qdio_handle_inbound(irq_ptr->input_qs[queue_number],
3563                                        callflags, qidx, count, buffers);
3564         else if (callflags&QDIO_FLAG_SYNC_OUTPUT)
3565                 do_qdio_handle_outbound(irq_ptr->output_qs[queue_number],
3566                                         callflags, qidx, count, buffers);
3567         else {
3568                 QDIO_DBF_TEXT3(1,trace,"doQD:inv");
3569                 return -EINVAL;
3570         }
3571         return 0;
3572 }
3573
3574 static int
3575 qdio_perf_procfile_read(char *buffer, char **buffer_location, off_t offset,
3576                         int buffer_length, int *eof, void *data)
3577 {
3578         int c=0;
3579
3580         /* we are always called with buffer_length=4k, so we all
3581            deliver on the first read */
3582         if (offset>0)
3583                 return 0;
3584
3585 #define _OUTP_IT(x...) c+=sprintf(buffer+c,x)
3586 #ifdef CONFIG_64BIT
3587         _OUTP_IT("Number of tasklet runs (total)                  : %li\n",
3588                  (long)atomic64_read(&perf_stats.tl_runs));
3589         _OUTP_IT("Inbound tasklet runs      tried/retried         : %li/%li\n",
3590                  (long)atomic64_read(&perf_stats.inbound_tl_runs),
3591                  (long)atomic64_read(&perf_stats.inbound_tl_runs_resched));
3592         _OUTP_IT("Inbound-thin tasklet runs tried/retried         : %li/%li\n",
3593                  (long)atomic64_read(&perf_stats.inbound_thin_tl_runs),
3594                  (long)atomic64_read(&perf_stats.inbound_thin_tl_runs_resched));
3595         _OUTP_IT("Outbound tasklet runs     tried/retried         : %li/%li\n",
3596                  (long)atomic64_read(&perf_stats.outbound_tl_runs),
3597                  (long)atomic64_read(&perf_stats.outbound_tl_runs_resched));
3598         _OUTP_IT("\n");
3599         _OUTP_IT("Number of SIGA sync's issued                    : %li\n",
3600                  (long)atomic64_read(&perf_stats.siga_syncs));
3601         _OUTP_IT("Number of SIGA in's issued                      : %li\n",
3602                  (long)atomic64_read(&perf_stats.siga_ins));
3603         _OUTP_IT("Number of SIGA out's issued                     : %li\n",
3604                  (long)atomic64_read(&perf_stats.siga_outs));
3605         _OUTP_IT("Number of PCIs caught                           : %li\n",
3606                  (long)atomic64_read(&perf_stats.pcis));
3607         _OUTP_IT("Number of adapter interrupts caught             : %li\n",
3608                  (long)atomic64_read(&perf_stats.thinints));
3609         _OUTP_IT("Number of fast requeues (outg. SBALs w/o SIGA)  : %li\n",
3610                  (long)atomic64_read(&perf_stats.fast_reqs));
3611         _OUTP_IT("\n");
3612         _OUTP_IT("Number of inbound transfers                     : %li\n",
3613                  (long)atomic64_read(&perf_stats.inbound_cnt));
3614         _OUTP_IT("Number of do_QDIOs outbound                     : %li\n",
3615                  (long)atomic64_read(&perf_stats.outbound_cnt));
3616 #else /* CONFIG_64BIT */
3617         _OUTP_IT("Number of tasklet runs (total)                  : %i\n",
3618                  atomic_read(&perf_stats.tl_runs));
3619         _OUTP_IT("Inbound tasklet runs      tried/retried         : %i/%i\n",
3620                  atomic_read(&perf_stats.inbound_tl_runs),
3621                  atomic_read(&perf_stats.inbound_tl_runs_resched));
3622         _OUTP_IT("Inbound-thin tasklet runs tried/retried         : %i/%i\n",
3623                  atomic_read(&perf_stats.inbound_thin_tl_runs),
3624                  atomic_read(&perf_stats.inbound_thin_tl_runs_resched));
3625         _OUTP_IT("Outbound tasklet runs     tried/retried         : %i/%i\n",
3626                  atomic_read(&perf_stats.outbound_tl_runs),
3627                  atomic_read(&perf_stats.outbound_tl_runs_resched));
3628         _OUTP_IT("\n");
3629         _OUTP_IT("Number of SIGA sync's issued                    : %i\n",
3630                  atomic_read(&perf_stats.siga_syncs));
3631         _OUTP_IT("Number of SIGA in's issued                      : %i\n",
3632                  atomic_read(&perf_stats.siga_ins));
3633         _OUTP_IT("Number of SIGA out's issued                     : %i\n",
3634                  atomic_read(&perf_stats.siga_outs));
3635         _OUTP_IT("Number of PCIs caught                           : %i\n",
3636                  atomic_read(&perf_stats.pcis));
3637         _OUTP_IT("Number of adapter interrupts caught             : %i\n",
3638                  atomic_read(&perf_stats.thinints));
3639         _OUTP_IT("Number of fast requeues (outg. SBALs w/o SIGA)  : %i\n",
3640                  atomic_read(&perf_stats.fast_reqs));
3641         _OUTP_IT("\n");
3642         _OUTP_IT("Number of inbound transfers                     : %i\n",
3643                  atomic_read(&perf_stats.inbound_cnt));
3644         _OUTP_IT("Number of do_QDIOs outbound                     : %i\n",
3645                  atomic_read(&perf_stats.outbound_cnt));
3646 #endif /* CONFIG_64BIT */
3647         _OUTP_IT("\n");
3648
3649         return c;
3650 }
3651
3652 static struct proc_dir_entry *qdio_perf_proc_file;
3653
3654 static void
3655 qdio_add_procfs_entry(void)
3656 {
3657         proc_perf_file_registration=0;
3658         qdio_perf_proc_file=create_proc_entry(QDIO_PERF,
3659                                               S_IFREG|0444,&proc_root);
3660         if (qdio_perf_proc_file) {
3661                 qdio_perf_proc_file->read_proc=&qdio_perf_procfile_read;
3662         } else proc_perf_file_registration=-1;
3663
3664         if (proc_perf_file_registration)
3665                 QDIO_PRINT_WARN("was not able to register perf. " \
3666                                 "proc-file (%i).\n",
3667                                 proc_perf_file_registration);
3668 }
3669
3670 static void
3671 qdio_remove_procfs_entry(void)
3672 {
3673         if (!proc_perf_file_registration) /* means if it went ok earlier */
3674                 remove_proc_entry(QDIO_PERF,&proc_root);
3675 }
3676
3677 /**
3678  * attributes in sysfs
3679  *****************************************************************************/
3680
3681 static ssize_t
3682 qdio_performance_stats_show(struct bus_type *bus, char *buf)
3683 {
3684         return sprintf(buf, "%i\n", qdio_performance_stats ? 1 : 0);
3685 }
3686
3687 static ssize_t
3688 qdio_performance_stats_store(struct bus_type *bus, const char *buf, size_t count)
3689 {
3690         char *tmp;
3691         int i;
3692
3693         i = simple_strtoul(buf, &tmp, 16);
3694         if ((i == 0) || (i == 1)) {
3695                 if (i == qdio_performance_stats)
3696                         return count;
3697                 qdio_performance_stats = i;
3698                 if (i==0) {
3699                         /* reset perf. stat. info */
3700 #ifdef CONFIG_64BIT
3701                         atomic64_set(&perf_stats.tl_runs, 0);
3702                         atomic64_set(&perf_stats.outbound_tl_runs, 0);
3703                         atomic64_set(&perf_stats.inbound_tl_runs, 0);
3704                         atomic64_set(&perf_stats.inbound_tl_runs_resched, 0);
3705                         atomic64_set(&perf_stats.inbound_thin_tl_runs, 0);
3706                         atomic64_set(&perf_stats.inbound_thin_tl_runs_resched,
3707                                      0);
3708                         atomic64_set(&perf_stats.siga_outs, 0);
3709                         atomic64_set(&perf_stats.siga_ins, 0);
3710                         atomic64_set(&perf_stats.siga_syncs, 0);
3711                         atomic64_set(&perf_stats.pcis, 0);
3712                         atomic64_set(&perf_stats.thinints, 0);
3713                         atomic64_set(&perf_stats.fast_reqs, 0);
3714                         atomic64_set(&perf_stats.outbound_cnt, 0);
3715                         atomic64_set(&perf_stats.inbound_cnt, 0);
3716 #else /* CONFIG_64BIT */
3717                         atomic_set(&perf_stats.tl_runs, 0);
3718                         atomic_set(&perf_stats.outbound_tl_runs, 0);
3719                         atomic_set(&perf_stats.inbound_tl_runs, 0);
3720                         atomic_set(&perf_stats.inbound_tl_runs_resched, 0);
3721                         atomic_set(&perf_stats.inbound_thin_tl_runs, 0);
3722                         atomic_set(&perf_stats.inbound_thin_tl_runs_resched, 0);
3723                         atomic_set(&perf_stats.siga_outs, 0);
3724                         atomic_set(&perf_stats.siga_ins, 0);
3725                         atomic_set(&perf_stats.siga_syncs, 0);
3726                         atomic_set(&perf_stats.pcis, 0);
3727                         atomic_set(&perf_stats.thinints, 0);
3728                         atomic_set(&perf_stats.fast_reqs, 0);
3729                         atomic_set(&perf_stats.outbound_cnt, 0);
3730                         atomic_set(&perf_stats.inbound_cnt, 0);
3731 #endif /* CONFIG_64BIT */
3732                 }
3733         } else {
3734                 QDIO_PRINT_WARN("QDIO performance_stats: write 0 or 1 to this file!\n");
3735                 return -EINVAL;
3736         }
3737         return count;
3738 }
3739
3740 static BUS_ATTR(qdio_performance_stats, 0644, qdio_performance_stats_show,
3741                         qdio_performance_stats_store);
3742
3743 static void
3744 tiqdio_register_thinints(void)
3745 {
3746         char dbf_text[20];
3747         register_thinint_result=
3748                 s390_register_adapter_interrupt(&tiqdio_thinint_handler);
3749         if (register_thinint_result) {
3750                 sprintf(dbf_text,"regthn%x",(register_thinint_result&0xff));
3751                 QDIO_DBF_TEXT0(0,setup,dbf_text);
3752                 QDIO_PRINT_ERR("failed to register adapter handler " \
3753                                "(rc=%i).\nAdapter interrupts might " \
3754                                "not work. Continuing.\n",
3755                                register_thinint_result);
3756         }
3757 }
3758
3759 static void
3760 tiqdio_unregister_thinints(void)
3761 {
3762         if (!register_thinint_result)
3763                 s390_unregister_adapter_interrupt(&tiqdio_thinint_handler);
3764 }
3765
3766 static int
3767 qdio_get_qdio_memory(void)
3768 {
3769         int i;
3770         indicator_used[0]=1;
3771
3772         for (i=1;i<INDICATORS_PER_CACHELINE;i++)
3773                 indicator_used[i]=0;
3774         indicators = kzalloc(sizeof(__u32)*(INDICATORS_PER_CACHELINE),
3775                                    GFP_KERNEL);
3776         if (!indicators)
3777                 return -ENOMEM;
3778         return 0;
3779 }
3780
3781 static void
3782 qdio_release_qdio_memory(void)
3783 {
3784         kfree(indicators);
3785 }
3786
3787
3788 static void
3789 qdio_unregister_dbf_views(void)
3790 {
3791         if (qdio_dbf_setup)
3792                 debug_unregister(qdio_dbf_setup);
3793         if (qdio_dbf_sbal)
3794                 debug_unregister(qdio_dbf_sbal);
3795         if (qdio_dbf_sense)
3796                 debug_unregister(qdio_dbf_sense);
3797         if (qdio_dbf_trace)
3798                 debug_unregister(qdio_dbf_trace);
3799 #ifdef CONFIG_QDIO_DEBUG
3800         if (qdio_dbf_slsb_out)
3801                 debug_unregister(qdio_dbf_slsb_out);
3802         if (qdio_dbf_slsb_in)
3803                 debug_unregister(qdio_dbf_slsb_in);
3804 #endif /* CONFIG_QDIO_DEBUG */
3805 }
3806
3807 static int
3808 qdio_register_dbf_views(void)
3809 {
3810         qdio_dbf_setup=debug_register(QDIO_DBF_SETUP_NAME,
3811                                       QDIO_DBF_SETUP_PAGES,
3812                                       QDIO_DBF_SETUP_NR_AREAS,
3813                                       QDIO_DBF_SETUP_LEN);
3814         if (!qdio_dbf_setup)
3815                 goto oom;
3816         debug_register_view(qdio_dbf_setup,&debug_hex_ascii_view);
3817         debug_set_level(qdio_dbf_setup,QDIO_DBF_SETUP_LEVEL);
3818
3819         qdio_dbf_sbal=debug_register(QDIO_DBF_SBAL_NAME,
3820                                      QDIO_DBF_SBAL_PAGES,
3821                                      QDIO_DBF_SBAL_NR_AREAS,
3822                                      QDIO_DBF_SBAL_LEN);
3823         if (!qdio_dbf_sbal)
3824                 goto oom;
3825
3826         debug_register_view(qdio_dbf_sbal,&debug_hex_ascii_view);
3827         debug_set_level(qdio_dbf_sbal,QDIO_DBF_SBAL_LEVEL);
3828
3829         qdio_dbf_sense=debug_register(QDIO_DBF_SENSE_NAME,
3830                                       QDIO_DBF_SENSE_PAGES,
3831                                       QDIO_DBF_SENSE_NR_AREAS,
3832                                       QDIO_DBF_SENSE_LEN);
3833         if (!qdio_dbf_sense)
3834                 goto oom;
3835
3836         debug_register_view(qdio_dbf_sense,&debug_hex_ascii_view);
3837         debug_set_level(qdio_dbf_sense,QDIO_DBF_SENSE_LEVEL);
3838
3839         qdio_dbf_trace=debug_register(QDIO_DBF_TRACE_NAME,
3840                                       QDIO_DBF_TRACE_PAGES,
3841                                       QDIO_DBF_TRACE_NR_AREAS,
3842                                       QDIO_DBF_TRACE_LEN);
3843         if (!qdio_dbf_trace)
3844                 goto oom;
3845
3846         debug_register_view(qdio_dbf_trace,&debug_hex_ascii_view);
3847         debug_set_level(qdio_dbf_trace,QDIO_DBF_TRACE_LEVEL);
3848
3849 #ifdef CONFIG_QDIO_DEBUG
3850         qdio_dbf_slsb_out=debug_register(QDIO_DBF_SLSB_OUT_NAME,
3851                                          QDIO_DBF_SLSB_OUT_PAGES,
3852                                          QDIO_DBF_SLSB_OUT_NR_AREAS,
3853                                          QDIO_DBF_SLSB_OUT_LEN);
3854         if (!qdio_dbf_slsb_out)
3855                 goto oom;
3856         debug_register_view(qdio_dbf_slsb_out,&debug_hex_ascii_view);
3857         debug_set_level(qdio_dbf_slsb_out,QDIO_DBF_SLSB_OUT_LEVEL);
3858
3859         qdio_dbf_slsb_in=debug_register(QDIO_DBF_SLSB_IN_NAME,
3860                                         QDIO_DBF_SLSB_IN_PAGES,
3861                                         QDIO_DBF_SLSB_IN_NR_AREAS,
3862                                         QDIO_DBF_SLSB_IN_LEN);
3863         if (!qdio_dbf_slsb_in)
3864                 goto oom;
3865         debug_register_view(qdio_dbf_slsb_in,&debug_hex_ascii_view);
3866         debug_set_level(qdio_dbf_slsb_in,QDIO_DBF_SLSB_IN_LEVEL);
3867 #endif /* CONFIG_QDIO_DEBUG */
3868         return 0;
3869 oom:
3870         QDIO_PRINT_ERR("not enough memory for dbf.\n");
3871         qdio_unregister_dbf_views();
3872         return -ENOMEM;
3873 }
3874
3875 static void *qdio_mempool_alloc(gfp_t gfp_mask, void *size)
3876 {
3877         return (void *) get_zeroed_page(gfp_mask|GFP_DMA);
3878 }
3879
3880 static void qdio_mempool_free(void *element, void *size)
3881 {
3882         free_page((unsigned long) element);
3883 }
3884
3885 static int __init
3886 init_QDIO(void)
3887 {
3888         int res;
3889         void *ptr;
3890
3891         printk("qdio: loading %s\n",version);
3892
3893         res=qdio_get_qdio_memory();
3894         if (res)
3895                 return res;
3896
3897         res = qdio_register_dbf_views();
3898         if (res)
3899                 return res;
3900
3901         QDIO_DBF_TEXT0(0,setup,"initQDIO");
3902         res = bus_create_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
3903
3904         memset((void*)&perf_stats,0,sizeof(perf_stats));
3905         QDIO_DBF_TEXT0(0,setup,"perfstat");
3906         ptr=&perf_stats;
3907         QDIO_DBF_HEX0(0,setup,&ptr,sizeof(void*));
3908
3909         qdio_add_procfs_entry();
3910
3911         qdio_mempool_scssc = mempool_create(QDIO_MEMPOOL_SCSSC_ELEMENTS,
3912                                             qdio_mempool_alloc,
3913                                             qdio_mempool_free, NULL);
3914
3915         if (tiqdio_check_chsc_availability())
3916                 QDIO_PRINT_ERR("Not all CHSCs supported. Continuing.\n");
3917
3918         tiqdio_register_thinints();
3919
3920         return 0;
3921  }
3922
3923 static void __exit
3924 cleanup_QDIO(void)
3925 {
3926         tiqdio_unregister_thinints();
3927         qdio_remove_procfs_entry();
3928         qdio_release_qdio_memory();
3929         qdio_unregister_dbf_views();
3930         mempool_destroy(qdio_mempool_scssc);
3931         bus_remove_file(&ccw_bus_type, &bus_attr_qdio_performance_stats);
3932         printk("qdio: %s: module removed\n",version);
3933 }
3934
3935 module_init(init_QDIO);
3936 module_exit(cleanup_QDIO);
3937
3938 EXPORT_SYMBOL(qdio_allocate);
3939 EXPORT_SYMBOL(qdio_establish);
3940 EXPORT_SYMBOL(qdio_initialize);
3941 EXPORT_SYMBOL(qdio_activate);
3942 EXPORT_SYMBOL(do_QDIO);
3943 EXPORT_SYMBOL(qdio_shutdown);
3944 EXPORT_SYMBOL(qdio_free);
3945 EXPORT_SYMBOL(qdio_cleanup);
3946 EXPORT_SYMBOL(qdio_synchronize);