xref: /freebsd/sys/cam/nvme/nvme_all.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Netflix, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 
31 #ifdef _KERNEL
32 #include "opt_scsi.h"
33 
34 #include <sys/systm.h>
35 #include <sys/libkern.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/sysctl.h>
39 #else
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #ifndef min
45 #define min(a,b) (((a)<(b))?(a):(b))
46 #endif
47 #endif
48 
49 #include <cam/cam.h>
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_xpt.h>
53 #include <cam/nvme/nvme_all.h>
54 #include <sys/sbuf.h>
55 #include <sys/endian.h>
56 
57 #ifdef _KERNEL
58 #include <cam/cam_periph.h>
59 #include <cam/cam_xpt_sim.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_xpt_internal.h>
62 #endif
63 
64 void
65 nvme_ns_cmd(struct ccb_nvmeio *nvmeio, uint8_t cmd, uint32_t nsid,
66     uint32_t cdw10, uint32_t cdw11, uint32_t cdw12, uint32_t cdw13,
67     uint32_t cdw14, uint32_t cdw15)
68 {
69 	bzero(&nvmeio->cmd, sizeof(struct nvme_command));
70 	nvmeio->cmd.opc = cmd;
71 	nvmeio->cmd.nsid = htole32(nsid);
72 	nvmeio->cmd.cdw10 = htole32(cdw10);
73 	nvmeio->cmd.cdw11 = htole32(cdw11);
74 	nvmeio->cmd.cdw12 = htole32(cdw12);
75 	nvmeio->cmd.cdw13 = htole32(cdw13);
76 	nvmeio->cmd.cdw14 = htole32(cdw14);
77 	nvmeio->cmd.cdw15 = htole32(cdw15);
78 }
79 
80 int
81 nvme_identify_match(caddr_t identbuffer, caddr_t table_entry)
82 {
83 	return 0;
84 }
85 
86 void
87 nvme_print_ident(const struct nvme_controller_data *cdata,
88     const struct nvme_namespace_data *data, struct sbuf *sb)
89 {
90 	nvme_print_ident_short(cdata, data, sb);
91 	sbuf_printf(sb, "\n");
92 }
93 
94 void
95 nvme_print_ident_short(const struct nvme_controller_data *cdata,
96     const struct nvme_namespace_data *data, struct sbuf *sb)
97 {
98 	sbuf_printf(sb, "<");
99 	cam_strvis_sbuf(sb, cdata->mn, sizeof(cdata->mn),
100 	    CAM_STRVIS_FLAG_NONASCII_SPC);
101 	sbuf_printf(sb, " ");
102 	cam_strvis_sbuf(sb, cdata->fr, sizeof(cdata->fr),
103 	    CAM_STRVIS_FLAG_NONASCII_SPC);
104 	sbuf_printf(sb, " ");
105 	cam_strvis_sbuf(sb, cdata->sn, sizeof(cdata->sn),
106 	    CAM_STRVIS_FLAG_NONASCII_SPC);
107 	sbuf_printf(sb, ">");
108 }
109 
110 /* XXX need to do nvme admin opcodes too, but those aren't used yet by nda */
111 static const char *
112 nvme_opc2str[] = {
113 	"FLUSH",
114 	"WRITE",
115 	"READ",
116 	"RSVD-3",
117 	"WRITE_UNCORRECTABLE",
118 	"COMPARE",
119 	"RSVD-6",
120 	"RSVD-7",
121 	"WRITE_ZEROES",
122 	"DATASET_MANAGEMENT",
123 	"RSVD-a",
124 	"RSVD-b",
125 	"RSVD-c",
126 	"RESERVATION_REGISTER",
127 	"RESERVATION_REPORT",
128 	"RSVD-f",
129 	"RSVD-10",
130 	"RESERVATION_ACQUIRE",
131 	"RSVD-12",
132 	"RSVD-13",
133 	"RSVD-14",
134 	"RESERVATION_RELEASE",
135 };
136 
137 const char *
138 nvme_op_string(const struct nvme_command *cmd, int admin)
139 {
140 
141 	if (admin) {
142 		return "ADMIN";
143 	} else {
144 		if (cmd->opc >= nitems(nvme_opc2str))
145 			return "UNKNOWN";
146 		return nvme_opc2str[cmd->opc];
147 	}
148 }
149 
150 const char *
151 nvme_cmd_string(const struct nvme_command *cmd, char *cmd_string, size_t len)
152 {
153 	struct sbuf sb;
154 	int error;
155 
156 	if (len == 0)
157 		return ("");
158 
159 	sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
160 	nvme_cmd_sbuf(cmd, &sb);
161 
162 	error = sbuf_finish(&sb);
163 	if (error != 0 &&
164 #ifdef _KERNEL
165 	    error != ENOMEM)
166 #else
167 	    errno != ENOMEM)
168 #endif
169 		return ("");
170 
171 	return(sbuf_data(&sb));
172 }
173 
174 void
175 nvme_cmd_sbuf(const struct nvme_command *cmd, struct sbuf *sb)
176 {
177 
178 	/*
179 	 * cid, rsvd areas and mptr not printed, since they are used
180 	 * only internally by the SIM.
181 	 */
182 	sbuf_printf(sb,
183 	    "opc=%x fuse=%x nsid=%x prp1=%llx prp2=%llx cdw=%x %x %x %x %x %x",
184 	    cmd->opc, cmd->fuse, cmd->nsid,
185 	    (unsigned long long)cmd->prp1, (unsigned long long)cmd->prp2,
186 	    cmd->cdw10, cmd->cdw11, cmd->cdw12,
187 	    cmd->cdw13, cmd->cdw14, cmd->cdw15);
188 }
189 
190 /*
191  * nvme_command_sbuf() returns 0 for success and -1 for failure.
192  */
193 int
194 nvme_command_sbuf(struct ccb_nvmeio *nvmeio, struct sbuf *sb)
195 {
196 
197 	sbuf_printf(sb, "%s. NCB: ", nvme_op_string(&nvmeio->cmd,
198 	    nvmeio->ccb_h.func_code == XPT_NVME_ADMIN));
199 	nvme_cmd_sbuf(&nvmeio->cmd, sb);
200 	return(0);
201 }
202 
203 #ifdef _KERNEL
204 const void *
205 nvme_get_identify_cntrl(struct cam_periph *periph)
206 {
207 	struct cam_ed *device;
208 
209 	device = periph->path->device;
210 
211 	return device->nvme_cdata;
212 }
213 
214 const void *
215 nvme_get_identify_ns(struct cam_periph *periph)
216 {
217 	struct cam_ed *device;
218 
219 	device = periph->path->device;
220 
221 	return device->nvme_data;
222 }
223 #endif
224