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>
51 #include <sys/sysctl.h>
53 #include <infiniband/umad.h>
55 #include <ibdiag_common.h>
57 static char *node_type_str[] = {
65 static void ca_dump(umad_ca_t * ca)
70 ((unsigned)ca->node_type <=
71 IB_NODE_MAX ? node_type_str[ca->node_type] : "???"),
73 printf("\t%s type: %s\n",
74 ((unsigned)ca->node_type <=
75 IB_NODE_MAX ? node_type_str[ca->node_type] : "???"),
77 printf("\tNumber of ports: %d\n", ca->numports);
78 printf("\tFirmware version: %s\n", ca->fw_ver);
79 printf("\tHardware version: %s\n", ca->hw_ver);
80 printf("\tNode GUID: 0x%016" PRIx64 "\n", ntohll(ca->node_guid));
81 printf("\tSystem image GUID: 0x%016" PRIx64 "\n",
82 ntohll(ca->system_guid));
85 static char *port_state_str[] = {
93 static char *port_phy_state_str[] = {
98 "PortConfigurationTraining",
104 static int ret_code(void)
113 int sys_read_string(const char *dir_name, const char *file_name, char *str, int max_len)
118 snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
120 for (s = &path[0]; *s != '\0'; s++)
125 if (sysctlbyname(&path[1], str, &len, NULL, 0) == -1)
128 str[(len < max_len) ? len : max_len - 1] = 0;
130 if ((s = strrchr(str, '\n')))
136 static int is_fdr10(umad_port_t *port)
143 len = snprintf(port_dir, sizeof(port_dir), "%s/%s/%s/%d",
144 SYS_INFINIBAND, port->ca_name, SYS_CA_PORTS_DIR,
146 if (len < 0 || len > sizeof(port_dir))
149 if (sys_read_string(port_dir, SYS_PORT_RATE, rate, sizeof(rate)) == 0) {
150 if ((p = strchr(rate, ')'))) {
151 if (!strncasecmp(p - 5, "fdr10", 5))
160 static int port_dump(umad_port_t * port, int alone)
173 printf("%sPort %d:\n", hdrpre, port->portnum);
174 printf("%sState: %s\n", pre,
175 (unsigned)port->state <=
176 4 ? port_state_str[port->state] : "???");
177 printf("%sPhysical state: %s\n", pre,
178 (unsigned)port->phys_state <=
179 7 ? port_phy_state_str[port->phys_state] : "???");
181 printf("%sRate: %d (FDR10)\n", pre, port->rate);
184 printf("%sRate: %d\n", pre, port->rate);
186 printf("%sRate: 2.5\n", pre);
187 printf("%sBase lid: %d\n", pre, port->base_lid);
188 printf("%sLMC: %d\n", pre, port->lmc);
189 printf("%sSM lid: %d\n", pre, port->sm_lid);
190 printf("%sCapability mask: 0x%08x\n", pre, ntohl(port->capmask));
191 printf("%sPort GUID: 0x%016" PRIx64 "\n", pre, ntohll(port->port_guid));
192 #ifdef HAVE_UMAD_PORT_LINK_LAYER
193 printf("%sLink layer: %s\n", pre, port->link_layer);
198 static int ca_stat(char *ca_name, int portnum, int no_ports)
203 if ((r = umad_get_ca(ca_name, &ca)) < 0)
209 if (!no_ports && portnum >= 0) {
210 if (portnum > ca.numports || !ca.ports[portnum]) {
211 IBWARN("%s: '%s' has no port number %d - max (%d)",
212 ((unsigned)ca.node_type <=
213 IB_NODE_MAX ? node_type_str[ca.node_type] :
214 "???"), ca_name, portnum, ca.numports);
218 ((unsigned)ca.node_type <=
219 IB_NODE_MAX ? node_type_str[ca.node_type] : "???"),
221 port_dump(ca.ports[portnum], 1);
225 /* print ca header */
231 for (portnum = 0; portnum <= ca.numports; portnum++)
232 port_dump(ca.ports[portnum], 0);
237 static int ports_list(char names[][UMAD_CA_NAME_LEN], int n)
242 for (i = 0, found = 0; i < n && found < 64; i++) {
244 umad_get_ca_portguids(names[i], guids + found,
250 for (i = 0; i < found; i++)
252 printf("0x%016" PRIx64 "\n", ntohll(guids[i]));
256 static int list_only, short_format, list_ports;
258 static int process_opt(void *context, int ch, char *optarg)
276 int main(int argc, char *argv[])
278 char names[UMAD_MAX_DEVICES][UMAD_CA_NAME_LEN];
282 const struct ibdiag_opt opts[] = {
283 {"list_of_cas", 'l', 0, NULL, "list all IB devices"},
284 {"short", 's', 0, NULL, "short output"},
285 {"port_list", 'p', 0, NULL, "show port list"},
288 char usage_args[] = "<ca_name> [portnum]";
289 const char *usage_examples[] = {
290 "-l # list all IB devices",
291 "mthca0 2 # stat port 2 of 'mthca0'",
295 ibdiag_process_opts(argc, argv, NULL, "CDeGKLPsty", opts, process_opt,
296 usage_args, usage_examples);
302 dev_port = strtol(argv[1], 0, 0);
305 IBPANIC("can't init UMAD library");
307 if ((n = umad_get_cas_names(names, UMAD_MAX_DEVICES)) < 0)
308 IBPANIC("can't list IB device names");
311 for (i = 0; i < n; i++)
312 if (!strncmp(names[i], argv[0], sizeof names[i]))
315 IBPANIC("'%s' IB device can't be found", argv[0]);
317 strncpy(names[0], argv[0], sizeof(names[0])-1);
318 names[0][sizeof(names[0])-1] = '\0';
323 if (ports_list(names, n) < 0)
324 IBPANIC("can't list ports");
328 for (i = 0; i < n; i++) {
330 printf("%s\n", names[i]);
331 else if (ca_stat(names[i], dev_port, short_format) < 0)
332 IBPANIC("stat of IB device '%s' failed", names[i]);