Linux 2.6.34-rc3
[pohmelfs.git] / drivers / s390 / cio / qdio_main.c
blob4f8f7431177867f3657c54c18fdce2e1127ce0d4
1 /*
2 * linux/drivers/s390/cio/qdio_main.c
4 * Linux for s390 qdio support, buffer handling, qdio API and module support.
6 * Copyright 2000,2008 IBM Corp.
7 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
8 * Jan Glauber <jang@linux.vnet.ibm.com>
9 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/timer.h>
15 #include <linux/delay.h>
16 #include <asm/atomic.h>
17 #include <asm/debug.h>
18 #include <asm/qdio.h>
20 #include "cio.h"
21 #include "css.h"
22 #include "device.h"
23 #include "qdio.h"
24 #include "qdio_debug.h"
26 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
27 "Jan Glauber <jang@linux.vnet.ibm.com>");
28 MODULE_DESCRIPTION("QDIO base support");
29 MODULE_LICENSE("GPL");
31 static inline int do_siga_sync(struct subchannel_id schid,
32 unsigned int out_mask, unsigned int in_mask)
34 register unsigned long __fc asm ("0") = 2;
35 register struct subchannel_id __schid asm ("1") = schid;
36 register unsigned long out asm ("2") = out_mask;
37 register unsigned long in asm ("3") = in_mask;
38 int cc;
40 asm volatile(
41 " siga 0\n"
42 " ipm %0\n"
43 " srl %0,28\n"
44 : "=d" (cc)
45 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
46 return cc;
49 static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
51 register unsigned long __fc asm ("0") = 1;
52 register struct subchannel_id __schid asm ("1") = schid;
53 register unsigned long __mask asm ("2") = mask;
54 int cc;
56 asm volatile(
57 " siga 0\n"
58 " ipm %0\n"
59 " srl %0,28\n"
60 : "=d" (cc)
61 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
62 return cc;
65 /**
66 * do_siga_output - perform SIGA-w/wt function
67 * @schid: subchannel id or in case of QEBSM the subchannel token
68 * @mask: which output queues to process
69 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
70 * @fc: function code to perform
72 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
73 * Note: For IQDC unicast queues only the highest priority queue is processed.
75 static inline int do_siga_output(unsigned long schid, unsigned long mask,
76 unsigned int *bb, unsigned int fc)
78 register unsigned long __fc asm("0") = fc;
79 register unsigned long __schid asm("1") = schid;
80 register unsigned long __mask asm("2") = mask;
81 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
83 asm volatile(
84 " siga 0\n"
85 "0: ipm %0\n"
86 " srl %0,28\n"
87 "1:\n"
88 EX_TABLE(0b, 1b)
89 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
90 : : "cc", "memory");
91 *bb = ((unsigned int) __fc) >> 31;
92 return cc;
95 static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
97 /* all done or next buffer state different */
98 if (ccq == 0 || ccq == 32)
99 return 0;
100 /* not all buffers processed */
101 if (ccq == 96 || ccq == 97)
102 return 1;
103 /* notify devices immediately */
104 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
105 return -EIO;
109 * qdio_do_eqbs - extract buffer states for QEBSM
110 * @q: queue to manipulate
111 * @state: state of the extracted buffers
112 * @start: buffer number to start at
113 * @count: count of buffers to examine
114 * @auto_ack: automatically acknowledge buffers
116 * Returns the number of successfully extracted equal buffer states.
117 * Stops processing if a state is different from the last buffers state.
119 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
120 int start, int count, int auto_ack)
122 unsigned int ccq = 0;
123 int tmp_count = count, tmp_start = start;
124 int nr = q->nr;
125 int rc;
127 BUG_ON(!q->irq_ptr->sch_token);
128 qperf_inc(q, eqbs);
130 if (!q->is_input_q)
131 nr += q->irq_ptr->nr_input_qs;
132 again:
133 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
134 auto_ack);
135 rc = qdio_check_ccq(q, ccq);
137 /* At least one buffer was processed, return and extract the remaining
138 * buffers later.
140 if ((ccq == 96) && (count != tmp_count)) {
141 qperf_inc(q, eqbs_partial);
142 return (count - tmp_count);
145 if (rc == 1) {
146 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
147 goto again;
150 if (rc < 0) {
151 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
152 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
153 q->handler(q->irq_ptr->cdev,
154 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
155 0, -1, -1, q->irq_ptr->int_parm);
156 return 0;
158 return count - tmp_count;
162 * qdio_do_sqbs - set buffer states for QEBSM
163 * @q: queue to manipulate
164 * @state: new state of the buffers
165 * @start: first buffer number to change
166 * @count: how many buffers to change
168 * Returns the number of successfully changed buffers.
169 * Does retrying until the specified count of buffer states is set or an
170 * error occurs.
172 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
173 int count)
175 unsigned int ccq = 0;
176 int tmp_count = count, tmp_start = start;
177 int nr = q->nr;
178 int rc;
180 if (!count)
181 return 0;
183 BUG_ON(!q->irq_ptr->sch_token);
184 qperf_inc(q, sqbs);
186 if (!q->is_input_q)
187 nr += q->irq_ptr->nr_input_qs;
188 again:
189 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
190 rc = qdio_check_ccq(q, ccq);
191 if (rc == 1) {
192 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
193 qperf_inc(q, sqbs_partial);
194 goto again;
196 if (rc < 0) {
197 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
198 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
199 q->handler(q->irq_ptr->cdev,
200 QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
201 0, -1, -1, q->irq_ptr->int_parm);
202 return 0;
204 WARN_ON(tmp_count);
205 return count - tmp_count;
208 /* returns number of examined buffers and their common state in *state */
209 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
210 unsigned char *state, unsigned int count,
211 int auto_ack)
213 unsigned char __state = 0;
214 int i;
216 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
217 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
219 if (is_qebsm(q))
220 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
222 for (i = 0; i < count; i++) {
223 if (!__state)
224 __state = q->slsb.val[bufnr];
225 else if (q->slsb.val[bufnr] != __state)
226 break;
227 bufnr = next_buf(bufnr);
229 *state = __state;
230 return i;
233 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
234 unsigned char *state, int auto_ack)
236 return get_buf_states(q, bufnr, state, 1, auto_ack);
239 /* wrap-around safe setting of slsb states, returns number of changed buffers */
240 static inline int set_buf_states(struct qdio_q *q, int bufnr,
241 unsigned char state, int count)
243 int i;
245 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
246 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
248 if (is_qebsm(q))
249 return qdio_do_sqbs(q, state, bufnr, count);
251 for (i = 0; i < count; i++) {
252 xchg(&q->slsb.val[bufnr], state);
253 bufnr = next_buf(bufnr);
255 return count;
258 static inline int set_buf_state(struct qdio_q *q, int bufnr,
259 unsigned char state)
261 return set_buf_states(q, bufnr, state, 1);
264 /* set slsb states to initial state */
265 void qdio_init_buf_states(struct qdio_irq *irq_ptr)
267 struct qdio_q *q;
268 int i;
270 for_each_input_queue(irq_ptr, q, i)
271 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
272 QDIO_MAX_BUFFERS_PER_Q);
273 for_each_output_queue(irq_ptr, q, i)
274 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
275 QDIO_MAX_BUFFERS_PER_Q);
278 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
279 unsigned int input)
281 int cc;
283 if (!need_siga_sync(q))
284 return 0;
286 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
287 qperf_inc(q, siga_sync);
289 cc = do_siga_sync(q->irq_ptr->schid, output, input);
290 if (cc)
291 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
292 return cc;
295 static inline int qdio_siga_sync_q(struct qdio_q *q)
297 if (q->is_input_q)
298 return qdio_siga_sync(q, 0, q->mask);
299 else
300 return qdio_siga_sync(q, q->mask, 0);
303 static inline int qdio_siga_sync_out(struct qdio_q *q)
305 return qdio_siga_sync(q, ~0U, 0);
308 static inline int qdio_siga_sync_all(struct qdio_q *q)
310 return qdio_siga_sync(q, ~0U, ~0U);
313 static int qdio_siga_output(struct qdio_q *q, unsigned int *busy_bit)
315 unsigned long schid;
316 unsigned int fc = 0;
317 u64 start_time = 0;
318 int cc;
320 if (q->u.out.use_enh_siga)
321 fc = 3;
323 if (is_qebsm(q)) {
324 schid = q->irq_ptr->sch_token;
325 fc |= 0x80;
327 else
328 schid = *((u32 *)&q->irq_ptr->schid);
330 again:
331 cc = do_siga_output(schid, q->mask, busy_bit, fc);
333 /* hipersocket busy condition */
334 if (*busy_bit) {
335 WARN_ON(queue_type(q) != QDIO_IQDIO_QFMT || cc != 2);
337 if (!start_time) {
338 start_time = get_usecs();
339 goto again;
341 if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
342 goto again;
344 return cc;
347 static inline int qdio_siga_input(struct qdio_q *q)
349 int cc;
351 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
352 qperf_inc(q, siga_read);
354 cc = do_siga_input(q->irq_ptr->schid, q->mask);
355 if (cc)
356 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
357 return cc;
360 static inline void qdio_sync_after_thinint(struct qdio_q *q)
362 if (pci_out_supported(q)) {
363 if (need_siga_sync_thinint(q))
364 qdio_siga_sync_all(q);
365 else if (need_siga_sync_out_thinint(q))
366 qdio_siga_sync_out(q);
367 } else
368 qdio_siga_sync_q(q);
371 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
372 unsigned char *state)
374 qdio_siga_sync_q(q);
375 return get_buf_states(q, bufnr, state, 1, 0);
378 static inline void qdio_stop_polling(struct qdio_q *q)
380 if (!q->u.in.polling)
381 return;
383 q->u.in.polling = 0;
384 qperf_inc(q, stop_polling);
386 /* show the card that we are not polling anymore */
387 if (is_qebsm(q)) {
388 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
389 q->u.in.ack_count);
390 q->u.in.ack_count = 0;
391 } else
392 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
395 static inline void account_sbals(struct qdio_q *q, int count)
397 int pos = 0;
399 q->q_stats.nr_sbal_total += count;
400 if (count == QDIO_MAX_BUFFERS_MASK) {
401 q->q_stats.nr_sbals[7]++;
402 return;
404 while (count >>= 1)
405 pos++;
406 q->q_stats.nr_sbals[pos]++;
409 static void announce_buffer_error(struct qdio_q *q, int count)
411 q->qdio_error |= QDIO_ERROR_SLSB_STATE;
413 /* special handling for no target buffer empty */
414 if ((!q->is_input_q &&
415 (q->sbal[q->first_to_check]->element[15].flags & 0xff) == 0x10)) {
416 qperf_inc(q, target_full);
417 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x",
418 q->first_to_check);
419 return;
422 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
423 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
424 DBF_ERROR("FTC:%3d C:%3d", q->first_to_check, count);
425 DBF_ERROR("F14:%2x F15:%2x",
426 q->sbal[q->first_to_check]->element[14].flags & 0xff,
427 q->sbal[q->first_to_check]->element[15].flags & 0xff);
430 static inline void inbound_primed(struct qdio_q *q, int count)
432 int new;
434 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim: %02x", count);
436 /* for QEBSM the ACK was already set by EQBS */
437 if (is_qebsm(q)) {
438 if (!q->u.in.polling) {
439 q->u.in.polling = 1;
440 q->u.in.ack_count = count;
441 q->u.in.ack_start = q->first_to_check;
442 return;
445 /* delete the previous ACK's */
446 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
447 q->u.in.ack_count);
448 q->u.in.ack_count = count;
449 q->u.in.ack_start = q->first_to_check;
450 return;
454 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
455 * or by the next inbound run.
457 new = add_buf(q->first_to_check, count - 1);
458 if (q->u.in.polling) {
459 /* reset the previous ACK but first set the new one */
460 set_buf_state(q, new, SLSB_P_INPUT_ACK);
461 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
462 } else {
463 q->u.in.polling = 1;
464 set_buf_state(q, new, SLSB_P_INPUT_ACK);
467 q->u.in.ack_start = new;
468 count--;
469 if (!count)
470 return;
471 /* need to change ALL buffers to get more interrupts */
472 set_buf_states(q, q->first_to_check, SLSB_P_INPUT_NOT_INIT, count);
475 static int get_inbound_buffer_frontier(struct qdio_q *q)
477 int count, stop;
478 unsigned char state;
481 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
482 * would return 0.
484 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
485 stop = add_buf(q->first_to_check, count);
487 if (q->first_to_check == stop)
488 goto out;
491 * No siga sync here, as a PCI or we after a thin interrupt
492 * already sync'ed the queues.
494 count = get_buf_states(q, q->first_to_check, &state, count, 1);
495 if (!count)
496 goto out;
498 switch (state) {
499 case SLSB_P_INPUT_PRIMED:
500 inbound_primed(q, count);
501 q->first_to_check = add_buf(q->first_to_check, count);
502 if (atomic_sub(count, &q->nr_buf_used) == 0)
503 qperf_inc(q, inbound_queue_full);
504 if (q->irq_ptr->perf_stat_enabled)
505 account_sbals(q, count);
506 break;
507 case SLSB_P_INPUT_ERROR:
508 announce_buffer_error(q, count);
509 /* process the buffer, the upper layer will take care of it */
510 q->first_to_check = add_buf(q->first_to_check, count);
511 atomic_sub(count, &q->nr_buf_used);
512 if (q->irq_ptr->perf_stat_enabled)
513 account_sbals_error(q, count);
514 break;
515 case SLSB_CU_INPUT_EMPTY:
516 case SLSB_P_INPUT_NOT_INIT:
517 case SLSB_P_INPUT_ACK:
518 if (q->irq_ptr->perf_stat_enabled)
519 q->q_stats.nr_sbal_nop++;
520 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop");
521 break;
522 default:
523 BUG();
525 out:
526 return q->first_to_check;
529 static int qdio_inbound_q_moved(struct qdio_q *q)
531 int bufnr;
533 bufnr = get_inbound_buffer_frontier(q);
535 if ((bufnr != q->last_move) || q->qdio_error) {
536 q->last_move = bufnr;
537 if (!is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
538 q->u.in.timestamp = get_usecs();
539 return 1;
540 } else
541 return 0;
544 static inline int qdio_inbound_q_done(struct qdio_q *q)
546 unsigned char state = 0;
548 if (!atomic_read(&q->nr_buf_used))
549 return 1;
551 qdio_siga_sync_q(q);
552 get_buf_state(q, q->first_to_check, &state, 0);
554 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
555 /* more work coming */
556 return 0;
558 if (is_thinint_irq(q->irq_ptr))
559 return 1;
561 /* don't poll under z/VM */
562 if (MACHINE_IS_VM)
563 return 1;
566 * At this point we know, that inbound first_to_check
567 * has (probably) not moved (see qdio_inbound_processing).
569 if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
570 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x",
571 q->first_to_check);
572 return 1;
573 } else
574 return 0;
577 static void qdio_kick_handler(struct qdio_q *q)
579 int start = q->first_to_kick;
580 int end = q->first_to_check;
581 int count;
583 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
584 return;
586 count = sub_buf(end, start);
588 if (q->is_input_q) {
589 qperf_inc(q, inbound_handler);
590 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
591 } else {
592 qperf_inc(q, outbound_handler);
593 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
594 start, count);
597 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
598 q->irq_ptr->int_parm);
600 /* for the next time */
601 q->first_to_kick = end;
602 q->qdio_error = 0;
605 static void __qdio_inbound_processing(struct qdio_q *q)
607 qperf_inc(q, tasklet_inbound);
608 again:
609 if (!qdio_inbound_q_moved(q))
610 return;
612 qdio_kick_handler(q);
614 if (!qdio_inbound_q_done(q)) {
615 /* means poll time is not yet over */
616 qperf_inc(q, tasklet_inbound_resched);
617 goto again;
620 qdio_stop_polling(q);
622 * We need to check again to not lose initiative after
623 * resetting the ACK state.
625 if (!qdio_inbound_q_done(q)) {
626 qperf_inc(q, tasklet_inbound_resched2);
627 goto again;
631 void qdio_inbound_processing(unsigned long data)
633 struct qdio_q *q = (struct qdio_q *)data;
634 __qdio_inbound_processing(q);
637 static int get_outbound_buffer_frontier(struct qdio_q *q)
639 int count, stop;
640 unsigned char state;
642 if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
643 (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
644 qdio_siga_sync_q(q);
647 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
648 * would return 0.
650 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
651 stop = add_buf(q->first_to_check, count);
653 if (q->first_to_check == stop)
654 return q->first_to_check;
656 count = get_buf_states(q, q->first_to_check, &state, count, 0);
657 if (!count)
658 return q->first_to_check;
660 switch (state) {
661 case SLSB_P_OUTPUT_EMPTY:
662 /* the adapter got it */
663 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out empty:%1d %02x", q->nr, count);
665 atomic_sub(count, &q->nr_buf_used);
666 q->first_to_check = add_buf(q->first_to_check, count);
667 if (q->irq_ptr->perf_stat_enabled)
668 account_sbals(q, count);
669 break;
670 case SLSB_P_OUTPUT_ERROR:
671 announce_buffer_error(q, count);
672 /* process the buffer, the upper layer will take care of it */
673 q->first_to_check = add_buf(q->first_to_check, count);
674 atomic_sub(count, &q->nr_buf_used);
675 if (q->irq_ptr->perf_stat_enabled)
676 account_sbals_error(q, count);
677 break;
678 case SLSB_CU_OUTPUT_PRIMED:
679 /* the adapter has not fetched the output yet */
680 if (q->irq_ptr->perf_stat_enabled)
681 q->q_stats.nr_sbal_nop++;
682 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d", q->nr);
683 break;
684 case SLSB_P_OUTPUT_NOT_INIT:
685 case SLSB_P_OUTPUT_HALTED:
686 break;
687 default:
688 BUG();
690 return q->first_to_check;
693 /* all buffers processed? */
694 static inline int qdio_outbound_q_done(struct qdio_q *q)
696 return atomic_read(&q->nr_buf_used) == 0;
699 static inline int qdio_outbound_q_moved(struct qdio_q *q)
701 int bufnr;
703 bufnr = get_outbound_buffer_frontier(q);
705 if ((bufnr != q->last_move) || q->qdio_error) {
706 q->last_move = bufnr;
707 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
708 return 1;
709 } else
710 return 0;
713 static int qdio_kick_outbound_q(struct qdio_q *q)
715 unsigned int busy_bit;
716 int cc;
718 if (!need_siga_out(q))
719 return 0;
721 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
722 qperf_inc(q, siga_write);
724 cc = qdio_siga_output(q, &busy_bit);
725 switch (cc) {
726 case 0:
727 break;
728 case 2:
729 if (busy_bit) {
730 DBF_ERROR("%4x cc2 REP:%1d", SCH_NO(q), q->nr);
731 cc |= QDIO_ERROR_SIGA_BUSY;
732 } else
733 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
734 break;
735 case 1:
736 case 3:
737 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
738 break;
740 return cc;
743 static void __qdio_outbound_processing(struct qdio_q *q)
745 qperf_inc(q, tasklet_outbound);
746 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
748 if (qdio_outbound_q_moved(q))
749 qdio_kick_handler(q);
751 if (queue_type(q) == QDIO_ZFCP_QFMT)
752 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
753 goto sched;
755 /* bail out for HiperSockets unicast queues */
756 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
757 return;
759 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
760 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL)
761 goto sched;
763 if (q->u.out.pci_out_enabled)
764 return;
767 * Now we know that queue type is either qeth without pci enabled
768 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
769 * EMPTY is noticed and outbound_handler is called after some time.
771 if (qdio_outbound_q_done(q))
772 del_timer(&q->u.out.timer);
773 else
774 if (!timer_pending(&q->u.out.timer))
775 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
776 return;
778 sched:
779 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
780 return;
781 tasklet_schedule(&q->tasklet);
784 /* outbound tasklet */
785 void qdio_outbound_processing(unsigned long data)
787 struct qdio_q *q = (struct qdio_q *)data;
788 __qdio_outbound_processing(q);
791 void qdio_outbound_timer(unsigned long data)
793 struct qdio_q *q = (struct qdio_q *)data;
795 if (unlikely(q->irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
796 return;
797 tasklet_schedule(&q->tasklet);
800 static inline void qdio_check_outbound_after_thinint(struct qdio_q *q)
802 struct qdio_q *out;
803 int i;
805 if (!pci_out_supported(q))
806 return;
808 for_each_output_queue(q->irq_ptr, out, i)
809 if (!qdio_outbound_q_done(out))
810 tasklet_schedule(&out->tasklet);
813 static void __tiqdio_inbound_processing(struct qdio_q *q)
815 qperf_inc(q, tasklet_inbound);
816 qdio_sync_after_thinint(q);
819 * The interrupt could be caused by a PCI request. Check the
820 * PCI capable outbound queues.
822 qdio_check_outbound_after_thinint(q);
824 if (!qdio_inbound_q_moved(q))
825 return;
827 qdio_kick_handler(q);
829 if (!qdio_inbound_q_done(q)) {
830 qperf_inc(q, tasklet_inbound_resched);
831 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED)) {
832 tasklet_schedule(&q->tasklet);
833 return;
837 qdio_stop_polling(q);
839 * We need to check again to not lose initiative after
840 * resetting the ACK state.
842 if (!qdio_inbound_q_done(q)) {
843 qperf_inc(q, tasklet_inbound_resched2);
844 if (likely(q->irq_ptr->state != QDIO_IRQ_STATE_STOPPED))
845 tasklet_schedule(&q->tasklet);
849 void tiqdio_inbound_processing(unsigned long data)
851 struct qdio_q *q = (struct qdio_q *)data;
852 __tiqdio_inbound_processing(q);
855 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
856 enum qdio_irq_states state)
858 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
860 irq_ptr->state = state;
861 mb();
864 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
866 if (irb->esw.esw0.erw.cons) {
867 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
868 DBF_ERROR_HEX(irb, 64);
869 DBF_ERROR_HEX(irb->ecw, 64);
873 /* PCI interrupt handler */
874 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
876 int i;
877 struct qdio_q *q;
879 if (unlikely(irq_ptr->state == QDIO_IRQ_STATE_STOPPED))
880 return;
882 for_each_input_queue(irq_ptr, q, i)
883 tasklet_schedule(&q->tasklet);
885 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
886 return;
888 for_each_output_queue(irq_ptr, q, i) {
889 if (qdio_outbound_q_done(q))
890 continue;
892 if (!siga_syncs_out_pci(q))
893 qdio_siga_sync_q(q);
895 tasklet_schedule(&q->tasklet);
899 static void qdio_handle_activate_check(struct ccw_device *cdev,
900 unsigned long intparm, int cstat, int dstat)
902 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
903 struct qdio_q *q;
905 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
906 DBF_ERROR("intp :%lx", intparm);
907 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
909 if (irq_ptr->nr_input_qs) {
910 q = irq_ptr->input_qs[0];
911 } else if (irq_ptr->nr_output_qs) {
912 q = irq_ptr->output_qs[0];
913 } else {
914 dump_stack();
915 goto no_handler;
917 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
918 0, -1, -1, irq_ptr->int_parm);
919 no_handler:
920 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
923 static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
924 int dstat)
926 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
928 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
930 if (cstat)
931 goto error;
932 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
933 goto error;
934 if (!(dstat & DEV_STAT_DEV_END))
935 goto error;
936 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
937 return;
939 error:
940 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
941 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
942 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
945 /* qdio interrupt handler */
946 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
947 struct irb *irb)
949 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
950 int cstat, dstat;
952 if (!intparm || !irq_ptr) {
953 DBF_ERROR("qint:%4x", cdev->private->schid.sch_no);
954 return;
957 if (IS_ERR(irb)) {
958 switch (PTR_ERR(irb)) {
959 case -EIO:
960 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
961 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
962 wake_up(&cdev->private->wait_q);
963 return;
964 default:
965 WARN_ON(1);
966 return;
969 qdio_irq_check_sense(irq_ptr, irb);
970 cstat = irb->scsw.cmd.cstat;
971 dstat = irb->scsw.cmd.dstat;
973 switch (irq_ptr->state) {
974 case QDIO_IRQ_STATE_INACTIVE:
975 qdio_establish_handle_irq(cdev, cstat, dstat);
976 break;
977 case QDIO_IRQ_STATE_CLEANUP:
978 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
979 break;
980 case QDIO_IRQ_STATE_ESTABLISHED:
981 case QDIO_IRQ_STATE_ACTIVE:
982 if (cstat & SCHN_STAT_PCI) {
983 qdio_int_handler_pci(irq_ptr);
984 return;
986 if (cstat || dstat)
987 qdio_handle_activate_check(cdev, intparm, cstat,
988 dstat);
989 break;
990 case QDIO_IRQ_STATE_STOPPED:
991 break;
992 default:
993 WARN_ON(1);
995 wake_up(&cdev->private->wait_q);
999 * qdio_get_ssqd_desc - get qdio subchannel description
1000 * @cdev: ccw device to get description for
1001 * @data: where to store the ssqd
1003 * Returns 0 or an error code. The results of the chsc are stored in the
1004 * specified structure.
1006 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1007 struct qdio_ssqd_desc *data)
1010 if (!cdev || !cdev->private)
1011 return -EINVAL;
1013 DBF_EVENT("get ssqd:%4x", cdev->private->schid.sch_no);
1014 return qdio_setup_get_ssqd(NULL, &cdev->private->schid, data);
1016 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1019 * qdio_cleanup - shutdown queues and free data structures
1020 * @cdev: associated ccw device
1021 * @how: use halt or clear to shutdown
1023 * This function calls qdio_shutdown() for @cdev with method @how.
1024 * and qdio_free(). The qdio_free() return value is ignored since
1025 * !irq_ptr is already checked.
1027 int qdio_cleanup(struct ccw_device *cdev, int how)
1029 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1030 int rc;
1032 if (!irq_ptr)
1033 return -ENODEV;
1035 rc = qdio_shutdown(cdev, how);
1037 qdio_free(cdev);
1038 return rc;
1040 EXPORT_SYMBOL_GPL(qdio_cleanup);
1042 static void qdio_shutdown_queues(struct ccw_device *cdev)
1044 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1045 struct qdio_q *q;
1046 int i;
1048 for_each_input_queue(irq_ptr, q, i)
1049 tasklet_kill(&q->tasklet);
1051 for_each_output_queue(irq_ptr, q, i) {
1052 del_timer(&q->u.out.timer);
1053 tasklet_kill(&q->tasklet);
1058 * qdio_shutdown - shut down a qdio subchannel
1059 * @cdev: associated ccw device
1060 * @how: use halt or clear to shutdown
1062 int qdio_shutdown(struct ccw_device *cdev, int how)
1064 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1065 int rc;
1066 unsigned long flags;
1068 if (!irq_ptr)
1069 return -ENODEV;
1071 BUG_ON(irqs_disabled());
1072 DBF_EVENT("qshutdown:%4x", cdev->private->schid.sch_no);
1074 mutex_lock(&irq_ptr->setup_mutex);
1076 * Subchannel was already shot down. We cannot prevent being called
1077 * twice since cio may trigger a shutdown asynchronously.
1079 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1080 mutex_unlock(&irq_ptr->setup_mutex);
1081 return 0;
1085 * Indicate that the device is going down. Scheduling the queue
1086 * tasklets is forbidden from here on.
1088 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1090 tiqdio_remove_input_queues(irq_ptr);
1091 qdio_shutdown_queues(cdev);
1092 qdio_shutdown_debug_entries(irq_ptr, cdev);
1094 /* cleanup subchannel */
1095 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1097 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1098 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1099 else
1100 /* default behaviour is halt */
1101 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1102 if (rc) {
1103 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1104 DBF_ERROR("rc:%4d", rc);
1105 goto no_cleanup;
1108 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1109 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1110 wait_event_interruptible_timeout(cdev->private->wait_q,
1111 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1112 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1113 10 * HZ);
1114 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1116 no_cleanup:
1117 qdio_shutdown_thinint(irq_ptr);
1119 /* restore interrupt handler */
1120 if ((void *)cdev->handler == (void *)qdio_int_handler)
1121 cdev->handler = irq_ptr->orig_handler;
1122 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1124 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1125 mutex_unlock(&irq_ptr->setup_mutex);
1126 if (rc)
1127 return rc;
1128 return 0;
1130 EXPORT_SYMBOL_GPL(qdio_shutdown);
1133 * qdio_free - free data structures for a qdio subchannel
1134 * @cdev: associated ccw device
1136 int qdio_free(struct ccw_device *cdev)
1138 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1140 if (!irq_ptr)
1141 return -ENODEV;
1143 DBF_EVENT("qfree:%4x", cdev->private->schid.sch_no);
1144 mutex_lock(&irq_ptr->setup_mutex);
1146 if (irq_ptr->debug_area != NULL) {
1147 debug_unregister(irq_ptr->debug_area);
1148 irq_ptr->debug_area = NULL;
1150 cdev->private->qdio_data = NULL;
1151 mutex_unlock(&irq_ptr->setup_mutex);
1153 qdio_release_memory(irq_ptr);
1154 return 0;
1156 EXPORT_SYMBOL_GPL(qdio_free);
1159 * qdio_initialize - allocate and establish queues for a qdio subchannel
1160 * @init_data: initialization data
1162 * This function first allocates queues via qdio_allocate() and on success
1163 * establishes them via qdio_establish().
1165 int qdio_initialize(struct qdio_initialize *init_data)
1167 int rc;
1169 rc = qdio_allocate(init_data);
1170 if (rc)
1171 return rc;
1173 rc = qdio_establish(init_data);
1174 if (rc)
1175 qdio_free(init_data->cdev);
1176 return rc;
1178 EXPORT_SYMBOL_GPL(qdio_initialize);
1181 * qdio_allocate - allocate qdio queues and associated data
1182 * @init_data: initialization data
1184 int qdio_allocate(struct qdio_initialize *init_data)
1186 struct qdio_irq *irq_ptr;
1188 DBF_EVENT("qallocate:%4x", init_data->cdev->private->schid.sch_no);
1190 if ((init_data->no_input_qs && !init_data->input_handler) ||
1191 (init_data->no_output_qs && !init_data->output_handler))
1192 return -EINVAL;
1194 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1195 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1196 return -EINVAL;
1198 if ((!init_data->input_sbal_addr_array) ||
1199 (!init_data->output_sbal_addr_array))
1200 return -EINVAL;
1202 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1203 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1204 if (!irq_ptr)
1205 goto out_err;
1207 mutex_init(&irq_ptr->setup_mutex);
1208 qdio_allocate_dbf(init_data, irq_ptr);
1211 * Allocate a page for the chsc calls in qdio_establish.
1212 * Must be pre-allocated since a zfcp recovery will call
1213 * qdio_establish. In case of low memory and swap on a zfcp disk
1214 * we may not be able to allocate memory otherwise.
1216 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1217 if (!irq_ptr->chsc_page)
1218 goto out_rel;
1220 /* qdr is used in ccw1.cda which is u32 */
1221 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1222 if (!irq_ptr->qdr)
1223 goto out_rel;
1224 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1226 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1227 init_data->no_output_qs))
1228 goto out_rel;
1230 init_data->cdev->private->qdio_data = irq_ptr;
1231 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1232 return 0;
1233 out_rel:
1234 qdio_release_memory(irq_ptr);
1235 out_err:
1236 return -ENOMEM;
1238 EXPORT_SYMBOL_GPL(qdio_allocate);
1241 * qdio_establish - establish queues on a qdio subchannel
1242 * @init_data: initialization data
1244 int qdio_establish(struct qdio_initialize *init_data)
1246 struct qdio_irq *irq_ptr;
1247 struct ccw_device *cdev = init_data->cdev;
1248 unsigned long saveflags;
1249 int rc;
1251 DBF_EVENT("qestablish:%4x", cdev->private->schid.sch_no);
1253 irq_ptr = cdev->private->qdio_data;
1254 if (!irq_ptr)
1255 return -ENODEV;
1257 if (cdev->private->state != DEV_STATE_ONLINE)
1258 return -EINVAL;
1260 mutex_lock(&irq_ptr->setup_mutex);
1261 qdio_setup_irq(init_data);
1263 rc = qdio_establish_thinint(irq_ptr);
1264 if (rc) {
1265 mutex_unlock(&irq_ptr->setup_mutex);
1266 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1267 return rc;
1270 /* establish q */
1271 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1272 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1273 irq_ptr->ccw.count = irq_ptr->equeue.count;
1274 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1276 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1277 ccw_device_set_options_mask(cdev, 0);
1279 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1280 if (rc) {
1281 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1282 DBF_ERROR("rc:%4x", rc);
1284 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1286 if (rc) {
1287 mutex_unlock(&irq_ptr->setup_mutex);
1288 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1289 return rc;
1292 wait_event_interruptible_timeout(cdev->private->wait_q,
1293 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1294 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1296 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1297 mutex_unlock(&irq_ptr->setup_mutex);
1298 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1299 return -EIO;
1302 qdio_setup_ssqd_info(irq_ptr);
1303 DBF_EVENT("qDmmwc:%2x", irq_ptr->ssqd_desc.mmwc);
1304 DBF_EVENT("qib ac:%4x", irq_ptr->qib.ac);
1306 /* qebsm is now setup if available, initialize buffer states */
1307 qdio_init_buf_states(irq_ptr);
1309 mutex_unlock(&irq_ptr->setup_mutex);
1310 qdio_print_subchannel_info(irq_ptr, cdev);
1311 qdio_setup_debug_entries(irq_ptr, cdev);
1312 return 0;
1314 EXPORT_SYMBOL_GPL(qdio_establish);
1317 * qdio_activate - activate queues on a qdio subchannel
1318 * @cdev: associated cdev
1320 int qdio_activate(struct ccw_device *cdev)
1322 struct qdio_irq *irq_ptr;
1323 int rc;
1324 unsigned long saveflags;
1326 DBF_EVENT("qactivate:%4x", cdev->private->schid.sch_no);
1328 irq_ptr = cdev->private->qdio_data;
1329 if (!irq_ptr)
1330 return -ENODEV;
1332 if (cdev->private->state != DEV_STATE_ONLINE)
1333 return -EINVAL;
1335 mutex_lock(&irq_ptr->setup_mutex);
1336 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1337 rc = -EBUSY;
1338 goto out;
1341 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1342 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1343 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1344 irq_ptr->ccw.cda = 0;
1346 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1347 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1349 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1350 0, DOIO_DENY_PREFETCH);
1351 if (rc) {
1352 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1353 DBF_ERROR("rc:%4x", rc);
1355 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1357 if (rc)
1358 goto out;
1360 if (is_thinint_irq(irq_ptr))
1361 tiqdio_add_input_queues(irq_ptr);
1363 /* wait for subchannel to become active */
1364 msleep(5);
1366 switch (irq_ptr->state) {
1367 case QDIO_IRQ_STATE_STOPPED:
1368 case QDIO_IRQ_STATE_ERR:
1369 rc = -EIO;
1370 break;
1371 default:
1372 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1373 rc = 0;
1375 out:
1376 mutex_unlock(&irq_ptr->setup_mutex);
1377 return rc;
1379 EXPORT_SYMBOL_GPL(qdio_activate);
1381 static inline int buf_in_between(int bufnr, int start, int count)
1383 int end = add_buf(start, count);
1385 if (end > start) {
1386 if (bufnr >= start && bufnr < end)
1387 return 1;
1388 else
1389 return 0;
1392 /* wrap-around case */
1393 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1394 (bufnr < end))
1395 return 1;
1396 else
1397 return 0;
1401 * handle_inbound - reset processed input buffers
1402 * @q: queue containing the buffers
1403 * @callflags: flags
1404 * @bufnr: first buffer to process
1405 * @count: how many buffers are emptied
1407 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1408 int bufnr, int count)
1410 int used, diff;
1412 qperf_inc(q, inbound_call);
1414 if (!q->u.in.polling)
1415 goto set;
1417 /* protect against stop polling setting an ACK for an emptied slsb */
1418 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1419 /* overwriting everything, just delete polling status */
1420 q->u.in.polling = 0;
1421 q->u.in.ack_count = 0;
1422 goto set;
1423 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1424 if (is_qebsm(q)) {
1425 /* partial overwrite, just update ack_start */
1426 diff = add_buf(bufnr, count);
1427 diff = sub_buf(diff, q->u.in.ack_start);
1428 q->u.in.ack_count -= diff;
1429 if (q->u.in.ack_count <= 0) {
1430 q->u.in.polling = 0;
1431 q->u.in.ack_count = 0;
1432 goto set;
1434 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1436 else
1437 /* the only ACK will be deleted, so stop polling */
1438 q->u.in.polling = 0;
1441 set:
1442 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1444 used = atomic_add_return(count, &q->nr_buf_used) - count;
1445 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1447 /* no need to signal as long as the adapter had free buffers */
1448 if (used)
1449 return 0;
1451 if (need_siga_in(q))
1452 return qdio_siga_input(q);
1453 return 0;
1457 * handle_outbound - process filled outbound buffers
1458 * @q: queue containing the buffers
1459 * @callflags: flags
1460 * @bufnr: first buffer to process
1461 * @count: how many buffers are filled
1463 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1464 int bufnr, int count)
1466 unsigned char state;
1467 int used, rc = 0;
1469 qperf_inc(q, outbound_call);
1471 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1472 used = atomic_add_return(count, &q->nr_buf_used);
1473 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1475 if (callflags & QDIO_FLAG_PCI_OUT) {
1476 q->u.out.pci_out_enabled = 1;
1477 qperf_inc(q, pci_request_int);
1479 else
1480 q->u.out.pci_out_enabled = 0;
1482 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1483 if (multicast_outbound(q))
1484 rc = qdio_kick_outbound_q(q);
1485 else
1486 if ((q->irq_ptr->ssqd_desc.mmwc > 1) &&
1487 (count > 1) &&
1488 (count <= q->irq_ptr->ssqd_desc.mmwc)) {
1489 /* exploit enhanced SIGA */
1490 q->u.out.use_enh_siga = 1;
1491 rc = qdio_kick_outbound_q(q);
1492 } else {
1494 * One siga-w per buffer required for unicast
1495 * HiperSockets.
1497 q->u.out.use_enh_siga = 0;
1498 while (count--) {
1499 rc = qdio_kick_outbound_q(q);
1500 if (rc)
1501 goto out;
1504 goto out;
1507 if (need_siga_sync(q)) {
1508 qdio_siga_sync_q(q);
1509 goto out;
1512 /* try to fast requeue buffers */
1513 get_buf_state(q, prev_buf(bufnr), &state, 0);
1514 if (state != SLSB_CU_OUTPUT_PRIMED)
1515 rc = qdio_kick_outbound_q(q);
1516 else
1517 qperf_inc(q, fast_requeue);
1519 out:
1520 tasklet_schedule(&q->tasklet);
1521 return rc;
1525 * do_QDIO - process input or output buffers
1526 * @cdev: associated ccw_device for the qdio subchannel
1527 * @callflags: input or output and special flags from the program
1528 * @q_nr: queue number
1529 * @bufnr: buffer number
1530 * @count: how many buffers to process
1532 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1533 int q_nr, unsigned int bufnr, unsigned int count)
1535 struct qdio_irq *irq_ptr;
1537 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1538 return -EINVAL;
1540 irq_ptr = cdev->private->qdio_data;
1541 if (!irq_ptr)
1542 return -ENODEV;
1544 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1545 "do%02x b:%02x c:%02x", callflags, bufnr, count);
1547 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1548 return -EBUSY;
1550 if (callflags & QDIO_FLAG_SYNC_INPUT)
1551 return handle_inbound(irq_ptr->input_qs[q_nr],
1552 callflags, bufnr, count);
1553 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1554 return handle_outbound(irq_ptr->output_qs[q_nr],
1555 callflags, bufnr, count);
1556 return -EINVAL;
1558 EXPORT_SYMBOL_GPL(do_QDIO);
1560 static int __init init_QDIO(void)
1562 int rc;
1564 rc = qdio_setup_init();
1565 if (rc)
1566 return rc;
1567 rc = tiqdio_allocate_memory();
1568 if (rc)
1569 goto out_cache;
1570 rc = qdio_debug_init();
1571 if (rc)
1572 goto out_ti;
1573 rc = tiqdio_register_thinints();
1574 if (rc)
1575 goto out_debug;
1576 return 0;
1578 out_debug:
1579 qdio_debug_exit();
1580 out_ti:
1581 tiqdio_free_memory();
1582 out_cache:
1583 qdio_setup_exit();
1584 return rc;
1587 static void __exit exit_QDIO(void)
1589 tiqdio_unregister_thinints();
1590 tiqdio_free_memory();
1591 qdio_debug_exit();
1592 qdio_setup_exit();
1595 module_init(init_QDIO);
1596 module_exit(exit_QDIO);