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
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/interface.h>
28 #include <ipxe/iobuf.h>
29 #include <ipxe/process.h>
30 #include <ipxe/xfer.h>
32 #include <ipxe/fcns.h>
36 * Fibre Channel name server lookups
40 /** A Fibre Channel name server query */
42 /** Reference count */
44 /** Fibre Channel exchange */
45 struct interface xchg;
47 /** Fibre Channel peer */
49 /** Fibre Channel port */
53 struct process process;
56 * @v peer Fibre Channel peer
57 * @v port Fibre Channel port
58 * @v peer_port_id Peer port ID
59 * @ret rc Return status code
61 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
62 struct fc_port_id *peer_port_id );
66 * Free name server query
68 * @v refcnt Reference count
70 static void fc_ns_query_free ( struct refcnt *refcnt ) {
71 struct fc_ns_query *query =
72 container_of ( refcnt, struct fc_ns_query, refcnt );
74 fc_peer_put ( query->peer );
75 fc_port_put ( query->port );
80 * Close name server query
82 * @v query Name server query
83 * @v rc Reason for close
85 static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {
88 process_del ( &query->process );
90 /* Shut down interfaces */
91 intf_shutdown ( &query->xchg, rc );
95 * Receive name server query response
97 * @v query Name server query
99 * @v meta Data transfer metadata
100 * @ret rc Return status code
102 static int fc_ns_query_deliver ( struct fc_ns_query *query,
103 struct io_buffer *iobuf,
104 struct xfer_metadata *meta __unused ) {
105 union fc_ns_response *resp = iobuf->data;
106 struct fc_port_id *peer_port_id;
110 if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
111 DBGC ( query, "FCNS %p received underlength response (%zd "
112 "bytes)\n", query, iob_len ( iobuf ) );
117 /* Handle response */
118 switch ( ntohs ( resp->ct.code ) ) {
120 if ( iob_len ( iobuf ) < sizeof ( resp->gid_pn ) ) {
121 DBGC ( query, "FCNS %p received underlength accept "
122 "response (%zd bytes)\n",
123 query, iob_len ( iobuf ) );
127 peer_port_id = &resp->gid_pn.port_id.port_id;
128 DBGC ( query, "FCNS %p resolved %s to %s via %s\n",
129 query, fc_ntoa ( &query->peer->port_wwn ),
130 fc_id_ntoa ( peer_port_id ), query->port->name );
131 if ( ( rc = query->done ( query->peer, query->port,
132 peer_port_id ) ) != 0 )
136 DBGC ( query, "FCNS %p rejected (reason %02x explanation "
137 "%02x)\n", query, resp->reject.ct.reason,
138 resp->reject.ct.explanation );
141 DBGC ( query, "FCNS %p received invalid response code %04x\n",
142 query, ntohs ( resp->ct.code ) );
150 fc_ns_query_close ( query, rc );
155 * Name server query process
157 * @v query Name server query
159 static void fc_ns_query_step ( struct fc_ns_query *query ) {
160 struct xfer_metadata meta;
161 struct fc_ns_gid_pn_request gid_pn;
165 /* Create exchange */
166 if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
168 FC_TYPE_CT ) ) < 0 ) {
170 DBGC ( query, "FCNS %p could not create exchange: %s\n",
171 query, strerror ( rc ) );
172 fc_ns_query_close ( query, rc );
176 /* Construct query request */
177 memset ( &gid_pn, 0, sizeof ( gid_pn ) );
178 gid_pn.ct.revision = FC_CT_REVISION;
179 gid_pn.ct.type = FC_GS_TYPE_DS;
180 gid_pn.ct.subtype = FC_DS_SUBTYPE_NAME;
181 gid_pn.ct.code = htons ( FC_NS_GET ( FC_NS_PORT_NAME, FC_NS_PORT_ID ));
182 memcpy ( &gid_pn.port_wwn, &query->peer->port_wwn,
183 sizeof ( gid_pn.port_wwn ) );
184 memset ( &meta, 0, sizeof ( meta ) );
185 meta.flags = XFER_FL_OVER;
188 if ( ( rc = xfer_deliver_raw_meta ( &query->xchg, &gid_pn,
189 sizeof ( gid_pn ), &meta ) ) != 0){
190 DBGC ( query, "FCNS %p could not deliver query: %s\n",
191 query, strerror ( rc ) );
192 fc_ns_query_close ( query, rc );
197 /** Name server exchange interface operations */
198 static struct interface_operation fc_ns_query_xchg_op[] = {
199 INTF_OP ( xfer_deliver, struct fc_ns_query *, fc_ns_query_deliver ),
200 INTF_OP ( intf_close, struct fc_ns_query *, fc_ns_query_close ),
203 /** Name server exchange interface descriptor */
204 static struct interface_descriptor fc_ns_query_xchg_desc =
205 INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );
207 /** Name server process descriptor */
208 static struct process_descriptor fc_ns_query_process_desc =
209 PROC_DESC_ONCE ( struct fc_ns_query, process, fc_ns_query_step );
212 * Issue Fibre Channel name server query
214 * @v peer Fibre Channel peer
215 * @v port Fibre Channel port
216 * @ret rc Return status code
218 int fc_ns_query ( struct fc_peer *peer, struct fc_port *port,
219 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
220 struct fc_port_id *peer_port_id ) ) {
221 struct fc_ns_query *query;
223 /* Allocate and initialise structure */
224 query = zalloc ( sizeof ( *query ) );
227 ref_init ( &query->refcnt, fc_ns_query_free );
228 intf_init ( &query->xchg, &fc_ns_query_xchg_desc, &query->refcnt );
229 process_init ( &query->process, &fc_ns_query_process_desc,
231 query->peer = fc_peer_get ( peer );
232 query->port = fc_port_get ( port );
235 DBGC ( query, "FCNS %p querying %s via %s\n",
236 query, fc_ntoa ( &query->peer->port_wwn ), port->name );
238 /* Mortalise self and return */
239 ref_put ( &query->refcnt );