2 * Copyright (c) 2004-2008 Voltaire Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #endif /* HAVE_CONFIG_H */
46 #include <infiniband/common.h>
47 #include <infiniband/umad.h>
48 #include <infiniband/mad.h>
50 #include "ibdiag_common.h"
53 #define DEBUG if (verbose>1) IBWARN
55 static int dest_type = IB_DEST_LID;
58 char *argv0 = "ibportstate";
60 /*******************************************/
63 get_node_info(ib_portid_t *dest, uint8_t *data)
67 if (!smp_query(data, dest, IB_ATTR_NODE_INFO, 0, 0))
70 node_type = mad_get_field(data, 0, IB_NODE_TYPE_F);
71 if (node_type == IB_NODE_SWITCH) /* Switch NodeType ? */
78 get_port_info(ib_portid_t *dest, uint8_t *data, int portnum, int port_op)
83 if (!smp_query(data, dest, IB_ATTR_PORT_INFO, portnum, 0))
87 mad_dump_portstates(buf, sizeof buf, data, sizeof data);
88 mad_decode_field(data, IB_PORT_LINK_WIDTH_SUPPORTED_F, val);
89 mad_dump_field(IB_PORT_LINK_WIDTH_SUPPORTED_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
90 sprintf(buf+strlen(buf), "%s", "\n");
91 mad_decode_field(data, IB_PORT_LINK_WIDTH_ENABLED_F, val);
92 mad_dump_field(IB_PORT_LINK_WIDTH_ENABLED_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
93 sprintf(buf+strlen(buf), "%s", "\n");
94 mad_decode_field(data, IB_PORT_LINK_WIDTH_ACTIVE_F, val);
95 mad_dump_field(IB_PORT_LINK_WIDTH_ACTIVE_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
96 sprintf(buf+strlen(buf), "%s", "\n");
97 mad_decode_field(data, IB_PORT_LINK_SPEED_SUPPORTED_F, val);
98 mad_dump_field(IB_PORT_LINK_SPEED_SUPPORTED_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
99 sprintf(buf+strlen(buf), "%s", "\n");
100 mad_decode_field(data, IB_PORT_LINK_SPEED_ENABLED_F, val);
101 mad_dump_field(IB_PORT_LINK_SPEED_ENABLED_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
102 sprintf(buf+strlen(buf), "%s", "\n");
103 mad_decode_field(data, IB_PORT_LINK_SPEED_ACTIVE_F, val);
104 mad_dump_field(IB_PORT_LINK_SPEED_ACTIVE_F, buf + strlen(buf), sizeof buf - strlen(buf), val);
105 sprintf(buf+strlen(buf), "%s", "\n");
107 mad_decode_field(data, IB_PORT_LINK_SPEED_ENABLED_F, val);
108 mad_dump_field(IB_PORT_LINK_SPEED_ENABLED_F, buf, sizeof buf, val);
109 sprintf(buf+strlen(buf), "%s", "\n");
112 printf("# Port info: %s port %d\n%s", portid2str(dest), portnum, buf);
117 set_port_info(ib_portid_t *dest, uint8_t *data, int portnum, int port_op)
122 if (!smp_set(data, dest, IB_ATTR_PORT_INFO, portnum, 0))
126 mad_dump_portstates(buf, sizeof buf, data, sizeof data);
128 mad_decode_field(data, IB_PORT_LINK_SPEED_ENABLED_F, val);
129 mad_dump_field(IB_PORT_LINK_SPEED_ENABLED_F, buf, sizeof buf, val);
130 sprintf(buf+strlen(buf), "%s", "\n");
133 printf("\nAfter PortInfo set:\n");
134 printf("# Port info: %s port %d\n%s", portid2str(dest), portnum, buf);
139 get_link_width(int lwe, int lws)
148 get_link_speed(int lse, int lss)
157 validate_width(int width, int peerwidth, int lwa)
159 if ((width & 0x8) && (peerwidth & 0x8)) {
161 IBWARN("Peer ports operating at active width %d rather than 8 (12x)", lwa);
163 if ((width & 0x4) && (peerwidth & 0x4)) {
165 IBWARN("Peer ports operating at active width %d rather than 4 (8x)", lwa);
167 if ((width & 0x2) && (peerwidth & 0x2)) {
169 IBWARN("Peer ports operating at active width %d rather than 2 (4x)", lwa);
171 if ((width & 0x1) && (peerwidth & 0x1)) {
173 IBWARN("Peer ports operating at active width %d rather than 1 (1x)", lwa);
181 validate_speed(int speed, int peerspeed, int lsa)
183 if ((speed & 0x4) && (peerspeed & 0x4)) {
185 IBWARN("Peer ports operating at active speed %d rather than 4 (10.0 Gbps)", lsa);
187 if ((speed & 0x2) && (peerspeed & 0x2)) {
189 IBWARN("Peer ports operating at active speed %d rather than 2 (5.0 Gbps)", lsa);
191 if ((speed & 0x1) && (peerspeed & 0x1)) {
193 IBWARN("Peer ports operating at active speed %d rather than 1 (2.5 Gbps)", lsa);
204 if (!(basename = strrchr(argv0, '/')))
209 fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -v(erbose) -D(irect) -G(uid) -s smlid -V(ersion) -C ca_name -P ca_port "
210 "-t(imeout) timeout_ms] <dest dr_path|lid|guid> <portnum> [<op>]\n",
212 fprintf(stderr, "\tsupported ops: enable, disable, reset, speed, query\n");
213 fprintf(stderr, "\n\texamples:\n");
214 fprintf(stderr, "\t\t%s 3 1 disable\t\t\t# by lid\n", basename);
215 fprintf(stderr, "\t\t%s -G 0x2C9000100D051 1 enable\t# by guid\n", basename);
216 fprintf(stderr, "\t\t%s -D 0 1\t\t\t# (query) by direct route\n", basename);
217 fprintf(stderr, "\t\t%s 3 1 reset\t\t\t# by lid\n", basename);
218 fprintf(stderr, "\t\t%s 3 1 speed 1\t\t\t# by lid\n", basename);
223 main(int argc, char **argv)
225 int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
226 ib_portid_t portid = {0};
227 ib_portid_t *sm_id = 0, sm_portid = {0};
230 int timeout = 0, udebug = 0;
233 int port_op = 0; /* default to query */
236 int state, physstate, lwe, lws, lwa, lse, lss, lsa;
237 int peerlocalportnum, peerlwe, peerlws, peerlwa, peerlse, peerlss, peerlsa;
238 int width, peerwidth, peerspeed;
239 uint8_t data[IB_SMP_DATA_SIZE];
240 ib_portid_t peerportid = {0};
242 ib_portid_t selfportid = {0};
245 static char const str_opts[] = "C:P:t:s:devDGVhu";
246 static const struct option long_opts[] = {
249 { "debug", 0, 0, 'd'},
250 { "err_show", 0, 0, 'e'},
251 { "verbose", 0, 0, 'v'},
252 { "Direct", 0, 0, 'D'},
253 { "Guid", 0, 0, 'G'},
254 { "timeout", 1, 0, 't'},
256 { "Version", 0, 0, 'V'},
257 { "help", 0, 0, 'h'},
258 { "usage", 0, 0, 'u'},
265 int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
271 madrpc_show_errors(1);
276 madrpc_show_errors(1);
279 dest_type = IB_DEST_DRPATH;
282 dest_type = IB_DEST_GUID;
288 ca_port = strtoul(optarg, 0, 0);
291 if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
292 IBERROR("can't resolve SM destination port %s", optarg);
296 timeout = strtoul(optarg, 0, 0);
297 madrpc_set_timeout(timeout);
303 fprintf(stderr, "%s %s\n", argv0, get_build_version() );
316 madrpc_init(ca, ca_port, mgmt_classes, 3);
318 if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
319 IBERROR("can't resolve destination port %s", argv[0]);
321 /* First, make sure it is a switch port if it is a "set" */
323 if (!strcmp(argv[2], "enable"))
325 else if (!strcmp(argv[2], "disable"))
327 else if (!strcmp(argv[2], "reset"))
329 else if (!strcmp(argv[2], "speed")) {
331 IBERROR("speed requires an additional parameter");
333 /* Parse speed value */
334 speed = strtoul(argv[3], 0, 0);
336 IBERROR("invalid speed value %d", speed);
340 err = get_node_info(&portid, data);
342 IBERROR("smp query nodeinfo failed");
343 if (err) { /* not switch */
344 if (port_op == 0) /* query op */
346 else if (port_op != 4) /* other than speed op */
347 IBERROR("smp query nodeinfo: Node type not switch");
351 portnum = strtol(argv[1], 0, 0);
354 printf("Initial PortInfo:\n");
356 printf("PortInfo:\n");
357 err = get_port_info(&portid, data, portnum, port_op);
359 IBERROR("smp query portinfo failed");
361 /* Only if one of the "set" options is chosen */
363 if (port_op == 1) /* Enable port */
364 mad_set_field(data, 0, IB_PORT_PHYS_STATE_F, 2); /* Polling */
365 else if ((port_op == 2) || (port_op == 3)) { /* Disable port */
366 mad_set_field(data, 0, IB_PORT_STATE_F, 1); /* Down */
367 mad_set_field(data, 0, IB_PORT_PHYS_STATE_F, 3); /* Disabled */
368 } else if (port_op == 4) { /* Set speed */
369 mad_set_field(data, 0, IB_PORT_LINK_SPEED_ENABLED_F, speed);
370 mad_set_field(data, 0, IB_PORT_STATE_F, 0);
371 mad_set_field(data, 0, IB_PORT_PHYS_STATE_F, 0);
374 err = set_port_info(&portid, data, portnum, port_op);
376 IBERROR("smp set portinfo failed");
378 if (port_op == 3) { /* Reset port - so also enable */
379 mad_set_field(data, 0, IB_PORT_PHYS_STATE_F, 2); /* Polling */
380 err = set_port_info(&portid, data, portnum, port_op);
382 IBERROR("smp set portinfo failed");
384 } else { /* query op */
385 /* only compare peer port if switch port */
387 /* First, exclude SP0 */
389 /* Now, make sure PortState is Active */
390 /* Or is PortPhysicalState LinkUp sufficient ? */
391 mad_decode_field(data, IB_PORT_STATE_F, &state);
392 mad_decode_field(data, IB_PORT_PHYS_STATE_F, &physstate);
393 if (state == 4) { /* Active */
394 mad_decode_field(data, IB_PORT_LINK_WIDTH_ENABLED_F, &lwe );
395 mad_decode_field(data, IB_PORT_LINK_WIDTH_SUPPORTED_F, &lws);
396 mad_decode_field(data, IB_PORT_LINK_WIDTH_ACTIVE_F, &lwa);
397 mad_decode_field(data, IB_PORT_LINK_SPEED_SUPPORTED_F, &lss);
398 mad_decode_field(data, IB_PORT_LINK_SPEED_ACTIVE_F, &lsa);
399 mad_decode_field(data, IB_PORT_LINK_SPEED_ENABLED_F, &lse);
401 /* Setup portid for peer port */
402 memcpy(&peerportid, &portid, sizeof(peerportid));
403 peerportid.drpath.cnt = 1;
404 peerportid.drpath.p[1] = portnum;
406 /* Set DrSLID to local lid */
407 if (ib_resolve_self(&selfportid, &selfport, 0) < 0)
408 IBERROR("could not resolve self");
409 peerportid.drpath.drslid = selfportid.lid;
410 peerportid.drpath.drdlid = 0xffff;
412 /* Get peer port NodeInfo to obtain peer port number */
413 err = get_node_info(&peerportid, data);
415 IBERROR("smp query nodeinfo failed");
417 mad_decode_field(data, IB_NODE_LOCAL_PORT_F, &peerlocalportnum);
419 printf("Peer PortInfo:\n");
420 /* Get peer port characteristics */
421 err = get_port_info(&peerportid, data, peerlocalportnum, port_op);
423 IBERROR("smp query peer portinfofailed");
425 mad_decode_field(data, IB_PORT_LINK_WIDTH_ENABLED_F, &peerlwe );
426 mad_decode_field(data, IB_PORT_LINK_WIDTH_SUPPORTED_F, &peerlws);
427 mad_decode_field(data, IB_PORT_LINK_WIDTH_ACTIVE_F, &peerlwa);
428 mad_decode_field(data, IB_PORT_LINK_SPEED_SUPPORTED_F, &peerlss);
429 mad_decode_field(data, IB_PORT_LINK_SPEED_ACTIVE_F, &peerlsa);
430 mad_decode_field(data, IB_PORT_LINK_SPEED_ENABLED_F, &peerlse);
432 /* Now validate peer port characteristics */
433 /* Examine Link Width */
434 width = get_link_width(lwe, lws);
435 peerwidth = get_link_width(peerlwe, peerlws);
436 validate_width(width, peerwidth, lwa);
438 /* Examine Link Speed */
439 speed = get_link_speed(lse, lss);
440 peerspeed = get_link_speed(peerlse, peerlss);
441 validate_speed(speed, peerspeed, lsa);