xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_ah.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
112515907SHans Petter Selasky /*-
2b633e08cSHans Petter Selasky  * Copyright (c) 2013-2020, Mellanox Technologies, Ltd.  All rights reserved.
312515907SHans Petter Selasky  *
412515907SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
512515907SHans Petter Selasky  * modification, are permitted provided that the following conditions
612515907SHans Petter Selasky  * are met:
712515907SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
812515907SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
912515907SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
1012515907SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
1112515907SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
1212515907SHans Petter Selasky  *
1312515907SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
1412515907SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1512515907SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1612515907SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1712515907SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1812515907SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1912515907SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2012515907SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2112515907SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2212515907SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2312515907SHans Petter Selasky  * SUCH DAMAGE.
2412515907SHans Petter Selasky  */
2512515907SHans Petter Selasky 
2670600979SKonstantin Belousov #include "opt_rss.h"
2770600979SKonstantin Belousov #include "opt_ratelimit.h"
2870600979SKonstantin Belousov 
29*028130b8SKonstantin Belousov #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
3012515907SHans Petter Selasky 
create_ib_ah(struct mlx5_ib_dev * dev,struct mlx5_ib_ah * ah,struct ib_ah_attr * ah_attr,enum rdma_link_layer ll)31b633e08cSHans Petter Selasky static void create_ib_ah(struct mlx5_ib_dev *dev,
328e6e287fSHans Petter Selasky 				  struct mlx5_ib_ah *ah,
3312515907SHans Petter Selasky 				  struct ib_ah_attr *ah_attr,
348e6e287fSHans Petter Selasky 				  enum rdma_link_layer ll)
3512515907SHans Petter Selasky {
3612515907SHans Petter Selasky 	if (ah_attr->ah_flags & IB_AH_GRH) {
3712515907SHans Petter Selasky 		memcpy(ah->av.rgid, &ah_attr->grh.dgid, 16);
3812515907SHans Petter Selasky 		ah->av.grh_gid_fl = cpu_to_be32(ah_attr->grh.flow_label |
3912515907SHans Petter Selasky 						(1 << 30) |
4012515907SHans Petter Selasky 						ah_attr->grh.sgid_index << 20);
4112515907SHans Petter Selasky 		ah->av.hop_limit = ah_attr->grh.hop_limit;
4212515907SHans Petter Selasky 		ah->av.tclass = ah_attr->grh.traffic_class;
4312515907SHans Petter Selasky 	}
4412515907SHans Petter Selasky 
4512515907SHans Petter Selasky 	ah->av.stat_rate_sl = (ah_attr->static_rate << 4);
4612515907SHans Petter Selasky 
4712515907SHans Petter Selasky 	if (ll == IB_LINK_LAYER_ETHERNET) {
4812515907SHans Petter Selasky 		memcpy(ah->av.rmac, ah_attr->dmac, sizeof(ah_attr->dmac));
498e6e287fSHans Petter Selasky 		ah->av.udp_sport =
508e6e287fSHans Petter Selasky 			mlx5_get_roce_udp_sport(dev,
5112515907SHans Petter Selasky 						ah_attr->port_num,
528e6e287fSHans Petter Selasky 						ah_attr->grh.sgid_index);
5312515907SHans Petter Selasky 		ah->av.stat_rate_sl |= (ah_attr->sl & 0x7) << 1;
5412515907SHans Petter Selasky 	} else {
5512515907SHans Petter Selasky 		ah->av.rlid = cpu_to_be16(ah_attr->dlid);
5612515907SHans Petter Selasky 		ah->av.fl_mlid = ah_attr->src_path_bits & 0x7f;
5712515907SHans Petter Selasky 		ah->av.stat_rate_sl |= (ah_attr->sl & 0xf);
5812515907SHans Petter Selasky 	}
5912515907SHans Petter Selasky }
6012515907SHans Petter Selasky 
mlx5_ib_create_ah(struct ib_ah * ibah,struct ib_ah_attr * ah_attr,u32 flags,struct ib_udata * udata)61b633e08cSHans Petter Selasky int mlx5_ib_create_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr,
62b633e08cSHans Petter Selasky 		      u32 flags, struct ib_udata *udata)
631456d97cSHans Petter Selasky 
6412515907SHans Petter Selasky {
65b633e08cSHans Petter Selasky 	struct mlx5_ib_ah *ah = to_mah(ibah);
66b633e08cSHans Petter Selasky 	struct mlx5_ib_dev *dev = to_mdev(ibah->device);
6712515907SHans Petter Selasky 	enum rdma_link_layer ll;
688e6e287fSHans Petter Selasky 
69b633e08cSHans Petter Selasky 	ll = dev->ib_dev.get_link_layer(&dev->ib_dev, ah_attr->port_num);
708e6e287fSHans Petter Selasky 
718e6e287fSHans Petter Selasky 	if (ll == IB_LINK_LAYER_ETHERNET && !(ah_attr->ah_flags & IB_AH_GRH))
72b633e08cSHans Petter Selasky 		return -EINVAL;
7312515907SHans Petter Selasky 
741456d97cSHans Petter Selasky 	if (ll == IB_LINK_LAYER_ETHERNET && udata) {
751456d97cSHans Petter Selasky 		int err;
761456d97cSHans Petter Selasky 		struct mlx5_ib_create_ah_resp resp = {};
771456d97cSHans Petter Selasky 		u32 min_resp_len = offsetof(typeof(resp), dmac) +
781456d97cSHans Petter Selasky 				   sizeof(resp.dmac);
791456d97cSHans Petter Selasky 
801456d97cSHans Petter Selasky 		if (udata->outlen < min_resp_len)
81b633e08cSHans Petter Selasky 			return -EINVAL;
821456d97cSHans Petter Selasky 
831456d97cSHans Petter Selasky 		resp.response_length = min_resp_len;
841456d97cSHans Petter Selasky 
85b633e08cSHans Petter Selasky 		err = ib_resolve_eth_dmac(&dev->ib_dev, ah_attr);
861456d97cSHans Petter Selasky 		if (err)
87b633e08cSHans Petter Selasky 			return err;
881456d97cSHans Petter Selasky 
891456d97cSHans Petter Selasky 		memcpy(resp.dmac, ah_attr->dmac, ETH_ALEN);
901456d97cSHans Petter Selasky 		err = ib_copy_to_udata(udata, &resp, resp.response_length);
911456d97cSHans Petter Selasky 		if (err)
92b633e08cSHans Petter Selasky 			return err;
931456d97cSHans Petter Selasky 	}
941456d97cSHans Petter Selasky 
95b633e08cSHans Petter Selasky 	create_ib_ah(dev, ah, ah_attr, ll);
96b633e08cSHans Petter Selasky 	return 0;
9712515907SHans Petter Selasky }
9812515907SHans Petter Selasky 
mlx5_ib_query_ah(struct ib_ah * ibah,struct ib_ah_attr * ah_attr)9912515907SHans Petter Selasky int mlx5_ib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
10012515907SHans Petter Selasky {
10112515907SHans Petter Selasky 	struct mlx5_ib_ah *ah = to_mah(ibah);
10212515907SHans Petter Selasky 	u32 tmp;
10312515907SHans Petter Selasky 
10412515907SHans Petter Selasky 	memset(ah_attr, 0, sizeof(*ah_attr));
10512515907SHans Petter Selasky 
10612515907SHans Petter Selasky 	tmp = be32_to_cpu(ah->av.grh_gid_fl);
10712515907SHans Petter Selasky 	if (tmp & (1 << 30)) {
10812515907SHans Petter Selasky 		ah_attr->ah_flags = IB_AH_GRH;
10912515907SHans Petter Selasky 		ah_attr->grh.sgid_index = (tmp >> 20) & 0xff;
11012515907SHans Petter Selasky 		ah_attr->grh.flow_label = tmp & 0xfffff;
11112515907SHans Petter Selasky 		memcpy(&ah_attr->grh.dgid, ah->av.rgid, 16);
11212515907SHans Petter Selasky 		ah_attr->grh.hop_limit = ah->av.hop_limit;
11312515907SHans Petter Selasky 		ah_attr->grh.traffic_class = ah->av.tclass;
11412515907SHans Petter Selasky 	}
11512515907SHans Petter Selasky 	ah_attr->dlid = be16_to_cpu(ah->av.rlid);
11612515907SHans Petter Selasky 	ah_attr->static_rate = ah->av.stat_rate_sl >> 4;
11712515907SHans Petter Selasky 	ah_attr->sl = ah->av.stat_rate_sl & 0xf;
11812515907SHans Petter Selasky 
11912515907SHans Petter Selasky 	return 0;
12012515907SHans Petter Selasky }
12112515907SHans Petter Selasky 
mlx5_ib_destroy_ah(struct ib_ah * ah,u32 flags)122b633e08cSHans Petter Selasky void mlx5_ib_destroy_ah(struct ib_ah *ah, u32 flags)
12312515907SHans Petter Selasky {
124b633e08cSHans Petter Selasky 	return;
12512515907SHans Petter Selasky }
126