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