blob: 444fc7e521d47b89a0d94b9e8cd4b999d0e1c269 [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
854 if (q->u.out.pci_out_enabled)
855 return;
856
857 /*
858 * Now we know that queue type is either qeth without pci enabled
859 * or HiperSockets multicast. Make sure buffer switch from PRIMED to
860 * EMPTY is noticed and outbound_handler is called after some time.
861 */
862 if (qdio_outbound_q_done(q))
863 del_timer(&q->u.out.timer);
864 else {
865 if (!timer_pending(&q->u.out.timer)) {
866 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
867 qdio_perf_stat_inc(&perf_stats.debug_tl_out_timer);
868 }
869 }
870}
871
872/* outbound tasklet */
873void qdio_outbound_processing(unsigned long data)
874{
875 struct qdio_q *q = (struct qdio_q *)data;
876 __qdio_outbound_processing(q);
877}
878
879void qdio_outbound_timer(unsigned long data)
880{
881 struct qdio_q *q = (struct qdio_q *)data;
882 tasklet_schedule(&q->tasklet);
883}
884
885/* called from thinint inbound tasklet */
886void qdio_check_outbound_after_thinint(struct qdio_q *q)
887{
888 struct qdio_q *out;
889 int i;
890
891 if (!pci_out_supported(q))
892 return;
893
894 for_each_output_queue(q->irq_ptr, out, i)
895 if (!qdio_outbound_q_done(out))
896 tasklet_schedule(&out->tasklet);
897}
898
899static inline void qdio_set_state(struct qdio_irq *irq_ptr,
900 enum qdio_irq_states state)
901{
902#ifdef CONFIG_QDIO_DEBUG
903 char dbf_text[15];
904
905 QDIO_DBF_TEXT5(0, trace, "newstate");
906 sprintf(dbf_text, "%4x%4x", irq_ptr->schid.sch_no, state);
907 QDIO_DBF_TEXT5(0, trace, dbf_text);
908#endif /* CONFIG_QDIO_DEBUG */
909
910 irq_ptr->state = state;
911 mb();
912}
913
914static void qdio_irq_check_sense(struct subchannel_id schid, struct irb *irb)
915{
916 char dbf_text[15];
917
918 if (irb->esw.esw0.erw.cons) {
919 sprintf(dbf_text, "sens%4x", schid.sch_no);
920 QDIO_DBF_TEXT2(1, trace, dbf_text);
921 QDIO_DBF_HEX0(0, trace, irb, 64);
922 QDIO_DBF_HEX0(0, trace, irb->ecw, 64);
923 }
924}
925
926/* PCI interrupt handler */
927static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
928{
929 int i;
930 struct qdio_q *q;
931
932 qdio_perf_stat_inc(&perf_stats.pci_int);
933
934 for_each_input_queue(irq_ptr, q, i)
935 tasklet_schedule(&q->tasklet);
936
937 if (!(irq_ptr->qib.ac & QIB_AC_OUTBOUND_PCI_SUPPORTED))
938 return;
939
940 for_each_output_queue(irq_ptr, q, i) {
941 if (qdio_outbound_q_done(q))
942 continue;
943
944 if (!siga_syncs_out_pci(q))
945 qdio_siga_sync_q(q);
946
947 tasklet_schedule(&q->tasklet);
948 }
949}
950
951static void qdio_handle_activate_check(struct ccw_device *cdev,
952 unsigned long intparm, int cstat, int dstat)
953{
954 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
955 struct qdio_q *q;
956 char dbf_text[15];
957
958 QDIO_DBF_TEXT2(1, trace, "ick2");
959 sprintf(dbf_text, "%s", cdev->dev.bus_id);
960 QDIO_DBF_TEXT2(1, trace, dbf_text);
961 QDIO_DBF_HEX2(0, trace, &intparm, sizeof(int));
962 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
963 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
964
965 if (irq_ptr->nr_input_qs) {
966 q = irq_ptr->input_qs[0];
967 } else if (irq_ptr->nr_output_qs) {
968 q = irq_ptr->output_qs[0];
969 } else {
970 dump_stack();
971 goto no_handler;
972 }
973 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE_CHECK_CONDITION,
974 0, -1, -1, irq_ptr->int_parm);
975no_handler:
976 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
977}
978
979static void qdio_call_shutdown(struct work_struct *work)
980{
981 struct ccw_device_private *priv;
982 struct ccw_device *cdev;
983
984 priv = container_of(work, struct ccw_device_private, kick_work);
985 cdev = priv->cdev;
986 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
987 put_device(&cdev->dev);
988}
989
990static void qdio_int_error(struct ccw_device *cdev)
991{
992 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
993
994 switch (irq_ptr->state) {
995 case QDIO_IRQ_STATE_INACTIVE:
996 case QDIO_IRQ_STATE_CLEANUP:
997 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
998 break;
999 case QDIO_IRQ_STATE_ESTABLISHED:
1000 case QDIO_IRQ_STATE_ACTIVE:
1001 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1002 if (get_device(&cdev->dev)) {
1003 /* Can't call shutdown from interrupt context. */
1004 PREPARE_WORK(&cdev->private->kick_work,
1005 qdio_call_shutdown);
1006 queue_work(ccw_device_work, &cdev->private->kick_work);
1007 }
1008 break;
1009 default:
1010 WARN_ON(1);
1011 }
1012 wake_up(&cdev->private->wait_q);
1013}
1014
1015static int qdio_establish_check_errors(struct ccw_device *cdev, int cstat,
1016 int dstat)
1017{
1018 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1019
1020 if (cstat || (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))) {
1021 QDIO_DBF_TEXT2(1, setup, "eq:ckcon");
1022 goto error;
1023 }
1024
1025 if (!(dstat & DEV_STAT_DEV_END)) {
1026 QDIO_DBF_TEXT2(1, setup, "eq:no de");
1027 goto error;
1028 }
1029
1030 if (dstat & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END)) {
1031 QDIO_DBF_TEXT2(1, setup, "eq:badio");
1032 goto error;
1033 }
1034 return 0;
1035error:
1036 QDIO_DBF_HEX2(0, trace, &cstat, sizeof(int));
1037 QDIO_DBF_HEX2(0, trace, &dstat, sizeof(int));
1038 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1039 return 1;
1040}
1041
1042static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1043 int dstat)
1044{
1045 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1046 char dbf_text[15];
1047
1048 sprintf(dbf_text, "qehi%4x", cdev->private->schid.sch_no);
1049 QDIO_DBF_TEXT0(0, setup, dbf_text);
1050 QDIO_DBF_TEXT0(0, trace, dbf_text);
1051
1052 if (!qdio_establish_check_errors(cdev, cstat, dstat))
1053 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1054}
1055
1056/* qdio interrupt handler */
1057void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1058 struct irb *irb)
1059{
1060 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1061 int cstat, dstat;
1062 char dbf_text[15];
1063
1064 qdio_perf_stat_inc(&perf_stats.qdio_int);
1065
1066 if (!intparm || !irq_ptr) {
1067 sprintf(dbf_text, "qihd%4x", cdev->private->schid.sch_no);
1068 QDIO_DBF_TEXT2(1, setup, dbf_text);
1069 return;
1070 }
1071
1072 if (IS_ERR(irb)) {
1073 switch (PTR_ERR(irb)) {
1074 case -EIO:
Jan Glauber58eb27c2008-08-21 19:46:34 +02001075 sprintf(dbf_text, "ierr%4x", irq_ptr->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001076 QDIO_DBF_TEXT2(1, setup, dbf_text);
1077 qdio_int_error(cdev);
1078 return;
1079 case -ETIMEDOUT:
Jan Glauber58eb27c2008-08-21 19:46:34 +02001080 sprintf(dbf_text, "qtoh%4x", irq_ptr->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001081 QDIO_DBF_TEXT2(1, setup, dbf_text);
1082 qdio_int_error(cdev);
1083 return;
1084 default:
1085 WARN_ON(1);
1086 return;
1087 }
1088 }
1089 qdio_irq_check_sense(irq_ptr->schid, irb);
1090
1091 cstat = irb->scsw.cmd.cstat;
1092 dstat = irb->scsw.cmd.dstat;
1093
1094 switch (irq_ptr->state) {
1095 case QDIO_IRQ_STATE_INACTIVE:
1096 qdio_establish_handle_irq(cdev, cstat, dstat);
1097 break;
1098
1099 case QDIO_IRQ_STATE_CLEANUP:
1100 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1101 break;
1102
1103 case QDIO_IRQ_STATE_ESTABLISHED:
1104 case QDIO_IRQ_STATE_ACTIVE:
1105 if (cstat & SCHN_STAT_PCI) {
1106 qdio_int_handler_pci(irq_ptr);
1107 /* no state change so no need to wake up wait_q */
1108 return;
1109 }
1110 if ((cstat & ~SCHN_STAT_PCI) || dstat) {
1111 qdio_handle_activate_check(cdev, intparm, cstat,
1112 dstat);
1113 break;
1114 }
1115 default:
1116 WARN_ON(1);
1117 }
1118 wake_up(&cdev->private->wait_q);
1119}
1120
1121/**
1122 * qdio_get_ssqd_desc - get qdio subchannel description
1123 * @cdev: ccw device to get description for
1124 *
1125 * Returns a pointer to the saved qdio subchannel description,
1126 * or NULL for not setup qdio devices.
1127 */
1128struct qdio_ssqd_desc *qdio_get_ssqd_desc(struct ccw_device *cdev)
1129{
1130 struct qdio_irq *irq_ptr;
Jan Glauber58eb27c2008-08-21 19:46:34 +02001131 char dbf_text[15];
Jan Glauber779e6e12008-07-17 17:16:48 +02001132
Jan Glauber58eb27c2008-08-21 19:46:34 +02001133 sprintf(dbf_text, "qssq%4x", cdev->private->schid.sch_no);
1134 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001135
1136 irq_ptr = cdev->private->qdio_data;
1137 if (!irq_ptr)
1138 return NULL;
1139
1140 return &irq_ptr->ssqd_desc;
1141}
1142EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1143
1144/**
1145 * qdio_cleanup - shutdown queues and free data structures
1146 * @cdev: associated ccw device
1147 * @how: use halt or clear to shutdown
1148 *
1149 * This function calls qdio_shutdown() for @cdev with method @how
1150 * and on success qdio_free() for @cdev.
1151 */
1152int qdio_cleanup(struct ccw_device *cdev, int how)
1153{
1154 struct qdio_irq *irq_ptr;
1155 char dbf_text[15];
1156 int rc;
1157
Jan Glauber58eb27c2008-08-21 19:46:34 +02001158 sprintf(dbf_text, "qcln%4x", cdev->private->schid.sch_no);
1159 QDIO_DBF_TEXT0(0, setup, dbf_text);
1160
Jan Glauber779e6e12008-07-17 17:16:48 +02001161 irq_ptr = cdev->private->qdio_data;
1162 if (!irq_ptr)
1163 return -ENODEV;
1164
Jan Glauber779e6e12008-07-17 17:16:48 +02001165 rc = qdio_shutdown(cdev, how);
1166 if (rc == 0)
1167 rc = qdio_free(cdev);
1168 return rc;
1169}
1170EXPORT_SYMBOL_GPL(qdio_cleanup);
1171
1172static void qdio_shutdown_queues(struct ccw_device *cdev)
1173{
1174 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1175 struct qdio_q *q;
1176 int i;
1177
1178 for_each_input_queue(irq_ptr, q, i)
1179 tasklet_disable(&q->tasklet);
1180
1181 for_each_output_queue(irq_ptr, q, i) {
1182 tasklet_disable(&q->tasklet);
1183 del_timer(&q->u.out.timer);
1184 }
1185}
1186
1187/**
1188 * qdio_shutdown - shut down a qdio subchannel
1189 * @cdev: associated ccw device
1190 * @how: use halt or clear to shutdown
1191 */
1192int qdio_shutdown(struct ccw_device *cdev, int how)
1193{
1194 struct qdio_irq *irq_ptr;
1195 int rc;
1196 unsigned long flags;
1197 char dbf_text[15];
1198
Jan Glauber58eb27c2008-08-21 19:46:34 +02001199 sprintf(dbf_text, "qshu%4x", cdev->private->schid.sch_no);
1200 QDIO_DBF_TEXT0(0, setup, dbf_text);
1201
Jan Glauber779e6e12008-07-17 17:16:48 +02001202 irq_ptr = cdev->private->qdio_data;
1203 if (!irq_ptr)
1204 return -ENODEV;
1205
1206 mutex_lock(&irq_ptr->setup_mutex);
1207 /*
1208 * Subchannel was already shot down. We cannot prevent being called
1209 * twice since cio may trigger a shutdown asynchronously.
1210 */
1211 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1212 mutex_unlock(&irq_ptr->setup_mutex);
1213 return 0;
1214 }
1215
Jan Glauber779e6e12008-07-17 17:16:48 +02001216 tiqdio_remove_input_queues(irq_ptr);
1217 qdio_shutdown_queues(cdev);
1218 qdio_shutdown_debug_entries(irq_ptr, cdev);
1219
1220 /* cleanup subchannel */
1221 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1222
1223 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1224 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1225 else
1226 /* default behaviour is halt */
1227 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1228 if (rc) {
1229 sprintf(dbf_text, "sher%4x", irq_ptr->schid.sch_no);
1230 QDIO_DBF_TEXT0(0, setup, dbf_text);
1231 sprintf(dbf_text, "rc=%d", rc);
1232 QDIO_DBF_TEXT0(0, setup, dbf_text);
1233 goto no_cleanup;
1234 }
1235
1236 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1237 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1238 wait_event_interruptible_timeout(cdev->private->wait_q,
1239 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1240 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1241 10 * HZ);
1242 spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
1243
1244no_cleanup:
1245 qdio_shutdown_thinint(irq_ptr);
1246
1247 /* restore interrupt handler */
1248 if ((void *)cdev->handler == (void *)qdio_int_handler)
1249 cdev->handler = irq_ptr->orig_handler;
1250 spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
1251
1252 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1253 mutex_unlock(&irq_ptr->setup_mutex);
1254 module_put(THIS_MODULE);
1255 if (rc)
1256 return rc;
1257 return 0;
1258}
1259EXPORT_SYMBOL_GPL(qdio_shutdown);
1260
1261/**
1262 * qdio_free - free data structures for a qdio subchannel
1263 * @cdev: associated ccw device
1264 */
1265int qdio_free(struct ccw_device *cdev)
1266{
1267 struct qdio_irq *irq_ptr;
1268 char dbf_text[15];
1269
Jan Glauber58eb27c2008-08-21 19:46:34 +02001270 sprintf(dbf_text, "qfre%4x", cdev->private->schid.sch_no);
1271 QDIO_DBF_TEXT0(0, setup, dbf_text);
1272
Jan Glauber779e6e12008-07-17 17:16:48 +02001273 irq_ptr = cdev->private->qdio_data;
1274 if (!irq_ptr)
1275 return -ENODEV;
1276
1277 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001278 cdev->private->qdio_data = NULL;
1279 mutex_unlock(&irq_ptr->setup_mutex);
1280
1281 qdio_release_memory(irq_ptr);
1282 return 0;
1283}
1284EXPORT_SYMBOL_GPL(qdio_free);
1285
1286/**
1287 * qdio_initialize - allocate and establish queues for a qdio subchannel
1288 * @init_data: initialization data
1289 *
1290 * This function first allocates queues via qdio_allocate() and on success
1291 * establishes them via qdio_establish().
1292 */
1293int qdio_initialize(struct qdio_initialize *init_data)
1294{
1295 int rc;
1296 char dbf_text[15];
1297
1298 sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
1299 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001300
1301 rc = qdio_allocate(init_data);
1302 if (rc)
1303 return rc;
1304
1305 rc = qdio_establish(init_data);
1306 if (rc)
1307 qdio_free(init_data->cdev);
1308 return rc;
1309}
1310EXPORT_SYMBOL_GPL(qdio_initialize);
1311
1312/**
1313 * qdio_allocate - allocate qdio queues and associated data
1314 * @init_data: initialization data
1315 */
1316int qdio_allocate(struct qdio_initialize *init_data)
1317{
1318 struct qdio_irq *irq_ptr;
1319 char dbf_text[15];
1320
1321 sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
1322 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001323
1324 if ((init_data->no_input_qs && !init_data->input_handler) ||
1325 (init_data->no_output_qs && !init_data->output_handler))
1326 return -EINVAL;
1327
1328 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1329 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1330 return -EINVAL;
1331
1332 if ((!init_data->input_sbal_addr_array) ||
1333 (!init_data->output_sbal_addr_array))
1334 return -EINVAL;
1335
1336 qdio_allocate_do_dbf(init_data);
1337
1338 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1339 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1340 if (!irq_ptr)
1341 goto out_err;
1342 QDIO_DBF_TEXT0(0, setup, "irq_ptr:");
1343 QDIO_DBF_HEX0(0, setup, &irq_ptr, sizeof(void *));
1344
1345 mutex_init(&irq_ptr->setup_mutex);
1346
1347 /*
1348 * Allocate a page for the chsc calls in qdio_establish.
1349 * Must be pre-allocated since a zfcp recovery will call
1350 * qdio_establish. In case of low memory and swap on a zfcp disk
1351 * we may not be able to allocate memory otherwise.
1352 */
1353 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1354 if (!irq_ptr->chsc_page)
1355 goto out_rel;
1356
1357 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001358 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001359 if (!irq_ptr->qdr)
1360 goto out_rel;
1361 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1362
1363 QDIO_DBF_TEXT0(0, setup, "qdr:");
1364 QDIO_DBF_HEX0(0, setup, &irq_ptr->qdr, sizeof(void *));
1365
1366 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1367 init_data->no_output_qs))
1368 goto out_rel;
1369
1370 init_data->cdev->private->qdio_data = irq_ptr;
1371 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1372 return 0;
1373out_rel:
1374 qdio_release_memory(irq_ptr);
1375out_err:
1376 return -ENOMEM;
1377}
1378EXPORT_SYMBOL_GPL(qdio_allocate);
1379
1380/**
1381 * qdio_establish - establish queues on a qdio subchannel
1382 * @init_data: initialization data
1383 */
1384int qdio_establish(struct qdio_initialize *init_data)
1385{
1386 char dbf_text[20];
1387 struct qdio_irq *irq_ptr;
1388 struct ccw_device *cdev = init_data->cdev;
1389 unsigned long saveflags;
1390 int rc;
1391
Jan Glauber58eb27c2008-08-21 19:46:34 +02001392 sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
1393 QDIO_DBF_TEXT0(0, setup, dbf_text);
1394
Jan Glauber779e6e12008-07-17 17:16:48 +02001395 irq_ptr = cdev->private->qdio_data;
1396 if (!irq_ptr)
1397 return -ENODEV;
1398
1399 if (cdev->private->state != DEV_STATE_ONLINE)
1400 return -EINVAL;
1401
1402 if (!try_module_get(THIS_MODULE))
1403 return -EINVAL;
1404
Jan Glauber779e6e12008-07-17 17:16:48 +02001405 mutex_lock(&irq_ptr->setup_mutex);
1406 qdio_setup_irq(init_data);
1407
1408 rc = qdio_establish_thinint(irq_ptr);
1409 if (rc) {
1410 mutex_unlock(&irq_ptr->setup_mutex);
1411 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1412 return rc;
1413 }
1414
1415 /* establish q */
1416 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1417 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1418 irq_ptr->ccw.count = irq_ptr->equeue.count;
1419 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1420
1421 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1422 ccw_device_set_options_mask(cdev, 0);
1423
1424 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1425 if (rc) {
1426 sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
1427 QDIO_DBF_TEXT2(1, setup, dbf_text);
1428 sprintf(dbf_text, "eq:rc%4x", rc);
1429 QDIO_DBF_TEXT2(1, setup, dbf_text);
1430 }
1431 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1432
1433 if (rc) {
1434 mutex_unlock(&irq_ptr->setup_mutex);
1435 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1436 return rc;
1437 }
1438
1439 wait_event_interruptible_timeout(cdev->private->wait_q,
1440 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1441 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1442
1443 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1444 mutex_unlock(&irq_ptr->setup_mutex);
1445 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1446 return -EIO;
1447 }
1448
1449 qdio_setup_ssqd_info(irq_ptr);
1450 sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
1451 QDIO_DBF_TEXT2(0, setup, dbf_text);
1452
1453 /* qebsm is now setup if available, initialize buffer states */
1454 qdio_init_buf_states(irq_ptr);
1455
1456 mutex_unlock(&irq_ptr->setup_mutex);
1457 qdio_print_subchannel_info(irq_ptr, cdev);
1458 qdio_setup_debug_entries(irq_ptr, cdev);
1459 return 0;
1460}
1461EXPORT_SYMBOL_GPL(qdio_establish);
1462
1463/**
1464 * qdio_activate - activate queues on a qdio subchannel
1465 * @cdev: associated cdev
1466 */
1467int qdio_activate(struct ccw_device *cdev)
1468{
1469 struct qdio_irq *irq_ptr;
1470 int rc;
1471 unsigned long saveflags;
1472 char dbf_text[20];
1473
Jan Glauber58eb27c2008-08-21 19:46:34 +02001474 sprintf(dbf_text, "qact%4x", cdev->private->schid.sch_no);
1475 QDIO_DBF_TEXT0(0, setup, dbf_text);
1476
Jan Glauber779e6e12008-07-17 17:16:48 +02001477 irq_ptr = cdev->private->qdio_data;
1478 if (!irq_ptr)
1479 return -ENODEV;
1480
1481 if (cdev->private->state != DEV_STATE_ONLINE)
1482 return -EINVAL;
1483
1484 mutex_lock(&irq_ptr->setup_mutex);
1485 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1486 rc = -EBUSY;
1487 goto out;
1488 }
1489
Jan Glauber779e6e12008-07-17 17:16:48 +02001490 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1491 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1492 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1493 irq_ptr->ccw.cda = 0;
1494
1495 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1496 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1497
1498 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1499 0, DOIO_DENY_PREFETCH);
1500 if (rc) {
1501 sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
1502 QDIO_DBF_TEXT2(1, setup, dbf_text);
1503 sprintf(dbf_text, "aq:rc%4x", rc);
1504 QDIO_DBF_TEXT2(1, setup, dbf_text);
1505 }
1506 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1507
1508 if (rc)
1509 goto out;
1510
1511 if (is_thinint_irq(irq_ptr))
1512 tiqdio_add_input_queues(irq_ptr);
1513
1514 /* wait for subchannel to become active */
1515 msleep(5);
1516
1517 switch (irq_ptr->state) {
1518 case QDIO_IRQ_STATE_STOPPED:
1519 case QDIO_IRQ_STATE_ERR:
1520 mutex_unlock(&irq_ptr->setup_mutex);
1521 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1522 return -EIO;
1523 default:
1524 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1525 rc = 0;
1526 }
1527out:
1528 mutex_unlock(&irq_ptr->setup_mutex);
1529 return rc;
1530}
1531EXPORT_SYMBOL_GPL(qdio_activate);
1532
1533static inline int buf_in_between(int bufnr, int start, int count)
1534{
1535 int end = add_buf(start, count);
1536
1537 if (end > start) {
1538 if (bufnr >= start && bufnr < end)
1539 return 1;
1540 else
1541 return 0;
1542 }
1543
1544 /* wrap-around case */
1545 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1546 (bufnr < end))
1547 return 1;
1548 else
1549 return 0;
1550}
1551
1552/**
1553 * handle_inbound - reset processed input buffers
1554 * @q: queue containing the buffers
1555 * @callflags: flags
1556 * @bufnr: first buffer to process
1557 * @count: how many buffers are emptied
1558 */
1559static void handle_inbound(struct qdio_q *q, unsigned int callflags,
1560 int bufnr, int count)
1561{
1562 unsigned long flags;
1563 int used, rc;
1564
1565 /*
1566 * do_QDIO could run in parallel with the queue tasklet so the
1567 * upper-layer programm could empty the ACK'ed buffer here.
1568 * If that happens we must clear the polling flag, otherwise
1569 * qdio_stop_polling() could set the buffer to NOT_INIT after
1570 * it was set to EMPTY which would kill us.
1571 */
1572 spin_lock_irqsave(&q->u.in.lock, flags);
1573 if (q->u.in.polling)
1574 if (buf_in_between(q->last_move_ftc, bufnr, count))
1575 q->u.in.polling = 0;
1576
1577 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1578 spin_unlock_irqrestore(&q->u.in.lock, flags);
1579
1580 used = atomic_add_return(count, &q->nr_buf_used) - count;
1581 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1582
1583 /* no need to signal as long as the adapter had free buffers */
1584 if (used)
1585 return;
1586
1587 if (need_siga_in(q)) {
1588 rc = qdio_siga_input(q);
1589 if (rc)
1590 q->qdio_error = rc;
1591 }
1592}
1593
1594/**
1595 * handle_outbound - process filled outbound buffers
1596 * @q: queue containing the buffers
1597 * @callflags: flags
1598 * @bufnr: first buffer to process
1599 * @count: how many buffers are filled
1600 */
1601static void handle_outbound(struct qdio_q *q, unsigned int callflags,
1602 int bufnr, int count)
1603{
1604 unsigned char state;
1605 int used;
1606
1607 qdio_perf_stat_inc(&perf_stats.outbound_handler);
1608
1609 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1610 used = atomic_add_return(count, &q->nr_buf_used);
1611 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1612
1613 if (callflags & QDIO_FLAG_PCI_OUT)
1614 q->u.out.pci_out_enabled = 1;
1615 else
1616 q->u.out.pci_out_enabled = 0;
1617
1618 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1619 if (multicast_outbound(q))
1620 qdio_kick_outbound_q(q);
1621 else
1622 /*
1623 * One siga-w per buffer required for unicast
1624 * HiperSockets.
1625 */
1626 while (count--)
1627 qdio_kick_outbound_q(q);
1628 goto out;
1629 }
1630
1631 if (need_siga_sync(q)) {
1632 qdio_siga_sync_q(q);
1633 goto out;
1634 }
1635
1636 /* try to fast requeue buffers */
1637 get_buf_state(q, prev_buf(bufnr), &state);
1638 if (state != SLSB_CU_OUTPUT_PRIMED)
1639 qdio_kick_outbound_q(q);
1640 else {
1641 QDIO_DBF_TEXT5(0, trace, "fast-req");
1642 qdio_perf_stat_inc(&perf_stats.fast_requeue);
1643 }
1644out:
1645 /* Fixme: could wait forever if called from process context */
1646 tasklet_schedule(&q->tasklet);
1647}
1648
1649/**
1650 * do_QDIO - process input or output buffers
1651 * @cdev: associated ccw_device for the qdio subchannel
1652 * @callflags: input or output and special flags from the program
1653 * @q_nr: queue number
1654 * @bufnr: buffer number
1655 * @count: how many buffers to process
1656 */
1657int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1658 int q_nr, int bufnr, int count)
1659{
1660 struct qdio_irq *irq_ptr;
1661#ifdef CONFIG_QDIO_DEBUG
1662 char dbf_text[20];
1663
Jan Glauber58eb27c2008-08-21 19:46:34 +02001664 sprintf(dbf_text, "doQD%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001665 QDIO_DBF_TEXT3(0, trace, dbf_text);
1666#endif /* CONFIG_QDIO_DEBUG */
1667
1668 if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
1669 (count > QDIO_MAX_BUFFERS_PER_Q) ||
1670 (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
1671 return -EINVAL;
1672
1673 if (!count)
1674 return 0;
1675
1676 irq_ptr = cdev->private->qdio_data;
1677 if (!irq_ptr)
1678 return -ENODEV;
1679
1680#ifdef CONFIG_QDIO_DEBUG
1681 if (callflags & QDIO_FLAG_SYNC_INPUT)
1682 QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
1683 sizeof(void *));
1684 else
1685 QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
1686 sizeof(void *));
1687
1688 sprintf(dbf_text, "flag%04x", callflags);
1689 QDIO_DBF_TEXT3(0, trace, dbf_text);
1690 sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
1691 QDIO_DBF_TEXT3(0, trace, dbf_text);
1692#endif /* CONFIG_QDIO_DEBUG */
1693
1694 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1695 return -EBUSY;
1696
1697 if (callflags & QDIO_FLAG_SYNC_INPUT)
1698 handle_inbound(irq_ptr->input_qs[q_nr],
1699 callflags, bufnr, count);
1700 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1701 handle_outbound(irq_ptr->output_qs[q_nr],
1702 callflags, bufnr, count);
1703 else {
1704 QDIO_DBF_TEXT3(1, trace, "doQD:inv");
1705 return -EINVAL;
1706 }
1707 return 0;
1708}
1709EXPORT_SYMBOL_GPL(do_QDIO);
1710
1711static int __init init_QDIO(void)
1712{
1713 int rc;
1714
1715 rc = qdio_setup_init();
1716 if (rc)
1717 return rc;
1718 rc = tiqdio_allocate_memory();
1719 if (rc)
1720 goto out_cache;
1721 rc = qdio_debug_init();
1722 if (rc)
1723 goto out_ti;
1724 rc = qdio_setup_perf_stats();
1725 if (rc)
1726 goto out_debug;
1727 rc = tiqdio_register_thinints();
1728 if (rc)
1729 goto out_perf;
1730 return 0;
1731
1732out_perf:
1733 qdio_remove_perf_stats();
1734out_debug:
1735 qdio_debug_exit();
1736out_ti:
1737 tiqdio_free_memory();
1738out_cache:
1739 qdio_setup_exit();
1740 return rc;
1741}
1742
1743static void __exit exit_QDIO(void)
1744{
1745 tiqdio_unregister_thinints();
1746 tiqdio_free_memory();
1747 qdio_remove_perf_stats();
1748 qdio_debug_exit();
1749 qdio_setup_exit();
1750}
1751
1752module_init(init_QDIO);
1753module_exit(exit_QDIO);