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