1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Topspin Communications.  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#if HAVE_CONFIG_H
34219820Sjeff#  include <config.h>
35219820Sjeff#endif /* HAVE_CONFIG_H */
36219820Sjeff
37219820Sjeff#include <stdio.h>
38219820Sjeff
39219820Sjeff#include <infiniband/verbs.h>
40219820Sjeff
41219820Sjeffstatic const char *event_name_str(enum ibv_event_type event_type)
42219820Sjeff{
43219820Sjeff	switch (event_type) {
44219820Sjeff	case IBV_EVENT_DEVICE_FATAL:
45219820Sjeff		return "IBV_EVENT_DEVICE_FATAL";
46219820Sjeff	case IBV_EVENT_PORT_ACTIVE:
47219820Sjeff		return "IBV_EVENT_PORT_ACTIVE";
48219820Sjeff	case IBV_EVENT_PORT_ERR:
49219820Sjeff		return "IBV_EVENT_PORT_ERR";
50219820Sjeff	case IBV_EVENT_LID_CHANGE:
51219820Sjeff		return "IBV_EVENT_LID_CHANGE";
52219820Sjeff	case IBV_EVENT_PKEY_CHANGE:
53219820Sjeff		return "IBV_EVENT_PKEY_CHANGE";
54219820Sjeff	case IBV_EVENT_SM_CHANGE:
55219820Sjeff		return "IBV_EVENT_SM_CHANGE";
56219820Sjeff	case IBV_EVENT_CLIENT_REREGISTER:
57219820Sjeff		return "IBV_EVENT_CLIENT_REREGISTER";
58219820Sjeff	case IBV_EVENT_GID_CHANGE:
59219820Sjeff		return "IBV_EVENT_GID_CHANGE";
60219820Sjeff
61219820Sjeff	case IBV_EVENT_CQ_ERR:
62219820Sjeff	case IBV_EVENT_QP_FATAL:
63219820Sjeff	case IBV_EVENT_QP_REQ_ERR:
64219820Sjeff	case IBV_EVENT_QP_ACCESS_ERR:
65219820Sjeff	case IBV_EVENT_COMM_EST:
66219820Sjeff	case IBV_EVENT_SQ_DRAINED:
67219820Sjeff	case IBV_EVENT_PATH_MIG:
68219820Sjeff	case IBV_EVENT_PATH_MIG_ERR:
69219820Sjeff	case IBV_EVENT_SRQ_ERR:
70219820Sjeff	case IBV_EVENT_SRQ_LIMIT_REACHED:
71219820Sjeff	case IBV_EVENT_QP_LAST_WQE_REACHED:
72219820Sjeff	default:
73219820Sjeff		return "unexpected";
74219820Sjeff	}
75219820Sjeff}
76219820Sjeff
77219820Sjeffint main(int argc, char *argv[])
78219820Sjeff{
79219820Sjeff	struct ibv_device **dev_list;
80219820Sjeff	struct ibv_context *context;
81219820Sjeff	struct ibv_async_event event;
82219820Sjeff
83219820Sjeff	/* Force line-buffering in case stdout is redirected */
84219820Sjeff	setvbuf(stdout, NULL, _IOLBF, 0);
85219820Sjeff
86219820Sjeff	dev_list = ibv_get_device_list(NULL);
87219820Sjeff	if (!dev_list) {
88219820Sjeff		perror("Failed to get IB devices list");
89219820Sjeff		return 1;
90219820Sjeff	}
91219820Sjeff
92219820Sjeff	if (!*dev_list) {
93219820Sjeff		fprintf(stderr, "No IB devices found\n");
94219820Sjeff		return 1;
95219820Sjeff	}
96219820Sjeff
97219820Sjeff	context = ibv_open_device(*dev_list);
98219820Sjeff	if (!context) {
99219820Sjeff		fprintf(stderr, "Couldn't get context for %s\n",
100219820Sjeff			ibv_get_device_name(*dev_list));
101219820Sjeff		return 1;
102219820Sjeff	}
103219820Sjeff
104219820Sjeff	printf("%s: async event FD %d\n",
105219820Sjeff	       ibv_get_device_name(*dev_list), context->async_fd);
106219820Sjeff
107219820Sjeff	while (1) {
108219820Sjeff		if (ibv_get_async_event(context, &event))
109219820Sjeff			return 1;
110219820Sjeff
111219820Sjeff		printf("  event_type %s (%d), port %d\n",
112219820Sjeff		       event_name_str(event.event_type),
113219820Sjeff		       event.event_type, event.element.port_num);
114219820Sjeff
115219820Sjeff		ibv_ack_async_event(&event);
116219820Sjeff	}
117219820Sjeff
118219820Sjeff	return 0;
119219820Sjeff}
120