[S390] qdio: EQBS retry after CCQ 96
[pandora-kernel.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 <linux/gfp.h>
17 #include <linux/io.h>
18 #include <linux/atomic.h>
19 #include <asm/debug.h>
20 #include <asm/qdio.h>
21
22 #include "cio.h"
23 #include "css.h"
24 #include "device.h"
25 #include "qdio.h"
26 #include "qdio_debug.h"
27
28 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29         "Jan Glauber <jang@linux.vnet.ibm.com>");
30 MODULE_DESCRIPTION("QDIO base support");
31 MODULE_LICENSE("GPL");
32
33 static inline int do_siga_sync(unsigned long schid,
34                                unsigned int out_mask, unsigned int in_mask,
35                                unsigned int fc)
36 {
37         register unsigned long __fc asm ("0") = fc;
38         register unsigned long __schid asm ("1") = schid;
39         register unsigned long out asm ("2") = out_mask;
40         register unsigned long in asm ("3") = in_mask;
41         int cc;
42
43         asm volatile(
44                 "       siga    0\n"
45                 "       ipm     %0\n"
46                 "       srl     %0,28\n"
47                 : "=d" (cc)
48                 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
49         return cc;
50 }
51
52 static inline int do_siga_input(unsigned long schid, unsigned int mask,
53                                 unsigned int fc)
54 {
55         register unsigned long __fc asm ("0") = fc;
56         register unsigned long __schid asm ("1") = schid;
57         register unsigned long __mask asm ("2") = mask;
58         int cc;
59
60         asm volatile(
61                 "       siga    0\n"
62                 "       ipm     %0\n"
63                 "       srl     %0,28\n"
64                 : "=d" (cc)
65                 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
66         return cc;
67 }
68
69 /**
70  * do_siga_output - perform SIGA-w/wt function
71  * @schid: subchannel id or in case of QEBSM the subchannel token
72  * @mask: which output queues to process
73  * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
74  * @fc: function code to perform
75  *
76  * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
77  * Note: For IQDC unicast queues only the highest priority queue is processed.
78  */
79 static inline int do_siga_output(unsigned long schid, unsigned long mask,
80                                  unsigned int *bb, unsigned int fc,
81                                  unsigned long aob)
82 {
83         register unsigned long __fc asm("0") = fc;
84         register unsigned long __schid asm("1") = schid;
85         register unsigned long __mask asm("2") = mask;
86         register unsigned long __aob asm("3") = aob;
87         int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
88
89         asm volatile(
90                 "       siga    0\n"
91                 "0:     ipm     %0\n"
92                 "       srl     %0,28\n"
93                 "1:\n"
94                 EX_TABLE(0b, 1b)
95                 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask),
96                   "+d" (__aob)
97                 : : "cc", "memory");
98         *bb = ((unsigned int) __fc) >> 31;
99         return cc;
100 }
101
102 static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
103 {
104         /* all done or next buffer state different */
105         if (ccq == 0 || ccq == 32)
106                 return 0;
107         /* no buffer processed */
108         if (ccq == 97)
109                 return 1;
110         /* not all buffers processed */
111         if (ccq == 96)
112                 return 2;
113         /* notify devices immediately */
114         DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
115         return -EIO;
116 }
117
118 /**
119  * qdio_do_eqbs - extract buffer states for QEBSM
120  * @q: queue to manipulate
121  * @state: state of the extracted buffers
122  * @start: buffer number to start at
123  * @count: count of buffers to examine
124  * @auto_ack: automatically acknowledge buffers
125  *
126  * Returns the number of successfully extracted equal buffer states.
127  * Stops processing if a state is different from the last buffers state.
128  */
129 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
130                         int start, int count, int auto_ack)
131 {
132         int rc, tmp_count = count, tmp_start = start, nr = q->nr, retried = 0;
133         unsigned int ccq = 0;
134
135         BUG_ON(!q->irq_ptr->sch_token);
136         qperf_inc(q, eqbs);
137
138         if (!q->is_input_q)
139                 nr += q->irq_ptr->nr_input_qs;
140 again:
141         ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
142                       auto_ack);
143         rc = qdio_check_ccq(q, ccq);
144         if (!rc)
145                 return count - tmp_count;
146
147         if (rc == 1) {
148                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
149                 goto again;
150         }
151
152         if (rc == 2) {
153                 BUG_ON(tmp_count == count);
154                 qperf_inc(q, eqbs_partial);
155                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
156                         tmp_count);
157                 /*
158                  * Retry once, if that fails bail out and process the
159                  * extracted buffers before trying again.
160                  */
161                 if (!retried++)
162                         goto again;
163                 else
164                         return count - tmp_count;
165         }
166
167         DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
168         DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
169         q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
170                    0, -1, -1, q->irq_ptr->int_parm);
171         return 0;
172 }
173
174 /**
175  * qdio_do_sqbs - set buffer states for QEBSM
176  * @q: queue to manipulate
177  * @state: new state of the buffers
178  * @start: first buffer number to change
179  * @count: how many buffers to change
180  *
181  * Returns the number of successfully changed buffers.
182  * Does retrying until the specified count of buffer states is set or an
183  * error occurs.
184  */
185 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
186                         int count)
187 {
188         unsigned int ccq = 0;
189         int tmp_count = count, tmp_start = start;
190         int nr = q->nr;
191         int rc;
192
193         if (!count)
194                 return 0;
195
196         BUG_ON(!q->irq_ptr->sch_token);
197         qperf_inc(q, sqbs);
198
199         if (!q->is_input_q)
200                 nr += q->irq_ptr->nr_input_qs;
201 again:
202         ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
203         rc = qdio_check_ccq(q, ccq);
204         if (!rc) {
205                 WARN_ON(tmp_count);
206                 return count - tmp_count;
207         }
208
209         if (rc == 1 || rc == 2) {
210                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
211                 qperf_inc(q, sqbs_partial);
212                 goto again;
213         }
214
215         DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
216         DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
217         q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
218                    0, -1, -1, q->irq_ptr->int_parm);
219         return 0;
220 }
221
222 /* returns number of examined buffers and their common state in *state */
223 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
224                                  unsigned char *state, unsigned int count,
225                                  int auto_ack, int merge_pending)
226 {
227         unsigned char __state = 0;
228         int i;
229
230         BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
231         BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
232
233         if (is_qebsm(q))
234                 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
235
236         for (i = 0; i < count; i++) {
237                 if (!__state) {
238                         __state = q->slsb.val[bufnr];
239                         if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
240                                 __state = SLSB_P_OUTPUT_EMPTY;
241                 } else if (merge_pending) {
242                         if ((q->slsb.val[bufnr] & __state) != __state)
243                                 break;
244                 } else if (q->slsb.val[bufnr] != __state)
245                         break;
246                 bufnr = next_buf(bufnr);
247         }
248         *state = __state;
249         return i;
250 }
251
252 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
253                                 unsigned char *state, int auto_ack)
254 {
255         return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
256 }
257
258 /* wrap-around safe setting of slsb states, returns number of changed buffers */
259 static inline int set_buf_states(struct qdio_q *q, int bufnr,
260                                  unsigned char state, int count)
261 {
262         int i;
263
264         BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
265         BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
266
267         if (is_qebsm(q))
268                 return qdio_do_sqbs(q, state, bufnr, count);
269
270         for (i = 0; i < count; i++) {
271                 xchg(&q->slsb.val[bufnr], state);
272                 bufnr = next_buf(bufnr);
273         }
274         return count;
275 }
276
277 static inline int set_buf_state(struct qdio_q *q, int bufnr,
278                                 unsigned char state)
279 {
280         return set_buf_states(q, bufnr, state, 1);
281 }
282
283 /* set slsb states to initial state */
284 void qdio_init_buf_states(struct qdio_irq *irq_ptr)
285 {
286         struct qdio_q *q;
287         int i;
288
289         for_each_input_queue(irq_ptr, q, i)
290                 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
291                                QDIO_MAX_BUFFERS_PER_Q);
292         for_each_output_queue(irq_ptr, q, i)
293                 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
294                                QDIO_MAX_BUFFERS_PER_Q);
295 }
296
297 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
298                           unsigned int input)
299 {
300         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
301         unsigned int fc = QDIO_SIGA_SYNC;
302         int cc;
303
304         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
305         qperf_inc(q, siga_sync);
306
307         if (is_qebsm(q)) {
308                 schid = q->irq_ptr->sch_token;
309                 fc |= QDIO_SIGA_QEBSM_FLAG;
310         }
311
312         cc = do_siga_sync(schid, output, input, fc);
313         if (unlikely(cc))
314                 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
315         return cc;
316 }
317
318 static inline int qdio_siga_sync_q(struct qdio_q *q)
319 {
320         if (q->is_input_q)
321                 return qdio_siga_sync(q, 0, q->mask);
322         else
323                 return qdio_siga_sync(q, q->mask, 0);
324 }
325
326 static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit,
327         unsigned long aob)
328 {
329         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
330         unsigned int fc = QDIO_SIGA_WRITE;
331         u64 start_time = 0;
332         int retries = 0, cc;
333         unsigned long laob = 0;
334
335         if (q->u.out.use_cq && aob != 0) {
336                 fc = QDIO_SIGA_WRITEQ;
337                 laob = aob;
338         }
339
340         if (is_qebsm(q)) {
341                 schid = q->irq_ptr->sch_token;
342                 fc |= QDIO_SIGA_QEBSM_FLAG;
343         }
344 again:
345         WARN_ON_ONCE((aob && queue_type(q) != QDIO_IQDIO_QFMT) ||
346                 (aob && fc != QDIO_SIGA_WRITEQ));
347         cc = do_siga_output(schid, q->mask, busy_bit, fc, laob);
348
349         /* hipersocket busy condition */
350         if (unlikely(*busy_bit)) {
351                 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
352                 retries++;
353
354                 if (!start_time) {
355                         start_time = get_clock();
356                         goto again;
357                 }
358                 if ((get_clock() - start_time) < QDIO_BUSY_BIT_PATIENCE)
359                         goto again;
360         }
361         if (retries) {
362                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
363                               "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
364                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
365         }
366         return cc;
367 }
368
369 static inline int qdio_siga_input(struct qdio_q *q)
370 {
371         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
372         unsigned int fc = QDIO_SIGA_READ;
373         int cc;
374
375         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
376         qperf_inc(q, siga_read);
377
378         if (is_qebsm(q)) {
379                 schid = q->irq_ptr->sch_token;
380                 fc |= QDIO_SIGA_QEBSM_FLAG;
381         }
382
383         cc = do_siga_input(schid, q->mask, fc);
384         if (unlikely(cc))
385                 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
386         return cc;
387 }
388
389 #define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
390 #define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
391
392 static inline void qdio_sync_queues(struct qdio_q *q)
393 {
394         /* PCI capable outbound queues will also be scanned so sync them too */
395         if (pci_out_supported(q))
396                 qdio_siga_sync_all(q);
397         else
398                 qdio_siga_sync_q(q);
399 }
400
401 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
402                         unsigned char *state)
403 {
404         if (need_siga_sync(q))
405                 qdio_siga_sync_q(q);
406         return get_buf_states(q, bufnr, state, 1, 0, 0);
407 }
408
409 static inline void qdio_stop_polling(struct qdio_q *q)
410 {
411         if (!q->u.in.polling)
412                 return;
413
414         q->u.in.polling = 0;
415         qperf_inc(q, stop_polling);
416
417         /* show the card that we are not polling anymore */
418         if (is_qebsm(q)) {
419                 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
420                                q->u.in.ack_count);
421                 q->u.in.ack_count = 0;
422         } else
423                 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
424 }
425
426 static inline void account_sbals(struct qdio_q *q, int count)
427 {
428         int pos = 0;
429
430         q->q_stats.nr_sbal_total += count;
431         if (count == QDIO_MAX_BUFFERS_MASK) {
432                 q->q_stats.nr_sbals[7]++;
433                 return;
434         }
435         while (count >>= 1)
436                 pos++;
437         q->q_stats.nr_sbals[pos]++;
438 }
439
440 static void process_buffer_error(struct qdio_q *q, int count)
441 {
442         unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
443                                         SLSB_P_OUTPUT_NOT_INIT;
444
445         q->qdio_error |= QDIO_ERROR_SLSB_STATE;
446
447         /* special handling for no target buffer empty */
448         if ((!q->is_input_q &&
449             (q->sbal[q->first_to_check]->element[15].sflags) == 0x10)) {
450                 qperf_inc(q, target_full);
451                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
452                               q->first_to_check);
453                 return;
454         }
455
456         DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
457         DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
458         DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
459         DBF_ERROR("F14:%2x F15:%2x",
460                   q->sbal[q->first_to_check]->element[14].sflags,
461                   q->sbal[q->first_to_check]->element[15].sflags);
462
463         /*
464          * Interrupts may be avoided as long as the error is present
465          * so change the buffer state immediately to avoid starvation.
466          */
467         set_buf_states(q, q->first_to_check, state, count);
468 }
469
470 static inline void inbound_primed(struct qdio_q *q, int count)
471 {
472         int new;
473
474         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
475
476         /* for QEBSM the ACK was already set by EQBS */
477         if (is_qebsm(q)) {
478                 if (!q->u.in.polling) {
479                         q->u.in.polling = 1;
480                         q->u.in.ack_count = count;
481                         q->u.in.ack_start = q->first_to_check;
482                         return;
483                 }
484
485                 /* delete the previous ACK's */
486                 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
487                                q->u.in.ack_count);
488                 q->u.in.ack_count = count;
489                 q->u.in.ack_start = q->first_to_check;
490                 return;
491         }
492
493         /*
494          * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
495          * or by the next inbound run.
496          */
497         new = add_buf(q->first_to_check, count - 1);
498         if (q->u.in.polling) {
499                 /* reset the previous ACK but first set the new one */
500                 set_buf_state(q, new, SLSB_P_INPUT_ACK);
501                 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
502         } else {
503                 q->u.in.polling = 1;
504                 set_buf_state(q, new, SLSB_P_INPUT_ACK);
505         }
506
507         q->u.in.ack_start = new;
508         count--;
509         if (!count)
510                 return;
511         /* need to change ALL buffers to get more interrupts */
512         set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
513 }
514
515 static int get_inbound_buffer_frontier(struct qdio_q *q)
516 {
517         int count, stop;
518         unsigned char state = 0;
519
520         q->timestamp = get_clock_fast();
521
522         /*
523          * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
524          * would return 0.
525          */
526         count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
527         stop = add_buf(q->first_to_check, count);
528
529         if (q->first_to_check == stop)
530                 goto out;
531
532         /*
533          * No siga sync here, as a PCI or we after a thin interrupt
534          * already sync'ed the queues.
535          */
536         count = get_buf_states(q, q->first_to_check, &state, count, 1, 0);
537         if (!count)
538                 goto out;
539
540         switch (state) {
541         case SLSB_P_INPUT_PRIMED:
542                 inbound_primed(q, count);
543                 q->first_to_check = add_buf(q->first_to_check, count);
544                 if (atomic_sub(count, &q->nr_buf_used) == 0)
545                         qperf_inc(q, inbound_queue_full);
546                 if (q->irq_ptr->perf_stat_enabled)
547                         account_sbals(q, count);
548                 break;
549         case SLSB_P_INPUT_ERROR:
550                 process_buffer_error(q, count);
551                 q->first_to_check = add_buf(q->first_to_check, count);
552                 atomic_sub(count, &q->nr_buf_used);
553                 if (q->irq_ptr->perf_stat_enabled)
554                         account_sbals_error(q, count);
555                 break;
556         case SLSB_CU_INPUT_EMPTY:
557         case SLSB_P_INPUT_NOT_INIT:
558         case SLSB_P_INPUT_ACK:
559                 if (q->irq_ptr->perf_stat_enabled)
560                         q->q_stats.nr_sbal_nop++;
561                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
562                 break;
563         default:
564                 BUG();
565         }
566 out:
567         return q->first_to_check;
568 }
569
570 static int qdio_inbound_q_moved(struct qdio_q *q)
571 {
572         int bufnr;
573
574         bufnr = get_inbound_buffer_frontier(q);
575
576         if ((bufnr != q->last_move) || q->qdio_error) {
577                 q->last_move = bufnr;
578                 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
579                         q->u.in.timestamp = get_clock();
580                 return 1;
581         } else
582                 return 0;
583 }
584
585 static inline int qdio_inbound_q_done(struct qdio_q *q)
586 {
587         unsigned char state = 0;
588
589         if (!atomic_read(&q->nr_buf_used))
590                 return 1;
591
592         if (need_siga_sync(q))
593                 qdio_siga_sync_q(q);
594         get_buf_state(q, q->first_to_check, &state, 0);
595
596         if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
597                 /* more work coming */
598                 return 0;
599
600         if (is_thinint_irq(q->irq_ptr))
601                 return 1;
602
603         /* don't poll under z/VM */
604         if (MACHINE_IS_VM)
605                 return 1;
606
607         /*
608          * At this point we know, that inbound first_to_check
609          * has (probably) not moved (see qdio_inbound_processing).
610          */
611         if (get_clock() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
612                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
613                               q->first_to_check);
614                 return 1;
615         } else
616                 return 0;
617 }
618
619 static inline int contains_aobs(struct qdio_q *q)
620 {
621         return !q->is_input_q && q->u.out.use_cq;
622 }
623
624 static inline void qdio_trace_aob(struct qdio_irq *irq, struct qdio_q *q,
625                                 int i, struct qaob *aob)
626 {
627         int tmp;
628
629         DBF_DEV_EVENT(DBF_INFO, irq, "AOB%d:%lx", i,
630                         (unsigned long) virt_to_phys(aob));
631         DBF_DEV_EVENT(DBF_INFO, irq, "RES00:%lx",
632                         (unsigned long) aob->res0[0]);
633         DBF_DEV_EVENT(DBF_INFO, irq, "RES01:%lx",
634                         (unsigned long) aob->res0[1]);
635         DBF_DEV_EVENT(DBF_INFO, irq, "RES02:%lx",
636                         (unsigned long) aob->res0[2]);
637         DBF_DEV_EVENT(DBF_INFO, irq, "RES03:%lx",
638                         (unsigned long) aob->res0[3]);
639         DBF_DEV_EVENT(DBF_INFO, irq, "RES04:%lx",
640                         (unsigned long) aob->res0[4]);
641         DBF_DEV_EVENT(DBF_INFO, irq, "RES05:%lx",
642                         (unsigned long) aob->res0[5]);
643         DBF_DEV_EVENT(DBF_INFO, irq, "RES1:%x", aob->res1);
644         DBF_DEV_EVENT(DBF_INFO, irq, "RES2:%x", aob->res2);
645         DBF_DEV_EVENT(DBF_INFO, irq, "RES3:%x", aob->res3);
646         DBF_DEV_EVENT(DBF_INFO, irq, "AORC:%u", aob->aorc);
647         DBF_DEV_EVENT(DBF_INFO, irq, "FLAGS:%u", aob->flags);
648         DBF_DEV_EVENT(DBF_INFO, irq, "CBTBS:%u", aob->cbtbs);
649         DBF_DEV_EVENT(DBF_INFO, irq, "SBC:%u", aob->sb_count);
650         for (tmp = 0; tmp < QDIO_MAX_ELEMENTS_PER_BUFFER; ++tmp) {
651                 DBF_DEV_EVENT(DBF_INFO, irq, "SBA%d:%lx", tmp,
652                                 (unsigned long) aob->sba[tmp]);
653                 DBF_DEV_EVENT(DBF_INFO, irq, "rSBA%d:%lx", tmp,
654                                 (unsigned long) q->sbal[i]->element[tmp].addr);
655                 DBF_DEV_EVENT(DBF_INFO, irq, "DC%d:%u", tmp, aob->dcount[tmp]);
656                 DBF_DEV_EVENT(DBF_INFO, irq, "rDC%d:%u", tmp,
657                                 q->sbal[i]->element[tmp].length);
658         }
659         DBF_DEV_EVENT(DBF_INFO, irq, "USER0:%lx", (unsigned long) aob->user0);
660         for (tmp = 0; tmp < 2; ++tmp) {
661                 DBF_DEV_EVENT(DBF_INFO, irq, "RES4%d:%lx", tmp,
662                         (unsigned long) aob->res4[tmp]);
663         }
664         DBF_DEV_EVENT(DBF_INFO, irq, "USER1:%lx", (unsigned long) aob->user1);
665         DBF_DEV_EVENT(DBF_INFO, irq, "USER2:%lx", (unsigned long) aob->user2);
666 }
667
668 static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
669 {
670         unsigned char state = 0;
671         int j, b = start;
672
673         if (!contains_aobs(q))
674                 return;
675
676         for (j = 0; j < count; ++j) {
677                 get_buf_state(q, b, &state, 0);
678                 if (state == SLSB_P_OUTPUT_PENDING) {
679                         struct qaob *aob = q->u.out.aobs[b];
680                         if (aob == NULL)
681                                 continue;
682
683                         BUG_ON(q->u.out.sbal_state == NULL);
684                         q->u.out.sbal_state[b].flags |=
685                                 QDIO_OUTBUF_STATE_FLAG_PENDING;
686                         q->u.out.aobs[b] = NULL;
687                 } else if (state == SLSB_P_OUTPUT_EMPTY) {
688                         BUG_ON(q->u.out.sbal_state == NULL);
689                         q->u.out.sbal_state[b].aob = NULL;
690                 }
691                 b = next_buf(b);
692         }
693 }
694
695 static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
696                                         int bufnr)
697 {
698         unsigned long phys_aob = 0;
699
700         if (!q->use_cq)
701                 goto out;
702
703         if (!q->aobs[bufnr]) {
704                 struct qaob *aob = qdio_allocate_aob();
705                 q->aobs[bufnr] = aob;
706         }
707         if (q->aobs[bufnr]) {
708                 BUG_ON(q->sbal_state == NULL);
709                 q->sbal_state[bufnr].flags = QDIO_OUTBUF_STATE_FLAG_NONE;
710                 q->sbal_state[bufnr].aob = q->aobs[bufnr];
711                 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
712                 phys_aob = virt_to_phys(q->aobs[bufnr]);
713                 BUG_ON(phys_aob & 0xFF);
714         }
715
716 out:
717         return phys_aob;
718 }
719
720 static void qdio_kick_handler(struct qdio_q *q)
721 {
722         int start = q->first_to_kick;
723         int end = q->first_to_check;
724         int count;
725
726         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
727                 return;
728
729         count = sub_buf(end, start);
730
731         if (q->is_input_q) {
732                 qperf_inc(q, inbound_handler);
733                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
734         } else {
735                 qperf_inc(q, outbound_handler);
736                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
737                               start, count);
738         }
739
740         qdio_handle_aobs(q, start, count);
741
742         q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
743                    q->irq_ptr->int_parm);
744
745         /* for the next time */
746         q->first_to_kick = end;
747         q->qdio_error = 0;
748 }
749
750 static void __qdio_inbound_processing(struct qdio_q *q)
751 {
752         qperf_inc(q, tasklet_inbound);
753
754         if (!qdio_inbound_q_moved(q))
755                 return;
756
757         qdio_kick_handler(q);
758
759         if (!qdio_inbound_q_done(q)) {
760                 /* means poll time is not yet over */
761                 qperf_inc(q, tasklet_inbound_resched);
762                 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
763                         tasklet_schedule(&q->tasklet);
764                         return;
765                 }
766         }
767
768         qdio_stop_polling(q);
769         /*
770          * We need to check again to not lose initiative after
771          * resetting the ACK state.
772          */
773         if (!qdio_inbound_q_done(q)) {
774                 qperf_inc(q, tasklet_inbound_resched2);
775                 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
776                         tasklet_schedule(&q->tasklet);
777         }
778 }
779
780 void qdio_inbound_processing(unsigned long data)
781 {
782         struct qdio_q *q = (struct qdio_q *)data;
783         __qdio_inbound_processing(q);
784 }
785
786 static int get_outbound_buffer_frontier(struct qdio_q *q)
787 {
788         int count, stop;
789         unsigned char state = 0;
790
791         q->timestamp = get_clock_fast();
792
793         if (need_siga_sync(q))
794                 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
795                     !pci_out_supported(q)) ||
796                     (queue_type(q) == QDIO_IQDIO_QFMT &&
797                     multicast_outbound(q)))
798                         qdio_siga_sync_q(q);
799
800         /*
801          * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
802          * would return 0.
803          */
804         count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
805         stop = add_buf(q->first_to_check, count);
806         if (q->first_to_check == stop)
807                 goto out;
808
809         count = get_buf_states(q, q->first_to_check, &state, count, 0, 1);
810         if (!count)
811                 goto out;
812
813         switch (state) {
814         case SLSB_P_OUTPUT_PENDING:
815                 BUG();
816         case SLSB_P_OUTPUT_EMPTY:
817                 /* the adapter got it */
818                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
819                         "out empty:%1d %02x", q->nr, count);
820
821                 atomic_sub(count, &q->nr_buf_used);
822                 q->first_to_check = add_buf(q->first_to_check, count);
823                 if (q->irq_ptr->perf_stat_enabled)
824                         account_sbals(q, count);
825
826                 break;
827         case SLSB_P_OUTPUT_ERROR:
828                 process_buffer_error(q, count);
829                 q->first_to_check = add_buf(q->first_to_check, count);
830                 atomic_sub(count, &q->nr_buf_used);
831                 if (q->irq_ptr->perf_stat_enabled)
832                         account_sbals_error(q, count);
833                 break;
834         case SLSB_CU_OUTPUT_PRIMED:
835                 /* the adapter has not fetched the output yet */
836                 if (q->irq_ptr->perf_stat_enabled)
837                         q->q_stats.nr_sbal_nop++;
838                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
839                               q->nr);
840                 break;
841         case SLSB_P_OUTPUT_NOT_INIT:
842         case SLSB_P_OUTPUT_HALTED:
843                 break;
844         default:
845                 BUG();
846         }
847
848 out:
849         return q->first_to_check;
850 }
851
852 /* all buffers processed? */
853 static inline int qdio_outbound_q_done(struct qdio_q *q)
854 {
855         return atomic_read(&q->nr_buf_used) == 0;
856 }
857
858 static inline int qdio_outbound_q_moved(struct qdio_q *q)
859 {
860         int bufnr;
861
862         bufnr = get_outbound_buffer_frontier(q);
863
864         if ((bufnr != q->last_move) || q->qdio_error) {
865                 q->last_move = bufnr;
866                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
867                 return 1;
868         } else
869                 return 0;
870 }
871
872 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned long aob)
873 {
874         int retries = 0, cc;
875         unsigned int busy_bit;
876
877         if (!need_siga_out(q))
878                 return 0;
879
880         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
881 retry:
882         qperf_inc(q, siga_write);
883
884         cc = qdio_siga_output(q, &busy_bit, aob);
885         switch (cc) {
886         case 0:
887                 break;
888         case 2:
889                 if (busy_bit) {
890                         while (++retries < QDIO_BUSY_BIT_RETRIES) {
891                                 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
892                                 goto retry;
893                         }
894                         DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
895                         cc |= QDIO_ERROR_SIGA_BUSY;
896                 } else
897                         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
898                 break;
899         case 1:
900         case 3:
901                 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
902                 break;
903         }
904         if (retries) {
905                 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
906                 DBF_ERROR("count:%u", retries);
907         }
908         return cc;
909 }
910
911 static void __qdio_outbound_processing(struct qdio_q *q)
912 {
913         qperf_inc(q, tasklet_outbound);
914         BUG_ON(atomic_read(&q->nr_buf_used) < 0);
915
916         if (qdio_outbound_q_moved(q))
917                 qdio_kick_handler(q);
918
919         if (queue_type(q) == QDIO_ZFCP_QFMT)
920                 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
921                         goto sched;
922
923         if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
924             (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
925                 goto sched;
926
927         if (q->u.out.pci_out_enabled)
928                 return;
929
930         /*
931          * Now we know that queue type is either qeth without pci enabled
932          * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
933          * is noticed and outbound_handler is called after some time.
934          */
935         if (qdio_outbound_q_done(q))
936                 del_timer(&q->u.out.timer);
937         else
938                 if (!timer_pending(&q->u.out.timer))
939                         mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
940         return;
941
942 sched:
943         if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
944                 return;
945         tasklet_schedule(&q->tasklet);
946 }
947
948 /* outbound tasklet */
949 void qdio_outbound_processing(unsigned long data)
950 {
951         struct qdio_q *q = (struct qdio_q *)data;
952         __qdio_outbound_processing(q);
953 }
954
955 void qdio_outbound_timer(unsigned long data)
956 {
957         struct qdio_q *q = (struct qdio_q *)data;
958
959         if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
960                 return;
961         tasklet_schedule(&q->tasklet);
962 }
963
964 static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
965 {
966         struct qdio_q *out;
967         int i;
968
969         if (!pci_out_supported(q))
970                 return;
971
972         for_each_output_queue(q->irq_ptr, out, i)
973                 if (!qdio_outbound_q_done(out))
974                         tasklet_schedule(&out->tasklet);
975 }
976
977 static void __tiqdio_inbound_processing(struct qdio_q *q)
978 {
979         qperf_inc(q, tasklet_inbound);
980         if (need_siga_sync(q) && need_siga_sync_after_ai(q))
981                 qdio_sync_queues(q);
982
983         /*
984          * The interrupt could be caused by a PCI request. Check the
985          * PCI capable outbound queues.
986          */
987         qdio_check_outbound_after_thinint(q);
988
989         if (!qdio_inbound_q_moved(q))
990                 return;
991
992         qdio_kick_handler(q);
993
994         if (!qdio_inbound_q_done(q)) {
995                 qperf_inc(q, tasklet_inbound_resched);
996                 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
997                         tasklet_schedule(&q->tasklet);
998                         return;
999                 }
1000         }
1001
1002         qdio_stop_polling(q);
1003         /*
1004          * We need to check again to not lose initiative after
1005          * resetting the ACK state.
1006          */
1007         if (!qdio_inbound_q_done(q)) {
1008                 qperf_inc(q, tasklet_inbound_resched2);
1009                 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
1010                         tasklet_schedule(&q->tasklet);
1011         }
1012 }
1013
1014 void tiqdio_inbound_processing(unsigned long data)
1015 {
1016         struct qdio_q *q = (struct qdio_q *)data;
1017         __tiqdio_inbound_processing(q);
1018 }
1019
1020 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
1021                                   enum qdio_irq_states state)
1022 {
1023         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
1024
1025         irq_ptr->state = state;
1026         mb();
1027 }
1028
1029 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
1030 {
1031         if (irb->esw.esw0.erw.cons) {
1032                 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
1033                 DBF_ERROR_HEX(irb, 64);
1034                 DBF_ERROR_HEX(irb->ecw, 64);
1035         }
1036 }
1037
1038 /* PCI interrupt handler */
1039 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
1040 {
1041         int i;
1042         struct qdio_q *q;
1043
1044         if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
1045                 return;
1046
1047         for_each_input_queue(irq_ptr, q, i) {
1048                 if (q->u.in.queue_start_poll) {
1049                         /* skip if polling is enabled or already in work */
1050                         if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1051                                      &q->u.in.queue_irq_state)) {
1052                                 qperf_inc(q, int_discarded);
1053                                 continue;
1054                         }
1055                         q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
1056                                                  q->irq_ptr->int_parm);
1057                 } else {
1058                         tasklet_schedule(&q->tasklet);
1059                 }
1060         }
1061
1062         if (!pci_out_supported(q))
1063                 return;
1064
1065         for_each_output_queue(irq_ptr, q, i) {
1066                 if (qdio_outbound_q_done(q))
1067                         continue;
1068                 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
1069                         qdio_siga_sync_q(q);
1070                 tasklet_schedule(&q->tasklet);
1071         }
1072 }
1073
1074 static void qdio_handle_activate_check(struct ccw_device *cdev,
1075                                 unsigned long intparm, int cstat, int dstat)
1076 {
1077         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1078         struct qdio_q *q;
1079         int count;
1080
1081         DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
1082         DBF_ERROR("intp :%lx", intparm);
1083         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1084
1085         if (irq_ptr->nr_input_qs) {
1086                 q = irq_ptr->input_qs[0];
1087         } else if (irq_ptr->nr_output_qs) {
1088                 q = irq_ptr->output_qs[0];
1089         } else {
1090                 dump_stack();
1091                 goto no_handler;
1092         }
1093
1094         count = sub_buf(q->first_to_check, q->first_to_kick);
1095         q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
1096                    q->nr, q->first_to_kick, count, irq_ptr->int_parm);
1097 no_handler:
1098         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1099 }
1100
1101 static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1102                                       int dstat)
1103 {
1104         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1105
1106         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
1107
1108         if (cstat)
1109                 goto error;
1110         if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1111                 goto error;
1112         if (!(dstat & DEV_STAT_DEV_END))
1113                 goto error;
1114         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1115         return;
1116
1117 error:
1118         DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1119         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1120         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1121 }
1122
1123 /* qdio interrupt handler */
1124 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1125                       struct irb *irb)
1126 {
1127         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1128         int cstat, dstat;
1129
1130         if (!intparm || !irq_ptr) {
1131                 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
1132                 return;
1133         }
1134
1135         if (irq_ptr->perf_stat_enabled)
1136                 irq_ptr->perf_stat.qdio_int++;
1137
1138         if (IS_ERR(irb)) {
1139                 switch (PTR_ERR(irb)) {
1140                 case -EIO:
1141                         DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1142                         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1143                         wake_up(&cdev->private->wait_q);
1144                         return;
1145                 default:
1146                         WARN_ON(1);
1147                         return;
1148                 }
1149         }
1150         qdio_irq_check_sense(irq_ptr, irb);
1151         cstat = irb->scsw.cmd.cstat;
1152         dstat = irb->scsw.cmd.dstat;
1153
1154         switch (irq_ptr->state) {
1155         case QDIO_IRQ_STATE_INACTIVE:
1156                 qdio_establish_handle_irq(cdev, cstat, dstat);
1157                 break;
1158         case QDIO_IRQ_STATE_CLEANUP:
1159                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1160                 break;
1161         case QDIO_IRQ_STATE_ESTABLISHED:
1162         case QDIO_IRQ_STATE_ACTIVE:
1163                 if (cstat & SCHN_STAT_PCI) {
1164                         qdio_int_handler_pci(irq_ptr);
1165                         return;
1166                 }
1167                 if (cstat || dstat)
1168                         qdio_handle_activate_check(cdev, intparm, cstat,
1169                                                    dstat);
1170                 break;
1171         case QDIO_IRQ_STATE_STOPPED:
1172                 break;
1173         default:
1174                 WARN_ON(1);
1175         }
1176         wake_up(&cdev->private->wait_q);
1177 }
1178
1179 /**
1180  * qdio_get_ssqd_desc - get qdio subchannel description
1181  * @cdev: ccw device to get description for
1182  * @data: where to store the ssqd
1183  *
1184  * Returns 0 or an error code. The results of the chsc are stored in the
1185  * specified structure.
1186  */
1187 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1188                        struct qdio_ssqd_desc *data)
1189 {
1190
1191         if (!cdev || !cdev->private)
1192                 return -EINVAL;
1193
1194         DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
1195         return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
1196 }
1197 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1198
1199 static void qdio_shutdown_queues(struct ccw_device *cdev)
1200 {
1201         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1202         struct qdio_q *q;
1203         int i;
1204
1205         for_each_input_queue(irq_ptr, q, i)
1206                 tasklet_kill(&q->tasklet);
1207
1208         for_each_output_queue(irq_ptr, q, i) {
1209                 del_timer(&q->u.out.timer);
1210                 tasklet_kill(&q->tasklet);
1211         }
1212 }
1213
1214 /**
1215  * qdio_shutdown - shut down a qdio subchannel
1216  * @cdev: associated ccw device
1217  * @how: use halt or clear to shutdown
1218  */
1219 int qdio_shutdown(struct ccw_device *cdev, int how)
1220 {
1221         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1222         int rc;
1223         unsigned long flags;
1224
1225         if (!irq_ptr)
1226                 return -ENODEV;
1227
1228         BUG_ON(irqs_disabled());
1229         DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1230
1231         mutex_lock(&irq_ptr->setup_mutex);
1232         /*
1233          * Subchannel was already shot down. We cannot prevent being called
1234          * twice since cio may trigger a shutdown asynchronously.
1235          */
1236         if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1237                 mutex_unlock(&irq_ptr->setup_mutex);
1238                 return 0;
1239         }
1240
1241         /*
1242          * Indicate that the device is going down. Scheduling the queue
1243          * tasklets is forbidden from here on.
1244          */
1245         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1246
1247         tiqdio_remove_input_queues(irq_ptr);
1248         qdio_shutdown_queues(cdev);
1249         qdio_shutdown_debug_entries(irq_ptr, cdev);
1250
1251         /* cleanup subchannel */
1252         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1253
1254         if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1255                 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1256         else
1257                 /* default behaviour is halt */
1258                 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1259         if (rc) {
1260                 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1261                 DBF_ERROR("rc:%4d", rc);
1262                 goto no_cleanup;
1263         }
1264
1265         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1266         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1267         wait_event_interruptible_timeout(cdev->private->wait_q,
1268                 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1269                 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1270                 10 * HZ);
1271         spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1272
1273 no_cleanup:
1274         qdio_shutdown_thinint(irq_ptr);
1275
1276         /* restore interrupt handler */
1277         if ((void *)cdev->handler == (void *)qdio_int_handler)
1278                 cdev->handler = irq_ptr->orig_handler;
1279         spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1280
1281         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1282         mutex_unlock(&irq_ptr->setup_mutex);
1283         if (rc)
1284                 return rc;
1285         return 0;
1286 }
1287 EXPORT_SYMBOL_GPL(qdio_shutdown);
1288
1289 /**
1290  * qdio_free - free data structures for a qdio subchannel
1291  * @cdev: associated ccw device
1292  */
1293 int qdio_free(struct ccw_device *cdev)
1294 {
1295         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1296
1297         if (!irq_ptr)
1298                 return -ENODEV;
1299
1300         DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
1301         mutex_lock(&irq_ptr->setup_mutex);
1302
1303         if (irq_ptr->debug_area != NULL) {
1304                 debug_unregister(irq_ptr->debug_area);
1305                 irq_ptr->debug_area = NULL;
1306         }
1307         cdev->private->qdio_data = NULL;
1308         mutex_unlock(&irq_ptr->setup_mutex);
1309
1310         qdio_release_memory(irq_ptr);
1311         return 0;
1312 }
1313 EXPORT_SYMBOL_GPL(qdio_free);
1314
1315 /**
1316  * qdio_allocate - allocate qdio queues and associated data
1317  * @init_data: initialization data
1318  */
1319 int qdio_allocate(struct qdio_initialize *init_data)
1320 {
1321         struct qdio_irq *irq_ptr;
1322
1323         DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
1324
1325         if ((init_data->no_input_qs && !init_data->input_handler) ||
1326             (init_data->no_output_qs && !init_data->output_handler))
1327                 return -EINVAL;
1328
1329         if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1330             (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1331                 return -EINVAL;
1332
1333         if ((!init_data->input_sbal_addr_array) ||
1334             (!init_data->output_sbal_addr_array))
1335                 return -EINVAL;
1336
1337         /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1338         irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1339         if (!irq_ptr)
1340                 goto out_err;
1341
1342         mutex_init(&irq_ptr->setup_mutex);
1343         qdio_allocate_dbf(init_data, irq_ptr);
1344
1345         /*
1346          * Allocate a page for the chsc calls in qdio_establish.
1347          * Must be pre-allocated since a zfcp recovery will call
1348          * qdio_establish. In case of low memory and swap on a zfcp disk
1349          * we may not be able to allocate memory otherwise.
1350          */
1351         irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1352         if (!irq_ptr->chsc_page)
1353                 goto out_rel;
1354
1355         /* qdr is used in ccw1.cda which is u32 */
1356         irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1357         if (!irq_ptr->qdr)
1358                 goto out_rel;
1359         WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1360
1361         if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1362                              init_data->no_output_qs))
1363                 goto out_rel;
1364
1365         init_data->cdev->private->qdio_data = irq_ptr;
1366         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1367         return 0;
1368 out_rel:
1369         qdio_release_memory(irq_ptr);
1370 out_err:
1371         return -ENOMEM;
1372 }
1373 EXPORT_SYMBOL_GPL(qdio_allocate);
1374
1375 static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1376 {
1377         struct qdio_q *q = irq_ptr->input_qs[0];
1378         int i, use_cq = 0;
1379
1380         if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1381                 use_cq = 1;
1382
1383         for_each_output_queue(irq_ptr, q, i) {
1384                 if (use_cq) {
1385                         if (qdio_enable_async_operation(&q->u.out) < 0) {
1386                                 use_cq = 0;
1387                                 continue;
1388                         }
1389                 } else
1390                         qdio_disable_async_operation(&q->u.out);
1391         }
1392         DBF_EVENT("use_cq:%d", use_cq);
1393 }
1394
1395 /**
1396  * qdio_establish - establish queues on a qdio subchannel
1397  * @init_data: initialization data
1398  */
1399 int qdio_establish(struct qdio_initialize *init_data)
1400 {
1401         struct qdio_irq *irq_ptr;
1402         struct ccw_device *cdev = init_data->cdev;
1403         unsigned long saveflags;
1404         int rc;
1405
1406         DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
1407
1408         irq_ptr = cdev->private->qdio_data;
1409         if (!irq_ptr)
1410                 return -ENODEV;
1411
1412         if (cdev->private->state != DEV_STATE_ONLINE)
1413                 return -EINVAL;
1414
1415         mutex_lock(&irq_ptr->setup_mutex);
1416         qdio_setup_irq(init_data);
1417
1418         rc = qdio_establish_thinint(irq_ptr);
1419         if (rc) {
1420                 mutex_unlock(&irq_ptr->setup_mutex);
1421                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1422                 return rc;
1423         }
1424
1425         /* establish q */
1426         irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1427         irq_ptr->ccw.flags = CCW_FLAG_SLI;
1428         irq_ptr->ccw.count = irq_ptr->equeue.count;
1429         irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1430
1431         spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1432         ccw_device_set_options_mask(cdev, 0);
1433
1434         rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1435         if (rc) {
1436                 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1437                 DBF_ERROR("rc:%4x", rc);
1438         }
1439         spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1440
1441         if (rc) {
1442                 mutex_unlock(&irq_ptr->setup_mutex);
1443                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1444                 return rc;
1445         }
1446
1447         wait_event_interruptible_timeout(cdev->private->wait_q,
1448                 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1449                 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1450
1451         if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1452                 mutex_unlock(&irq_ptr->setup_mutex);
1453                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1454                 return -EIO;
1455         }
1456
1457         qdio_setup_ssqd_info(irq_ptr);
1458         DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
1459
1460         qdio_detect_hsicq(irq_ptr);
1461
1462         /* qebsm is now setup if available, initialize buffer states */
1463         qdio_init_buf_states(irq_ptr);
1464
1465         mutex_unlock(&irq_ptr->setup_mutex);
1466         qdio_print_subchannel_info(irq_ptr, cdev);
1467         qdio_setup_debug_entries(irq_ptr, cdev);
1468         return 0;
1469 }
1470 EXPORT_SYMBOL_GPL(qdio_establish);
1471
1472 /**
1473  * qdio_activate - activate queues on a qdio subchannel
1474  * @cdev: associated cdev
1475  */
1476 int qdio_activate(struct ccw_device *cdev)
1477 {
1478         struct qdio_irq *irq_ptr;
1479         int rc;
1480         unsigned long saveflags;
1481
1482         DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
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                 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1509                 DBF_ERROR("rc:%4x", rc);
1510         }
1511         spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1512
1513         if (rc)
1514                 goto out;
1515
1516         if (is_thinint_irq(irq_ptr))
1517                 tiqdio_add_input_queues(irq_ptr);
1518
1519         /* wait for subchannel to become active */
1520         msleep(5);
1521
1522         switch (irq_ptr->state) {
1523         case QDIO_IRQ_STATE_STOPPED:
1524         case QDIO_IRQ_STATE_ERR:
1525                 rc = -EIO;
1526                 break;
1527         default:
1528                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1529                 rc = 0;
1530         }
1531 out:
1532         mutex_unlock(&irq_ptr->setup_mutex);
1533         return rc;
1534 }
1535 EXPORT_SYMBOL_GPL(qdio_activate);
1536
1537 static inline int buf_in_between(int bufnr, int start, int count)
1538 {
1539         int end = add_buf(start, count);
1540
1541         if (end > start) {
1542                 if (bufnr >= start && bufnr < end)
1543                         return 1;
1544                 else
1545                         return 0;
1546         }
1547
1548         /* wrap-around case */
1549         if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1550             (bufnr < end))
1551                 return 1;
1552         else
1553                 return 0;
1554 }
1555
1556 /**
1557  * handle_inbound - reset processed input buffers
1558  * @q: queue containing the buffers
1559  * @callflags: flags
1560  * @bufnr: first buffer to process
1561  * @count: how many buffers are emptied
1562  */
1563 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1564                           int bufnr, int count)
1565 {
1566         int used, diff;
1567
1568         qperf_inc(q, inbound_call);
1569
1570         if (!q->u.in.polling)
1571                 goto set;
1572
1573         /* protect against stop polling setting an ACK for an emptied slsb */
1574         if (count == QDIO_MAX_BUFFERS_PER_Q) {
1575                 /* overwriting everything, just delete polling status */
1576                 q->u.in.polling = 0;
1577                 q->u.in.ack_count = 0;
1578                 goto set;
1579         } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1580                 if (is_qebsm(q)) {
1581                         /* partial overwrite, just update ack_start */
1582                         diff = add_buf(bufnr, count);
1583                         diff = sub_buf(diff, q->u.in.ack_start);
1584                         q->u.in.ack_count -= diff;
1585                         if (q->u.in.ack_count <= 0) {
1586                                 q->u.in.polling = 0;
1587                                 q->u.in.ack_count = 0;
1588                                 goto set;
1589                         }
1590                         q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1591                 }
1592                 else
1593                         /* the only ACK will be deleted, so stop polling */
1594                         q->u.in.polling = 0;
1595         }
1596
1597 set:
1598         count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1599
1600         used = atomic_add_return(count, &q->nr_buf_used) - count;
1601         BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1602
1603         if (need_siga_in(q))
1604                 return qdio_siga_input(q);
1605
1606         return 0;
1607 }
1608
1609 /**
1610  * handle_outbound - process filled outbound buffers
1611  * @q: queue containing the buffers
1612  * @callflags: flags
1613  * @bufnr: first buffer to process
1614  * @count: how many buffers are filled
1615  */
1616 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1617                            int bufnr, int count)
1618 {
1619         unsigned char state = 0;
1620         int used, rc = 0;
1621
1622         qperf_inc(q, outbound_call);
1623
1624         count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1625         used = atomic_add_return(count, &q->nr_buf_used);
1626         BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1627
1628         if (used == QDIO_MAX_BUFFERS_PER_Q)
1629                 qperf_inc(q, outbound_queue_full);
1630
1631         if (callflags & QDIO_FLAG_PCI_OUT) {
1632                 q->u.out.pci_out_enabled = 1;
1633                 qperf_inc(q, pci_request_int);
1634         } else
1635                 q->u.out.pci_out_enabled = 0;
1636
1637         if (queue_type(q) == QDIO_IQDIO_QFMT) {
1638                 unsigned long phys_aob = 0;
1639
1640                 /* One SIGA-W per buffer required for unicast HSI */
1641                 WARN_ON_ONCE(count > 1 && !multicast_outbound(q));
1642
1643                 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1644
1645                 rc = qdio_kick_outbound_q(q, phys_aob);
1646         } else if (need_siga_sync(q)) {
1647                 rc = qdio_siga_sync_q(q);
1648         } else {
1649                 /* try to fast requeue buffers */
1650                 get_buf_state(q, prev_buf(bufnr), &state, 0);
1651                 if (state != SLSB_CU_OUTPUT_PRIMED)
1652                         rc = qdio_kick_outbound_q(q, 0);
1653                 else
1654                         qperf_inc(q, fast_requeue);
1655         }
1656
1657         /* in case of SIGA errors we must process the error immediately */
1658         if (used >= q->u.out.scan_threshold || rc)
1659                 tasklet_schedule(&q->tasklet);
1660         else
1661                 /* free the SBALs in case of no further traffic */
1662                 if (!timer_pending(&q->u.out.timer))
1663                         mod_timer(&q->u.out.timer, jiffies + HZ);
1664         return rc;
1665 }
1666
1667 /**
1668  * do_QDIO - process input or output buffers
1669  * @cdev: associated ccw_device for the qdio subchannel
1670  * @callflags: input or output and special flags from the program
1671  * @q_nr: queue number
1672  * @bufnr: buffer number
1673  * @count: how many buffers to process
1674  */
1675 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1676             int q_nr, unsigned int bufnr, unsigned int count)
1677 {
1678         struct qdio_irq *irq_ptr;
1679
1680
1681         if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1682                 return -EINVAL;
1683
1684         irq_ptr = cdev->private->qdio_data;
1685         if (!irq_ptr)
1686                 return -ENODEV;
1687
1688         DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1689                       "do%02x b:%02x c:%02x", callflags, bufnr, count);
1690
1691         if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1692                 return -EBUSY;
1693         if (!count)
1694                 return 0;
1695         if (callflags & QDIO_FLAG_SYNC_INPUT)
1696                 return handle_inbound(irq_ptr->input_qs[q_nr],
1697                                       callflags, bufnr, count);
1698         else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1699                 return handle_outbound(irq_ptr->output_qs[q_nr],
1700                                        callflags, bufnr, count);
1701         return -EINVAL;
1702 }
1703 EXPORT_SYMBOL_GPL(do_QDIO);
1704
1705 /**
1706  * qdio_start_irq - process input buffers
1707  * @cdev: associated ccw_device for the qdio subchannel
1708  * @nr: input queue number
1709  *
1710  * Return codes
1711  *   0 - success
1712  *   1 - irqs not started since new data is available
1713  */
1714 int qdio_start_irq(struct ccw_device *cdev, int nr)
1715 {
1716         struct qdio_q *q;
1717         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1718
1719         if (!irq_ptr)
1720                 return -ENODEV;
1721         q = irq_ptr->input_qs[nr];
1722
1723         WARN_ON(queue_irqs_enabled(q));
1724
1725         if (!shared_ind(q))
1726                 xchg(q->irq_ptr->dsci, 0);
1727
1728         qdio_stop_polling(q);
1729         clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1730
1731         /*
1732          * We need to check again to not lose initiative after
1733          * resetting the ACK state.
1734          */
1735         if (!shared_ind(q) && *q->irq_ptr->dsci)
1736                 goto rescan;
1737         if (!qdio_inbound_q_done(q))
1738                 goto rescan;
1739         return 0;
1740
1741 rescan:
1742         if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1743                              &q->u.in.queue_irq_state))
1744                 return 0;
1745         else
1746                 return 1;
1747
1748 }
1749 EXPORT_SYMBOL(qdio_start_irq);
1750
1751 /**
1752  * qdio_get_next_buffers - process input buffers
1753  * @cdev: associated ccw_device for the qdio subchannel
1754  * @nr: input queue number
1755  * @bufnr: first filled buffer number
1756  * @error: buffers are in error state
1757  *
1758  * Return codes
1759  *   < 0 - error
1760  *   = 0 - no new buffers found
1761  *   > 0 - number of processed buffers
1762  */
1763 int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1764                           int *error)
1765 {
1766         struct qdio_q *q;
1767         int start, end;
1768         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1769
1770         if (!irq_ptr)
1771                 return -ENODEV;
1772         q = irq_ptr->input_qs[nr];
1773         WARN_ON(queue_irqs_enabled(q));
1774
1775         /*
1776          * Cannot rely on automatic sync after interrupt since queues may
1777          * also be examined without interrupt.
1778          */
1779         if (need_siga_sync(q))
1780                 qdio_sync_queues(q);
1781
1782         /* check the PCI capable outbound queues. */
1783         qdio_check_outbound_after_thinint(q);
1784
1785         if (!qdio_inbound_q_moved(q))
1786                 return 0;
1787
1788         /* Note: upper-layer MUST stop processing immediately here ... */
1789         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1790                 return -EIO;
1791
1792         start = q->first_to_kick;
1793         end = q->first_to_check;
1794         *bufnr = start;
1795         *error = q->qdio_error;
1796
1797         /* for the next time */
1798         q->first_to_kick = end;
1799         q->qdio_error = 0;
1800         return sub_buf(end, start);
1801 }
1802 EXPORT_SYMBOL(qdio_get_next_buffers);
1803
1804 /**
1805  * qdio_stop_irq - disable interrupt processing for the device
1806  * @cdev: associated ccw_device for the qdio subchannel
1807  * @nr: input queue number
1808  *
1809  * Return codes
1810  *   0 - interrupts were already disabled
1811  *   1 - interrupts successfully disabled
1812  */
1813 int qdio_stop_irq(struct ccw_device *cdev, int nr)
1814 {
1815         struct qdio_q *q;
1816         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1817
1818         if (!irq_ptr)
1819                 return -ENODEV;
1820         q = irq_ptr->input_qs[nr];
1821
1822         if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1823                              &q->u.in.queue_irq_state))
1824                 return 0;
1825         else
1826                 return 1;
1827 }
1828 EXPORT_SYMBOL(qdio_stop_irq);
1829
1830 static int __init init_QDIO(void)
1831 {
1832         int rc;
1833
1834         rc = qdio_debug_init();
1835         if (rc)
1836                 return rc;
1837         rc = qdio_setup_init();
1838         if (rc)
1839                 goto out_debug;
1840         rc = tiqdio_allocate_memory();
1841         if (rc)
1842                 goto out_cache;
1843         rc = tiqdio_register_thinints();
1844         if (rc)
1845                 goto out_ti;
1846         return 0;
1847
1848 out_ti:
1849         tiqdio_free_memory();
1850 out_cache:
1851         qdio_setup_exit();
1852 out_debug:
1853         qdio_debug_exit();
1854         return rc;
1855 }
1856
1857 static void __exit exit_QDIO(void)
1858 {
1859         tiqdio_unregister_thinints();
1860         tiqdio_free_memory();
1861         qdio_setup_exit();
1862         qdio_debug_exit();
1863 }
1864
1865 module_init(init_QDIO);
1866 module_exit(exit_QDIO);