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