s390/qdio: track time of last data IRQ for each device
[linux-2.6-microblaze.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_check, 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_check, 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         /* Ensure that all preceding changes to the SBALs are visible: */
258         mb();
259
260         for (i = 0; i < count; i++) {
261                 WRITE_ONCE(q->slsb.val[bufnr], state);
262                 bufnr = next_buf(bufnr);
263         }
264
265         /* Make our SLSB changes visible: */
266         mb();
267
268         return count;
269 }
270
271 static inline int set_buf_state(struct qdio_q *q, int bufnr,
272                                 unsigned char state)
273 {
274         return set_buf_states(q, bufnr, state, 1);
275 }
276
277 /* set slsb states to initial state */
278 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
279 {
280         struct qdio_q *q;
281         int i;
282
283         for_each_input_queue(irq_ptr, q, i)
284                 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
285                                QDIO_MAX_BUFFERS_PER_Q);
286         for_each_output_queue(irq_ptr, q, i)
287                 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
288                                QDIO_MAX_BUFFERS_PER_Q);
289 }
290
291 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
292                           unsigned int input)
293 {
294         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
295         unsigned int fc = QDIO_SIGA_SYNC;
296         int cc;
297
298         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
299         qperf_inc(q, siga_sync);
300
301         if (is_qebsm(q)) {
302                 schid = q->irq_ptr->sch_token;
303                 fc |= QDIO_SIGA_QEBSM_FLAG;
304         }
305
306         cc = do_siga_sync(schid, output, input, fc);
307         if (unlikely(cc))
308                 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
309         return (cc) ? -EIO : 0;
310 }
311
312 static inline int qdio_siga_sync_q(struct qdio_q *q)
313 {
314         if (q->is_input_q)
315                 return qdio_siga_sync(q, 0, q->mask);
316         else
317                 return qdio_siga_sync(q, q->mask, 0);
318 }
319
320 static int qdio_siga_output(struct qdio_q *q, unsigned int count,
321                             unsigned int *busy_bit, unsigned long aob)
322 {
323         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
324         unsigned int fc = QDIO_SIGA_WRITE;
325         u64 start_time = 0;
326         int retries = 0, cc;
327
328         if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
329                 if (count > 1)
330                         fc = QDIO_SIGA_WRITEM;
331                 else if (aob)
332                         fc = QDIO_SIGA_WRITEQ;
333         }
334
335         if (is_qebsm(q)) {
336                 schid = q->irq_ptr->sch_token;
337                 fc |= QDIO_SIGA_QEBSM_FLAG;
338         }
339 again:
340         cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
341
342         /* hipersocket busy condition */
343         if (unlikely(*busy_bit)) {
344                 retries++;
345
346                 if (!start_time) {
347                         start_time = get_tod_clock_fast();
348                         goto again;
349                 }
350                 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
351                         goto again;
352         }
353         if (retries) {
354                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
355                               "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
356                 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
357         }
358         return cc;
359 }
360
361 static inline int qdio_siga_input(struct qdio_q *q)
362 {
363         unsigned long schid = *((u32 *) &q->irq_ptr->schid);
364         unsigned int fc = QDIO_SIGA_READ;
365         int cc;
366
367         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
368         qperf_inc(q, siga_read);
369
370         if (is_qebsm(q)) {
371                 schid = q->irq_ptr->sch_token;
372                 fc |= QDIO_SIGA_QEBSM_FLAG;
373         }
374
375         cc = do_siga_input(schid, q->mask, fc);
376         if (unlikely(cc))
377                 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
378         return (cc) ? -EIO : 0;
379 }
380
381 #define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
382 #define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
383
384 static inline void qdio_sync_queues(struct qdio_q *q)
385 {
386         /* PCI capable outbound queues will also be scanned so sync them too */
387         if (pci_out_supported(q->irq_ptr))
388                 qdio_siga_sync_all(q);
389         else
390                 qdio_siga_sync_q(q);
391 }
392
393 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
394                         unsigned char *state)
395 {
396         if (need_siga_sync(q))
397                 qdio_siga_sync_q(q);
398         return get_buf_state(q, bufnr, state, 0);
399 }
400
401 static inline void qdio_stop_polling(struct qdio_q *q)
402 {
403         if (!q->u.in.batch_count)
404                 return;
405
406         qperf_inc(q, stop_polling);
407
408         /* show the card that we are not polling anymore */
409         set_buf_states(q, q->u.in.batch_start, SLSB_P_INPUT_NOT_INIT,
410                        q->u.in.batch_count);
411         q->u.in.batch_count = 0;
412 }
413
414 static inline void account_sbals(struct qdio_q *q, unsigned int count)
415 {
416         q->q_stats.nr_sbal_total += count;
417         q->q_stats.nr_sbals[ilog2(count)]++;
418 }
419
420 static void process_buffer_error(struct qdio_q *q, unsigned int start,
421                                  int count)
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[start]->element[15].sflags == 0x10) {
428                 qperf_inc(q, target_full);
429                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
430                 return;
431         }
432
433         DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
434         DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
435         DBF_ERROR("FTC:%3d C:%3d", start, count);
436         DBF_ERROR("F14:%2x F15:%2x",
437                   q->sbal[start]->element[14].sflags,
438                   q->sbal[start]->element[15].sflags);
439 }
440
441 static inline void inbound_handle_work(struct qdio_q *q, unsigned int start,
442                                        int count, bool auto_ack)
443 {
444         /* ACK the newest SBAL: */
445         if (!auto_ack)
446                 set_buf_state(q, add_buf(start, count - 1), SLSB_P_INPUT_ACK);
447
448         if (!q->u.in.batch_count)
449                 q->u.in.batch_start = start;
450         q->u.in.batch_count += count;
451 }
452
453 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
454 {
455         unsigned char state = 0;
456         int count;
457
458         q->timestamp = get_tod_clock_fast();
459
460         count = atomic_read(&q->nr_buf_used);
461         if (!count)
462                 return 0;
463
464         /*
465          * No siga sync here, as a PCI or we after a thin interrupt
466          * already sync'ed the queues.
467          */
468         count = get_buf_states(q, start, &state, count, 1, 0);
469         if (!count)
470                 return 0;
471
472         switch (state) {
473         case SLSB_P_INPUT_PRIMED:
474                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr,
475                               count);
476
477                 inbound_handle_work(q, start, count, is_qebsm(q));
478                 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
479                         qperf_inc(q, inbound_queue_full);
480                 if (q->irq_ptr->perf_stat_enabled)
481                         account_sbals(q, count);
482                 return count;
483         case SLSB_P_INPUT_ERROR:
484                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in err:%1d %02x", q->nr,
485                               count);
486
487                 process_buffer_error(q, start, count);
488                 inbound_handle_work(q, start, count, false);
489                 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
490                         qperf_inc(q, inbound_queue_full);
491                 if (q->irq_ptr->perf_stat_enabled)
492                         account_sbals_error(q, count);
493                 return count;
494         case SLSB_CU_INPUT_EMPTY:
495                 if (q->irq_ptr->perf_stat_enabled)
496                         q->q_stats.nr_sbal_nop++;
497                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
498                               q->nr, start);
499                 return 0;
500         case SLSB_P_INPUT_NOT_INIT:
501         case SLSB_P_INPUT_ACK:
502                 /* We should never see this state, throw a WARN: */
503         default:
504                 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
505                               "found state %#x at index %u on queue %u\n",
506                               state, start, q->nr);
507                 return 0;
508         }
509 }
510
511 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
512 {
513         unsigned char state = 0;
514
515         if (!atomic_read(&q->nr_buf_used))
516                 return 1;
517
518         if (need_siga_sync(q))
519                 qdio_siga_sync_q(q);
520         get_buf_state(q, start, &state, 0);
521
522         if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
523                 /* more work coming */
524                 return 0;
525
526         return 1;
527 }
528
529 static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
530                                         int bufnr)
531 {
532         unsigned long phys_aob = 0;
533
534         if (!q->aobs[bufnr]) {
535                 struct qaob *aob = qdio_allocate_aob();
536                 q->aobs[bufnr] = aob;
537         }
538         if (q->aobs[bufnr]) {
539                 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
540                 phys_aob = virt_to_phys(q->aobs[bufnr]);
541                 WARN_ON_ONCE(phys_aob & 0xFF);
542         }
543
544         q->sbal_state[bufnr].flags = 0;
545         return phys_aob;
546 }
547
548 static void qdio_kick_handler(struct qdio_q *q, unsigned int start,
549                               unsigned int count)
550 {
551         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
552                 return;
553
554         if (q->is_input_q) {
555                 qperf_inc(q, inbound_handler);
556                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
557         } else {
558                 qperf_inc(q, outbound_handler);
559                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
560                               start, count);
561         }
562
563         q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
564                    q->irq_ptr->int_parm);
565
566         /* for the next time */
567         q->qdio_error = 0;
568 }
569
570 static inline int qdio_tasklet_schedule(struct qdio_q *q)
571 {
572         if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
573                 tasklet_schedule(&q->u.out.tasklet);
574                 return 0;
575         }
576         return -EPERM;
577 }
578
579 static void qdio_check_pending(struct qdio_q *q, unsigned int index)
580 {
581         unsigned char state;
582
583         if (get_buf_state(q, index, &state, 0) > 0 &&
584             state == SLSB_P_OUTPUT_PENDING &&
585             q->u.out.aobs[index]) {
586                 q->u.out.sbal_state[index].flags |=
587                         QDIO_OUTBUF_STATE_FLAG_PENDING;
588                 q->u.out.aobs[index] = NULL;
589         }
590 }
591
592 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
593 {
594         unsigned char state = 0;
595         int count;
596
597         q->timestamp = get_tod_clock_fast();
598
599         if (need_siga_sync(q))
600                 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
601                     !pci_out_supported(q->irq_ptr)) ||
602                     (queue_type(q) == QDIO_IQDIO_QFMT &&
603                     multicast_outbound(q)))
604                         qdio_siga_sync_q(q);
605
606         count = atomic_read(&q->nr_buf_used);
607         if (!count)
608                 return 0;
609
610         count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
611         if (!count)
612                 return 0;
613
614         switch (state) {
615         case SLSB_P_OUTPUT_EMPTY:
616         case SLSB_P_OUTPUT_PENDING:
617                 /* the adapter got it */
618                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
619                         "out empty:%1d %02x", q->nr, count);
620
621                 atomic_sub(count, &q->nr_buf_used);
622                 if (q->irq_ptr->perf_stat_enabled)
623                         account_sbals(q, count);
624                 return count;
625         case SLSB_P_OUTPUT_ERROR:
626                 process_buffer_error(q, start, count);
627                 atomic_sub(count, &q->nr_buf_used);
628                 if (q->irq_ptr->perf_stat_enabled)
629                         account_sbals_error(q, count);
630                 return count;
631         case SLSB_CU_OUTPUT_PRIMED:
632                 /* the adapter has not fetched the output yet */
633                 if (q->irq_ptr->perf_stat_enabled)
634                         q->q_stats.nr_sbal_nop++;
635                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
636                               q->nr);
637                 return 0;
638         case SLSB_P_OUTPUT_HALTED:
639                 return 0;
640         case SLSB_P_OUTPUT_NOT_INIT:
641                 /* We should never see this state, throw a WARN: */
642         default:
643                 dev_WARN_ONCE(&q->irq_ptr->cdev->dev, 1,
644                               "found state %#x at index %u on queue %u\n",
645                               state, start, q->nr);
646                 return 0;
647         }
648 }
649
650 /* all buffers processed? */
651 static inline int qdio_outbound_q_done(struct qdio_q *q)
652 {
653         return atomic_read(&q->nr_buf_used) == 0;
654 }
655
656 static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
657 {
658         int count;
659
660         count = get_outbound_buffer_frontier(q, start);
661
662         if (count) {
663                 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
664
665                 if (q->u.out.use_cq) {
666                         unsigned int i;
667
668                         for (i = 0; i < count; i++)
669                                 qdio_check_pending(q, QDIO_BUFNR(start + i));
670                 }
671         }
672
673         return count;
674 }
675
676 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
677                                 unsigned long aob)
678 {
679         int retries = 0, cc;
680         unsigned int busy_bit;
681
682         if (!need_siga_out(q))
683                 return 0;
684
685         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
686 retry:
687         qperf_inc(q, siga_write);
688
689         cc = qdio_siga_output(q, count, &busy_bit, aob);
690         switch (cc) {
691         case 0:
692                 break;
693         case 2:
694                 if (busy_bit) {
695                         while (++retries < QDIO_BUSY_BIT_RETRIES) {
696                                 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
697                                 goto retry;
698                         }
699                         DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
700                         cc = -EBUSY;
701                 } else {
702                         DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
703                         cc = -ENOBUFS;
704                 }
705                 break;
706         case 1:
707         case 3:
708                 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
709                 cc = -EIO;
710                 break;
711         }
712         if (retries) {
713                 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
714                 DBF_ERROR("count:%u", retries);
715         }
716         return cc;
717 }
718
719 void qdio_outbound_tasklet(struct tasklet_struct *t)
720 {
721         struct qdio_output_q *out_q = from_tasklet(out_q, t, tasklet);
722         struct qdio_q *q = container_of(out_q, struct qdio_q, u.out);
723         unsigned int start = q->first_to_check;
724         int count;
725
726         qperf_inc(q, tasklet_outbound);
727         WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
728
729         count = qdio_outbound_q_moved(q, start);
730         if (count) {
731                 q->first_to_check = add_buf(start, count);
732                 qdio_kick_handler(q, start, count);
733         }
734
735         if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
736             !qdio_outbound_q_done(q))
737                 goto sched;
738
739         if (q->u.out.pci_out_enabled)
740                 return;
741
742         /*
743          * Now we know that queue type is either qeth without pci enabled
744          * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
745          * is noticed and outbound_handler is called after some time.
746          */
747         if (qdio_outbound_q_done(q))
748                 del_timer_sync(&q->u.out.timer);
749         else
750                 if (!timer_pending(&q->u.out.timer) &&
751                     likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
752                         mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
753         return;
754
755 sched:
756         qdio_tasklet_schedule(q);
757 }
758
759 void qdio_outbound_timer(struct timer_list *t)
760 {
761         struct qdio_q *q = from_timer(q, t, u.out.timer);
762
763         qdio_tasklet_schedule(q);
764 }
765
766 static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
767 {
768         struct qdio_q *out;
769         int i;
770
771         if (!pci_out_supported(irq) || !irq->scan_threshold)
772                 return;
773
774         for_each_output_queue(irq, out, i)
775                 if (!qdio_outbound_q_done(out))
776                         qdio_tasklet_schedule(out);
777 }
778
779 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
780                                   enum qdio_irq_states state)
781 {
782         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
783
784         irq_ptr->state = state;
785         mb();
786 }
787
788 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
789 {
790         if (irb->esw.esw0.erw.cons) {
791                 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
792                 DBF_ERROR_HEX(irb, 64);
793                 DBF_ERROR_HEX(irb->ecw, 64);
794         }
795 }
796
797 /* PCI interrupt handler */
798 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
799 {
800         int i;
801         struct qdio_q *q;
802
803         if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
804                 return;
805
806         qdio_deliver_irq(irq_ptr);
807         irq_ptr->last_data_irq_time = S390_lowcore.int_clock;
808
809         if (!pci_out_supported(irq_ptr) || !irq_ptr->scan_threshold)
810                 return;
811
812         for_each_output_queue(irq_ptr, q, i) {
813                 if (qdio_outbound_q_done(q))
814                         continue;
815                 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
816                         qdio_siga_sync_q(q);
817                 qdio_tasklet_schedule(q);
818         }
819 }
820
821 static void qdio_handle_activate_check(struct qdio_irq *irq_ptr,
822                                        unsigned long intparm, int cstat,
823                                        int dstat)
824 {
825         struct qdio_q *q;
826
827         DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
828         DBF_ERROR("intp :%lx", intparm);
829         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
830
831         if (irq_ptr->nr_input_qs) {
832                 q = irq_ptr->input_qs[0];
833         } else if (irq_ptr->nr_output_qs) {
834                 q = irq_ptr->output_qs[0];
835         } else {
836                 dump_stack();
837                 goto no_handler;
838         }
839
840         q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
841                    q->nr, q->first_to_check, 0, irq_ptr->int_parm);
842 no_handler:
843         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
844         /*
845          * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
846          * Therefore we call the LGR detection function here.
847          */
848         lgr_info_log();
849 }
850
851 static void qdio_establish_handle_irq(struct qdio_irq *irq_ptr, int cstat,
852                                       int dstat)
853 {
854         DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
855
856         if (cstat)
857                 goto error;
858         if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
859                 goto error;
860         if (!(dstat & DEV_STAT_DEV_END))
861                 goto error;
862         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
863         return;
864
865 error:
866         DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
867         DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
868         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
869 }
870
871 /* qdio interrupt handler */
872 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
873                       struct irb *irb)
874 {
875         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
876         struct subchannel_id schid;
877         int cstat, dstat;
878
879         if (!intparm || !irq_ptr) {
880                 ccw_device_get_schid(cdev, &schid);
881                 DBF_ERROR("qint:%4x", schid.sch_no);
882                 return;
883         }
884
885         if (irq_ptr->perf_stat_enabled)
886                 irq_ptr->perf_stat.qdio_int++;
887
888         if (IS_ERR(irb)) {
889                 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
890                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
891                 wake_up(&cdev->private->wait_q);
892                 return;
893         }
894         qdio_irq_check_sense(irq_ptr, irb);
895         cstat = irb->scsw.cmd.cstat;
896         dstat = irb->scsw.cmd.dstat;
897
898         switch (irq_ptr->state) {
899         case QDIO_IRQ_STATE_INACTIVE:
900                 qdio_establish_handle_irq(irq_ptr, cstat, dstat);
901                 break;
902         case QDIO_IRQ_STATE_CLEANUP:
903                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
904                 break;
905         case QDIO_IRQ_STATE_ESTABLISHED:
906         case QDIO_IRQ_STATE_ACTIVE:
907                 if (cstat & SCHN_STAT_PCI) {
908                         qdio_int_handler_pci(irq_ptr);
909                         return;
910                 }
911                 if (cstat || dstat)
912                         qdio_handle_activate_check(irq_ptr, intparm, cstat,
913                                                    dstat);
914                 break;
915         case QDIO_IRQ_STATE_STOPPED:
916                 break;
917         default:
918                 WARN_ON_ONCE(1);
919         }
920         wake_up(&cdev->private->wait_q);
921 }
922
923 /**
924  * qdio_get_ssqd_desc - get qdio subchannel description
925  * @cdev: ccw device to get description for
926  * @data: where to store the ssqd
927  *
928  * Returns 0 or an error code. The results of the chsc are stored in the
929  * specified structure.
930  */
931 int qdio_get_ssqd_desc(struct ccw_device *cdev,
932                        struct qdio_ssqd_desc *data)
933 {
934         struct subchannel_id schid;
935
936         if (!cdev || !cdev->private)
937                 return -EINVAL;
938
939         ccw_device_get_schid(cdev, &schid);
940         DBF_EVENT("get ssqd:%4x", schid.sch_no);
941         return qdio_setup_get_ssqd(NULL, &schid, data);
942 }
943 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
944
945 static void qdio_shutdown_queues(struct qdio_irq *irq_ptr)
946 {
947         struct qdio_q *q;
948         int i;
949
950         for_each_output_queue(irq_ptr, q, i) {
951                 del_timer_sync(&q->u.out.timer);
952                 tasklet_kill(&q->u.out.tasklet);
953         }
954 }
955
956 /**
957  * qdio_shutdown - shut down a qdio subchannel
958  * @cdev: associated ccw device
959  * @how: use halt or clear to shutdown
960  */
961 int qdio_shutdown(struct ccw_device *cdev, int how)
962 {
963         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
964         struct subchannel_id schid;
965         int rc;
966
967         if (!irq_ptr)
968                 return -ENODEV;
969
970         WARN_ON_ONCE(irqs_disabled());
971         ccw_device_get_schid(cdev, &schid);
972         DBF_EVENT("qshutdown:%4x", schid.sch_no);
973
974         mutex_lock(&irq_ptr->setup_mutex);
975         /*
976          * Subchannel was already shot down. We cannot prevent being called
977          * twice since cio may trigger a shutdown asynchronously.
978          */
979         if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
980                 mutex_unlock(&irq_ptr->setup_mutex);
981                 return 0;
982         }
983
984         /*
985          * Indicate that the device is going down. Scheduling the queue
986          * tasklets is forbidden from here on.
987          */
988         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
989
990         qdio_shutdown_queues(irq_ptr);
991         qdio_shutdown_debug_entries(irq_ptr);
992
993         /* cleanup subchannel */
994         spin_lock_irq(get_ccwdev_lock(cdev));
995         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
996         if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
997                 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
998         else
999                 /* default behaviour is halt */
1000                 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1001         spin_unlock_irq(get_ccwdev_lock(cdev));
1002         if (rc) {
1003                 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1004                 DBF_ERROR("rc:%4d", rc);
1005                 goto no_cleanup;
1006         }
1007
1008         wait_event_interruptible_timeout(cdev->private->wait_q,
1009                 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1010                 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1011                 10 * HZ);
1012
1013 no_cleanup:
1014         qdio_shutdown_thinint(irq_ptr);
1015         qdio_shutdown_irq(irq_ptr);
1016
1017         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1018         mutex_unlock(&irq_ptr->setup_mutex);
1019         if (rc)
1020                 return rc;
1021         return 0;
1022 }
1023 EXPORT_SYMBOL_GPL(qdio_shutdown);
1024
1025 /**
1026  * qdio_free - free data structures for a qdio subchannel
1027  * @cdev: associated ccw device
1028  */
1029 int qdio_free(struct ccw_device *cdev)
1030 {
1031         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1032         struct subchannel_id schid;
1033
1034         if (!irq_ptr)
1035                 return -ENODEV;
1036
1037         ccw_device_get_schid(cdev, &schid);
1038         DBF_EVENT("qfree:%4x", schid.sch_no);
1039         DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
1040         mutex_lock(&irq_ptr->setup_mutex);
1041
1042         irq_ptr->debug_area = NULL;
1043         cdev->private->qdio_data = NULL;
1044         mutex_unlock(&irq_ptr->setup_mutex);
1045
1046         qdio_free_async_data(irq_ptr);
1047         qdio_free_queues(irq_ptr);
1048         free_page((unsigned long) irq_ptr->qdr);
1049         free_page(irq_ptr->chsc_page);
1050         free_page((unsigned long) irq_ptr);
1051         return 0;
1052 }
1053 EXPORT_SYMBOL_GPL(qdio_free);
1054
1055 /**
1056  * qdio_allocate - allocate qdio queues and associated data
1057  * @cdev: associated ccw device
1058  * @no_input_qs: allocate this number of Input Queues
1059  * @no_output_qs: allocate this number of Output Queues
1060  */
1061 int qdio_allocate(struct ccw_device *cdev, unsigned int no_input_qs,
1062                   unsigned int no_output_qs)
1063 {
1064         struct subchannel_id schid;
1065         struct qdio_irq *irq_ptr;
1066         int rc = -ENOMEM;
1067
1068         ccw_device_get_schid(cdev, &schid);
1069         DBF_EVENT("qallocate:%4x", schid.sch_no);
1070
1071         if (no_input_qs > QDIO_MAX_QUEUES_PER_IRQ ||
1072             no_output_qs > QDIO_MAX_QUEUES_PER_IRQ)
1073                 return -EINVAL;
1074
1075         /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1076         irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1077         if (!irq_ptr)
1078                 return -ENOMEM;
1079
1080         irq_ptr->cdev = cdev;
1081         mutex_init(&irq_ptr->setup_mutex);
1082         if (qdio_allocate_dbf(irq_ptr))
1083                 goto err_dbf;
1084
1085         DBF_DEV_EVENT(DBF_ERR, irq_ptr, "alloc niq:%1u noq:%1u", no_input_qs,
1086                       no_output_qs);
1087
1088         /*
1089          * Allocate a page for the chsc calls in qdio_establish.
1090          * Must be pre-allocated since a zfcp recovery will call
1091          * qdio_establish. In case of low memory and swap on a zfcp disk
1092          * we may not be able to allocate memory otherwise.
1093          */
1094         irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1095         if (!irq_ptr->chsc_page)
1096                 goto err_chsc;
1097
1098         /* qdr is used in ccw1.cda which is u32 */
1099         irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1100         if (!irq_ptr->qdr)
1101                 goto err_qdr;
1102
1103         rc = qdio_allocate_qs(irq_ptr, no_input_qs, no_output_qs);
1104         if (rc)
1105                 goto err_queues;
1106
1107         cdev->private->qdio_data = irq_ptr;
1108         qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1109         return 0;
1110
1111 err_queues:
1112         free_page((unsigned long) irq_ptr->qdr);
1113 err_qdr:
1114         free_page(irq_ptr->chsc_page);
1115 err_chsc:
1116 err_dbf:
1117         free_page((unsigned long) irq_ptr);
1118         return rc;
1119 }
1120 EXPORT_SYMBOL_GPL(qdio_allocate);
1121
1122 static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1123 {
1124         struct qdio_q *q = irq_ptr->input_qs[0];
1125         int i, use_cq = 0;
1126
1127         if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1128                 use_cq = 1;
1129
1130         for_each_output_queue(irq_ptr, q, i) {
1131                 if (use_cq) {
1132                         if (multicast_outbound(q))
1133                                 continue;
1134                         if (qdio_enable_async_operation(&q->u.out) < 0) {
1135                                 use_cq = 0;
1136                                 continue;
1137                         }
1138                 } else
1139                         qdio_disable_async_operation(&q->u.out);
1140         }
1141         DBF_EVENT("use_cq:%d", use_cq);
1142 }
1143
1144 static void qdio_trace_init_data(struct qdio_irq *irq,
1145                                  struct qdio_initialize *data)
1146 {
1147         DBF_DEV_EVENT(DBF_ERR, irq, "qfmt:%1u", data->q_format);
1148         DBF_DEV_EVENT(DBF_ERR, irq, "qpff%4x", data->qib_param_field_format);
1149         DBF_DEV_HEX(irq, &data->qib_param_field, sizeof(void *), DBF_ERR);
1150         DBF_DEV_HEX(irq, &data->input_slib_elements, sizeof(void *), DBF_ERR);
1151         DBF_DEV_HEX(irq, &data->output_slib_elements, sizeof(void *), DBF_ERR);
1152         DBF_DEV_EVENT(DBF_ERR, irq, "niq:%1u noq:%1u", data->no_input_qs,
1153                       data->no_output_qs);
1154         DBF_DEV_HEX(irq, &data->input_handler, sizeof(void *), DBF_ERR);
1155         DBF_DEV_HEX(irq, &data->output_handler, sizeof(void *), DBF_ERR);
1156         DBF_DEV_HEX(irq, &data->int_parm, sizeof(long), DBF_ERR);
1157         DBF_DEV_HEX(irq, &data->input_sbal_addr_array, sizeof(void *), DBF_ERR);
1158         DBF_DEV_HEX(irq, &data->output_sbal_addr_array, sizeof(void *),
1159                     DBF_ERR);
1160 }
1161
1162 /**
1163  * qdio_establish - establish queues on a qdio subchannel
1164  * @cdev: associated ccw device
1165  * @init_data: initialization data
1166  */
1167 int qdio_establish(struct ccw_device *cdev,
1168                    struct qdio_initialize *init_data)
1169 {
1170         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1171         struct subchannel_id schid;
1172         int rc;
1173
1174         ccw_device_get_schid(cdev, &schid);
1175         DBF_EVENT("qestablish:%4x", schid.sch_no);
1176
1177         if (!irq_ptr)
1178                 return -ENODEV;
1179
1180         if (init_data->no_input_qs > irq_ptr->max_input_qs ||
1181             init_data->no_output_qs > irq_ptr->max_output_qs)
1182                 return -EINVAL;
1183
1184         if ((init_data->no_input_qs && !init_data->input_handler) ||
1185             (init_data->no_output_qs && !init_data->output_handler))
1186                 return -EINVAL;
1187
1188         if (!init_data->input_sbal_addr_array ||
1189             !init_data->output_sbal_addr_array)
1190                 return -EINVAL;
1191
1192         if (!init_data->irq_poll)
1193                 return -EINVAL;
1194
1195         mutex_lock(&irq_ptr->setup_mutex);
1196         qdio_trace_init_data(irq_ptr, init_data);
1197         qdio_setup_irq(irq_ptr, init_data);
1198
1199         rc = qdio_establish_thinint(irq_ptr);
1200         if (rc) {
1201                 qdio_shutdown_irq(irq_ptr);
1202                 mutex_unlock(&irq_ptr->setup_mutex);
1203                 return rc;
1204         }
1205
1206         /* establish q */
1207         irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1208         irq_ptr->ccw.flags = CCW_FLAG_SLI;
1209         irq_ptr->ccw.count = irq_ptr->equeue.count;
1210         irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1211
1212         spin_lock_irq(get_ccwdev_lock(cdev));
1213         ccw_device_set_options_mask(cdev, 0);
1214
1215         rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1216         spin_unlock_irq(get_ccwdev_lock(cdev));
1217         if (rc) {
1218                 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1219                 DBF_ERROR("rc:%4x", rc);
1220                 qdio_shutdown_thinint(irq_ptr);
1221                 qdio_shutdown_irq(irq_ptr);
1222                 mutex_unlock(&irq_ptr->setup_mutex);
1223                 return rc;
1224         }
1225
1226         wait_event_interruptible_timeout(cdev->private->wait_q,
1227                 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1228                 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1229
1230         if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1231                 mutex_unlock(&irq_ptr->setup_mutex);
1232                 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1233                 return -EIO;
1234         }
1235
1236         qdio_setup_ssqd_info(irq_ptr);
1237
1238         qdio_detect_hsicq(irq_ptr);
1239
1240         /* qebsm is now setup if available, initialize buffer states */
1241         qdio_init_buf_states(irq_ptr);
1242
1243         mutex_unlock(&irq_ptr->setup_mutex);
1244         qdio_print_subchannel_info(irq_ptr);
1245         qdio_setup_debug_entries(irq_ptr);
1246         return 0;
1247 }
1248 EXPORT_SYMBOL_GPL(qdio_establish);
1249
1250 /**
1251  * qdio_activate - activate queues on a qdio subchannel
1252  * @cdev: associated cdev
1253  */
1254 int qdio_activate(struct ccw_device *cdev)
1255 {
1256         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1257         struct subchannel_id schid;
1258         int rc;
1259
1260         ccw_device_get_schid(cdev, &schid);
1261         DBF_EVENT("qactivate:%4x", schid.sch_no);
1262
1263         if (!irq_ptr)
1264                 return -ENODEV;
1265
1266         mutex_lock(&irq_ptr->setup_mutex);
1267         if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1268                 rc = -EBUSY;
1269                 goto out;
1270         }
1271
1272         irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1273         irq_ptr->ccw.flags = CCW_FLAG_SLI;
1274         irq_ptr->ccw.count = irq_ptr->aqueue.count;
1275         irq_ptr->ccw.cda = 0;
1276
1277         spin_lock_irq(get_ccwdev_lock(cdev));
1278         ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1279
1280         rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1281                               0, DOIO_DENY_PREFETCH);
1282         spin_unlock_irq(get_ccwdev_lock(cdev));
1283         if (rc) {
1284                 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1285                 DBF_ERROR("rc:%4x", rc);
1286                 goto out;
1287         }
1288
1289         /* wait for subchannel to become active */
1290         msleep(5);
1291
1292         switch (irq_ptr->state) {
1293         case QDIO_IRQ_STATE_STOPPED:
1294         case QDIO_IRQ_STATE_ERR:
1295                 rc = -EIO;
1296                 break;
1297         default:
1298                 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1299                 rc = 0;
1300         }
1301 out:
1302         mutex_unlock(&irq_ptr->setup_mutex);
1303         return rc;
1304 }
1305 EXPORT_SYMBOL_GPL(qdio_activate);
1306
1307 /**
1308  * handle_inbound - reset processed input buffers
1309  * @q: queue containing the buffers
1310  * @callflags: flags
1311  * @bufnr: first buffer to process
1312  * @count: how many buffers are emptied
1313  */
1314 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1315                           int bufnr, int count)
1316 {
1317         int overlap;
1318
1319         qperf_inc(q, inbound_call);
1320
1321         /* If any processed SBALs are returned to HW, adjust our tracking: */
1322         overlap = min_t(int, count - sub_buf(q->u.in.batch_start, bufnr),
1323                              q->u.in.batch_count);
1324         if (overlap > 0) {
1325                 q->u.in.batch_start = add_buf(q->u.in.batch_start, overlap);
1326                 q->u.in.batch_count -= overlap;
1327         }
1328
1329         count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1330         atomic_add(count, &q->nr_buf_used);
1331
1332         if (need_siga_in(q))
1333                 return qdio_siga_input(q);
1334
1335         return 0;
1336 }
1337
1338 /**
1339  * handle_outbound - process filled outbound buffers
1340  * @q: queue containing the buffers
1341  * @callflags: flags
1342  * @bufnr: first buffer to process
1343  * @count: how many buffers are filled
1344  */
1345 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1346                            unsigned int bufnr, unsigned int count)
1347 {
1348         const unsigned int scan_threshold = q->irq_ptr->scan_threshold;
1349         unsigned char state = 0;
1350         int used, rc = 0;
1351
1352         qperf_inc(q, outbound_call);
1353
1354         count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1355         used = atomic_add_return(count, &q->nr_buf_used);
1356
1357         if (used == QDIO_MAX_BUFFERS_PER_Q)
1358                 qperf_inc(q, outbound_queue_full);
1359
1360         if (callflags & QDIO_FLAG_PCI_OUT) {
1361                 q->u.out.pci_out_enabled = 1;
1362                 qperf_inc(q, pci_request_int);
1363         } else
1364                 q->u.out.pci_out_enabled = 0;
1365
1366         if (queue_type(q) == QDIO_IQDIO_QFMT) {
1367                 unsigned long phys_aob = 0;
1368
1369                 if (q->u.out.use_cq && count == 1)
1370                         phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1371
1372                 rc = qdio_kick_outbound_q(q, count, phys_aob);
1373         } else if (need_siga_sync(q)) {
1374                 rc = qdio_siga_sync_q(q);
1375         } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1376                    get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1377                    state == SLSB_CU_OUTPUT_PRIMED) {
1378                 /* The previous buffer is not processed yet, tack on. */
1379                 qperf_inc(q, fast_requeue);
1380         } else {
1381                 rc = qdio_kick_outbound_q(q, count, 0);
1382         }
1383
1384         /* Let drivers implement their own completion scanning: */
1385         if (!scan_threshold)
1386                 return rc;
1387
1388         /* in case of SIGA errors we must process the error immediately */
1389         if (used >= scan_threshold || rc)
1390                 qdio_tasklet_schedule(q);
1391         else
1392                 /* free the SBALs in case of no further traffic */
1393                 if (!timer_pending(&q->u.out.timer) &&
1394                     likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
1395                         mod_timer(&q->u.out.timer, jiffies + HZ);
1396         return rc;
1397 }
1398
1399 /**
1400  * do_QDIO - process input or output buffers
1401  * @cdev: associated ccw_device for the qdio subchannel
1402  * @callflags: input or output and special flags from the program
1403  * @q_nr: queue number
1404  * @bufnr: buffer number
1405  * @count: how many buffers to process
1406  */
1407 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1408             int q_nr, unsigned int bufnr, unsigned int count)
1409 {
1410         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1411
1412         if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1413                 return -EINVAL;
1414
1415         if (!irq_ptr)
1416                 return -ENODEV;
1417
1418         DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1419                       "do%02x b:%02x c:%02x", callflags, bufnr, count);
1420
1421         if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1422                 return -EIO;
1423         if (!count)
1424                 return 0;
1425         if (callflags & QDIO_FLAG_SYNC_INPUT)
1426                 return handle_inbound(irq_ptr->input_qs[q_nr],
1427                                       callflags, bufnr, count);
1428         else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1429                 return handle_outbound(irq_ptr->output_qs[q_nr],
1430                                        callflags, bufnr, count);
1431         return -EINVAL;
1432 }
1433 EXPORT_SYMBOL_GPL(do_QDIO);
1434
1435 /**
1436  * qdio_start_irq - enable interrupt processing for the device
1437  * @cdev: associated ccw_device for the qdio subchannel
1438  *
1439  * Return codes
1440  *   0 - success
1441  *   1 - irqs not started since new data is available
1442  */
1443 int qdio_start_irq(struct ccw_device *cdev)
1444 {
1445         struct qdio_q *q;
1446         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1447         unsigned int i;
1448
1449         if (!irq_ptr)
1450                 return -ENODEV;
1451
1452         for_each_input_queue(irq_ptr, q, i)
1453                 qdio_stop_polling(q);
1454
1455         clear_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state);
1456
1457         /*
1458          * We need to check again to not lose initiative after
1459          * resetting the ACK state.
1460          */
1461         if (test_nonshared_ind(irq_ptr))
1462                 goto rescan;
1463
1464         for_each_input_queue(irq_ptr, q, i) {
1465                 if (!qdio_inbound_q_done(q, q->first_to_check))
1466                         goto rescan;
1467         }
1468
1469         return 0;
1470
1471 rescan:
1472         if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1473                 return 0;
1474         else
1475                 return 1;
1476
1477 }
1478 EXPORT_SYMBOL(qdio_start_irq);
1479
1480 static int __qdio_inspect_queue(struct qdio_q *q, unsigned int *bufnr,
1481                                 unsigned int *error)
1482 {
1483         unsigned int start = q->first_to_check;
1484         int count;
1485
1486         count = q->is_input_q ? get_inbound_buffer_frontier(q, start) :
1487                                 qdio_outbound_q_moved(q, start);
1488         if (count == 0)
1489                 return 0;
1490
1491         *bufnr = start;
1492         *error = q->qdio_error;
1493
1494         /* for the next time */
1495         q->first_to_check = add_buf(start, count);
1496         q->qdio_error = 0;
1497
1498         return count;
1499 }
1500
1501 int qdio_inspect_queue(struct ccw_device *cdev, unsigned int nr, bool is_input,
1502                        unsigned int *bufnr, unsigned int *error)
1503 {
1504         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1505         struct qdio_q *q;
1506
1507         if (!irq_ptr)
1508                 return -ENODEV;
1509         q = is_input ? irq_ptr->input_qs[nr] : irq_ptr->output_qs[nr];
1510
1511         if (need_siga_sync(q))
1512                 qdio_siga_sync_q(q);
1513
1514         return __qdio_inspect_queue(q, bufnr, error);
1515 }
1516 EXPORT_SYMBOL_GPL(qdio_inspect_queue);
1517
1518 /**
1519  * qdio_get_next_buffers - process input buffers
1520  * @cdev: associated ccw_device for the qdio subchannel
1521  * @nr: input queue number
1522  * @bufnr: first filled buffer number
1523  * @error: buffers are in error state
1524  *
1525  * Return codes
1526  *   < 0 - error
1527  *   = 0 - no new buffers found
1528  *   > 0 - number of processed buffers
1529  */
1530 int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1531                           int *error)
1532 {
1533         struct qdio_q *q;
1534         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1535
1536         if (!irq_ptr)
1537                 return -ENODEV;
1538         q = irq_ptr->input_qs[nr];
1539
1540         /*
1541          * Cannot rely on automatic sync after interrupt since queues may
1542          * also be examined without interrupt.
1543          */
1544         if (need_siga_sync(q))
1545                 qdio_sync_queues(q);
1546
1547         qdio_check_outbound_pci_queues(irq_ptr);
1548
1549         /* Note: upper-layer MUST stop processing immediately here ... */
1550         if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1551                 return -EIO;
1552
1553         return __qdio_inspect_queue(q, bufnr, error);
1554 }
1555 EXPORT_SYMBOL(qdio_get_next_buffers);
1556
1557 /**
1558  * qdio_stop_irq - disable interrupt processing for the device
1559  * @cdev: associated ccw_device for the qdio subchannel
1560  *
1561  * Return codes
1562  *   0 - interrupts were already disabled
1563  *   1 - interrupts successfully disabled
1564  */
1565 int qdio_stop_irq(struct ccw_device *cdev)
1566 {
1567         struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1568
1569         if (!irq_ptr)
1570                 return -ENODEV;
1571
1572         if (test_and_set_bit(QDIO_IRQ_DISABLED, &irq_ptr->poll_state))
1573                 return 0;
1574         else
1575                 return 1;
1576 }
1577 EXPORT_SYMBOL(qdio_stop_irq);
1578
1579 static int __init init_QDIO(void)
1580 {
1581         int rc;
1582
1583         rc = qdio_debug_init();
1584         if (rc)
1585                 return rc;
1586         rc = qdio_setup_init();
1587         if (rc)
1588                 goto out_debug;
1589         rc = qdio_thinint_init();
1590         if (rc)
1591                 goto out_cache;
1592         return 0;
1593
1594 out_cache:
1595         qdio_setup_exit();
1596 out_debug:
1597         qdio_debug_exit();
1598         return rc;
1599 }
1600
1601 static void __exit exit_QDIO(void)
1602 {
1603         qdio_thinint_exit();
1604         qdio_setup_exit();
1605         qdio_debug_exit();
1606 }
1607
1608 module_init(init_QDIO);
1609 module_exit(exit_QDIO);