iPXE
fc.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 <stdio.h>
00030 #include <errno.h>
00031 #include <assert.h>
00032 #include <byteswap.h>
00033 #include <ipxe/refcnt.h>
00034 #include <ipxe/list.h>
00035 #include <ipxe/tables.h>
00036 #include <ipxe/timer.h>
00037 #include <ipxe/retry.h>
00038 #include <ipxe/interface.h>
00039 #include <ipxe/xfer.h>
00040 #include <ipxe/iobuf.h>
00041 #include <ipxe/fc.h>
00042 #include <ipxe/fcels.h>
00043 #include <ipxe/fcns.h>
00044 
00045 /** @file
00046  *
00047  * Fibre Channel
00048  *
00049  */
00050 
00051 /** List of Fibre Channel ports */
00052 LIST_HEAD ( fc_ports );
00053 
00054 /** List of Fibre Channel peers */
00055 LIST_HEAD ( fc_peers );
00056 
00057 /******************************************************************************
00058  *
00059  * Well-known addresses
00060  *
00061  ******************************************************************************
00062  */
00063 
00064 /** Unassigned port ID */
00065 struct fc_port_id fc_empty_port_id = { .bytes = { 0x00, 0x00, 0x00 } };
00066 
00067 /** F_Port contoller port ID */
00068 struct fc_port_id fc_f_port_id = { .bytes = { 0xff, 0xff, 0xfe } };
00069 
00070 /** Generic services port ID */
00071 struct fc_port_id fc_gs_port_id = { .bytes = { 0xff, 0xff, 0xfc } };
00072 
00073 /** Point-to-point low port ID */
00074 struct fc_port_id fc_ptp_low_port_id = { .bytes = { 0x01, 0x01, 0x01 } };
00075 
00076 /** Point-to-point high port ID */
00077 struct fc_port_id fc_ptp_high_port_id = { .bytes = { 0x01, 0x01, 0x02 } };
00078 
00079 /******************************************************************************
00080  *
00081  * Utility functions
00082  *
00083  ******************************************************************************
00084  */
00085 
00086 /**
00087  * Format Fibre Channel port ID
00088  *
00089  * @v id                Fibre Channel port ID
00090  * @ret id_text         Port ID text
00091  */
00092 const char * fc_id_ntoa ( const struct fc_port_id *id ) {
00093         static char id_text[ FC_PORT_ID_STRLEN + 1 /* NUL */ ];
00094 
00095         snprintf ( id_text, sizeof ( id_text ), "%02x.%02x.%02x",
00096                    id->bytes[0], id->bytes[1], id->bytes[2] );
00097         return id_text;
00098 }
00099 
00100 /**
00101  * Parse Fibre Channel port ID
00102  *
00103  * @v id_text           Port ID text
00104  * @ret id              Fibre Channel port ID
00105  * @ret rc              Return status code
00106  */
00107 int fc_id_aton ( const char *id_text, struct fc_port_id *id ) {
00108         char *ptr = ( ( char * ) id_text );
00109         unsigned int i = 0;
00110 
00111         while ( 1 ) {
00112                 id->bytes[i++] = strtoul ( ptr, &ptr, 16 );
00113                 if ( i == sizeof ( id->bytes ) )
00114                         return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
00115                 if ( *ptr != '.' )
00116                         return -EINVAL;
00117                 ptr++;
00118         }
00119 }
00120 
00121 /**
00122  * Format Fibre Channel WWN
00123  *
00124  * @v wwn               Fibre Channel WWN
00125  * @ret wwn_text        WWN text
00126  */
00127 const char * fc_ntoa ( const struct fc_name *wwn ) {
00128         static char wwn_text[ FC_NAME_STRLEN + 1 /* NUL */ ];
00129 
00130         snprintf ( wwn_text, sizeof ( wwn_text ),
00131                    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
00132                    wwn->bytes[0], wwn->bytes[1], wwn->bytes[2], wwn->bytes[3],
00133                    wwn->bytes[4], wwn->bytes[5], wwn->bytes[6], wwn->bytes[7] );
00134         return wwn_text;
00135 }
00136 
00137 /**
00138  * Parse Fibre Channel WWN
00139  *
00140  * @v wwn_text          WWN text
00141  * @ret wwn             Fibre Channel WWN
00142  * @ret rc              Return status code
00143  */
00144 int fc_aton ( const char *wwn_text, struct fc_name *wwn ) {
00145         char *ptr = ( ( char * ) wwn_text );
00146         unsigned int i = 0;
00147 
00148         while ( 1 ) {
00149                 wwn->bytes[i++] = strtoul ( ptr, &ptr, 16 );
00150                 if ( i == sizeof ( wwn->bytes ) )
00151                         return ( ( *ptr == '\0' ) ? 0 : -EINVAL );
00152                 if ( *ptr != ':' )
00153                         return -EINVAL;
00154                 ptr++;
00155         }
00156 }
00157 
00158 /**
00159  * Fill Fibre Channel socket address
00160  *
00161  * @v sa_fc             Fibre Channel socket address to fill in
00162  * @v id                Fibre Channel port ID
00163  * @ret sa              Socket address
00164  */
00165 struct sockaddr * fc_fill_sockaddr ( struct sockaddr_fc *sa_fc,
00166                                      struct fc_port_id *id ) {
00167         union {
00168                 struct sockaddr sa;
00169                 struct sockaddr_fc fc;
00170         } *u = container_of ( sa_fc, typeof ( *u ), fc );
00171 
00172         memset ( sa_fc, 0, sizeof ( *sa_fc ) );
00173         sa_fc->sfc_family = AF_FC;
00174         memcpy ( &sa_fc->sfc_port_id, id, sizeof ( sa_fc->sfc_port_id ) );
00175         return &u->sa;
00176 }
00177 
00178 /******************************************************************************
00179  *
00180  * Fibre Channel link state
00181  *
00182  ******************************************************************************
00183  */
00184 
00185 /** Default link status code */
00186 #define EUNKNOWN_LINK_STATUS __einfo_error ( EINFO_EUNKNOWN_LINK_STATUS )
00187 #define EINFO_EUNKNOWN_LINK_STATUS \
00188         __einfo_uniqify ( EINFO_EINPROGRESS, 0x01, "Unknown" )
00189 
00190 /**
00191  * Mark Fibre Channel link as up
00192  *
00193  * @v link              Fibre Channel link state monitor
00194  */
00195 static void fc_link_up ( struct fc_link_state *link ) {
00196 
00197         /* Stop retry timer */
00198         stop_timer ( &link->timer );
00199 
00200         /* Record link state */
00201         link->rc = 0;
00202 }
00203 
00204 /**
00205  * Mark Fibre Channel link as down
00206  *
00207  * @v link              Fibre Channel link state monitor
00208  * @v rc                Link state
00209  */
00210 static void fc_link_err ( struct fc_link_state *link, int rc ) {
00211 
00212         /* Record link state */
00213         if ( rc == 0 )
00214                 rc = -EUNKNOWN_LINK_STATUS;
00215         link->rc = rc;
00216 
00217         /* Schedule another link examination */
00218         start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );
00219 }
00220 
00221 /**
00222  * Examine Fibre Channel link state
00223  *
00224  * @v link              Fibre Channel link state monitor
00225  */
00226 static void fc_link_examine ( struct fc_link_state *link ) {
00227 
00228         link->examine ( link );
00229 }
00230 
00231 /**
00232  * Handle Fibre Channel link retry timer expiry
00233  */
00234 static void fc_link_expired ( struct retry_timer *timer, int over __unused ) {
00235         struct fc_link_state *link =
00236                 container_of ( timer, struct fc_link_state, timer );
00237 
00238         /* Schedule another link examination */
00239         start_timer_fixed ( &link->timer, FC_LINK_RETRY_DELAY );
00240 
00241         /* Examine link */
00242         fc_link_examine ( link );
00243 }
00244 
00245 /**
00246  * Initialise Fibre Channel link state monitor
00247  *
00248  * @v link              Fibre Channel link state monitor
00249  * @v examine           Examine link state method
00250  * @v refcnt            Reference counter
00251  */
00252 static void fc_link_init ( struct fc_link_state *link,
00253                            void ( * examine ) ( struct fc_link_state *link ),
00254                            struct refcnt *refcnt ) {
00255 
00256         link->rc = -EUNKNOWN_LINK_STATUS;
00257         timer_init ( &link->timer, fc_link_expired, refcnt );
00258         link->examine = examine;
00259 }
00260 
00261 /**
00262  * Start monitoring Fibre Channel link state
00263  *
00264  * @v link              Fibre Channel link state monitor
00265  */
00266 static void fc_link_start ( struct fc_link_state *link ) {
00267         start_timer_nodelay ( &link->timer );
00268 }
00269 
00270 /**
00271  * Stop monitoring Fibre Channel link state
00272  *
00273  * @v link              Fibre Channel link state monitor
00274  */
00275 static void fc_link_stop ( struct fc_link_state *link ) {
00276         stop_timer ( &link->timer );
00277 }
00278 
00279 /******************************************************************************
00280  *
00281  * Fibre Channel exchanges
00282  *
00283  ******************************************************************************
00284  */
00285 
00286 /** A Fibre Channel exchange */
00287 struct fc_exchange {
00288         /** Reference count */
00289         struct refcnt refcnt;
00290         /** Fibre Channel port */
00291         struct fc_port *port;
00292         /** List of active exchanges within this port */
00293         struct list_head list;
00294 
00295         /** Peer port ID */
00296         struct fc_port_id peer_port_id;
00297         /** Data structure type */
00298         unsigned int type;
00299         /** Flags */
00300         unsigned int flags;
00301         /** Local exchange ID */
00302         uint16_t xchg_id;
00303         /** Peer exchange ID */
00304         uint16_t peer_xchg_id;
00305         /** Active sequence ID */
00306         uint8_t seq_id;
00307         /** Active sequence count */
00308         uint16_t seq_cnt;
00309 
00310         /** Timeout timer */
00311         struct retry_timer timer;
00312 
00313         /** Upper-layer protocol interface */
00314         struct interface ulp;
00315 };
00316 
00317 /** Fibre Channel exchange flags */
00318 enum fc_exchange_flags {
00319         /** We are the exchange originator */
00320         FC_XCHG_ORIGINATOR = 0x0001,
00321         /** We have the sequence initiative */
00322         FC_XCHG_SEQ_INITIATIVE = 0x0002,
00323         /** This is the first sequence of the exchange */
00324         FC_XCHG_SEQ_FIRST = 0x0004,
00325 };
00326 
00327 /** Fibre Channel timeout */
00328 #define FC_TIMEOUT ( 1 * TICKS_PER_SEC )
00329 
00330 /**
00331  * Create local Fibre Channel exchange identifier
00332  *
00333  * @ret xchg_id         Local exchange ID
00334  */
00335 static unsigned int fc_new_xchg_id ( void ) {
00336         static uint16_t next_id = 0x0000;
00337 
00338         /* We must avoid using FC_RX_ID_UNKNOWN (0xffff) */
00339         next_id += 2;
00340         return next_id;
00341 }
00342 
00343 /**
00344  * Create local Fibre Channel sequence identifier
00345  *
00346  * @ret seq_id          Local sequence identifier
00347  */
00348 static unsigned int fc_new_seq_id ( void ) {
00349         static uint8_t seq_id = 0x00;
00350 
00351         return (++seq_id);
00352 }
00353 
00354 /**
00355  * Free Fibre Channel exchange
00356  *
00357  * @v refcnt            Reference count
00358  */
00359 static void fc_xchg_free ( struct refcnt *refcnt ) {
00360         struct fc_exchange *xchg =
00361                 container_of ( refcnt, struct fc_exchange, refcnt );
00362 
00363         assert ( ! timer_running ( &xchg->timer ) );
00364         assert ( list_empty ( &xchg->list ) );
00365 
00366         fc_port_put ( xchg->port );
00367         free ( xchg );
00368 }
00369 
00370 /**
00371  * Close Fibre Channel exchange
00372  *
00373  * @v xchg              Fibre Channel exchange
00374  * @v rc                Reason for close
00375  */
00376 static void fc_xchg_close ( struct fc_exchange *xchg, int rc ) {
00377         struct fc_port *port = xchg->port;
00378 
00379         if ( rc != 0 ) {
00380                 DBGC2 ( port, "FCXCHG %s/%04x closed: %s\n",
00381                         port->name, xchg->xchg_id, strerror ( rc ) );
00382         }
00383 
00384         /* Stop timer */
00385         stop_timer ( &xchg->timer );
00386 
00387         /* If list still holds a reference, remove from list of open
00388          * exchanges and drop list's reference.
00389          */
00390         if ( ! list_empty ( &xchg->list ) ) {
00391                 list_del ( &xchg->list );
00392                 INIT_LIST_HEAD ( &xchg->list );
00393                 ref_put ( &xchg->refcnt );
00394         }
00395 
00396         /* Shutdown interfaces */
00397         intf_shutdown ( &xchg->ulp, rc );
00398 }
00399 
00400 /**
00401  * Handle exchange timeout
00402  *
00403  * @v timer             Timeout timer
00404  * @v over              Failure indicator
00405  */
00406 static void fc_xchg_expired ( struct retry_timer *timer, int over __unused ) {
00407         struct fc_exchange *xchg =
00408                 container_of ( timer, struct fc_exchange, timer );
00409         struct fc_port *port = xchg->port;
00410 
00411         DBGC ( port, "FCXCHG %s/%04x timed out\n", port->name, xchg->xchg_id );
00412 
00413         /* Terminate the exchange */
00414         fc_xchg_close ( xchg, -ETIMEDOUT );
00415 }
00416 
00417 /**
00418  * Check Fibre Channel exchange window
00419  *
00420  * @v xchg              Fibre Channel exchange
00421  * @ret len             Length opf window
00422  */
00423 static size_t fc_xchg_window ( struct fc_exchange *xchg __unused ) {
00424 
00425         /* We don't currently store the path MTU */
00426         return FC_LOGIN_DEFAULT_MTU;
00427 }
00428 
00429 /**
00430  * Allocate Fibre Channel I/O buffer
00431  *
00432  * @v xchg              Fibre Channel exchange
00433  * @v len               Payload length
00434  * @ret iobuf           I/O buffer, or NULL
00435  */
00436 static struct io_buffer * fc_xchg_alloc_iob ( struct fc_exchange *xchg,
00437                                               size_t len ) {
00438         struct fc_port *port = xchg->port;
00439         struct io_buffer *iobuf;
00440 
00441         iobuf = xfer_alloc_iob ( &port->transport,
00442                                  ( sizeof ( struct fc_frame_header ) + len ) );
00443         if ( iobuf ) {
00444                 iob_reserve ( iobuf, sizeof ( struct fc_frame_header ) );
00445         }
00446         return iobuf;
00447 }
00448 
00449 /**
00450  * Transmit data as part of a Fibre Channel exchange
00451  *
00452  * @v xchg              Fibre Channel exchange
00453  * @v iobuf             I/O buffer
00454  * @v meta              Data transfer metadata
00455  * @ret rc              Return status code
00456  */
00457 static int fc_xchg_tx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
00458                         struct xfer_metadata *meta ) {
00459         struct fc_port *port = xchg->port;
00460         struct sockaddr_fc *dest = ( ( struct sockaddr_fc * ) meta->dest );
00461         struct fc_frame_header *fchdr;
00462         unsigned int r_ctl;
00463         unsigned int f_ctl_es;
00464         int rc;
00465 
00466         /* Sanity checks */
00467         if ( ! ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) ) {
00468                 DBGC ( port, "FCXCHG %s/%04x cannot transmit while not "
00469                        "holding sequence initiative\n",
00470                        port->name, xchg->xchg_id );
00471                 rc = -EBUSY;
00472                 goto done;
00473         }
00474 
00475         /* Calculate routing control */
00476         switch ( xchg->type ) {
00477         case FC_TYPE_ELS:
00478                 r_ctl = FC_R_CTL_ELS;
00479                 if ( meta->flags & XFER_FL_RESPONSE ) {
00480                         r_ctl |= FC_R_CTL_SOL_CTRL;
00481                 } else {
00482                         r_ctl |= FC_R_CTL_UNSOL_CTRL;
00483                 }
00484                 break;
00485         case FC_TYPE_CT:
00486                 r_ctl = FC_R_CTL_DATA;
00487                 if ( meta->flags & XFER_FL_RESPONSE ) {
00488                         r_ctl |= FC_R_CTL_SOL_CTRL;
00489                 } else {
00490                         r_ctl |= FC_R_CTL_UNSOL_CTRL;
00491                 }
00492                 break;
00493         default:
00494                 r_ctl = FC_R_CTL_DATA;
00495                 switch ( meta->flags &
00496                          ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ) ) {
00497                 case ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ):
00498                         r_ctl |= FC_R_CTL_CMD_STAT;
00499                         break;
00500                 case ( XFER_FL_CMD_STAT ):
00501                         r_ctl |= FC_R_CTL_UNSOL_CMD;
00502                         break;
00503                 case ( XFER_FL_RESPONSE ):
00504                         r_ctl |= FC_R_CTL_SOL_DATA;
00505                         break;
00506                 default:
00507                         r_ctl |= FC_R_CTL_UNSOL_DATA;
00508                         break;
00509                 }
00510                 break;
00511         }
00512 
00513         /* Calculate exchange and sequence control */
00514         f_ctl_es = 0;
00515         if ( ! ( xchg->flags & FC_XCHG_ORIGINATOR ) )
00516                 f_ctl_es |= FC_F_CTL_ES_RESPONDER;
00517         if ( xchg->flags & FC_XCHG_SEQ_FIRST )
00518                 f_ctl_es |= FC_F_CTL_ES_FIRST;
00519         if ( meta->flags & XFER_FL_OUT )
00520                 f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_LAST );
00521         if ( meta->flags & XFER_FL_OVER )
00522                 f_ctl_es |= ( FC_F_CTL_ES_END | FC_F_CTL_ES_TRANSFER );
00523 
00524         /* Create frame header */
00525         fchdr = iob_push ( iobuf, sizeof ( *fchdr ) );
00526         memset ( fchdr, 0, sizeof ( *fchdr ) );
00527         fchdr->r_ctl = r_ctl;
00528         memcpy ( &fchdr->d_id,
00529                  ( dest ? &dest->sfc_port_id : &xchg->peer_port_id ),
00530                  sizeof ( fchdr->d_id ) );
00531         memcpy ( &fchdr->s_id, &port->port_id, sizeof ( fchdr->s_id ) );
00532         fchdr->type = xchg->type;
00533         fchdr->f_ctl_es = f_ctl_es;
00534         fchdr->seq_id = xchg->seq_id;
00535         fchdr->seq_cnt = htons ( xchg->seq_cnt++ );
00536         fchdr->ox_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
00537                                xchg->xchg_id : xchg->peer_xchg_id );
00538         fchdr->rx_id = htons ( ( xchg->flags & FC_XCHG_ORIGINATOR ) ?
00539                                xchg->peer_xchg_id : xchg->xchg_id );
00540         if ( meta->flags & XFER_FL_ABS_OFFSET ) {
00541                 fchdr->f_ctl_misc |= FC_F_CTL_MISC_REL_OFF;
00542                 fchdr->parameter = htonl ( meta->offset );
00543         }
00544 
00545         /* Relinquish sequence initiative if applicable */
00546         if ( meta->flags & XFER_FL_OVER ) {
00547                 xchg->flags &= ~( FC_XCHG_SEQ_INITIATIVE | FC_XCHG_SEQ_FIRST );
00548                 xchg->seq_cnt = 0;
00549         }
00550 
00551         /* Reset timeout */
00552         start_timer_fixed ( &xchg->timer, FC_TIMEOUT );
00553 
00554         /* Deliver frame */
00555         if ( ( rc = xfer_deliver_iob ( &port->transport,
00556                                        iob_disown ( iobuf ) ) ) != 0 ) {
00557                 DBGC ( port, "FCXCHG %s/%04x cannot transmit: %s\n",
00558                        port->name, xchg->xchg_id, strerror ( rc ) );
00559                 goto done;
00560         }
00561 
00562  done:
00563         free_iob ( iobuf );
00564         return rc;
00565 }
00566 
00567 /** Mapping from Fibre Channel routing control information to xfer metadata */
00568 static const uint8_t fc_r_ctl_info_meta_flags[ FC_R_CTL_INFO_MASK + 1 ] = {
00569         [FC_R_CTL_UNCAT]        = ( 0 ),
00570         [FC_R_CTL_SOL_DATA]     = ( XFER_FL_RESPONSE ),
00571         [FC_R_CTL_UNSOL_CTRL]   = ( XFER_FL_CMD_STAT ),
00572         [FC_R_CTL_SOL_CTRL]     = ( XFER_FL_CMD_STAT ),
00573         [FC_R_CTL_UNSOL_DATA]   = ( 0 ),
00574         [FC_R_CTL_DATA_DESC]    = ( XFER_FL_CMD_STAT ),
00575         [FC_R_CTL_UNSOL_CMD]    = ( XFER_FL_CMD_STAT ),
00576         [FC_R_CTL_CMD_STAT]     = ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ),
00577 };
00578 
00579 /**
00580  * Receive data as part of a Fibre Channel exchange
00581  *
00582  * @v xchg              Fibre Channel exchange
00583  * @v iobuf             I/O buffer
00584  * @v meta              Data transfer metadata
00585  * @ret rc              Return status code
00586  */
00587 static int fc_xchg_rx ( struct fc_exchange *xchg, struct io_buffer *iobuf,
00588                         struct xfer_metadata *meta __unused ) {
00589         struct fc_port *port = xchg->port;
00590         struct fc_frame_header *fchdr = iobuf->data;
00591         struct xfer_metadata fc_meta;
00592         struct sockaddr_fc src;
00593         struct sockaddr_fc dest;
00594         int rc;
00595 
00596         /* Record peer exchange ID */
00597         xchg->peer_xchg_id =
00598                 ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
00599                         fchdr->rx_id : fchdr->ox_id );
00600 
00601         /* Sequence checks */
00602         if ( xchg->flags & FC_XCHG_SEQ_INITIATIVE ) {
00603                 DBGC ( port, "FCXCHG %s/%04x received frame while holding "
00604                        "sequence initiative\n", port->name, xchg->xchg_id );
00605                 rc = -EBUSY;
00606                 goto done;
00607         }
00608         if ( ntohs ( fchdr->seq_cnt ) != xchg->seq_cnt ) {
00609                 DBGC ( port, "FCXCHG %s/%04x received out-of-order frame %d "
00610                        "(expected %d)\n", port->name, xchg->xchg_id,
00611                        ntohs ( fchdr->seq_cnt ), xchg->seq_cnt );
00612                 rc = -EPIPE;
00613                 goto done;
00614         }
00615         if ( xchg->seq_cnt == 0 )
00616                 xchg->seq_id = fchdr->seq_id;
00617         xchg->seq_cnt++;
00618         if ( fchdr->seq_id != xchg->seq_id ) {
00619                 DBGC ( port, "FCXCHG %s/%04x received frame for incorrect "
00620                        "sequence %02x (expected %02x)\n", port->name,
00621                        xchg->xchg_id, fchdr->seq_id, xchg->seq_id );
00622                 rc = -EPIPE;
00623                 goto done;
00624         }
00625 
00626         /* Check for end of sequence and transfer of sequence initiative */
00627         if ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) {
00628                 xchg->seq_cnt = 0;
00629                 if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
00630                         xchg->flags |= FC_XCHG_SEQ_INITIATIVE;
00631                         xchg->seq_id = fc_new_seq_id();
00632                 }
00633         }
00634 
00635         /* Construct metadata */
00636         memset ( &fc_meta, 0, sizeof ( fc_meta ) );
00637         fc_meta.flags =
00638                 fc_r_ctl_info_meta_flags[ fchdr->r_ctl & FC_R_CTL_INFO_MASK ];
00639         if ( fchdr->f_ctl_es & FC_F_CTL_ES_TRANSFER ) {
00640                 fc_meta.flags |= XFER_FL_OVER;
00641         }
00642         if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
00643              ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
00644                 fc_meta.flags |= XFER_FL_OUT;
00645         }
00646         if ( fchdr->f_ctl_misc & FC_F_CTL_MISC_REL_OFF ) {
00647                 fc_meta.flags |= XFER_FL_ABS_OFFSET;
00648                 fc_meta.offset = ntohl ( fchdr->parameter );
00649         }
00650         fc_meta.src = fc_fill_sockaddr ( &src, &fchdr->s_id );
00651         fc_meta.dest = fc_fill_sockaddr ( &dest, &fchdr->d_id );
00652 
00653         /* Reset timeout */
00654         start_timer_fixed ( &xchg->timer, FC_TIMEOUT );
00655 
00656         /* Deliver via exchange's ULP interface */
00657         iob_pull ( iobuf, sizeof ( *fchdr ) );
00658         if ( ( rc = xfer_deliver ( &xchg->ulp, iob_disown ( iobuf ),
00659                                    &fc_meta ) ) != 0 ) {
00660                 DBGC ( port, "FCXCHG %s/%04x cannot deliver frame: %s\n",
00661                        port->name, xchg->xchg_id, strerror ( rc ) );
00662                 goto done;
00663         }
00664 
00665         /* Close exchange if applicable */
00666         if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_LAST ) &&
00667              ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ) {
00668                 fc_xchg_close ( xchg, 0 );
00669         }
00670 
00671  done:
00672         free_iob ( iobuf );
00673         return rc;
00674 }
00675 
00676 /** Fibre Channel exchange ULP interface operations */
00677 static struct interface_operation fc_xchg_ulp_op[] = {
00678         INTF_OP ( xfer_deliver, struct fc_exchange *, fc_xchg_tx ),
00679         INTF_OP ( xfer_alloc_iob, struct fc_exchange *, fc_xchg_alloc_iob ),
00680         INTF_OP ( xfer_window, struct fc_exchange *, fc_xchg_window ),
00681         INTF_OP ( intf_close, struct fc_exchange *, fc_xchg_close ),
00682 };
00683 
00684 /** Fibre Channel exchange ULP interface descriptor */
00685 static struct interface_descriptor fc_xchg_ulp_desc =
00686         INTF_DESC ( struct fc_exchange, ulp, fc_xchg_ulp_op );
00687 
00688 /**
00689  * Create new Fibre Channel exchange
00690  *
00691  * @v port              Fibre Channel port
00692  * @v peer_port_id      Peer port ID
00693  * @ret xchg            Exchange, or NULL
00694  */
00695 static struct fc_exchange * fc_xchg_create ( struct fc_port *port,
00696                                              struct fc_port_id *peer_port_id,
00697                                              unsigned int type ) {
00698         struct fc_exchange *xchg;
00699 
00700         /* Allocate and initialise structure */
00701         xchg = zalloc ( sizeof ( *xchg ) );
00702         if ( ! xchg )
00703                 return NULL;
00704         ref_init ( &xchg->refcnt, fc_xchg_free );
00705         intf_init ( &xchg->ulp, &fc_xchg_ulp_desc, &xchg->refcnt );
00706         timer_init ( &xchg->timer, fc_xchg_expired, &xchg->refcnt );
00707         xchg->port = fc_port_get ( port );
00708         memcpy ( &xchg->peer_port_id, peer_port_id,
00709                  sizeof ( xchg->peer_port_id ) );
00710         xchg->type = type;
00711         xchg->xchg_id = fc_new_xchg_id();
00712         xchg->peer_xchg_id = FC_RX_ID_UNKNOWN;
00713         xchg->seq_id = fc_new_seq_id();
00714 
00715         /* Transfer reference to list of exchanges and return */
00716         list_add ( &xchg->list, &port->xchgs );
00717         return xchg;
00718 }
00719 
00720 /**
00721  * Originate a new Fibre Channel exchange
00722  *
00723  * @v parent            Interface to which to attach
00724  * @v port              Fibre Channel port
00725  * @v peer_port_id      Peer port ID
00726  * @ret xchg_id         Exchange ID, or negative error
00727  */
00728 int fc_xchg_originate ( struct interface *parent, struct fc_port *port,
00729                         struct fc_port_id *peer_port_id, unsigned int type ) {
00730         struct fc_exchange *xchg;
00731 
00732         /* Allocate and initialise structure */
00733         xchg = fc_xchg_create ( port, peer_port_id, type );
00734         if ( ! xchg )
00735                 return -ENOMEM;
00736         xchg->flags = ( FC_XCHG_ORIGINATOR | FC_XCHG_SEQ_INITIATIVE |
00737                         FC_XCHG_SEQ_FIRST );
00738 
00739         DBGC2 ( port, "FCXCHG %s/%04x originating to %s (type %02x)\n",
00740                 port->name, xchg->xchg_id, fc_id_ntoa ( &xchg->peer_port_id ),
00741                 xchg->type );
00742 
00743         /* Attach to parent interface and return */
00744         intf_plug_plug ( &xchg->ulp, parent );
00745         return xchg->xchg_id;
00746 }
00747 
00748 /**
00749  * Open a new responder Fibre Channel exchange
00750  *
00751  * @v port              Fibre Channel port
00752  * @v fchdr             Fibre Channel frame header
00753  * @ret xchg            Fibre Channel exchange, or NULL
00754  */
00755 static struct fc_exchange * fc_xchg_respond ( struct fc_port *port,
00756                                               struct fc_frame_header *fchdr ) {
00757         struct fc_exchange *xchg;
00758         struct fc_responder *responder;
00759         unsigned int type = fchdr->type;
00760         int rc;
00761 
00762         /* Allocate and initialise structure */
00763         xchg = fc_xchg_create ( port, &fchdr->s_id, type );
00764         if ( ! xchg )
00765                 return NULL;
00766         xchg->seq_id = fchdr->seq_id;
00767 
00768         DBGC2 ( port, "FCXCHG %s/%04x responding to %s xchg %04x (type "
00769                 "%02x)\n", port->name, xchg->xchg_id,
00770                 fc_id_ntoa ( &xchg->peer_port_id ),
00771                 ntohs ( fchdr->ox_id ), xchg->type );
00772 
00773         /* Find a responder, if any */
00774         for_each_table_entry ( responder, FC_RESPONDERS ) {
00775                 if ( responder->type == type ) {
00776                         if ( ( rc = responder->respond ( &xchg->ulp, port,
00777                                                          &fchdr->d_id,
00778                                                          &fchdr->s_id ) ) !=0 ){
00779                                 DBGC ( port, "FCXCHG %s/%04x could not "
00780                                        "respond: %s\n", port->name,
00781                                        xchg->xchg_id, strerror ( rc ) );
00782                         }
00783                 }
00784                 break;
00785         }
00786 
00787         /* We may or may not have a ULP attached at this point, but
00788          * the exchange does exist.
00789          */
00790         return xchg;
00791 }
00792 
00793 /******************************************************************************
00794  *
00795  * Fibre Channel ports
00796  *
00797  ******************************************************************************
00798  */
00799 
00800 /**
00801  * Close Fibre Channel port
00802  *
00803  * @v port              Fibre Channel port
00804  * @v rc                Reason for close
00805  */
00806 static void fc_port_close ( struct fc_port *port, int rc ) {
00807         struct fc_exchange *xchg;
00808         struct fc_exchange *tmp;
00809 
00810         DBGC ( port, "FCPORT %s closed\n", port->name );
00811 
00812         /* Log out port, if necessary */
00813         if ( fc_link_ok ( &port->link ) )
00814                 fc_port_logout ( port, rc );
00815 
00816         /* Stop link monitor */
00817         fc_link_stop ( &port->link );
00818 
00819         /* Shut down interfaces */
00820         intf_shutdown ( &port->transport, rc );
00821         intf_shutdown ( &port->flogi, rc );
00822         intf_shutdown ( &port->ns_plogi, rc );
00823 
00824         /* Shut down any remaining exchanges */
00825         list_for_each_entry_safe ( xchg, tmp, &port->xchgs, list )
00826                 fc_xchg_close ( xchg, rc );
00827 
00828         /* Remove from list of ports */
00829         list_del ( &port->list );
00830         INIT_LIST_HEAD ( &port->list );
00831 }
00832 
00833 /**
00834  * Identify Fibre Channel exchange by local exchange ID
00835  *
00836  * @v port              Fibre Channel port
00837  * @v xchg_id           Local exchange ID
00838  * @ret xchg            Fibre Channel exchange, or NULL
00839  */
00840 static struct fc_exchange * fc_port_demux ( struct fc_port *port,
00841                                             unsigned int xchg_id ) {
00842         struct fc_exchange *xchg;
00843 
00844         list_for_each_entry ( xchg, &port->xchgs, list ) {
00845                 if ( xchg->xchg_id == xchg_id )
00846                         return xchg;
00847         }
00848         return NULL;
00849 }
00850 
00851 /**
00852  * Handle received frame from Fibre Channel port
00853  *
00854  * @v port              Fibre Channel port
00855  * @v iobuf             I/O buffer
00856  * @v meta              Data transfer metadata
00857  * @ret rc              Return status code
00858  */
00859 static int fc_port_deliver ( struct fc_port *port, struct io_buffer *iobuf,
00860                              struct xfer_metadata *meta ) {
00861         struct fc_frame_header *fchdr = iobuf->data;
00862         unsigned int xchg_id;
00863         struct fc_exchange *xchg;
00864         int rc;
00865 
00866         /* Sanity check */
00867         if ( iob_len ( iobuf ) < sizeof ( *fchdr ) ) {
00868                 DBGC ( port, "FCPORT %s received underlength frame (%zd "
00869                        "bytes)\n", port->name, iob_len ( iobuf ) );
00870                 rc = -EINVAL;
00871                 goto err_sanity;
00872         }
00873 
00874         /* Verify local port ID */
00875         if ( ( memcmp ( &fchdr->d_id, &port->port_id,
00876                         sizeof ( fchdr->d_id ) ) != 0 ) &&
00877              ( memcmp ( &fchdr->d_id, &fc_f_port_id,
00878                         sizeof ( fchdr->d_id ) ) != 0 ) &&
00879              ( memcmp ( &port->port_id, &fc_empty_port_id,
00880                         sizeof ( port->port_id ) ) != 0 ) ) {
00881                 DBGC ( port, "FCPORT %s received frame for incorrect port ID "
00882                        "%s\n", port->name, fc_id_ntoa ( &fchdr->d_id ) );
00883                 rc = -ENOTCONN;
00884                 goto err_port_id;
00885         }
00886 
00887         /* Demultiplex amongst active exchanges */
00888         xchg_id = ntohs ( ( fchdr->f_ctl_es & FC_F_CTL_ES_RESPONDER ) ?
00889                           fchdr->ox_id : fchdr->rx_id );
00890         xchg = fc_port_demux ( port, xchg_id );
00891 
00892         /* If we have no active exchange and this frame starts a new
00893          * exchange, try to create a new responder exchange
00894          */
00895         if ( ( fchdr->f_ctl_es & FC_F_CTL_ES_FIRST ) &&
00896              ( fchdr->seq_cnt == 0 ) ) {
00897 
00898                 /* Create new exchange */
00899                 xchg = fc_xchg_respond ( port, fchdr );
00900                 if ( ! xchg ) {
00901                         DBGC ( port, "FCPORT %s cannot create new exchange\n",
00902                                port->name );
00903                         rc = -ENOMEM;
00904                         goto err_respond;
00905                 }
00906         }
00907 
00908         /* Fail if no exchange exists */
00909         if ( ! xchg ) {
00910                 DBGC ( port, "FCPORT %s xchg %04x unknown\n",
00911                        port->name, xchg_id );
00912                 rc = -ENOTCONN;
00913                 goto err_no_xchg;
00914         }
00915 
00916         /* Pass received frame to exchange */
00917         ref_get ( &xchg->refcnt );
00918         if ( ( rc = fc_xchg_rx ( xchg, iob_disown ( iobuf ), meta ) ) != 0 )
00919                 goto err_xchg_rx;
00920 
00921  err_xchg_rx:
00922         ref_put ( &xchg->refcnt );
00923  err_no_xchg:
00924  err_respond:
00925  err_port_id:
00926  err_sanity:
00927         free_iob ( iobuf );
00928         return rc;
00929 }
00930 
00931 /**
00932  * Log in Fibre Channel port
00933  *
00934  * @v port              Fibre Channel port
00935  * @v port_id           Local port ID
00936  * @v link_node_wwn     Link node name
00937  * @v link_port_wwn     Link port name
00938  * @v has_fabric        Link is to a fabric
00939  * @ret rc              Return status code
00940  */
00941 int fc_port_login ( struct fc_port *port, struct fc_port_id *port_id,
00942                     const struct fc_name *link_node_wwn,
00943                     const struct fc_name *link_port_wwn, int has_fabric ) {
00944         struct fc_peer *peer;
00945         struct fc_peer *tmp;
00946         int rc;
00947 
00948         /* Perform implicit logout if logged in and details differ */
00949         if ( fc_link_ok ( &port->link ) &&
00950              ( ( ( !! ( port->flags & FC_PORT_HAS_FABRIC ) ) !=
00951                  ( !! has_fabric ) ) ||
00952                ( memcmp ( &port->link_node_wwn, link_node_wwn,
00953                           sizeof ( port->link_node_wwn ) ) != 0 ) ||
00954                ( memcmp ( &port->link_port_wwn, link_port_wwn,
00955                           sizeof ( port->link_port_wwn ) ) != 0 ) ||
00956                ( has_fabric &&
00957                  ( memcmp ( &port->port_id, port_id,
00958                             sizeof ( port->port_id ) ) != 0 ) ) ) ) {
00959                 fc_port_logout ( port, 0 );
00960         }
00961 
00962         /* Log in, if applicable */
00963         if ( ! fc_link_ok ( &port->link ) ) {
00964 
00965                 /* Record link port name */
00966                 memcpy ( &port->link_node_wwn, link_node_wwn,
00967                          sizeof ( port->link_node_wwn ) );
00968                 memcpy ( &port->link_port_wwn, link_port_wwn,
00969                          sizeof ( port->link_port_wwn ) );
00970                 DBGC ( port, "FCPORT %s logged in to %s",
00971                        port->name, fc_ntoa ( &port->link_node_wwn ) );
00972                 DBGC ( port, " port %s\n", fc_ntoa ( &port->link_port_wwn ) );
00973 
00974                 /* Calculate local (and possibly remote) port IDs */
00975                 if ( has_fabric ) {
00976                         port->flags |= FC_PORT_HAS_FABRIC;
00977                         memcpy ( &port->port_id, port_id,
00978                                  sizeof ( port->port_id ) );
00979                 } else {
00980                         port->flags &= ~FC_PORT_HAS_FABRIC;
00981                         if ( memcmp ( &port->port_wwn, link_port_wwn,
00982                                       sizeof ( port->port_wwn ) ) > 0 ) {
00983                                 memcpy ( &port->port_id, &fc_ptp_high_port_id,
00984                                          sizeof ( port->port_id ) );
00985                                 memcpy ( &port->ptp_link_port_id,
00986                                          &fc_ptp_low_port_id,
00987                                          sizeof ( port->ptp_link_port_id ) );
00988                         } else {
00989                                 memcpy ( &port->port_id, &fc_ptp_low_port_id,
00990                                          sizeof ( port->port_id ) );
00991                                 memcpy ( &port->ptp_link_port_id,
00992                                          &fc_ptp_high_port_id,
00993                                          sizeof ( port->ptp_link_port_id ) );
00994                         }
00995                 }
00996                 DBGC ( port, "FCPORT %s logged in via a %s, with local ID "
00997                        "%s\n", port->name,
00998                        ( ( port->flags & FC_PORT_HAS_FABRIC ) ?
00999                          "fabric" : "point-to-point link" ),
01000                        fc_id_ntoa ( &port->port_id ) );
01001         }
01002 
01003         /* Log in to name server, if attached to a fabric */
01004         if ( has_fabric && ! ( port->flags & FC_PORT_HAS_NS ) ) {
01005 
01006                 DBGC ( port, "FCPORT %s attempting login to name server\n",
01007                        port->name );
01008 
01009                 intf_restart ( &port->ns_plogi, -ECANCELED );
01010                 if ( ( rc = fc_els_plogi ( &port->ns_plogi, port,
01011                                            &fc_gs_port_id ) ) != 0 ) {
01012                         DBGC ( port, "FCPORT %s could not initiate name "
01013                                "server PLOGI: %s\n",
01014                                port->name, strerror ( rc ) );
01015                         fc_port_logout ( port, rc );
01016                         return rc;
01017                 }
01018         }
01019 
01020         /* Record login */
01021         fc_link_up ( &port->link );
01022 
01023         /* Notify peers of link state change */
01024         list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
01025                 fc_peer_get ( peer );
01026                 fc_link_examine ( &peer->link );
01027                 fc_peer_put ( peer );
01028         }
01029 
01030         return 0;
01031 }
01032 
01033 /**
01034  * Log out Fibre Channel port
01035  *
01036  * @v port              Fibre Channel port
01037  * @v rc                Reason for logout
01038  */
01039 void fc_port_logout ( struct fc_port *port, int rc ) {
01040         struct fc_peer *peer;
01041         struct fc_peer *tmp;
01042 
01043         DBGC ( port, "FCPORT %s logged out: %s\n",
01044                port->name, strerror ( rc ) );
01045 
01046         /* Erase port details */
01047         memset ( &port->port_id, 0, sizeof ( port->port_id ) );
01048         port->flags = 0;
01049 
01050         /* Record logout */
01051         fc_link_err ( &port->link, rc );
01052 
01053         /* Notify peers of link state change */
01054         list_for_each_entry_safe ( peer, tmp, &fc_peers, list ) {
01055                 fc_peer_get ( peer );
01056                 fc_link_examine ( &peer->link );
01057                 fc_peer_put ( peer );
01058         }
01059 }
01060 
01061 /**
01062  * Handle FLOGI completion
01063  *
01064  * @v port              Fibre Channel port
01065  * @v rc                Reason for completion
01066  */
01067 static void fc_port_flogi_done ( struct fc_port *port, int rc ) {
01068 
01069         intf_restart ( &port->flogi, rc );
01070 
01071         if ( rc != 0 )
01072                 fc_port_logout ( port, rc );
01073 }
01074 
01075 /**
01076  * Handle name server PLOGI completion
01077  *
01078  * @v port              Fibre Channel port
01079  * @v rc                Reason for completion
01080  */
01081 static void fc_port_ns_plogi_done ( struct fc_port *port, int rc ) {
01082 
01083         intf_restart ( &port->ns_plogi, rc );
01084 
01085         if ( rc == 0 ) {
01086                 port->flags |= FC_PORT_HAS_NS;
01087                 DBGC ( port, "FCPORT %s logged in to name server\n",
01088                        port->name );
01089         } else {
01090                 DBGC ( port, "FCPORT %s could not log in to name server: %s\n",
01091                        port->name, strerror ( rc ) );
01092                 /* Absence of a name server is not a fatal error */
01093         }
01094 }
01095 
01096 /**
01097  * Examine Fibre Channel port link state
01098  *
01099  * @ link               Fibre Channel link state monitor
01100  */
01101 static void fc_port_examine ( struct fc_link_state *link ) {
01102         struct fc_port *port = container_of ( link, struct fc_port, link );
01103         int rc;
01104 
01105         /* Do nothing if already logged in */
01106         if ( fc_link_ok ( &port->link ) )
01107                 return;
01108 
01109         DBGC ( port, "FCPORT %s attempting login\n", port->name );
01110 
01111         /* Try to create FLOGI ELS */
01112         intf_restart ( &port->flogi, -ECANCELED );
01113         if ( ( rc = fc_els_flogi ( &port->flogi, port ) ) != 0 ) {
01114                 DBGC ( port, "FCPORT %s could not initiate FLOGI: %s\n",
01115                        port->name, strerror ( rc ) );
01116                 fc_port_logout ( port, rc );
01117                 return;
01118         }
01119 }
01120 
01121 /**
01122  * Handle change of flow control window
01123  *
01124  * @v port              Fibre Channel port
01125  */
01126 static void fc_port_window_changed ( struct fc_port *port ) {
01127         size_t window;
01128 
01129         /* Check if transport layer is ready */
01130         window = xfer_window ( &port->transport );
01131         if ( window > 0 ) {
01132 
01133                 /* Transport layer is ready.  Start login if the link
01134                  * is not already up.
01135                  */
01136                 if ( ! fc_link_ok ( &port->link ) )
01137                         fc_link_start ( &port->link );
01138 
01139         } else {
01140 
01141                 /* Transport layer is not ready.  Log out port and
01142                  * wait for transport layer before attempting log in
01143                  * again.
01144                  */
01145                 fc_port_logout ( port, -ENOTCONN );
01146                 fc_link_stop ( &port->link );
01147         }
01148 }
01149 
01150 /** Fibre Channel port transport interface operations */
01151 static struct interface_operation fc_port_transport_op[] = {
01152         INTF_OP ( xfer_deliver, struct fc_port *, fc_port_deliver ),
01153         INTF_OP ( xfer_window_changed, struct fc_port *,
01154                   fc_port_window_changed ),
01155         INTF_OP ( intf_close, struct fc_port *, fc_port_close ),
01156 };
01157 
01158 /** Fibre Channel port transport interface descriptor */
01159 static struct interface_descriptor fc_port_transport_desc =
01160         INTF_DESC ( struct fc_port, transport, fc_port_transport_op );
01161 
01162 /** Fibre Channel port FLOGI interface operations */
01163 static struct interface_operation fc_port_flogi_op[] = {
01164         INTF_OP ( intf_close, struct fc_port *, fc_port_flogi_done ),
01165 };
01166 
01167 /** Fibre Channel port FLOGI interface descriptor */
01168 static struct interface_descriptor fc_port_flogi_desc =
01169         INTF_DESC ( struct fc_port, flogi, fc_port_flogi_op );
01170 
01171 /** Fibre Channel port name server PLOGI interface operations */
01172 static struct interface_operation fc_port_ns_plogi_op[] = {
01173         INTF_OP ( intf_close, struct fc_port *, fc_port_ns_plogi_done ),
01174 };
01175 
01176 /** Fibre Channel port name server PLOGI interface descriptor */
01177 static struct interface_descriptor fc_port_ns_plogi_desc =
01178         INTF_DESC ( struct fc_port, ns_plogi, fc_port_ns_plogi_op );
01179 
01180 /**
01181  * Create Fibre Channel port
01182  *
01183  * @v transport         Transport interface
01184  * @v node              Fibre Channel node name
01185  * @v port              Fibre Channel port name
01186  * @v name              Symbolic port name
01187  * @ret rc              Return status code
01188  */
01189 int fc_port_open ( struct interface *transport, const struct fc_name *node_wwn,
01190                    const struct fc_name *port_wwn, const char *name ) {
01191         struct fc_port *port;
01192 
01193         /* Allocate and initialise structure */
01194         port = zalloc ( sizeof ( *port ) );
01195         if ( ! port )
01196                 return -ENOMEM;
01197         ref_init ( &port->refcnt, NULL );
01198         intf_init ( &port->transport, &fc_port_transport_desc, &port->refcnt );
01199         fc_link_init ( &port->link, fc_port_examine, &port->refcnt );
01200         intf_init ( &port->flogi, &fc_port_flogi_desc, &port->refcnt );
01201         intf_init ( &port->ns_plogi, &fc_port_ns_plogi_desc, &port->refcnt );
01202         list_add_tail ( &port->list, &fc_ports );
01203         INIT_LIST_HEAD ( &port->xchgs );
01204         memcpy ( &port->node_wwn, node_wwn, sizeof ( port->node_wwn ) );
01205         memcpy ( &port->port_wwn, port_wwn, sizeof ( port->port_wwn ) );
01206         snprintf ( port->name, sizeof ( port->name ), "%s", name );
01207 
01208         DBGC ( port, "FCPORT %s opened as %s",
01209                port->name, fc_ntoa ( &port->node_wwn ) );
01210         DBGC ( port, " port %s\n", fc_ntoa ( &port->port_wwn ) );
01211 
01212         /* Attach to transport layer, mortalise self, and return */
01213         intf_plug_plug ( &port->transport, transport );
01214         ref_put ( &port->refcnt );
01215         return 0;
01216 }
01217 
01218 /**
01219  * Find Fibre Channel port by name
01220  *
01221  * @v name              Fibre Channel port name
01222  * @ret port            Fibre Channel port, or NULL
01223  */
01224 struct fc_port * fc_port_find ( const char *name ) {
01225         struct fc_port *port;
01226 
01227         list_for_each_entry ( port, &fc_ports, list ) {
01228                 if ( strcmp ( name, port->name ) == 0 )
01229                         return port;
01230         }
01231         return NULL;
01232 }
01233 
01234 /******************************************************************************
01235  *
01236  * Fibre Channel peers
01237  *
01238  ******************************************************************************
01239  */
01240 
01241 /**
01242  * Close Fibre Channel peer
01243  *
01244  * @v peer              Fibre Channel peer
01245  * @v rc                Reason for close
01246  */
01247 static void fc_peer_close ( struct fc_peer *peer, int rc ) {
01248 
01249         DBGC ( peer, "FCPEER %s closed: %s\n",
01250                fc_ntoa ( &peer->port_wwn ) , strerror ( rc ) );
01251 
01252         /* Sanity check */
01253         assert ( list_empty ( &peer->ulps ) );
01254 
01255         /* Stop link timer */
01256         fc_link_stop ( &peer->link );
01257 
01258         /* Shut down interfaces */
01259         intf_shutdown ( &peer->plogi, rc );
01260 
01261         /* Remove from list of peers */
01262         list_del ( &peer->list );
01263         INIT_LIST_HEAD ( &peer->list );
01264 }
01265 
01266 /**
01267  * Increment Fibre Channel peer active usage count
01268  *
01269  * @v peer              Fibre Channel peer
01270  */
01271 static void fc_peer_increment ( struct fc_peer *peer ) {
01272 
01273         /* Increment our usage count */
01274         peer->usage++;
01275 }
01276 
01277 /**
01278  * Decrement Fibre Channel peer active usage count
01279  *
01280  * @v peer              Fibre Channel peer
01281  */
01282 static void fc_peer_decrement ( struct fc_peer *peer ) {
01283 
01284         /* Sanity check */
01285         assert ( peer->usage > 0 );
01286 
01287         /* Decrement our usage count and log out if we reach zero */
01288         if ( --(peer->usage) == 0 )
01289                 fc_peer_logout ( peer, 0 );
01290 }
01291 
01292 /**
01293  * Log in Fibre Channel peer
01294  *
01295  * @v peer              Fibre Channel peer
01296  * @v port              Fibre Channel port
01297  * @v port_id           Port ID
01298  * @ret rc              Return status code
01299  */
01300 int fc_peer_login ( struct fc_peer *peer, struct fc_port *port,
01301                     struct fc_port_id *port_id ) {
01302         struct fc_ulp *ulp;
01303         struct fc_ulp *tmp;
01304 
01305         /* Perform implicit logout if logged in and details differ */
01306         if ( fc_link_ok ( &peer->link ) &&
01307              ( ( peer->port != port ) ||
01308                ( memcmp ( &peer->port_id, port_id,
01309                           sizeof ( peer->port_id ) ) !=0 ) ) ) {
01310                 fc_peer_logout ( peer, 0 );
01311         }
01312 
01313         /* Log in, if applicable */
01314         if ( ! fc_link_ok ( &peer->link ) ) {
01315 
01316                 /* Record peer details */
01317                 assert ( peer->port == NULL );
01318                 peer->port = fc_port_get ( port );
01319                 memcpy ( &peer->port_id, port_id, sizeof ( peer->port_id ) );
01320                 DBGC ( peer, "FCPEER %s logged in via %s as %s\n",
01321                        fc_ntoa ( &peer->port_wwn ), peer->port->name,
01322                        fc_id_ntoa ( &peer->port_id ) );
01323 
01324                 /* Add login reference */
01325                 fc_peer_get ( peer );
01326         }
01327 
01328         /* Record login */
01329         fc_link_up ( &peer->link );
01330 
01331         /* Notify ULPs of link state change */
01332         list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
01333                 fc_ulp_get ( ulp );
01334                 fc_link_examine ( &ulp->link );
01335                 fc_ulp_put ( ulp );
01336         }
01337 
01338         return 0;
01339 }
01340 
01341 /**
01342  * Log out Fibre Channel peer
01343  *
01344  * @v peer              Fibre Channel peer
01345  * @v rc                Reason for logout
01346  */
01347 void fc_peer_logout ( struct fc_peer *peer, int rc ) {
01348         struct fc_ulp *ulp;
01349         struct fc_ulp *tmp;
01350 
01351         DBGC ( peer, "FCPEER %s logged out: %s\n",
01352                fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );
01353 
01354         /* Drop login reference, if applicable */
01355         if ( fc_link_ok ( &peer->link ) )
01356                 fc_peer_put ( peer );
01357 
01358         /* Erase peer details */
01359         fc_port_put ( peer->port );
01360         peer->port = NULL;
01361 
01362         /* Record logout */
01363         fc_link_err ( &peer->link, rc );
01364 
01365         /* Notify ULPs of link state change */
01366         list_for_each_entry_safe ( ulp, tmp, &peer->ulps, list ) {
01367                 fc_ulp_get ( ulp );
01368                 fc_link_examine ( &ulp->link );
01369                 fc_ulp_put ( ulp );
01370         }
01371 
01372         /* Close peer if there are no active users */
01373         if ( peer->usage == 0 )
01374                 fc_peer_close ( peer, rc );
01375 }
01376 
01377 /**
01378  * Handle PLOGI completion
01379  *
01380  * @v peer              Fibre Channel peer
01381  * @v rc                Reason for completion
01382  */
01383 static void fc_peer_plogi_done ( struct fc_peer *peer, int rc ) {
01384 
01385         intf_restart ( &peer->plogi, rc );
01386 
01387         if ( rc != 0 )
01388                 fc_peer_logout ( peer, rc );
01389 }
01390 
01391 /**
01392  * Initiate PLOGI
01393  *
01394  * @v peer              Fibre Channel peer
01395  * @v port              Fibre Channel port
01396  * @v peer_port_id      Peer port ID
01397  * @ret rc              Return status code
01398  */
01399 static int fc_peer_plogi ( struct fc_peer *peer, struct fc_port *port,
01400                            struct fc_port_id *peer_port_id ) {
01401         int rc;
01402 
01403         /* Try to create PLOGI ELS */
01404         intf_restart ( &peer->plogi, -ECANCELED );
01405         if ( ( rc = fc_els_plogi ( &peer->plogi, port, peer_port_id ) ) != 0 ) {
01406                 DBGC ( peer, "FCPEER %s could not initiate PLOGI: %s\n",
01407                        fc_ntoa ( &peer->port_wwn ), strerror ( rc ) );
01408                 fc_peer_logout ( peer, rc );
01409                 return rc;
01410         }
01411 
01412         return 0;
01413 }
01414 
01415 /**
01416  * Examine Fibre Channel peer link state
01417  *
01418  * @ link               Fibre Channel link state monitor
01419  */
01420 static void fc_peer_examine ( struct fc_link_state *link ) {
01421         struct fc_peer *peer = container_of ( link, struct fc_peer, link );
01422         struct fc_port *port;
01423         int rc;
01424 
01425         /* Check to see if underlying port link has gone down */
01426         if ( peer->port && ( ! fc_link_ok ( &peer->port->link ) ) ) {
01427                 fc_peer_logout ( peer, -ENOTCONN );
01428                 return;
01429         }
01430 
01431         /* Do nothing if already logged in */
01432         if ( fc_link_ok ( &peer->link ) )
01433                 return;
01434 
01435         DBGC ( peer, "FCPEER %s attempting login\n",
01436                fc_ntoa ( &peer->port_wwn ) );
01437 
01438         /* Sanity check */
01439         assert ( peer->port == NULL );
01440 
01441         /* First, look for a port with the peer attached via a
01442          * point-to-point link.
01443          */
01444         list_for_each_entry ( port, &fc_ports, list ) {
01445                 if ( fc_link_ok ( &port->link ) &&
01446                      ( ! ( port->flags & FC_PORT_HAS_FABRIC ) ) &&
01447                      ( memcmp ( &peer->port_wwn, &port->link_port_wwn,
01448                                 sizeof ( peer->port_wwn ) ) == 0 ) ) {
01449                         /* Use this peer port ID, and stop looking */
01450                         fc_peer_plogi ( peer, port, &port->ptp_link_port_id );
01451                         return;
01452                 }
01453         }
01454 
01455         /* If the peer is not directly attached, try initiating a name
01456          * server lookup on any suitable ports.
01457          */
01458         list_for_each_entry ( port, &fc_ports, list ) {
01459                 if ( fc_link_ok ( &port->link ) &&
01460                      ( port->flags & FC_PORT_HAS_FABRIC ) &&
01461                      ( port->flags & FC_PORT_HAS_NS ) ) {
01462                         if ( ( rc = fc_ns_query ( peer, port,
01463                                                   fc_peer_plogi ) ) != 0 ) {
01464                                 DBGC ( peer, "FCPEER %s could not attempt "
01465                                        "name server lookup on %s: %s\n",
01466                                        fc_ntoa ( &peer->port_wwn ), port->name,
01467                                        strerror ( rc ) );
01468                                 /* Non-fatal */
01469                         }
01470                 }
01471         }
01472 }
01473 
01474 /** Fibre Channel peer PLOGI interface operations */
01475 static struct interface_operation fc_peer_plogi_op[] = {
01476         INTF_OP ( intf_close, struct fc_peer *, fc_peer_plogi_done ),
01477 };
01478 
01479 /** Fibre Channel peer PLOGI interface descriptor */
01480 static struct interface_descriptor fc_peer_plogi_desc =
01481         INTF_DESC ( struct fc_peer, plogi, fc_peer_plogi_op );
01482 
01483 /**
01484  * Create Fibre Channel peer
01485  *
01486  * @v port_wwn          Node name
01487  * @ret peer            Fibre Channel peer, or NULL
01488  */
01489 static struct fc_peer * fc_peer_create ( const struct fc_name *port_wwn ) {
01490         struct fc_peer *peer;
01491 
01492         /* Allocate and initialise structure */
01493         peer = zalloc ( sizeof ( *peer ) );
01494         if ( ! peer )
01495                 return NULL;
01496         ref_init ( &peer->refcnt, NULL );
01497         fc_link_init ( &peer->link, fc_peer_examine, &peer->refcnt );
01498         intf_init ( &peer->plogi, &fc_peer_plogi_desc, &peer->refcnt );
01499         list_add_tail ( &peer->list, &fc_peers );
01500         memcpy ( &peer->port_wwn, port_wwn, sizeof ( peer->port_wwn ) );
01501         INIT_LIST_HEAD ( &peer->ulps );
01502 
01503         /* Start link monitor */
01504         fc_link_start ( &peer->link );
01505 
01506         DBGC ( peer, "FCPEER %s created\n", fc_ntoa ( &peer->port_wwn ) );
01507         return peer;
01508 }
01509 
01510 /**
01511  * Get Fibre Channel peer by node name
01512  *
01513  * @v port_wwn          Node name
01514  * @ret peer            Fibre Channel peer, or NULL
01515  */
01516 struct fc_peer * fc_peer_get_wwn ( const struct fc_name *port_wwn ) {
01517         struct fc_peer *peer;
01518 
01519         /* Look for an existing peer */
01520         list_for_each_entry ( peer, &fc_peers, list ) {
01521                 if ( memcmp ( &peer->port_wwn, port_wwn,
01522                               sizeof ( peer->port_wwn ) ) == 0 )
01523                         return fc_peer_get ( peer );
01524         }
01525 
01526         /* Create a new peer */
01527         peer = fc_peer_create ( port_wwn );
01528         if ( ! peer )
01529                 return NULL;
01530 
01531         return peer;
01532 }
01533 
01534 /**
01535  * Get Fibre Channel peer by port ID
01536  *
01537  * @v port              Fibre Channel port
01538  * @v peer_port_id      Peer port ID
01539  * @ret peer            Fibre Channel peer, or NULL
01540  */
01541 struct fc_peer * fc_peer_get_port_id ( struct fc_port *port,
01542                                        const struct fc_port_id *peer_port_id ){
01543         struct fc_peer *peer;
01544 
01545         /* Look for an existing peer */
01546         list_for_each_entry ( peer, &fc_peers, list ) {
01547                 if ( ( peer->port == port ) &&
01548                      ( memcmp ( &peer->port_id, peer_port_id,
01549                                 sizeof ( peer->port_id ) ) == 0 ) )
01550                         return fc_peer_get ( peer );
01551         }
01552 
01553         /* Cannot create a new peer, since we have no port name to use */
01554         return NULL;
01555 }
01556 
01557 /******************************************************************************
01558  *
01559  * Fibre Channel upper-layer protocols
01560  *
01561  ******************************************************************************
01562  */
01563 
01564 /**
01565  * Free Fibre Channel upper-layer protocol
01566  *
01567  * @v refcnt            Reference count
01568  */
01569 static void fc_ulp_free ( struct refcnt *refcnt ) {
01570         struct fc_ulp *ulp = container_of ( refcnt, struct fc_ulp, refcnt );
01571 
01572         fc_peer_put ( ulp->peer );
01573         free ( ulp );
01574 }
01575 
01576 /**
01577  * Close Fibre Channel upper-layer protocol
01578  *
01579  * @v ulp               Fibre Channel upper-layer protocol
01580  * @v rc                Reason for close
01581  */
01582 static void fc_ulp_close ( struct fc_ulp *ulp, int rc ) {
01583 
01584         DBGC ( ulp, "FCULP %s/%02x closed: %s\n",
01585                fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );
01586 
01587         /* Sanity check */
01588         assert ( list_empty ( &ulp->users ) );
01589 
01590         /* Stop link monitor */
01591         fc_link_stop ( &ulp->link );
01592 
01593         /* Shut down interfaces */
01594         intf_shutdown ( &ulp->prli, rc );
01595 
01596         /* Remove from list of ULPs */
01597         list_del ( &ulp->list );
01598         INIT_LIST_HEAD ( &ulp->list );
01599 }
01600 
01601 /**
01602  * Attach Fibre Channel upper-layer protocol user
01603  *
01604  * @v ulp               Fibre Channel upper-layer protocol
01605  * @v user              Fibre Channel upper-layer protocol user
01606  */
01607 void fc_ulp_attach ( struct fc_ulp *ulp, struct fc_ulp_user *user ) {
01608 
01609         /* Sanity check */
01610         assert ( user->ulp == NULL );
01611 
01612         /* Increment peer's usage count */
01613         fc_peer_increment ( ulp->peer );
01614 
01615         /* Attach user */
01616         user->ulp = fc_ulp_get ( ulp );
01617         list_add ( &user->list, &ulp->users );
01618 }
01619 
01620 /**
01621  * Detach Fibre Channel upper-layer protocol user
01622  *
01623  * @v user              Fibre Channel upper-layer protocol user
01624  */
01625 void fc_ulp_detach ( struct fc_ulp_user *user ) {
01626         struct fc_ulp *ulp = user->ulp;
01627 
01628         /* Do nothing if not attached */
01629         if ( ! ulp )
01630                 return;
01631 
01632         /* Sanity checks */
01633         list_check_contains_entry ( user, &ulp->users, list );
01634 
01635         /* Detach user and log out if no users remain */
01636         list_del ( &user->list );
01637         if ( list_empty ( &ulp->users ) )
01638                 fc_ulp_logout ( ulp, 0 );
01639 
01640         /* Decrement our peer's usage count */
01641         fc_peer_decrement ( ulp->peer );
01642 
01643         /* Drop reference */
01644         user->ulp = NULL;
01645         fc_ulp_put ( ulp );
01646 }
01647 
01648 /**
01649  * Log in Fibre Channel upper-layer protocol
01650  *
01651  * @v ulp               Fibre Channel upper-layer protocol
01652  * @v param             Service parameters
01653  * @v param_len         Length of service parameters
01654  * @v originated        Login was originated by us
01655  * @ret rc              Return status code
01656  */
01657 int fc_ulp_login ( struct fc_ulp *ulp, const void *param, size_t param_len,
01658                    int originated ) {
01659         struct fc_ulp_user *user;
01660         struct fc_ulp_user *tmp;
01661 
01662         /* Perform implicit logout if logged in and service parameters differ */
01663         if ( fc_link_ok ( &ulp->link ) &&
01664              ( ( ulp->param_len != param_len ) ||
01665                ( memcmp ( ulp->param, param, ulp->param_len ) != 0 ) ) ) {
01666                 fc_ulp_logout ( ulp, 0 );
01667         }
01668 
01669         /* Work around a bug in some versions of the Linux Fibre
01670          * Channel stack, which fail to fully initialise image pairs
01671          * established via a PRLI originated by the Linux stack
01672          * itself.
01673          */
01674         if ( originated )
01675                 ulp->flags |= FC_ULP_ORIGINATED_LOGIN_OK;
01676         if ( ! ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) ) {
01677                 DBGC ( ulp, "FCULP %s/%02x sending extra PRLI to work around "
01678                        "Linux bug\n",
01679                        fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
01680                 fc_link_stop ( &ulp->link );
01681                 fc_link_start ( &ulp->link );
01682                 return 0;
01683         }
01684 
01685         /* Log in, if applicable */
01686         if ( ! fc_link_ok ( &ulp->link ) ) {
01687 
01688                 /* Record service parameters */
01689                 assert ( ulp->param == NULL );
01690                 assert ( ulp->param_len == 0 );
01691                 ulp->param = malloc ( param_len );
01692                 if ( ! ulp->param ) {
01693                         DBGC ( ulp, "FCULP %s/%02x could not record "
01694                                "parameters\n",
01695                                fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
01696                         return -ENOMEM;
01697                 }
01698                 memcpy ( ulp->param, param, param_len );
01699                 ulp->param_len = param_len;
01700                 DBGC ( ulp, "FCULP %s/%02x logged in with parameters:\n",
01701                        fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
01702                 DBGC_HDA ( ulp, 0, ulp->param, ulp->param_len );
01703 
01704                 /* Add login reference */
01705                 fc_ulp_get ( ulp );
01706         }
01707 
01708         /* Record login */
01709         fc_link_up ( &ulp->link );
01710 
01711         /* Notify users of link state change */
01712         list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
01713                 fc_ulp_user_get ( user );
01714                 user->examine ( user );
01715                 fc_ulp_user_put ( user );
01716         }
01717 
01718         return 0;
01719 }
01720 
01721 /**
01722  * Log out Fibre Channel upper-layer protocol
01723  *
01724  * @v ulp               Fibre Channel upper-layer protocol
01725  * @v rc                Reason for logout
01726  */
01727 void fc_ulp_logout ( struct fc_ulp *ulp, int rc ) {
01728         struct fc_ulp_user *user;
01729         struct fc_ulp_user *tmp;
01730 
01731         DBGC ( ulp, "FCULP %s/%02x logged out: %s\n",
01732                fc_ntoa ( &ulp->peer->port_wwn ), ulp->type, strerror ( rc ) );
01733 
01734         /* Drop login reference, if applicable */
01735         if ( fc_link_ok ( &ulp->link ) )
01736                 fc_ulp_put ( ulp );
01737 
01738         /* Discard service parameters */
01739         free ( ulp->param );
01740         ulp->param = NULL;
01741         ulp->param_len = 0;
01742         ulp->flags = 0;
01743 
01744         /* Record logout */
01745         fc_link_err ( &ulp->link, rc );
01746 
01747         /* Notify users of link state change */
01748         list_for_each_entry_safe ( user, tmp, &ulp->users, list ) {
01749                 fc_ulp_user_get ( user );
01750                 user->examine ( user );
01751                 fc_ulp_user_put ( user );
01752         }
01753 
01754         /* Close ULP if there are no clients attached */
01755         if ( list_empty ( &ulp->users ) )
01756                 fc_ulp_close ( ulp, rc );
01757 }
01758 
01759 /**
01760  * Handle PRLI completion
01761  *
01762  * @v ulp               Fibre Channel upper-layer protocol
01763  * @v rc                Reason for completion
01764  */
01765 static void fc_ulp_prli_done ( struct fc_ulp *ulp, int rc ) {
01766 
01767         intf_restart ( &ulp->prli, rc );
01768 
01769         if ( rc != 0 )
01770                 fc_ulp_logout ( ulp, rc );
01771 }
01772 
01773 /**
01774  * Examine Fibre Channel upper-layer protocol link state
01775  *
01776  * @ link               Fibre Channel link state monitor
01777  */
01778 static void fc_ulp_examine ( struct fc_link_state *link ) {
01779         struct fc_ulp *ulp = container_of ( link, struct fc_ulp, link );
01780         int rc;
01781 
01782         /* Check to see if underlying peer link has gone down */
01783         if ( ! fc_link_ok ( &ulp->peer->link ) ) {
01784                 fc_ulp_logout ( ulp, -ENOTCONN );
01785                 return;
01786         }
01787 
01788         /* Do nothing if already logged in */
01789         if ( fc_link_ok ( &ulp->link ) &&
01790              ( ulp->flags & FC_ULP_ORIGINATED_LOGIN_OK ) )
01791                 return;
01792 
01793         DBGC ( ulp, "FCULP %s/%02x attempting login\n",
01794                fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
01795 
01796         /* Try to create PRLI ELS */
01797         intf_restart ( &ulp->prli, -ECANCELED );
01798         if ( ( rc = fc_els_prli ( &ulp->prli, ulp->peer->port,
01799                                   &ulp->peer->port_id, ulp->type ) ) != 0 ) {
01800                 DBGC ( ulp, "FCULP %s/%02x could not initiate PRLI: %s\n",
01801                        fc_ntoa ( &ulp->peer->port_wwn ), ulp->type,
01802                        strerror ( rc ) );
01803                 fc_ulp_logout ( ulp, rc );
01804                 return;
01805         }
01806 }
01807 
01808 /** Fibre Channel upper-layer protocol PRLI interface operations */
01809 static struct interface_operation fc_ulp_prli_op[] = {
01810         INTF_OP ( intf_close, struct fc_ulp *, fc_ulp_prli_done ),
01811 };
01812 
01813 /** Fibre Channel upper-layer protocol PRLI interface descriptor */
01814 static struct interface_descriptor fc_ulp_prli_desc =
01815         INTF_DESC ( struct fc_ulp, prli, fc_ulp_prli_op );
01816 
01817 /**
01818  * Create Fibre Channel upper-layer protocl
01819  *
01820  * @v peer              Fibre Channel peer
01821  * @v type              Type
01822  * @ret ulp             Fibre Channel upper-layer protocol, or NULL
01823  */
01824 static struct fc_ulp * fc_ulp_create ( struct fc_peer *peer,
01825                                        unsigned int type ) {
01826         struct fc_ulp *ulp;
01827 
01828         /* Allocate and initialise structure */
01829         ulp = zalloc ( sizeof ( *ulp ) );
01830         if ( ! ulp )
01831                 return NULL;
01832         ref_init ( &ulp->refcnt, fc_ulp_free );
01833         fc_link_init ( &ulp->link, fc_ulp_examine, &ulp->refcnt );
01834         intf_init ( &ulp->prli, &fc_ulp_prli_desc, &ulp->refcnt );
01835         ulp->peer = fc_peer_get ( peer );
01836         list_add_tail ( &ulp->list, &peer->ulps );
01837         ulp->type = type;
01838         INIT_LIST_HEAD ( &ulp->users );
01839 
01840         /* Start link state monitor */
01841         fc_link_start ( &ulp->link );
01842 
01843         DBGC ( ulp, "FCULP %s/%02x created\n",
01844                fc_ntoa ( &ulp->peer->port_wwn ), ulp->type );
01845         return ulp;
01846 }
01847 
01848 /**
01849  * Get Fibre Channel upper-layer protocol by peer and type
01850  *
01851  * @v peer              Fibre Channel peer
01852  * @v type              Type
01853  * @ret ulp             Fibre Channel upper-layer protocol, or NULL
01854  */
01855 static struct fc_ulp * fc_ulp_get_type ( struct fc_peer *peer,
01856                                          unsigned int type ) {
01857         struct fc_ulp *ulp;
01858 
01859         /* Look for an existing ULP */
01860         list_for_each_entry ( ulp, &peer->ulps, list ) {
01861                 if ( ulp->type == type )
01862                         return fc_ulp_get ( ulp );
01863         }
01864 
01865         /* Create a new ULP */
01866         ulp = fc_ulp_create ( peer, type );
01867         if ( ! ulp )
01868                 return NULL;
01869 
01870         return ulp;
01871 }
01872 
01873 /**
01874  * Get Fibre Channel upper-layer protocol by port name and type
01875  *
01876  * @v port_wwn          Port name
01877  * @v type              Type
01878  * @ret ulp             Fibre Channel upper-layer protocol, or NULL
01879  */
01880 struct fc_ulp * fc_ulp_get_wwn_type ( const struct fc_name *port_wwn,
01881                                       unsigned int type ) {
01882         struct fc_ulp *ulp;
01883         struct fc_peer *peer;
01884 
01885         /* Get peer */
01886         peer = fc_peer_get_wwn ( port_wwn );
01887         if ( ! peer )
01888                 goto err_peer_get_wwn;
01889 
01890         /* Get ULP */
01891         ulp = fc_ulp_get_type ( peer, type );
01892         if ( ! ulp )
01893                 goto err_ulp_get_type;
01894 
01895         /* Drop temporary reference to peer */
01896         fc_peer_put ( peer );
01897 
01898         return ulp;
01899 
01900         fc_ulp_put ( ulp );
01901  err_ulp_get_type:
01902         fc_peer_put ( peer );
01903  err_peer_get_wwn:
01904         return NULL;
01905 }
01906 
01907 /**
01908  * Get Fibre Channel upper-layer protocol by port ID and type
01909  *
01910  * @v port              Fibre Channel port
01911  * @v peer_port_id      Peer port ID
01912  * @v type              Type
01913  * @ret ulp             Fibre Channel upper-layer protocol, or NULL
01914  */
01915 struct fc_ulp * fc_ulp_get_port_id_type ( struct fc_port *port,
01916                                           const struct fc_port_id *peer_port_id,
01917                                           unsigned int type ) {
01918         struct fc_ulp *ulp;
01919         struct fc_peer *peer;
01920 
01921         /* Get peer */
01922         peer = fc_peer_get_port_id ( port, peer_port_id );
01923         if ( ! peer )
01924                 goto err_peer_get_wwn;
01925 
01926         /* Get ULP */
01927         ulp = fc_ulp_get_type ( peer, type );
01928         if ( ! ulp )
01929                 goto err_ulp_get_type;
01930 
01931         /* Drop temporary reference to peer */
01932         fc_peer_put ( peer );
01933 
01934         return ulp;
01935 
01936         fc_ulp_put ( ulp );
01937  err_ulp_get_type:
01938         fc_peer_put ( peer );
01939  err_peer_get_wwn:
01940         return NULL;
01941 }
01942 
01943 /* Drag in objects via fc_ports */
01944 REQUIRING_SYMBOL ( fc_ports );
01945 
01946 /* Drag in Fibre Channel configuration */
01947 REQUIRE_OBJECT ( config_fc );