iPXE
ib_cmrc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 Fen Systems Ltd <mbrown@fensystems.co.uk>.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  *   Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  *
00012  *   Redistributions in binary form must reproduce the above copyright
00013  *   notice, this list of conditions and the following disclaimer in
00014  *   the documentation and/or other materials provided with the
00015  *   distribution.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00021  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00022  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00026  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00028  * OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 FILE_LICENCE ( BSD2 );
00032 
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <errno.h>
00036 #include <ipxe/iobuf.h>
00037 #include <ipxe/xfer.h>
00038 #include <ipxe/process.h>
00039 #include <ipxe/infiniband.h>
00040 #include <ipxe/ib_cm.h>
00041 #include <ipxe/ib_cmrc.h>
00042 
00043 /**
00044  * @file
00045  *
00046  * Infiniband Communication-managed Reliable Connections
00047  *
00048  */
00049 
00050 /** CMRC number of send WQEs
00051  *
00052  * This is a policy decision.
00053  */
00054 #define IB_CMRC_NUM_SEND_WQES 4
00055 
00056 /** CMRC number of receive WQEs
00057  *
00058  * This is a policy decision.
00059  */
00060 #define IB_CMRC_NUM_RECV_WQES 2
00061 
00062 /** CMRC number of completion queue entries
00063  *
00064  * This is a policy decision
00065  */
00066 #define IB_CMRC_NUM_CQES 8
00067 
00068 /** An Infiniband Communication-Managed Reliable Connection */
00069 struct ib_cmrc_connection {
00070         /** Reference count */
00071         struct refcnt refcnt;
00072         /** Name */
00073         const char *name;
00074         /** Data transfer interface */
00075         struct interface xfer;
00076         /** Infiniband device */
00077         struct ib_device *ibdev;
00078         /** Completion queue */
00079         struct ib_completion_queue *cq;
00080         /** Queue pair */
00081         struct ib_queue_pair *qp;
00082         /** Connection */
00083         struct ib_connection *conn;
00084         /** Destination GID */
00085         union ib_gid dgid;
00086         /** Service ID */
00087         union ib_guid service_id;
00088         /** QP is connected */
00089         int connected;
00090         /** Shutdown process */
00091         struct process shutdown;
00092 };
00093 
00094 /**
00095  * Shut down CMRC connection gracefully
00096  *
00097  * @v cmrc              Communication-Managed Reliable Connection
00098  *
00099  * The Infiniband data structures are not reference-counted or
00100  * guarded.  It is therefore unsafe to shut them down while we may be
00101  * in the middle of a callback from the Infiniband stack (e.g. in a
00102  * receive completion handler).
00103  *
00104  * This shutdown process will run some time after the call to
00105  * ib_cmrc_close(), after control has returned out of the Infiniband
00106  * core, and will shut down the Infiniband interfaces cleanly.
00107  *
00108  * The shutdown process holds an implicit reference on the CMRC
00109  * connection, ensuring that the structure is not freed before the
00110  * shutdown process has run.
00111  */
00112 static void ib_cmrc_shutdown ( struct ib_cmrc_connection *cmrc ) {
00113         struct ib_device *ibdev = cmrc->ibdev;
00114 
00115         DBGC ( cmrc, "CMRC %s %s shutting down\n",
00116                ibdev->name, cmrc->name );
00117 
00118         /* Shut down Infiniband interface */
00119         ib_destroy_conn ( ibdev, cmrc->qp, cmrc->conn );
00120         ib_destroy_qp ( ibdev, cmrc->qp );
00121         ib_destroy_cq ( ibdev, cmrc->cq );
00122         ib_close ( ibdev );
00123 
00124         /* Cancel any pending shutdown */
00125         process_del ( &cmrc->shutdown );
00126 
00127         /* Drop the remaining reference */
00128         ref_put ( &cmrc->refcnt );
00129 }
00130 
00131 /**
00132  * Close CMRC connection
00133  *
00134  * @v cmrc              Communication-Managed Reliable Connection
00135  * @v rc                Reason for close
00136  */
00137 static void ib_cmrc_close ( struct ib_cmrc_connection *cmrc, int rc ) {
00138 
00139         /* Close data transfer interface */
00140         intf_shutdown ( &cmrc->xfer, rc );
00141 
00142         /* Schedule shutdown process */
00143         process_add ( &cmrc->shutdown );
00144 }
00145 
00146 /**
00147  * Handle change of CMRC connection status
00148  *
00149  * @v ibdev             Infiniband device
00150  * @v qp                Queue pair
00151  * @v conn              Connection
00152  * @v rc_cm             Connection status code
00153  * @v private_data      Private data, if available
00154  * @v private_data_len  Length of private data
00155  */
00156 static void ib_cmrc_changed ( struct ib_device *ibdev,
00157                               struct ib_queue_pair *qp,
00158                               struct ib_connection *conn __unused, int rc_cm,
00159                               void *private_data, size_t private_data_len ) {
00160         struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
00161         int rc_xfer;
00162 
00163         /* Record connection status */
00164         if ( rc_cm == 0 ) {
00165                 DBGC ( cmrc, "CMRC %s %s connected\n",
00166                        ibdev->name, cmrc->name );
00167                 cmrc->connected = 1;
00168         } else {
00169                 DBGC ( cmrc, "CMRC %s %s disconnected: %s\n",
00170                        ibdev->name, cmrc->name, strerror ( rc_cm ) );
00171                 cmrc->connected = 0;
00172         }
00173 
00174         /* Pass up any private data */
00175         DBGC2 ( cmrc, "CMRC %s %s received private data:\n",
00176                 ibdev->name, cmrc->name );
00177         DBGC2_HDA ( cmrc, 0, private_data, private_data_len );
00178         if ( private_data &&
00179              ( rc_xfer = xfer_deliver_raw ( &cmrc->xfer, private_data,
00180                                             private_data_len ) ) != 0 ) {
00181                 DBGC ( cmrc, "CMRC %s %s could not deliver private data: %s\n",
00182                        ibdev->name, cmrc->name, strerror ( rc_xfer ) );
00183                 ib_cmrc_close ( cmrc, rc_xfer );
00184                 return;
00185         }
00186 
00187         /* Notify upper connection of window change */
00188         xfer_window_changed ( &cmrc->xfer );
00189 
00190         /* If we are disconnected, close the upper connection */
00191         if ( rc_cm != 0 ) {
00192                 ib_cmrc_close ( cmrc, rc_cm );
00193                 return;
00194         }
00195 }
00196 
00197 /** CMRC connection operations */
00198 static struct ib_connection_operations ib_cmrc_conn_op = {
00199         .changed = ib_cmrc_changed,
00200 };
00201 
00202 /**
00203  * Handle CMRC send completion
00204  *
00205  * @v ibdev             Infiniband device
00206  * @v qp                Queue pair
00207  * @v iobuf             I/O buffer
00208  * @v rc                Completion status code
00209  */
00210 static void ib_cmrc_complete_send ( struct ib_device *ibdev,
00211                                     struct ib_queue_pair *qp,
00212                                     struct io_buffer *iobuf, int rc ) {
00213         struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
00214 
00215         /* Free the completed I/O buffer */
00216         free_iob ( iobuf );
00217 
00218         /* Close the connection on any send errors */
00219         if ( rc != 0 ) {
00220                 DBGC ( cmrc, "CMRC %s %s send error: %s\n",
00221                        ibdev->name, cmrc->name, strerror ( rc ) );
00222                 ib_cmrc_close ( cmrc, rc );
00223                 return;
00224         }
00225 }
00226 
00227 /**
00228  * Handle CMRC receive completion
00229  *
00230  * @v ibdev             Infiniband device
00231  * @v qp                Queue pair
00232  * @v dest              Destination address vector, or NULL
00233  * @v source            Source address vector, or NULL
00234  * @v iobuf             I/O buffer
00235  * @v rc                Completion status code
00236  */
00237 static void ib_cmrc_complete_recv ( struct ib_device *ibdev,
00238                                     struct ib_queue_pair *qp,
00239                                     struct ib_address_vector *dest __unused,
00240                                     struct ib_address_vector *source __unused,
00241                                     struct io_buffer *iobuf, int rc ) {
00242         struct ib_cmrc_connection *cmrc = ib_qp_get_ownerdata ( qp );
00243 
00244         /* Close the connection on any receive errors */
00245         if ( rc != 0 ) {
00246                 DBGC ( cmrc, "CMRC %s %s receive error: %s\n",
00247                        ibdev->name, cmrc->name, strerror ( rc ) );
00248                 free_iob ( iobuf );
00249                 ib_cmrc_close ( cmrc, rc );
00250                 return;
00251         }
00252 
00253         DBGC2 ( cmrc, "CMRC %s %s received:\n", ibdev->name, cmrc->name );
00254         DBGC2_HDA ( cmrc, 0, iobuf->data, iob_len ( iobuf ) );
00255 
00256         /* Pass up data */
00257         if ( ( rc = xfer_deliver_iob ( &cmrc->xfer, iobuf ) ) != 0 ) {
00258                 DBGC ( cmrc, "CMRC %s %s could not deliver data: %s\n",
00259                        ibdev->name, cmrc->name, strerror ( rc ) );
00260                 ib_cmrc_close ( cmrc, rc );
00261                 return;
00262         }
00263 }
00264 
00265 /** Infiniband CMRC completion operations */
00266 static struct ib_completion_queue_operations ib_cmrc_completion_ops = {
00267         .complete_send = ib_cmrc_complete_send,
00268         .complete_recv = ib_cmrc_complete_recv,
00269 };
00270 
00271 /** Infiniband CMRC queue pair operations */
00272 static struct ib_queue_pair_operations ib_cmrc_queue_pair_ops = {
00273         .alloc_iob = alloc_iob,
00274 };
00275 
00276 /**
00277  * Send data via CMRC
00278  *
00279  * @v cmrc              CMRC connection
00280  * @v iobuf             Datagram I/O buffer
00281  * @v meta              Data transfer metadata
00282  * @ret rc              Return status code
00283  */
00284 static int ib_cmrc_xfer_deliver ( struct ib_cmrc_connection *cmrc,
00285                                   struct io_buffer *iobuf,
00286                                   struct xfer_metadata *meta __unused ) {
00287         struct ib_device *ibdev = cmrc->ibdev;
00288         int rc;
00289 
00290         /* If no connection has yet been attempted, send this datagram
00291          * as the CM REQ private data.  Otherwise, send it via the QP.
00292          */
00293         if ( ! cmrc->connected ) {
00294 
00295                 /* Abort if we have already sent a CM connection request */
00296                 if ( cmrc->conn ) {
00297                         DBGC ( cmrc, "CMRC %s %s attempt to send before "
00298                                "connection is complete\n",
00299                                ibdev->name, cmrc->name );
00300                         rc = -EIO;
00301                         goto out;
00302                 }
00303 
00304                 /* Send via CM connection request */
00305                 cmrc->conn = ib_create_conn ( cmrc->ibdev, cmrc->qp,
00306                                               &cmrc->dgid, &cmrc->service_id,
00307                                               iobuf->data, iob_len ( iobuf ),
00308                                               &ib_cmrc_conn_op );
00309                 if ( ! cmrc->conn ) {
00310                         DBGC ( cmrc, "CMRC %s %s could not connect\n",
00311                                ibdev->name, cmrc->name );
00312                         rc = -ENOMEM;
00313                         goto out;
00314                 }
00315                 DBGC ( cmrc, "CMRC %s %s using CM %08x\n",
00316                        ibdev->name, cmrc->name, cmrc->conn->local_id );
00317 
00318         } else {
00319 
00320                 /* Send via QP */
00321                 if ( ( rc = ib_post_send ( cmrc->ibdev, cmrc->qp, NULL,
00322                                            iob_disown ( iobuf ) ) ) != 0 ) {
00323                         DBGC ( cmrc, "CMRC %s %s could not send: %s\n",
00324                                ibdev->name, cmrc->name, strerror ( rc ) );
00325                         goto out;
00326                 }
00327 
00328         }
00329         return 0;
00330 
00331  out:
00332         /* Free the I/O buffer if necessary */
00333         free_iob ( iobuf );
00334 
00335         /* Close the connection on any errors */
00336         if ( rc != 0 )
00337                 ib_cmrc_close ( cmrc, rc );
00338 
00339         return rc;
00340 }
00341 
00342 /**
00343  * Check CMRC flow control window
00344  *
00345  * @v cmrc              CMRC connection
00346  * @ret len             Length of window
00347  */
00348 static size_t ib_cmrc_xfer_window ( struct ib_cmrc_connection *cmrc ) {
00349 
00350         /* We indicate a window only when we are successfully
00351          * connected.
00352          */
00353         return ( cmrc->connected ? IB_MAX_PAYLOAD_SIZE : 0 );
00354 }
00355 
00356 /**
00357  * Identify device underlying CMRC connection
00358  *
00359  * @v cmrc              CMRC connection
00360  * @ret device          Underlying device
00361  */
00362 static struct device *
00363 ib_cmrc_identify_device ( struct ib_cmrc_connection *cmrc ) {
00364         return cmrc->ibdev->dev;
00365 }
00366 
00367 /** CMRC data transfer interface operations */
00368 static struct interface_operation ib_cmrc_xfer_operations[] = {
00369         INTF_OP ( xfer_deliver, struct ib_cmrc_connection *,
00370                   ib_cmrc_xfer_deliver ),
00371         INTF_OP ( xfer_window, struct ib_cmrc_connection *,
00372                   ib_cmrc_xfer_window ),
00373         INTF_OP ( intf_close, struct ib_cmrc_connection *, ib_cmrc_close ),
00374         INTF_OP ( identify_device, struct ib_cmrc_connection *,
00375                   ib_cmrc_identify_device ),
00376 };
00377 
00378 /** CMRC data transfer interface descriptor */
00379 static struct interface_descriptor ib_cmrc_xfer_desc =
00380         INTF_DESC ( struct ib_cmrc_connection, xfer, ib_cmrc_xfer_operations );
00381 
00382 /** CMRC shutdown process descriptor */
00383 static struct process_descriptor ib_cmrc_shutdown_desc =
00384         PROC_DESC_ONCE ( struct ib_cmrc_connection, shutdown,
00385                          ib_cmrc_shutdown );
00386 
00387 /**
00388  * Open CMRC connection
00389  *
00390  * @v xfer              Data transfer interface
00391  * @v ibdev             Infiniband device
00392  * @v dgid              Destination GID
00393  * @v service_id        Service ID
00394  * @v name              Connection name
00395  * @ret rc              Returns status code
00396  */
00397 int ib_cmrc_open ( struct interface *xfer, struct ib_device *ibdev,
00398                    union ib_gid *dgid, union ib_guid *service_id,
00399                    const char *name ) {
00400         struct ib_cmrc_connection *cmrc;
00401         int rc;
00402 
00403         /* Allocate and initialise structure */
00404         cmrc = zalloc ( sizeof ( *cmrc ) );
00405         if ( ! cmrc ) {
00406                 rc = -ENOMEM;
00407                 goto err_alloc;
00408         }
00409         ref_init ( &cmrc->refcnt, NULL );
00410         cmrc->name = name;
00411         intf_init ( &cmrc->xfer, &ib_cmrc_xfer_desc, &cmrc->refcnt );
00412         cmrc->ibdev = ibdev;
00413         memcpy ( &cmrc->dgid, dgid, sizeof ( cmrc->dgid ) );
00414         memcpy ( &cmrc->service_id, service_id, sizeof ( cmrc->service_id ) );
00415         process_init_stopped ( &cmrc->shutdown, &ib_cmrc_shutdown_desc,
00416                                &cmrc->refcnt );
00417 
00418         /* Open Infiniband device */
00419         if ( ( rc = ib_open ( ibdev ) ) != 0 ) {
00420                 DBGC ( cmrc, "CMRC %s %s could not open device: %s\n",
00421                        ibdev->name, cmrc->name, strerror ( rc ) );
00422                 goto err_open;
00423         }
00424 
00425         /* Create completion queue */
00426         if ( ( rc = ib_create_cq ( ibdev, IB_CMRC_NUM_CQES,
00427                                    &ib_cmrc_completion_ops, &cmrc->cq ) ) != 0){
00428                 DBGC ( cmrc, "CMRC %s %s could not create completion queue: "
00429                        "%s\n", ibdev->name, cmrc->name, strerror ( rc ) );
00430                 goto err_create_cq;
00431         }
00432 
00433         /* Create queue pair */
00434         if ( ( rc = ib_create_qp ( ibdev, IB_QPT_RC, IB_CMRC_NUM_SEND_WQES,
00435                                    cmrc->cq, IB_CMRC_NUM_RECV_WQES, cmrc->cq,
00436                                    &ib_cmrc_queue_pair_ops, name,
00437                                    &cmrc->qp ) ) != 0 ) {
00438                 DBGC ( cmrc, "CMRC %s %s could not create queue pair: %s\n",
00439                        ibdev->name, cmrc->name, strerror ( rc ) );
00440                 goto err_create_qp;
00441         }
00442         ib_qp_set_ownerdata ( cmrc->qp, cmrc );
00443         DBGC ( cmrc, "CMRC %s %s using QPN %#lx\n",
00444                ibdev->name, cmrc->name, cmrc->qp->qpn );
00445 
00446         /* Attach to parent interface, transfer reference (implicitly)
00447          * to our shutdown process, and return.
00448          */
00449         intf_plug_plug ( &cmrc->xfer, xfer );
00450         return 0;
00451 
00452         ib_destroy_qp ( ibdev, cmrc->qp );
00453  err_create_qp:
00454         ib_destroy_cq ( ibdev, cmrc->cq );
00455  err_create_cq:
00456         ib_close ( ibdev );
00457  err_open:
00458         ref_put ( &cmrc->refcnt );
00459  err_alloc:
00460         return rc;
00461 }