xref: /openbmc/linux/drivers/infiniband/sw/rxe/rxe_av.c (revision d8966fcd)
18700e3e7SMoni Shoua /*
28700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
38700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
48700e3e7SMoni Shoua  *
58700e3e7SMoni Shoua  * This software is available to you under a choice of one of two
68700e3e7SMoni Shoua  * licenses.  You may choose to be licensed under the terms of the GNU
78700e3e7SMoni Shoua  * General Public License (GPL) Version 2, available from the file
88700e3e7SMoni Shoua  * COPYING in the main directory of this source tree, or the
98700e3e7SMoni Shoua  * OpenIB.org BSD license below:
108700e3e7SMoni Shoua  *
118700e3e7SMoni Shoua  *	   Redistribution and use in source and binary forms, with or
128700e3e7SMoni Shoua  *	   without modification, are permitted provided that the following
138700e3e7SMoni Shoua  *	   conditions are met:
148700e3e7SMoni Shoua  *
158700e3e7SMoni Shoua  *		- Redistributions of source code must retain the above
168700e3e7SMoni Shoua  *		  copyright notice, this list of conditions and the following
178700e3e7SMoni Shoua  *		  disclaimer.
188700e3e7SMoni Shoua  *
198700e3e7SMoni Shoua  *		- Redistributions in binary form must reproduce the above
208700e3e7SMoni Shoua  *		  copyright notice, this list of conditions and the following
218700e3e7SMoni Shoua  *		  disclaimer in the documentation and/or other materials
228700e3e7SMoni Shoua  *		  provided with the distribution.
238700e3e7SMoni Shoua  *
248700e3e7SMoni Shoua  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258700e3e7SMoni Shoua  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268700e3e7SMoni Shoua  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278700e3e7SMoni Shoua  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288700e3e7SMoni Shoua  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298700e3e7SMoni Shoua  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308700e3e7SMoni Shoua  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318700e3e7SMoni Shoua  * SOFTWARE.
328700e3e7SMoni Shoua  */
338700e3e7SMoni Shoua 
348700e3e7SMoni Shoua #include "rxe.h"
358700e3e7SMoni Shoua #include "rxe_loc.h"
368700e3e7SMoni Shoua 
3790898850SDasaratharaman Chandramouli int rxe_av_chk_attr(struct rxe_dev *rxe, struct rdma_ah_attr *attr)
388700e3e7SMoni Shoua {
398700e3e7SMoni Shoua 	struct rxe_port *port;
408700e3e7SMoni Shoua 
41d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_port_num(attr) != 1) {
42d8966fcdSDasaratharaman Chandramouli 		pr_info("invalid port_num = %d\n", rdma_ah_get_port_num(attr));
438700e3e7SMoni Shoua 		return -EINVAL;
448700e3e7SMoni Shoua 	}
458700e3e7SMoni Shoua 
468700e3e7SMoni Shoua 	port = &rxe->port;
478700e3e7SMoni Shoua 
48d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_ah_flags(attr) & IB_AH_GRH) {
49d8966fcdSDasaratharaman Chandramouli 		u8 sgid_index = rdma_ah_read_grh(attr)->sgid_index;
50d8966fcdSDasaratharaman Chandramouli 
51d8966fcdSDasaratharaman Chandramouli 		if (sgid_index > port->attr.gid_tbl_len) {
52d8966fcdSDasaratharaman Chandramouli 			pr_info("invalid sgid index = %d\n", sgid_index);
538700e3e7SMoni Shoua 			return -EINVAL;
548700e3e7SMoni Shoua 		}
558700e3e7SMoni Shoua 	}
568700e3e7SMoni Shoua 
578700e3e7SMoni Shoua 	return 0;
588700e3e7SMoni Shoua }
598700e3e7SMoni Shoua 
608700e3e7SMoni Shoua int rxe_av_from_attr(struct rxe_dev *rxe, u8 port_num,
6190898850SDasaratharaman Chandramouli 		     struct rxe_av *av, struct rdma_ah_attr *attr)
628700e3e7SMoni Shoua {
638700e3e7SMoni Shoua 	memset(av, 0, sizeof(*av));
64d8966fcdSDasaratharaman Chandramouli 	memcpy(&av->grh, rdma_ah_read_grh(attr),
65d8966fcdSDasaratharaman Chandramouli 	       sizeof(*rdma_ah_read_grh(attr)));
668700e3e7SMoni Shoua 	av->port_num = port_num;
678700e3e7SMoni Shoua 	return 0;
688700e3e7SMoni Shoua }
698700e3e7SMoni Shoua 
708700e3e7SMoni Shoua int rxe_av_to_attr(struct rxe_dev *rxe, struct rxe_av *av,
7190898850SDasaratharaman Chandramouli 		   struct rdma_ah_attr *attr)
728700e3e7SMoni Shoua {
73d8966fcdSDasaratharaman Chandramouli 	memcpy(rdma_ah_retrieve_grh(attr), &av->grh, sizeof(av->grh));
74d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_ah_flags(attr, IB_AH_GRH);
75d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_port_num(attr, av->port_num);
768700e3e7SMoni Shoua 	return 0;
778700e3e7SMoni Shoua }
788700e3e7SMoni Shoua 
798700e3e7SMoni Shoua int rxe_av_fill_ip_info(struct rxe_dev *rxe,
808700e3e7SMoni Shoua 			struct rxe_av *av,
8190898850SDasaratharaman Chandramouli 			struct rdma_ah_attr *attr,
828700e3e7SMoni Shoua 			struct ib_gid_attr *sgid_attr,
838700e3e7SMoni Shoua 			union ib_gid *sgid)
848700e3e7SMoni Shoua {
858700e3e7SMoni Shoua 	rdma_gid2ip(&av->sgid_addr._sockaddr, sgid);
86d8966fcdSDasaratharaman Chandramouli 	rdma_gid2ip(&av->dgid_addr._sockaddr, &rdma_ah_read_grh(attr)->dgid);
878700e3e7SMoni Shoua 	av->network_type = ib_gid_to_network_type(sgid_attr->gid_type, sgid);
888700e3e7SMoni Shoua 
898700e3e7SMoni Shoua 	return 0;
908700e3e7SMoni Shoua }
918700e3e7SMoni Shoua 
928700e3e7SMoni Shoua struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt)
938700e3e7SMoni Shoua {
948700e3e7SMoni Shoua 	if (!pkt || !pkt->qp)
958700e3e7SMoni Shoua 		return NULL;
968700e3e7SMoni Shoua 
978700e3e7SMoni Shoua 	if (qp_type(pkt->qp) == IB_QPT_RC || qp_type(pkt->qp) == IB_QPT_UC)
988700e3e7SMoni Shoua 		return &pkt->qp->pri_av;
998700e3e7SMoni Shoua 
1008700e3e7SMoni Shoua 	return (pkt->wqe) ? &pkt->wqe->av : NULL;
1018700e3e7SMoni Shoua }
102