RDMA/cxgb3: Handle build_phys_page_list() failure in iwch_reregister_phys_mem()
[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>
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"
7e28db5d 48#include "ipz_pt_fn.h"
fab97220
HS
49
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
fab97220
HS
66static void queue_comp_task(struct ehca_cq *__cq);
67
68static struct ehca_comp_pool* pool;
69static struct notifier_block comp_pool_callback_nb;
70
fab97220
HS
71static inline void comp_event_callback(struct ehca_cq *cq)
72{
73 if (!cq->ib_cq.comp_handler)
74 return;
75
76 spin_lock(&cq->cb_lock);
77 cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context);
78 spin_unlock(&cq->cb_lock);
79
80 return;
81}
82
83static void print_error_data(struct ehca_shca * shca, void* data,
84 u64* rblock, int length)
85{
86 u64 type = EHCA_BMASK_GET(ERROR_DATA_TYPE, rblock[2]);
87 u64 resource = rblock[1];
88
89 switch (type) {
90 case 0x1: /* Queue Pair */
91 {
92 struct ehca_qp *qp = (struct ehca_qp*)data;
93
94 /* only print error data if AER is set */
95 if (rblock[6] == 0)
96 return;
97
98 ehca_err(&shca->ib_device,
99 "QP 0x%x (resource=%lx) has errors.",
100 qp->ib_qp.qp_num, resource);
101 break;
102 }
103 case 0x4: /* Completion Queue */
104 {
105 struct ehca_cq *cq = (struct ehca_cq*)data;
106
107 ehca_err(&shca->ib_device,
108 "CQ 0x%x (resource=%lx) has errors.",
109 cq->cq_number, resource);
110 break;
111 }
112 default:
113 ehca_err(&shca->ib_device,
114 "Unknown errror type: %lx on %s.",
115 type, shca->ib_device.name);
116 break;
117 }
118
119 ehca_err(&shca->ib_device, "Error data is available: %lx.", resource);
120 ehca_err(&shca->ib_device, "EHCA ----- error data begin "
121 "---------------------------------------------------");
122 ehca_dmp(rblock, length, "resource=%lx", resource);
123 ehca_err(&shca->ib_device, "EHCA ----- error data end "
124 "----------------------------------------------------");
125
126 return;
127}
128
129int ehca_error_data(struct ehca_shca *shca, void *data,
130 u64 resource)
131{
132
133 unsigned long ret;
134 u64 *rblock;
135 unsigned long block_count;
136
f2d91361 137 rblock = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);
fab97220
HS
138 if (!rblock) {
139 ehca_err(&shca->ib_device, "Cannot allocate rblock memory.");
140 ret = -ENOMEM;
141 goto error_data1;
142 }
143
7e28db5d 144 /* rblock must be 4K aligned and should be 4K large */
fab97220
HS
145 ret = hipz_h_error_data(shca->ipz_hca_handle,
146 resource,
147 rblock,
148 &block_count);
149
7e28db5d 150 if (ret == H_R_STATE)
fab97220
HS
151 ehca_err(&shca->ib_device,
152 "No error data is available: %lx.", resource);
fab97220
HS
153 else if (ret == H_SUCCESS) {
154 int length;
155
156 length = EHCA_BMASK_GET(ERROR_DATA_LENGTH, rblock[0]);
157
7e28db5d
HNN
158 if (length > EHCA_PAGESIZE)
159 length = EHCA_PAGESIZE;
fab97220
HS
160
161 print_error_data(shca, data, rblock, length);
7e28db5d 162 } else
fab97220
HS
163 ehca_err(&shca->ib_device,
164 "Error data could not be fetched: %lx", resource);
fab97220 165
7e28db5d 166 ehca_free_fw_ctrlblock(rblock);
fab97220
HS
167
168error_data1:
169 return ret;
170
171}
172
173static void qp_event_callback(struct ehca_shca *shca,
174 u64 eqe,
175 enum ib_event_type event_type)
176{
177 struct ib_event event;
178 struct ehca_qp *qp;
179 unsigned long flags;
180 u32 token = EHCA_BMASK_GET(EQE_QP_TOKEN, eqe);
181
182 spin_lock_irqsave(&ehca_qp_idr_lock, flags);
183 qp = idr_find(&ehca_qp_idr, token);
184 spin_unlock_irqrestore(&ehca_qp_idr_lock, flags);
185
186
187 if (!qp)
188 return;
189
190 ehca_error_data(shca, qp, qp->ipz_qp_handle.handle);
191
192 if (!qp->ib_qp.event_handler)
193 return;
194
195 event.device = &shca->ib_device;
196 event.event = event_type;
197 event.element.qp = &qp->ib_qp;
198
199 qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
200
201 return;
202}
203
204static void cq_event_callback(struct ehca_shca *shca,
78d8d5f9 205 u64 eqe)
fab97220
HS
206{
207 struct ehca_cq *cq;
208 unsigned long flags;
209 u32 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe);
210
211 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
212 cq = idr_find(&ehca_cq_idr, token);
213 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
214
215 if (!cq)
216 return;
217
218 ehca_error_data(shca, cq, cq->ipz_cq_handle.handle);
219
220 return;
221}
222
223static void parse_identifier(struct ehca_shca *shca, u64 eqe)
224{
225 u8 identifier = EHCA_BMASK_GET(EQE_EE_IDENTIFIER, eqe);
226
227 switch (identifier) {
228 case 0x02: /* path migrated */
229 qp_event_callback(shca, eqe, IB_EVENT_PATH_MIG);
230 break;
231 case 0x03: /* communication established */
232 qp_event_callback(shca, eqe, IB_EVENT_COMM_EST);
233 break;
234 case 0x04: /* send queue drained */
235 qp_event_callback(shca, eqe, IB_EVENT_SQ_DRAINED);
236 break;
237 case 0x05: /* QP error */
238 case 0x06: /* QP error */
239 qp_event_callback(shca, eqe, IB_EVENT_QP_FATAL);
240 break;
241 case 0x07: /* CQ error */
242 case 0x08: /* CQ error */
243 cq_event_callback(shca, eqe);
244 break;
245 case 0x09: /* MRMWPTE error */
246 ehca_err(&shca->ib_device, "MRMWPTE error.");
247 break;
248 case 0x0A: /* port event */
249 ehca_err(&shca->ib_device, "Port event.");
250 break;
251 case 0x0B: /* MR access error */
252 ehca_err(&shca->ib_device, "MR access error.");
253 break;
254 case 0x0C: /* EQ error */
255 ehca_err(&shca->ib_device, "EQ error.");
256 break;
257 case 0x0D: /* P/Q_Key mismatch */
258 ehca_err(&shca->ib_device, "P/Q_Key mismatch.");
259 break;
260 case 0x10: /* sampling complete */
261 ehca_err(&shca->ib_device, "Sampling complete.");
262 break;
263 case 0x11: /* unaffiliated access error */
264 ehca_err(&shca->ib_device, "Unaffiliated access error.");
265 break;
266 case 0x12: /* path migrating error */
267 ehca_err(&shca->ib_device, "Path migration error.");
268 break;
269 case 0x13: /* interface trace stopped */
270 ehca_err(&shca->ib_device, "Interface trace stopped.");
271 break;
272 case 0x14: /* first error capture info available */
273 default:
274 ehca_err(&shca->ib_device, "Unknown identifier: %x on %s.",
275 identifier, shca->ib_device.name);
276 break;
277 }
278
279 return;
280}
281
282static void parse_ec(struct ehca_shca *shca, u64 eqe)
283{
284 struct ib_event event;
285 u8 ec = EHCA_BMASK_GET(NEQE_EVENT_CODE, eqe);
286 u8 port = EHCA_BMASK_GET(NEQE_PORT_NUMBER, eqe);
287
288 switch (ec) {
289 case 0x30: /* port availability change */
290 if (EHCA_BMASK_GET(NEQE_PORT_AVAILABILITY, eqe)) {
291 ehca_info(&shca->ib_device,
292 "port %x is active.", port);
293 event.device = &shca->ib_device;
294 event.event = IB_EVENT_PORT_ACTIVE;
295 event.element.port_num = port;
296 shca->sport[port - 1].port_state = IB_PORT_ACTIVE;
297 ib_dispatch_event(&event);
298 } else {
299 ehca_info(&shca->ib_device,
300 "port %x is inactive.", port);
301 event.device = &shca->ib_device;
302 event.event = IB_EVENT_PORT_ERR;
303 event.element.port_num = port;
304 shca->sport[port - 1].port_state = IB_PORT_DOWN;
305 ib_dispatch_event(&event);
306 }
307 break;
308 case 0x31:
309 /* port configuration change
310 * disruptive change is caused by
311 * LID, PKEY or SM change
312 */
313 ehca_warn(&shca->ib_device,
314 "disruptive port %x configuration change", port);
315
316 ehca_info(&shca->ib_device,
78d8d5f9 317 "port %x is inactive.", port);
fab97220
HS
318 event.device = &shca->ib_device;
319 event.event = IB_EVENT_PORT_ERR;
320 event.element.port_num = port;
321 shca->sport[port - 1].port_state = IB_PORT_DOWN;
322 ib_dispatch_event(&event);
323
324 ehca_info(&shca->ib_device,
78d8d5f9 325 "port %x is active.", port);
fab97220
HS
326 event.device = &shca->ib_device;
327 event.event = IB_EVENT_PORT_ACTIVE;
328 event.element.port_num = port;
329 shca->sport[port - 1].port_state = IB_PORT_ACTIVE;
330 ib_dispatch_event(&event);
331 break;
332 case 0x32: /* adapter malfunction */
333 ehca_err(&shca->ib_device, "Adapter malfunction.");
334 break;
335 case 0x33: /* trace stopped */
336 ehca_err(&shca->ib_device, "Traced stopped.");
337 break;
338 default:
339 ehca_err(&shca->ib_device, "Unknown event code: %x on %s.",
340 ec, shca->ib_device.name);
341 break;
342 }
343
344 return;
345}
346
347static inline void reset_eq_pending(struct ehca_cq *cq)
348{
349 u64 CQx_EP;
350 struct h_galpa gal = cq->galpas.kernel;
351
352 hipz_galpa_store_cq(gal, cqx_ep, 0x0);
353 CQx_EP = hipz_galpa_load(gal, CQTEMM_OFFSET(cqx_ep));
354
355 return;
356}
357
7d12e780 358irqreturn_t ehca_interrupt_neq(int irq, void *dev_id)
fab97220
HS
359{
360 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
361
362 tasklet_hi_schedule(&shca->neq.interrupt_task);
363
364 return IRQ_HANDLED;
365}
366
367void ehca_tasklet_neq(unsigned long data)
368{
369 struct ehca_shca *shca = (struct ehca_shca*)data;
370 struct ehca_eqe *eqe;
371 u64 ret;
372
373 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
374
375 while (eqe) {
376 if (!EHCA_BMASK_GET(NEQE_COMPLETION_EVENT, eqe->entry))
377 parse_ec(shca, eqe->entry);
378
379 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->neq);
380 }
381
382 ret = hipz_h_reset_event(shca->ipz_hca_handle,
383 shca->neq.ipz_eq_handle, 0xFFFFFFFFFFFFFFFFL);
384
385 if (ret != H_SUCCESS)
386 ehca_err(&shca->ib_device, "Can't clear notification events.");
387
388 return;
389}
390
7d12e780 391irqreturn_t ehca_interrupt_eq(int irq, void *dev_id)
fab97220
HS
392{
393 struct ehca_shca *shca = (struct ehca_shca*)dev_id;
394
395 tasklet_hi_schedule(&shca->eq.interrupt_task);
396
397 return IRQ_HANDLED;
398}
399
fab97220 400
78d8d5f9
HNN
401static inline void process_eqe(struct ehca_shca *shca, struct ehca_eqe *eqe)
402{
403 u64 eqe_value;
404 u32 token;
405 unsigned long flags;
406 struct ehca_cq *cq;
31726798 407
78d8d5f9
HNN
408 eqe_value = eqe->entry;
409 ehca_dbg(&shca->ib_device, "eqe_value=%lx", eqe_value);
410 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
31726798 411 ehca_dbg(&shca->ib_device, "Got completion event");
78d8d5f9
HNN
412 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
413 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
414 cq = idr_find(&ehca_cq_idr, token);
415 if (cq == NULL) {
416 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
417 ehca_err(&shca->ib_device,
418 "Invalid eqe for non-existing cq token=%x",
419 token);
420 return;
421 }
422 reset_eq_pending(cq);
31726798
HNN
423 cq->nr_events++;
424 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
425 if (ehca_scaling_code)
4fd30060 426 queue_comp_task(cq);
31726798 427 else {
4fd30060 428 comp_event_callback(cq);
31726798
HNN
429 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
430 cq->nr_events--;
431 if (!cq->nr_events)
432 wake_up(&cq->wait_completion);
433 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
4fd30060 434 }
78d8d5f9 435 } else {
31726798 436 ehca_dbg(&shca->ib_device, "Got non completion event");
78d8d5f9
HNN
437 parse_identifier(shca, eqe_value);
438 }
439}
fab97220 440
78d8d5f9
HNN
441void ehca_process_eq(struct ehca_shca *shca, int is_irq)
442{
443 struct ehca_eq *eq = &shca->eq;
444 struct ehca_eqe_cache_entry *eqe_cache = eq->eqe_cache;
445 u64 eqe_value;
446 unsigned long flags;
447 int eqe_cnt, i;
448 int eq_empty = 0;
449
450 spin_lock_irqsave(&eq->irq_spinlock, flags);
451 if (is_irq) {
452 const int max_query_cnt = 100;
453 int query_cnt = 0;
454 int int_state = 1;
455 do {
456 int_state = hipz_h_query_int_state(
457 shca->ipz_hca_handle, eq->ist);
458 query_cnt++;
459 iosync();
460 } while (int_state && query_cnt < max_query_cnt);
461 if (unlikely((query_cnt == max_query_cnt)))
462 ehca_dbg(&shca->ib_device, "int_state=%x query_cnt=%x",
463 int_state, query_cnt);
464 }
fab97220 465
78d8d5f9
HNN
466 /* read out all eqes */
467 eqe_cnt = 0;
468 do {
469 u32 token;
470 eqe_cache[eqe_cnt].eqe =
471 (struct ehca_eqe *)ehca_poll_eq(shca, eq);
472 if (!eqe_cache[eqe_cnt].eqe)
473 break;
474 eqe_value = eqe_cache[eqe_cnt].eqe->entry;
475 if (EHCA_BMASK_GET(EQE_COMPLETION_EVENT, eqe_value)) {
476 token = EHCA_BMASK_GET(EQE_CQ_TOKEN, eqe_value);
477 spin_lock(&ehca_cq_idr_lock);
478 eqe_cache[eqe_cnt].cq = idr_find(&ehca_cq_idr, token);
479 if (!eqe_cache[eqe_cnt].cq) {
480 spin_unlock(&ehca_cq_idr_lock);
481 ehca_err(&shca->ib_device,
482 "Invalid eqe for non-existing cq "
483 "token=%x", token);
484 continue;
485 }
31726798 486 eqe_cache[eqe_cnt].cq->nr_events++;
78d8d5f9
HNN
487 spin_unlock(&ehca_cq_idr_lock);
488 } else
489 eqe_cache[eqe_cnt].cq = NULL;
490 eqe_cnt++;
491 } while (eqe_cnt < EHCA_EQE_CACHE_SIZE);
492 if (!eqe_cnt) {
493 if (is_irq)
494 ehca_dbg(&shca->ib_device,
495 "No eqe found for irq event");
496 goto unlock_irq_spinlock;
497 } else if (!is_irq)
498 ehca_dbg(&shca->ib_device, "deadman found %x eqe", eqe_cnt);
499 if (unlikely(eqe_cnt == EHCA_EQE_CACHE_SIZE))
500 ehca_dbg(&shca->ib_device, "too many eqes for one irq event");
501 /* enable irq for new packets */
502 for (i = 0; i < eqe_cnt; i++) {
503 if (eq->eqe_cache[i].cq)
504 reset_eq_pending(eq->eqe_cache[i].cq);
505 }
506 /* check eq */
507 spin_lock(&eq->spinlock);
508 eq_empty = (!ipz_eqit_eq_peek_valid(&shca->eq.ipz_queue));
509 spin_unlock(&eq->spinlock);
510 /* call completion handler for cached eqes */
511 for (i = 0; i < eqe_cnt; i++)
512 if (eq->eqe_cache[i].cq) {
31726798 513 if (ehca_scaling_code)
4fd30060 514 queue_comp_task(eq->eqe_cache[i].cq);
31726798
HNN
515 else {
516 struct ehca_cq *cq = eq->eqe_cache[i].cq;
517 comp_event_callback(cq);
518 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
519 cq->nr_events--;
520 if (!cq->nr_events)
521 wake_up(&cq->wait_completion);
522 spin_unlock_irqrestore(&ehca_cq_idr_lock,
523 flags);
524 }
78d8d5f9
HNN
525 } else {
526 ehca_dbg(&shca->ib_device, "Got non completion event");
527 parse_identifier(shca, eq->eqe_cache[i].eqe->entry);
fab97220 528 }
78d8d5f9
HNN
529 /* poll eq if not empty */
530 if (eq_empty)
531 goto unlock_irq_spinlock;
532 do {
533 struct ehca_eqe *eqe;
534 eqe = (struct ehca_eqe *)ehca_poll_eq(shca, &shca->eq);
535 if (!eqe)
536 break;
537 process_eqe(shca, eqe);
78d8d5f9
HNN
538 } while (1);
539
540unlock_irq_spinlock:
541 spin_unlock_irqrestore(&eq->irq_spinlock, flags);
542}
fab97220 543
78d8d5f9
HNN
544void ehca_tasklet_eq(unsigned long data)
545{
546 ehca_process_eq((struct ehca_shca*)data, 1);
fab97220
HS
547}
548
fab97220
HS
549static inline int find_next_online_cpu(struct ehca_comp_pool* pool)
550{
8b16cef3
HNN
551 int cpu;
552 unsigned long flags;
fab97220 553
8b16cef3 554 WARN_ON_ONCE(!in_interrupt());
fab97220
HS
555 if (ehca_debug_level)
556 ehca_dmp(&cpu_online_map, sizeof(cpumask_t), "");
557
8b16cef3
HNN
558 spin_lock_irqsave(&pool->last_cpu_lock, flags);
559 cpu = next_cpu(pool->last_cpu, cpu_online_map);
560 if (cpu == NR_CPUS)
561 cpu = first_cpu(cpu_online_map);
562 pool->last_cpu = cpu;
563 spin_unlock_irqrestore(&pool->last_cpu_lock, flags);
fab97220 564
8b16cef3 565 return cpu;
fab97220
HS
566}
567
568static void __queue_comp_task(struct ehca_cq *__cq,
569 struct ehca_cpu_comp_task *cct)
570{
8b16cef3 571 unsigned long flags;
fab97220 572
8b16cef3
HNN
573 spin_lock_irqsave(&cct->task_lock, flags);
574 spin_lock(&__cq->task_lock);
fab97220
HS
575
576 if (__cq->nr_callbacks == 0) {
577 __cq->nr_callbacks++;
578 list_add_tail(&__cq->entry, &cct->cq_list);
579 cct->cq_jobs++;
580 wake_up(&cct->wait_queue);
31726798 581 } else
fab97220
HS
582 __cq->nr_callbacks++;
583
8b16cef3
HNN
584 spin_unlock(&__cq->task_lock);
585 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220
HS
586}
587
588static void queue_comp_task(struct ehca_cq *__cq)
589{
fab97220
HS
590 int cpu_id;
591 struct ehca_cpu_comp_task *cct;
31726798
HNN
592 int cq_jobs;
593 unsigned long flags;
fab97220 594
fab97220 595 cpu_id = find_next_online_cpu(pool);
fab97220
HS
596 BUG_ON(!cpu_online(cpu_id));
597
598 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
8b16cef3 599 BUG_ON(!cct);
fab97220 600
31726798
HNN
601 spin_lock_irqsave(&cct->task_lock, flags);
602 cq_jobs = cct->cq_jobs;
603 spin_unlock_irqrestore(&cct->task_lock, flags);
604 if (cq_jobs > 0) {
fab97220
HS
605 cpu_id = find_next_online_cpu(pool);
606 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu_id);
8b16cef3 607 BUG_ON(!cct);
fab97220
HS
608 }
609
610 __queue_comp_task(__cq, cct);
fab97220
HS
611}
612
613static void run_comp_task(struct ehca_cpu_comp_task* cct)
614{
615 struct ehca_cq *cq;
8b16cef3 616 unsigned long flags;
fab97220 617
8b16cef3 618 spin_lock_irqsave(&cct->task_lock, flags);
fab97220
HS
619
620 while (!list_empty(&cct->cq_list)) {
621 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
8b16cef3 622 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220 623 comp_event_callback(cq);
fab97220 624
31726798
HNN
625 spin_lock_irqsave(&ehca_cq_idr_lock, flags);
626 cq->nr_events--;
627 if (!cq->nr_events)
628 wake_up(&cq->wait_completion);
629 spin_unlock_irqrestore(&ehca_cq_idr_lock, flags);
630
631 spin_lock_irqsave(&cct->task_lock, flags);
8b16cef3 632 spin_lock(&cq->task_lock);
fab97220 633 cq->nr_callbacks--;
31726798 634 if (!cq->nr_callbacks) {
fab97220
HS
635 list_del_init(cct->cq_list.next);
636 cct->cq_jobs--;
637 }
8b16cef3 638 spin_unlock(&cq->task_lock);
fab97220
HS
639 }
640
8b16cef3 641 spin_unlock_irqrestore(&cct->task_lock, flags);
fab97220
HS
642}
643
644static int comp_task(void *__cct)
645{
646 struct ehca_cpu_comp_task* cct = __cct;
8b16cef3 647 int cql_empty;
fab97220
HS
648 DECLARE_WAITQUEUE(wait, current);
649
650 set_current_state(TASK_INTERRUPTIBLE);
651 while(!kthread_should_stop()) {
652 add_wait_queue(&cct->wait_queue, &wait);
653
8b16cef3
HNN
654 spin_lock_irq(&cct->task_lock);
655 cql_empty = list_empty(&cct->cq_list);
656 spin_unlock_irq(&cct->task_lock);
657 if (cql_empty)
fab97220
HS
658 schedule();
659 else
660 __set_current_state(TASK_RUNNING);
661
662 remove_wait_queue(&cct->wait_queue, &wait);
663
8b16cef3
HNN
664 spin_lock_irq(&cct->task_lock);
665 cql_empty = list_empty(&cct->cq_list);
666 spin_unlock_irq(&cct->task_lock);
667 if (!cql_empty)
fab97220
HS
668 run_comp_task(__cct);
669
670 set_current_state(TASK_INTERRUPTIBLE);
671 }
672 __set_current_state(TASK_RUNNING);
673
674 return 0;
675}
676
677static struct task_struct *create_comp_task(struct ehca_comp_pool *pool,
678 int cpu)
679{
680 struct ehca_cpu_comp_task *cct;
681
682 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
683 spin_lock_init(&cct->task_lock);
684 INIT_LIST_HEAD(&cct->cq_list);
685 init_waitqueue_head(&cct->wait_queue);
686 cct->task = kthread_create(comp_task, cct, "ehca_comp/%d", cpu);
687
688 return cct->task;
689}
690
691static void destroy_comp_task(struct ehca_comp_pool *pool,
692 int cpu)
693{
694 struct ehca_cpu_comp_task *cct;
695 struct task_struct *task;
696 unsigned long flags_cct;
697
698 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
699
700 spin_lock_irqsave(&cct->task_lock, flags_cct);
701
702 task = cct->task;
703 cct->task = NULL;
704 cct->cq_jobs = 0;
705
706 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
707
708 if (task)
709 kthread_stop(task);
fab97220
HS
710}
711
712static void take_over_work(struct ehca_comp_pool *pool,
713 int cpu)
714{
715 struct ehca_cpu_comp_task *cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
716 LIST_HEAD(list);
717 struct ehca_cq *cq;
718 unsigned long flags_cct;
719
720 spin_lock_irqsave(&cct->task_lock, flags_cct);
721
722 list_splice_init(&cct->cq_list, &list);
723
724 while(!list_empty(&list)) {
78d8d5f9 725 cq = list_entry(cct->cq_list.next, struct ehca_cq, entry);
fab97220 726
78d8d5f9
HNN
727 list_del(&cq->entry);
728 __queue_comp_task(cq, per_cpu_ptr(pool->cpu_comp_tasks,
729 smp_processor_id()));
fab97220
HS
730 }
731
732 spin_unlock_irqrestore(&cct->task_lock, flags_cct);
733
734}
735
736static int comp_pool_callback(struct notifier_block *nfb,
737 unsigned long action,
738 void *hcpu)
739{
740 unsigned int cpu = (unsigned long)hcpu;
741 struct ehca_cpu_comp_task *cct;
742
743 switch (action) {
744 case CPU_UP_PREPARE:
745 ehca_gen_dbg("CPU: %x (CPU_PREPARE)", cpu);
746 if(!create_comp_task(pool, cpu)) {
747 ehca_gen_err("Can't create comp_task for cpu: %x", cpu);
748 return NOTIFY_BAD;
749 }
750 break;
751 case CPU_UP_CANCELED:
752 ehca_gen_dbg("CPU: %x (CPU_CANCELED)", cpu);
753 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
754 kthread_bind(cct->task, any_online_cpu(cpu_online_map));
755 destroy_comp_task(pool, cpu);
756 break;
757 case CPU_ONLINE:
758 ehca_gen_dbg("CPU: %x (CPU_ONLINE)", cpu);
759 cct = per_cpu_ptr(pool->cpu_comp_tasks, cpu);
760 kthread_bind(cct->task, cpu);
761 wake_up_process(cct->task);
762 break;
763 case CPU_DOWN_PREPARE:
764 ehca_gen_dbg("CPU: %x (CPU_DOWN_PREPARE)", cpu);
765 break;
766 case CPU_DOWN_FAILED:
767 ehca_gen_dbg("CPU: %x (CPU_DOWN_FAILED)", cpu);
768 break;
769 case CPU_DEAD:
770 ehca_gen_dbg("CPU: %x (CPU_DEAD)", cpu);
771 destroy_comp_task(pool, cpu);
772 take_over_work(pool, cpu);
773 break;
774 }
775
776 return NOTIFY_OK;
777}
778
fab97220
HS
779int ehca_create_comp_pool(void)
780{
fab97220
HS
781 int cpu;
782 struct task_struct *task;
783
4fd30060
HNN
784 if (!ehca_scaling_code)
785 return 0;
786
fab97220
HS
787 pool = kzalloc(sizeof(struct ehca_comp_pool), GFP_KERNEL);
788 if (pool == NULL)
789 return -ENOMEM;
790
791 spin_lock_init(&pool->last_cpu_lock);
792 pool->last_cpu = any_online_cpu(cpu_online_map);
793
794 pool->cpu_comp_tasks = alloc_percpu(struct ehca_cpu_comp_task);
795 if (pool->cpu_comp_tasks == NULL) {
796 kfree(pool);
797 return -EINVAL;
798 }
799
800 for_each_online_cpu(cpu) {
801 task = create_comp_task(pool, cpu);
802 if (task) {
803 kthread_bind(task, cpu);
804 wake_up_process(task);
805 }
806 }
807
808 comp_pool_callback_nb.notifier_call = comp_pool_callback;
809 comp_pool_callback_nb.priority =0;
810 register_cpu_notifier(&comp_pool_callback_nb);
4fd30060
HNN
811
812 printk(KERN_INFO "eHCA scaling code enabled\n");
fab97220
HS
813
814 return 0;
815}
816
817void ehca_destroy_comp_pool(void)
818{
fab97220
HS
819 int i;
820
4fd30060
HNN
821 if (!ehca_scaling_code)
822 return;
823
fab97220
HS
824 unregister_cpu_notifier(&comp_pool_callback_nb);
825
826 for (i = 0; i < NR_CPUS; i++) {
827 if (cpu_online(i))
828 destroy_comp_task(pool, i);
829 }
65e5c026
AM
830 free_percpu(pool->cpu_comp_tasks);
831 kfree(pool);
fab97220 832}