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