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 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32
33 #ifdef _KERNEL
34 #include "opt_scsi.h"
35
36 #include <sys/systm.h>
37 #include <sys/libkern.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #else
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #ifndef min
47 #define min(a,b) (((a)<(b))?(a):(b))
48 #endif
49 #endif
50
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
55 #include <cam/nvme/nvme_all.h>
56 #include <sys/sbuf.h>
57 #include <sys/endian.h>
58
59 #ifdef _KERNEL
60 #include <cam/cam_periph.h>
61 #include <cam/cam_xpt_sim.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_xpt_internal.h>
64 #endif
65
66 void
67 nvme_ns_cmd(struct ccb_nvmeio *nvmeio, uint8_t cmd, uint32_t nsid,
68 uint32_t cdw10, uint32_t cdw11, uint32_t cdw12, uint32_t cdw13,
69 uint32_t cdw14, uint32_t cdw15)
70 {
71 bzero(&nvmeio->cmd, sizeof(struct nvme_command));
72 nvmeio->cmd.opc = cmd;
73 nvmeio->cmd.nsid = htole32(nsid);
74 nvmeio->cmd.cdw10 = htole32(cdw10);
75 nvmeio->cmd.cdw11 = htole32(cdw11);
76 nvmeio->cmd.cdw12 = htole32(cdw12);
77 nvmeio->cmd.cdw13 = htole32(cdw13);
78 nvmeio->cmd.cdw14 = htole32(cdw14);
79 nvmeio->cmd.cdw15 = htole32(cdw15);
80 }
81
82 int
83 nvme_identify_match(caddr_t identbuffer, caddr_t table_entry)
84 {
85 return 0;
86 }
87
88 void
89 nvme_print_ident(const struct nvme_controller_data *cdata,
90 const struct nvme_namespace_data *data, struct sbuf *sb)
91 {
92
93 sbuf_printf(sb, "<");
94 cam_strvis_sbuf(sb, cdata->mn, sizeof(cdata->mn),
95 CAM_STRVIS_FLAG_NONASCII_SPC);
96 sbuf_printf(sb, " ");
97 cam_strvis_sbuf(sb, cdata->fr, sizeof(cdata->fr),
98 CAM_STRVIS_FLAG_NONASCII_SPC);
99 sbuf_printf(sb, " ");
100 cam_strvis_sbuf(sb, cdata->sn, sizeof(cdata->sn),
101 CAM_STRVIS_FLAG_NONASCII_SPC);
102 sbuf_printf(sb, ">\n");
103 }
104
105 /* XXX need to do nvme admin opcodes too, but those aren't used yet by nda */
106 static const char *
107 nvme_opc2str[] = {
108 "FLUSH",
109 "WRITE",
110 "READ",
111 "RSVD-3",
112 "WRITE_UNCORRECTABLE",
113 "COMPARE",
114 "RSVD-6",
115 "RSVD-7",
116 "WRITE_ZEROES",
117 "DATASET_MANAGEMENT",
118 "RSVD-a",
119 "RSVD-b",
120 "RSVD-c",
121 "RESERVATION_REGISTER",
122 "RESERVATION_REPORT",
123 "RSVD-f",
124 "RSVD-10",
125 "RESERVATION_ACQUIRE",
126 "RSVD-12",
127 "RSVD-13",
128 "RSVD-14",
129 "RESERVATION_RELEASE",
130 };
131
132 const char *
133 nvme_op_string(const struct nvme_command *cmd, int admin)
134 {
135
136 if (admin) {
137 return "ADMIN";
138 } else {
139 if (cmd->opc >= nitems(nvme_opc2str))
140 return "UNKNOWN";
141 return nvme_opc2str[cmd->opc];
142 }
143 }
144
145 const char *
146 nvme_cmd_string(const struct nvme_command *cmd, char *cmd_string, size_t len)
147 {
148 struct sbuf sb;
149 int error;
150
151 if (len == 0)
152 return ("");
153
154 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
155 nvme_cmd_sbuf(cmd, &sb);
156
157 error = sbuf_finish(&sb);
158 if (error != 0 &&
159 #ifdef _KERNEL
160 error != ENOMEM)
161 #else
162 errno != ENOMEM)
163 #endif
164 return ("");
165
166 return(sbuf_data(&sb));
167 }
168
169 void
170 nvme_cmd_sbuf(const struct nvme_command *cmd, struct sbuf *sb)
171 {
172
173 /*
174 * cid, rsvd areas and mptr not printed, since they are used
175 * only internally by the SIM.
176 */
177 sbuf_printf(sb,
178 "opc=%x fuse=%x nsid=%x prp1=%llx prp2=%llx cdw=%x %x %x %x %x %x",
179 cmd->opc, cmd->fuse, cmd->nsid,
180 (unsigned long long)cmd->prp1, (unsigned long long)cmd->prp2,
181 cmd->cdw10, cmd->cdw11, cmd->cdw12,
182 cmd->cdw13, cmd->cdw14, cmd->cdw15);
183 }
184
185 /*
186 * nvme_command_sbuf() returns 0 for success and -1 for failure.
187 */
188 int
189 nvme_command_sbuf(struct ccb_nvmeio *nvmeio, struct sbuf *sb)
190 {
191
192 sbuf_printf(sb, "%s. NCB: ", nvme_op_string(&nvmeio->cmd,
193 nvmeio->ccb_h.func_code == XPT_NVME_ADMIN));
194 nvme_cmd_sbuf(&nvmeio->cmd, sb);
195 return(0);
196 }
197
198 #ifdef _KERNEL
199 const void *
200 nvme_get_identify_cntrl(struct cam_periph *periph)
201 {
202 struct cam_ed *device;
203
204 device = periph->path->device;
205
206 return device->nvme_cdata;
207 }
208
209 const void *
210 nvme_get_identify_ns(struct cam_periph *periph)
211 {
212 struct cam_ed *device;
213
214 device = periph->path->device;
215
216 return device->nvme_data;
217 }
218 #endif
Cache object: c6483d908981d56ebae4d456ee2f93ad
|