|
| 1 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | +/** |
| 3 | + * This file is part of libnvme. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * mi-mctp-ae: open a MI connection over MCTP, supporting asynchronous event messages |
| 8 | + */ |
| 9 | + |
| 10 | +#include <assert.h> |
| 11 | +#include <ctype.h> |
| 12 | +#include <err.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <stddef.h> |
| 16 | +#include <string.h> |
| 17 | +#include <errno.h> |
| 18 | +#include <unistd.h> // for usleep |
| 19 | + |
| 20 | +#include <libnvme-mi.h> |
| 21 | +#include <poll.h> |
| 22 | + |
| 23 | +#include <ccan/array_size/array_size.h> |
| 24 | +#include <ccan/endian/endian.h> |
| 25 | +#include <sys/select.h> |
| 26 | + |
| 27 | +// Function to print the byte array |
| 28 | +static void print_byte_array(void *data, size_t len) |
| 29 | +{ |
| 30 | + uint8_t *byte_data = (uint8_t *)data; |
| 31 | + |
| 32 | + for (size_t i = 0; i < len; ++i) |
| 33 | + printf("%02X ", byte_data[i]); |
| 34 | + printf("\n"); |
| 35 | +} |
| 36 | + |
| 37 | +static void print_event_info(struct nvme_mi_event *event) |
| 38 | +{ |
| 39 | + printf("aeoi: %02X\n", event->aeoi); |
| 40 | + printf("aeocidi: %04X\n", event->aeocidi); |
| 41 | + printf("aessi: %02X\n", event->aessi); |
| 42 | + |
| 43 | + printf("specific_info: "); |
| 44 | + if (event->spec_info_len && event->spec_info) |
| 45 | + print_byte_array(event->spec_info, event->spec_info_len); |
| 46 | + else |
| 47 | + printf("EMPTY\n"); |
| 48 | + |
| 49 | + printf("vendor_specific_info: "); |
| 50 | + if (event->vend_spec_info_len && event->vend_spec_info) |
| 51 | + print_byte_array(event->vend_spec_info, event->vend_spec_info_len); |
| 52 | + else |
| 53 | + printf("EMPTY\n"); |
| 54 | +} |
| 55 | + |
| 56 | +enum nvme_mi_aem_handler_next_action aem_handler(nvme_mi_ep_t ep, size_t num_events, void *userdata) |
| 57 | +{ |
| 58 | + uint32_t *count = (uint32_t *) userdata; |
| 59 | + *count = *count+1; |
| 60 | + |
| 61 | + printf("Received notification #%d with %zu events:\n", *count, num_events); |
| 62 | + for (int i = 0; i < num_events; i++) { |
| 63 | + struct nvme_mi_event *event = nvme_mi_aem_get_next_event(ep); |
| 64 | + |
| 65 | + if (event == NULL) |
| 66 | + printf("Unexpected NULL event\n"); |
| 67 | + else { |
| 68 | + printf("Event:\n"); |
| 69 | + print_event_info(event); |
| 70 | + printf("\n"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return NVME_MI_AEM_HNA_ACK; |
| 75 | +} |
| 76 | + |
| 77 | +int main(int argc, char **argv) |
| 78 | +{ |
| 79 | + nvme_root_t root; |
| 80 | + nvme_mi_ep_t ep; |
| 81 | + bool usage = true; |
| 82 | + uint8_t eid = 0; |
| 83 | + int rc = 0, net = 0; |
| 84 | + struct nvme_mi_aem_callbacks aem_cb_info = {0}; |
| 85 | + uint32_t notification_counter = 0; |
| 86 | + |
| 87 | + const uint8_t AEM_FD_INDEX = 0; |
| 88 | + const uint8_t STD_IN_FD_INDEX = 1; |
| 89 | + |
| 90 | + if (argc > 3) { |
| 91 | + usage = false; |
| 92 | + net = atoi(argv[1]); |
| 93 | + eid = atoi(argv[2]) & 0xff; |
| 94 | + argv += 2; |
| 95 | + argc -= 2; |
| 96 | + |
| 97 | + int event_count = argc - 1; |
| 98 | + |
| 99 | + for (int i = 0; i < event_count; i++) { |
| 100 | + int event = atoi(argv[1+i]); |
| 101 | + |
| 102 | + aem_cb_info.enabled[event] = true; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (usage) { |
| 107 | + fprintf(stderr, |
| 108 | + "usage: %s <net> <eid> [AE #s separated by spaces]\n", |
| 109 | + argv[0]); |
| 110 | + return EXIT_FAILURE; |
| 111 | + } |
| 112 | + |
| 113 | + root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL); |
| 114 | + if (!root) |
| 115 | + err(EXIT_FAILURE, "can't create NVMe root"); |
| 116 | + |
| 117 | + ep = nvme_mi_open_mctp(root, net, eid); |
| 118 | + if (!ep) |
| 119 | + errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid); |
| 120 | + |
| 121 | + aem_cb_info.aem_handler = aem_handler; |
| 122 | + |
| 123 | + rc = nvme_mi_enable_aem(ep, true, true, true, 1, 4, &aem_cb_info, ¬ification_counter); |
| 124 | + if (rc) |
| 125 | + errx(EXIT_FAILURE, "Can't enable aem:%d (%d)", rc, errno); |
| 126 | + |
| 127 | + struct pollfd fds[2]; |
| 128 | + |
| 129 | + fds[AEM_FD_INDEX].fd = nvme_mi_get_aem_fd(ep); |
| 130 | + if (fds[AEM_FD_INDEX].fd < 0) |
| 131 | + errx(EXIT_FAILURE, "Can't get aem fd\n"); |
| 132 | + |
| 133 | + fds[STD_IN_FD_INDEX].fd = STDIN_FILENO; |
| 134 | + |
| 135 | + fds[AEM_FD_INDEX].events = POLLIN; |
| 136 | + fds[STD_IN_FD_INDEX].events = POLLIN; |
| 137 | + |
| 138 | + printf("Press any key to exit\n"); |
| 139 | + while (1) { |
| 140 | + int poll_timeout = 500; // Timeout in milliseconds |
| 141 | + |
| 142 | + rc = poll(fds, 2, poll_timeout); |
| 143 | + |
| 144 | + if (rc == -1) { |
| 145 | + perror("poll"); |
| 146 | + break; |
| 147 | + } else if (rc == 0) { |
| 148 | + //printf("No data within %d milliseconds.\n", timeout); |
| 149 | + } else { |
| 150 | + //Time to do the work |
| 151 | + if (fds[AEM_FD_INDEX].revents & POLLIN) { |
| 152 | + rc = nvme_mi_aem_process(ep, ¬ification_counter); |
| 153 | + if (rc) |
| 154 | + errx(EXIT_FAILURE, |
| 155 | + "nvme_mi_aem_process failed with:%d (%d)", |
| 156 | + rc, |
| 157 | + errno); |
| 158 | + } |
| 159 | + if (fds[STD_IN_FD_INDEX].revents & POLLIN) |
| 160 | + break;//we are done |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + //Cleanup |
| 165 | + nvme_mi_disable_aem(ep); |
| 166 | + nvme_mi_close(ep); |
| 167 | + nvme_mi_free_root(root); |
| 168 | + |
| 169 | + return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 170 | +} |
| 171 | + |
| 172 | + |
0 commit comments