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