|
| 1 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | +/** |
| 3 | + * This file is part of libnvme. |
| 4 | + * Copyright (c) 2021 Code Construct Pty Ltd. |
| 5 | + * |
| 6 | + * Authors: Jeremy Kerr <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * mi-mctp: open a MI connection over MCTP, and query controller info |
| 11 | + */ |
| 12 | + |
| 13 | +#include <err.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +#include <libnvme-mi.h> |
| 18 | + |
| 19 | +#include <ccan/array_size/array_size.h> |
| 20 | +#include <ccan/endian/endian.h> |
| 21 | + |
| 22 | +static void show_port_pcie(struct nvme_mi_read_port_info *port) |
| 23 | +{ |
| 24 | + printf(" PCIe max payload: 0x%x\n", 0x80 << port->pcie.mps); |
| 25 | + printf(" PCIe link speeds: 0x%02x\n", port->pcie.sls); |
| 26 | + printf(" PCIe current speed: 0x%02x\n", port->pcie.cls); |
| 27 | + printf(" PCIe max link width: 0x%02x\n", port->pcie.mlw); |
| 28 | + printf(" PCIe neg link width: 0x%02x\n", port->pcie.nlw); |
| 29 | + printf(" PCIe port: 0x%02x\n", port->pcie.pn); |
| 30 | +} |
| 31 | + |
| 32 | +static void show_port_smbus(struct nvme_mi_read_port_info *port) |
| 33 | +{ |
| 34 | + printf(" SMBus address: 0x%02x\n", port->smb.vpd_addr); |
| 35 | + printf(" VPD access freq: 0x%02x\n", port->smb.mvpd_freq); |
| 36 | + printf(" MCTP address: 0x%02x\n", port->smb.mme_addr); |
| 37 | + printf(" MCTP access freq: 0x%02x\n", port->smb.mme_freq); |
| 38 | + printf(" NVMe basic management: %s\n", |
| 39 | + (port->smb.nvmebm & 0x1) ? "enabled" : "disabled"); |
| 40 | +} |
| 41 | + |
| 42 | +static struct { |
| 43 | + int typeid; |
| 44 | + const char *name; |
| 45 | + void (*fn)(struct nvme_mi_read_port_info *); |
| 46 | +} port_types[] = { |
| 47 | + { 0x00, "inactive", NULL }, |
| 48 | + { 0x01, "PCIe", show_port_pcie }, |
| 49 | + { 0x02, "SMBus", show_port_smbus }, |
| 50 | +}; |
| 51 | + |
| 52 | +static int show_port(nvme_mi_ep_t ep, int portid) |
| 53 | +{ |
| 54 | + void (*show_fn)(struct nvme_mi_read_port_info *); |
| 55 | + struct nvme_mi_read_port_info port; |
| 56 | + const char *typestr; |
| 57 | + int rc; |
| 58 | + |
| 59 | + rc = nvme_mi_mi_read_mi_data_port(ep, portid, &port); |
| 60 | + if (rc) |
| 61 | + return rc; |
| 62 | + |
| 63 | + if (port.portt < ARRAY_SIZE(port_types)) { |
| 64 | + show_fn = port_types[port.portt].fn; |
| 65 | + typestr = port_types[port.portt].name; |
| 66 | + } else { |
| 67 | + show_fn = NULL; |
| 68 | + typestr = "INVALID"; |
| 69 | + } |
| 70 | + |
| 71 | + printf(" port %d\n", portid); |
| 72 | + printf(" type %s[%d]\n", typestr, port.portt); |
| 73 | + printf(" MCTP MTU: %d\n", port.mmctptus); |
| 74 | + printf(" MEB size: %d\n", port.meb); |
| 75 | + |
| 76 | + if (show_fn) |
| 77 | + show_fn(&port); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +int main(int argc, char **argv) |
| 83 | +{ |
| 84 | + struct nvme_mi_nvm_ss_health_status ss_health; |
| 85 | + struct nvme_mi_read_nvm_ss_info ss_info; |
| 86 | + nvme_root_t root; |
| 87 | + nvme_mi_ep_t ep; |
| 88 | + uint8_t eid; |
| 89 | + int net; |
| 90 | + int rc; |
| 91 | + int i; |
| 92 | + |
| 93 | + if (argc != 3) { |
| 94 | + fprintf(stderr, "usage: %s <net> <eid>\n", argv[0]); |
| 95 | + return EXIT_FAILURE; |
| 96 | + } |
| 97 | + |
| 98 | + net = atoi(argv[1]); |
| 99 | + eid = atoi(argv[2]) & 0xff; |
| 100 | + |
| 101 | + root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL); |
| 102 | + if (!root) |
| 103 | + err(EXIT_FAILURE, "can't create NVMe root"); |
| 104 | + |
| 105 | + ep = nvme_mi_open_mctp(root, net, eid); |
| 106 | + if (!ep) |
| 107 | + err(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid); |
| 108 | + |
| 109 | + rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info); |
| 110 | + if (rc) |
| 111 | + err(EXIT_FAILURE, "can't perform Read MI Data operation"); |
| 112 | + |
| 113 | + printf("NVMe MI subsys info:\n"); |
| 114 | + printf(" num ports: %d\n", ss_info.nump + 1); |
| 115 | + printf(" major ver: %d\n", ss_info.mjr); |
| 116 | + printf(" minor ver: %d\n", ss_info.mnr); |
| 117 | + |
| 118 | + printf("NVMe MI port info:\n"); |
| 119 | + for (i = 0; i <= ss_info.nump; i++) |
| 120 | + show_port(ep, i); |
| 121 | + |
| 122 | + rc = nvme_mi_mi_subsystem_health_status_poll(ep, true, &ss_health); |
| 123 | + if (rc) |
| 124 | + err(EXIT_FAILURE, "can't perform Health Status Poll operation"); |
| 125 | + |
| 126 | + printf("NVMe MI subsys health:\n"); |
| 127 | + printf(" subsystem status: 0x%x\n", ss_health.nss); |
| 128 | + printf(" smart warnings: 0x%x\n", ss_health.sw); |
| 129 | + printf(" composite temp: %d\n", ss_health.ctemp); |
| 130 | + printf(" drive life used: %d%%\n", ss_health.pdlu); |
| 131 | + printf(" controller status: 0x%04x\n", le16_to_cpu(ss_health.pdlu)); |
| 132 | + |
| 133 | + nvme_mi_close(ep); |
| 134 | + |
| 135 | + nvme_mi_free_root(root); |
| 136 | + |
| 137 | + return EXIT_SUCCESS; |
| 138 | +} |
| 139 | + |
| 140 | + |
0 commit comments