xref: /linux/drivers/infiniband/hw/mlx4/cm.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 
35 #include <linux/mlx4/cmd.h>
36 #include <linux/rbtree.h>
37 #include <linux/idr.h>
38 #include <rdma/ib_cm.h>
39 
40 #include "mlx4_ib.h"
41 
42 #define CM_CLEANUP_CACHE_TIMEOUT  (30 * HZ)
43 #define CM_RTU_TIMEOUT		  (60 * HZ)
44 
45 struct id_map_entry {
46 	struct rb_node node;
47 
48 	u32 sl_cm_id;
49 	u32 pv_cm_id;
50 	int slave_id;
51 	int scheduled_delete;
52 	bool rtu_timeout;
53 	struct mlx4_ib_dev *dev;
54 
55 	struct list_head list;
56 	struct delayed_work timeout;
57 };
58 
59 struct rej_tmout_entry {
60 	int slave;
61 	u32 rem_pv_cm_id;
62 	struct delayed_work timeout;
63 	struct xarray *xa_rej_tmout;
64 };
65 
66 struct cm_generic_msg {
67 	struct ib_mad_hdr hdr;
68 
69 	__be32 local_comm_id;
70 	__be32 remote_comm_id;
71 	unsigned char unused[2];
72 	__be16 rej_reason;
73 };
74 
75 struct cm_sidr_generic_msg {
76 	struct ib_mad_hdr hdr;
77 	__be32 request_id;
78 };
79 
80 struct cm_req_msg {
81 	unsigned char unused[0x60];
82 	union ib_gid primary_path_sgid;
83 };
84 
85 static struct workqueue_struct *cm_wq;
86 
87 static void set_local_comm_id(struct ib_mad *mad, u32 cm_id)
88 {
89 	if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
90 		struct cm_sidr_generic_msg *msg =
91 			(struct cm_sidr_generic_msg *)mad;
92 		msg->request_id = cpu_to_be32(cm_id);
93 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
94 		pr_err("trying to set local_comm_id in SIDR_REP\n");
95 		return;
96 	} else {
97 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
98 		msg->local_comm_id = cpu_to_be32(cm_id);
99 	}
100 }
101 
102 static u32 get_local_comm_id(struct ib_mad *mad)
103 {
104 	if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
105 		struct cm_sidr_generic_msg *msg =
106 			(struct cm_sidr_generic_msg *)mad;
107 		return be32_to_cpu(msg->request_id);
108 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
109 		pr_err("trying to set local_comm_id in SIDR_REP\n");
110 		return -1;
111 	} else {
112 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
113 		return be32_to_cpu(msg->local_comm_id);
114 	}
115 }
116 
117 static void set_remote_comm_id(struct ib_mad *mad, u32 cm_id)
118 {
119 	if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
120 		struct cm_sidr_generic_msg *msg =
121 			(struct cm_sidr_generic_msg *)mad;
122 		msg->request_id = cpu_to_be32(cm_id);
123 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
124 		pr_err("trying to set remote_comm_id in SIDR_REQ\n");
125 		return;
126 	} else {
127 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
128 		msg->remote_comm_id = cpu_to_be32(cm_id);
129 	}
130 }
131 
132 static u32 get_remote_comm_id(struct ib_mad *mad)
133 {
134 	if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
135 		struct cm_sidr_generic_msg *msg =
136 			(struct cm_sidr_generic_msg *)mad;
137 		return be32_to_cpu(msg->request_id);
138 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
139 		pr_err("trying to set remote_comm_id in SIDR_REQ\n");
140 		return -1;
141 	} else {
142 		struct cm_generic_msg *msg = (struct cm_generic_msg *)mad;
143 		return be32_to_cpu(msg->remote_comm_id);
144 	}
145 }
146 
147 static union ib_gid gid_from_req_msg(struct ib_device *ibdev, struct ib_mad *mad)
148 {
149 	struct cm_req_msg *msg = (struct cm_req_msg *)mad;
150 
151 	return msg->primary_path_sgid;
152 }
153 
154 /* Lock should be taken before called */
155 static struct id_map_entry *
156 id_map_find_by_sl_id(struct ib_device *ibdev, u32 slave_id, u32 sl_cm_id)
157 {
158 	struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
159 	struct rb_node *node = sl_id_map->rb_node;
160 
161 	while (node) {
162 		struct id_map_entry *id_map_entry =
163 			rb_entry(node, struct id_map_entry, node);
164 
165 		if (id_map_entry->sl_cm_id > sl_cm_id)
166 			node = node->rb_left;
167 		else if (id_map_entry->sl_cm_id < sl_cm_id)
168 			node = node->rb_right;
169 		else if (id_map_entry->slave_id > slave_id)
170 			node = node->rb_left;
171 		else if (id_map_entry->slave_id < slave_id)
172 			node = node->rb_right;
173 		else
174 			return id_map_entry;
175 	}
176 	return NULL;
177 }
178 
179 static void id_map_ent_timeout(struct work_struct *work)
180 {
181 	struct delayed_work *delay = to_delayed_work(work);
182 	struct id_map_entry *ent = container_of(delay, struct id_map_entry, timeout);
183 	struct id_map_entry *found_ent;
184 	struct mlx4_ib_dev *dev = ent->dev;
185 	struct mlx4_ib_sriov *sriov = &dev->sriov;
186 	struct rb_root *sl_id_map = &sriov->sl_id_map;
187 
188 	spin_lock(&sriov->id_map_lock);
189 	if (!ent->scheduled_delete) {
190 		spin_unlock(&sriov->id_map_lock);
191 		return;
192 	}
193 	if (!xa_erase(&sriov->pv_id_table, ent->pv_cm_id))
194 		goto out;
195 	found_ent = id_map_find_by_sl_id(&dev->ib_dev, ent->slave_id, ent->sl_cm_id);
196 	if (found_ent && found_ent == ent)
197 		rb_erase(&found_ent->node, sl_id_map);
198 
199 out:
200 	list_del(&ent->list);
201 	spin_unlock(&sriov->id_map_lock);
202 	kfree(ent);
203 }
204 
205 static void sl_id_map_add(struct ib_device *ibdev, struct id_map_entry *new)
206 {
207 	struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map;
208 	struct rb_node **link = &sl_id_map->rb_node, *parent = NULL;
209 	struct id_map_entry *ent;
210 	int slave_id = new->slave_id;
211 	int sl_cm_id = new->sl_cm_id;
212 
213 	ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id);
214 	if (ent) {
215 		pr_debug("overriding existing sl_id_map entry (cm_id = %x)\n",
216 			 sl_cm_id);
217 
218 		rb_replace_node(&ent->node, &new->node, sl_id_map);
219 		return;
220 	}
221 
222 	/* Go to the bottom of the tree */
223 	while (*link) {
224 		parent = *link;
225 		ent = rb_entry(parent, struct id_map_entry, node);
226 
227 		if (ent->sl_cm_id > sl_cm_id || (ent->sl_cm_id == sl_cm_id && ent->slave_id > slave_id))
228 			link = &(*link)->rb_left;
229 		else
230 			link = &(*link)->rb_right;
231 	}
232 
233 	rb_link_node(&new->node, parent, link);
234 	rb_insert_color(&new->node, sl_id_map);
235 }
236 
237 static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id,
238 			     unsigned long timeout, bool rtu_timeout);
239 
240 static struct id_map_entry *
241 id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id,
242 	     u32 *pv_cm_id)
243 {
244 	int ret;
245 	struct id_map_entry *ent;
246 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
247 
248 	ent = kmalloc_obj(struct id_map_entry);
249 	if (!ent)
250 		return ERR_PTR(-ENOMEM);
251 
252 	ent->sl_cm_id = sl_cm_id;
253 	ent->slave_id = slave_id;
254 	ent->scheduled_delete = 0;
255 	ent->rtu_timeout = false;
256 	ent->dev = to_mdev(ibdev);
257 	INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout);
258 
259 	ret = xa_alloc_cyclic(&sriov->pv_id_table, &ent->pv_cm_id, ent,
260 			xa_limit_32b, &sriov->pv_id_next, GFP_KERNEL);
261 	if (ret >= 0) {
262 		spin_lock(&sriov->id_map_lock);
263 		sl_id_map_add(ibdev, ent);
264 		list_add_tail(&ent->list, &sriov->cm_list);
265 		*pv_cm_id = ent->pv_cm_id;
266 		schedule_delayed(ibdev, ent, CM_RTU_TIMEOUT, true);
267 		spin_unlock(&sriov->id_map_lock);
268 		return ent;
269 	}
270 
271 	/*error flow*/
272 	kfree(ent);
273 	mlx4_ib_warn(ibdev, "Allocation failed (err:0x%x)\n", ret);
274 	return ERR_PTR(-ENOMEM);
275 }
276 
277 static struct id_map_entry *
278 id_map_get(struct ib_device *ibdev, int *pv_cm_id, int slave_id, int sl_cm_id)
279 {
280 	struct id_map_entry *ent;
281 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
282 
283 	lockdep_assert_held(&sriov->id_map_lock);
284 	if (*pv_cm_id == -1) {
285 		ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id);
286 		if (ent)
287 			*pv_cm_id = (int) ent->pv_cm_id;
288 	} else
289 		ent = xa_load(&sriov->pv_id_table, *pv_cm_id);
290 
291 	return ent;
292 }
293 
294 static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id,
295 			     unsigned long timeout, bool rtu_timeout)
296 {
297 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
298 	unsigned long flags;
299 
300 	lockdep_assert_held(&sriov->id_map_lock);
301 	spin_lock_irqsave(&sriov->going_down_lock, flags);
302 	/*make sure that there is no schedule inside the scheduled work.*/
303 	if (!sriov->is_going_down || id->scheduled_delete) {
304 		id->scheduled_delete = 1;
305 		id->rtu_timeout = rtu_timeout;
306 		mod_delayed_work(cm_wq, &id->timeout, timeout);
307 	}
308 	spin_unlock_irqrestore(&sriov->going_down_lock, flags);
309 }
310 
311 #define REJ_REASON(m) be16_to_cpu(((struct cm_generic_msg *)(m))->rej_reason)
312 int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id,
313 		struct ib_mad *mad)
314 {
315 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
316 	struct id_map_entry *id;
317 	u32 pv_cm_id_to_set = 0;
318 	u32 sl_cm_id;
319 	int pv_cm_id = -1;
320 
321 	if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
322 	    mad->mad_hdr.attr_id == CM_REP_ATTR_ID ||
323 	    mad->mad_hdr.attr_id == CM_MRA_ATTR_ID ||
324 	    mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID ||
325 	    (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID && REJ_REASON(mad) == IB_CM_REJ_TIMEOUT)) {
326 		sl_cm_id = get_local_comm_id(mad);
327 		spin_lock(&sriov->id_map_lock);
328 		id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
329 		if (id) {
330 			pv_cm_id_to_set = id->pv_cm_id;
331 			if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID)
332 				schedule_delayed(ibdev, id,
333 						 CM_CLEANUP_CACHE_TIMEOUT,
334 						 false);
335 		}
336 		spin_unlock(&sriov->id_map_lock);
337 		if (id)
338 			goto cont;
339 		if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID)
340 			return 0;
341 		id = id_map_alloc(ibdev, slave_id, sl_cm_id,
342 				  &pv_cm_id_to_set);
343 		if (IS_ERR(id)) {
344 			mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n",
345 				__func__, slave_id, sl_cm_id);
346 			return PTR_ERR(id);
347 		}
348 	} else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID) {
349 		sl_cm_id = get_local_comm_id(mad);
350 		spin_lock(&sriov->id_map_lock);
351 		id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
352 		if (id) {
353 			pv_cm_id_to_set = id->pv_cm_id;
354 			schedule_delayed(ibdev, id, CM_CLEANUP_CACHE_TIMEOUT,
355 					 false);
356 		}
357 		spin_unlock(&sriov->id_map_lock);
358 		if (!id)
359 			return 0;
360 	} else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) {
361 		return 0;
362 	} else {
363 		sl_cm_id = get_local_comm_id(mad);
364 		spin_lock(&sriov->id_map_lock);
365 		id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id);
366 		if (id) {
367 			if (mad->mad_hdr.attr_id == CM_RTU_ATTR_ID &&
368 			    id->rtu_timeout) {
369 				id->rtu_timeout = false;
370 				if (cancel_delayed_work(&id->timeout))
371 					id->scheduled_delete = 0;
372 				else
373 					id = NULL;
374 			}
375 			if (id)
376 				pv_cm_id_to_set = id->pv_cm_id;
377 			if (id && mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID)
378 				schedule_delayed(ibdev, id,
379 						 CM_CLEANUP_CACHE_TIMEOUT,
380 						 false);
381 		}
382 		spin_unlock(&sriov->id_map_lock);
383 	}
384 
385 	if (!id) {
386 		pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL! attr_id: 0x%x\n",
387 			 slave_id, sl_cm_id, be16_to_cpu(mad->mad_hdr.attr_id));
388 		return -EINVAL;
389 	}
390 
391 cont:
392 	set_local_comm_id(mad, pv_cm_id_to_set);
393 	return 0;
394 }
395 
396 static void rej_tmout_timeout(struct work_struct *work)
397 {
398 	struct delayed_work *delay = to_delayed_work(work);
399 	struct rej_tmout_entry *item = container_of(delay, struct rej_tmout_entry, timeout);
400 	struct rej_tmout_entry *deleted;
401 
402 	deleted = xa_cmpxchg(item->xa_rej_tmout, item->rem_pv_cm_id, item, NULL, 0);
403 
404 	if (deleted != item)
405 		pr_debug("deleted(%p) != item(%p)\n", deleted, item);
406 
407 	kfree(item);
408 }
409 
410 static int alloc_rej_tmout(struct mlx4_ib_sriov *sriov, u32 rem_pv_cm_id, int slave)
411 {
412 	struct rej_tmout_entry *item;
413 	struct rej_tmout_entry *old;
414 	int ret = 0;
415 
416 	xa_lock(&sriov->xa_rej_tmout);
417 	item = xa_load(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id);
418 
419 	if (item) {
420 		if (xa_err(item))
421 			ret =  xa_err(item);
422 		else
423 			/* If a retry, adjust delayed work */
424 			mod_delayed_work(cm_wq, &item->timeout, CM_CLEANUP_CACHE_TIMEOUT);
425 		goto err_or_exists;
426 	}
427 	xa_unlock(&sriov->xa_rej_tmout);
428 
429 	item = kmalloc_obj(*item);
430 	if (!item)
431 		return -ENOMEM;
432 
433 	INIT_DELAYED_WORK(&item->timeout, rej_tmout_timeout);
434 	item->slave = slave;
435 	item->rem_pv_cm_id = rem_pv_cm_id;
436 	item->xa_rej_tmout = &sriov->xa_rej_tmout;
437 
438 	old = xa_cmpxchg(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id, NULL, item, GFP_KERNEL);
439 	if (old) {
440 		pr_debug(
441 			"Non-null old entry (%p) or error (%d) when inserting\n",
442 			old, xa_err(old));
443 		kfree(item);
444 		return xa_err(old);
445 	}
446 
447 	queue_delayed_work(cm_wq, &item->timeout, CM_CLEANUP_CACHE_TIMEOUT);
448 
449 	return 0;
450 
451 err_or_exists:
452 	xa_unlock(&sriov->xa_rej_tmout);
453 	return ret;
454 }
455 
456 static int lookup_rej_tmout_slave(struct mlx4_ib_sriov *sriov, u32 rem_pv_cm_id)
457 {
458 	struct rej_tmout_entry *item;
459 	int slave;
460 
461 	xa_lock(&sriov->xa_rej_tmout);
462 	item = xa_load(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id);
463 
464 	if (!item || xa_err(item)) {
465 		pr_debug("Could not find slave. rem_pv_cm_id 0x%x error: %d\n",
466 			 rem_pv_cm_id, xa_err(item));
467 		slave = !item ? -ENOENT : xa_err(item);
468 	} else {
469 		slave = item->slave;
470 	}
471 	xa_unlock(&sriov->xa_rej_tmout);
472 
473 	return slave;
474 }
475 
476 int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave,
477 			     struct ib_mad *mad)
478 {
479 	struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov;
480 	u32 rem_pv_cm_id = get_local_comm_id(mad);
481 	u32 pv_cm_id;
482 	u32 sl_cm_id = 0;
483 	struct id_map_entry *id;
484 	int pv_cm_id_int;
485 	int slave_id = 0;
486 	int sts;
487 
488 	if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID ||
489 	    mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) {
490 		union ib_gid gid;
491 
492 		if (!slave)
493 			return 0;
494 
495 		gid = gid_from_req_msg(ibdev, mad);
496 		*slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id);
497 		if (*slave < 0) {
498 			mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n",
499 				     be64_to_cpu(gid.global.interface_id));
500 			return -ENOENT;
501 		}
502 
503 		sts = alloc_rej_tmout(sriov, rem_pv_cm_id, *slave);
504 		if (sts)
505 			/* Even if this fails, we pass on the REQ to the slave */
506 			pr_debug("Could not allocate rej_tmout entry. rem_pv_cm_id 0x%x slave %d status %d\n",
507 				 rem_pv_cm_id, *slave, sts);
508 
509 		return 0;
510 	}
511 
512 	pv_cm_id = get_remote_comm_id(mad);
513 	pv_cm_id_int = pv_cm_id;
514 	spin_lock(&sriov->id_map_lock);
515 	id = id_map_get(ibdev, &pv_cm_id_int, -1, -1);
516 	if (id) {
517 		if (mad->mad_hdr.attr_id == CM_RTU_ATTR_ID &&
518 		    id->rtu_timeout) {
519 			id->rtu_timeout = false;
520 			if (cancel_delayed_work(&id->timeout))
521 				id->scheduled_delete = 0;
522 			else
523 				id = NULL;
524 		}
525 		if (id && slave)
526 			slave_id = id->slave_id;
527 		if (id)
528 			sl_cm_id = id->sl_cm_id;
529 		if (id && (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID ||
530 			   mad->mad_hdr.attr_id == CM_REJ_ATTR_ID))
531 			schedule_delayed(ibdev, id,
532 					 CM_CLEANUP_CACHE_TIMEOUT, false);
533 	}
534 	spin_unlock(&sriov->id_map_lock);
535 
536 	if (!id) {
537 		if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID &&
538 		    REJ_REASON(mad) == IB_CM_REJ_TIMEOUT && slave) {
539 			*slave = lookup_rej_tmout_slave(sriov, rem_pv_cm_id);
540 
541 			return (*slave < 0) ? *slave : 0;
542 		}
543 		pr_debug("Couldn't find an entry for pv_cm_id 0x%x, attr_id 0x%x\n",
544 			 pv_cm_id, be16_to_cpu(mad->mad_hdr.attr_id));
545 		return -ENOENT;
546 	}
547 
548 	if (slave)
549 		*slave = slave_id;
550 	set_remote_comm_id(mad, sl_cm_id);
551 
552 	return 0;
553 }
554 
555 void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev)
556 {
557 	spin_lock_init(&dev->sriov.id_map_lock);
558 	INIT_LIST_HEAD(&dev->sriov.cm_list);
559 	dev->sriov.sl_id_map = RB_ROOT;
560 	xa_init_flags(&dev->sriov.pv_id_table, XA_FLAGS_ALLOC);
561 	xa_init(&dev->sriov.xa_rej_tmout);
562 }
563 
564 static void rej_tmout_xa_cleanup(struct mlx4_ib_sriov *sriov, int slave)
565 {
566 	struct rej_tmout_entry *item;
567 	bool flush_needed = false;
568 	unsigned long id;
569 	int cnt = 0;
570 
571 	xa_lock(&sriov->xa_rej_tmout);
572 	xa_for_each(&sriov->xa_rej_tmout, id, item) {
573 		if (slave < 0 || slave == item->slave) {
574 			mod_delayed_work(cm_wq, &item->timeout, 0);
575 			flush_needed = true;
576 			++cnt;
577 		}
578 	}
579 	xa_unlock(&sriov->xa_rej_tmout);
580 
581 	if (flush_needed) {
582 		flush_workqueue(cm_wq);
583 		pr_debug("Deleted %d entries in xarray for slave %d during cleanup\n",
584 			 cnt, slave);
585 	}
586 
587 	if (slave < 0)
588 		WARN_ON(!xa_empty(&sriov->xa_rej_tmout));
589 }
590 
591 /* slave = -1 ==> all slaves */
592 /* TBD -- call paravirt clean for single slave.  Need for slave RESET event */
593 void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave)
594 {
595 	struct mlx4_ib_sriov *sriov = &dev->sriov;
596 	struct rb_root *sl_id_map = &sriov->sl_id_map;
597 	struct list_head lh;
598 	struct rb_node *nd;
599 	int need_flush = 0;
600 	struct id_map_entry *map, *tmp_map;
601 	/* cancel all delayed work queue entries */
602 	INIT_LIST_HEAD(&lh);
603 	spin_lock(&sriov->id_map_lock);
604 	list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
605 		if (slave < 0 || slave == map->slave_id) {
606 			if (map->scheduled_delete)
607 				need_flush |= !cancel_delayed_work(&map->timeout);
608 		}
609 	}
610 
611 	spin_unlock(&sriov->id_map_lock);
612 
613 	if (need_flush)
614 		flush_workqueue(cm_wq); /* make sure all timers were flushed */
615 
616 	/* now, remove all leftover entries from databases*/
617 	spin_lock(&sriov->id_map_lock);
618 	if (slave < 0) {
619 		while (rb_first(sl_id_map)) {
620 			struct id_map_entry *ent =
621 				rb_entry(rb_first(sl_id_map),
622 					 struct id_map_entry, node);
623 
624 			rb_erase(&ent->node, sl_id_map);
625 			xa_erase(&sriov->pv_id_table, ent->pv_cm_id);
626 		}
627 		list_splice_init(&dev->sriov.cm_list, &lh);
628 	} else {
629 		/* first, move nodes belonging to slave to db remove list */
630 		nd = rb_first(sl_id_map);
631 		while (nd) {
632 			struct id_map_entry *ent =
633 				rb_entry(nd, struct id_map_entry, node);
634 			nd = rb_next(nd);
635 			if (ent->slave_id == slave)
636 				list_move_tail(&ent->list, &lh);
637 		}
638 		/* remove those nodes from databases */
639 		list_for_each_entry_safe(map, tmp_map, &lh, list) {
640 			rb_erase(&map->node, sl_id_map);
641 			xa_erase(&sriov->pv_id_table, map->pv_cm_id);
642 		}
643 
644 		/* add remaining nodes from cm_list */
645 		list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) {
646 			if (slave == map->slave_id)
647 				list_move_tail(&map->list, &lh);
648 		}
649 	}
650 
651 	spin_unlock(&sriov->id_map_lock);
652 
653 	/* free any map entries left behind due to cancel_delayed_work above */
654 	list_for_each_entry_safe(map, tmp_map, &lh, list) {
655 		list_del(&map->list);
656 		kfree(map);
657 	}
658 
659 	rej_tmout_xa_cleanup(sriov, slave);
660 }
661 
662 int mlx4_ib_cm_init(void)
663 {
664 	cm_wq = alloc_workqueue("mlx4_ib_cm", WQ_PERCPU, 0);
665 	if (!cm_wq)
666 		return -ENOMEM;
667 
668 	return 0;
669 }
670 
671 void mlx4_ib_cm_destroy(void)
672 {
673 	destroy_workqueue(cm_wq);
674 }
675