2 * Copyright (C) 2009 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
29 #include <ipxe/infiniband.h>
30 #include <ipxe/iobuf.h>
31 #include <ipxe/ib_mi.h>
36 * Infiniband management interfaces
40 /** Management interface number of send WQEs
42 * This is a policy decision.
44 #define IB_MI_NUM_SEND_WQES 4
46 /** Management interface number of receive WQEs
48 * This is a policy decision.
50 #define IB_MI_NUM_RECV_WQES 2
52 /** Management interface number of completion queue entries
54 * This is a policy decision
56 #define IB_MI_NUM_CQES 8
58 /** TID magic signature */
59 #define IB_MI_TID_MAGIC ( ( 'i' << 24 ) | ( 'P' << 16 ) | ( 'X' << 8 ) | 'E' )
61 /** TID to use for next MAD */
62 static unsigned int next_tid;
67 * @v ibdev Infiniband device
68 * @v mi Management interface
70 * @v av Source address vector
71 * @ret rc Return status code
73 static int ib_mi_handle ( struct ib_device *ibdev,
74 struct ib_mad_interface *mi,
76 struct ib_address_vector *av ) {
77 struct ib_mad_hdr *hdr = &mad->hdr;
78 struct ib_mad_transaction *madx;
79 struct ib_mad_agent *agent;
81 /* Look for a matching transaction by TID */
82 list_for_each_entry ( madx, &mi->madx, list ) {
83 if ( memcmp ( &hdr->tid, &madx->mad.hdr.tid,
84 sizeof ( hdr->tid ) ) != 0 )
86 /* Found a matching transaction */
87 madx->op->complete ( ibdev, mi, madx, 0, mad, av );
91 /* If there is no matching transaction, look for a listening agent */
92 for_each_table_entry ( agent, IB_MAD_AGENTS ) {
93 if ( ( ( agent->mgmt_class & IB_MGMT_CLASS_MASK ) !=
94 ( hdr->mgmt_class & IB_MGMT_CLASS_MASK ) ) ||
95 ( agent->class_version != hdr->class_version ) ||
96 ( agent->attr_id != hdr->attr_id ) )
98 /* Found a matching agent */
99 agent->handle ( ibdev, mi, mad, av );
103 /* Otherwise, ignore it */
104 DBGC ( mi, "MI %p RX TID %08x%08x ignored\n",
105 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
110 * Complete receive via management interface
113 * @v ibdev Infiniband device
115 * @v dest Destination address vector
116 * @v source Source address vector
117 * @v iobuf I/O buffer
118 * @v rc Completion status code
120 static void ib_mi_complete_recv ( struct ib_device *ibdev,
121 struct ib_queue_pair *qp,
122 struct ib_address_vector *dest __unused,
123 struct ib_address_vector *source,
124 struct io_buffer *iobuf, int rc ) {
125 struct ib_mad_interface *mi = ib_qp_get_ownerdata ( qp );
127 struct ib_mad_hdr *hdr;
131 DBGC ( mi, "MI %p RX error: %s\n", mi, strerror ( rc ) );
136 if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
137 DBGC ( mi, "MI %p RX bad size (%zd bytes)\n",
138 mi, iob_len ( iobuf ) );
139 DBGC_HDA ( mi, 0, iobuf->data, iob_len ( iobuf ) );
144 if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
145 DBGC ( mi, "MI %p RX unsupported base version %x\n",
146 mi, hdr->base_version );
147 DBGC_HDA ( mi, 0, mad, sizeof ( *mad ) );
150 DBGC ( mi, "MI %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
151 "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
152 hdr->mgmt_class, hdr->class_version, hdr->method,
153 ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
154 DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
157 if ( ( rc = ib_mi_handle ( ibdev, mi, mad, source ) ) != 0 )
164 /** Management interface completion operations */
165 static struct ib_completion_queue_operations ib_mi_completion_ops = {
166 .complete_recv = ib_mi_complete_recv,
169 /** Management interface queue pair operations */
170 static struct ib_queue_pair_operations ib_mi_queue_pair_ops = {
171 .alloc_iob = alloc_iob,
177 * @v ibdev Infiniband device
178 * @v mi Management interface
180 * @v av Destination address vector
181 * @ret rc Return status code
183 int ib_mi_send ( struct ib_device *ibdev, struct ib_mad_interface *mi,
184 union ib_mad *mad, struct ib_address_vector *av ) {
185 struct ib_mad_hdr *hdr = &mad->hdr;
186 struct io_buffer *iobuf;
189 /* Set common fields */
190 hdr->base_version = IB_MGMT_BASE_VERSION;
191 if ( ( hdr->tid[0] == 0 ) && ( hdr->tid[1] == 0 ) ) {
192 hdr->tid[0] = htonl ( IB_MI_TID_MAGIC );
193 hdr->tid[1] = htonl ( ++next_tid );
195 DBGC ( mi, "MI %p TX TID %08x%08x (%02x,%02x,%02x,%04x) status "
196 "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
197 hdr->mgmt_class, hdr->class_version, hdr->method,
198 ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
199 DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
201 /* Construct directed route portion of response, if necessary */
202 if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
203 struct ib_mad_smp *smp = &mad->smp;
204 unsigned int hop_pointer;
205 unsigned int hop_count;
207 smp->mad_hdr.status |= htons ( IB_SMP_STATUS_D_INBOUND );
208 hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
209 hop_count = smp->mad_hdr.class_specific.smp.hop_count;
210 assert ( hop_count == hop_pointer );
211 if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
212 sizeof ( smp->return_path.hops[0] ) ) ) {
213 smp->return_path.hops[hop_pointer] = ibdev->port;
215 DBGC ( mi, "MI %p TX TID %08x%08x invalid hop pointer "
216 "%d\n", mi, ntohl ( hdr->tid[0] ),
217 ntohl ( hdr->tid[1] ), hop_pointer );
222 /* Construct I/O buffer */
223 iobuf = alloc_iob ( sizeof ( *mad ) );
225 DBGC ( mi, "MI %p could not allocate buffer for TID "
227 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
230 memcpy ( iob_put ( iobuf, sizeof ( *mad ) ), mad, sizeof ( *mad ) );
232 /* Send I/O buffer */
233 if ( ( rc = ib_post_send ( ibdev, mi->qp, av, iobuf ) ) != 0 ) {
234 DBGC ( mi, "MI %p TX TID %08x%08x failed: %s\n",
235 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
245 * Handle management transaction timer expiry
247 * @v timer Retry timer
248 * @v expired Failure indicator
250 static void ib_mi_timer_expired ( struct retry_timer *timer, int expired ) {
251 struct ib_mad_transaction *madx =
252 container_of ( timer, struct ib_mad_transaction, timer );
253 struct ib_mad_interface *mi = madx->mi;
254 struct ib_device *ibdev = mi->ibdev;
255 struct ib_mad_hdr *hdr = &madx->mad.hdr;
257 /* Abandon transaction if we have tried too many times */
259 DBGC ( mi, "MI %p abandoning TID %08x%08x\n",
260 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
261 madx->op->complete ( ibdev, mi, madx, -ETIMEDOUT, NULL, NULL );
265 /* Restart retransmission timer */
266 start_timer ( timer );
269 ib_mi_send ( ibdev, mi, &madx->mad, &madx->av );
273 * Create management transaction
275 * @v ibdev Infiniband device
276 * @v mi Management interface
278 * @v av Destination address, or NULL to use SM's GSI
279 * @v op Management transaction operations
280 * @ret madx Management transaction, or NULL
282 struct ib_mad_transaction *
283 ib_create_madx ( struct ib_device *ibdev, struct ib_mad_interface *mi,
284 union ib_mad *mad, struct ib_address_vector *av,
285 struct ib_mad_transaction_operations *op ) {
286 struct ib_mad_transaction *madx;
288 /* Allocate and initialise structure */
289 madx = zalloc ( sizeof ( *madx ) );
292 timer_init ( &madx->timer, ib_mi_timer_expired, NULL );
296 /* Determine address vector */
298 memcpy ( &madx->av, av, sizeof ( madx->av ) );
300 madx->av.lid = ibdev->sm_lid;
301 madx->av.sl = ibdev->sm_sl;
302 madx->av.qpn = IB_QPN_GSI;
303 madx->av.qkey = IB_QKEY_GSI;
307 memcpy ( &madx->mad, mad, sizeof ( madx->mad ) );
309 /* Add to list and start timer to send initial MAD */
310 list_add ( &madx->list, &mi->madx );
311 start_timer_nodelay ( &madx->timer );
317 * Destroy management transaction
319 * @v ibdev Infiniband device
320 * @v mi Management interface
321 * @v madx Management transaction
323 void ib_destroy_madx ( struct ib_device *ibdev __unused,
324 struct ib_mad_interface *mi __unused,
325 struct ib_mad_transaction *madx ) {
327 /* Stop timer and remove from list */
328 stop_timer ( &madx->timer );
329 list_del ( &madx->list );
331 /* Free transaction */
336 * Create management interface
338 * @v ibdev Infiniband device
339 * @v type Queue pair type
340 * @ret mi Management agent, or NULL
342 struct ib_mad_interface * ib_create_mi ( struct ib_device *ibdev,
343 enum ib_queue_pair_type type ) {
344 struct ib_mad_interface *mi;
347 /* Allocate and initialise fields */
348 mi = zalloc ( sizeof ( *mi ) );
352 INIT_LIST_HEAD ( &mi->madx );
354 /* Create completion queue */
355 mi->cq = ib_create_cq ( ibdev, IB_MI_NUM_CQES, &ib_mi_completion_ops );
357 DBGC ( mi, "MI %p could not allocate completion queue\n", mi );
361 /* Create queue pair */
362 mi->qp = ib_create_qp ( ibdev, type, IB_MI_NUM_SEND_WQES, mi->cq,
363 IB_MI_NUM_RECV_WQES, mi->cq,
364 &ib_mi_queue_pair_ops );
366 DBGC ( mi, "MI %p could not allocate queue pair\n", mi );
369 ib_qp_set_ownerdata ( mi->qp, mi );
370 DBGC ( mi, "MI %p (%s) running on QPN %#lx\n",
371 mi, ( ( type == IB_QPT_SMI ) ? "SMI" : "GSI" ), mi->qp->qpn );
374 mi->qp->qkey = ( ( type == IB_QPT_SMI ) ? IB_QKEY_SMI : IB_QKEY_GSI );
375 if ( ( rc = ib_modify_qp ( ibdev, mi->qp ) ) != 0 ) {
376 DBGC ( mi, "MI %p could not set queue key: %s\n",
377 mi, strerror ( rc ) );
381 /* Fill receive ring */
382 ib_refill_recv ( ibdev, mi->qp );
386 ib_destroy_qp ( ibdev, mi->qp );
388 ib_destroy_cq ( ibdev, mi->cq );
396 * Destroy management interface
398 * @v mi Management interface
400 void ib_destroy_mi ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
401 struct ib_mad_transaction *madx;
402 struct ib_mad_transaction *tmp;
404 /* Flush any outstanding requests */
405 list_for_each_entry_safe ( madx, tmp, &mi->madx, list ) {
406 DBGC ( mi, "MI %p destroyed while TID %08x%08x in progress\n",
407 mi, ntohl ( madx->mad.hdr.tid[0] ),
408 ntohl ( madx->mad.hdr.tid[1] ) );
409 madx->op->complete ( ibdev, mi, madx, -ECANCELED, NULL, NULL );
412 ib_destroy_qp ( ibdev, mi->qp );
413 ib_destroy_cq ( ibdev, mi->cq );