[infiniband] Record multicast GID attachment as part of group membership
[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 gid Multicast GID
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, union ib_gid *gid,
50 int join, union ib_mad *mad ) {
51 struct ib_mad_sa *sa = &mad->sa;
52
53 /* Construct multicast membership record request */
54 memset ( sa, 0, sizeof ( *sa ) );
55 sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
56 sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
57 sa->mad_hdr.method =
58 ( join ? IB_MGMT_METHOD_SET : IB_MGMT_METHOD_DELETE );
59 sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_MC_MEMBER_REC );
60 sa->sa_hdr.comp_mask[1] =
61 htonl ( IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
62 IB_SA_MCMEMBER_REC_JOIN_STATE );
63 sa->sa_data.mc_member_record.scope__join_state = 1;
64 memcpy ( &sa->sa_data.mc_member_record.mgid, gid,
65 sizeof ( sa->sa_data.mc_member_record.mgid ) );
66 memcpy ( &sa->sa_data.mc_member_record.port_gid, &ibdev->gid,
67 sizeof ( sa->sa_data.mc_member_record.port_gid ) );
68 }
69
70 /**
71 * Handle multicast membership record join response
72 *
73 * @v ibdev Infiniband device
74 * @v mi Management interface
75 * @v madx Management transaction
76 * @v rc Status code
77 * @v mad Received MAD (or NULL on error)
78 * @v av Source address vector (or NULL on error)
79 */
80 static void ib_mcast_complete ( struct ib_device *ibdev,
81 struct ib_mad_interface *mi __unused,
82 struct ib_mad_transaction *madx,
83 int rc, union ib_mad *mad,
84 struct ib_address_vector *av __unused ) {
85 struct ib_mc_membership *membership = ib_madx_get_ownerdata ( madx );
86 struct ib_queue_pair *qp = membership->qp;
87 union ib_gid *gid = &membership->gid;
88 struct ib_mc_member_record *mc_member_record =
89 &mad->sa.sa_data.mc_member_record;
90 int joined;
91 unsigned long qkey;
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 qkey = ntohl ( mc_member_record->qkey );
105 DBGC ( ibdev, "IBDEV %s QPN %#lx %s " IB_GID_FMT " qkey %lx\n",
106 ibdev->name, qp->qpn, ( joined ? "joined" : "left" ),
107 IB_GID_ARGS ( gid ), qkey );
108
109 /* Set queue key */
110 qp->qkey = qkey;
111 if ( ( rc = ib_modify_qp ( ibdev, qp ) ) != 0 ) {
112 DBGC ( ibdev, "IBDEV %s QPN %#lx could not modify qkey: %s\n",
113 ibdev->name, qp->qpn, strerror ( rc ) );
114 goto out;
115 }
116
117 out:
118 /* Destroy the completed transaction */
119 ib_destroy_madx ( ibdev, mi, madx );
120 membership->madx = NULL;
121
122 /* Hand off to upper completion handler */
123 membership->complete ( ibdev, qp, membership, rc, mad );
124 }
125
126 /** Multicast membership management transaction completion operations */
127 static struct ib_mad_transaction_operations ib_mcast_op = {
128 .complete = ib_mcast_complete,
129 };
130
131 /**
132 * Join multicast group
133 *
134 * @v ibdev Infiniband device
135 * @v qp Queue pair
136 * @v membership Multicast group membership
137 * @v gid Multicast GID to join
138 * @v joined Join completion handler
139 * @ret rc Return status code
140 */
141 int ib_mcast_join ( struct ib_device *ibdev, struct ib_queue_pair *qp,
142 struct ib_mc_membership *membership, union ib_gid *gid,
143 void ( * complete ) ( struct ib_device *ibdev,
144 struct ib_queue_pair *qp,
145 struct ib_mc_membership *membership,
146 int rc, union ib_mad *mad ) ) {
147 union ib_mad mad;
148 int rc;
149
150 DBGC ( ibdev, "IBDEV %s QPN %#lx joining " IB_GID_FMT "\n",
151 ibdev->name, qp->qpn, IB_GID_ARGS ( gid ) );
152
153 /* Sanity checks */
154 assert ( qp != NULL );
155 assert ( ! membership->attached );
156
157 /* Initialise structure */
158 membership->qp = qp;
159 memcpy ( &membership->gid, gid, sizeof ( membership->gid ) );
160 membership->complete = complete;
161
162 /* Attach queue pair to multicast GID */
163 if ( ( rc = ib_mcast_attach ( ibdev, qp, gid ) ) != 0 ) {
164 DBGC ( ibdev, "IBDEV %s QPN %#lx could not attach: %s\n",
165 ibdev->name, qp->qpn, strerror ( rc ) );
166 goto err_mcast_attach;
167 }
168 membership->attached = 1;
169
170 /* Initiate multicast membership join */
171 ib_mcast_mad ( ibdev, gid, 1, &mad );
172 membership->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
173 &ib_mcast_op );
174 if ( ! membership->madx ) {
175 DBGC ( ibdev, "IBDEV %s QPN %#lx could not create join "
176 "transaction\n", ibdev->name, qp->qpn );
177 rc = -ENOMEM;
178 goto err_create_madx;
179 }
180 ib_madx_set_ownerdata ( membership->madx, membership );
181
182 return 0;
183
184 ib_destroy_madx ( ibdev, ibdev->gsi, membership->madx );
185 err_create_madx:
186 ib_mcast_detach ( ibdev, qp, gid );
187 membership->attached = 0;
188 err_mcast_attach:
189 return rc;
190 }
191
192 /**
193 * Leave multicast group
194 *
195 * @v ibdev Infiniband device
196 * @v qp Queue pair
197 * @v membership Multicast group membership
198 */
199 void ib_mcast_leave ( struct ib_device *ibdev, struct ib_queue_pair *qp,
200 struct ib_mc_membership *membership ) {
201 union ib_gid *gid = &membership->gid;
202 union ib_mad mad;
203 int rc;
204
205 /* Do nothing if we are already detached from the multicast GID */
206 if ( ! membership->attached )
207 return;
208
209 DBGC ( ibdev, "IBDEV %s QPN %#lx leaving " IB_GID_FMT "\n",
210 ibdev->name, qp->qpn, IB_GID_ARGS ( gid ) );
211
212 /* Sanity check */
213 assert ( qp != NULL );
214
215 /* Detach from multicast GID */
216 ib_mcast_detach ( ibdev, qp, &membership->gid );
217 membership->attached = 0;
218
219 /* Cancel multicast membership join, if applicable */
220 if ( membership->madx ) {
221 ib_destroy_madx ( ibdev, ibdev->gsi, membership->madx );
222 membership->madx = NULL;
223 }
224
225 /* Send a single group leave MAD */
226 ib_mcast_mad ( ibdev, &membership->gid, 0, &mad );
227 if ( ( rc = ib_mi_send ( ibdev, ibdev->gsi, &mad, NULL ) ) != 0 ) {
228 DBGC ( ibdev, "IBDEV %s QPN %#lx could not send leave request: "
229 "%s\n", ibdev->name, qp->qpn, strerror ( rc ) );
230 }
231 }