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/init.h>
37 #include "mthca_dev.h"
38 #include "mthca_cmd.h"
41 MTHCA_QP_PER_MGM = 4 * (MTHCA_MGM_ENTRY_SIZE / 16 - 2)
45 __be32 next_gid_index;
48 __be32 qp[MTHCA_QP_PER_MGM];
51 static const u8 zero_gid[16]; /* automatically initialized to 0 */
54 * Caller must hold MCG table semaphore. gid and mgm parameters must
55 * be properly aligned for command interface.
57 * Returns 0 unless a firmware command error occurs.
59 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
60 * and *mgm holds MGM entry.
62 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
63 * previous entry in hash chain and *mgm holds AMGM entry.
65 * If no AMGM exists for given gid, *index = -1, *prev = index of last
66 * entry in hash chain and *mgm holds end of hash chain.
68 static int find_mgm(struct mthca_dev *dev,
69 u8 *gid, struct mthca_mailbox *mgm_mailbox,
70 u16 *hash, int *prev, int *index)
72 struct mthca_mailbox *mailbox;
73 struct mthca_mgm *mgm = mgm_mailbox->buf;
78 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
83 memcpy(mgid, gid, 16);
85 err = mthca_MGID_HASH(dev, mailbox, hash, &status);
89 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
95 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
96 "%04x:%04x:%04x:%04x is %04x\n",
97 be16_to_cpu(((__be16 *) gid)[0]),
98 be16_to_cpu(((__be16 *) gid)[1]),
99 be16_to_cpu(((__be16 *) gid)[2]),
100 be16_to_cpu(((__be16 *) gid)[3]),
101 be16_to_cpu(((__be16 *) gid)[4]),
102 be16_to_cpu(((__be16 *) gid)[5]),
103 be16_to_cpu(((__be16 *) gid)[6]),
104 be16_to_cpu(((__be16 *) gid)[7]),
111 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
115 mthca_err(dev, "READ_MGM returned status %02x\n", status);
119 if (!memcmp(mgm->gid, zero_gid, 16)) {
120 if (*index != *hash) {
121 mthca_err(dev, "Found zero MGID in AMGM.\n");
127 if (!memcmp(mgm->gid, gid, 16))
131 *index = be32_to_cpu(mgm->next_gid_index) >> 5;
137 mthca_free_mailbox(dev, mailbox);
141 int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
143 struct mthca_dev *dev = to_mdev(ibqp->device);
144 struct mthca_mailbox *mailbox;
145 struct mthca_mgm *mgm;
153 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
155 return PTR_ERR(mailbox);
158 if (down_interruptible(&dev->mcg_table.sem))
161 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
166 if (!memcmp(mgm->gid, zero_gid, 16))
167 memcpy(mgm->gid, gid->raw, 16);
171 index = mthca_alloc(&dev->mcg_table.alloc);
173 mthca_err(dev, "No AMGM entries left\n");
178 err = mthca_READ_MGM(dev, index, mailbox, &status);
182 mthca_err(dev, "READ_MGM returned status %02x\n", status);
187 memcpy(mgm->gid, gid->raw, 16);
188 mgm->next_gid_index = 0;
191 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
192 if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
193 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
197 if (i == MTHCA_QP_PER_MGM) {
198 mthca_err(dev, "MGM at index %x is full.\n", index);
203 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
207 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
214 err = mthca_READ_MGM(dev, prev, mailbox, &status);
218 mthca_err(dev, "READ_MGM returned status %02x\n", status);
223 mgm->next_gid_index = cpu_to_be32(index << 5);
225 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
229 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
234 up(&dev->mcg_table.sem);
235 mthca_free_mailbox(dev, mailbox);
239 int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
241 struct mthca_dev *dev = to_mdev(ibqp->device);
242 struct mthca_mailbox *mailbox;
243 struct mthca_mgm *mgm;
250 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
252 return PTR_ERR(mailbox);
255 if (down_interruptible(&dev->mcg_table.sem))
258 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
263 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
265 be16_to_cpu(((__be16 *) gid->raw)[0]),
266 be16_to_cpu(((__be16 *) gid->raw)[1]),
267 be16_to_cpu(((__be16 *) gid->raw)[2]),
268 be16_to_cpu(((__be16 *) gid->raw)[3]),
269 be16_to_cpu(((__be16 *) gid->raw)[4]),
270 be16_to_cpu(((__be16 *) gid->raw)[5]),
271 be16_to_cpu(((__be16 *) gid->raw)[6]),
272 be16_to_cpu(((__be16 *) gid->raw)[7]));
277 for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
278 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
280 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
285 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
290 mgm->qp[loc] = mgm->qp[i - 1];
293 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
297 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
308 /* Remove entry from MGM */
309 if (be32_to_cpu(mgm->next_gid_index) >> 5) {
310 err = mthca_READ_MGM(dev,
311 be32_to_cpu(mgm->next_gid_index) >> 5,
316 mthca_err(dev, "READ_MGM returned status %02x\n",
322 memset(mgm->gid, 0, 16);
324 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
328 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
333 /* Remove entry from AMGM */
334 index = be32_to_cpu(mgm->next_gid_index) >> 5;
335 err = mthca_READ_MGM(dev, prev, mailbox, &status);
339 mthca_err(dev, "READ_MGM returned status %02x\n", status);
344 mgm->next_gid_index = cpu_to_be32(index << 5);
346 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
350 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
357 up(&dev->mcg_table.sem);
358 mthca_free_mailbox(dev, mailbox);
362 int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
366 err = mthca_alloc_init(&dev->mcg_table.alloc,
367 dev->limits.num_amgms,
368 dev->limits.num_amgms - 1,
373 init_MUTEX(&dev->mcg_table.sem);
378 void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
380 mthca_alloc_cleanup(&dev->mcg_table.alloc);