[zbin] Fix check for existence of most recent output byte
[ipxe.git] / src / net / fcns.c
1 /*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <byteswap.h>
27 #include <ipxe/interface.h>
28 #include <ipxe/iobuf.h>
29 #include <ipxe/process.h>
30 #include <ipxe/xfer.h>
31 #include <ipxe/fc.h>
32 #include <ipxe/fcns.h>
33
34 /** @file
35 *
36 * Fibre Channel name server lookups
37 *
38 */
39
40 /** A Fibre Channel name server query */
41 struct fc_ns_query {
42 /** Reference count */
43 struct refcnt refcnt;
44 /** Fibre Channel exchange */
45 struct interface xchg;
46
47 /** Fibre Channel peer */
48 struct fc_peer *peer;
49 /** Fibre Channel port */
50 struct fc_port *port;
51
52 /** Process */
53 struct process process;
54 /** Success handler
55 *
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
60 */
61 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
62 struct fc_port_id *peer_port_id );
63 };
64
65 /**
66 * Free name server query
67 *
68 * @v refcnt Reference count
69 */
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 );
73
74 fc_peer_put ( query->peer );
75 fc_port_put ( query->port );
76 free ( query );
77 }
78
79 /**
80 * Close name server query
81 *
82 * @v query Name server query
83 * @v rc Reason for close
84 */
85 static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {
86
87 /* Stop process */
88 process_del ( &query->process );
89
90 /* Shut down interfaces */
91 intf_shutdown ( &query->xchg, rc );
92 }
93
94 /**
95 * Receive name server query response
96 *
97 * @v query Name server query
98 * @v iobuf I/O buffer
99 * @v meta Data transfer metadata
100 * @ret rc Return status code
101 */
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;
107 int rc;
108
109 /* Sanity check */
110 if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
111 DBGC ( query, "FCNS %p received underlength response (%zd "
112 "bytes)\n", query, iob_len ( iobuf ) );
113 rc = -EINVAL;
114 goto done;
115 }
116
117 /* Handle response */
118 switch ( ntohs ( resp->ct.code ) ) {
119 case FC_GS_ACCEPT:
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 ) );
124 rc = -EINVAL;
125 goto done;
126 }
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 )
133 goto done;
134 break;
135 case FC_GS_REJECT:
136 DBGC ( query, "FCNS %p rejected (reason %02x explanation "
137 "%02x)\n", query, resp->reject.ct.reason,
138 resp->reject.ct.explanation );
139 break;
140 default:
141 DBGC ( query, "FCNS %p received invalid response code %04x\n",
142 query, ntohs ( resp->ct.code ) );
143 rc = -ENOTSUP;
144 goto done;
145 }
146
147 rc = 0;
148 done:
149 free_iob ( iobuf );
150 fc_ns_query_close ( query, rc );
151 return rc;
152 }
153
154 /**
155 * Name server query process
156 *
157 * @v query Name server query
158 */
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;
162 int xchg_id;
163 int rc;
164
165 /* Create exchange */
166 if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
167 &fc_gs_port_id,
168 FC_TYPE_CT ) ) < 0 ) {
169 rc = xchg_id;
170 DBGC ( query, "FCNS %p could not create exchange: %s\n",
171 query, strerror ( rc ) );
172 fc_ns_query_close ( query, rc );
173 return;
174 }
175
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;
186
187 /* Send query */
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 );
193 return;
194 }
195 }
196
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 ),
201 };
202
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 );
206
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 );
210
211 /**
212 * Issue Fibre Channel name server query
213 *
214 * @v peer Fibre Channel peer
215 * @v port Fibre Channel port
216 * @ret rc Return status code
217 */
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;
222
223 /* Allocate and initialise structure */
224 query = zalloc ( sizeof ( *query ) );
225 if ( ! query )
226 return -ENOMEM;
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,
230 &query->refcnt );
231 query->peer = fc_peer_get ( peer );
232 query->port = fc_port_get ( port );
233 query->done = done;
234
235 DBGC ( query, "FCNS %p querying %s via %s\n",
236 query, fc_ntoa ( &query->peer->port_wwn ), port->name );
237
238 /* Mortalise self and return */
239 ref_put ( &query->refcnt );
240 return 0;
241 }