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