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>
36 #include <linux/string.h>
37 #include <linux/slab.h>
39 #include "mthca_dev.h"
40 #include "mthca_cmd.h"
43 __be32 next_gid_index;
46 __be32 qp[MTHCA_QP_PER_MGM];
49 static const u8 zero_gid[16]; /* automatically initialized to 0 */
52 * Caller must hold MCG table semaphore. gid and mgm parameters must
53 * be properly aligned for command interface.
55 * Returns 0 unless a firmware command error occurs.
57 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
58 * and *mgm holds MGM entry.
60 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
61 * previous entry in hash chain and *mgm holds AMGM entry.
63 * If no AMGM exists for given gid, *index = -1, *prev = index of last
64 * entry in hash chain and *mgm holds end of hash chain.
66 static int find_mgm(struct mthca_dev *dev,
67 u8 *gid, struct mthca_mailbox *mgm_mailbox,
68 u16 *hash, int *prev, int *index)
70 struct mthca_mailbox *mailbox;
71 struct mthca_mgm *mgm = mgm_mailbox->buf;
76 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
81 memcpy(mgid, gid, 16);
83 err = mthca_MGID_HASH(dev, mailbox, hash, &status);
87 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
93 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
94 "%04x:%04x:%04x:%04x is %04x\n",
95 be16_to_cpu(((__be16 *) gid)[0]),
96 be16_to_cpu(((__be16 *) gid)[1]),
97 be16_to_cpu(((__be16 *) gid)[2]),
98 be16_to_cpu(((__be16 *) gid)[3]),
99 be16_to_cpu(((__be16 *) gid)[4]),
100 be16_to_cpu(((__be16 *) gid)[5]),
101 be16_to_cpu(((__be16 *) gid)[6]),
102 be16_to_cpu(((__be16 *) gid)[7]),
109 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
113 mthca_err(dev, "READ_MGM returned status %02x\n", status);
118 if (!memcmp(mgm->gid, zero_gid, 16)) {
119 if (*index != *hash) {
120 mthca_err(dev, "Found zero MGID in AMGM.\n");
126 if (!memcmp(mgm->gid, gid, 16))
130 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
136 mthca_free_mailbox(dev, mailbox);
140 int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
142 struct mthca_dev *dev = to_mdev(ibqp->device);
143 struct mthca_mailbox *mailbox;
144 struct mthca_mgm *mgm;
152 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
154 return PTR_ERR(mailbox);
157 if (down_interruptible(&dev->mcg_table.sem)) {
162 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
167 if (!memcmp(mgm->gid, zero_gid, 16))
168 memcpy(mgm->gid, gid->raw, 16);
172 index = mthca_alloc(&dev->mcg_table.alloc);
174 mthca_err(dev, "No AMGM entries left\n");
179 err = mthca_READ_MGM(dev, index, mailbox, &status);
183 mthca_err(dev, "READ_MGM returned status %02x\n", status);
187 memset(mgm, 0, sizeof *mgm);
188 memcpy(mgm->gid, gid->raw, 16);
191 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
192 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
193 mthca_dbg(dev, "QP %06x already a member of MGM\n",
197 } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
198 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
202 if (i == MTHCA_QP_PER_MGM) {
203 mthca_err(dev, "MGM at index %x is full.\n", index);
208 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
212 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
220 err = mthca_READ_MGM(dev, prev, mailbox, &status);
224 mthca_err(dev, "READ_MGM returned status %02x\n", status);
229 mgm->next_gid_index = cpu_to_be32(index << 6);
231 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
235 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
240 if (err && link && index != -1) {
241 BUG_ON(index < dev->limits.num_mgms);
242 mthca_free(&dev->mcg_table.alloc, index);
244 up(&dev->mcg_table.sem);
246 mthca_free_mailbox(dev, mailbox);
250 int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
252 struct mthca_dev *dev = to_mdev(ibqp->device);
253 struct mthca_mailbox *mailbox;
254 struct mthca_mgm *mgm;
261 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
263 return PTR_ERR(mailbox);
266 if (down_interruptible(&dev->mcg_table.sem)) {
271 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
276 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
278 be16_to_cpu(((__be16 *) gid->raw)[0]),
279 be16_to_cpu(((__be16 *) gid->raw)[1]),
280 be16_to_cpu(((__be16 *) gid->raw)[2]),
281 be16_to_cpu(((__be16 *) gid->raw)[3]),
282 be16_to_cpu(((__be16 *) gid->raw)[4]),
283 be16_to_cpu(((__be16 *) gid->raw)[5]),
284 be16_to_cpu(((__be16 *) gid->raw)[6]),
285 be16_to_cpu(((__be16 *) gid->raw)[7]));
290 for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
291 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
293 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
298 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
303 mgm->qp[loc] = mgm->qp[i - 1];
306 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
310 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
319 /* Remove entry from MGM */
320 int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
321 if (amgm_index_to_free) {
322 err = mthca_READ_MGM(dev, amgm_index_to_free,
327 mthca_err(dev, "READ_MGM returned status %02x\n",
333 memset(mgm->gid, 0, 16);
335 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
339 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
343 if (amgm_index_to_free) {
344 BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
345 mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
348 /* Remove entry from AMGM */
349 int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
350 err = mthca_READ_MGM(dev, prev, mailbox, &status);
354 mthca_err(dev, "READ_MGM returned status %02x\n", status);
359 mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
361 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
365 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
369 BUG_ON(index < dev->limits.num_mgms);
370 mthca_free(&dev->mcg_table.alloc, index);
374 up(&dev->mcg_table.sem);
376 mthca_free_mailbox(dev, mailbox);
380 int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
383 int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
385 err = mthca_alloc_init(&dev->mcg_table.alloc,
388 dev->limits.num_mgms);
392 init_MUTEX(&dev->mcg_table.sem);
397 void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
399 mthca_alloc_cleanup(&dev->mcg_table.alloc);