blob: 515790a606e6e56369e57840e39cadc2a2ee166d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
Tim Schmielau4e57b682005-10-30 15:03:48 -080033#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "mthca_dev.h"
37#include "mthca_cmd.h"
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct mthca_mgm {
Sean Hefty97f52eb2005-08-13 21:05:57 -070040 __be32 next_gid_index;
41 u32 reserved[3];
42 u8 gid[16];
43 __be32 qp[MTHCA_QP_PER_MGM];
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46static const u8 zero_gid[16]; /* automatically initialized to 0 */
47
48/*
49 * Caller must hold MCG table semaphore. gid and mgm parameters must
50 * be properly aligned for command interface.
51 *
52 * Returns 0 unless a firmware command error occurs.
53 *
54 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
55 * and *mgm holds MGM entry.
56 *
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.
59 *
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.
62 */
63static int find_mgm(struct mthca_dev *dev,
Roland Dreiered878452005-06-27 14:36:45 -070064 u8 *gid, struct mthca_mailbox *mgm_mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 u16 *hash, int *prev, int *index)
66{
Roland Dreiered878452005-06-27 14:36:45 -070067 struct mthca_mailbox *mailbox;
68 struct mthca_mgm *mgm = mgm_mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 u8 *mgid;
70 int err;
71 u8 status;
72
Roland Dreiered878452005-06-27 14:36:45 -070073 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
74 if (IS_ERR(mailbox))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -ENOMEM;
Roland Dreiered878452005-06-27 14:36:45 -070076 mgid = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 memcpy(mgid, gid, 16);
79
Roland Dreiered878452005-06-27 14:36:45 -070080 err = mthca_MGID_HASH(dev, mailbox, hash, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (err)
82 goto out;
83 if (status) {
84 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
85 err = -EINVAL;
86 goto out;
87 }
88
89 if (0)
Harvey Harrison5b095d9892008-10-29 12:52:50 -070090 mthca_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 *index = *hash;
93 *prev = -1;
94
95 do {
Roland Dreiered878452005-06-27 14:36:45 -070096 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (err)
98 goto out;
99 if (status) {
100 mthca_err(dev, "READ_MGM returned status %02x\n", status);
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800101 err = -EINVAL;
102 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
105 if (!memcmp(mgm->gid, zero_gid, 16)) {
106 if (*index != *hash) {
107 mthca_err(dev, "Found zero MGID in AMGM.\n");
108 err = -EINVAL;
109 }
110 goto out;
111 }
112
113 if (!memcmp(mgm->gid, gid, 16))
114 goto out;
115
116 *prev = *index;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800117 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 } while (*index);
119
120 *index = -1;
121
122 out:
Roland Dreiered878452005-06-27 14:36:45 -0700123 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return err;
125}
126
127int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
128{
129 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700130 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct mthca_mgm *mgm;
132 u16 hash;
133 int index, prev;
134 int link = 0;
135 int i;
136 int err;
137 u8 status;
138
Roland Dreiered878452005-06-27 14:36:45 -0700139 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
140 if (IS_ERR(mailbox))
141 return PTR_ERR(mailbox);
142 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800144 mutex_lock(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Roland Dreiered878452005-06-27 14:36:45 -0700146 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (err)
148 goto out;
149
150 if (index != -1) {
151 if (!memcmp(mgm->gid, zero_gid, 16))
152 memcpy(mgm->gid, gid->raw, 16);
153 } else {
154 link = 1;
155
156 index = mthca_alloc(&dev->mcg_table.alloc);
157 if (index == -1) {
158 mthca_err(dev, "No AMGM entries left\n");
159 err = -ENOMEM;
160 goto out;
161 }
162
Roland Dreiered878452005-06-27 14:36:45 -0700163 err = mthca_READ_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (err)
165 goto out;
166 if (status) {
167 mthca_err(dev, "READ_MGM returned status %02x\n", status);
168 err = -EINVAL;
169 goto out;
170 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800171 memset(mgm, 0, sizeof *mgm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 memcpy(mgm->gid, gid->raw, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
175 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
Jack Morgenstein7150bf82005-10-18 14:46:38 -0700176 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800177 mthca_dbg(dev, "QP %06x already a member of MGM\n",
Jack Morgenstein7150bf82005-10-18 14:46:38 -0700178 ibqp->qp_num);
179 err = 0;
180 goto out;
181 } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
183 break;
184 }
185
186 if (i == MTHCA_QP_PER_MGM) {
187 mthca_err(dev, "MGM at index %x is full.\n", index);
188 err = -ENOMEM;
189 goto out;
190 }
191
Roland Dreiered878452005-06-27 14:36:45 -0700192 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (err)
194 goto out;
195 if (status) {
196 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
197 err = -EINVAL;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 if (!link)
202 goto out;
203
Roland Dreiered878452005-06-27 14:36:45 -0700204 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (err)
206 goto out;
207 if (status) {
208 mthca_err(dev, "READ_MGM returned status %02x\n", status);
209 err = -EINVAL;
210 goto out;
211 }
212
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800213 mgm->next_gid_index = cpu_to_be32(index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Roland Dreiered878452005-06-27 14:36:45 -0700215 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (err)
217 goto out;
218 if (status) {
219 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
220 err = -EINVAL;
221 }
222
223 out:
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800224 if (err && link && index != -1) {
225 BUG_ON(index < dev->limits.num_mgms);
226 mthca_free(&dev->mcg_table.alloc, index);
227 }
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800228 mutex_unlock(&dev->mcg_table.mutex);
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800229
Roland Dreiered878452005-06-27 14:36:45 -0700230 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return err;
232}
233
234int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
235{
236 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700237 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct mthca_mgm *mgm;
239 u16 hash;
240 int prev, index;
241 int i, loc;
242 int err;
243 u8 status;
244
Roland Dreiered878452005-06-27 14:36:45 -0700245 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
246 if (IS_ERR(mailbox))
247 return PTR_ERR(mailbox);
248 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800250 mutex_lock(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Roland Dreiered878452005-06-27 14:36:45 -0700252 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (err)
254 goto out;
255
256 if (index == -1) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700257 mthca_err(dev, "MGID %pI6 not found\n", gid->raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 err = -EINVAL;
259 goto out;
260 }
261
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)))
264 loc = i;
265 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
266 break;
267 }
268
269 if (loc == -1) {
270 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
271 err = -EINVAL;
272 goto out;
273 }
274
275 mgm->qp[loc] = mgm->qp[i - 1];
276 mgm->qp[i - 1] = 0;
277
Roland Dreiered878452005-06-27 14:36:45 -0700278 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (err)
280 goto out;
281 if (status) {
282 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
283 err = -EINVAL;
284 goto out;
285 }
286
287 if (i != 1)
288 goto out;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (prev == -1) {
291 /* Remove entry from MGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800292 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,
Roland Dreiered878452005-06-27 14:36:45 -0700295 mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (err)
297 goto out;
298 if (status) {
299 mthca_err(dev, "READ_MGM returned status %02x\n",
300 status);
301 err = -EINVAL;
302 goto out;
303 }
304 } else
305 memset(mgm->gid, 0, 16);
306
Roland Dreiered878452005-06-27 14:36:45 -0700307 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (err)
309 goto out;
310 if (status) {
311 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
312 err = -EINVAL;
313 goto out;
314 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800315 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);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else {
320 /* Remove entry from AMGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800321 int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
Roland Dreiered878452005-06-27 14:36:45 -0700322 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (err)
324 goto out;
325 if (status) {
326 mthca_err(dev, "READ_MGM returned status %02x\n", status);
327 err = -EINVAL;
328 goto out;
329 }
330
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800331 mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Roland Dreiered878452005-06-27 14:36:45 -0700333 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (err)
335 goto out;
336 if (status) {
337 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
338 err = -EINVAL;
339 goto out;
340 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800341 BUG_ON(index < dev->limits.num_mgms);
342 mthca_free(&dev->mcg_table.alloc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800346 mutex_unlock(&dev->mcg_table.mutex);
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800347
Roland Dreiered878452005-06-27 14:36:45 -0700348 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return err;
350}
351
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800352int mthca_init_mcg_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int err;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800355 int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 err = mthca_alloc_init(&dev->mcg_table.alloc,
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800358 table_size,
359 table_size - 1,
360 dev->limits.num_mgms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (err)
362 return err;
363
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800364 mutex_init(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return 0;
367}
368
Roland Dreiere1f78682006-03-29 09:36:46 -0800369void mthca_cleanup_mcg_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 mthca_alloc_cleanup(&dev->mcg_table.alloc);
372}