xref: /freebsd/contrib/ofed/libibverbs/examples/devinfo.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
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 #include <config.h>
35 
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <getopt.h>
41 #include <infiniband/endian.h>
42 #include <inttypes.h>
43 
44 #include <infiniband/verbs.h>
45 #include <infiniband/driver.h>
46 
47 static int verbose;
48 
49 static int null_gid(union ibv_gid *gid)
50 {
51 	return !(gid->raw[8] | gid->raw[9] | gid->raw[10] | gid->raw[11] |
52 		 gid->raw[12] | gid->raw[13] | gid->raw[14] | gid->raw[15]);
53 }
54 
55 static const char *guid_str(__be64 _node_guid, char *str)
56 {
57 	uint64_t node_guid = be64toh(_node_guid);
58 	sprintf(str, "%04x:%04x:%04x:%04x",
59 		(unsigned) (node_guid >> 48) & 0xffff,
60 		(unsigned) (node_guid >> 32) & 0xffff,
61 		(unsigned) (node_guid >> 16) & 0xffff,
62 		(unsigned) (node_guid >>  0) & 0xffff);
63 	return str;
64 }
65 
66 static const char *transport_str(enum ibv_transport_type transport)
67 {
68 	switch (transport) {
69 	case IBV_TRANSPORT_IB:		return "InfiniBand";
70 	case IBV_TRANSPORT_IWARP:	return "iWARP";
71 	case IBV_TRANSPORT_USNIC:	return "usNIC";
72 	case IBV_TRANSPORT_USNIC_UDP:	return "usNIC UDP";
73 	default:			return "invalid transport";
74 	}
75 }
76 
77 static const char *port_state_str(enum ibv_port_state pstate)
78 {
79 	switch (pstate) {
80 	case IBV_PORT_DOWN:   return "PORT_DOWN";
81 	case IBV_PORT_INIT:   return "PORT_INIT";
82 	case IBV_PORT_ARMED:  return "PORT_ARMED";
83 	case IBV_PORT_ACTIVE: return "PORT_ACTIVE";
84 	default:              return "invalid state";
85 	}
86 }
87 
88 static const char *port_phy_state_str(uint8_t phys_state)
89 {
90 	switch (phys_state) {
91 	case 1:  return "SLEEP";
92 	case 2:  return "POLLING";
93 	case 3:  return "DISABLED";
94 	case 4:  return "PORT_CONFIGURATION TRAINNING";
95 	case 5:  return "LINK_UP";
96 	case 6:  return "LINK_ERROR_RECOVERY";
97 	case 7:  return "PHY TEST";
98 	default: return "invalid physical state";
99 	}
100 }
101 
102 static const char *atomic_cap_str(enum ibv_atomic_cap atom_cap)
103 {
104 	switch (atom_cap) {
105 	case IBV_ATOMIC_NONE: return "ATOMIC_NONE";
106 	case IBV_ATOMIC_HCA:  return "ATOMIC_HCA";
107 	case IBV_ATOMIC_GLOB: return "ATOMIC_GLOB";
108 	default:              return "invalid atomic capability";
109 	}
110 }
111 
112 static const char *mtu_str(enum ibv_mtu max_mtu)
113 {
114 	switch (max_mtu) {
115 	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 
124 static 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 	case 16:  return "2";
132 	default: return "invalid width";
133 	}
134 }
135 
136 static const char *speed_str(uint8_t speed)
137 {
138 	switch (speed) {
139 	case 1:  return "2.5 Gbps";
140 	case 2:  return "5.0 Gbps";
141 
142 	case 4:  /* fall through */
143 	case 8:  return "10.0 Gbps";
144 
145 	case 16: return "14.0 Gbps";
146 	case 32: return "25.0 Gbps";
147 	case 64: return "50.0 Gbps";
148 	default: return "invalid speed";
149 	}
150 }
151 
152 static const char *vl_str(uint8_t vl_num)
153 {
154 	switch (vl_num) {
155 	case 1:  return "1";
156 	case 2:  return "2";
157 	case 3:  return "4";
158 	case 4:  return "8";
159 	case 5:  return "15";
160 	default: return "invalid value";
161 	}
162 }
163 
164 static int print_all_port_gids(struct ibv_context *ctx, uint8_t port_num, int tbl_len)
165 {
166 	union ibv_gid gid;
167 	int rc = 0;
168 	int i;
169 
170 	for (i = 0; i < tbl_len; i++) {
171 		rc = ibv_query_gid(ctx, port_num, i, &gid);
172 		if (rc) {
173 			fprintf(stderr, "Failed to query gid to port %d, index %d\n",
174 			       port_num, i);
175 			return rc;
176 		}
177 		if (!null_gid(&gid))
178 			printf("\t\t\tGID[%3d]:\t\t%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
179 			       i,
180 			       gid.raw[ 0], gid.raw[ 1],
181 			       gid.raw[ 2], gid.raw[ 3],
182 			       gid.raw[ 4], gid.raw[ 5],
183 			       gid.raw[ 6], gid.raw[ 7],
184 			       gid.raw[ 8], gid.raw[ 9],
185 			       gid.raw[10], gid.raw[11],
186 			       gid.raw[12], gid.raw[13],
187 			       gid.raw[14], gid.raw[15]);
188 	}
189 	return rc;
190 }
191 
192 static const char *link_layer_str(uint8_t link_layer)
193 {
194 	switch (link_layer) {
195 	case IBV_LINK_LAYER_UNSPECIFIED:
196 	case IBV_LINK_LAYER_INFINIBAND:
197 		return "InfiniBand";
198 	case IBV_LINK_LAYER_ETHERNET:
199 		return "Ethernet";
200 	default:
201 		return "Unknown";
202 	}
203 }
204 
205 static void print_device_cap_flags(uint32_t dev_cap_flags)
206 {
207 	uint32_t unknown_flags = ~(IBV_DEVICE_RESIZE_MAX_WR |
208 				   IBV_DEVICE_BAD_PKEY_CNTR |
209 				   IBV_DEVICE_BAD_QKEY_CNTR |
210 				   IBV_DEVICE_RAW_MULTI |
211 				   IBV_DEVICE_AUTO_PATH_MIG |
212 				   IBV_DEVICE_CHANGE_PHY_PORT |
213 				   IBV_DEVICE_UD_AV_PORT_ENFORCE |
214 				   IBV_DEVICE_CURR_QP_STATE_MOD |
215 				   IBV_DEVICE_SHUTDOWN_PORT |
216 				   IBV_DEVICE_INIT_TYPE |
217 				   IBV_DEVICE_PORT_ACTIVE_EVENT |
218 				   IBV_DEVICE_SYS_IMAGE_GUID |
219 				   IBV_DEVICE_RC_RNR_NAK_GEN |
220 				   IBV_DEVICE_SRQ_RESIZE |
221 				   IBV_DEVICE_N_NOTIFY_CQ |
222 				   IBV_DEVICE_MEM_WINDOW |
223 				   IBV_DEVICE_UD_IP_CSUM |
224 				   IBV_DEVICE_XRC |
225 				   IBV_DEVICE_MEM_MGT_EXTENSIONS |
226 				   IBV_DEVICE_MEM_WINDOW_TYPE_2A |
227 				   IBV_DEVICE_MEM_WINDOW_TYPE_2B |
228 				   IBV_DEVICE_RC_IP_CSUM |
229 				   IBV_DEVICE_RAW_IP_CSUM |
230 				   IBV_DEVICE_MANAGED_FLOW_STEERING);
231 
232 	if (dev_cap_flags & IBV_DEVICE_RESIZE_MAX_WR)
233 		printf("\t\t\t\t\tRESIZE_MAX_WR\n");
234 	if (dev_cap_flags & IBV_DEVICE_BAD_PKEY_CNTR)
235 		printf("\t\t\t\t\tBAD_PKEY_CNTR\n");
236 	if (dev_cap_flags & IBV_DEVICE_BAD_QKEY_CNTR)
237 		printf("\t\t\t\t\tBAD_QKEY_CNTR\n");
238 	if (dev_cap_flags & IBV_DEVICE_RAW_MULTI)
239 		printf("\t\t\t\t\tRAW_MULTI\n");
240 	if (dev_cap_flags & IBV_DEVICE_AUTO_PATH_MIG)
241 		printf("\t\t\t\t\tAUTO_PATH_MIG\n");
242 	if (dev_cap_flags & IBV_DEVICE_CHANGE_PHY_PORT)
243 		printf("\t\t\t\t\tCHANGE_PHY_PORT\n");
244 	if (dev_cap_flags & IBV_DEVICE_UD_AV_PORT_ENFORCE)
245 		printf("\t\t\t\t\tUD_AV_PORT_ENFORCE\n");
246 	if (dev_cap_flags & IBV_DEVICE_CURR_QP_STATE_MOD)
247 		printf("\t\t\t\t\tCURR_QP_STATE_MOD\n");
248 	if (dev_cap_flags & IBV_DEVICE_SHUTDOWN_PORT)
249 		printf("\t\t\t\t\tSHUTDOWN_PORT\n");
250 	if (dev_cap_flags & IBV_DEVICE_INIT_TYPE)
251 		printf("\t\t\t\t\tINIT_TYPE\n");
252 	if (dev_cap_flags & IBV_DEVICE_PORT_ACTIVE_EVENT)
253 		printf("\t\t\t\t\tPORT_ACTIVE_EVENT\n");
254 	if (dev_cap_flags & IBV_DEVICE_SYS_IMAGE_GUID)
255 		printf("\t\t\t\t\tSYS_IMAGE_GUID\n");
256 	if (dev_cap_flags & IBV_DEVICE_RC_RNR_NAK_GEN)
257 		printf("\t\t\t\t\tRC_RNR_NAK_GEN\n");
258 	if (dev_cap_flags & IBV_DEVICE_SRQ_RESIZE)
259 		printf("\t\t\t\t\tSRQ_RESIZE\n");
260 	if (dev_cap_flags & IBV_DEVICE_N_NOTIFY_CQ)
261 		printf("\t\t\t\t\tN_NOTIFY_CQ\n");
262 	if (dev_cap_flags & IBV_DEVICE_MEM_WINDOW)
263 		printf("\t\t\t\t\tMEM_WINDOW\n");
264 	if (dev_cap_flags & IBV_DEVICE_UD_IP_CSUM)
265 		printf("\t\t\t\t\tUD_IP_CSUM\n");
266 	if (dev_cap_flags & IBV_DEVICE_XRC)
267 		printf("\t\t\t\t\tXRC\n");
268 	if (dev_cap_flags & IBV_DEVICE_MEM_MGT_EXTENSIONS)
269 		printf("\t\t\t\t\tMEM_MGT_EXTENSIONS\n");
270 	if (dev_cap_flags & IBV_DEVICE_MEM_WINDOW_TYPE_2A)
271 		printf("\t\t\t\t\tMEM_WINDOW_TYPE_2A\n");
272 	if (dev_cap_flags & IBV_DEVICE_MEM_WINDOW_TYPE_2B)
273 		printf("\t\t\t\t\tMEM_WINDOW_TYPE_2B\n");
274 	if (dev_cap_flags & IBV_DEVICE_RC_IP_CSUM)
275 		printf("\t\t\t\t\tRC_IP_CSUM\n");
276 	if (dev_cap_flags & IBV_DEVICE_RAW_IP_CSUM)
277 		printf("\t\t\t\t\tRAW_IP_CSUM\n");
278 	if (dev_cap_flags & IBV_DEVICE_MANAGED_FLOW_STEERING)
279 		printf("\t\t\t\t\tMANAGED_FLOW_STEERING\n");
280 	if (dev_cap_flags & unknown_flags)
281 		printf("\t\t\t\t\tUnknown flags: 0x%" PRIX32 "\n",
282 		       dev_cap_flags & unknown_flags);
283 }
284 
285 static void print_odp_trans_caps(uint32_t trans)
286 {
287 	uint32_t unknown_transport_caps = ~(IBV_ODP_SUPPORT_SEND |
288 					    IBV_ODP_SUPPORT_RECV |
289 					    IBV_ODP_SUPPORT_WRITE |
290 					    IBV_ODP_SUPPORT_READ |
291 					    IBV_ODP_SUPPORT_ATOMIC);
292 
293 	if (!trans) {
294 		printf("\t\t\t\t\tNO SUPPORT\n");
295 	} else {
296 		if (trans & IBV_ODP_SUPPORT_SEND)
297 			printf("\t\t\t\t\tSUPPORT_SEND\n");
298 		if (trans & IBV_ODP_SUPPORT_RECV)
299 			printf("\t\t\t\t\tSUPPORT_RECV\n");
300 		if (trans & IBV_ODP_SUPPORT_WRITE)
301 			printf("\t\t\t\t\tSUPPORT_WRITE\n");
302 		if (trans & IBV_ODP_SUPPORT_READ)
303 			printf("\t\t\t\t\tSUPPORT_READ\n");
304 		if (trans & IBV_ODP_SUPPORT_ATOMIC)
305 			printf("\t\t\t\t\tSUPPORT_ATOMIC\n");
306 		if (trans & unknown_transport_caps)
307 			printf("\t\t\t\t\tUnknown flags: 0x%" PRIX32 "\n",
308 			       trans & unknown_transport_caps);
309 	}
310 }
311 
312 static void print_odp_caps(const struct ibv_odp_caps *caps)
313 {
314 	uint64_t unknown_general_caps = ~(IBV_ODP_SUPPORT);
315 
316 	/* general odp caps */
317 	printf("\tgeneral_odp_caps:\n");
318 	if (caps->general_caps & IBV_ODP_SUPPORT)
319 		printf("\t\t\t\t\tODP_SUPPORT\n");
320 	if (caps->general_caps & unknown_general_caps)
321 		printf("\t\t\t\t\tUnknown flags: 0x%" PRIX64 "\n",
322 		       caps->general_caps & unknown_general_caps);
323 
324 	/* RC transport */
325 	printf("\trc_odp_caps:\n");
326 	print_odp_trans_caps(caps->per_transport_caps.rc_odp_caps);
327 	printf("\tuc_odp_caps:\n");
328 	print_odp_trans_caps(caps->per_transport_caps.uc_odp_caps);
329 	printf("\tud_odp_caps:\n");
330 	print_odp_trans_caps(caps->per_transport_caps.ud_odp_caps);
331 }
332 
333 static void print_device_cap_flags_ex(uint64_t device_cap_flags_ex)
334 {
335 	uint64_t ex_flags = device_cap_flags_ex & 0xffffffff00000000ULL;
336 	uint64_t unknown_flags = ~(IBV_DEVICE_RAW_SCATTER_FCS);
337 
338 	if (ex_flags & IBV_DEVICE_RAW_SCATTER_FCS)
339 		printf("\t\t\t\t\tRAW_SCATTER_FCS\n");
340 	if (ex_flags & unknown_flags)
341 		printf("\t\t\t\t\tUnknown flags: 0x%" PRIX64 "\n",
342 		       ex_flags & unknown_flags);
343 }
344 
345 static void print_tso_caps(const struct ibv_tso_caps *caps)
346 {
347 	uint32_t unknown_general_caps = ~(1 << IBV_QPT_RAW_PACKET |
348 					  1 << IBV_QPT_UD);
349 	printf("\ttso_caps:\n");
350 	printf("\tmax_tso:\t\t\t%d\n", caps->max_tso);
351 
352 	if (caps->max_tso) {
353 		printf("\tsupported_qp:\n");
354 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_RAW_PACKET))
355 			printf("\t\t\t\t\tSUPPORT_RAW_PACKET\n");
356 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_UD))
357 			printf("\t\t\t\t\tSUPPORT_UD\n");
358 		if (caps->supported_qpts & unknown_general_caps)
359 			printf("\t\t\t\t\tUnknown flags: 0x%" PRIX32 "\n",
360 			       caps->supported_qpts & unknown_general_caps);
361 	}
362 }
363 
364 static void print_rss_caps(const struct ibv_rss_caps *caps)
365 {
366 	uint32_t unknown_general_caps = ~(1 << IBV_QPT_RAW_PACKET |
367 					  1 << IBV_QPT_UD);
368 	printf("\trss_caps:\n");
369 	printf("\t\tmax_rwq_indirection_tables:\t\t\t%u\n", caps->max_rwq_indirection_tables);
370 	printf("\t\tmax_rwq_indirection_table_size:\t\t\t%u\n", caps->max_rwq_indirection_table_size);
371 	printf("\t\trx_hash_function:\t\t\t\t0x%x\n", caps->rx_hash_function);
372 	printf("\t\trx_hash_fields_mask:\t\t\t\t0x%" PRIX64 "\n", caps->rx_hash_fields_mask);
373 
374 	if (caps->supported_qpts) {
375 		printf("\t\tsupported_qp:\n");
376 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_RAW_PACKET))
377 			printf("\t\t\t\t\tSUPPORT_RAW_PACKET\n");
378 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_UD))
379 			printf("\t\t\t\t\tSUPPORT_UD\n");
380 		if (caps->supported_qpts & unknown_general_caps)
381 			printf("\t\t\t\t\tUnknown flags: 0x%" PRIX32 "\n",
382 			       caps->supported_qpts & unknown_general_caps);
383 	}
384 }
385 
386 static void print_packet_pacing_caps(const struct ibv_packet_pacing_caps *caps)
387 {
388 	uint32_t unknown_general_caps = ~(1 << IBV_QPT_RAW_PACKET |
389 					  1 << IBV_QPT_UD);
390 	printf("\tpacket_pacing_caps:\n");
391 	printf("\t\tqp_rate_limit_min:\t%ukbps\n", caps->qp_rate_limit_min);
392 	printf("\t\tqp_rate_limit_max:\t%ukbps\n", caps->qp_rate_limit_max);
393 
394 	if (caps->qp_rate_limit_max) {
395 		printf("\t\tsupported_qp:\n");
396 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_RAW_PACKET))
397 			printf("\t\t\t\t\tSUPPORT_RAW_PACKET\n");
398 		if (ibv_is_qpt_supported(caps->supported_qpts, IBV_QPT_UD))
399 			printf("\t\t\t\t\tSUPPORT_UD\n");
400 		if (caps->supported_qpts & unknown_general_caps)
401 			printf("\t\t\t\t\tUnknown flags: 0x%" PRIX32 "\n",
402 			       caps->supported_qpts & unknown_general_caps);
403 	}
404 }
405 
406 static void print_raw_packet_caps(uint32_t raw_packet_caps)
407 {
408 	printf("\traw packet caps:\n");
409 	if (raw_packet_caps & IBV_RAW_PACKET_CAP_CVLAN_STRIPPING)
410 		printf("\t\t\t\t\tC-VLAN stripping offload\n");
411 	if (raw_packet_caps & IBV_RAW_PACKET_CAP_SCATTER_FCS)
412 		printf("\t\t\t\t\tScatter FCS offload\n");
413 	if (raw_packet_caps & IBV_RAW_PACKET_CAP_IP_CSUM)
414 		printf("\t\t\t\t\tIP csum offload\n");
415 }
416 
417 static int print_hca_cap(struct ibv_device *ib_dev, uint8_t ib_port)
418 {
419 	struct ibv_context *ctx;
420 	struct ibv_device_attr_ex device_attr;
421 	struct ibv_port_attr port_attr;
422 	int rc = 0;
423 	uint8_t port;
424 	char buf[256];
425 
426 	ctx = ibv_open_device(ib_dev);
427 	if (!ctx) {
428 		fprintf(stderr, "Failed to open device\n");
429 		rc = 1;
430 		goto cleanup;
431 	}
432 	if (ibv_query_device_ex(ctx, NULL, &device_attr)) {
433 		fprintf(stderr, "Failed to query device props\n");
434 		rc = 2;
435 		goto cleanup;
436 	}
437 	if (ib_port && ib_port > device_attr.orig_attr.phys_port_cnt) {
438 		fprintf(stderr, "Invalid port requested for device\n");
439 		/* rc = 3 is taken by failure to clean up */
440 		rc = 4;
441 		goto cleanup;
442 	}
443 
444 	printf("hca_id:\t%s\n", ibv_get_device_name(ib_dev));
445 	printf("\ttransport:\t\t\t%s (%d)\n",
446 	       transport_str(ib_dev->transport_type), ib_dev->transport_type);
447 	if (strlen(device_attr.orig_attr.fw_ver))
448 		printf("\tfw_ver:\t\t\t\t%s\n", device_attr.orig_attr.fw_ver);
449 	printf("\tnode_guid:\t\t\t%s\n", guid_str(device_attr.orig_attr.node_guid, buf));
450 	printf("\tsys_image_guid:\t\t\t%s\n", guid_str(device_attr.orig_attr.sys_image_guid, buf));
451 	printf("\tvendor_id:\t\t\t0x%04x\n", device_attr.orig_attr.vendor_id);
452 	printf("\tvendor_part_id:\t\t\t%d\n", device_attr.orig_attr.vendor_part_id);
453 	printf("\thw_ver:\t\t\t\t0x%X\n", device_attr.orig_attr.hw_ver);
454 
455 	if (ibv_read_sysfs_file(ib_dev->ibdev_path, "board_id", buf, sizeof buf) > 0)
456 		printf("\tboard_id:\t\t\t%s\n", buf);
457 
458 	printf("\tphys_port_cnt:\t\t\t%d\n", device_attr.orig_attr.phys_port_cnt);
459 
460 	if (verbose) {
461 		printf("\tmax_mr_size:\t\t\t0x%llx\n",
462 		       (unsigned long long) device_attr.orig_attr.max_mr_size);
463 		printf("\tpage_size_cap:\t\t\t0x%llx\n",
464 		       (unsigned long long) device_attr.orig_attr.page_size_cap);
465 		printf("\tmax_qp:\t\t\t\t%d\n", device_attr.orig_attr.max_qp);
466 		printf("\tmax_qp_wr:\t\t\t%d\n", device_attr.orig_attr.max_qp_wr);
467 		printf("\tdevice_cap_flags:\t\t0x%08x\n", device_attr.orig_attr.device_cap_flags);
468 		print_device_cap_flags(device_attr.orig_attr.device_cap_flags);
469 		printf("\tmax_sge:\t\t\t%d\n", device_attr.orig_attr.max_sge);
470 		printf("\tmax_sge_rd:\t\t\t%d\n", device_attr.orig_attr.max_sge_rd);
471 		printf("\tmax_cq:\t\t\t\t%d\n", device_attr.orig_attr.max_cq);
472 		printf("\tmax_cqe:\t\t\t%d\n", device_attr.orig_attr.max_cqe);
473 		printf("\tmax_mr:\t\t\t\t%d\n", device_attr.orig_attr.max_mr);
474 		printf("\tmax_pd:\t\t\t\t%d\n", device_attr.orig_attr.max_pd);
475 		printf("\tmax_qp_rd_atom:\t\t\t%d\n", device_attr.orig_attr.max_qp_rd_atom);
476 		printf("\tmax_ee_rd_atom:\t\t\t%d\n", device_attr.orig_attr.max_ee_rd_atom);
477 		printf("\tmax_res_rd_atom:\t\t%d\n", device_attr.orig_attr.max_res_rd_atom);
478 		printf("\tmax_qp_init_rd_atom:\t\t%d\n", device_attr.orig_attr.max_qp_init_rd_atom);
479 		printf("\tmax_ee_init_rd_atom:\t\t%d\n", device_attr.orig_attr.max_ee_init_rd_atom);
480 		printf("\tatomic_cap:\t\t\t%s (%d)\n",
481 		       atomic_cap_str(device_attr.orig_attr.atomic_cap), device_attr.orig_attr.atomic_cap);
482 		printf("\tmax_ee:\t\t\t\t%d\n", device_attr.orig_attr.max_ee);
483 		printf("\tmax_rdd:\t\t\t%d\n", device_attr.orig_attr.max_rdd);
484 		printf("\tmax_mw:\t\t\t\t%d\n", device_attr.orig_attr.max_mw);
485 		printf("\tmax_raw_ipv6_qp:\t\t%d\n", device_attr.orig_attr.max_raw_ipv6_qp);
486 		printf("\tmax_raw_ethy_qp:\t\t%d\n", device_attr.orig_attr.max_raw_ethy_qp);
487 		printf("\tmax_mcast_grp:\t\t\t%d\n", device_attr.orig_attr.max_mcast_grp);
488 		printf("\tmax_mcast_qp_attach:\t\t%d\n", device_attr.orig_attr.max_mcast_qp_attach);
489 		printf("\tmax_total_mcast_qp_attach:\t%d\n",
490 		       device_attr.orig_attr.max_total_mcast_qp_attach);
491 		printf("\tmax_ah:\t\t\t\t%d\n", device_attr.orig_attr.max_ah);
492 		printf("\tmax_fmr:\t\t\t%d\n", device_attr.orig_attr.max_fmr);
493 		if (device_attr.orig_attr.max_fmr)
494 			printf("\tmax_map_per_fmr:\t\t%d\n", device_attr.orig_attr.max_map_per_fmr);
495 		printf("\tmax_srq:\t\t\t%d\n", device_attr.orig_attr.max_srq);
496 		if (device_attr.orig_attr.max_srq) {
497 			printf("\tmax_srq_wr:\t\t\t%d\n", device_attr.orig_attr.max_srq_wr);
498 			printf("\tmax_srq_sge:\t\t\t%d\n", device_attr.orig_attr.max_srq_sge);
499 		}
500 		printf("\tmax_pkeys:\t\t\t%d\n", device_attr.orig_attr.max_pkeys);
501 		printf("\tlocal_ca_ack_delay:\t\t%d\n", device_attr.orig_attr.local_ca_ack_delay);
502 
503 		print_odp_caps(&device_attr.odp_caps);
504 		if (device_attr.completion_timestamp_mask)
505 			printf("\tcompletion timestamp_mask:\t\t\t0x%016" PRIx64 "\n",
506 			       device_attr.completion_timestamp_mask);
507 		else
508 			printf("\tcompletion_timestamp_mask not supported\n");
509 
510 		if (device_attr.hca_core_clock)
511 			printf("\thca_core_clock:\t\t\t%" PRIu64 "kHZ\n", device_attr.hca_core_clock);
512 		else
513 			printf("\tcore clock not supported\n");
514 
515 		if (device_attr.raw_packet_caps)
516 			print_raw_packet_caps(device_attr.raw_packet_caps);
517 
518 		printf("\tdevice_cap_flags_ex:\t\t0x%" PRIX64 "\n", device_attr.device_cap_flags_ex);
519 		print_device_cap_flags_ex(device_attr.device_cap_flags_ex);
520 		print_tso_caps(&device_attr.tso_caps);
521 		print_rss_caps(&device_attr.rss_caps);
522 		printf("\tmax_wq_type_rq:\t\t\t%u\n", device_attr.max_wq_type_rq);
523 		print_packet_pacing_caps(&device_attr.packet_pacing_caps);
524 	}
525 
526 	for (port = 1; port <= device_attr.orig_attr.phys_port_cnt; ++port) {
527 		/* if in the command line the user didn't ask for info about this port */
528 		if ((ib_port) && (port != ib_port))
529 			continue;
530 
531 		rc = ibv_query_port(ctx, port, &port_attr);
532 		if (rc) {
533 			fprintf(stderr, "Failed to query port %u props\n", port);
534 			goto cleanup;
535 		}
536 		printf("\t\tport:\t%d\n", port);
537 		printf("\t\t\tstate:\t\t\t%s (%d)\n",
538 		       port_state_str(port_attr.state), port_attr.state);
539 		printf("\t\t\tmax_mtu:\t\t%s (%d)\n",
540 		       mtu_str(port_attr.max_mtu), port_attr.max_mtu);
541 		printf("\t\t\tactive_mtu:\t\t%s (%d)\n",
542 		       mtu_str(port_attr.active_mtu), port_attr.active_mtu);
543 		printf("\t\t\tsm_lid:\t\t\t%d\n", port_attr.sm_lid);
544 		printf("\t\t\tport_lid:\t\t%d\n", port_attr.lid);
545 		printf("\t\t\tport_lmc:\t\t0x%02x\n", port_attr.lmc);
546 		printf("\t\t\tlink_layer:\t\t%s\n",
547 					link_layer_str(port_attr.link_layer));
548 
549 		if (verbose) {
550 			printf("\t\t\tmax_msg_sz:\t\t0x%x\n", port_attr.max_msg_sz);
551 			printf("\t\t\tport_cap_flags:\t\t0x%08x\n", port_attr.port_cap_flags);
552 			printf("\t\t\tmax_vl_num:\t\t%s (%d)\n",
553 			       vl_str(port_attr.max_vl_num), port_attr.max_vl_num);
554 			printf("\t\t\tbad_pkey_cntr:\t\t0x%x\n", port_attr.bad_pkey_cntr);
555 			printf("\t\t\tqkey_viol_cntr:\t\t0x%x\n", port_attr.qkey_viol_cntr);
556 			printf("\t\t\tsm_sl:\t\t\t%d\n", port_attr.sm_sl);
557 			printf("\t\t\tpkey_tbl_len:\t\t%d\n", port_attr.pkey_tbl_len);
558 			printf("\t\t\tgid_tbl_len:\t\t%d\n", port_attr.gid_tbl_len);
559 			printf("\t\t\tsubnet_timeout:\t\t%d\n", port_attr.subnet_timeout);
560 			printf("\t\t\tinit_type_reply:\t%d\n", port_attr.init_type_reply);
561 			printf("\t\t\tactive_width:\t\t%sX (%d)\n",
562 			       width_str(port_attr.active_width), port_attr.active_width);
563 			printf("\t\t\tactive_speed:\t\t%s (%d)\n",
564 			       speed_str(port_attr.active_speed), port_attr.active_speed);
565 			if (ib_dev->transport_type == IBV_TRANSPORT_IB)
566 				printf("\t\t\tphys_state:\t\t%s (%d)\n",
567 				       port_phy_state_str(port_attr.phys_state), port_attr.phys_state);
568 
569 			if (print_all_port_gids(ctx, port, port_attr.gid_tbl_len))
570 				goto cleanup;
571 		}
572 		printf("\n");
573 	}
574 cleanup:
575 	if (ctx)
576 		if (ibv_close_device(ctx)) {
577 			fprintf(stderr, "Failed to close device");
578 			rc = 3;
579 		}
580 	return rc;
581 }
582 
583 static void usage(const char *argv0)
584 {
585 	printf("Usage: %s             print the ca attributes\n", argv0);
586 	printf("\n");
587 	printf("Options:\n");
588 	printf("  -d, --ib-dev=<dev>     use IB device <dev> (default first device found)\n");
589 	printf("  -i, --ib-port=<port>   use port <port> of IB device (default all ports)\n");
590 	printf("  -l, --list             print only the IB devices names\n");
591 	printf("  -v, --verbose          print all the attributes of the IB device(s)\n");
592 }
593 
594 int main(int argc, char *argv[])
595 {
596 	char *ib_devname = NULL;
597 	int ret = 0;
598 	struct ibv_device **dev_list, **orig_dev_list;
599 	int num_of_hcas;
600 	int ib_port = 0;
601 
602 	/* parse command line options */
603 	while (1) {
604 		int c;
605 		static struct option long_options[] = {
606 			{ .name = "ib-dev",   .has_arg = 1, .val = 'd' },
607 			{ .name = "ib-port",  .has_arg = 1, .val = 'i' },
608 			{ .name = "list",     .has_arg = 0, .val = 'l' },
609 			{ .name = "verbose",  .has_arg = 0, .val = 'v' },
610 			{ }
611 		};
612 
613 		c = getopt_long(argc, argv, "d:i:lv", long_options, NULL);
614 		if (c == -1)
615 			break;
616 
617 		switch (c) {
618 		case 'd':
619 			ib_devname = strdup(optarg);
620 			break;
621 
622 		case 'i':
623 			ib_port = strtol(optarg, NULL, 0);
624 			if (ib_port <= 0) {
625 				usage(argv[0]);
626 				return 1;
627 			}
628 			break;
629 
630 		case 'v':
631 			verbose = 1;
632 			break;
633 
634 		case 'l':
635 			dev_list = orig_dev_list = ibv_get_device_list(&num_of_hcas);
636 			if (!dev_list) {
637 				perror("Failed to get IB devices list");
638 				return -1;
639 			}
640 
641 			printf("%d HCA%s found:\n", num_of_hcas,
642 			       num_of_hcas != 1 ? "s" : "");
643 
644 			while (*dev_list) {
645 				printf("\t%s\n", ibv_get_device_name(*dev_list));
646 				++dev_list;
647 			}
648 
649 			printf("\n");
650 
651 			ibv_free_device_list(orig_dev_list);
652 
653 			return 0;
654 
655 		default:
656 			usage(argv[0]);
657 			return -1;
658 		}
659 	}
660 
661 	dev_list = orig_dev_list = ibv_get_device_list(NULL);
662 	if (!dev_list) {
663 		perror("Failed to get IB devices list");
664 		return -1;
665 	}
666 
667 	if (ib_devname) {
668 		while (*dev_list) {
669 			if (!strcmp(ibv_get_device_name(*dev_list), ib_devname))
670 				break;
671 			++dev_list;
672 		}
673 
674 		if (!*dev_list) {
675 			fprintf(stderr, "IB device '%s' wasn't found\n", ib_devname);
676 			return -1;
677 		}
678 
679 		ret |= print_hca_cap(*dev_list, ib_port);
680 	} else {
681 		if (!*dev_list) {
682 			fprintf(stderr, "No IB devices found\n");
683 			return -1;
684 		}
685 
686 		while (*dev_list) {
687 			ret |= print_hca_cap(*dev_list, ib_port);
688 			++dev_list;
689 		}
690 	}
691 
692 	if (ib_devname)
693 		free(ib_devname);
694 
695 	ibv_free_device_list(orig_dev_list);
696 
697 	return ret;
698 }
699