]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/nvme/host/core.c
nvme: use the block layer for userspace passthrough metadata
[karo-tx-linux.git] / drivers / nvme / host / core.c
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
24 struct request *nvme_alloc_request(struct request_queue *q,
25                 struct nvme_command *cmd, unsigned int flags)
26 {
27         bool write = cmd->common.opcode & 1;
28         struct request *req;
29
30         req = blk_mq_alloc_request(q, write, flags);
31         if (IS_ERR(req))
32                 return req;
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
40         req->cmd = (unsigned char *)cmd;
41         req->cmd_len = sizeof(struct nvme_command);
42         req->special = (void *)0;
43
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  */
51 int __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
63         if (buffer && bufflen) {
64                 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
65                 if (ret)
66                         goto out;
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
78 int 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
84 int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
85                 void __user *ubuffer, unsigned bufflen,
86                 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
87                 u32 *result, unsigned timeout)
88 {
89         bool write = cmd->common.opcode & 1;
90         struct nvme_ns *ns = q->queuedata;
91         struct gendisk *disk = ns ? ns->disk : NULL;
92         struct request *req;
93         struct bio *bio = NULL;
94         void *meta = NULL;
95         int ret;
96
97         req = nvme_alloc_request(q, cmd, 0);
98         if (IS_ERR(req))
99                 return PTR_ERR(req);
100
101         req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
102
103         if (ubuffer && bufflen) {
104                 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
105                                 GFP_KERNEL);
106                 if (ret)
107                         goto out;
108                 bio = req->bio;
109
110                 if (!disk)
111                         goto submit;
112                 bio->bi_bdev = bdget_disk(disk, 0);
113                 if (!bio->bi_bdev) {
114                         ret = -ENODEV;
115                         goto out_unmap;
116                 }
117
118                 if (meta_buffer) {
119                         struct bio_integrity_payload *bip;
120
121                         meta = kmalloc(meta_len, GFP_KERNEL);
122                         if (!meta) {
123                                 ret = -ENOMEM;
124                                 goto out_unmap;
125                         }
126
127                         if (write) {
128                                 if (copy_from_user(meta, meta_buffer,
129                                                 meta_len)) {
130                                         ret = -EFAULT;
131                                         goto out_free_meta;
132                                 }
133                         }
134
135                         bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
136                         if (!bip) {
137                                 ret = -ENOMEM;
138                                 goto out_free_meta;
139                         }
140
141                         bip->bip_iter.bi_size = meta_len;
142                         bip->bip_iter.bi_sector = meta_seed;
143
144                         ret = bio_integrity_add_page(bio, virt_to_page(meta),
145                                         meta_len, offset_in_page(meta));
146                         if (ret != meta_len) {
147                                 ret = -ENOMEM;
148                                 goto out_free_meta;
149                         }
150                 }
151         }
152  submit:
153         blk_execute_rq(req->q, disk, req, 0);
154         ret = req->errors;
155         if (result)
156                 *result = (u32)(uintptr_t)req->special;
157         if (meta && !ret && !write) {
158                 if (copy_to_user(meta_buffer, meta, meta_len))
159                         ret = -EFAULT;
160         }
161  out_free_meta:
162         kfree(meta);
163  out_unmap:
164         if (bio) {
165                 if (disk && bio->bi_bdev)
166                         bdput(bio->bi_bdev);
167                 blk_rq_unmap_user(bio);
168         }
169  out:
170         blk_mq_free_request(req);
171         return ret;
172 }
173
174 int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
175                 void __user *ubuffer, unsigned bufflen, u32 *result,
176                 unsigned timeout)
177 {
178         return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
179                         result, timeout);
180 }
181
182 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
183 {
184         struct nvme_command c = { };
185         int error;
186
187         /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
188         c.identify.opcode = nvme_admin_identify;
189         c.identify.cns = cpu_to_le32(1);
190
191         *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
192         if (!*id)
193                 return -ENOMEM;
194
195         error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
196                         sizeof(struct nvme_id_ctrl));
197         if (error)
198                 kfree(*id);
199         return error;
200 }
201
202 int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
203                 struct nvme_id_ns **id)
204 {
205         struct nvme_command c = { };
206         int error;
207
208         /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
209         c.identify.opcode = nvme_admin_identify,
210         c.identify.nsid = cpu_to_le32(nsid),
211
212         *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
213         if (!*id)
214                 return -ENOMEM;
215
216         error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
217                         sizeof(struct nvme_id_ns));
218         if (error)
219                 kfree(*id);
220         return error;
221 }
222
223 int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
224                                         dma_addr_t dma_addr, u32 *result)
225 {
226         struct nvme_command c;
227
228         memset(&c, 0, sizeof(c));
229         c.features.opcode = nvme_admin_get_features;
230         c.features.nsid = cpu_to_le32(nsid);
231         c.features.prp1 = cpu_to_le64(dma_addr);
232         c.features.fid = cpu_to_le32(fid);
233
234         return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
235 }
236
237 int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
238                                         dma_addr_t dma_addr, u32 *result)
239 {
240         struct nvme_command c;
241
242         memset(&c, 0, sizeof(c));
243         c.features.opcode = nvme_admin_set_features;
244         c.features.prp1 = cpu_to_le64(dma_addr);
245         c.features.fid = cpu_to_le32(fid);
246         c.features.dword11 = cpu_to_le32(dword11);
247
248         return __nvme_submit_sync_cmd(dev->admin_q, &c, NULL, 0, result, 0);
249 }
250
251 int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
252 {
253         struct nvme_command c = { };
254         int error;
255
256         c.common.opcode = nvme_admin_get_log_page,
257         c.common.nsid = cpu_to_le32(0xFFFFFFFF),
258         c.common.cdw10[0] = cpu_to_le32(
259                         (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
260                          NVME_LOG_SMART),
261
262         *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
263         if (!*log)
264                 return -ENOMEM;
265
266         error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
267                         sizeof(struct nvme_smart_log));
268         if (error)
269                 kfree(*log);
270         return error;
271 }