iPXE
fcns.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 #include <byteswap.h>
00031 #include <ipxe/interface.h>
00032 #include <ipxe/iobuf.h>
00033 #include <ipxe/process.h>
00034 #include <ipxe/xfer.h>
00035 #include <ipxe/fc.h>
00036 #include <ipxe/fcns.h>
00037 
00038 /** @file
00039  *
00040  * Fibre Channel name server lookups
00041  *
00042  */
00043 
00044 /** A Fibre Channel name server query */
00045 struct fc_ns_query {
00046         /** Reference count */
00047         struct refcnt refcnt;
00048         /** Fibre Channel exchange */
00049         struct interface xchg;
00050 
00051         /** Fibre Channel peer */
00052         struct fc_peer *peer;
00053         /** Fibre Channel port */
00054         struct fc_port *port;
00055 
00056         /** Process */
00057         struct process process;
00058         /** Success handler
00059          *
00060          * @v peer              Fibre Channel peer
00061          * @v port              Fibre Channel port
00062          * @v peer_port_id      Peer port ID
00063          * @ret rc              Return status code
00064          */
00065         int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
00066                          struct fc_port_id *peer_port_id );
00067 };
00068 
00069 /**
00070  * Free name server query
00071  *
00072  * @v refcnt            Reference count
00073  */
00074 static void fc_ns_query_free ( struct refcnt *refcnt ) {
00075         struct fc_ns_query *query =
00076                 container_of ( refcnt, struct fc_ns_query, refcnt );
00077 
00078         fc_peer_put ( query->peer );
00079         fc_port_put ( query->port );
00080         free ( query );
00081 }
00082 
00083 /**
00084  * Close name server query
00085  *
00086  * @v query             Name server query
00087  * @v rc                Reason for close
00088  */
00089 static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {
00090 
00091         /* Stop process */
00092         process_del ( &query->process );
00093 
00094         /* Shut down interfaces */
00095         intf_shutdown ( &query->xchg, rc );
00096 }
00097 
00098 /**
00099  * Receive name server query response
00100  *
00101  * @v query             Name server query
00102  * @v iobuf             I/O buffer
00103  * @v meta              Data transfer metadata
00104  * @ret rc              Return status code
00105  */
00106 static int fc_ns_query_deliver ( struct fc_ns_query *query,
00107                                  struct io_buffer *iobuf,
00108                                  struct xfer_metadata *meta __unused ) {
00109         union fc_ns_response *resp = iobuf->data;
00110         struct fc_port_id *peer_port_id;
00111         int rc;
00112 
00113         /* Sanity check */
00114         if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
00115                 DBGC ( query, "FCNS %p received underlength response (%zd "
00116                        "bytes)\n", query, iob_len ( iobuf ) );
00117                 rc = -EINVAL;
00118                 goto done;
00119         }
00120 
00121         /* Handle response */
00122         switch ( ntohs ( resp->ct.code ) ) {
00123         case FC_GS_ACCEPT:
00124                 if ( iob_len ( iobuf ) < sizeof ( resp->gid_pn ) ) {
00125                         DBGC ( query, "FCNS %p received underlength accept "
00126                                "response (%zd bytes)\n",
00127                                query, iob_len ( iobuf ) );
00128                         rc = -EINVAL;
00129                         goto done;
00130                 }
00131                 peer_port_id = &resp->gid_pn.port_id.port_id;
00132                 DBGC ( query, "FCNS %p resolved %s to %s via %s\n",
00133                        query, fc_ntoa ( &query->peer->port_wwn ),
00134                        fc_id_ntoa ( peer_port_id ), query->port->name );
00135                 if ( ( rc = query->done ( query->peer, query->port,
00136                                           peer_port_id ) ) != 0 )
00137                         goto done;
00138                 break;
00139         case FC_GS_REJECT:
00140                 DBGC ( query, "FCNS %p rejected (reason %02x explanation "
00141                        "%02x)\n", query, resp->reject.ct.reason,
00142                        resp->reject.ct.explanation );
00143                 break;
00144         default:
00145                 DBGC ( query, "FCNS %p received invalid response code %04x\n",
00146                        query, ntohs ( resp->ct.code ) );
00147                 rc = -ENOTSUP;
00148                 goto done;
00149         }
00150 
00151         rc = 0;
00152  done:
00153         free_iob ( iobuf );
00154         fc_ns_query_close ( query, rc );
00155         return rc;
00156 }
00157 
00158 /**
00159  * Name server query process
00160  *
00161  * @v query             Name server query
00162  */
00163 static void fc_ns_query_step ( struct fc_ns_query *query ) {
00164         struct xfer_metadata meta;
00165         struct fc_ns_gid_pn_request gid_pn;
00166         int xchg_id;
00167         int rc;
00168 
00169         /* Create exchange */
00170         if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
00171                                              &fc_gs_port_id,
00172                                              FC_TYPE_CT ) ) < 0 ) {
00173                 rc = xchg_id;
00174                 DBGC ( query, "FCNS %p could not create exchange: %s\n",
00175                        query, strerror ( rc ) );
00176                 fc_ns_query_close ( query, rc );
00177                 return;
00178         }
00179 
00180         /* Construct query request */
00181         memset ( &gid_pn, 0, sizeof ( gid_pn ) );
00182         gid_pn.ct.revision = FC_CT_REVISION;
00183         gid_pn.ct.type = FC_GS_TYPE_DS;
00184         gid_pn.ct.subtype = FC_DS_SUBTYPE_NAME;
00185         gid_pn.ct.code = htons ( FC_NS_GET ( FC_NS_PORT_NAME, FC_NS_PORT_ID ));
00186         memcpy ( &gid_pn.port_wwn, &query->peer->port_wwn,
00187                  sizeof ( gid_pn.port_wwn ) );
00188         memset ( &meta, 0, sizeof ( meta ) );
00189         meta.flags = XFER_FL_OVER;
00190 
00191         /* Send query */
00192         if ( ( rc = xfer_deliver_raw_meta ( &query->xchg, &gid_pn,
00193                                             sizeof ( gid_pn ), &meta ) ) != 0){
00194                 DBGC ( query, "FCNS %p could not deliver query: %s\n",
00195                        query, strerror ( rc ) );
00196                 fc_ns_query_close ( query, rc );
00197                 return;
00198         }
00199 }
00200 
00201 /** Name server exchange interface operations */
00202 static struct interface_operation fc_ns_query_xchg_op[] = {
00203         INTF_OP ( xfer_deliver, struct fc_ns_query *, fc_ns_query_deliver ),
00204         INTF_OP ( intf_close, struct fc_ns_query *, fc_ns_query_close ),
00205 };
00206 
00207 /** Name server exchange interface descriptor */
00208 static struct interface_descriptor fc_ns_query_xchg_desc =
00209         INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );
00210 
00211 /** Name server process descriptor */
00212 static struct process_descriptor fc_ns_query_process_desc =
00213         PROC_DESC_ONCE ( struct fc_ns_query, process, fc_ns_query_step );
00214 
00215 /**
00216  * Issue Fibre Channel name server query
00217  *
00218  * @v peer              Fibre Channel peer
00219  * @v port              Fibre Channel port
00220  * @ret rc              Return status code
00221  */
00222 int fc_ns_query ( struct fc_peer *peer, struct fc_port *port,
00223                   int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
00224                                    struct fc_port_id *peer_port_id ) ) {
00225         struct fc_ns_query *query;
00226 
00227         /* Allocate and initialise structure */
00228         query = zalloc ( sizeof ( *query ) );
00229         if ( ! query )
00230                 return -ENOMEM;
00231         ref_init ( &query->refcnt, fc_ns_query_free );
00232         intf_init ( &query->xchg, &fc_ns_query_xchg_desc, &query->refcnt );
00233         process_init ( &query->process, &fc_ns_query_process_desc,
00234                        &query->refcnt );
00235         query->peer = fc_peer_get ( peer );
00236         query->port = fc_port_get ( port );
00237         query->done = done;
00238 
00239         DBGC ( query, "FCNS %p querying %s via %s\n",
00240                query, fc_ntoa ( &query->peer->port_wwn ), port->name );
00241 
00242         /* Mortalise self and return */
00243         ref_put ( &query->refcnt );
00244         return 0;
00245 }