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