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