2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 #include <ipxe/interface.h>
32 #include <ipxe/iobuf.h>
33 #include <ipxe/process.h>
34 #include <ipxe/xfer.h>
36 #include <ipxe/fcns.h>
40 * Fibre Channel name server lookups
44 /** A Fibre Channel name server query */
46 /** Reference count */
48 /** Fibre Channel exchange */
49 struct interface xchg;
51 /** Fibre Channel peer */
53 /** Fibre Channel port */
57 struct process process;
60 * @v peer Fibre Channel peer
61 * @v port Fibre Channel port
62 * @v peer_port_id Peer port ID
63 * @ret rc Return status code
65 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
66 struct fc_port_id *peer_port_id );
70 * Free name server query
72 * @v refcnt Reference count
74 static void fc_ns_query_free ( struct refcnt *refcnt ) {
75 struct fc_ns_query *query =
76 container_of ( refcnt, struct fc_ns_query, refcnt );
78 fc_peer_put ( query->peer );
79 fc_port_put ( query->port );
84 * Close name server query
86 * @v query Name server query
87 * @v rc Reason for close
89 static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {
92 process_del ( &query->process );
94 /* Shut down interfaces */
95 intf_shutdown ( &query->xchg, rc );
99 * Receive name server query response
101 * @v query Name server query
102 * @v iobuf I/O buffer
103 * @v meta Data transfer metadata
104 * @ret rc Return status code
106 static int fc_ns_query_deliver ( struct fc_ns_query *query,
107 struct io_buffer *iobuf,
108 struct xfer_metadata *meta __unused ) {
109 union fc_ns_response *resp = iobuf->data;
110 struct fc_port_id *peer_port_id;
114 if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
115 DBGC ( query, "FCNS %p received underlength response (%zd "
116 "bytes)\n", query, iob_len ( iobuf ) );
121 /* Handle response */
122 switch ( ntohs ( resp->ct.code ) ) {
124 if ( iob_len ( iobuf ) < sizeof ( resp->gid_pn ) ) {
125 DBGC ( query, "FCNS %p received underlength accept "
126 "response (%zd bytes)\n",
127 query, iob_len ( iobuf ) );
131 peer_port_id = &resp->gid_pn.port_id.port_id;
132 DBGC ( query, "FCNS %p resolved %s to %s via %s\n",
133 query, fc_ntoa ( &query->peer->port_wwn ),
134 fc_id_ntoa ( peer_port_id ), query->port->name );
135 if ( ( rc = query->done ( query->peer, query->port,
136 peer_port_id ) ) != 0 )
140 DBGC ( query, "FCNS %p rejected (reason %02x explanation "
141 "%02x)\n", query, resp->reject.ct.reason,
142 resp->reject.ct.explanation );
145 DBGC ( query, "FCNS %p received invalid response code %04x\n",
146 query, ntohs ( resp->ct.code ) );
154 fc_ns_query_close ( query, rc );
159 * Name server query process
161 * @v query Name server query
163 static void fc_ns_query_step ( struct fc_ns_query *query ) {
164 struct xfer_metadata meta;
165 struct fc_ns_gid_pn_request gid_pn;
169 /* Create exchange */
170 if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
172 FC_TYPE_CT ) ) < 0 ) {
174 DBGC ( query, "FCNS %p could not create exchange: %s\n",
175 query, strerror ( rc ) );
176 fc_ns_query_close ( query, rc );
180 /* Construct query request */
181 memset ( &gid_pn, 0, sizeof ( gid_pn ) );
182 gid_pn.ct.revision = FC_CT_REVISION;
183 gid_pn.ct.type = FC_GS_TYPE_DS;
184 gid_pn.ct.subtype = FC_DS_SUBTYPE_NAME;
185 gid_pn.ct.code = htons ( FC_NS_GET ( FC_NS_PORT_NAME, FC_NS_PORT_ID ));
186 memcpy ( &gid_pn.port_wwn, &query->peer->port_wwn,
187 sizeof ( gid_pn.port_wwn ) );
188 memset ( &meta, 0, sizeof ( meta ) );
189 meta.flags = XFER_FL_OVER;
192 if ( ( rc = xfer_deliver_raw_meta ( &query->xchg, &gid_pn,
193 sizeof ( gid_pn ), &meta ) ) != 0){
194 DBGC ( query, "FCNS %p could not deliver query: %s\n",
195 query, strerror ( rc ) );
196 fc_ns_query_close ( query, rc );
201 /** Name server exchange interface operations */
202 static struct interface_operation fc_ns_query_xchg_op[] = {
203 INTF_OP ( xfer_deliver, struct fc_ns_query *, fc_ns_query_deliver ),
204 INTF_OP ( intf_close, struct fc_ns_query *, fc_ns_query_close ),
207 /** Name server exchange interface descriptor */
208 static struct interface_descriptor fc_ns_query_xchg_desc =
209 INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );
211 /** Name server process descriptor */
212 static struct process_descriptor fc_ns_query_process_desc =
213 PROC_DESC_ONCE ( struct fc_ns_query, process, fc_ns_query_step );
216 * Issue Fibre Channel name server query
218 * @v peer Fibre Channel peer
219 * @v port Fibre Channel port
220 * @ret rc Return status code
222 int fc_ns_query ( struct fc_peer *peer, struct fc_port *port,
223 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
224 struct fc_port_id *peer_port_id ) ) {
225 struct fc_ns_query *query;
227 /* Allocate and initialise structure */
228 query = zalloc ( sizeof ( *query ) );
231 ref_init ( &query->refcnt, fc_ns_query_free );
232 intf_init ( &query->xchg, &fc_ns_query_xchg_desc, &query->refcnt );
233 process_init ( &query->process, &fc_ns_query_process_desc,
235 query->peer = fc_peer_get ( peer );
236 query->port = fc_port_get ( port );
239 DBGC ( query, "FCNS %p querying %s via %s\n",
240 query, fc_ntoa ( &query->peer->port_wwn ), port->name );
242 /* Mortalise self and return */
243 ref_put ( &query->refcnt );