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