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