1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2025 Oxide Computer Company
*/
/*
* This file contains shared pieces of the NVMe field validation logic and has
* shared pieces that are used between different parts.
*/
#include "nvme_common.h"
#include <sys/sysmacros.h>
#ifdef _KERNEL
#include <sys/sunddi.h>
#include <sys/stdint.h>
#else
#include <stdio.h>
#include <inttypes.h>
#endif
bool
nvme_field_atleast(const nvme_valid_ctrl_data_t *data,
const nvme_version_t *targ)
{
return (nvme_vers_atleast(data->vcd_vers, targ));
}
/*
* Note, we rely on external logic to determine if the broadcast nsid is valid.
* We always accept it.
*/
bool
nvme_field_valid_nsid(const nvme_field_info_t *field,
const nvme_valid_ctrl_data_t *data, uint64_t nsid, char *msg, size_t msglen)
{
if ((nsid != 0 && nsid <= data->vcd_id->id_nn) ||
nsid == NVME_NSID_BCAST) {
return (true);
}
(void) snprintf(msg, msglen, "namespace id %" PRIu64 "is outside the "
"valid range [0x%x, 0x%x], the broadcast nsid (0x%x) may be valid",
nsid, NVME_NSID_MIN, NVME_NSID_BCAST, data->vcd_id->id_nn);
return (false);
}
bool
nvme_field_range_check(const nvme_field_info_t *field, uint64_t min,
uint64_t max, char *msg, size_t msglen, uint64_t value)
{
if (value >= min && value <= max) {
return (true);
}
(void) snprintf(msg, msglen, "field %s (%s) value 0x%"
PRIx64 " is outside the valid range: [0x%" PRIx64 ", 0x%" PRIx64
"]", field->nlfi_human, field->nlfi_spec, value, min, max);
return (false);
}
bool
nvme_field_mask_check(const nvme_field_info_t *field, uint64_t valid_mask,
char *msg, size_t msglen, uint64_t value)
{
uint64_t inval = ~valid_mask & value;
if (inval == 0) {
return (true);
}
(void) snprintf(msg, msglen, "field %s (%s) value 0x%" PRIx64
" uses bits outside of the mask 0x%" PRIx64 ": 0x%" PRIx64,
field->nlfi_human, field->nlfi_spec, value, valid_mask, inval);
return (false);
}
/*
* This is a general validation function for fields that are part of a command.
* It will check if the field is supported by the controller and if so, that its
* value is within the expected range. On error, an optional message will be
* written that explains the error. This is intended to be shared between
* userland and the kernel. The kernel should pass NULL/0 for msg/msglen because
* there is no message translation capability in the kernel.
*/
nvme_field_error_t
nvme_field_validate(const nvme_field_info_t *field,
const nvme_valid_ctrl_data_t *data, uint64_t value, char *msg,
size_t msglen)
{
ASSERT3P(field->nlfi_vers, !=, NULL);
if (msglen > 0)
*msg = '\0';
if (!nvme_field_atleast(data, field->nlfi_vers)) {
(void) snprintf(msg, msglen, "field %s (%s) requires "
"version %u.%u, but device is at %u.%u", field->nlfi_human,
field->nlfi_spec, data->vcd_vers->v_major,
data->vcd_vers->v_minor, field->nlfi_vers->v_major,
field->nlfi_vers->v_minor);
return (NVME_FIELD_ERR_UNSUP_VERSION);
}
if (field->nlfi_sup != NULL && !field->nlfi_sup(field, data, msg,
msglen)) {
(void) snprintf(msg, msglen, "field %s (%s) is not "
"supported by the controller", field->nlfi_human,
field->nlfi_spec);
return (NVME_FIELD_ERR_UNSUP_FIELD);
}
if (field->nlfi_valid != NULL) {
if (!field->nlfi_valid(field, data, value, msg, msglen)) {
if (msglen > 0 && *msg == '\0') {
(void) snprintf(msg, msglen,
"field %s (%s) value 0x%" PRIx64
" is invalid",
field->nlfi_human,
field->nlfi_spec, value);
}
return (NVME_FIELD_ERR_BAD_VALUE);
}
} else if (!nvme_field_range_check(field, 0, field->nlfi_max_size, msg,
msglen, value)) {
return (NVME_FIELD_ERR_BAD_VALUE);
}
return (NVME_FIELD_ERR_OK);
}
|