18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2012 Mellanox Technologies. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 58c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 118c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 128c2ecf20Sopenharmony_ci * conditions are met: 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 168c2ecf20Sopenharmony_ci * disclaimer. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 198c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 208c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 218c2ecf20Sopenharmony_ci * provided with the distribution. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 308c2ecf20Sopenharmony_ci * SOFTWARE. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#include <rdma/ib_mad.h> 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#include <linux/mlx4/cmd.h> 368c2ecf20Sopenharmony_ci#include <linux/rbtree.h> 378c2ecf20Sopenharmony_ci#include <linux/idr.h> 388c2ecf20Sopenharmony_ci#include <rdma/ib_cm.h> 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#include "mlx4_ib.h" 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define CM_CLEANUP_CACHE_TIMEOUT (30 * HZ) 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistruct id_map_entry { 458c2ecf20Sopenharmony_ci struct rb_node node; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci u32 sl_cm_id; 488c2ecf20Sopenharmony_ci u32 pv_cm_id; 498c2ecf20Sopenharmony_ci int slave_id; 508c2ecf20Sopenharmony_ci int scheduled_delete; 518c2ecf20Sopenharmony_ci struct mlx4_ib_dev *dev; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci struct list_head list; 548c2ecf20Sopenharmony_ci struct delayed_work timeout; 558c2ecf20Sopenharmony_ci}; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistruct rej_tmout_entry { 588c2ecf20Sopenharmony_ci int slave; 598c2ecf20Sopenharmony_ci u32 rem_pv_cm_id; 608c2ecf20Sopenharmony_ci struct delayed_work timeout; 618c2ecf20Sopenharmony_ci struct xarray *xa_rej_tmout; 628c2ecf20Sopenharmony_ci}; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistruct cm_generic_msg { 658c2ecf20Sopenharmony_ci struct ib_mad_hdr hdr; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci __be32 local_comm_id; 688c2ecf20Sopenharmony_ci __be32 remote_comm_id; 698c2ecf20Sopenharmony_ci unsigned char unused[2]; 708c2ecf20Sopenharmony_ci __be16 rej_reason; 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistruct cm_sidr_generic_msg { 748c2ecf20Sopenharmony_ci struct ib_mad_hdr hdr; 758c2ecf20Sopenharmony_ci __be32 request_id; 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistruct cm_req_msg { 798c2ecf20Sopenharmony_ci unsigned char unused[0x60]; 808c2ecf20Sopenharmony_ci union ib_gid primary_path_sgid; 818c2ecf20Sopenharmony_ci}; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic void set_local_comm_id(struct ib_mad *mad, u32 cm_id) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 878c2ecf20Sopenharmony_ci struct cm_sidr_generic_msg *msg = 888c2ecf20Sopenharmony_ci (struct cm_sidr_generic_msg *)mad; 898c2ecf20Sopenharmony_ci msg->request_id = cpu_to_be32(cm_id); 908c2ecf20Sopenharmony_ci } else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 918c2ecf20Sopenharmony_ci pr_err("trying to set local_comm_id in SIDR_REP\n"); 928c2ecf20Sopenharmony_ci return; 938c2ecf20Sopenharmony_ci } else { 948c2ecf20Sopenharmony_ci struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 958c2ecf20Sopenharmony_ci msg->local_comm_id = cpu_to_be32(cm_id); 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic u32 get_local_comm_id(struct ib_mad *mad) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 1028c2ecf20Sopenharmony_ci struct cm_sidr_generic_msg *msg = 1038c2ecf20Sopenharmony_ci (struct cm_sidr_generic_msg *)mad; 1048c2ecf20Sopenharmony_ci return be32_to_cpu(msg->request_id); 1058c2ecf20Sopenharmony_ci } else if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 1068c2ecf20Sopenharmony_ci pr_err("trying to set local_comm_id in SIDR_REP\n"); 1078c2ecf20Sopenharmony_ci return -1; 1088c2ecf20Sopenharmony_ci } else { 1098c2ecf20Sopenharmony_ci struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1108c2ecf20Sopenharmony_ci return be32_to_cpu(msg->local_comm_id); 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic void set_remote_comm_id(struct ib_mad *mad, u32 cm_id) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 1178c2ecf20Sopenharmony_ci struct cm_sidr_generic_msg *msg = 1188c2ecf20Sopenharmony_ci (struct cm_sidr_generic_msg *)mad; 1198c2ecf20Sopenharmony_ci msg->request_id = cpu_to_be32(cm_id); 1208c2ecf20Sopenharmony_ci } else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 1218c2ecf20Sopenharmony_ci pr_err("trying to set remote_comm_id in SIDR_REQ\n"); 1228c2ecf20Sopenharmony_ci return; 1238c2ecf20Sopenharmony_ci } else { 1248c2ecf20Sopenharmony_ci struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1258c2ecf20Sopenharmony_ci msg->remote_comm_id = cpu_to_be32(cm_id); 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci} 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic u32 get_remote_comm_id(struct ib_mad *mad) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 1328c2ecf20Sopenharmony_ci struct cm_sidr_generic_msg *msg = 1338c2ecf20Sopenharmony_ci (struct cm_sidr_generic_msg *)mad; 1348c2ecf20Sopenharmony_ci return be32_to_cpu(msg->request_id); 1358c2ecf20Sopenharmony_ci } else if (mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 1368c2ecf20Sopenharmony_ci pr_err("trying to set remote_comm_id in SIDR_REQ\n"); 1378c2ecf20Sopenharmony_ci return -1; 1388c2ecf20Sopenharmony_ci } else { 1398c2ecf20Sopenharmony_ci struct cm_generic_msg *msg = (struct cm_generic_msg *)mad; 1408c2ecf20Sopenharmony_ci return be32_to_cpu(msg->remote_comm_id); 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic union ib_gid gid_from_req_msg(struct ib_device *ibdev, struct ib_mad *mad) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct cm_req_msg *msg = (struct cm_req_msg *)mad; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return msg->primary_path_sgid; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci/* Lock should be taken before called */ 1528c2ecf20Sopenharmony_cistatic struct id_map_entry * 1538c2ecf20Sopenharmony_ciid_map_find_by_sl_id(struct ib_device *ibdev, u32 slave_id, u32 sl_cm_id) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map; 1568c2ecf20Sopenharmony_ci struct rb_node *node = sl_id_map->rb_node; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci while (node) { 1598c2ecf20Sopenharmony_ci struct id_map_entry *id_map_entry = 1608c2ecf20Sopenharmony_ci rb_entry(node, struct id_map_entry, node); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci if (id_map_entry->sl_cm_id > sl_cm_id) 1638c2ecf20Sopenharmony_ci node = node->rb_left; 1648c2ecf20Sopenharmony_ci else if (id_map_entry->sl_cm_id < sl_cm_id) 1658c2ecf20Sopenharmony_ci node = node->rb_right; 1668c2ecf20Sopenharmony_ci else if (id_map_entry->slave_id > slave_id) 1678c2ecf20Sopenharmony_ci node = node->rb_left; 1688c2ecf20Sopenharmony_ci else if (id_map_entry->slave_id < slave_id) 1698c2ecf20Sopenharmony_ci node = node->rb_right; 1708c2ecf20Sopenharmony_ci else 1718c2ecf20Sopenharmony_ci return id_map_entry; 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci return NULL; 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_cistatic void id_map_ent_timeout(struct work_struct *work) 1778c2ecf20Sopenharmony_ci{ 1788c2ecf20Sopenharmony_ci struct delayed_work *delay = to_delayed_work(work); 1798c2ecf20Sopenharmony_ci struct id_map_entry *ent = container_of(delay, struct id_map_entry, timeout); 1808c2ecf20Sopenharmony_ci struct id_map_entry *found_ent; 1818c2ecf20Sopenharmony_ci struct mlx4_ib_dev *dev = ent->dev; 1828c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &dev->sriov; 1838c2ecf20Sopenharmony_ci struct rb_root *sl_id_map = &sriov->sl_id_map; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 1868c2ecf20Sopenharmony_ci if (!xa_erase(&sriov->pv_id_table, ent->pv_cm_id)) 1878c2ecf20Sopenharmony_ci goto out; 1888c2ecf20Sopenharmony_ci found_ent = id_map_find_by_sl_id(&dev->ib_dev, ent->slave_id, ent->sl_cm_id); 1898c2ecf20Sopenharmony_ci if (found_ent && found_ent == ent) 1908c2ecf20Sopenharmony_ci rb_erase(&found_ent->node, sl_id_map); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ciout: 1938c2ecf20Sopenharmony_ci list_del(&ent->list); 1948c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 1958c2ecf20Sopenharmony_ci kfree(ent); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic void sl_id_map_add(struct ib_device *ibdev, struct id_map_entry *new) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci struct rb_root *sl_id_map = &to_mdev(ibdev)->sriov.sl_id_map; 2018c2ecf20Sopenharmony_ci struct rb_node **link = &sl_id_map->rb_node, *parent = NULL; 2028c2ecf20Sopenharmony_ci struct id_map_entry *ent; 2038c2ecf20Sopenharmony_ci int slave_id = new->slave_id; 2048c2ecf20Sopenharmony_ci int sl_cm_id = new->sl_cm_id; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id); 2078c2ecf20Sopenharmony_ci if (ent) { 2088c2ecf20Sopenharmony_ci pr_debug("overriding existing sl_id_map entry (cm_id = %x)\n", 2098c2ecf20Sopenharmony_ci sl_cm_id); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci rb_replace_node(&ent->node, &new->node, sl_id_map); 2128c2ecf20Sopenharmony_ci return; 2138c2ecf20Sopenharmony_ci } 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci /* Go to the bottom of the tree */ 2168c2ecf20Sopenharmony_ci while (*link) { 2178c2ecf20Sopenharmony_ci parent = *link; 2188c2ecf20Sopenharmony_ci ent = rb_entry(parent, struct id_map_entry, node); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci if (ent->sl_cm_id > sl_cm_id || (ent->sl_cm_id == sl_cm_id && ent->slave_id > slave_id)) 2218c2ecf20Sopenharmony_ci link = &(*link)->rb_left; 2228c2ecf20Sopenharmony_ci else 2238c2ecf20Sopenharmony_ci link = &(*link)->rb_right; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci rb_link_node(&new->node, parent, link); 2278c2ecf20Sopenharmony_ci rb_insert_color(&new->node, sl_id_map); 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic struct id_map_entry * 2318c2ecf20Sopenharmony_ciid_map_alloc(struct ib_device *ibdev, int slave_id, u32 sl_cm_id) 2328c2ecf20Sopenharmony_ci{ 2338c2ecf20Sopenharmony_ci int ret; 2348c2ecf20Sopenharmony_ci struct id_map_entry *ent; 2358c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci ent = kmalloc(sizeof (struct id_map_entry), GFP_KERNEL); 2388c2ecf20Sopenharmony_ci if (!ent) 2398c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci ent->sl_cm_id = sl_cm_id; 2428c2ecf20Sopenharmony_ci ent->slave_id = slave_id; 2438c2ecf20Sopenharmony_ci ent->scheduled_delete = 0; 2448c2ecf20Sopenharmony_ci ent->dev = to_mdev(ibdev); 2458c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&ent->timeout, id_map_ent_timeout); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci ret = xa_alloc_cyclic(&sriov->pv_id_table, &ent->pv_cm_id, ent, 2488c2ecf20Sopenharmony_ci xa_limit_32b, &sriov->pv_id_next, GFP_KERNEL); 2498c2ecf20Sopenharmony_ci if (ret >= 0) { 2508c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 2518c2ecf20Sopenharmony_ci sl_id_map_add(ibdev, ent); 2528c2ecf20Sopenharmony_ci list_add_tail(&ent->list, &sriov->cm_list); 2538c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 2548c2ecf20Sopenharmony_ci return ent; 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /*error flow*/ 2588c2ecf20Sopenharmony_ci kfree(ent); 2598c2ecf20Sopenharmony_ci mlx4_ib_warn(ibdev, "Allocation failed (err:0x%x)\n", ret); 2608c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic struct id_map_entry * 2648c2ecf20Sopenharmony_ciid_map_get(struct ib_device *ibdev, int *pv_cm_id, int slave_id, int sl_cm_id) 2658c2ecf20Sopenharmony_ci{ 2668c2ecf20Sopenharmony_ci struct id_map_entry *ent; 2678c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 2708c2ecf20Sopenharmony_ci if (*pv_cm_id == -1) { 2718c2ecf20Sopenharmony_ci ent = id_map_find_by_sl_id(ibdev, slave_id, sl_cm_id); 2728c2ecf20Sopenharmony_ci if (ent) 2738c2ecf20Sopenharmony_ci *pv_cm_id = (int) ent->pv_cm_id; 2748c2ecf20Sopenharmony_ci } else 2758c2ecf20Sopenharmony_ci ent = xa_load(&sriov->pv_id_table, *pv_cm_id); 2768c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci return ent; 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_cistatic void schedule_delayed(struct ib_device *ibdev, struct id_map_entry *id) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 2848c2ecf20Sopenharmony_ci unsigned long flags; 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 2878c2ecf20Sopenharmony_ci spin_lock_irqsave(&sriov->going_down_lock, flags); 2888c2ecf20Sopenharmony_ci /*make sure that there is no schedule inside the scheduled work.*/ 2898c2ecf20Sopenharmony_ci if (!sriov->is_going_down && !id->scheduled_delete) { 2908c2ecf20Sopenharmony_ci id->scheduled_delete = 1; 2918c2ecf20Sopenharmony_ci schedule_delayed_work(&id->timeout, CM_CLEANUP_CACHE_TIMEOUT); 2928c2ecf20Sopenharmony_ci } else if (id->scheduled_delete) { 2938c2ecf20Sopenharmony_ci /* Adjust timeout if already scheduled */ 2948c2ecf20Sopenharmony_ci mod_delayed_work(system_wq, &id->timeout, CM_CLEANUP_CACHE_TIMEOUT); 2958c2ecf20Sopenharmony_ci } 2968c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&sriov->going_down_lock, flags); 2978c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 2988c2ecf20Sopenharmony_ci} 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci#define REJ_REASON(m) be16_to_cpu(((struct cm_generic_msg *)(m))->rej_reason) 3018c2ecf20Sopenharmony_ciint mlx4_ib_multiplex_cm_handler(struct ib_device *ibdev, int port, int slave_id, 3028c2ecf20Sopenharmony_ci struct ib_mad *mad) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci struct id_map_entry *id; 3058c2ecf20Sopenharmony_ci u32 sl_cm_id; 3068c2ecf20Sopenharmony_ci int pv_cm_id = -1; 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID || 3098c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_REP_ATTR_ID || 3108c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_MRA_ATTR_ID || 3118c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID || 3128c2ecf20Sopenharmony_ci (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID && REJ_REASON(mad) == IB_CM_REJ_TIMEOUT)) { 3138c2ecf20Sopenharmony_ci sl_cm_id = get_local_comm_id(mad); 3148c2ecf20Sopenharmony_ci id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id); 3158c2ecf20Sopenharmony_ci if (id) 3168c2ecf20Sopenharmony_ci goto cont; 3178c2ecf20Sopenharmony_ci id = id_map_alloc(ibdev, slave_id, sl_cm_id); 3188c2ecf20Sopenharmony_ci if (IS_ERR(id)) { 3198c2ecf20Sopenharmony_ci mlx4_ib_warn(ibdev, "%s: id{slave: %d, sl_cm_id: 0x%x} Failed to id_map_alloc\n", 3208c2ecf20Sopenharmony_ci __func__, slave_id, sl_cm_id); 3218c2ecf20Sopenharmony_ci return PTR_ERR(id); 3228c2ecf20Sopenharmony_ci } 3238c2ecf20Sopenharmony_ci } else if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID || 3248c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_SIDR_REP_ATTR_ID) { 3258c2ecf20Sopenharmony_ci return 0; 3268c2ecf20Sopenharmony_ci } else { 3278c2ecf20Sopenharmony_ci sl_cm_id = get_local_comm_id(mad); 3288c2ecf20Sopenharmony_ci id = id_map_get(ibdev, &pv_cm_id, slave_id, sl_cm_id); 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci if (!id) { 3328c2ecf20Sopenharmony_ci pr_debug("id{slave: %d, sl_cm_id: 0x%x} is NULL! attr_id: 0x%x\n", 3338c2ecf20Sopenharmony_ci slave_id, sl_cm_id, be16_to_cpu(mad->mad_hdr.attr_id)); 3348c2ecf20Sopenharmony_ci return -EINVAL; 3358c2ecf20Sopenharmony_ci } 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_cicont: 3388c2ecf20Sopenharmony_ci set_local_comm_id(mad, id->pv_cm_id); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID) 3418c2ecf20Sopenharmony_ci schedule_delayed(ibdev, id); 3428c2ecf20Sopenharmony_ci return 0; 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic void rej_tmout_timeout(struct work_struct *work) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct delayed_work *delay = to_delayed_work(work); 3488c2ecf20Sopenharmony_ci struct rej_tmout_entry *item = container_of(delay, struct rej_tmout_entry, timeout); 3498c2ecf20Sopenharmony_ci struct rej_tmout_entry *deleted; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci deleted = xa_cmpxchg(item->xa_rej_tmout, item->rem_pv_cm_id, item, NULL, 0); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci if (deleted != item) 3548c2ecf20Sopenharmony_ci pr_debug("deleted(%p) != item(%p)\n", deleted, item); 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci kfree(item); 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic int alloc_rej_tmout(struct mlx4_ib_sriov *sriov, u32 rem_pv_cm_id, int slave) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci struct rej_tmout_entry *item; 3628c2ecf20Sopenharmony_ci struct rej_tmout_entry *old; 3638c2ecf20Sopenharmony_ci int ret = 0; 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci xa_lock(&sriov->xa_rej_tmout); 3668c2ecf20Sopenharmony_ci item = xa_load(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci if (item) { 3698c2ecf20Sopenharmony_ci if (xa_err(item)) 3708c2ecf20Sopenharmony_ci ret = xa_err(item); 3718c2ecf20Sopenharmony_ci else 3728c2ecf20Sopenharmony_ci /* If a retry, adjust delayed work */ 3738c2ecf20Sopenharmony_ci mod_delayed_work(system_wq, &item->timeout, CM_CLEANUP_CACHE_TIMEOUT); 3748c2ecf20Sopenharmony_ci goto err_or_exists; 3758c2ecf20Sopenharmony_ci } 3768c2ecf20Sopenharmony_ci xa_unlock(&sriov->xa_rej_tmout); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci item = kmalloc(sizeof(*item), GFP_KERNEL); 3798c2ecf20Sopenharmony_ci if (!item) 3808c2ecf20Sopenharmony_ci return -ENOMEM; 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci INIT_DELAYED_WORK(&item->timeout, rej_tmout_timeout); 3838c2ecf20Sopenharmony_ci item->slave = slave; 3848c2ecf20Sopenharmony_ci item->rem_pv_cm_id = rem_pv_cm_id; 3858c2ecf20Sopenharmony_ci item->xa_rej_tmout = &sriov->xa_rej_tmout; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci old = xa_cmpxchg(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id, NULL, item, GFP_KERNEL); 3888c2ecf20Sopenharmony_ci if (old) { 3898c2ecf20Sopenharmony_ci pr_debug( 3908c2ecf20Sopenharmony_ci "Non-null old entry (%p) or error (%d) when inserting\n", 3918c2ecf20Sopenharmony_ci old, xa_err(old)); 3928c2ecf20Sopenharmony_ci kfree(item); 3938c2ecf20Sopenharmony_ci return xa_err(old); 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci schedule_delayed_work(&item->timeout, CM_CLEANUP_CACHE_TIMEOUT); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci return 0; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_cierr_or_exists: 4018c2ecf20Sopenharmony_ci xa_unlock(&sriov->xa_rej_tmout); 4028c2ecf20Sopenharmony_ci return ret; 4038c2ecf20Sopenharmony_ci} 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic int lookup_rej_tmout_slave(struct mlx4_ib_sriov *sriov, u32 rem_pv_cm_id) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci struct rej_tmout_entry *item; 4088c2ecf20Sopenharmony_ci int slave; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci xa_lock(&sriov->xa_rej_tmout); 4118c2ecf20Sopenharmony_ci item = xa_load(&sriov->xa_rej_tmout, (unsigned long)rem_pv_cm_id); 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci if (!item || xa_err(item)) { 4148c2ecf20Sopenharmony_ci pr_debug("Could not find slave. rem_pv_cm_id 0x%x error: %d\n", 4158c2ecf20Sopenharmony_ci rem_pv_cm_id, xa_err(item)); 4168c2ecf20Sopenharmony_ci slave = !item ? -ENOENT : xa_err(item); 4178c2ecf20Sopenharmony_ci } else { 4188c2ecf20Sopenharmony_ci slave = item->slave; 4198c2ecf20Sopenharmony_ci } 4208c2ecf20Sopenharmony_ci xa_unlock(&sriov->xa_rej_tmout); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci return slave; 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ciint mlx4_ib_demux_cm_handler(struct ib_device *ibdev, int port, int *slave, 4268c2ecf20Sopenharmony_ci struct ib_mad *mad) 4278c2ecf20Sopenharmony_ci{ 4288c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &to_mdev(ibdev)->sriov; 4298c2ecf20Sopenharmony_ci u32 rem_pv_cm_id = get_local_comm_id(mad); 4308c2ecf20Sopenharmony_ci u32 pv_cm_id; 4318c2ecf20Sopenharmony_ci struct id_map_entry *id; 4328c2ecf20Sopenharmony_ci int sts; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_REQ_ATTR_ID || 4358c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_SIDR_REQ_ATTR_ID) { 4368c2ecf20Sopenharmony_ci union ib_gid gid; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci if (!slave) 4398c2ecf20Sopenharmony_ci return 0; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci gid = gid_from_req_msg(ibdev, mad); 4428c2ecf20Sopenharmony_ci *slave = mlx4_ib_find_real_gid(ibdev, port, gid.global.interface_id); 4438c2ecf20Sopenharmony_ci if (*slave < 0) { 4448c2ecf20Sopenharmony_ci mlx4_ib_warn(ibdev, "failed matching slave_id by gid (0x%llx)\n", 4458c2ecf20Sopenharmony_ci be64_to_cpu(gid.global.interface_id)); 4468c2ecf20Sopenharmony_ci return -ENOENT; 4478c2ecf20Sopenharmony_ci } 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci sts = alloc_rej_tmout(sriov, rem_pv_cm_id, *slave); 4508c2ecf20Sopenharmony_ci if (sts) 4518c2ecf20Sopenharmony_ci /* Even if this fails, we pass on the REQ to the slave */ 4528c2ecf20Sopenharmony_ci pr_debug("Could not allocate rej_tmout entry. rem_pv_cm_id 0x%x slave %d status %d\n", 4538c2ecf20Sopenharmony_ci rem_pv_cm_id, *slave, sts); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci return 0; 4568c2ecf20Sopenharmony_ci } 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci pv_cm_id = get_remote_comm_id(mad); 4598c2ecf20Sopenharmony_ci id = id_map_get(ibdev, (int *)&pv_cm_id, -1, -1); 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci if (!id) { 4628c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_REJ_ATTR_ID && 4638c2ecf20Sopenharmony_ci REJ_REASON(mad) == IB_CM_REJ_TIMEOUT && slave) { 4648c2ecf20Sopenharmony_ci *slave = lookup_rej_tmout_slave(sriov, rem_pv_cm_id); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci return (*slave < 0) ? *slave : 0; 4678c2ecf20Sopenharmony_ci } 4688c2ecf20Sopenharmony_ci pr_debug("Couldn't find an entry for pv_cm_id 0x%x, attr_id 0x%x\n", 4698c2ecf20Sopenharmony_ci pv_cm_id, be16_to_cpu(mad->mad_hdr.attr_id)); 4708c2ecf20Sopenharmony_ci return -ENOENT; 4718c2ecf20Sopenharmony_ci } 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci if (slave) 4748c2ecf20Sopenharmony_ci *slave = id->slave_id; 4758c2ecf20Sopenharmony_ci set_remote_comm_id(mad, id->sl_cm_id); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci if (mad->mad_hdr.attr_id == CM_DREQ_ATTR_ID || 4788c2ecf20Sopenharmony_ci mad->mad_hdr.attr_id == CM_REJ_ATTR_ID) 4798c2ecf20Sopenharmony_ci schedule_delayed(ibdev, id); 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return 0; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_civoid mlx4_ib_cm_paravirt_init(struct mlx4_ib_dev *dev) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci spin_lock_init(&dev->sriov.id_map_lock); 4878c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&dev->sriov.cm_list); 4888c2ecf20Sopenharmony_ci dev->sriov.sl_id_map = RB_ROOT; 4898c2ecf20Sopenharmony_ci xa_init_flags(&dev->sriov.pv_id_table, XA_FLAGS_ALLOC); 4908c2ecf20Sopenharmony_ci xa_init(&dev->sriov.xa_rej_tmout); 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic void rej_tmout_xa_cleanup(struct mlx4_ib_sriov *sriov, int slave) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct rej_tmout_entry *item; 4968c2ecf20Sopenharmony_ci bool flush_needed = false; 4978c2ecf20Sopenharmony_ci unsigned long id; 4988c2ecf20Sopenharmony_ci int cnt = 0; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci xa_lock(&sriov->xa_rej_tmout); 5018c2ecf20Sopenharmony_ci xa_for_each(&sriov->xa_rej_tmout, id, item) { 5028c2ecf20Sopenharmony_ci if (slave < 0 || slave == item->slave) { 5038c2ecf20Sopenharmony_ci mod_delayed_work(system_wq, &item->timeout, 0); 5048c2ecf20Sopenharmony_ci flush_needed = true; 5058c2ecf20Sopenharmony_ci ++cnt; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci } 5088c2ecf20Sopenharmony_ci xa_unlock(&sriov->xa_rej_tmout); 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci if (flush_needed) { 5118c2ecf20Sopenharmony_ci flush_scheduled_work(); 5128c2ecf20Sopenharmony_ci pr_debug("Deleted %d entries in xarray for slave %d during cleanup\n", 5138c2ecf20Sopenharmony_ci cnt, slave); 5148c2ecf20Sopenharmony_ci } 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci if (slave < 0) 5178c2ecf20Sopenharmony_ci WARN_ON(!xa_empty(&sriov->xa_rej_tmout)); 5188c2ecf20Sopenharmony_ci} 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci/* slave = -1 ==> all slaves */ 5218c2ecf20Sopenharmony_ci/* TBD -- call paravirt clean for single slave. Need for slave RESET event */ 5228c2ecf20Sopenharmony_civoid mlx4_ib_cm_paravirt_clean(struct mlx4_ib_dev *dev, int slave) 5238c2ecf20Sopenharmony_ci{ 5248c2ecf20Sopenharmony_ci struct mlx4_ib_sriov *sriov = &dev->sriov; 5258c2ecf20Sopenharmony_ci struct rb_root *sl_id_map = &sriov->sl_id_map; 5268c2ecf20Sopenharmony_ci struct list_head lh; 5278c2ecf20Sopenharmony_ci struct rb_node *nd; 5288c2ecf20Sopenharmony_ci int need_flush = 0; 5298c2ecf20Sopenharmony_ci struct id_map_entry *map, *tmp_map; 5308c2ecf20Sopenharmony_ci /* cancel all delayed work queue entries */ 5318c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&lh); 5328c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 5338c2ecf20Sopenharmony_ci list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) { 5348c2ecf20Sopenharmony_ci if (slave < 0 || slave == map->slave_id) { 5358c2ecf20Sopenharmony_ci if (map->scheduled_delete) 5368c2ecf20Sopenharmony_ci need_flush |= !cancel_delayed_work(&map->timeout); 5378c2ecf20Sopenharmony_ci } 5388c2ecf20Sopenharmony_ci } 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci if (need_flush) 5438c2ecf20Sopenharmony_ci flush_scheduled_work(); /* make sure all timers were flushed */ 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci /* now, remove all leftover entries from databases*/ 5468c2ecf20Sopenharmony_ci spin_lock(&sriov->id_map_lock); 5478c2ecf20Sopenharmony_ci if (slave < 0) { 5488c2ecf20Sopenharmony_ci while (rb_first(sl_id_map)) { 5498c2ecf20Sopenharmony_ci struct id_map_entry *ent = 5508c2ecf20Sopenharmony_ci rb_entry(rb_first(sl_id_map), 5518c2ecf20Sopenharmony_ci struct id_map_entry, node); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci rb_erase(&ent->node, sl_id_map); 5548c2ecf20Sopenharmony_ci xa_erase(&sriov->pv_id_table, ent->pv_cm_id); 5558c2ecf20Sopenharmony_ci } 5568c2ecf20Sopenharmony_ci list_splice_init(&dev->sriov.cm_list, &lh); 5578c2ecf20Sopenharmony_ci } else { 5588c2ecf20Sopenharmony_ci /* first, move nodes belonging to slave to db remove list */ 5598c2ecf20Sopenharmony_ci nd = rb_first(sl_id_map); 5608c2ecf20Sopenharmony_ci while (nd) { 5618c2ecf20Sopenharmony_ci struct id_map_entry *ent = 5628c2ecf20Sopenharmony_ci rb_entry(nd, struct id_map_entry, node); 5638c2ecf20Sopenharmony_ci nd = rb_next(nd); 5648c2ecf20Sopenharmony_ci if (ent->slave_id == slave) 5658c2ecf20Sopenharmony_ci list_move_tail(&ent->list, &lh); 5668c2ecf20Sopenharmony_ci } 5678c2ecf20Sopenharmony_ci /* remove those nodes from databases */ 5688c2ecf20Sopenharmony_ci list_for_each_entry_safe(map, tmp_map, &lh, list) { 5698c2ecf20Sopenharmony_ci rb_erase(&map->node, sl_id_map); 5708c2ecf20Sopenharmony_ci xa_erase(&sriov->pv_id_table, map->pv_cm_id); 5718c2ecf20Sopenharmony_ci } 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci /* add remaining nodes from cm_list */ 5748c2ecf20Sopenharmony_ci list_for_each_entry_safe(map, tmp_map, &dev->sriov.cm_list, list) { 5758c2ecf20Sopenharmony_ci if (slave == map->slave_id) 5768c2ecf20Sopenharmony_ci list_move_tail(&map->list, &lh); 5778c2ecf20Sopenharmony_ci } 5788c2ecf20Sopenharmony_ci } 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci spin_unlock(&sriov->id_map_lock); 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci /* free any map entries left behind due to cancel_delayed_work above */ 5838c2ecf20Sopenharmony_ci list_for_each_entry_safe(map, tmp_map, &lh, list) { 5848c2ecf20Sopenharmony_ci list_del(&map->list); 5858c2ecf20Sopenharmony_ci kfree(map); 5868c2ecf20Sopenharmony_ci } 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci rej_tmout_xa_cleanup(sriov, slave); 5898c2ecf20Sopenharmony_ci} 590