1 /*
2  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3  * Copyright (c) 2005 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 #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 <string.h>
42 #include <getopt.h>
43 #include <netinet/in.h>
44 #include <endian.h>
45 #include <byteswap.h>
46 
47 #include <infiniband/verbs.h>
48 #include <infiniband/driver.h>
49 #include <infiniband/arch.h>
50 
51 static int verbose;
52 
53 static int null_gid(union ibv_gid *gid)
54 {
55 	return !(gid->raw[8] | gid->raw[9] | gid->raw[10] | gid->raw[11] |
56 		 gid->raw[12] | gid->raw[13] | gid->raw[14] | gid->raw[15]);
57 }
58 
59 static const char *guid_str(uint64_t node_guid, char *str)
60 {
61 	node_guid = ntohll(node_guid);
62 	sprintf(str, "%04x:%04x:%04x:%04x",
63 		(unsigned) (node_guid >> 48) & 0xffff,
64 		(unsigned) (node_guid >> 32) & 0xffff,
65 		(unsigned) (node_guid >> 16) & 0xffff,
66 		(unsigned) (node_guid >>  0) & 0xffff);
67 	return str;
68 }
69 
70 static const char *transport_str(enum ibv_transport_type transport)
71 {
72 	switch (transport) {
73 	case IBV_TRANSPORT_IB:    return "InfiniBand";
74 	case IBV_TRANSPORT_IWARP: return "iWARP";
75 	default:		  return "invalid transport";
76 	}
77 }
78 
79 static const char *port_state_str(enum ibv_port_state pstate)
80 {
81 	switch (pstate) {
82 	case IBV_PORT_DOWN:   return "PORT_DOWN";
83 	case IBV_PORT_INIT:   return "PORT_INIT";
84 	case IBV_PORT_ARMED:  return "PORT_ARMED";
85 	case IBV_PORT_ACTIVE: return "PORT_ACTIVE";
86 	default:              return "invalid state";
87 	}
88 }
89 
90 static const char *port_phy_state_str(uint8_t phys_state)
91 {
92 	switch (phys_state) {
93 	case 1:  return "SLEEP";
94 	case 2:  return "POLLING";
95 	case 3:  return "DISABLED";
96 	case 4:  return "PORT_CONFIGURATION TRAINNING";
97 	case 5:  return "LINK_UP";
98 	case 6:  return "LINK_ERROR_RECOVERY";
99 	case 7:  return "PHY TEST";
100 	default: return "invalid physical state";
101 	}
102 }
103 
104 static const char *atomic_cap_str(enum ibv_atomic_cap atom_cap)
105 {
106 	switch (atom_cap) {
107 	case IBV_ATOMIC_NONE: return "ATOMIC_NONE";
108 	case IBV_ATOMIC_HCA:  return "ATOMIC_HCA";
109 	case IBV_ATOMIC_GLOB: return "ATOMIC_GLOB";
110 	default:              return "invalid atomic capability";
111 	}
112 }
113 
114 static const char *mtu_str(enum ibv_mtu max_mtu)
115 {
116 	switch (max_mtu) {
117 	case IBV_MTU_256:  return "256";
118 	case IBV_MTU_512:  return "512";
119 	case IBV_MTU_1024: return "1024";
120 	case IBV_MTU_2048: return "2048";
121 	case IBV_MTU_4096: return "4096";
122 	default:           return "invalid MTU";
123 	}
124 }
125 
126 static const char *width_str(uint8_t width)
127 {
128 	switch (width) {
129 	case 1:  return "1";
130 	case 2:  return "4";
131 	case 4:  return "8";
132 	case 8:  return "12";
133 	default: return "invalid width";
134 	}
135 }
136 
137 static const char *speed_str(uint8_t speed)
138 {
139 	switch (speed) {
140 	case 1:  return "2.5 Gbps";
141 	case 2:  return "5.0 Gbps";
142 	case 4:  return "10.0 Gbps";
143 	default: return "invalid speed";
144 	}
145 }
146 
147 static const char *vl_str(uint8_t vl_num)
148 {
149 	switch (vl_num) {
150 	case 1:  return "1";
151 	case 2:  return "2";
152 	case 3:  return "4";
153 	case 4:  return "8";
154 	case 5:  return "15";
155 	default: return "invalid value";
156 	}
157 }
158 
159 static int print_all_port_gids(struct ibv_context *ctx, uint8_t port_num, int tbl_len)
160 {
161 	union ibv_gid gid;
162 	int rc = 0;
163 	int i;
164 
165 	for (i = 0; i < tbl_len; i++) {
166 		rc = ibv_query_gid(ctx, port_num, i, &gid);
167 		if (rc) {
168 			fprintf(stderr, "Failed to query gid to port %d, index %d\n",
169 			       port_num, i);
170 			return rc;
171 		}
172 		if (!null_gid(&gid))
173 			printf("\t\t\tGID[%3d]:\t\t%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
174 			       i,
175 			       gid.raw[ 0], gid.raw[ 1],
176 			       gid.raw[ 2], gid.raw[ 3],
177 			       gid.raw[ 4], gid.raw[ 5],
178 			       gid.raw[ 6], gid.raw[ 7],
179 			       gid.raw[ 8], gid.raw[ 9],
180 			       gid.raw[10], gid.raw[11],
181 			       gid.raw[12], gid.raw[13],
182 			       gid.raw[14], gid.raw[15]);
183 	}
184 	return rc;
185 }
186 
187 static const char *link_layer_str(uint8_t link_layer)
188 {
189 	switch (link_layer) {
190 	case IBV_LINK_LAYER_UNSPECIFIED:
191 	case IBV_LINK_LAYER_INFINIBAND:
192 		return "IB";
193 	case IBV_LINK_LAYER_ETHERNET:
194 		return "Ethernet";
195 	default:
196 		return "Unknown";
197 	}
198 }
199 
200 static int print_hca_cap(struct ibv_device *ib_dev, uint8_t ib_port)
201 {
202 	struct ibv_context *ctx;
203 	struct ibv_device_attr device_attr;
204 	struct ibv_port_attr port_attr;
205 	int rc = 0;
206 	uint8_t port;
207 	char buf[256];
208 
209 	ctx = ibv_open_device(ib_dev);
210 	if (!ctx) {
211 		fprintf(stderr, "Failed to open device\n");
212 		rc = 1;
213 		goto cleanup;
214 	}
215 	if (ibv_query_device(ctx, &device_attr)) {
216 		fprintf(stderr, "Failed to query device props");
217 		rc = 2;
218 		goto cleanup;
219 	}
220 
221 	printf("hca_id:\t%s\n", ibv_get_device_name(ib_dev));
222 	printf("\ttransport:\t\t\t%s (%d)\n",
223 	       transport_str(ib_dev->transport_type), ib_dev->transport_type);
224 	if (strlen(device_attr.fw_ver))
225 		printf("\tfw_ver:\t\t\t\t%s\n", device_attr.fw_ver);
226 	printf("\tnode_guid:\t\t\t%s\n", guid_str(device_attr.node_guid, buf));
227 	printf("\tsys_image_guid:\t\t\t%s\n", guid_str(device_attr.sys_image_guid, buf));
228 	printf("\tvendor_id:\t\t\t0x%04x\n", device_attr.vendor_id);
229 	printf("\tvendor_part_id:\t\t\t%d\n", device_attr.vendor_part_id);
230 	printf("\thw_ver:\t\t\t\t0x%X\n", device_attr.hw_ver);
231 
232 	if (ibv_read_sysfs_file(ib_dev->ibdev_path, "board_id", buf, sizeof buf) > 0)
233 		printf("\tboard_id:\t\t\t%s\n", buf);
234 
235 	printf("\tphys_port_cnt:\t\t\t%d\n", device_attr.phys_port_cnt);
236 
237 	if (verbose) {
238 		printf("\tmax_mr_size:\t\t\t0x%llx\n",
239 		       (unsigned long long) device_attr.max_mr_size);
240 		printf("\tpage_size_cap:\t\t\t0x%llx\n",
241 		       (unsigned long long) device_attr.page_size_cap);
242 		printf("\tmax_qp:\t\t\t\t%d\n", device_attr.max_qp);
243 		printf("\tmax_qp_wr:\t\t\t%d\n", device_attr.max_qp_wr);
244 		printf("\tdevice_cap_flags:\t\t0x%08x\n", device_attr.device_cap_flags);
245 		printf("\tmax_sge:\t\t\t%d\n", device_attr.max_sge);
246 		printf("\tmax_sge_rd:\t\t\t%d\n", device_attr.max_sge_rd);
247 		printf("\tmax_cq:\t\t\t\t%d\n", device_attr.max_cq);
248 		printf("\tmax_cqe:\t\t\t%d\n", device_attr.max_cqe);
249 		printf("\tmax_mr:\t\t\t\t%d\n", device_attr.max_mr);
250 		printf("\tmax_pd:\t\t\t\t%d\n", device_attr.max_pd);
251 		printf("\tmax_qp_rd_atom:\t\t\t%d\n", device_attr.max_qp_rd_atom);
252 		printf("\tmax_ee_rd_atom:\t\t\t%d\n", device_attr.max_ee_rd_atom);
253 		printf("\tmax_res_rd_atom:\t\t%d\n", device_attr.max_res_rd_atom);
254 		printf("\tmax_qp_init_rd_atom:\t\t%d\n", device_attr.max_qp_init_rd_atom);
255 		printf("\tmax_ee_init_rd_atom:\t\t%d\n", device_attr.max_ee_init_rd_atom);
256 		printf("\tatomic_cap:\t\t\t%s (%d)\n",
257 		       atomic_cap_str(device_attr.atomic_cap), device_attr.atomic_cap);
258 		printf("\tmax_ee:\t\t\t\t%d\n", device_attr.max_ee);
259 		printf("\tmax_rdd:\t\t\t%d\n", device_attr.max_rdd);
260 		printf("\tmax_mw:\t\t\t\t%d\n", device_attr.max_mw);
261 		printf("\tmax_raw_ipv6_qp:\t\t%d\n", device_attr.max_raw_ipv6_qp);
262 		printf("\tmax_raw_ethy_qp:\t\t%d\n", device_attr.max_raw_ethy_qp);
263 		printf("\tmax_mcast_grp:\t\t\t%d\n", device_attr.max_mcast_grp);
264 		printf("\tmax_mcast_qp_attach:\t\t%d\n", device_attr.max_mcast_qp_attach);
265 		printf("\tmax_total_mcast_qp_attach:\t%d\n",
266 		       device_attr.max_total_mcast_qp_attach);
267 		printf("\tmax_ah:\t\t\t\t%d\n", device_attr.max_ah);
268 		printf("\tmax_fmr:\t\t\t%d\n", device_attr.max_fmr);
269 		if (device_attr.max_fmr)
270 			printf("\tmax_map_per_fmr:\t\t%d\n", device_attr.max_map_per_fmr);
271 		printf("\tmax_srq:\t\t\t%d\n", device_attr.max_srq);
272 		if (device_attr.max_srq) {
273 			printf("\tmax_srq_wr:\t\t\t%d\n", device_attr.max_srq_wr);
274 			printf("\tmax_srq_sge:\t\t\t%d\n", device_attr.max_srq_sge);
275 		}
276 		printf("\tmax_pkeys:\t\t\t%d\n", device_attr.max_pkeys);
277 		printf("\tlocal_ca_ack_delay:\t\t%d\n", device_attr.local_ca_ack_delay);
278 	}
279 
280 	for (port = 1; port <= device_attr.phys_port_cnt; ++port) {
281 		/* if in the command line the user didn't ask for info about this port */
282 		if ((ib_port) && (port != ib_port))
283 			continue;
284 
285 		rc = ibv_query_port(ctx, port, &port_attr);
286 		if (rc) {
287 			fprintf(stderr, "Failed to query port %u props\n", port);
288 			goto cleanup;
289 		}
290 		printf("\t\tport:\t%d\n", port);
291 		printf("\t\t\tstate:\t\t\t%s (%d)\n",
292 		       port_state_str(port_attr.state), port_attr.state);
293 		printf("\t\t\tmax_mtu:\t\t%s (%d)\n",
294 		       mtu_str(port_attr.max_mtu), port_attr.max_mtu);
295 		printf("\t\t\tactive_mtu:\t\t%s (%d)\n",
296 		       mtu_str(port_attr.active_mtu), port_attr.active_mtu);
297 		printf("\t\t\tsm_lid:\t\t\t%d\n", port_attr.sm_lid);
298 		printf("\t\t\tport_lid:\t\t%d\n", port_attr.lid);
299 		printf("\t\t\tport_lmc:\t\t0x%02x\n", port_attr.lmc);
300 		printf("\t\t\tlink_layer:\t\t%s\n", link_layer_str(port_attr.link_layer));
301 
302 		if (verbose) {
303 			printf("\t\t\tmax_msg_sz:\t\t0x%x\n", port_attr.max_msg_sz);
304 			printf("\t\t\tport_cap_flags:\t\t0x%08x\n", port_attr.port_cap_flags);
305 			printf("\t\t\tmax_vl_num:\t\t%s (%d)\n",
306 			       vl_str(port_attr.max_vl_num), port_attr.max_vl_num);
307 			printf("\t\t\tbad_pkey_cntr:\t\t0x%x\n", port_attr.bad_pkey_cntr);
308 			printf("\t\t\tqkey_viol_cntr:\t\t0x%x\n", port_attr.qkey_viol_cntr);
309 			printf("\t\t\tsm_sl:\t\t\t%d\n", port_attr.sm_sl);
310 			printf("\t\t\tpkey_tbl_len:\t\t%d\n", port_attr.pkey_tbl_len);
311 			printf("\t\t\tgid_tbl_len:\t\t%d\n", port_attr.gid_tbl_len);
312 			printf("\t\t\tsubnet_timeout:\t\t%d\n", port_attr.subnet_timeout);
313 			printf("\t\t\tinit_type_reply:\t%d\n", port_attr.init_type_reply);
314 			printf("\t\t\tactive_width:\t\t%sX (%d)\n",
315 			       width_str(port_attr.active_width), port_attr.active_width);
316 			printf("\t\t\tactive_speed:\t\t%s (%d)\n",
317 			       speed_str(port_attr.active_speed), port_attr.active_speed);
318 			printf("\t\t\tphys_state:\t\t%s (%d)\n",
319 			       port_phy_state_str(port_attr.phys_state), port_attr.phys_state);
320 
321 			if (print_all_port_gids(ctx, port, port_attr.gid_tbl_len))
322 				goto cleanup;
323 		}
324 		printf("\n");
325 	}
326 cleanup:
327 	if (ctx)
328 		if (ibv_close_device(ctx)) {
329 			fprintf(stderr, "Failed to close device");
330 			rc = 3;
331 		}
332 	return rc;
333 }
334 
335 static void usage(const char *argv0)
336 {
337 	printf("Usage: %s             print the ca attributes\n", argv0);
338 	printf("\n");
339 	printf("Options:\n");
340 	printf("  -d, --ib-dev=<dev>     use IB device <dev> (default first device found)\n");
341 	printf("  -i, --ib-port=<port>   use port <port> of IB device (default all ports)\n");
342 	printf("  -l, --list             print only the IB devices names\n");
343 	printf("  -v, --verbose          print all the attributes of the IB device(s)\n");
344 }
345 
346 int main(int argc, char *argv[])
347 {
348 	char *ib_devname = NULL;
349 	int ret = 0;
350 	struct ibv_device **dev_list, **orig_dev_list;
351 	int num_of_hcas;
352 	int ib_port = 0;
353 
354 	/* parse command line options */
355 	while (1) {
356 		int c;
357 		static struct option long_options[] = {
358 			{ .name = "ib-dev",   .has_arg = 1, .val = 'd' },
359 			{ .name = "ib-port",  .has_arg = 1, .val = 'i' },
360 			{ .name = "list",     .has_arg = 0, .val = 'l' },
361 			{ .name = "verbose",  .has_arg = 0, .val = 'v' },
362 			{ 0, 0, 0, 0}
363 		};
364 
365 		c = getopt_long(argc, argv, "d:i:lv", long_options, NULL);
366 		if (c == -1)
367 			break;
368 
369 		switch (c) {
370 		case 'd':
371 			ib_devname = strdup(optarg);
372 			break;
373 
374 		case 'i':
375 			ib_port = strtol(optarg, NULL, 0);
376 			if (ib_port < 0) {
377 				usage(argv[0]);
378 				return 1;
379 			}
380 			break;
381 
382 		case 'v':
383 			verbose = 1;
384 			break;
385 
386 		case 'l':
387 			dev_list = orig_dev_list = ibv_get_device_list(&num_of_hcas);
388 			if (!dev_list) {
389 				perror("Failed to get IB devices list");
390 				return -1;
391 			}
392 
393 			printf("%d HCA%s found:\n", num_of_hcas,
394 			       num_of_hcas != 1 ? "s" : "");
395 
396 			while (*dev_list) {
397 				printf("\t%s\n", ibv_get_device_name(*dev_list));
398 				++dev_list;
399 			}
400 
401 			printf("\n");
402 
403 			ibv_free_device_list(orig_dev_list);
404 
405 			return 0;
406 
407 		default:
408 			usage(argv[0]);
409 			return -1;
410 		}
411 	}
412 
413 	dev_list = orig_dev_list = ibv_get_device_list(NULL);
414 	if (!dev_list) {
415 		perror("Failed to get IB devices list");
416 		return -1;
417 	}
418 
419 	if (ib_devname) {
420 		while (*dev_list) {
421 			if (!strcmp(ibv_get_device_name(*dev_list), ib_devname))
422 				break;
423 			++dev_list;
424 		}
425 
426 		if (!*dev_list) {
427 			fprintf(stderr, "IB device '%s' wasn't found\n", ib_devname);
428 			return -1;
429 		}
430 
431 		ret |= print_hca_cap(*dev_list, ib_port);
432 	} else {
433 		if (!*dev_list) {
434 			fprintf(stderr, "No IB devices found\n");
435 			return -1;
436 		}
437 
438 		while (*dev_list) {
439 			ret |= print_hca_cap(*dev_list, ib_port);
440 			++dev_list;
441 		}
442 	}
443 
444 	if (ib_devname)
445 		free(ib_devname);
446 
447 	ibv_free_device_list(orig_dev_list);
448 
449 	return ret;
450 }
451