aboutsummaryrefslogtreecommitdiff
path: root/src/net/fcns.c
blob: be4dfea246167ee2a2bc60267270eb1068ed7f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/interface.h>
#include <ipxe/iobuf.h>
#include <ipxe/process.h>
#include <ipxe/xfer.h>
#include <ipxe/fc.h>
#include <ipxe/fcns.h>

/** @file
 *
 * Fibre Channel name server lookups
 *
 */

/** A Fibre Channel name server query */
struct fc_ns_query {
	/** Reference count */
	struct refcnt refcnt;
	/** Fibre Channel exchange */
	struct interface xchg;

	/** Fibre Channel peer */
	struct fc_peer *peer;
	/** Fibre Channel port */
	struct fc_port *port;

	/** Process */
	struct process process;
	/** Success handler
	 *
	 * @v peer		Fibre Channel peer
	 * @v port		Fibre Channel port
	 * @v peer_port_id	Peer port ID
	 * @ret rc		Return status code
	 */
	int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
			 struct fc_port_id *peer_port_id );
};

/**
 * Free name server query
 *
 * @v refcnt		Reference count
 */
static void fc_ns_query_free ( struct refcnt *refcnt ) {
	struct fc_ns_query *query =
		container_of ( refcnt, struct fc_ns_query, refcnt );

	fc_peer_put ( query->peer );
	fc_port_put ( query->port );
	free ( query );
}

/**
 * Close name server query
 *
 * @v query		Name server query
 * @v rc		Reason for close
 */
static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {

	/* Stop process */
	process_del ( &query->process );

	/* Shut down interfaces */
	intf_shutdown ( &query->xchg, rc );
}

/**
 * Receive name server query response
 *
 * @v query		Name server query
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int fc_ns_query_deliver ( struct fc_ns_query *query,
				 struct io_buffer *iobuf,
				 struct xfer_metadata *meta __unused ) {
	union fc_ns_response *resp = iobuf->data;
	struct fc_port_id *peer_port_id;
	int rc;

	/* Sanity check */
	if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
		DBGC ( query, "FCNS %p received underlength response (%zd "
		       "bytes)\n", query, iob_len ( iobuf ) );
		rc = -EINVAL;
		goto done;
	}

	/* Handle response */
	switch ( ntohs ( resp->ct.code ) ) {
	case FC_GS_ACCEPT:
		if ( iob_len ( iobuf ) < sizeof ( resp->gid_pn ) ) {
			DBGC ( query, "FCNS %p received underlength accept "
			       "response (%zd bytes)\n",
			       query, iob_len ( iobuf ) );
			rc = -EINVAL;
			goto done;
		}
		peer_port_id = &resp->gid_pn.port_id.port_id;
		DBGC ( query, "FCNS %p resolved %s to %s via %s\n",
		       query, fc_ntoa ( &query->peer->port_wwn ),
		       fc_id_ntoa ( peer_port_id ), query->port->name );
		if ( ( rc = query->done ( query->peer, query->port,
					  peer_port_id ) ) != 0 )
			goto done;
		break;
	case FC_GS_REJECT:
		DBGC ( query, "FCNS %p rejected (reason %02x explanation "
		       "%02x)\n", query, resp->reject.ct.reason,
		       resp->reject.ct.explanation );
		break;
	default:
		DBGC ( query, "FCNS %p received invalid response code %04x\n",
		       query, ntohs ( resp->ct.code ) );
		rc = -ENOTSUP;
		goto done;
	}

	rc = 0;
 done:
	free_iob ( iobuf );
	fc_ns_query_close ( query, rc );
	return rc;
}

/**
 * Name server query process
 *
 * @v query		Name server query
 */
static void fc_ns_query_step ( struct fc_ns_query *query ) {
	struct xfer_metadata meta;
	struct fc_ns_gid_pn_request gid_pn;
	int xchg_id;
	int rc;

	/* Create exchange */
	if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
					     &fc_gs_port_id,
					     FC_TYPE_CT ) ) < 0 ) {
		rc = xchg_id;
		DBGC ( query, "FCNS %p could not create exchange: %s\n",
		       query, strerror ( rc ) );
		fc_ns_query_close ( query, rc );
		return;
	}

	/* Construct query request */
	memset ( &gid_pn, 0, sizeof ( gid_pn ) );
	gid_pn.ct.revision = FC_CT_REVISION;
	gid_pn.ct.type = FC_GS_TYPE_DS;
	gid_pn.ct.subtype = FC_DS_SUBTYPE_NAME;
	gid_pn.ct.code = htons ( FC_NS_GET ( FC_NS_PORT_NAME, FC_NS_PORT_ID ));
	memcpy ( &gid_pn.port_wwn, &query->peer->port_wwn,
		 sizeof ( gid_pn.port_wwn ) );
	memset ( &meta, 0, sizeof ( meta ) );
	meta.flags = XFER_FL_OVER;

	/* Send query */
	if ( ( rc = xfer_deliver_raw_meta ( &query->xchg, &gid_pn,
					    sizeof ( gid_pn ), &meta ) ) != 0){
		DBGC ( query, "FCNS %p could not deliver query: %s\n",
		       query, strerror ( rc ) );
		fc_ns_query_close ( query, rc );
		return;
	}
}

/** Name server exchange interface operations */
static struct interface_operation fc_ns_query_xchg_op[] = {
	INTF_OP ( xfer_deliver, struct fc_ns_query *, fc_ns_query_deliver ),
	INTF_OP ( intf_close, struct fc_ns_query *, fc_ns_query_close ),
};

/** Name server exchange interface descriptor */
static struct interface_descriptor fc_ns_query_xchg_desc =
	INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );

/** Name server process descriptor */
static struct process_descriptor fc_ns_query_process_desc =
	PROC_DESC_ONCE ( struct fc_ns_query, process, fc_ns_query_step );

/**
 * Issue Fibre Channel name server query
 *
 * @v peer		Fibre Channel peer
 * @v port		Fibre Channel port
 * @ret rc		Return status code
 */
int fc_ns_query ( struct fc_peer *peer, struct fc_port *port,
		  int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
				   struct fc_port_id *peer_port_id ) ) {
	struct fc_ns_query *query;

	/* Allocate and initialise structure */
	query = zalloc ( sizeof ( *query ) );
	if ( ! query )
		return -ENOMEM;
	ref_init ( &query->refcnt, fc_ns_query_free );
	intf_init ( &query->xchg, &fc_ns_query_xchg_desc, &query->refcnt );
	process_init ( &query->process, &fc_ns_query_process_desc,
		       &query->refcnt );
	query->peer = fc_peer_get ( peer );
	query->port = fc_port_get ( port );
	query->done = done;

	DBGC ( query, "FCNS %p querying %s via %s\n",
	       query, fc_ntoa ( &query->peer->port_wwn ), port->name );

	/* Mortalise self and return */
	ref_put ( &query->refcnt );
	return 0;
}