xref: /openbmc/linux/drivers/infiniband/sw/rxe/rxe_av.c (revision 45062f44)
163fa15dbSBob Pearson // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
28700e3e7SMoni Shoua /*
38700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
48700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
58700e3e7SMoni Shoua  */
68700e3e7SMoni Shoua 
78700e3e7SMoni Shoua #include "rxe.h"
88700e3e7SMoni Shoua #include "rxe_loc.h"
98700e3e7SMoni Shoua 
10fa407188SKamal Heib void rxe_init_av(struct rdma_ah_attr *attr, struct rxe_av *av)
11fa407188SKamal Heib {
12fa407188SKamal Heib 	rxe_av_from_attr(rdma_ah_get_port_num(attr), av, attr);
13fa407188SKamal Heib 	rxe_av_fill_ip_info(av, attr);
14668aa15bSKamal Heib 	memcpy(av->dmac, attr->roce.dmac, ETH_ALEN);
15fa407188SKamal Heib }
16fa407188SKamal Heib 
1790898850SDasaratharaman Chandramouli int rxe_av_chk_attr(struct rxe_dev *rxe, struct rdma_ah_attr *attr)
188700e3e7SMoni Shoua {
19edebc840SBob Pearson 	const struct ib_global_route *grh = rdma_ah_read_grh(attr);
208700e3e7SMoni Shoua 	struct rxe_port *port;
21edebc840SBob Pearson 	int type;
228700e3e7SMoni Shoua 
238700e3e7SMoni Shoua 	port = &rxe->port;
248700e3e7SMoni Shoua 
25d8966fcdSDasaratharaman Chandramouli 	if (rdma_ah_get_ah_flags(attr) & IB_AH_GRH) {
26edebc840SBob Pearson 		if (grh->sgid_index > port->attr.gid_tbl_len) {
27edebc840SBob Pearson 			pr_warn("invalid sgid index = %d\n",
28edebc840SBob Pearson 					grh->sgid_index);
29edebc840SBob Pearson 			return -EINVAL;
30edebc840SBob Pearson 		}
31d8966fcdSDasaratharaman Chandramouli 
32edebc840SBob Pearson 		type = rdma_gid_attr_network_type(grh->sgid_attr);
33edebc840SBob Pearson 		if (type < RDMA_NETWORK_IPV4 ||
34edebc840SBob Pearson 		    type > RDMA_NETWORK_IPV6) {
35edebc840SBob Pearson 			pr_warn("invalid network type for rdma_rxe = %d\n",
36edebc840SBob Pearson 					type);
378700e3e7SMoni Shoua 			return -EINVAL;
388700e3e7SMoni Shoua 		}
398700e3e7SMoni Shoua 	}
408700e3e7SMoni Shoua 
418700e3e7SMoni Shoua 	return 0;
428700e3e7SMoni Shoua }
438700e3e7SMoni Shoua 
44ca3d9feeSZhu Yanjun void rxe_av_from_attr(u8 port_num, struct rxe_av *av,
451a241db1SZhu Yanjun 		     struct rdma_ah_attr *attr)
468700e3e7SMoni Shoua {
477f3ee8e0SJason Gunthorpe 	const struct ib_global_route *grh = rdma_ah_read_grh(attr);
487f3ee8e0SJason Gunthorpe 
498700e3e7SMoni Shoua 	memset(av, 0, sizeof(*av));
507f3ee8e0SJason Gunthorpe 	memcpy(av->grh.dgid.raw, grh->dgid.raw, sizeof(grh->dgid.raw));
517f3ee8e0SJason Gunthorpe 	av->grh.flow_label = grh->flow_label;
527f3ee8e0SJason Gunthorpe 	av->grh.sgid_index = grh->sgid_index;
537f3ee8e0SJason Gunthorpe 	av->grh.hop_limit = grh->hop_limit;
547f3ee8e0SJason Gunthorpe 	av->grh.traffic_class = grh->traffic_class;
558700e3e7SMoni Shoua 	av->port_num = port_num;
568700e3e7SMoni Shoua }
578700e3e7SMoni Shoua 
58a402dc44SZhu Yanjun void rxe_av_to_attr(struct rxe_av *av, struct rdma_ah_attr *attr)
598700e3e7SMoni Shoua {
607f3ee8e0SJason Gunthorpe 	struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
617f3ee8e0SJason Gunthorpe 
6244c58487SDasaratharaman Chandramouli 	attr->type = RDMA_AH_ATTR_TYPE_ROCE;
637f3ee8e0SJason Gunthorpe 
647f3ee8e0SJason Gunthorpe 	memcpy(grh->dgid.raw, av->grh.dgid.raw, sizeof(av->grh.dgid.raw));
657f3ee8e0SJason Gunthorpe 	grh->flow_label = av->grh.flow_label;
667f3ee8e0SJason Gunthorpe 	grh->sgid_index = av->grh.sgid_index;
677f3ee8e0SJason Gunthorpe 	grh->hop_limit = av->grh.hop_limit;
687f3ee8e0SJason Gunthorpe 	grh->traffic_class = av->grh.traffic_class;
697f3ee8e0SJason Gunthorpe 
70d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_ah_flags(attr, IB_AH_GRH);
71d8966fcdSDasaratharaman Chandramouli 	rdma_ah_set_port_num(attr, av->port_num);
728700e3e7SMoni Shoua }
738700e3e7SMoni Shoua 
7447ec3866SParav Pandit void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr)
758700e3e7SMoni Shoua {
7647ec3866SParav Pandit 	const struct ib_gid_attr *sgid_attr = attr->grh.sgid_attr;
77edebc840SBob Pearson 	int ibtype;
78edebc840SBob Pearson 	int type;
7947ec3866SParav Pandit 
8047ec3866SParav Pandit 	rdma_gid2ip((struct sockaddr *)&av->sgid_addr, &sgid_attr->gid);
817061f28dSJason Gunthorpe 	rdma_gid2ip((struct sockaddr *)&av->dgid_addr,
827061f28dSJason Gunthorpe 		    &rdma_ah_read_grh(attr)->dgid);
83edebc840SBob Pearson 
84edebc840SBob Pearson 	ibtype = rdma_gid_attr_network_type(sgid_attr);
85edebc840SBob Pearson 
86edebc840SBob Pearson 	switch (ibtype) {
87edebc840SBob Pearson 	case RDMA_NETWORK_IPV4:
88edebc840SBob Pearson 		type = RXE_NETWORK_TYPE_IPV4;
89edebc840SBob Pearson 		break;
90edebc840SBob Pearson 	case RDMA_NETWORK_IPV6:
91*45062f44SBob Pearson 		type = RXE_NETWORK_TYPE_IPV6;
92edebc840SBob Pearson 		break;
93edebc840SBob Pearson 	default:
94edebc840SBob Pearson 		/* not reached - checked in rxe_av_chk_attr */
95edebc840SBob Pearson 		type = 0;
96edebc840SBob Pearson 		break;
97edebc840SBob Pearson 	}
98edebc840SBob Pearson 
99edebc840SBob Pearson 	av->network_type = type;
1008700e3e7SMoni Shoua }
1018700e3e7SMoni Shoua 
1028700e3e7SMoni Shoua struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt)
1038700e3e7SMoni Shoua {
1048700e3e7SMoni Shoua 	if (!pkt || !pkt->qp)
1058700e3e7SMoni Shoua 		return NULL;
1068700e3e7SMoni Shoua 
1078700e3e7SMoni Shoua 	if (qp_type(pkt->qp) == IB_QPT_RC || qp_type(pkt->qp) == IB_QPT_UC)
1088700e3e7SMoni Shoua 		return &pkt->qp->pri_av;
1098700e3e7SMoni Shoua 
1108700e3e7SMoni Shoua 	return (pkt->wqe) ? &pkt->wqe->av : NULL;
1118700e3e7SMoni Shoua }
112