1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff
34219820Sjeff#if HAVE_CONFIG_H
35219820Sjeff#  include <config.h>
36219820Sjeff#endif /* HAVE_CONFIG_H */
37219820Sjeff
38219820Sjeff#include <stdio.h>
39219820Sjeff#include <stdlib.h>
40219820Sjeff#include <unistd.h>
41219820Sjeff#include <stdarg.h>
42219820Sjeff#include <inttypes.h>
43219820Sjeff#include <getopt.h>
44219820Sjeff
45219820Sjeff#include <infiniband/common.h>
46219820Sjeff#include <infiniband/umad.h>
47219820Sjeff#include <infiniband/mad.h>
48219820Sjeff
49219820Sjeff#include "ibdiag_common.h"
50219820Sjeff
51219820Sjeffstatic uint8_t sminfo[1024];
52219820Sjeff
53219820Sjeffchar *argv0 = "sminfo";
54219820Sjeff
55219820Sjeffstatic void
56219820Sjeffusage(void)
57219820Sjeff{
58219820Sjeff	fprintf(stderr, "Usage: %s [-d(ebug) -e(rr_show) -s state -p prio -a activity -D(irect) -G(uid) -V(ersion) -C ca_name -P ca_port "
59219820Sjeff			"-t(imeout) timeout_ms] <sm_lid|sm_dr_path> [modifier]\n",
60219820Sjeff			argv0);
61219820Sjeff	exit(-1);
62219820Sjeff}
63219820Sjeff
64219820Sjeffint strdata, xdata=1, bindata;
65219820Sjeffenum {
66219820Sjeff	SMINFO_NOTACT,
67219820Sjeff	SMINFO_DISCOVER,
68219820Sjeff	SMINFO_STANDBY,
69219820Sjeff	SMINFO_MASTER,
70219820Sjeff
71219820Sjeff	SMINFO_STATE_LAST,
72219820Sjeff};
73219820Sjeff
74219820Sjeffchar *statestr[] = {
75219820Sjeff	[SMINFO_NOTACT] "SMINFO_NOTACT",
76219820Sjeff	[SMINFO_DISCOVER] "SMINFO_DISCOVER",
77219820Sjeff	[SMINFO_STANDBY] "SMINFO_STANDBY",
78219820Sjeff	[SMINFO_MASTER] "SMINFO_MASTER",
79219820Sjeff};
80219820Sjeff
81219820Sjeff#define STATESTR(s)	(((unsigned)(s)) < SMINFO_STATE_LAST ? statestr[s] : "???")
82219820Sjeff
83219820Sjeffint
84219820Sjeffmain(int argc, char **argv)
85219820Sjeff{
86219820Sjeff	int mgmt_classes[3] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS};
87219820Sjeff	int mod = 0;
88219820Sjeff	ib_portid_t portid = {0};
89219820Sjeff	int timeout = 0;	/* use default */
90219820Sjeff	uint8_t *p;
91219820Sjeff	unsigned act = 0;
92219820Sjeff	int prio = 0, state = SMINFO_STANDBY;
93219820Sjeff	uint64_t guid = 0, key = 0;
94219820Sjeff	extern int ibdebug;
95219820Sjeff	int dest_type = IB_DEST_LID;
96219820Sjeff	int udebug = 0;
97219820Sjeff	char *ca = 0;
98219820Sjeff	int ca_port = 0;
99219820Sjeff
100219820Sjeff	static char const str_opts[] = "C:P:t:s:p:a:deDGVhu";
101219820Sjeff	static const struct option long_opts[] = {
102219820Sjeff		{ "C", 1, 0, 'C'},
103219820Sjeff		{ "P", 1, 0, 'P'},
104219820Sjeff		{ "debug", 0, 0, 'd'},
105219820Sjeff		{ "err_show", 0, 0, 'e'},
106219820Sjeff		{ "s", 1, 0, 's'},
107219820Sjeff		{ "p", 1, 0, 'p'},
108219820Sjeff		{ "a", 1, 0, 'a'},
109219820Sjeff		{ "Direct", 0, 0, 'D'},
110219820Sjeff		{ "Guid", 0, 0, 'G'},
111219820Sjeff		{ "Version", 0, 0, 'V'},
112219820Sjeff		{ "timeout", 1, 0, 't'},
113219820Sjeff		{ "help", 0, 0, 'h'},
114219820Sjeff		{ "usage", 0, 0, 'u'},
115219820Sjeff		{ }
116219820Sjeff	};
117219820Sjeff
118219820Sjeff	argv0 = argv[0];
119219820Sjeff
120219820Sjeff	while (1) {
121219820Sjeff		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
122219820Sjeff		if ( ch == -1 )
123219820Sjeff			break;
124219820Sjeff		switch(ch) {
125219820Sjeff		case 'C':
126219820Sjeff			ca = optarg;
127219820Sjeff			break;
128219820Sjeff		case 'P':
129219820Sjeff			ca_port = strtoul(optarg, 0, 0);
130219820Sjeff			break;
131219820Sjeff		case 'd':
132219820Sjeff			ibdebug++;
133219820Sjeff			madrpc_show_errors(1);
134219820Sjeff			umad_debug(udebug);
135219820Sjeff			udebug++;
136219820Sjeff			break;
137219820Sjeff		case 'e':
138219820Sjeff			madrpc_show_errors(1);
139219820Sjeff			break;
140219820Sjeff		case 'D':
141219820Sjeff			dest_type = IB_DEST_DRPATH;
142219820Sjeff			break;
143219820Sjeff		case 'G':
144219820Sjeff			dest_type = IB_DEST_GUID;
145219820Sjeff			break;
146219820Sjeff		case 't':
147219820Sjeff			timeout = strtoul(optarg, 0, 0);
148219820Sjeff			madrpc_set_timeout(timeout);
149219820Sjeff			break;
150219820Sjeff		case 'a':
151219820Sjeff			act = strtoul(optarg, 0, 0);
152219820Sjeff			break;
153219820Sjeff		case 's':
154219820Sjeff			state = strtoul(optarg, 0, 0);
155219820Sjeff			break;
156219820Sjeff		case 'p':
157219820Sjeff			prio = strtoul(optarg, 0, 0);
158219820Sjeff			break;
159219820Sjeff		case 'V':
160219820Sjeff			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
161219820Sjeff			exit(-1);
162219820Sjeff		default:
163219820Sjeff			usage();
164219820Sjeff			break;
165219820Sjeff		}
166219820Sjeff	}
167219820Sjeff	argc -= optind;
168219820Sjeff	argv += optind;
169219820Sjeff
170219820Sjeff	if (argc > 1)
171219820Sjeff		mod = atoi(argv[1]);
172219820Sjeff
173219820Sjeff	madrpc_init(ca, ca_port, mgmt_classes, 3);
174219820Sjeff
175219820Sjeff	if (argc) {
176219820Sjeff		if (ib_resolve_portid_str(&portid, argv[0], dest_type, 0) < 0)
177219820Sjeff			IBERROR("can't resolve destination port %s", argv[0]);
178219820Sjeff	} else {
179219820Sjeff		if (ib_resolve_smlid(&portid, timeout) < 0)
180219820Sjeff			IBERROR("can't resolve sm port %s", argv[0]);
181219820Sjeff	}
182219820Sjeff
183219820Sjeff	mad_encode_field(sminfo, IB_SMINFO_GUID_F, &guid);
184219820Sjeff	mad_encode_field(sminfo, IB_SMINFO_ACT_F, &act);
185219820Sjeff	mad_encode_field(sminfo, IB_SMINFO_KEY_F, &key);
186219820Sjeff	mad_encode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
187219820Sjeff	mad_encode_field(sminfo, IB_SMINFO_STATE_F, &state);
188219820Sjeff
189219820Sjeff	if (mod) {
190219820Sjeff		if (!(p = smp_set(sminfo, &portid, IB_ATTR_SMINFO, mod, timeout)))
191219820Sjeff			IBERROR("query");
192219820Sjeff	} else
193219820Sjeff		if (!(p = smp_query(sminfo, &portid, IB_ATTR_SMINFO, 0, timeout)))
194219820Sjeff			IBERROR("query");
195219820Sjeff
196219820Sjeff	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
197219820Sjeff	mad_decode_field(sminfo, IB_SMINFO_ACT_F, &act);
198219820Sjeff	mad_decode_field(sminfo, IB_SMINFO_KEY_F, &key);
199219820Sjeff	mad_decode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
200219820Sjeff	mad_decode_field(sminfo, IB_SMINFO_STATE_F, &state);
201219820Sjeff
202219820Sjeff	printf("sminfo: sm lid %d sm guid 0x%" PRIx64 ", activity count %u priority %d state %d %s\n",
203219820Sjeff		portid.lid, guid, act, prio, state, STATESTR(state));
204219820Sjeff
205219820Sjeff	exit(0);
206219820Sjeff}
207