1 /*
2  * Copyright (c) 2004-2009 Voltaire Inc.  All rights reserved.
3  * Copyright (c) 2011 Mellanox Technologies LTD.  All rights reserved.
4  *
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:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  *
33  */
34 
35 #if HAVE_CONFIG_H
36 #  include <config.h>
37 #endif				/* HAVE_CONFIG_H */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <inttypes.h>
43 #include <getopt.h>
44 
45 #include <infiniband/umad.h>
46 #include <infiniband/mad.h>
47 
48 #include "ibdiag_common.h"
49 
50 static uint8_t sminfo[1024] = { 0 };
51 
52 struct ibmad_port *srcport;
53 
54 int strdata, xdata = 1, bindata;
55 
56 enum {
57 	SMINFO_NOTACT,
58 	SMINFO_DISCOVER,
59 	SMINFO_STANDBY,
60 	SMINFO_MASTER,
61 
62 	SMINFO_STATE_LAST,
63 };
64 
65 char *statestr[] = {
66 	"SMINFO_NOTACT",
67 	"SMINFO_DISCOVER",
68 	"SMINFO_STANDBY",
69 	"SMINFO_MASTER",
70 };
71 
72 #define STATESTR(s)	(((unsigned)(s)) < SMINFO_STATE_LAST ? statestr[s] : "???")
73 
74 static unsigned act;
75 static int prio, state = SMINFO_STANDBY;
76 
77 static int process_opt(void *context, int ch, char *optarg)
78 {
79 	switch (ch) {
80 	case 'a':
81 		act = strtoul(optarg, 0, 0);
82 		break;
83 	case 's':
84 		state = strtoul(optarg, 0, 0);
85 		break;
86 	case 'p':
87 		prio = strtoul(optarg, 0, 0);
88 		break;
89 	default:
90 		return -1;
91 	}
92 	return 0;
93 }
94 
95 int main(int argc, char **argv)
96 {
97 	int mgmt_classes[3] =
98 	    { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS };
99 	int mod = 0;
100 	ib_portid_t portid = { 0 };
101 	uint8_t *p;
102 	uint64_t guid = 0, key = 0;
103 
104 	const struct ibdiag_opt opts[] = {
105 		{"state", 's', 1, "<0-3>", "set SM state"},
106 		{"priority", 'p', 1, "<0-15>", "set SM priority"},
107 		{"activity", 'a', 1, NULL, "set activity count"},
108 		{0}
109 	};
110 	char usage_args[] = "<sm_lid|sm_dr_path> [modifier]";
111 
112 	ibdiag_process_opts(argc, argv, NULL, "sK", opts, process_opt,
113 			    usage_args, NULL);
114 
115 	argc -= optind;
116 	argv += optind;
117 
118 	if (argc > 1)
119 		mod = atoi(argv[1]);
120 
121 	srcport = mad_rpc_open_port(ibd_ca, ibd_ca_port, mgmt_classes, 3);
122 	if (!srcport)
123 		IBEXIT("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
124 
125 	smp_mkey_set(srcport, ibd_mkey);
126 
127 	if (argc) {
128 		if (resolve_portid_str(ibd_ca, ibd_ca_port, &portid, argv[0],
129 				       ibd_dest_type, 0, srcport) < 0)
130 			IBEXIT("can't resolve destination port %s", argv[0]);
131 	} else {
132 		if (resolve_sm_portid(ibd_ca, ibd_ca_port, &portid) < 0)
133 			IBEXIT("can't resolve sm port %s", argv[0]);
134 	}
135 
136 	mad_encode_field(sminfo, IB_SMINFO_GUID_F, &guid);
137 	mad_encode_field(sminfo, IB_SMINFO_ACT_F, &act);
138 	mad_encode_field(sminfo, IB_SMINFO_KEY_F, &key);
139 	mad_encode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
140 	mad_encode_field(sminfo, IB_SMINFO_STATE_F, &state);
141 
142 	if (mod) {
143 		if (!(p = smp_set_via(sminfo, &portid, IB_ATTR_SMINFO, mod,
144 				      ibd_timeout, srcport)))
145 			IBEXIT("query");
146 	} else if (!(p = smp_query_via(sminfo, &portid, IB_ATTR_SMINFO, 0,
147 				       ibd_timeout, srcport)))
148 		IBEXIT("query");
149 
150 	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
151 	mad_decode_field(sminfo, IB_SMINFO_ACT_F, &act);
152 	mad_decode_field(sminfo, IB_SMINFO_KEY_F, &key);
153 	mad_decode_field(sminfo, IB_SMINFO_PRIO_F, &prio);
154 	mad_decode_field(sminfo, IB_SMINFO_STATE_F, &state);
155 
156 	printf("sminfo: sm lid %d sm guid 0x%" PRIx64
157 	       ", activity count %u priority %d state %d %s\n", portid.lid,
158 	       guid, act, prio, state, STATESTR(state));
159 
160 	mad_rpc_close_port(srcport);
161 	exit(0);
162 }
163