18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2007 Cisco Systems, Inc. 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_addr.h>
348c2ecf20Sopenharmony_ci#include <rdma/ib_cache.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/slab.h>
378c2ecf20Sopenharmony_ci#include <linux/inet.h>
388c2ecf20Sopenharmony_ci#include <linux/string.h>
398c2ecf20Sopenharmony_ci#include <linux/mlx4/driver.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include "mlx4_ib.h"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void create_ib_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct mlx4_ib_ah *ah = to_mah(ib_ah);
468c2ecf20Sopenharmony_ci	struct mlx4_dev *dev = to_mdev(ib_ah->device)->dev;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ah->av.ib.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn |
498c2ecf20Sopenharmony_ci			    (rdma_ah_get_port_num(ah_attr) << 24));
508c2ecf20Sopenharmony_ci	ah->av.ib.g_slid  = rdma_ah_get_path_bits(ah_attr);
518c2ecf20Sopenharmony_ci	ah->av.ib.sl_tclass_flowlabel =
528c2ecf20Sopenharmony_ci			cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
538c2ecf20Sopenharmony_ci	if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
548c2ecf20Sopenharmony_ci		const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		ah->av.ib.g_slid   |= 0x80;
578c2ecf20Sopenharmony_ci		ah->av.ib.gid_index = grh->sgid_index;
588c2ecf20Sopenharmony_ci		ah->av.ib.hop_limit = grh->hop_limit;
598c2ecf20Sopenharmony_ci		ah->av.ib.sl_tclass_flowlabel |=
608c2ecf20Sopenharmony_ci			cpu_to_be32((grh->traffic_class << 20) |
618c2ecf20Sopenharmony_ci				    grh->flow_label);
628c2ecf20Sopenharmony_ci		memcpy(ah->av.ib.dgid, grh->dgid.raw, 16);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
668c2ecf20Sopenharmony_ci	if (rdma_ah_get_static_rate(ah_attr)) {
678c2ecf20Sopenharmony_ci		u8 static_rate = rdma_ah_get_static_rate(ah_attr) +
688c2ecf20Sopenharmony_ci					MLX4_STAT_RATE_OFFSET;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci		while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
718c2ecf20Sopenharmony_ci		       !(1 << static_rate & dev->caps.stat_rate_support))
728c2ecf20Sopenharmony_ci			--static_rate;
738c2ecf20Sopenharmony_ci		ah->av.ib.stat_rate = static_rate;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int create_iboe_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct mlx4_ib_dev *ibdev = to_mdev(ib_ah->device);
808c2ecf20Sopenharmony_ci	struct mlx4_ib_ah *ah = to_mah(ib_ah);
818c2ecf20Sopenharmony_ci	const struct ib_gid_attr *gid_attr;
828c2ecf20Sopenharmony_ci	struct mlx4_dev *dev = ibdev->dev;
838c2ecf20Sopenharmony_ci	int is_mcast = 0;
848c2ecf20Sopenharmony_ci	struct in6_addr in6;
858c2ecf20Sopenharmony_ci	u16 vlan_tag = 0xffff;
868c2ecf20Sopenharmony_ci	const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
878c2ecf20Sopenharmony_ci	int ret;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	memcpy(&in6, grh->dgid.raw, sizeof(in6));
908c2ecf20Sopenharmony_ci	if (rdma_is_multicast_addr(&in6))
918c2ecf20Sopenharmony_ci		is_mcast = 1;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN);
948c2ecf20Sopenharmony_ci	eth_zero_addr(ah->av.eth.s_mac);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave
988c2ecf20Sopenharmony_ci	 * and we are directly creating an AV for a slave's gid_index.
998c2ecf20Sopenharmony_ci	 */
1008c2ecf20Sopenharmony_ci	gid_attr = ah_attr->grh.sgid_attr;
1018c2ecf20Sopenharmony_ci	if (gid_attr) {
1028c2ecf20Sopenharmony_ci		ret = rdma_read_gid_l2_fields(gid_attr, &vlan_tag,
1038c2ecf20Sopenharmony_ci					      &ah->av.eth.s_mac[0]);
1048c2ecf20Sopenharmony_ci		if (ret)
1058c2ecf20Sopenharmony_ci			return ret;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr);
1088c2ecf20Sopenharmony_ci		if (ret < 0)
1098c2ecf20Sopenharmony_ci			return ret;
1108c2ecf20Sopenharmony_ci		ah->av.eth.gid_index = ret;
1118c2ecf20Sopenharmony_ci	} else {
1128c2ecf20Sopenharmony_ci		/* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */
1138c2ecf20Sopenharmony_ci		ah->av.eth.gid_index = ah_attr->grh.sgid_index;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (vlan_tag < 0x1000)
1178c2ecf20Sopenharmony_ci		vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
1188c2ecf20Sopenharmony_ci	ah->av.eth.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn |
1198c2ecf20Sopenharmony_ci					 (rdma_ah_get_port_num(ah_attr) << 24));
1208c2ecf20Sopenharmony_ci	ah->av.eth.vlan = cpu_to_be16(vlan_tag);
1218c2ecf20Sopenharmony_ci	ah->av.eth.hop_limit = grh->hop_limit;
1228c2ecf20Sopenharmony_ci	if (rdma_ah_get_static_rate(ah_attr)) {
1238c2ecf20Sopenharmony_ci		ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) +
1248c2ecf20Sopenharmony_ci					MLX4_STAT_RATE_OFFSET;
1258c2ecf20Sopenharmony_ci		while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
1268c2ecf20Sopenharmony_ci		       !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support))
1278c2ecf20Sopenharmony_ci			--ah->av.eth.stat_rate;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci	ah->av.eth.sl_tclass_flowlabel |=
1308c2ecf20Sopenharmony_ci			cpu_to_be32((grh->traffic_class << 20) |
1318c2ecf20Sopenharmony_ci				    grh->flow_label);
1328c2ecf20Sopenharmony_ci	/*
1338c2ecf20Sopenharmony_ci	 * HW requires multicast LID so we just choose one.
1348c2ecf20Sopenharmony_ci	 */
1358c2ecf20Sopenharmony_ci	if (is_mcast)
1368c2ecf20Sopenharmony_ci		ah->av.ib.dlid = cpu_to_be16(0xc000);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	memcpy(ah->av.eth.dgid, grh->dgid.raw, 16);
1398c2ecf20Sopenharmony_ci	ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr)
1408c2ecf20Sopenharmony_ci						      << 29);
1418c2ecf20Sopenharmony_ci	return 0;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciint mlx4_ib_create_ah(struct ib_ah *ib_ah, struct rdma_ah_init_attr *init_attr,
1458c2ecf20Sopenharmony_ci		      struct ib_udata *udata)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct rdma_ah_attr *ah_attr = init_attr->ah_attr;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
1508c2ecf20Sopenharmony_ci		if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
1518c2ecf20Sopenharmony_ci			return -EINVAL;
1528c2ecf20Sopenharmony_ci		/*
1538c2ecf20Sopenharmony_ci		 * TBD: need to handle the case when we get
1548c2ecf20Sopenharmony_ci		 * called in an atomic context and there we
1558c2ecf20Sopenharmony_ci		 * might sleep.  We don't expect this
1568c2ecf20Sopenharmony_ci		 * currently since we're working with link
1578c2ecf20Sopenharmony_ci		 * local addresses which we can translate
1588c2ecf20Sopenharmony_ci		 * without going to sleep.
1598c2ecf20Sopenharmony_ci		 */
1608c2ecf20Sopenharmony_ci		return create_iboe_ah(ib_ah, ah_attr);
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	create_ib_ah(ib_ah, ah_attr);
1648c2ecf20Sopenharmony_ci	return 0;
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciint mlx4_ib_create_ah_slave(struct ib_ah *ah, struct rdma_ah_attr *ah_attr,
1688c2ecf20Sopenharmony_ci			    int slave_sgid_index, u8 *s_mac, u16 vlan_tag)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct rdma_ah_attr slave_attr = *ah_attr;
1718c2ecf20Sopenharmony_ci	struct rdma_ah_init_attr init_attr = {};
1728c2ecf20Sopenharmony_ci	struct mlx4_ib_ah *mah = to_mah(ah);
1738c2ecf20Sopenharmony_ci	int ret;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	slave_attr.grh.sgid_attr = NULL;
1768c2ecf20Sopenharmony_ci	slave_attr.grh.sgid_index = slave_sgid_index;
1778c2ecf20Sopenharmony_ci	init_attr.ah_attr = &slave_attr;
1788c2ecf20Sopenharmony_ci	ret = mlx4_ib_create_ah(ah, &init_attr, NULL);
1798c2ecf20Sopenharmony_ci	if (ret)
1808c2ecf20Sopenharmony_ci		return ret;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	ah->type = ah_attr->type;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* get rid of force-loopback bit */
1858c2ecf20Sopenharmony_ci	mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
1888c2ecf20Sopenharmony_ci		memcpy(mah->av.eth.s_mac, s_mac, 6);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	if (vlan_tag < 0x1000)
1918c2ecf20Sopenharmony_ci		vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
1928c2ecf20Sopenharmony_ci	mah->av.eth.vlan = cpu_to_be16(vlan_tag);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return 0;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciint mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct mlx4_ib_ah *ah = to_mah(ibah);
2008c2ecf20Sopenharmony_ci	int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	memset(ah_attr, 0, sizeof *ah_attr);
2038c2ecf20Sopenharmony_ci	ah_attr->type = ibah->type;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
2068c2ecf20Sopenharmony_ci		rdma_ah_set_dlid(ah_attr, 0);
2078c2ecf20Sopenharmony_ci		rdma_ah_set_sl(ah_attr,
2088c2ecf20Sopenharmony_ci			       be32_to_cpu(ah->av.eth.sl_tclass_flowlabel)
2098c2ecf20Sopenharmony_ci			       >> 29);
2108c2ecf20Sopenharmony_ci	} else {
2118c2ecf20Sopenharmony_ci		rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid));
2128c2ecf20Sopenharmony_ci		rdma_ah_set_sl(ah_attr,
2138c2ecf20Sopenharmony_ci			       be32_to_cpu(ah->av.ib.sl_tclass_flowlabel)
2148c2ecf20Sopenharmony_ci			       >> 28);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	rdma_ah_set_port_num(ah_attr, port_num);
2188c2ecf20Sopenharmony_ci	if (ah->av.ib.stat_rate)
2198c2ecf20Sopenharmony_ci		rdma_ah_set_static_rate(ah_attr,
2208c2ecf20Sopenharmony_ci					ah->av.ib.stat_rate -
2218c2ecf20Sopenharmony_ci					MLX4_STAT_RATE_OFFSET);
2228c2ecf20Sopenharmony_ci	rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F);
2238c2ecf20Sopenharmony_ci	if (mlx4_ib_ah_grh_present(ah)) {
2248c2ecf20Sopenharmony_ci		u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		rdma_ah_set_grh(ah_attr, NULL,
2278c2ecf20Sopenharmony_ci				tc_fl & 0xfffff, ah->av.ib.gid_index,
2288c2ecf20Sopenharmony_ci				ah->av.ib.hop_limit,
2298c2ecf20Sopenharmony_ci				tc_fl >> 20);
2308c2ecf20Sopenharmony_ci		rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid);
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
235