1/*-
2 * Copyright (c) 2013-2020, Mellanox Technologies, Ltd.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include "opt_rss.h"
27#include "opt_ratelimit.h"
28
29#include <dev/mlx5/mlx5_ib/mlx5_ib.h>
30
31static void create_ib_ah(struct mlx5_ib_dev *dev,
32				  struct mlx5_ib_ah *ah,
33				  struct ib_ah_attr *ah_attr,
34				  enum rdma_link_layer ll)
35{
36	if (ah_attr->ah_flags & IB_AH_GRH) {
37		memcpy(ah->av.rgid, &ah_attr->grh.dgid, 16);
38		ah->av.grh_gid_fl = cpu_to_be32(ah_attr->grh.flow_label |
39						(1 << 30) |
40						ah_attr->grh.sgid_index << 20);
41		ah->av.hop_limit = ah_attr->grh.hop_limit;
42		ah->av.tclass = ah_attr->grh.traffic_class;
43	}
44
45	ah->av.stat_rate_sl = (ah_attr->static_rate << 4);
46
47	if (ll == IB_LINK_LAYER_ETHERNET) {
48		memcpy(ah->av.rmac, ah_attr->dmac, sizeof(ah_attr->dmac));
49		ah->av.udp_sport =
50			mlx5_get_roce_udp_sport(dev,
51						ah_attr->port_num,
52						ah_attr->grh.sgid_index);
53		ah->av.stat_rate_sl |= (ah_attr->sl & 0x7) << 1;
54	} else {
55		ah->av.rlid = cpu_to_be16(ah_attr->dlid);
56		ah->av.fl_mlid = ah_attr->src_path_bits & 0x7f;
57		ah->av.stat_rate_sl |= (ah_attr->sl & 0xf);
58	}
59}
60
61int mlx5_ib_create_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr,
62		      u32 flags, struct ib_udata *udata)
63
64{
65	struct mlx5_ib_ah *ah = to_mah(ibah);
66	struct mlx5_ib_dev *dev = to_mdev(ibah->device);
67	enum rdma_link_layer ll;
68
69	ll = dev->ib_dev.get_link_layer(&dev->ib_dev, ah_attr->port_num);
70
71	if (ll == IB_LINK_LAYER_ETHERNET && !(ah_attr->ah_flags & IB_AH_GRH))
72		return -EINVAL;
73
74	if (ll == IB_LINK_LAYER_ETHERNET && udata) {
75		int err;
76		struct mlx5_ib_create_ah_resp resp = {};
77		u32 min_resp_len = offsetof(typeof(resp), dmac) +
78				   sizeof(resp.dmac);
79
80		if (udata->outlen < min_resp_len)
81			return -EINVAL;
82
83		resp.response_length = min_resp_len;
84
85		err = ib_resolve_eth_dmac(&dev->ib_dev, ah_attr);
86		if (err)
87			return err;
88
89		memcpy(resp.dmac, ah_attr->dmac, ETH_ALEN);
90		err = ib_copy_to_udata(udata, &resp, resp.response_length);
91		if (err)
92			return err;
93	}
94
95	create_ib_ah(dev, ah, ah_attr, ll);
96	return 0;
97}
98
99int mlx5_ib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
100{
101	struct mlx5_ib_ah *ah = to_mah(ibah);
102	u32 tmp;
103
104	memset(ah_attr, 0, sizeof(*ah_attr));
105
106	tmp = be32_to_cpu(ah->av.grh_gid_fl);
107	if (tmp & (1 << 30)) {
108		ah_attr->ah_flags = IB_AH_GRH;
109		ah_attr->grh.sgid_index = (tmp >> 20) & 0xff;
110		ah_attr->grh.flow_label = tmp & 0xfffff;
111		memcpy(&ah_attr->grh.dgid, ah->av.rgid, 16);
112		ah_attr->grh.hop_limit = ah->av.hop_limit;
113		ah_attr->grh.traffic_class = ah->av.tclass;
114	}
115	ah_attr->dlid = be16_to_cpu(ah->av.rlid);
116	ah_attr->static_rate = ah->av.stat_rate_sl >> 4;
117	ah_attr->sl = ah->av.stat_rate_sl & 0xf;
118
119	return 0;
120}
121
122void mlx5_ib_destroy_ah(struct ib_ah *ah, u32 flags)
123{
124	return;
125}
126