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.
26 #include <gpxe/list.h>
27 #include <gpxe/if_arp.h>
28 #include <gpxe/netdevice.h>
29 #include <gpxe/iobuf.h>
30 #include <gpxe/infiniband.h>
39 * Create completion queue
41 * @v ibdev Infiniband device
42 * @v num_cqes Number of completion queue entries
43 * @ret cq New completion queue
45 struct ib_completion_queue * ib_create_cq ( struct ib_device *ibdev,
46 unsigned int num_cqes ) {
47 struct ib_completion_queue *cq;
50 DBGC ( ibdev, "IBDEV %p creating completion queue\n", ibdev );
52 /* Allocate and initialise data structure */
53 cq = zalloc ( sizeof ( *cq ) );
56 cq->num_cqes = num_cqes;
57 INIT_LIST_HEAD ( &cq->work_queues );
59 /* Perform device-specific initialisation and get CQN */
60 if ( ( rc = ibdev->op->create_cq ( ibdev, cq ) ) != 0 ) {
61 DBGC ( ibdev, "IBDEV %p could not initialise completion "
62 "queue: %s\n", ibdev, strerror ( rc ) );
67 DBGC ( ibdev, "IBDEV %p created %d-entry completion queue %p (%p) "
68 "with CQN %#lx\n", ibdev, num_cqes, cq, cq->dev_priv, cq->cqn );
73 * Destroy completion queue
75 * @v ibdev Infiniband device
76 * @v cq Completion queue
78 void ib_destroy_cq ( struct ib_device *ibdev,
79 struct ib_completion_queue *cq ) {
80 DBGC ( ibdev, "IBDEV %p destroying completion queue %#lx\n",
82 assert ( list_empty ( &cq->work_queues ) );
83 ibdev->op->destroy_cq ( ibdev, cq );
90 * @v ibdev Infiniband device
91 * @v num_send_wqes Number of send work queue entries
92 * @v send_cq Send completion queue
93 * @v num_recv_wqes Number of receive work queue entries
94 * @v recv_cq Receive completion queue
98 struct ib_queue_pair * ib_create_qp ( struct ib_device *ibdev,
99 unsigned int num_send_wqes,
100 struct ib_completion_queue *send_cq,
101 unsigned int num_recv_wqes,
102 struct ib_completion_queue *recv_cq,
103 unsigned long qkey ) {
104 struct ib_queue_pair *qp;
108 DBGC ( ibdev, "IBDEV %p creating queue pair\n", ibdev );
110 /* Allocate and initialise data structure */
111 total_size = ( sizeof ( *qp ) +
112 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ) +
113 ( num_recv_wqes * sizeof ( qp->recv.iobufs[0] ) ) );
114 qp = zalloc ( total_size );
119 qp->send.is_send = 1;
120 qp->send.cq = send_cq;
121 list_add ( &qp->send.list, &send_cq->work_queues );
122 qp->send.num_wqes = num_send_wqes;
123 qp->send.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) );
125 qp->recv.cq = recv_cq;
126 list_add ( &qp->recv.list, &recv_cq->work_queues );
127 qp->recv.num_wqes = num_recv_wqes;
128 qp->recv.iobufs = ( ( ( void * ) qp ) + sizeof ( *qp ) +
129 ( num_send_wqes * sizeof ( qp->send.iobufs[0] ) ));
131 /* Perform device-specific initialisation and get QPN */
132 if ( ( rc = ibdev->op->create_qp ( ibdev, qp ) ) != 0 ) {
133 DBGC ( ibdev, "IBDEV %p could not initialise queue pair: "
134 "%s\n", ibdev, strerror ( rc ) );
135 list_del ( &qp->send.list );
136 list_del ( &qp->recv.list );
141 DBGC ( ibdev, "IBDEV %p created queue pair %p (%p) with QPN %#lx\n",
142 ibdev, qp, qp->dev_priv, qp->qpn );
143 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d send entries at [%p,%p)\n",
144 ibdev, qp->qpn, num_send_wqes, qp->send.iobufs,
146 DBGC ( ibdev, "IBDEV %p QPN %#lx has %d receive entries at [%p,%p)\n",
147 ibdev, qp->qpn, num_send_wqes, qp->recv.iobufs,
148 ( ( ( void * ) qp ) + total_size ) );
155 * @v ibdev Infiniband device
158 void ib_destroy_qp ( struct ib_device *ibdev,
159 struct ib_queue_pair *qp ) {
160 DBGC ( ibdev, "IBDEV %p destroying queue pair %#lx\n",
162 ibdev->op->destroy_qp ( ibdev, qp );
163 list_del ( &qp->send.list );
164 list_del ( &qp->recv.list );
169 * Find work queue belonging to completion queue
171 * @v cq Completion queue
172 * @v qpn Queue pair number
173 * @v is_send Find send work queue (rather than receive)
174 * @ret wq Work queue, or NULL if not found
176 struct ib_work_queue * ib_find_wq ( struct ib_completion_queue *cq,
177 unsigned long qpn, int is_send ) {
178 struct ib_work_queue *wq;
180 list_for_each_entry ( wq, &cq->work_queues, list ) {
181 if ( ( wq->qp->qpn == qpn ) && ( wq->is_send == is_send ) )
188 * Allocate Infiniband device
190 * @v priv_size Size of private data area
191 * @ret ibdev Infiniband device, or NULL
193 struct ib_device * alloc_ibdev ( size_t priv_size ) {
194 struct ib_device *ibdev;
197 total_len = ( sizeof ( *ibdev ) + priv_size );
198 ibdev = zalloc ( total_len );
200 ibdev->dev_priv = ( ( ( void * ) ibdev ) + sizeof ( *ibdev ) );
206 * Free Infiniband device
208 * @v ibdev Infiniband device
210 void free_ibdev ( struct ib_device *ibdev ) {