blob: 9679b072ad013bffbb265375535802f44d39839a [file] [log] [blame]
Heiko J Schickfab97222006-09-22 15:22:22 -07001/*
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>
8 *
9 * Copyright (c) 2005 IBM Corporation
10 *
11 * All rights reserved.
12 *
13 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
14 * BSD.
15 *
16 * OpenIB BSD License
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are met:
20 *
21 * Redistributions of source code must retain the above copyright notice, this
22 * list of conditions and the following disclaimer.
23 *
24 * Redistributions in binary form must reproduce the above copyright notice,
25 * this list of conditions and the following disclaimer in the documentation
26 * and/or other materials
27 * provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include "ehca_classes.h"
43#include "ehca_irq.h"
44#include "ehca_iverbs.h"
45#include "ehca_tools.h"
46#include "hcp_if.h"
47#include "hipz_fns.h"
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +010048#include "ipz_pt_fn.h"
Heiko J Schickfab97222006-09-22 15:22:22 -070049
50#define EQE_COMPLETION_EVENT EHCA_BMASK_IBM(1,1)
51#define EQE_CQ_QP_NUMBER EHCA_BMASK_IBM(8,31)
52#define EQE_EE_IDENTIFIER EHCA_BMASK_IBM(2,7)
53#define EQE_CQ_NUMBER EHCA_BMASK_IBM(8,31)
54#define EQE_QP_NUMBER EHCA_BMASK_IBM(8,31)
55#define EQE_QP_TOKEN EHCA_BMASK_IBM(32,63)
56#define EQE_CQ_TOKEN EHCA_BMASK_IBM(32,63)
57
58#define NEQE_COMPLETION_EVENT EHCA_BMASK_IBM(1,1)
59#define NEQE_EVENT_CODE EHCA_BMASK_IBM(2,7)
60#define NEQE_PORT_NUMBER EHCA_BMASK_IBM(8,15)
61#define NEQE_PORT_AVAILABILITY EHCA_BMASK_IBM(16,16)
62
63#define ERROR_DATA_LENGTH EHCA_BMASK_IBM(52,63)
64#define ERROR_DATA_TYPE EHCA_BMASK_IBM(0,7)
65
66#ifdef CONFIG_INFINIBAND_EHCA_SCALING
67
68static void queue_comp_task(struct ehca_cq *__cq);
69
70static struct ehca_comp_pool* pool;
71static struct notifier_block comp_pool_callback_nb;
72
73#endif
74
75static inline void comp_event_callback(struct ehca_cq *cq)
76{
77 if (!cq->ib_cq.comp_handler)
78 return;
79
80 spin_lock(&cq->cb_lock);
81 cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context);
82 spin_unlock(&cq->cb_lock);
83
84 return;
85}
86
87static void print_error_data(struct ehca_shca * shca, void* data,
88 u64* rblock, int length)
89{
90 u64 type = EHCA_BMASK_GET(ERROR_DATA_TYPE, rblock[2]);
91 u64 resource = rblock[1];
92
93 switch (type) {
94 case 0x1: /* Queue Pair */
95 {
96 struct ehca_qp *qp = (struct ehca_qp*)data;
97
98 /* only print error data if AER is set */
99 if (rblock[6] == 0)
100 return;
101
102 ehca_err(&shca->ib_device,
103 "QP 0x%x (resource=%lx) has errors.",
104 qp->ib_qp.qp_num, resource);
105 break;
106 }
107 case 0x4: /* Completion Queue */
108 {
109 struct ehca_cq *cq = (struct ehca_cq*)data;
110
111 ehca_err(&shca->ib_device,
112 "CQ 0x%x (resource=%lx) has errors.",
113 cq->cq_number, resource);
114 break;
115 }
116 default:
117 ehca_err(&shca->ib_device,
118 "Unknown errror type: %lx on %s.",
119 type, shca->ib_device.name);
120 break;
121 }
122
123 ehca_err(&shca->ib_device, "Error data is available: %lx.", resource);
124 ehca_err(&shca->ib_device, "EHCA ----- error data begin "
125 "---------------------------------------------------");
126 ehca_dmp(rblock, length, "resource=%lx", resource);
127 ehca_err(&shca->ib_device, "EHCA ----- error data end "
128 "----------------------------------------------------");
129
130 return;
131}
132
133int ehca_error_data(struct ehca_shca *shca, void *data,
134 u64 resource)
135{
136
137 unsigned long ret;
138 u64 *rblock;
139 unsigned long block_count;
140
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +0100141 rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
Heiko J Schickfab97222006-09-22 15:22:22 -0700142 if (!rblock) {
143 ehca_err(&shca->ib_device, "Cannot allocate rblock memory.");
144 ret = -ENOMEM;
145 goto error_data1;
146 }
147
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100148 /* rblock must be 4K aligned and should be 4K large */
Heiko J Schickfab97222006-09-22 15:22:22 -0700149 ret = hipz_h_error_data(shca->ipz_hca_handle,
150 resource,
151 rblock,
152 &block_count);
153
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100154 if (ret == H_R_STATE)
Heiko J Schickfab97222006-09-22 15:22:22 -0700155 ehca_err(&shca->ib_device,
156 "No error data is available: %lx.", resource);
Heiko J Schickfab97222006-09-22 15:22:22 -0700157 else if (ret == H_SUCCESS) {
158 int length;
159
160 length = EHCA_BMASK_GET(ERROR_DATA_LENGTH, rblock[0]);
161
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100162 if (length > EHCA_PAGESIZE)
163 length = EHCA_PAGESIZE;
Heiko J Schickfab97222006-09-22 15:22:22 -0700164
165 print_error_data(shca, data, rblock, length);
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100166 } else
Heiko J Schickfab97222006-09-22 15:22:22 -0700167 ehca_err(&shca->ib_device,
168 "Error data could not be fetched: %lx", resource);
Heiko J Schickfab97222006-09-22 15:22:22 -0700169
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +0100170 ehca_free_fw_ctrlblock(rblock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700171
172error_data1:
173 return ret;
174
175}
176
177static void qp_event_callback(struct ehca_shca *shca,
178 u64 eqe,
179 enum ib_event_type event_type)
180{
181 struct ib_event event;
182 struct ehca_qp *qp;
183 unsigned long flags;
184 u32 token = EHCA_BMASK_GET(EQE_QP_TOKEN, eqe);
185
186 spin_lock_irqsave(&ehca_qp_idr_lock, flags);
187 qp = idr_find(&ehca_qp_idr, token);
188 spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
189
190
191 if (!qp)
192 return;
193
194 ehca_error_data(shca, qp, qp->ipz_qp_handle.handle);
195
196 if (!qp->ib_qp.event_handler)
197 return;
198
199 event.device = &shca->ib_device;
200 event.event = event_type;
201 event.element.qp = &qp->ib_qp;
202
203 qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
204
205 return;
206}
207
208static void cq_event_callback(struct ehca_shca *shca,
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100209 u64 eqe)
Heiko J Schickfab97222006-09-22 15:22:22 -0700210{
211 struct ehca_cq *cq;
212 unsigned long flags;
213 u32 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe);
214
215 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
216 cq = idr_find(&ehca_cq_idr, token);
217 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
218
219 if (!cq)
220 return;
221
222 ehca_error_data(shca, cq, cq->ipz_cq_handle.handle);
223
224 return;
225}
226
227static void parse_identifier(struct ehca_shca *shca, u64 eqe)
228{
229 u8 identifier = EHCA_BMASK_GET(EQE_EE_IDENTIFIER, eqe);
230
231 switch (identifier) {
232 case 0x02: /* path migrated */
233 qp_event_callback(shca, eqe, IB_EVENT_PATH_MIG);
234 break;
235 case 0x03: /* communication established */
236 qp_event_callback(shca, eqe, IB_EVENT_COMM_EST);
237 break;
238 case 0x04: /* send queue drained */
239 qp_event_callback(shca, eqe, IB_EVENT_SQ_DRAINED);
240 break;
241 case 0x05: /* QP error */
242 case 0x06: /* QP error */
243 qp_event_callback(shca, eqe, IB_EVENT_QP_FATAL);
244 break;
245 case 0x07: /* CQ error */
246 case 0x08: /* CQ error */
247 cq_event_callback(shca, eqe);
248 break;
249 case 0x09: /* MRMWPTE error */
250 ehca_err(&shca->ib_device, "MRMWPTE error.");
251 break;
252 case 0x0A: /* port event */
253 ehca_err(&shca->ib_device, "Port event.");
254 break;
255 case 0x0B: /* MR access error */
256 ehca_err(&shca->ib_device, "MR access error.");
257 break;
258 case 0x0C: /* EQ error */
259 ehca_err(&shca->ib_device, "EQ error.");
260 break;
261 case 0x0D: /* P/Q_Key mismatch */
262 ehca_err(&shca->ib_device, "P/Q_Key mismatch.");
263 break;
264 case 0x10: /* sampling complete */
265 ehca_err(&shca->ib_device, "Sampling complete.");
266 break;
267 case 0x11: /* unaffiliated access error */
268 ehca_err(&shca->ib_device, "Unaffiliated access error.");
269 break;
270 case 0x12: /* path migrating error */
271 ehca_err(&shca->ib_device, "Path migration error.");
272 break;
273 case 0x13: /* interface trace stopped */
274 ehca_err(&shca->ib_device, "Interface trace stopped.");
275 break;
276 case 0x14: /* first error capture info available */
277 default:
278 ehca_err(&shca->ib_device, "Unknown identifier: %x on %s.",
279 identifier, shca->ib_device.name);
280 break;
281 }
282
283 return;
284}
285
286static void parse_ec(struct ehca_shca *shca, u64 eqe)
287{
288 struct ib_event event;
289 u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
290 u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
291
292 switch (ec) {
293 case 0x30: /* port availability change */
294 if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
295 ehca_info(&shca->ib_device,
296 "port %x is active.", port);
297 event.device = &shca->ib_device;
298 event.event = IB_EVENT_PORT_ACTIVE;
299 event.element.port_num = port;
300 shca->sport[port - 1].port_state = IB_PORT_ACTIVE;
301 ib_dispatch_event(&event);
302 } else {
303 ehca_info(&shca->ib_device,
304 "port %x is inactive.", port);
305 event.device = &shca->ib_device;
306 event.event = IB_EVENT_PORT_ERR;
307 event.element.port_num = port;
308 shca->sport[port - 1].port_state = IB_PORT_DOWN;
309 ib_dispatch_event(&event);
310 }
311 break;
312 case 0x31:
313 /* port configuration change
314 * disruptive change is caused by
315 * LID, PKEY or SM change
316 */
317 ehca_warn(&shca->ib_device,
318 "disruptive port %x configuration change", port);
319
320 ehca_info(&shca->ib_device,
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100321 "port %x is inactive.", port);
Heiko J Schickfab97222006-09-22 15:22:22 -0700322 event.device = &shca->ib_device;
323 event.event = IB_EVENT_PORT_ERR;
324 event.element.port_num = port;
325 shca->sport[port - 1].port_state = IB_PORT_DOWN;
326 ib_dispatch_event(&event);
327
328 ehca_info(&shca->ib_device,
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100329 "port %x is active.", port);
Heiko J Schickfab97222006-09-22 15:22:22 -0700330 event.device = &shca->ib_device;
331 event.event = IB_EVENT_PORT_ACTIVE;
332 event.element.port_num = port;
333 shca->sport[port - 1].port_state = IB_PORT_ACTIVE;
334 ib_dispatch_event(&event);
335 break;
336 case 0x32: /* adapter malfunction */
337 ehca_err(&shca->ib_device, "Adapter malfunction.");
338 break;
339 case 0x33: /* trace stopped */
340 ehca_err(&shca->ib_device, "Traced stopped.");
341 break;
342 default:
343 ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
344 ec, shca->ib_device.name);
345 break;
346 }
347
348 return;
349}
350
351static inline void reset_eq_pending(struct ehca_cq *cq)
352{
353 u64 CQx_EP;
354 struct h_galpa gal = cq->galpas.kernel;
355
356 hipz_galpa_store_cq(gal, cqx_ep, 0x0);
357 CQx_EP = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_ep));
358
359 return;
360}
361
David Howells7d12e782006-10-05 14:55:46 +0100362irqreturn_t ehca_interrupt_neq(int irq, void *dev_id)
Heiko J Schickfab97222006-09-22 15:22:22 -0700363{
364 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
365
366 tasklet_hi_schedule(&shca->neq.interrupt_task);
367
368 return IRQ_HANDLED;
369}
370
371void ehca_tasklet_neq(unsigned long data)
372{
373 struct ehca_shca *shca = (struct ehca_shca*)data;
374 struct ehca_eqe *eqe;
375 u64 ret;
376
377 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
378
379 while (eqe) {
380 if (!EHCA_BMASK_GET(NEQE_COMPLETION_EVENT, eqe->entry))
381 parse_ec(shca, eqe->entry);
382
383 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
384 }
385
386 ret = hipz_h_reset_event(shca->ipz_hca_handle,
387 shca->neq.ipz_eq_handle, 0xFFFFFFFFFFFFFFFFL);
388
389 if (ret != H_SUCCESS)
390 ehca_err(&shca->ib_device, "Can't clear notification events.");
391
392 return;
393}
394
David Howells7d12e782006-10-05 14:55:46 +0100395irqreturn_t ehca_interrupt_eq(int irq, void *dev_id)
Heiko J Schickfab97222006-09-22 15:22:22 -0700396{
397 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
398
399 tasklet_hi_schedule(&shca->eq.interrupt_task);
400
401 return IRQ_HANDLED;
402}
403
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100404
405static inline void process_eqe(struct ehca_shca *shca, struct ehca_eqe *eqe)
406{
407 u64 eqe_value;
408 u32 token;
409 unsigned long flags;
410 struct ehca_cq *cq;
411 eqe_value = eqe->entry;
412 ehca_dbg(&shca->ib_device, "eqe_value=%lx", eqe_value);
413 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
414 ehca_dbg(&shca->ib_device, "... completion event");
415 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
416 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
417 cq = idr_find(&ehca_cq_idr, token);
418 if (cq == NULL) {
419 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
420 ehca_err(&shca->ib_device,
421 "Invalid eqe for non-existing cq token=%x",
422 token);
423 return;
424 }
425 reset_eq_pending(cq);
426#ifdef CONFIG_INFINIBAND_EHCA_SCALING
427 queue_comp_task(cq);
428 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
429#else
430 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
431 comp_event_callback(cq);
432#endif
433 } else {
434 ehca_dbg(&shca->ib_device,
435 "Got non completion event");
436 parse_identifier(shca, eqe_value);
437 }
438}
439
440void ehca_process_eq(struct ehca_shca *shca, int is_irq)
441{
442 struct ehca_eq *eq = &shca->eq;
443 struct ehca_eqe_cache_entry *eqe_cache = eq->eqe_cache;
444 u64 eqe_value;
445 unsigned long flags;
446 int eqe_cnt, i;
447 int eq_empty = 0;
448
449 spin_lock_irqsave(&eq->irq_spinlock, flags);
450 if (is_irq) {
451 const int max_query_cnt = 100;
452 int query_cnt = 0;
453 int int_state = 1;
454 do {
455 int_state = hipz_h_query_int_state(
456 shca->ipz_hca_handle, eq->ist);
457 query_cnt++;
458 iosync();
459 } while (int_state && query_cnt < max_query_cnt);
460 if (unlikely((query_cnt == max_query_cnt)))
461 ehca_dbg(&shca->ib_device, "int_state=%x query_cnt=%x",
462 int_state, query_cnt);
463 }
464
465 /* read out all eqes */
466 eqe_cnt = 0;
467 do {
468 u32 token;
469 eqe_cache[eqe_cnt].eqe =
470 (struct ehca_eqe *)ehca_poll_eq(shca, eq);
471 if (!eqe_cache[eqe_cnt].eqe)
472 break;
473 eqe_value = eqe_cache[eqe_cnt].eqe->entry;
474 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
475 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
476 spin_lock(&ehca_cq_idr_lock);
477 eqe_cache[eqe_cnt].cq = idr_find(&ehca_cq_idr, token);
478 if (!eqe_cache[eqe_cnt].cq) {
479 spin_unlock(&ehca_cq_idr_lock);
480 ehca_err(&shca->ib_device,
481 "Invalid eqe for non-existing cq "
482 "token=%x", token);
483 continue;
484 }
485 spin_unlock(&ehca_cq_idr_lock);
486 } else
487 eqe_cache[eqe_cnt].cq = NULL;
488 eqe_cnt++;
489 } while (eqe_cnt < EHCA_EQE_CACHE_SIZE);
490 if (!eqe_cnt) {
491 if (is_irq)
492 ehca_dbg(&shca->ib_device,
493 "No eqe found for irq event");
494 goto unlock_irq_spinlock;
495 } else if (!is_irq)
496 ehca_dbg(&shca->ib_device, "deadman found %x eqe", eqe_cnt);
497 if (unlikely(eqe_cnt == EHCA_EQE_CACHE_SIZE))
498 ehca_dbg(&shca->ib_device, "too many eqes for one irq event");
499 /* enable irq for new packets */
500 for (i = 0; i < eqe_cnt; i++) {
501 if (eq->eqe_cache[i].cq)
502 reset_eq_pending(eq->eqe_cache[i].cq);
503 }
504 /* check eq */
505 spin_lock(&eq->spinlock);
506 eq_empty = (!ipz_eqit_eq_peek_valid(&shca->eq.ipz_queue));
507 spin_unlock(&eq->spinlock);
508 /* call completion handler for cached eqes */
509 for (i = 0; i < eqe_cnt; i++)
510 if (eq->eqe_cache[i].cq) {
511#ifdef CONFIG_INFINIBAND_EHCA_SCALING
512 spin_lock(&ehca_cq_idr_lock);
513 queue_comp_task(eq->eqe_cache[i].cq);
514 spin_unlock(&ehca_cq_idr_lock);
515#else
516 comp_event_callback(eq->eqe_cache[i].cq);
517#endif
518 } else {
519 ehca_dbg(&shca->ib_device, "Got non completion event");
520 parse_identifier(shca, eq->eqe_cache[i].eqe->entry);
521 }
522 /* poll eq if not empty */
523 if (eq_empty)
524 goto unlock_irq_spinlock;
525 do {
526 struct ehca_eqe *eqe;
527 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->eq);
528 if (!eqe)
529 break;
530 process_eqe(shca, eqe);
531 eqe_cnt++;
532 } while (1);
533
534unlock_irq_spinlock:
535 spin_unlock_irqrestore(&eq->irq_spinlock, flags);
536}
537
Heiko J Schickfab97222006-09-22 15:22:22 -0700538void ehca_tasklet_eq(unsigned long data)
539{
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100540 ehca_process_eq((struct ehca_shca*)data, 1);
Heiko J Schickfab97222006-09-22 15:22:22 -0700541}
542
543#ifdef CONFIG_INFINIBAND_EHCA_SCALING
544
545static inline int find_next_online_cpu(struct ehca_comp_pool* pool)
546{
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100547 int cpu;
548 unsigned long flags;
Heiko J Schickfab97222006-09-22 15:22:22 -0700549
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100550 WARN_ON_ONCE(!in_interrupt());
Heiko J Schickfab97222006-09-22 15:22:22 -0700551 if (ehca_debug_level)
552 ehca_dmp(&cpu_online_map, sizeof(cpumask_t), "");
553
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100554 spin_lock_irqsave(&pool->last_cpu_lock, flags);
555 cpu = next_cpu(pool->last_cpu, cpu_online_map);
556 if (cpu == NR_CPUS)
557 cpu = first_cpu(cpu_online_map);
558 pool->last_cpu = cpu;
559 spin_unlock_irqrestore(&pool->last_cpu_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700560
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100561 return cpu;
Heiko J Schickfab97222006-09-22 15:22:22 -0700562}
563
564static void __queue_comp_task(struct ehca_cq *__cq,
565 struct ehca_cpu_comp_task *cct)
566{
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100567 unsigned long flags;
Heiko J Schickfab97222006-09-22 15:22:22 -0700568
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100569 spin_lock_irqsave(&cct->task_lock, flags);
570 spin_lock(&__cq->task_lock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700571
572 if (__cq->nr_callbacks == 0) {
573 __cq->nr_callbacks++;
574 list_add_tail(&__cq->entry, &cct->cq_list);
575 cct->cq_jobs++;
576 wake_up(&cct->wait_queue);
577 }
578 else
579 __cq->nr_callbacks++;
580
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100581 spin_unlock(&__cq->task_lock);
582 spin_unlock_irqrestore(&cct->task_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700583}
584
585static void queue_comp_task(struct ehca_cq *__cq)
586{
587 int cpu;
588 int cpu_id;
589 struct ehca_cpu_comp_task *cct;
590
591 cpu = get_cpu();
592 cpu_id = find_next_online_cpu(pool);
Heiko J Schickfab97222006-09-22 15:22:22 -0700593 BUG_ON(!cpu_online(cpu_id));
594
595 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100596 BUG_ON(!cct);
Heiko J Schickfab97222006-09-22 15:22:22 -0700597
598 if (cct->cq_jobs > 0) {
599 cpu_id = find_next_online_cpu(pool);
600 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100601 BUG_ON(!cct);
Heiko J Schickfab97222006-09-22 15:22:22 -0700602 }
603
604 __queue_comp_task(__cq, cct);
Heiko J Schickfab97222006-09-22 15:22:22 -0700605}
606
607static void run_comp_task(struct ehca_cpu_comp_task* cct)
608{
609 struct ehca_cq *cq;
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100610 unsigned long flags;
Heiko J Schickfab97222006-09-22 15:22:22 -0700611
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100612 spin_lock_irqsave(&cct->task_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700613
614 while (!list_empty(&cct->cq_list)) {
615 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100616 spin_unlock_irqrestore(&cct->task_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700617 comp_event_callback(cq);
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100618 spin_lock_irqsave(&cct->task_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700619
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100620 spin_lock(&cq->task_lock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700621 cq->nr_callbacks--;
622 if (cq->nr_callbacks == 0) {
623 list_del_init(cct->cq_list.next);
624 cct->cq_jobs--;
625 }
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100626 spin_unlock(&cq->task_lock);
Heiko J Schickfab97222006-09-22 15:22:22 -0700627 }
628
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100629 spin_unlock_irqrestore(&cct->task_lock, flags);
Heiko J Schickfab97222006-09-22 15:22:22 -0700630}
631
632static int comp_task(void *__cct)
633{
634 struct ehca_cpu_comp_task* cct = __cct;
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100635 int cql_empty;
Heiko J Schickfab97222006-09-22 15:22:22 -0700636 DECLARE_WAITQUEUE(wait, current);
637
638 set_current_state(TASK_INTERRUPTIBLE);
639 while(!kthread_should_stop()) {
640 add_wait_queue(&cct->wait_queue, &wait);
641
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100642 spin_lock_irq(&cct->task_lock);
643 cql_empty = list_empty(&cct->cq_list);
644 spin_unlock_irq(&cct->task_lock);
645 if (cql_empty)
Heiko J Schickfab97222006-09-22 15:22:22 -0700646 schedule();
647 else
648 __set_current_state(TASK_RUNNING);
649
650 remove_wait_queue(&cct->wait_queue, &wait);
651
Hoang-Nam Nguyen8b16cef2007-02-15 17:07:30 +0100652 spin_lock_irq(&cct->task_lock);
653 cql_empty = list_empty(&cct->cq_list);
654 spin_unlock_irq(&cct->task_lock);
655 if (!cql_empty)
Heiko J Schickfab97222006-09-22 15:22:22 -0700656 run_comp_task(__cct);
657
658 set_current_state(TASK_INTERRUPTIBLE);
659 }
660 __set_current_state(TASK_RUNNING);
661
662 return 0;
663}
664
665static struct task_struct *create_comp_task(struct ehca_comp_pool *pool,
666 int cpu)
667{
668 struct ehca_cpu_comp_task *cct;
669
670 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
671 spin_lock_init(&cct->task_lock);
672 INIT_LIST_HEAD(&cct->cq_list);
673 init_waitqueue_head(&cct->wait_queue);
674 cct->task = kthread_create(comp_task, cct, "ehca_comp/%d", cpu);
675
676 return cct->task;
677}
678
679static void destroy_comp_task(struct ehca_comp_pool *pool,
680 int cpu)
681{
682 struct ehca_cpu_comp_task *cct;
683 struct task_struct *task;
684 unsigned long flags_cct;
685
686 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
687
688 spin_lock_irqsave(&cct->task_lock, flags_cct);
689
690 task = cct->task;
691 cct->task = NULL;
692 cct->cq_jobs = 0;
693
694 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
695
696 if (task)
697 kthread_stop(task);
Heiko J Schickfab97222006-09-22 15:22:22 -0700698}
699
700static void take_over_work(struct ehca_comp_pool *pool,
701 int cpu)
702{
703 struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
704 LIST_HEAD(list);
705 struct ehca_cq *cq;
706 unsigned long flags_cct;
707
708 spin_lock_irqsave(&cct->task_lock, flags_cct);
709
710 list_splice_init(&cct->cq_list, &list);
711
712 while(!list_empty(&list)) {
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100713 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
Heiko J Schickfab97222006-09-22 15:22:22 -0700714
Hoang-Nam Nguyen78d8d5f2007-02-15 17:06:33 +0100715 list_del(&cq->entry);
716 __queue_comp_task(cq, per_cpu_ptr(pool->cpu_comp_tasks,
717 smp_processor_id()));
Heiko J Schickfab97222006-09-22 15:22:22 -0700718 }
719
720 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
721
722}
723
724static int comp_pool_callback(struct notifier_block *nfb,
725 unsigned long action,
726 void *hcpu)
727{
728 unsigned int cpu = (unsigned long)hcpu;
729 struct ehca_cpu_comp_task *cct;
730
731 switch (action) {
732 case CPU_UP_PREPARE:
733 ehca_gen_dbg("CPU: %x (CPU_PREPARE)", cpu);
734 if(!create_comp_task(pool, cpu)) {
735 ehca_gen_err("Can't create comp_task for cpu: %x", cpu);
736 return NOTIFY_BAD;
737 }
738 break;
739 case CPU_UP_CANCELED:
740 ehca_gen_dbg("CPU: %x (CPU_CANCELED)", cpu);
741 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
742 kthread_bind(cct->task, any_online_cpu(cpu_online_map));
743 destroy_comp_task(pool, cpu);
744 break;
745 case CPU_ONLINE:
746 ehca_gen_dbg("CPU: %x (CPU_ONLINE)", cpu);
747 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
748 kthread_bind(cct->task, cpu);
749 wake_up_process(cct->task);
750 break;
751 case CPU_DOWN_PREPARE:
752 ehca_gen_dbg("CPU: %x (CPU_DOWN_PREPARE)", cpu);
753 break;
754 case CPU_DOWN_FAILED:
755 ehca_gen_dbg("CPU: %x (CPU_DOWN_FAILED)", cpu);
756 break;
757 case CPU_DEAD:
758 ehca_gen_dbg("CPU: %x (CPU_DEAD)", cpu);
759 destroy_comp_task(pool, cpu);
760 take_over_work(pool, cpu);
761 break;
762 }
763
764 return NOTIFY_OK;
765}
766
767#endif
768
769int ehca_create_comp_pool(void)
770{
771#ifdef CONFIG_INFINIBAND_EHCA_SCALING
772 int cpu;
773 struct task_struct *task;
774
775 pool = kzalloc(sizeof(struct ehca_comp_pool), GFP_KERNEL);
776 if (pool == NULL)
777 return -ENOMEM;
778
779 spin_lock_init(&pool->last_cpu_lock);
780 pool->last_cpu = any_online_cpu(cpu_online_map);
781
782 pool->cpu_comp_tasks = alloc_percpu(struct ehca_cpu_comp_task);
783 if (pool->cpu_comp_tasks == NULL) {
784 kfree(pool);
785 return -EINVAL;
786 }
787
788 for_each_online_cpu(cpu) {
789 task = create_comp_task(pool, cpu);
790 if (task) {
791 kthread_bind(task, cpu);
792 wake_up_process(task);
793 }
794 }
795
796 comp_pool_callback_nb.notifier_call = comp_pool_callback;
797 comp_pool_callback_nb.priority =0;
798 register_cpu_notifier(&comp_pool_callback_nb);
799#endif
800
801 return 0;
802}
803
804void ehca_destroy_comp_pool(void)
805{
806#ifdef CONFIG_INFINIBAND_EHCA_SCALING
807 int i;
808
809 unregister_cpu_notifier(&comp_pool_callback_nb);
810
811 for (i = 0; i < NR_CPUS; i++) {
812 if (cpu_online(i))
813 destroy_comp_task(pool, i);
814 }
Akinobu Mita65e5c022007-02-05 16:21:09 -0800815 free_percpu(pool->cpu_comp_tasks);
816 kfree(pool);
Heiko J Schickfab97222006-09-22 15:22:22 -0700817#endif
Heiko J Schickfab97222006-09-22 15:22:22 -0700818}