2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2012 Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "nvme_private.h"
35 nvme_ns_cmd_read(struct nvme_namespace *ns, void *payload, uint64_t lba,
36 uint32_t lba_count, nvme_cb_fn_t cb_fn, void *cb_arg)
38 struct nvme_request *req;
40 req = nvme_allocate_request_vaddr(payload,
41 lba_count*nvme_ns_get_sector_size(ns), cb_fn, cb_arg);
46 nvme_ns_read_cmd(&req->cmd, ns->id, lba, lba_count);
48 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
54 nvme_ns_cmd_read_bio(struct nvme_namespace *ns, struct bio *bp,
55 nvme_cb_fn_t cb_fn, void *cb_arg)
57 struct nvme_request *req;
61 req = nvme_allocate_request_bio(bp, cb_fn, cb_arg);
66 lba = bp->bio_offset / nvme_ns_get_sector_size(ns);
67 lba_count = bp->bio_bcount / nvme_ns_get_sector_size(ns);
68 nvme_ns_read_cmd(&req->cmd, ns->id, lba, lba_count);
70 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
76 nvme_ns_cmd_write(struct nvme_namespace *ns, void *payload, uint64_t lba,
77 uint32_t lba_count, nvme_cb_fn_t cb_fn, void *cb_arg)
79 struct nvme_request *req;
81 req = nvme_allocate_request_vaddr(payload,
82 lba_count*nvme_ns_get_sector_size(ns), cb_fn, cb_arg);
87 nvme_ns_write_cmd(&req->cmd, ns->id, lba, lba_count);
89 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
95 nvme_ns_cmd_write_bio(struct nvme_namespace *ns, struct bio *bp,
96 nvme_cb_fn_t cb_fn, void *cb_arg)
98 struct nvme_request *req;
102 req = nvme_allocate_request_bio(bp, cb_fn, cb_arg);
106 lba = bp->bio_offset / nvme_ns_get_sector_size(ns);
107 lba_count = bp->bio_bcount / nvme_ns_get_sector_size(ns);
108 nvme_ns_write_cmd(&req->cmd, ns->id, lba, lba_count);
110 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
116 nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload,
117 uint8_t num_ranges, nvme_cb_fn_t cb_fn, void *cb_arg)
119 struct nvme_request *req;
120 struct nvme_command *cmd;
122 req = nvme_allocate_request_vaddr(payload,
123 num_ranges * sizeof(struct nvme_dsm_range), cb_fn, cb_arg);
129 cmd->opc = NVME_OPC_DATASET_MANAGEMENT;
130 cmd->nsid = htole32(ns->id);
132 /* TODO: create a delete command data structure */
133 cmd->cdw10 = htole32(num_ranges - 1);
134 cmd->cdw11 = htole32(NVME_DSM_ATTR_DEALLOCATE);
136 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
142 nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn, void *cb_arg)
144 struct nvme_request *req;
146 req = nvme_allocate_request_null(cb_fn, cb_arg);
151 nvme_ns_flush_cmd(&req->cmd, ns->id);
152 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
157 /* Timeout = 1 sec */
158 #define NVD_DUMP_TIMEOUT 200000
161 nvme_ns_dump(struct nvme_namespace *ns, void *virt, off_t offset, size_t len)
163 struct nvme_completion_poll_status status;
164 struct nvme_request *req;
165 struct nvme_command *cmd;
166 uint64_t lba, lba_count;
170 req = nvme_allocate_request_vaddr(virt, len, nvme_completion_poll_cb,
178 lba = offset / nvme_ns_get_sector_size(ns);
179 lba_count = len / nvme_ns_get_sector_size(ns);
180 nvme_ns_write_cmd(cmd, ns->id, lba, lba_count);
182 nvme_ns_flush_cmd(cmd, ns->id);
184 nvme_ctrlr_submit_io_request(ns->ctrlr, req);
185 if (req->qpair == NULL)
189 while ((i++ < NVD_DUMP_TIMEOUT) && (status.done == FALSE)) {
191 nvme_qpair_process_completions(req->qpair);
195 * Normally, when using the polling interface, we can't return a
196 * timeout error because we don't know when the completion routines
197 * will be called if the command later completes. However, in this
198 * case we're running a system dump, so all interrupts are turned
199 * off, the scheduler isn't running so there's nothing to complete
202 if (status.done == FALSE)