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