| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/** |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2020 Western Digital Corporation or its affiliates. |
| 5 |
* |
| 6 |
* Authors: Keith Busch <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Search out for ZNS type namespaces, and if found, report their properties. |
| 11 |
*/ |
| 12 |
#include <stdio.h> |
| 13 |
#include <string.h> |
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <libnvme.h> |
| 17 |
#include <inttypes.h> |
| 18 |
|
| 19 |
#include <ccan/endian/endian.h> |
| 20 |
|
| 21 |
static void show_zns_properties(nvme_ns_t n) |
| 22 |
{ |
| 23 |
struct nvme_zns_id_ns zns_ns; |
| 24 |
struct nvme_zns_id_ctrl zns_ctrl; |
| 25 |
struct nvme_zone_report *zr; |
| 26 |
__u32 result; |
| 27 |
|
| 28 |
zr = calloc(1, 0x1000); |
| 29 |
if (!zr) |
| 30 |
return; |
| 31 |
|
| 32 |
if (nvme_zns_identify_ns(nvme_ns_get_fd(n), nvme_ns_get_nsid(n), |
| 33 |
&zns_ns)) { |
| 34 |
fprintf(stderr, "failed to identify zns ns\n");; |
| 35 |
} |
| 36 |
|
| 37 |
printf("zoc:%x ozcs:%x mar:%x mor:%x\n", le16_to_cpu(zns_ns.zoc), |
| 38 |
le16_to_cpu(zns_ns.ozcs), le32_to_cpu(zns_ns.mar), |
| 39 |
le32_to_cpu(zns_ns.mor)); |
| 40 |
|
| 41 |
if (nvme_zns_identify_ctrl(nvme_ns_get_fd(n), &zns_ctrl)) { |
| 42 |
fprintf(stderr, "failed to identify zns ctrl\n");; |
| 43 |
free(zr); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
printf("zasl:%u\n", zns_ctrl.zasl); |
| 48 |
|
| 49 |
if (nvme_zns_report_zones(nvme_ns_get_fd(n), nvme_ns_get_nsid(n), 0, |
| 50 |
NVME_ZNS_ZRAS_REPORT_ALL, false, |
| 51 |
true, 0x1000, (void *)zr, |
| 52 |
NVME_DEFAULT_IOCTL_TIMEOUT, &result)) { |
| 53 |
fprintf(stderr, "failed to report zones, result %x\n", |
| 54 |
le32_to_cpu(result)); |
| 55 |
free(zr); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
printf("nr_zones:%"PRIu64"\n", le64_to_cpu(zr->nr_zones)); |
| 60 |
free(zr); |
| 61 |
} |
| 62 |
|
| 63 |
int main() |
| 64 |
{ |
| 65 |
nvme_subsystem_t s; |
| 66 |
nvme_root_t r; |
| 67 |
nvme_host_t h; |
| 68 |
nvme_ctrl_t c; |
| 69 |
nvme_ns_t n; |
| 70 |
|
| 71 |
r = nvme_scan(NULL); |
| 72 |
if (!r) |
| 73 |
return -1; |
| 74 |
|
| 75 |
nvme_for_each_host(r, h) { |
| 76 |
nvme_for_each_subsystem(h, s) { |
| 77 |
nvme_subsystem_for_each_ctrl(s, c) { |
| 78 |
nvme_ctrl_for_each_ns(c, n) { |
| 79 |
if (nvme_ns_get_csi(n) == NVME_CSI_ZNS) |
| 80 |
show_zns_properties(n); |
| 81 |
} |
| 82 |
} |
| 83 |
nvme_subsystem_for_each_ns(s, n) { |
| 84 |
if (nvme_ns_get_csi(n) == NVME_CSI_ZNS) |
| 85 |
show_zns_properties(n); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
nvme_free_tree(r); |
| 90 |
} |