2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: mthca_mcg.c 1349 2004-12-16 21:09:43Z roland $
35 #include <linux/string.h>
36 #include <linux/slab.h>
38 #include "mthca_dev.h"
39 #include "mthca_cmd.h"
42 __be32 next_gid_index;
45 __be32 qp[MTHCA_QP_PER_MGM];
48 static const u8 zero_gid[16]; /* automatically initialized to 0 */
51 * Caller must hold MCG table semaphore. gid and mgm parameters must
52 * be properly aligned for command interface.
54 * Returns 0 unless a firmware command error occurs.
56 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
57 * and *mgm holds MGM entry.
59 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
60 * previous entry in hash chain and *mgm holds AMGM entry.
62 * If no AMGM exists for given gid, *index = -1, *prev = index of last
63 * entry in hash chain and *mgm holds end of hash chain.
65 static int find_mgm(struct mthca_dev *dev,
66 u8 *gid, struct mthca_mailbox *mgm_mailbox,
67 u16 *hash, int *prev, int *index)
69 struct mthca_mailbox *mailbox;
70 struct mthca_mgm *mgm = mgm_mailbox->buf;
75 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
80 memcpy(mgid, gid, 16);
82 err = mthca_MGID_HASH(dev, mailbox, hash, &status);
86 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
92 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
93 "%04x:%04x:%04x:%04x is %04x\n",
94 be16_to_cpu(((__be16 *) gid)[0]),
95 be16_to_cpu(((__be16 *) gid)[1]),
96 be16_to_cpu(((__be16 *) gid)[2]),
97 be16_to_cpu(((__be16 *) gid)[3]),
98 be16_to_cpu(((__be16 *) gid)[4]),
99 be16_to_cpu(((__be16 *) gid)[5]),
100 be16_to_cpu(((__be16 *) gid)[6]),
101 be16_to_cpu(((__be16 *) gid)[7]),
108 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
112 mthca_err(dev, "READ_MGM returned status %02x\n", status);
117 if (!memcmp(mgm->gid, zero_gid, 16)) {
118 if (*index != *hash) {
119 mthca_err(dev, "Found zero MGID in AMGM.\n");
125 if (!memcmp(mgm->gid, gid, 16))
129 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
135 mthca_free_mailbox(dev, mailbox);
139 int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
141 struct mthca_dev *dev = to_mdev(ibqp->device);
142 struct mthca_mailbox *mailbox;
143 struct mthca_mgm *mgm;
151 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
153 return PTR_ERR(mailbox);
156 mutex_lock(&dev->mcg_table.mutex);
158 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
163 if (!memcmp(mgm->gid, zero_gid, 16))
164 memcpy(mgm->gid, gid->raw, 16);
168 index = mthca_alloc(&dev->mcg_table.alloc);
170 mthca_err(dev, "No AMGM entries left\n");
175 err = mthca_READ_MGM(dev, index, mailbox, &status);
179 mthca_err(dev, "READ_MGM returned status %02x\n", status);
183 memset(mgm, 0, sizeof *mgm);
184 memcpy(mgm->gid, gid->raw, 16);
187 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
188 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
189 mthca_dbg(dev, "QP %06x already a member of MGM\n",
193 } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
194 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
198 if (i == MTHCA_QP_PER_MGM) {
199 mthca_err(dev, "MGM at index %x is full.\n", index);
204 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
208 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
216 err = mthca_READ_MGM(dev, prev, mailbox, &status);
220 mthca_err(dev, "READ_MGM returned status %02x\n", status);
225 mgm->next_gid_index = cpu_to_be32(index << 6);
227 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
231 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
236 if (err && link && index != -1) {
237 BUG_ON(index < dev->limits.num_mgms);
238 mthca_free(&dev->mcg_table.alloc, index);
240 mutex_unlock(&dev->mcg_table.mutex);
242 mthca_free_mailbox(dev, mailbox);
246 int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
248 struct mthca_dev *dev = to_mdev(ibqp->device);
249 struct mthca_mailbox *mailbox;
250 struct mthca_mgm *mgm;
257 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
259 return PTR_ERR(mailbox);
262 mutex_lock(&dev->mcg_table.mutex);
264 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
269 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
271 be16_to_cpu(((__be16 *) gid->raw)[0]),
272 be16_to_cpu(((__be16 *) gid->raw)[1]),
273 be16_to_cpu(((__be16 *) gid->raw)[2]),
274 be16_to_cpu(((__be16 *) gid->raw)[3]),
275 be16_to_cpu(((__be16 *) gid->raw)[4]),
276 be16_to_cpu(((__be16 *) gid->raw)[5]),
277 be16_to_cpu(((__be16 *) gid->raw)[6]),
278 be16_to_cpu(((__be16 *) gid->raw)[7]));
283 for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
284 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
286 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
291 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
296 mgm->qp[loc] = mgm->qp[i - 1];
299 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
303 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
312 /* Remove entry from MGM */
313 int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
314 if (amgm_index_to_free) {
315 err = mthca_READ_MGM(dev, amgm_index_to_free,
320 mthca_err(dev, "READ_MGM returned status %02x\n",
326 memset(mgm->gid, 0, 16);
328 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
332 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
336 if (amgm_index_to_free) {
337 BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
338 mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
341 /* Remove entry from AMGM */
342 int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
343 err = mthca_READ_MGM(dev, prev, mailbox, &status);
347 mthca_err(dev, "READ_MGM returned status %02x\n", status);
352 mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
354 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
358 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
362 BUG_ON(index < dev->limits.num_mgms);
363 mthca_free(&dev->mcg_table.alloc, index);
367 mutex_unlock(&dev->mcg_table.mutex);
369 mthca_free_mailbox(dev, mailbox);
373 int mthca_init_mcg_table(struct mthca_dev *dev)
376 int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
378 err = mthca_alloc_init(&dev->mcg_table.alloc,
381 dev->limits.num_mgms);
385 mutex_init(&dev->mcg_table.mutex);
390 void mthca_cleanup_mcg_table(struct mthca_dev *dev)
392 mthca_alloc_cleanup(&dev->mcg_table.alloc);