Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6-block.git] / drivers / net / mlx4 / mcg.c
CommitLineData
225c7b1f
RD
1/*
2 * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
51a379d0 3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
225c7b1f
RD
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
225c7b1f
RD
34#include <linux/string.h>
35#include <linux/slab.h>
36
37#include <linux/mlx4/cmd.h>
38
39#include "mlx4.h"
40
521e575b
RL
41#define MGM_QPN_MASK 0x00FFFFFF
42#define MGM_BLCK_LB_BIT 30
43
225c7b1f
RD
44struct mlx4_mgm {
45 __be32 next_gid_index;
46 __be32 members_count;
47 u32 reserved[2];
48 u8 gid[16];
49 __be32 qp[MLX4_QP_PER_MGM];
50};
51
52static const u8 zero_gid[16]; /* automatically initialized to 0 */
53
54static int mlx4_READ_MCG(struct mlx4_dev *dev, int index,
55 struct mlx4_cmd_mailbox *mailbox)
56{
57 return mlx4_cmd_box(dev, 0, mailbox->dma, index, 0, MLX4_CMD_READ_MCG,
58 MLX4_CMD_TIME_CLASS_A);
59}
60
61static int mlx4_WRITE_MCG(struct mlx4_dev *dev, int index,
62 struct mlx4_cmd_mailbox *mailbox)
63{
64 return mlx4_cmd(dev, mailbox->dma, index, 0, MLX4_CMD_WRITE_MCG,
65 MLX4_CMD_TIME_CLASS_A);
66}
67
68static int mlx4_MGID_HASH(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
69 u16 *hash)
70{
71 u64 imm;
72 int err;
73
74 err = mlx4_cmd_imm(dev, mailbox->dma, &imm, 0, 0, MLX4_CMD_MGID_HASH,
75 MLX4_CMD_TIME_CLASS_A);
76
77 if (!err)
78 *hash = imm;
79
80 return err;
81}
82
83/*
84 * Caller must hold MCG table semaphore. gid and mgm parameters must
85 * be properly aligned for command interface.
86 *
87 * Returns 0 unless a firmware command error occurs.
88 *
89 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
90 * and *mgm holds MGM entry.
91 *
92 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
93 * previous entry in hash chain and *mgm holds AMGM entry.
94 *
95 * If no AMGM exists for given gid, *index = -1, *prev = index of last
96 * entry in hash chain and *mgm holds end of hash chain.
97 */
98static int find_mgm(struct mlx4_dev *dev,
99 u8 *gid, struct mlx4_cmd_mailbox *mgm_mailbox,
100 u16 *hash, int *prev, int *index)
101{
102 struct mlx4_cmd_mailbox *mailbox;
103 struct mlx4_mgm *mgm = mgm_mailbox->buf;
104 u8 *mgid;
105 int err;
106
107 mailbox = mlx4_alloc_cmd_mailbox(dev);
108 if (IS_ERR(mailbox))
109 return -ENOMEM;
110 mgid = mailbox->buf;
111
112 memcpy(mgid, gid, 16);
113
114 err = mlx4_MGID_HASH(dev, mailbox, hash);
115 mlx4_free_cmd_mailbox(dev, mailbox);
116 if (err)
117 return err;
118
119 if (0)
5b095d98 120 mlx4_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
225c7b1f
RD
121
122 *index = *hash;
123 *prev = -1;
124
125 do {
126 err = mlx4_READ_MCG(dev, *index, mgm_mailbox);
127 if (err)
128 return err;
129
130 if (!memcmp(mgm->gid, zero_gid, 16)) {
131 if (*index != *hash) {
132 mlx4_err(dev, "Found zero MGID in AMGM.\n");
133 err = -EINVAL;
134 }
135 return err;
136 }
137
138 if (!memcmp(mgm->gid, gid, 16))
139 return err;
140
141 *prev = *index;
142 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
143 } while (*index);
144
145 *index = -1;
146 return err;
147}
148
521e575b
RL
149int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
150 int block_mcast_loopback)
225c7b1f
RD
151{
152 struct mlx4_priv *priv = mlx4_priv(dev);
153 struct mlx4_cmd_mailbox *mailbox;
154 struct mlx4_mgm *mgm;
155 u32 members_count;
156 u16 hash;
157 int index, prev;
158 int link = 0;
159 int i;
160 int err;
161
162 mailbox = mlx4_alloc_cmd_mailbox(dev);
163 if (IS_ERR(mailbox))
164 return PTR_ERR(mailbox);
165 mgm = mailbox->buf;
166
167 mutex_lock(&priv->mcg_table.mutex);
168
169 err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
170 if (err)
171 goto out;
172
173 if (index != -1) {
174 if (!memcmp(mgm->gid, zero_gid, 16))
175 memcpy(mgm->gid, gid, 16);
176 } else {
177 link = 1;
178
179 index = mlx4_bitmap_alloc(&priv->mcg_table.bitmap);
180 if (index == -1) {
181 mlx4_err(dev, "No AMGM entries left\n");
182 err = -ENOMEM;
183 goto out;
184 }
185 index += dev->caps.num_mgms;
186
225c7b1f
RD
187 memset(mgm, 0, sizeof *mgm);
188 memcpy(mgm->gid, gid, 16);
189 }
190
191 members_count = be32_to_cpu(mgm->members_count);
192 if (members_count == MLX4_QP_PER_MGM) {
193 mlx4_err(dev, "MGM at index %x is full.\n", index);
194 err = -ENOMEM;
195 goto out;
196 }
197
198 for (i = 0; i < members_count; ++i)
521e575b 199 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn) {
225c7b1f
RD
200 mlx4_dbg(dev, "QP %06x already a member of MGM\n", qp->qpn);
201 err = 0;
202 goto out;
203 }
204
521e575b
RL
205 if (block_mcast_loopback)
206 mgm->qp[members_count++] = cpu_to_be32((qp->qpn & MGM_QPN_MASK) |
e6a17622 207 (1U << MGM_BLCK_LB_BIT));
521e575b
RL
208 else
209 mgm->qp[members_count++] = cpu_to_be32(qp->qpn & MGM_QPN_MASK);
210
225c7b1f
RD
211 mgm->members_count = cpu_to_be32(members_count);
212
213 err = mlx4_WRITE_MCG(dev, index, mailbox);
214 if (err)
215 goto out;
216
217 if (!link)
218 goto out;
219
220 err = mlx4_READ_MCG(dev, prev, mailbox);
221 if (err)
222 goto out;
223
224 mgm->next_gid_index = cpu_to_be32(index << 6);
225
226 err = mlx4_WRITE_MCG(dev, prev, mailbox);
227 if (err)
228 goto out;
229
230out:
231 if (err && link && index != -1) {
232 if (index < dev->caps.num_mgms)
233 mlx4_warn(dev, "Got AMGM index %d < %d",
234 index, dev->caps.num_mgms);
235 else
236 mlx4_bitmap_free(&priv->mcg_table.bitmap,
237 index - dev->caps.num_mgms);
238 }
239 mutex_unlock(&priv->mcg_table.mutex);
240
241 mlx4_free_cmd_mailbox(dev, mailbox);
242 return err;
243}
244EXPORT_SYMBOL_GPL(mlx4_multicast_attach);
245
246int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16])
247{
248 struct mlx4_priv *priv = mlx4_priv(dev);
249 struct mlx4_cmd_mailbox *mailbox;
250 struct mlx4_mgm *mgm;
251 u32 members_count;
252 u16 hash;
253 int prev, index;
254 int i, loc;
255 int err;
256
257 mailbox = mlx4_alloc_cmd_mailbox(dev);
258 if (IS_ERR(mailbox))
259 return PTR_ERR(mailbox);
260 mgm = mailbox->buf;
261
262 mutex_lock(&priv->mcg_table.mutex);
263
264 err = find_mgm(dev, gid, mailbox, &hash, &prev, &index);
265 if (err)
266 goto out;
267
268 if (index == -1) {
5b095d98 269 mlx4_err(dev, "MGID %pI6 not found\n", gid);
225c7b1f
RD
270 err = -EINVAL;
271 goto out;
272 }
273
274 members_count = be32_to_cpu(mgm->members_count);
275 for (loc = -1, i = 0; i < members_count; ++i)
521e575b 276 if ((be32_to_cpu(mgm->qp[i]) & MGM_QPN_MASK) == qp->qpn)
225c7b1f
RD
277 loc = i;
278
279 if (loc == -1) {
280 mlx4_err(dev, "QP %06x not found in MGM\n", qp->qpn);
281 err = -EINVAL;
282 goto out;
283 }
284
285
286 mgm->members_count = cpu_to_be32(--members_count);
287 mgm->qp[loc] = mgm->qp[i - 1];
288 mgm->qp[i - 1] = 0;
289
4dc51b32
EC
290 if (i != 1) {
291 err = mlx4_WRITE_MCG(dev, index, mailbox);
225c7b1f 292 goto out;
4dc51b32 293 }
225c7b1f
RD
294
295 if (prev == -1) {
296 /* Remove entry from MGM */
297 int amgm_index = be32_to_cpu(mgm->next_gid_index) >> 6;
298 if (amgm_index) {
299 err = mlx4_READ_MCG(dev, amgm_index, mailbox);
300 if (err)
301 goto out;
302 } else
303 memset(mgm->gid, 0, 16);
304
305 err = mlx4_WRITE_MCG(dev, index, mailbox);
306 if (err)
307 goto out;
308
309 if (amgm_index) {
310 if (amgm_index < dev->caps.num_mgms)
311 mlx4_warn(dev, "MGM entry %d had AMGM index %d < %d",
312 index, amgm_index, dev->caps.num_mgms);
313 else
314 mlx4_bitmap_free(&priv->mcg_table.bitmap,
315 amgm_index - dev->caps.num_mgms);
316 }
317 } else {
318 /* Remove entry from AMGM */
319 int cur_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
320 err = mlx4_READ_MCG(dev, prev, mailbox);
321 if (err)
322 goto out;
323
324 mgm->next_gid_index = cpu_to_be32(cur_next_index << 6);
325
326 err = mlx4_WRITE_MCG(dev, prev, mailbox);
327 if (err)
328 goto out;
329
330 if (index < dev->caps.num_mgms)
331 mlx4_warn(dev, "entry %d had next AMGM index %d < %d",
332 prev, index, dev->caps.num_mgms);
333 else
334 mlx4_bitmap_free(&priv->mcg_table.bitmap,
335 index - dev->caps.num_mgms);
336 }
337
338out:
339 mutex_unlock(&priv->mcg_table.mutex);
340
341 mlx4_free_cmd_mailbox(dev, mailbox);
342 return err;
343}
344EXPORT_SYMBOL_GPL(mlx4_multicast_detach);
345
3d73c288 346int mlx4_init_mcg_table(struct mlx4_dev *dev)
225c7b1f
RD
347{
348 struct mlx4_priv *priv = mlx4_priv(dev);
349 int err;
350
93fc9e1b
YP
351 err = mlx4_bitmap_init(&priv->mcg_table.bitmap, dev->caps.num_amgms,
352 dev->caps.num_amgms - 1, 0, 0);
225c7b1f
RD
353 if (err)
354 return err;
355
356 mutex_init(&priv->mcg_table.mutex);
357
358 return 0;
359}
360
361void mlx4_cleanup_mcg_table(struct mlx4_dev *dev)
362{
363 mlx4_bitmap_cleanup(&mlx4_priv(dev)->mcg_table.bitmap);
364}