]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/infiniband/hw/mthca/mthca_mcg.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[linux-2.6-omap-h63xx.git] / drivers / infiniband / hw / mthca / mthca_mcg.c
1 /*
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>
36
37 #include "mthca_dev.h"
38 #include "mthca_cmd.h"
39
40 enum {
41         MTHCA_QP_PER_MGM = 4 * (MTHCA_MGM_ENTRY_SIZE / 16 - 2)
42 };
43
44 struct mthca_mgm {
45         __be32 next_gid_index;
46         u32    reserved[3];
47         u8     gid[16];
48         __be32 qp[MTHCA_QP_PER_MGM];
49 };
50
51 static const u8 zero_gid[16];   /* automatically initialized to 0 */
52
53 /*
54  * Caller must hold MCG table semaphore.  gid and mgm parameters must
55  * be properly aligned for command interface.
56  *
57  *  Returns 0 unless a firmware command error occurs.
58  *
59  * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
60  * and *mgm holds MGM entry.
61  *
62  * if GID is found in AMGM, *index = index in AMGM, *prev = index of
63  * previous entry in hash chain and *mgm holds AMGM entry.
64  *
65  * If no AMGM exists for given gid, *index = -1, *prev = index of last
66  * entry in hash chain and *mgm holds end of hash chain.
67  */
68 static int find_mgm(struct mthca_dev *dev,
69                     u8 *gid, struct mthca_mailbox *mgm_mailbox,
70                     u16 *hash, int *prev, int *index)
71 {
72         struct mthca_mailbox *mailbox;
73         struct mthca_mgm *mgm = mgm_mailbox->buf;
74         u8 *mgid;
75         int err;
76         u8 status;
77
78         mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
79         if (IS_ERR(mailbox))
80                 return -ENOMEM;
81         mgid = mailbox->buf;
82
83         memcpy(mgid, gid, 16);
84
85         err = mthca_MGID_HASH(dev, mailbox, hash, &status);
86         if (err)
87                 goto out;
88         if (status) {
89                 mthca_err(dev, "MGID_HASH returned status %02x\n", status);
90                 err = -EINVAL;
91                 goto out;
92         }
93
94         if (0)
95                 mthca_dbg(dev, "Hash for %04x:%04x:%04x:%04x:"
96                           "%04x:%04x:%04x:%04x is %04x\n",
97                           be16_to_cpu(((__be16 *) gid)[0]),
98                           be16_to_cpu(((__be16 *) gid)[1]),
99                           be16_to_cpu(((__be16 *) gid)[2]),
100                           be16_to_cpu(((__be16 *) gid)[3]),
101                           be16_to_cpu(((__be16 *) gid)[4]),
102                           be16_to_cpu(((__be16 *) gid)[5]),
103                           be16_to_cpu(((__be16 *) gid)[6]),
104                           be16_to_cpu(((__be16 *) gid)[7]),
105                           *hash);
106
107         *index = *hash;
108         *prev  = -1;
109
110         do {
111                 err = mthca_READ_MGM(dev, *index, mgm_mailbox, &status);
112                 if (err)
113                         goto out;
114                 if (status) {
115                         mthca_err(dev, "READ_MGM returned status %02x\n", status);
116                         return -EINVAL;
117                 }
118
119                 if (!memcmp(mgm->gid, zero_gid, 16)) {
120                         if (*index != *hash) {
121                                 mthca_err(dev, "Found zero MGID in AMGM.\n");
122                                 err = -EINVAL;
123                         }
124                         goto out;
125                 }
126
127                 if (!memcmp(mgm->gid, gid, 16))
128                         goto out;
129
130                 *prev = *index;
131                 *index = be32_to_cpu(mgm->next_gid_index) >> 5;
132         } while (*index);
133
134         *index = -1;
135
136  out:
137         mthca_free_mailbox(dev, mailbox);
138         return err;
139 }
140
141 int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
142 {
143         struct mthca_dev *dev = to_mdev(ibqp->device);
144         struct mthca_mailbox *mailbox;
145         struct mthca_mgm *mgm;
146         u16 hash;
147         int index, prev;
148         int link = 0;
149         int i;
150         int err;
151         u8 status;
152
153         mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
154         if (IS_ERR(mailbox))
155                 return PTR_ERR(mailbox);
156         mgm = mailbox->buf;
157
158         if (down_interruptible(&dev->mcg_table.sem))
159                 return -EINTR;
160
161         err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
162         if (err)
163                 goto out;
164
165         if (index != -1) {
166                 if (!memcmp(mgm->gid, zero_gid, 16))
167                         memcpy(mgm->gid, gid->raw, 16);
168         } else {
169                 link = 1;
170
171                 index = mthca_alloc(&dev->mcg_table.alloc);
172                 if (index == -1) {
173                         mthca_err(dev, "No AMGM entries left\n");
174                         err = -ENOMEM;
175                         goto out;
176                 }
177
178                 err = mthca_READ_MGM(dev, index, mailbox, &status);
179                 if (err)
180                         goto out;
181                 if (status) {
182                         mthca_err(dev, "READ_MGM returned status %02x\n", status);
183                         err = -EINVAL;
184                         goto out;
185                 }
186
187                 memcpy(mgm->gid, gid->raw, 16);
188                 mgm->next_gid_index = 0;
189         }
190
191         for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
192                 if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
193                         mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
194                         break;
195                 }
196
197         if (i == MTHCA_QP_PER_MGM) {
198                 mthca_err(dev, "MGM at index %x is full.\n", index);
199                 err = -ENOMEM;
200                 goto out;
201         }
202
203         err = mthca_WRITE_MGM(dev, index, mailbox, &status);
204         if (err)
205                 goto out;
206         if (status) {
207                 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
208                 err = -EINVAL;
209         }
210
211         if (!link)
212                 goto out;
213
214         err = mthca_READ_MGM(dev, prev, mailbox, &status);
215         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
223         mgm->next_gid_index = cpu_to_be32(index << 5);
224
225         err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
226         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:
234         up(&dev->mcg_table.sem);
235         mthca_free_mailbox(dev, mailbox);
236         return err;
237 }
238
239 int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
240 {
241         struct mthca_dev *dev = to_mdev(ibqp->device);
242         struct mthca_mailbox *mailbox;
243         struct mthca_mgm *mgm;
244         u16 hash;
245         int prev, index;
246         int i, loc;
247         int err;
248         u8 status;
249
250         mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
251         if (IS_ERR(mailbox))
252                 return PTR_ERR(mailbox);
253         mgm = mailbox->buf;
254
255         if (down_interruptible(&dev->mcg_table.sem))
256                 return -EINTR;
257
258         err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
259         if (err)
260                 goto out;
261
262         if (index == -1) {
263                 mthca_err(dev, "MGID %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
264                           "not found\n",
265                           be16_to_cpu(((__be16 *) gid->raw)[0]),
266                           be16_to_cpu(((__be16 *) gid->raw)[1]),
267                           be16_to_cpu(((__be16 *) gid->raw)[2]),
268                           be16_to_cpu(((__be16 *) gid->raw)[3]),
269                           be16_to_cpu(((__be16 *) gid->raw)[4]),
270                           be16_to_cpu(((__be16 *) gid->raw)[5]),
271                           be16_to_cpu(((__be16 *) gid->raw)[6]),
272                           be16_to_cpu(((__be16 *) gid->raw)[7]));
273                 err = -EINVAL;
274                 goto out;
275         }
276
277         for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
278                 if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
279                         loc = i;
280                 if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
281                         break;
282         }
283
284         if (loc == -1) {
285                 mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
286                 err = -EINVAL;
287                 goto out;
288         }
289
290         mgm->qp[loc]   = mgm->qp[i - 1];
291         mgm->qp[i - 1] = 0;
292
293         err = mthca_WRITE_MGM(dev, index, mailbox, &status);
294         if (err)
295                 goto out;
296         if (status) {
297                 mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
298                 err = -EINVAL;
299                 goto out;
300         }
301
302         if (i != 1)
303                 goto out;
304
305         goto out;
306
307         if (prev == -1) {
308                 /* Remove entry from MGM */
309                 if (be32_to_cpu(mgm->next_gid_index) >> 5) {
310                         err = mthca_READ_MGM(dev,
311                                              be32_to_cpu(mgm->next_gid_index) >> 5,
312                                              mailbox, &status);
313                         if (err)
314                                 goto out;
315                         if (status) {
316                                 mthca_err(dev, "READ_MGM returned status %02x\n",
317                                           status);
318                                 err = -EINVAL;
319                                 goto out;
320                         }
321                 } else
322                         memset(mgm->gid, 0, 16);
323
324                 err = mthca_WRITE_MGM(dev, index, mailbox, &status);
325                 if (err)
326                         goto out;
327                 if (status) {
328                         mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
329                         err = -EINVAL;
330                         goto out;
331                 }
332         } else {
333                 /* Remove entry from AMGM */
334                 index = be32_to_cpu(mgm->next_gid_index) >> 5;
335                 err = mthca_READ_MGM(dev, prev, mailbox, &status);
336                 if (err)
337                         goto out;
338                 if (status) {
339                         mthca_err(dev, "READ_MGM returned status %02x\n", status);
340                         err = -EINVAL;
341                         goto out;
342                 }
343
344                 mgm->next_gid_index = cpu_to_be32(index << 5);
345
346                 err = mthca_WRITE_MGM(dev, prev, mailbox, &status);
347                 if (err)
348                         goto out;
349                 if (status) {
350                         mthca_err(dev, "WRITE_MGM returned status %02x\n", status);
351                         err = -EINVAL;
352                         goto out;
353                 }
354         }
355
356  out:
357         up(&dev->mcg_table.sem);
358         mthca_free_mailbox(dev, mailbox);
359         return err;
360 }
361
362 int __devinit mthca_init_mcg_table(struct mthca_dev *dev)
363 {
364         int err;
365
366         err = mthca_alloc_init(&dev->mcg_table.alloc,
367                                dev->limits.num_amgms,
368                                dev->limits.num_amgms - 1,
369                                0);
370         if (err)
371                 return err;
372
373         init_MUTEX(&dev->mcg_table.sem);
374
375         return 0;
376 }
377
378 void __devexit mthca_cleanup_mcg_table(struct mthca_dev *dev)
379 {
380         mthca_alloc_cleanup(&dev->mcg_table.alloc);
381 }