]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/s390/cio/qdio_main.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-jammy-kernel.git] / drivers / s390 / cio / qdio_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Linux for s390 qdio support, buffer handling, qdio API and module support.
4 *
5 * Copyright IBM Corp. 2000, 2008
6 * Author(s): Utz Bacher <utz.bacher@de.ibm.com>
7 * Jan Glauber <jang@linux.vnet.ibm.com>
8 * 2.6 cio integration by Cornelia Huck <cornelia.huck@de.ibm.com>
9 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/timer.h>
14 #include <linux/delay.h>
15 #include <linux/gfp.h>
16 #include <linux/io.h>
17 #include <linux/atomic.h>
18 #include <asm/debug.h>
19 #include <asm/qdio.h>
20 #include <asm/ipl.h>
21
22 #include "cio.h"
23 #include "css.h"
24 #include "device.h"
25 #include "qdio.h"
26 #include "qdio_debug.h"
27
28 MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>,"\
29 "Jan Glauber <jang@linux.vnet.ibm.com>");
30 MODULE_DESCRIPTION("QDIO base support");
31 MODULE_LICENSE("GPL");
32
33 static inline int do_siga_sync(unsigned long schid,
34 unsigned int out_mask, unsigned int in_mask,
35 unsigned int fc)
36 {
37 register unsigned long __fc asm ("0") = fc;
38 register unsigned long __schid asm ("1") = schid;
39 register unsigned long out asm ("2") = out_mask;
40 register unsigned long in asm ("3") = in_mask;
41 int cc;
42
43 asm volatile(
44 " siga 0\n"
45 " ipm %0\n"
46 " srl %0,28\n"
47 : "=d" (cc)
48 : "d" (__fc), "d" (__schid), "d" (out), "d" (in) : "cc");
49 return cc;
50 }
51
52 static inline int do_siga_input(unsigned long schid, unsigned int mask,
53 unsigned int fc)
54 {
55 register unsigned long __fc asm ("0") = fc;
56 register unsigned long __schid asm ("1") = schid;
57 register unsigned long __mask asm ("2") = mask;
58 int cc;
59
60 asm volatile(
61 " siga 0\n"
62 " ipm %0\n"
63 " srl %0,28\n"
64 : "=d" (cc)
65 : "d" (__fc), "d" (__schid), "d" (__mask) : "cc");
66 return cc;
67 }
68
69 /**
70 * do_siga_output - perform SIGA-w/wt function
71 * @schid: subchannel id or in case of QEBSM the subchannel token
72 * @mask: which output queues to process
73 * @bb: busy bit indicator, set only if SIGA-w/wt could not access a buffer
74 * @fc: function code to perform
75 * @aob: asynchronous operation block
76 *
77 * Returns condition code.
78 * Note: For IQDC unicast queues only the highest priority queue is processed.
79 */
80 static inline int do_siga_output(unsigned long schid, unsigned long mask,
81 unsigned int *bb, unsigned int fc,
82 unsigned long aob)
83 {
84 register unsigned long __fc asm("0") = fc;
85 register unsigned long __schid asm("1") = schid;
86 register unsigned long __mask asm("2") = mask;
87 register unsigned long __aob asm("3") = aob;
88 int cc;
89
90 asm volatile(
91 " siga 0\n"
92 " ipm %0\n"
93 " srl %0,28\n"
94 : "=d" (cc), "+d" (__fc), "+d" (__aob)
95 : "d" (__schid), "d" (__mask)
96 : "cc");
97 *bb = __fc >> 31;
98 return cc;
99 }
100
101 /**
102 * qdio_do_eqbs - extract buffer states for QEBSM
103 * @q: queue to manipulate
104 * @state: state of the extracted buffers
105 * @start: buffer number to start at
106 * @count: count of buffers to examine
107 * @auto_ack: automatically acknowledge buffers
108 *
109 * Returns the number of successfully extracted equal buffer states.
110 * Stops processing if a state is different from the last buffers state.
111 */
112 static int qdio_do_eqbs(struct qdio_q *q, unsigned char *state,
113 int start, int count, int auto_ack)
114 {
115 int tmp_count = count, tmp_start = start, nr = q->nr;
116 unsigned int ccq = 0;
117
118 qperf_inc(q, eqbs);
119
120 if (!q->is_input_q)
121 nr += q->irq_ptr->nr_input_qs;
122 again:
123 ccq = do_eqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count,
124 auto_ack);
125
126 switch (ccq) {
127 case 0:
128 case 32:
129 /* all done, or next buffer state different */
130 return count - tmp_count;
131 case 96:
132 /* not all buffers processed */
133 qperf_inc(q, eqbs_partial);
134 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS part:%02x",
135 tmp_count);
136 return count - tmp_count;
137 case 97:
138 /* no buffer processed */
139 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "EQBS again:%2d", ccq);
140 goto again;
141 default:
142 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
143 DBF_ERROR("%4x EQBS ERROR", SCH_NO(q));
144 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
145 q->handler(q->irq_ptr->cdev, QDIO_ERROR_GET_BUF_STATE, q->nr,
146 q->first_to_kick, count, q->irq_ptr->int_parm);
147 return 0;
148 }
149 }
150
151 /**
152 * qdio_do_sqbs - set buffer states for QEBSM
153 * @q: queue to manipulate
154 * @state: new state of the buffers
155 * @start: first buffer number to change
156 * @count: how many buffers to change
157 *
158 * Returns the number of successfully changed buffers.
159 * Does retrying until the specified count of buffer states is set or an
160 * error occurs.
161 */
162 static int qdio_do_sqbs(struct qdio_q *q, unsigned char state, int start,
163 int count)
164 {
165 unsigned int ccq = 0;
166 int tmp_count = count, tmp_start = start;
167 int nr = q->nr;
168
169 if (!count)
170 return 0;
171 qperf_inc(q, sqbs);
172
173 if (!q->is_input_q)
174 nr += q->irq_ptr->nr_input_qs;
175 again:
176 ccq = do_sqbs(q->irq_ptr->sch_token, state, nr, &tmp_start, &tmp_count);
177
178 switch (ccq) {
179 case 0:
180 case 32:
181 /* all done, or active buffer adapter-owned */
182 WARN_ON_ONCE(tmp_count);
183 return count - tmp_count;
184 case 96:
185 /* not all buffers processed */
186 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "SQBS again:%2d", ccq);
187 qperf_inc(q, sqbs_partial);
188 goto again;
189 default:
190 DBF_ERROR("%4x ccq:%3d", SCH_NO(q), ccq);
191 DBF_ERROR("%4x SQBS ERROR", SCH_NO(q));
192 DBF_ERROR("%3d%3d%2d", count, tmp_count, nr);
193 q->handler(q->irq_ptr->cdev, QDIO_ERROR_SET_BUF_STATE, q->nr,
194 q->first_to_kick, count, q->irq_ptr->int_parm);
195 return 0;
196 }
197 }
198
199 /*
200 * Returns number of examined buffers and their common state in *state.
201 * Requested number of buffers-to-examine must be > 0.
202 */
203 static inline int get_buf_states(struct qdio_q *q, unsigned int bufnr,
204 unsigned char *state, unsigned int count,
205 int auto_ack, int merge_pending)
206 {
207 unsigned char __state = 0;
208 int i = 1;
209
210 if (is_qebsm(q))
211 return qdio_do_eqbs(q, state, bufnr, count, auto_ack);
212
213 /* get initial state: */
214 __state = q->slsb.val[bufnr];
215
216 /* Bail out early if there is no work on the queue: */
217 if (__state & SLSB_OWNER_CU)
218 goto out;
219
220 if (merge_pending && __state == SLSB_P_OUTPUT_PENDING)
221 __state = SLSB_P_OUTPUT_EMPTY;
222
223 for (; i < count; i++) {
224 bufnr = next_buf(bufnr);
225
226 /* merge PENDING into EMPTY: */
227 if (merge_pending &&
228 q->slsb.val[bufnr] == SLSB_P_OUTPUT_PENDING &&
229 __state == SLSB_P_OUTPUT_EMPTY)
230 continue;
231
232 /* stop if next state differs from initial state: */
233 if (q->slsb.val[bufnr] != __state)
234 break;
235 }
236
237 out:
238 *state = __state;
239 return i;
240 }
241
242 static inline int get_buf_state(struct qdio_q *q, unsigned int bufnr,
243 unsigned char *state, int auto_ack)
244 {
245 return get_buf_states(q, bufnr, state, 1, auto_ack, 0);
246 }
247
248 /* wrap-around safe setting of slsb states, returns number of changed buffers */
249 static inline int set_buf_states(struct qdio_q *q, int bufnr,
250 unsigned char state, int count)
251 {
252 int i;
253
254 if (is_qebsm(q))
255 return qdio_do_sqbs(q, state, bufnr, count);
256
257 for (i = 0; i < count; i++) {
258 xchg(&q->slsb.val[bufnr], state);
259 bufnr = next_buf(bufnr);
260 }
261 return count;
262 }
263
264 static inline int set_buf_state(struct qdio_q *q, int bufnr,
265 unsigned char state)
266 {
267 return set_buf_states(q, bufnr, state, 1);
268 }
269
270 /* set slsb states to initial state */
271 static void qdio_init_buf_states(struct qdio_irq *irq_ptr)
272 {
273 struct qdio_q *q;
274 int i;
275
276 for_each_input_queue(irq_ptr, q, i)
277 set_buf_states(q, 0, SLSB_P_INPUT_NOT_INIT,
278 QDIO_MAX_BUFFERS_PER_Q);
279 for_each_output_queue(irq_ptr, q, i)
280 set_buf_states(q, 0, SLSB_P_OUTPUT_NOT_INIT,
281 QDIO_MAX_BUFFERS_PER_Q);
282 }
283
284 static inline int qdio_siga_sync(struct qdio_q *q, unsigned int output,
285 unsigned int input)
286 {
287 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
288 unsigned int fc = QDIO_SIGA_SYNC;
289 int cc;
290
291 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-s:%1d", q->nr);
292 qperf_inc(q, siga_sync);
293
294 if (is_qebsm(q)) {
295 schid = q->irq_ptr->sch_token;
296 fc |= QDIO_SIGA_QEBSM_FLAG;
297 }
298
299 cc = do_siga_sync(schid, output, input, fc);
300 if (unlikely(cc))
301 DBF_ERROR("%4x SIGA-S:%2d", SCH_NO(q), cc);
302 return (cc) ? -EIO : 0;
303 }
304
305 static inline int qdio_siga_sync_q(struct qdio_q *q)
306 {
307 if (q->is_input_q)
308 return qdio_siga_sync(q, 0, q->mask);
309 else
310 return qdio_siga_sync(q, q->mask, 0);
311 }
312
313 static int qdio_siga_output(struct qdio_q *q, unsigned int count,
314 unsigned int *busy_bit, unsigned long aob)
315 {
316 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
317 unsigned int fc = QDIO_SIGA_WRITE;
318 u64 start_time = 0;
319 int retries = 0, cc;
320
321 if (queue_type(q) == QDIO_IQDIO_QFMT && !multicast_outbound(q)) {
322 if (count > 1)
323 fc = QDIO_SIGA_WRITEM;
324 else if (aob)
325 fc = QDIO_SIGA_WRITEQ;
326 }
327
328 if (is_qebsm(q)) {
329 schid = q->irq_ptr->sch_token;
330 fc |= QDIO_SIGA_QEBSM_FLAG;
331 }
332 again:
333 cc = do_siga_output(schid, q->mask, busy_bit, fc, aob);
334
335 /* hipersocket busy condition */
336 if (unlikely(*busy_bit)) {
337 retries++;
338
339 if (!start_time) {
340 start_time = get_tod_clock_fast();
341 goto again;
342 }
343 if (get_tod_clock_fast() - start_time < QDIO_BUSY_BIT_PATIENCE)
344 goto again;
345 }
346 if (retries) {
347 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr,
348 "%4x cc2 BB1:%1d", SCH_NO(q), q->nr);
349 DBF_DEV_EVENT(DBF_WARN, q->irq_ptr, "count:%u", retries);
350 }
351 return cc;
352 }
353
354 static inline int qdio_siga_input(struct qdio_q *q)
355 {
356 unsigned long schid = *((u32 *) &q->irq_ptr->schid);
357 unsigned int fc = QDIO_SIGA_READ;
358 int cc;
359
360 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-r:%1d", q->nr);
361 qperf_inc(q, siga_read);
362
363 if (is_qebsm(q)) {
364 schid = q->irq_ptr->sch_token;
365 fc |= QDIO_SIGA_QEBSM_FLAG;
366 }
367
368 cc = do_siga_input(schid, q->mask, fc);
369 if (unlikely(cc))
370 DBF_ERROR("%4x SIGA-R:%2d", SCH_NO(q), cc);
371 return (cc) ? -EIO : 0;
372 }
373
374 #define qdio_siga_sync_out(q) qdio_siga_sync(q, ~0U, 0)
375 #define qdio_siga_sync_all(q) qdio_siga_sync(q, ~0U, ~0U)
376
377 static inline void qdio_sync_queues(struct qdio_q *q)
378 {
379 /* PCI capable outbound queues will also be scanned so sync them too */
380 if (pci_out_supported(q->irq_ptr))
381 qdio_siga_sync_all(q);
382 else
383 qdio_siga_sync_q(q);
384 }
385
386 int debug_get_buf_state(struct qdio_q *q, unsigned int bufnr,
387 unsigned char *state)
388 {
389 if (need_siga_sync(q))
390 qdio_siga_sync_q(q);
391 return get_buf_state(q, bufnr, state, 0);
392 }
393
394 static inline void qdio_stop_polling(struct qdio_q *q)
395 {
396 if (!q->u.in.polling)
397 return;
398
399 q->u.in.polling = 0;
400 qperf_inc(q, stop_polling);
401
402 /* show the card that we are not polling anymore */
403 if (is_qebsm(q)) {
404 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
405 q->u.in.ack_count);
406 q->u.in.ack_count = 0;
407 } else
408 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
409 }
410
411 static inline void account_sbals(struct qdio_q *q, unsigned int count)
412 {
413 int pos;
414
415 q->q_stats.nr_sbal_total += count;
416 if (count == QDIO_MAX_BUFFERS_MASK) {
417 q->q_stats.nr_sbals[7]++;
418 return;
419 }
420 pos = ilog2(count);
421 q->q_stats.nr_sbals[pos]++;
422 }
423
424 static void process_buffer_error(struct qdio_q *q, unsigned int start,
425 int count)
426 {
427 unsigned char state = (q->is_input_q) ? SLSB_P_INPUT_NOT_INIT :
428 SLSB_P_OUTPUT_NOT_INIT;
429
430 q->qdio_error = QDIO_ERROR_SLSB_STATE;
431
432 /* special handling for no target buffer empty */
433 if (queue_type(q) == QDIO_IQDIO_QFMT && !q->is_input_q &&
434 q->sbal[start]->element[15].sflags == 0x10) {
435 qperf_inc(q, target_full);
436 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "OUTFULL FTC:%02x", start);
437 goto set;
438 }
439
440 DBF_ERROR("%4x BUF ERROR", SCH_NO(q));
441 DBF_ERROR((q->is_input_q) ? "IN:%2d" : "OUT:%2d", q->nr);
442 DBF_ERROR("FTC:%3d C:%3d", start, count);
443 DBF_ERROR("F14:%2x F15:%2x",
444 q->sbal[start]->element[14].sflags,
445 q->sbal[start]->element[15].sflags);
446
447 set:
448 /*
449 * Interrupts may be avoided as long as the error is present
450 * so change the buffer state immediately to avoid starvation.
451 */
452 set_buf_states(q, start, state, count);
453 }
454
455 static inline void inbound_primed(struct qdio_q *q, unsigned int start,
456 int count)
457 {
458 int new;
459
460 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in prim:%1d %02x", q->nr, count);
461
462 /* for QEBSM the ACK was already set by EQBS */
463 if (is_qebsm(q)) {
464 if (!q->u.in.polling) {
465 q->u.in.polling = 1;
466 q->u.in.ack_count = count;
467 q->u.in.ack_start = start;
468 return;
469 }
470
471 /* delete the previous ACK's */
472 set_buf_states(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT,
473 q->u.in.ack_count);
474 q->u.in.ack_count = count;
475 q->u.in.ack_start = start;
476 return;
477 }
478
479 /*
480 * ACK the newest buffer. The ACK will be removed in qdio_stop_polling
481 * or by the next inbound run.
482 */
483 new = add_buf(start, count - 1);
484 if (q->u.in.polling) {
485 /* reset the previous ACK but first set the new one */
486 set_buf_state(q, new, SLSB_P_INPUT_ACK);
487 set_buf_state(q, q->u.in.ack_start, SLSB_P_INPUT_NOT_INIT);
488 } else {
489 q->u.in.polling = 1;
490 set_buf_state(q, new, SLSB_P_INPUT_ACK);
491 }
492
493 q->u.in.ack_start = new;
494 count--;
495 if (!count)
496 return;
497 /* need to change ALL buffers to get more interrupts */
498 set_buf_states(q, start, SLSB_P_INPUT_NOT_INIT, count);
499 }
500
501 static int get_inbound_buffer_frontier(struct qdio_q *q, unsigned int start)
502 {
503 unsigned char state = 0;
504 int count;
505
506 q->timestamp = get_tod_clock_fast();
507
508 /*
509 * Don't check 128 buffers, as otherwise qdio_inbound_q_moved
510 * would return 0.
511 */
512 count = min(atomic_read(&q->nr_buf_used), QDIO_MAX_BUFFERS_MASK);
513 if (!count)
514 return 0;
515
516 /*
517 * No siga sync here, as a PCI or we after a thin interrupt
518 * already sync'ed the queues.
519 */
520 count = get_buf_states(q, start, &state, count, 1, 0);
521 if (!count)
522 return 0;
523
524 switch (state) {
525 case SLSB_P_INPUT_PRIMED:
526 inbound_primed(q, start, count);
527 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
528 qperf_inc(q, inbound_queue_full);
529 if (q->irq_ptr->perf_stat_enabled)
530 account_sbals(q, count);
531 return count;
532 case SLSB_P_INPUT_ERROR:
533 process_buffer_error(q, start, count);
534 if (atomic_sub_return(count, &q->nr_buf_used) == 0)
535 qperf_inc(q, inbound_queue_full);
536 if (q->irq_ptr->perf_stat_enabled)
537 account_sbals_error(q, count);
538 return count;
539 case SLSB_CU_INPUT_EMPTY:
540 case SLSB_P_INPUT_NOT_INIT:
541 case SLSB_P_INPUT_ACK:
542 if (q->irq_ptr->perf_stat_enabled)
543 q->q_stats.nr_sbal_nop++;
544 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in nop:%1d %#02x",
545 q->nr, start);
546 return 0;
547 default:
548 WARN_ON_ONCE(1);
549 return 0;
550 }
551 }
552
553 static int qdio_inbound_q_moved(struct qdio_q *q, unsigned int start)
554 {
555 int count;
556
557 count = get_inbound_buffer_frontier(q, start);
558
559 if (count && !is_thinint_irq(q->irq_ptr) && MACHINE_IS_LPAR)
560 q->u.in.timestamp = get_tod_clock();
561
562 return count;
563 }
564
565 static inline int qdio_inbound_q_done(struct qdio_q *q, unsigned int start)
566 {
567 unsigned char state = 0;
568
569 if (!atomic_read(&q->nr_buf_used))
570 return 1;
571
572 if (need_siga_sync(q))
573 qdio_siga_sync_q(q);
574 get_buf_state(q, start, &state, 0);
575
576 if (state == SLSB_P_INPUT_PRIMED || state == SLSB_P_INPUT_ERROR)
577 /* more work coming */
578 return 0;
579
580 if (is_thinint_irq(q->irq_ptr))
581 return 1;
582
583 /* don't poll under z/VM */
584 if (MACHINE_IS_VM)
585 return 1;
586
587 /*
588 * At this point we know, that inbound first_to_check
589 * has (probably) not moved (see qdio_inbound_processing).
590 */
591 if (get_tod_clock_fast() > q->u.in.timestamp + QDIO_INPUT_THRESHOLD) {
592 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "in done:%02x", start);
593 return 1;
594 } else
595 return 0;
596 }
597
598 static inline void qdio_handle_aobs(struct qdio_q *q, int start, int count)
599 {
600 unsigned char state = 0;
601 int j, b = start;
602
603 for (j = 0; j < count; ++j) {
604 get_buf_state(q, b, &state, 0);
605 if (state == SLSB_P_OUTPUT_PENDING) {
606 struct qaob *aob = q->u.out.aobs[b];
607 if (aob == NULL)
608 continue;
609
610 q->u.out.sbal_state[b].flags |=
611 QDIO_OUTBUF_STATE_FLAG_PENDING;
612 q->u.out.aobs[b] = NULL;
613 }
614 b = next_buf(b);
615 }
616 }
617
618 static inline unsigned long qdio_aob_for_buffer(struct qdio_output_q *q,
619 int bufnr)
620 {
621 unsigned long phys_aob = 0;
622
623 if (!q->aobs[bufnr]) {
624 struct qaob *aob = qdio_allocate_aob();
625 q->aobs[bufnr] = aob;
626 }
627 if (q->aobs[bufnr]) {
628 q->aobs[bufnr]->user1 = (u64) q->sbal_state[bufnr].user;
629 phys_aob = virt_to_phys(q->aobs[bufnr]);
630 WARN_ON_ONCE(phys_aob & 0xFF);
631 }
632
633 q->sbal_state[bufnr].flags = 0;
634 return phys_aob;
635 }
636
637 static void qdio_kick_handler(struct qdio_q *q, unsigned int count)
638 {
639 int start = q->first_to_kick;
640
641 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
642 return;
643
644 if (q->is_input_q) {
645 qperf_inc(q, inbound_handler);
646 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "kih s:%02x c:%02x", start, count);
647 } else {
648 qperf_inc(q, outbound_handler);
649 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "koh: s:%02x c:%02x",
650 start, count);
651 }
652
653 q->handler(q->irq_ptr->cdev, q->qdio_error, q->nr, start, count,
654 q->irq_ptr->int_parm);
655
656 /* for the next time */
657 q->first_to_kick = add_buf(start, count);
658 q->qdio_error = 0;
659 }
660
661 static inline int qdio_tasklet_schedule(struct qdio_q *q)
662 {
663 if (likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE)) {
664 tasklet_schedule(&q->tasklet);
665 return 0;
666 }
667 return -EPERM;
668 }
669
670 static void __qdio_inbound_processing(struct qdio_q *q)
671 {
672 unsigned int start = q->first_to_check;
673 int count;
674
675 qperf_inc(q, tasklet_inbound);
676
677 count = qdio_inbound_q_moved(q, start);
678 if (count == 0)
679 return;
680
681 start = add_buf(start, count);
682 q->first_to_check = start;
683 qdio_kick_handler(q, count);
684
685 if (!qdio_inbound_q_done(q, start)) {
686 /* means poll time is not yet over */
687 qperf_inc(q, tasklet_inbound_resched);
688 if (!qdio_tasklet_schedule(q))
689 return;
690 }
691
692 qdio_stop_polling(q);
693 /*
694 * We need to check again to not lose initiative after
695 * resetting the ACK state.
696 */
697 if (!qdio_inbound_q_done(q, start)) {
698 qperf_inc(q, tasklet_inbound_resched2);
699 qdio_tasklet_schedule(q);
700 }
701 }
702
703 void qdio_inbound_processing(unsigned long data)
704 {
705 struct qdio_q *q = (struct qdio_q *)data;
706 __qdio_inbound_processing(q);
707 }
708
709 static int get_outbound_buffer_frontier(struct qdio_q *q, unsigned int start)
710 {
711 unsigned char state = 0;
712 int count;
713
714 q->timestamp = get_tod_clock_fast();
715
716 if (need_siga_sync(q))
717 if (((queue_type(q) != QDIO_IQDIO_QFMT) &&
718 !pci_out_supported(q->irq_ptr)) ||
719 (queue_type(q) == QDIO_IQDIO_QFMT &&
720 multicast_outbound(q)))
721 qdio_siga_sync_q(q);
722
723 count = atomic_read(&q->nr_buf_used);
724 if (!count)
725 return 0;
726
727 count = get_buf_states(q, start, &state, count, 0, q->u.out.use_cq);
728 if (!count)
729 return 0;
730
731 switch (state) {
732 case SLSB_P_OUTPUT_EMPTY:
733 case SLSB_P_OUTPUT_PENDING:
734 /* the adapter got it */
735 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr,
736 "out empty:%1d %02x", q->nr, count);
737
738 atomic_sub(count, &q->nr_buf_used);
739 if (q->irq_ptr->perf_stat_enabled)
740 account_sbals(q, count);
741 return count;
742 case SLSB_P_OUTPUT_ERROR:
743 process_buffer_error(q, start, count);
744 atomic_sub(count, &q->nr_buf_used);
745 if (q->irq_ptr->perf_stat_enabled)
746 account_sbals_error(q, count);
747 return count;
748 case SLSB_CU_OUTPUT_PRIMED:
749 /* the adapter has not fetched the output yet */
750 if (q->irq_ptr->perf_stat_enabled)
751 q->q_stats.nr_sbal_nop++;
752 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out primed:%1d",
753 q->nr);
754 return 0;
755 case SLSB_P_OUTPUT_NOT_INIT:
756 case SLSB_P_OUTPUT_HALTED:
757 return 0;
758 default:
759 WARN_ON_ONCE(1);
760 return 0;
761 }
762 }
763
764 /* all buffers processed? */
765 static inline int qdio_outbound_q_done(struct qdio_q *q)
766 {
767 return atomic_read(&q->nr_buf_used) == 0;
768 }
769
770 static inline int qdio_outbound_q_moved(struct qdio_q *q, unsigned int start)
771 {
772 int count;
773
774 count = get_outbound_buffer_frontier(q, start);
775
776 if (count) {
777 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "out moved:%1d", q->nr);
778 if (q->u.out.use_cq)
779 qdio_handle_aobs(q, start, count);
780 }
781
782 return count;
783 }
784
785 static int qdio_kick_outbound_q(struct qdio_q *q, unsigned int count,
786 unsigned long aob)
787 {
788 int retries = 0, cc;
789 unsigned int busy_bit;
790
791 if (!need_siga_out(q))
792 return 0;
793
794 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w:%1d", q->nr);
795 retry:
796 qperf_inc(q, siga_write);
797
798 cc = qdio_siga_output(q, count, &busy_bit, aob);
799 switch (cc) {
800 case 0:
801 break;
802 case 2:
803 if (busy_bit) {
804 while (++retries < QDIO_BUSY_BIT_RETRIES) {
805 mdelay(QDIO_BUSY_BIT_RETRY_DELAY);
806 goto retry;
807 }
808 DBF_ERROR("%4x cc2 BBC:%1d", SCH_NO(q), q->nr);
809 cc = -EBUSY;
810 } else {
811 DBF_DEV_EVENT(DBF_INFO, q->irq_ptr, "siga-w cc2:%1d", q->nr);
812 cc = -ENOBUFS;
813 }
814 break;
815 case 1:
816 case 3:
817 DBF_ERROR("%4x SIGA-W:%1d", SCH_NO(q), cc);
818 cc = -EIO;
819 break;
820 }
821 if (retries) {
822 DBF_ERROR("%4x cc2 BB2:%1d", SCH_NO(q), q->nr);
823 DBF_ERROR("count:%u", retries);
824 }
825 return cc;
826 }
827
828 static void __qdio_outbound_processing(struct qdio_q *q)
829 {
830 unsigned int start = q->first_to_check;
831 int count;
832
833 qperf_inc(q, tasklet_outbound);
834 WARN_ON_ONCE(atomic_read(&q->nr_buf_used) < 0);
835
836 count = qdio_outbound_q_moved(q, start);
837 if (count) {
838 q->first_to_check = add_buf(start, count);
839 qdio_kick_handler(q, count);
840 }
841
842 if (queue_type(q) == QDIO_ZFCP_QFMT && !pci_out_supported(q->irq_ptr) &&
843 !qdio_outbound_q_done(q))
844 goto sched;
845
846 if (q->u.out.pci_out_enabled)
847 return;
848
849 /*
850 * Now we know that queue type is either qeth without pci enabled
851 * or HiperSockets. Make sure buffer switch from PRIMED to EMPTY
852 * is noticed and outbound_handler is called after some time.
853 */
854 if (qdio_outbound_q_done(q))
855 del_timer_sync(&q->u.out.timer);
856 else
857 if (!timer_pending(&q->u.out.timer) &&
858 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
859 mod_timer(&q->u.out.timer, jiffies + 10 * HZ);
860 return;
861
862 sched:
863 qdio_tasklet_schedule(q);
864 }
865
866 /* outbound tasklet */
867 void qdio_outbound_processing(unsigned long data)
868 {
869 struct qdio_q *q = (struct qdio_q *)data;
870 __qdio_outbound_processing(q);
871 }
872
873 void qdio_outbound_timer(struct timer_list *t)
874 {
875 struct qdio_q *q = from_timer(q, t, u.out.timer);
876
877 qdio_tasklet_schedule(q);
878 }
879
880 static inline void qdio_check_outbound_pci_queues(struct qdio_irq *irq)
881 {
882 struct qdio_q *out;
883 int i;
884
885 if (!pci_out_supported(irq) || !irq->scan_threshold)
886 return;
887
888 for_each_output_queue(irq, out, i)
889 if (!qdio_outbound_q_done(out))
890 qdio_tasklet_schedule(out);
891 }
892
893 static void __tiqdio_inbound_processing(struct qdio_q *q)
894 {
895 unsigned int start = q->first_to_check;
896 int count;
897
898 qperf_inc(q, tasklet_inbound);
899 if (need_siga_sync(q) && need_siga_sync_after_ai(q))
900 qdio_sync_queues(q);
901
902 /* The interrupt could be caused by a PCI request: */
903 qdio_check_outbound_pci_queues(q->irq_ptr);
904
905 count = qdio_inbound_q_moved(q, start);
906 if (count == 0)
907 return;
908
909 start = add_buf(start, count);
910 q->first_to_check = start;
911 qdio_kick_handler(q, count);
912
913 if (!qdio_inbound_q_done(q, start)) {
914 qperf_inc(q, tasklet_inbound_resched);
915 if (!qdio_tasklet_schedule(q))
916 return;
917 }
918
919 qdio_stop_polling(q);
920 /*
921 * We need to check again to not lose initiative after
922 * resetting the ACK state.
923 */
924 if (!qdio_inbound_q_done(q, start)) {
925 qperf_inc(q, tasklet_inbound_resched2);
926 qdio_tasklet_schedule(q);
927 }
928 }
929
930 void tiqdio_inbound_processing(unsigned long data)
931 {
932 struct qdio_q *q = (struct qdio_q *)data;
933 __tiqdio_inbound_processing(q);
934 }
935
936 static inline void qdio_set_state(struct qdio_irq *irq_ptr,
937 enum qdio_irq_states state)
938 {
939 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "newstate: %1d", state);
940
941 irq_ptr->state = state;
942 mb();
943 }
944
945 static void qdio_irq_check_sense(struct qdio_irq *irq_ptr, struct irb *irb)
946 {
947 if (irb->esw.esw0.erw.cons) {
948 DBF_ERROR("%4x sense:", irq_ptr->schid.sch_no);
949 DBF_ERROR_HEX(irb, 64);
950 DBF_ERROR_HEX(irb->ecw, 64);
951 }
952 }
953
954 /* PCI interrupt handler */
955 static void qdio_int_handler_pci(struct qdio_irq *irq_ptr)
956 {
957 int i;
958 struct qdio_q *q;
959
960 if (unlikely(irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
961 return;
962
963 for_each_input_queue(irq_ptr, q, i) {
964 if (q->u.in.queue_start_poll) {
965 /* skip if polling is enabled or already in work */
966 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
967 &q->u.in.queue_irq_state)) {
968 qperf_inc(q, int_discarded);
969 continue;
970 }
971 q->u.in.queue_start_poll(q->irq_ptr->cdev, q->nr,
972 q->irq_ptr->int_parm);
973 } else {
974 tasklet_schedule(&q->tasklet);
975 }
976 }
977
978 if (!pci_out_supported(irq_ptr) || !irq_ptr->scan_threshold)
979 return;
980
981 for_each_output_queue(irq_ptr, q, i) {
982 if (qdio_outbound_q_done(q))
983 continue;
984 if (need_siga_sync(q) && need_siga_sync_out_after_pci(q))
985 qdio_siga_sync_q(q);
986 qdio_tasklet_schedule(q);
987 }
988 }
989
990 static void qdio_handle_activate_check(struct ccw_device *cdev,
991 unsigned long intparm, int cstat, int dstat)
992 {
993 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
994 struct qdio_q *q;
995 int count;
996
997 DBF_ERROR("%4x ACT CHECK", irq_ptr->schid.sch_no);
998 DBF_ERROR("intp :%lx", intparm);
999 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1000
1001 if (irq_ptr->nr_input_qs) {
1002 q = irq_ptr->input_qs[0];
1003 } else if (irq_ptr->nr_output_qs) {
1004 q = irq_ptr->output_qs[0];
1005 } else {
1006 dump_stack();
1007 goto no_handler;
1008 }
1009
1010 count = sub_buf(q->first_to_check, q->first_to_kick);
1011 q->handler(q->irq_ptr->cdev, QDIO_ERROR_ACTIVATE,
1012 q->nr, q->first_to_kick, count, irq_ptr->int_parm);
1013 no_handler:
1014 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1015 /*
1016 * In case of z/VM LGR (Live Guest Migration) QDIO recovery will happen.
1017 * Therefore we call the LGR detection function here.
1018 */
1019 lgr_info_log();
1020 }
1021
1022 static void qdio_establish_handle_irq(struct ccw_device *cdev, int cstat,
1023 int dstat)
1024 {
1025 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1026
1027 DBF_DEV_EVENT(DBF_INFO, irq_ptr, "qest irq");
1028
1029 if (cstat)
1030 goto error;
1031 if (dstat & ~(DEV_STAT_DEV_END | DEV_STAT_CHN_END))
1032 goto error;
1033 if (!(dstat & DEV_STAT_DEV_END))
1034 goto error;
1035 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ESTABLISHED);
1036 return;
1037
1038 error:
1039 DBF_ERROR("%4x EQ:error", irq_ptr->schid.sch_no);
1040 DBF_ERROR("ds: %2x cs:%2x", dstat, cstat);
1041 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1042 }
1043
1044 /* qdio interrupt handler */
1045 void qdio_int_handler(struct ccw_device *cdev, unsigned long intparm,
1046 struct irb *irb)
1047 {
1048 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1049 struct subchannel_id schid;
1050 int cstat, dstat;
1051
1052 if (!intparm || !irq_ptr) {
1053 ccw_device_get_schid(cdev, &schid);
1054 DBF_ERROR("qint:%4x", schid.sch_no);
1055 return;
1056 }
1057
1058 if (irq_ptr->perf_stat_enabled)
1059 irq_ptr->perf_stat.qdio_int++;
1060
1061 if (IS_ERR(irb)) {
1062 DBF_ERROR("%4x IO error", irq_ptr->schid.sch_no);
1063 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ERR);
1064 wake_up(&cdev->private->wait_q);
1065 return;
1066 }
1067 qdio_irq_check_sense(irq_ptr, irb);
1068 cstat = irb->scsw.cmd.cstat;
1069 dstat = irb->scsw.cmd.dstat;
1070
1071 switch (irq_ptr->state) {
1072 case QDIO_IRQ_STATE_INACTIVE:
1073 qdio_establish_handle_irq(cdev, cstat, dstat);
1074 break;
1075 case QDIO_IRQ_STATE_CLEANUP:
1076 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1077 break;
1078 case QDIO_IRQ_STATE_ESTABLISHED:
1079 case QDIO_IRQ_STATE_ACTIVE:
1080 if (cstat & SCHN_STAT_PCI) {
1081 qdio_int_handler_pci(irq_ptr);
1082 return;
1083 }
1084 if (cstat || dstat)
1085 qdio_handle_activate_check(cdev, intparm, cstat,
1086 dstat);
1087 break;
1088 case QDIO_IRQ_STATE_STOPPED:
1089 break;
1090 default:
1091 WARN_ON_ONCE(1);
1092 }
1093 wake_up(&cdev->private->wait_q);
1094 }
1095
1096 /**
1097 * qdio_get_ssqd_desc - get qdio subchannel description
1098 * @cdev: ccw device to get description for
1099 * @data: where to store the ssqd
1100 *
1101 * Returns 0 or an error code. The results of the chsc are stored in the
1102 * specified structure.
1103 */
1104 int qdio_get_ssqd_desc(struct ccw_device *cdev,
1105 struct qdio_ssqd_desc *data)
1106 {
1107 struct subchannel_id schid;
1108
1109 if (!cdev || !cdev->private)
1110 return -EINVAL;
1111
1112 ccw_device_get_schid(cdev, &schid);
1113 DBF_EVENT("get ssqd:%4x", schid.sch_no);
1114 return qdio_setup_get_ssqd(NULL, &schid, data);
1115 }
1116 EXPORT_SYMBOL_GPL(qdio_get_ssqd_desc);
1117
1118 static void qdio_shutdown_queues(struct ccw_device *cdev)
1119 {
1120 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1121 struct qdio_q *q;
1122 int i;
1123
1124 for_each_input_queue(irq_ptr, q, i)
1125 tasklet_kill(&q->tasklet);
1126
1127 for_each_output_queue(irq_ptr, q, i) {
1128 del_timer_sync(&q->u.out.timer);
1129 tasklet_kill(&q->tasklet);
1130 }
1131 }
1132
1133 /**
1134 * qdio_shutdown - shut down a qdio subchannel
1135 * @cdev: associated ccw device
1136 * @how: use halt or clear to shutdown
1137 */
1138 int qdio_shutdown(struct ccw_device *cdev, int how)
1139 {
1140 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1141 struct subchannel_id schid;
1142 int rc;
1143
1144 if (!irq_ptr)
1145 return -ENODEV;
1146
1147 WARN_ON_ONCE(irqs_disabled());
1148 ccw_device_get_schid(cdev, &schid);
1149 DBF_EVENT("qshutdown:%4x", schid.sch_no);
1150
1151 mutex_lock(&irq_ptr->setup_mutex);
1152 /*
1153 * Subchannel was already shot down. We cannot prevent being called
1154 * twice since cio may trigger a shutdown asynchronously.
1155 */
1156 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1157 mutex_unlock(&irq_ptr->setup_mutex);
1158 return 0;
1159 }
1160
1161 /*
1162 * Indicate that the device is going down. Scheduling the queue
1163 * tasklets is forbidden from here on.
1164 */
1165 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_STOPPED);
1166
1167 tiqdio_remove_input_queues(irq_ptr);
1168 qdio_shutdown_queues(cdev);
1169 qdio_shutdown_debug_entries(irq_ptr);
1170
1171 /* cleanup subchannel */
1172 spin_lock_irq(get_ccwdev_lock(cdev));
1173
1174 if (how & QDIO_FLAG_CLEANUP_USING_CLEAR)
1175 rc = ccw_device_clear(cdev, QDIO_DOING_CLEANUP);
1176 else
1177 /* default behaviour is halt */
1178 rc = ccw_device_halt(cdev, QDIO_DOING_CLEANUP);
1179 if (rc) {
1180 DBF_ERROR("%4x SHUTD ERR", irq_ptr->schid.sch_no);
1181 DBF_ERROR("rc:%4d", rc);
1182 goto no_cleanup;
1183 }
1184
1185 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_CLEANUP);
1186 spin_unlock_irq(get_ccwdev_lock(cdev));
1187 wait_event_interruptible_timeout(cdev->private->wait_q,
1188 irq_ptr->state == QDIO_IRQ_STATE_INACTIVE ||
1189 irq_ptr->state == QDIO_IRQ_STATE_ERR,
1190 10 * HZ);
1191 spin_lock_irq(get_ccwdev_lock(cdev));
1192
1193 no_cleanup:
1194 qdio_shutdown_thinint(irq_ptr);
1195
1196 /* restore interrupt handler */
1197 if ((void *)cdev->handler == (void *)qdio_int_handler) {
1198 cdev->handler = irq_ptr->orig_handler;
1199 cdev->private->intparm = 0;
1200 }
1201 spin_unlock_irq(get_ccwdev_lock(cdev));
1202
1203 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1204 mutex_unlock(&irq_ptr->setup_mutex);
1205 if (rc)
1206 return rc;
1207 return 0;
1208 }
1209 EXPORT_SYMBOL_GPL(qdio_shutdown);
1210
1211 /**
1212 * qdio_free - free data structures for a qdio subchannel
1213 * @cdev: associated ccw device
1214 */
1215 int qdio_free(struct ccw_device *cdev)
1216 {
1217 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1218 struct subchannel_id schid;
1219
1220 if (!irq_ptr)
1221 return -ENODEV;
1222
1223 ccw_device_get_schid(cdev, &schid);
1224 DBF_EVENT("qfree:%4x", schid.sch_no);
1225 DBF_DEV_EVENT(DBF_ERR, irq_ptr, "dbf abandoned");
1226 mutex_lock(&irq_ptr->setup_mutex);
1227
1228 irq_ptr->debug_area = NULL;
1229 cdev->private->qdio_data = NULL;
1230 mutex_unlock(&irq_ptr->setup_mutex);
1231
1232 qdio_release_memory(irq_ptr);
1233 return 0;
1234 }
1235 EXPORT_SYMBOL_GPL(qdio_free);
1236
1237 /**
1238 * qdio_allocate - allocate qdio queues and associated data
1239 * @init_data: initialization data
1240 */
1241 int qdio_allocate(struct qdio_initialize *init_data)
1242 {
1243 struct subchannel_id schid;
1244 struct qdio_irq *irq_ptr;
1245
1246 ccw_device_get_schid(init_data->cdev, &schid);
1247 DBF_EVENT("qallocate:%4x", schid.sch_no);
1248
1249 if ((init_data->no_input_qs && !init_data->input_handler) ||
1250 (init_data->no_output_qs && !init_data->output_handler))
1251 return -EINVAL;
1252
1253 if ((init_data->no_input_qs > QDIO_MAX_QUEUES_PER_IRQ) ||
1254 (init_data->no_output_qs > QDIO_MAX_QUEUES_PER_IRQ))
1255 return -EINVAL;
1256
1257 if ((!init_data->input_sbal_addr_array) ||
1258 (!init_data->output_sbal_addr_array))
1259 return -EINVAL;
1260
1261 /* irq_ptr must be in GFP_DMA since it contains ccw1.cda */
1262 irq_ptr = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1263 if (!irq_ptr)
1264 goto out_err;
1265
1266 mutex_init(&irq_ptr->setup_mutex);
1267 if (qdio_allocate_dbf(init_data, irq_ptr))
1268 goto out_rel;
1269
1270 /*
1271 * Allocate a page for the chsc calls in qdio_establish.
1272 * Must be pre-allocated since a zfcp recovery will call
1273 * qdio_establish. In case of low memory and swap on a zfcp disk
1274 * we may not be able to allocate memory otherwise.
1275 */
1276 irq_ptr->chsc_page = get_zeroed_page(GFP_KERNEL);
1277 if (!irq_ptr->chsc_page)
1278 goto out_rel;
1279
1280 /* qdr is used in ccw1.cda which is u32 */
1281 irq_ptr->qdr = (struct qdr *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1282 if (!irq_ptr->qdr)
1283 goto out_rel;
1284
1285 if (qdio_allocate_qs(irq_ptr, init_data->no_input_qs,
1286 init_data->no_output_qs))
1287 goto out_rel;
1288
1289 init_data->cdev->private->qdio_data = irq_ptr;
1290 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_INACTIVE);
1291 return 0;
1292 out_rel:
1293 qdio_release_memory(irq_ptr);
1294 out_err:
1295 return -ENOMEM;
1296 }
1297 EXPORT_SYMBOL_GPL(qdio_allocate);
1298
1299 static void qdio_detect_hsicq(struct qdio_irq *irq_ptr)
1300 {
1301 struct qdio_q *q = irq_ptr->input_qs[0];
1302 int i, use_cq = 0;
1303
1304 if (irq_ptr->nr_input_qs > 1 && queue_type(q) == QDIO_IQDIO_QFMT)
1305 use_cq = 1;
1306
1307 for_each_output_queue(irq_ptr, q, i) {
1308 if (use_cq) {
1309 if (multicast_outbound(q))
1310 continue;
1311 if (qdio_enable_async_operation(&q->u.out) < 0) {
1312 use_cq = 0;
1313 continue;
1314 }
1315 } else
1316 qdio_disable_async_operation(&q->u.out);
1317 }
1318 DBF_EVENT("use_cq:%d", use_cq);
1319 }
1320
1321 /**
1322 * qdio_establish - establish queues on a qdio subchannel
1323 * @init_data: initialization data
1324 */
1325 int qdio_establish(struct qdio_initialize *init_data)
1326 {
1327 struct ccw_device *cdev = init_data->cdev;
1328 struct subchannel_id schid;
1329 struct qdio_irq *irq_ptr;
1330 int rc;
1331
1332 ccw_device_get_schid(cdev, &schid);
1333 DBF_EVENT("qestablish:%4x", schid.sch_no);
1334
1335 irq_ptr = cdev->private->qdio_data;
1336 if (!irq_ptr)
1337 return -ENODEV;
1338
1339 mutex_lock(&irq_ptr->setup_mutex);
1340 qdio_setup_irq(init_data);
1341
1342 rc = qdio_establish_thinint(irq_ptr);
1343 if (rc) {
1344 mutex_unlock(&irq_ptr->setup_mutex);
1345 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1346 return rc;
1347 }
1348
1349 /* establish q */
1350 irq_ptr->ccw.cmd_code = irq_ptr->equeue.cmd;
1351 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1352 irq_ptr->ccw.count = irq_ptr->equeue.count;
1353 irq_ptr->ccw.cda = (u32)((addr_t)irq_ptr->qdr);
1354
1355 spin_lock_irq(get_ccwdev_lock(cdev));
1356 ccw_device_set_options_mask(cdev, 0);
1357
1358 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ESTABLISH, 0, 0);
1359 spin_unlock_irq(get_ccwdev_lock(cdev));
1360 if (rc) {
1361 DBF_ERROR("%4x est IO ERR", irq_ptr->schid.sch_no);
1362 DBF_ERROR("rc:%4x", rc);
1363 mutex_unlock(&irq_ptr->setup_mutex);
1364 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1365 return rc;
1366 }
1367
1368 wait_event_interruptible_timeout(cdev->private->wait_q,
1369 irq_ptr->state == QDIO_IRQ_STATE_ESTABLISHED ||
1370 irq_ptr->state == QDIO_IRQ_STATE_ERR, HZ);
1371
1372 if (irq_ptr->state != QDIO_IRQ_STATE_ESTABLISHED) {
1373 mutex_unlock(&irq_ptr->setup_mutex);
1374 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
1375 return -EIO;
1376 }
1377
1378 qdio_setup_ssqd_info(irq_ptr);
1379
1380 qdio_detect_hsicq(irq_ptr);
1381
1382 /* qebsm is now setup if available, initialize buffer states */
1383 qdio_init_buf_states(irq_ptr);
1384
1385 mutex_unlock(&irq_ptr->setup_mutex);
1386 qdio_print_subchannel_info(irq_ptr, cdev);
1387 qdio_setup_debug_entries(irq_ptr, cdev);
1388 return 0;
1389 }
1390 EXPORT_SYMBOL_GPL(qdio_establish);
1391
1392 /**
1393 * qdio_activate - activate queues on a qdio subchannel
1394 * @cdev: associated cdev
1395 */
1396 int qdio_activate(struct ccw_device *cdev)
1397 {
1398 struct subchannel_id schid;
1399 struct qdio_irq *irq_ptr;
1400 int rc;
1401
1402 ccw_device_get_schid(cdev, &schid);
1403 DBF_EVENT("qactivate:%4x", schid.sch_no);
1404
1405 irq_ptr = cdev->private->qdio_data;
1406 if (!irq_ptr)
1407 return -ENODEV;
1408
1409 mutex_lock(&irq_ptr->setup_mutex);
1410 if (irq_ptr->state == QDIO_IRQ_STATE_INACTIVE) {
1411 rc = -EBUSY;
1412 goto out;
1413 }
1414
1415 irq_ptr->ccw.cmd_code = irq_ptr->aqueue.cmd;
1416 irq_ptr->ccw.flags = CCW_FLAG_SLI;
1417 irq_ptr->ccw.count = irq_ptr->aqueue.count;
1418 irq_ptr->ccw.cda = 0;
1419
1420 spin_lock_irq(get_ccwdev_lock(cdev));
1421 ccw_device_set_options(cdev, CCWDEV_REPORT_ALL);
1422
1423 rc = ccw_device_start(cdev, &irq_ptr->ccw, QDIO_DOING_ACTIVATE,
1424 0, DOIO_DENY_PREFETCH);
1425 spin_unlock_irq(get_ccwdev_lock(cdev));
1426 if (rc) {
1427 DBF_ERROR("%4x act IO ERR", irq_ptr->schid.sch_no);
1428 DBF_ERROR("rc:%4x", rc);
1429 goto out;
1430 }
1431
1432 if (is_thinint_irq(irq_ptr))
1433 tiqdio_add_input_queues(irq_ptr);
1434
1435 /* wait for subchannel to become active */
1436 msleep(5);
1437
1438 switch (irq_ptr->state) {
1439 case QDIO_IRQ_STATE_STOPPED:
1440 case QDIO_IRQ_STATE_ERR:
1441 rc = -EIO;
1442 break;
1443 default:
1444 qdio_set_state(irq_ptr, QDIO_IRQ_STATE_ACTIVE);
1445 rc = 0;
1446 }
1447 out:
1448 mutex_unlock(&irq_ptr->setup_mutex);
1449 return rc;
1450 }
1451 EXPORT_SYMBOL_GPL(qdio_activate);
1452
1453 static inline int buf_in_between(int bufnr, int start, int count)
1454 {
1455 int end = add_buf(start, count);
1456
1457 if (end > start) {
1458 if (bufnr >= start && bufnr < end)
1459 return 1;
1460 else
1461 return 0;
1462 }
1463
1464 /* wrap-around case */
1465 if ((bufnr >= start && bufnr <= QDIO_MAX_BUFFERS_PER_Q) ||
1466 (bufnr < end))
1467 return 1;
1468 else
1469 return 0;
1470 }
1471
1472 /**
1473 * handle_inbound - reset processed input buffers
1474 * @q: queue containing the buffers
1475 * @callflags: flags
1476 * @bufnr: first buffer to process
1477 * @count: how many buffers are emptied
1478 */
1479 static int handle_inbound(struct qdio_q *q, unsigned int callflags,
1480 int bufnr, int count)
1481 {
1482 int diff;
1483
1484 qperf_inc(q, inbound_call);
1485
1486 if (!q->u.in.polling)
1487 goto set;
1488
1489 /* protect against stop polling setting an ACK for an emptied slsb */
1490 if (count == QDIO_MAX_BUFFERS_PER_Q) {
1491 /* overwriting everything, just delete polling status */
1492 q->u.in.polling = 0;
1493 q->u.in.ack_count = 0;
1494 goto set;
1495 } else if (buf_in_between(q->u.in.ack_start, bufnr, count)) {
1496 if (is_qebsm(q)) {
1497 /* partial overwrite, just update ack_start */
1498 diff = add_buf(bufnr, count);
1499 diff = sub_buf(diff, q->u.in.ack_start);
1500 q->u.in.ack_count -= diff;
1501 if (q->u.in.ack_count <= 0) {
1502 q->u.in.polling = 0;
1503 q->u.in.ack_count = 0;
1504 goto set;
1505 }
1506 q->u.in.ack_start = add_buf(q->u.in.ack_start, diff);
1507 }
1508 else
1509 /* the only ACK will be deleted, so stop polling */
1510 q->u.in.polling = 0;
1511 }
1512
1513 set:
1514 count = set_buf_states(q, bufnr, SLSB_CU_INPUT_EMPTY, count);
1515 atomic_add(count, &q->nr_buf_used);
1516
1517 if (need_siga_in(q))
1518 return qdio_siga_input(q);
1519
1520 return 0;
1521 }
1522
1523 /**
1524 * handle_outbound - process filled outbound buffers
1525 * @q: queue containing the buffers
1526 * @callflags: flags
1527 * @bufnr: first buffer to process
1528 * @count: how many buffers are filled
1529 */
1530 static int handle_outbound(struct qdio_q *q, unsigned int callflags,
1531 unsigned int bufnr, unsigned int count)
1532 {
1533 const unsigned int scan_threshold = q->irq_ptr->scan_threshold;
1534 unsigned char state = 0;
1535 int used, rc = 0;
1536
1537 qperf_inc(q, outbound_call);
1538
1539 count = set_buf_states(q, bufnr, SLSB_CU_OUTPUT_PRIMED, count);
1540 used = atomic_add_return(count, &q->nr_buf_used);
1541
1542 if (used == QDIO_MAX_BUFFERS_PER_Q)
1543 qperf_inc(q, outbound_queue_full);
1544
1545 if (callflags & QDIO_FLAG_PCI_OUT) {
1546 q->u.out.pci_out_enabled = 1;
1547 qperf_inc(q, pci_request_int);
1548 } else
1549 q->u.out.pci_out_enabled = 0;
1550
1551 if (queue_type(q) == QDIO_IQDIO_QFMT) {
1552 unsigned long phys_aob = 0;
1553
1554 if (q->u.out.use_cq && count == 1)
1555 phys_aob = qdio_aob_for_buffer(&q->u.out, bufnr);
1556
1557 rc = qdio_kick_outbound_q(q, count, phys_aob);
1558 } else if (need_siga_sync(q)) {
1559 rc = qdio_siga_sync_q(q);
1560 } else if (count < QDIO_MAX_BUFFERS_PER_Q &&
1561 get_buf_state(q, prev_buf(bufnr), &state, 0) > 0 &&
1562 state == SLSB_CU_OUTPUT_PRIMED) {
1563 /* The previous buffer is not processed yet, tack on. */
1564 qperf_inc(q, fast_requeue);
1565 } else {
1566 rc = qdio_kick_outbound_q(q, count, 0);
1567 }
1568
1569 /* Let drivers implement their own completion scanning: */
1570 if (!scan_threshold)
1571 return rc;
1572
1573 /* in case of SIGA errors we must process the error immediately */
1574 if (used >= scan_threshold || rc)
1575 qdio_tasklet_schedule(q);
1576 else
1577 /* free the SBALs in case of no further traffic */
1578 if (!timer_pending(&q->u.out.timer) &&
1579 likely(q->irq_ptr->state == QDIO_IRQ_STATE_ACTIVE))
1580 mod_timer(&q->u.out.timer, jiffies + HZ);
1581 return rc;
1582 }
1583
1584 /**
1585 * do_QDIO - process input or output buffers
1586 * @cdev: associated ccw_device for the qdio subchannel
1587 * @callflags: input or output and special flags from the program
1588 * @q_nr: queue number
1589 * @bufnr: buffer number
1590 * @count: how many buffers to process
1591 */
1592 int do_QDIO(struct ccw_device *cdev, unsigned int callflags,
1593 int q_nr, unsigned int bufnr, unsigned int count)
1594 {
1595 struct qdio_irq *irq_ptr;
1596
1597 if (bufnr >= QDIO_MAX_BUFFERS_PER_Q || count > QDIO_MAX_BUFFERS_PER_Q)
1598 return -EINVAL;
1599
1600 irq_ptr = cdev->private->qdio_data;
1601 if (!irq_ptr)
1602 return -ENODEV;
1603
1604 DBF_DEV_EVENT(DBF_INFO, irq_ptr,
1605 "do%02x b:%02x c:%02x", callflags, bufnr, count);
1606
1607 if (irq_ptr->state != QDIO_IRQ_STATE_ACTIVE)
1608 return -EIO;
1609 if (!count)
1610 return 0;
1611 if (callflags & QDIO_FLAG_SYNC_INPUT)
1612 return handle_inbound(irq_ptr->input_qs[q_nr],
1613 callflags, bufnr, count);
1614 else if (callflags & QDIO_FLAG_SYNC_OUTPUT)
1615 return handle_outbound(irq_ptr->output_qs[q_nr],
1616 callflags, bufnr, count);
1617 return -EINVAL;
1618 }
1619 EXPORT_SYMBOL_GPL(do_QDIO);
1620
1621 /**
1622 * qdio_start_irq - process input buffers
1623 * @cdev: associated ccw_device for the qdio subchannel
1624 * @nr: input queue number
1625 *
1626 * Return codes
1627 * 0 - success
1628 * 1 - irqs not started since new data is available
1629 */
1630 int qdio_start_irq(struct ccw_device *cdev, int nr)
1631 {
1632 struct qdio_q *q;
1633 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1634
1635 if (!irq_ptr)
1636 return -ENODEV;
1637 q = irq_ptr->input_qs[nr];
1638
1639 clear_nonshared_ind(irq_ptr);
1640 qdio_stop_polling(q);
1641 clear_bit(QDIO_QUEUE_IRQS_DISABLED, &q->u.in.queue_irq_state);
1642
1643 /*
1644 * We need to check again to not lose initiative after
1645 * resetting the ACK state.
1646 */
1647 if (test_nonshared_ind(irq_ptr))
1648 goto rescan;
1649 if (!qdio_inbound_q_done(q, q->first_to_check))
1650 goto rescan;
1651 return 0;
1652
1653 rescan:
1654 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1655 &q->u.in.queue_irq_state))
1656 return 0;
1657 else
1658 return 1;
1659
1660 }
1661 EXPORT_SYMBOL(qdio_start_irq);
1662
1663 static int __qdio_inspect_queue(struct qdio_q *q, unsigned int *bufnr,
1664 unsigned int *error)
1665 {
1666 unsigned int start = q->first_to_check;
1667 int count;
1668
1669 count = q->is_input_q ? qdio_inbound_q_moved(q, start) :
1670 qdio_outbound_q_moved(q, start);
1671 if (count == 0)
1672 return 0;
1673
1674 *bufnr = start;
1675 *error = q->qdio_error;
1676
1677 /* for the next time */
1678 q->first_to_check = add_buf(start, count);
1679 q->qdio_error = 0;
1680
1681 return count;
1682 }
1683
1684 int qdio_inspect_queue(struct ccw_device *cdev, unsigned int nr, bool is_input,
1685 unsigned int *bufnr, unsigned int *error)
1686 {
1687 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1688 struct qdio_q *q;
1689
1690 if (!irq_ptr)
1691 return -ENODEV;
1692 q = is_input ? irq_ptr->input_qs[nr] : irq_ptr->output_qs[nr];
1693
1694 if (need_siga_sync(q))
1695 qdio_siga_sync_q(q);
1696
1697 return __qdio_inspect_queue(q, bufnr, error);
1698 }
1699 EXPORT_SYMBOL_GPL(qdio_inspect_queue);
1700
1701 /**
1702 * qdio_get_next_buffers - process input buffers
1703 * @cdev: associated ccw_device for the qdio subchannel
1704 * @nr: input queue number
1705 * @bufnr: first filled buffer number
1706 * @error: buffers are in error state
1707 *
1708 * Return codes
1709 * < 0 - error
1710 * = 0 - no new buffers found
1711 * > 0 - number of processed buffers
1712 */
1713 int qdio_get_next_buffers(struct ccw_device *cdev, int nr, int *bufnr,
1714 int *error)
1715 {
1716 struct qdio_q *q;
1717 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1718
1719 if (!irq_ptr)
1720 return -ENODEV;
1721 q = irq_ptr->input_qs[nr];
1722
1723 /*
1724 * Cannot rely on automatic sync after interrupt since queues may
1725 * also be examined without interrupt.
1726 */
1727 if (need_siga_sync(q))
1728 qdio_sync_queues(q);
1729
1730 qdio_check_outbound_pci_queues(irq_ptr);
1731
1732 /* Note: upper-layer MUST stop processing immediately here ... */
1733 if (unlikely(q->irq_ptr->state != QDIO_IRQ_STATE_ACTIVE))
1734 return -EIO;
1735
1736 return __qdio_inspect_queue(q, bufnr, error);
1737 }
1738 EXPORT_SYMBOL(qdio_get_next_buffers);
1739
1740 /**
1741 * qdio_stop_irq - disable interrupt processing for the device
1742 * @cdev: associated ccw_device for the qdio subchannel
1743 * @nr: input queue number
1744 *
1745 * Return codes
1746 * 0 - interrupts were already disabled
1747 * 1 - interrupts successfully disabled
1748 */
1749 int qdio_stop_irq(struct ccw_device *cdev, int nr)
1750 {
1751 struct qdio_q *q;
1752 struct qdio_irq *irq_ptr = cdev->private->qdio_data;
1753
1754 if (!irq_ptr)
1755 return -ENODEV;
1756 q = irq_ptr->input_qs[nr];
1757
1758 if (test_and_set_bit(QDIO_QUEUE_IRQS_DISABLED,
1759 &q->u.in.queue_irq_state))
1760 return 0;
1761 else
1762 return 1;
1763 }
1764 EXPORT_SYMBOL(qdio_stop_irq);
1765
1766 /**
1767 * qdio_pnso_brinfo() - perform network subchannel op #0 - bridge info.
1768 * @schid: Subchannel ID.
1769 * @cnc: Boolean Change-Notification Control
1770 * @response: Response code will be stored at this address
1771 * @cb: Callback function will be executed for each element
1772 * of the address list
1773 * @priv: Pointer to pass to the callback function.
1774 *
1775 * Performs "Store-network-bridging-information list" operation and calls
1776 * the callback function for every entry in the list. If "change-
1777 * notification-control" is set, further changes in the address list
1778 * will be reported via the IPA command.
1779 */
1780 int qdio_pnso_brinfo(struct subchannel_id schid,
1781 int cnc, u16 *response,
1782 void (*cb)(void *priv, enum qdio_brinfo_entry_type type,
1783 void *entry),
1784 void *priv)
1785 {
1786 struct chsc_pnso_area *rr;
1787 int rc;
1788 u32 prev_instance = 0;
1789 int isfirstblock = 1;
1790 int i, size, elems;
1791
1792 rr = (struct chsc_pnso_area *)get_zeroed_page(GFP_KERNEL);
1793 if (rr == NULL)
1794 return -ENOMEM;
1795 do {
1796 /* on the first iteration, naihdr.resume_token will be zero */
1797 rc = chsc_pnso_brinfo(schid, rr, rr->naihdr.resume_token, cnc);
1798 if (rc != 0 && rc != -EBUSY)
1799 goto out;
1800 if (rr->response.code != 1) {
1801 rc = -EIO;
1802 continue;
1803 } else
1804 rc = 0;
1805
1806 if (cb == NULL)
1807 continue;
1808
1809 size = rr->naihdr.naids;
1810 elems = (rr->response.length -
1811 sizeof(struct chsc_header) -
1812 sizeof(struct chsc_brinfo_naihdr)) /
1813 size;
1814
1815 if (!isfirstblock && (rr->naihdr.instance != prev_instance)) {
1816 /* Inform the caller that they need to scrap */
1817 /* the data that was already reported via cb */
1818 rc = -EAGAIN;
1819 break;
1820 }
1821 isfirstblock = 0;
1822 prev_instance = rr->naihdr.instance;
1823 for (i = 0; i < elems; i++)
1824 switch (size) {
1825 case sizeof(struct qdio_brinfo_entry_l3_ipv6):
1826 (*cb)(priv, l3_ipv6_addr,
1827 &rr->entries.l3_ipv6[i]);
1828 break;
1829 case sizeof(struct qdio_brinfo_entry_l3_ipv4):
1830 (*cb)(priv, l3_ipv4_addr,
1831 &rr->entries.l3_ipv4[i]);
1832 break;
1833 case sizeof(struct qdio_brinfo_entry_l2):
1834 (*cb)(priv, l2_addr_lnid,
1835 &rr->entries.l2[i]);
1836 break;
1837 default:
1838 WARN_ON_ONCE(1);
1839 rc = -EIO;
1840 goto out;
1841 }
1842 } while (rr->response.code == 0x0107 || /* channel busy */
1843 (rr->response.code == 1 && /* list stored */
1844 /* resume token is non-zero => list incomplete */
1845 (rr->naihdr.resume_token.t1 || rr->naihdr.resume_token.t2)));
1846 (*response) = rr->response.code;
1847
1848 out:
1849 free_page((unsigned long)rr);
1850 return rc;
1851 }
1852 EXPORT_SYMBOL_GPL(qdio_pnso_brinfo);
1853
1854 static int __init init_QDIO(void)
1855 {
1856 int rc;
1857
1858 rc = qdio_debug_init();
1859 if (rc)
1860 return rc;
1861 rc = qdio_setup_init();
1862 if (rc)
1863 goto out_debug;
1864 rc = tiqdio_allocate_memory();
1865 if (rc)
1866 goto out_cache;
1867 rc = tiqdio_register_thinints();
1868 if (rc)
1869 goto out_ti;
1870 return 0;
1871
1872 out_ti:
1873 tiqdio_free_memory();
1874 out_cache:
1875 qdio_setup_exit();
1876 out_debug:
1877 qdio_debug_exit();
1878 return rc;
1879 }
1880
1881 static void __exit exit_QDIO(void)
1882 {
1883 tiqdio_unregister_thinints();
1884 tiqdio_free_memory();
1885 qdio_setup_exit();
1886 qdio_debug_exit();
1887 }
1888
1889 module_init(init_QDIO);
1890 module_exit(exit_QDIO);