treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / infiniband / sw / rxe / rxe_mcast.c
blob522a7942c56cb788ac73db7910335f3caaa16af9
1 /*
2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
34 #include "rxe.h"
35 #include "rxe_loc.h"
37 int rxe_mcast_get_grp(struct rxe_dev *rxe, union ib_gid *mgid,
38 struct rxe_mc_grp **grp_p)
40 int err;
41 struct rxe_mc_grp *grp;
43 if (rxe->attr.max_mcast_qp_attach == 0) {
44 err = -EINVAL;
45 goto err1;
48 grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid);
49 if (grp)
50 goto done;
52 grp = rxe_alloc(&rxe->mc_grp_pool);
53 if (!grp) {
54 err = -ENOMEM;
55 goto err1;
58 INIT_LIST_HEAD(&grp->qp_list);
59 spin_lock_init(&grp->mcg_lock);
60 grp->rxe = rxe;
62 rxe_add_key(grp, mgid);
64 err = rxe_mcast_add(rxe, mgid);
65 if (err)
66 goto err2;
68 done:
69 *grp_p = grp;
70 return 0;
72 err2:
73 rxe_drop_ref(grp);
74 err1:
75 return err;
78 int rxe_mcast_add_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp,
79 struct rxe_mc_grp *grp)
81 int err;
82 struct rxe_mc_elem *elem;
84 /* check to see of the qp is already a member of the group */
85 spin_lock_bh(&qp->grp_lock);
86 spin_lock_bh(&grp->mcg_lock);
87 list_for_each_entry(elem, &grp->qp_list, qp_list) {
88 if (elem->qp == qp) {
89 err = 0;
90 goto out;
94 if (grp->num_qp >= rxe->attr.max_mcast_qp_attach) {
95 err = -ENOMEM;
96 goto out;
99 elem = rxe_alloc(&rxe->mc_elem_pool);
100 if (!elem) {
101 err = -ENOMEM;
102 goto out;
105 /* each qp holds a ref on the grp */
106 rxe_add_ref(grp);
108 grp->num_qp++;
109 elem->qp = qp;
110 elem->grp = grp;
112 list_add(&elem->qp_list, &grp->qp_list);
113 list_add(&elem->grp_list, &qp->grp_list);
115 err = 0;
116 out:
117 spin_unlock_bh(&grp->mcg_lock);
118 spin_unlock_bh(&qp->grp_lock);
119 return err;
122 int rxe_mcast_drop_grp_elem(struct rxe_dev *rxe, struct rxe_qp *qp,
123 union ib_gid *mgid)
125 struct rxe_mc_grp *grp;
126 struct rxe_mc_elem *elem, *tmp;
128 grp = rxe_pool_get_key(&rxe->mc_grp_pool, mgid);
129 if (!grp)
130 goto err1;
132 spin_lock_bh(&qp->grp_lock);
133 spin_lock_bh(&grp->mcg_lock);
135 list_for_each_entry_safe(elem, tmp, &grp->qp_list, qp_list) {
136 if (elem->qp == qp) {
137 list_del(&elem->qp_list);
138 list_del(&elem->grp_list);
139 grp->num_qp--;
141 spin_unlock_bh(&grp->mcg_lock);
142 spin_unlock_bh(&qp->grp_lock);
143 rxe_drop_ref(elem);
144 rxe_drop_ref(grp); /* ref held by QP */
145 rxe_drop_ref(grp); /* ref from get_key */
146 return 0;
150 spin_unlock_bh(&grp->mcg_lock);
151 spin_unlock_bh(&qp->grp_lock);
152 rxe_drop_ref(grp); /* ref from get_key */
153 err1:
154 return -EINVAL;
157 void rxe_drop_all_mcast_groups(struct rxe_qp *qp)
159 struct rxe_mc_grp *grp;
160 struct rxe_mc_elem *elem;
162 while (1) {
163 spin_lock_bh(&qp->grp_lock);
164 if (list_empty(&qp->grp_list)) {
165 spin_unlock_bh(&qp->grp_lock);
166 break;
168 elem = list_first_entry(&qp->grp_list, struct rxe_mc_elem,
169 grp_list);
170 list_del(&elem->grp_list);
171 spin_unlock_bh(&qp->grp_lock);
173 grp = elem->grp;
174 spin_lock_bh(&grp->mcg_lock);
175 list_del(&elem->qp_list);
176 grp->num_qp--;
177 spin_unlock_bh(&grp->mcg_lock);
178 rxe_drop_ref(grp);
179 rxe_drop_ref(elem);
183 void rxe_mc_cleanup(struct rxe_pool_entry *arg)
185 struct rxe_mc_grp *grp = container_of(arg, typeof(*grp), pelem);
186 struct rxe_dev *rxe = grp->rxe;
188 rxe_drop_key(grp);
189 rxe_mcast_delete(rxe, &grp->mgid);