[S390] qdio: rework debug feature logging
[linux-2.6] / drivers / s390 / cio / qdio_main.c
1 /*
2  * linux/drivers/s390/cio/qdio_main.c
3  *
4  * Linux for s390 qdio support, buffer handling, qdio API and module support.
5  *
6  * Copyright 2000,2008 IBM Corp.
7  * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
8  *            Jan Glauber <jang@linux.vnet.ibm.com>
9  * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
10  */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/delay.h>
16 #include <asm/atomic.h>
17 #include <asm/debug.h>
18 #include <asm/qdio.h>
19
20 #include "cio.h"
21 #include "css.h"
22 #include "device.h"
23 #include "qdio.h"
24 #include "qdio_debug.h"
25 #include "qdio_perf.h"
26
27 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
28         "Jan Glauber <jang@linux.vnet.ibm.com>");
29 MODULE_DESCRIPTION("QDIO base support");
30 MODULE_LICENSE("GPL");
31
32 static inline int do_siga_sync(struct subchannel_id schid,
33                                unsigned int out_mask, unsigned int in_mask)
34 {
35         register unsigned long __fc asm ("0") = 2;
36         register struct subchannel_id __schid asm ("1") = schid;
37         register unsigned long out asm ("2") = out_mask;
38         register unsigned long in asm ("3") = in_mask;
39         int cc;
40
41         asm volatile(
42                 "       siga    0\n"
43                 "       ipm     %0\n"
44                 "       srl     %0,28\n"
45                 : "=d" (cc)
46                 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
47         return cc;
48 }
49
50 static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
51 {
52         register unsigned long __fc asm ("0") = 1;
53         register struct subchannel_id __schid asm ("1") = schid;
54         register unsigned long __mask asm ("2") = mask;
55         int cc;
56
57         asm volatile(
58                 "       siga    0\n"
59                 "       ipm     %0\n"
60                 "       srl     %0,28\n"
61                 : "=d" (cc)
62                 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
63         return cc;
64 }
65
66 /**
67  * do_siga_output - perform SIGA-w/wt function
68  * @schid: subchannel id or in case of QEBSM the subchannel token
69  * @mask: which output queues to process
70  * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
71  * @fc: function code to perform
72  *
73  * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
74  * Note: For IQDC unicast queues only the highest priority queue is processed.
75  */
76 static inline int do_siga_output(unsigned long schid, unsigned long mask,
77                                  u32 *bb, unsigned int fc)
78 {
79         register unsigned long __fc asm("0") = fc;
80         register unsigned long __schid asm("1") = schid;
81         register unsigned long __mask asm("2") = mask;
82         int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
83
84         asm volatile(
85                 "       siga    0\n"
86                 "0:     ipm     %0\n"
87                 "       srl     %0,28\n"
88                 "1:\n"
89                 EX_TABLE(0b, 1b)
90                 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
91                 : : "cc", "memory");
92         *bb = ((unsigned int) __fc) >> 31;
93         return cc;
94 }
95
96 static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
97 {
98         /* all done or next buffer state different */
99         if (ccq == 0 || ccq == 32)
100                 return 0;
101         /* not all buffers processed */
102         if (ccq == 96 || ccq == 97)
103                 return 1;
104         /* notify devices immediately */
105         DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
106         return -EIO;
107 }
108
109 /**
110  * qdio_do_eqbs - extract buffer states for QEBSM
111  * @q: queue to manipulate
112  * @state: state of the extracted buffers
113  * @start: buffer number to start at
114  * @count: count of buffers to examine
115  *
116  * Returns the number of successfull extracted equal buffer states.
117  * Stops processing if a state is different from the last buffers state.
118  */
119 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
120                         int start, int count)
121 {
122         unsigned int ccq = 0;
123         int tmp_count = count, tmp_start = start;
124         int nr = q->nr;
125         int rc;
126
127         BUG_ON(!q->irq_ptr->sch_token);
128         qdio_perf_stat_inc(&perf_stats.debug_eqbs_all);
129
130         if (!q->is_input_q)
131                 nr += q->irq_ptr->nr_input_qs;
132 again:
133         ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
134         rc = qdio_check_ccq(q, ccq);
135
136         /* At least one buffer was processed, return and extract the remaining
137          * buffers later.
138          */
139         if ((ccq == 96) && (count != tmp_count)) {
140                 qdio_perf_stat_inc(&perf_stats.debug_eqbs_incomplete);
141                 return (count - tmp_count);
142         }
143
144         if (rc == 1) {
145                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
146                 goto again;
147         }
148
149         if (rc < 0) {
150                 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
151                 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
152                 q->handler(q->irq_ptr->cdev,
153                            QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
154                            0, -1, -1, q->irq_ptr->int_parm);
155                 return 0;
156         }
157         return count - tmp_count;
158 }
159
160 /**
161  * qdio_do_sqbs - set buffer states for QEBSM
162  * @q: queue to manipulate
163  * @state: new state of the buffers
164  * @start: first buffer number to change
165  * @count: how many buffers to change
166  *
167  * Returns the number of successfully changed buffers.
168  * Does retrying until the specified count of buffer states is set or an
169  * error occurs.
170  */
171 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
172                         int count)
173 {
174         unsigned int ccq = 0;
175         int tmp_count = count, tmp_start = start;
176         int nr = q->nr;
177         int rc;
178
179         BUG_ON(!q->irq_ptr->sch_token);
180         qdio_perf_stat_inc(&perf_stats.debug_sqbs_all);
181
182         if (!q->is_input_q)
183                 nr += q->irq_ptr->nr_input_qs;
184 again:
185         ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
186         rc = qdio_check_ccq(q, ccq);
187         if (rc == 1) {
188                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
189                 qdio_perf_stat_inc(&perf_stats.debug_sqbs_incomplete);
190                 goto again;
191         }
192         if (rc < 0) {
193                 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
194                 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
195                 q->handler(q->irq_ptr->cdev,
196                            QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
197                            0, -1, -1, q->irq_ptr->int_parm);
198                 return 0;
199         }
200         WARN_ON(tmp_count);
201         return count - tmp_count;
202 }
203
204 /* returns number of examined buffers and their common state in *state */
205 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
206                                  unsigned char *state, unsigned int count)
207 {
208         unsigned char __state = 0;
209         int i;
210
211         BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
212         BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
213
214         if (is_qebsm(q))
215                 return qdio_do_eqbs(q, state, bufnr, count);
216
217         for (i = 0; i < count; i++) {
218                 if (!__state)
219                         __state = q->slsb.val[bufnr];
220                 else if (q->slsb.val[bufnr] != __state)
221                         break;
222                 bufnr = next_buf(bufnr);
223         }
224         *state = __state;
225         return i;
226 }
227
228 inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
229                   unsigned char *state)
230 {
231         return get_buf_states(q, bufnr, state, 1);
232 }
233
234 /* wrap-around safe setting of slsb states, returns number of changed buffers */
235 static inline int set_buf_states(struct qdio_q *q, int bufnr,
236                                  unsigned char state, int count)
237 {
238         int i;
239
240         BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
241         BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
242
243         if (is_qebsm(q))
244                 return qdio_do_sqbs(q, state, bufnr, count);
245
246         for (i = 0; i < count; i++) {
247                 xchg(&q->slsb.val[bufnr], state);
248                 bufnr = next_buf(bufnr);
249         }
250         return count;
251 }
252
253 static inline int set_buf_state(struct qdio_q *q, int bufnr,
254                                 unsigned char state)
255 {
256         return set_buf_states(q, bufnr, state, 1);
257 }
258
259 /* set slsb states to initial state */
260 void qdio_init_buf_states(struct qdio_irq *irq_ptr)
261 {
262         struct qdio_q *q;
263         int i;
264
265         for_each_input_queue(irq_ptr, q, i)
266                 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
267                                QDIO_MAX_BUFFERS_PER_Q);
268         for_each_output_queue(irq_ptr, q, i)
269                 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
270                                QDIO_MAX_BUFFERS_PER_Q);
271 }
272
273 static int qdio_siga_sync(struct qdio_q *q, unsigned int output,
274                           unsigned int input)
275 {
276         int cc;
277
278         if (!need_siga_sync(q))
279                 return 0;
280
281         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:");
282         DBF_DEV_HEX(DBF_INFO, q->irq_ptr, q, sizeof(void *));
283         qdio_perf_stat_inc(&perf_stats.siga_sync);
284
285         cc = do_siga_sync(q->irq_ptr->schid, output, input);
286         if (cc)
287                 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
288         return cc;
289 }
290
291 inline int 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         else
296                 return qdio_siga_sync(q, q->mask, 0);
297 }
298
299 static inline int qdio_siga_sync_out(struct qdio_q *q)
300 {
301         return qdio_siga_sync(q, ~0U, 0);
302 }
303
304 static inline int qdio_siga_sync_all(struct qdio_q *q)
305 {
306         return qdio_siga_sync(q, ~0U, ~0U);
307 }
308
309 static inline int qdio_do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
310 {
311         unsigned int fc = 0;
312         unsigned long schid;
313
314         if (q->u.out.use_enh_siga) {
315                 fc = 3;
316         }
317         if (!is_qebsm(q))
318                 schid = *((u32 *)&q->irq_ptr->schid);
319         else {
320                 schid = q->irq_ptr->sch_token;
321                 fc |= 0x80;
322         }
323         return do_siga_output(schid, q->mask, busy_bit, fc);
324 }
325
326 static int qdio_siga_output(struct qdio_q *q)
327 {
328         int cc;
329         u32 busy_bit;
330         u64 start_time = 0;
331
332         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
333         qdio_perf_stat_inc(&perf_stats.siga_out);
334 again:
335         cc = qdio_do_siga_output(q, &busy_bit);
336         if (queue_type(q) == QDIO_IQDIO_QFMT && cc == 2 && busy_bit) {
337                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w bb:%2d", q->nr);
338
339                 if (!start_time)
340                         start_time = get_usecs();
341                 else if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
342                         goto again;
343         }
344
345         if (cc == 2 && busy_bit)
346                 cc |= QDIO_ERROR_SIGA_BUSY;
347         if (cc)
348                 DBF_ERROR("%4x SIGA-W:%2d", SCH_NO(q), cc);
349         return cc;
350 }
351
352 static inline int qdio_siga_input(struct qdio_q *q)
353 {
354         int cc;
355
356         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
357         qdio_perf_stat_inc(&perf_stats.siga_in);
358
359         cc = do_siga_input(q->irq_ptr->schid, q->mask);
360         if (cc)
361                 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
362         return cc;
363 }
364
365 /* called from thinint inbound handler */
366 void qdio_sync_after_thinint(struct qdio_q *q)
367 {
368         if (pci_out_supported(q)) {
369                 if (need_siga_sync_thinint(q))
370                         qdio_siga_sync_all(q);
371                 else if (need_siga_sync_out_thinint(q))
372                         qdio_siga_sync_out(q);
373         } else
374                 qdio_siga_sync_q(q);
375 }
376
377 inline void qdio_stop_polling(struct qdio_q *q)
378 {
379         spin_lock_bh(&q->u.in.lock);
380         if (!q->u.in.polling) {
381                 spin_unlock_bh(&q->u.in.lock);
382                 return;
383         }
384         q->u.in.polling = 0;
385         qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
386
387         /* show the card that we are not polling anymore */
388         set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
389         spin_unlock_bh(&q->u.in.lock);
390 }
391
392 static void announce_buffer_error(struct qdio_q *q)
393 {
394         DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
395         DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
396         DBF_ERROR("FTC:%3d", q->first_to_check);
397         DBF_ERROR("F14:%2x F15:%2x",
398                   q->sbal[q->first_to_check]->element[14].flags & 0xff,
399                   q->sbal[q->first_to_check]->element[15].flags & 0xff);
400
401         q->qdio_error = QDIO_ERROR_SLSB_STATE;
402 }
403
404 static int get_inbound_buffer_frontier(struct qdio_q *q)
405 {
406         int count, stop;
407         unsigned char state;
408
409         /*
410          * If we still poll don't update last_move_ftc, keep the
411          * previously ACK buffer there.
412          */
413         if (!q->u.in.polling)
414                 q->last_move_ftc = q->first_to_check;
415
416         /*
417          * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
418          * would return 0.
419          */
420         count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
421         stop = add_buf(q->first_to_check, count);
422
423         /*
424          * No siga sync here, as a PCI or we after a thin interrupt
425          * will sync the queues.
426          */
427
428         /* need to set count to 1 for non-qebsm */
429         if (!is_qebsm(q))
430                 count = 1;
431
432 check_next:
433         if (q->first_to_check == stop)
434                 goto out;
435
436         count = get_buf_states(q, q->first_to_check, &state, count);
437         if (!count)
438                 goto out;
439
440         switch (state) {
441         case SLSB_P_INPUT_PRIMED:
442                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %3d", count);
443
444                 /*
445                  * Only ACK the first buffer. The ACK will be removed in
446                  * qdio_stop_polling.
447                  */
448                 if (q->u.in.polling)
449                         state = SLSB_P_INPUT_NOT_INIT;
450                 else {
451                         q->u.in.polling = 1;
452                         state = SLSB_P_INPUT_ACK;
453                 }
454                 set_buf_state(q, q->first_to_check, state);
455
456                 /*
457                  * Need to change all PRIMED buffers to NOT_INIT, otherwise
458                  * we're loosing initiative in the thinint code.
459                  */
460                 if (count > 1)
461                         set_buf_states(q, next_buf(q->first_to_check),
462                                        SLSB_P_INPUT_NOT_INIT, count - 1);
463
464                 /*
465                  * No siga-sync needed for non-qebsm here, as the inbound queue
466                  * will be synced on the next siga-r, resp.
467                  * tiqdio_is_inbound_q_done will do the siga-sync.
468                  */
469                 q->first_to_check = add_buf(q->first_to_check, count);
470                 atomic_sub(count, &q->nr_buf_used);
471                 goto check_next;
472         case SLSB_P_INPUT_ERROR:
473                 announce_buffer_error(q);
474                 /* process the buffer, the upper layer will take care of it */
475                 q->first_to_check = add_buf(q->first_to_check, count);
476                 atomic_sub(count, &q->nr_buf_used);
477                 break;
478         case SLSB_CU_INPUT_EMPTY:
479         case SLSB_P_INPUT_NOT_INIT:
480         case SLSB_P_INPUT_ACK:
481                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
482                 break;
483         default:
484                 BUG();
485         }
486 out:
487         return q->first_to_check;
488 }
489
490 int qdio_inbound_q_moved(struct qdio_q *q)
491 {
492         int bufnr;
493
494         bufnr = get_inbound_buffer_frontier(q);
495
496         if ((bufnr != q->last_move_ftc) || q->qdio_error) {
497                 if (!need_siga_sync(q) && !pci_out_supported(q))
498                         q->u.in.timestamp = get_usecs();
499
500                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in moved");
501                 return 1;
502         } else
503                 return 0;
504 }
505
506 static int qdio_inbound_q_done(struct qdio_q *q)
507 {
508         unsigned char state = 0;
509
510         if (!atomic_read(&q->nr_buf_used))
511                 return 1;
512
513         /*
514          * We need that one for synchronization with the adapter, as it
515          * does a kind of PCI avoidance.
516          */
517         qdio_siga_sync_q(q);
518
519         get_buf_state(q, q->first_to_check, &state);
520         if (state == SLSB_P_INPUT_PRIMED)
521                 /* we got something to do */
522                 return 0;
523
524         /* on VM, we don't poll, so the q is always done here */
525         if (need_siga_sync(q) || pci_out_supported(q))
526                 return 1;
527
528         /*
529          * At this point we know, that inbound first_to_check
530          * has (probably) not moved (see qdio_inbound_processing).
531          */
532         if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
533                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%3d",
534                               q->first_to_check);
535                 return 1;
536         } else {
537                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in notd:%3d",
538                               q->first_to_check);
539                 return 0;
540         }
541 }
542
543 void qdio_kick_inbound_handler(struct qdio_q *q)
544 {
545         int count, start, end;
546
547         qdio_perf_stat_inc(&perf_stats.inbound_handler);
548
549         start = q->first_to_kick;
550         end = q->first_to_check;
551         if (end >= start)
552                 count = end - start;
553         else
554                 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
555
556         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%3d c:%3d", start, count);
557
558         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
559                 return;
560
561         q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
562                    start, count, q->irq_ptr->int_parm);
563
564         /* for the next time */
565         q->first_to_kick = q->first_to_check;
566         q->qdio_error = 0;
567 }
568
569 static void __qdio_inbound_processing(struct qdio_q *q)
570 {
571         qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
572 again:
573         if (!qdio_inbound_q_moved(q))
574                 return;
575
576         qdio_kick_inbound_handler(q);
577
578         if (!qdio_inbound_q_done(q))
579                 /* means poll time is not yet over */
580                 goto again;
581
582         qdio_stop_polling(q);
583         /*
584          * We need to check again to not lose initiative after
585          * resetting the ACK state.
586          */
587         if (!qdio_inbound_q_done(q))
588                 goto again;
589 }
590
591 /* inbound tasklet */
592 void qdio_inbound_processing(unsigned long data)
593 {
594         struct qdio_q *q = (struct qdio_q *)data;
595         __qdio_inbound_processing(q);
596 }
597
598 static int get_outbound_buffer_frontier(struct qdio_q *q)
599 {
600         int count, stop;
601         unsigned char state;
602
603         if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
604             (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
605                 qdio_siga_sync_q(q);
606
607         /*
608          * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
609          * would return 0.
610          */
611         count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
612         stop = add_buf(q->first_to_check, count);
613
614         /* need to set count to 1 for non-qebsm */
615         if (!is_qebsm(q))
616                 count = 1;
617
618 check_next:
619         if (q->first_to_check == stop)
620                 return q->first_to_check;
621
622         count = get_buf_states(q, q->first_to_check, &state, count);
623         if (!count)
624                 return q->first_to_check;
625
626         switch (state) {
627         case SLSB_P_OUTPUT_EMPTY:
628                 /* the adapter got it */
629                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %3d", q->nr, count);
630
631                 atomic_sub(count, &q->nr_buf_used);
632                 q->first_to_check = add_buf(q->first_to_check, count);
633                 /*
634                  * We fetch all buffer states at once. get_buf_states may
635                  * return count < stop. For QEBSM we do not loop.
636                  */
637                 if (is_qebsm(q))
638                         break;
639                 goto check_next;
640         case SLSB_P_OUTPUT_ERROR:
641                 announce_buffer_error(q);
642                 /* process the buffer, the upper layer will take care of it */
643                 q->first_to_check = add_buf(q->first_to_check, count);
644                 atomic_sub(count, &q->nr_buf_used);
645                 break;
646         case SLSB_CU_OUTPUT_PRIMED:
647                 /* the adapter has not fetched the output yet */
648                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
649                 break;
650         case SLSB_P_OUTPUT_NOT_INIT:
651         case SLSB_P_OUTPUT_HALTED:
652                 break;
653         default:
654                 BUG();
655         }
656         return q->first_to_check;
657 }
658
659 /* all buffers processed? */
660 static inline int qdio_outbound_q_done(struct qdio_q *q)
661 {
662         return atomic_read(&q->nr_buf_used) == 0;
663 }
664
665 static inline int qdio_outbound_q_moved(struct qdio_q *q)
666 {
667         int bufnr;
668
669         bufnr = get_outbound_buffer_frontier(q);
670
671         if ((bufnr != q->last_move_ftc) || q->qdio_error) {
672                 q->last_move_ftc = bufnr;
673                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
674                 return 1;
675         } else
676                 return 0;
677 }
678
679 /*
680  * VM could present us cc=2 and busy bit set on SIGA-write
681  * during reconfiguration of their Guest LAN (only in iqdio mode,
682  * otherwise qdio is asynchronous and cc=2 and busy bit there will take
683  * the queues down immediately).
684  *
685  * Therefore qdio_siga_output will try for a short time constantly,
686  * if such a condition occurs. If it doesn't change, it will
687  * increase the busy_siga_counter and save the timestamp, and
688  * schedule the queue for later processing. qdio_outbound_processing
689  * will check out the counter. If non-zero, it will call qdio_kick_outbound_q
690  * as often as the value of the counter. This will attempt further SIGA
691  * instructions. For each successful SIGA, the counter is
692  * decreased, for failing SIGAs the counter remains the same, after
693  * all. After some time of no movement, qdio_kick_outbound_q will
694  * finally fail and reflect corresponding error codes to call
695  * the upper layer module and have it take the queues down.
696  *
697  * Note that this is a change from the original HiperSockets design
698  * (saying cc=2 and busy bit means take the queues down), but in
699  * these days Guest LAN didn't exist... excessive cc=2 with busy bit
700  * conditions will still take the queues down, but the threshold is
701  * higher due to the Guest LAN environment.
702  *
703  * Called from outbound tasklet and do_QDIO handler.
704  */
705 static void qdio_kick_outbound_q(struct qdio_q *q)
706 {
707         int rc;
708
709         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kickoutq:%1d", q->nr);
710
711         if (!need_siga_out(q))
712                 return;
713
714         rc = qdio_siga_output(q);
715         switch (rc) {
716         case 0:
717                 /* TODO: improve error handling for CC=0 case */
718                 if (q->u.out.timestamp)
719                         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "cc2 rslv:%4x",
720                                       atomic_read(&q->u.out.busy_siga_counter));
721                 /* went smooth this time, reset timestamp */
722                 q->u.out.timestamp = 0;
723                 break;
724         /* cc=2 and busy bit */
725         case (2 | QDIO_ERROR_SIGA_BUSY):
726                 atomic_inc(&q->u.out.busy_siga_counter);
727
728                 /* if the last siga was successful, save timestamp here */
729                 if (!q->u.out.timestamp)
730                         q->u.out.timestamp = get_usecs();
731
732                 /* if we're in time, don't touch qdio_error */
733                 if (get_usecs() - q->u.out.timestamp < QDIO_BUSY_BIT_GIVE_UP) {
734                         tasklet_schedule(&q->tasklet);
735                         break;
736                 }
737                 DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
738         default:
739                 /* for plain cc=1, 2 or 3 */
740                 q->qdio_error = rc;
741         }
742 }
743
744 static void qdio_kick_outbound_handler(struct qdio_q *q)
745 {
746         int start, end, count;
747
748         start = q->first_to_kick;
749         end = q->last_move_ftc;
750         if (end >= start)
751                 count = end - start;
752         else
753                 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
754
755         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kickouth: %1d", q->nr);
756         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "s:%3d c:%3d", start, count);
757
758         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
759                 return;
760
761         q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
762                    q->irq_ptr->int_parm);
763
764         /* for the next time: */
765         q->first_to_kick = q->last_move_ftc;
766         q->qdio_error = 0;
767 }
768
769 static void __qdio_outbound_processing(struct qdio_q *q)
770 {
771         int siga_attempts;
772
773         qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
774
775         /* see comment in qdio_kick_outbound_q */
776         siga_attempts = atomic_read(&q->u.out.busy_siga_counter);
777         while (siga_attempts--) {
778                 atomic_dec(&q->u.out.busy_siga_counter);
779                 qdio_kick_outbound_q(q);
780         }
781
782         BUG_ON(atomic_read(&q->nr_buf_used) < 0);
783
784         if (qdio_outbound_q_moved(q))
785                 qdio_kick_outbound_handler(q);
786
787         if (queue_type(q) == QDIO_ZFCP_QFMT) {
788                 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
789                         tasklet_schedule(&q->tasklet);
790                 return;
791         }
792
793         /* bail out for HiperSockets unicast queues */
794         if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
795                 return;
796
797         if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
798             (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL) {
799                 tasklet_schedule(&q->tasklet);
800                 return;
801         }
802
803         if (q->u.out.pci_out_enabled)
804                 return;
805
806         /*
807          * Now we know that queue type is either qeth without pci enabled
808          * or HiperSockets multicast. Make sure buffer switch from PRIMED to
809          * EMPTY is noticed and outbound_handler is called after some time.
810          */
811         if (qdio_outbound_q_done(q))
812                 del_timer(&q->u.out.timer);
813         else {
814                 if (!timer_pending(&q->u.out.timer)) {
815                         mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
816                         qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
817                 }
818         }
819 }
820
821 /* outbound tasklet */
822 void qdio_outbound_processing(unsigned long data)
823 {
824         struct qdio_q *q = (struct qdio_q *)data;
825         __qdio_outbound_processing(q);
826 }
827
828 void qdio_outbound_timer(unsigned long data)
829 {
830         struct qdio_q *q = (struct qdio_q *)data;
831         tasklet_schedule(&q->tasklet);
832 }
833
834 /* called from thinint inbound tasklet */
835 void qdio_check_outbound_after_thinint(struct qdio_q *q)
836 {
837         struct qdio_q *out;
838         int i;
839
840         if (!pci_out_supported(q))
841                 return;
842
843         for_each_output_queue(q->irq_ptr, out, i)
844                 if (!qdio_outbound_q_done(out))
845                         tasklet_schedule(&out->tasklet);
846 }
847
848 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
849                                   enum qdio_irq_states state)
850 {
851         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
852
853         irq_ptr->state = state;
854         mb();
855 }
856
857 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
858 {
859         if (irb->esw.esw0.erw.cons) {
860                 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
861                 DBF_ERROR_HEX(irb, 64);
862                 DBF_ERROR_HEX(irb->ecw, 64);
863         }
864 }
865
866 /* PCI interrupt handler */
867 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
868 {
869         int i;
870         struct qdio_q *q;
871
872         qdio_perf_stat_inc(&perf_stats.pci_int);
873
874         for_each_input_queue(irq_ptr, q, i)
875                 tasklet_schedule(&q->tasklet);
876
877         if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
878                 return;
879
880         for_each_output_queue(irq_ptr, q, i) {
881                 if (qdio_outbound_q_done(q))
882                         continue;
883
884                 if (!siga_syncs_out_pci(q))
885                         qdio_siga_sync_q(q);
886
887                 tasklet_schedule(&q->tasklet);
888         }
889 }
890
891 static void qdio_handle_activate_check(struct ccw_device *cdev,
892                                 unsigned long intparm, int cstat, int dstat)
893 {
894         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
895         struct qdio_q *q;
896
897         DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
898         DBF_ERROR("intp :%lx", intparm);
899         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
900
901         if (irq_ptr->nr_input_qs) {
902                 q = irq_ptr->input_qs[0];
903         } else if (irq_ptr->nr_output_qs) {
904                 q = irq_ptr->output_qs[0];
905         } else {
906                 dump_stack();
907                 goto no_handler;
908         }
909         q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
910                    0, -1, -1, irq_ptr->int_parm);
911 no_handler:
912         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
913 }
914
915 static void qdio_call_shutdown(struct work_struct *work)
916 {
917         struct ccw_device_private *priv;
918         struct ccw_device *cdev;
919
920         priv = container_of(work, struct ccw_device_private, kick_work);
921         cdev = priv->cdev;
922         qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
923         put_device(&cdev->dev);
924 }
925
926 static void qdio_int_error(struct ccw_device *cdev)
927 {
928         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
929
930         switch (irq_ptr->state) {
931         case QDIO_IRQ_STATE_INACTIVE:
932         case QDIO_IRQ_STATE_CLEANUP:
933                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
934                 break;
935         case QDIO_IRQ_STATE_ESTABLISHED:
936         case QDIO_IRQ_STATE_ACTIVE:
937                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
938                 if (get_device(&cdev->dev)) {
939                         /* Can't call shutdown from interrupt context. */
940                         PREPARE_WORK(&cdev->private->kick_work,
941                                      qdio_call_shutdown);
942                         queue_work(ccw_device_work, &cdev->private->kick_work);
943                 }
944                 break;
945         default:
946                 WARN_ON(1);
947         }
948         wake_up(&cdev->private->wait_q);
949 }
950
951 static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
952                                        int dstat)
953 {
954         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
955
956         if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
957                 DBF_ERROR("EQ:ck con");
958                 goto error;
959         }
960
961         if (!(dstat & DEV_STAT_DEV_END)) {
962                 DBF_ERROR("EQ:no dev");
963                 goto error;
964         }
965
966         if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
967                 DBF_ERROR("EQ: bad io");
968                 goto error;
969         }
970         return 0;
971 error:
972         DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
973         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
974
975         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
976         return 1;
977 }
978
979 static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
980                                       int dstat)
981 {
982         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
983
984         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
985         if (!qdio_establish_check_errors(cdev, cstat, dstat))
986                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
987 }
988
989 /* qdio interrupt handler */
990 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
991                       struct irb *irb)
992 {
993         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
994         int cstat, dstat;
995
996         qdio_perf_stat_inc(&perf_stats.qdio_int);
997
998         if (!intparm || !irq_ptr) {
999                 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
1000                 return;
1001         }
1002
1003         if (IS_ERR(irb)) {
1004                 switch (PTR_ERR(irb)) {
1005                 case -EIO:
1006                         DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1007                         return;
1008                 case -ETIMEDOUT:
1009                         DBF_ERROR("%4x IO timeout", irq_ptr->schid.sch_no);
1010                         qdio_int_error(cdev);
1011                         return;
1012                 default:
1013                         WARN_ON(1);
1014                         return;
1015                 }
1016         }
1017         qdio_irq_check_sense(irq_ptr, irb);
1018
1019         cstat = irb->scsw.cmd.cstat;
1020         dstat = irb->scsw.cmd.dstat;
1021
1022         switch (irq_ptr->state) {
1023         case QDIO_IRQ_STATE_INACTIVE:
1024                 qdio_establish_handle_irq(cdev, cstat, dstat);
1025                 break;
1026
1027         case QDIO_IRQ_STATE_CLEANUP:
1028                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1029                 break;
1030
1031         case QDIO_IRQ_STATE_ESTABLISHED:
1032         case QDIO_IRQ_STATE_ACTIVE:
1033                 if (cstat & SCHN_STAT_PCI) {
1034                         qdio_int_handler_pci(irq_ptr);
1035                         /* no state change so no need to wake up wait_q */
1036                         return;
1037                 }
1038                 if ((cstat & ~SCHN_STAT_PCI) || dstat) {
1039                         qdio_handle_activate_check(cdev, intparm, cstat,
1040                                                    dstat);
1041                         break;
1042                 }
1043         default:
1044                 WARN_ON(1);
1045         }
1046         wake_up(&cdev->private->wait_q);
1047 }
1048
1049 /**
1050  * qdio_get_ssqd_desc - get qdio subchannel description
1051  * @cdev: ccw device to get description for
1052  * @data: where to store the ssqd
1053  *
1054  * Returns 0 or an error code. The results of the chsc are stored in the
1055  * specified structure.
1056  */
1057 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1058                        struct qdio_ssqd_desc *data)
1059 {
1060
1061         if (!cdev || !cdev->private)
1062                 return -EINVAL;
1063
1064         DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
1065         return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
1066 }
1067 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1068
1069 /**
1070  * qdio_cleanup - shutdown queues and free data structures
1071  * @cdev: associated ccw device
1072  * @how: use halt or clear to shutdown
1073  *
1074  * This function calls qdio_shutdown() for @cdev with method @how
1075  * and on success qdio_free() for @cdev.
1076  */
1077 int qdio_cleanup(struct ccw_device *cdev, int how)
1078 {
1079         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1080         int rc;
1081
1082         if (!irq_ptr)
1083                 return -ENODEV;
1084
1085         rc = qdio_shutdown(cdev, how);
1086         if (rc == 0)
1087                 rc = qdio_free(cdev);
1088         return rc;
1089 }
1090 EXPORT_SYMBOL_GPL(qdio_cleanup);
1091
1092 static void qdio_shutdown_queues(struct ccw_device *cdev)
1093 {
1094         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1095         struct qdio_q *q;
1096         int i;
1097
1098         for_each_input_queue(irq_ptr, q, i)
1099                 tasklet_disable(&q->tasklet);
1100
1101         for_each_output_queue(irq_ptr, q, i) {
1102                 tasklet_disable(&q->tasklet);
1103                 del_timer(&q->u.out.timer);
1104         }
1105 }
1106
1107 /**
1108  * qdio_shutdown - shut down a qdio subchannel
1109  * @cdev: associated ccw device
1110  * @how: use halt or clear to shutdown
1111  */
1112 int qdio_shutdown(struct ccw_device *cdev, int how)
1113 {
1114         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1115         int rc;
1116         unsigned long flags;
1117
1118         if (!irq_ptr)
1119                 return -ENODEV;
1120
1121         DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1122
1123         mutex_lock(&irq_ptr->setup_mutex);
1124         /*
1125          * Subchannel was already shot down. We cannot prevent being called
1126          * twice since cio may trigger a shutdown asynchronously.
1127          */
1128         if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1129                 mutex_unlock(&irq_ptr->setup_mutex);
1130                 return 0;
1131         }
1132
1133         tiqdio_remove_input_queues(irq_ptr);
1134         qdio_shutdown_queues(cdev);
1135         qdio_shutdown_debug_entries(irq_ptr, cdev);
1136
1137         /* cleanup subchannel */
1138         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1139
1140         if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1141                 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1142         else
1143                 /* default behaviour is halt */
1144                 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1145         if (rc) {
1146                 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1147                 DBF_ERROR("rc:%4d", rc);
1148                 goto no_cleanup;
1149         }
1150
1151         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1152         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1153         wait_event_interruptible_timeout(cdev->private->wait_q,
1154                 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1155                 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1156                 10 * HZ);
1157         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1158
1159 no_cleanup:
1160         qdio_shutdown_thinint(irq_ptr);
1161
1162         /* restore interrupt handler */
1163         if ((void *)cdev->handler == (void *)qdio_int_handler)
1164                 cdev->handler = irq_ptr->orig_handler;
1165         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1166
1167         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1168         mutex_unlock(&irq_ptr->setup_mutex);
1169         if (rc)
1170                 return rc;
1171         return 0;
1172 }
1173 EXPORT_SYMBOL_GPL(qdio_shutdown);
1174
1175 /**
1176  * qdio_free - free data structures for a qdio subchannel
1177  * @cdev: associated ccw device
1178  */
1179 int qdio_free(struct ccw_device *cdev)
1180 {
1181         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1182
1183         if (!irq_ptr)
1184                 return -ENODEV;
1185
1186         DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
1187         mutex_lock(&irq_ptr->setup_mutex);
1188
1189         if (irq_ptr->debug_area != NULL) {
1190                 debug_unregister(irq_ptr->debug_area);
1191                 irq_ptr->debug_area = NULL;
1192         }
1193         cdev->private->qdio_data = NULL;
1194         mutex_unlock(&irq_ptr->setup_mutex);
1195
1196         qdio_release_memory(irq_ptr);
1197         return 0;
1198 }
1199 EXPORT_SYMBOL_GPL(qdio_free);
1200
1201 /**
1202  * qdio_initialize - allocate and establish queues for a qdio subchannel
1203  * @init_data: initialization data
1204  *
1205  * This function first allocates queues via qdio_allocate() and on success
1206  * establishes them via qdio_establish().
1207  */
1208 int qdio_initialize(struct qdio_initialize *init_data)
1209 {
1210         int rc;
1211
1212         rc = qdio_allocate(init_data);
1213         if (rc)
1214                 return rc;
1215
1216         rc = qdio_establish(init_data);
1217         if (rc)
1218                 qdio_free(init_data->cdev);
1219         return rc;
1220 }
1221 EXPORT_SYMBOL_GPL(qdio_initialize);
1222
1223 /**
1224  * qdio_allocate - allocate qdio queues and associated data
1225  * @init_data: initialization data
1226  */
1227 int qdio_allocate(struct qdio_initialize *init_data)
1228 {
1229         struct qdio_irq *irq_ptr;
1230
1231         DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
1232
1233         if ((init_data->no_input_qs && !init_data->input_handler) ||
1234             (init_data->no_output_qs && !init_data->output_handler))
1235                 return -EINVAL;
1236
1237         if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1238             (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1239                 return -EINVAL;
1240
1241         if ((!init_data->input_sbal_addr_array) ||
1242             (!init_data->output_sbal_addr_array))
1243                 return -EINVAL;
1244
1245         /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1246         irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1247         if (!irq_ptr)
1248                 goto out_err;
1249
1250         mutex_init(&irq_ptr->setup_mutex);
1251         qdio_allocate_dbf(init_data, irq_ptr);
1252
1253         /*
1254          * Allocate a page for the chsc calls in qdio_establish.
1255          * Must be pre-allocated since a zfcp recovery will call
1256          * qdio_establish. In case of low memory and swap on a zfcp disk
1257          * we may not be able to allocate memory otherwise.
1258          */
1259         irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1260         if (!irq_ptr->chsc_page)
1261                 goto out_rel;
1262
1263         /* qdr is used in ccw1.cda which is u32 */
1264         irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1265         if (!irq_ptr->qdr)
1266                 goto out_rel;
1267         WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1268
1269         if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1270                              init_data->no_output_qs))
1271                 goto out_rel;
1272
1273         init_data->cdev->private->qdio_data = irq_ptr;
1274         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1275         return 0;
1276 out_rel:
1277         qdio_release_memory(irq_ptr);
1278 out_err:
1279         return -ENOMEM;
1280 }
1281 EXPORT_SYMBOL_GPL(qdio_allocate);
1282
1283 /**
1284  * qdio_establish - establish queues on a qdio subchannel
1285  * @init_data: initialization data
1286  */
1287 int qdio_establish(struct qdio_initialize *init_data)
1288 {
1289         struct qdio_irq *irq_ptr;
1290         struct ccw_device *cdev = init_data->cdev;
1291         unsigned long saveflags;
1292         int rc;
1293
1294         DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
1295
1296         irq_ptr = cdev->private->qdio_data;
1297         if (!irq_ptr)
1298                 return -ENODEV;
1299
1300         if (cdev->private->state != DEV_STATE_ONLINE)
1301                 return -EINVAL;
1302
1303         mutex_lock(&irq_ptr->setup_mutex);
1304         qdio_setup_irq(init_data);
1305
1306         rc = qdio_establish_thinint(irq_ptr);
1307         if (rc) {
1308                 mutex_unlock(&irq_ptr->setup_mutex);
1309                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1310                 return rc;
1311         }
1312
1313         /* establish q */
1314         irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1315         irq_ptr->ccw.flags = CCW_FLAG_SLI;
1316         irq_ptr->ccw.count = irq_ptr->equeue.count;
1317         irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1318
1319         spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1320         ccw_device_set_options_mask(cdev, 0);
1321
1322         rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1323         if (rc) {
1324                 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1325                 DBF_ERROR("rc:%4x", rc);
1326         }
1327         spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1328
1329         if (rc) {
1330                 mutex_unlock(&irq_ptr->setup_mutex);
1331                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1332                 return rc;
1333         }
1334
1335         wait_event_interruptible_timeout(cdev->private->wait_q,
1336                 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1337                 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1338
1339         if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1340                 mutex_unlock(&irq_ptr->setup_mutex);
1341                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1342                 return -EIO;
1343         }
1344
1345         qdio_setup_ssqd_info(irq_ptr);
1346         DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
1347         DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
1348
1349         /* qebsm is now setup if available, initialize buffer states */
1350         qdio_init_buf_states(irq_ptr);
1351
1352         mutex_unlock(&irq_ptr->setup_mutex);
1353         qdio_print_subchannel_info(irq_ptr, cdev);
1354         qdio_setup_debug_entries(irq_ptr, cdev);
1355         return 0;
1356 }
1357 EXPORT_SYMBOL_GPL(qdio_establish);
1358
1359 /**
1360  * qdio_activate - activate queues on a qdio subchannel
1361  * @cdev: associated cdev
1362  */
1363 int qdio_activate(struct ccw_device *cdev)
1364 {
1365         struct qdio_irq *irq_ptr;
1366         int rc;
1367         unsigned long saveflags;
1368
1369         DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
1370
1371         irq_ptr = cdev->private->qdio_data;
1372         if (!irq_ptr)
1373                 return -ENODEV;
1374
1375         if (cdev->private->state != DEV_STATE_ONLINE)
1376                 return -EINVAL;
1377
1378         mutex_lock(&irq_ptr->setup_mutex);
1379         if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1380                 rc = -EBUSY;
1381                 goto out;
1382         }
1383
1384         irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1385         irq_ptr->ccw.flags = CCW_FLAG_SLI;
1386         irq_ptr->ccw.count = irq_ptr->aqueue.count;
1387         irq_ptr->ccw.cda = 0;
1388
1389         spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1390         ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1391
1392         rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1393                               0, DOIO_DENY_PREFETCH);
1394         if (rc) {
1395                 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1396                 DBF_ERROR("rc:%4x", rc);
1397         }
1398         spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1399
1400         if (rc)
1401                 goto out;
1402
1403         if (is_thinint_irq(irq_ptr))
1404                 tiqdio_add_input_queues(irq_ptr);
1405
1406         /* wait for subchannel to become active */
1407         msleep(5);
1408
1409         switch (irq_ptr->state) {
1410         case QDIO_IRQ_STATE_STOPPED:
1411         case QDIO_IRQ_STATE_ERR:
1412                 mutex_unlock(&irq_ptr->setup_mutex);
1413                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1414                 return -EIO;
1415         default:
1416                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1417                 rc = 0;
1418         }
1419 out:
1420         mutex_unlock(&irq_ptr->setup_mutex);
1421         return rc;
1422 }
1423 EXPORT_SYMBOL_GPL(qdio_activate);
1424
1425 static inline int buf_in_between(int bufnr, int start, int count)
1426 {
1427         int end = add_buf(start, count);
1428
1429         if (end > start) {
1430                 if (bufnr >= start && bufnr < end)
1431                         return 1;
1432                 else
1433                         return 0;
1434         }
1435
1436         /* wrap-around case */
1437         if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1438             (bufnr < end))
1439                 return 1;
1440         else
1441                 return 0;
1442 }
1443
1444 /**
1445  * handle_inbound - reset processed input buffers
1446  * @q: queue containing the buffers
1447  * @callflags: flags
1448  * @bufnr: first buffer to process
1449  * @count: how many buffers are emptied
1450  */
1451 static void handle_inbound(struct qdio_q *q, unsigned int callflags,
1452                            int bufnr, int count)
1453 {
1454         unsigned long flags;
1455         int used, rc;
1456
1457         /*
1458          * do_QDIO could run in parallel with the queue tasklet so the
1459          * upper-layer programm could empty the ACK'ed buffer here.
1460          * If that happens we must clear the polling flag, otherwise
1461          * qdio_stop_polling() could set the buffer to NOT_INIT after
1462          * it was set to EMPTY which would kill us.
1463          */
1464         spin_lock_irqsave(&q->u.in.lock, flags);
1465         if (q->u.in.polling)
1466                 if (buf_in_between(q->last_move_ftc, bufnr, count))
1467                         q->u.in.polling = 0;
1468
1469         count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1470         spin_unlock_irqrestore(&q->u.in.lock, flags);
1471
1472         used = atomic_add_return(count, &q->nr_buf_used) - count;
1473         BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1474
1475         /* no need to signal as long as the adapter had free buffers */
1476         if (used)
1477                 return;
1478
1479         if (need_siga_in(q)) {
1480                 rc = qdio_siga_input(q);
1481                 if (rc)
1482                         q->qdio_error = rc;
1483         }
1484 }
1485
1486 /**
1487  * handle_outbound - process filled outbound buffers
1488  * @q: queue containing the buffers
1489  * @callflags: flags
1490  * @bufnr: first buffer to process
1491  * @count: how many buffers are filled
1492  */
1493 static void handle_outbound(struct qdio_q *q, unsigned int callflags,
1494                             int bufnr, int count)
1495 {
1496         unsigned char state;
1497         int used;
1498
1499         qdio_perf_stat_inc(&perf_stats.outbound_handler);
1500
1501         count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1502         used = atomic_add_return(count, &q->nr_buf_used);
1503         BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1504
1505         if (callflags & QDIO_FLAG_PCI_OUT)
1506                 q->u.out.pci_out_enabled = 1;
1507         else
1508                 q->u.out.pci_out_enabled = 0;
1509
1510         if (queue_type(q) == QDIO_IQDIO_QFMT) {
1511                 if (multicast_outbound(q))
1512                         qdio_kick_outbound_q(q);
1513                 else
1514                         if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
1515                             (count > 1) &&
1516                             (count <= q->irq_ptr->ssqd_desc.mmwc)) {
1517                                 /* exploit enhanced SIGA */
1518                                 q->u.out.use_enh_siga = 1;
1519                                 qdio_kick_outbound_q(q);
1520                         } else {
1521                                 /*
1522                                 * One siga-w per buffer required for unicast
1523                                 * HiperSockets.
1524                                 */
1525                                 q->u.out.use_enh_siga = 0;
1526                                 while (count--)
1527                                         qdio_kick_outbound_q(q);
1528                         }
1529                 goto out;
1530         }
1531
1532         if (need_siga_sync(q)) {
1533                 qdio_siga_sync_q(q);
1534                 goto out;
1535         }
1536
1537         /* try to fast requeue buffers */
1538         get_buf_state(q, prev_buf(bufnr), &state);
1539         if (state != SLSB_CU_OUTPUT_PRIMED)
1540                 qdio_kick_outbound_q(q);
1541         else {
1542                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "fast-req");
1543                 qdio_perf_stat_inc(&perf_stats.fast_requeue);
1544         }
1545 out:
1546         /* Fixme: could wait forever if called from process context */
1547         tasklet_schedule(&q->tasklet);
1548 }
1549
1550 /**
1551  * do_QDIO - process input or output buffers
1552  * @cdev: associated ccw_device for the qdio subchannel
1553  * @callflags: input or output and special flags from the program
1554  * @q_nr: queue number
1555  * @bufnr: buffer number
1556  * @count: how many buffers to process
1557  */
1558 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1559             int q_nr, int bufnr, int count)
1560 {
1561         struct qdio_irq *irq_ptr;
1562
1563         if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
1564             (count > QDIO_MAX_BUFFERS_PER_Q) ||
1565             (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
1566                 return -EINVAL;
1567
1568         if (!count)
1569                 return 0;
1570
1571         irq_ptr = cdev->private->qdio_data;
1572         if (!irq_ptr)
1573                 return -ENODEV;
1574
1575         if (callflags & QDIO_FLAG_SYNC_INPUT)
1576                 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO input");
1577         else
1578                 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "doQDIO output");
1579         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "q:%1d flag:%4x", q_nr, callflags);
1580         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "buf:%2d cnt:%3d", bufnr, count);
1581
1582         if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1583                 return -EBUSY;
1584
1585         if (callflags & QDIO_FLAG_SYNC_INPUT)
1586                 handle_inbound(irq_ptr->input_qs[q_nr], callflags, bufnr,
1587                                count);
1588         else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1589                 handle_outbound(irq_ptr->output_qs[q_nr], callflags, bufnr,
1590                                 count);
1591         else
1592                 return -EINVAL;
1593         return 0;
1594 }
1595 EXPORT_SYMBOL_GPL(do_QDIO);
1596
1597 static int __init init_QDIO(void)
1598 {
1599         int rc;
1600
1601         rc = qdio_setup_init();
1602         if (rc)
1603                 return rc;
1604         rc = tiqdio_allocate_memory();
1605         if (rc)
1606                 goto out_cache;
1607         rc = qdio_debug_init();
1608         if (rc)
1609                 goto out_ti;
1610         rc = qdio_setup_perf_stats();
1611         if (rc)
1612                 goto out_debug;
1613         rc = tiqdio_register_thinints();
1614         if (rc)
1615                 goto out_perf;
1616         return 0;
1617
1618 out_perf:
1619         qdio_remove_perf_stats();
1620 out_debug:
1621         qdio_debug_exit();
1622 out_ti:
1623         tiqdio_free_memory();
1624 out_cache:
1625         qdio_setup_exit();
1626         return rc;
1627 }
1628
1629 static void __exit exit_QDIO(void)
1630 {
1631         tiqdio_unregister_thinints();
1632         tiqdio_free_memory();
1633         qdio_remove_perf_stats();
1634         qdio_debug_exit();
1635         qdio_setup_exit();
1636 }
1637
1638 module_init(init_QDIO);
1639 module_exit(exit_QDIO);