Merge tag 'for-3.7-rc1' of git://gitorious.org/linux-pwm/linux-pwm
[cascardo/linux.git] / drivers / infiniband / hw / mlx4 / mcg.c
1 /*
2  * Copyright (c) 2012 Mellanox Technologies. 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
33 #include <rdma/ib_mad.h>
34 #include <rdma/ib_smi.h>
35 #include <rdma/ib_cache.h>
36 #include <rdma/ib_sa.h>
37
38 #include <linux/mlx4/cmd.h>
39 #include <linux/rbtree.h>
40 #include <linux/delay.h>
41
42 #include "mlx4_ib.h"
43
44 #define MAX_VFS         80
45 #define MAX_PEND_REQS_PER_FUNC 4
46 #define MAD_TIMEOUT_MS  2000
47
48 #define mcg_warn(fmt, arg...)   pr_warn("MCG WARNING: " fmt, ##arg)
49 #define mcg_error(fmt, arg...)  pr_err(fmt, ##arg)
50 #define mcg_warn_group(group, format, arg...) \
51         pr_warn("%s-%d: %16s (port %d): WARNING: " format, __func__, __LINE__,\
52         (group)->name, group->demux->port, ## arg)
53
54 #define mcg_error_group(group, format, arg...) \
55         pr_err("  %16s: " format, (group)->name, ## arg)
56
57
58 static union ib_gid mgid0;
59
60 static struct workqueue_struct *clean_wq;
61
62 enum mcast_state {
63         MCAST_NOT_MEMBER = 0,
64         MCAST_MEMBER,
65 };
66
67 enum mcast_group_state {
68         MCAST_IDLE,
69         MCAST_JOIN_SENT,
70         MCAST_LEAVE_SENT,
71         MCAST_RESP_READY
72 };
73
74 struct mcast_member {
75         enum mcast_state state;
76         uint8_t                 join_state;
77         int                     num_pend_reqs;
78         struct list_head        pending;
79 };
80
81 struct ib_sa_mcmember_data {
82         union ib_gid    mgid;
83         union ib_gid    port_gid;
84         __be32          qkey;
85         __be16          mlid;
86         u8              mtusel_mtu;
87         u8              tclass;
88         __be16          pkey;
89         u8              ratesel_rate;
90         u8              lifetmsel_lifetm;
91         __be32          sl_flowlabel_hoplimit;
92         u8              scope_join_state;
93         u8              proxy_join;
94         u8              reserved[2];
95 };
96
97 struct mcast_group {
98         struct ib_sa_mcmember_data rec;
99         struct rb_node          node;
100         struct list_head        mgid0_list;
101         struct mlx4_ib_demux_ctx *demux;
102         struct mcast_member     func[MAX_VFS];
103         struct mutex            lock;
104         struct work_struct      work;
105         struct list_head        pending_list;
106         int                     members[3];
107         enum mcast_group_state  state;
108         enum mcast_group_state  prev_state;
109         struct ib_sa_mad        response_sa_mad;
110         __be64                  last_req_tid;
111
112         char                    name[33]; /* MGID string */
113         struct device_attribute dentry;
114
115         /* refcount is the reference count for the following:
116            1. Each queued request
117            2. Each invocation of the worker thread
118            3. Membership of the port at the SA
119         */
120         atomic_t                refcount;
121
122         /* delayed work to clean pending SM request */
123         struct delayed_work     timeout_work;
124         struct list_head        cleanup_list;
125 };
126
127 struct mcast_req {
128         int                     func;
129         struct ib_sa_mad        sa_mad;
130         struct list_head        group_list;
131         struct list_head        func_list;
132         struct mcast_group      *group;
133         int                     clean;
134 };
135
136
137 #define safe_atomic_dec(ref) \
138         do {\
139                 if (atomic_dec_and_test(ref)) \
140                         mcg_warn_group(group, "did not expect to reach zero\n"); \
141         } while (0)
142
143 static const char *get_state_string(enum mcast_group_state state)
144 {
145         switch (state) {
146         case MCAST_IDLE:
147                 return "MCAST_IDLE";
148         case MCAST_JOIN_SENT:
149                 return "MCAST_JOIN_SENT";
150         case MCAST_LEAVE_SENT:
151                 return "MCAST_LEAVE_SENT";
152         case MCAST_RESP_READY:
153                 return "MCAST_RESP_READY";
154         }
155         return "Invalid State";
156 }
157
158 static struct mcast_group *mcast_find(struct mlx4_ib_demux_ctx *ctx,
159                                       union ib_gid *mgid)
160 {
161         struct rb_node *node = ctx->mcg_table.rb_node;
162         struct mcast_group *group;
163         int ret;
164
165         while (node) {
166                 group = rb_entry(node, struct mcast_group, node);
167                 ret = memcmp(mgid->raw, group->rec.mgid.raw, sizeof *mgid);
168                 if (!ret)
169                         return group;
170
171                 if (ret < 0)
172                         node = node->rb_left;
173                 else
174                         node = node->rb_right;
175         }
176         return NULL;
177 }
178
179 static struct mcast_group *mcast_insert(struct mlx4_ib_demux_ctx *ctx,
180                                         struct mcast_group *group)
181 {
182         struct rb_node **link = &ctx->mcg_table.rb_node;
183         struct rb_node *parent = NULL;
184         struct mcast_group *cur_group;
185         int ret;
186
187         while (*link) {
188                 parent = *link;
189                 cur_group = rb_entry(parent, struct mcast_group, node);
190
191                 ret = memcmp(group->rec.mgid.raw, cur_group->rec.mgid.raw,
192                              sizeof group->rec.mgid);
193                 if (ret < 0)
194                         link = &(*link)->rb_left;
195                 else if (ret > 0)
196                         link = &(*link)->rb_right;
197                 else
198                         return cur_group;
199         }
200         rb_link_node(&group->node, parent, link);
201         rb_insert_color(&group->node, &ctx->mcg_table);
202         return NULL;
203 }
204
205 static int send_mad_to_wire(struct mlx4_ib_demux_ctx *ctx, struct ib_mad *mad)
206 {
207         struct mlx4_ib_dev *dev = ctx->dev;
208         struct ib_ah_attr       ah_attr;
209
210         spin_lock(&dev->sm_lock);
211         if (!dev->sm_ah[ctx->port - 1]) {
212                 /* port is not yet Active, sm_ah not ready */
213                 spin_unlock(&dev->sm_lock);
214                 return -EAGAIN;
215         }
216         mlx4_ib_query_ah(dev->sm_ah[ctx->port - 1], &ah_attr);
217         spin_unlock(&dev->sm_lock);
218         return mlx4_ib_send_to_wire(dev, mlx4_master_func_num(dev->dev), ctx->port,
219                                     IB_QPT_GSI, 0, 1, IB_QP1_QKEY, &ah_attr, mad);
220 }
221
222 static int send_mad_to_slave(int slave, struct mlx4_ib_demux_ctx *ctx,
223                              struct ib_mad *mad)
224 {
225         struct mlx4_ib_dev *dev = ctx->dev;
226         struct ib_mad_agent *agent = dev->send_agent[ctx->port - 1][1];
227         struct ib_wc wc;
228         struct ib_ah_attr ah_attr;
229
230         /* Our agent might not yet be registered when mads start to arrive */
231         if (!agent)
232                 return -EAGAIN;
233
234         ib_query_ah(dev->sm_ah[ctx->port - 1], &ah_attr);
235
236         wc.pkey_index = 0;
237         wc.sl = 0;
238         wc.dlid_path_bits = 0;
239         wc.port_num = ctx->port;
240         wc.slid = ah_attr.dlid;  /* opensm lid */
241         wc.src_qp = 1;
242         return mlx4_ib_send_to_slave(dev, slave, ctx->port, IB_QPT_GSI, &wc, NULL, mad);
243 }
244
245 static int send_join_to_wire(struct mcast_group *group, struct ib_sa_mad *sa_mad)
246 {
247         struct ib_sa_mad mad;
248         struct ib_sa_mcmember_data *sa_mad_data = (struct ib_sa_mcmember_data *)&mad.data;
249         int ret;
250
251         /* we rely on a mad request as arrived from a VF */
252         memcpy(&mad, sa_mad, sizeof mad);
253
254         /* fix port GID to be the real one (slave 0) */
255         sa_mad_data->port_gid.global.interface_id = group->demux->guid_cache[0];
256
257         /* assign our own TID */
258         mad.mad_hdr.tid = mlx4_ib_get_new_demux_tid(group->demux);
259         group->last_req_tid = mad.mad_hdr.tid; /* keep it for later validation */
260
261         ret = send_mad_to_wire(group->demux, (struct ib_mad *)&mad);
262         /* set timeout handler */
263         if (!ret) {
264                 /* calls mlx4_ib_mcg_timeout_handler */
265                 queue_delayed_work(group->demux->mcg_wq, &group->timeout_work,
266                                 msecs_to_jiffies(MAD_TIMEOUT_MS));
267         }
268
269         return ret;
270 }
271
272 static int send_leave_to_wire(struct mcast_group *group, u8 join_state)
273 {
274         struct ib_sa_mad mad;
275         struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)&mad.data;
276         int ret;
277
278         memset(&mad, 0, sizeof mad);
279         mad.mad_hdr.base_version = 1;
280         mad.mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
281         mad.mad_hdr.class_version = 2;
282         mad.mad_hdr.method = IB_SA_METHOD_DELETE;
283         mad.mad_hdr.status = cpu_to_be16(0);
284         mad.mad_hdr.class_specific = cpu_to_be16(0);
285         mad.mad_hdr.tid = mlx4_ib_get_new_demux_tid(group->demux);
286         group->last_req_tid = mad.mad_hdr.tid; /* keep it for later validation */
287         mad.mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
288         mad.mad_hdr.attr_mod = cpu_to_be32(0);
289         mad.sa_hdr.sm_key = 0x0;
290         mad.sa_hdr.attr_offset = cpu_to_be16(7);
291         mad.sa_hdr.comp_mask = IB_SA_MCMEMBER_REC_MGID |
292                 IB_SA_MCMEMBER_REC_PORT_GID | IB_SA_MCMEMBER_REC_JOIN_STATE;
293
294         *sa_data = group->rec;
295         sa_data->scope_join_state = join_state;
296
297         ret = send_mad_to_wire(group->demux, (struct ib_mad *)&mad);
298         if (ret)
299                 group->state = MCAST_IDLE;
300
301         /* set timeout handler */
302         if (!ret) {
303                 /* calls mlx4_ib_mcg_timeout_handler */
304                 queue_delayed_work(group->demux->mcg_wq, &group->timeout_work,
305                                 msecs_to_jiffies(MAD_TIMEOUT_MS));
306         }
307
308         return ret;
309 }
310
311 static int send_reply_to_slave(int slave, struct mcast_group *group,
312                 struct ib_sa_mad *req_sa_mad, u16 status)
313 {
314         struct ib_sa_mad mad;
315         struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)&mad.data;
316         struct ib_sa_mcmember_data *req_sa_data = (struct ib_sa_mcmember_data *)&req_sa_mad->data;
317         int ret;
318
319         memset(&mad, 0, sizeof mad);
320         mad.mad_hdr.base_version = 1;
321         mad.mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
322         mad.mad_hdr.class_version = 2;
323         mad.mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
324         mad.mad_hdr.status = cpu_to_be16(status);
325         mad.mad_hdr.class_specific = cpu_to_be16(0);
326         mad.mad_hdr.tid = req_sa_mad->mad_hdr.tid;
327         *(u8 *)&mad.mad_hdr.tid = 0; /* resetting tid to 0 */
328         mad.mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
329         mad.mad_hdr.attr_mod = cpu_to_be32(0);
330         mad.sa_hdr.sm_key = req_sa_mad->sa_hdr.sm_key;
331         mad.sa_hdr.attr_offset = cpu_to_be16(7);
332         mad.sa_hdr.comp_mask = 0; /* ignored on responses, see IBTA spec */
333
334         *sa_data = group->rec;
335
336         /* reconstruct VF's requested join_state and port_gid */
337         sa_data->scope_join_state &= 0xf0;
338         sa_data->scope_join_state |= (group->func[slave].join_state & 0x0f);
339         memcpy(&sa_data->port_gid, &req_sa_data->port_gid, sizeof req_sa_data->port_gid);
340
341         ret = send_mad_to_slave(slave, group->demux, (struct ib_mad *)&mad);
342         return ret;
343 }
344
345 static int check_selector(ib_sa_comp_mask comp_mask,
346                           ib_sa_comp_mask selector_mask,
347                           ib_sa_comp_mask value_mask,
348                           u8 src_value, u8 dst_value)
349 {
350         int err;
351         u8 selector = dst_value >> 6;
352         dst_value &= 0x3f;
353         src_value &= 0x3f;
354
355         if (!(comp_mask & selector_mask) || !(comp_mask & value_mask))
356                 return 0;
357
358         switch (selector) {
359         case IB_SA_GT:
360                 err = (src_value <= dst_value);
361                 break;
362         case IB_SA_LT:
363                 err = (src_value >= dst_value);
364                 break;
365         case IB_SA_EQ:
366                 err = (src_value != dst_value);
367                 break;
368         default:
369                 err = 0;
370                 break;
371         }
372
373         return err;
374 }
375
376 static u16 cmp_rec(struct ib_sa_mcmember_data *src,
377                    struct ib_sa_mcmember_data *dst, ib_sa_comp_mask comp_mask)
378 {
379         /* src is group record, dst is request record */
380         /* MGID must already match */
381         /* Port_GID we always replace to our Port_GID, so it is a match */
382
383 #define MAD_STATUS_REQ_INVALID 0x0200
384         if (comp_mask & IB_SA_MCMEMBER_REC_QKEY && src->qkey != dst->qkey)
385                 return MAD_STATUS_REQ_INVALID;
386         if (comp_mask & IB_SA_MCMEMBER_REC_MLID && src->mlid != dst->mlid)
387                 return MAD_STATUS_REQ_INVALID;
388         if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_MTU_SELECTOR,
389                                  IB_SA_MCMEMBER_REC_MTU,
390                                  src->mtusel_mtu, dst->mtusel_mtu))
391                 return MAD_STATUS_REQ_INVALID;
392         if (comp_mask & IB_SA_MCMEMBER_REC_TRAFFIC_CLASS &&
393             src->tclass != dst->tclass)
394                 return MAD_STATUS_REQ_INVALID;
395         if (comp_mask & IB_SA_MCMEMBER_REC_PKEY && src->pkey != dst->pkey)
396                 return MAD_STATUS_REQ_INVALID;
397         if (check_selector(comp_mask, IB_SA_MCMEMBER_REC_RATE_SELECTOR,
398                                  IB_SA_MCMEMBER_REC_RATE,
399                                  src->ratesel_rate, dst->ratesel_rate))
400                 return MAD_STATUS_REQ_INVALID;
401         if (check_selector(comp_mask,
402                                  IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME_SELECTOR,
403                                  IB_SA_MCMEMBER_REC_PACKET_LIFE_TIME,
404                                  src->lifetmsel_lifetm, dst->lifetmsel_lifetm))
405                 return MAD_STATUS_REQ_INVALID;
406         if (comp_mask & IB_SA_MCMEMBER_REC_SL &&
407                         (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0xf0000000) !=
408                         (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0xf0000000))
409                 return MAD_STATUS_REQ_INVALID;
410         if (comp_mask & IB_SA_MCMEMBER_REC_FLOW_LABEL &&
411                         (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0x0fffff00) !=
412                         (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0x0fffff00))
413                 return MAD_STATUS_REQ_INVALID;
414         if (comp_mask & IB_SA_MCMEMBER_REC_HOP_LIMIT &&
415                         (be32_to_cpu(src->sl_flowlabel_hoplimit) & 0x000000ff) !=
416                         (be32_to_cpu(dst->sl_flowlabel_hoplimit) & 0x000000ff))
417                 return MAD_STATUS_REQ_INVALID;
418         if (comp_mask & IB_SA_MCMEMBER_REC_SCOPE &&
419                         (src->scope_join_state & 0xf0) !=
420                         (dst->scope_join_state & 0xf0))
421                 return MAD_STATUS_REQ_INVALID;
422
423         /* join_state checked separately, proxy_join ignored */
424
425         return 0;
426 }
427
428 /* release group, return 1 if this was last release and group is destroyed
429  * timout work is canceled sync */
430 static int release_group(struct mcast_group *group, int from_timeout_handler)
431 {
432         struct mlx4_ib_demux_ctx *ctx = group->demux;
433         int nzgroup;
434
435         mutex_lock(&ctx->mcg_table_lock);
436         mutex_lock(&group->lock);
437         if (atomic_dec_and_test(&group->refcount)) {
438                 if (!from_timeout_handler) {
439                         if (group->state != MCAST_IDLE &&
440                             !cancel_delayed_work(&group->timeout_work)) {
441                                 atomic_inc(&group->refcount);
442                                 mutex_unlock(&group->lock);
443                                 mutex_unlock(&ctx->mcg_table_lock);
444                                 return 0;
445                         }
446                 }
447
448                 nzgroup = memcmp(&group->rec.mgid, &mgid0, sizeof mgid0);
449                 if (nzgroup)
450                         del_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
451                 if (!list_empty(&group->pending_list))
452                         mcg_warn_group(group, "releasing a group with non empty pending list\n");
453                 if (nzgroup)
454                         rb_erase(&group->node, &ctx->mcg_table);
455                 list_del_init(&group->mgid0_list);
456                 mutex_unlock(&group->lock);
457                 mutex_unlock(&ctx->mcg_table_lock);
458                 kfree(group);
459                 return 1;
460         } else {
461                 mutex_unlock(&group->lock);
462                 mutex_unlock(&ctx->mcg_table_lock);
463         }
464         return 0;
465 }
466
467 static void adjust_membership(struct mcast_group *group, u8 join_state, int inc)
468 {
469         int i;
470
471         for (i = 0; i < 3; i++, join_state >>= 1)
472                 if (join_state & 0x1)
473                         group->members[i] += inc;
474 }
475
476 static u8 get_leave_state(struct mcast_group *group)
477 {
478         u8 leave_state = 0;
479         int i;
480
481         for (i = 0; i < 3; i++)
482                 if (!group->members[i])
483                         leave_state |= (1 << i);
484
485         return leave_state & (group->rec.scope_join_state & 7);
486 }
487
488 static int join_group(struct mcast_group *group, int slave, u8 join_mask)
489 {
490         int ret = 0;
491         u8 join_state;
492
493         /* remove bits that slave is already member of, and adjust */
494         join_state = join_mask & (~group->func[slave].join_state);
495         adjust_membership(group, join_state, 1);
496         group->func[slave].join_state |= join_state;
497         if (group->func[slave].state != MCAST_MEMBER && join_state) {
498                 group->func[slave].state = MCAST_MEMBER;
499                 ret = 1;
500         }
501         return ret;
502 }
503
504 static int leave_group(struct mcast_group *group, int slave, u8 leave_state)
505 {
506         int ret = 0;
507
508         adjust_membership(group, leave_state, -1);
509         group->func[slave].join_state &= ~leave_state;
510         if (!group->func[slave].join_state) {
511                 group->func[slave].state = MCAST_NOT_MEMBER;
512                 ret = 1;
513         }
514         return ret;
515 }
516
517 static int check_leave(struct mcast_group *group, int slave, u8 leave_mask)
518 {
519         if (group->func[slave].state != MCAST_MEMBER)
520                 return MAD_STATUS_REQ_INVALID;
521
522         /* make sure we're not deleting unset bits */
523         if (~group->func[slave].join_state & leave_mask)
524                 return MAD_STATUS_REQ_INVALID;
525
526         if (!leave_mask)
527                 return MAD_STATUS_REQ_INVALID;
528
529         return 0;
530 }
531
532 static void mlx4_ib_mcg_timeout_handler(struct work_struct *work)
533 {
534         struct delayed_work *delay = to_delayed_work(work);
535         struct mcast_group *group;
536         struct mcast_req *req = NULL;
537
538         group = container_of(delay, typeof(*group), timeout_work);
539
540         mutex_lock(&group->lock);
541         if (group->state == MCAST_JOIN_SENT) {
542                 if (!list_empty(&group->pending_list)) {
543                         req = list_first_entry(&group->pending_list, struct mcast_req, group_list);
544                         list_del(&req->group_list);
545                         list_del(&req->func_list);
546                         --group->func[req->func].num_pend_reqs;
547                         mutex_unlock(&group->lock);
548                         kfree(req);
549                         if (memcmp(&group->rec.mgid, &mgid0, sizeof mgid0)) {
550                                 if (release_group(group, 1))
551                                         return;
552                         } else {
553                                 kfree(group);
554                                 return;
555                         }
556                         mutex_lock(&group->lock);
557                 } else
558                         mcg_warn_group(group, "DRIVER BUG\n");
559         } else if (group->state == MCAST_LEAVE_SENT) {
560                 if (group->rec.scope_join_state & 7)
561                         group->rec.scope_join_state &= 0xf8;
562                 group->state = MCAST_IDLE;
563                 mutex_unlock(&group->lock);
564                 if (release_group(group, 1))
565                         return;
566                 mutex_lock(&group->lock);
567         } else
568                 mcg_warn_group(group, "invalid state %s\n", get_state_string(group->state));
569         group->state = MCAST_IDLE;
570         atomic_inc(&group->refcount);
571         if (!queue_work(group->demux->mcg_wq, &group->work))
572                 safe_atomic_dec(&group->refcount);
573
574         mutex_unlock(&group->lock);
575 }
576
577 static int handle_leave_req(struct mcast_group *group, u8 leave_mask,
578                             struct mcast_req *req)
579 {
580         u16 status;
581
582         if (req->clean)
583                 leave_mask = group->func[req->func].join_state;
584
585         status = check_leave(group, req->func, leave_mask);
586         if (!status)
587                 leave_group(group, req->func, leave_mask);
588
589         if (!req->clean)
590                 send_reply_to_slave(req->func, group, &req->sa_mad, status);
591         --group->func[req->func].num_pend_reqs;
592         list_del(&req->group_list);
593         list_del(&req->func_list);
594         kfree(req);
595         return 1;
596 }
597
598 static int handle_join_req(struct mcast_group *group, u8 join_mask,
599                            struct mcast_req *req)
600 {
601         u8 group_join_state = group->rec.scope_join_state & 7;
602         int ref = 0;
603         u16 status;
604         struct ib_sa_mcmember_data *sa_data = (struct ib_sa_mcmember_data *)req->sa_mad.data;
605
606         if (join_mask == (group_join_state & join_mask)) {
607                 /* port's membership need not change */
608                 status = cmp_rec(&group->rec, sa_data, req->sa_mad.sa_hdr.comp_mask);
609                 if (!status)
610                         join_group(group, req->func, join_mask);
611
612                 --group->func[req->func].num_pend_reqs;
613                 send_reply_to_slave(req->func, group, &req->sa_mad, status);
614                 list_del(&req->group_list);
615                 list_del(&req->func_list);
616                 kfree(req);
617                 ++ref;
618         } else {
619                 /* port's membership needs to be updated */
620                 group->prev_state = group->state;
621                 if (send_join_to_wire(group, &req->sa_mad)) {
622                         --group->func[req->func].num_pend_reqs;
623                         list_del(&req->group_list);
624                         list_del(&req->func_list);
625                         kfree(req);
626                         ref = 1;
627                         group->state = group->prev_state;
628                 } else
629                         group->state = MCAST_JOIN_SENT;
630         }
631
632         return ref;
633 }
634
635 static void mlx4_ib_mcg_work_handler(struct work_struct *work)
636 {
637         struct mcast_group *group;
638         struct mcast_req *req = NULL;
639         struct ib_sa_mcmember_data *sa_data;
640         u8 req_join_state;
641         int rc = 1; /* release_count - this is for the scheduled work */
642         u16 status;
643         u8 method;
644
645         group = container_of(work, typeof(*group), work);
646
647         mutex_lock(&group->lock);
648
649         /* First, let's see if a response from SM is waiting regarding this group.
650          * If so, we need to update the group's REC. If this is a bad response, we
651          * may need to send a bad response to a VF waiting for it. If VF is waiting
652          * and this is a good response, the VF will be answered later in this func. */
653         if (group->state == MCAST_RESP_READY) {
654                 /* cancels mlx4_ib_mcg_timeout_handler */
655                 cancel_delayed_work(&group->timeout_work);
656                 status = be16_to_cpu(group->response_sa_mad.mad_hdr.status);
657                 method = group->response_sa_mad.mad_hdr.method;
658                 if (group->last_req_tid != group->response_sa_mad.mad_hdr.tid) {
659                         mcg_warn_group(group, "Got MAD response to existing MGID but wrong TID, dropping. Resp TID=%llx, group TID=%llx\n",
660                                 be64_to_cpu(group->response_sa_mad.mad_hdr.tid),
661                                 be64_to_cpu(group->last_req_tid));
662                         group->state = group->prev_state;
663                         goto process_requests;
664                 }
665                 if (status) {
666                         if (!list_empty(&group->pending_list))
667                                 req = list_first_entry(&group->pending_list,
668                                                 struct mcast_req, group_list);
669                         if ((method == IB_MGMT_METHOD_GET_RESP)) {
670                                         if (req) {
671                                                 send_reply_to_slave(req->func, group, &req->sa_mad, status);
672                                                 --group->func[req->func].num_pend_reqs;
673                                                 list_del(&req->group_list);
674                                                 list_del(&req->func_list);
675                                                 kfree(req);
676                                                 ++rc;
677                                         } else
678                                                 mcg_warn_group(group, "no request for failed join\n");
679                         } else if (method == IB_SA_METHOD_DELETE_RESP && group->demux->flushing)
680                                 ++rc;
681                 } else {
682                         u8 resp_join_state;
683                         u8 cur_join_state;
684
685                         resp_join_state = ((struct ib_sa_mcmember_data *)
686                                                 group->response_sa_mad.data)->scope_join_state & 7;
687                         cur_join_state = group->rec.scope_join_state & 7;
688
689                         if (method == IB_MGMT_METHOD_GET_RESP) {
690                                 /* successfull join */
691                                 if (!cur_join_state && resp_join_state)
692                                         --rc;
693                         } else if (!resp_join_state)
694                                         ++rc;
695                         memcpy(&group->rec, group->response_sa_mad.data, sizeof group->rec);
696                 }
697                 group->state = MCAST_IDLE;
698         }
699
700 process_requests:
701         /* We should now go over pending join/leave requests, as long as we are idle. */
702         while (!list_empty(&group->pending_list) && group->state == MCAST_IDLE) {
703                 req = list_first_entry(&group->pending_list, struct mcast_req,
704                                        group_list);
705                 sa_data = (struct ib_sa_mcmember_data *)req->sa_mad.data;
706                 req_join_state = sa_data->scope_join_state & 0x7;
707
708                 /* For a leave request, we will immediately answer the VF, and
709                  * update our internal counters. The actual leave will be sent
710                  * to SM later, if at all needed. We dequeue the request now. */
711                 if (req->sa_mad.mad_hdr.method == IB_SA_METHOD_DELETE)
712                         rc += handle_leave_req(group, req_join_state, req);
713                 else
714                         rc += handle_join_req(group, req_join_state, req);
715         }
716
717         /* Handle leaves */
718         if (group->state == MCAST_IDLE) {
719                 req_join_state = get_leave_state(group);
720                 if (req_join_state) {
721                         group->rec.scope_join_state &= ~req_join_state;
722                         group->prev_state = group->state;
723                         if (send_leave_to_wire(group, req_join_state)) {
724                                 group->state = group->prev_state;
725                                 ++rc;
726                         } else
727                                 group->state = MCAST_LEAVE_SENT;
728                 }
729         }
730
731         if (!list_empty(&group->pending_list) && group->state == MCAST_IDLE)
732                 goto process_requests;
733         mutex_unlock(&group->lock);
734
735         while (rc--)
736                 release_group(group, 0);
737 }
738
739 static struct mcast_group *search_relocate_mgid0_group(struct mlx4_ib_demux_ctx *ctx,
740                                                        __be64 tid,
741                                                        union ib_gid *new_mgid)
742 {
743         struct mcast_group *group = NULL, *cur_group;
744         struct mcast_req *req;
745         struct list_head *pos;
746         struct list_head *n;
747
748         mutex_lock(&ctx->mcg_table_lock);
749         list_for_each_safe(pos, n, &ctx->mcg_mgid0_list) {
750                 group = list_entry(pos, struct mcast_group, mgid0_list);
751                 mutex_lock(&group->lock);
752                 if (group->last_req_tid == tid) {
753                         if (memcmp(new_mgid, &mgid0, sizeof mgid0)) {
754                                 group->rec.mgid = *new_mgid;
755                                 sprintf(group->name, "%016llx%016llx",
756                                                 be64_to_cpu(group->rec.mgid.global.subnet_prefix),
757                                                 be64_to_cpu(group->rec.mgid.global.interface_id));
758                                 list_del_init(&group->mgid0_list);
759                                 cur_group = mcast_insert(ctx, group);
760                                 if (cur_group) {
761                                         /* A race between our code and SM. Silently cleaning the new one */
762                                         req = list_first_entry(&group->pending_list,
763                                                                struct mcast_req, group_list);
764                                         --group->func[req->func].num_pend_reqs;
765                                         list_del(&req->group_list);
766                                         list_del(&req->func_list);
767                                         kfree(req);
768                                         mutex_unlock(&group->lock);
769                                         mutex_unlock(&ctx->mcg_table_lock);
770                                         release_group(group, 0);
771                                         return NULL;
772                                 }
773
774                                 atomic_inc(&group->refcount);
775                                 add_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
776                                 mutex_unlock(&group->lock);
777                                 mutex_unlock(&ctx->mcg_table_lock);
778                                 return group;
779                         } else {
780                                 struct mcast_req *tmp1, *tmp2;
781
782                                 list_del(&group->mgid0_list);
783                                 if (!list_empty(&group->pending_list) && group->state != MCAST_IDLE)
784                                         cancel_delayed_work_sync(&group->timeout_work);
785
786                                 list_for_each_entry_safe(tmp1, tmp2, &group->pending_list, group_list) {
787                                         list_del(&tmp1->group_list);
788                                         kfree(tmp1);
789                                 }
790                                 mutex_unlock(&group->lock);
791                                 mutex_unlock(&ctx->mcg_table_lock);
792                                 kfree(group);
793                                 return NULL;
794                         }
795                 }
796                 mutex_unlock(&group->lock);
797         }
798         mutex_unlock(&ctx->mcg_table_lock);
799
800         return NULL;
801 }
802
803 static ssize_t sysfs_show_group(struct device *dev,
804                 struct device_attribute *attr, char *buf);
805
806 static struct mcast_group *acquire_group(struct mlx4_ib_demux_ctx *ctx,
807                                          union ib_gid *mgid, int create,
808                                          gfp_t gfp_mask)
809 {
810         struct mcast_group *group, *cur_group;
811         int is_mgid0;
812         int i;
813
814         is_mgid0 = !memcmp(&mgid0, mgid, sizeof mgid0);
815         if (!is_mgid0) {
816                 group = mcast_find(ctx, mgid);
817                 if (group)
818                         goto found;
819         }
820
821         if (!create)
822                 return ERR_PTR(-ENOENT);
823
824         group = kzalloc(sizeof *group, gfp_mask);
825         if (!group)
826                 return ERR_PTR(-ENOMEM);
827
828         group->demux = ctx;
829         group->rec.mgid = *mgid;
830         INIT_LIST_HEAD(&group->pending_list);
831         INIT_LIST_HEAD(&group->mgid0_list);
832         for (i = 0; i < MAX_VFS; ++i)
833                 INIT_LIST_HEAD(&group->func[i].pending);
834         INIT_WORK(&group->work, mlx4_ib_mcg_work_handler);
835         INIT_DELAYED_WORK(&group->timeout_work, mlx4_ib_mcg_timeout_handler);
836         mutex_init(&group->lock);
837         sprintf(group->name, "%016llx%016llx",
838                         be64_to_cpu(group->rec.mgid.global.subnet_prefix),
839                         be64_to_cpu(group->rec.mgid.global.interface_id));
840         sysfs_attr_init(&group->dentry.attr);
841         group->dentry.show = sysfs_show_group;
842         group->dentry.store = NULL;
843         group->dentry.attr.name = group->name;
844         group->dentry.attr.mode = 0400;
845         group->state = MCAST_IDLE;
846
847         if (is_mgid0) {
848                 list_add(&group->mgid0_list, &ctx->mcg_mgid0_list);
849                 goto found;
850         }
851
852         cur_group = mcast_insert(ctx, group);
853         if (cur_group) {
854                 mcg_warn("group just showed up %s - confused\n", cur_group->name);
855                 kfree(group);
856                 return ERR_PTR(-EINVAL);
857         }
858
859         add_sysfs_port_mcg_attr(ctx->dev, ctx->port, &group->dentry.attr);
860
861 found:
862         atomic_inc(&group->refcount);
863         return group;
864 }
865
866 static void queue_req(struct mcast_req *req)
867 {
868         struct mcast_group *group = req->group;
869
870         atomic_inc(&group->refcount); /* for the request */
871         atomic_inc(&group->refcount); /* for scheduling the work */
872         list_add_tail(&req->group_list, &group->pending_list);
873         list_add_tail(&req->func_list, &group->func[req->func].pending);
874         /* calls mlx4_ib_mcg_work_handler */
875         if (!queue_work(group->demux->mcg_wq, &group->work))
876                 safe_atomic_dec(&group->refcount);
877 }
878
879 int mlx4_ib_mcg_demux_handler(struct ib_device *ibdev, int port, int slave,
880                               struct ib_sa_mad *mad)
881 {
882         struct mlx4_ib_dev *dev = to_mdev(ibdev);
883         struct ib_sa_mcmember_data *rec = (struct ib_sa_mcmember_data *)mad->data;
884         struct mlx4_ib_demux_ctx *ctx = &dev->sriov.demux[port - 1];
885         struct mcast_group *group;
886
887         switch (mad->mad_hdr.method) {
888         case IB_MGMT_METHOD_GET_RESP:
889         case IB_SA_METHOD_DELETE_RESP:
890                 mutex_lock(&ctx->mcg_table_lock);
891                 group = acquire_group(ctx, &rec->mgid, 0, GFP_KERNEL);
892                 mutex_unlock(&ctx->mcg_table_lock);
893                 if (IS_ERR(group)) {
894                         if (mad->mad_hdr.method == IB_MGMT_METHOD_GET_RESP) {
895                                 __be64 tid = mad->mad_hdr.tid;
896                                 *(u8 *)(&tid) = (u8)slave; /* in group we kept the modified TID */
897                                 group = search_relocate_mgid0_group(ctx, tid, &rec->mgid);
898                         } else
899                                 group = NULL;
900                 }
901
902                 if (!group)
903                         return 1;
904
905                 mutex_lock(&group->lock);
906                 group->response_sa_mad = *mad;
907                 group->prev_state = group->state;
908                 group->state = MCAST_RESP_READY;
909                 /* calls mlx4_ib_mcg_work_handler */
910                 atomic_inc(&group->refcount);
911                 if (!queue_work(ctx->mcg_wq, &group->work))
912                         safe_atomic_dec(&group->refcount);
913                 mutex_unlock(&group->lock);
914                 release_group(group, 0);
915                 return 1; /* consumed */
916         case IB_MGMT_METHOD_SET:
917         case IB_SA_METHOD_GET_TABLE:
918         case IB_SA_METHOD_GET_TABLE_RESP:
919         case IB_SA_METHOD_DELETE:
920                 return 0; /* not consumed, pass-through to guest over tunnel */
921         default:
922                 mcg_warn("In demux, port %d: unexpected MCMember method: 0x%x, dropping\n",
923                         port, mad->mad_hdr.method);
924                 return 1; /* consumed */
925         }
926 }
927
928 int mlx4_ib_mcg_multiplex_handler(struct ib_device *ibdev, int port,
929                                   int slave, struct ib_sa_mad *sa_mad)
930 {
931         struct mlx4_ib_dev *dev = to_mdev(ibdev);
932         struct ib_sa_mcmember_data *rec = (struct ib_sa_mcmember_data *)sa_mad->data;
933         struct mlx4_ib_demux_ctx *ctx = &dev->sriov.demux[port - 1];
934         struct mcast_group *group;
935         struct mcast_req *req;
936         int may_create = 0;
937
938         if (ctx->flushing)
939                 return -EAGAIN;
940
941         switch (sa_mad->mad_hdr.method) {
942         case IB_MGMT_METHOD_SET:
943                 may_create = 1;
944         case IB_SA_METHOD_DELETE:
945                 req = kzalloc(sizeof *req, GFP_KERNEL);
946                 if (!req)
947                         return -ENOMEM;
948
949                 req->func = slave;
950                 req->sa_mad = *sa_mad;
951
952                 mutex_lock(&ctx->mcg_table_lock);
953                 group = acquire_group(ctx, &rec->mgid, may_create, GFP_KERNEL);
954                 mutex_unlock(&ctx->mcg_table_lock);
955                 if (IS_ERR(group)) {
956                         kfree(req);
957                         return PTR_ERR(group);
958                 }
959                 mutex_lock(&group->lock);
960                 if (group->func[slave].num_pend_reqs > MAX_PEND_REQS_PER_FUNC) {
961                         mutex_unlock(&group->lock);
962                         mcg_warn_group(group, "Port %d, Func %d has too many pending requests (%d), dropping\n",
963                                        port, slave, MAX_PEND_REQS_PER_FUNC);
964                         release_group(group, 0);
965                         kfree(req);
966                         return -ENOMEM;
967                 }
968                 ++group->func[slave].num_pend_reqs;
969                 req->group = group;
970                 queue_req(req);
971                 mutex_unlock(&group->lock);
972                 release_group(group, 0);
973                 return 1; /* consumed */
974         case IB_SA_METHOD_GET_TABLE:
975         case IB_MGMT_METHOD_GET_RESP:
976         case IB_SA_METHOD_GET_TABLE_RESP:
977         case IB_SA_METHOD_DELETE_RESP:
978                 return 0; /* not consumed, pass-through */
979         default:
980                 mcg_warn("In multiplex, port %d, func %d: unexpected MCMember method: 0x%x, dropping\n",
981                         port, slave, sa_mad->mad_hdr.method);
982                 return 1; /* consumed */
983         }
984 }
985
986 static ssize_t sysfs_show_group(struct device *dev,
987                 struct device_attribute *attr, char *buf)
988 {
989         struct mcast_group *group =
990                 container_of(attr, struct mcast_group, dentry);
991         struct mcast_req *req = NULL;
992         char pending_str[40];
993         char state_str[40];
994         ssize_t len = 0;
995         int f;
996
997         if (group->state == MCAST_IDLE)
998                 sprintf(state_str, "%s", get_state_string(group->state));
999         else
1000                 sprintf(state_str, "%s(TID=0x%llx)",
1001                                 get_state_string(group->state),
1002                                 be64_to_cpu(group->last_req_tid));
1003         if (list_empty(&group->pending_list)) {
1004                 sprintf(pending_str, "No");
1005         } else {
1006                 req = list_first_entry(&group->pending_list, struct mcast_req, group_list);
1007                 sprintf(pending_str, "Yes(TID=0x%llx)",
1008                                 be64_to_cpu(req->sa_mad.mad_hdr.tid));
1009         }
1010         len += sprintf(buf + len, "%1d [%02d,%02d,%02d] %4d %4s %5s     ",
1011                         group->rec.scope_join_state & 0xf,
1012                         group->members[2], group->members[1], group->members[0],
1013                         atomic_read(&group->refcount),
1014                         pending_str,
1015                         state_str);
1016         for (f = 0; f < MAX_VFS; ++f)
1017                 if (group->func[f].state == MCAST_MEMBER)
1018                         len += sprintf(buf + len, "%d[%1x] ",
1019                                         f, group->func[f].join_state);
1020
1021         len += sprintf(buf + len, "\t\t(%4hx %4x %2x %2x %2x %2x %2x "
1022                 "%4x %4x %2x %2x)\n",
1023                 be16_to_cpu(group->rec.pkey),
1024                 be32_to_cpu(group->rec.qkey),
1025                 (group->rec.mtusel_mtu & 0xc0) >> 6,
1026                 group->rec.mtusel_mtu & 0x3f,
1027                 group->rec.tclass,
1028                 (group->rec.ratesel_rate & 0xc0) >> 6,
1029                 group->rec.ratesel_rate & 0x3f,
1030                 (be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0xf0000000) >> 28,
1031                 (be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x0fffff00) >> 8,
1032                 be32_to_cpu(group->rec.sl_flowlabel_hoplimit) & 0x000000ff,
1033                 group->rec.proxy_join);
1034
1035         return len;
1036 }
1037
1038 int mlx4_ib_mcg_port_init(struct mlx4_ib_demux_ctx *ctx)
1039 {
1040         char name[20];
1041
1042         atomic_set(&ctx->tid, 0);
1043         sprintf(name, "mlx4_ib_mcg%d", ctx->port);
1044         ctx->mcg_wq = create_singlethread_workqueue(name);
1045         if (!ctx->mcg_wq)
1046                 return -ENOMEM;
1047
1048         mutex_init(&ctx->mcg_table_lock);
1049         ctx->mcg_table = RB_ROOT;
1050         INIT_LIST_HEAD(&ctx->mcg_mgid0_list);
1051         ctx->flushing = 0;
1052
1053         return 0;
1054 }
1055
1056 static void force_clean_group(struct mcast_group *group)
1057 {
1058         struct mcast_req *req, *tmp
1059                 ;
1060         list_for_each_entry_safe(req, tmp, &group->pending_list, group_list) {
1061                 list_del(&req->group_list);
1062                 kfree(req);
1063         }
1064         del_sysfs_port_mcg_attr(group->demux->dev, group->demux->port, &group->dentry.attr);
1065         rb_erase(&group->node, &group->demux->mcg_table);
1066         kfree(group);
1067 }
1068
1069 static void _mlx4_ib_mcg_port_cleanup(struct mlx4_ib_demux_ctx *ctx, int destroy_wq)
1070 {
1071         int i;
1072         struct rb_node *p;
1073         struct mcast_group *group;
1074         unsigned long end;
1075         int count;
1076
1077         if (ctx->flushing)
1078                 return;
1079
1080         ctx->flushing = 1;
1081         for (i = 0; i < MAX_VFS; ++i)
1082                 clean_vf_mcast(ctx, i);
1083
1084         end = jiffies + msecs_to_jiffies(MAD_TIMEOUT_MS + 3000);
1085         do {
1086                 count = 0;
1087                 mutex_lock(&ctx->mcg_table_lock);
1088                 for (p = rb_first(&ctx->mcg_table); p; p = rb_next(p))
1089                         ++count;
1090                 mutex_unlock(&ctx->mcg_table_lock);
1091                 if (!count)
1092                         break;
1093
1094                 msleep(1);
1095         } while (time_after(end, jiffies));
1096
1097         flush_workqueue(ctx->mcg_wq);
1098         if (destroy_wq)
1099                 destroy_workqueue(ctx->mcg_wq);
1100
1101         mutex_lock(&ctx->mcg_table_lock);
1102         while ((p = rb_first(&ctx->mcg_table)) != NULL) {
1103                 group = rb_entry(p, struct mcast_group, node);
1104                 if (atomic_read(&group->refcount))
1105                         mcg_warn_group(group, "group refcount %d!!! (pointer %p)\n", atomic_read(&group->refcount), group);
1106
1107                 force_clean_group(group);
1108         }
1109         mutex_unlock(&ctx->mcg_table_lock);
1110
1111         if (!destroy_wq)
1112                 ctx->flushing = 0;
1113 }
1114
1115 struct clean_work {
1116         struct work_struct work;
1117         struct mlx4_ib_demux_ctx *ctx;
1118         int destroy_wq;
1119 };
1120
1121 static void mcg_clean_task(struct work_struct *work)
1122 {
1123         struct clean_work *cw = container_of(work, struct clean_work, work);
1124
1125         _mlx4_ib_mcg_port_cleanup(cw->ctx, cw->destroy_wq);
1126         kfree(cw);
1127 }
1128
1129 void mlx4_ib_mcg_port_cleanup(struct mlx4_ib_demux_ctx *ctx, int destroy_wq)
1130 {
1131         struct clean_work *work;
1132
1133         if (destroy_wq) {
1134                 _mlx4_ib_mcg_port_cleanup(ctx, destroy_wq);
1135                 return;
1136         }
1137
1138         work = kmalloc(sizeof *work, GFP_KERNEL);
1139         if (!work) {
1140                 mcg_warn("failed allocating work for cleanup\n");
1141                 return;
1142         }
1143
1144         work->ctx = ctx;
1145         work->destroy_wq = destroy_wq;
1146         INIT_WORK(&work->work, mcg_clean_task);
1147         queue_work(clean_wq, &work->work);
1148 }
1149
1150 static void build_leave_mad(struct mcast_req *req)
1151 {
1152         struct ib_sa_mad *mad = &req->sa_mad;
1153
1154         mad->mad_hdr.method = IB_SA_METHOD_DELETE;
1155 }
1156
1157
1158 static void clear_pending_reqs(struct mcast_group *group, int vf)
1159 {
1160         struct mcast_req *req, *tmp, *group_first = NULL;
1161         int clear;
1162         int pend = 0;
1163
1164         if (!list_empty(&group->pending_list))
1165                 group_first = list_first_entry(&group->pending_list, struct mcast_req, group_list);
1166
1167         list_for_each_entry_safe(req, tmp, &group->func[vf].pending, func_list) {
1168                 clear = 1;
1169                 if (group_first == req &&
1170                     (group->state == MCAST_JOIN_SENT ||
1171                      group->state == MCAST_LEAVE_SENT)) {
1172                         clear = cancel_delayed_work(&group->timeout_work);
1173                         pend = !clear;
1174                         group->state = MCAST_IDLE;
1175                 }
1176                 if (clear) {
1177                         --group->func[vf].num_pend_reqs;
1178                         list_del(&req->group_list);
1179                         list_del(&req->func_list);
1180                         kfree(req);
1181                         atomic_dec(&group->refcount);
1182                 }
1183         }
1184
1185         if (!pend && (!list_empty(&group->func[vf].pending) || group->func[vf].num_pend_reqs)) {
1186                 mcg_warn_group(group, "DRIVER BUG: list_empty %d, num_pend_reqs %d\n",
1187                                list_empty(&group->func[vf].pending), group->func[vf].num_pend_reqs);
1188         }
1189 }
1190
1191 static int push_deleteing_req(struct mcast_group *group, int slave)
1192 {
1193         struct mcast_req *req;
1194         struct mcast_req *pend_req;
1195
1196         if (!group->func[slave].join_state)
1197                 return 0;
1198
1199         req = kzalloc(sizeof *req, GFP_KERNEL);
1200         if (!req) {
1201                 mcg_warn_group(group, "failed allocation - may leave stall groups\n");
1202                 return -ENOMEM;
1203         }
1204
1205         if (!list_empty(&group->func[slave].pending)) {
1206                 pend_req = list_entry(group->func[slave].pending.prev, struct mcast_req, group_list);
1207                 if (pend_req->clean) {
1208                         kfree(req);
1209                         return 0;
1210                 }
1211         }
1212
1213         req->clean = 1;
1214         req->func = slave;
1215         req->group = group;
1216         ++group->func[slave].num_pend_reqs;
1217         build_leave_mad(req);
1218         queue_req(req);
1219         return 0;
1220 }
1221
1222 void clean_vf_mcast(struct mlx4_ib_demux_ctx *ctx, int slave)
1223 {
1224         struct mcast_group *group;
1225         struct rb_node *p;
1226
1227         mutex_lock(&ctx->mcg_table_lock);
1228         for (p = rb_first(&ctx->mcg_table); p; p = rb_next(p)) {
1229                 group = rb_entry(p, struct mcast_group, node);
1230                 mutex_lock(&group->lock);
1231                 if (atomic_read(&group->refcount)) {
1232                         /* clear pending requests of this VF */
1233                         clear_pending_reqs(group, slave);
1234                         push_deleteing_req(group, slave);
1235                 }
1236                 mutex_unlock(&group->lock);
1237         }
1238         mutex_unlock(&ctx->mcg_table_lock);
1239 }
1240
1241
1242 int mlx4_ib_mcg_init(void)
1243 {
1244         clean_wq = create_singlethread_workqueue("mlx4_ib_mcg");
1245         if (!clean_wq)
1246                 return -ENOMEM;
1247
1248         return 0;
1249 }
1250
1251 void mlx4_ib_mcg_destroy(void)
1252 {
1253         destroy_workqueue(clean_wq);
1254 }