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