2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <rdma/ib_addr.h>
34 #include <rdma/ib_cache.h>
36 #include <linux/slab.h>
37 #include <linux/inet.h>
38 #include <linux/string.h>
39 #include <linux/mlx4/driver.h>
43 static void create_ib_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
45 struct mlx4_ib_ah *ah = to_mah(ib_ah);
46 struct mlx4_dev *dev = to_mdev(ib_ah->device)->dev;
48 ah->av.ib.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn |
49 (rdma_ah_get_port_num(ah_attr) << 24));
50 ah->av.ib.g_slid = rdma_ah_get_path_bits(ah_attr);
51 ah->av.ib.sl_tclass_flowlabel =
52 cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28);
53 if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) {
54 const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
56 ah->av.ib.g_slid |= 0x80;
57 ah->av.ib.gid_index = grh->sgid_index;
58 ah->av.ib.hop_limit = grh->hop_limit;
59 ah->av.ib.sl_tclass_flowlabel |=
60 cpu_to_be32((grh->traffic_class << 20) |
62 memcpy(ah->av.ib.dgid, grh->dgid.raw, 16);
65 ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr));
66 if (rdma_ah_get_static_rate(ah_attr)) {
67 u8 static_rate = rdma_ah_get_static_rate(ah_attr) +
68 MLX4_STAT_RATE_OFFSET;
70 while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
71 !(1 << static_rate & dev->caps.stat_rate_support))
73 ah->av.ib.stat_rate = static_rate;
77 static int create_iboe_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
79 struct mlx4_ib_dev *ibdev = to_mdev(ib_ah->device);
80 struct mlx4_ib_ah *ah = to_mah(ib_ah);
81 const struct ib_gid_attr *gid_attr;
82 struct mlx4_dev *dev = ibdev->dev;
85 u16 vlan_tag = 0xffff;
86 const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
89 memcpy(&in6, grh->dgid.raw, sizeof(in6));
90 if (rdma_is_multicast_addr(&in6))
93 memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN);
94 eth_zero_addr(ah->av.eth.s_mac);
97 * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave
98 * and we are directly creating an AV for a slave's gid_index.
100 gid_attr = ah_attr->grh.sgid_attr;
102 if (is_vlan_dev(gid_attr->ndev))
103 vlan_tag = vlan_dev_vlan_id(gid_attr->ndev);
104 memcpy(ah->av.eth.s_mac, gid_attr->ndev->dev_addr, ETH_ALEN);
105 ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr);
108 ah->av.eth.gid_index = ret;
110 /* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */
111 ah->av.eth.gid_index = ah_attr->grh.sgid_index;
114 if (vlan_tag < 0x1000)
115 vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
116 ah->av.eth.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn |
117 (rdma_ah_get_port_num(ah_attr) << 24));
118 ah->av.eth.vlan = cpu_to_be16(vlan_tag);
119 ah->av.eth.hop_limit = grh->hop_limit;
120 if (rdma_ah_get_static_rate(ah_attr)) {
121 ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) +
122 MLX4_STAT_RATE_OFFSET;
123 while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
124 !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support))
125 --ah->av.eth.stat_rate;
127 ah->av.eth.sl_tclass_flowlabel |=
128 cpu_to_be32((grh->traffic_class << 20) |
131 * HW requires multicast LID so we just choose one.
134 ah->av.ib.dlid = cpu_to_be16(0xc000);
136 memcpy(ah->av.eth.dgid, grh->dgid.raw, 16);
137 ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr)
142 int mlx4_ib_create_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr,
143 u32 flags, struct ib_udata *udata)
146 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
147 if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
150 * TBD: need to handle the case when we get
151 * called in an atomic context and there we
152 * might sleep. We don't expect this
153 * currently since we're working with link
154 * local addresses which we can translate
155 * without going to sleep.
157 return create_iboe_ah(ib_ah, ah_attr);
160 create_ib_ah(ib_ah, ah_attr);
164 int mlx4_ib_create_ah_slave(struct ib_ah *ah, struct rdma_ah_attr *ah_attr,
165 int slave_sgid_index, u8 *s_mac, u16 vlan_tag)
167 struct rdma_ah_attr slave_attr = *ah_attr;
168 struct mlx4_ib_ah *mah = to_mah(ah);
171 slave_attr.grh.sgid_attr = NULL;
172 slave_attr.grh.sgid_index = slave_sgid_index;
173 ret = mlx4_ib_create_ah(ah, &slave_attr, 0, NULL);
177 ah->type = ah_attr->type;
179 /* get rid of force-loopback bit */
180 mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF);
182 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
183 memcpy(mah->av.eth.s_mac, s_mac, 6);
185 if (vlan_tag < 0x1000)
186 vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13;
187 mah->av.eth.vlan = cpu_to_be16(vlan_tag);
192 int mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr)
194 struct mlx4_ib_ah *ah = to_mah(ibah);
195 int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24;
197 memset(ah_attr, 0, sizeof *ah_attr);
198 ah_attr->type = ibah->type;
200 if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
201 rdma_ah_set_dlid(ah_attr, 0);
202 rdma_ah_set_sl(ah_attr,
203 be32_to_cpu(ah->av.eth.sl_tclass_flowlabel)
206 rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid));
207 rdma_ah_set_sl(ah_attr,
208 be32_to_cpu(ah->av.ib.sl_tclass_flowlabel)
212 rdma_ah_set_port_num(ah_attr, port_num);
213 if (ah->av.ib.stat_rate)
214 rdma_ah_set_static_rate(ah_attr,
215 ah->av.ib.stat_rate -
216 MLX4_STAT_RATE_OFFSET);
217 rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F);
218 if (mlx4_ib_ah_grh_present(ah)) {
219 u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel);
221 rdma_ah_set_grh(ah_attr, NULL,
222 tc_fl & 0xfffff, ah->av.ib.gid_index,
225 rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid);
231 void mlx4_ib_destroy_ah(struct ib_ah *ah, u32 flags)