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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 #include <ipxe/infiniband.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/ib_mi.h>
40 * Infiniband management interfaces
44 /** Management interface number of send WQEs
46 * This is a policy decision.
48 #define IB_MI_NUM_SEND_WQES 4
50 /** Management interface number of receive WQEs
52 * This is a policy decision.
54 #define IB_MI_NUM_RECV_WQES 2
56 /** Management interface number of completion queue entries
58 * This is a policy decision
60 #define IB_MI_NUM_CQES 8
62 /** TID magic signature */
63 #define IB_MI_TID_MAGIC ( ( 'i' << 24 ) | ( 'P' << 16 ) | ( 'X' << 8 ) | 'E' )
65 /** TID to use for next MAD */
66 static unsigned int next_tid;
71 * @v ibdev Infiniband device
72 * @v mi Management interface
74 * @v av Source address vector
75 * @ret rc Return status code
77 static int ib_mi_handle ( struct ib_device *ibdev,
78 struct ib_mad_interface *mi,
80 struct ib_address_vector *av ) {
81 struct ib_mad_hdr *hdr = &mad->hdr;
82 struct ib_mad_transaction *madx;
83 struct ib_mad_agent *agent;
85 /* Look for a matching transaction by TID */
86 list_for_each_entry ( madx, &mi->madx, list ) {
87 if ( memcmp ( &hdr->tid, &madx->mad.hdr.tid,
88 sizeof ( hdr->tid ) ) != 0 )
90 /* Found a matching transaction */
91 madx->op->complete ( ibdev, mi, madx, 0, mad, av );
95 /* If there is no matching transaction, look for a listening agent */
96 for_each_table_entry ( agent, IB_MAD_AGENTS ) {
97 if ( ( ( agent->mgmt_class & IB_MGMT_CLASS_MASK ) !=
98 ( hdr->mgmt_class & IB_MGMT_CLASS_MASK ) ) ||
99 ( agent->class_version != hdr->class_version ) ||
100 ( agent->attr_id != hdr->attr_id ) )
102 /* Found a matching agent */
103 agent->handle ( ibdev, mi, mad, av );
107 /* Otherwise, ignore it */
108 DBGC ( mi, "MI %p RX TID %08x%08x ignored\n",
109 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
114 * Complete receive via management interface
117 * @v ibdev Infiniband device
119 * @v dest Destination address vector
120 * @v source Source address vector
121 * @v iobuf I/O buffer
122 * @v rc Completion status code
124 static void ib_mi_complete_recv ( struct ib_device *ibdev,
125 struct ib_queue_pair *qp,
126 struct ib_address_vector *dest __unused,
127 struct ib_address_vector *source,
128 struct io_buffer *iobuf, int rc ) {
129 struct ib_mad_interface *mi = ib_qp_get_ownerdata ( qp );
131 struct ib_mad_hdr *hdr;
135 DBGC ( mi, "MI %p RX error: %s\n", mi, strerror ( rc ) );
140 if ( iob_len ( iobuf ) != sizeof ( *mad ) ) {
141 DBGC ( mi, "MI %p RX bad size (%zd bytes)\n",
142 mi, iob_len ( iobuf ) );
143 DBGC_HDA ( mi, 0, iobuf->data, iob_len ( iobuf ) );
148 if ( hdr->base_version != IB_MGMT_BASE_VERSION ) {
149 DBGC ( mi, "MI %p RX unsupported base version %x\n",
150 mi, hdr->base_version );
151 DBGC_HDA ( mi, 0, mad, sizeof ( *mad ) );
154 DBGC ( mi, "MI %p RX TID %08x%08x (%02x,%02x,%02x,%04x) status "
155 "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
156 hdr->mgmt_class, hdr->class_version, hdr->method,
157 ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
158 DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
161 if ( ( rc = ib_mi_handle ( ibdev, mi, mad, source ) ) != 0 )
168 /** Management interface completion operations */
169 static struct ib_completion_queue_operations ib_mi_completion_ops = {
170 .complete_recv = ib_mi_complete_recv,
173 /** Management interface queue pair operations */
174 static struct ib_queue_pair_operations ib_mi_queue_pair_ops = {
175 .alloc_iob = alloc_iob,
181 * @v ibdev Infiniband device
182 * @v mi Management interface
184 * @v av Destination address vector
185 * @ret rc Return status code
187 int ib_mi_send ( struct ib_device *ibdev, struct ib_mad_interface *mi,
188 union ib_mad *mad, struct ib_address_vector *av ) {
189 struct ib_mad_hdr *hdr = &mad->hdr;
190 struct io_buffer *iobuf;
193 /* Set common fields */
194 hdr->base_version = IB_MGMT_BASE_VERSION;
195 if ( ( hdr->tid[0] == 0 ) && ( hdr->tid[1] == 0 ) ) {
196 hdr->tid[0] = htonl ( IB_MI_TID_MAGIC );
197 hdr->tid[1] = htonl ( ++next_tid );
199 DBGC ( mi, "MI %p TX TID %08x%08x (%02x,%02x,%02x,%04x) status "
200 "%04x\n", mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
201 hdr->mgmt_class, hdr->class_version, hdr->method,
202 ntohs ( hdr->attr_id ), ntohs ( hdr->status ) );
203 DBGC2_HDA ( mi, 0, mad, sizeof ( *mad ) );
205 /* Construct directed route portion of response, if necessary */
206 if ( hdr->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ) {
207 struct ib_mad_smp *smp = &mad->smp;
208 unsigned int hop_pointer;
209 unsigned int hop_count;
211 smp->mad_hdr.status |= htons ( IB_SMP_STATUS_D_INBOUND );
212 hop_pointer = smp->mad_hdr.class_specific.smp.hop_pointer;
213 hop_count = smp->mad_hdr.class_specific.smp.hop_count;
214 assert ( hop_count == hop_pointer );
215 if ( hop_pointer < ( sizeof ( smp->return_path.hops ) /
216 sizeof ( smp->return_path.hops[0] ) ) ) {
217 smp->return_path.hops[hop_pointer] = ibdev->port;
219 DBGC ( mi, "MI %p TX TID %08x%08x invalid hop pointer "
220 "%d\n", mi, ntohl ( hdr->tid[0] ),
221 ntohl ( hdr->tid[1] ), hop_pointer );
226 /* Construct I/O buffer */
227 iobuf = alloc_iob ( sizeof ( *mad ) );
229 DBGC ( mi, "MI %p could not allocate buffer for TID "
231 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
234 memcpy ( iob_put ( iobuf, sizeof ( *mad ) ), mad, sizeof ( *mad ) );
236 /* Send I/O buffer */
237 if ( ( rc = ib_post_send ( ibdev, mi->qp, av, iobuf ) ) != 0 ) {
238 DBGC ( mi, "MI %p TX TID %08x%08x failed: %s\n",
239 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ),
249 * Handle management transaction timer expiry
251 * @v timer Retry timer
252 * @v expired Failure indicator
254 static void ib_mi_timer_expired ( struct retry_timer *timer, int expired ) {
255 struct ib_mad_transaction *madx =
256 container_of ( timer, struct ib_mad_transaction, timer );
257 struct ib_mad_interface *mi = madx->mi;
258 struct ib_device *ibdev = mi->ibdev;
259 struct ib_mad_hdr *hdr = &madx->mad.hdr;
261 /* Abandon transaction if we have tried too many times */
263 DBGC ( mi, "MI %p abandoning TID %08x%08x\n",
264 mi, ntohl ( hdr->tid[0] ), ntohl ( hdr->tid[1] ) );
265 madx->op->complete ( ibdev, mi, madx, -ETIMEDOUT, NULL, NULL );
269 /* Restart retransmission timer */
270 start_timer ( timer );
273 ib_mi_send ( ibdev, mi, &madx->mad, &madx->av );
277 * Create management transaction
279 * @v ibdev Infiniband device
280 * @v mi Management interface
282 * @v av Destination address, or NULL to use SM's GSI
283 * @v op Management transaction operations
284 * @ret madx Management transaction, or NULL
286 struct ib_mad_transaction *
287 ib_create_madx ( struct ib_device *ibdev, struct ib_mad_interface *mi,
288 union ib_mad *mad, struct ib_address_vector *av,
289 struct ib_mad_transaction_operations *op ) {
290 struct ib_mad_transaction *madx;
292 /* Allocate and initialise structure */
293 madx = zalloc ( sizeof ( *madx ) );
296 timer_init ( &madx->timer, ib_mi_timer_expired, NULL );
300 /* Determine address vector */
302 memcpy ( &madx->av, av, sizeof ( madx->av ) );
304 madx->av.lid = ibdev->sm_lid;
305 madx->av.sl = ibdev->sm_sl;
306 madx->av.qpn = IB_QPN_GSI;
307 madx->av.qkey = IB_QKEY_GSI;
311 memcpy ( &madx->mad, mad, sizeof ( madx->mad ) );
313 /* Add to list and start timer to send initial MAD */
314 list_add ( &madx->list, &mi->madx );
315 start_timer_nodelay ( &madx->timer );
321 * Destroy management transaction
323 * @v ibdev Infiniband device
324 * @v mi Management interface
325 * @v madx Management transaction
327 void ib_destroy_madx ( struct ib_device *ibdev __unused,
328 struct ib_mad_interface *mi __unused,
329 struct ib_mad_transaction *madx ) {
331 /* Stop timer and remove from list */
332 stop_timer ( &madx->timer );
333 list_del ( &madx->list );
335 /* Free transaction */
340 * Create management interface
342 * @v ibdev Infiniband device
343 * @v type Queue pair type
344 * @ret mi Management agent, or NULL
346 struct ib_mad_interface * ib_create_mi ( struct ib_device *ibdev,
347 enum ib_queue_pair_type type ) {
348 struct ib_mad_interface *mi;
351 /* Allocate and initialise fields */
352 mi = zalloc ( sizeof ( *mi ) );
356 INIT_LIST_HEAD ( &mi->madx );
358 /* Create completion queue */
359 mi->cq = ib_create_cq ( ibdev, IB_MI_NUM_CQES, &ib_mi_completion_ops );
361 DBGC ( mi, "MI %p could not allocate completion queue\n", mi );
365 /* Create queue pair */
366 mi->qp = ib_create_qp ( ibdev, type, IB_MI_NUM_SEND_WQES, mi->cq,
367 IB_MI_NUM_RECV_WQES, mi->cq,
368 &ib_mi_queue_pair_ops );
370 DBGC ( mi, "MI %p could not allocate queue pair\n", mi );
373 ib_qp_set_ownerdata ( mi->qp, mi );
374 DBGC ( mi, "MI %p (%s) running on QPN %#lx\n",
375 mi, ( ( type == IB_QPT_SMI ) ? "SMI" : "GSI" ), mi->qp->qpn );
378 mi->qp->qkey = ( ( type == IB_QPT_SMI ) ? IB_QKEY_SMI : IB_QKEY_GSI );
379 if ( ( rc = ib_modify_qp ( ibdev, mi->qp ) ) != 0 ) {
380 DBGC ( mi, "MI %p could not set queue key: %s\n",
381 mi, strerror ( rc ) );
385 /* Fill receive ring */
386 ib_refill_recv ( ibdev, mi->qp );
390 ib_destroy_qp ( ibdev, mi->qp );
392 ib_destroy_cq ( ibdev, mi->cq );
400 * Destroy management interface
402 * @v mi Management interface
404 void ib_destroy_mi ( struct ib_device *ibdev, struct ib_mad_interface *mi ) {
405 struct ib_mad_transaction *madx;
406 struct ib_mad_transaction *tmp;
408 /* Flush any outstanding requests */
409 list_for_each_entry_safe ( madx, tmp, &mi->madx, list ) {
410 DBGC ( mi, "MI %p destroyed while TID %08x%08x in progress\n",
411 mi, ntohl ( madx->mad.hdr.tid[0] ),
412 ntohl ( madx->mad.hdr.tid[1] ) );
413 madx->op->complete ( ibdev, mi, madx, -ECANCELED, NULL, NULL );
416 ib_destroy_qp ( ibdev, mi->qp );
417 ib_destroy_cq ( ibdev, mi->cq );