xref: /freebsd/sys/dev/nvme/nvme_ns_cmd.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
1 /*-
2  * Copyright (C) 2012 Intel Corporation
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  * 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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "nvme_private.h"
31 
32 int
33 nvme_ns_cmd_read(struct nvme_namespace *ns, void *payload, uint64_t lba,
34     uint32_t lba_count, nvme_cb_fn_t cb_fn, void *cb_arg)
35 {
36 	struct nvme_request	*req;
37 	struct nvme_command	*cmd;
38 
39 	req = nvme_allocate_request_vaddr(payload, lba_count*512, cb_fn, cb_arg);
40 
41 	if (req == NULL)
42 		return (ENOMEM);
43 	cmd = &req->cmd;
44 	cmd->opc = NVME_OPC_READ;
45 	cmd->nsid = ns->id;
46 
47 	/* TODO: create a read command data structure */
48 	*(uint64_t *)&cmd->cdw10 = lba;
49 	cmd->cdw12 = lba_count-1;
50 
51 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
52 
53 	return (0);
54 }
55 
56 int
57 nvme_ns_cmd_read_bio(struct nvme_namespace *ns, struct bio *bp,
58     nvme_cb_fn_t cb_fn, void *cb_arg)
59 {
60 	struct nvme_request	*req;
61 	struct nvme_command	*cmd;
62 	uint64_t		lba;
63 	uint64_t		lba_count;
64 
65 	req = nvme_allocate_request_bio(bp, cb_fn, cb_arg);
66 
67 	if (req == NULL)
68 		return (ENOMEM);
69 	cmd = &req->cmd;
70 	cmd->opc = NVME_OPC_READ;
71 	cmd->nsid = ns->id;
72 
73 	lba = bp->bio_offset / nvme_ns_get_sector_size(ns);
74 	lba_count = bp->bio_bcount / nvme_ns_get_sector_size(ns);
75 
76 	/* TODO: create a read command data structure */
77 	*(uint64_t *)&cmd->cdw10 = lba;
78 	cmd->cdw12 = lba_count-1;
79 
80 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
81 
82 	return (0);
83 }
84 
85 int
86 nvme_ns_cmd_write(struct nvme_namespace *ns, void *payload, uint64_t lba,
87     uint32_t lba_count, nvme_cb_fn_t cb_fn, void *cb_arg)
88 {
89 	struct nvme_request	*req;
90 	struct nvme_command	*cmd;
91 
92 	req = nvme_allocate_request_vaddr(payload, lba_count*512, cb_fn,
93 	    cb_arg);
94 
95 	if (req == NULL)
96 		return (ENOMEM);
97 
98 	cmd = &req->cmd;
99 	cmd->opc = NVME_OPC_WRITE;
100 	cmd->nsid = ns->id;
101 
102 	/* TODO: create a write command data structure */
103 	*(uint64_t *)&cmd->cdw10 = lba;
104 	cmd->cdw12 = lba_count-1;
105 
106 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
107 
108 	return (0);
109 }
110 
111 int
112 nvme_ns_cmd_write_bio(struct nvme_namespace *ns, struct bio *bp,
113     nvme_cb_fn_t cb_fn, void *cb_arg)
114 {
115 	struct nvme_request	*req;
116 	struct nvme_command	*cmd;
117 	uint64_t		lba;
118 	uint64_t		lba_count;
119 
120 	req = nvme_allocate_request_bio(bp, cb_fn, cb_arg);
121 
122 	if (req == NULL)
123 		return (ENOMEM);
124 	cmd = &req->cmd;
125 	cmd->opc = NVME_OPC_WRITE;
126 	cmd->nsid = ns->id;
127 
128 	lba = bp->bio_offset / nvme_ns_get_sector_size(ns);
129 	lba_count = bp->bio_bcount / nvme_ns_get_sector_size(ns);
130 
131 	/* TODO: create a write command data structure */
132 	*(uint64_t *)&cmd->cdw10 = lba;
133 	cmd->cdw12 = lba_count-1;
134 
135 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
136 
137 	return (0);
138 }
139 
140 int
141 nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload,
142     uint8_t num_ranges, nvme_cb_fn_t cb_fn, void *cb_arg)
143 {
144 	struct nvme_request	*req;
145 	struct nvme_command	*cmd;
146 
147 	req = nvme_allocate_request_vaddr(payload,
148 	    num_ranges * sizeof(struct nvme_dsm_range), cb_fn, cb_arg);
149 
150 	if (req == NULL)
151 		return (ENOMEM);
152 
153 	cmd = &req->cmd;
154 	cmd->opc = NVME_OPC_DATASET_MANAGEMENT;
155 	cmd->nsid = ns->id;
156 
157 	/* TODO: create a delete command data structure */
158 	cmd->cdw10 = num_ranges - 1;
159 	cmd->cdw11 = NVME_DSM_ATTR_DEALLOCATE;
160 
161 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
162 
163 	return (0);
164 }
165 
166 int
167 nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn, void *cb_arg)
168 {
169 	struct nvme_request	*req;
170 	struct nvme_command	*cmd;
171 
172 	req = nvme_allocate_request_null(cb_fn, cb_arg);
173 
174 	if (req == NULL)
175 		return (ENOMEM);
176 
177 	cmd = &req->cmd;
178 	cmd->opc = NVME_OPC_FLUSH;
179 	cmd->nsid = ns->id;
180 
181 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
182 
183 	return (0);
184 }
185