Merge branch 'linus' into cpus4096
[linux-2.6-block.git] / drivers / infiniband / hw / ehca / ehca_irq.c
CommitLineData
fab97220
HS
1/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 *
4 * Functions for EQs, NEQs and interrupts
5 *
6 * Authors: Heiko J Schick <schickhj@de.ibm.com>
7 * Khadija Souissi <souissi@de.ibm.com>
28db6beb
JF
8 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
9 * Joachim Fenkes <fenkes@de.ibm.com>
fab97220
HS
10 *
11 * Copyright (c) 2005 IBM Corporation
12 *
13 * All rights reserved.
14 *
15 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
16 * BSD.
17 *
18 * OpenIB BSD License
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are met:
22 *
23 * Redistributions of source code must retain the above copyright notice, this
24 * list of conditions and the following disclaimer.
25 *
26 * Redistributions in binary form must reproduce the above copyright notice,
27 * this list of conditions and the following disclaimer in the documentation
28 * and/or other materials
29 * provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
38 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44#include "ehca_classes.h"
45#include "ehca_irq.h"
46#include "ehca_iverbs.h"
47#include "ehca_tools.h"
48#include "hcp_if.h"
49#include "hipz_fns.h"
7e28db5d 50#include "ipz_pt_fn.h"
fab97220 51
2b94397a
HNN
52#define EQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
53#define EQE_CQ_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
54#define EQE_EE_IDENTIFIER EHCA_BMASK_IBM( 2, 7)
55#define EQE_CQ_NUMBER EHCA_BMASK_IBM( 8, 31)
56#define EQE_QP_NUMBER EHCA_BMASK_IBM( 8, 31)
57#define EQE_QP_TOKEN EHCA_BMASK_IBM(32, 63)
58#define EQE_CQ_TOKEN EHCA_BMASK_IBM(32, 63)
59
60#define NEQE_COMPLETION_EVENT EHCA_BMASK_IBM( 1, 1)
61#define NEQE_EVENT_CODE EHCA_BMASK_IBM( 2, 7)
62#define NEQE_PORT_NUMBER EHCA_BMASK_IBM( 8, 15)
63#define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16, 16)
64#define NEQE_DISRUPTIVE EHCA_BMASK_IBM(16, 16)
e57d62a1 65#define NEQE_SPECIFIC_EVENT EHCA_BMASK_IBM(16, 23)
2b94397a
HNN
66
67#define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52, 63)
68#define ERROR_DATA_TYPE EHCA_BMASK_IBM( 0, 7)
fab97220 69
fab97220
HS
70static void queue_comp_task(struct ehca_cq *__cq);
71
2b94397a 72static struct ehca_comp_pool *pool;
fab97220 73
fab97220
HS
74static inline void comp_event_callback(struct ehca_cq *cq)
75{
76 if (!cq->ib_cq.comp_handler)
77 return;
78
79 spin_lock(&cq->cb_lock);
80 cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context);
81 spin_unlock(&cq->cb_lock);
82
83 return;
84}
85
2b94397a
HNN
86static void print_error_data(struct ehca_shca *shca, void *data,
87 u64 *rblock, int length)
fab97220
HS
88{
89 u64 type = EHCA_BMASK_GET(ERROR_DATA_TYPE, rblock[2]);
90 u64 resource = rblock[1];
91
92 switch (type) {
93 case 0x1: /* Queue Pair */
94 {
2b94397a 95 struct ehca_qp *qp = (struct ehca_qp *)data;
fab97220
HS
96
97 /* only print error data if AER is set */
98 if (rblock[6] == 0)
99 return;
100
101 ehca_err(&shca->ib_device,
102 "QP 0x%x (resource=%lx) has errors.",
103 qp->ib_qp.qp_num, resource);
104 break;
105 }
106 case 0x4: /* Completion Queue */
107 {
2b94397a 108 struct ehca_cq *cq = (struct ehca_cq *)data;
fab97220
HS
109
110 ehca_err(&shca->ib_device,
111 "CQ 0x%x (resource=%lx) has errors.",
112 cq->cq_number, resource);
113 break;
114 }
115 default:
116 ehca_err(&shca->ib_device,
0e6ff158 117 "Unknown error type: %lx on %s.",
fab97220
HS
118 type, shca->ib_device.name);
119 break;
120 }
121
122 ehca_err(&shca->ib_device, "Error data is available: %lx.", resource);
123 ehca_err(&shca->ib_device, "EHCA ----- error data begin "
124 "---------------------------------------------------");
125 ehca_dmp(rblock, length, "resource=%lx", resource);
126 ehca_err(&shca->ib_device, "EHCA ----- error data end "
127 "----------------------------------------------------");
128
129 return;
130}
131
132int ehca_error_data(struct ehca_shca *shca, void *data,
133 u64 resource)
134{
135
136 unsigned long ret;
137 u64 *rblock;
138 unsigned long block_count;
139
f2d91361 140 rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
fab97220
HS
141 if (!rblock) {
142 ehca_err(&shca->ib_device, "Cannot allocate rblock memory.");
143 ret = -ENOMEM;
144 goto error_data1;
145 }
146
7e28db5d 147 /* rblock must be 4K aligned and should be 4K large */
fab97220
HS
148 ret = hipz_h_error_data(shca->ipz_hca_handle,
149 resource,
150 rblock,
151 &block_count);
152
7e28db5d 153 if (ret == H_R_STATE)
fab97220
HS
154 ehca_err(&shca->ib_device,
155 "No error data is available: %lx.", resource);
fab97220
HS
156 else if (ret == H_SUCCESS) {
157 int length;
158
159 length = EHCA_BMASK_GET(ERROR_DATA_LENGTH, rblock[0]);
160
7e28db5d
HNN
161 if (length > EHCA_PAGESIZE)
162 length = EHCA_PAGESIZE;
fab97220
HS
163
164 print_error_data(shca, data, rblock, length);
7e28db5d 165 } else
fab97220
HS
166 ehca_err(&shca->ib_device,
167 "Error data could not be fetched: %lx", resource);
fab97220 168
7e28db5d 169 ehca_free_fw_ctrlblock(rblock);
fab97220
HS
170
171error_data1:
172 return ret;
173
174}
175
5ff70cac
JF
176static void dispatch_qp_event(struct ehca_shca *shca, struct ehca_qp *qp,
177 enum ib_event_type event_type)
fab97220
HS
178{
179 struct ib_event event;
fab97220 180
633a5aed 181 event.device = &shca->ib_device;
5ff70cac 182 event.event = event_type;
fab97220 183
633a5aed
HNN
184 if (qp->ext_type == EQPT_SRQ) {
185 if (!qp->ib_srq.event_handler)
186 return;
fab97220 187
633a5aed
HNN
188 event.element.srq = &qp->ib_srq;
189 qp->ib_srq.event_handler(&event, qp->ib_srq.srq_context);
190 } else {
191 if (!qp->ib_qp.event_handler)
192 return;
193
633a5aed
HNN
194 event.element.qp = &qp->ib_qp;
195 qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
196 }
5ff70cac
JF
197}
198
199static void qp_event_callback(struct ehca_shca *shca, u64 eqe,
200 enum ib_event_type event_type, int fatal)
201{
202 struct ehca_qp *qp;
203 u32 token = EHCA_BMASK_GET(EQE_QP_TOKEN, eqe);
204
205 read_lock(&ehca_qp_idr_lock);
206 qp = idr_find(&ehca_qp_idr, token);
12137c59
SR
207 if (qp)
208 atomic_inc(&qp->nr_events);
5ff70cac
JF
209 read_unlock(&ehca_qp_idr_lock);
210
211 if (!qp)
212 return;
213
214 if (fatal)
215 ehca_error_data(shca, qp, qp->ipz_qp_handle.handle);
216
217 dispatch_qp_event(shca, qp, fatal && qp->ext_type == EQPT_SRQ ?
218 IB_EVENT_SRQ_ERR : event_type);
219
220 /*
221 * eHCA only processes one WQE at a time for SRQ base QPs,
222 * so the last WQE has been processed as soon as the QP enters
223 * error state.
224 */
225 if (fatal && qp->ext_type == EQPT_SRQBASE)
226 dispatch_qp_event(shca, qp, IB_EVENT_QP_LAST_WQE_REACHED);
fab97220 227
12137c59
SR
228 if (atomic_dec_and_test(&qp->nr_events))
229 wake_up(&qp->wait_completion);
fab97220
HS
230 return;
231}
232
233static void cq_event_callback(struct ehca_shca *shca,
78d8d5f9 234 u64 eqe)
fab97220
HS
235{
236 struct ehca_cq *cq;
fab97220
HS
237 u32 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe);
238
26ed687f 239 read_lock(&ehca_cq_idr_lock);
fab97220 240 cq = idr_find(&ehca_cq_idr, token);
28db6beb
JF
241 if (cq)
242 atomic_inc(&cq->nr_events);
26ed687f 243 read_unlock(&ehca_cq_idr_lock);
fab97220
HS
244
245 if (!cq)
246 return;
247
248 ehca_error_data(shca, cq, cq->ipz_cq_handle.handle);
249
28db6beb
JF
250 if (atomic_dec_and_test(&cq->nr_events))
251 wake_up(&cq->wait_completion);
252
fab97220
HS
253 return;
254}
255
256static void parse_identifier(struct ehca_shca *shca, u64 eqe)
257{
258 u8 identifier = EHCA_BMASK_GET(EQE_EE_IDENTIFIER, eqe);
259
260 switch (identifier) {
261 case 0x02: /* path migrated */
633a5aed 262 qp_event_callback(shca, eqe, IB_EVENT_PATH_MIG, 0);
fab97220
HS
263 break;
264 case 0x03: /* communication established */
633a5aed 265 qp_event_callback(shca, eqe, IB_EVENT_COMM_EST, 0);
fab97220
HS
266 break;
267 case 0x04: /* send queue drained */
633a5aed 268 qp_event_callback(shca, eqe, IB_EVENT_SQ_DRAINED, 0);
fab97220
HS
269 break;
270 case 0x05: /* QP error */
271 case 0x06: /* QP error */
633a5aed 272 qp_event_callback(shca, eqe, IB_EVENT_QP_FATAL, 1);
fab97220
HS
273 break;
274 case 0x07: /* CQ error */
275 case 0x08: /* CQ error */
276 cq_event_callback(shca, eqe);
277 break;
278 case 0x09: /* MRMWPTE error */
279 ehca_err(&shca->ib_device, "MRMWPTE error.");
280 break;
281 case 0x0A: /* port event */
282 ehca_err(&shca->ib_device, "Port event.");
283 break;
284 case 0x0B: /* MR access error */
285 ehca_err(&shca->ib_device, "MR access error.");
286 break;
287 case 0x0C: /* EQ error */
288 ehca_err(&shca->ib_device, "EQ error.");
289 break;
290 case 0x0D: /* P/Q_Key mismatch */
291 ehca_err(&shca->ib_device, "P/Q_Key mismatch.");
292 break;
293 case 0x10: /* sampling complete */
294 ehca_err(&shca->ib_device, "Sampling complete.");
295 break;
296 case 0x11: /* unaffiliated access error */
297 ehca_err(&shca->ib_device, "Unaffiliated access error.");
298 break;
e90d0b3d
JF
299 case 0x12: /* path migrating */
300 ehca_err(&shca->ib_device, "Path migrating.");
fab97220
HS
301 break;
302 case 0x13: /* interface trace stopped */
303 ehca_err(&shca->ib_device, "Interface trace stopped.");
304 break;
305 case 0x14: /* first error capture info available */
633a5aed
HNN
306 ehca_info(&shca->ib_device, "First error capture available");
307 break;
308 case 0x15: /* SRQ limit reached */
309 qp_event_callback(shca, eqe, IB_EVENT_SRQ_LIMIT_REACHED, 0);
310 break;
fab97220
HS
311 default:
312 ehca_err(&shca->ib_device, "Unknown identifier: %x on %s.",
313 identifier, shca->ib_device.name);
314 break;
315 }
316
317 return;
318}
319
8705ce5b
JF
320static void dispatch_port_event(struct ehca_shca *shca, int port_num,
321 enum ib_event_type type, const char *msg)
fab97220
HS
322{
323 struct ib_event event;
8705ce5b
JF
324
325 ehca_info(&shca->ib_device, "port %d %s.", port_num, msg);
326 event.device = &shca->ib_device;
327 event.event = type;
328 event.element.port_num = port_num;
329 ib_dispatch_event(&event);
330}
331
332static void notify_port_conf_change(struct ehca_shca *shca, int port_num)
333{
334 struct ehca_sma_attr new_attr;
335 struct ehca_sma_attr *old_attr = &shca->sport[port_num - 1].saved_attr;
336
337 ehca_query_sma_attr(shca, port_num, &new_attr);
338
339 if (new_attr.sm_sl != old_attr->sm_sl ||
340 new_attr.sm_lid != old_attr->sm_lid)
341 dispatch_port_event(shca, port_num, IB_EVENT_SM_CHANGE,
342 "SM changed");
343
344 if (new_attr.lid != old_attr->lid ||
345 new_attr.lmc != old_attr->lmc)
346 dispatch_port_event(shca, port_num, IB_EVENT_LID_CHANGE,
347 "LID changed");
348
349 if (new_attr.pkey_tbl_len != old_attr->pkey_tbl_len ||
350 memcmp(new_attr.pkeys, old_attr->pkeys,
351 sizeof(u16) * new_attr.pkey_tbl_len))
352 dispatch_port_event(shca, port_num, IB_EVENT_PKEY_CHANGE,
353 "P_Key changed");
354
355 *old_attr = new_attr;
356}
357
358static void parse_ec(struct ehca_shca *shca, u64 eqe)
359{
fab97220
HS
360 u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
361 u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
e57d62a1 362 u8 spec_event;
bbdd267e
HNN
363 struct ehca_sport *sport = &shca->sport[port - 1];
364 unsigned long flags;
fab97220
HS
365
366 switch (ec) {
367 case 0x30: /* port availability change */
368 if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
bbdd267e
HNN
369 int suppress_event;
370 /* replay modify_qp for sqps */
371 spin_lock_irqsave(&sport->mod_sqp_lock, flags);
372 suppress_event = !sport->ibqp_sqp[IB_QPT_GSI];
373 if (sport->ibqp_sqp[IB_QPT_SMI])
374 ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_SMI]);
375 if (!suppress_event)
376 ehca_recover_sqp(sport->ibqp_sqp[IB_QPT_GSI]);
377 spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);
378
379 /* AQP1 was destroyed, ignore this event */
380 if (suppress_event)
381 break;
382
383 sport->port_state = IB_PORT_ACTIVE;
8705ce5b
JF
384 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
385 "is active");
386 ehca_query_sma_attr(shca, port,
bbdd267e 387 &sport->saved_attr);
fab97220 388 } else {
bbdd267e 389 sport->port_state = IB_PORT_DOWN;
8705ce5b
JF
390 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
391 "is inactive");
fab97220
HS
392 }
393 break;
394 case 0x31:
395 /* port configuration change
396 * disruptive change is caused by
397 * LID, PKEY or SM change
398 */
8705ce5b
JF
399 if (EHCA_BMASK_GET(NEQE_DISRUPTIVE, eqe)) {
400 ehca_warn(&shca->ib_device, "disruptive port "
401 "%d configuration change", port);
402
bbdd267e 403 sport->port_state = IB_PORT_DOWN;
8705ce5b
JF
404 dispatch_port_event(shca, port, IB_EVENT_PORT_ERR,
405 "is inactive");
406
bbdd267e 407 sport->port_state = IB_PORT_ACTIVE;
8705ce5b
JF
408 dispatch_port_event(shca, port, IB_EVENT_PORT_ACTIVE,
409 "is active");
528b03f7
JF
410 ehca_query_sma_attr(shca, port,
411 &sport->saved_attr);
8705ce5b
JF
412 } else
413 notify_port_conf_change(shca, port);
fab97220
HS
414 break;
415 case 0x32: /* adapter malfunction */
416 ehca_err(&shca->ib_device, "Adapter malfunction.");
417 break;
418 case 0x33: /* trace stopped */
419 ehca_err(&shca->ib_device, "Traced stopped.");
420 break;
e57d62a1
HNN
421 case 0x34: /* util async event */
422 spec_event = EHCA_BMASK_GET(NEQE_SPECIFIC_EVENT, eqe);
423 if (spec_event == 0x80) /* client reregister required */
424 dispatch_port_event(shca, port,
425 IB_EVENT_CLIENT_REREGISTER,
426 "client reregister req.");
427 else
428 ehca_warn(&shca->ib_device, "Unknown util async "
429 "event %x on port %x", spec_event, port);
430 break;
fab97220
HS
431 default:
432 ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
433 ec, shca->ib_device.name);
434 break;
435 }
436
437 return;
438}
439
440static inline void reset_eq_pending(struct ehca_cq *cq)
441{
442 u64 CQx_EP;
443 struct h_galpa gal = cq->galpas.kernel;
444
445 hipz_galpa_store_cq(gal, cqx_ep, 0x0);
446 CQx_EP = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_ep));
447
448 return;
449}
450
7d12e780 451irqreturn_t ehca_interrupt_neq(int irq, void *dev_id)
fab97220
HS
452{
453 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
454
455 tasklet_hi_schedule(&shca->neq.interrupt_task);
456
457 return IRQ_HANDLED;
458}
459
460void ehca_tasklet_neq(unsigned long data)
461{
462 struct ehca_shca *shca = (struct ehca_shca*)data;
463 struct ehca_eqe *eqe;
464 u64 ret;
465
466 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
467
468 while (eqe) {
469 if (!EHCA_BMASK_GET(NEQE_COMPLETION_EVENT, eqe->entry))
470 parse_ec(shca, eqe->entry);
471
472 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
473 }
474
475 ret = hipz_h_reset_event(shca->ipz_hca_handle,
476 shca->neq.ipz_eq_handle, 0xFFFFFFFFFFFFFFFFL);
477
478 if (ret != H_SUCCESS)
479 ehca_err(&shca->ib_device, "Can't clear notification events.");
480
481 return;
482}
483
7d12e780 484irqreturn_t ehca_interrupt_eq(int irq, void *dev_id)
fab97220
HS
485{
486 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
487
488 tasklet_hi_schedule(&shca->eq.interrupt_task);
489
490 return IRQ_HANDLED;
491}
492
fab97220 493
78d8d5f9
HNN
494static inline void process_eqe(struct ehca_shca *shca, struct ehca_eqe *eqe)
495{
496 u64 eqe_value;
497 u32 token;
78d8d5f9 498 struct ehca_cq *cq;
31726798 499
78d8d5f9
HNN
500 eqe_value = eqe->entry;
501 ehca_dbg(&shca->ib_device, "eqe_value=%lx", eqe_value);
502 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
31726798 503 ehca_dbg(&shca->ib_device, "Got completion event");
78d8d5f9 504 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
26ed687f 505 read_lock(&ehca_cq_idr_lock);
78d8d5f9 506 cq = idr_find(&ehca_cq_idr, token);
28db6beb
JF
507 if (cq)
508 atomic_inc(&cq->nr_events);
26ed687f 509 read_unlock(&ehca_cq_idr_lock);
78d8d5f9 510 if (cq == NULL) {
78d8d5f9
HNN
511 ehca_err(&shca->ib_device,
512 "Invalid eqe for non-existing cq token=%x",
513 token);
514 return;
515 }
516 reset_eq_pending(cq);
31726798 517 if (ehca_scaling_code)
4fd30060 518 queue_comp_task(cq);
31726798 519 else {
4fd30060 520 comp_event_callback(cq);
28db6beb 521 if (atomic_dec_and_test(&cq->nr_events))
31726798 522 wake_up(&cq->wait_completion);
4fd30060 523 }
78d8d5f9 524 } else {
31726798 525 ehca_dbg(&shca->ib_device, "Got non completion event");
78d8d5f9
HNN
526 parse_identifier(shca, eqe_value);
527 }
528}
fab97220 529
78d8d5f9
HNN
530void ehca_process_eq(struct ehca_shca *shca, int is_irq)
531{
532 struct ehca_eq *eq = &shca->eq;
533 struct ehca_eqe_cache_entry *eqe_cache = eq->eqe_cache;
6f7bc01a 534 u64 eqe_value, ret;
78d8d5f9
HNN
535 unsigned long flags;
536 int eqe_cnt, i;
537 int eq_empty = 0;
538
539 spin_lock_irqsave(&eq->irq_spinlock, flags);
540 if (is_irq) {
541 const int max_query_cnt = 100;
542 int query_cnt = 0;
543 int int_state = 1;
544 do {
545 int_state = hipz_h_query_int_state(
546 shca->ipz_hca_handle, eq->ist);
547 query_cnt++;
548 iosync();
549 } while (int_state && query_cnt < max_query_cnt);
550 if (unlikely((query_cnt == max_query_cnt)))
551 ehca_dbg(&shca->ib_device, "int_state=%x query_cnt=%x",
552 int_state, query_cnt);
553 }
fab97220 554
78d8d5f9
HNN
555 /* read out all eqes */
556 eqe_cnt = 0;
557 do {
558 u32 token;
559 eqe_cache[eqe_cnt].eqe =
560 (struct ehca_eqe *)ehca_poll_eq(shca, eq);
561 if (!eqe_cache[eqe_cnt].eqe)
562 break;
563 eqe_value = eqe_cache[eqe_cnt].eqe->entry;
564 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
565 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
26ed687f 566 read_lock(&ehca_cq_idr_lock);
78d8d5f9 567 eqe_cache[eqe_cnt].cq = idr_find(&ehca_cq_idr, token);
28db6beb
JF
568 if (eqe_cache[eqe_cnt].cq)
569 atomic_inc(&eqe_cache[eqe_cnt].cq->nr_events);
26ed687f 570 read_unlock(&ehca_cq_idr_lock);
78d8d5f9 571 if (!eqe_cache[eqe_cnt].cq) {
78d8d5f9
HNN
572 ehca_err(&shca->ib_device,
573 "Invalid eqe for non-existing cq "
574 "token=%x", token);
575 continue;
576 }
78d8d5f9
HNN
577 } else
578 eqe_cache[eqe_cnt].cq = NULL;
579 eqe_cnt++;
580 } while (eqe_cnt < EHCA_EQE_CACHE_SIZE);
581 if (!eqe_cnt) {
582 if (is_irq)
583 ehca_dbg(&shca->ib_device,
584 "No eqe found for irq event");
585 goto unlock_irq_spinlock;
6f7bc01a
SR
586 } else if (!is_irq) {
587 ret = hipz_h_eoi(eq->ist);
588 if (ret != H_SUCCESS)
589 ehca_err(&shca->ib_device,
590 "bad return code EOI -rc = %ld\n", ret);
78d8d5f9 591 ehca_dbg(&shca->ib_device, "deadman found %x eqe", eqe_cnt);
6f7bc01a 592 }
78d8d5f9
HNN
593 if (unlikely(eqe_cnt == EHCA_EQE_CACHE_SIZE))
594 ehca_dbg(&shca->ib_device, "too many eqes for one irq event");
595 /* enable irq for new packets */
596 for (i = 0; i < eqe_cnt; i++) {
597 if (eq->eqe_cache[i].cq)
598 reset_eq_pending(eq->eqe_cache[i].cq);
599 }
600 /* check eq */
601 spin_lock(&eq->spinlock);
602 eq_empty = (!ipz_eqit_eq_peek_valid(&shca->eq.ipz_queue));
603 spin_unlock(&eq->spinlock);
604 /* call completion handler for cached eqes */
605 for (i = 0; i < eqe_cnt; i++)
606 if (eq->eqe_cache[i].cq) {
31726798 607 if (ehca_scaling_code)
4fd30060 608 queue_comp_task(eq->eqe_cache[i].cq);
31726798
HNN
609 else {
610 struct ehca_cq *cq = eq->eqe_cache[i].cq;
611 comp_event_callback(cq);
28db6beb 612 if (atomic_dec_and_test(&cq->nr_events))
31726798 613 wake_up(&cq->wait_completion);
31726798 614 }
78d8d5f9
HNN
615 } else {
616 ehca_dbg(&shca->ib_device, "Got non completion event");
617 parse_identifier(shca, eq->eqe_cache[i].eqe->entry);
fab97220 618 }
78d8d5f9
HNN
619 /* poll eq if not empty */
620 if (eq_empty)
621 goto unlock_irq_spinlock;
622 do {
623 struct ehca_eqe *eqe;
624 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->eq);
625 if (!eqe)
626 break;
627 process_eqe(shca, eqe);
78d8d5f9
HNN
628 } while (1);
629
630unlock_irq_spinlock:
631 spin_unlock_irqrestore(&eq->irq_spinlock, flags);
632}
fab97220 633
78d8d5f9
HNN
634void ehca_tasklet_eq(unsigned long data)
635{
636 ehca_process_eq((struct ehca_shca*)data, 1);
fab97220
HS
637}
638
2b94397a 639static inline int find_next_online_cpu(struct ehca_comp_pool *pool)
fab97220 640{
8b16cef3
HNN
641 int cpu;
642 unsigned long flags;
fab97220 643
8b16cef3 644 WARN_ON_ONCE(!in_interrupt());
4da27d6d 645 if (ehca_debug_level >= 3)
fab97220
HS
646 ehca_dmp(&cpu_online_map, sizeof(cpumask_t), "");
647
8b16cef3 648 spin_lock_irqsave(&pool->last_cpu_lock, flags);
5d7bfd0c
MT
649 cpu = next_cpu_nr(pool->last_cpu, cpu_online_map);
650 if (cpu >= nr_cpu_ids)
8b16cef3
HNN
651 cpu = first_cpu(cpu_online_map);
652 pool->last_cpu = cpu;
653 spin_unlock_irqrestore(&pool->last_cpu_lock, flags);
fab97220 654
8b16cef3 655 return cpu;
fab97220
HS
656}
657
658static void __queue_comp_task(struct ehca_cq *__cq,
659 struct ehca_cpu_comp_task *cct)
660{
8b16cef3 661 unsigned long flags;
fab97220 662
8b16cef3
HNN
663 spin_lock_irqsave(&cct->task_lock, flags);
664 spin_lock(&__cq->task_lock);
fab97220
HS
665
666 if (__cq->nr_callbacks == 0) {
667 __cq->nr_callbacks++;
668 list_add_tail(&__cq->entry, &cct->cq_list);
669 cct->cq_jobs++;
670 wake_up(&cct->wait_queue);
31726798 671 } else
fab97220
HS
672 __cq->nr_callbacks++;
673
8b16cef3
HNN
674 spin_unlock(&__cq->task_lock);
675 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220
HS
676}
677
678static void queue_comp_task(struct ehca_cq *__cq)
679{
fab97220
HS
680 int cpu_id;
681 struct ehca_cpu_comp_task *cct;
31726798
HNN
682 int cq_jobs;
683 unsigned long flags;
fab97220 684
fab97220 685 cpu_id = find_next_online_cpu(pool);
fab97220
HS
686 BUG_ON(!cpu_online(cpu_id));
687
688 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
8b16cef3 689 BUG_ON(!cct);
fab97220 690
31726798
HNN
691 spin_lock_irqsave(&cct->task_lock, flags);
692 cq_jobs = cct->cq_jobs;
693 spin_unlock_irqrestore(&cct->task_lock, flags);
694 if (cq_jobs > 0) {
fab97220
HS
695 cpu_id = find_next_online_cpu(pool);
696 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
8b16cef3 697 BUG_ON(!cct);
fab97220
HS
698 }
699
700 __queue_comp_task(__cq, cct);
fab97220
HS
701}
702
2b94397a 703static void run_comp_task(struct ehca_cpu_comp_task *cct)
fab97220
HS
704{
705 struct ehca_cq *cq;
8b16cef3 706 unsigned long flags;
fab97220 707
8b16cef3 708 spin_lock_irqsave(&cct->task_lock, flags);
fab97220
HS
709
710 while (!list_empty(&cct->cq_list)) {
711 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
8b16cef3 712 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220 713
28db6beb
JF
714 comp_event_callback(cq);
715 if (atomic_dec_and_test(&cq->nr_events))
31726798 716 wake_up(&cq->wait_completion);
31726798
HNN
717
718 spin_lock_irqsave(&cct->task_lock, flags);
8b16cef3 719 spin_lock(&cq->task_lock);
fab97220 720 cq->nr_callbacks--;
31726798 721 if (!cq->nr_callbacks) {
fab97220
HS
722 list_del_init(cct->cq_list.next);
723 cct->cq_jobs--;
724 }
8b16cef3 725 spin_unlock(&cq->task_lock);
fab97220
HS
726 }
727
8b16cef3 728 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220
HS
729}
730
731static int comp_task(void *__cct)
732{
2b94397a 733 struct ehca_cpu_comp_task *cct = __cct;
8b16cef3 734 int cql_empty;
fab97220
HS
735 DECLARE_WAITQUEUE(wait, current);
736
737 set_current_state(TASK_INTERRUPTIBLE);
2b94397a 738 while (!kthread_should_stop()) {
fab97220
HS
739 add_wait_queue(&cct->wait_queue, &wait);
740
8b16cef3
HNN
741 spin_lock_irq(&cct->task_lock);
742 cql_empty = list_empty(&cct->cq_list);
743 spin_unlock_irq(&cct->task_lock);
744 if (cql_empty)
fab97220
HS
745 schedule();
746 else
747 __set_current_state(TASK_RUNNING);
748
749 remove_wait_queue(&cct->wait_queue, &wait);
750
8b16cef3
HNN
751 spin_lock_irq(&cct->task_lock);
752 cql_empty = list_empty(&cct->cq_list);
753 spin_unlock_irq(&cct->task_lock);
754 if (!cql_empty)
fab97220
HS
755 run_comp_task(__cct);
756
757 set_current_state(TASK_INTERRUPTIBLE);
758 }
759 __set_current_state(TASK_RUNNING);
760
761 return 0;
762}
763
764static struct task_struct *create_comp_task(struct ehca_comp_pool *pool,
765 int cpu)
766{
767 struct ehca_cpu_comp_task *cct;
768
769 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
770 spin_lock_init(&cct->task_lock);
771 INIT_LIST_HEAD(&cct->cq_list);
772 init_waitqueue_head(&cct->wait_queue);
773 cct->task = kthread_create(comp_task, cct, "ehca_comp/%d", cpu);
774
775 return cct->task;
776}
777
778static void destroy_comp_task(struct ehca_comp_pool *pool,
779 int cpu)
780{
781 struct ehca_cpu_comp_task *cct;
782 struct task_struct *task;
783 unsigned long flags_cct;
784
785 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
786
787 spin_lock_irqsave(&cct->task_lock, flags_cct);
788
789 task = cct->task;
790 cct->task = NULL;
791 cct->cq_jobs = 0;
792
793 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
794
795 if (task)
796 kthread_stop(task);
fab97220
HS
797}
798
9faa559c 799static void __cpuinit take_over_work(struct ehca_comp_pool *pool, int cpu)
fab97220
HS
800{
801 struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
802 LIST_HEAD(list);
803 struct ehca_cq *cq;
804 unsigned long flags_cct;
805
806 spin_lock_irqsave(&cct->task_lock, flags_cct);
807
808 list_splice_init(&cct->cq_list, &list);
809
2b94397a 810 while (!list_empty(&list)) {
78d8d5f9 811 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
fab97220 812
78d8d5f9
HNN
813 list_del(&cq->entry);
814 __queue_comp_task(cq, per_cpu_ptr(pool->cpu_comp_tasks,
815 smp_processor_id()));
fab97220
HS
816 }
817
818 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
819
820}
821
9faa559c
SS
822static int __cpuinit comp_pool_callback(struct notifier_block *nfb,
823 unsigned long action,
824 void *hcpu)
fab97220
HS
825{
826 unsigned int cpu = (unsigned long)hcpu;
827 struct ehca_cpu_comp_task *cct;
828
829 switch (action) {
830 case CPU_UP_PREPARE:
8bb78442 831 case CPU_UP_PREPARE_FROZEN:
fab97220 832 ehca_gen_dbg("CPU: %x (CPU_PREPARE)", cpu);
2b94397a 833 if (!create_comp_task(pool, cpu)) {
fab97220
HS
834 ehca_gen_err("Can't create comp_task for cpu: %x", cpu);
835 return NOTIFY_BAD;
836 }
837 break;
838 case CPU_UP_CANCELED:
8bb78442 839 case CPU_UP_CANCELED_FROZEN:
fab97220
HS
840 ehca_gen_dbg("CPU: %x (CPU_CANCELED)", cpu);
841 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
842 kthread_bind(cct->task, any_online_cpu(cpu_online_map));
843 destroy_comp_task(pool, cpu);
844 break;
845 case CPU_ONLINE:
8bb78442 846 case CPU_ONLINE_FROZEN:
fab97220
HS
847 ehca_gen_dbg("CPU: %x (CPU_ONLINE)", cpu);
848 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
849 kthread_bind(cct->task, cpu);
850 wake_up_process(cct->task);
851 break;
852 case CPU_DOWN_PREPARE:
8bb78442 853 case CPU_DOWN_PREPARE_FROZEN:
fab97220
HS
854 ehca_gen_dbg("CPU: %x (CPU_DOWN_PREPARE)", cpu);
855 break;
856 case CPU_DOWN_FAILED:
8bb78442 857 case CPU_DOWN_FAILED_FROZEN:
fab97220
HS
858 ehca_gen_dbg("CPU: %x (CPU_DOWN_FAILED)", cpu);
859 break;
860 case CPU_DEAD:
8bb78442 861 case CPU_DEAD_FROZEN:
fab97220
HS
862 ehca_gen_dbg("CPU: %x (CPU_DEAD)", cpu);
863 destroy_comp_task(pool, cpu);
864 take_over_work(pool, cpu);
865 break;
866 }
867
868 return NOTIFY_OK;
869}
9faa559c
SS
870
871static struct notifier_block comp_pool_callback_nb __cpuinitdata = {
872 .notifier_call = comp_pool_callback,
873 .priority = 0,
874};
fab97220 875
fab97220
HS
876int ehca_create_comp_pool(void)
877{
fab97220
HS
878 int cpu;
879 struct task_struct *task;
880
4fd30060
HNN
881 if (!ehca_scaling_code)
882 return 0;
883
fab97220
HS
884 pool = kzalloc(sizeof(struct ehca_comp_pool), GFP_KERNEL);
885 if (pool == NULL)
886 return -ENOMEM;
887
888 spin_lock_init(&pool->last_cpu_lock);
889 pool->last_cpu = any_online_cpu(cpu_online_map);
890
891 pool->cpu_comp_tasks = alloc_percpu(struct ehca_cpu_comp_task);
892 if (pool->cpu_comp_tasks == NULL) {
893 kfree(pool);
894 return -EINVAL;
895 }
896
897 for_each_online_cpu(cpu) {
898 task = create_comp_task(pool, cpu);
899 if (task) {
900 kthread_bind(task, cpu);
901 wake_up_process(task);
902 }
903 }
904
9faa559c 905 register_hotcpu_notifier(&comp_pool_callback_nb);
4fd30060
HNN
906
907 printk(KERN_INFO "eHCA scaling code enabled\n");
fab97220
HS
908
909 return 0;
910}
911
912void ehca_destroy_comp_pool(void)
913{
fab97220
HS
914 int i;
915
4fd30060
HNN
916 if (!ehca_scaling_code)
917 return;
918
9faa559c 919 unregister_hotcpu_notifier(&comp_pool_callback_nb);
fab97220
HS
920
921 for (i = 0; i < NR_CPUS; i++) {
922 if (cpu_online(i))
923 destroy_comp_task(pool, i);
924 }
65e5c026
AM
925 free_percpu(pool->cpu_comp_tasks);
926 kfree(pool);
fab97220 927}