xref: /freebsd/sys/dev/mlx4/mlx4_ib/mlx4_ib_cm.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
197549c34SHans Petter Selasky /*
297549c34SHans Petter Selasky  * Copyright (c) 2012 Mellanox Technologies. All rights reserved.
397549c34SHans Petter Selasky  *
497549c34SHans Petter Selasky  * This software is available to you under a choice of one of two
597549c34SHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
697549c34SHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
797549c34SHans Petter Selasky  * COPYING in the main directory of this source tree, or the
897549c34SHans Petter Selasky  * OpenIB.org BSD license below:
997549c34SHans Petter Selasky  *
1097549c34SHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
1197549c34SHans Petter Selasky  *     without modification, are permitted provided that the following
1297549c34SHans Petter Selasky  *     conditions are met:
1397549c34SHans Petter Selasky  *
1497549c34SHans Petter Selasky  *      - Redistributions of source code must retain the above
1597549c34SHans Petter Selasky  *        copyright notice, this list of conditions and the following
1697549c34SHans Petter Selasky  *        disclaimer.
1797549c34SHans Petter Selasky  *
1897549c34SHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
1997549c34SHans Petter Selasky  *        copyright notice, this list of conditions and the following
2097549c34SHans Petter Selasky  *        disclaimer in the documentation and/or other materials
2197549c34SHans Petter Selasky  *        provided with the distribution.
2297549c34SHans Petter Selasky  *
2397549c34SHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2497549c34SHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2597549c34SHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2697549c34SHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2797549c34SHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2897549c34SHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2997549c34SHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3097549c34SHans Petter Selasky  * SOFTWARE.
3197549c34SHans Petter Selasky  */
3297549c34SHans Petter Selasky 
3397549c34SHans Petter Selasky #include <rdma/ib_mad.h>
3497549c34SHans Petter Selasky 
3597549c34SHans Petter Selasky #include <dev/mlx4/cmd.h>
3697549c34SHans Petter Selasky #include <linux/rbtree.h>
3797549c34SHans Petter Selasky #include <linux/idr.h>
3897549c34SHans Petter Selasky #include <rdma/ib_cm.h>
3997549c34SHans Petter Selasky 
4097549c34SHans Petter Selasky #include "mlx4_ib.h"
4197549c34SHans Petter Selasky 
4297549c34SHans Petter Selasky #define CM_CLEANUP_CACHE_TIMEOUT  (5 * HZ)
4397549c34SHans Petter Selasky 
4497549c34SHans Petter Selasky struct id_map_entry {
4597549c34SHans Petter Selasky 	struct rb_node node;
4697549c34SHans Petter Selasky 
4797549c34SHans Petter Selasky 	u32 sl_cm_id;
4897549c34SHans Petter Selasky 	u32 pv_cm_id;
4997549c34SHans Petter Selasky 	int slave_id;
5097549c34SHans Petter Selasky 	int scheduled_delete;
5197549c34SHans Petter Selasky 	struct mlx4_ib_dev *dev;
5297549c34SHans Petter Selasky 
5397549c34SHans Petter Selasky 	struct list_head list;
5497549c34SHans Petter Selasky 	struct delayed_work timeout;
5597549c34SHans Petter Selasky };
5697549c34SHans Petter Selasky 
5797549c34SHans Petter Selasky struct cm_generic_msg {
5897549c34SHans Petter Selasky 	struct ib_mad_hdr hdr;
5997549c34SHans Petter Selasky 
6097549c34SHans Petter Selasky 	__be32 local_comm_id;
6197549c34SHans Petter Selasky 	__be32 remote_comm_id;
6297549c34SHans Petter Selasky };
6397549c34SHans Petter Selasky 
6497549c34SHans Petter Selasky struct cm_sidr_generic_msg {
6597549c34SHans Petter Selasky 	struct ib_mad_hdr hdr;
6697549c34SHans Petter Selasky 	__be32 request_id;
6797549c34SHans Petter Selasky };
6897549c34SHans Petter Selasky 
6997549c34SHans Petter Selasky struct cm_req_msg {
7097549c34SHans Petter Selasky 	unsigned char unused[0x60];
7197549c34SHans Petter Selasky 	union ib_gid primary_path_sgid;
7297549c34SHans Petter Selasky };
7397549c34SHans Petter Selasky 
7497549c34SHans Petter Selasky 
set_local_comm_id(struct ib_mad * mad,u32 cm_id)7597549c34SHans Petter Selasky static void set_local_comm_id(struct ib_mad *mad, u32 cm_id)
7697549c34SHans Petter Selasky {
7797549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
7897549c34SHans Petter Selasky 		struct cm_sidr_generic_msg *msg =
7997549c34SHans Petter Selasky 			(struct cm_sidr_generic_msg *)mad;
8097549c34SHans Petter Selasky 		msg->request_id = cpu_to_be32(cm_id);
8197549c34SHans Petter Selasky 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
8297549c34SHans Petter Selasky 		pr_err("trying to set local_comm_id in SIDR_REP\n");
8397549c34SHans Petter Selasky 		return;
8497549c34SHans Petter Selasky 	} else {
8597549c34SHans Petter Selasky 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
8697549c34SHans Petter Selasky 		msg->local_comm_id = cpu_to_be32(cm_id);
8797549c34SHans Petter Selasky 	}
8897549c34SHans Petter Selasky }
8997549c34SHans Petter Selasky 
get_local_comm_id(struct ib_mad * mad)9097549c34SHans Petter Selasky static u32 get_local_comm_id(struct ib_mad *mad)
9197549c34SHans Petter Selasky {
9297549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
9397549c34SHans Petter Selasky 		struct cm_sidr_generic_msg *msg =
9497549c34SHans Petter Selasky 			(struct cm_sidr_generic_msg *)mad;
9597549c34SHans Petter Selasky 		return be32_to_cpu(msg->request_id);
9697549c34SHans Petter Selasky 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
9797549c34SHans Petter Selasky 		pr_err("trying to set local_comm_id in SIDR_REP\n");
9897549c34SHans Petter Selasky 		return -1;
9997549c34SHans Petter Selasky 	} else {
10097549c34SHans Petter Selasky 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
10197549c34SHans Petter Selasky 		return be32_to_cpu(msg->local_comm_id);
10297549c34SHans Petter Selasky 	}
10397549c34SHans Petter Selasky }
10497549c34SHans Petter Selasky 
set_remote_comm_id(struct ib_mad * mad,u32 cm_id)10597549c34SHans Petter Selasky static void set_remote_comm_id(struct ib_mad *mad, u32 cm_id)
10697549c34SHans Petter Selasky {
10797549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
10897549c34SHans Petter Selasky 		struct cm_sidr_generic_msg *msg =
10997549c34SHans Petter Selasky 			(struct cm_sidr_generic_msg *)mad;
11097549c34SHans Petter Selasky 		msg->request_id = cpu_to_be32(cm_id);
11197549c34SHans Petter Selasky 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
11297549c34SHans Petter Selasky 		pr_err("trying to set remote_comm_id in SIDR_REQ\n");
11397549c34SHans Petter Selasky 		return;
11497549c34SHans Petter Selasky 	} else {
11597549c34SHans Petter Selasky 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
11697549c34SHans Petter Selasky 		msg->remote_comm_id = cpu_to_be32(cm_id);
11797549c34SHans Petter Selasky 	}
11897549c34SHans Petter Selasky }
11997549c34SHans Petter Selasky 
get_remote_comm_id(struct ib_mad * mad)12097549c34SHans Petter Selasky static u32 get_remote_comm_id(struct ib_mad *mad)
12197549c34SHans Petter Selasky {
12297549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
12397549c34SHans Petter Selasky 		struct cm_sidr_generic_msg *msg =
12497549c34SHans Petter Selasky 			(struct cm_sidr_generic_msg *)mad;
12597549c34SHans Petter Selasky 		return be32_to_cpu(msg->request_id);
12697549c34SHans Petter Selasky 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
12797549c34SHans Petter Selasky 		pr_err("trying to set remote_comm_id in SIDR_REQ\n");
12897549c34SHans Petter Selasky 		return -1;
12997549c34SHans Petter Selasky 	} else {
13097549c34SHans Petter Selasky 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
13197549c34SHans Petter Selasky 		return be32_to_cpu(msg->remote_comm_id);
13297549c34SHans Petter Selasky 	}
13397549c34SHans Petter Selasky }
13497549c34SHans Petter Selasky 
gid_from_req_msg(struct ib_device * ibdev,struct ib_mad * mad)13597549c34SHans Petter Selasky static union ib_gid gid_from_req_msg(struct ib_device *ibdev, struct ib_mad *mad)
13697549c34SHans Petter Selasky {
13797549c34SHans Petter Selasky 	struct cm_req_msg *msg = (struct cm_req_msg *)mad;
13897549c34SHans Petter Selasky 
13997549c34SHans Petter Selasky 	return msg->primary_path_sgid;
14097549c34SHans Petter Selasky }
14197549c34SHans Petter Selasky 
14297549c34SHans Petter Selasky /* Lock should be taken before called */
14397549c34SHans Petter Selasky static struct id_map_entry *
id_map_find_by_sl_id(struct ib_device * ibdev,u32 slave_id,u32 sl_cm_id)14497549c34SHans Petter Selasky id_map_find_by_sl_id(struct ib_device *ibdev, u32 slave_id, u32 sl_cm_id)
14597549c34SHans Petter Selasky {
14697549c34SHans Petter Selasky 	struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
14797549c34SHans Petter Selasky 	struct rb_node *node = sl_id_map->rb_node;
14897549c34SHans Petter Selasky 
14997549c34SHans Petter Selasky 	while (node) {
15097549c34SHans Petter Selasky 		struct id_map_entry *id_map_entry =
15197549c34SHans Petter Selasky 			rb_entry(node, struct id_map_entry, node);
15297549c34SHans Petter Selasky 
15397549c34SHans Petter Selasky 		if (id_map_entry->sl_cm_id > sl_cm_id)
15497549c34SHans Petter Selasky 			node = node->rb_left;
15597549c34SHans Petter Selasky 		else if (id_map_entry->sl_cm_id < sl_cm_id)
15697549c34SHans Petter Selasky 			node = node->rb_right;
15797549c34SHans Petter Selasky 		else if (id_map_entry->slave_id > slave_id)
15897549c34SHans Petter Selasky 			node = node->rb_left;
15997549c34SHans Petter Selasky 		else if (id_map_entry->slave_id < slave_id)
16097549c34SHans Petter Selasky 			node = node->rb_right;
16197549c34SHans Petter Selasky 		else
16297549c34SHans Petter Selasky 			return id_map_entry;
16397549c34SHans Petter Selasky 	}
16497549c34SHans Petter Selasky 	return NULL;
16597549c34SHans Petter Selasky }
16697549c34SHans Petter Selasky 
id_map_ent_timeout(struct work_struct * work)16797549c34SHans Petter Selasky static void id_map_ent_timeout(struct work_struct *work)
16897549c34SHans Petter Selasky {
16997549c34SHans Petter Selasky 	struct delayed_work *delay = to_delayed_work(work);
17097549c34SHans Petter Selasky 	struct id_map_entry *ent = container_of(delay, struct id_map_entry, timeout);
17197549c34SHans Petter Selasky 	struct id_map_entry *db_ent, *found_ent;
17297549c34SHans Petter Selasky 	struct mlx4_ib_dev *dev = ent->dev;
17397549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &dev->sriov;
17497549c34SHans Petter Selasky 	struct rb_root *sl_id_map = &sriov->sl_id_map;
17597549c34SHans Petter Selasky 	int pv_id = (int) ent->pv_cm_id;
17697549c34SHans Petter Selasky 
17797549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
17897549c34SHans Petter Selasky 	db_ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_id);
17997549c34SHans Petter Selasky 	if (!db_ent)
18097549c34SHans Petter Selasky 		goto out;
18197549c34SHans Petter Selasky 	found_ent = id_map_find_by_sl_id(&dev->ib_dev, ent->slave_id, ent->sl_cm_id);
18297549c34SHans Petter Selasky 	if (found_ent && found_ent == ent)
18397549c34SHans Petter Selasky 		rb_erase(&found_ent->node, sl_id_map);
18497549c34SHans Petter Selasky 	idr_remove(&sriov->pv_id_table, pv_id);
18597549c34SHans Petter Selasky 
18697549c34SHans Petter Selasky out:
18797549c34SHans Petter Selasky 	list_del(&ent->list);
18897549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
18997549c34SHans Petter Selasky 	kfree(ent);
19097549c34SHans Petter Selasky }
19197549c34SHans Petter Selasky 
id_map_find_del(struct ib_device * ibdev,int pv_cm_id)19297549c34SHans Petter Selasky static void id_map_find_del(struct ib_device *ibdev, int pv_cm_id)
19397549c34SHans Petter Selasky {
19497549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
19597549c34SHans Petter Selasky 	struct rb_root *sl_id_map = &sriov->sl_id_map;
19697549c34SHans Petter Selasky 	struct id_map_entry *ent, *found_ent;
19797549c34SHans Petter Selasky 
19897549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
19997549c34SHans Petter Selasky 	ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_cm_id);
20097549c34SHans Petter Selasky 	if (!ent)
20197549c34SHans Petter Selasky 		goto out;
20297549c34SHans Petter Selasky 	found_ent = id_map_find_by_sl_id(ibdev, ent->slave_id, ent->sl_cm_id);
20397549c34SHans Petter Selasky 	if (found_ent && found_ent == ent)
20497549c34SHans Petter Selasky 		rb_erase(&found_ent->node, sl_id_map);
20597549c34SHans Petter Selasky 	idr_remove(&sriov->pv_id_table, pv_cm_id);
20697549c34SHans Petter Selasky out:
20797549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
20897549c34SHans Petter Selasky }
20997549c34SHans Petter Selasky 
sl_id_map_add(struct ib_device * ibdev,struct id_map_entry * new)21097549c34SHans Petter Selasky static void sl_id_map_add(struct ib_device *ibdev, struct id_map_entry *new)
21197549c34SHans Petter Selasky {
21297549c34SHans Petter Selasky 	struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
21397549c34SHans Petter Selasky 	struct rb_node **link = &sl_id_map->rb_node, *parent = NULL;
21497549c34SHans Petter Selasky 	struct id_map_entry *ent;
21597549c34SHans Petter Selasky 	int slave_id = new->slave_id;
21697549c34SHans Petter Selasky 	int sl_cm_id = new->sl_cm_id;
21797549c34SHans Petter Selasky 
21897549c34SHans Petter Selasky 	ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id);
21997549c34SHans Petter Selasky 	if (ent) {
22097549c34SHans Petter Selasky 		pr_debug("overriding existing sl_id_map entry (cm_id = %x)\n",
22197549c34SHans Petter Selasky 			 sl_cm_id);
22297549c34SHans Petter Selasky 
22397549c34SHans Petter Selasky 		rb_replace_node(&ent->node, &new->node, sl_id_map);
22497549c34SHans Petter Selasky 		return;
22597549c34SHans Petter Selasky 	}
22697549c34SHans Petter Selasky 
22797549c34SHans Petter Selasky 	/* Go to the bottom of the tree */
22897549c34SHans Petter Selasky 	while (*link) {
22997549c34SHans Petter Selasky 		parent = *link;
23097549c34SHans Petter Selasky 		ent = rb_entry(parent, struct id_map_entry, node);
23197549c34SHans Petter Selasky 
23297549c34SHans Petter Selasky 		if (ent->sl_cm_id > sl_cm_id || (ent->sl_cm_id == sl_cm_id && ent->slave_id > slave_id))
23397549c34SHans Petter Selasky 			link = &(*link)->rb_left;
23497549c34SHans Petter Selasky 		else
23597549c34SHans Petter Selasky 			link = &(*link)->rb_right;
23697549c34SHans Petter Selasky 	}
23797549c34SHans Petter Selasky 
23897549c34SHans Petter Selasky 	rb_link_node(&new->node, parent, link);
23997549c34SHans Petter Selasky 	rb_insert_color(&new->node, sl_id_map);
24097549c34SHans Petter Selasky }
24197549c34SHans Petter Selasky 
24297549c34SHans Petter Selasky static struct id_map_entry *
id_map_alloc(struct ib_device * ibdev,int slave_id,u32 sl_cm_id)24397549c34SHans Petter Selasky id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id)
24497549c34SHans Petter Selasky {
245*8cc48704SHans Petter Selasky 	int ret;
24697549c34SHans Petter Selasky 	struct id_map_entry *ent;
24797549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
24897549c34SHans Petter Selasky 
24997549c34SHans Petter Selasky 	ent = kmalloc(sizeof (struct id_map_entry), GFP_KERNEL);
25097549c34SHans Petter Selasky 	if (!ent) {
25197549c34SHans Petter Selasky 		mlx4_ib_warn(ibdev, "Couldn't allocate id cache entry - out of memory\n");
25297549c34SHans Petter Selasky 		return ERR_PTR(-ENOMEM);
25397549c34SHans Petter Selasky 	}
25497549c34SHans Petter Selasky 
25597549c34SHans Petter Selasky 	ent->sl_cm_id = sl_cm_id;
25697549c34SHans Petter Selasky 	ent->slave_id = slave_id;
25797549c34SHans Petter Selasky 	ent->scheduled_delete = 0;
25897549c34SHans Petter Selasky 	ent->dev = to_mdev(ibdev);
25997549c34SHans Petter Selasky 	INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout);
26097549c34SHans Petter Selasky 
261*8cc48704SHans Petter Selasky 	idr_preload(GFP_KERNEL);
26297549c34SHans Petter Selasky 	spin_lock(&to_mdev(ibdev)->sriov.id_map_lock);
263*8cc48704SHans Petter Selasky 
264*8cc48704SHans Petter Selasky 	ret = idr_alloc_cyclic(&sriov->pv_id_table, ent, 0, 0, GFP_NOWAIT);
265*8cc48704SHans Petter Selasky 	if (ret >= 0) {
266*8cc48704SHans Petter Selasky 		ent->pv_cm_id = (u32)ret;
26797549c34SHans Petter Selasky 		sl_id_map_add(ibdev, ent);
268*8cc48704SHans Petter Selasky 		list_add_tail(&ent->list, &sriov->cm_list);
26997549c34SHans Petter Selasky 	}
27097549c34SHans Petter Selasky 
27197549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
272*8cc48704SHans Petter Selasky 	idr_preload_end();
273*8cc48704SHans Petter Selasky 
274*8cc48704SHans Petter Selasky 	if (ret >= 0)
27597549c34SHans Petter Selasky 		return ent;
276*8cc48704SHans Petter Selasky 
27797549c34SHans Petter Selasky 	/*error flow*/
27897549c34SHans Petter Selasky 	kfree(ent);
27997549c34SHans Petter Selasky 	mlx4_ib_warn(ibdev, "No more space in the idr (err:0x%x)\n", ret);
28097549c34SHans Petter Selasky 	return ERR_PTR(-ENOMEM);
28197549c34SHans Petter Selasky }
28297549c34SHans Petter Selasky 
28397549c34SHans Petter Selasky static struct id_map_entry *
id_map_get(struct ib_device * ibdev,int * pv_cm_id,int sl_cm_id,int slave_id)28497549c34SHans Petter Selasky id_map_get(struct ib_device *ibdev, int *pv_cm_id, int sl_cm_id, int slave_id)
28597549c34SHans Petter Selasky {
28697549c34SHans Petter Selasky 	struct id_map_entry *ent;
28797549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
28897549c34SHans Petter Selasky 
28997549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
29097549c34SHans Petter Selasky 	if (*pv_cm_id == -1) {
29197549c34SHans Petter Selasky 		ent = id_map_find_by_sl_id(ibdev, sl_cm_id, slave_id);
29297549c34SHans Petter Selasky 		if (ent)
29397549c34SHans Petter Selasky 			*pv_cm_id = (int) ent->pv_cm_id;
29497549c34SHans Petter Selasky 	} else
29597549c34SHans Petter Selasky 		ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, *pv_cm_id);
29697549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
29797549c34SHans Petter Selasky 
29897549c34SHans Petter Selasky 	return ent;
29997549c34SHans Petter Selasky }
30097549c34SHans Petter Selasky 
schedule_delayed(struct ib_device * ibdev,struct id_map_entry * id)30197549c34SHans Petter Selasky static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id)
30297549c34SHans Petter Selasky {
30397549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
30497549c34SHans Petter Selasky 	unsigned long flags;
30597549c34SHans Petter Selasky 
30697549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
30797549c34SHans Petter Selasky 	spin_lock_irqsave(&sriov->going_down_lock, flags);
30897549c34SHans Petter Selasky 	/*make sure that there is no schedule inside the scheduled work.*/
30997549c34SHans Petter Selasky 	if (!sriov->is_going_down) {
31097549c34SHans Petter Selasky 		id->scheduled_delete = 1;
31197549c34SHans Petter Selasky 		schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT);
31297549c34SHans Petter Selasky 	}
31397549c34SHans Petter Selasky 	spin_unlock_irqrestore(&sriov->going_down_lock, flags);
31497549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
31597549c34SHans Petter Selasky }
31697549c34SHans Petter Selasky 
mlx4_ib_multiplex_cm_handler(struct ib_device * ibdev,int port,int slave_id,struct ib_mad * mad)31797549c34SHans Petter Selasky int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id,
31897549c34SHans Petter Selasky 		struct ib_mad *mad)
31997549c34SHans Petter Selasky {
32097549c34SHans Petter Selasky 	struct id_map_entry *id;
32197549c34SHans Petter Selasky 	u32 sl_cm_id;
32297549c34SHans Petter Selasky 	int pv_cm_id = -1;
32397549c34SHans Petter Selasky 
32497549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
32597549c34SHans Petter Selasky 			mad->mad_hdr.attr_id == CM_REP_ATTR_ID ||
326*8cc48704SHans Petter Selasky 			mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
32797549c34SHans Petter Selasky 		sl_cm_id = get_local_comm_id(mad);
32897549c34SHans Petter Selasky 		id = id_map_alloc(ibdev, slave_id, sl_cm_id);
32997549c34SHans Petter Selasky 		if (IS_ERR(id)) {
33097549c34SHans Petter Selasky 			mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n",
33197549c34SHans Petter Selasky 				__func__, slave_id, sl_cm_id);
33297549c34SHans Petter Selasky 			return PTR_ERR(id);
33397549c34SHans Petter Selasky 		}
33497549c34SHans Petter Selasky 	} else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
33597549c34SHans Petter Selasky 		   mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
33697549c34SHans Petter Selasky 		return 0;
33797549c34SHans Petter Selasky 	} else {
33897549c34SHans Petter Selasky 		sl_cm_id = get_local_comm_id(mad);
33997549c34SHans Petter Selasky 		id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
34097549c34SHans Petter Selasky 	}
34197549c34SHans Petter Selasky 
34297549c34SHans Petter Selasky 	if (!id) {
34397549c34SHans Petter Selasky 		pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL!\n",
34497549c34SHans Petter Selasky 			 slave_id, sl_cm_id);
34597549c34SHans Petter Selasky 		return -EINVAL;
34697549c34SHans Petter Selasky 	}
34797549c34SHans Petter Selasky 
34897549c34SHans Petter Selasky 	set_local_comm_id(mad, id->pv_cm_id);
34997549c34SHans Petter Selasky 
35097549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
35197549c34SHans Petter Selasky 		schedule_delayed(ibdev, id);
35297549c34SHans Petter Selasky 	else if (mad->mad_hdr.attr_id == CM_DREP_ATTR_ID)
35397549c34SHans Petter Selasky 		id_map_find_del(ibdev, pv_cm_id);
35497549c34SHans Petter Selasky 
35597549c34SHans Petter Selasky 	return 0;
35697549c34SHans Petter Selasky }
35797549c34SHans Petter Selasky 
mlx4_ib_demux_cm_handler(struct ib_device * ibdev,int port,int * slave,struct ib_mad * mad)35897549c34SHans Petter Selasky int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave,
359*8cc48704SHans Petter Selasky 			     struct ib_mad *mad)
36097549c34SHans Petter Selasky {
36197549c34SHans Petter Selasky 	u32 pv_cm_id;
36297549c34SHans Petter Selasky 	struct id_map_entry *id;
36397549c34SHans Petter Selasky 
36497549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
36597549c34SHans Petter Selasky 	    mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
36697549c34SHans Petter Selasky 		union ib_gid gid;
36797549c34SHans Petter Selasky 
368*8cc48704SHans Petter Selasky 		if (!slave)
36997549c34SHans Petter Selasky 			return 0;
37097549c34SHans Petter Selasky 
37197549c34SHans Petter Selasky 		gid = gid_from_req_msg(ibdev, mad);
37297549c34SHans Petter Selasky 		*slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id);
37397549c34SHans Petter Selasky 		if (*slave < 0) {
37497549c34SHans Petter Selasky 			mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n",
37597549c34SHans Petter Selasky 					(unsigned long long)gid.global.interface_id);
37697549c34SHans Petter Selasky 			return -ENOENT;
37797549c34SHans Petter Selasky 		}
37897549c34SHans Petter Selasky 		return 0;
37997549c34SHans Petter Selasky 	}
38097549c34SHans Petter Selasky 
38197549c34SHans Petter Selasky 	pv_cm_id = get_remote_comm_id(mad);
38297549c34SHans Petter Selasky 	id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1);
38397549c34SHans Petter Selasky 
38497549c34SHans Petter Selasky 	if (!id) {
38597549c34SHans Petter Selasky 		pr_debug("Couldn't find an entry for pv_cm_id 0x%x\n", pv_cm_id);
38697549c34SHans Petter Selasky 		return -ENOENT;
38797549c34SHans Petter Selasky 	}
38897549c34SHans Petter Selasky 
389*8cc48704SHans Petter Selasky 	if (slave)
39097549c34SHans Petter Selasky 		*slave = id->slave_id;
39197549c34SHans Petter Selasky 	set_remote_comm_id(mad, id->sl_cm_id);
39297549c34SHans Petter Selasky 
39397549c34SHans Petter Selasky 	if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
39497549c34SHans Petter Selasky 		schedule_delayed(ibdev, id);
39597549c34SHans Petter Selasky 	else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID ||
39697549c34SHans Petter Selasky 			mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) {
39797549c34SHans Petter Selasky 		id_map_find_del(ibdev, (int) pv_cm_id);
39897549c34SHans Petter Selasky 	}
39997549c34SHans Petter Selasky 
40097549c34SHans Petter Selasky 	return 0;
40197549c34SHans Petter Selasky }
40297549c34SHans Petter Selasky 
mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev * dev)40397549c34SHans Petter Selasky void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
40497549c34SHans Petter Selasky {
40597549c34SHans Petter Selasky 	spin_lock_init(&dev->sriov.id_map_lock);
40697549c34SHans Petter Selasky 	INIT_LIST_HEAD(&dev->sriov.cm_list);
40797549c34SHans Petter Selasky 	dev->sriov.sl_id_map = RB_ROOT;
40897549c34SHans Petter Selasky 	idr_init(&dev->sriov.pv_id_table);
40997549c34SHans Petter Selasky }
41097549c34SHans Petter Selasky 
41197549c34SHans Petter Selasky /* slave = -1 ==> all slaves */
41297549c34SHans Petter Selasky /* TBD -- call paravirt clean for single slave.  Need for slave RESET event */
mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev * dev,int slave)41397549c34SHans Petter Selasky void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave)
41497549c34SHans Petter Selasky {
41597549c34SHans Petter Selasky 	struct mlx4_ib_sriov *sriov = &dev->sriov;
41697549c34SHans Petter Selasky 	struct rb_root *sl_id_map = &sriov->sl_id_map;
41797549c34SHans Petter Selasky 	struct list_head lh;
41897549c34SHans Petter Selasky 	struct rb_node *nd;
41997549c34SHans Petter Selasky 	int need_flush = 1;
42097549c34SHans Petter Selasky 	struct id_map_entry *map, *tmp_map;
42197549c34SHans Petter Selasky 	/* cancel all delayed work queue entries */
42297549c34SHans Petter Selasky 	INIT_LIST_HEAD(&lh);
42397549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
42497549c34SHans Petter Selasky 	list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
42597549c34SHans Petter Selasky 		if (slave < 0 || slave == map->slave_id) {
42697549c34SHans Petter Selasky 			if (map->scheduled_delete)
42797549c34SHans Petter Selasky 				need_flush &= !!cancel_delayed_work(&map->timeout);
42897549c34SHans Petter Selasky 		}
42997549c34SHans Petter Selasky 	}
43097549c34SHans Petter Selasky 
43197549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
43297549c34SHans Petter Selasky 
43397549c34SHans Petter Selasky 	if (!need_flush)
43497549c34SHans Petter Selasky 		flush_scheduled_work(); /* make sure all timers were flushed */
43597549c34SHans Petter Selasky 
43697549c34SHans Petter Selasky 	/* now, remove all leftover entries from databases*/
43797549c34SHans Petter Selasky 	spin_lock(&sriov->id_map_lock);
43897549c34SHans Petter Selasky 	if (slave < 0) {
43997549c34SHans Petter Selasky 		while (rb_first(sl_id_map)) {
44097549c34SHans Petter Selasky 			struct id_map_entry *ent =
44197549c34SHans Petter Selasky 				rb_entry(rb_first(sl_id_map),
44297549c34SHans Petter Selasky 					 struct id_map_entry, node);
44397549c34SHans Petter Selasky 
44497549c34SHans Petter Selasky 			rb_erase(&ent->node, sl_id_map);
44597549c34SHans Petter Selasky 			idr_remove(&sriov->pv_id_table, (int) ent->pv_cm_id);
44697549c34SHans Petter Selasky 		}
44797549c34SHans Petter Selasky 		list_splice_init(&dev->sriov.cm_list, &lh);
44897549c34SHans Petter Selasky 	} else {
44997549c34SHans Petter Selasky 		/* first, move nodes belonging to slave to db remove list */
45097549c34SHans Petter Selasky 		nd = rb_first(sl_id_map);
45197549c34SHans Petter Selasky 		while (nd) {
45297549c34SHans Petter Selasky 			struct id_map_entry *ent =
45397549c34SHans Petter Selasky 				rb_entry(nd, struct id_map_entry, node);
45497549c34SHans Petter Selasky 			nd = rb_next(nd);
45597549c34SHans Petter Selasky 			if (ent->slave_id == slave)
45697549c34SHans Petter Selasky 				list_move_tail(&ent->list, &lh);
45797549c34SHans Petter Selasky 		}
45897549c34SHans Petter Selasky 		/* remove those nodes from databases */
45997549c34SHans Petter Selasky 		list_for_each_entry_safe(map, tmp_map, &lh, list) {
46097549c34SHans Petter Selasky 			rb_erase(&map->node, sl_id_map);
46197549c34SHans Petter Selasky 			idr_remove(&sriov->pv_id_table, (int) map->pv_cm_id);
46297549c34SHans Petter Selasky 		}
46397549c34SHans Petter Selasky 
46497549c34SHans Petter Selasky 		/* add remaining nodes from cm_list */
46597549c34SHans Petter Selasky 		list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
46697549c34SHans Petter Selasky 			if (slave == map->slave_id)
46797549c34SHans Petter Selasky 				list_move_tail(&map->list, &lh);
46897549c34SHans Petter Selasky 		}
46997549c34SHans Petter Selasky 	}
47097549c34SHans Petter Selasky 
47197549c34SHans Petter Selasky 	spin_unlock(&sriov->id_map_lock);
47297549c34SHans Petter Selasky 
47397549c34SHans Petter Selasky 	/* free any map entries left behind due to cancel_delayed_work above */
47497549c34SHans Petter Selasky 	list_for_each_entry_safe(map, tmp_map, &lh, list) {
47597549c34SHans Petter Selasky 		list_del(&map->list);
47697549c34SHans Petter Selasky 		kfree(map);
47797549c34SHans Petter Selasky 	}
47897549c34SHans Petter Selasky }
479