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 );
29 #include <gpxe/list.h>
30 #include <gpxe/if_arp.h>
31 #include <gpxe/netdevice.h>
32 #include <gpxe/iobuf.h>
33 #include <gpxe/ipoib.h>
34 #include <gpxe/process.h>
35 #include <gpxe/infiniband.h>
36 #include <gpxe/ib_gma.h>
44 /** List of Infiniband devices */
45 struct list_head ib_devices = LIST_HEAD_INIT ( ib_devices );
47 /***************************************************************************
51 ***************************************************************************
55 * Create completion queue
57 * @v ibdev Infiniband device
58 * @v num_cqes Number of completion queue entries
59 * @v op Completion queue operations
60 * @ret cq New completion queue
62 struct ib_completion_queue *
63 ib_create_cq ( struct ib_device *ibdev, unsigned int num_cqes,
64 struct ib_completion_queue_operations *op ) {
65 struct ib_completion_queue *cq;
68 DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
70 /* Allocate and initialise data structure */
71 cq = zalloc ( sizeof ( *cq ) );
75 list_add ( &cq->list, &ibdev->cqs );
76 cq->num_cqes = num_cqes;
77 INIT_LIST_HEAD ( &cq->work_queues );
80 /* Perform device-specific initialisation and get CQN */
81 if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
82 DBGC ( ibdev, "IBDEV %p could not initialise completion "
83 "queue: %s\n", ibdev, strerror ( rc ) );
84 goto err_dev_create_cq;
87 DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
88 "with CQN %#lx\n", ibdev, num_cqes, cq,
89 ib_cq_get_drvdata ( cq ), cq->cqn );
92 ibdev->op->destroy_cq ( ibdev, cq );
94 list_del ( &cq->list );
101 * Destroy completion queue
103 * @v ibdev Infiniband device
104 * @v cq Completion queue
106 void ib_destroy_cq ( struct ib_device *ibdev,
107 struct ib_completion_queue *cq ) {
108 DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
110 assert ( list_empty ( &cq->work_queues ) );
111 ibdev->op->destroy_cq ( ibdev, cq );
112 list_del ( &cq->list );
117 * Poll completion queue
119 * @v ibdev Infiniband device
120 * @v cq Completion queue
122 void ib_poll_cq ( struct ib_device *ibdev,
123 struct ib_completion_queue *cq ) {
124 struct ib_work_queue *wq;
126 /* Poll completion queue */
127 ibdev->op->poll_cq ( ibdev, cq );
129 /* Refill receive work queues */
130 list_for_each_entry ( wq, &cq->work_queues, list ) {
132 ib_refill_recv ( ibdev, wq->qp );
136 /***************************************************************************
140 ***************************************************************************
146 * @v ibdev Infiniband device
147 * @v type Queue pair type
148 * @v num_send_wqes Number of send work queue entries
149 * @v send_cq Send completion queue
150 * @v num_recv_wqes Number of receive work queue entries
151 * @v recv_cq Receive completion queue
155 struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
156 enum ib_queue_pair_type type,
157 unsigned int num_send_wqes,
158 struct ib_completion_queue *send_cq,
159 unsigned int num_recv_wqes,
160 struct ib_completion_queue *recv_cq,
161 unsigned long qkey ) {
162 struct ib_queue_pair *qp;
166 DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
168 /* Allocate and initialise data structure */
169 total_size = ( sizeof ( *qp ) +
170 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
171 ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
172 qp = zalloc ( total_size );
176 list_add ( &qp->list, &ibdev->qps );
180 qp->send.is_send = 1;
181 qp->send.cq = send_cq;
182 list_add ( &qp->send.list, &send_cq->work_queues );
183 qp->send.num_wqes = num_send_wqes;
184 qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
186 qp->recv.cq = recv_cq;
187 list_add ( &qp->recv.list, &recv_cq->work_queues );
188 qp->recv.num_wqes = num_recv_wqes;
189 qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
190 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
191 INIT_LIST_HEAD ( &qp->mgids );
193 /* Perform device-specific initialisation and get QPN */
194 if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
195 DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
196 "%s\n", ibdev, strerror ( rc ) );
197 goto err_dev_create_qp;
200 DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
201 ibdev, qp, ib_qp_get_drvdata ( qp ), qp->qpn );
202 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
203 ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
205 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
206 ibdev, qp->qpn, num_recv_wqes, qp->recv.iobufs,
207 ( ( ( void * ) qp ) + total_size ) );
210 ibdev->op->destroy_qp ( ibdev, qp );
212 list_del ( &qp->send.list );
213 list_del ( &qp->recv.list );
214 list_del ( &qp->list );
223 * @v ibdev Infiniband device
225 * @v mod_list Modification list
226 * @v qkey New queue key, if applicable
227 * @ret rc Return status code
229 int ib_modify_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp,
230 unsigned long mod_list, unsigned long qkey ) {
233 DBGC ( ibdev, "IBDEV %p modifying QPN %#lx\n", ibdev, qp->qpn );
235 if ( mod_list & IB_MODIFY_QKEY )
238 if ( ( rc = ibdev->op->modify_qp ( ibdev, qp, mod_list ) ) != 0 ) {
239 DBGC ( ibdev, "IBDEV %p could not modify QPN %#lx: %s\n",
240 ibdev, qp->qpn, strerror ( rc ) );
250 * @v ibdev Infiniband device
253 void ib_destroy_qp ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
254 struct io_buffer *iobuf;
257 DBGC ( ibdev, "IBDEV %p destroying QPN %#lx\n",
260 assert ( list_empty ( &qp->mgids ) );
262 /* Perform device-specific destruction */
263 ibdev->op->destroy_qp ( ibdev, qp );
265 /* Complete any remaining I/O buffers with errors */
266 for ( i = 0 ; i < qp->send.num_wqes ; i++ ) {
267 if ( ( iobuf = qp->send.iobufs[i] ) != NULL )
268 ib_complete_send ( ibdev, qp, iobuf, -ECANCELED );
270 for ( i = 0 ; i < qp->recv.num_wqes ; i++ ) {
271 if ( ( iobuf = qp->recv.iobufs[i] ) != NULL ) {
272 ib_complete_recv ( ibdev, qp, NULL, iobuf,
277 /* Remove work queues from completion queue */
278 list_del ( &qp->send.list );
279 list_del ( &qp->recv.list );
282 list_del ( &qp->list );
287 * Find queue pair by QPN
289 * @v ibdev Infiniband device
290 * @v qpn Queue pair number
291 * @ret qp Queue pair, or NULL
293 struct ib_queue_pair * ib_find_qp_qpn ( struct ib_device *ibdev,
294 unsigned long qpn ) {
295 struct ib_queue_pair *qp;
297 list_for_each_entry ( qp, &ibdev->qps, list ) {
298 if ( qp->qpn == qpn )
305 * Find queue pair by multicast GID
307 * @v ibdev Infiniband device
308 * @v gid Multicast GID
309 * @ret qp Queue pair, or NULL
311 struct ib_queue_pair * ib_find_qp_mgid ( struct ib_device *ibdev,
312 struct ib_gid *gid ) {
313 struct ib_queue_pair *qp;
314 struct ib_multicast_gid *mgid;
316 list_for_each_entry ( qp, &ibdev->qps, list ) {
317 list_for_each_entry ( mgid, &qp->mgids, list ) {
318 if ( memcmp ( &mgid->gid, gid,
319 sizeof ( mgid->gid ) ) == 0 ) {
328 * Find work queue belonging to completion queue
330 * @v cq Completion queue
331 * @v qpn Queue pair number
332 * @v is_send Find send work queue (rather than receive)
333 * @ret wq Work queue, or NULL if not found
335 struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
336 unsigned long qpn, int is_send ) {
337 struct ib_work_queue *wq;
339 list_for_each_entry ( wq, &cq->work_queues, list ) {
340 if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
347 * Post send work queue entry
349 * @v ibdev Infiniband device
351 * @v av Address vector
352 * @v iobuf I/O buffer
353 * @ret rc Return status code
355 int ib_post_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
356 struct ib_address_vector *av,
357 struct io_buffer *iobuf ) {
360 /* Check queue fill level */
361 if ( qp->send.fill >= qp->send.num_wqes ) {
362 DBGC ( ibdev, "IBDEV %p QPN %#lx send queue full\n",
367 /* Fill in optional parameters in address vector */
371 av->rate = IB_RATE_2_5;
373 /* Post to hardware */
374 if ( ( rc = ibdev->op->post_send ( ibdev, qp, av, iobuf ) ) != 0 ) {
375 DBGC ( ibdev, "IBDEV %p QPN %#lx could not post send WQE: "
376 "%s\n", ibdev, qp->qpn, strerror ( rc ) );
385 * Post receive work queue entry
387 * @v ibdev Infiniband device
389 * @v iobuf I/O buffer
390 * @ret rc Return status code
392 int ib_post_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
393 struct io_buffer *iobuf ) {
396 /* Check packet length */
397 if ( iob_tailroom ( iobuf ) < IB_MAX_PAYLOAD_SIZE ) {
398 DBGC ( ibdev, "IBDEV %p QPN %#lx wrong RX buffer size (%zd)\n",
399 ibdev, qp->qpn, iob_tailroom ( iobuf ) );
403 /* Check queue fill level */
404 if ( qp->recv.fill >= qp->recv.num_wqes ) {
405 DBGC ( ibdev, "IBDEV %p QPN %#lx receive queue full\n",
410 /* Post to hardware */
411 if ( ( rc = ibdev->op->post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
412 DBGC ( ibdev, "IBDEV %p QPN %#lx could not post receive WQE: "
413 "%s\n", ibdev, qp->qpn, strerror ( rc ) );
422 * Complete send work queue entry
424 * @v ibdev Infiniband device
426 * @v iobuf I/O buffer
427 * @v rc Completion status code
429 void ib_complete_send ( struct ib_device *ibdev, struct ib_queue_pair *qp,
430 struct io_buffer *iobuf, int rc ) {
432 if ( qp->send.cq->op->complete_send ) {
433 qp->send.cq->op->complete_send ( ibdev, qp, iobuf, rc );
441 * Complete receive work queue entry
443 * @v ibdev Infiniband device
445 * @v av Address vector
446 * @v iobuf I/O buffer
447 * @v rc Completion status code
449 void ib_complete_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp,
450 struct ib_address_vector *av,
451 struct io_buffer *iobuf, int rc ) {
453 if ( qp->recv.cq->op->complete_recv ) {
454 qp->recv.cq->op->complete_recv ( ibdev, qp, av, iobuf, rc );
462 * Refill receive work queue
464 * @v ibdev Infiniband device
467 void ib_refill_recv ( struct ib_device *ibdev, struct ib_queue_pair *qp ) {
468 struct io_buffer *iobuf;
471 /* Keep filling while unfilled entries remain */
472 while ( qp->recv.fill < qp->recv.num_wqes ) {
474 /* Allocate I/O buffer */
475 iobuf = alloc_iob ( IB_MAX_PAYLOAD_SIZE );
477 /* Non-fatal; we will refill on next attempt */
481 /* Post I/O buffer */
482 if ( ( rc = ib_post_recv ( ibdev, qp, iobuf ) ) != 0 ) {
483 DBGC ( ibdev, "IBDEV %p could not refill: %s\n",
484 ibdev, strerror ( rc ) );
492 /***************************************************************************
496 ***************************************************************************
502 * @v ibdev Infiniband device
503 * @ret rc Return status code
505 int ib_open ( struct ib_device *ibdev ) {
508 /* Increment device open request counter */
509 if ( ibdev->open_count++ > 0 ) {
510 /* Device was already open; do nothing */
514 /* Create subnet management agent */
515 ibdev->sma = ib_create_gma ( ibdev, IB_QPT_SMA );
516 if ( ! ibdev->sma ) {
517 DBGC ( ibdev, "IBDEV %p could not create SMA\n", ibdev );
522 /* Create general management agent */
523 ibdev->gma = ib_create_gma ( ibdev, IB_QPT_GMA );
524 if ( ! ibdev->gma ) {
525 DBGC ( ibdev, "IBDEV %p could not create GMA\n", ibdev );
531 if ( ( rc = ibdev->op->open ( ibdev ) ) != 0 ) {
532 DBGC ( ibdev, "IBDEV %p could not open: %s\n",
533 ibdev, strerror ( rc ) );
537 assert ( ibdev->open_count == 1 );
540 ibdev->op->close ( ibdev );
542 ib_destroy_gma ( ibdev->gma );
544 ib_destroy_gma ( ibdev->sma );
546 assert ( ibdev->open_count == 1 );
547 ibdev->open_count = 0;
554 * @v ibdev Infiniband device
556 void ib_close ( struct ib_device *ibdev ) {
558 /* Decrement device open request counter */
561 /* Close device if this was the last remaining requested opening */
562 if ( ibdev->open_count == 0 ) {
563 ib_destroy_gma ( ibdev->gma );
564 ib_destroy_gma ( ibdev->sma );
565 ibdev->op->close ( ibdev );
569 /***************************************************************************
573 ***************************************************************************
577 * Attach to multicast group
579 * @v ibdev Infiniband device
581 * @v gid Multicast GID
582 * @ret rc Return status code
584 * Note that this function handles only the local device's attachment
585 * to the multicast GID; it does not issue the relevant MADs to join
586 * the multicast group on the subnet.
588 int ib_mcast_attach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
589 struct ib_gid *gid ) {
590 struct ib_multicast_gid *mgid;
593 /* Add to software multicast GID list */
594 mgid = zalloc ( sizeof ( *mgid ) );
599 memcpy ( &mgid->gid, gid, sizeof ( mgid->gid ) );
600 list_add ( &mgid->list, &qp->mgids );
602 /* Add to hardware multicast GID list */
603 if ( ( rc = ibdev->op->mcast_attach ( ibdev, qp, gid ) ) != 0 )
604 goto err_dev_mcast_attach;
608 err_dev_mcast_attach:
609 list_del ( &mgid->list );
616 * Detach from multicast group
618 * @v ibdev Infiniband device
620 * @v gid Multicast GID
622 void ib_mcast_detach ( struct ib_device *ibdev, struct ib_queue_pair *qp,
623 struct ib_gid *gid ) {
624 struct ib_multicast_gid *mgid;
626 /* Remove from hardware multicast GID list */
627 ibdev->op->mcast_detach ( ibdev, qp, gid );
629 /* Remove from software multicast GID list */
630 list_for_each_entry ( mgid, &qp->mgids, list ) {
631 if ( memcmp ( &mgid->gid, gid, sizeof ( mgid->gid ) ) == 0 ) {
632 list_del ( &mgid->list );
639 /***************************************************************************
643 ***************************************************************************
647 * Get Infiniband HCA information
649 * @v ibdev Infiniband device
650 * @ret hca_guid HCA GUID
651 * @ret num_ports Number of ports
653 int ib_get_hca_info ( struct ib_device *ibdev,
654 struct ib_gid_half *hca_guid ) {
655 struct ib_device *tmp;
658 /* Search for IB devices with the same physical device to
659 * identify port count and a suitable Node GUID.
661 for_each_ibdev ( tmp ) {
662 if ( tmp->dev != ibdev->dev )
664 if ( num_ports == 0 ) {
665 memcpy ( hca_guid, &tmp->gid.u.half[1],
666 sizeof ( *hca_guid ) );
673 /** Set port information
675 * @v ibdev Infiniband device
676 * @v port_info New port information
678 int ib_set_port_info ( struct ib_device *ibdev,
679 const struct ib_port_info *port_info ) {
682 /* Adapters with embedded SMAs do not need to support this method */
683 if ( ! ibdev->op->set_port_info ) {
684 DBGC ( ibdev, "IBDEV %p does not support setting port "
685 "information\n", ibdev );
689 if ( ( rc = ibdev->op->set_port_info ( ibdev, port_info ) ) != 0 ) {
690 DBGC ( ibdev, "IBDEV %p could not set port information: %s\n",
691 ibdev, strerror ( rc ) );
698 /***************************************************************************
702 ***************************************************************************
706 * Handle Infiniband link state change
708 * @v ibdev Infiniband device
710 void ib_link_state_changed ( struct ib_device *ibdev ) {
712 /* Notify IPoIB of link state change */
713 ipoib_link_state_changed ( ibdev );
719 * @v ibdev Infiniband device
721 void ib_poll_eq ( struct ib_device *ibdev ) {
722 struct ib_completion_queue *cq;
724 /* Poll device's event queue */
725 ibdev->op->poll_eq ( ibdev );
727 /* Poll all completion queues */
728 list_for_each_entry ( cq, &ibdev->cqs, list )
729 ib_poll_cq ( ibdev, cq );
733 * Single-step the Infiniband event queue
735 * @v process Infiniband event queue process
737 static void ib_step ( struct process *process __unused ) {
738 struct ib_device *ibdev;
740 for_each_ibdev ( ibdev )
741 ib_poll_eq ( ibdev );
744 /** Infiniband event queue process */
745 struct process ib_process __permanent_process = {
749 /***************************************************************************
751 * Infiniband device creation/destruction
753 ***************************************************************************
757 * Allocate Infiniband device
759 * @v priv_size Size of driver private data area
760 * @ret ibdev Infiniband device, or NULL
762 struct ib_device * alloc_ibdev ( size_t priv_size ) {
763 struct ib_device *ibdev;
767 total_len = ( sizeof ( *ibdev ) + priv_size );
768 ibdev = zalloc ( total_len );
770 drv_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
771 ib_set_drvdata ( ibdev, drv_priv );
772 INIT_LIST_HEAD ( &ibdev->cqs );
773 INIT_LIST_HEAD ( &ibdev->qps );
774 ibdev->lid = IB_LID_NONE;
775 ibdev->pkey = IB_PKEY_NONE;
781 * Register Infiniband device
783 * @v ibdev Infiniband device
784 * @ret rc Return status code
786 int register_ibdev ( struct ib_device *ibdev ) {
789 /* Add to device list */
791 list_add_tail ( &ibdev->list, &ib_devices );
793 /* Add IPoIB device */
794 if ( ( rc = ipoib_probe ( ibdev ) ) != 0 ) {
795 DBGC ( ibdev, "IBDEV %p could not add IPoIB device: %s\n",
796 ibdev, strerror ( rc ) );
797 goto err_ipoib_probe;
800 DBGC ( ibdev, "IBDEV %p registered (phys %s)\n", ibdev,
805 list_del ( &ibdev->list );
811 * Unregister Infiniband device
813 * @v ibdev Infiniband device
815 void unregister_ibdev ( struct ib_device *ibdev ) {
818 ipoib_remove ( ibdev );
820 /* Remove from device list */
821 list_del ( &ibdev->list );
823 DBGC ( ibdev, "IBDEV %p unregistered\n", ibdev );