13cf69cc8SAmir Vadai /* 23cf69cc8SAmir Vadai * Copyright (c) 2012 Mellanox Technologies. All rights reserved. 33cf69cc8SAmir Vadai * 43cf69cc8SAmir Vadai * This software is available to you under a choice of one of two 53cf69cc8SAmir Vadai * licenses. You may choose to be licensed under the terms of the GNU 63cf69cc8SAmir Vadai * General Public License (GPL) Version 2, available from the file 73cf69cc8SAmir Vadai * COPYING in the main directory of this source tree, or the 83cf69cc8SAmir Vadai * OpenIB.org BSD license below: 93cf69cc8SAmir Vadai * 103cf69cc8SAmir Vadai * Redistribution and use in source and binary forms, with or 113cf69cc8SAmir Vadai * without modification, are permitted provided that the following 123cf69cc8SAmir Vadai * conditions are met: 133cf69cc8SAmir Vadai * 143cf69cc8SAmir Vadai * - Redistributions of source code must retain the above 153cf69cc8SAmir Vadai * copyright notice, this list of conditions and the following 163cf69cc8SAmir Vadai * disclaimer. 173cf69cc8SAmir Vadai * 183cf69cc8SAmir Vadai * - Redistributions in binary form must reproduce the above 193cf69cc8SAmir Vadai * copyright notice, this list of conditions and the following 203cf69cc8SAmir Vadai * disclaimer in the documentation and/or other materials 213cf69cc8SAmir Vadai * provided with the distribution. 223cf69cc8SAmir Vadai * 233cf69cc8SAmir Vadai * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 243cf69cc8SAmir Vadai * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 253cf69cc8SAmir Vadai * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 263cf69cc8SAmir Vadai * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 273cf69cc8SAmir Vadai * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 283cf69cc8SAmir Vadai * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 293cf69cc8SAmir Vadai * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 303cf69cc8SAmir Vadai * SOFTWARE. 313cf69cc8SAmir Vadai */ 323cf69cc8SAmir Vadai 333cf69cc8SAmir Vadai #include <rdma/ib_mad.h> 343cf69cc8SAmir Vadai 353cf69cc8SAmir Vadai #include <linux/mlx4/cmd.h> 363cf69cc8SAmir Vadai #include <linux/rbtree.h> 373cf69cc8SAmir Vadai #include <linux/idr.h> 383cf69cc8SAmir Vadai #include <rdma/ib_cm.h> 393cf69cc8SAmir Vadai 403cf69cc8SAmir Vadai #include "mlx4_ib.h" 413cf69cc8SAmir Vadai 423cf69cc8SAmir Vadai #define CM_CLEANUP_CACHE_TIMEOUT (5 * HZ) 433cf69cc8SAmir Vadai 443cf69cc8SAmir Vadai struct id_map_entry { 453cf69cc8SAmir Vadai struct rb_node node; 463cf69cc8SAmir Vadai 473cf69cc8SAmir Vadai u32 sl_cm_id; 483cf69cc8SAmir Vadai u32 pv_cm_id; 493cf69cc8SAmir Vadai int slave_id; 503cf69cc8SAmir Vadai int scheduled_delete; 513cf69cc8SAmir Vadai struct mlx4_ib_dev *dev; 523cf69cc8SAmir Vadai 533cf69cc8SAmir Vadai struct list_head list; 543cf69cc8SAmir Vadai struct delayed_work timeout; 553cf69cc8SAmir Vadai }; 563cf69cc8SAmir Vadai 573cf69cc8SAmir Vadai struct cm_generic_msg { 583cf69cc8SAmir Vadai struct ib_mad_hdr hdr; 593cf69cc8SAmir Vadai 603cf69cc8SAmir Vadai __be32 local_comm_id; 613cf69cc8SAmir Vadai __be32 remote_comm_id; 623cf69cc8SAmir Vadai }; 633cf69cc8SAmir Vadai 64*ceb5433bSShani Michaelli struct cm_sidr_generic_msg { 65*ceb5433bSShani Michaelli struct ib_mad_hdr hdr; 66*ceb5433bSShani Michaelli __be32 request_id; 67*ceb5433bSShani Michaelli }; 68*ceb5433bSShani Michaelli 693cf69cc8SAmir Vadai struct cm_req_msg { 703cf69cc8SAmir Vadai unsigned char unused[0x60]; 713cf69cc8SAmir Vadai union ib_gid primary_path_sgid; 723cf69cc8SAmir Vadai }; 733cf69cc8SAmir Vadai 743cf69cc8SAmir Vadai 753cf69cc8SAmir Vadai static void set_local_comm_id(struct ib_mad *mad, u32 cm_id) 763cf69cc8SAmir Vadai { 77*ceb5433bSShani Michaelli if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 78*ceb5433bSShani Michaelli struct cm_sidr_generic_msg *msg = 79*ceb5433bSShani Michaelli (struct cm_sidr_generic_msg *)mad; 80*ceb5433bSShani Michaelli msg->request_id = cpu_to_be32(cm_id); 81*ceb5433bSShani Michaelli } else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 82*ceb5433bSShani Michaelli pr_err("trying to set local_comm_id in SIDR_REP\n"); 83*ceb5433bSShani Michaelli return; 84*ceb5433bSShani Michaelli } else { 853cf69cc8SAmir Vadai struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 863cf69cc8SAmir Vadai msg->local_comm_id = cpu_to_be32(cm_id); 873cf69cc8SAmir Vadai } 88*ceb5433bSShani Michaelli } 893cf69cc8SAmir Vadai 903cf69cc8SAmir Vadai static u32 get_local_comm_id(struct ib_mad *mad) 913cf69cc8SAmir Vadai { 92*ceb5433bSShani Michaelli if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 93*ceb5433bSShani Michaelli struct cm_sidr_generic_msg *msg = 94*ceb5433bSShani Michaelli (struct cm_sidr_generic_msg *)mad; 95*ceb5433bSShani Michaelli return be32_to_cpu(msg->request_id); 96*ceb5433bSShani Michaelli } else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 97*ceb5433bSShani Michaelli pr_err("trying to set local_comm_id in SIDR_REP\n"); 98*ceb5433bSShani Michaelli return -1; 99*ceb5433bSShani Michaelli } else { 1003cf69cc8SAmir Vadai struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1013cf69cc8SAmir Vadai return be32_to_cpu(msg->local_comm_id); 1023cf69cc8SAmir Vadai } 103*ceb5433bSShani Michaelli } 1043cf69cc8SAmir Vadai 1053cf69cc8SAmir Vadai static void set_remote_comm_id(struct ib_mad *mad, u32 cm_id) 1063cf69cc8SAmir Vadai { 107*ceb5433bSShani Michaelli if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 108*ceb5433bSShani Michaelli struct cm_sidr_generic_msg *msg = 109*ceb5433bSShani Michaelli (struct cm_sidr_generic_msg *)mad; 110*ceb5433bSShani Michaelli msg->request_id = cpu_to_be32(cm_id); 111*ceb5433bSShani Michaelli } else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 112*ceb5433bSShani Michaelli pr_err("trying to set remote_comm_id in SIDR_REQ\n"); 113*ceb5433bSShani Michaelli return; 114*ceb5433bSShani Michaelli } else { 1153cf69cc8SAmir Vadai struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1163cf69cc8SAmir Vadai msg->remote_comm_id = cpu_to_be32(cm_id); 1173cf69cc8SAmir Vadai } 118*ceb5433bSShani Michaelli } 1193cf69cc8SAmir Vadai 1203cf69cc8SAmir Vadai static u32 get_remote_comm_id(struct ib_mad *mad) 1213cf69cc8SAmir Vadai { 122*ceb5433bSShani Michaelli if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 123*ceb5433bSShani Michaelli struct cm_sidr_generic_msg *msg = 124*ceb5433bSShani Michaelli (struct cm_sidr_generic_msg *)mad; 125*ceb5433bSShani Michaelli return be32_to_cpu(msg->request_id); 126*ceb5433bSShani Michaelli } else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 127*ceb5433bSShani Michaelli pr_err("trying to set remote_comm_id in SIDR_REQ\n"); 128*ceb5433bSShani Michaelli return -1; 129*ceb5433bSShani Michaelli } else { 1303cf69cc8SAmir Vadai struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1313cf69cc8SAmir Vadai return be32_to_cpu(msg->remote_comm_id); 1323cf69cc8SAmir Vadai } 133*ceb5433bSShani Michaelli } 1343cf69cc8SAmir Vadai 1353cf69cc8SAmir Vadai static union ib_gid gid_from_req_msg(struct ib_device *ibdev, struct ib_mad *mad) 1363cf69cc8SAmir Vadai { 1373cf69cc8SAmir Vadai struct cm_req_msg *msg = (struct cm_req_msg *)mad; 1383cf69cc8SAmir Vadai 1393cf69cc8SAmir Vadai return msg->primary_path_sgid; 1403cf69cc8SAmir Vadai } 1413cf69cc8SAmir Vadai 1423cf69cc8SAmir Vadai /* Lock should be taken before called */ 1433cf69cc8SAmir Vadai static struct id_map_entry * 1443cf69cc8SAmir Vadai id_map_find_by_sl_id(struct ib_device *ibdev, u32 slave_id, u32 sl_cm_id) 1453cf69cc8SAmir Vadai { 1463cf69cc8SAmir Vadai struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map; 1473cf69cc8SAmir Vadai struct rb_node *node = sl_id_map->rb_node; 1483cf69cc8SAmir Vadai 1493cf69cc8SAmir Vadai while (node) { 1503cf69cc8SAmir Vadai struct id_map_entry *id_map_entry = 1513cf69cc8SAmir Vadai rb_entry(node, struct id_map_entry, node); 1523cf69cc8SAmir Vadai 1533cf69cc8SAmir Vadai if (id_map_entry->sl_cm_id > sl_cm_id) 1543cf69cc8SAmir Vadai node = node->rb_left; 1553cf69cc8SAmir Vadai else if (id_map_entry->sl_cm_id < sl_cm_id) 1563cf69cc8SAmir Vadai node = node->rb_right; 1573cf69cc8SAmir Vadai else if (id_map_entry->slave_id > slave_id) 1583cf69cc8SAmir Vadai node = node->rb_left; 1593cf69cc8SAmir Vadai else if (id_map_entry->slave_id < slave_id) 1603cf69cc8SAmir Vadai node = node->rb_right; 1613cf69cc8SAmir Vadai else 1623cf69cc8SAmir Vadai return id_map_entry; 1633cf69cc8SAmir Vadai } 1643cf69cc8SAmir Vadai return NULL; 1653cf69cc8SAmir Vadai } 1663cf69cc8SAmir Vadai 1673cf69cc8SAmir Vadai static void id_map_ent_timeout(struct work_struct *work) 1683cf69cc8SAmir Vadai { 1693cf69cc8SAmir Vadai struct delayed_work *delay = to_delayed_work(work); 1703cf69cc8SAmir Vadai struct id_map_entry *ent = container_of(delay, struct id_map_entry, timeout); 1713cf69cc8SAmir Vadai struct id_map_entry *db_ent, *found_ent; 1723cf69cc8SAmir Vadai struct mlx4_ib_dev *dev = ent->dev; 1733cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &dev->sriov; 1743cf69cc8SAmir Vadai struct rb_root *sl_id_map = &sriov->sl_id_map; 1753cf69cc8SAmir Vadai int pv_id = (int) ent->pv_cm_id; 1763cf69cc8SAmir Vadai 1773cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 1783cf69cc8SAmir Vadai db_ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_id); 1793cf69cc8SAmir Vadai if (!db_ent) 1803cf69cc8SAmir Vadai goto out; 1813cf69cc8SAmir Vadai found_ent = id_map_find_by_sl_id(&dev->ib_dev, ent->slave_id, ent->sl_cm_id); 1823cf69cc8SAmir Vadai if (found_ent && found_ent == ent) 1833cf69cc8SAmir Vadai rb_erase(&found_ent->node, sl_id_map); 1843cf69cc8SAmir Vadai idr_remove(&sriov->pv_id_table, pv_id); 1853cf69cc8SAmir Vadai 1863cf69cc8SAmir Vadai out: 1873cf69cc8SAmir Vadai list_del(&ent->list); 1883cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 1893cf69cc8SAmir Vadai kfree(ent); 1903cf69cc8SAmir Vadai } 1913cf69cc8SAmir Vadai 1923cf69cc8SAmir Vadai static void id_map_find_del(struct ib_device *ibdev, int pv_cm_id) 1933cf69cc8SAmir Vadai { 1943cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 1953cf69cc8SAmir Vadai struct rb_root *sl_id_map = &sriov->sl_id_map; 1963cf69cc8SAmir Vadai struct id_map_entry *ent, *found_ent; 1973cf69cc8SAmir Vadai 1983cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 1993cf69cc8SAmir Vadai ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, pv_cm_id); 2003cf69cc8SAmir Vadai if (!ent) 2013cf69cc8SAmir Vadai goto out; 2023cf69cc8SAmir Vadai found_ent = id_map_find_by_sl_id(ibdev, ent->slave_id, ent->sl_cm_id); 2033cf69cc8SAmir Vadai if (found_ent && found_ent == ent) 2043cf69cc8SAmir Vadai rb_erase(&found_ent->node, sl_id_map); 2053cf69cc8SAmir Vadai idr_remove(&sriov->pv_id_table, pv_cm_id); 2063cf69cc8SAmir Vadai out: 2073cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 2083cf69cc8SAmir Vadai } 2093cf69cc8SAmir Vadai 2103cf69cc8SAmir Vadai static void sl_id_map_add(struct ib_device *ibdev, struct id_map_entry *new) 2113cf69cc8SAmir Vadai { 2123cf69cc8SAmir Vadai struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map; 2133cf69cc8SAmir Vadai struct rb_node **link = &sl_id_map->rb_node, *parent = NULL; 2143cf69cc8SAmir Vadai struct id_map_entry *ent; 2153cf69cc8SAmir Vadai int slave_id = new->slave_id; 2163cf69cc8SAmir Vadai int sl_cm_id = new->sl_cm_id; 2173cf69cc8SAmir Vadai 2183cf69cc8SAmir Vadai ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id); 2193cf69cc8SAmir Vadai if (ent) { 2203cf69cc8SAmir Vadai pr_debug("overriding existing sl_id_map entry (cm_id = %x)\n", 2213cf69cc8SAmir Vadai sl_cm_id); 2223cf69cc8SAmir Vadai 2233cf69cc8SAmir Vadai rb_replace_node(&ent->node, &new->node, sl_id_map); 2243cf69cc8SAmir Vadai return; 2253cf69cc8SAmir Vadai } 2263cf69cc8SAmir Vadai 2273cf69cc8SAmir Vadai /* Go to the bottom of the tree */ 2283cf69cc8SAmir Vadai while (*link) { 2293cf69cc8SAmir Vadai parent = *link; 2303cf69cc8SAmir Vadai ent = rb_entry(parent, struct id_map_entry, node); 2313cf69cc8SAmir Vadai 2323cf69cc8SAmir Vadai if (ent->sl_cm_id > sl_cm_id || (ent->sl_cm_id == sl_cm_id && ent->slave_id > slave_id)) 2333cf69cc8SAmir Vadai link = &(*link)->rb_left; 2343cf69cc8SAmir Vadai else 2353cf69cc8SAmir Vadai link = &(*link)->rb_right; 2363cf69cc8SAmir Vadai } 2373cf69cc8SAmir Vadai 2383cf69cc8SAmir Vadai rb_link_node(&new->node, parent, link); 2393cf69cc8SAmir Vadai rb_insert_color(&new->node, sl_id_map); 2403cf69cc8SAmir Vadai } 2413cf69cc8SAmir Vadai 2423cf69cc8SAmir Vadai static struct id_map_entry * 2433cf69cc8SAmir Vadai id_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id) 2443cf69cc8SAmir Vadai { 2456a920060STejun Heo int ret; 2463cf69cc8SAmir Vadai struct id_map_entry *ent; 2473cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 2483cf69cc8SAmir Vadai 2493cf69cc8SAmir Vadai ent = kmalloc(sizeof (struct id_map_entry), GFP_KERNEL); 2503cf69cc8SAmir Vadai if (!ent) { 2513cf69cc8SAmir Vadai mlx4_ib_warn(ibdev, "Couldn't allocate id cache entry - out of memory\n"); 2523cf69cc8SAmir Vadai return ERR_PTR(-ENOMEM); 2533cf69cc8SAmir Vadai } 2543cf69cc8SAmir Vadai 2553cf69cc8SAmir Vadai ent->sl_cm_id = sl_cm_id; 2563cf69cc8SAmir Vadai ent->slave_id = slave_id; 2573cf69cc8SAmir Vadai ent->scheduled_delete = 0; 2583cf69cc8SAmir Vadai ent->dev = to_mdev(ibdev); 2593cf69cc8SAmir Vadai INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout); 2603cf69cc8SAmir Vadai 2616a920060STejun Heo idr_preload(GFP_KERNEL); 2623cf69cc8SAmir Vadai spin_lock(&to_mdev(ibdev)->sriov.id_map_lock); 2636a920060STejun Heo 264f2d9db87SJeff Layton ret = idr_alloc_cyclic(&sriov->pv_id_table, ent, 0, 0, GFP_NOWAIT); 2656a920060STejun Heo if (ret >= 0) { 2666a920060STejun Heo ent->pv_cm_id = (u32)ret; 2673cf69cc8SAmir Vadai sl_id_map_add(ibdev, ent); 2686a920060STejun Heo list_add_tail(&ent->list, &sriov->cm_list); 2693cf69cc8SAmir Vadai } 2703cf69cc8SAmir Vadai 2713cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 2726a920060STejun Heo idr_preload_end(); 2736a920060STejun Heo 2746a920060STejun Heo if (ret >= 0) 2753cf69cc8SAmir Vadai return ent; 2766a920060STejun Heo 2773cf69cc8SAmir Vadai /*error flow*/ 2783cf69cc8SAmir Vadai kfree(ent); 2793cf69cc8SAmir Vadai mlx4_ib_warn(ibdev, "No more space in the idr (err:0x%x)\n", ret); 2803cf69cc8SAmir Vadai return ERR_PTR(-ENOMEM); 2813cf69cc8SAmir Vadai } 2823cf69cc8SAmir Vadai 2833cf69cc8SAmir Vadai static struct id_map_entry * 2843cf69cc8SAmir Vadai id_map_get(struct ib_device *ibdev, int *pv_cm_id, int sl_cm_id, int slave_id) 2853cf69cc8SAmir Vadai { 2863cf69cc8SAmir Vadai struct id_map_entry *ent; 2873cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 2883cf69cc8SAmir Vadai 2893cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 2903cf69cc8SAmir Vadai if (*pv_cm_id == -1) { 2913cf69cc8SAmir Vadai ent = id_map_find_by_sl_id(ibdev, sl_cm_id, slave_id); 2923cf69cc8SAmir Vadai if (ent) 2933cf69cc8SAmir Vadai *pv_cm_id = (int) ent->pv_cm_id; 2943cf69cc8SAmir Vadai } else 2953cf69cc8SAmir Vadai ent = (struct id_map_entry *)idr_find(&sriov->pv_id_table, *pv_cm_id); 2963cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 2973cf69cc8SAmir Vadai 2983cf69cc8SAmir Vadai return ent; 2993cf69cc8SAmir Vadai } 3003cf69cc8SAmir Vadai 3013cf69cc8SAmir Vadai static void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id) 3023cf69cc8SAmir Vadai { 3033cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 3043cf69cc8SAmir Vadai unsigned long flags; 3053cf69cc8SAmir Vadai 3063cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 307ceb7decbSJack Morgenstein spin_lock_irqsave(&sriov->going_down_lock, flags); 3083cf69cc8SAmir Vadai /*make sure that there is no schedule inside the scheduled work.*/ 3093cf69cc8SAmir Vadai if (!sriov->is_going_down) { 3103cf69cc8SAmir Vadai id->scheduled_delete = 1; 3113cf69cc8SAmir Vadai schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT); 3123cf69cc8SAmir Vadai } 3133cf69cc8SAmir Vadai spin_unlock_irqrestore(&sriov->going_down_lock, flags); 314ceb7decbSJack Morgenstein spin_unlock(&sriov->id_map_lock); 3153cf69cc8SAmir Vadai } 3163cf69cc8SAmir Vadai 3173cf69cc8SAmir Vadai int mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id, 3183cf69cc8SAmir Vadai struct ib_mad *mad) 3193cf69cc8SAmir Vadai { 3203cf69cc8SAmir Vadai struct id_map_entry *id; 3213cf69cc8SAmir Vadai u32 sl_cm_id; 3223cf69cc8SAmir Vadai int pv_cm_id = -1; 3233cf69cc8SAmir Vadai 3243cf69cc8SAmir Vadai if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID || 325*ceb5433bSShani Michaelli mad->mad_hdr.attr_id == CM_REP_ATTR_ID || 326*ceb5433bSShani Michaelli mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 327*ceb5433bSShani Michaelli sl_cm_id = get_local_comm_id(mad); 3283cf69cc8SAmir Vadai id = id_map_alloc(ibdev, slave_id, sl_cm_id); 3293cf69cc8SAmir Vadai if (IS_ERR(id)) { 3303cf69cc8SAmir Vadai mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n", 3313cf69cc8SAmir Vadai __func__, slave_id, sl_cm_id); 3323cf69cc8SAmir Vadai return PTR_ERR(id); 3333cf69cc8SAmir Vadai } 334*ceb5433bSShani Michaelli } else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID || 335*ceb5433bSShani Michaelli mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 3363cf69cc8SAmir Vadai return 0; 3373cf69cc8SAmir Vadai } else { 338*ceb5433bSShani Michaelli sl_cm_id = get_local_comm_id(mad); 3393cf69cc8SAmir Vadai id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id); 3403cf69cc8SAmir Vadai } 3413cf69cc8SAmir Vadai 3423cf69cc8SAmir Vadai if (!id) { 3433cf69cc8SAmir Vadai pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL!\n", 3443cf69cc8SAmir Vadai slave_id, sl_cm_id); 3453cf69cc8SAmir Vadai return -EINVAL; 3463cf69cc8SAmir Vadai } 3473cf69cc8SAmir Vadai 3483cf69cc8SAmir Vadai set_local_comm_id(mad, id->pv_cm_id); 3493cf69cc8SAmir Vadai 3503cf69cc8SAmir Vadai if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID) 3513cf69cc8SAmir Vadai schedule_delayed(ibdev, id); 3523cf69cc8SAmir Vadai else if (mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) 3533cf69cc8SAmir Vadai id_map_find_del(ibdev, pv_cm_id); 3543cf69cc8SAmir Vadai 3553cf69cc8SAmir Vadai return 0; 3563cf69cc8SAmir Vadai } 3573cf69cc8SAmir Vadai 3583cf69cc8SAmir Vadai int mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave, 3593cf69cc8SAmir Vadai struct ib_mad *mad) 3603cf69cc8SAmir Vadai { 3613cf69cc8SAmir Vadai u32 pv_cm_id; 3623cf69cc8SAmir Vadai struct id_map_entry *id; 3633cf69cc8SAmir Vadai 364*ceb5433bSShani Michaelli if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID || 365*ceb5433bSShani Michaelli mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 3663cf69cc8SAmir Vadai union ib_gid gid; 3673cf69cc8SAmir Vadai 3686ee51a4eSJack Morgenstein if (!slave) 3696ee51a4eSJack Morgenstein return 0; 3706ee51a4eSJack Morgenstein 3713cf69cc8SAmir Vadai gid = gid_from_req_msg(ibdev, mad); 3723cf69cc8SAmir Vadai *slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id); 3733cf69cc8SAmir Vadai if (*slave < 0) { 3743cf69cc8SAmir Vadai mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n", 3753cf69cc8SAmir Vadai gid.global.interface_id); 3763cf69cc8SAmir Vadai return -ENOENT; 3773cf69cc8SAmir Vadai } 3783cf69cc8SAmir Vadai return 0; 3793cf69cc8SAmir Vadai } 3803cf69cc8SAmir Vadai 3813cf69cc8SAmir Vadai pv_cm_id = get_remote_comm_id(mad); 3823cf69cc8SAmir Vadai id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1); 3833cf69cc8SAmir Vadai 3843cf69cc8SAmir Vadai if (!id) { 3853cf69cc8SAmir Vadai pr_debug("Couldn't find an entry for pv_cm_id 0x%x\n", pv_cm_id); 3863cf69cc8SAmir Vadai return -ENOENT; 3873cf69cc8SAmir Vadai } 3883cf69cc8SAmir Vadai 3896ee51a4eSJack Morgenstein if (slave) 3903cf69cc8SAmir Vadai *slave = id->slave_id; 3913cf69cc8SAmir Vadai set_remote_comm_id(mad, id->sl_cm_id); 3923cf69cc8SAmir Vadai 3933cf69cc8SAmir Vadai if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID) 3943cf69cc8SAmir Vadai schedule_delayed(ibdev, id); 3953cf69cc8SAmir Vadai else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID || 3963cf69cc8SAmir Vadai mad->mad_hdr.attr_id == CM_DREP_ATTR_ID) { 3973cf69cc8SAmir Vadai id_map_find_del(ibdev, (int) pv_cm_id); 3983cf69cc8SAmir Vadai } 3993cf69cc8SAmir Vadai 4003cf69cc8SAmir Vadai return 0; 4013cf69cc8SAmir Vadai } 4023cf69cc8SAmir Vadai 4033cf69cc8SAmir Vadai void mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev) 4043cf69cc8SAmir Vadai { 4053cf69cc8SAmir Vadai spin_lock_init(&dev->sriov.id_map_lock); 4063cf69cc8SAmir Vadai INIT_LIST_HEAD(&dev->sriov.cm_list); 4073cf69cc8SAmir Vadai dev->sriov.sl_id_map = RB_ROOT; 4083cf69cc8SAmir Vadai idr_init(&dev->sriov.pv_id_table); 4093cf69cc8SAmir Vadai } 4103cf69cc8SAmir Vadai 4113cf69cc8SAmir Vadai /* slave = -1 ==> all slaves */ 4123cf69cc8SAmir Vadai /* TBD -- call paravirt clean for single slave. Need for slave RESET event */ 4133cf69cc8SAmir Vadai void mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave) 4143cf69cc8SAmir Vadai { 4153cf69cc8SAmir Vadai struct mlx4_ib_sriov *sriov = &dev->sriov; 4163cf69cc8SAmir Vadai struct rb_root *sl_id_map = &sriov->sl_id_map; 4173cf69cc8SAmir Vadai struct list_head lh; 4183cf69cc8SAmir Vadai struct rb_node *nd; 4193cf69cc8SAmir Vadai int need_flush = 1; 4203cf69cc8SAmir Vadai struct id_map_entry *map, *tmp_map; 4213cf69cc8SAmir Vadai /* cancel all delayed work queue entries */ 4223cf69cc8SAmir Vadai INIT_LIST_HEAD(&lh); 4233cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 4243cf69cc8SAmir Vadai list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) { 4253cf69cc8SAmir Vadai if (slave < 0 || slave == map->slave_id) { 4263cf69cc8SAmir Vadai if (map->scheduled_delete) 4273cf69cc8SAmir Vadai need_flush &= !!cancel_delayed_work(&map->timeout); 4283cf69cc8SAmir Vadai } 4293cf69cc8SAmir Vadai } 4303cf69cc8SAmir Vadai 4313cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 4323cf69cc8SAmir Vadai 4333cf69cc8SAmir Vadai if (!need_flush) 4343cf69cc8SAmir Vadai flush_scheduled_work(); /* make sure all timers were flushed */ 4353cf69cc8SAmir Vadai 4363cf69cc8SAmir Vadai /* now, remove all leftover entries from databases*/ 4373cf69cc8SAmir Vadai spin_lock(&sriov->id_map_lock); 4383cf69cc8SAmir Vadai if (slave < 0) { 4393cf69cc8SAmir Vadai while (rb_first(sl_id_map)) { 4403cf69cc8SAmir Vadai struct id_map_entry *ent = 4413cf69cc8SAmir Vadai rb_entry(rb_first(sl_id_map), 4423cf69cc8SAmir Vadai struct id_map_entry, node); 4433cf69cc8SAmir Vadai 4443cf69cc8SAmir Vadai rb_erase(&ent->node, sl_id_map); 4453cf69cc8SAmir Vadai idr_remove(&sriov->pv_id_table, (int) ent->pv_cm_id); 4463cf69cc8SAmir Vadai } 4473cf69cc8SAmir Vadai list_splice_init(&dev->sriov.cm_list, &lh); 4483cf69cc8SAmir Vadai } else { 4493cf69cc8SAmir Vadai /* first, move nodes belonging to slave to db remove list */ 4503cf69cc8SAmir Vadai nd = rb_first(sl_id_map); 4513cf69cc8SAmir Vadai while (nd) { 4523cf69cc8SAmir Vadai struct id_map_entry *ent = 4533cf69cc8SAmir Vadai rb_entry(nd, struct id_map_entry, node); 4543cf69cc8SAmir Vadai nd = rb_next(nd); 4553cf69cc8SAmir Vadai if (ent->slave_id == slave) 4563cf69cc8SAmir Vadai list_move_tail(&ent->list, &lh); 4573cf69cc8SAmir Vadai } 4583cf69cc8SAmir Vadai /* remove those nodes from databases */ 4593cf69cc8SAmir Vadai list_for_each_entry_safe(map, tmp_map, &lh, list) { 4603cf69cc8SAmir Vadai rb_erase(&map->node, sl_id_map); 4613cf69cc8SAmir Vadai idr_remove(&sriov->pv_id_table, (int) map->pv_cm_id); 4623cf69cc8SAmir Vadai } 4633cf69cc8SAmir Vadai 4643cf69cc8SAmir Vadai /* add remaining nodes from cm_list */ 4653cf69cc8SAmir Vadai list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) { 4663cf69cc8SAmir Vadai if (slave == map->slave_id) 4673cf69cc8SAmir Vadai list_move_tail(&map->list, &lh); 4683cf69cc8SAmir Vadai } 4693cf69cc8SAmir Vadai } 4703cf69cc8SAmir Vadai 4713cf69cc8SAmir Vadai spin_unlock(&sriov->id_map_lock); 4723cf69cc8SAmir Vadai 4733cf69cc8SAmir Vadai /* free any map entries left behind due to cancel_delayed_work above */ 4743cf69cc8SAmir Vadai list_for_each_entry_safe(map, tmp_map, &lh, list) { 4753cf69cc8SAmir Vadai list_del(&map->list); 4763cf69cc8SAmir Vadai kfree(map); 4773cf69cc8SAmir Vadai } 4783cf69cc8SAmir Vadai } 479