[infiniband] Parse MLID, rate, and SL from multicast membership record
[ipxe.git] / src / net / infiniband / ib_mcast.c
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <byteswap.h>
29 #include <errno.h>
30 #include <ipxe/list.h>
31 #include <ipxe/infiniband.h>
32 #include <ipxe/ib_mi.h>
33 #include <ipxe/ib_mcast.h>
34
35 /** @file
36 *
37 * Infiniband multicast groups
38 *
39 */
40
41 /**
42 * Generate multicast membership MAD
43 *
44 * @v ibdev Infiniband device
45 * @v av Address vector
46 * @v join Join (rather than leave) group
47 * @v mad MAD to fill in
48 */
49 static void ib_mcast_mad ( struct ib_device *ibdev,
50 struct ib_address_vector *av,
51 int join, union ib_mad *mad ) {
52 struct ib_mad_sa *sa = &mad->sa;
53
54 /* Construct multicast membership record request */
55 memset ( sa, 0, sizeof ( *sa ) );
56 sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
57 sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
58 sa->mad_hdr.method =
59 ( join ? IB_MGMT_METHOD_SET : IB_MGMT_METHOD_DELETE );
60 sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_MC_MEMBER_REC );
61 sa->sa_hdr.comp_mask[1] =
62 htonl ( IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
63 IB_SA_MCMEMBER_REC_JOIN_STATE );
64 sa->sa_data.mc_member_record.scope__join_state = 1;
65 memcpy ( &sa->sa_data.mc_member_record.mgid, &av->gid,
66 sizeof ( sa->sa_data.mc_member_record.mgid ) );
67 memcpy ( &sa->sa_data.mc_member_record.port_gid, &ibdev->gid,
68 sizeof ( sa->sa_data.mc_member_record.port_gid ) );
69 }
70
71 /**
72 * Handle multicast membership record join response
73 *
74 * @v ibdev Infiniband device
75 * @v mi Management interface
76 * @v madx Management transaction
77 * @v rc Status code
78 * @v mad Received MAD (or NULL on error)
79 * @v src Source address vector (or NULL on error)
80 */
81 static void ib_mcast_complete ( struct ib_device *ibdev,
82 struct ib_mad_interface *mi __unused,
83 struct ib_mad_transaction *madx,
84 int rc, union ib_mad *mad,
85 struct ib_address_vector *src __unused ) {
86 struct ib_mc_membership *membership = ib_madx_get_ownerdata ( madx );
87 struct ib_queue_pair *qp = membership->qp;
88 struct ib_address_vector *av = membership->av;
89 struct ib_mc_member_record *mc_member_record =
90 &mad->sa.sa_data.mc_member_record;
91 int joined;
92
93 /* Report failures */
94 if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
95 rc = -ENOTCONN;
96 if ( rc != 0 ) {
97 DBGC ( ibdev, "IBDEV %s QPN %#lx join failed: %s\n",
98 ibdev->name, qp->qpn, strerror ( rc ) );
99 goto out;
100 }
101
102 /* Extract values from MAD */
103 joined = ( mad->hdr.method == IB_MGMT_METHOD_GET_RESP );
104 av->qkey = ntohl ( mc_member_record->qkey );
105 av->lid = ntohs ( mc_member_record->mlid );
106 av->rate = ( mc_member_record->rate_selector__rate & 0x3f );
107 av->sl = ( ( ntohl ( mc_member_record->sl__flow_label__hop_limit )
108 >> 28 ) & 0x0f );
109 DBGC ( ibdev, "IBDEV %s QPN %#lx %s " IB_GID_FMT " qkey %#lx\n",
110 ibdev->name, qp->qpn, ( joined ? "joined" : "left" ),
111 IB_GID_ARGS ( &av->gid ), av->qkey );
112
113 /* Set queue key */
114 qp->qkey = av->qkey;
115 if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
116 DBGC ( ibdev, "IBDEV %s QPN %#lx could not modify qkey: %s\n",
117 ibdev->name, qp->qpn, strerror ( rc ) );
118 goto out;
119 }
120
121 out:
122 /* Destroy the completed transaction */
123 ib_destroy_madx ( ibdev, mi, madx );
124 membership->madx = NULL;
125
126 /* Hand off to upper completion handler */
127 membership->complete ( membership, rc );
128 }
129
130 /** Multicast membership management transaction completion operations */
131 static struct ib_mad_transaction_operations ib_mcast_op = {
132 .complete = ib_mcast_complete,
133 };
134
135 /**
136 * Join multicast group
137 *
138 * @v ibdev Infiniband device
139 * @v qp Queue pair
140 * @v membership Multicast group membership
141 * @v av Address vector to fill in
142 * @v joined Join completion handler
143 * @ret rc Return status code
144 */
145 int ib_mcast_join ( struct ib_device *ibdev, struct ib_queue_pair *qp,
146 struct ib_mc_membership *membership,
147 struct ib_address_vector *av,
148 void ( * complete ) ( struct ib_mc_membership *membership,
149 int rc ) ) {
150 union ib_mad mad;
151 int rc;
152
153 DBGC ( ibdev, "IBDEV %s QPN %#lx joining " IB_GID_FMT "\n",
154 ibdev->name, qp->qpn, IB_GID_ARGS ( &av->gid ) );
155
156 /* Sanity checks */
157 assert ( qp != NULL );
158 assert ( ! membership->attached );
159
160 /* Initialise structure */
161 membership->qp = qp;
162 membership->av = av;
163 membership->complete = complete;
164
165 /* Attach queue pair to multicast GID */
166 if ( ( rc = ib_mcast_attach ( ibdev, qp, &av->gid ) ) != 0 ) {
167 DBGC ( ibdev, "IBDEV %s QPN %#lx could not attach: %s\n",
168 ibdev->name, qp->qpn, strerror ( rc ) );
169 goto err_mcast_attach;
170 }
171 membership->attached = 1;
172
173 /* Initiate multicast membership join */
174 ib_mcast_mad ( ibdev, av, 1, &mad );
175 membership->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
176 &ib_mcast_op );
177 if ( ! membership->madx ) {
178 DBGC ( ibdev, "IBDEV %s QPN %#lx could not create join "
179 "transaction\n", ibdev->name, qp->qpn );
180 rc = -ENOMEM;
181 goto err_create_madx;
182 }
183 ib_madx_set_ownerdata ( membership->madx, membership );
184
185 return 0;
186
187 ib_destroy_madx ( ibdev, ibdev->gsi, membership->madx );
188 err_create_madx:
189 ib_mcast_detach ( ibdev, qp, &av->gid );
190 membership->attached = 0;
191 err_mcast_attach:
192 return rc;
193 }
194
195 /**
196 * Leave multicast group
197 *
198 * @v ibdev Infiniband device
199 * @v qp Queue pair
200 * @v membership Multicast group membership
201 */
202 void ib_mcast_leave ( struct ib_device *ibdev, struct ib_queue_pair *qp,
203 struct ib_mc_membership *membership ) {
204 struct ib_address_vector *av = membership->av;
205 union ib_mad mad;
206 int rc;
207
208 /* Do nothing if we are already detached from the multicast GID */
209 if ( ! membership->attached )
210 return;
211
212 DBGC ( ibdev, "IBDEV %s QPN %#lx leaving " IB_GID_FMT "\n",
213 ibdev->name, qp->qpn, IB_GID_ARGS ( &av->gid ) );
214
215 /* Sanity check */
216 assert ( qp != NULL );
217
218 /* Detach from multicast GID */
219 ib_mcast_detach ( ibdev, qp, &av->gid );
220 membership->attached = 0;
221
222 /* Cancel multicast membership join, if applicable */
223 if ( membership->madx ) {
224 ib_destroy_madx ( ibdev, ibdev->gsi, membership->madx );
225 membership->madx = NULL;
226 }
227
228 /* Send a single group leave MAD */
229 ib_mcast_mad ( ibdev, av, 0, &mad );
230 if ( ( rc = ib_mi_send ( ibdev, ibdev->gsi, &mad, NULL ) ) != 0 ) {
231 DBGC ( ibdev, "IBDEV %s QPN %#lx could not send leave request: "
232 "%s\n", ibdev->name, qp->qpn, strerror ( rc ) );
233 }
234 }