xref: /freebsd/sys/dev/nvme/nvme_ctrlr_cmd.c (revision f18976136625a7d016e97bfd9eabddf640b3e06d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2012-2013 Intel Corporation
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  * 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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "nvme_private.h"
33 
34 void
35 nvme_ctrlr_cmd_identify_controller(struct nvme_controller *ctrlr, void *payload,
36 	nvme_cb_fn_t cb_fn, void *cb_arg)
37 {
38 	struct nvme_request *req;
39 	struct nvme_command *cmd;
40 
41 	req = nvme_allocate_request_vaddr(payload,
42 	    sizeof(struct nvme_controller_data), cb_fn, cb_arg);
43 
44 	cmd = &req->cmd;
45 	cmd->opc = NVME_OPC_IDENTIFY;
46 
47 	/*
48 	 * TODO: create an identify command data structure, which
49 	 *  includes this CNS bit in cdw10.
50 	 */
51 	cmd->cdw10 = htole32(1);
52 
53 	nvme_ctrlr_submit_admin_request(ctrlr, req);
54 }
55 
56 void
57 nvme_ctrlr_cmd_identify_namespace(struct nvme_controller *ctrlr, uint32_t nsid,
58 	void *payload, nvme_cb_fn_t cb_fn, void *cb_arg)
59 {
60 	struct nvme_request *req;
61 	struct nvme_command *cmd;
62 
63 	req = nvme_allocate_request_vaddr(payload,
64 	    sizeof(struct nvme_namespace_data), cb_fn, cb_arg);
65 
66 	cmd = &req->cmd;
67 	cmd->opc = NVME_OPC_IDENTIFY;
68 
69 	/*
70 	 * TODO: create an identify command data structure
71 	 */
72 	cmd->nsid = htole32(nsid);
73 
74 	nvme_ctrlr_submit_admin_request(ctrlr, req);
75 }
76 
77 void
78 nvme_ctrlr_cmd_create_io_cq(struct nvme_controller *ctrlr,
79     struct nvme_qpair *io_que, nvme_cb_fn_t cb_fn, void *cb_arg)
80 {
81 	struct nvme_request *req;
82 	struct nvme_command *cmd;
83 
84 	req = nvme_allocate_request_null(cb_fn, cb_arg);
85 
86 	cmd = &req->cmd;
87 	cmd->opc = NVME_OPC_CREATE_IO_CQ;
88 
89 	/*
90 	 * TODO: create a create io completion queue command data
91 	 *  structure.
92 	 */
93 	cmd->cdw10 = htole32(((io_que->num_entries-1) << 16) | io_que->id);
94 	/* 0x3 = interrupts enabled | physically contiguous */
95 	cmd->cdw11 = htole32((io_que->vector << 16) | 0x3);
96 	cmd->prp1 = htole64(io_que->cpl_bus_addr);
97 
98 	nvme_ctrlr_submit_admin_request(ctrlr, req);
99 }
100 
101 void
102 nvme_ctrlr_cmd_create_io_sq(struct nvme_controller *ctrlr,
103     struct nvme_qpair *io_que, nvme_cb_fn_t cb_fn, void *cb_arg)
104 {
105 	struct nvme_request *req;
106 	struct nvme_command *cmd;
107 
108 	req = nvme_allocate_request_null(cb_fn, cb_arg);
109 
110 	cmd = &req->cmd;
111 	cmd->opc = NVME_OPC_CREATE_IO_SQ;
112 
113 	/*
114 	 * TODO: create a create io submission queue command data
115 	 *  structure.
116 	 */
117 	cmd->cdw10 = htole32(((io_que->num_entries-1) << 16) | io_que->id);
118 	/* 0x1 = physically contiguous */
119 	cmd->cdw11 = htole32((io_que->id << 16) | 0x1);
120 	cmd->prp1 = htole64(io_que->cmd_bus_addr);
121 
122 	nvme_ctrlr_submit_admin_request(ctrlr, req);
123 }
124 
125 void
126 nvme_ctrlr_cmd_delete_io_cq(struct nvme_controller *ctrlr,
127     struct nvme_qpair *io_que, nvme_cb_fn_t cb_fn, void *cb_arg)
128 {
129 	struct nvme_request *req;
130 	struct nvme_command *cmd;
131 
132 	req = nvme_allocate_request_null(cb_fn, cb_arg);
133 
134 	cmd = &req->cmd;
135 	cmd->opc = NVME_OPC_DELETE_IO_CQ;
136 
137 	/*
138 	 * TODO: create a delete io completion queue command data
139 	 *  structure.
140 	 */
141 	cmd->cdw10 = htole32(io_que->id);
142 
143 	nvme_ctrlr_submit_admin_request(ctrlr, req);
144 }
145 
146 void
147 nvme_ctrlr_cmd_delete_io_sq(struct nvme_controller *ctrlr,
148     struct nvme_qpair *io_que, nvme_cb_fn_t cb_fn, void *cb_arg)
149 {
150 	struct nvme_request *req;
151 	struct nvme_command *cmd;
152 
153 	req = nvme_allocate_request_null(cb_fn, cb_arg);
154 
155 	cmd = &req->cmd;
156 	cmd->opc = NVME_OPC_DELETE_IO_SQ;
157 
158 	/*
159 	 * TODO: create a delete io submission queue command data
160 	 *  structure.
161 	 */
162 	cmd->cdw10 = htole32(io_que->id);
163 
164 	nvme_ctrlr_submit_admin_request(ctrlr, req);
165 }
166 
167 void
168 nvme_ctrlr_cmd_set_feature(struct nvme_controller *ctrlr, uint8_t feature,
169     uint32_t cdw11, void *payload, uint32_t payload_size,
170     nvme_cb_fn_t cb_fn, void *cb_arg)
171 {
172 	struct nvme_request *req;
173 	struct nvme_command *cmd;
174 
175 	req = nvme_allocate_request_null(cb_fn, cb_arg);
176 
177 	cmd = &req->cmd;
178 	cmd->opc = NVME_OPC_SET_FEATURES;
179 	cmd->cdw10 = htole32(feature);
180 	cmd->cdw11 = htole32(cdw11);
181 
182 	nvme_ctrlr_submit_admin_request(ctrlr, req);
183 }
184 
185 void
186 nvme_ctrlr_cmd_get_feature(struct nvme_controller *ctrlr, uint8_t feature,
187     uint32_t cdw11, void *payload, uint32_t payload_size,
188     nvme_cb_fn_t cb_fn, void *cb_arg)
189 {
190 	struct nvme_request *req;
191 	struct nvme_command *cmd;
192 
193 	req = nvme_allocate_request_null(cb_fn, cb_arg);
194 
195 	cmd = &req->cmd;
196 	cmd->opc = NVME_OPC_GET_FEATURES;
197 	cmd->cdw10 = htole32(feature);
198 	cmd->cdw11 = htole32(cdw11);
199 
200 	nvme_ctrlr_submit_admin_request(ctrlr, req);
201 }
202 
203 void
204 nvme_ctrlr_cmd_set_num_queues(struct nvme_controller *ctrlr,
205     uint32_t num_queues, nvme_cb_fn_t cb_fn, void *cb_arg)
206 {
207 	uint32_t cdw11;
208 
209 	cdw11 = ((num_queues - 1) << 16) | (num_queues - 1);
210 	nvme_ctrlr_cmd_set_feature(ctrlr, NVME_FEAT_NUMBER_OF_QUEUES, cdw11,
211 	    NULL, 0, cb_fn, cb_arg);
212 }
213 
214 void
215 nvme_ctrlr_cmd_set_async_event_config(struct nvme_controller *ctrlr,
216     uint32_t state, nvme_cb_fn_t cb_fn, void *cb_arg)
217 {
218 	uint32_t cdw11;
219 
220 	cdw11 = state;
221 	nvme_ctrlr_cmd_set_feature(ctrlr,
222 	    NVME_FEAT_ASYNC_EVENT_CONFIGURATION, cdw11, NULL, 0, cb_fn,
223 	    cb_arg);
224 }
225 
226 void
227 nvme_ctrlr_cmd_set_interrupt_coalescing(struct nvme_controller *ctrlr,
228     uint32_t microseconds, uint32_t threshold, nvme_cb_fn_t cb_fn, void *cb_arg)
229 {
230 	uint32_t cdw11;
231 
232 	if ((microseconds/100) >= 0x100) {
233 		nvme_printf(ctrlr, "invalid coal time %d, disabling\n",
234 		    microseconds);
235 		microseconds = 0;
236 		threshold = 0;
237 	}
238 
239 	if (threshold >= 0x100) {
240 		nvme_printf(ctrlr, "invalid threshold %d, disabling\n",
241 		    threshold);
242 		threshold = 0;
243 		microseconds = 0;
244 	}
245 
246 	cdw11 = ((microseconds/100) << 8) | threshold;
247 	nvme_ctrlr_cmd_set_feature(ctrlr, NVME_FEAT_INTERRUPT_COALESCING, cdw11,
248 	    NULL, 0, cb_fn, cb_arg);
249 }
250 
251 void
252 nvme_ctrlr_cmd_get_log_page(struct nvme_controller *ctrlr, uint8_t log_page,
253     uint32_t nsid, void *payload, uint32_t payload_size, nvme_cb_fn_t cb_fn,
254     void *cb_arg)
255 {
256 	struct nvme_request *req;
257 	struct nvme_command *cmd;
258 
259 	req = nvme_allocate_request_vaddr(payload, payload_size, cb_fn, cb_arg);
260 
261 	cmd = &req->cmd;
262 	cmd->opc = NVME_OPC_GET_LOG_PAGE;
263 	cmd->nsid = htole32(nsid);
264 	cmd->cdw10 = ((payload_size/sizeof(uint32_t)) - 1) << 16;
265 	cmd->cdw10 |= log_page;
266 	cmd->cdw10 = htole32(cmd->cdw10);
267 
268 	nvme_ctrlr_submit_admin_request(ctrlr, req);
269 }
270 
271 void
272 nvme_ctrlr_cmd_get_error_page(struct nvme_controller *ctrlr,
273     struct nvme_error_information_entry *payload, uint32_t num_entries,
274     nvme_cb_fn_t cb_fn, void *cb_arg)
275 {
276 
277 	KASSERT(num_entries > 0, ("%s called with num_entries==0\n", __func__));
278 
279 	/* Controller's error log page entries is 0-based. */
280 	KASSERT(num_entries <= (ctrlr->cdata.elpe + 1),
281 	    ("%s called with num_entries=%d but (elpe+1)=%d\n", __func__,
282 	    num_entries, ctrlr->cdata.elpe + 1));
283 
284 	if (num_entries > (ctrlr->cdata.elpe + 1))
285 		num_entries = ctrlr->cdata.elpe + 1;
286 
287 	nvme_ctrlr_cmd_get_log_page(ctrlr, NVME_LOG_ERROR,
288 	    NVME_GLOBAL_NAMESPACE_TAG, payload, sizeof(*payload) * num_entries,
289 	    cb_fn, cb_arg);
290 }
291 
292 void
293 nvme_ctrlr_cmd_get_health_information_page(struct nvme_controller *ctrlr,
294     uint32_t nsid, struct nvme_health_information_page *payload,
295     nvme_cb_fn_t cb_fn, void *cb_arg)
296 {
297 
298 	nvme_ctrlr_cmd_get_log_page(ctrlr, NVME_LOG_HEALTH_INFORMATION,
299 	    nsid, payload, sizeof(*payload), cb_fn, cb_arg);
300 }
301 
302 void
303 nvme_ctrlr_cmd_get_firmware_page(struct nvme_controller *ctrlr,
304     struct nvme_firmware_page *payload, nvme_cb_fn_t cb_fn, void *cb_arg)
305 {
306 
307 	nvme_ctrlr_cmd_get_log_page(ctrlr, NVME_LOG_FIRMWARE_SLOT,
308 	    NVME_GLOBAL_NAMESPACE_TAG, payload, sizeof(*payload), cb_fn,
309 	    cb_arg);
310 }
311 
312 void
313 nvme_ctrlr_cmd_abort(struct nvme_controller *ctrlr, uint16_t cid,
314     uint16_t sqid, nvme_cb_fn_t cb_fn, void *cb_arg)
315 {
316 	struct nvme_request *req;
317 	struct nvme_command *cmd;
318 
319 	req = nvme_allocate_request_null(cb_fn, cb_arg);
320 
321 	cmd = &req->cmd;
322 	cmd->opc = NVME_OPC_ABORT;
323 	cmd->cdw10 = htole32((cid << 16) | sqid);
324 
325 	nvme_ctrlr_submit_admin_request(ctrlr, req);
326 }
327