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