]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/nvme/host/core.c
nvme: split __nvme_submit_sync_cmd
[mirror_ubuntu-artful-kernel.git] / drivers / nvme / host / core.c
CommitLineData
21d34711
CH
1/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/types.h>
21
22#include "nvme.h"
23
4160982e
CH
24struct request *nvme_alloc_request(struct request_queue *q,
25 struct nvme_command *cmd, unsigned int flags)
21d34711
CH
26{
27 bool write = cmd->common.opcode & 1;
21d34711 28 struct request *req;
21d34711 29
4160982e 30 req = blk_mq_alloc_request(q, write, flags);
21d34711 31 if (IS_ERR(req))
4160982e 32 return req;
21d34711
CH
33
34 req->cmd_type = REQ_TYPE_DRV_PRIV;
35 req->cmd_flags |= REQ_FAILFAST_DRIVER;
36 req->__data_len = 0;
37 req->__sector = (sector_t) -1;
38 req->bio = req->biotail = NULL;
39
21d34711
CH
40 req->cmd = (unsigned char *)cmd;
41 req->cmd_len = sizeof(struct nvme_command);
42 req->special = (void *)0;
43
4160982e
CH
44 return req;
45}
46
47/*
48 * Returns 0 on success. If the result is negative, it's a Linux error code;
49 * if the result is positive, it's an NVM Express status code
50 */
51int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
52 void *buffer, unsigned bufflen, u32 *result, unsigned timeout)
53{
54 struct request *req;
55 int ret;
56
57 req = nvme_alloc_request(q, cmd, 0);
58 if (IS_ERR(req))
59 return PTR_ERR(req);
60
61 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
62
21d34711
CH
63 if (buffer && bufflen) {
64 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
65 if (ret)
66 goto out;
4160982e
CH
67 }
68
69 blk_execute_rq(req->q, NULL, req, 0);
70 if (result)
71 *result = (u32)(uintptr_t)req->special;
72 ret = req->errors;
73 out:
74 blk_mq_free_request(req);
75 return ret;
76}
77
78int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
79 void *buffer, unsigned bufflen)
80{
81 return __nvme_submit_sync_cmd(q, cmd, buffer, bufflen, NULL, 0);
82}
83
84int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
85 void __user *ubuffer, unsigned bufflen, u32 *result,
86 unsigned timeout)
87{
88 struct bio *bio = NULL;
89 struct request *req;
90 int ret;
91
92 req = nvme_alloc_request(q, cmd, 0);
93 if (IS_ERR(req))
94 return PTR_ERR(req);
95
96 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
97
98 if (ubuffer && bufflen) {
21d34711
CH
99 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
100 GFP_KERNEL);
101 if (ret)
102 goto out;
103 bio = req->bio;
104 }
105
106 blk_execute_rq(req->q, NULL, req, 0);
107 if (bio)
108 blk_rq_unmap_user(bio);
109 if (result)
110 *result = (u32)(uintptr_t)req->special;
111 ret = req->errors;
112 out:
113 blk_mq_free_request(req);
114 return ret;
115}
116
1c63dc66 117int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
21d34711
CH
118{
119 struct nvme_command c = { };
120 int error;
121
122 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
123 c.identify.opcode = nvme_admin_identify;
124 c.identify.cns = cpu_to_le32(1);
125
126 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
127 if (!*id)
128 return -ENOMEM;
129
130 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
131 sizeof(struct nvme_id_ctrl));
132 if (error)
133 kfree(*id);
134 return error;
135}
136
1c63dc66 137int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
21d34711
CH
138 struct nvme_id_ns **id)
139{
140 struct nvme_command c = { };
141 int error;
142
143 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
144 c.identify.opcode = nvme_admin_identify,
145 c.identify.nsid = cpu_to_le32(nsid),
146
147 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
148 if (!*id)
149 return -ENOMEM;
150
151 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
152 sizeof(struct nvme_id_ns));
153 if (error)
154 kfree(*id);
155 return error;
156}
157
1c63dc66 158int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
21d34711
CH
159 dma_addr_t dma_addr, u32 *result)
160{
161 struct nvme_command c;
162
163 memset(&c, 0, sizeof(c));
164 c.features.opcode = nvme_admin_get_features;
165 c.features.nsid = cpu_to_le32(nsid);
166 c.features.prp1 = cpu_to_le64(dma_addr);
167 c.features.fid = cpu_to_le32(fid);
168
4160982e 169 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
21d34711
CH
170}
171
1c63dc66 172int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
21d34711
CH
173 dma_addr_t dma_addr, u32 *result)
174{
175 struct nvme_command c;
176
177 memset(&c, 0, sizeof(c));
178 c.features.opcode = nvme_admin_set_features;
179 c.features.prp1 = cpu_to_le64(dma_addr);
180 c.features.fid = cpu_to_le32(fid);
181 c.features.dword11 = cpu_to_le32(dword11);
182
4160982e 183 return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
21d34711
CH
184}
185
1c63dc66 186int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
21d34711
CH
187{
188 struct nvme_command c = { };
189 int error;
190
191 c.common.opcode = nvme_admin_get_log_page,
192 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
193 c.common.cdw10[0] = cpu_to_le32(
194 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
195 NVME_LOG_SMART),
196
197 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
198 if (!*log)
199 return -ENOMEM;
200
201 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
202 sizeof(struct nvme_smart_log));
203 if (error)
204 kfree(*log);
205 return error;
206}