1321936Shselasky/*
2321936Shselasky * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3321936Shselasky * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4321936Shselasky *
5321936Shselasky * This software is available to you under a choice of one of two
6321936Shselasky * licenses.  You may choose to be licensed under the terms of the GNU
7321936Shselasky * General Public License (GPL) Version 2, available from the file
8321936Shselasky * COPYING in the main directory of this source tree, or the
9321936Shselasky * OpenIB.org BSD license below:
10321936Shselasky *
11321936Shselasky *     Redistribution and use in source and binary forms, with or
12321936Shselasky *     without modification, are permitted provided that the following
13321936Shselasky *     conditions are met:
14321936Shselasky *
15321936Shselasky *      - Redistributions of source code must retain the above
16321936Shselasky *        copyright notice, this list of conditions and the following
17321936Shselasky *        disclaimer.
18321936Shselasky *
19321936Shselasky *      - Redistributions in binary form must reproduce the above
20321936Shselasky *        copyright notice, this list of conditions and the following
21321936Shselasky *        disclaimer in the documentation and/or other materials
22321936Shselasky *        provided with the distribution.
23321936Shselasky *
24321936Shselasky * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25321936Shselasky * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26321936Shselasky * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27321936Shselasky * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28321936Shselasky * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29321936Shselasky * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30321936Shselasky * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31321936Shselasky * SOFTWARE.
32321936Shselasky *
33321936Shselasky */
34321936Shselasky
35321936Shselasky#if HAVE_CONFIG_H
36321936Shselasky#  include <config.h>
37321936Shselasky#endif				/* HAVE_CONFIG_H */
38321936Shselasky
39321936Shselasky#include <stdio.h>
40321936Shselasky#include <stdlib.h>
41321936Shselasky#include <unistd.h>
42321936Shselasky#include <inttypes.h>
43321936Shselasky#include <getopt.h>
44321936Shselasky
45321936Shselasky#include <infiniband/umad.h>
46321936Shselasky#include <infiniband/mad.h>
47321936Shselasky
48321936Shselasky#include "ibdiag_common.h"
49321936Shselasky
50321936Shselaskystatic uint8_t sminfo[1024] = { 0 };
51321936Shselasky
52321936Shselaskystruct ibmad_port *srcport;
53321936Shselasky
54321936Shselaskyint strdata, xdata = 1, bindata;
55321936Shselasky
56321936Shselaskyenum {
57321936Shselasky	SMINFO_NOTACT,
58321936Shselasky	SMINFO_DISCOVER,
59321936Shselasky	SMINFO_STANDBY,
60321936Shselasky	SMINFO_MASTER,
61321936Shselasky
62321936Shselasky	SMINFO_STATE_LAST,
63321936Shselasky};
64321936Shselasky
65321936Shselaskychar *statestr[] = {
66321936Shselasky	"SMINFO_NOTACT",
67321936Shselasky	"SMINFO_DISCOVER",
68321936Shselasky	"SMINFO_STANDBY",
69321936Shselasky	"SMINFO_MASTER",
70321936Shselasky};
71321936Shselasky
72321936Shselasky#define STATESTR(s)	(((unsigned)(s)) < SMINFO_STATE_LAST ? statestr[s] : "???")
73321936Shselasky
74321936Shselaskystatic unsigned act;
75321936Shselaskystatic int prio, state = SMINFO_STANDBY;
76321936Shselasky
77321936Shselaskystatic int process_opt(void *context, int ch, char *optarg)
78321936Shselasky{
79321936Shselasky	switch (ch) {
80321936Shselasky	case 'a':
81321936Shselasky		act = strtoul(optarg, 0, 0);
82321936Shselasky		break;
83321936Shselasky	case 's':
84321936Shselasky		state = strtoul(optarg, 0, 0);
85321936Shselasky		break;
86321936Shselasky	case 'p':
87321936Shselasky		prio = strtoul(optarg, 0, 0);
88321936Shselasky		break;
89321936Shselasky	default:
90321936Shselasky		return -1;
91321936Shselasky	}
92321936Shselasky	return 0;
93321936Shselasky}
94321936Shselasky
95321936Shselaskyint main(int argc, char **argv)
96321936Shselasky{
97321936Shselasky	int mgmt_classes[3] =
98321936Shselasky	    { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS };
99321936Shselasky	int mod = 0;
100321936Shselasky	ib_portid_t portid = { 0 };
101321936Shselasky	uint8_t *p;
102321936Shselasky	uint64_t guid = 0, key = 0;
103321936Shselasky
104321936Shselasky	const struct ibdiag_opt opts[] = {
105321936Shselasky		{"state", 's', 1, "<0-3>", "set SM state"},
106321936Shselasky		{"priority", 'p', 1, "<0-15>", "set SM priority"},
107321936Shselasky		{"activity", 'a', 1, NULL, "set activity count"},
108321936Shselasky		{0}
109321936Shselasky	};
110321936Shselasky	char usage_args[] = "<sm_lid|sm_dr_path> [modifier]";
111321936Shselasky
112321936Shselasky	ibdiag_process_opts(argc, argv, NULL, "sK", opts, process_opt,
113321936Shselasky			    usage_args, NULL);
114321936Shselasky
115321936Shselasky	argc -= optind;
116321936Shselasky	argv += optind;
117321936Shselasky
118321936Shselasky	if (argc > 1)
119321936Shselasky		mod = atoi(argv[1]);
120321936Shselasky
121321936Shselasky	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
122321936Shselasky	if (!srcport)
123321936Shselasky		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
124321936Shselasky
125321936Shselasky	smp_mkey_set(srcport, ibd_mkey);
126321936Shselasky
127321936Shselasky	if (argc) {
128321936Shselasky		if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[0],
129321936Shselasky				       ibd_dest_type, 0, srcport) < 0)
130321936Shselasky			IBEXIT("can't resolve destination port %s", argv[0]);
131321936Shselasky	} else {
132321936Shselasky		if (resolve_sm_portid(ibd_ca, ibd_ca_port, &portid) < 0)
133321936Shselasky			IBEXIT("can't resolve sm port %s", argv[0]);
134321936Shselasky	}
135321936Shselasky
136321936Shselasky	mad_encode_field(sminfo, IB_SMINFO_GUID_F, &guid);
137321936Shselasky	mad_encode_field(sminfo, IB_SMINFO_ACT_F, &act);
138321936Shselasky	mad_encode_field(sminfo, IB_SMINFO_KEY_F, &key);
139321936Shselasky	mad_encode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
140321936Shselasky	mad_encode_field(sminfo, IB_SMINFO_STATE_F, &state);
141321936Shselasky
142321936Shselasky	if (mod) {
143321936Shselasky		if (!(p = smp_set_via(sminfo, &portid, IB_ATTR_SMINFO, mod,
144321936Shselasky				      ibd_timeout, srcport)))
145321936Shselasky			IBEXIT("query");
146321936Shselasky	} else if (!(p = smp_query_via(sminfo, &portid, IB_ATTR_SMINFO, 0,
147321936Shselasky				       ibd_timeout, srcport)))
148321936Shselasky		IBEXIT("query");
149321936Shselasky
150321936Shselasky	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
151321936Shselasky	mad_decode_field(sminfo, IB_SMINFO_ACT_F, &act);
152321936Shselasky	mad_decode_field(sminfo, IB_SMINFO_KEY_F, &key);
153321936Shselasky	mad_decode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
154321936Shselasky	mad_decode_field(sminfo, IB_SMINFO_STATE_F, &state);
155321936Shselasky
156321936Shselasky	printf("sminfo: sm lid %d sm guid 0x%" PRIx64
157321936Shselasky	       ", activity count %u priority %d state %d %s\n", portid.lid,
158321936Shselasky	       guid, act, prio, state, STATESTR(state));
159321936Shselasky
160321936Shselasky	mad_rpc_close_port(srcport);
161321936Shselasky	exit(0);
162321936Shselasky}
163