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