blob: 77bc6c746f43ac2783c837194b2b29ffd82c416c [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.
31 *
32 * $Id: mthca_mcg.c 1349 2004-12-16 21:09:43Z roland $
33 */
34
35#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080036#include <linux/string.h>
37#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include "mthca_dev.h"
40#include "mthca_cmd.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042struct mthca_mgm {
Sean Hefty97f52eb2005-08-13 21:05:57 -070043 __be32 next_gid_index;
44 u32 reserved[3];
45 u8 gid[16];
46 __be32 qp[MTHCA_QP_PER_MGM];
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49static const u8 zero_gid[16]; /* automatically initialized to 0 */
50
51/*
52 * Caller must hold MCG table semaphore. gid and mgm parameters must
53 * be properly aligned for command interface.
54 *
55 * Returns 0 unless a firmware command error occurs.
56 *
57 * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
58 * and *mgm holds MGM entry.
59 *
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.
62 *
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.
65 */
66static int find_mgm(struct mthca_dev *dev,
Roland Dreiered878452005-06-27 14:36:45 -070067 u8 *gid, struct mthca_mailbox *mgm_mailbox,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 u16 *hash, int *prev, int *index)
69{
Roland Dreiered878452005-06-27 14:36:45 -070070 struct mthca_mailbox *mailbox;
71 struct mthca_mgm *mgm = mgm_mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u8 *mgid;
73 int err;
74 u8 status;
75
Roland Dreiered878452005-06-27 14:36:45 -070076 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
77 if (IS_ERR(mailbox))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -ENOMEM;
Roland Dreiered878452005-06-27 14:36:45 -070079 mgid = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 memcpy(mgid, gid, 16);
82
Roland Dreiered878452005-06-27 14:36:45 -070083 err = mthca_MGID_HASH(dev, mailbox, hash, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (err)
85 goto out;
86 if (status) {
87 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
88 err = -EINVAL;
89 goto out;
90 }
91
92 if (0)
93 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
94 "%04x:%04x:%04x:%04x is %04x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -070095 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]),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *hash);
104
105 *index = *hash;
106 *prev = -1;
107
108 do {
Roland Dreiered878452005-06-27 14:36:45 -0700109 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (err)
111 goto out;
112 if (status) {
113 mthca_err(dev, "READ_MGM returned status %02x\n", status);
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800114 err = -EINVAL;
115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 if (!memcmp(mgm->gid, zero_gid, 16)) {
119 if (*index != *hash) {
120 mthca_err(dev, "Found zero MGID in AMGM.\n");
121 err = -EINVAL;
122 }
123 goto out;
124 }
125
126 if (!memcmp(mgm->gid, gid, 16))
127 goto out;
128
129 *prev = *index;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800130 *index = be32_to_cpu(mgm->next_gid_index) >> 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } while (*index);
132
133 *index = -1;
134
135 out:
Roland Dreiered878452005-06-27 14:36:45 -0700136 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return err;
138}
139
140int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
141{
142 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700143 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct mthca_mgm *mgm;
145 u16 hash;
146 int index, prev;
147 int link = 0;
148 int i;
149 int err;
150 u8 status;
151
Roland Dreiered878452005-06-27 14:36:45 -0700152 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
153 if (IS_ERR(mailbox))
154 return PTR_ERR(mailbox);
155 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800157 if (down_interruptible(&dev->mcg_table.sem)) {
158 err = -EINTR;
159 goto err_sem;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Roland Dreiered878452005-06-27 14:36:45 -0700162 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (err)
164 goto out;
165
166 if (index != -1) {
167 if (!memcmp(mgm->gid, zero_gid, 16))
168 memcpy(mgm->gid, gid->raw, 16);
169 } else {
170 link = 1;
171
172 index = mthca_alloc(&dev->mcg_table.alloc);
173 if (index == -1) {
174 mthca_err(dev, "No AMGM entries left\n");
175 err = -ENOMEM;
176 goto out;
177 }
178
Roland Dreiered878452005-06-27 14:36:45 -0700179 err = mthca_READ_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (err)
181 goto out;
182 if (status) {
183 mthca_err(dev, "READ_MGM returned status %02x\n", status);
184 err = -EINVAL;
185 goto out;
186 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800187 memset(mgm, 0, sizeof *mgm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 memcpy(mgm->gid, gid->raw, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
Jack Morgenstein7150bf82005-10-18 14:46:38 -0700192 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
193 mthca_dbg(dev, "QP %06x already a member of MGM\n",
194 ibqp->qp_num);
195 err = 0;
196 goto out;
197 } else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
199 break;
200 }
201
202 if (i == MTHCA_QP_PER_MGM) {
203 mthca_err(dev, "MGM at index %x is full.\n", index);
204 err = -ENOMEM;
205 goto out;
206 }
207
Roland Dreiered878452005-06-27 14:36:45 -0700208 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (err)
210 goto out;
211 if (status) {
212 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
213 err = -EINVAL;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 if (!link)
218 goto out;
219
Roland Dreiered878452005-06-27 14:36:45 -0700220 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (err)
222 goto out;
223 if (status) {
224 mthca_err(dev, "READ_MGM returned status %02x\n", status);
225 err = -EINVAL;
226 goto out;
227 }
228
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800229 mgm->next_gid_index = cpu_to_be32(index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Roland Dreiered878452005-06-27 14:36:45 -0700231 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (err)
233 goto out;
234 if (status) {
235 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
236 err = -EINVAL;
237 }
238
239 out:
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800240 if (err && link && index != -1) {
241 BUG_ON(index < dev->limits.num_mgms);
242 mthca_free(&dev->mcg_table.alloc, index);
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 up(&dev->mcg_table.sem);
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800245 err_sem:
Roland Dreiered878452005-06-27 14:36:45 -0700246 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return err;
248}
249
250int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
251{
252 struct mthca_dev *dev = to_mdev(ibqp->device);
Roland Dreiered878452005-06-27 14:36:45 -0700253 struct mthca_mailbox *mailbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct mthca_mgm *mgm;
255 u16 hash;
256 int prev, index;
257 int i, loc;
258 int err;
259 u8 status;
260
Roland Dreiered878452005-06-27 14:36:45 -0700261 mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
262 if (IS_ERR(mailbox))
263 return PTR_ERR(mailbox);
264 mgm = mailbox->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800266 if (down_interruptible(&dev->mcg_table.sem)) {
267 err = -EINTR;
268 goto err_sem;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Roland Dreiered878452005-06-27 14:36:45 -0700271 err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (err)
273 goto out;
274
275 if (index == -1) {
276 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
277 "not found\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700278 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]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 err = -EINVAL;
287 goto out;
288 }
289
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)))
292 loc = i;
293 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
294 break;
295 }
296
297 if (loc == -1) {
298 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
299 err = -EINVAL;
300 goto out;
301 }
302
303 mgm->qp[loc] = mgm->qp[i - 1];
304 mgm->qp[i - 1] = 0;
305
Roland Dreiered878452005-06-27 14:36:45 -0700306 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (err)
308 goto out;
309 if (status) {
310 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
311 err = -EINVAL;
312 goto out;
313 }
314
315 if (i != 1)
316 goto out;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (prev == -1) {
319 /* Remove entry from MGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800320 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,
Roland Dreiered878452005-06-27 14:36:45 -0700323 mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (err)
325 goto out;
326 if (status) {
327 mthca_err(dev, "READ_MGM returned status %02x\n",
328 status);
329 err = -EINVAL;
330 goto out;
331 }
332 } else
333 memset(mgm->gid, 0, 16);
334
Roland Dreiered878452005-06-27 14:36:45 -0700335 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (err)
337 goto out;
338 if (status) {
339 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
340 err = -EINVAL;
341 goto out;
342 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800343 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);
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
348 /* Remove entry from AMGM */
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800349 int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
Roland Dreiered878452005-06-27 14:36:45 -0700350 err = mthca_READ_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (err)
352 goto out;
353 if (status) {
354 mthca_err(dev, "READ_MGM returned status %02x\n", status);
355 err = -EINVAL;
356 goto out;
357 }
358
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800359 mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Roland Dreiered878452005-06-27 14:36:45 -0700361 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (err)
363 goto out;
364 if (status) {
365 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
366 err = -EINVAL;
367 goto out;
368 }
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800369 BUG_ON(index < dev->limits.num_mgms);
370 mthca_free(&dev->mcg_table.alloc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 out:
374 up(&dev->mcg_table.sem);
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800375 err_sem:
Roland Dreiered878452005-06-27 14:36:45 -0700376 mthca_free_mailbox(dev, mailbox);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return err;
378}
379
380int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
381{
382 int err;
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800383 int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 err = mthca_alloc_init(&dev->mcg_table.alloc,
Jack Morgenstein5ceb7452006-01-06 13:11:07 -0800386 table_size,
387 table_size - 1,
388 dev->limits.num_mgms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (err)
390 return err;
391
392 init_MUTEX(&dev->mcg_table.sem);
393
394 return 0;
395}
396
397void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
398{
399 mthca_alloc_cleanup(&dev->mcg_table.alloc);
400}