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