| 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 <assert.h> |
| 14 |
#include <ctype.h> |
| 15 |
#include <err.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include <libnvme-mi.h> |
| 22 |
|
| 23 |
#include <ccan/array_size/array_size.h> |
| 24 |
#include <ccan/endian/endian.h> |
| 25 |
|
| 26 |
static void show_port_pcie(struct nvme_mi_read_port_info *port) |
| 27 |
{ |
| 28 |
printf(" PCIe max payload: 0x%x\n", 0x80 << port->pcie.mps); |
| 29 |
printf(" PCIe link speeds: 0x%02x\n", port->pcie.sls); |
| 30 |
printf(" PCIe current speed: 0x%02x\n", port->pcie.cls); |
| 31 |
printf(" PCIe max link width: 0x%02x\n", port->pcie.mlw); |
| 32 |
printf(" PCIe neg link width: 0x%02x\n", port->pcie.nlw); |
| 33 |
printf(" PCIe port: 0x%02x\n", port->pcie.pn); |
| 34 |
} |
| 35 |
|
| 36 |
static void show_port_smbus(struct nvme_mi_read_port_info *port) |
| 37 |
{ |
| 38 |
printf(" SMBus address: 0x%02x\n", port->smb.vpd_addr); |
| 39 |
printf(" VPD access freq: 0x%02x\n", port->smb.mvpd_freq); |
| 40 |
printf(" MCTP address: 0x%02x\n", port->smb.mme_addr); |
| 41 |
printf(" MCTP access freq: 0x%02x\n", port->smb.mme_freq); |
| 42 |
printf(" NVMe basic management: %s\n", |
| 43 |
(port->smb.nvmebm & 0x1) ? "enabled" : "disabled"); |
| 44 |
} |
| 45 |
|
| 46 |
static struct { |
| 47 |
int typeid; |
| 48 |
const char *name; |
| 49 |
void (*fn)(struct nvme_mi_read_port_info *); |
| 50 |
} port_types[] = { |
| 51 |
{ 0x00, "inactive", NULL }, |
| 52 |
{ 0x01, "PCIe", show_port_pcie }, |
| 53 |
{ 0x02, "SMBus", show_port_smbus }, |
| 54 |
}; |
| 55 |
|
| 56 |
static int show_port(nvme_mi_ep_t ep, int portid) |
| 57 |
{ |
| 58 |
void (*show_fn)(struct nvme_mi_read_port_info *); |
| 59 |
struct nvme_mi_read_port_info port; |
| 60 |
const char *typestr; |
| 61 |
int rc; |
| 62 |
|
| 63 |
rc = nvme_mi_mi_read_mi_data_port(ep, portid, &port); |
| 64 |
if (rc) |
| 65 |
return rc; |
| 66 |
|
| 67 |
if (port.portt < ARRAY_SIZE(port_types)) { |
| 68 |
show_fn = port_types[port.portt].fn; |
| 69 |
typestr = port_types[port.portt].name; |
| 70 |
} else { |
| 71 |
show_fn = NULL; |
| 72 |
typestr = "INVALID"; |
| 73 |
} |
| 74 |
|
| 75 |
printf(" port %d\n", portid); |
| 76 |
printf(" type %s[%d]\n", typestr, port.portt); |
| 77 |
printf(" MCTP MTU: %d\n", port.mmctptus); |
| 78 |
printf(" MEB size: %d\n", port.meb); |
| 79 |
|
| 80 |
if (show_fn) |
| 81 |
show_fn(&port); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
int do_info(nvme_mi_ep_t ep) |
| 87 |
{ |
| 88 |
struct nvme_mi_nvm_ss_health_status ss_health; |
| 89 |
struct nvme_mi_read_nvm_ss_info ss_info; |
| 90 |
int i, rc; |
| 91 |
|
| 92 |
rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info); |
| 93 |
if (rc) { |
| 94 |
warn("can't perform Read MI Data operation"); |
| 95 |
return -1; |
| 96 |
} |
| 97 |
|
| 98 |
printf("NVMe MI subsys info:\n"); |
| 99 |
printf(" num ports: %d\n", ss_info.nump + 1); |
| 100 |
printf(" major ver: %d\n", ss_info.mjr); |
| 101 |
printf(" minor ver: %d\n", ss_info.mnr); |
| 102 |
|
| 103 |
printf("NVMe MI port info:\n"); |
| 104 |
for (i = 0; i <= ss_info.nump; i++) |
| 105 |
show_port(ep, i); |
| 106 |
|
| 107 |
rc = nvme_mi_mi_subsystem_health_status_poll(ep, true, &ss_health); |
| 108 |
if (rc) |
| 109 |
err(EXIT_FAILURE, "can't perform Health Status Poll operation"); |
| 110 |
|
| 111 |
printf("NVMe MI subsys health:\n"); |
| 112 |
printf(" subsystem status: 0x%x\n", ss_health.nss); |
| 113 |
printf(" smart warnings: 0x%x\n", ss_health.sw); |
| 114 |
printf(" composite temp: %d\n", ss_health.ctemp); |
| 115 |
printf(" drive life used: %d%%\n", ss_health.pdlu); |
| 116 |
printf(" controller status: 0x%04x\n", le16_to_cpu(ss_health.ccs)); |
| 117 |
|
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static int show_ctrl(nvme_mi_ep_t ep, uint16_t ctrl_id) |
| 122 |
{ |
| 123 |
struct nvme_mi_read_ctrl_info ctrl; |
| 124 |
int rc; |
| 125 |
|
| 126 |
rc = nvme_mi_mi_read_mi_data_ctrl(ep, ctrl_id, &ctrl); |
| 127 |
if (rc) |
| 128 |
return rc; |
| 129 |
|
| 130 |
printf(" Controller id: %d\n", ctrl_id); |
| 131 |
printf(" port id: %d\n", ctrl.portid); |
| 132 |
if (ctrl.prii & 0x1) { |
| 133 |
uint16_t bdfn = le16_to_cpu(ctrl.pri); |
| 134 |
printf(" PCIe routing valid\n"); |
| 135 |
printf(" PCIe bus: 0x%02x\n", bdfn >> 8); |
| 136 |
printf(" PCIe dev: 0x%02x\n", bdfn >> 3 & 0x1f); |
| 137 |
printf(" PCIe fn : 0x%02x\n", bdfn & 0x7); |
| 138 |
} else { |
| 139 |
printf(" PCIe routing invalid\n"); |
| 140 |
} |
| 141 |
printf(" PCI vendor: %04x\n", le16_to_cpu(ctrl.vid)); |
| 142 |
printf(" PCI device: %04x\n", le16_to_cpu(ctrl.did)); |
| 143 |
printf(" PCI subsys vendor: %04x\n", le16_to_cpu(ctrl.ssvid)); |
| 144 |
printf(" PCI subsys device: %04x\n", le16_to_cpu(ctrl.ssvid)); |
| 145 |
|
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
static int do_controllers(nvme_mi_ep_t ep) |
| 150 |
{ |
| 151 |
struct nvme_ctrl_list ctrl_list; |
| 152 |
int rc, i; |
| 153 |
|
| 154 |
rc = nvme_mi_mi_read_mi_data_ctrl_list(ep, 0, &ctrl_list); |
| 155 |
if (rc) { |
| 156 |
warnx("Can't perform Controller List operation"); |
| 157 |
return rc; |
| 158 |
} |
| 159 |
|
| 160 |
printf("NVMe controller list:\n"); |
| 161 |
for (i = 0; i < le16_to_cpu(ctrl_list.num); i++) { |
| 162 |
uint16_t id = le16_to_cpu(ctrl_list.identifier[i]); |
| 163 |
show_ctrl(ep, id); |
| 164 |
} |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
static const char *__copy_id_str(const void *field, size_t size, |
| 169 |
char *buf, size_t buf_size) |
| 170 |
{ |
| 171 |
assert(size < buf_size); |
| 172 |
strncpy(buf, field, size); |
| 173 |
buf[size] = '\0'; |
| 174 |
return buf; |
| 175 |
} |
| 176 |
|
| 177 |
#define copy_id_str(f,b) __copy_id_str(f, sizeof(f), b, sizeof(b)) |
| 178 |
|
| 179 |
int do_identify(nvme_mi_ep_t ep, int argc, char **argv) |
| 180 |
{ |
| 181 |
struct nvme_identify_args id_args = { 0 }; |
| 182 |
struct nvme_mi_ctrl *ctrl; |
| 183 |
struct nvme_id_ctrl id; |
| 184 |
uint16_t ctrl_id; |
| 185 |
char buf[41]; |
| 186 |
bool partial; |
| 187 |
int rc, tmp; |
| 188 |
|
| 189 |
if (argc < 2) { |
| 190 |
fprintf(stderr, "no controller ID specified\n"); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
tmp = atoi(argv[1]); |
| 195 |
if (tmp < 0 || tmp > 0xffff) { |
| 196 |
fprintf(stderr, "invalid controller ID\n"); |
| 197 |
return -1; |
| 198 |
} |
| 199 |
|
| 200 |
ctrl_id = tmp & 0xffff; |
| 201 |
|
| 202 |
partial = argc > 2 && !strcmp(argv[2], "--partial"); |
| 203 |
|
| 204 |
ctrl = nvme_mi_init_ctrl(ep, ctrl_id); |
| 205 |
if (!ctrl) { |
| 206 |
warn("can't create controller"); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
id_args.data = &id; |
| 211 |
id_args.args_size = sizeof(id_args); |
| 212 |
id_args.cns = NVME_IDENTIFY_CNS_CTRL; |
| 213 |
id_args.nsid = NVME_NSID_NONE; |
| 214 |
id_args.cntid = 0; |
| 215 |
id_args.csi = NVME_CSI_NVM; |
| 216 |
|
| 217 |
/* for this example code, we can either do a full or partial identify; |
| 218 |
* since we're only printing the fields before the 'rab' member, |
| 219 |
* these will be equivalent, aside from the size of the MI |
| 220 |
* response. |
| 221 |
*/ |
| 222 |
if (partial) { |
| 223 |
rc = nvme_mi_admin_identify_partial(ctrl, &id_args, 0, |
| 224 |
offsetof(struct nvme_id_ctrl, rab)); |
| 225 |
} else { |
| 226 |
rc = nvme_mi_admin_identify(ctrl, &id_args); |
| 227 |
} |
| 228 |
|
| 229 |
if (rc) { |
| 230 |
warn("can't perform Admin Identify command"); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
printf("NVMe Controller %d identify\n", ctrl_id); |
| 235 |
printf(" PCI vendor: %04x\n", le16_to_cpu(id.vid)); |
| 236 |
printf(" PCI subsys vendor: %04x\n", le16_to_cpu(id.ssvid)); |
| 237 |
printf(" Serial number: %s\n", copy_id_str(id.sn, buf)); |
| 238 |
printf(" Model number: %s\n", copy_id_str(id.mn, buf)); |
| 239 |
printf(" Firmware rev: %s\n", copy_id_str(id.fr, buf)); |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
int do_control_primitive(nvme_mi_ep_t ep, int argc, char **argv) |
| 245 |
{ |
| 246 |
int rc = 0; |
| 247 |
char *action = NULL; |
| 248 |
uint8_t opcode = 0xff; |
| 249 |
uint16_t cpsr; |
| 250 |
|
| 251 |
static const struct { |
| 252 |
const char *name; |
| 253 |
uint8_t opcode; |
| 254 |
} control_actions[] = { |
| 255 |
{ "pause", nvme_mi_control_opcode_pause }, |
| 256 |
{ "resume", nvme_mi_control_opcode_resume }, |
| 257 |
{ "abort", nvme_mi_control_opcode_abort }, |
| 258 |
{ "get-state", nvme_mi_control_opcode_get_state }, |
| 259 |
{ "replay", nvme_mi_control_opcode_replay }, |
| 260 |
}; |
| 261 |
|
| 262 |
static const char * const slot_state[] = { |
| 263 |
"Idle", |
| 264 |
"Receive", |
| 265 |
"Process", |
| 266 |
"Transmit", |
| 267 |
}; |
| 268 |
|
| 269 |
static const char * const cpas_state[] = { |
| 270 |
"Command aborted after processing completed or no command to abort", |
| 271 |
"Command aborted before processing began", |
| 272 |
"Command processing partially completed", |
| 273 |
"Reserved", |
| 274 |
}; |
| 275 |
|
| 276 |
if (argc < 2) { |
| 277 |
fprintf(stderr, "no opcode specified\n"); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
|
| 281 |
action = argv[1]; |
| 282 |
|
| 283 |
for (int i = 0; i < ARRAY_SIZE(control_actions); i++) { |
| 284 |
if (!strcmp(action, control_actions[i].name)) { |
| 285 |
opcode = control_actions[i].opcode; |
| 286 |
break; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (opcode == 0xff) { |
| 291 |
fprintf(stderr, "invalid action specified: %s\n", action); |
| 292 |
return -1; |
| 293 |
} |
| 294 |
|
| 295 |
rc = nvme_mi_control(ep, opcode, 0, &cpsr); /* cpsp reserved in example */ |
| 296 |
if (rc) { |
| 297 |
warn("can't perform primitive control command"); |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
printf("NVMe control primitive\n"); |
| 302 |
switch (opcode) { |
| 303 |
case nvme_mi_control_opcode_pause: |
| 304 |
printf(" Pause : cspr is %#x\n", cpsr); |
| 305 |
printf(" Pause Flag Status Slot 0: %s\n", (cpsr & (1 << 0)) ? "Yes" : "No"); |
| 306 |
printf(" Pause Flag Status Slot 1: %s\n", (cpsr & (1 << 1)) ? "Yes" : "No"); |
| 307 |
break; |
| 308 |
case nvme_mi_control_opcode_resume: |
| 309 |
printf(" Resume : cspr is %#x\n", cpsr); |
| 310 |
break; |
| 311 |
case nvme_mi_control_opcode_abort: |
| 312 |
printf(" Abort : cspr is %#x\n", cpsr); |
| 313 |
printf(" Command Aborted Status: %s\n", cpas_state[cpsr & 0x3]); |
| 314 |
break; |
| 315 |
case nvme_mi_control_opcode_get_state: |
| 316 |
printf(" Get State : cspr is %#x\n", cpsr); |
| 317 |
printf(" Slot Command Servicing State: %s\n", slot_state[cpsr & 0x3]); |
| 318 |
printf(" Bad Message Integrity Check: %s\n", (cpsr & (1 << 4)) ? "Yes" : "No"); |
| 319 |
printf(" Timeout Waiting for a Packet: %s\n", (cpsr & (1 << 5)) ? "Yes" : "No"); |
| 320 |
printf(" Unsupported Transmission Unit: %s\n", (cpsr & (1 << 6)) ? "Yes" : "No"); |
| 321 |
printf(" Bad Header Version: %s\n", (cpsr & (1 << 7)) ? "Yes" : "No"); |
| 322 |
printf(" Unknown Destination ID: %s\n", (cpsr & (1 << 8)) ? "Yes" : "No"); |
| 323 |
printf(" Incorrect Transmission Unit: %s\n", (cpsr & (1 << 9)) ? "Yes" : "No"); |
| 324 |
printf(" Unexpected Middle or End of Packet: %s\n", (cpsr & (1 << 10)) ? "Yes" : "No"); |
| 325 |
printf(" Out-of-Sequence Packet Sequence Number: %s\n", (cpsr & (1 << 11)) ? "Yes" : "No"); |
| 326 |
printf(" Bad, Unexpected, or Expired Message Tag: %s\n", (cpsr & (1 << 12)) ? "Yes" : "No"); |
| 327 |
printf(" Bad Packet or Other Physical Layer: %s\n", (cpsr & (1 << 13)) ? "Yes" : "No"); |
| 328 |
printf(" NVM Subsystem Reset Occurred: %s\n", (cpsr & (1 << 14)) ? "Yes" : "No"); |
| 329 |
printf(" Pause Flag: %s\n", (cpsr & (1 << 15)) ? "Yes" : "No"); |
| 330 |
break; |
| 331 |
case nvme_mi_control_opcode_replay: |
| 332 |
printf(" Replay : cspr is %#x\n", cpsr); |
| 333 |
break; |
| 334 |
default: |
| 335 |
/* unreachable */ |
| 336 |
break; |
| 337 |
} |
| 338 |
|
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
void fhexdump(FILE *fp, const unsigned char *buf, int len) |
| 343 |
{ |
| 344 |
const int row_len = 16; |
| 345 |
int i, j; |
| 346 |
|
| 347 |
for (i = 0; i < len; i += row_len) { |
| 348 |
char hbuf[row_len * strlen("00 ") + 1]; |
| 349 |
char cbuf[row_len + strlen("|") + 1]; |
| 350 |
|
| 351 |
for (j = 0; (j < row_len) && ((i+j) < len); j++) { |
| 352 |
unsigned char c = buf[i + j]; |
| 353 |
|
| 354 |
sprintf(hbuf + j * 3, "%02x ", c); |
| 355 |
|
| 356 |
if (!isprint(c)) |
| 357 |
c = '.'; |
| 358 |
|
| 359 |
sprintf(cbuf + j, "%c", c); |
| 360 |
} |
| 361 |
|
| 362 |
strcat(cbuf, "|"); |
| 363 |
|
| 364 |
fprintf(fp, "%08x %*s |%s\n", i, |
| 365 |
0 - (int)sizeof(hbuf) + 1, hbuf, cbuf); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
void hexdump(const unsigned char *buf, int len) |
| 370 |
{ |
| 371 |
fhexdump(stdout, buf, len); |
| 372 |
} |
| 373 |
|
| 374 |
int do_get_log_page(nvme_mi_ep_t ep, int argc, char **argv) |
| 375 |
{ |
| 376 |
struct nvme_get_log_args args = { 0 }; |
| 377 |
struct nvme_mi_ctrl *ctrl; |
| 378 |
uint8_t buf[512]; |
| 379 |
uint16_t ctrl_id; |
| 380 |
int rc, tmp; |
| 381 |
|
| 382 |
if (argc < 2) { |
| 383 |
fprintf(stderr, "no controller ID specified\n"); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
tmp = atoi(argv[1]); |
| 388 |
if (tmp < 0 || tmp > 0xffff) { |
| 389 |
fprintf(stderr, "invalid controller ID\n"); |
| 390 |
return -1; |
| 391 |
} |
| 392 |
|
| 393 |
ctrl_id = tmp & 0xffff; |
| 394 |
|
| 395 |
args.args_size = sizeof(args); |
| 396 |
args.log = buf; |
| 397 |
args.len = sizeof(buf); |
| 398 |
|
| 399 |
if (argc > 2) { |
| 400 |
tmp = atoi(argv[2]); |
| 401 |
args.lid = tmp & 0xff; |
| 402 |
} else { |
| 403 |
args.lid = 0x1; |
| 404 |
} |
| 405 |
|
| 406 |
ctrl = nvme_mi_init_ctrl(ep, ctrl_id); |
| 407 |
if (!ctrl) { |
| 408 |
warn("can't create controller"); |
| 409 |
return -1; |
| 410 |
} |
| 411 |
|
| 412 |
rc = nvme_mi_admin_get_log(ctrl, &args); |
| 413 |
if (rc) { |
| 414 |
warn("can't perform Get Log page command"); |
| 415 |
return -1; |
| 416 |
} |
| 417 |
|
| 418 |
printf("Get log page (log id = 0x%02x) data:\n", args.lid); |
| 419 |
hexdump(buf, args.len); |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
int do_admin_raw(nvme_mi_ep_t ep, int argc, char **argv) |
| 425 |
{ |
| 426 |
struct nvme_mi_admin_req_hdr req; |
| 427 |
struct nvme_mi_admin_resp_hdr *resp; |
| 428 |
struct nvme_mi_ctrl *ctrl; |
| 429 |
size_t resp_data_len; |
| 430 |
unsigned long tmp; |
| 431 |
uint8_t buf[512]; |
| 432 |
uint16_t ctrl_id; |
| 433 |
uint8_t opcode; |
| 434 |
__le32 *cdw; |
| 435 |
int i, rc; |
| 436 |
|
| 437 |
if (argc < 2) { |
| 438 |
fprintf(stderr, "no controller ID specified\n"); |
| 439 |
return -1; |
| 440 |
} |
| 441 |
|
| 442 |
if (argc < 3) { |
| 443 |
fprintf(stderr, "no opcode specified\n"); |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
tmp = atoi(argv[1]); |
| 448 |
if (tmp > 0xffff) { |
| 449 |
fprintf(stderr, "invalid controller ID\n"); |
| 450 |
return -1; |
| 451 |
} |
| 452 |
ctrl_id = tmp & 0xffff; |
| 453 |
|
| 454 |
tmp = atoi(argv[2]); |
| 455 |
if (tmp > 0xff) { |
| 456 |
fprintf(stderr, "invalid opcode\n"); |
| 457 |
return -1; |
| 458 |
} |
| 459 |
opcode = tmp & 0xff; |
| 460 |
|
| 461 |
memset(&req, 0, sizeof(req)); |
| 462 |
req.opcode = opcode; |
| 463 |
req.ctrl_id = cpu_to_le16(ctrl_id); |
| 464 |
|
| 465 |
/* The cdw10 - cdw16 fields are contiguous in req; set from argv. */ |
| 466 |
cdw = (void *)&req + offsetof(typeof(req), cdw10); |
| 467 |
for (i = 0; i < 6; i++) { |
| 468 |
if (argc >= 4 + i) |
| 469 |
tmp = strtoul(argv[3 + i], NULL, 0); |
| 470 |
else |
| 471 |
tmp = 0; |
| 472 |
*cdw = cpu_to_le32(tmp & 0xffffffff); |
| 473 |
cdw++; |
| 474 |
} |
| 475 |
|
| 476 |
printf("Admin request:\n"); |
| 477 |
printf(" opcode: 0x%02x\n", req.opcode); |
| 478 |
printf(" ctrl: 0x%04x\n", le16_to_cpu(req.ctrl_id)); |
| 479 |
printf(" cdw10: 0x%08x\n", le32_to_cpu(req.cdw10)); |
| 480 |
printf(" cdw11: 0x%08x\n", le32_to_cpu(req.cdw11)); |
| 481 |
printf(" cdw12: 0x%08x\n", le32_to_cpu(req.cdw12)); |
| 482 |
printf(" cdw13: 0x%08x\n", le32_to_cpu(req.cdw13)); |
| 483 |
printf(" cdw14: 0x%08x\n", le32_to_cpu(req.cdw14)); |
| 484 |
printf(" cdw15: 0x%08x\n", le32_to_cpu(req.cdw15)); |
| 485 |
printf(" raw:\n"); |
| 486 |
hexdump((void *)&req, sizeof(req)); |
| 487 |
|
| 488 |
memset(buf, 0, sizeof(buf)); |
| 489 |
resp = (void *)buf; |
| 490 |
|
| 491 |
ctrl = nvme_mi_init_ctrl(ep, ctrl_id); |
| 492 |
if (!ctrl) { |
| 493 |
warn("can't create controller"); |
| 494 |
return -1; |
| 495 |
} |
| 496 |
|
| 497 |
resp_data_len = sizeof(buf) - sizeof(*resp); |
| 498 |
|
| 499 |
rc = nvme_mi_admin_xfer(ctrl, &req, 0, resp, 0, &resp_data_len); |
| 500 |
if (rc) { |
| 501 |
warn("nvme_admin_xfer failed: %d", rc); |
| 502 |
return -1; |
| 503 |
} |
| 504 |
|
| 505 |
printf("Admin response:\n"); |
| 506 |
printf(" Status: 0x%02x\n", resp->status); |
| 507 |
printf(" cdw0: 0x%08x\n", le32_to_cpu(resp->cdw0)); |
| 508 |
printf(" cdw1: 0x%08x\n", le32_to_cpu(resp->cdw1)); |
| 509 |
printf(" cdw3: 0x%08x\n", le32_to_cpu(resp->cdw3)); |
| 510 |
printf(" data [%zd bytes]\n", resp_data_len); |
| 511 |
|
| 512 |
hexdump(buf + sizeof(*resp), resp_data_len); |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
static struct { |
| 517 |
uint8_t id; |
| 518 |
const char *name; |
| 519 |
} sec_protos[] = { |
| 520 |
{ 0x00, "Security protocol information" }, |
| 521 |
{ 0xea, "NVMe" }, |
| 522 |
{ 0xec, "JEDEC Universal Flash Storage" }, |
| 523 |
{ 0xed, "SDCard TrustedFlash Security" }, |
| 524 |
{ 0xee, "IEEE 1667" }, |
| 525 |
{ 0xef, "ATA Device Server Password Security" }, |
| 526 |
}; |
| 527 |
|
| 528 |
static const char *sec_proto_description(uint8_t id) |
| 529 |
{ |
| 530 |
unsigned int i; |
| 531 |
|
| 532 |
for (i = 0; i < ARRAY_SIZE(sec_protos); i++) { |
| 533 |
if (sec_protos[i].id == id) |
| 534 |
return sec_protos[i].name; |
| 535 |
} |
| 536 |
|
| 537 |
if (id >= 0xf0) |
| 538 |
return "Vendor specific"; |
| 539 |
|
| 540 |
return "unknown"; |
| 541 |
} |
| 542 |
|
| 543 |
int do_security_info(nvme_mi_ep_t ep, int argc, char **argv) |
| 544 |
{ |
| 545 |
struct nvme_security_receive_args args = { 0 }; |
| 546 |
nvme_mi_ctrl_t ctrl; |
| 547 |
int i, rc, n_proto; |
| 548 |
unsigned long tmp; |
| 549 |
uint16_t ctrl_id; |
| 550 |
struct { |
| 551 |
uint8_t rsvd[6]; |
| 552 |
uint16_t len; |
| 553 |
uint8_t protocols[256]; |
| 554 |
} proto_info; |
| 555 |
|
| 556 |
if (argc != 2) { |
| 557 |
fprintf(stderr, "no controller ID specified\n"); |
| 558 |
return -1; |
| 559 |
} |
| 560 |
|
| 561 |
tmp = atoi(argv[1]); |
| 562 |
if (tmp > 0xffff) { |
| 563 |
fprintf(stderr, "invalid controller ID\n"); |
| 564 |
return -1; |
| 565 |
} |
| 566 |
|
| 567 |
ctrl_id = tmp & 0xffff; |
| 568 |
|
| 569 |
ctrl = nvme_mi_init_ctrl(ep, ctrl_id); |
| 570 |
if (!ctrl) { |
| 571 |
warn("can't create controller"); |
| 572 |
return -1; |
| 573 |
} |
| 574 |
|
| 575 |
/* protocol 0x00, spsp 0x0000: retrieve supported protocols */ |
| 576 |
args.args_size = sizeof(args); |
| 577 |
args.data = &proto_info; |
| 578 |
args.data_len = sizeof(proto_info); |
| 579 |
|
| 580 |
rc = nvme_mi_admin_security_recv(ctrl, &args); |
| 581 |
if (rc) { |
| 582 |
warnx("can't perform Security Receive command: rc %d", rc); |
| 583 |
return -1; |
| 584 |
} |
| 585 |
|
| 586 |
if (args.data_len < 6) { |
| 587 |
warnx("Short response in security receive command (%d bytes)", |
| 588 |
args.data_len); |
| 589 |
return -1; |
| 590 |
} |
| 591 |
|
| 592 |
n_proto = be16_to_cpu(proto_info.len); |
| 593 |
if (args.data_len < 6 + n_proto) { |
| 594 |
warnx("Short response in security receive command (%d bytes), " |
| 595 |
"for %d protocols", args.data_len, n_proto); |
| 596 |
return -1; |
| 597 |
} |
| 598 |
|
| 599 |
printf("Supported protocols:\n"); |
| 600 |
for (i = 0; i < n_proto; i++) { |
| 601 |
uint8_t id = proto_info.protocols[i]; |
| 602 |
printf(" 0x%02x: %s\n", id, sec_proto_description(id)); |
| 603 |
} |
| 604 |
|
| 605 |
return 0; |
| 606 |
} |
| 607 |
|
| 608 |
struct { |
| 609 |
enum nvme_mi_config_smbus_freq id; |
| 610 |
const char *str; |
| 611 |
} smbus_freqs[] = { |
| 612 |
{ NVME_MI_CONFIG_SMBUS_FREQ_100kHz, "100k" }, |
| 613 |
{ NVME_MI_CONFIG_SMBUS_FREQ_400kHz, "400k" }, |
| 614 |
{ NVME_MI_CONFIG_SMBUS_FREQ_1MHz, "1M" }, |
| 615 |
}; |
| 616 |
|
| 617 |
static const char *smbus_freq_str(enum nvme_mi_config_smbus_freq freq) |
| 618 |
{ |
| 619 |
unsigned int i; |
| 620 |
|
| 621 |
for (i = 0; i < ARRAY_SIZE(smbus_freqs); i++) { |
| 622 |
if (smbus_freqs[i].id == freq) |
| 623 |
return smbus_freqs[i].str; |
| 624 |
} |
| 625 |
|
| 626 |
return NULL; |
| 627 |
} |
| 628 |
|
| 629 |
static int smbus_freq_val(const char *str, enum nvme_mi_config_smbus_freq *freq) |
| 630 |
{ |
| 631 |
unsigned int i; |
| 632 |
|
| 633 |
for (i = 0; i < ARRAY_SIZE(smbus_freqs); i++) { |
| 634 |
if (!strcmp(smbus_freqs[i].str, str)) { |
| 635 |
*freq = smbus_freqs[i].id; |
| 636 |
return 0; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
return -1; |
| 641 |
} |
| 642 |
|
| 643 |
int do_config_get(nvme_mi_ep_t ep, int argc, char **argv) |
| 644 |
{ |
| 645 |
enum nvme_mi_config_smbus_freq freq; |
| 646 |
uint16_t mtu; |
| 647 |
uint8_t port; |
| 648 |
int rc; |
| 649 |
|
| 650 |
if (argc > 1) |
| 651 |
port = atoi(argv[1]) & 0xff; |
| 652 |
else |
| 653 |
port = 0; |
| 654 |
|
| 655 |
rc = nvme_mi_mi_config_get_smbus_freq(ep, port, &freq); |
| 656 |
if (rc) { |
| 657 |
warn("can't query SMBus freq for port %d\n", port); |
| 658 |
} else { |
| 659 |
const char *fstr = smbus_freq_str(freq); |
| 660 |
printf("SMBus access frequency (port %d): %s [0x%x]\n", port, |
| 661 |
fstr ?: "unknown", freq); |
| 662 |
} |
| 663 |
|
| 664 |
rc = nvme_mi_mi_config_get_mctp_mtu(ep, port, &mtu); |
| 665 |
if (rc) |
| 666 |
warn("can't query MCTP MTU for port %d\n", port); |
| 667 |
else |
| 668 |
printf("MCTP MTU (port %d): %d\n", port, mtu); |
| 669 |
|
| 670 |
return 0; |
| 671 |
} |
| 672 |
|
| 673 |
int do_config_set(nvme_mi_ep_t ep, int argc, char **argv) |
| 674 |
{ |
| 675 |
const char *name, *val; |
| 676 |
uint8_t port; |
| 677 |
int rc; |
| 678 |
|
| 679 |
if (argc != 4) { |
| 680 |
fprintf(stderr, "config set requires <port> <type> <val>\n"); |
| 681 |
return -1; |
| 682 |
} |
| 683 |
|
| 684 |
port = atoi(argv[1]) & 0xff; |
| 685 |
name = argv[2]; |
| 686 |
val = argv[3]; |
| 687 |
|
| 688 |
if (!strcmp(name, "freq")) { |
| 689 |
enum nvme_mi_config_smbus_freq freq; |
| 690 |
rc = smbus_freq_val(val, &freq); |
| 691 |
if (rc) { |
| 692 |
fprintf(stderr, "unknown SMBus freq %s. " |
| 693 |
"Try 100k, 400k or 1M\n", val); |
| 694 |
return -1; |
| 695 |
} |
| 696 |
rc = nvme_mi_mi_config_set_smbus_freq(ep, port, freq); |
| 697 |
|
| 698 |
} else if (!strcmp(name, "mtu")) { |
| 699 |
uint16_t mtu; |
| 700 |
mtu = atoi(val) & 0xffff; |
| 701 |
/* controllers should reject this, but prevent the potential |
| 702 |
* footgun of disabling futher comunication with the device |
| 703 |
*/ |
| 704 |
if (mtu < 64) { |
| 705 |
fprintf(stderr, "MTU value too small\n"); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
rc = nvme_mi_mi_config_set_mctp_mtu(ep, port, mtu); |
| 709 |
|
| 710 |
} else { |
| 711 |
fprintf(stderr, "Invalid configuration '%s', " |
| 712 |
"try freq or mtu\n", name); |
| 713 |
return -1; |
| 714 |
} |
| 715 |
|
| 716 |
if (rc) |
| 717 |
fprintf(stderr, "config set failed with status %d\n", rc); |
| 718 |
|
| 719 |
return rc; |
| 720 |
} |
| 721 |
|
| 722 |
enum action { |
| 723 |
ACTION_INFO, |
| 724 |
ACTION_CONTROLLERS, |
| 725 |
ACTION_IDENTIFY, |
| 726 |
ACTION_GET_LOG_PAGE, |
| 727 |
ACTION_ADMIN_RAW, |
| 728 |
ACTION_SECURITY_INFO, |
| 729 |
ACTION_CONFIG_GET, |
| 730 |
ACTION_CONFIG_SET, |
| 731 |
ACTION_CONTROL_PRIMITIVE, |
| 732 |
}; |
| 733 |
|
| 734 |
static int do_action_endpoint(enum action action, nvme_mi_ep_t ep, int argc, char** argv) |
| 735 |
{ |
| 736 |
int rc; |
| 737 |
|
| 738 |
switch (action) { |
| 739 |
case ACTION_INFO: |
| 740 |
rc = do_info(ep); |
| 741 |
break; |
| 742 |
case ACTION_CONTROLLERS: |
| 743 |
rc = do_controllers(ep); |
| 744 |
break; |
| 745 |
case ACTION_IDENTIFY: |
| 746 |
rc = do_identify(ep, argc, argv); |
| 747 |
break; |
| 748 |
case ACTION_GET_LOG_PAGE: |
| 749 |
rc = do_get_log_page(ep, argc, argv); |
| 750 |
break; |
| 751 |
case ACTION_ADMIN_RAW: |
| 752 |
rc = do_admin_raw(ep, argc, argv); |
| 753 |
break; |
| 754 |
case ACTION_SECURITY_INFO: |
| 755 |
rc = do_security_info(ep, argc, argv); |
| 756 |
break; |
| 757 |
case ACTION_CONFIG_GET: |
| 758 |
rc = do_config_get(ep, argc, argv); |
| 759 |
break; |
| 760 |
case ACTION_CONFIG_SET: |
| 761 |
rc = do_config_set(ep, argc, argv); |
| 762 |
break; |
| 763 |
case ACTION_CONTROL_PRIMITIVE: |
| 764 |
rc = do_control_primitive(ep, argc, argv); |
| 765 |
break; |
| 766 |
default: |
| 767 |
/* This shouldn't be possible, as we should be covering all |
| 768 |
* of the enum action options above. Hoever, keep the compilers |
| 769 |
* happy and fail gracefully. */ |
| 770 |
fprintf(stderr, "invalid action %d?\n", action); |
| 771 |
rc = -1; |
| 772 |
} |
| 773 |
return rc; |
| 774 |
} |
| 775 |
|
| 776 |
int main(int argc, char **argv) |
| 777 |
{ |
| 778 |
enum action action; |
| 779 |
nvme_root_t root; |
| 780 |
nvme_mi_ep_t ep; |
| 781 |
bool dbus = false, usage = true; |
| 782 |
uint8_t eid = 0; |
| 783 |
int rc = 0, net = 0; |
| 784 |
|
| 785 |
if (argc >= 2 && strcmp(argv[1], "dbus") == 0) { |
| 786 |
usage = false; |
| 787 |
dbus= true; |
| 788 |
argv += 1; |
| 789 |
argc -= 1; |
| 790 |
} else if (argc >= 3) { |
| 791 |
usage = false; |
| 792 |
net = atoi(argv[1]); |
| 793 |
eid = atoi(argv[2]) & 0xff; |
| 794 |
argv += 2; |
| 795 |
argc -= 2; |
| 796 |
} |
| 797 |
|
| 798 |
if (usage) { |
| 799 |
fprintf(stderr, |
| 800 |
"usage: %s <net> <eid> [action] [action args]\n" |
| 801 |
" %s 'dbus' [action] [action args]\n", |
| 802 |
argv[0], argv[0]); |
| 803 |
fprintf(stderr, "where action is:\n" |
| 804 |
" info\n" |
| 805 |
" controllers\n" |
| 806 |
" identify <controller-id> [--partial]\n" |
| 807 |
" get-log-page <controller-id> [<log-id>]\n" |
| 808 |
" admin <controller-id> <opcode> [<cdw10>, <cdw11>, ...]\n" |
| 809 |
" security-info <controller-id>\n" |
| 810 |
" get-config [port]\n" |
| 811 |
" set-config <port> <type> <val>\n" |
| 812 |
" control-primitive [action(abort|pause|resume|get-state|replay)]\n" |
| 813 |
"\n" |
| 814 |
" 'dbus' target will query D-Bus for known MCTP endpoints\n" |
| 815 |
); |
| 816 |
return EXIT_FAILURE; |
| 817 |
} |
| 818 |
|
| 819 |
if (argc == 1) { |
| 820 |
action = ACTION_INFO; |
| 821 |
} else { |
| 822 |
char *action_str = argv[1]; |
| 823 |
argc--; |
| 824 |
argv++; |
| 825 |
|
| 826 |
if (!strcmp(action_str, "info")) { |
| 827 |
action = ACTION_INFO; |
| 828 |
} else if (!strcmp(action_str, "controllers")) { |
| 829 |
action = ACTION_CONTROLLERS; |
| 830 |
} else if (!strcmp(action_str, "identify")) { |
| 831 |
action = ACTION_IDENTIFY; |
| 832 |
} else if (!strcmp(action_str, "get-log-page")) { |
| 833 |
action = ACTION_GET_LOG_PAGE; |
| 834 |
} else if (!strcmp(action_str, "admin")) { |
| 835 |
action = ACTION_ADMIN_RAW; |
| 836 |
} else if (!strcmp(action_str, "security-info")) { |
| 837 |
action = ACTION_SECURITY_INFO; |
| 838 |
} else if (!strcmp(action_str, "get-config")) { |
| 839 |
action = ACTION_CONFIG_GET; |
| 840 |
} else if (!strcmp(action_str, "set-config")) { |
| 841 |
action = ACTION_CONFIG_SET; |
| 842 |
} else if (!strcmp(action_str, "control-primitive")) { |
| 843 |
action = ACTION_CONTROL_PRIMITIVE; |
| 844 |
} else { |
| 845 |
fprintf(stderr, "invalid action '%s'\n", action_str); |
| 846 |
return EXIT_FAILURE; |
| 847 |
} |
| 848 |
} |
| 849 |
if (dbus) { |
| 850 |
nvme_root_t root; |
| 851 |
int i = 0; |
| 852 |
|
| 853 |
root = nvme_mi_scan_mctp(); |
| 854 |
if (!root) |
| 855 |
errx(EXIT_FAILURE, "can't scan D-Bus entries"); |
| 856 |
|
| 857 |
nvme_mi_for_each_endpoint(root, ep) i++; |
| 858 |
printf("Found %d endpoints in D-Bus:\n", i); |
| 859 |
nvme_mi_for_each_endpoint(root, ep) { |
| 860 |
char *desc = nvme_mi_endpoint_desc(ep); |
| 861 |
printf("%s\n", desc); |
| 862 |
rc = do_action_endpoint(action, ep, argc, argv); |
| 863 |
printf("---\n"); |
| 864 |
free(desc); |
| 865 |
} |
| 866 |
nvme_mi_free_root(root); |
| 867 |
} else { |
| 868 |
root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL); |
| 869 |
if (!root) |
| 870 |
err(EXIT_FAILURE, "can't create NVMe root"); |
| 871 |
|
| 872 |
ep = nvme_mi_open_mctp(root, net, eid); |
| 873 |
if (!ep) |
| 874 |
errx(EXIT_FAILURE, "can't open MCTP endpoint %d:%d", net, eid); |
| 875 |
rc = do_action_endpoint(action, ep, argc, argv); |
| 876 |
nvme_mi_close(ep); |
| 877 |
nvme_mi_free_root(root); |
| 878 |
} |
| 879 |
|
| 880 |
return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 881 |
} |
| 882 |
|
| 883 |
|