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
33 #include <linux/string.h>
34 #include <linux/gfp.h>
36 #include "mthca_dev.h"
37 #include "mthca_cmd.h"
40 __be32 next_gid_index
;
43 __be32 qp
[MTHCA_QP_PER_MGM
];
46 static const u8 zero_gid
[16]; /* automatically initialized to 0 */
49 * Caller must hold MCG table semaphore. gid and mgm parameters must
50 * be properly aligned for command interface.
52 * Returns 0 unless a firmware command error occurs.
54 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
55 * and *mgm holds MGM entry.
57 * if GID is found in AMGM, *index = index in AMGM, *prev = index of
58 * previous entry in hash chain and *mgm holds AMGM entry.
60 * If no AMGM exists for given gid, *index = -1, *prev = index of last
61 * entry in hash chain and *mgm holds end of hash chain.
63 static int find_mgm(struct mthca_dev
*dev
,
64 u8
*gid
, struct mthca_mailbox
*mgm_mailbox
,
65 u16
*hash
, int *prev
, int *index
)
67 struct mthca_mailbox
*mailbox
;
68 struct mthca_mgm
*mgm
= mgm_mailbox
->buf
;
73 mailbox
= mthca_alloc_mailbox(dev
, GFP_KERNEL
);
78 memcpy(mgid
, gid
, 16);
80 err
= mthca_MGID_HASH(dev
, mailbox
, hash
, &status
);
84 mthca_err(dev
, "MGID_HASH returned status %02x\n", status
);
90 mthca_dbg(dev
, "Hash for %pI6 is %04x\n", gid
, *hash
);
96 err
= mthca_READ_MGM(dev
, *index
, mgm_mailbox
, &status
);
100 mthca_err(dev
, "READ_MGM returned status %02x\n", status
);
105 if (!memcmp(mgm
->gid
, zero_gid
, 16)) {
106 if (*index
!= *hash
) {
107 mthca_err(dev
, "Found zero MGID in AMGM.\n");
113 if (!memcmp(mgm
->gid
, gid
, 16))
117 *index
= be32_to_cpu(mgm
->next_gid_index
) >> 6;
123 mthca_free_mailbox(dev
, mailbox
);
127 int mthca_multicast_attach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
129 struct mthca_dev
*dev
= to_mdev(ibqp
->device
);
130 struct mthca_mailbox
*mailbox
;
131 struct mthca_mgm
*mgm
;
139 mailbox
= mthca_alloc_mailbox(dev
, GFP_KERNEL
);
141 return PTR_ERR(mailbox
);
144 mutex_lock(&dev
->mcg_table
.mutex
);
146 err
= find_mgm(dev
, gid
->raw
, mailbox
, &hash
, &prev
, &index
);
151 if (!memcmp(mgm
->gid
, zero_gid
, 16))
152 memcpy(mgm
->gid
, gid
->raw
, 16);
156 index
= mthca_alloc(&dev
->mcg_table
.alloc
);
158 mthca_err(dev
, "No AMGM entries left\n");
163 err
= mthca_READ_MGM(dev
, index
, mailbox
, &status
);
167 mthca_err(dev
, "READ_MGM returned status %02x\n", status
);
171 memset(mgm
, 0, sizeof *mgm
);
172 memcpy(mgm
->gid
, gid
->raw
, 16);
175 for (i
= 0; i
< MTHCA_QP_PER_MGM
; ++i
)
176 if (mgm
->qp
[i
] == cpu_to_be32(ibqp
->qp_num
| (1 << 31))) {
177 mthca_dbg(dev
, "QP %06x already a member of MGM\n",
181 } else if (!(mgm
->qp
[i
] & cpu_to_be32(1 << 31))) {
182 mgm
->qp
[i
] = cpu_to_be32(ibqp
->qp_num
| (1 << 31));
186 if (i
== MTHCA_QP_PER_MGM
) {
187 mthca_err(dev
, "MGM at index %x is full.\n", index
);
192 err
= mthca_WRITE_MGM(dev
, index
, mailbox
, &status
);
196 mthca_err(dev
, "WRITE_MGM returned status %02x\n", status
);
204 err
= mthca_READ_MGM(dev
, prev
, mailbox
, &status
);
208 mthca_err(dev
, "READ_MGM returned status %02x\n", status
);
213 mgm
->next_gid_index
= cpu_to_be32(index
<< 6);
215 err
= mthca_WRITE_MGM(dev
, prev
, mailbox
, &status
);
219 mthca_err(dev
, "WRITE_MGM returned status %02x\n", status
);
224 if (err
&& link
&& index
!= -1) {
225 BUG_ON(index
< dev
->limits
.num_mgms
);
226 mthca_free(&dev
->mcg_table
.alloc
, index
);
228 mutex_unlock(&dev
->mcg_table
.mutex
);
230 mthca_free_mailbox(dev
, mailbox
);
234 int mthca_multicast_detach(struct ib_qp
*ibqp
, union ib_gid
*gid
, u16 lid
)
236 struct mthca_dev
*dev
= to_mdev(ibqp
->device
);
237 struct mthca_mailbox
*mailbox
;
238 struct mthca_mgm
*mgm
;
245 mailbox
= mthca_alloc_mailbox(dev
, GFP_KERNEL
);
247 return PTR_ERR(mailbox
);
250 mutex_lock(&dev
->mcg_table
.mutex
);
252 err
= find_mgm(dev
, gid
->raw
, mailbox
, &hash
, &prev
, &index
);
257 mthca_err(dev
, "MGID %pI6 not found\n", gid
->raw
);
262 for (loc
= -1, i
= 0; i
< MTHCA_QP_PER_MGM
; ++i
) {
263 if (mgm
->qp
[i
] == cpu_to_be32(ibqp
->qp_num
| (1 << 31)))
265 if (!(mgm
->qp
[i
] & cpu_to_be32(1 << 31)))
270 mthca_err(dev
, "QP %06x not found in MGM\n", ibqp
->qp_num
);
275 mgm
->qp
[loc
] = mgm
->qp
[i
- 1];
278 err
= mthca_WRITE_MGM(dev
, index
, mailbox
, &status
);
282 mthca_err(dev
, "WRITE_MGM returned status %02x\n", status
);
291 /* Remove entry from MGM */
292 int amgm_index_to_free
= be32_to_cpu(mgm
->next_gid_index
) >> 6;
293 if (amgm_index_to_free
) {
294 err
= mthca_READ_MGM(dev
, amgm_index_to_free
,
299 mthca_err(dev
, "READ_MGM returned status %02x\n",
305 memset(mgm
->gid
, 0, 16);
307 err
= mthca_WRITE_MGM(dev
, index
, mailbox
, &status
);
311 mthca_err(dev
, "WRITE_MGM returned status %02x\n", status
);
315 if (amgm_index_to_free
) {
316 BUG_ON(amgm_index_to_free
< dev
->limits
.num_mgms
);
317 mthca_free(&dev
->mcg_table
.alloc
, amgm_index_to_free
);
320 /* Remove entry from AMGM */
321 int curr_next_index
= be32_to_cpu(mgm
->next_gid_index
) >> 6;
322 err
= mthca_READ_MGM(dev
, prev
, mailbox
, &status
);
326 mthca_err(dev
, "READ_MGM returned status %02x\n", status
);
331 mgm
->next_gid_index
= cpu_to_be32(curr_next_index
<< 6);
333 err
= mthca_WRITE_MGM(dev
, prev
, mailbox
, &status
);
337 mthca_err(dev
, "WRITE_MGM returned status %02x\n", status
);
341 BUG_ON(index
< dev
->limits
.num_mgms
);
342 mthca_free(&dev
->mcg_table
.alloc
, index
);
346 mutex_unlock(&dev
->mcg_table
.mutex
);
348 mthca_free_mailbox(dev
, mailbox
);
352 int mthca_init_mcg_table(struct mthca_dev
*dev
)
355 int table_size
= dev
->limits
.num_mgms
+ dev
->limits
.num_amgms
;
357 err
= mthca_alloc_init(&dev
->mcg_table
.alloc
,
360 dev
->limits
.num_mgms
);
364 mutex_init(&dev
->mcg_table
.mutex
);
369 void mthca_cleanup_mcg_table(struct mthca_dev
*dev
)
371 mthca_alloc_cleanup(&dev
->mcg_table
.alloc
);