devinfo.c revision 272407
1231869Skib/*
2231869Skib * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3231869Skib * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
4231869Skib *
5231869Skib * This software is available to you under a choice of one of two
6231869Skib * licenses.  You may choose to be licensed under the terms of the GNU
7231869Skib * General Public License (GPL) Version 2, available from the file
8231869Skib * COPYING in the main directory of this source tree, or the
9231869Skib * OpenIB.org BSD license below:
10231869Skib *
11231869Skib *     Redistribution and use in source and binary forms, with or
12231869Skib *     without modification, are permitted provided that the following
13231869Skib *     conditions are met:
14231869Skib *
15231869Skib *      - Redistributions of source code must retain the above
16231869Skib *        copyright notice, this list of conditions and the following
17231869Skib *        disclaimer.
18274531Semaste *
19231869Skib *      - Redistributions in binary form must reproduce the above
20231869Skib *        copyright notice, this list of conditions and the following
21231869Skib *        disclaimer in the documentation and/or other materials
22231869Skib *        provided with the distribution.
23231869Skib *
24231869Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25231869Skib * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26231873Skib * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27231869Skib * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28231869Skib * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29231869Skib * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30231869Skib * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31231869Skib * SOFTWARE.
32251369Sjoel */
33231869Skib
34231869Skib#if HAVE_CONFIG_H
35231869Skib#  include <config.h>
36231869Skib#endif /* HAVE_CONFIG_H */
37231869Skib
38231869Skib#include <stdio.h>
39231869Skib#include <stdlib.h>
40231869Skib#include <unistd.h>
41231869Skib#include <string.h>
42231869Skib#include <getopt.h>
43231869Skib#include <netinet/in.h>
44231869Skib
45231869Skib#include <infiniband/verbs.h>
46231869Skib#include <infiniband/driver.h>
47231869Skib#include <infiniband/arch.h>
48231869Skib
49231869Skibstatic int verbose;
50231869Skib
51231869Skibstatic int null_gid(union ibv_gid *gid)
52231869Skib{
53231869Skib	return !(gid->raw[8] | gid->raw[9] | gid->raw[10] | gid->raw[11] |
54231869Skib		 gid->raw[12] | gid->raw[13] | gid->raw[14] | gid->raw[15]);
55231869Skib}
56231869Skib
57231869Skibstatic const char *guid_str(uint64_t node_guid, char *str)
58231869Skib{
59231869Skib	node_guid = ntohll(node_guid);
60231869Skib	sprintf(str, "%04x:%04x:%04x:%04x",
61231869Skib		(unsigned) (node_guid >> 48) & 0xffff,
62231869Skib		(unsigned) (node_guid >> 32) & 0xffff,
63231869Skib		(unsigned) (node_guid >> 16) & 0xffff,
64231869Skib		(unsigned) (node_guid >>  0) & 0xffff);
65231869Skib	return str;
66231869Skib}
67231869Skib
68231869Skibstatic const char *transport_str(enum ibv_transport_type transport)
69231869Skib{
70231869Skib	switch (transport) {
71274531Semaste	case IBV_TRANSPORT_IB:    return "InfiniBand";
72231869Skib	case IBV_TRANSPORT_IWARP: return "iWARP";
73231869Skib	default:		  return "invalid transport";
74231869Skib	}
75231869Skib}
76231869Skib
77231869Skibstatic const char *port_state_str(enum ibv_port_state pstate)
78231869Skib{
79231869Skib	switch (pstate) {
80231869Skib	case IBV_PORT_DOWN:   return "PORT_DOWN";
81231869Skib	case IBV_PORT_INIT:   return "PORT_INIT";
82231869Skib	case IBV_PORT_ARMED:  return "PORT_ARMED";
83231869Skib	case IBV_PORT_ACTIVE: return "PORT_ACTIVE";
84231869Skib	default:              return "invalid state";
85231869Skib	}
86231869Skib}
87231869Skib
88231869Skibstatic const char *port_phy_state_str(uint8_t phys_state)
89231869Skib{
90231869Skib	switch (phys_state) {
91231869Skib	case 1:  return "SLEEP";
92231869Skib	case 2:  return "POLLING";
93231869Skib	case 3:  return "DISABLED";
94231869Skib	case 4:  return "PORT_CONFIGURATION TRAINNING";
95231869Skib	case 5:  return "LINK_UP";
96231869Skib	case 6:  return "LINK_ERROR_RECOVERY";
97231869Skib	case 7:  return "PHY TEST";
98231869Skib	default: return "invalid physical state";
99231869Skib	}
100231869Skib}
101231869Skib
102231869Skibstatic const char *atomic_cap_str(enum ibv_atomic_cap atom_cap)
103231869Skib{
104231869Skib	switch (atom_cap) {
105231869Skib	case IBV_ATOMIC_NONE: return "ATOMIC_NONE";
106231869Skib	case IBV_ATOMIC_HCA:  return "ATOMIC_HCA";
107231869Skib	case IBV_ATOMIC_GLOB: return "ATOMIC_GLOB";
108231873Skib	default:              return "invalid atomic capability";
109231869Skib	}
110231869Skib}
111231869Skib
112231869Skibstatic const char *mtu_str(enum ibv_mtu max_mtu)
113231869Skib{
114231869Skib	switch (max_mtu) {
115231869Skib	case IBV_MTU_256:  return "256";
116	case IBV_MTU_512:  return "512";
117	case IBV_MTU_1024: return "1024";
118	case IBV_MTU_2048: return "2048";
119	case IBV_MTU_4096: return "4096";
120	default:           return "invalid MTU";
121	}
122}
123
124static const char *width_str(uint8_t width)
125{
126	switch (width) {
127	case 1:  return "1";
128	case 2:  return "4";
129	case 4:  return "8";
130	case 8:  return "12";
131	default: return "invalid width";
132	}
133}
134
135static const char *speed_str(uint8_t speed)
136{
137	switch (speed) {
138	case 1:  return "2.5 Gbps";
139	case 2:  return "5.0 Gbps";
140	case 4:  return "10.0 Gbps";
141	default: return "invalid speed";
142	}
143}
144
145static const char *vl_str(uint8_t vl_num)
146{
147	switch (vl_num) {
148	case 1:  return "1";
149	case 2:  return "2";
150	case 3:  return "4";
151	case 4:  return "8";
152	case 5:  return "15";
153	default: return "invalid value";
154	}
155}
156
157static int print_all_port_gids(struct ibv_context *ctx, uint8_t port_num, int tbl_len)
158{
159	union ibv_gid gid;
160	int rc = 0;
161	int i;
162
163	for (i = 0; i < tbl_len; i++) {
164		rc = ibv_query_gid(ctx, port_num, i, &gid);
165		if (rc) {
166			fprintf(stderr, "Failed to query gid to port %d, index %d\n",
167			       port_num, i);
168			return rc;
169		}
170		if (!null_gid(&gid))
171			printf("\t\t\tGID[%3d]:\t\t%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
172			       i,
173			       gid.raw[ 0], gid.raw[ 1],
174			       gid.raw[ 2], gid.raw[ 3],
175			       gid.raw[ 4], gid.raw[ 5],
176			       gid.raw[ 6], gid.raw[ 7],
177			       gid.raw[ 8], gid.raw[ 9],
178			       gid.raw[10], gid.raw[11],
179			       gid.raw[12], gid.raw[13],
180			       gid.raw[14], gid.raw[15]);
181	}
182	return rc;
183}
184
185static const char *link_layer_str(uint8_t link_layer)
186{
187	switch (link_layer) {
188	case IBV_LINK_LAYER_UNSPECIFIED:
189	case IBV_LINK_LAYER_INFINIBAND:
190		return "IB";
191	case IBV_LINK_LAYER_ETHERNET:
192		return "Ethernet";
193	default:
194		return "Unknown";
195	}
196}
197
198static int print_hca_cap(struct ibv_device *ib_dev, uint8_t ib_port)
199{
200	struct ibv_context *ctx;
201	struct ibv_device_attr device_attr;
202	struct ibv_port_attr port_attr;
203	int rc = 0;
204	uint8_t port;
205	char buf[256];
206
207	ctx = ibv_open_device(ib_dev);
208	if (!ctx) {
209		fprintf(stderr, "Failed to open device\n");
210		rc = 1;
211		goto cleanup;
212	}
213	if (ibv_query_device(ctx, &device_attr)) {
214		fprintf(stderr, "Failed to query device props");
215		rc = 2;
216		goto cleanup;
217	}
218
219	printf("hca_id:\t%s\n", ibv_get_device_name(ib_dev));
220	printf("\ttransport:\t\t\t%s (%d)\n",
221	       transport_str(ib_dev->transport_type), ib_dev->transport_type);
222	if (strlen(device_attr.fw_ver))
223		printf("\tfw_ver:\t\t\t\t%s\n", device_attr.fw_ver);
224	printf("\tnode_guid:\t\t\t%s\n", guid_str(device_attr.node_guid, buf));
225	printf("\tsys_image_guid:\t\t\t%s\n", guid_str(device_attr.sys_image_guid, buf));
226	printf("\tvendor_id:\t\t\t0x%04x\n", device_attr.vendor_id);
227	printf("\tvendor_part_id:\t\t\t%d\n", device_attr.vendor_part_id);
228	printf("\thw_ver:\t\t\t\t0x%X\n", device_attr.hw_ver);
229
230	if (ibv_read_sysfs_file(ib_dev->ibdev_path, "board_id", buf, sizeof buf) > 0)
231		printf("\tboard_id:\t\t\t%s\n", buf);
232
233	printf("\tphys_port_cnt:\t\t\t%d\n", device_attr.phys_port_cnt);
234
235	if (verbose) {
236		printf("\tmax_mr_size:\t\t\t0x%llx\n",
237		       (unsigned long long) device_attr.max_mr_size);
238		printf("\tpage_size_cap:\t\t\t0x%llx\n",
239		       (unsigned long long) device_attr.page_size_cap);
240		printf("\tmax_qp:\t\t\t\t%d\n", device_attr.max_qp);
241		printf("\tmax_qp_wr:\t\t\t%d\n", device_attr.max_qp_wr);
242		printf("\tdevice_cap_flags:\t\t0x%08x\n", device_attr.device_cap_flags);
243		printf("\tmax_sge:\t\t\t%d\n", device_attr.max_sge);
244		printf("\tmax_sge_rd:\t\t\t%d\n", device_attr.max_sge_rd);
245		printf("\tmax_cq:\t\t\t\t%d\n", device_attr.max_cq);
246		printf("\tmax_cqe:\t\t\t%d\n", device_attr.max_cqe);
247		printf("\tmax_mr:\t\t\t\t%d\n", device_attr.max_mr);
248		printf("\tmax_pd:\t\t\t\t%d\n", device_attr.max_pd);
249		printf("\tmax_qp_rd_atom:\t\t\t%d\n", device_attr.max_qp_rd_atom);
250		printf("\tmax_ee_rd_atom:\t\t\t%d\n", device_attr.max_ee_rd_atom);
251		printf("\tmax_res_rd_atom:\t\t%d\n", device_attr.max_res_rd_atom);
252		printf("\tmax_qp_init_rd_atom:\t\t%d\n", device_attr.max_qp_init_rd_atom);
253		printf("\tmax_ee_init_rd_atom:\t\t%d\n", device_attr.max_ee_init_rd_atom);
254		printf("\tatomic_cap:\t\t\t%s (%d)\n",
255		       atomic_cap_str(device_attr.atomic_cap), device_attr.atomic_cap);
256		printf("\tmax_ee:\t\t\t\t%d\n", device_attr.max_ee);
257		printf("\tmax_rdd:\t\t\t%d\n", device_attr.max_rdd);
258		printf("\tmax_mw:\t\t\t\t%d\n", device_attr.max_mw);
259		printf("\tmax_raw_ipv6_qp:\t\t%d\n", device_attr.max_raw_ipv6_qp);
260		printf("\tmax_raw_ethy_qp:\t\t%d\n", device_attr.max_raw_ethy_qp);
261		printf("\tmax_mcast_grp:\t\t\t%d\n", device_attr.max_mcast_grp);
262		printf("\tmax_mcast_qp_attach:\t\t%d\n", device_attr.max_mcast_qp_attach);
263		printf("\tmax_total_mcast_qp_attach:\t%d\n",
264		       device_attr.max_total_mcast_qp_attach);
265		printf("\tmax_ah:\t\t\t\t%d\n", device_attr.max_ah);
266		printf("\tmax_fmr:\t\t\t%d\n", device_attr.max_fmr);
267		if (device_attr.max_fmr)
268			printf("\tmax_map_per_fmr:\t\t%d\n", device_attr.max_map_per_fmr);
269		printf("\tmax_srq:\t\t\t%d\n", device_attr.max_srq);
270		if (device_attr.max_srq) {
271			printf("\tmax_srq_wr:\t\t\t%d\n", device_attr.max_srq_wr);
272			printf("\tmax_srq_sge:\t\t\t%d\n", device_attr.max_srq_sge);
273		}
274		printf("\tmax_pkeys:\t\t\t%d\n", device_attr.max_pkeys);
275		printf("\tlocal_ca_ack_delay:\t\t%d\n", device_attr.local_ca_ack_delay);
276	}
277
278	for (port = 1; port <= device_attr.phys_port_cnt; ++port) {
279		/* if in the command line the user didn't ask for info about this port */
280		if ((ib_port) && (port != ib_port))
281			continue;
282
283		rc = ibv_query_port(ctx, port, &port_attr);
284		if (rc) {
285			fprintf(stderr, "Failed to query port %u props\n", port);
286			goto cleanup;
287		}
288		printf("\t\tport:\t%d\n", port);
289		printf("\t\t\tstate:\t\t\t%s (%d)\n",
290		       port_state_str(port_attr.state), port_attr.state);
291		printf("\t\t\tmax_mtu:\t\t%s (%d)\n",
292		       mtu_str(port_attr.max_mtu), port_attr.max_mtu);
293		printf("\t\t\tactive_mtu:\t\t%s (%d)\n",
294		       mtu_str(port_attr.active_mtu), port_attr.active_mtu);
295		printf("\t\t\tsm_lid:\t\t\t%d\n", port_attr.sm_lid);
296		printf("\t\t\tport_lid:\t\t%d\n", port_attr.lid);
297		printf("\t\t\tport_lmc:\t\t0x%02x\n", port_attr.lmc);
298		printf("\t\t\tlink_layer:\t\t%s\n", link_layer_str(port_attr.link_layer));
299
300		if (verbose) {
301			printf("\t\t\tmax_msg_sz:\t\t0x%x\n", port_attr.max_msg_sz);
302			printf("\t\t\tport_cap_flags:\t\t0x%08x\n", port_attr.port_cap_flags);
303			printf("\t\t\tmax_vl_num:\t\t%s (%d)\n",
304			       vl_str(port_attr.max_vl_num), port_attr.max_vl_num);
305			printf("\t\t\tbad_pkey_cntr:\t\t0x%x\n", port_attr.bad_pkey_cntr);
306			printf("\t\t\tqkey_viol_cntr:\t\t0x%x\n", port_attr.qkey_viol_cntr);
307			printf("\t\t\tsm_sl:\t\t\t%d\n", port_attr.sm_sl);
308			printf("\t\t\tpkey_tbl_len:\t\t%d\n", port_attr.pkey_tbl_len);
309			printf("\t\t\tgid_tbl_len:\t\t%d\n", port_attr.gid_tbl_len);
310			printf("\t\t\tsubnet_timeout:\t\t%d\n", port_attr.subnet_timeout);
311			printf("\t\t\tinit_type_reply:\t%d\n", port_attr.init_type_reply);
312			printf("\t\t\tactive_width:\t\t%sX (%d)\n",
313			       width_str(port_attr.active_width), port_attr.active_width);
314			printf("\t\t\tactive_speed:\t\t%s (%d)\n",
315			       speed_str(port_attr.active_speed), port_attr.active_speed);
316			printf("\t\t\tphys_state:\t\t%s (%d)\n",
317			       port_phy_state_str(port_attr.phys_state), port_attr.phys_state);
318
319			if (print_all_port_gids(ctx, port, port_attr.gid_tbl_len))
320				goto cleanup;
321		}
322		printf("\n");
323	}
324cleanup:
325	if (ctx)
326		if (ibv_close_device(ctx)) {
327			fprintf(stderr, "Failed to close device");
328			rc = 3;
329		}
330	return rc;
331}
332
333static void usage(const char *argv0)
334{
335	printf("Usage: %s             print the ca attributes\n", argv0);
336	printf("\n");
337	printf("Options:\n");
338	printf("  -d, --ib-dev=<dev>     use IB device <dev> (default first device found)\n");
339	printf("  -i, --ib-port=<port>   use port <port> of IB device (default all ports)\n");
340	printf("  -l, --list             print only the IB devices names\n");
341	printf("  -v, --verbose          print all the attributes of the IB device(s)\n");
342}
343
344int main(int argc, char *argv[])
345{
346	char *ib_devname = NULL;
347	int ret = 0;
348	struct ibv_device **dev_list, **orig_dev_list;
349	int num_of_hcas;
350	int ib_port = 0;
351
352	/* parse command line options */
353	while (1) {
354		int c;
355		static struct option long_options[] = {
356			{ .name = "ib-dev",   .has_arg = 1, .val = 'd' },
357			{ .name = "ib-port",  .has_arg = 1, .val = 'i' },
358			{ .name = "list",     .has_arg = 0, .val = 'l' },
359			{ .name = "verbose",  .has_arg = 0, .val = 'v' },
360			{ 0, 0, 0, 0}
361		};
362
363		c = getopt_long(argc, argv, "d:i:lv", long_options, NULL);
364		if (c == -1)
365			break;
366
367		switch (c) {
368		case 'd':
369			ib_devname = strdup(optarg);
370			break;
371
372		case 'i':
373			ib_port = strtol(optarg, NULL, 0);
374			if (ib_port < 0) {
375				usage(argv[0]);
376				return 1;
377			}
378			break;
379
380		case 'v':
381			verbose = 1;
382			break;
383
384		case 'l':
385			dev_list = orig_dev_list = ibv_get_device_list(&num_of_hcas);
386			if (!dev_list) {
387				perror("Failed to get IB devices list");
388				return -1;
389			}
390
391			printf("%d HCA%s found:\n", num_of_hcas,
392			       num_of_hcas != 1 ? "s" : "");
393
394			while (*dev_list) {
395				printf("\t%s\n", ibv_get_device_name(*dev_list));
396				++dev_list;
397			}
398
399			printf("\n");
400
401			ibv_free_device_list(orig_dev_list);
402
403			return 0;
404
405		default:
406			usage(argv[0]);
407			return -1;
408		}
409	}
410
411	dev_list = orig_dev_list = ibv_get_device_list(NULL);
412	if (!dev_list) {
413		perror("Failed to get IB devices list");
414		return -1;
415	}
416
417	if (ib_devname) {
418		while (*dev_list) {
419			if (!strcmp(ibv_get_device_name(*dev_list), ib_devname))
420				break;
421			++dev_list;
422		}
423
424		if (!*dev_list) {
425			fprintf(stderr, "IB device '%s' wasn't found\n", ib_devname);
426			return -1;
427		}
428
429		ret |= print_hca_cap(*dev_list, ib_port);
430	} else {
431		if (!*dev_list) {
432			fprintf(stderr, "No IB devices found\n");
433			return -1;
434		}
435
436		while (*dev_list) {
437			ret |= print_hca_cap(*dev_list, ib_port);
438			++dev_list;
439		}
440	}
441
442	if (ib_devname)
443		free(ib_devname);
444
445	ibv_free_device_list(orig_dev_list);
446
447	return ret;
448}
449