[ipv6] Fix router solicitation struct and flag checking
[gpxe.git] / src / net / infiniband.c
blobd2ca8dd31d6d52e5c3680df43b4e58dc8f0ee4a2
1 /*
2 * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <byteswap.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <gpxe/list.h>
30 #include <gpxe/errortab.h>
31 #include <gpxe/if_arp.h>
32 #include <gpxe/netdevice.h>
33 #include <gpxe/iobuf.h>
34 #include <gpxe/ipoib.h>
35 #include <gpxe/process.h>
36 #include <gpxe/infiniband.h>
37 #include <gpxe/ib_mi.h>
38 #include <gpxe/ib_sma.h>
40 /** @file
42 * Infiniband protocol
46 /** List of Infiniband devices */
47 struct list_head ib_devices = LIST_HEAD_INIT ( ib_devices );
49 /** List of open Infiniband devices, in reverse order of opening */
50 static struct list_head open_ib_devices = LIST_HEAD_INIT ( open_ib_devices );
52 /* Disambiguate the various possible EINPROGRESSes */
53 #define EINPROGRESS_INIT __einfo_error ( EINFO_EINPROGRESS_INIT )
54 #define EINFO_EINPROGRESS_INIT __einfo_uniqify \
55 ( EINFO_EINPROGRESS, 0x01, "Initialising" )
56 #define EINPROGRESS_ARMED __einfo_error ( EINFO_EINPROGRESS_ARMED )
57 #define EINFO_EINPROGRESS_ARMED __einfo_uniqify \
58 ( EINFO_EINPROGRESS, 0x02, "Armed" )
60 /** Human-readable message for the link statuses */
61 struct errortab infiniband_errors[] __errortab = {
62 __einfo_errortab ( EINFO_EINPROGRESS_INIT ),
63 __einfo_errortab ( EINFO_EINPROGRESS_ARMED ),
66 /***************************************************************************
68 * Completion queues
70 ***************************************************************************
73 /**
74 * Create completion queue
76 * @v ibdev Infiniband device
77 * @v num_cqes Number of completion queue entries
78 * @v op Completion queue operations
79 * @ret cq New completion queue
81 struct ib_completion_queue *
82 ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
83 struct ib_completion_queue_operations *op ) {
84 struct ib_completion_queue *cq;
85 int rc;
87 DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
89 /* Allocate and initialise data structure */
90 cq = zalloc ( sizeof ( *cq ) );
91 if ( ! cq )
92 goto err_alloc_cq;
93 cq->ibdev = ibdev;
94 list_add ( &cq->list, &ibdev->cqs );
95 cq->num_cqes = num_cqes;
96 INIT_LIST_HEAD ( &cq->work_queues );
97 cq->op = op;
99 /* Perform device-specific initialisation and get CQN */
100 if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
101 DBGC ( ibdev, "IBDEV %p could not initialise completion "
102 "queue: %s\n", ibdev, strerror ( rc ) );
103 goto err_dev_create_cq;
106 DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
107 "with CQN %#lx\n", ibdev, num_cqes, cq,
108 ib_cq_get_drvdata ( cq ), cq->cqn );
109 return cq;
111 ibdev->op->destroy_cq ( ibdev, cq );
112 err_dev_create_cq:
113 list_del ( &cq->list );
114 free ( cq );
115 err_alloc_cq:
116 return NULL;
120 * Destroy completion queue
122 * @v ibdev Infiniband device
123 * @v cq Completion queue
125 void ib_destroy_cq ( struct ib_device *ibdev,
126 struct ib_completion_queue *cq ) {
127 DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
128 ibdev, cq->cqn );
129 assert ( list_empty ( &cq->work_queues ) );
130 ibdev->op->destroy_cq ( ibdev, cq );
131 list_del ( &cq->list );
132 free ( cq );
136 * Poll completion queue
138 * @v ibdev Infiniband device
139 * @v cq Completion queue
141 void ib_poll_cq ( struct ib_device *ibdev,
142 struct ib_completion_queue *cq ) {
143 struct ib_work_queue *wq;
145 /* Poll completion queue */
146 ibdev->op->poll_cq ( ibdev, cq );
148 /* Refill receive work queues */
149 list_for_each_entry ( wq, &cq->work_queues, list ) {
150 if ( ! wq->is_send )
151 ib_refill_recv ( ibdev, wq->qp );
155 /***************************************************************************
157 * Work queues
159 ***************************************************************************
163 * Create queue pair
165 * @v ibdev Infiniband device
166 * @v type Queue pair type
167 * @v num_send_wqes Number of send work queue entries
168 * @v send_cq Send completion queue
169 * @v num_recv_wqes Number of receive work queue entries
170 * @v recv_cq Receive completion queue
171 * @ret qp Queue pair
173 * The queue pair will be left in the INIT state; you must call
174 * ib_modify_qp() before it is ready to use for sending and receiving.
176 struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
177 enum ib_queue_pair_type type,
178 unsigned int num_send_wqes,
179 struct ib_completion_queue *send_cq,
180 unsigned int num_recv_wqes,
181 struct ib_completion_queue *recv_cq ) {
182 struct ib_queue_pair *qp;
183 size_t total_size;
184 int rc;
186 DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
188 /* Allocate and initialise data structure */
189 total_size = ( sizeof ( *qp ) +
190 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
191 ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
192 qp = zalloc ( total_size );
193 if ( ! qp )
194 goto err_alloc_qp;
195 qp->ibdev = ibdev;
196 list_add ( &qp->list, &ibdev->qps );
197 qp->type = type;
198 qp->send.qp = qp;
199 qp->send.is_send = 1;
200 qp->send.cq = send_cq;
201 list_add ( &qp->send.list, &send_cq->work_queues );
202 qp->send.psn = ( random() & 0xffffffUL );
203 qp->send.num_wqes = num_send_wqes;
204 qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
205 qp->recv.qp = qp;
206 qp->recv.cq = recv_cq;
207 list_add ( &qp->recv.list, &recv_cq->work_queues );
208 qp->recv.psn = ( random() & 0xffffffUL );
209 qp->recv.num_wqes = num_recv_wqes;
210 qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
211 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
212 INIT_LIST_HEAD ( &qp->mgids );
214 /* Perform device-specific initialisation and get QPN */
215 if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
216 DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
217 "%s\n", ibdev, strerror ( rc ) );
218 goto err_dev_create_qp;
220 DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
221 ibdev, qp, ib_qp_get_drvdata ( qp ), qp->qpn );
222 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
223 ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
224 qp->recv.iobufs );
225 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
226 ibdev, qp->qpn, num_recv_wqes, qp->recv.iobufs,
227 ( ( ( void * ) qp ) + total_size ) );
229 /* Calculate externally-visible QPN */
230 switch ( type ) {
231 case IB_QPT_SMI:
232 qp->ext_qpn = IB_QPN_SMI;
233 break;
234 case IB_QPT_GSI:
235 qp->ext_qpn = IB_QPN_GSI;
236 break;
237 default:
238 qp->ext_qpn = qp->qpn;
239 break;
241 if ( qp->ext_qpn != qp->qpn ) {
242 DBGC ( ibdev, "IBDEV %p QPN %#lx has external QPN %#lx\n",
243 ibdev, qp->qpn, qp->ext_qpn );
246 return qp;
248 ibdev->op->destroy_qp ( ibdev, qp );
249 err_dev_create_qp:
250 list_del ( &qp->send.list );
251 list_del ( &qp->recv.list );
252 list_del ( &qp->list );
253 free ( qp );
254 err_alloc_qp:
255 return NULL;
259 * Modify queue pair
261 * @v ibdev Infiniband device
262 * @v qp Queue pair
263 * @v av New address vector, if applicable
264 * @ret rc Return status code
266 int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
267 int rc;
269 DBGC ( ibdev, "IBDEV %p modifying QPN %#lx\n", ibdev, qp->qpn );
271 if ( ( rc = ibdev->op->modify_qp ( ibdev, qp ) ) != 0 ) {
272 DBGC ( ibdev, "IBDEV %p could not modify QPN %#lx: %s\n",
273 ibdev, qp->qpn, strerror ( rc ) );
274 return rc;
277 return 0;
281 * Destroy queue pair
283 * @v ibdev Infiniband device
284 * @v qp Queue pair
286 void ib_destroy_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
287 struct io_buffer *iobuf;
288 unsigned int i;
290 DBGC ( ibdev, "IBDEV %p destroying QPN %#lx\n",
291 ibdev, qp->qpn );
293 assert ( list_empty ( &qp->mgids ) );
295 /* Perform device-specific destruction */
296 ibdev->op->destroy_qp ( ibdev, qp );
298 /* Complete any remaining I/O buffers with errors */
299 for ( i = 0 ; i < qp->send.num_wqes ; i++ ) {
300 if ( ( iobuf = qp->send.iobufs[i] ) != NULL )
301 ib_complete_send ( ibdev, qp, iobuf, -ECANCELED );
303 for ( i = 0 ; i < qp->recv.num_wqes ; i++ ) {
304 if ( ( iobuf = qp->recv.iobufs[i] ) != NULL ) {
305 ib_complete_recv ( ibdev, qp, NULL, iobuf,
306 -ECANCELED );
310 /* Remove work queues from completion queue */
311 list_del ( &qp->send.list );
312 list_del ( &qp->recv.list );
314 /* Free QP */
315 list_del ( &qp->list );
316 free ( qp );
320 * Find queue pair by QPN
322 * @v ibdev Infiniband device
323 * @v qpn Queue pair number
324 * @ret qp Queue pair, or NULL
326 struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
327 unsigned long qpn ) {
328 struct ib_queue_pair *qp;
330 list_for_each_entry ( qp, &ibdev->qps, list ) {
331 if ( ( qpn == qp->qpn ) || ( qpn == qp->ext_qpn ) )
332 return qp;
334 return NULL;
338 * Find queue pair by multicast GID
340 * @v ibdev Infiniband device
341 * @v gid Multicast GID
342 * @ret qp Queue pair, or NULL
344 struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
345 struct ib_gid *gid ) {
346 struct ib_queue_pair *qp;
347 struct ib_multicast_gid *mgid;
349 list_for_each_entry ( qp, &ibdev->qps, list ) {
350 list_for_each_entry ( mgid, &qp->mgids, list ) {
351 if ( memcmp ( &mgid->gid, gid,
352 sizeof ( mgid->gid ) ) == 0 ) {
353 return qp;
357 return NULL;
361 * Find work queue belonging to completion queue
363 * @v cq Completion queue
364 * @v qpn Queue pair number
365 * @v is_send Find send work queue (rather than receive)
366 * @ret wq Work queue, or NULL if not found
368 struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
369 unsigned long qpn, int is_send ) {
370 struct ib_work_queue *wq;
372 list_for_each_entry ( wq, &cq->work_queues, list ) {
373 if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
374 return wq;
376 return NULL;
380 * Post send work queue entry
382 * @v ibdev Infiniband device
383 * @v qp Queue pair
384 * @v av Address vector
385 * @v iobuf I/O buffer
386 * @ret rc Return status code
388 int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
389 struct ib_address_vector *av,
390 struct io_buffer *iobuf ) {
391 struct ib_address_vector av_copy;
392 int rc;
394 /* Check queue fill level */
395 if ( qp->send.fill >= qp->send.num_wqes ) {
396 DBGC ( ibdev, "IBDEV %p QPN %#lx send queue full\n",
397 ibdev, qp->qpn );
398 return -ENOBUFS;
401 /* Use default address vector if none specified */
402 if ( ! av )
403 av = &qp->av;
405 /* Make modifiable copy of address vector */
406 memcpy ( &av_copy, av, sizeof ( av_copy ) );
407 av = &av_copy;
409 /* Fill in optional parameters in address vector */
410 if ( ! av->qkey )
411 av->qkey = qp->qkey;
412 if ( ! av->rate )
413 av->rate = IB_RATE_2_5;
415 /* Post to hardware */
416 if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
417 DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
418 "%s\n", ibdev, qp->qpn, strerror ( rc ) );
419 return rc;
422 qp->send.fill++;
423 return 0;
427 * Post receive work queue entry
429 * @v ibdev Infiniband device
430 * @v qp Queue pair
431 * @v iobuf I/O buffer
432 * @ret rc Return status code
434 int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
435 struct io_buffer *iobuf ) {
436 int rc;
438 /* Check packet length */
439 if ( iob_tailroom ( iobuf ) < IB_MAX_PAYLOAD_SIZE ) {
440 DBGC ( ibdev, "IBDEV %p QPN %#lx wrong RX buffer size (%zd)\n",
441 ibdev, qp->qpn, iob_tailroom ( iobuf ) );
442 return -EINVAL;
445 /* Check queue fill level */
446 if ( qp->recv.fill >= qp->recv.num_wqes ) {
447 DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
448 ibdev, qp->qpn );
449 return -ENOBUFS;
452 /* Post to hardware */
453 if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
454 DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
455 "%s\n", ibdev, qp->qpn, strerror ( rc ) );
456 return rc;
459 qp->recv.fill++;
460 return 0;
464 * Complete send work queue entry
466 * @v ibdev Infiniband device
467 * @v qp Queue pair
468 * @v iobuf I/O buffer
469 * @v rc Completion status code
471 void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
472 struct io_buffer *iobuf, int rc ) {
474 if ( qp->send.cq->op->complete_send ) {
475 qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
476 } else {
477 free_iob ( iobuf );
479 qp->send.fill--;
483 * Complete receive work queue entry
485 * @v ibdev Infiniband device
486 * @v qp Queue pair
487 * @v av Address vector
488 * @v iobuf I/O buffer
489 * @v rc Completion status code
491 void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
492 struct ib_address_vector *av,
493 struct io_buffer *iobuf, int rc ) {
495 if ( qp->recv.cq->op->complete_recv ) {
496 qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
497 } else {
498 free_iob ( iobuf );
500 qp->recv.fill--;
504 * Refill receive work queue
506 * @v ibdev Infiniband device
507 * @v qp Queue pair
509 void ib_refill_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
510 struct io_buffer *iobuf;
511 int rc;
513 /* Keep filling while unfilled entries remain */
514 while ( qp->recv.fill < qp->recv.num_wqes ) {
516 /* Allocate I/O buffer */
517 iobuf = alloc_iob ( IB_MAX_PAYLOAD_SIZE );
518 if ( ! iobuf ) {
519 /* Non-fatal; we will refill on next attempt */
520 return;
523 /* Post I/O buffer */
524 if ( ( rc = ib_post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
525 DBGC ( ibdev, "IBDEV %p could not refill: %s\n",
526 ibdev, strerror ( rc ) );
527 free_iob ( iobuf );
528 /* Give up */
529 return;
534 /***************************************************************************
536 * Link control
538 ***************************************************************************
542 * Open port
544 * @v ibdev Infiniband device
545 * @ret rc Return status code
547 int ib_open ( struct ib_device *ibdev ) {
548 int rc;
550 /* Increment device open request counter */
551 if ( ibdev->open_count++ > 0 ) {
552 /* Device was already open; do nothing */
553 return 0;
556 /* Create subnet management interface */
557 ibdev->smi = ib_create_mi ( ibdev, IB_QPT_SMI );
558 if ( ! ibdev->smi ) {
559 DBGC ( ibdev, "IBDEV %p could not create SMI\n", ibdev );
560 rc = -ENOMEM;
561 goto err_create_smi;
564 /* Create subnet management agent */
565 if ( ( rc = ib_create_sma ( ibdev, ibdev->smi ) ) != 0 ) {
566 DBGC ( ibdev, "IBDEV %p could not create SMA: %s\n",
567 ibdev, strerror ( rc ) );
568 goto err_create_sma;
571 /* Create general services interface */
572 ibdev->gsi = ib_create_mi ( ibdev, IB_QPT_GSI );
573 if ( ! ibdev->gsi ) {
574 DBGC ( ibdev, "IBDEV %p could not create GSI\n", ibdev );
575 rc = -ENOMEM;
576 goto err_create_gsi;
579 /* Open device */
580 if ( ( rc = ibdev->op->open ( ibdev ) ) != 0 ) {
581 DBGC ( ibdev, "IBDEV %p could not open: %s\n",
582 ibdev, strerror ( rc ) );
583 goto err_open;
586 /* Add to head of open devices list */
587 list_add ( &ibdev->open_list, &open_ib_devices );
589 assert ( ibdev->open_count == 1 );
590 return 0;
592 ibdev->op->close ( ibdev );
593 err_open:
594 ib_destroy_mi ( ibdev, ibdev->gsi );
595 err_create_gsi:
596 ib_destroy_sma ( ibdev, ibdev->smi );
597 err_create_sma:
598 ib_destroy_mi ( ibdev, ibdev->smi );
599 err_create_smi:
600 assert ( ibdev->open_count == 1 );
601 ibdev->open_count = 0;
602 return rc;
606 * Close port
608 * @v ibdev Infiniband device
610 void ib_close ( struct ib_device *ibdev ) {
612 /* Decrement device open request counter */
613 ibdev->open_count--;
615 /* Close device if this was the last remaining requested opening */
616 if ( ibdev->open_count == 0 ) {
617 list_del ( &ibdev->open_list );
618 ib_destroy_mi ( ibdev, ibdev->gsi );
619 ib_destroy_sma ( ibdev, ibdev->smi );
620 ib_destroy_mi ( ibdev, ibdev->smi );
621 ibdev->op->close ( ibdev );
626 * Get link state
628 * @v ibdev Infiniband device
629 * @ret rc Link status code
631 int ib_link_rc ( struct ib_device *ibdev ) {
632 switch ( ibdev->port_state ) {
633 case IB_PORT_STATE_DOWN: return -ENOTCONN;
634 case IB_PORT_STATE_INIT: return -EINPROGRESS_INIT;
635 case IB_PORT_STATE_ARMED: return -EINPROGRESS_ARMED;
636 case IB_PORT_STATE_ACTIVE: return 0;
637 default: return -EINVAL;
641 /***************************************************************************
643 * Multicast
645 ***************************************************************************
649 * Attach to multicast group
651 * @v ibdev Infiniband device
652 * @v qp Queue pair
653 * @v gid Multicast GID
654 * @ret rc Return status code
656 * Note that this function handles only the local device's attachment
657 * to the multicast GID; it does not issue the relevant MADs to join
658 * the multicast group on the subnet.
660 int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
661 struct ib_gid *gid ) {
662 struct ib_multicast_gid *mgid;
663 int rc;
665 /* Add to software multicast GID list */
666 mgid = zalloc ( sizeof ( *mgid ) );
667 if ( ! mgid ) {
668 rc = -ENOMEM;
669 goto err_alloc_mgid;
671 memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
672 list_add ( &mgid->list, &qp->mgids );
674 /* Add to hardware multicast GID list */
675 if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
676 goto err_dev_mcast_attach;
678 return 0;
680 err_dev_mcast_attach:
681 list_del ( &mgid->list );
682 free ( mgid );
683 err_alloc_mgid:
684 return rc;
688 * Detach from multicast group
690 * @v ibdev Infiniband device
691 * @v qp Queue pair
692 * @v gid Multicast GID
694 void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
695 struct ib_gid *gid ) {
696 struct ib_multicast_gid *mgid;
698 /* Remove from hardware multicast GID list */
699 ibdev->op->mcast_detach ( ibdev, qp, gid );
701 /* Remove from software multicast GID list */
702 list_for_each_entry ( mgid, &qp->mgids, list ) {
703 if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
704 list_del ( &mgid->list );
705 free ( mgid );
706 break;
711 /***************************************************************************
713 * Miscellaneous
715 ***************************************************************************
719 * Get Infiniband HCA information
721 * @v ibdev Infiniband device
722 * @ret hca_guid HCA GUID
723 * @ret num_ports Number of ports
725 int ib_get_hca_info ( struct ib_device *ibdev,
726 struct ib_gid_half *hca_guid ) {
727 struct ib_device *tmp;
728 int num_ports = 0;
730 /* Search for IB devices with the same physical device to
731 * identify port count and a suitable Node GUID.
733 for_each_ibdev ( tmp ) {
734 if ( tmp->dev != ibdev->dev )
735 continue;
736 if ( num_ports == 0 ) {
737 memcpy ( hca_guid, &tmp->gid.u.half[1],
738 sizeof ( *hca_guid ) );
740 num_ports++;
742 return num_ports;
746 * Set port information
748 * @v ibdev Infiniband device
749 * @v mad Set port information MAD
751 int ib_set_port_info ( struct ib_device *ibdev, union ib_mad *mad ) {
752 int rc;
754 /* Adapters with embedded SMAs do not need to support this method */
755 if ( ! ibdev->op->set_port_info ) {
756 DBGC ( ibdev, "IBDEV %p does not support setting port "
757 "information\n", ibdev );
758 return -ENOTSUP;
761 if ( ( rc = ibdev->op->set_port_info ( ibdev, mad ) ) != 0 ) {
762 DBGC ( ibdev, "IBDEV %p could not set port information: %s\n",
763 ibdev, strerror ( rc ) );
764 return rc;
767 return 0;
771 * Set partition key table
773 * @v ibdev Infiniband device
774 * @v mad Set partition key table MAD
776 int ib_set_pkey_table ( struct ib_device *ibdev, union ib_mad *mad ) {
777 int rc;
779 /* Adapters with embedded SMAs do not need to support this method */
780 if ( ! ibdev->op->set_pkey_table ) {
781 DBGC ( ibdev, "IBDEV %p does not support setting partition "
782 "key table\n", ibdev );
783 return -ENOTSUP;
786 if ( ( rc = ibdev->op->set_pkey_table ( ibdev, mad ) ) != 0 ) {
787 DBGC ( ibdev, "IBDEV %p could not set partition key table: "
788 "%s\n", ibdev, strerror ( rc ) );
789 return rc;
792 return 0;
795 /***************************************************************************
797 * Event queues
799 ***************************************************************************
803 * Handle Infiniband link state change
805 * @v ibdev Infiniband device
807 void ib_link_state_changed ( struct ib_device *ibdev ) {
809 /* Notify IPoIB of link state change */
810 ipoib_link_state_changed ( ibdev );
814 * Poll event queue
816 * @v ibdev Infiniband device
818 void ib_poll_eq ( struct ib_device *ibdev ) {
819 struct ib_completion_queue *cq;
821 /* Poll device's event queue */
822 ibdev->op->poll_eq ( ibdev );
824 /* Poll all completion queues */
825 list_for_each_entry ( cq, &ibdev->cqs, list )
826 ib_poll_cq ( ibdev, cq );
830 * Single-step the Infiniband event queue
832 * @v process Infiniband event queue process
834 static void ib_step ( struct process *process __unused ) {
835 struct ib_device *ibdev;
837 for_each_ibdev ( ibdev )
838 ib_poll_eq ( ibdev );
841 /** Infiniband event queue process */
842 struct process ib_process __permanent_process = {
843 .list = LIST_HEAD_INIT ( ib_process.list ),
844 .step = ib_step,
847 /***************************************************************************
849 * Infiniband device creation/destruction
851 ***************************************************************************
855 * Allocate Infiniband device
857 * @v priv_size Size of driver private data area
858 * @ret ibdev Infiniband device, or NULL
860 struct ib_device * alloc_ibdev ( size_t priv_size ) {
861 struct ib_device *ibdev;
862 void *drv_priv;
863 size_t total_len;
865 total_len = ( sizeof ( *ibdev ) + priv_size );
866 ibdev = zalloc ( total_len );
867 if ( ibdev ) {
868 drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
869 ib_set_drvdata ( ibdev, drv_priv );
870 INIT_LIST_HEAD ( &ibdev->cqs );
871 INIT_LIST_HEAD ( &ibdev->qps );
872 ibdev->port_state = IB_PORT_STATE_DOWN;
873 ibdev->lid = IB_LID_NONE;
874 ibdev->pkey = IB_PKEY_DEFAULT;
876 return ibdev;
880 * Register Infiniband device
882 * @v ibdev Infiniband device
883 * @ret rc Return status code
885 int register_ibdev ( struct ib_device *ibdev ) {
886 int rc;
888 /* Add to device list */
889 ibdev_get ( ibdev );
890 list_add_tail ( &ibdev->list, &ib_devices );
892 /* Add IPoIB device */
893 if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
894 DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
895 ibdev, strerror ( rc ) );
896 goto err_ipoib_probe;
899 DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
900 ibdev->dev->name );
901 return 0;
903 err_ipoib_probe:
904 list_del ( &ibdev->list );
905 ibdev_put ( ibdev );
906 return rc;
910 * Unregister Infiniband device
912 * @v ibdev Infiniband device
914 void unregister_ibdev ( struct ib_device *ibdev ) {
916 /* Close device */
917 ipoib_remove ( ibdev );
919 /* Remove from device list */
920 list_del ( &ibdev->list );
921 ibdev_put ( ibdev );
922 DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );
926 * Find Infiniband device by GID
928 * @v gid GID
929 * @ret ibdev Infiniband device, or NULL
931 struct ib_device * find_ibdev ( struct ib_gid *gid ) {
932 struct ib_device *ibdev;
934 for_each_ibdev ( ibdev ) {
935 if ( memcmp ( gid, &ibdev->gid, sizeof ( *gid ) ) == 0 )
936 return ibdev;
938 return NULL;
942 * Get most recently opened Infiniband device
944 * @ret ibdev Most recently opened Infiniband device, or NULL
946 struct ib_device * last_opened_ibdev ( void ) {
947 struct ib_device *ibdev;
949 list_for_each_entry ( ibdev, &open_ib_devices, open_list ) {
950 assert ( ibdev->open_count != 0 );
951 return ibdev;
954 return NULL;