These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / rdma / hfi1 / verbs_mcast.c
1 /*
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Corporation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Copyright(c) 2015 Intel Corporation.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 #include <linux/rculist.h>
52
53 #include "hfi.h"
54
55 /**
56  * mcast_qp_alloc - alloc a struct to link a QP to mcast GID struct
57  * @qp: the QP to link
58  */
59 static struct hfi1_mcast_qp *mcast_qp_alloc(struct hfi1_qp *qp)
60 {
61         struct hfi1_mcast_qp *mqp;
62
63         mqp = kmalloc(sizeof(*mqp), GFP_KERNEL);
64         if (!mqp)
65                 goto bail;
66
67         mqp->qp = qp;
68         atomic_inc(&qp->refcount);
69
70 bail:
71         return mqp;
72 }
73
74 static void mcast_qp_free(struct hfi1_mcast_qp *mqp)
75 {
76         struct hfi1_qp *qp = mqp->qp;
77
78         /* Notify hfi1_destroy_qp() if it is waiting. */
79         if (atomic_dec_and_test(&qp->refcount))
80                 wake_up(&qp->wait);
81
82         kfree(mqp);
83 }
84
85 /**
86  * mcast_alloc - allocate the multicast GID structure
87  * @mgid: the multicast GID
88  *
89  * A list of QPs will be attached to this structure.
90  */
91 static struct hfi1_mcast *mcast_alloc(union ib_gid *mgid)
92 {
93         struct hfi1_mcast *mcast;
94
95         mcast = kmalloc(sizeof(*mcast), GFP_KERNEL);
96         if (!mcast)
97                 goto bail;
98
99         mcast->mgid = *mgid;
100         INIT_LIST_HEAD(&mcast->qp_list);
101         init_waitqueue_head(&mcast->wait);
102         atomic_set(&mcast->refcount, 0);
103         mcast->n_attached = 0;
104
105 bail:
106         return mcast;
107 }
108
109 static void mcast_free(struct hfi1_mcast *mcast)
110 {
111         struct hfi1_mcast_qp *p, *tmp;
112
113         list_for_each_entry_safe(p, tmp, &mcast->qp_list, list)
114                 mcast_qp_free(p);
115
116         kfree(mcast);
117 }
118
119 /**
120  * hfi1_mcast_find - search the global table for the given multicast GID
121  * @ibp: the IB port structure
122  * @mgid: the multicast GID to search for
123  *
124  * Returns NULL if not found.
125  *
126  * The caller is responsible for decrementing the reference count if found.
127  */
128 struct hfi1_mcast *hfi1_mcast_find(struct hfi1_ibport *ibp, union ib_gid *mgid)
129 {
130         struct rb_node *n;
131         unsigned long flags;
132         struct hfi1_mcast *mcast;
133
134         spin_lock_irqsave(&ibp->lock, flags);
135         n = ibp->mcast_tree.rb_node;
136         while (n) {
137                 int ret;
138
139                 mcast = rb_entry(n, struct hfi1_mcast, rb_node);
140
141                 ret = memcmp(mgid->raw, mcast->mgid.raw,
142                              sizeof(union ib_gid));
143                 if (ret < 0)
144                         n = n->rb_left;
145                 else if (ret > 0)
146                         n = n->rb_right;
147                 else {
148                         atomic_inc(&mcast->refcount);
149                         spin_unlock_irqrestore(&ibp->lock, flags);
150                         goto bail;
151                 }
152         }
153         spin_unlock_irqrestore(&ibp->lock, flags);
154
155         mcast = NULL;
156
157 bail:
158         return mcast;
159 }
160
161 /**
162  * mcast_add - insert mcast GID into table and attach QP struct
163  * @mcast: the mcast GID table
164  * @mqp: the QP to attach
165  *
166  * Return zero if both were added.  Return EEXIST if the GID was already in
167  * the table but the QP was added.  Return ESRCH if the QP was already
168  * attached and neither structure was added.
169  */
170 static int mcast_add(struct hfi1_ibdev *dev, struct hfi1_ibport *ibp,
171                      struct hfi1_mcast *mcast, struct hfi1_mcast_qp *mqp)
172 {
173         struct rb_node **n = &ibp->mcast_tree.rb_node;
174         struct rb_node *pn = NULL;
175         int ret;
176
177         spin_lock_irq(&ibp->lock);
178
179         while (*n) {
180                 struct hfi1_mcast *tmcast;
181                 struct hfi1_mcast_qp *p;
182
183                 pn = *n;
184                 tmcast = rb_entry(pn, struct hfi1_mcast, rb_node);
185
186                 ret = memcmp(mcast->mgid.raw, tmcast->mgid.raw,
187                              sizeof(union ib_gid));
188                 if (ret < 0) {
189                         n = &pn->rb_left;
190                         continue;
191                 }
192                 if (ret > 0) {
193                         n = &pn->rb_right;
194                         continue;
195                 }
196
197                 /* Search the QP list to see if this is already there. */
198                 list_for_each_entry_rcu(p, &tmcast->qp_list, list) {
199                         if (p->qp == mqp->qp) {
200                                 ret = ESRCH;
201                                 goto bail;
202                         }
203                 }
204                 if (tmcast->n_attached == hfi1_max_mcast_qp_attached) {
205                         ret = ENOMEM;
206                         goto bail;
207                 }
208
209                 tmcast->n_attached++;
210
211                 list_add_tail_rcu(&mqp->list, &tmcast->qp_list);
212                 ret = EEXIST;
213                 goto bail;
214         }
215
216         spin_lock(&dev->n_mcast_grps_lock);
217         if (dev->n_mcast_grps_allocated == hfi1_max_mcast_grps) {
218                 spin_unlock(&dev->n_mcast_grps_lock);
219                 ret = ENOMEM;
220                 goto bail;
221         }
222
223         dev->n_mcast_grps_allocated++;
224         spin_unlock(&dev->n_mcast_grps_lock);
225
226         mcast->n_attached++;
227
228         list_add_tail_rcu(&mqp->list, &mcast->qp_list);
229
230         atomic_inc(&mcast->refcount);
231         rb_link_node(&mcast->rb_node, pn, n);
232         rb_insert_color(&mcast->rb_node, &ibp->mcast_tree);
233
234         ret = 0;
235
236 bail:
237         spin_unlock_irq(&ibp->lock);
238
239         return ret;
240 }
241
242 int hfi1_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
243 {
244         struct hfi1_qp *qp = to_iqp(ibqp);
245         struct hfi1_ibdev *dev = to_idev(ibqp->device);
246         struct hfi1_ibport *ibp;
247         struct hfi1_mcast *mcast;
248         struct hfi1_mcast_qp *mqp;
249         int ret;
250
251         if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET) {
252                 ret = -EINVAL;
253                 goto bail;
254         }
255
256         /*
257          * Allocate data structures since its better to do this outside of
258          * spin locks and it will most likely be needed.
259          */
260         mcast = mcast_alloc(gid);
261         if (mcast == NULL) {
262                 ret = -ENOMEM;
263                 goto bail;
264         }
265         mqp = mcast_qp_alloc(qp);
266         if (mqp == NULL) {
267                 mcast_free(mcast);
268                 ret = -ENOMEM;
269                 goto bail;
270         }
271         ibp = to_iport(ibqp->device, qp->port_num);
272         switch (mcast_add(dev, ibp, mcast, mqp)) {
273         case ESRCH:
274                 /* Neither was used: OK to attach the same QP twice. */
275                 mcast_qp_free(mqp);
276                 mcast_free(mcast);
277                 break;
278
279         case EEXIST:            /* The mcast wasn't used */
280                 mcast_free(mcast);
281                 break;
282
283         case ENOMEM:
284                 /* Exceeded the maximum number of mcast groups. */
285                 mcast_qp_free(mqp);
286                 mcast_free(mcast);
287                 ret = -ENOMEM;
288                 goto bail;
289
290         default:
291                 break;
292         }
293
294         ret = 0;
295
296 bail:
297         return ret;
298 }
299
300 int hfi1_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
301 {
302         struct hfi1_qp *qp = to_iqp(ibqp);
303         struct hfi1_ibdev *dev = to_idev(ibqp->device);
304         struct hfi1_ibport *ibp = to_iport(ibqp->device, qp->port_num);
305         struct hfi1_mcast *mcast = NULL;
306         struct hfi1_mcast_qp *p, *tmp;
307         struct rb_node *n;
308         int last = 0;
309         int ret;
310
311         if (ibqp->qp_num <= 1 || qp->state == IB_QPS_RESET) {
312                 ret = -EINVAL;
313                 goto bail;
314         }
315
316         spin_lock_irq(&ibp->lock);
317
318         /* Find the GID in the mcast table. */
319         n = ibp->mcast_tree.rb_node;
320         while (1) {
321                 if (n == NULL) {
322                         spin_unlock_irq(&ibp->lock);
323                         ret = -EINVAL;
324                         goto bail;
325                 }
326
327                 mcast = rb_entry(n, struct hfi1_mcast, rb_node);
328                 ret = memcmp(gid->raw, mcast->mgid.raw,
329                              sizeof(union ib_gid));
330                 if (ret < 0)
331                         n = n->rb_left;
332                 else if (ret > 0)
333                         n = n->rb_right;
334                 else
335                         break;
336         }
337
338         /* Search the QP list. */
339         list_for_each_entry_safe(p, tmp, &mcast->qp_list, list) {
340                 if (p->qp != qp)
341                         continue;
342                 /*
343                  * We found it, so remove it, but don't poison the forward
344                  * link until we are sure there are no list walkers.
345                  */
346                 list_del_rcu(&p->list);
347                 mcast->n_attached--;
348
349                 /* If this was the last attached QP, remove the GID too. */
350                 if (list_empty(&mcast->qp_list)) {
351                         rb_erase(&mcast->rb_node, &ibp->mcast_tree);
352                         last = 1;
353                 }
354                 break;
355         }
356
357         spin_unlock_irq(&ibp->lock);
358
359         if (p) {
360                 /*
361                  * Wait for any list walkers to finish before freeing the
362                  * list element.
363                  */
364                 wait_event(mcast->wait, atomic_read(&mcast->refcount) <= 1);
365                 mcast_qp_free(p);
366         }
367         if (last) {
368                 atomic_dec(&mcast->refcount);
369                 wait_event(mcast->wait, !atomic_read(&mcast->refcount));
370                 mcast_free(mcast);
371                 spin_lock_irq(&dev->n_mcast_grps_lock);
372                 dev->n_mcast_grps_allocated--;
373                 spin_unlock_irq(&dev->n_mcast_grps_lock);
374         }
375
376         ret = 0;
377
378 bail:
379         return ret;
380 }
381
382 int hfi1_mcast_tree_empty(struct hfi1_ibport *ibp)
383 {
384         return ibp->mcast_tree.rb_node == NULL;
385 }