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