OSDN Git Service

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