2 * Copyright (c) 2004-2009 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2011 Mellanox Technologies LTD. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #endif /* HAVE_CONFIG_H */
46 #include <netinet/in.h>
47 #include <sys/types.h>
52 #include <infiniband/umad.h>
54 #include <ibdiag_common.h>
56 static char *node_type_str[] = {
64 static void ca_dump(umad_ca_t * ca)
69 ((unsigned)ca->node_type <=
70 IB_NODE_MAX ? node_type_str[ca->node_type] : "???"),
72 printf("\t%s type: %s\n",
73 ((unsigned)ca->node_type <=
74 IB_NODE_MAX ? node_type_str[ca->node_type] : "???"),
76 printf("\tNumber of ports: %d\n", ca->numports);
77 printf("\tFirmware version: %s\n", ca->fw_ver);
78 printf("\tHardware version: %s\n", ca->hw_ver);
79 printf("\tNode GUID: 0x%016" PRIx64 "\n", ntohll(ca->node_guid));
80 printf("\tSystem image GUID: 0x%016" PRIx64 "\n",
81 ntohll(ca->system_guid));
84 static char *port_state_str[] = {
92 static char *port_phy_state_str[] = {
97 "PortConfigurationTraining",
103 static int ret_code(void)
112 int sys_read_string(const char *dir_name, const char *file_name, char *str, int max_len)
117 snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
119 for (s = &path[0]; *s != '\0'; s++)
124 if (sysctlbyname(&path[1], str, &len, NULL, 0) == -1)
127 str[(len < max_len) ? len : max_len - 1] = 0;
129 if ((s = strrchr(str, '\n')))
135 static int is_fdr10(umad_port_t *port)
142 len = snprintf(port_dir, sizeof(port_dir), "%s/%s/%s/%d",
143 SYS_INFINIBAND, port->ca_name, SYS_CA_PORTS_DIR,
145 if (len < 0 || len > sizeof(port_dir))
148 if (sys_read_string(port_dir, SYS_PORT_RATE, rate, sizeof(rate)) == 0) {
149 if ((p = strchr(rate, ')'))) {
150 if (!strncasecmp(p - 5, "fdr10", 5))
159 static int port_dump(umad_port_t * port, int alone)
172 printf("%sPort %d:\n", hdrpre, port->portnum);
173 printf("%sState: %s\n", pre,
174 (unsigned)port->state <=
175 4 ? port_state_str[port->state] : "???");
176 printf("%sPhysical state: %s\n", pre,
177 (unsigned)port->phys_state <=
178 7 ? port_phy_state_str[port->phys_state] : "???");
180 printf("%sRate: %d (FDR10)\n", pre, port->rate);
183 printf("%sRate: %d\n", pre, port->rate);
185 printf("%sRate: 2.5\n", pre);
186 printf("%sBase lid: %d\n", pre, port->base_lid);
187 printf("%sLMC: %d\n", pre, port->lmc);
188 printf("%sSM lid: %d\n", pre, port->sm_lid);
189 printf("%sCapability mask: 0x%08x\n", pre, ntohl(port->capmask));
190 printf("%sPort GUID: 0x%016" PRIx64 "\n", pre, ntohll(port->port_guid));
191 #ifdef HAVE_UMAD_PORT_LINK_LAYER
192 printf("%sLink layer: %s\n", pre, port->link_layer);
197 static int ca_stat(char *ca_name, int portnum, int no_ports)
202 if ((r = umad_get_ca(ca_name, &ca)) < 0)
208 if (!no_ports && portnum >= 0) {
209 if (portnum > ca.numports || !ca.ports[portnum]) {
210 IBWARN("%s: '%s' has no port number %d - max (%d)",
211 ((unsigned)ca.node_type <=
212 IB_NODE_MAX ? node_type_str[ca.node_type] :
213 "???"), ca_name, portnum, ca.numports);
217 ((unsigned)ca.node_type <=
218 IB_NODE_MAX ? node_type_str[ca.node_type] : "???"),
220 port_dump(ca.ports[portnum], 1);
224 /* print ca header */
230 for (portnum = 0; portnum <= ca.numports; portnum++)
231 port_dump(ca.ports[portnum], 0);
236 static int ports_list(char names[][UMAD_CA_NAME_LEN], int n)
241 for (i = 0, found = 0; i < n && found < 64; i++) {
243 umad_get_ca_portguids(names[i], guids + found,
249 for (i = 0; i < found; i++)
251 printf("0x%016" PRIx64 "\n", ntohll(guids[i]));
255 static int list_only, short_format, list_ports;
257 static int process_opt(void *context, int ch, char *optarg)
275 int main(int argc, char *argv[])
277 char names[UMAD_MAX_DEVICES][UMAD_CA_NAME_LEN];
281 const struct ibdiag_opt opts[] = {
282 {"list_of_cas", 'l', 0, NULL, "list all IB devices"},
283 {"short", 's', 0, NULL, "short output"},
284 {"port_list", 'p', 0, NULL, "show port list"},
287 char usage_args[] = "<ca_name> [portnum]";
288 const char *usage_examples[] = {
289 "-l # list all IB devices",
290 "mthca0 2 # stat port 2 of 'mthca0'",
294 ibdiag_process_opts(argc, argv, NULL, "CDeGKLPsty", opts, process_opt,
295 usage_args, usage_examples);
301 dev_port = strtol(argv[1], 0, 0);
304 IBPANIC("can't init UMAD library");
306 if ((n = umad_get_cas_names(names, UMAD_MAX_DEVICES)) < 0)
307 IBPANIC("can't list IB device names");
310 for (i = 0; i < n; i++)
311 if (!strncmp(names[i], argv[0], sizeof names[i]))
314 IBPANIC("'%s' IB device can't be found", argv[0]);
316 strncpy(names[0], argv[0], sizeof(names[0])-1);
317 names[0][sizeof(names[0])-1] = '\0';
322 if (ports_list(names, n) < 0)
323 IBPANIC("can't list ports");
327 for (i = 0; i < n; i++) {
329 printf("%s\n", names[i]);
330 else if (ca_stat(names[i], dev_port, short_format) < 0)
331 IBPANIC("stat of IB device '%s' failed", names[i]);