1/*
2 * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3 *
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:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 *
32 */
33
34#if HAVE_CONFIG_H
35#  include <config.h>
36#endif /* HAVE_CONFIG_H */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include <stdarg.h>
42#include <getopt.h>
43#include <netinet/in.h>
44
45#include <infiniband/common.h>
46#include <infiniband/umad.h>
47#include <infiniband/mad.h>
48
49#include "ibdiag_common.h"
50
51#define IS3_DEVICE_ID			47396
52
53#define IB_MLX_VENDOR_CLASS		10
54/* Vendor specific Attribute IDs */
55#define IB_MLX_IS3_GENERAL_INFO		0x17
56#define IB_MLX_IS3_CONFIG_SPACE_ACCESS	0x50
57/* Config space addresses */
58#define IB_MLX_IS3_PORT_XMIT_WAIT	0x10013C
59
60char *argv0 = "vendstat";
61
62typedef struct {
63	uint16_t hw_revision;
64	uint16_t device_id;
65	uint8_t  reserved[24];
66	uint32_t uptime;
67} is3_hw_info_t;
68
69typedef struct {
70	uint8_t resv1;
71	uint8_t major;
72	uint8_t minor;
73	uint8_t sub_minor;
74	uint32_t build_id;
75	uint8_t month;
76	uint8_t day;
77	uint16_t year;
78	uint16_t resv2;
79	uint16_t hour;
80	uint8_t psid[16];
81	uint32_t ini_file_version;
82} is3_fw_info_t;
83
84typedef struct {
85	uint8_t resv1;
86	uint8_t major;
87	uint8_t minor;
88	uint8_t sub_minor;
89	uint8_t resv2[28];
90} is3_sw_info_t;
91
92typedef struct {
93	uint8_t       reserved[8];
94	is3_hw_info_t hw_info;
95	is3_fw_info_t fw_info;
96	is3_sw_info_t sw_info;
97} is3_general_info_t;
98
99typedef struct {
100	uint32_t address;
101	uint32_t data;
102	uint32_t mask;
103} is3_record_t;
104
105typedef struct {
106	uint8_t      reserved[8];
107	is3_record_t record[18];
108} is3_config_space_t;
109
110static void
111usage(void)
112{
113	char *basename;
114
115	if (!(basename = strrchr(argv0, '/')))
116		basename = argv0;
117	else
118		basename++;
119
120	fprintf(stderr, "Usage: %s [-d(ebug) -N -w -G(uid) -C ca_name -P ca_port "
121			"-t(imeout) timeout_ms -V(ersion) -h(elp)] <lid|guid>\n",
122			basename);
123	fprintf(stderr, "\tExamples:\n");
124	fprintf(stderr, "\t\t%s -N 6\t\t# read IS3 general information\n", basename);
125	fprintf(stderr, "\t\t%s -w 6\t\t# read IS3 port xmit wait counters\n", basename);
126	exit(-1);
127}
128
129int
130main(int argc, char **argv)
131{
132	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_MLX_VENDOR_CLASS};
133	ib_portid_t *sm_id = 0, sm_portid = {0};
134	ib_portid_t portid = {0};
135	extern int ibdebug;
136	int dest_type = IB_DEST_LID;
137	int timeout = 0;	/* use default */
138	int port = 0;
139	char buf[1024];
140	int udebug = 0;
141	char *ca = 0;
142	int ca_port = 0;
143	ib_vendor_call_t call;
144	is3_general_info_t *gi;
145	is3_config_space_t *cs;
146	int general_info = 0;
147	int xmit_wait = 0;
148	int i;
149
150	static char const str_opts[] = "C:P:s:t:dNwGVhu";
151	static const struct option long_opts[] = {
152		{ "C", 1, 0, 'C'},
153		{ "P", 1, 0, 'P'},
154		{ "N", 1, 0, 'N'},
155		{ "w", 1, 0, 'w'},
156		{ "debug", 0, 0, 'd'},
157		{ "Guid", 0, 0, 'G'},
158		{ "sm_portid", 1, 0, 's'},
159		{ "timeout", 1, 0, 't'},
160		{ "Version", 0, 0, 'V'},
161		{ "help", 0, 0, 'h'},
162		{ "usage", 0, 0, 'u'},
163		{ }
164	};
165
166	argv0 = argv[0];
167
168	while (1) {
169		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
170		if ( ch == -1 )
171			break;
172		switch(ch) {
173		case 'C':
174			ca = optarg;
175			break;
176		case 'P':
177			ca_port = strtoul(optarg, 0, 0);
178			break;
179		case 'N':
180			general_info = 1;
181			break;
182		case 'w':
183			xmit_wait = 1;
184			break;
185		case 'd':
186			ibdebug++;
187			madrpc_show_errors(1);
188			umad_debug(udebug);
189			udebug++;
190			break;
191		case 'G':
192			dest_type = IB_DEST_GUID;
193			break;
194		case 's':
195			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
196				IBERROR("can't resolve SM destination port %s", optarg);
197			sm_id = &sm_portid;
198			break;
199		case 't':
200			timeout = strtoul(optarg, 0, 0);
201			madrpc_set_timeout(timeout);
202			break;
203		case 'V':
204			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
205			exit(-1);
206		default:
207			usage();
208			break;
209		}
210	}
211	argc -= optind;
212	argv += optind;
213
214	if (argc > 1)
215		port = strtoul(argv[1], 0, 0);
216
217	madrpc_init(ca, ca_port, mgmt_classes, 4);
218
219	if (argc) {
220		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
221			IBERROR("can't resolve destination port %s", argv[0]);
222	} else {
223		if (ib_resolve_self(&portid, &port, 0) < 0)
224			IBERROR("can't resolve self port %s", argv[0]);
225	}
226
227	/* Only General Info and Port Xmit Wait Counters */
228	/* queries are currently supported */
229	if (!general_info && !xmit_wait)
230		IBERROR("at least one of -N and -w must be specified");
231
232	/* These are Mellanox specific vendor MADs */
233	/* but vendors change the VendorId so how know for sure ? */
234	/* Would need a list of these and it might not be complete */
235	/* so for right now, punt on this */
236
237	memset(&call, 0, sizeof(call));
238	call.mgmt_class = IB_MLX_VENDOR_CLASS;
239	call.method = IB_MAD_METHOD_GET;
240	call.timeout = timeout;
241
242	memset(&buf, 0, sizeof(buf));
243	/* vendor ClassPortInfo is required attribute if class supported */
244	call.attrid = CLASS_PORT_INFO;
245	if (!ib_vendor_call(&buf, &portid, &call))
246		IBERROR("classportinfo query");
247
248	memset(&buf, 0, sizeof(buf));
249	call.attrid = IB_MLX_IS3_GENERAL_INFO;
250	if (!ib_vendor_call(&buf, &portid, &call))
251		IBERROR("vendstat");
252	gi = (is3_general_info_t *)&buf;
253
254	if (general_info) {
255		/* dump IS3 general info here */
256		printf("hw_dev_rev:  0x%04x\n", ntohs(gi->hw_info.hw_revision));
257		printf("hw_dev_id:   0x%04x\n", ntohs(gi->hw_info.device_id));
258		printf("hw_uptime:   0x%08x\n", ntohl(gi->hw_info.uptime));
259		printf("fw_version:  %02d.%02d.%02d\n",
260		       gi->fw_info.major, gi->fw_info.minor, gi->fw_info.sub_minor);
261		printf("fw_build_id: 0x%04x\n", ntohl(gi->fw_info.build_id));
262		printf("fw_date:     %02d/%02d/%04x\n",
263		       gi->fw_info.month, gi->fw_info.day, ntohs(gi->fw_info.year));
264		printf("fw_psid:     '%s'\n", gi->fw_info.psid);
265		printf("fw_ini_ver:  %d\n", ntohl(gi->fw_info.ini_file_version));
266		printf("sw_version:  %02d.%02d.%02d\n",
267		       gi->sw_info.major, gi->sw_info.minor, gi->sw_info.sub_minor);
268	}
269
270	if (xmit_wait) {
271		if (ntohs(gi->hw_info.device_id) != IS3_DEVICE_ID)
272			IBERROR("Unsupported device ID 0x%x", ntohs(gi->hw_info.device_id));
273
274		memset(&buf, 0, sizeof(buf));
275		call.attrid = IB_MLX_IS3_CONFIG_SPACE_ACCESS;
276		/* Limit of 18 accesses per MAD ? */
277		call.mod = 2 << 22 | 16 << 16; /* 16 records */
278		/* Set record addresses for each port */
279		cs = (is3_config_space_t *)&buf;
280		for (i = 0; i < 16; i++)
281			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 1) << 12));
282		if (!ib_vendor_call(&buf, &portid, &call))
283			IBERROR("vendstat");
284
285		for (i = 0; i < 16; i++)
286			if (cs->record[i].data)	/* PortXmitWait is 32 bit counter */
287				printf("Port %d: PortXmitWait 0x%x\n", i + 4, ntohl(cs->record[i].data)); /* port 4 is first port */
288
289		/* Last 8 ports is another query */
290		memset(&buf, 0, sizeof(buf));
291		call.attrid = IB_MLX_IS3_CONFIG_SPACE_ACCESS;
292                call.mod = 2 << 22 | 8 << 16; /* 8 records */
293		/* Set record addresses for each port */
294		cs = (is3_config_space_t *)&buf;
295		for (i = 0; i < 8; i++)
296			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 17) << 12));
297		if (!ib_vendor_call(&buf, &portid, &call))
298			IBERROR("vendstat");
299
300		for (i = 0; i < 8; i++)
301			if (cs->record[i].data) /* PortXmitWait is 32 bit counter */
302				printf("Port %d: PortXmitWait 0x%x\n",
303				       i < 4 ? i + 21 : i - 3,
304				       ntohl(cs->record[i].data));
305	}
306
307	exit(0);
308}
309