Pull thinkpad-2.6.24 into release branch
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_av.c
1 /*
2  *  IBM eServer eHCA Infiniband device driver for Linux on POWER
3  *
4  *  adress vector functions
5  *
6  *  Authors: Hoang-Nam Nguyen <hnguyen@de.ibm.com>
7  *           Khadija Souissi <souissik@de.ibm.com>
8  *           Reinhard Ernst <rernst@de.ibm.com>
9  *           Christoph Raisch <raisch@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
45 #include <asm/current.h>
46
47 #include "ehca_tools.h"
48 #include "ehca_iverbs.h"
49 #include "hcp_if.h"
50
51 static struct kmem_cache *av_cache;
52
53 int ehca_calc_ipd(struct ehca_shca *shca, int port,
54                   enum ib_rate path_rate, u32 *ipd)
55 {
56         int path = ib_rate_to_mult(path_rate);
57         int link, ret;
58         struct ib_port_attr pa;
59
60         if (path_rate == IB_RATE_PORT_CURRENT) {
61                 *ipd = 0;
62                 return 0;
63         }
64
65         if (unlikely(path < 0)) {
66                 ehca_err(&shca->ib_device, "Invalid static rate! path_rate=%x",
67                          path_rate);
68                 return -EINVAL;
69         }
70
71         ret = ehca_query_port(&shca->ib_device, port, &pa);
72         if (unlikely(ret < 0)) {
73                 ehca_err(&shca->ib_device, "Failed to query port  ret=%i", ret);
74                 return ret;
75         }
76
77         link = ib_width_enum_to_int(pa.active_width) * pa.active_speed;
78
79         /* IPD = round((link / path) - 1) */
80         *ipd = ((link + (path >> 1)) / path) - 1;
81
82         return 0;
83 }
84
85 struct ib_ah *ehca_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
86 {
87         int ret;
88         struct ehca_av *av;
89         struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,
90                                               ib_device);
91
92         av = kmem_cache_alloc(av_cache, GFP_KERNEL);
93         if (!av) {
94                 ehca_err(pd->device, "Out of memory pd=%p ah_attr=%p",
95                          pd, ah_attr);
96                 return ERR_PTR(-ENOMEM);
97         }
98
99         av->av.sl = ah_attr->sl;
100         av->av.dlid = ah_attr->dlid;
101         av->av.slid_path_bits = ah_attr->src_path_bits;
102
103         if (ehca_static_rate < 0) {
104                 u32 ipd;
105                 if (ehca_calc_ipd(shca, ah_attr->port_num,
106                                   ah_attr->static_rate, &ipd)) {
107                         ret = -EINVAL;
108                         goto create_ah_exit1;
109                 }
110                 av->av.ipd = ipd;
111         } else
112                 av->av.ipd = ehca_static_rate;
113
114         av->av.lnh = ah_attr->ah_flags;
115         av->av.grh.word_0 = EHCA_BMASK_SET(GRH_IPVERSION_MASK, 6);
116         av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_TCLASS_MASK,
117                                             ah_attr->grh.traffic_class);
118         av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_FLOWLABEL_MASK,
119                                             ah_attr->grh.flow_label);
120         av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_HOPLIMIT_MASK,
121                                             ah_attr->grh.hop_limit);
122         av->av.grh.word_0 |= EHCA_BMASK_SET(GRH_NEXTHEADER_MASK, 0x1B);
123         /* set sgid in grh.word_1 */
124         if (ah_attr->ah_flags & IB_AH_GRH) {
125                 int rc;
126                 struct ib_port_attr port_attr;
127                 union ib_gid gid;
128                 memset(&port_attr, 0, sizeof(port_attr));
129                 rc = ehca_query_port(pd->device, ah_attr->port_num,
130                                      &port_attr);
131                 if (rc) { /* invalid port number */
132                         ret = -EINVAL;
133                         ehca_err(pd->device, "Invalid port number "
134                                  "ehca_query_port() returned %x "
135                                  "pd=%p ah_attr=%p", rc, pd, ah_attr);
136                         goto create_ah_exit1;
137                 }
138                 memset(&gid, 0, sizeof(gid));
139                 rc = ehca_query_gid(pd->device,
140                                     ah_attr->port_num,
141                                     ah_attr->grh.sgid_index, &gid);
142                 if (rc) {
143                         ret = -EINVAL;
144                         ehca_err(pd->device, "Failed to retrieve sgid "
145                                  "ehca_query_gid() returned %x "
146                                  "pd=%p ah_attr=%p", rc, pd, ah_attr);
147                         goto create_ah_exit1;
148                 }
149                 memcpy(&av->av.grh.word_1, &gid, sizeof(gid));
150         }
151         av->av.pmtu = shca->max_mtu;
152
153         /* dgid comes in grh.word_3 */
154         memcpy(&av->av.grh.word_3, &ah_attr->grh.dgid,
155                sizeof(ah_attr->grh.dgid));
156
157         return &av->ib_ah;
158
159 create_ah_exit1:
160         kmem_cache_free(av_cache, av);
161
162         return ERR_PTR(ret);
163 }
164
165 int ehca_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
166 {
167         struct ehca_av *av;
168         struct ehca_ud_av new_ehca_av;
169         struct ehca_pd *my_pd = container_of(ah->pd, struct ehca_pd, ib_pd);
170         struct ehca_shca *shca = container_of(ah->pd->device, struct ehca_shca,
171                                               ib_device);
172         u32 cur_pid = current->tgid;
173
174         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
175             my_pd->ownpid != cur_pid) {
176                 ehca_err(ah->device, "Invalid caller pid=%x ownpid=%x",
177                          cur_pid, my_pd->ownpid);
178                 return -EINVAL;
179         }
180
181         memset(&new_ehca_av, 0, sizeof(new_ehca_av));
182         new_ehca_av.sl = ah_attr->sl;
183         new_ehca_av.dlid = ah_attr->dlid;
184         new_ehca_av.slid_path_bits = ah_attr->src_path_bits;
185         new_ehca_av.ipd = ah_attr->static_rate;
186         new_ehca_av.lnh = EHCA_BMASK_SET(GRH_FLAG_MASK,
187                                          (ah_attr->ah_flags & IB_AH_GRH) > 0);
188         new_ehca_av.grh.word_0 = EHCA_BMASK_SET(GRH_TCLASS_MASK,
189                                                 ah_attr->grh.traffic_class);
190         new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_FLOWLABEL_MASK,
191                                                  ah_attr->grh.flow_label);
192         new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_HOPLIMIT_MASK,
193                                                  ah_attr->grh.hop_limit);
194         new_ehca_av.grh.word_0 |= EHCA_BMASK_SET(GRH_NEXTHEADER_MASK, 0x1b);
195
196         /* set sgid in grh.word_1 */
197         if (ah_attr->ah_flags & IB_AH_GRH) {
198                 int rc;
199                 struct ib_port_attr port_attr;
200                 union ib_gid gid;
201                 memset(&port_attr, 0, sizeof(port_attr));
202                 rc = ehca_query_port(ah->device, ah_attr->port_num,
203                                      &port_attr);
204                 if (rc) { /* invalid port number */
205                         ehca_err(ah->device, "Invalid port number "
206                                  "ehca_query_port() returned %x "
207                                  "ah=%p ah_attr=%p port_num=%x",
208                                  rc, ah, ah_attr, ah_attr->port_num);
209                         return -EINVAL;
210                 }
211                 memset(&gid, 0, sizeof(gid));
212                 rc = ehca_query_gid(ah->device,
213                                     ah_attr->port_num,
214                                     ah_attr->grh.sgid_index, &gid);
215                 if (rc) {
216                         ehca_err(ah->device, "Failed to retrieve sgid "
217                                  "ehca_query_gid() returned %x "
218                                  "ah=%p ah_attr=%p port_num=%x "
219                                  "sgid_index=%x",
220                                  rc, ah, ah_attr, ah_attr->port_num,
221                                  ah_attr->grh.sgid_index);
222                         return -EINVAL;
223                 }
224                 memcpy(&new_ehca_av.grh.word_1, &gid, sizeof(gid));
225         }
226
227         new_ehca_av.pmtu = shca->max_mtu;
228
229         memcpy(&new_ehca_av.grh.word_3, &ah_attr->grh.dgid,
230                sizeof(ah_attr->grh.dgid));
231
232         av = container_of(ah, struct ehca_av, ib_ah);
233         av->av = new_ehca_av;
234
235         return 0;
236 }
237
238 int ehca_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr)
239 {
240         struct ehca_av *av = container_of(ah, struct ehca_av, ib_ah);
241         struct ehca_pd *my_pd = container_of(ah->pd, struct ehca_pd, ib_pd);
242         u32 cur_pid = current->tgid;
243
244         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
245             my_pd->ownpid != cur_pid) {
246                 ehca_err(ah->device, "Invalid caller pid=%x ownpid=%x",
247                          cur_pid, my_pd->ownpid);
248                 return -EINVAL;
249         }
250
251         memcpy(&ah_attr->grh.dgid, &av->av.grh.word_3,
252                sizeof(ah_attr->grh.dgid));
253         ah_attr->sl = av->av.sl;
254
255         ah_attr->dlid = av->av.dlid;
256
257         ah_attr->src_path_bits = av->av.slid_path_bits;
258         ah_attr->static_rate = av->av.ipd;
259         ah_attr->ah_flags = EHCA_BMASK_GET(GRH_FLAG_MASK, av->av.lnh);
260         ah_attr->grh.traffic_class = EHCA_BMASK_GET(GRH_TCLASS_MASK,
261                                                     av->av.grh.word_0);
262         ah_attr->grh.hop_limit = EHCA_BMASK_GET(GRH_HOPLIMIT_MASK,
263                                                 av->av.grh.word_0);
264         ah_attr->grh.flow_label = EHCA_BMASK_GET(GRH_FLOWLABEL_MASK,
265                                                  av->av.grh.word_0);
266
267         return 0;
268 }
269
270 int ehca_destroy_ah(struct ib_ah *ah)
271 {
272         struct ehca_pd *my_pd = container_of(ah->pd, struct ehca_pd, ib_pd);
273         u32 cur_pid = current->tgid;
274
275         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
276             my_pd->ownpid != cur_pid) {
277                 ehca_err(ah->device, "Invalid caller pid=%x ownpid=%x",
278                          cur_pid, my_pd->ownpid);
279                 return -EINVAL;
280         }
281
282         kmem_cache_free(av_cache, container_of(ah, struct ehca_av, ib_ah));
283
284         return 0;
285 }
286
287 int ehca_init_av_cache(void)
288 {
289         av_cache = kmem_cache_create("ehca_cache_av",
290                                    sizeof(struct ehca_av), 0,
291                                    SLAB_HWCACHE_ALIGN,
292                                    NULL);
293         if (!av_cache)
294                 return -ENOMEM;
295         return 0;
296 }
297
298 void ehca_cleanup_av_cache(void)
299 {
300         if (av_cache)
301                 kmem_cache_destroy(av_cache);
302 }