1225c7b1fSRoland Dreier /* 2225c7b1fSRoland Dreier * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved. 3225c7b1fSRoland Dreier * 4225c7b1fSRoland Dreier * This software is available to you under a choice of one of two 5225c7b1fSRoland Dreier * licenses. You may choose to be licensed under the terms of the GNU 6225c7b1fSRoland Dreier * General Public License (GPL) Version 2, available from the file 7225c7b1fSRoland Dreier * COPYING in the main directory of this source tree, or the 8225c7b1fSRoland Dreier * OpenIB.org BSD license below: 9225c7b1fSRoland Dreier * 10225c7b1fSRoland Dreier * Redistribution and use in source and binary forms, with or 11225c7b1fSRoland Dreier * without modification, are permitted provided that the following 12225c7b1fSRoland Dreier * conditions are met: 13225c7b1fSRoland Dreier * 14225c7b1fSRoland Dreier * - Redistributions of source code must retain the above 15225c7b1fSRoland Dreier * copyright notice, this list of conditions and the following 16225c7b1fSRoland Dreier * disclaimer. 17225c7b1fSRoland Dreier * 18225c7b1fSRoland Dreier * - Redistributions in binary form must reproduce the above 19225c7b1fSRoland Dreier * copyright notice, this list of conditions and the following 20225c7b1fSRoland Dreier * disclaimer in the documentation and/or other materials 21225c7b1fSRoland Dreier * provided with the distribution. 22225c7b1fSRoland Dreier * 23225c7b1fSRoland Dreier * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24225c7b1fSRoland Dreier * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25225c7b1fSRoland Dreier * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26225c7b1fSRoland Dreier * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27225c7b1fSRoland Dreier * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28225c7b1fSRoland Dreier * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29225c7b1fSRoland Dreier * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30225c7b1fSRoland Dreier * SOFTWARE. 31225c7b1fSRoland Dreier */ 32225c7b1fSRoland Dreier 33fa417f7bSEli Cohen #include <rdma/ib_addr.h> 344c3eb3caSEli Cohen #include <rdma/ib_cache.h> 35fa417f7bSEli Cohen 365a0e3ad6STejun Heo #include <linux/slab.h> 37fa417f7bSEli Cohen #include <linux/inet.h> 38fa417f7bSEli Cohen #include <linux/string.h> 39c6215745SMoni Shoua #include <linux/mlx4/driver.h> 405a0e3ad6STejun Heo 41225c7b1fSRoland Dreier #include "mlx4_ib.h" 42225c7b1fSRoland Dreier 43d3456914SLeon Romanovsky static void create_ib_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr) 44225c7b1fSRoland Dreier { 45d3456914SLeon Romanovsky struct mlx4_ib_ah *ah = to_mah(ib_ah); 46d3456914SLeon Romanovsky struct mlx4_dev *dev = to_mdev(ib_ah->device)->dev; 47225c7b1fSRoland Dreier 48d3456914SLeon Romanovsky ah->av.ib.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn | 49d8966fcdSDasaratharaman Chandramouli (rdma_ah_get_port_num(ah_attr) << 24)); 50d8966fcdSDasaratharaman Chandramouli ah->av.ib.g_slid = rdma_ah_get_path_bits(ah_attr); 51d8966fcdSDasaratharaman Chandramouli ah->av.ib.sl_tclass_flowlabel = 52d8966fcdSDasaratharaman Chandramouli cpu_to_be32(rdma_ah_get_sl(ah_attr) << 28); 53d8966fcdSDasaratharaman Chandramouli if (rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) { 54d8966fcdSDasaratharaman Chandramouli const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr); 55d8966fcdSDasaratharaman Chandramouli 56fa417f7bSEli Cohen ah->av.ib.g_slid |= 0x80; 57d8966fcdSDasaratharaman Chandramouli ah->av.ib.gid_index = grh->sgid_index; 58d8966fcdSDasaratharaman Chandramouli ah->av.ib.hop_limit = grh->hop_limit; 59fa417f7bSEli Cohen ah->av.ib.sl_tclass_flowlabel |= 60d8966fcdSDasaratharaman Chandramouli cpu_to_be32((grh->traffic_class << 20) | 61d8966fcdSDasaratharaman Chandramouli grh->flow_label); 62d8966fcdSDasaratharaman Chandramouli memcpy(ah->av.ib.dgid, grh->dgid.raw, 16); 63fa417f7bSEli Cohen } 64fa417f7bSEli Cohen 65d8966fcdSDasaratharaman Chandramouli ah->av.ib.dlid = cpu_to_be16(rdma_ah_get_dlid(ah_attr)); 66d8966fcdSDasaratharaman Chandramouli if (rdma_ah_get_static_rate(ah_attr)) { 67d8966fcdSDasaratharaman Chandramouli u8 static_rate = rdma_ah_get_static_rate(ah_attr) + 68d8966fcdSDasaratharaman Chandramouli MLX4_STAT_RATE_OFFSET; 69d8966fcdSDasaratharaman Chandramouli 70d8966fcdSDasaratharaman Chandramouli while (static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET && 71d8966fcdSDasaratharaman Chandramouli !(1 << static_rate & dev->caps.stat_rate_support)) 72d8966fcdSDasaratharaman Chandramouli --static_rate; 73d8966fcdSDasaratharaman Chandramouli ah->av.ib.stat_rate = static_rate; 74fa417f7bSEli Cohen } 75fa417f7bSEli Cohen } 76fa417f7bSEli Cohen 77d3456914SLeon Romanovsky static int create_iboe_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr) 78fa417f7bSEli Cohen { 79d3456914SLeon Romanovsky struct mlx4_ib_dev *ibdev = to_mdev(ib_ah->device); 80d3456914SLeon Romanovsky struct mlx4_ib_ah *ah = to_mah(ib_ah); 8147ec3866SParav Pandit const struct ib_gid_attr *gid_attr; 82fa417f7bSEli Cohen struct mlx4_dev *dev = ibdev->dev; 83bfdfcfeeSColin Ian King int is_mcast = 0; 84297e0dadSMoni Shoua struct in6_addr in6; 85dbf727deSMatan Barak u16 vlan_tag = 0xffff; 86d8966fcdSDasaratharaman Chandramouli const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr); 87dbf727deSMatan Barak int ret; 88fa417f7bSEli Cohen 89d8966fcdSDasaratharaman Chandramouli memcpy(&in6, grh->dgid.raw, sizeof(in6)); 90c0348eb0SParav Pandit if (rdma_is_multicast_addr(&in6)) 91297e0dadSMoni Shoua is_mcast = 1; 92c0348eb0SParav Pandit 9344c58487SDasaratharaman Chandramouli memcpy(ah->av.eth.mac, ah_attr->roce.dmac, ETH_ALEN); 9439f42655SLucas Tanure eth_zero_addr(ah->av.eth.s_mac); 955e62d5ffSJason Gunthorpe 965e62d5ffSJason Gunthorpe /* 975e62d5ffSJason Gunthorpe * If sgid_attr is NULL we are being called by mlx4_ib_create_ah_slave 985e62d5ffSJason Gunthorpe * and we are directly creating an AV for a slave's gid_index. 995e62d5ffSJason Gunthorpe */ 10047ec3866SParav Pandit gid_attr = ah_attr->grh.sgid_attr; 1015e62d5ffSJason Gunthorpe if (gid_attr) { 102a70c0739SParav Pandit ret = rdma_read_gid_l2_fields(gid_attr, &vlan_tag, 103a70c0739SParav Pandit &ah->av.eth.s_mac[0]); 104a70c0739SParav Pandit if (ret) 105a70c0739SParav Pandit return ret; 106a70c0739SParav Pandit 1075e62d5ffSJason Gunthorpe ret = mlx4_ib_gid_index_to_real_index(ibdev, gid_attr); 1085e62d5ffSJason Gunthorpe if (ret < 0) 109d3456914SLeon Romanovsky return ret; 1105e62d5ffSJason Gunthorpe ah->av.eth.gid_index = ret; 1115e62d5ffSJason Gunthorpe } else { 1125e62d5ffSJason Gunthorpe /* mlx4_ib_create_ah_slave fills in the s_mac and the vlan */ 1135e62d5ffSJason Gunthorpe ah->av.eth.gid_index = ah_attr->grh.sgid_index; 1145e62d5ffSJason Gunthorpe } 11547ec3866SParav Pandit 1164c3eb3caSEli Cohen if (vlan_tag < 0x1000) 117d8966fcdSDasaratharaman Chandramouli vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13; 118d3456914SLeon Romanovsky ah->av.eth.port_pd = cpu_to_be32(to_mpd(ib_ah->pd)->pdn | 119d8966fcdSDasaratharaman Chandramouli (rdma_ah_get_port_num(ah_attr) << 24)); 1204c3eb3caSEli Cohen ah->av.eth.vlan = cpu_to_be16(vlan_tag); 121d8966fcdSDasaratharaman Chandramouli ah->av.eth.hop_limit = grh->hop_limit; 122d8966fcdSDasaratharaman Chandramouli if (rdma_ah_get_static_rate(ah_attr)) { 123d8966fcdSDasaratharaman Chandramouli ah->av.eth.stat_rate = rdma_ah_get_static_rate(ah_attr) + 124d8966fcdSDasaratharaman Chandramouli MLX4_STAT_RATE_OFFSET; 125fa417f7bSEli Cohen while (ah->av.eth.stat_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET && 126fa417f7bSEli Cohen !(1 << ah->av.eth.stat_rate & dev->caps.stat_rate_support)) 127fa417f7bSEli Cohen --ah->av.eth.stat_rate; 128fa417f7bSEli Cohen } 129af4295c1SMaor Gottlieb ah->av.eth.sl_tclass_flowlabel |= 130d8966fcdSDasaratharaman Chandramouli cpu_to_be32((grh->traffic_class << 20) | 131d8966fcdSDasaratharaman Chandramouli grh->flow_label); 132fa417f7bSEli Cohen /* 133fa417f7bSEli Cohen * HW requires multicast LID so we just choose one. 134fa417f7bSEli Cohen */ 135fa417f7bSEli Cohen if (is_mcast) 136fa417f7bSEli Cohen ah->av.ib.dlid = cpu_to_be16(0xc000); 137fa417f7bSEli Cohen 138d8966fcdSDasaratharaman Chandramouli memcpy(ah->av.eth.dgid, grh->dgid.raw, 16); 139d8966fcdSDasaratharaman Chandramouli ah->av.eth.sl_tclass_flowlabel |= cpu_to_be32(rdma_ah_get_sl(ah_attr) 140d8966fcdSDasaratharaman Chandramouli << 29); 141d3456914SLeon Romanovsky return 0; 142fa417f7bSEli Cohen } 143fa417f7bSEli Cohen 144*fa5d010cSMaor Gottlieb int mlx4_ib_create_ah(struct ib_ah *ib_ah, struct rdma_ah_init_attr *init_attr, 145*fa5d010cSMaor Gottlieb struct ib_udata *udata) 146fa417f7bSEli Cohen { 147*fa5d010cSMaor Gottlieb struct rdma_ah_attr *ah_attr = init_attr->ah_attr; 148*fa5d010cSMaor Gottlieb 14944c58487SDasaratharaman Chandramouli if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { 150d3456914SLeon Romanovsky if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) 151d3456914SLeon Romanovsky return -EINVAL; 152fa417f7bSEli Cohen /* 153fa417f7bSEli Cohen * TBD: need to handle the case when we get 154fa417f7bSEli Cohen * called in an atomic context and there we 155fa417f7bSEli Cohen * might sleep. We don't expect this 156fa417f7bSEli Cohen * currently since we're working with link 157fa417f7bSEli Cohen * local addresses which we can translate 158fa417f7bSEli Cohen * without going to sleep. 159fa417f7bSEli Cohen */ 160d3456914SLeon Romanovsky return create_iboe_ah(ib_ah, ah_attr); 161225c7b1fSRoland Dreier } 162225c7b1fSRoland Dreier 163d3456914SLeon Romanovsky create_ib_ah(ib_ah, ah_attr); 164d3456914SLeon Romanovsky return 0; 165225c7b1fSRoland Dreier } 166225c7b1fSRoland Dreier 167d3456914SLeon Romanovsky int mlx4_ib_create_ah_slave(struct ib_ah *ah, struct rdma_ah_attr *ah_attr, 168d3456914SLeon Romanovsky int slave_sgid_index, u8 *s_mac, u16 vlan_tag) 1695e62d5ffSJason Gunthorpe { 1705e62d5ffSJason Gunthorpe struct rdma_ah_attr slave_attr = *ah_attr; 171*fa5d010cSMaor Gottlieb struct rdma_ah_init_attr init_attr = {}; 172d3456914SLeon Romanovsky struct mlx4_ib_ah *mah = to_mah(ah); 173d3456914SLeon Romanovsky int ret; 1745e62d5ffSJason Gunthorpe 1755e62d5ffSJason Gunthorpe slave_attr.grh.sgid_attr = NULL; 1765e62d5ffSJason Gunthorpe slave_attr.grh.sgid_index = slave_sgid_index; 177*fa5d010cSMaor Gottlieb init_attr.ah_attr = &slave_attr; 178*fa5d010cSMaor Gottlieb ret = mlx4_ib_create_ah(ah, &init_attr, NULL); 179d3456914SLeon Romanovsky if (ret) 180d3456914SLeon Romanovsky return ret; 1815e62d5ffSJason Gunthorpe 1825e62d5ffSJason Gunthorpe ah->type = ah_attr->type; 1835e62d5ffSJason Gunthorpe 1845e62d5ffSJason Gunthorpe /* get rid of force-loopback bit */ 1855e62d5ffSJason Gunthorpe mah->av.ib.port_pd &= cpu_to_be32(0x7FFFFFFF); 1865e62d5ffSJason Gunthorpe 1875e62d5ffSJason Gunthorpe if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) 1885e62d5ffSJason Gunthorpe memcpy(mah->av.eth.s_mac, s_mac, 6); 1895e62d5ffSJason Gunthorpe 1905e62d5ffSJason Gunthorpe if (vlan_tag < 0x1000) 1915e62d5ffSJason Gunthorpe vlan_tag |= (rdma_ah_get_sl(ah_attr) & 7) << 13; 1925e62d5ffSJason Gunthorpe mah->av.eth.vlan = cpu_to_be16(vlan_tag); 1935e62d5ffSJason Gunthorpe 194d3456914SLeon Romanovsky return 0; 1955e62d5ffSJason Gunthorpe } 1965e62d5ffSJason Gunthorpe 19790898850SDasaratharaman Chandramouli int mlx4_ib_query_ah(struct ib_ah *ibah, struct rdma_ah_attr *ah_attr) 198225c7b1fSRoland Dreier { 199225c7b1fSRoland Dreier struct mlx4_ib_ah *ah = to_mah(ibah); 20044c58487SDasaratharaman Chandramouli int port_num = be32_to_cpu(ah->av.ib.port_pd) >> 24; 201225c7b1fSRoland Dreier 202225c7b1fSRoland Dreier memset(ah_attr, 0, sizeof *ah_attr); 20344c58487SDasaratharaman Chandramouli ah_attr->type = ibah->type; 20444c58487SDasaratharaman Chandramouli 20544c58487SDasaratharaman Chandramouli if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { 20644c58487SDasaratharaman Chandramouli rdma_ah_set_dlid(ah_attr, 0); 207d8966fcdSDasaratharaman Chandramouli rdma_ah_set_sl(ah_attr, 208d8966fcdSDasaratharaman Chandramouli be32_to_cpu(ah->av.eth.sl_tclass_flowlabel) 209d8966fcdSDasaratharaman Chandramouli >> 29); 21044c58487SDasaratharaman Chandramouli } else { 21144c58487SDasaratharaman Chandramouli rdma_ah_set_dlid(ah_attr, be16_to_cpu(ah->av.ib.dlid)); 212d8966fcdSDasaratharaman Chandramouli rdma_ah_set_sl(ah_attr, 213d8966fcdSDasaratharaman Chandramouli be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) 214d8966fcdSDasaratharaman Chandramouli >> 28); 21544c58487SDasaratharaman Chandramouli } 2165e99b139SNoa Osherovich 21744c58487SDasaratharaman Chandramouli rdma_ah_set_port_num(ah_attr, port_num); 218fa417f7bSEli Cohen if (ah->av.ib.stat_rate) 219d8966fcdSDasaratharaman Chandramouli rdma_ah_set_static_rate(ah_attr, 220d8966fcdSDasaratharaman Chandramouli ah->av.ib.stat_rate - 221d8966fcdSDasaratharaman Chandramouli MLX4_STAT_RATE_OFFSET); 222d8966fcdSDasaratharaman Chandramouli rdma_ah_set_path_bits(ah_attr, ah->av.ib.g_slid & 0x7F); 223225c7b1fSRoland Dreier if (mlx4_ib_ah_grh_present(ah)) { 224d8966fcdSDasaratharaman Chandramouli u32 tc_fl = be32_to_cpu(ah->av.ib.sl_tclass_flowlabel); 225225c7b1fSRoland Dreier 226d8966fcdSDasaratharaman Chandramouli rdma_ah_set_grh(ah_attr, NULL, 227d8966fcdSDasaratharaman Chandramouli tc_fl & 0xfffff, ah->av.ib.gid_index, 228d8966fcdSDasaratharaman Chandramouli ah->av.ib.hop_limit, 229d8966fcdSDasaratharaman Chandramouli tc_fl >> 20); 230d8966fcdSDasaratharaman Chandramouli rdma_ah_set_dgid_raw(ah_attr, ah->av.ib.dgid); 231225c7b1fSRoland Dreier } 232225c7b1fSRoland Dreier 233225c7b1fSRoland Dreier return 0; 234225c7b1fSRoland Dreier } 235