1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 *    without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 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
66void
67nvme_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
82int
83nvme_identify_match(caddr_t identbuffer, caddr_t table_entry)
84{
85	return 0;
86}
87
88
89void
90nvme_print_ident(const struct nvme_controller_data *cdata,
91    const struct nvme_namespace_data *data, struct sbuf *sb)
92{
93
94	sbuf_printf(sb, "<");
95	cam_strvis_sbuf(sb, cdata->mn, sizeof(cdata->mn), 0);
96	sbuf_printf(sb, " ");
97	cam_strvis_sbuf(sb, cdata->fr, sizeof(cdata->fr), 0);
98	sbuf_printf(sb, " ");
99	cam_strvis_sbuf(sb, cdata->sn, sizeof(cdata->sn), 0);
100	sbuf_printf(sb, ">\n");
101}
102
103/* XXX need to do nvme admin opcodes too, but those aren't used yet by nda */
104static const char *
105nvme_opc2str[] = {
106	"FLUSH",
107	"WRITE",
108	"READ",
109	"RSVD-3",
110	"WRITE_UNCORRECTABLE",
111	"COMPARE",
112	"RSVD-6",
113	"RSVD-7",
114	"WRITE_ZEROES",
115	"DATASET_MANAGEMENT",
116	"RSVD-a",
117	"RSVD-b",
118	"RSVD-c",
119	"RESERVATION_REGISTER",
120	"RESERVATION_REPORT",
121	"RSVD-f",
122	"RSVD-10",
123	"RESERVATION_ACQUIRE",
124	"RSVD-12",
125	"RSVD-13",
126	"RSVD-14",
127	"RESERVATION_RELEASE",
128};
129
130const char *
131nvme_op_string(const struct nvme_command *cmd, int admin)
132{
133
134	if (admin) {
135		return "ADMIN";
136	} else {
137		if (cmd->opc >= nitems(nvme_opc2str))
138			return "UNKNOWN";
139		return nvme_opc2str[cmd->opc];
140	}
141}
142
143const char *
144nvme_cmd_string(const struct nvme_command *cmd, char *cmd_string, size_t len)
145{
146	struct sbuf sb;
147	int error;
148
149	if (len == 0)
150		return ("");
151
152	sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
153	nvme_cmd_sbuf(cmd, &sb);
154
155	error = sbuf_finish(&sb);
156	if (error != 0 &&
157#ifdef _KERNEL
158	    error != ENOMEM)
159#else
160	    errno != ENOMEM)
161#endif
162		return ("");
163
164	return(sbuf_data(&sb));
165}
166
167void
168nvme_cmd_sbuf(const struct nvme_command *cmd, struct sbuf *sb)
169{
170
171	/*
172	 * cid, rsvd areas and mptr not printed, since they are used
173	 * only internally by the SIM.
174	 */
175	sbuf_printf(sb,
176	    "opc=%x fuse=%x nsid=%x prp1=%llx prp2=%llx cdw=%x %x %x %x %x %x",
177	    cmd->opc, cmd->fuse, cmd->nsid,
178	    (unsigned long long)cmd->prp1, (unsigned long long)cmd->prp2,
179	    cmd->cdw10, cmd->cdw11, cmd->cdw12,
180	    cmd->cdw13, cmd->cdw14, cmd->cdw15);
181}
182
183/*
184 * nvme_command_sbuf() returns 0 for success and -1 for failure.
185 */
186int
187nvme_command_sbuf(struct ccb_nvmeio *nvmeio, struct sbuf *sb)
188{
189
190	sbuf_printf(sb, "%s. NCB: ", nvme_op_string(&nvmeio->cmd,
191	    nvmeio->ccb_h.func_code == XPT_NVME_ADMIN));
192	nvme_cmd_sbuf(&nvmeio->cmd, sb);
193	return(0);
194}
195
196#ifdef _KERNEL
197const void *
198nvme_get_identify_cntrl(struct cam_periph *periph)
199{
200	struct cam_ed *device;
201
202	device = periph->path->device;
203
204	return device->nvme_cdata;
205}
206
207const void *
208nvme_get_identify_ns(struct cam_periph *periph)
209{
210	struct cam_ed *device;
211
212	device = periph->path->device;
213
214	return device->nvme_data;
215}
216#endif
217