blob: 3f5f948792089a3bf33440794f263bf35dc33d6e [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>
34#include <linux/slab.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)
90 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
91 "%04x:%04x:%04x:%04x is %04x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -070092 be16_to_cpu(((__be16 *) gid)[0]),
93 be16_to_cpu(((__be16 *) gid)[1]),
94 be16_to_cpu(((__be16 *) gid)[2]),
95 be16_to_cpu(((__be16 *) gid)[3]),
96 be16_to_cpu(((__be16 *) gid)[4]),
97 be16_to_cpu(((__be16 *) gid)[5]),
98 be16_to_cpu(((__be16 *) gid)[6]),
99 be16_to_cpu(((__be16 *) gid)[7]),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *hash);
101
102 *index = *hash;
103 *prev = -1;
104
105 do {
Roland Dreiered878452005-06-27 14:36:45 -0700106 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (err)
108 goto out;
109 if (status) {
110 mthca_err(dev, "READ_MGM returned status %02x\n", status);
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800111 err = -EINVAL;
112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
115 if (!memcmp(mgm->gid, zero_gid, 16)) {
116 if (*index != *hash) {
117 mthca_err(dev, "Found zero MGID in AMGM.\n");
118 err = -EINVAL;
119 }
120 goto out;
121 }
122
123 if (!memcmp(mgm->gid, gid, 16))
124 goto out;
125
126 *prev = *index;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800127 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 } while (*index);
129
130 *index = -1;
131
132 out:
Roland Dreiered878452005-06-27 14:36:45 -0700133 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return err;
135}
136
137int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
138{
139 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700140 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct mthca_mgm *mgm;
142 u16 hash;
143 int index, prev;
144 int link = 0;
145 int i;
146 int err;
147 u8 status;
148
Roland Dreiered878452005-06-27 14:36:45 -0700149 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
150 if (IS_ERR(mailbox))
151 return PTR_ERR(mailbox);
152 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800154 mutex_lock(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Roland Dreiered878452005-06-27 14:36:45 -0700156 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (err)
158 goto out;
159
160 if (index != -1) {
161 if (!memcmp(mgm->gid, zero_gid, 16))
162 memcpy(mgm->gid, gid->raw, 16);
163 } else {
164 link = 1;
165
166 index = mthca_alloc(&dev->mcg_table.alloc);
167 if (index == -1) {
168 mthca_err(dev, "No AMGM entries left\n");
169 err = -ENOMEM;
170 goto out;
171 }
172
Roland Dreiered878452005-06-27 14:36:45 -0700173 err = mthca_READ_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (err)
175 goto out;
176 if (status) {
177 mthca_err(dev, "READ_MGM returned status %02x\n", status);
178 err = -EINVAL;
179 goto out;
180 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800181 memset(mgm, 0, sizeof *mgm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 memcpy(mgm->gid, gid->raw, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
185 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
Jack Morgenstein7150bf82005-10-18 14:46:38 -0700186 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
Roland Dreier2fa5e2e2006-02-01 13:38:24 -0800187 mthca_dbg(dev, "QP %06x already a member of MGM\n",
Jack Morgenstein7150bf82005-10-18 14:46:38 -0700188 ibqp->qp_num);
189 err = 0;
190 goto out;
191 } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
193 break;
194 }
195
196 if (i == MTHCA_QP_PER_MGM) {
197 mthca_err(dev, "MGM at index %x is full.\n", index);
198 err = -ENOMEM;
199 goto out;
200 }
201
Roland Dreiered878452005-06-27 14:36:45 -0700202 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (err)
204 goto out;
205 if (status) {
206 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
207 err = -EINVAL;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 if (!link)
212 goto out;
213
Roland Dreiered878452005-06-27 14:36:45 -0700214 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (err)
216 goto out;
217 if (status) {
218 mthca_err(dev, "READ_MGM returned status %02x\n", status);
219 err = -EINVAL;
220 goto out;
221 }
222
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800223 mgm->next_gid_index = cpu_to_be32(index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Roland Dreiered878452005-06-27 14:36:45 -0700225 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (err)
227 goto out;
228 if (status) {
229 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
230 err = -EINVAL;
231 }
232
233 out:
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800234 if (err && link && index != -1) {
235 BUG_ON(index < dev->limits.num_mgms);
236 mthca_free(&dev->mcg_table.alloc, index);
237 }
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800238 mutex_unlock(&dev->mcg_table.mutex);
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800239
Roland Dreiered878452005-06-27 14:36:45 -0700240 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return err;
242}
243
244int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
245{
246 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700247 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct mthca_mgm *mgm;
249 u16 hash;
250 int prev, index;
251 int i, loc;
252 int err;
253 u8 status;
254
Roland Dreiered878452005-06-27 14:36:45 -0700255 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
256 if (IS_ERR(mailbox))
257 return PTR_ERR(mailbox);
258 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800260 mutex_lock(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Roland Dreiered878452005-06-27 14:36:45 -0700262 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (err)
264 goto out;
265
266 if (index == -1) {
267 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
268 "not found\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700269 be16_to_cpu(((__be16 *) gid->raw)[0]),
270 be16_to_cpu(((__be16 *) gid->raw)[1]),
271 be16_to_cpu(((__be16 *) gid->raw)[2]),
272 be16_to_cpu(((__be16 *) gid->raw)[3]),
273 be16_to_cpu(((__be16 *) gid->raw)[4]),
274 be16_to_cpu(((__be16 *) gid->raw)[5]),
275 be16_to_cpu(((__be16 *) gid->raw)[6]),
276 be16_to_cpu(((__be16 *) gid->raw)[7]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 err = -EINVAL;
278 goto out;
279 }
280
281 for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
282 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
283 loc = i;
284 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
285 break;
286 }
287
288 if (loc == -1) {
289 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
290 err = -EINVAL;
291 goto out;
292 }
293
294 mgm->qp[loc] = mgm->qp[i - 1];
295 mgm->qp[i - 1] = 0;
296
Roland Dreiered878452005-06-27 14:36:45 -0700297 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (err)
299 goto out;
300 if (status) {
301 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
302 err = -EINVAL;
303 goto out;
304 }
305
306 if (i != 1)
307 goto out;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (prev == -1) {
310 /* Remove entry from MGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800311 int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
312 if (amgm_index_to_free) {
313 err = mthca_READ_MGM(dev, amgm_index_to_free,
Roland Dreiered878452005-06-27 14:36:45 -0700314 mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (err)
316 goto out;
317 if (status) {
318 mthca_err(dev, "READ_MGM returned status %02x\n",
319 status);
320 err = -EINVAL;
321 goto out;
322 }
323 } else
324 memset(mgm->gid, 0, 16);
325
Roland Dreiered878452005-06-27 14:36:45 -0700326 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (err)
328 goto out;
329 if (status) {
330 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
331 err = -EINVAL;
332 goto out;
333 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800334 if (amgm_index_to_free) {
335 BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
336 mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 } else {
339 /* Remove entry from AMGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800340 int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
Roland Dreiered878452005-06-27 14:36:45 -0700341 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (err)
343 goto out;
344 if (status) {
345 mthca_err(dev, "READ_MGM returned status %02x\n", status);
346 err = -EINVAL;
347 goto out;
348 }
349
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800350 mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Roland Dreiered878452005-06-27 14:36:45 -0700352 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (err)
354 goto out;
355 if (status) {
356 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
357 err = -EINVAL;
358 goto out;
359 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800360 BUG_ON(index < dev->limits.num_mgms);
361 mthca_free(&dev->mcg_table.alloc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 out:
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800365 mutex_unlock(&dev->mcg_table.mutex);
Michael S. Tsirkine3aa31c2006-01-30 16:22:29 -0800366
Roland Dreiered878452005-06-27 14:36:45 -0700367 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return err;
369}
370
Roland Dreierf4f3d0f2006-11-29 15:33:06 -0800371int mthca_init_mcg_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 int err;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800374 int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 err = mthca_alloc_init(&dev->mcg_table.alloc,
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800377 table_size,
378 table_size - 1,
379 dev->limits.num_mgms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (err)
381 return err;
382
Roland Dreierfd9cfdd2006-01-30 16:45:11 -0800383 mutex_init(&dev->mcg_table.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return 0;
386}
387
Roland Dreiere1f78682006-03-29 09:36:46 -0800388void mthca_cleanup_mcg_table(struct mthca_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 mthca_alloc_cleanup(&dev->mcg_table.alloc);
391}