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 <getopt.h>
43219820Sjeff#include <netinet/in.h>
44219820Sjeff
45219820Sjeff#include <infiniband/common.h>
46219820Sjeff#include <infiniband/umad.h>
47219820Sjeff#include <infiniband/mad.h>
48219820Sjeff
49219820Sjeff#include "ibdiag_common.h"
50219820Sjeff
51219820Sjeff#define IS3_DEVICE_ID			47396
52219820Sjeff
53219820Sjeff#define IB_MLX_VENDOR_CLASS		10
54219820Sjeff/* Vendor specific Attribute IDs */
55219820Sjeff#define IB_MLX_IS3_GENERAL_INFO		0x17
56219820Sjeff#define IB_MLX_IS3_CONFIG_SPACE_ACCESS	0x50
57219820Sjeff/* Config space addresses */
58219820Sjeff#define IB_MLX_IS3_PORT_XMIT_WAIT	0x10013C
59219820Sjeff
60219820Sjeffchar *argv0 = "vendstat";
61219820Sjeff
62219820Sjefftypedef struct {
63219820Sjeff	uint16_t hw_revision;
64219820Sjeff	uint16_t device_id;
65219820Sjeff	uint8_t  reserved[24];
66219820Sjeff	uint32_t uptime;
67219820Sjeff} is3_hw_info_t;
68219820Sjeff
69219820Sjefftypedef struct {
70219820Sjeff	uint8_t resv1;
71219820Sjeff	uint8_t major;
72219820Sjeff	uint8_t minor;
73219820Sjeff	uint8_t sub_minor;
74219820Sjeff	uint32_t build_id;
75219820Sjeff	uint8_t month;
76219820Sjeff	uint8_t day;
77219820Sjeff	uint16_t year;
78219820Sjeff	uint16_t resv2;
79219820Sjeff	uint16_t hour;
80219820Sjeff	uint8_t psid[16];
81219820Sjeff	uint32_t ini_file_version;
82219820Sjeff} is3_fw_info_t;
83219820Sjeff
84219820Sjefftypedef struct {
85219820Sjeff	uint8_t resv1;
86219820Sjeff	uint8_t major;
87219820Sjeff	uint8_t minor;
88219820Sjeff	uint8_t sub_minor;
89219820Sjeff	uint8_t resv2[28];
90219820Sjeff} is3_sw_info_t;
91219820Sjeff
92219820Sjefftypedef struct {
93219820Sjeff	uint8_t       reserved[8];
94219820Sjeff	is3_hw_info_t hw_info;
95219820Sjeff	is3_fw_info_t fw_info;
96219820Sjeff	is3_sw_info_t sw_info;
97219820Sjeff} is3_general_info_t;
98219820Sjeff
99219820Sjefftypedef struct {
100219820Sjeff	uint32_t address;
101219820Sjeff	uint32_t data;
102219820Sjeff	uint32_t mask;
103219820Sjeff} is3_record_t;
104219820Sjeff
105219820Sjefftypedef struct {
106219820Sjeff	uint8_t      reserved[8];
107219820Sjeff	is3_record_t record[18];
108219820Sjeff} is3_config_space_t;
109219820Sjeff
110219820Sjeffstatic void
111219820Sjeffusage(void)
112219820Sjeff{
113219820Sjeff	char *basename;
114219820Sjeff
115219820Sjeff	if (!(basename = strrchr(argv0, '/')))
116219820Sjeff		basename = argv0;
117219820Sjeff	else
118219820Sjeff		basename++;
119219820Sjeff
120219820Sjeff	fprintf(stderr, "Usage: %s [-d(ebug) -N -w -G(uid) -C ca_name -P ca_port "
121219820Sjeff			"-t(imeout) timeout_ms -V(ersion) -h(elp)] <lid|guid>\n",
122219820Sjeff			basename);
123219820Sjeff	fprintf(stderr, "\tExamples:\n");
124219820Sjeff	fprintf(stderr, "\t\t%s -N 6\t\t# read IS3 general information\n", basename);
125219820Sjeff	fprintf(stderr, "\t\t%s -w 6\t\t# read IS3 port xmit wait counters\n", basename);
126219820Sjeff	exit(-1);
127219820Sjeff}
128219820Sjeff
129219820Sjeffint
130219820Sjeffmain(int argc, char **argv)
131219820Sjeff{
132219820Sjeff	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_MLX_VENDOR_CLASS};
133219820Sjeff	ib_portid_t *sm_id = 0, sm_portid = {0};
134219820Sjeff	ib_portid_t portid = {0};
135219820Sjeff	extern int ibdebug;
136219820Sjeff	int dest_type = IB_DEST_LID;
137219820Sjeff	int timeout = 0;	/* use default */
138219820Sjeff	int port = 0;
139219820Sjeff	char buf[1024];
140219820Sjeff	int udebug = 0;
141219820Sjeff	char *ca = 0;
142219820Sjeff	int ca_port = 0;
143219820Sjeff	ib_vendor_call_t call;
144219820Sjeff	is3_general_info_t *gi;
145219820Sjeff	is3_config_space_t *cs;
146219820Sjeff	int general_info = 0;
147219820Sjeff	int xmit_wait = 0;
148219820Sjeff	int i;
149219820Sjeff
150219820Sjeff	static char const str_opts[] = "C:P:s:t:dNwGVhu";
151219820Sjeff	static const struct option long_opts[] = {
152219820Sjeff		{ "C", 1, 0, 'C'},
153219820Sjeff		{ "P", 1, 0, 'P'},
154219820Sjeff		{ "N", 1, 0, 'N'},
155219820Sjeff		{ "w", 1, 0, 'w'},
156219820Sjeff		{ "debug", 0, 0, 'd'},
157219820Sjeff		{ "Guid", 0, 0, 'G'},
158219820Sjeff		{ "sm_portid", 1, 0, 's'},
159219820Sjeff		{ "timeout", 1, 0, 't'},
160219820Sjeff		{ "Version", 0, 0, 'V'},
161219820Sjeff		{ "help", 0, 0, 'h'},
162219820Sjeff		{ "usage", 0, 0, 'u'},
163219820Sjeff		{ }
164219820Sjeff	};
165219820Sjeff
166219820Sjeff	argv0 = argv[0];
167219820Sjeff
168219820Sjeff	while (1) {
169219820Sjeff		int ch = getopt_long(argc, argv, str_opts, long_opts, NULL);
170219820Sjeff		if ( ch == -1 )
171219820Sjeff			break;
172219820Sjeff		switch(ch) {
173219820Sjeff		case 'C':
174219820Sjeff			ca = optarg;
175219820Sjeff			break;
176219820Sjeff		case 'P':
177219820Sjeff			ca_port = strtoul(optarg, 0, 0);
178219820Sjeff			break;
179219820Sjeff		case 'N':
180219820Sjeff			general_info = 1;
181219820Sjeff			break;
182219820Sjeff		case 'w':
183219820Sjeff			xmit_wait = 1;
184219820Sjeff			break;
185219820Sjeff		case 'd':
186219820Sjeff			ibdebug++;
187219820Sjeff			madrpc_show_errors(1);
188219820Sjeff			umad_debug(udebug);
189219820Sjeff			udebug++;
190219820Sjeff			break;
191219820Sjeff		case 'G':
192219820Sjeff			dest_type = IB_DEST_GUID;
193219820Sjeff			break;
194219820Sjeff		case 's':
195219820Sjeff			if (ib_resolve_portid_str(&sm_portid, optarg, IB_DEST_LID, 0) < 0)
196219820Sjeff				IBERROR("can't resolve SM destination port %s", optarg);
197219820Sjeff			sm_id = &sm_portid;
198219820Sjeff			break;
199219820Sjeff		case 't':
200219820Sjeff			timeout = strtoul(optarg, 0, 0);
201219820Sjeff			madrpc_set_timeout(timeout);
202219820Sjeff			break;
203219820Sjeff		case 'V':
204219820Sjeff			fprintf(stderr, "%s %s\n", argv0, get_build_version() );
205219820Sjeff			exit(-1);
206219820Sjeff		default:
207219820Sjeff			usage();
208219820Sjeff			break;
209219820Sjeff		}
210219820Sjeff	}
211219820Sjeff	argc -= optind;
212219820Sjeff	argv += optind;
213219820Sjeff
214219820Sjeff	if (argc > 1)
215219820Sjeff		port = strtoul(argv[1], 0, 0);
216219820Sjeff
217219820Sjeff	madrpc_init(ca, ca_port, mgmt_classes, 4);
218219820Sjeff
219219820Sjeff	if (argc) {
220219820Sjeff		if (ib_resolve_portid_str(&portid, argv[0], dest_type, sm_id) < 0)
221219820Sjeff			IBERROR("can't resolve destination port %s", argv[0]);
222219820Sjeff	} else {
223219820Sjeff		if (ib_resolve_self(&portid, &port, 0) < 0)
224219820Sjeff			IBERROR("can't resolve self port %s", argv[0]);
225219820Sjeff	}
226219820Sjeff
227219820Sjeff	/* Only General Info and Port Xmit Wait Counters */
228219820Sjeff	/* queries are currently supported */
229219820Sjeff	if (!general_info && !xmit_wait)
230219820Sjeff		IBERROR("at least one of -N and -w must be specified");
231219820Sjeff
232219820Sjeff	/* These are Mellanox specific vendor MADs */
233219820Sjeff	/* but vendors change the VendorId so how know for sure ? */
234219820Sjeff	/* Would need a list of these and it might not be complete */
235219820Sjeff	/* so for right now, punt on this */
236219820Sjeff
237219820Sjeff	memset(&call, 0, sizeof(call));
238219820Sjeff	call.mgmt_class = IB_MLX_VENDOR_CLASS;
239219820Sjeff	call.method = IB_MAD_METHOD_GET;
240219820Sjeff	call.timeout = timeout;
241219820Sjeff
242219820Sjeff	memset(&buf, 0, sizeof(buf));
243219820Sjeff	/* vendor ClassPortInfo is required attribute if class supported */
244219820Sjeff	call.attrid = CLASS_PORT_INFO;
245219820Sjeff	if (!ib_vendor_call(&buf, &portid, &call))
246219820Sjeff		IBERROR("classportinfo query");
247219820Sjeff
248219820Sjeff	memset(&buf, 0, sizeof(buf));
249219820Sjeff	call.attrid = IB_MLX_IS3_GENERAL_INFO;
250219820Sjeff	if (!ib_vendor_call(&buf, &portid, &call))
251219820Sjeff		IBERROR("vendstat");
252219820Sjeff	gi = (is3_general_info_t *)&buf;
253219820Sjeff
254219820Sjeff	if (general_info) {
255219820Sjeff		/* dump IS3 general info here */
256219820Sjeff		printf("hw_dev_rev:  0x%04x\n", ntohs(gi->hw_info.hw_revision));
257219820Sjeff		printf("hw_dev_id:   0x%04x\n", ntohs(gi->hw_info.device_id));
258219820Sjeff		printf("hw_uptime:   0x%08x\n", ntohl(gi->hw_info.uptime));
259219820Sjeff		printf("fw_version:  %02d.%02d.%02d\n",
260219820Sjeff		       gi->fw_info.major, gi->fw_info.minor, gi->fw_info.sub_minor);
261219820Sjeff		printf("fw_build_id: 0x%04x\n", ntohl(gi->fw_info.build_id));
262219820Sjeff		printf("fw_date:     %02d/%02d/%04x\n",
263219820Sjeff		       gi->fw_info.month, gi->fw_info.day, ntohs(gi->fw_info.year));
264219820Sjeff		printf("fw_psid:     '%s'\n", gi->fw_info.psid);
265219820Sjeff		printf("fw_ini_ver:  %d\n", ntohl(gi->fw_info.ini_file_version));
266219820Sjeff		printf("sw_version:  %02d.%02d.%02d\n",
267219820Sjeff		       gi->sw_info.major, gi->sw_info.minor, gi->sw_info.sub_minor);
268219820Sjeff	}
269219820Sjeff
270219820Sjeff	if (xmit_wait) {
271219820Sjeff		if (ntohs(gi->hw_info.device_id) != IS3_DEVICE_ID)
272219820Sjeff			IBERROR("Unsupported device ID 0x%x", ntohs(gi->hw_info.device_id));
273219820Sjeff
274219820Sjeff		memset(&buf, 0, sizeof(buf));
275219820Sjeff		call.attrid = IB_MLX_IS3_CONFIG_SPACE_ACCESS;
276219820Sjeff		/* Limit of 18 accesses per MAD ? */
277219820Sjeff		call.mod = 2 << 22 | 16 << 16; /* 16 records */
278219820Sjeff		/* Set record addresses for each port */
279219820Sjeff		cs = (is3_config_space_t *)&buf;
280219820Sjeff		for (i = 0; i < 16; i++)
281219820Sjeff			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 1) << 12));
282219820Sjeff		if (!ib_vendor_call(&buf, &portid, &call))
283219820Sjeff			IBERROR("vendstat");
284219820Sjeff
285219820Sjeff		for (i = 0; i < 16; i++)
286219820Sjeff			if (cs->record[i].data)	/* PortXmitWait is 32 bit counter */
287219820Sjeff				printf("Port %d: PortXmitWait 0x%x\n", i + 4, ntohl(cs->record[i].data)); /* port 4 is first port */
288219820Sjeff
289219820Sjeff		/* Last 8 ports is another query */
290219820Sjeff		memset(&buf, 0, sizeof(buf));
291219820Sjeff		call.attrid = IB_MLX_IS3_CONFIG_SPACE_ACCESS;
292219820Sjeff                call.mod = 2 << 22 | 8 << 16; /* 8 records */
293219820Sjeff		/* Set record addresses for each port */
294219820Sjeff		cs = (is3_config_space_t *)&buf;
295219820Sjeff		for (i = 0; i < 8; i++)
296219820Sjeff			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 17) << 12));
297219820Sjeff		if (!ib_vendor_call(&buf, &portid, &call))
298219820Sjeff			IBERROR("vendstat");
299219820Sjeff
300219820Sjeff		for (i = 0; i < 8; i++)
301219820Sjeff			if (cs->record[i].data) /* PortXmitWait is 32 bit counter */
302219820Sjeff				printf("Port %d: PortXmitWait 0x%x\n",
303219820Sjeff				       i < 4 ? i + 21 : i - 3,
304219820Sjeff				       ntohl(cs->record[i].data));
305219820Sjeff	}
306219820Sjeff
307219820Sjeff	exit(0);
308219820Sjeff}
309