xref: /freebsd/sys/dev/nvme/nvme_ns_cmd.c (revision c6ec7d31830ab1c80edae95ad5e4b9dba10c47ac)
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(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_write(struct nvme_namespace *ns, void *payload, uint64_t lba,
58     uint32_t lba_count, 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(payload, lba_count*512, cb_fn, cb_arg);
64 
65 	if (req == NULL)
66 		return (ENOMEM);
67 
68 	cmd = &req->cmd;
69 	cmd->opc = NVME_OPC_WRITE;
70 	cmd->nsid = ns->id;
71 
72 	/* TODO: create a write command data structure */
73 	*(uint64_t *)&cmd->cdw10 = lba;
74 	cmd->cdw12 = lba_count-1;
75 
76 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
77 
78 	return (0);
79 }
80 
81 int
82 nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload,
83     uint8_t num_ranges, nvme_cb_fn_t cb_fn, void *cb_arg)
84 {
85 	struct nvme_request	*req;
86 	struct nvme_command	*cmd;
87 
88 	req = nvme_allocate_request(payload,
89 	    num_ranges * sizeof(struct nvme_dsm_range), cb_fn, cb_arg);
90 
91 	if (req == NULL)
92 		return (ENOMEM);
93 
94 	cmd = &req->cmd;
95 	cmd->opc = NVME_OPC_DATASET_MANAGEMENT;
96 	cmd->nsid = ns->id;
97 
98 	/* TODO: create a delete command data structure */
99 	cmd->cdw10 = num_ranges;
100 	cmd->cdw11 = NVME_DSM_ATTR_DEALLOCATE;
101 
102 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
103 
104 	return (0);
105 }
106 
107 int
108 nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn, void *cb_arg)
109 {
110 	struct nvme_request	*req;
111 	struct nvme_command	*cmd;
112 
113 	req = nvme_allocate_request(NULL, 0, cb_fn, cb_arg);
114 
115 	if (req == NULL)
116 		return (ENOMEM);
117 
118 	cmd = &req->cmd;
119 	cmd->opc = NVME_OPC_FLUSH;
120 	cmd->nsid = ns->id;
121 
122 	nvme_ctrlr_submit_io_request(ns->ctrlr, req);
123 
124 	return (0);
125 }
126