blob: e6eabc853422cc9320c7925099471ec04e48f44b [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);
Jan Glauber779e6e12008-07-17 17:16:48 +02001254 if (rc)
1255 return rc;
1256 return 0;
1257}
1258EXPORT_SYMBOL_GPL(qdio_shutdown);
1259
1260/**
1261 * qdio_free - free data structures for a qdio subchannel
1262 * @cdev: associated ccw device
1263 */
1264int qdio_free(struct ccw_device *cdev)
1265{
1266 struct qdio_irq *irq_ptr;
1267 char dbf_text[15];
1268
Jan Glauber58eb27c2008-08-21 19:46:34 +02001269 sprintf(dbf_text, "qfre%4x", cdev->private->schid.sch_no);
1270 QDIO_DBF_TEXT0(0, setup, dbf_text);
1271
Jan Glauber779e6e12008-07-17 17:16:48 +02001272 irq_ptr = cdev->private->qdio_data;
1273 if (!irq_ptr)
1274 return -ENODEV;
1275
1276 mutex_lock(&irq_ptr->setup_mutex);
Jan Glauber779e6e12008-07-17 17:16:48 +02001277 cdev->private->qdio_data = NULL;
1278 mutex_unlock(&irq_ptr->setup_mutex);
1279
1280 qdio_release_memory(irq_ptr);
1281 return 0;
1282}
1283EXPORT_SYMBOL_GPL(qdio_free);
1284
1285/**
1286 * qdio_initialize - allocate and establish queues for a qdio subchannel
1287 * @init_data: initialization data
1288 *
1289 * This function first allocates queues via qdio_allocate() and on success
1290 * establishes them via qdio_establish().
1291 */
1292int qdio_initialize(struct qdio_initialize *init_data)
1293{
1294 int rc;
1295 char dbf_text[15];
1296
1297 sprintf(dbf_text, "qini%4x", init_data->cdev->private->schid.sch_no);
1298 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001299
1300 rc = qdio_allocate(init_data);
1301 if (rc)
1302 return rc;
1303
1304 rc = qdio_establish(init_data);
1305 if (rc)
1306 qdio_free(init_data->cdev);
1307 return rc;
1308}
1309EXPORT_SYMBOL_GPL(qdio_initialize);
1310
1311/**
1312 * qdio_allocate - allocate qdio queues and associated data
1313 * @init_data: initialization data
1314 */
1315int qdio_allocate(struct qdio_initialize *init_data)
1316{
1317 struct qdio_irq *irq_ptr;
1318 char dbf_text[15];
1319
1320 sprintf(dbf_text, "qalc%4x", init_data->cdev->private->schid.sch_no);
1321 QDIO_DBF_TEXT0(0, setup, dbf_text);
Jan Glauber779e6e12008-07-17 17:16:48 +02001322
1323 if ((init_data->no_input_qs && !init_data->input_handler) ||
1324 (init_data->no_output_qs && !init_data->output_handler))
1325 return -EINVAL;
1326
1327 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1328 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1329 return -EINVAL;
1330
1331 if ((!init_data->input_sbal_addr_array) ||
1332 (!init_data->output_sbal_addr_array))
1333 return -EINVAL;
1334
1335 qdio_allocate_do_dbf(init_data);
1336
1337 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1338 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1339 if (!irq_ptr)
1340 goto out_err;
1341 QDIO_DBF_TEXT0(0, setup, "irq_ptr:");
1342 QDIO_DBF_HEX0(0, setup, &irq_ptr, sizeof(void *));
1343
1344 mutex_init(&irq_ptr->setup_mutex);
1345
1346 /*
1347 * Allocate a page for the chsc calls in qdio_establish.
1348 * Must be pre-allocated since a zfcp recovery will call
1349 * qdio_establish. In case of low memory and swap on a zfcp disk
1350 * we may not be able to allocate memory otherwise.
1351 */
1352 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1353 if (!irq_ptr->chsc_page)
1354 goto out_rel;
1355
1356 /* qdr is used in ccw1.cda which is u32 */
Jan Glauber3b8e3002008-08-01 16:39:17 +02001357 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
Jan Glauber779e6e12008-07-17 17:16:48 +02001358 if (!irq_ptr->qdr)
1359 goto out_rel;
1360 WARN_ON((unsigned long)irq_ptr->qdr & 0xfff);
1361
1362 QDIO_DBF_TEXT0(0, setup, "qdr:");
1363 QDIO_DBF_HEX0(0, setup, &irq_ptr->qdr, sizeof(void *));
1364
1365 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1366 init_data->no_output_qs))
1367 goto out_rel;
1368
1369 init_data->cdev->private->qdio_data = irq_ptr;
1370 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1371 return 0;
1372out_rel:
1373 qdio_release_memory(irq_ptr);
1374out_err:
1375 return -ENOMEM;
1376}
1377EXPORT_SYMBOL_GPL(qdio_allocate);
1378
1379/**
1380 * qdio_establish - establish queues on a qdio subchannel
1381 * @init_data: initialization data
1382 */
1383int qdio_establish(struct qdio_initialize *init_data)
1384{
1385 char dbf_text[20];
1386 struct qdio_irq *irq_ptr;
1387 struct ccw_device *cdev = init_data->cdev;
1388 unsigned long saveflags;
1389 int rc;
1390
Jan Glauber58eb27c2008-08-21 19:46:34 +02001391 sprintf(dbf_text, "qest%4x", cdev->private->schid.sch_no);
1392 QDIO_DBF_TEXT0(0, setup, dbf_text);
1393
Jan Glauber779e6e12008-07-17 17:16:48 +02001394 irq_ptr = cdev->private->qdio_data;
1395 if (!irq_ptr)
1396 return -ENODEV;
1397
1398 if (cdev->private->state != DEV_STATE_ONLINE)
1399 return -EINVAL;
1400
Jan Glauber779e6e12008-07-17 17:16:48 +02001401 mutex_lock(&irq_ptr->setup_mutex);
1402 qdio_setup_irq(init_data);
1403
1404 rc = qdio_establish_thinint(irq_ptr);
1405 if (rc) {
1406 mutex_unlock(&irq_ptr->setup_mutex);
1407 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1408 return rc;
1409 }
1410
1411 /* establish q */
1412 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1413 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1414 irq_ptr->ccw.count = irq_ptr->equeue.count;
1415 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1416
1417 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1418 ccw_device_set_options_mask(cdev, 0);
1419
1420 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1421 if (rc) {
1422 sprintf(dbf_text, "eq:io%4x", irq_ptr->schid.sch_no);
1423 QDIO_DBF_TEXT2(1, setup, dbf_text);
1424 sprintf(dbf_text, "eq:rc%4x", rc);
1425 QDIO_DBF_TEXT2(1, setup, dbf_text);
1426 }
1427 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1428
1429 if (rc) {
1430 mutex_unlock(&irq_ptr->setup_mutex);
1431 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1432 return rc;
1433 }
1434
1435 wait_event_interruptible_timeout(cdev->private->wait_q,
1436 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1437 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1438
1439 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1440 mutex_unlock(&irq_ptr->setup_mutex);
1441 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1442 return -EIO;
1443 }
1444
1445 qdio_setup_ssqd_info(irq_ptr);
1446 sprintf(dbf_text, "qib ac%2x", irq_ptr->qib.ac);
1447 QDIO_DBF_TEXT2(0, setup, dbf_text);
1448
1449 /* qebsm is now setup if available, initialize buffer states */
1450 qdio_init_buf_states(irq_ptr);
1451
1452 mutex_unlock(&irq_ptr->setup_mutex);
1453 qdio_print_subchannel_info(irq_ptr, cdev);
1454 qdio_setup_debug_entries(irq_ptr, cdev);
1455 return 0;
1456}
1457EXPORT_SYMBOL_GPL(qdio_establish);
1458
1459/**
1460 * qdio_activate - activate queues on a qdio subchannel
1461 * @cdev: associated cdev
1462 */
1463int qdio_activate(struct ccw_device *cdev)
1464{
1465 struct qdio_irq *irq_ptr;
1466 int rc;
1467 unsigned long saveflags;
1468 char dbf_text[20];
1469
Jan Glauber58eb27c2008-08-21 19:46:34 +02001470 sprintf(dbf_text, "qact%4x", cdev->private->schid.sch_no);
1471 QDIO_DBF_TEXT0(0, setup, dbf_text);
1472
Jan Glauber779e6e12008-07-17 17:16:48 +02001473 irq_ptr = cdev->private->qdio_data;
1474 if (!irq_ptr)
1475 return -ENODEV;
1476
1477 if (cdev->private->state != DEV_STATE_ONLINE)
1478 return -EINVAL;
1479
1480 mutex_lock(&irq_ptr->setup_mutex);
1481 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1482 rc = -EBUSY;
1483 goto out;
1484 }
1485
Jan Glauber779e6e12008-07-17 17:16:48 +02001486 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1487 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1488 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1489 irq_ptr->ccw.cda = 0;
1490
1491 spin_lock_irqsave(get_ccwdev_lock(cdev), saveflags);
1492 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1493
1494 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1495 0, DOIO_DENY_PREFETCH);
1496 if (rc) {
1497 sprintf(dbf_text, "aq:io%4x", irq_ptr->schid.sch_no);
1498 QDIO_DBF_TEXT2(1, setup, dbf_text);
1499 sprintf(dbf_text, "aq:rc%4x", rc);
1500 QDIO_DBF_TEXT2(1, setup, dbf_text);
1501 }
1502 spin_unlock_irqrestore(get_ccwdev_lock(cdev), saveflags);
1503
1504 if (rc)
1505 goto out;
1506
1507 if (is_thinint_irq(irq_ptr))
1508 tiqdio_add_input_queues(irq_ptr);
1509
1510 /* wait for subchannel to become active */
1511 msleep(5);
1512
1513 switch (irq_ptr->state) {
1514 case QDIO_IRQ_STATE_STOPPED:
1515 case QDIO_IRQ_STATE_ERR:
1516 mutex_unlock(&irq_ptr->setup_mutex);
1517 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1518 return -EIO;
1519 default:
1520 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1521 rc = 0;
1522 }
1523out:
1524 mutex_unlock(&irq_ptr->setup_mutex);
1525 return rc;
1526}
1527EXPORT_SYMBOL_GPL(qdio_activate);
1528
1529static inline int buf_in_between(int bufnr, int start, int count)
1530{
1531 int end = add_buf(start, count);
1532
1533 if (end > start) {
1534 if (bufnr >= start && bufnr < end)
1535 return 1;
1536 else
1537 return 0;
1538 }
1539
1540 /* wrap-around case */
1541 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1542 (bufnr < end))
1543 return 1;
1544 else
1545 return 0;
1546}
1547
1548/**
1549 * handle_inbound - reset processed input buffers
1550 * @q: queue containing the buffers
1551 * @callflags: flags
1552 * @bufnr: first buffer to process
1553 * @count: how many buffers are emptied
1554 */
1555static void handle_inbound(struct qdio_q *q, unsigned int callflags,
1556 int bufnr, int count)
1557{
1558 unsigned long flags;
1559 int used, rc;
1560
1561 /*
1562 * do_QDIO could run in parallel with the queue tasklet so the
1563 * upper-layer programm could empty the ACK'ed buffer here.
1564 * If that happens we must clear the polling flag, otherwise
1565 * qdio_stop_polling() could set the buffer to NOT_INIT after
1566 * it was set to EMPTY which would kill us.
1567 */
1568 spin_lock_irqsave(&q->u.in.lock, flags);
1569 if (q->u.in.polling)
1570 if (buf_in_between(q->last_move_ftc, bufnr, count))
1571 q->u.in.polling = 0;
1572
1573 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1574 spin_unlock_irqrestore(&q->u.in.lock, flags);
1575
1576 used = atomic_add_return(count, &q->nr_buf_used) - count;
1577 BUG_ON(used + count > QDIO_MAX_BUFFERS_PER_Q);
1578
1579 /* no need to signal as long as the adapter had free buffers */
1580 if (used)
1581 return;
1582
1583 if (need_siga_in(q)) {
1584 rc = qdio_siga_input(q);
1585 if (rc)
1586 q->qdio_error = rc;
1587 }
1588}
1589
1590/**
1591 * handle_outbound - process filled outbound buffers
1592 * @q: queue containing the buffers
1593 * @callflags: flags
1594 * @bufnr: first buffer to process
1595 * @count: how many buffers are filled
1596 */
1597static void handle_outbound(struct qdio_q *q, unsigned int callflags,
1598 int bufnr, int count)
1599{
1600 unsigned char state;
1601 int used;
1602
1603 qdio_perf_stat_inc(&perf_stats.outbound_handler);
1604
1605 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1606 used = atomic_add_return(count, &q->nr_buf_used);
1607 BUG_ON(used > QDIO_MAX_BUFFERS_PER_Q);
1608
1609 if (callflags & QDIO_FLAG_PCI_OUT)
1610 q->u.out.pci_out_enabled = 1;
1611 else
1612 q->u.out.pci_out_enabled = 0;
1613
1614 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1615 if (multicast_outbound(q))
1616 qdio_kick_outbound_q(q);
1617 else
1618 /*
1619 * One siga-w per buffer required for unicast
1620 * HiperSockets.
1621 */
1622 while (count--)
1623 qdio_kick_outbound_q(q);
1624 goto out;
1625 }
1626
1627 if (need_siga_sync(q)) {
1628 qdio_siga_sync_q(q);
1629 goto out;
1630 }
1631
1632 /* try to fast requeue buffers */
1633 get_buf_state(q, prev_buf(bufnr), &state);
1634 if (state != SLSB_CU_OUTPUT_PRIMED)
1635 qdio_kick_outbound_q(q);
1636 else {
1637 QDIO_DBF_TEXT5(0, trace, "fast-req");
1638 qdio_perf_stat_inc(&perf_stats.fast_requeue);
1639 }
1640out:
1641 /* Fixme: could wait forever if called from process context */
1642 tasklet_schedule(&q->tasklet);
1643}
1644
1645/**
1646 * do_QDIO - process input or output buffers
1647 * @cdev: associated ccw_device for the qdio subchannel
1648 * @callflags: input or output and special flags from the program
1649 * @q_nr: queue number
1650 * @bufnr: buffer number
1651 * @count: how many buffers to process
1652 */
1653int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1654 int q_nr, int bufnr, int count)
1655{
1656 struct qdio_irq *irq_ptr;
1657#ifdef CONFIG_QDIO_DEBUG
1658 char dbf_text[20];
1659
Jan Glauber58eb27c2008-08-21 19:46:34 +02001660 sprintf(dbf_text, "doQD%4x", cdev->private->schid.sch_no);
Jan Glauber779e6e12008-07-17 17:16:48 +02001661 QDIO_DBF_TEXT3(0, trace, dbf_text);
1662#endif /* CONFIG_QDIO_DEBUG */
1663
1664 if ((bufnr > QDIO_MAX_BUFFERS_PER_Q) ||
1665 (count > QDIO_MAX_BUFFERS_PER_Q) ||
1666 (q_nr > QDIO_MAX_QUEUES_PER_IRQ))
1667 return -EINVAL;
1668
1669 if (!count)
1670 return 0;
1671
1672 irq_ptr = cdev->private->qdio_data;
1673 if (!irq_ptr)
1674 return -ENODEV;
1675
1676#ifdef CONFIG_QDIO_DEBUG
1677 if (callflags & QDIO_FLAG_SYNC_INPUT)
1678 QDIO_DBF_HEX3(0, trace, &irq_ptr->input_qs[q_nr],
1679 sizeof(void *));
1680 else
1681 QDIO_DBF_HEX3(0, trace, &irq_ptr->output_qs[q_nr],
1682 sizeof(void *));
1683
1684 sprintf(dbf_text, "flag%04x", callflags);
1685 QDIO_DBF_TEXT3(0, trace, dbf_text);
1686 sprintf(dbf_text, "qi%02xct%02x", bufnr, count);
1687 QDIO_DBF_TEXT3(0, trace, dbf_text);
1688#endif /* CONFIG_QDIO_DEBUG */
1689
1690 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1691 return -EBUSY;
1692
1693 if (callflags & QDIO_FLAG_SYNC_INPUT)
1694 handle_inbound(irq_ptr->input_qs[q_nr],
1695 callflags, bufnr, count);
1696 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1697 handle_outbound(irq_ptr->output_qs[q_nr],
1698 callflags, bufnr, count);
1699 else {
1700 QDIO_DBF_TEXT3(1, trace, "doQD:inv");
1701 return -EINVAL;
1702 }
1703 return 0;
1704}
1705EXPORT_SYMBOL_GPL(do_QDIO);
1706
1707static int __init init_QDIO(void)
1708{
1709 int rc;
1710
1711 rc = qdio_setup_init();
1712 if (rc)
1713 return rc;
1714 rc = tiqdio_allocate_memory();
1715 if (rc)
1716 goto out_cache;
1717 rc = qdio_debug_init();
1718 if (rc)
1719 goto out_ti;
1720 rc = qdio_setup_perf_stats();
1721 if (rc)
1722 goto out_debug;
1723 rc = tiqdio_register_thinints();
1724 if (rc)
1725 goto out_perf;
1726 return 0;
1727
1728out_perf:
1729 qdio_remove_perf_stats();
1730out_debug:
1731 qdio_debug_exit();
1732out_ti:
1733 tiqdio_free_memory();
1734out_cache:
1735 qdio_setup_exit();
1736 return rc;
1737}
1738
1739static void __exit exit_QDIO(void)
1740{
1741 tiqdio_unregister_thinints();
1742 tiqdio_free_memory();
1743 qdio_remove_perf_stats();
1744 qdio_debug_exit();
1745 qdio_setup_exit();
1746}
1747
1748module_init(init_QDIO);
1749module_exit(exit_QDIO);