blob: 9307512132fe565f3ea738c7ff2401d9e7a0a552 [file] [log] [blame]
Jan Glauber779e6e12008-07-17 17:16:48 +02001/*
2 * linux/drivers/s390/cio/qdio_main.c
3 *
4 * Linux for s390 qdio support, buffer handling, qdio API and module support.
5 *
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>
10 */
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>
19
20#include "cio.h"
21#include "css.h"
22#include "device.h"
23#include "qdio.h"
24#include "qdio_debug.h"
25#include "qdio_perf.h"
26
27MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
28 "Jan Glauber <jang@linux.vnet.ibm.com>");
29MODULE_DESCRIPTION("QDIO base support");
30MODULE_LICENSE("GPL");
31
32static inline int do_siga_sync(struct subchannel_id schid,
33 unsigned int out_mask, unsigned int in_mask)
34{
35 register unsigned long __fc asm ("0") = 2;
36 register struct subchannel_id __schid asm ("1") = schid;
37 register unsigned long out asm ("2") = out_mask;
38 register unsigned long in asm ("3") = in_mask;
39 int cc;
40
41 asm volatile(
42 " siga 0\n"
43 " ipm %0\n"
44 " srl %0,28\n"
45 : "=d" (cc)
46 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
47 return cc;
48}
49
50static inline int do_siga_input(struct subchannel_id schid, unsigned int mask)
51{
52 register unsigned long __fc asm ("0") = 1;
53 register struct subchannel_id __schid asm ("1") = schid;
54 register unsigned long __mask asm ("2") = mask;
55 int cc;
56
57 asm volatile(
58 " siga 0\n"
59 " ipm %0\n"
60 " srl %0,28\n"
61 : "=d" (cc)
62 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc", "memory");
63 return cc;
64}
65
66/**
67 * do_siga_output - perform SIGA-w/wt function
68 * @schid: subchannel id or in case of QEBSM the subchannel token
69 * @mask: which output queues to process
70 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
71 * @fc: function code to perform
72 *
73 * Returns cc or QDIO_ERROR_SIGA_ACCESS_EXCEPTION.
74 * Note: For IQDC unicast queues only the highest priority queue is processed.
75 */
76static inline int do_siga_output(unsigned long schid, unsigned long mask,
77 u32 *bb, unsigned int fc)
78{
79 register unsigned long __fc asm("0") = fc;
80 register unsigned long __schid asm("1") = schid;
81 register unsigned long __mask asm("2") = mask;
82 int cc = QDIO_ERROR_SIGA_ACCESS_EXCEPTION;
83
84 asm volatile(
85 " siga 0\n"
86 "0: ipm %0\n"
87 " srl %0,28\n"
88 "1:\n"
89 EX_TABLE(0b, 1b)
90 : "+d" (cc), "+d" (__fc), "+d" (__schid), "+d" (__mask)
91 : : "cc", "memory");
92 *bb = ((unsigned int) __fc) >> 31;
93 return cc;
94}
95
96static inline int qdio_check_ccq(struct qdio_q *q, unsigned int ccq)
97{
98 char dbf_text[15];
99
100 /* all done or next buffer state different */
101 if (ccq == 0 || ccq == 32)
102 return 0;
103 /* not all buffers processed */
104 if (ccq == 96 || ccq == 97)
105 return 1;
106 /* notify devices immediately */
107 sprintf(dbf_text, "%d", ccq);
108 QDIO_DBF_TEXT2(1, trace, dbf_text);
109 return -EIO;
110}
111
112/**
113 * qdio_do_eqbs - extract buffer states for QEBSM
114 * @q: queue to manipulate
115 * @state: state of the extracted buffers
116 * @start: buffer number to start at
117 * @count: count of buffers to examine
118 *
119 * Returns the number of successfull extracted equal buffer states.
120 * Stops processing if a state is different from the last buffers state.
121 */
122static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
123 int start, int count)
124{
125 unsigned int ccq = 0;
126 int tmp_count = count, tmp_start = start;
127 int nr = q->nr;
128 int rc;
129 char dbf_text[15];
130
131 BUG_ON(!q->irq_ptr->sch_token);
132
133 if (!q->is_input_q)
134 nr += q->irq_ptr->nr_input_qs;
135again:
136 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
137 rc = qdio_check_ccq(q, ccq);
138
139 /* At least one buffer was processed, return and extract the remaining
140 * buffers later.
141 */
142 if ((ccq == 96) && (count != tmp_count))
143 return (count - tmp_count);
144 if (rc == 1) {
145 QDIO_DBF_TEXT5(1, trace, "eqAGAIN");
146 goto again;
147 }
148
149 if (rc < 0) {
150 QDIO_DBF_TEXT2(1, trace, "eqberr");
151 sprintf(dbf_text, "%2x,%2x,%d,%d", count, tmp_count, ccq, nr);
152 QDIO_DBF_TEXT2(1, trace, dbf_text);
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;
157 }
158 return count - tmp_count;
159}
160
161/**
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
167 *
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.
171 */
172static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
173 int count)
174{
175 unsigned int ccq = 0;
176 int tmp_count = count, tmp_start = start;
177 int nr = q->nr;
178 int rc;
179 char dbf_text[15];
180
181 BUG_ON(!q->irq_ptr->sch_token);
182
183 if (!q->is_input_q)
184 nr += q->irq_ptr->nr_input_qs;
185again:
186 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
187 rc = qdio_check_ccq(q, ccq);
188 if (rc == 1) {
189 QDIO_DBF_TEXT5(1, trace, "sqAGAIN");
190 goto again;
191 }
192 if (rc < 0) {
193 QDIO_DBF_TEXT3(1, trace, "sqberr");
194 sprintf(dbf_text, "%2x,%2x", count, tmp_count);
195 QDIO_DBF_TEXT3(1, trace, dbf_text);
196 sprintf(dbf_text, "%d,%d", ccq, nr);
197 QDIO_DBF_TEXT3(1, trace, dbf_text);
198
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;
203 }
204 WARN_ON(tmp_count);
205 return count - tmp_count;
206}
207
208/* returns number of examined buffers and their common state in *state */
209static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
210 unsigned char *state, unsigned int count)
211{
212 unsigned char __state = 0;
213 int i;
214
215 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
216 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
217
218 if (is_qebsm(q))
219 return qdio_do_eqbs(q, state, bufnr, count);
220
221 for (i = 0; i < count; i++) {
222 if (!__state)
223 __state = q->slsb.val[bufnr];
224 else if (q->slsb.val[bufnr] != __state)
225 break;
226 bufnr = next_buf(bufnr);
227 }
228 *state = __state;
229 return i;
230}
231
232inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
233 unsigned char *state)
234{
235 return get_buf_states(q, bufnr, state, 1);
236}
237
238/* wrap-around safe setting of slsb states, returns number of changed buffers */
239static inline int set_buf_states(struct qdio_q *q, int bufnr,
240 unsigned char state, int count)
241{
242 int i;
243
244 BUG_ON(bufnr > QDIO_MAX_BUFFERS_MASK);
245 BUG_ON(count > QDIO_MAX_BUFFERS_PER_Q);
246
247 if (is_qebsm(q))
248 return qdio_do_sqbs(q, state, bufnr, count);
249
250 for (i = 0; i < count; i++) {
251 xchg(&q->slsb.val[bufnr], state);
252 bufnr = next_buf(bufnr);
253 }
254 return count;
255}
256
257static inline int set_buf_state(struct qdio_q *q, int bufnr,
258 unsigned char state)
259{
260 return set_buf_states(q, bufnr, state, 1);
261}
262
263/* set slsb states to initial state */
264void qdio_init_buf_states(struct qdio_irq *irq_ptr)
265{
266 struct qdio_q *q;
267 int i;
268
269 for_each_input_queue(irq_ptr, q, i)
270 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
271 QDIO_MAX_BUFFERS_PER_Q);
272 for_each_output_queue(irq_ptr, q, i)
273 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
274 QDIO_MAX_BUFFERS_PER_Q);
275}
276
277static int qdio_siga_sync(struct qdio_q *q, unsigned int output,
278 unsigned int input)
279{
280 int cc;
281
282 if (!need_siga_sync(q))
283 return 0;
284
285 qdio_perf_stat_inc(&perf_stats.siga_sync);
286
287 cc = do_siga_sync(q->irq_ptr->schid, output, input);
288 if (cc) {
289 QDIO_DBF_TEXT4(0, trace, "sigasync");
290 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
291 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
292 }
293 return cc;
294}
295
296inline int qdio_siga_sync_q(struct qdio_q *q)
297{
298 if (q->is_input_q)
299 return qdio_siga_sync(q, 0, q->mask);
300 else
301 return qdio_siga_sync(q, q->mask, 0);
302}
303
304static inline int qdio_siga_sync_out(struct qdio_q *q)
305{
306 return qdio_siga_sync(q, ~0U, 0);
307}
308
309static inline int qdio_siga_sync_all(struct qdio_q *q)
310{
311 return qdio_siga_sync(q, ~0U, ~0U);
312}
313
314static inline int qdio_do_siga_output(struct qdio_q *q, unsigned int *busy_bit)
315{
316 unsigned int fc = 0;
317 unsigned long schid;
318
319 if (!is_qebsm(q))
320 schid = *((u32 *)&q->irq_ptr->schid);
321 else {
322 schid = q->irq_ptr->sch_token;
323 fc |= 0x80;
324 }
325 return do_siga_output(schid, q->mask, busy_bit, fc);
326}
327
328static int qdio_siga_output(struct qdio_q *q)
329{
330 int cc;
331 u32 busy_bit;
332 u64 start_time = 0;
Jan Glauber58eb27c2008-08-21 19:46:34 +0200333 char dbf_text[15];
Jan Glauber779e6e12008-07-17 17:16:48 +0200334
335 QDIO_DBF_TEXT5(0, trace, "sigaout");
336 QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
337
338 qdio_perf_stat_inc(&perf_stats.siga_out);
339again:
340 cc = qdio_do_siga_output(q, &busy_bit);
341 if (queue_type(q) == QDIO_IQDIO_QFMT && cc == 2 && busy_bit) {
Jan Glauber58eb27c2008-08-21 19:46:34 +0200342 sprintf(dbf_text, "bb%4x%2x", q->irq_ptr->schid.sch_no, q->nr);
343 QDIO_DBF_TEXT3(0, trace, dbf_text);
344
Jan Glauber779e6e12008-07-17 17:16:48 +0200345 if (!start_time)
346 start_time = get_usecs();
347 else if ((get_usecs() - start_time) < QDIO_BUSY_BIT_PATIENCE)
348 goto again;
349 }
350
351 if (cc == 2 && busy_bit)
352 cc |= QDIO_ERROR_SIGA_BUSY;
353 if (cc)
354 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
355 return cc;
356}
357
358static inline int qdio_siga_input(struct qdio_q *q)
359{
360 int cc;
361
362 QDIO_DBF_TEXT4(0, trace, "sigain");
363 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
364
365 qdio_perf_stat_inc(&perf_stats.siga_in);
366
367 cc = do_siga_input(q->irq_ptr->schid, q->mask);
368 if (cc)
369 QDIO_DBF_HEX3(0, trace, &cc, sizeof(int *));
370 return cc;
371}
372
373/* called from thinint inbound handler */
374void qdio_sync_after_thinint(struct qdio_q *q)
375{
376 if (pci_out_supported(q)) {
377 if (need_siga_sync_thinint(q))
378 qdio_siga_sync_all(q);
379 else if (need_siga_sync_out_thinint(q))
380 qdio_siga_sync_out(q);
381 } else
382 qdio_siga_sync_q(q);
383}
384
385inline void qdio_stop_polling(struct qdio_q *q)
386{
387 spin_lock_bh(&q->u.in.lock);
388 if (!q->u.in.polling) {
389 spin_unlock_bh(&q->u.in.lock);
390 return;
391 }
392 q->u.in.polling = 0;
393 qdio_perf_stat_inc(&perf_stats.debug_stop_polling);
394
395 /* show the card that we are not polling anymore */
396 set_buf_state(q, q->last_move_ftc, SLSB_P_INPUT_NOT_INIT);
397 spin_unlock_bh(&q->u.in.lock);
398}
399
400static void announce_buffer_error(struct qdio_q *q)
401{
402 char dbf_text[15];
403
404 if (q->is_input_q)
405 QDIO_DBF_TEXT3(1, trace, "inperr");
406 else
407 QDIO_DBF_TEXT3(0, trace, "outperr");
408
409 sprintf(dbf_text, "%x-%x-%x", q->first_to_check,
410 q->sbal[q->first_to_check]->element[14].flags,
411 q->sbal[q->first_to_check]->element[15].flags);
412 QDIO_DBF_TEXT3(1, trace, dbf_text);
413 QDIO_DBF_HEX2(1, trace, q->sbal[q->first_to_check], 256);
414
415 q->qdio_error = QDIO_ERROR_SLSB_STATE;
416}
417
418static int get_inbound_buffer_frontier(struct qdio_q *q)
419{
420 int count, stop;
421 unsigned char state;
422
423 /*
424 * If we still poll don't update last_move_ftc, keep the
425 * previously ACK buffer there.
426 */
427 if (!q->u.in.polling)
428 q->last_move_ftc = q->first_to_check;
429
430 /*
431 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
432 * would return 0.
433 */
434 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
435 stop = add_buf(q->first_to_check, count);
436
437 /*
438 * No siga sync here, as a PCI or we after a thin interrupt
439 * will sync the queues.
440 */
441
442 /* need to set count to 1 for non-qebsm */
443 if (!is_qebsm(q))
444 count = 1;
445
446check_next:
447 if (q->first_to_check == stop)
448 goto out;
449
450 count = get_buf_states(q, q->first_to_check, &state, count);
451 if (!count)
452 goto out;
453
454 switch (state) {
455 case SLSB_P_INPUT_PRIMED:
456 QDIO_DBF_TEXT5(0, trace, "inptprim");
457
458 /*
459 * Only ACK the first buffer. The ACK will be removed in
460 * qdio_stop_polling.
461 */
462 if (q->u.in.polling)
463 state = SLSB_P_INPUT_NOT_INIT;
464 else {
465 q->u.in.polling = 1;
466 state = SLSB_P_INPUT_ACK;
467 }
468 set_buf_state(q, q->first_to_check, state);
469
470 /*
471 * Need to change all PRIMED buffers to NOT_INIT, otherwise
472 * we're loosing initiative in the thinint code.
473 */
474 if (count > 1)
475 set_buf_states(q, next_buf(q->first_to_check),
476 SLSB_P_INPUT_NOT_INIT, count - 1);
477
478 /*
479 * No siga-sync needed for non-qebsm here, as the inbound queue
480 * will be synced on the next siga-r, resp.
481 * tiqdio_is_inbound_q_done will do the siga-sync.
482 */
483 q->first_to_check = add_buf(q->first_to_check, count);
484 atomic_sub(count, &q->nr_buf_used);
485 goto check_next;
486 case SLSB_P_INPUT_ERROR:
487 announce_buffer_error(q);
488 /* process the buffer, the upper layer will take care of it */
489 q->first_to_check = add_buf(q->first_to_check, count);
490 atomic_sub(count, &q->nr_buf_used);
491 break;
492 case SLSB_CU_INPUT_EMPTY:
493 case SLSB_P_INPUT_NOT_INIT:
494 case SLSB_P_INPUT_ACK:
495 QDIO_DBF_TEXT5(0, trace, "inpnipro");
496 break;
497 default:
498 BUG();
499 }
500out:
501 QDIO_DBF_HEX4(0, trace, &q->first_to_check, sizeof(int));
502 return q->first_to_check;
503}
504
505int qdio_inbound_q_moved(struct qdio_q *q)
506{
507 int bufnr;
508
509 bufnr = get_inbound_buffer_frontier(q);
510
511 if ((bufnr != q->last_move_ftc) || q->qdio_error) {
512 if (!need_siga_sync(q) && !pci_out_supported(q))
513 q->u.in.timestamp = get_usecs();
514
515 QDIO_DBF_TEXT4(0, trace, "inhasmvd");
516 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
517 return 1;
518 } else
519 return 0;
520}
521
522static int qdio_inbound_q_done(struct qdio_q *q)
523{
524 unsigned char state;
525#ifdef CONFIG_QDIO_DEBUG
526 char dbf_text[15];
527#endif
528
529 if (!atomic_read(&q->nr_buf_used))
530 return 1;
531
532 /*
533 * We need that one for synchronization with the adapter, as it
534 * does a kind of PCI avoidance.
535 */
536 qdio_siga_sync_q(q);
537
538 get_buf_state(q, q->first_to_check, &state);
539 if (state == SLSB_P_INPUT_PRIMED)
540 /* we got something to do */
541 return 0;
542
543 /* on VM, we don't poll, so the q is always done here */
544 if (need_siga_sync(q) || pci_out_supported(q))
545 return 1;
546
547 /*
548 * At this point we know, that inbound first_to_check
549 * has (probably) not moved (see qdio_inbound_processing).
550 */
551 if (get_usecs() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
552#ifdef CONFIG_QDIO_DEBUG
553 QDIO_DBF_TEXT4(0, trace, "inqisdon");
554 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
555 sprintf(dbf_text, "pf%02x", q->first_to_check);
556 QDIO_DBF_TEXT4(0, trace, dbf_text);
557#endif /* CONFIG_QDIO_DEBUG */
558 return 1;
559 } else {
560#ifdef CONFIG_QDIO_DEBUG
561 QDIO_DBF_TEXT4(0, trace, "inqisntd");
562 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
563 sprintf(dbf_text, "pf%02x", q->first_to_check);
564 QDIO_DBF_TEXT4(0, trace, dbf_text);
565#endif /* CONFIG_QDIO_DEBUG */
566 return 0;
567 }
568}
569
570void qdio_kick_inbound_handler(struct qdio_q *q)
571{
572 int count, start, end;
573#ifdef CONFIG_QDIO_DEBUG
574 char dbf_text[15];
575#endif
576
577 qdio_perf_stat_inc(&perf_stats.inbound_handler);
578
579 start = q->first_to_kick;
580 end = q->first_to_check;
581 if (end >= start)
582 count = end - start;
583 else
584 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
585
586#ifdef CONFIG_QDIO_DEBUG
587 sprintf(dbf_text, "s=%2xc=%2x", start, count);
588 QDIO_DBF_TEXT4(0, trace, dbf_text);
589#endif /* CONFIG_QDIO_DEBUG */
590
591 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
592 return;
593
594 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr,
595 start, count, q->irq_ptr->int_parm);
596
597 /* for the next time */
598 q->first_to_kick = q->first_to_check;
599 q->qdio_error = 0;
600}
601
602static void __qdio_inbound_processing(struct qdio_q *q)
603{
604 qdio_perf_stat_inc(&perf_stats.tasklet_inbound);
605again:
606 if (!qdio_inbound_q_moved(q))
607 return;
608
609 qdio_kick_inbound_handler(q);
610
611 if (!qdio_inbound_q_done(q))
612 /* means poll time is not yet over */
613 goto again;
614
615 qdio_stop_polling(q);
616 /*
617 * We need to check again to not lose initiative after
618 * resetting the ACK state.
619 */
620 if (!qdio_inbound_q_done(q))
621 goto again;
622}
623
624/* inbound tasklet */
625void qdio_inbound_processing(unsigned long data)
626{
627 struct qdio_q *q = (struct qdio_q *)data;
628 __qdio_inbound_processing(q);
629}
630
631static int get_outbound_buffer_frontier(struct qdio_q *q)
632{
633 int count, stop;
634 unsigned char state;
635
636 if (((queue_type(q) != QDIO_IQDIO_QFMT) && !pci_out_supported(q)) ||
637 (queue_type(q) == QDIO_IQDIO_QFMT && multicast_outbound(q)))
638 qdio_siga_sync_q(q);
639
640 /*
641 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
642 * would return 0.
643 */
644 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
645 stop = add_buf(q->first_to_check, count);
646
647 /* need to set count to 1 for non-qebsm */
648 if (!is_qebsm(q))
649 count = 1;
650
651check_next:
652 if (q->first_to_check == stop)
653 return q->first_to_check;
654
655 count = get_buf_states(q, q->first_to_check, &state, count);
656 if (!count)
657 return q->first_to_check;
658
659 switch (state) {
660 case SLSB_P_OUTPUT_EMPTY:
661 /* the adapter got it */
662 QDIO_DBF_TEXT5(0, trace, "outpempt");
663
664 atomic_sub(count, &q->nr_buf_used);
665 q->first_to_check = add_buf(q->first_to_check, count);
666 /*
667 * We fetch all buffer states at once. get_buf_states may
668 * return count < stop. For QEBSM we do not loop.
669 */
670 if (is_qebsm(q))
671 break;
672 goto check_next;
673 case SLSB_P_OUTPUT_ERROR:
674 announce_buffer_error(q);
675 /* process the buffer, the upper layer will take care of it */
676 q->first_to_check = add_buf(q->first_to_check, count);
677 atomic_sub(count, &q->nr_buf_used);
678 break;
679 case SLSB_CU_OUTPUT_PRIMED:
680 /* the adapter has not fetched the output yet */
681 QDIO_DBF_TEXT5(0, trace, "outpprim");
682 break;
683 case SLSB_P_OUTPUT_NOT_INIT:
684 case SLSB_P_OUTPUT_HALTED:
685 break;
686 default:
687 BUG();
688 }
689 return q->first_to_check;
690}
691
692/* all buffers processed? */
693static inline int qdio_outbound_q_done(struct qdio_q *q)
694{
695 return atomic_read(&q->nr_buf_used) == 0;
696}
697
698static inline int qdio_outbound_q_moved(struct qdio_q *q)
699{
700 int bufnr;
701
702 bufnr = get_outbound_buffer_frontier(q);
703
704 if ((bufnr != q->last_move_ftc) || q->qdio_error) {
705 q->last_move_ftc = bufnr;
706 QDIO_DBF_TEXT4(0, trace, "oqhasmvd");
707 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
708 return 1;
709 } else
710 return 0;
711}
712
713/*
714 * VM could present us cc=2 and busy bit set on SIGA-write
715 * during reconfiguration of their Guest LAN (only in iqdio mode,
716 * otherwise qdio is asynchronous and cc=2 and busy bit there will take
717 * the queues down immediately).
718 *
719 * Therefore qdio_siga_output will try for a short time constantly,
720 * if such a condition occurs. If it doesn't change, it will
721 * increase the busy_siga_counter and save the timestamp, and
722 * schedule the queue for later processing. qdio_outbound_processing
723 * will check out the counter. If non-zero, it will call qdio_kick_outbound_q
724 * as often as the value of the counter. This will attempt further SIGA
725 * instructions. For each successful SIGA, the counter is
726 * decreased, for failing SIGAs the counter remains the same, after
727 * all. After some time of no movement, qdio_kick_outbound_q will
728 * finally fail and reflect corresponding error codes to call
729 * the upper layer module and have it take the queues down.
730 *
731 * Note that this is a change from the original HiperSockets design
732 * (saying cc=2 and busy bit means take the queues down), but in
733 * these days Guest LAN didn't exist... excessive cc=2 with busy bit
734 * conditions will still take the queues down, but the threshold is
735 * higher due to the Guest LAN environment.
736 *
737 * Called from outbound tasklet and do_QDIO handler.
738 */
739static void qdio_kick_outbound_q(struct qdio_q *q)
740{
741 int rc;
742#ifdef CONFIG_QDIO_DEBUG
743 char dbf_text[15];
744
745 QDIO_DBF_TEXT5(0, trace, "kickoutq");
746 QDIO_DBF_HEX5(0, trace, &q, sizeof(void *));
747#endif /* CONFIG_QDIO_DEBUG */
748
749 if (!need_siga_out(q))
750 return;
751
752 rc = qdio_siga_output(q);
753 switch (rc) {
754 case 0:
Jan Glauber779e6e12008-07-17 17:16:48 +0200755 /* TODO: improve error handling for CC=0 case */
756#ifdef CONFIG_QDIO_DEBUG
Jan Glauber58eb27c2008-08-21 19:46:34 +0200757 if (q->u.out.timestamp) {
758 QDIO_DBF_TEXT3(0, trace, "cc2reslv");
759 sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no,
760 q->nr,
761 atomic_read(&q->u.out.busy_siga_counter));
762 QDIO_DBF_TEXT3(0, trace, dbf_text);
763 }
Jan Glauber779e6e12008-07-17 17:16:48 +0200764#endif /* CONFIG_QDIO_DEBUG */
Jan Glauber58eb27c2008-08-21 19:46:34 +0200765 /* went smooth this time, reset timestamp */
766 q->u.out.timestamp = 0;
Jan Glauber779e6e12008-07-17 17:16:48 +0200767 break;
768 /* cc=2 and busy bit */
769 case (2 | QDIO_ERROR_SIGA_BUSY):
770 atomic_inc(&q->u.out.busy_siga_counter);
771
772 /* if the last siga was successful, save timestamp here */
773 if (!q->u.out.timestamp)
774 q->u.out.timestamp = get_usecs();
775
776 /* if we're in time, don't touch qdio_error */
777 if (get_usecs() - q->u.out.timestamp < QDIO_BUSY_BIT_GIVE_UP) {
778 tasklet_schedule(&q->tasklet);
779 break;
780 }
781 QDIO_DBF_TEXT2(0, trace, "cc2REPRT");
782#ifdef CONFIG_QDIO_DEBUG
783 sprintf(dbf_text, "%4x%2x%2x", q->irq_ptr->schid.sch_no, q->nr,
784 atomic_read(&q->u.out.busy_siga_counter));
785 QDIO_DBF_TEXT3(0, trace, dbf_text);
786#endif /* CONFIG_QDIO_DEBUG */
787 default:
788 /* for plain cc=1, 2 or 3 */
789 q->qdio_error = rc;
790 }
791}
792
793static void qdio_kick_outbound_handler(struct qdio_q *q)
794{
795 int start, end, count;
796#ifdef CONFIG_QDIO_DEBUG
797 char dbf_text[15];
798#endif
799
800 start = q->first_to_kick;
801 end = q->last_move_ftc;
802 if (end >= start)
803 count = end - start;
804 else
805 count = end + QDIO_MAX_BUFFERS_PER_Q - start;
806
807#ifdef CONFIG_QDIO_DEBUG
808 QDIO_DBF_TEXT4(0, trace, "kickouth");
809 QDIO_DBF_HEX4(0, trace, &q, sizeof(void *));
810
811 sprintf(dbf_text, "s=%2xc=%2x", start, count);
812 QDIO_DBF_TEXT4(0, trace, dbf_text);
813#endif /* CONFIG_QDIO_DEBUG */
814
815 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
816 return;
817
818 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
819 q->irq_ptr->int_parm);
820
821 /* for the next time: */
822 q->first_to_kick = q->last_move_ftc;
823 q->qdio_error = 0;
824}
825
826static void __qdio_outbound_processing(struct qdio_q *q)
827{
828 int siga_attempts;
829
830 qdio_perf_stat_inc(&perf_stats.tasklet_outbound);
831
832 /* see comment in qdio_kick_outbound_q */
833 siga_attempts = atomic_read(&q->u.out.busy_siga_counter);
834 while (siga_attempts--) {
835 atomic_dec(&q->u.out.busy_siga_counter);
836 qdio_kick_outbound_q(q);
837 }
838
839 BUG_ON(atomic_read(&q->nr_buf_used) < 0);
840
841 if (qdio_outbound_q_moved(q))
842 qdio_kick_outbound_handler(q);
843
844 if (queue_type(q) == QDIO_ZFCP_QFMT) {
845 if (!pci_out_supported(q) && !qdio_outbound_q_done(q))
846 tasklet_schedule(&q->tasklet);
847 return;
848 }
849
850 /* bail out for HiperSockets unicast queues */
851 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q))
852 return;
853
Ursula Braun4bcb3a32008-10-10 21:33:04 +0200854 if ((queue_type(q) == QDIO_IQDIO_QFMT) &&
855 (atomic_read(&q->nr_buf_used)) > QDIO_IQDIO_POLL_LVL) {
856 tasklet_schedule(&q->tasklet);
857 return;
858 }
859
Jan Glauber779e6e12008-07-17 17:16:48 +0200860 if (q->u.out.pci_out_enabled)
861 return;
862
863 /*
864 * Now we know that queue type is either qeth without pci enabled
865 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
866 * EMPTY is noticed and outbound_handler is called after some time.
867 */
868 if (qdio_outbound_q_done(q))
869 del_timer(&q->u.out.timer);
870 else {
871 if (!timer_pending(&q->u.out.timer)) {
872 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
873 qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
874 }
875 }
876}
877
878/* outbound tasklet */
879void qdio_outbound_processing(unsigned long data)
880{
881 struct qdio_q *q = (struct qdio_q *)data;
882 __qdio_outbound_processing(q);
883}
884
885void qdio_outbound_timer(unsigned long data)
886{
887 struct qdio_q *q = (struct qdio_q *)data;
888 tasklet_schedule(&q->tasklet);
889}
890
891/* called from thinint inbound tasklet */
892void qdio_check_outbound_after_thinint(struct qdio_q *q)
893{
894 struct qdio_q *out;
895 int i;
896
897 if (!pci_out_supported(q))
898 return;
899
900 for_each_output_queue(q->irq_ptr, out, i)
901 if (!qdio_outbound_q_done(out))
902 tasklet_schedule(&out->tasklet);
903}
904
905static inline void qdio_set_state(struct qdio_irq *irq_ptr,
906 enum qdio_irq_states state)
907{
908#ifdef CONFIG_QDIO_DEBUG
909 char dbf_text[15];
910
911 QDIO_DBF_TEXT5(0, trace, "newstate");
912 sprintf(dbf_text, "%4x%4x", irq_ptr->schid.sch_no, state);
913 QDIO_DBF_TEXT5(0, trace, dbf_text);
914#endif /* CONFIG_QDIO_DEBUG */
915
916 irq_ptr->state = state;
917 mb();
918}
919
920static void qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
921{
922 char dbf_text[15];
923
924 if (irb->esw.esw0.erw.cons) {
925 sprintf(dbf_text, "sens%4x", schid.sch_no);
926 QDIO_DBF_TEXT2(1, trace, dbf_text);
927 QDIO_DBF_HEX0(0, trace, irb, 64);
928 QDIO_DBF_HEX0(0, trace, irb->ecw, 64);
929 }
930}
931
932/* PCI interrupt handler */
933static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
934{
935 int i;
936 struct qdio_q *q;
937
938 qdio_perf_stat_inc(&perf_stats.pci_int);
939
940 for_each_input_queue(irq_ptr, q, i)
941 tasklet_schedule(&q->tasklet);
942
943 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
944 return;
945
946 for_each_output_queue(irq_ptr, q, i) {
947 if (qdio_outbound_q_done(q))
948 continue;
949
950 if (!siga_syncs_out_pci(q))
951 qdio_siga_sync_q(q);
952
953 tasklet_schedule(&q->tasklet);
954 }
955}
956
957static void qdio_handle_activate_check(struct ccw_device *cdev,
958 unsigned long intparm, int cstat, int dstat)
959{
960 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
961 struct qdio_q *q;
962 char dbf_text[15];
963
964 QDIO_DBF_TEXT2(1, trace, "ick2");
965 sprintf(dbf_text, "%s", cdev->dev.bus_id);
966 QDIO_DBF_TEXT2(1, trace, dbf_text);
967 QDIO_DBF_HEX2(0, trace, &intparm, sizeof(int));
968 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
969 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
970
971 if (irq_ptr->nr_input_qs) {
972 q = irq_ptr->input_qs[0];
973 } else if (irq_ptr->nr_output_qs) {
974 q = irq_ptr->output_qs[0];
975 } else {
976 dump_stack();
977 goto no_handler;
978 }
979 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
980 0, -1, -1, irq_ptr->int_parm);
981no_handler:
982 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
983}
984
985static void qdio_call_shutdown(struct work_struct *work)
986{
987 struct ccw_device_private *priv;
988 struct ccw_device *cdev;
989
990 priv = container_of(work, struct ccw_device_private, kick_work);
991 cdev = priv->cdev;
992 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
993 put_device(&cdev->dev);
994}
995
996static void qdio_int_error(struct ccw_device *cdev)
997{
998 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
999
1000 switch (irq_ptr->state) {
1001 case QDIO_IRQ_STATE_INACTIVE:
1002 case QDIO_IRQ_STATE_CLEANUP:
1003 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1004 break;
1005 case QDIO_IRQ_STATE_ESTABLISHED:
1006 case QDIO_IRQ_STATE_ACTIVE:
1007 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1008 if (get_device(&cdev->dev)) {
1009 /* Can't call shutdown from interrupt context. */
1010 PREPARE_WORK(&cdev->private->kick_work,
1011 qdio_call_shutdown);
1012 queue_work(ccw_device_work, &cdev->private->kick_work);
1013 }
1014 break;
1015 default:
1016 WARN_ON(1);
1017 }
1018 wake_up(&cdev->private->wait_q);
1019}
1020
1021static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
1022 int dstat)
1023{
1024 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1025
1026 if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
1027 QDIO_DBF_TEXT2(1, setup, "eq:ckcon");
1028 goto error;
1029 }
1030
1031 if (!(dstat & DEV_STAT_DEV_END)) {
1032 QDIO_DBF_TEXT2(1, setup, "eq:no de");
1033 goto error;
1034 }
1035
1036 if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
1037 QDIO_DBF_TEXT2(1, setup, "eq:badio");
1038 goto error;
1039 }
1040 return 0;
1041error:
1042 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
1043 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
1044 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1045 return 1;
1046}
1047
1048static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1049 int dstat)
1050{
1051 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1052 char dbf_text[15];
1053
1054 sprintf(dbf_text, "qehi%4x", cdev->private->schid.sch_no);
1055 QDIO_DBF_TEXT0(0, setup, dbf_text);
1056 QDIO_DBF_TEXT0(0, trace, dbf_text);
1057
1058 if (!qdio_establish_check_errors(cdev, cstat, dstat))
1059 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1060}
1061
1062/* qdio interrupt handler */
1063void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1064 struct irb *irb)
1065{
1066 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1067 int cstat, dstat;
1068 char dbf_text[15];
1069
1070 qdio_perf_stat_inc(&perf_stats.qdio_int);
1071
1072 if (!intparm || !irq_ptr) {
1073 sprintf(dbf_text, "qihd%4x", cdev->private->schid.sch_no);
1074 QDIO_DBF_TEXT2(1, setup, dbf_text);
1075 return;
1076 }
1077
1078 if (IS_ERR(irb)) {
1079 switch (PTR_ERR(irb)) {
1080 case -EIO:
Jan Glauber58eb27c2008-08-21 19:46:34 +02001081 sprintf(dbf_text, "ierr%4x", irq_ptr->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001082 QDIO_DBF_TEXT2(1, setup, dbf_text);
1083 qdio_int_error(cdev);
1084 return;
1085 case -ETIMEDOUT:
Jan Glauber58eb27c2008-08-21 19:46:34 +02001086 sprintf(dbf_text, "qtoh%4x", irq_ptr->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001087 QDIO_DBF_TEXT2(1, setup, dbf_text);
1088 qdio_int_error(cdev);
1089 return;
1090 default:
1091 WARN_ON(1);
1092 return;
1093 }
1094 }
1095 qdio_irq_check_sense(irq_ptr->schid, irb);
1096
1097 cstat = irb->scsw.cmd.cstat;
1098 dstat = irb->scsw.cmd.dstat;
1099
1100 switch (irq_ptr->state) {
1101 case QDIO_IRQ_STATE_INACTIVE:
1102 qdio_establish_handle_irq(cdev, cstat, dstat);
1103 break;
1104
1105 case QDIO_IRQ_STATE_CLEANUP:
1106 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1107 break;
1108
1109 case QDIO_IRQ_STATE_ESTABLISHED:
1110 case QDIO_IRQ_STATE_ACTIVE:
1111 if (cstat & SCHN_STAT_PCI) {
1112 qdio_int_handler_pci(irq_ptr);
1113 /* no state change so no need to wake up wait_q */
1114 return;
1115 }
1116 if ((cstat & ~SCHN_STAT_PCI) || dstat) {
1117 qdio_handle_activate_check(cdev, intparm, cstat,
1118 dstat);
1119 break;
1120 }
1121 default:
1122 WARN_ON(1);
1123 }
1124 wake_up(&cdev->private->wait_q);
1125}
1126
1127/**
1128 * qdio_get_ssqd_desc - get qdio subchannel description
1129 * @cdev: ccw device to get description for
1130 *
1131 * Returns a pointer to the saved qdio subchannel description,
1132 * or NULL for not setup qdio devices.
1133 */
1134struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev)
1135{
1136 struct qdio_irq *irq_ptr;
Jan Glauber58eb27c2008-08-21 19:46:34 +02001137 char dbf_text[15];
Jan Glauber779e6e12008-07-17 17:16:48 +02001138
Jan Glauber58eb27c2008-08-21 19:46:34 +02001139 sprintf(dbf_text, "qssq%4x", cdev->private->schid.sch_no);
1140 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001141
1142 irq_ptr = cdev->private->qdio_data;
1143 if (!irq_ptr)
1144 return NULL;
1145
1146 return &irq_ptr->ssqd_desc;
1147}
1148EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1149
1150/**
1151 * qdio_cleanup - shutdown queues and free data structures
1152 * @cdev: associated ccw device
1153 * @how: use halt or clear to shutdown
1154 *
1155 * This function calls qdio_shutdown() for @cdev with method @how
1156 * and on success qdio_free() for @cdev.
1157 */
1158int qdio_cleanup(struct ccw_device *cdev, int how)
1159{
1160 struct qdio_irq *irq_ptr;
1161 char dbf_text[15];
1162 int rc;
1163
Jan Glauber58eb27c2008-08-21 19:46:34 +02001164 sprintf(dbf_text, "qcln%4x", cdev->private->schid.sch_no);
1165 QDIO_DBF_TEXT0(0, setup, dbf_text);
1166
Jan Glauber779e6e12008-07-17 17:16:48 +02001167 irq_ptr = cdev->private->qdio_data;
1168 if (!irq_ptr)
1169 return -ENODEV;
1170
Jan Glauber779e6e12008-07-17 17:16:48 +02001171 rc = qdio_shutdown(cdev, how);
1172 if (rc == 0)
1173 rc = qdio_free(cdev);
1174 return rc;
1175}
1176EXPORT_SYMBOL_GPL(qdio_cleanup);
1177
1178static void qdio_shutdown_queues(struct ccw_device *cdev)
1179{
1180 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1181 struct qdio_q *q;
1182 int i;
1183
1184 for_each_input_queue(irq_ptr, q, i)
1185 tasklet_disable(&q->tasklet);
1186
1187 for_each_output_queue(irq_ptr, q, i) {
1188 tasklet_disable(&q->tasklet);
1189 del_timer(&q->u.out.timer);
1190 }
1191}
1192
1193/**
1194 * qdio_shutdown - shut down a qdio subchannel
1195 * @cdev: associated ccw device
1196 * @how: use halt or clear to shutdown
1197 */
1198int qdio_shutdown(struct ccw_device *cdev, int how)
1199{
1200 struct qdio_irq *irq_ptr;
1201 int rc;
1202 unsigned long flags;
1203 char dbf_text[15];
1204
Jan Glauber58eb27c2008-08-21 19:46:34 +02001205 sprintf(dbf_text, "qshu%4x", cdev->private->schid.sch_no);
1206 QDIO_DBF_TEXT0(0, setup, dbf_text);
1207
Jan Glauber779e6e12008-07-17 17:16:48 +02001208 irq_ptr = cdev->private->qdio_data;
1209 if (!irq_ptr)
1210 return -ENODEV;
1211
1212 mutex_lock(&irq_ptr->setup_mutex);
1213 /*
1214 * Subchannel was already shot down. We cannot prevent being called
1215 * twice since cio may trigger a shutdown asynchronously.
1216 */
1217 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1218 mutex_unlock(&irq_ptr->setup_mutex);
1219 return 0;
1220 }
1221
Jan Glauber779e6e12008-07-17 17:16:48 +02001222 tiqdio_remove_input_queues(irq_ptr);
1223 qdio_shutdown_queues(cdev);
1224 qdio_shutdown_debug_entries(irq_ptr, cdev);
1225
1226 /* cleanup subchannel */
1227 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1228
1229 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1230 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1231 else
1232 /* default behaviour is halt */
1233 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1234 if (rc) {
1235 sprintf(dbf_text, "sher%4x", irq_ptr->schid.sch_no);
1236 QDIO_DBF_TEXT0(0, setup, dbf_text);
1237 sprintf(dbf_text, "rc=%d", rc);
1238 QDIO_DBF_TEXT0(0, setup, dbf_text);
1239 goto no_cleanup;
1240 }
1241
1242 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1243 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1244 wait_event_interruptible_timeout(cdev->private->wait_q,
1245 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1246 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1247 10 * HZ);
1248 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1249
1250no_cleanup:
1251 qdio_shutdown_thinint(irq_ptr);
1252
1253 /* restore interrupt handler */
1254 if ((void *)cdev->handler == (void *)qdio_int_handler)
1255 cdev->handler = irq_ptr->orig_handler;
1256 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1257
1258 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1259 mutex_unlock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001260 if (rc)
1261 return rc;
1262 return 0;
1263}
1264EXPORT_SYMBOL_GPL(qdio_shutdown);
1265
1266/**
1267 * qdio_free - free data structures for a qdio subchannel
1268 * @cdev: associated ccw device
1269 */
1270int qdio_free(struct ccw_device *cdev)
1271{
1272 struct qdio_irq *irq_ptr;
1273 char dbf_text[15];
1274
Jan Glauber58eb27c2008-08-21 19:46:34 +02001275 sprintf(dbf_text, "qfre%4x", cdev->private->schid.sch_no);
1276 QDIO_DBF_TEXT0(0, setup, dbf_text);
1277
Jan Glauber779e6e12008-07-17 17:16:48 +02001278 irq_ptr = cdev->private->qdio_data;
1279 if (!irq_ptr)
1280 return -ENODEV;
1281
1282 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001283 cdev->private->qdio_data = NULL;
1284 mutex_unlock(&irq_ptr->setup_mutex);
1285
1286 qdio_release_memory(irq_ptr);
1287 return 0;
1288}
1289EXPORT_SYMBOL_GPL(qdio_free);
1290
1291/**
1292 * qdio_initialize - allocate and establish queues for a qdio subchannel
1293 * @init_data: initialization data
1294 *
1295 * This function first allocates queues via qdio_allocate() and on success
1296 * establishes them via qdio_establish().
1297 */
1298int qdio_initialize(struct qdio_initialize *init_data)
1299{
1300 int rc;
1301 char dbf_text[15];
1302
1303 sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
1304 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001305
1306 rc = qdio_allocate(init_data);
1307 if (rc)
1308 return rc;
1309
1310 rc = qdio_establish(init_data);
1311 if (rc)
1312 qdio_free(init_data->cdev);
1313 return rc;
1314}
1315EXPORT_SYMBOL_GPL(qdio_initialize);
1316
1317/**
1318 * qdio_allocate - allocate qdio queues and associated data
1319 * @init_data: initialization data
1320 */
1321int qdio_allocate(struct qdio_initialize *init_data)
1322{
1323 struct qdio_irq *irq_ptr;
1324 char dbf_text[15];
1325
1326 sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
1327 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001328
1329 if ((init_data->no_input_qs && !init_data->input_handler) ||
1330 (init_data->no_output_qs && !init_data->output_handler))
1331 return -EINVAL;
1332
1333 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1334 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1335 return -EINVAL;
1336
1337 if ((!init_data->input_sbal_addr_array) ||
1338 (!init_data->output_sbal_addr_array))
1339 return -EINVAL;
1340
1341 qdio_allocate_do_dbf(init_data);
1342
1343 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1344 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1345 if (!irq_ptr)
1346 goto out_err;
1347 QDIO_DBF_TEXT0(0, setup, "irq_ptr:");
1348 QDIO_DBF_HEX0(0, setup, &irq_ptr, sizeof(void *));
1349
1350 mutex_init(&irq_ptr->setup_mutex);
1351
1352 /*
1353 * Allocate a page for the chsc calls in qdio_establish.
1354 * Must be pre-allocated since a zfcp recovery will call
1355 * qdio_establish. In case of low memory and swap on a zfcp disk
1356 * we may not be able to allocate memory otherwise.
1357 */
1358 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1359 if (!irq_ptr->chsc_page)
1360 goto out_rel;
1361
1362 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001363 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001364 if (!irq_ptr->qdr)
1365 goto out_rel;
1366 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1367
1368 QDIO_DBF_TEXT0(0, setup, "qdr:");
1369 QDIO_DBF_HEX0(0, setup, &irq_ptr->qdr, sizeof(void *));
1370
1371 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1372 init_data->no_output_qs))
1373 goto out_rel;
1374
1375 init_data->cdev->private->qdio_data = irq_ptr;
1376 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1377 return 0;
1378out_rel:
1379 qdio_release_memory(irq_ptr);
1380out_err:
1381 return -ENOMEM;
1382}
1383EXPORT_SYMBOL_GPL(qdio_allocate);
1384
1385/**
1386 * qdio_establish - establish queues on a qdio subchannel
1387 * @init_data: initialization data
1388 */
1389int qdio_establish(struct qdio_initialize *init_data)
1390{
1391 char dbf_text[20];
1392 struct qdio_irq *irq_ptr;
1393 struct ccw_device *cdev = init_data->cdev;
1394 unsigned long saveflags;
1395 int rc;
1396
Jan Glauber58eb27c2008-08-21 19:46:34 +02001397 sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
1398 QDIO_DBF_TEXT0(0, setup, dbf_text);
1399
Jan Glauber779e6e12008-07-17 17:16:48 +02001400 irq_ptr = cdev->private->qdio_data;
1401 if (!irq_ptr)
1402 return -ENODEV;
1403
1404 if (cdev->private->state != DEV_STATE_ONLINE)
1405 return -EINVAL;
1406
Jan Glauber779e6e12008-07-17 17:16:48 +02001407 mutex_lock(&irq_ptr->setup_mutex);
1408 qdio_setup_irq(init_data);
1409
1410 rc = qdio_establish_thinint(irq_ptr);
1411 if (rc) {
1412 mutex_unlock(&irq_ptr->setup_mutex);
1413 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1414 return rc;
1415 }
1416
1417 /* establish q */
1418 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1419 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1420 irq_ptr->ccw.count = irq_ptr->equeue.count;
1421 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1422
1423 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1424 ccw_device_set_options_mask(cdev, 0);
1425
1426 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1427 if (rc) {
1428 sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
1429 QDIO_DBF_TEXT2(1, setup, dbf_text);
1430 sprintf(dbf_text, "eq:rc%4x", rc);
1431 QDIO_DBF_TEXT2(1, setup, dbf_text);
1432 }
1433 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1434
1435 if (rc) {
1436 mutex_unlock(&irq_ptr->setup_mutex);
1437 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1438 return rc;
1439 }
1440
1441 wait_event_interruptible_timeout(cdev->private->wait_q,
1442 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1443 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1444
1445 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1446 mutex_unlock(&irq_ptr->setup_mutex);
1447 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1448 return -EIO;
1449 }
1450
1451 qdio_setup_ssqd_info(irq_ptr);
1452 sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
1453 QDIO_DBF_TEXT2(0, setup, dbf_text);
1454
1455 /* qebsm is now setup if available, initialize buffer states */
1456 qdio_init_buf_states(irq_ptr);
1457
1458 mutex_unlock(&irq_ptr->setup_mutex);
1459 qdio_print_subchannel_info(irq_ptr, cdev);
1460 qdio_setup_debug_entries(irq_ptr, cdev);
1461 return 0;
1462}
1463EXPORT_SYMBOL_GPL(qdio_establish);
1464
1465/**
1466 * qdio_activate - activate queues on a qdio subchannel
1467 * @cdev: associated cdev
1468 */
1469int qdio_activate(struct ccw_device *cdev)
1470{
1471 struct qdio_irq *irq_ptr;
1472 int rc;
1473 unsigned long saveflags;
1474 char dbf_text[20];
1475
Jan Glauber58eb27c2008-08-21 19:46:34 +02001476 sprintf(dbf_text, "qact%4x", cdev->private->schid.sch_no);
1477 QDIO_DBF_TEXT0(0, setup, dbf_text);
1478
Jan Glauber779e6e12008-07-17 17:16:48 +02001479 irq_ptr = cdev->private->qdio_data;
1480 if (!irq_ptr)
1481 return -ENODEV;
1482
1483 if (cdev->private->state != DEV_STATE_ONLINE)
1484 return -EINVAL;
1485
1486 mutex_lock(&irq_ptr->setup_mutex);
1487 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1488 rc = -EBUSY;
1489 goto out;
1490 }
1491
Jan Glauber779e6e12008-07-17 17:16:48 +02001492 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1493 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1494 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1495 irq_ptr->ccw.cda = 0;
1496
1497 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1498 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1499
1500 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1501 0, DOIO_DENY_PREFETCH);
1502 if (rc) {
1503 sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
1504 QDIO_DBF_TEXT2(1, setup, dbf_text);
1505 sprintf(dbf_text, "aq:rc%4x", rc);
1506 QDIO_DBF_TEXT2(1, setup, dbf_text);
1507 }
1508 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1509
1510 if (rc)
1511 goto out;
1512
1513 if (is_thinint_irq(irq_ptr))
1514 tiqdio_add_input_queues(irq_ptr);
1515
1516 /* wait for subchannel to become active */
1517 msleep(5);
1518
1519 switch (irq_ptr->state) {
1520 case QDIO_IRQ_STATE_STOPPED:
1521 case QDIO_IRQ_STATE_ERR:
1522 mutex_unlock(&irq_ptr->setup_mutex);
1523 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1524 return -EIO;
1525 default:
1526 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1527 rc = 0;
1528 }
1529out:
1530 mutex_unlock(&irq_ptr->setup_mutex);
1531 return rc;
1532}
1533EXPORT_SYMBOL_GPL(qdio_activate);
1534
1535static inline int buf_in_between(int bufnr, int start, int count)
1536{
1537 int end = add_buf(start, count);
1538
1539 if (end > start) {
1540 if (bufnr >= start && bufnr < end)
1541 return 1;
1542 else
1543 return 0;
1544 }
1545
1546 /* wrap-around case */
1547 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1548 (bufnr < end))
1549 return 1;
1550 else
1551 return 0;
1552}
1553
1554/**
1555 * handle_inbound - reset processed input buffers
1556 * @q: queue containing the buffers
1557 * @callflags: flags
1558 * @bufnr: first buffer to process
1559 * @count: how many buffers are emptied
1560 */
1561static void handle_inbound(struct qdio_q *q, unsigned int callflags,
1562 int bufnr, int count)
1563{
1564 unsigned long flags;
1565 int used, rc;
1566
1567 /*
1568 * do_QDIO could run in parallel with the queue tasklet so the
1569 * upper-layer programm could empty the ACK'ed buffer here.
1570 * If that happens we must clear the polling flag, otherwise
1571 * qdio_stop_polling() could set the buffer to NOT_INIT after
1572 * it was set to EMPTY which would kill us.
1573 */
1574 spin_lock_irqsave(&q->u.in.lock, flags);
1575 if (q->u.in.polling)
1576 if (buf_in_between(q->last_move_ftc, bufnr, count))
1577 q->u.in.polling = 0;
1578
1579 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1580 spin_unlock_irqrestore(&q->u.in.lock, flags);
1581
1582 used = atomic_add_return(count, &q->nr_buf_used) - count;
1583 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1584
1585 /* no need to signal as long as the adapter had free buffers */
1586 if (used)
1587 return;
1588
1589 if (need_siga_in(q)) {
1590 rc = qdio_siga_input(q);
1591 if (rc)
1592 q->qdio_error = rc;
1593 }
1594}
1595
1596/**
1597 * handle_outbound - process filled outbound buffers
1598 * @q: queue containing the buffers
1599 * @callflags: flags
1600 * @bufnr: first buffer to process
1601 * @count: how many buffers are filled
1602 */
1603static void handle_outbound(struct qdio_q *q, unsigned int callflags,
1604 int bufnr, int count)
1605{
1606 unsigned char state;
1607 int used;
1608
1609 qdio_perf_stat_inc(&perf_stats.outbound_handler);
1610
1611 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1612 used = atomic_add_return(count, &q->nr_buf_used);
1613 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1614
1615 if (callflags & QDIO_FLAG_PCI_OUT)
1616 q->u.out.pci_out_enabled = 1;
1617 else
1618 q->u.out.pci_out_enabled = 0;
1619
1620 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1621 if (multicast_outbound(q))
1622 qdio_kick_outbound_q(q);
1623 else
1624 /*
1625 * One siga-w per buffer required for unicast
1626 * HiperSockets.
1627 */
1628 while (count--)
1629 qdio_kick_outbound_q(q);
1630 goto out;
1631 }
1632
1633 if (need_siga_sync(q)) {
1634 qdio_siga_sync_q(q);
1635 goto out;
1636 }
1637
1638 /* try to fast requeue buffers */
1639 get_buf_state(q, prev_buf(bufnr), &state);
1640 if (state != SLSB_CU_OUTPUT_PRIMED)
1641 qdio_kick_outbound_q(q);
1642 else {
1643 QDIO_DBF_TEXT5(0, trace, "fast-req");
1644 qdio_perf_stat_inc(&perf_stats.fast_requeue);
1645 }
1646out:
1647 /* Fixme: could wait forever if called from process context */
1648 tasklet_schedule(&q->tasklet);
1649}
1650
1651/**
1652 * do_QDIO - process input or output buffers
1653 * @cdev: associated ccw_device for the qdio subchannel
1654 * @callflags: input or output and special flags from the program
1655 * @q_nr: queue number
1656 * @bufnr: buffer number
1657 * @count: how many buffers to process
1658 */
1659int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1660 int q_nr, int bufnr, int count)
1661{
1662 struct qdio_irq *irq_ptr;
1663#ifdef CONFIG_QDIO_DEBUG
1664 char dbf_text[20];
1665
Jan Glauber58eb27c2008-08-21 19:46:34 +02001666 sprintf(dbf_text, "doQD%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001667 QDIO_DBF_TEXT3(0, trace, dbf_text);
1668#endif /* CONFIG_QDIO_DEBUG */
1669
1670 if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
1671 (count > QDIO_MAX_BUFFERS_PER_Q) ||
1672 (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
1673 return -EINVAL;
1674
1675 if (!count)
1676 return 0;
1677
1678 irq_ptr = cdev->private->qdio_data;
1679 if (!irq_ptr)
1680 return -ENODEV;
1681
1682#ifdef CONFIG_QDIO_DEBUG
1683 if (callflags & QDIO_FLAG_SYNC_INPUT)
1684 QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
1685 sizeof(void *));
1686 else
1687 QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
1688 sizeof(void *));
1689
1690 sprintf(dbf_text, "flag%04x", callflags);
1691 QDIO_DBF_TEXT3(0, trace, dbf_text);
1692 sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
1693 QDIO_DBF_TEXT3(0, trace, dbf_text);
1694#endif /* CONFIG_QDIO_DEBUG */
1695
1696 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1697 return -EBUSY;
1698
1699 if (callflags & QDIO_FLAG_SYNC_INPUT)
1700 handle_inbound(irq_ptr->input_qs[q_nr],
1701 callflags, bufnr, count);
1702 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1703 handle_outbound(irq_ptr->output_qs[q_nr],
1704 callflags, bufnr, count);
1705 else {
1706 QDIO_DBF_TEXT3(1, trace, "doQD:inv");
1707 return -EINVAL;
1708 }
1709 return 0;
1710}
1711EXPORT_SYMBOL_GPL(do_QDIO);
1712
1713static int __init init_QDIO(void)
1714{
1715 int rc;
1716
1717 rc = qdio_setup_init();
1718 if (rc)
1719 return rc;
1720 rc = tiqdio_allocate_memory();
1721 if (rc)
1722 goto out_cache;
1723 rc = qdio_debug_init();
1724 if (rc)
1725 goto out_ti;
1726 rc = qdio_setup_perf_stats();
1727 if (rc)
1728 goto out_debug;
1729 rc = tiqdio_register_thinints();
1730 if (rc)
1731 goto out_perf;
1732 return 0;
1733
1734out_perf:
1735 qdio_remove_perf_stats();
1736out_debug:
1737 qdio_debug_exit();
1738out_ti:
1739 tiqdio_free_memory();
1740out_cache:
1741 qdio_setup_exit();
1742 return rc;
1743}
1744
1745static void __exit exit_QDIO(void)
1746{
1747 tiqdio_unregister_thinints();
1748 tiqdio_free_memory();
1749 qdio_remove_perf_stats();
1750 qdio_debug_exit();
1751 qdio_setup_exit();
1752}
1753
1754module_init(init_QDIO);
1755module_exit(exit_QDIO);