xref: /OK3568_Linux_fs/kernel/drivers/infiniband/hw/mthca/mthca_mcg.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
5*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
6*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
7*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
8*4882a593Smuzhiyun  * OpenIB.org BSD license below:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
11*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
12*4882a593Smuzhiyun  *     conditions are met:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
15*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
16*4882a593Smuzhiyun  *        disclaimer.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
19*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
20*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
21*4882a593Smuzhiyun  *        provided with the distribution.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*4882a593Smuzhiyun  * SOFTWARE.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/string.h>
34*4882a593Smuzhiyun #include <linux/gfp.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include "mthca_dev.h"
37*4882a593Smuzhiyun #include "mthca_cmd.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun struct mthca_mgm {
40*4882a593Smuzhiyun 	__be32 next_gid_index;
41*4882a593Smuzhiyun 	u32    reserved[3];
42*4882a593Smuzhiyun 	u8     gid[16];
43*4882a593Smuzhiyun 	__be32 qp[MTHCA_QP_PER_MGM];
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun static const u8 zero_gid[16];	/* automatically initialized to 0 */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /*
49*4882a593Smuzhiyun  * Caller must hold MCG table semaphore.  gid and mgm parameters must
50*4882a593Smuzhiyun  * be properly aligned for command interface.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  *  Returns 0 unless a firmware command error occurs.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * If GID is found in MGM or MGM is empty, *index = *hash, *prev = -1
55*4882a593Smuzhiyun  * and *mgm holds MGM entry.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * if GID is found in AMGM, *index = index in AMGM, *prev = index of
58*4882a593Smuzhiyun  * previous entry in hash chain and *mgm holds AMGM entry.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * If no AMGM exists for given gid, *index = -1, *prev = index of last
61*4882a593Smuzhiyun  * entry in hash chain and *mgm holds end of hash chain.
62*4882a593Smuzhiyun  */
find_mgm(struct mthca_dev * dev,u8 * gid,struct mthca_mailbox * mgm_mailbox,u16 * hash,int * prev,int * index)63*4882a593Smuzhiyun static int find_mgm(struct mthca_dev *dev,
64*4882a593Smuzhiyun 		    u8 *gid, struct mthca_mailbox *mgm_mailbox,
65*4882a593Smuzhiyun 		    u16 *hash, int *prev, int *index)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct mthca_mailbox *mailbox;
68*4882a593Smuzhiyun 	struct mthca_mgm *mgm = mgm_mailbox->buf;
69*4882a593Smuzhiyun 	u8 *mgid;
70*4882a593Smuzhiyun 	int err;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
73*4882a593Smuzhiyun 	if (IS_ERR(mailbox))
74*4882a593Smuzhiyun 		return -ENOMEM;
75*4882a593Smuzhiyun 	mgid = mailbox->buf;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	memcpy(mgid, gid, 16);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	err = mthca_MGID_HASH(dev, mailbox, hash);
80*4882a593Smuzhiyun 	if (err) {
81*4882a593Smuzhiyun 		mthca_err(dev, "MGID_HASH failed (%d)\n", err);
82*4882a593Smuzhiyun 		goto out;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (0)
86*4882a593Smuzhiyun 		mthca_dbg(dev, "Hash for %pI6 is %04x\n", gid, *hash);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	*index = *hash;
89*4882a593Smuzhiyun 	*prev  = -1;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	do {
92*4882a593Smuzhiyun 		err = mthca_READ_MGM(dev, *index, mgm_mailbox);
93*4882a593Smuzhiyun 		if (err) {
94*4882a593Smuzhiyun 			mthca_err(dev, "READ_MGM failed (%d)\n", err);
95*4882a593Smuzhiyun 			goto out;
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		if (!memcmp(mgm->gid, zero_gid, 16)) {
99*4882a593Smuzhiyun 			if (*index != *hash) {
100*4882a593Smuzhiyun 				mthca_err(dev, "Found zero MGID in AMGM.\n");
101*4882a593Smuzhiyun 				err = -EINVAL;
102*4882a593Smuzhiyun 			}
103*4882a593Smuzhiyun 			goto out;
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 		if (!memcmp(mgm->gid, gid, 16))
107*4882a593Smuzhiyun 			goto out;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 		*prev = *index;
110*4882a593Smuzhiyun 		*index = be32_to_cpu(mgm->next_gid_index) >> 6;
111*4882a593Smuzhiyun 	} while (*index);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	*index = -1;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun  out:
116*4882a593Smuzhiyun 	mthca_free_mailbox(dev, mailbox);
117*4882a593Smuzhiyun 	return err;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
mthca_multicast_attach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)120*4882a593Smuzhiyun int mthca_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	struct mthca_dev *dev = to_mdev(ibqp->device);
123*4882a593Smuzhiyun 	struct mthca_mailbox *mailbox;
124*4882a593Smuzhiyun 	struct mthca_mgm *mgm;
125*4882a593Smuzhiyun 	u16 hash;
126*4882a593Smuzhiyun 	int index, prev;
127*4882a593Smuzhiyun 	int link = 0;
128*4882a593Smuzhiyun 	int i;
129*4882a593Smuzhiyun 	int err;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
132*4882a593Smuzhiyun 	if (IS_ERR(mailbox))
133*4882a593Smuzhiyun 		return PTR_ERR(mailbox);
134*4882a593Smuzhiyun 	mgm = mailbox->buf;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	mutex_lock(&dev->mcg_table.mutex);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
139*4882a593Smuzhiyun 	if (err)
140*4882a593Smuzhiyun 		goto out;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (index != -1) {
143*4882a593Smuzhiyun 		if (!memcmp(mgm->gid, zero_gid, 16))
144*4882a593Smuzhiyun 			memcpy(mgm->gid, gid->raw, 16);
145*4882a593Smuzhiyun 	} else {
146*4882a593Smuzhiyun 		link = 1;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 		index = mthca_alloc(&dev->mcg_table.alloc);
149*4882a593Smuzhiyun 		if (index == -1) {
150*4882a593Smuzhiyun 			mthca_err(dev, "No AMGM entries left\n");
151*4882a593Smuzhiyun 			err = -ENOMEM;
152*4882a593Smuzhiyun 			goto out;
153*4882a593Smuzhiyun 		}
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		err = mthca_READ_MGM(dev, index, mailbox);
156*4882a593Smuzhiyun 		if (err) {
157*4882a593Smuzhiyun 			mthca_err(dev, "READ_MGM failed (%d)\n", err);
158*4882a593Smuzhiyun 			goto out;
159*4882a593Smuzhiyun 		}
160*4882a593Smuzhiyun 		memset(mgm, 0, sizeof *mgm);
161*4882a593Smuzhiyun 		memcpy(mgm->gid, gid->raw, 16);
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	for (i = 0; i < MTHCA_QP_PER_MGM; ++i)
165*4882a593Smuzhiyun 		if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31))) {
166*4882a593Smuzhiyun 			mthca_dbg(dev, "QP %06x already a member of MGM\n",
167*4882a593Smuzhiyun 				  ibqp->qp_num);
168*4882a593Smuzhiyun 			err = 0;
169*4882a593Smuzhiyun 			goto out;
170*4882a593Smuzhiyun 		} else if (!(mgm->qp[i] & cpu_to_be32(1 << 31))) {
171*4882a593Smuzhiyun 			mgm->qp[i] = cpu_to_be32(ibqp->qp_num | (1 << 31));
172*4882a593Smuzhiyun 			break;
173*4882a593Smuzhiyun 		}
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (i == MTHCA_QP_PER_MGM) {
176*4882a593Smuzhiyun 		mthca_err(dev, "MGM at index %x is full.\n", index);
177*4882a593Smuzhiyun 		err = -ENOMEM;
178*4882a593Smuzhiyun 		goto out;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	err = mthca_WRITE_MGM(dev, index, mailbox);
182*4882a593Smuzhiyun 	if (err) {
183*4882a593Smuzhiyun 		mthca_err(dev, "WRITE_MGM failed %d\n", err);
184*4882a593Smuzhiyun 		err = -EINVAL;
185*4882a593Smuzhiyun 		goto out;
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (!link)
189*4882a593Smuzhiyun 		goto out;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	err = mthca_READ_MGM(dev, prev, mailbox);
192*4882a593Smuzhiyun 	if (err) {
193*4882a593Smuzhiyun 		mthca_err(dev, "READ_MGM failed %d\n", err);
194*4882a593Smuzhiyun 		goto out;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	mgm->next_gid_index = cpu_to_be32(index << 6);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	err = mthca_WRITE_MGM(dev, prev, mailbox);
200*4882a593Smuzhiyun 	if (err)
201*4882a593Smuzhiyun 		mthca_err(dev, "WRITE_MGM returned %d\n", err);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun  out:
204*4882a593Smuzhiyun 	if (err && link && index != -1) {
205*4882a593Smuzhiyun 		BUG_ON(index < dev->limits.num_mgms);
206*4882a593Smuzhiyun 		mthca_free(&dev->mcg_table.alloc, index);
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 	mutex_unlock(&dev->mcg_table.mutex);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	mthca_free_mailbox(dev, mailbox);
211*4882a593Smuzhiyun 	return err;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
mthca_multicast_detach(struct ib_qp * ibqp,union ib_gid * gid,u16 lid)214*4882a593Smuzhiyun int mthca_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	struct mthca_dev *dev = to_mdev(ibqp->device);
217*4882a593Smuzhiyun 	struct mthca_mailbox *mailbox;
218*4882a593Smuzhiyun 	struct mthca_mgm *mgm;
219*4882a593Smuzhiyun 	u16 hash;
220*4882a593Smuzhiyun 	int prev, index;
221*4882a593Smuzhiyun 	int i, loc;
222*4882a593Smuzhiyun 	int err;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
225*4882a593Smuzhiyun 	if (IS_ERR(mailbox))
226*4882a593Smuzhiyun 		return PTR_ERR(mailbox);
227*4882a593Smuzhiyun 	mgm = mailbox->buf;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	mutex_lock(&dev->mcg_table.mutex);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	err = find_mgm(dev, gid->raw, mailbox, &hash, &prev, &index);
232*4882a593Smuzhiyun 	if (err)
233*4882a593Smuzhiyun 		goto out;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (index == -1) {
236*4882a593Smuzhiyun 		mthca_err(dev, "MGID %pI6 not found\n", gid->raw);
237*4882a593Smuzhiyun 		err = -EINVAL;
238*4882a593Smuzhiyun 		goto out;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	for (loc = -1, i = 0; i < MTHCA_QP_PER_MGM; ++i) {
242*4882a593Smuzhiyun 		if (mgm->qp[i] == cpu_to_be32(ibqp->qp_num | (1 << 31)))
243*4882a593Smuzhiyun 			loc = i;
244*4882a593Smuzhiyun 		if (!(mgm->qp[i] & cpu_to_be32(1 << 31)))
245*4882a593Smuzhiyun 			break;
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (loc == -1) {
249*4882a593Smuzhiyun 		mthca_err(dev, "QP %06x not found in MGM\n", ibqp->qp_num);
250*4882a593Smuzhiyun 		err = -EINVAL;
251*4882a593Smuzhiyun 		goto out;
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	mgm->qp[loc]   = mgm->qp[i - 1];
255*4882a593Smuzhiyun 	mgm->qp[i - 1] = 0;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	err = mthca_WRITE_MGM(dev, index, mailbox);
258*4882a593Smuzhiyun 	if (err) {
259*4882a593Smuzhiyun 		mthca_err(dev, "WRITE_MGM returned %d\n", err);
260*4882a593Smuzhiyun 		goto out;
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	if (i != 1)
264*4882a593Smuzhiyun 		goto out;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	if (prev == -1) {
267*4882a593Smuzhiyun 		/* Remove entry from MGM */
268*4882a593Smuzhiyun 		int amgm_index_to_free = be32_to_cpu(mgm->next_gid_index) >> 6;
269*4882a593Smuzhiyun 		if (amgm_index_to_free) {
270*4882a593Smuzhiyun 			err = mthca_READ_MGM(dev, amgm_index_to_free,
271*4882a593Smuzhiyun 					     mailbox);
272*4882a593Smuzhiyun 			if (err) {
273*4882a593Smuzhiyun 				mthca_err(dev, "READ_MGM returned %d\n", err);
274*4882a593Smuzhiyun 				goto out;
275*4882a593Smuzhiyun 			}
276*4882a593Smuzhiyun 		} else
277*4882a593Smuzhiyun 			memset(mgm->gid, 0, 16);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 		err = mthca_WRITE_MGM(dev, index, mailbox);
280*4882a593Smuzhiyun 		if (err) {
281*4882a593Smuzhiyun 			mthca_err(dev, "WRITE_MGM returned %d\n", err);
282*4882a593Smuzhiyun 			goto out;
283*4882a593Smuzhiyun 		}
284*4882a593Smuzhiyun 		if (amgm_index_to_free) {
285*4882a593Smuzhiyun 			BUG_ON(amgm_index_to_free < dev->limits.num_mgms);
286*4882a593Smuzhiyun 			mthca_free(&dev->mcg_table.alloc, amgm_index_to_free);
287*4882a593Smuzhiyun 		}
288*4882a593Smuzhiyun 	} else {
289*4882a593Smuzhiyun 		/* Remove entry from AMGM */
290*4882a593Smuzhiyun 		int curr_next_index = be32_to_cpu(mgm->next_gid_index) >> 6;
291*4882a593Smuzhiyun 		err = mthca_READ_MGM(dev, prev, mailbox);
292*4882a593Smuzhiyun 		if (err) {
293*4882a593Smuzhiyun 			mthca_err(dev, "READ_MGM returned %d\n", err);
294*4882a593Smuzhiyun 			goto out;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 		mgm->next_gid_index = cpu_to_be32(curr_next_index << 6);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		err = mthca_WRITE_MGM(dev, prev, mailbox);
300*4882a593Smuzhiyun 		if (err) {
301*4882a593Smuzhiyun 			mthca_err(dev, "WRITE_MGM returned %d\n", err);
302*4882a593Smuzhiyun 			goto out;
303*4882a593Smuzhiyun 		}
304*4882a593Smuzhiyun 		BUG_ON(index < dev->limits.num_mgms);
305*4882a593Smuzhiyun 		mthca_free(&dev->mcg_table.alloc, index);
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun  out:
309*4882a593Smuzhiyun 	mutex_unlock(&dev->mcg_table.mutex);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	mthca_free_mailbox(dev, mailbox);
312*4882a593Smuzhiyun 	return err;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
mthca_init_mcg_table(struct mthca_dev * dev)315*4882a593Smuzhiyun int mthca_init_mcg_table(struct mthca_dev *dev)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	int err;
318*4882a593Smuzhiyun 	int table_size = dev->limits.num_mgms + dev->limits.num_amgms;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	err = mthca_alloc_init(&dev->mcg_table.alloc,
321*4882a593Smuzhiyun 			       table_size,
322*4882a593Smuzhiyun 			       table_size - 1,
323*4882a593Smuzhiyun 			       dev->limits.num_mgms);
324*4882a593Smuzhiyun 	if (err)
325*4882a593Smuzhiyun 		return err;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	mutex_init(&dev->mcg_table.mutex);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return 0;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
mthca_cleanup_mcg_table(struct mthca_dev * dev)332*4882a593Smuzhiyun void mthca_cleanup_mcg_table(struct mthca_dev *dev)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	mthca_alloc_cleanup(&dev->mcg_table.alloc);
335*4882a593Smuzhiyun }
336