162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * This software is available to you under a choice of one of two 562306a36Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 662306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 762306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the 862306a36Sopenharmony_ci * OpenIB.org BSD license below: 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or 1162306a36Sopenharmony_ci * without modification, are permitted provided that the following 1262306a36Sopenharmony_ci * conditions are met: 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * - Redistributions of source code must retain the above 1562306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 1662306a36Sopenharmony_ci * disclaimer. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * - Redistributions in binary form must reproduce the above 1962306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2062306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials 2162306a36Sopenharmony_ci * provided with the distribution. 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2462306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2562306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2662306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2762306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2862306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3062306a36Sopenharmony_ci * SOFTWARE. 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci#include <rdma/ib_addr.h> 3462306a36Sopenharmony_ci#include <rdma/ib_cache.h> 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ci#include <linux/slab.h> 3762306a36Sopenharmony_ci#include <linux/inet.h> 3862306a36Sopenharmony_ci#include <linux/string.h> 3962306a36Sopenharmony_ci#include <linux/mlx4/driver.h> 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#include "mlx4_ib.h" 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_cistatic void create_ib_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr) 4462306a36Sopenharmony_ci{ 4562306a36Sopenharmony_ci struct mlx4_ib_ah *ah = to_mah(ib_ah); 4662306a36Sopenharmony_ci struct mlx4_dev *dev = to_mdev(ib_ah->device)->dev; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci ah->av.ib.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn | 4962306a36Sopenharmony_ci (rdma_ah_get_port_num(ah_attr) << 24)); 5062306a36Sopenharmony_ci ah->av.ib.g_slid = rdma_ah_get_path_bits(ah_attr); 5162306a36Sopenharmony_ci ah->av.ib.sl_tclass_flowlabel = 5262306a36Sopenharmony_ci cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28); 5362306a36Sopenharmony_ci if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) { 5462306a36Sopenharmony_ci const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr); 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci ah->av.ib.g_slid |= 0x80; 5762306a36Sopenharmony_ci ah->av.ib.gid_index = grh->sgid_index; 5862306a36Sopenharmony_ci ah->av.ib.hop_limit = grh->hop_limit; 5962306a36Sopenharmony_ci ah->av.ib.sl_tclass_flowlabel |= 6062306a36Sopenharmony_ci cpu_to_be32((grh->traffic_class << 20) | 6162306a36Sopenharmony_ci grh->flow_label); 6262306a36Sopenharmony_ci memcpy(ah->av.ib.dgid, grh->dgid.raw, 16); 6362306a36Sopenharmony_ci } 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr)); 6662306a36Sopenharmony_ci if (rdma_ah_get_static_rate(ah_attr)) { 6762306a36Sopenharmony_ci u8 static_rate = rdma_ah_get_static_rate(ah_attr) + 6862306a36Sopenharmony_ci MLX4_STAT_RATE_OFFSET; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET && 7162306a36Sopenharmony_ci !(1 << static_rate & dev->caps.stat_rate_support)) 7262306a36Sopenharmony_ci --static_rate; 7362306a36Sopenharmony_ci ah->av.ib.stat_rate = static_rate; 7462306a36Sopenharmony_ci } 7562306a36Sopenharmony_ci} 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_cistatic int create_iboe_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr) 7862306a36Sopenharmony_ci{ 7962306a36Sopenharmony_ci struct mlx4_ib_dev *ibdev = to_mdev(ib_ah->device); 8062306a36Sopenharmony_ci struct mlx4_ib_ah *ah = to_mah(ib_ah); 8162306a36Sopenharmony_ci const struct ib_gid_attr *gid_attr; 8262306a36Sopenharmony_ci struct mlx4_dev *dev = ibdev->dev; 8362306a36Sopenharmony_ci int is_mcast = 0; 8462306a36Sopenharmony_ci struct in6_addr in6; 8562306a36Sopenharmony_ci u16 vlan_tag = 0xffff; 8662306a36Sopenharmony_ci const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr); 8762306a36Sopenharmony_ci int ret; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci memcpy(&in6, grh->dgid.raw, sizeof(in6)); 9062306a36Sopenharmony_ci if (rdma_is_multicast_addr(&in6)) 9162306a36Sopenharmony_ci is_mcast = 1; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN); 9462306a36Sopenharmony_ci eth_zero_addr(ah->av.eth.s_mac); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci /* 9762306a36Sopenharmony_ci * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave 9862306a36Sopenharmony_ci * and we are directly creating an AV for a slave's gid_index. 9962306a36Sopenharmony_ci */ 10062306a36Sopenharmony_ci gid_attr = ah_attr->grh.sgid_attr; 10162306a36Sopenharmony_ci if (gid_attr) { 10262306a36Sopenharmony_ci ret = rdma_read_gid_l2_fields(gid_attr, &vlan_tag, 10362306a36Sopenharmony_ci &ah->av.eth.s_mac[0]); 10462306a36Sopenharmony_ci if (ret) 10562306a36Sopenharmony_ci return ret; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr); 10862306a36Sopenharmony_ci if (ret < 0) 10962306a36Sopenharmony_ci return ret; 11062306a36Sopenharmony_ci ah->av.eth.gid_index = ret; 11162306a36Sopenharmony_ci } else { 11262306a36Sopenharmony_ci /* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */ 11362306a36Sopenharmony_ci ah->av.eth.gid_index = ah_attr->grh.sgid_index; 11462306a36Sopenharmony_ci } 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci if (vlan_tag < 0x1000) 11762306a36Sopenharmony_ci vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13; 11862306a36Sopenharmony_ci ah->av.eth.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn | 11962306a36Sopenharmony_ci (rdma_ah_get_port_num(ah_attr) << 24)); 12062306a36Sopenharmony_ci ah->av.eth.vlan = cpu_to_be16(vlan_tag); 12162306a36Sopenharmony_ci ah->av.eth.hop_limit = grh->hop_limit; 12262306a36Sopenharmony_ci if (rdma_ah_get_static_rate(ah_attr)) { 12362306a36Sopenharmony_ci ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) + 12462306a36Sopenharmony_ci MLX4_STAT_RATE_OFFSET; 12562306a36Sopenharmony_ci while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET && 12662306a36Sopenharmony_ci !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support)) 12762306a36Sopenharmony_ci --ah->av.eth.stat_rate; 12862306a36Sopenharmony_ci } 12962306a36Sopenharmony_ci ah->av.eth.sl_tclass_flowlabel |= 13062306a36Sopenharmony_ci cpu_to_be32((grh->traffic_class << 20) | 13162306a36Sopenharmony_ci grh->flow_label); 13262306a36Sopenharmony_ci /* 13362306a36Sopenharmony_ci * HW requires multicast LID so we just choose one. 13462306a36Sopenharmony_ci */ 13562306a36Sopenharmony_ci if (is_mcast) 13662306a36Sopenharmony_ci ah->av.ib.dlid = cpu_to_be16(0xc000); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci memcpy(ah->av.eth.dgid, grh->dgid.raw, 16); 13962306a36Sopenharmony_ci ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr) 14062306a36Sopenharmony_ci << 29); 14162306a36Sopenharmony_ci return 0; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ciint mlx4_ib_create_ah(struct ib_ah *ib_ah, struct rdma_ah_init_attr *init_attr, 14562306a36Sopenharmony_ci struct ib_udata *udata) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci struct rdma_ah_attr *ah_attr = init_attr->ah_attr; 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { 15062306a36Sopenharmony_ci if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) 15162306a36Sopenharmony_ci return -EINVAL; 15262306a36Sopenharmony_ci /* 15362306a36Sopenharmony_ci * TBD: need to handle the case when we get 15462306a36Sopenharmony_ci * called in an atomic context and there we 15562306a36Sopenharmony_ci * might sleep. We don't expect this 15662306a36Sopenharmony_ci * currently since we're working with link 15762306a36Sopenharmony_ci * local addresses which we can translate 15862306a36Sopenharmony_ci * without going to sleep. 15962306a36Sopenharmony_ci */ 16062306a36Sopenharmony_ci return create_iboe_ah(ib_ah, ah_attr); 16162306a36Sopenharmony_ci } 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci create_ib_ah(ib_ah, ah_attr); 16462306a36Sopenharmony_ci return 0; 16562306a36Sopenharmony_ci} 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ciint mlx4_ib_create_ah_slave(struct ib_ah *ah, struct rdma_ah_attr *ah_attr, 16862306a36Sopenharmony_ci int slave_sgid_index, u8 *s_mac, u16 vlan_tag) 16962306a36Sopenharmony_ci{ 17062306a36Sopenharmony_ci struct rdma_ah_attr slave_attr = *ah_attr; 17162306a36Sopenharmony_ci struct rdma_ah_init_attr init_attr = {}; 17262306a36Sopenharmony_ci struct mlx4_ib_ah *mah = to_mah(ah); 17362306a36Sopenharmony_ci int ret; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci slave_attr.grh.sgid_attr = NULL; 17662306a36Sopenharmony_ci slave_attr.grh.sgid_index = slave_sgid_index; 17762306a36Sopenharmony_ci init_attr.ah_attr = &slave_attr; 17862306a36Sopenharmony_ci ret = mlx4_ib_create_ah(ah, &init_attr, NULL); 17962306a36Sopenharmony_ci if (ret) 18062306a36Sopenharmony_ci return ret; 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci ah->type = ah_attr->type; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci /* get rid of force-loopback bit */ 18562306a36Sopenharmony_ci mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF); 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) 18862306a36Sopenharmony_ci memcpy(mah->av.eth.s_mac, s_mac, 6); 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci if (vlan_tag < 0x1000) 19162306a36Sopenharmony_ci vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13; 19262306a36Sopenharmony_ci mah->av.eth.vlan = cpu_to_be16(vlan_tag); 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci return 0; 19562306a36Sopenharmony_ci} 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ciint mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr) 19862306a36Sopenharmony_ci{ 19962306a36Sopenharmony_ci struct mlx4_ib_ah *ah = to_mah(ibah); 20062306a36Sopenharmony_ci int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24; 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci memset(ah_attr, 0, sizeof *ah_attr); 20362306a36Sopenharmony_ci ah_attr->type = ibah->type; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { 20662306a36Sopenharmony_ci rdma_ah_set_dlid(ah_attr, 0); 20762306a36Sopenharmony_ci rdma_ah_set_sl(ah_attr, 20862306a36Sopenharmony_ci be32_to_cpu(ah->av.eth.sl_tclass_flowlabel) 20962306a36Sopenharmony_ci >> 29); 21062306a36Sopenharmony_ci } else { 21162306a36Sopenharmony_ci rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid)); 21262306a36Sopenharmony_ci rdma_ah_set_sl(ah_attr, 21362306a36Sopenharmony_ci be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) 21462306a36Sopenharmony_ci >> 28); 21562306a36Sopenharmony_ci } 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci rdma_ah_set_port_num(ah_attr, port_num); 21862306a36Sopenharmony_ci if (ah->av.ib.stat_rate) 21962306a36Sopenharmony_ci rdma_ah_set_static_rate(ah_attr, 22062306a36Sopenharmony_ci ah->av.ib.stat_rate - 22162306a36Sopenharmony_ci MLX4_STAT_RATE_OFFSET); 22262306a36Sopenharmony_ci rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F); 22362306a36Sopenharmony_ci if (mlx4_ib_ah_grh_present(ah)) { 22462306a36Sopenharmony_ci u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel); 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci rdma_ah_set_grh(ah_attr, NULL, 22762306a36Sopenharmony_ci tc_fl & 0xfffff, ah->av.ib.gid_index, 22862306a36Sopenharmony_ci ah->av.ib.hop_limit, 22962306a36Sopenharmony_ci tc_fl >> 20); 23062306a36Sopenharmony_ci rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid); 23162306a36Sopenharmony_ci } 23262306a36Sopenharmony_ci 23362306a36Sopenharmony_ci return 0; 23462306a36Sopenharmony_ci} 235