1 /*	$NetBSD: nvmereg.h,v 1.1 2016/05/01 10:21:02 nonaka Exp $	*/
2 /*	$OpenBSD: nvmereg.h,v 1.10 2016/04/14 11:18:32 dlg Exp $ */
3 
4 /*
5  * Copyright (c) 2014 David Gwynne <dlg@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 
22 struct nvme_sge {
23 	uint8_t		id;
24 	uint8_t		_reserved[15];
25 } __packed __aligned(8);
26 
27 struct nvme_sqe {
28 	uint8_t		opcode;
29 	uint8_t		flags;
30 	uint16_t	cid;
31 
32 	uint32_t	nsid;
33 
34 	uint8_t		_reserved[8];
35 
36 	uint64_t	mptr;
37 
38 	union {
39 		uint64_t	prp[2];
40 		struct nvme_sge	sge;
41 	} __packed	entry;
42 
43 	uint32_t	cdw10;
44 	uint32_t	cdw11;
45 	uint32_t	cdw12;
46 	uint32_t	cdw13;
47 	uint32_t	cdw14;
48 	uint32_t	cdw15;
49 } __packed __aligned(8);
50 
51 struct nvme_cqe {
52 	uint32_t	cdw0;
53 
54 	uint32_t	_reserved;
55 
56 	uint16_t	sqhd; /* SQ Head Pointer */
57 	uint16_t	sqid; /* SQ Identifier */
58 
59 	uint16_t	cid; /* Command Identifier */
60 	uint16_t	flags;
61 #define NVME_CQE_DNR		__BIT(15)
62 #define NVME_CQE_M		__BIT(14)
63 #define NVME_CQE_SCT(_f)	((_f) & (0x07 << 8))
64 #define  NVME_CQE_SCT_GENERIC		(0x00 << 8)
65 #define  NVME_CQE_SCT_COMMAND		(0x01 << 8)
66 #define  NVME_CQE_SCT_MEDIAERR		(0x02 << 8)
67 #define  NVME_CQE_SCT_VENDOR		(0x07 << 8)
68 #define NVME_CQE_SC(_f)		((_f) & (0x7f << 1))
69 #define  NVME_CQE_SC_SUCCESS		(0x00 << 1)
70 #define  NVME_CQE_SC_INVALID_OPCODE	(0x01 << 1)
71 #define  NVME_CQE_SC_INVALID_FIELD	(0x02 << 1)
72 #define  NVME_CQE_SC_CID_CONFLICT	(0x03 << 1)
73 #define  NVME_CQE_SC_DATA_XFER_ERR	(0x04 << 1)
74 #define  NVME_CQE_SC_ABRT_BY_NO_PWR	(0x05 << 1)
75 #define  NVME_CQE_SC_INTERNAL_DEV_ERR	(0x06 << 1)
76 #define  NVME_CQE_SC_CMD_ABRT_REQD	(0x07 << 1)
77 #define  NVME_CQE_SC_CMD_ABDR_SQ_DEL	(0x08 << 1)
78 #define  NVME_CQE_SC_CMD_ABDR_FUSE_ERR	(0x09 << 1)
79 #define  NVME_CQE_SC_CMD_ABDR_FUSE_MISS	(0x0a << 1)
80 #define  NVME_CQE_SC_INVALID_NS		(0x0b << 1)
81 #define  NVME_CQE_SC_CMD_SEQ_ERR	(0x0c << 1)
82 #define  NVME_CQE_SC_INVALID_LAST_SGL	(0x0d << 1)
83 #define  NVME_CQE_SC_INVALID_NUM_SGL	(0x0e << 1)
84 #define  NVME_CQE_SC_DATA_SGL_LEN	(0x0f << 1)
85 #define  NVME_CQE_SC_MDATA_SGL_LEN	(0x10 << 1)
86 #define  NVME_CQE_SC_SGL_TYPE_INVALID	(0x11 << 1)
87 #define  NVME_CQE_SC_LBA_RANGE		(0x80 << 1)
88 #define  NVME_CQE_SC_CAP_EXCEEDED	(0x81 << 1)
89 #define  NVME_CQE_NS_NOT_RDY		(0x82 << 1)
90 #define  NVME_CQE_RSV_CONFLICT		(0x83 << 1)
91 #define NVME_CQE_PHASE		__BIT(0)
92 } __packed __aligned(8);
93 
94 /*-
95  * Copyright (C) 2012-2013 Intel Corporation
96  * All rights reserved.
97  *
98  * Redistribution and use in source and binary forms, with or without
99  * modification, are permitted provided that the following conditions
100  * are met:
101  * 1. Redistributions of source code must retain the above copyright
102  *    notice, this list of conditions and the following disclaimer.
103  * 2. Redistributions in binary form must reproduce the above copyright
104  *    notice, this list of conditions and the following disclaimer in the
105  *    documentation and/or other materials provided with the distribution.
106  *
107  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
108  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
109  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
110  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
111  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
112  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
113  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
114  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
115  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
116  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
117  * SUCH DAMAGE.
118  *
119  * $FreeBSD$
120  */
121 
122 #define	NVME_PASSTHROUGH_CMD		_IOWR('n', 0, struct nvme_pt_command)
123 
124 #define	nvme_completion_is_error(cpl)					\
125 	((NVME_CQE_SC((cpl)->flags) != NVME_CQE_SC_SUCCESS)   		\
126 	    || (NVME_CQE_SCT((cpl)->flags) != NVME_CQE_SCT_GENERIC))
127 
128 struct nvme_pt_command {
129 
130 	/*
131 	 * cmd is used to specify a passthrough command to a controller or
132 	 *  namespace.
133 	 *
134 	 * The following fields from cmd may be specified by the caller:
135 	 *	* opcode
136 	 *	* nsid (namespace id) - for admin commands only
137 	 *	* cdw10-cdw15
138 	 *
139 	 * Remaining fields must be set to 0 by the caller.
140 	 */
141 	struct nvme_sqe		cmd;
142 
143 	/*
144 	 * cpl returns completion status for the passthrough command
145 	 *  specified by cmd.
146 	 *
147 	 * The following fields will be filled out by the driver, for
148 	 *  consumption by the caller:
149 	 *	* cdw0
150 	 *	* flags (except for phase)
151 	 *
152 	 * Remaining fields will be set to 0 by the driver.
153 	 */
154 	struct nvme_cqe		cpl;
155 
156 	/* buf is the data buffer associated with this passthrough command. */
157 	void			*buf;
158 
159 	/*
160 	 * len is the length of the data buffer associated with this
161 	 *  passthrough command.
162 	 */
163 	uint32_t		len;
164 
165 	/*
166 	 * is_read = 1 if the passthrough command will read data into the
167 	 *  supplied buffer from the controller.
168 	 *
169 	 * is_read = 0 if the passthrough command will write data from the
170 	 *  supplied buffer to the controller.
171 	 */
172 	uint32_t		is_read;
173 
174 	/*
175 	 * timeout (unit: ms)
176 	 *
177 	 * 0: use default timeout value
178 	 */
179 	uint32_t		timeout;
180 };
181 
182 #define NVME_PREFIX		"/dev/nvme"
183 #define NVME_NS_PREFIX		"ns"
184