You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fcmgmt_cmd.c 5.0KB

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