Merge branch 'for-linus' of git://one.firstfloor.org/home/andi/git/linux-2.6
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_eq.c
1 /*
2  *  IBM eServer eHCA Infiniband device driver for Linux on POWER
3  *
4  *  Event queue handling
5  *
6  *  Authors: Waleri Fomin <fomin@de.ibm.com>
7  *           Khadija Souissi <souissi@de.ibm.com>
8  *           Reinhard Ernst <rernst@de.ibm.com>
9  *           Heiko J Schick <schickhj@de.ibm.com>
10  *           Hoang-Nam Nguyen <hnguyen@de.ibm.com>
11  *
12  *
13  *  Copyright (c) 2005 IBM Corporation
14  *
15  *  All rights reserved.
16  *
17  *  This source code is distributed under a dual license of GPL v2.0 and OpenIB
18  *  BSD.
19  *
20  * OpenIB BSD License
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions are met:
24  *
25  * Redistributions of source code must retain the above copyright notice, this
26  * list of conditions and the following disclaimer.
27  *
28  * Redistributions in binary form must reproduce the above copyright notice,
29  * this list of conditions and the following disclaimer in the documentation
30  * and/or other materials
31  * provided with the distribution.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
37  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
40  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
41  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  * POSSIBILITY OF SUCH DAMAGE.
44  */
45
46 #include "ehca_classes.h"
47 #include "ehca_irq.h"
48 #include "ehca_iverbs.h"
49 #include "ehca_qes.h"
50 #include "hcp_if.h"
51 #include "ipz_pt_fn.h"
52
53 int ehca_create_eq(struct ehca_shca *shca,
54                    struct ehca_eq *eq,
55                    const enum ehca_eq_type type, const u32 length)
56 {
57         u64 ret;
58         u32 nr_pages;
59         u32 i;
60         void *vpage;
61         struct ib_device *ib_dev = &shca->ib_device;
62
63         spin_lock_init(&eq->spinlock);
64         eq->is_initialized = 0;
65
66         if (type != EHCA_EQ && type != EHCA_NEQ) {
67                 ehca_err(ib_dev, "Invalid EQ type %x. eq=%p", type, eq);
68                 return -EINVAL;
69         }
70         if (!length) {
71                 ehca_err(ib_dev, "EQ length must not be zero. eq=%p", eq);
72                 return -EINVAL;
73         }
74
75         ret = hipz_h_alloc_resource_eq(shca->ipz_hca_handle,
76                                        &eq->pf,
77                                        type,
78                                        length,
79                                        &eq->ipz_eq_handle,
80                                        &eq->length,
81                                        &nr_pages, &eq->ist);
82
83         if (ret != H_SUCCESS) {
84                 ehca_err(ib_dev, "Can't allocate EQ/NEQ. eq=%p", eq);
85                 return -EINVAL;
86         }
87
88         ret = ipz_queue_ctor(&eq->ipz_queue, nr_pages,
89                              EHCA_PAGESIZE, sizeof(struct ehca_eqe), 0);
90         if (!ret) {
91                 ehca_err(ib_dev, "Can't allocate EQ pages eq=%p", eq);
92                 goto create_eq_exit1;
93         }
94
95         for (i = 0; i < nr_pages; i++) {
96                 u64 rpage;
97
98                 if (!(vpage = ipz_qpageit_get_inc(&eq->ipz_queue))) {
99                         ret = H_RESOURCE;
100                         goto create_eq_exit2;
101                 }
102
103                 rpage = virt_to_abs(vpage);
104                 ret = hipz_h_register_rpage_eq(shca->ipz_hca_handle,
105                                                eq->ipz_eq_handle,
106                                                &eq->pf,
107                                                0, 0, rpage, 1);
108
109                 if (i == (nr_pages - 1)) {
110                         /* last page */
111                         vpage = ipz_qpageit_get_inc(&eq->ipz_queue);
112                         if (ret != H_SUCCESS || vpage)
113                                 goto create_eq_exit2;
114                 } else {
115                         if (ret != H_PAGE_REGISTERED || !vpage)
116                                 goto create_eq_exit2;
117                 }
118         }
119
120         ipz_qeit_reset(&eq->ipz_queue);
121
122         /* register interrupt handlers and initialize work queues */
123         if (type == EHCA_EQ) {
124                 ret = ibmebus_request_irq(NULL, eq->ist, ehca_interrupt_eq,
125                                           IRQF_DISABLED, "ehca_eq",
126                                           (void *)shca);
127                 if (ret < 0)
128                         ehca_err(ib_dev, "Can't map interrupt handler.");
129
130                 tasklet_init(&eq->interrupt_task, ehca_tasklet_eq, (long)shca);
131         } else if (type == EHCA_NEQ) {
132                 ret = ibmebus_request_irq(NULL, eq->ist, ehca_interrupt_neq,
133                                           IRQF_DISABLED, "ehca_neq",
134                                           (void *)shca);
135                 if (ret < 0)
136                         ehca_err(ib_dev, "Can't map interrupt handler.");
137
138                 tasklet_init(&eq->interrupt_task, ehca_tasklet_neq, (long)shca);
139         }
140
141         eq->is_initialized = 1;
142
143         return 0;
144
145 create_eq_exit2:
146         ipz_queue_dtor(&eq->ipz_queue);
147
148 create_eq_exit1:
149         hipz_h_destroy_eq(shca->ipz_hca_handle, eq);
150
151         return -EINVAL;
152 }
153
154 void *ehca_poll_eq(struct ehca_shca *shca, struct ehca_eq *eq)
155 {
156         unsigned long flags;
157         void *eqe;
158
159         spin_lock_irqsave(&eq->spinlock, flags);
160         eqe = ipz_eqit_eq_get_inc_valid(&eq->ipz_queue);
161         spin_unlock_irqrestore(&eq->spinlock, flags);
162
163         return eqe;
164 }
165
166 int ehca_destroy_eq(struct ehca_shca *shca, struct ehca_eq *eq)
167 {
168         unsigned long flags;
169         u64 h_ret;
170
171         spin_lock_irqsave(&eq->spinlock, flags);
172         ibmebus_free_irq(NULL, eq->ist, (void *)shca);
173
174         h_ret = hipz_h_destroy_eq(shca->ipz_hca_handle, eq);
175
176         spin_unlock_irqrestore(&eq->spinlock, flags);
177
178         if (h_ret != H_SUCCESS) {
179                 ehca_err(&shca->ib_device, "Can't free EQ resources.");
180                 return -EINVAL;
181         }
182         ipz_queue_dtor(&eq->ipz_queue);
183
184         return 0;
185 }