2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "nvme_private.h"
33 nvme_ctrlr_cmd_identify_controller(struct nvme_controller
*ctrlr
, void *payload
,
34 nvme_cb_fn_t cb_fn
, void *cb_arg
)
36 struct nvme_request
*req
;
37 struct nvme_command
*cmd
;
39 req
= nvme_allocate_request_vaddr(payload
,
40 sizeof(struct nvme_controller_data
), cb_fn
, cb_arg
);
43 cmd
->opc
= NVME_OPC_IDENTIFY
;
46 * TODO: create an identify command data structure, which
47 * includes this CNS bit in cdw10.
51 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
55 nvme_ctrlr_cmd_identify_namespace(struct nvme_controller
*ctrlr
, uint16_t nsid
,
56 void *payload
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
58 struct nvme_request
*req
;
59 struct nvme_command
*cmd
;
61 req
= nvme_allocate_request_vaddr(payload
,
62 sizeof(struct nvme_namespace_data
), cb_fn
, cb_arg
);
65 cmd
->opc
= NVME_OPC_IDENTIFY
;
68 * TODO: create an identify command data structure
72 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
76 nvme_ctrlr_cmd_create_io_cq(struct nvme_controller
*ctrlr
,
77 struct nvme_qpair
*io_que
, uint16_t vector
, nvme_cb_fn_t cb_fn
,
80 struct nvme_request
*req
;
81 struct nvme_command
*cmd
;
83 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
86 cmd
->opc
= NVME_OPC_CREATE_IO_CQ
;
89 * TODO: create a create io completion queue command data
92 cmd
->cdw10
= ((io_que
->num_entries
-1) << 16) | io_que
->id
;
93 /* 0x3 = interrupts enabled | physically contiguous */
94 cmd
->cdw11
= (vector
<< 16) | 0x3;
95 cmd
->prp1
= io_que
->cpl_bus_addr
;
97 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
101 nvme_ctrlr_cmd_create_io_sq(struct nvme_controller
*ctrlr
,
102 struct nvme_qpair
*io_que
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
104 struct nvme_request
*req
;
105 struct nvme_command
*cmd
;
107 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
110 cmd
->opc
= NVME_OPC_CREATE_IO_SQ
;
113 * TODO: create a create io submission queue command data
116 cmd
->cdw10
= ((io_que
->num_entries
-1) << 16) | io_que
->id
;
117 /* 0x1 = physically contiguous */
118 cmd
->cdw11
= (io_que
->id
<< 16) | 0x1;
119 cmd
->prp1
= io_que
->cmd_bus_addr
;
121 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
125 nvme_ctrlr_cmd_delete_io_cq(struct nvme_controller
*ctrlr
,
126 struct nvme_qpair
*io_que
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
128 struct nvme_request
*req
;
129 struct nvme_command
*cmd
;
131 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
134 cmd
->opc
= NVME_OPC_DELETE_IO_CQ
;
137 * TODO: create a delete io completion queue command data
140 cmd
->cdw10
= io_que
->id
;
142 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
146 nvme_ctrlr_cmd_delete_io_sq(struct nvme_controller
*ctrlr
,
147 struct nvme_qpair
*io_que
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
149 struct nvme_request
*req
;
150 struct nvme_command
*cmd
;
152 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
155 cmd
->opc
= NVME_OPC_DELETE_IO_SQ
;
158 * TODO: create a delete io submission queue command data
161 cmd
->cdw10
= io_que
->id
;
163 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
167 nvme_ctrlr_cmd_set_feature(struct nvme_controller
*ctrlr
, uint8_t feature
,
168 uint32_t cdw11
, void *payload
, uint32_t payload_size
,
169 nvme_cb_fn_t cb_fn
, void *cb_arg
)
171 struct nvme_request
*req
;
172 struct nvme_command
*cmd
;
174 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
177 cmd
->opc
= NVME_OPC_SET_FEATURES
;
178 cmd
->cdw10
= feature
;
181 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
185 nvme_ctrlr_cmd_get_feature(struct nvme_controller
*ctrlr
, uint8_t feature
,
186 uint32_t cdw11
, void *payload
, uint32_t payload_size
,
187 nvme_cb_fn_t cb_fn
, void *cb_arg
)
189 struct nvme_request
*req
;
190 struct nvme_command
*cmd
;
192 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
195 cmd
->opc
= NVME_OPC_GET_FEATURES
;
196 cmd
->cdw10
= feature
;
199 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
203 nvme_ctrlr_cmd_set_num_queues(struct nvme_controller
*ctrlr
,
204 uint32_t num_queues
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
208 cdw11
= ((num_queues
- 1) << 16) | (num_queues
- 1);
209 nvme_ctrlr_cmd_set_feature(ctrlr
, NVME_FEAT_NUMBER_OF_QUEUES
, cdw11
,
210 NULL
, 0, cb_fn
, cb_arg
);
214 nvme_ctrlr_cmd_set_async_event_config(struct nvme_controller
*ctrlr
,
215 union nvme_critical_warning_state state
, nvme_cb_fn_t cb_fn
,
221 nvme_ctrlr_cmd_set_feature(ctrlr
,
222 NVME_FEAT_ASYNC_EVENT_CONFIGURATION
, cdw11
, NULL
, 0, cb_fn
,
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
)
232 if ((microseconds
/100) >= 0x100) {
233 nvme_printf(ctrlr
, "invalid coal time %d, disabling\n",
239 if (threshold
>= 0x100) {
240 nvme_printf(ctrlr
, "invalid threshold %d, disabling\n",
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
);
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
,
256 struct nvme_request
*req
;
257 struct nvme_command
*cmd
;
259 req
= nvme_allocate_request_vaddr(payload
, payload_size
, cb_fn
, cb_arg
);
262 cmd
->opc
= NVME_OPC_GET_LOG_PAGE
;
264 cmd
->cdw10
= ((payload_size
/sizeof(uint32_t)) - 1) << 16;
265 cmd
->cdw10
|= log_page
;
267 nvme_ctrlr_submit_admin_request(ctrlr
, req
);
271 nvme_ctrlr_cmd_get_error_page(struct nvme_controller
*ctrlr
,
272 struct nvme_error_information_entry
*payload
, uint32_t num_entries
,
273 nvme_cb_fn_t cb_fn
, void *cb_arg
)
276 KASSERT(num_entries
> 0, ("%s called with num_entries==0\n", __func__
));
278 /* Controller's error log page entries is 0-based. */
279 KASSERT(num_entries
<= (ctrlr
->cdata
.elpe
+ 1),
280 ("%s called with num_entries=%d but (elpe+1)=%d\n", __func__
,
281 num_entries
, ctrlr
->cdata
.elpe
+ 1));
283 if (num_entries
> (ctrlr
->cdata
.elpe
+ 1))
284 num_entries
= ctrlr
->cdata
.elpe
+ 1;
286 nvme_ctrlr_cmd_get_log_page(ctrlr
, NVME_LOG_ERROR
,
287 NVME_GLOBAL_NAMESPACE_TAG
, payload
, sizeof(*payload
) * num_entries
,
292 nvme_ctrlr_cmd_get_health_information_page(struct nvme_controller
*ctrlr
,
293 uint32_t nsid
, struct nvme_health_information_page
*payload
,
294 nvme_cb_fn_t cb_fn
, void *cb_arg
)
297 nvme_ctrlr_cmd_get_log_page(ctrlr
, NVME_LOG_HEALTH_INFORMATION
,
298 nsid
, payload
, sizeof(*payload
), cb_fn
, cb_arg
);
302 nvme_ctrlr_cmd_get_firmware_page(struct nvme_controller
*ctrlr
,
303 struct nvme_firmware_page
*payload
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
306 nvme_ctrlr_cmd_get_log_page(ctrlr
, NVME_LOG_FIRMWARE_SLOT
,
307 NVME_GLOBAL_NAMESPACE_TAG
, payload
, sizeof(*payload
), cb_fn
,
312 nvme_ctrlr_cmd_abort(struct nvme_controller
*ctrlr
, uint16_t cid
,
313 uint16_t sqid
, nvme_cb_fn_t cb_fn
, void *cb_arg
)
315 struct nvme_request
*req
;
316 struct nvme_command
*cmd
;
318 req
= nvme_allocate_request_null(cb_fn
, cb_arg
);
321 cmd
->opc
= NVME_OPC_ABORT
;
322 cmd
->cdw10
= (cid
<< 16) | sqid
;
324 nvme_ctrlr_submit_admin_request(ctrlr
, req
);