[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / src / hci / commands / fcmgmt_cmd.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 * 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.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <strings.h>
30 #include <ipxe/fc.h>
31 #include <ipxe/fcels.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/tables.h>
35 #include <usr/fcmgmt.h>
36
37 /** @file
38 *
39 * Fibre Channel management commands
40 *
41 */
42
43 /**
44 * Parse Fibre Channel port name
45 *
46 * @v text Text
47 * @ret port Fibre Channel port
48 * @ret rc Return status code
49 */
50 static int parse_fc_port ( char *text, struct fc_port **port ) {
51
52 /* Sanity check */
53 assert ( text != NULL );
54
55 /* Find Fibre Channel port */
56 *port = fc_port_find ( text );
57 if ( ! *port ) {
58 printf ( "\"%s\": no such port\n", text );
59 return -ENODEV;
60 }
61
62 return 0;
63 }
64
65 /**
66 * Parse Fibre Channel port ID
67 *
68 * @v text Text
69 * @ret port_id Fibre Channel port ID
70 * @ret rc Return status code
71 */
72 static int parse_fc_port_id ( char *text, struct fc_port_id *port_id ) {
73 int rc;
74
75 /* Sanity check */
76 assert ( text != NULL );
77
78 /* Parse port ID */
79 if ( ( rc = fc_id_aton ( text, port_id ) ) != 0 ) {
80 printf ( "\"%s\": invalid port ID\n", text );
81 return -EINVAL;
82 }
83
84 return 0;
85 }
86
87 /**
88 * Parse Fibre Channel ELS handler name
89 *
90 * @v text Text
91 * @ret handler Fibre Channel ELS handler
92 * @ret rc Return status code
93 */
94 static int parse_fc_els_handler ( char *text, struct fc_els_handler **handler ){
95
96 for_each_table_entry ( (*handler), FC_ELS_HANDLERS ) {
97 if ( strcasecmp ( (*handler)->name, text ) == 0 )
98 return 0;
99 }
100
101 printf ( "\"%s\": unrecognised ELS\n", text );
102 return -ENOENT;
103 }
104
105 /** "fcstat" options */
106 struct fcstat_options {};
107
108 /** "fcstat" option list */
109 static struct option_descriptor fcstat_opts[] = {};
110
111 /** "fcstat" command descriptor */
112 static struct command_descriptor fcstat_cmd =
113 COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, NULL );
114
115 /**
116 * The "fcstat" command
117 *
118 * @v argc Argument count
119 * @v argv Argument list
120 * @ret rc Return status code
121 */
122 static int fcstat_exec ( int argc, char **argv ) {
123 struct fcstat_options opts;
124 struct fc_port *port;
125 struct fc_peer *peer;
126 int rc;
127
128 /* Parse options */
129 if ( ( rc = parse_options ( argc, argv, &fcstat_cmd, &opts ) ) != 0 )
130 return rc;
131
132 list_for_each_entry ( port, &fc_ports, list )
133 fcportstat ( port );
134 list_for_each_entry ( peer, &fc_peers, list )
135 fcpeerstat ( peer );
136
137 return 0;
138 }
139
140 /** "fcels" options */
141 struct fcels_options {
142 /** Fibre Channel port */
143 struct fc_port *port;
144 /** Fibre Channel peer port ID */
145 struct fc_port_id peer_port_id;
146 };
147
148 /** "fcels" option list */
149 static struct option_descriptor fcels_opts[] = {
150 OPTION_DESC ( "port", 'p', required_argument,
151 struct fcels_options, port, parse_fc_port ),
152 OPTION_DESC ( "id", 'i', required_argument,
153 struct fcels_options, peer_port_id, parse_fc_port_id ),
154 };
155
156 /** "fcels" command descriptor */
157 static struct command_descriptor fcels_cmd =
158 COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1, "<request>" );
159
160 /**
161 * The "fcels" command
162 *
163 * @v argc Argument count
164 * @v argv Argument list
165 * @ret rc Return status code
166 */
167 static int fcels_exec ( int argc, char **argv ) {
168 struct fcels_options opts;
169 struct fc_els_handler *handler;
170 struct fc_port_id *id;
171 int rc;
172
173 /* Parse options */
174 if ( ( rc = parse_options ( argc, argv, &fcels_cmd, &opts ) ) != 0 )
175 return rc;
176
177 /* Parse ELS handler */
178 if ( ( rc = parse_fc_els_handler ( argv[optind], &handler ) ) != 0 )
179 return rc;
180
181 /* Use first port if no port specified */
182 if ( ! opts.port ) {
183 opts.port = list_first_entry ( &fc_ports, struct fc_port,
184 list );
185 if ( ! opts.port ) {
186 printf ( "No ports\n" );
187 return -ENODEV;
188 }
189 }
190
191 /* Use link peer port ID if no peer port ID specified */
192 id = &opts.peer_port_id;
193 if ( memcmp ( id, &fc_empty_port_id, sizeof ( *id ) ) == 0 ) {
194 if ( fc_link_ok ( &opts.port->link ) &&
195 ! ( opts.port->flags & FC_PORT_HAS_FABRIC ) ) {
196 id = &opts.port->ptp_link_port_id;
197 } else {
198 id = &fc_f_port_id;
199 }
200 }
201
202 /** Issue ELS */
203 if ( ( rc = fcels ( opts.port, id, handler ) ) != 0 )
204 return rc;
205
206 return 0;
207 }
208
209 /** Fibre Channel management commands */
210 struct command fcmgmt_commands[] __command = {
211 {
212 .name = "fcstat",
213 .exec = fcstat_exec,
214 },
215 {
216 .name = "fcels",
217 .exec = fcels_exec,
218 },
219 };