1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
5 */
6
7 #include "rxe.h"
8 #include "rxe_loc.h"
9
rxe_init_av(struct rdma_ah_attr * attr,struct rxe_av * av)10 void rxe_init_av(struct rdma_ah_attr *attr, struct rxe_av *av)
11 {
12 rxe_av_from_attr(rdma_ah_get_port_num(attr), av, attr);
13 rxe_av_fill_ip_info(av, attr);
14 memcpy(av->dmac, attr->roce.dmac, ETH_ALEN);
15 }
16
rxe_av_chk_attr(struct rxe_dev * rxe,struct rdma_ah_attr * attr)17 int rxe_av_chk_attr(struct rxe_dev *rxe, struct rdma_ah_attr *attr)
18 {
19 const struct ib_global_route *grh = rdma_ah_read_grh(attr);
20 struct rxe_port *port;
21 int type;
22
23 port = &rxe->port;
24
25 if (rdma_ah_get_ah_flags(attr) & IB_AH_GRH) {
26 if (grh->sgid_index > port->attr.gid_tbl_len) {
27 pr_warn("invalid sgid index = %d\n",
28 grh->sgid_index);
29 return -EINVAL;
30 }
31
32 type = rdma_gid_attr_network_type(grh->sgid_attr);
33 if (type < RDMA_NETWORK_IPV4 ||
34 type > RDMA_NETWORK_IPV6) {
35 pr_warn("invalid network type for rdma_rxe = %d\n",
36 type);
37 return -EINVAL;
38 }
39 }
40
41 return 0;
42 }
43
rxe_av_from_attr(u8 port_num,struct rxe_av * av,struct rdma_ah_attr * attr)44 void rxe_av_from_attr(u8 port_num, struct rxe_av *av,
45 struct rdma_ah_attr *attr)
46 {
47 const struct ib_global_route *grh = rdma_ah_read_grh(attr);
48
49 memset(av, 0, sizeof(*av));
50 memcpy(av->grh.dgid.raw, grh->dgid.raw, sizeof(grh->dgid.raw));
51 av->grh.flow_label = grh->flow_label;
52 av->grh.sgid_index = grh->sgid_index;
53 av->grh.hop_limit = grh->hop_limit;
54 av->grh.traffic_class = grh->traffic_class;
55 av->port_num = port_num;
56 }
57
rxe_av_to_attr(struct rxe_av * av,struct rdma_ah_attr * attr)58 void rxe_av_to_attr(struct rxe_av *av, struct rdma_ah_attr *attr)
59 {
60 struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
61
62 attr->type = RDMA_AH_ATTR_TYPE_ROCE;
63
64 memcpy(grh->dgid.raw, av->grh.dgid.raw, sizeof(av->grh.dgid.raw));
65 grh->flow_label = av->grh.flow_label;
66 grh->sgid_index = av->grh.sgid_index;
67 grh->hop_limit = av->grh.hop_limit;
68 grh->traffic_class = av->grh.traffic_class;
69
70 rdma_ah_set_ah_flags(attr, IB_AH_GRH);
71 rdma_ah_set_port_num(attr, av->port_num);
72 }
73
rxe_av_fill_ip_info(struct rxe_av * av,struct rdma_ah_attr * attr)74 void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr)
75 {
76 const struct ib_gid_attr *sgid_attr = attr->grh.sgid_attr;
77 int ibtype;
78 int type;
79
80 rdma_gid2ip((struct sockaddr *)&av->sgid_addr, &sgid_attr->gid);
81 rdma_gid2ip((struct sockaddr *)&av->dgid_addr,
82 &rdma_ah_read_grh(attr)->dgid);
83
84 ibtype = rdma_gid_attr_network_type(sgid_attr);
85
86 switch (ibtype) {
87 case RDMA_NETWORK_IPV4:
88 type = RXE_NETWORK_TYPE_IPV4;
89 break;
90 case RDMA_NETWORK_IPV6:
91 type = RXE_NETWORK_TYPE_IPV6;
92 break;
93 default:
94 /* not reached - checked in rxe_av_chk_attr */
95 type = 0;
96 break;
97 }
98
99 av->network_type = type;
100 }
101
rxe_get_av(struct rxe_pkt_info * pkt,struct rxe_ah ** ahp)102 struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt, struct rxe_ah **ahp)
103 {
104 struct rxe_ah *ah;
105 u32 ah_num;
106
107 if (ahp)
108 *ahp = NULL;
109
110 if (!pkt || !pkt->qp)
111 return NULL;
112
113 if (qp_type(pkt->qp) == IB_QPT_RC || qp_type(pkt->qp) == IB_QPT_UC)
114 return &pkt->qp->pri_av;
115
116 if (!pkt->wqe)
117 return NULL;
118
119 ah_num = pkt->wqe->wr.wr.ud.ah_num;
120 if (ah_num) {
121 /* only new user provider or kernel client */
122 ah = rxe_pool_get_index(&pkt->rxe->ah_pool, ah_num);
123 if (!ah) {
124 pr_warn("Unable to find AH matching ah_num\n");
125 return NULL;
126 }
127
128 if (rxe_ah_pd(ah) != pkt->qp->pd) {
129 pr_warn("PDs don't match for AH and QP\n");
130 rxe_put(ah);
131 return NULL;
132 }
133
134 if (ahp)
135 *ahp = ah;
136 else
137 rxe_put(ah);
138
139 return &ah->av;
140 }
141
142 /* only old user provider for UD sends*/
143 return &pkt->wqe->wr.wr.ud.av;
144 }
145