]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ofed/libibverbs/examples/devinfo.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ofed / libibverbs / examples / devinfo.c
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
45 #include <infiniband/verbs.h>
46 #include <infiniband/driver.h>
47 #include <infiniband/arch.h>
48
49 static int verbose;
50
51 static int null_gid(union ibv_gid *gid)
52 {
53         return !(gid->raw[8] | gid->raw[9] | gid->raw[10] | gid->raw[11] |
54                  gid->raw[12] | gid->raw[13] | gid->raw[14] | gid->raw[15]);
55 }
56
57 static const char *guid_str(uint64_t node_guid, char *str)
58 {
59         node_guid = ntohll(node_guid);
60         sprintf(str, "%04x:%04x:%04x:%04x",
61                 (unsigned) (node_guid >> 48) & 0xffff,
62                 (unsigned) (node_guid >> 32) & 0xffff,
63                 (unsigned) (node_guid >> 16) & 0xffff,
64                 (unsigned) (node_guid >>  0) & 0xffff);
65         return str;
66 }
67
68 static const char *transport_str(enum ibv_transport_type transport)
69 {
70         switch (transport) {
71         case IBV_TRANSPORT_IB:    return "InfiniBand";
72         case IBV_TRANSPORT_IWARP: return "iWARP";
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         default: return "invalid width";
132         }
133 }
134
135 static 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
145 static 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
157 static 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
185 static 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
198 static 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         }
324 cleanup:
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
333 static 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
344 int 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 }