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