blob: 38efe4f752a196e1d2323bbe4d48006411d90290 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
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>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
29#include <scsi/sg.h>
30#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Ming Linba0ba7d2016-02-10 10:03:30 -080037unsigned char admin_timeout = 60;
38module_param(admin_timeout, byte, 0644);
39MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
42unsigned char nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
44MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
47unsigned char shutdown_timeout = 5;
48module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Keith Buschf80ec962016-07-12 16:20:31 -070051unsigned int nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, uint, 0644);
53MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
54EXPORT_SYMBOL_GPL(nvme_max_retries);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010055
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010056static int nvme_char_major;
57module_param(nvme_char_major, int, 0);
58
59static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080060static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010061
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010062static struct class *nvme_class;
63
Ming Linc55a2fd2016-05-18 14:05:02 -070064void nvme_cancel_request(struct request *req, void *data, bool reserved)
65{
66 int status;
67
68 if (!blk_mq_request_started(req))
69 return;
70
71 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
72 "Cancelling I/O %d", req->tag);
73
74 status = NVME_SC_ABORT_REQ;
75 if (blk_queue_dying(req->q))
76 status |= NVME_SC_DNR;
77 blk_mq_complete_request(req, status);
78}
79EXPORT_SYMBOL_GPL(nvme_cancel_request);
80
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020081bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
82 enum nvme_ctrl_state new_state)
83{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -030084 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020085 bool changed = false;
86
87 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -030088
89 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020090 switch (new_state) {
91 case NVME_CTRL_LIVE:
92 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +020093 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020094 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +090095 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +020096 changed = true;
97 /* FALLTHRU */
98 default:
99 break;
100 }
101 break;
102 case NVME_CTRL_RESETTING:
103 switch (old_state) {
104 case NVME_CTRL_NEW:
105 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900106 case NVME_CTRL_RECONNECTING:
107 changed = true;
108 /* FALLTHRU */
109 default:
110 break;
111 }
112 break;
113 case NVME_CTRL_RECONNECTING:
114 switch (old_state) {
115 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200116 changed = true;
117 /* FALLTHRU */
118 default:
119 break;
120 }
121 break;
122 case NVME_CTRL_DELETING:
123 switch (old_state) {
124 case NVME_CTRL_LIVE:
125 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900126 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200127 changed = true;
128 /* FALLTHRU */
129 default:
130 break;
131 }
132 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600133 case NVME_CTRL_DEAD:
134 switch (old_state) {
135 case NVME_CTRL_DELETING:
136 changed = true;
137 /* FALLTHRU */
138 default:
139 break;
140 }
141 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200142 default:
143 break;
144 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200145
146 if (changed)
147 ctrl->state = new_state;
148
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300149 spin_unlock_irq(&ctrl->lock);
150
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200151 return changed;
152}
153EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
154
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100155static void nvme_free_ns(struct kref *kref)
156{
157 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
158
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200159 if (ns->ndev)
160 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100161
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200162 if (ns->disk) {
163 spin_lock(&dev_list_lock);
164 ns->disk->private_data = NULL;
165 spin_unlock(&dev_list_lock);
166 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100167
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100168 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700169 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
170 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100171 kfree(ns);
172}
173
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100174static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100175{
176 kref_put(&ns->kref, nvme_free_ns);
177}
178
179static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
180{
181 struct nvme_ns *ns;
182
183 spin_lock(&dev_list_lock);
184 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800185 if (ns) {
186 if (!kref_get_unless_zero(&ns->kref))
187 goto fail;
188 if (!try_module_get(ns->ctrl->ops->module))
189 goto fail_put_ns;
190 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100191 spin_unlock(&dev_list_lock);
192
193 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800194
195fail_put_ns:
196 kref_put(&ns->kref, nvme_free_ns);
197fail:
198 spin_unlock(&dev_list_lock);
199 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100200}
201
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100202void nvme_requeue_req(struct request *req)
203{
Bart Van Asschea6eaa882016-10-28 17:23:40 -0700204 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100205}
Ming Lin576d55d2016-02-10 10:03:32 -0800206EXPORT_SYMBOL_GPL(nvme_requeue_req);
Christoph Hellwig7688faa2015-11-28 15:41:58 +0100207
Christoph Hellwig41609822015-11-20 09:00:02 +0100208struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200209 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100210{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100211 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100212 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100213
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200214 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100215 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200216 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100217 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200218 qid ? qid - 1 : 0);
219 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100220 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100221 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100222
Christoph Hellwig21d34712015-11-26 09:08:36 +0100223 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800224 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100225
Christoph Hellwig41609822015-11-20 09:00:02 +0100226 return req;
227}
Ming Lin576d55d2016-02-10 10:03:32 -0800228EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100229
Ming Lin8093f7c2016-04-12 13:10:14 -0600230static inline void nvme_setup_flush(struct nvme_ns *ns,
231 struct nvme_command *cmnd)
232{
233 memset(cmnd, 0, sizeof(*cmnd));
234 cmnd->common.opcode = nvme_cmd_flush;
235 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
236}
237
238static inline int nvme_setup_discard(struct nvme_ns *ns, struct request *req,
239 struct nvme_command *cmnd)
240{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100241 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600242 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100243 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600244
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100245 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600246 if (!range)
247 return BLK_MQ_RQ_QUEUE_BUSY;
248
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100249 __rq_for_each_bio(bio, req) {
250 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
251 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
252
253 range[n].cattr = cpu_to_le32(0);
254 range[n].nlb = cpu_to_le32(nlb);
255 range[n].slba = cpu_to_le64(slba);
256 n++;
257 }
258
259 if (WARN_ON_ONCE(n != segments)) {
260 kfree(range);
261 return BLK_MQ_RQ_QUEUE_ERROR;
262 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600263
264 memset(cmnd, 0, sizeof(*cmnd));
265 cmnd->dsm.opcode = nvme_cmd_dsm;
266 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100267 cmnd->dsm.nr = segments - 1;
Ming Lin8093f7c2016-04-12 13:10:14 -0600268 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
269
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700270 req->special_vec.bv_page = virt_to_page(range);
271 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100272 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700273 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600274
Omar Sandovalbac00002016-11-15 11:11:58 -0800275 return BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600276}
Ming Lin8093f7c2016-04-12 13:10:14 -0600277
Ming Lin8093f7c2016-04-12 13:10:14 -0600278static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
279 struct nvme_command *cmnd)
280{
281 u16 control = 0;
282 u32 dsmgmt = 0;
283
284 if (req->cmd_flags & REQ_FUA)
285 control |= NVME_RW_FUA;
286 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
287 control |= NVME_RW_LR;
288
289 if (req->cmd_flags & REQ_RAHEAD)
290 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
291
292 memset(cmnd, 0, sizeof(*cmnd));
293 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600294 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
295 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
296 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
297
298 if (ns->ms) {
299 switch (ns->pi_type) {
300 case NVME_NS_DPS_PI_TYPE3:
301 control |= NVME_RW_PRINFO_PRCHK_GUARD;
302 break;
303 case NVME_NS_DPS_PI_TYPE1:
304 case NVME_NS_DPS_PI_TYPE2:
305 control |= NVME_RW_PRINFO_PRCHK_GUARD |
306 NVME_RW_PRINFO_PRCHK_REF;
307 cmnd->rw.reftag = cpu_to_le32(
308 nvme_block_nr(ns, blk_rq_pos(req)));
309 break;
310 }
311 if (!blk_integrity_rq(req))
312 control |= NVME_RW_PRINFO_PRACT;
313 }
314
315 cmnd->rw.control = cpu_to_le16(control);
316 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
317}
318
319int nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
320 struct nvme_command *cmd)
321{
Omar Sandovalbac00002016-11-15 11:11:58 -0800322 int ret = BLK_MQ_RQ_QUEUE_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600323
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100324 switch (req_op(req)) {
325 case REQ_OP_DRV_IN:
326 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800327 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100328 break;
329 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600330 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100331 break;
332 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600333 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100334 break;
335 case REQ_OP_READ:
336 case REQ_OP_WRITE:
Ming Lin8093f7c2016-04-12 13:10:14 -0600337 nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100338 break;
339 default:
340 WARN_ON_ONCE(1);
341 return BLK_MQ_RQ_QUEUE_ERROR;
342 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600343
James Smart721b3912016-10-21 23:33:34 +0300344 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600345 return ret;
346}
347EXPORT_SYMBOL_GPL(nvme_setup_cmd);
348
Christoph Hellwig41609822015-11-20 09:00:02 +0100349/*
350 * Returns 0 on success. If the result is negative, it's a Linux error code;
351 * if the result is positive, it's an NVM Express status code
352 */
353int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800354 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200355 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100356{
357 struct request *req;
358 int ret;
359
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200360 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100361 if (IS_ERR(req))
362 return PTR_ERR(req);
363
364 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
365
Christoph Hellwig21d34712015-11-26 09:08:36 +0100366 if (buffer && bufflen) {
367 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
368 if (ret)
369 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100370 }
371
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200372 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800373 if (result)
374 *result = nvme_req(req)->result;
Christoph Hellwig41609822015-11-20 09:00:02 +0100375 ret = req->errors;
376 out:
377 blk_mq_free_request(req);
378 return ret;
379}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200380EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100381
382int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
383 void *buffer, unsigned bufflen)
384{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200385 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
386 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100387}
Ming Lin576d55d2016-02-10 10:03:32 -0800388EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100389
Keith Busch0b7f1f22015-10-23 09:47:28 -0600390int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
391 void __user *ubuffer, unsigned bufflen,
392 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
393 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100394{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200395 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600396 struct nvme_ns *ns = q->queuedata;
397 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100398 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600399 struct bio *bio = NULL;
400 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100401 int ret;
402
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200403 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100404 if (IS_ERR(req))
405 return PTR_ERR(req);
406
407 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
408
409 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100410 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
411 GFP_KERNEL);
412 if (ret)
413 goto out;
414 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100415
Keith Busch0b7f1f22015-10-23 09:47:28 -0600416 if (!disk)
417 goto submit;
418 bio->bi_bdev = bdget_disk(disk, 0);
419 if (!bio->bi_bdev) {
420 ret = -ENODEV;
421 goto out_unmap;
422 }
423
Keith Busche9fc63d2016-02-24 09:15:58 -0700424 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600425 struct bio_integrity_payload *bip;
426
427 meta = kmalloc(meta_len, GFP_KERNEL);
428 if (!meta) {
429 ret = -ENOMEM;
430 goto out_unmap;
431 }
432
433 if (write) {
434 if (copy_from_user(meta, meta_buffer,
435 meta_len)) {
436 ret = -EFAULT;
437 goto out_free_meta;
438 }
439 }
440
441 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700442 if (IS_ERR(bip)) {
443 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600444 goto out_free_meta;
445 }
446
447 bip->bip_iter.bi_size = meta_len;
448 bip->bip_iter.bi_sector = meta_seed;
449
450 ret = bio_integrity_add_page(bio, virt_to_page(meta),
451 meta_len, offset_in_page(meta));
452 if (ret != meta_len) {
453 ret = -ENOMEM;
454 goto out_free_meta;
455 }
456 }
457 }
458 submit:
459 blk_execute_rq(req->q, disk, req, 0);
460 ret = req->errors;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100461 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800462 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600463 if (meta && !ret && !write) {
464 if (copy_to_user(meta_buffer, meta, meta_len))
465 ret = -EFAULT;
466 }
467 out_free_meta:
468 kfree(meta);
469 out_unmap:
470 if (bio) {
471 if (disk && bio->bi_bdev)
472 bdput(bio->bi_bdev);
473 blk_rq_unmap_user(bio);
474 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100475 out:
476 blk_mq_free_request(req);
477 return ret;
478}
479
Keith Busch0b7f1f22015-10-23 09:47:28 -0600480int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
481 void __user *ubuffer, unsigned bufflen, u32 *result,
482 unsigned timeout)
483{
484 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
485 result, timeout);
486}
487
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200488static void nvme_keep_alive_end_io(struct request *rq, int error)
489{
490 struct nvme_ctrl *ctrl = rq->end_io_data;
491
492 blk_mq_free_request(rq);
493
494 if (error) {
495 dev_err(ctrl->device,
496 "failed nvme_keep_alive_end_io error=%d\n", error);
497 return;
498 }
499
500 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
501}
502
503static int nvme_keep_alive(struct nvme_ctrl *ctrl)
504{
505 struct nvme_command c;
506 struct request *rq;
507
508 memset(&c, 0, sizeof(c));
509 c.common.opcode = nvme_admin_keep_alive;
510
511 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
512 NVME_QID_ANY);
513 if (IS_ERR(rq))
514 return PTR_ERR(rq);
515
516 rq->timeout = ctrl->kato * HZ;
517 rq->end_io_data = ctrl;
518
519 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
520
521 return 0;
522}
523
524static void nvme_keep_alive_work(struct work_struct *work)
525{
526 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
527 struct nvme_ctrl, ka_work);
528
529 if (nvme_keep_alive(ctrl)) {
530 /* allocation failure, reset the controller */
531 dev_err(ctrl->device, "keep-alive failed\n");
532 ctrl->ops->reset_ctrl(ctrl);
533 return;
534 }
535}
536
537void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
538{
539 if (unlikely(ctrl->kato == 0))
540 return;
541
542 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
543 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
544}
545EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
546
547void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
548{
549 if (unlikely(ctrl->kato == 0))
550 return;
551
552 cancel_delayed_work_sync(&ctrl->ka_work);
553}
554EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
555
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100556int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100557{
558 struct nvme_command c = { };
559 int error;
560
561 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
562 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200563 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100564
565 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
566 if (!*id)
567 return -ENOMEM;
568
569 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
570 sizeof(struct nvme_id_ctrl));
571 if (error)
572 kfree(*id);
573 return error;
574}
575
Keith Busch540c8012015-10-22 15:45:06 -0600576static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
577{
578 struct nvme_command c = { };
579
580 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200581 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600582 c.identify.nsid = cpu_to_le32(nsid);
583 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
584}
585
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100586int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100587 struct nvme_id_ns **id)
588{
589 struct nvme_command c = { };
590 int error;
591
592 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200593 c.identify.opcode = nvme_admin_identify;
594 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200595 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100596
597 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
598 if (!*id)
599 return -ENOMEM;
600
601 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
602 sizeof(struct nvme_id_ns));
603 if (error)
604 kfree(*id);
605 return error;
606}
607
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100608int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700609 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100610{
611 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800612 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100613 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100614
615 memset(&c, 0, sizeof(c));
616 c.features.opcode = nvme_admin_get_features;
617 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100618 c.features.fid = cpu_to_le32(fid);
619
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800620 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200621 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700622 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800623 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100624 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100625}
626
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100627int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700628 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100629{
630 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800631 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100632 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100633
634 memset(&c, 0, sizeof(c));
635 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100636 c.features.fid = cpu_to_le32(fid);
637 c.features.dword11 = cpu_to_le32(dword11);
638
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800639 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700640 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700641 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800642 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100643 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100644}
645
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100646int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100647{
648 struct nvme_command c = { };
649 int error;
650
651 c.common.opcode = nvme_admin_get_log_page,
652 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
653 c.common.cdw10[0] = cpu_to_le32(
654 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
655 NVME_LOG_SMART),
656
657 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
658 if (!*log)
659 return -ENOMEM;
660
661 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
662 sizeof(struct nvme_smart_log));
663 if (error)
664 kfree(*log);
665 return error;
666}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100667
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100668int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
669{
670 u32 q_count = (*count - 1) | ((*count - 1) << 16);
671 u32 result;
672 int status, nr_io_queues;
673
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700674 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100675 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200676 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100677 return status;
678
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200679 /*
680 * Degraded controllers might return an error when setting the queue
681 * count. We still want to be able to bring them online and offer
682 * access to the admin queue, as that might be only way to fix them up.
683 */
684 if (status > 0) {
685 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
686 *count = 0;
687 } else {
688 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
689 *count = min(*count, nr_io_queues);
690 }
691
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100692 return 0;
693}
Ming Lin576d55d2016-02-10 10:03:32 -0800694EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100695
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100696static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
697{
698 struct nvme_user_io io;
699 struct nvme_command c;
700 unsigned length, meta_len;
701 void __user *metadata;
702
703 if (copy_from_user(&io, uio, sizeof(io)))
704 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700705 if (io.flags)
706 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100707
708 switch (io.opcode) {
709 case nvme_cmd_write:
710 case nvme_cmd_read:
711 case nvme_cmd_compare:
712 break;
713 default:
714 return -EINVAL;
715 }
716
717 length = (io.nblocks + 1) << ns->lba_shift;
718 meta_len = (io.nblocks + 1) * ns->ms;
719 metadata = (void __user *)(uintptr_t)io.metadata;
720
721 if (ns->ext) {
722 length += meta_len;
723 meta_len = 0;
724 } else if (meta_len) {
725 if ((io.metadata & 3) || !io.metadata)
726 return -EINVAL;
727 }
728
729 memset(&c, 0, sizeof(c));
730 c.rw.opcode = io.opcode;
731 c.rw.flags = io.flags;
732 c.rw.nsid = cpu_to_le32(ns->ns_id);
733 c.rw.slba = cpu_to_le64(io.slba);
734 c.rw.length = cpu_to_le16(io.nblocks);
735 c.rw.control = cpu_to_le16(io.control);
736 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
737 c.rw.reftag = cpu_to_le32(io.reftag);
738 c.rw.apptag = cpu_to_le16(io.apptag);
739 c.rw.appmask = cpu_to_le16(io.appmask);
740
741 return __nvme_submit_user_cmd(ns->queue, &c,
742 (void __user *)(uintptr_t)io.addr, length,
743 metadata, meta_len, io.slba, NULL, 0);
744}
745
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100746static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100747 struct nvme_passthru_cmd __user *ucmd)
748{
749 struct nvme_passthru_cmd cmd;
750 struct nvme_command c;
751 unsigned timeout = 0;
752 int status;
753
754 if (!capable(CAP_SYS_ADMIN))
755 return -EACCES;
756 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
757 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700758 if (cmd.flags)
759 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100760
761 memset(&c, 0, sizeof(c));
762 c.common.opcode = cmd.opcode;
763 c.common.flags = cmd.flags;
764 c.common.nsid = cpu_to_le32(cmd.nsid);
765 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
766 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
767 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
768 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
769 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
770 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
771 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
772 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
773
774 if (cmd.timeout_ms)
775 timeout = msecs_to_jiffies(cmd.timeout_ms);
776
777 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100778 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100779 &cmd.result, timeout);
780 if (status >= 0) {
781 if (put_user(cmd.result, &ucmd->result))
782 return -EFAULT;
783 }
784
785 return status;
786}
787
788static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
789 unsigned int cmd, unsigned long arg)
790{
791 struct nvme_ns *ns = bdev->bd_disk->private_data;
792
793 switch (cmd) {
794 case NVME_IOCTL_ID:
795 force_successful_syscall_return();
796 return ns->ns_id;
797 case NVME_IOCTL_ADMIN_CMD:
798 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
799 case NVME_IOCTL_IO_CMD:
800 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
801 case NVME_IOCTL_SUBMIT_IO:
802 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100803#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100804 case SG_GET_VERSION_NUM:
805 return nvme_sg_get_version_num((void __user *)arg);
806 case SG_IO:
807 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100808#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100809 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100810#ifdef CONFIG_NVM
811 if (ns->ndev)
812 return nvme_nvm_ioctl(ns, cmd, arg);
813#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700814 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100815 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700816 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100817 return -ENOTTY;
818 }
819}
820
821#ifdef CONFIG_COMPAT
822static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
823 unsigned int cmd, unsigned long arg)
824{
825 switch (cmd) {
826 case SG_IO:
827 return -ENOIOCTLCMD;
828 }
829 return nvme_ioctl(bdev, mode, cmd, arg);
830}
831#else
832#define nvme_compat_ioctl NULL
833#endif
834
835static int nvme_open(struct block_device *bdev, fmode_t mode)
836{
837 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
838}
839
840static void nvme_release(struct gendisk *disk, fmode_t mode)
841{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800842 struct nvme_ns *ns = disk->private_data;
843
844 module_put(ns->ctrl->ops->module);
845 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100846}
847
848static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
849{
850 /* some standard values */
851 geo->heads = 1 << 6;
852 geo->sectors = 1 << 5;
853 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
854 return 0;
855}
856
857#ifdef CONFIG_BLK_DEV_INTEGRITY
858static void nvme_init_integrity(struct nvme_ns *ns)
859{
860 struct blk_integrity integrity;
861
Jay Freyenseefa9a89f2016-07-20 21:26:16 -0600862 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100863 switch (ns->pi_type) {
864 case NVME_NS_DPS_PI_TYPE3:
865 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000866 integrity.tag_size = sizeof(u16) + sizeof(u32);
867 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100868 break;
869 case NVME_NS_DPS_PI_TYPE1:
870 case NVME_NS_DPS_PI_TYPE2:
871 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +0000872 integrity.tag_size = sizeof(u16);
873 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100874 break;
875 default:
876 integrity.profile = NULL;
877 break;
878 }
879 integrity.tuple_size = ns->ms;
880 blk_integrity_register(ns->disk, &integrity);
881 blk_queue_max_integrity_segments(ns->queue, 1);
882}
883#else
884static void nvme_init_integrity(struct nvme_ns *ns)
885{
886}
887#endif /* CONFIG_BLK_DEV_INTEGRITY */
888
889static void nvme_config_discard(struct nvme_ns *ns)
890{
Keith Busch08095e72016-03-04 13:15:17 -0700891 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100892 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -0700893
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100894 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
895 NVME_DSM_MAX_RANGES);
896
Keith Busch08095e72016-03-04 13:15:17 -0700897 if (ctrl->quirks & NVME_QUIRK_DISCARD_ZEROES)
898 ns->queue->limits.discard_zeroes_data = 1;
899 else
900 ns->queue->limits.discard_zeroes_data = 0;
901
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100902 ns->queue->limits.discard_alignment = logical_block_size;
903 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +0800904 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100905 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100906 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
907}
908
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200909static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100910{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200911 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200912 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100913 return -ENODEV;
914 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200915
916 if ((*id)->ncap == 0) {
917 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100918 return -ENODEV;
919 }
920
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600921 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200922 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -0600923 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200924 memcpy(ns->uuid, (*id)->nguid, sizeof(ns->uuid));
925
926 return 0;
927}
928
929static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
930{
931 struct nvme_ns *ns = disk->private_data;
932 u8 lbaf, pi_type;
933 u16 old_ms;
934 unsigned short bs;
Keith Busch2b9b6e82015-12-22 10:10:45 -0700935
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100936 old_ms = ns->ms;
937 lbaf = id->flbas & NVME_NS_FLBAS_LBA_MASK;
938 ns->lba_shift = id->lbaf[lbaf].ds;
939 ns->ms = le16_to_cpu(id->lbaf[lbaf].ms);
940 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
941
942 /*
943 * If identify namespace failed, use default 512 byte block size so
944 * block layer can use before failing read/write for 0 capacity.
945 */
946 if (ns->lba_shift == 0)
947 ns->lba_shift = 9;
948 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100949 /* XXX: PI implementation requires metadata equal t10 pi tuple size */
950 pi_type = ns->ms == sizeof(struct t10_pi_tuple) ?
951 id->dps & NVME_NS_DPS_PI_MASK : 0;
952
953 blk_mq_freeze_queue(disk->queue);
954 if (blk_get_integrity(disk) && (ns->pi_type != pi_type ||
955 ns->ms != old_ms ||
956 bs != queue_logical_block_size(disk->queue) ||
957 (ns->ms && ns->ext)))
958 blk_integrity_unregister(disk);
959
960 ns->pi_type = pi_type;
961 blk_queue_logical_block_size(ns->queue, bs);
962
Keith Busch4b9d5b12015-11-20 09:13:30 +0100963 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100964 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100965 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
966 set_capacity(disk, 0);
967 else
968 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
969
970 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
971 nvme_config_discard(ns);
972 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200973}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100974
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200975static int nvme_revalidate_disk(struct gendisk *disk)
976{
977 struct nvme_ns *ns = disk->private_data;
978 struct nvme_id_ns *id = NULL;
979 int ret;
980
981 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
982 set_capacity(disk, 0);
983 return -ENODEV;
984 }
985
986 ret = nvme_revalidate_ns(ns, &id);
987 if (ret)
988 return ret;
989
990 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100991 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200992
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100993 return 0;
994}
995
996static char nvme_pr_type(enum pr_type type)
997{
998 switch (type) {
999 case PR_WRITE_EXCLUSIVE:
1000 return 1;
1001 case PR_EXCLUSIVE_ACCESS:
1002 return 2;
1003 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1004 return 3;
1005 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1006 return 4;
1007 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1008 return 5;
1009 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1010 return 6;
1011 default:
1012 return 0;
1013 }
1014};
1015
1016static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1017 u64 key, u64 sa_key, u8 op)
1018{
1019 struct nvme_ns *ns = bdev->bd_disk->private_data;
1020 struct nvme_command c;
1021 u8 data[16] = { 0, };
1022
1023 put_unaligned_le64(key, &data[0]);
1024 put_unaligned_le64(sa_key, &data[8]);
1025
1026 memset(&c, 0, sizeof(c));
1027 c.common.opcode = op;
1028 c.common.nsid = cpu_to_le32(ns->ns_id);
1029 c.common.cdw10[0] = cpu_to_le32(cdw10);
1030
1031 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1032}
1033
1034static int nvme_pr_register(struct block_device *bdev, u64 old,
1035 u64 new, unsigned flags)
1036{
1037 u32 cdw10;
1038
1039 if (flags & ~PR_FL_IGNORE_KEY)
1040 return -EOPNOTSUPP;
1041
1042 cdw10 = old ? 2 : 0;
1043 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1044 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1045 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1046}
1047
1048static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1049 enum pr_type type, unsigned flags)
1050{
1051 u32 cdw10;
1052
1053 if (flags & ~PR_FL_IGNORE_KEY)
1054 return -EOPNOTSUPP;
1055
1056 cdw10 = nvme_pr_type(type) << 8;
1057 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1058 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1059}
1060
1061static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1062 enum pr_type type, bool abort)
1063{
1064 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1065 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1066}
1067
1068static int nvme_pr_clear(struct block_device *bdev, u64 key)
1069{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001070 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001071 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1072}
1073
1074static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1075{
1076 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1077 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1078}
1079
1080static const struct pr_ops nvme_pr_ops = {
1081 .pr_register = nvme_pr_register,
1082 .pr_reserve = nvme_pr_reserve,
1083 .pr_release = nvme_pr_release,
1084 .pr_preempt = nvme_pr_preempt,
1085 .pr_clear = nvme_pr_clear,
1086};
1087
Scott Bauera98e58e52017-02-03 12:50:32 -07001088#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001089int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1090 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001091{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001092 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001093 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001094
1095 memset(&cmd, 0, sizeof(cmd));
1096 if (send)
1097 cmd.common.opcode = nvme_admin_security_send;
1098 else
1099 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001100 cmd.common.nsid = 0;
1101 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1102 cmd.common.cdw10[1] = cpu_to_le32(len);
1103
1104 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1105 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1106}
1107EXPORT_SYMBOL_GPL(nvme_sec_submit);
1108#endif /* CONFIG_BLK_SED_OPAL */
1109
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001110static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111 .owner = THIS_MODULE,
1112 .ioctl = nvme_ioctl,
1113 .compat_ioctl = nvme_compat_ioctl,
1114 .open = nvme_open,
1115 .release = nvme_release,
1116 .getgeo = nvme_getgeo,
1117 .revalidate_disk= nvme_revalidate_disk,
1118 .pr_ops = &nvme_pr_ops,
1119};
1120
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001121static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1122{
1123 unsigned long timeout =
1124 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1125 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1126 int ret;
1127
1128 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001129 if (csts == ~0)
1130 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001131 if ((csts & NVME_CSTS_RDY) == bit)
1132 break;
1133
1134 msleep(100);
1135 if (fatal_signal_pending(current))
1136 return -EINTR;
1137 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001138 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001139 "Device not ready; aborting %s\n", enabled ?
1140 "initialisation" : "reset");
1141 return -ENODEV;
1142 }
1143 }
1144
1145 return ret;
1146}
1147
1148/*
1149 * If the device has been passed off to us in an enabled state, just clear
1150 * the enabled bit. The spec says we should set the 'shutdown notification
1151 * bits', but doing so may cause the device to complete commands to the
1152 * admin queue ... and we don't know what memory that might be pointing at!
1153 */
1154int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1155{
1156 int ret;
1157
1158 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1159 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1160
1161 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1162 if (ret)
1163 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001164
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001165 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001166 msleep(NVME_QUIRK_DELAY_AMOUNT);
1167
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001168 return nvme_wait_ready(ctrl, cap, false);
1169}
Ming Lin576d55d2016-02-10 10:03:32 -08001170EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001171
1172int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1173{
1174 /*
1175 * Default to a 4K page size, with the intention to update this
1176 * path in the future to accomodate architectures with differing
1177 * kernel and IO page sizes.
1178 */
1179 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1180 int ret;
1181
1182 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001183 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001184 "Minimum device page size %u too large for host (%u)\n",
1185 1 << dev_page_min, 1 << page_shift);
1186 return -ENODEV;
1187 }
1188
1189 ctrl->page_size = 1 << page_shift;
1190
1191 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1192 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1193 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1194 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1195 ctrl->ctrl_config |= NVME_CC_ENABLE;
1196
1197 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1198 if (ret)
1199 return ret;
1200 return nvme_wait_ready(ctrl, cap, true);
1201}
Ming Lin576d55d2016-02-10 10:03:32 -08001202EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001203
1204int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1205{
1206 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1207 u32 csts;
1208 int ret;
1209
1210 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1211 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1212
1213 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1214 if (ret)
1215 return ret;
1216
1217 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1218 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1219 break;
1220
1221 msleep(100);
1222 if (fatal_signal_pending(current))
1223 return -EINTR;
1224 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001225 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001226 "Device shutdown incomplete; abort shutdown\n");
1227 return -ENODEV;
1228 }
1229 }
1230
1231 return ret;
1232}
Ming Lin576d55d2016-02-10 10:03:32 -08001233EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001234
Christoph Hellwigda358252016-03-02 18:07:11 +01001235static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1236 struct request_queue *q)
1237{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001238 bool vwc = false;
1239
Christoph Hellwigda358252016-03-02 18:07:11 +01001240 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001241 u32 max_segments =
1242 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1243
Christoph Hellwigda358252016-03-02 18:07:11 +01001244 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001245 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001246 }
Keith Busche6282ae2016-12-19 11:37:50 -05001247 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1248 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001249 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001250 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1251 vwc = true;
1252 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001253}
1254
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001255struct nvme_core_quirk_entry {
1256 /*
1257 * NVMe model and firmware strings are padded with spaces. For
1258 * simplicity, strings in the quirk table are padded with NULLs
1259 * instead.
1260 */
1261 u16 vid;
1262 const char *mn;
1263 const char *fr;
1264 unsigned long quirks;
1265};
1266
1267static const struct nvme_core_quirk_entry core_quirks[] = {
1268};
1269
1270/* match is null-terminated but idstr is space-padded. */
1271static bool string_matches(const char *idstr, const char *match, size_t len)
1272{
1273 size_t matchlen;
1274
1275 if (!match)
1276 return true;
1277
1278 matchlen = strlen(match);
1279 WARN_ON_ONCE(matchlen > len);
1280
1281 if (memcmp(idstr, match, matchlen))
1282 return false;
1283
1284 for (; matchlen < len; matchlen++)
1285 if (idstr[matchlen] != ' ')
1286 return false;
1287
1288 return true;
1289}
1290
1291static bool quirk_matches(const struct nvme_id_ctrl *id,
1292 const struct nvme_core_quirk_entry *q)
1293{
1294 return q->vid == le16_to_cpu(id->vid) &&
1295 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1296 string_matches(id->fr, q->fr, sizeof(id->fr));
1297}
1298
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001299/*
1300 * Initialize the cached copies of the Identify data and various controller
1301 * register in our nvme_ctrl structure. This should be called as soon as
1302 * the admin queue is fully up and running.
1303 */
1304int nvme_init_identify(struct nvme_ctrl *ctrl)
1305{
1306 struct nvme_id_ctrl *id;
1307 u64 cap;
1308 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001309 u32 max_hw_sectors;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001310
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001311 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1312 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001313 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001314 return ret;
1315 }
1316
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001317 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1318 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001319 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001320 return ret;
1321 }
1322 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1323
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001324 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001325 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1326
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001327 ret = nvme_identify_ctrl(ctrl, &id);
1328 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001329 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001330 return -EIO;
1331 }
1332
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001333 if (!ctrl->identified) {
1334 /*
1335 * Check for quirks. Quirk can depend on firmware version,
1336 * so, in principle, the set of quirks present can change
1337 * across a reset. As a possible future enhancement, we
1338 * could re-scan for quirks every time we reinitialize
1339 * the device, but we'd have to make sure that the driver
1340 * behaves intelligently if the quirks change.
1341 */
1342
1343 int i;
1344
1345 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1346 if (quirk_matches(id, &core_quirks[i]))
1347 ctrl->quirks |= core_quirks[i].quirks;
1348 }
1349 }
1350
Scott Bauer8a9ae522017-02-17 13:59:40 +01001351 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001352 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001353 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001354 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001355 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001356 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001357 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1358 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1359 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1360 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001361 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001362 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001363 max_hw_sectors = UINT_MAX;
1364 ctrl->max_hw_sectors =
1365 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001366
Christoph Hellwigda358252016-03-02 18:07:11 +01001367 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001368 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001369 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001370
1371 if (ctrl->ops->is_fabrics) {
1372 ctrl->icdoff = le16_to_cpu(id->icdoff);
1373 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1374 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1375 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1376
1377 /*
1378 * In fabrics we need to verify the cntlid matches the
1379 * admin connect
1380 */
1381 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1382 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001383
1384 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1385 dev_err(ctrl->dev,
1386 "keep-alive support is mandatory for fabrics\n");
1387 ret = -EINVAL;
1388 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001389 } else {
1390 ctrl->cntlid = le16_to_cpu(id->cntlid);
1391 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001392
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001393 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001394
1395 ctrl->identified = true;
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001396 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001397}
Ming Lin576d55d2016-02-10 10:03:32 -08001398EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001399
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001400static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001401{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001402 struct nvme_ctrl *ctrl;
1403 int instance = iminor(inode);
1404 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001405
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001406 spin_lock(&dev_list_lock);
1407 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1408 if (ctrl->instance != instance)
1409 continue;
1410
1411 if (!ctrl->admin_q) {
1412 ret = -EWOULDBLOCK;
1413 break;
1414 }
1415 if (!kref_get_unless_zero(&ctrl->kref))
1416 break;
1417 file->private_data = ctrl;
1418 ret = 0;
1419 break;
1420 }
1421 spin_unlock(&dev_list_lock);
1422
1423 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001424}
1425
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001426static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001427{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001428 nvme_put_ctrl(file->private_data);
1429 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001430}
1431
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001432static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1433{
1434 struct nvme_ns *ns;
1435 int ret;
1436
1437 mutex_lock(&ctrl->namespaces_mutex);
1438 if (list_empty(&ctrl->namespaces)) {
1439 ret = -ENOTTY;
1440 goto out_unlock;
1441 }
1442
1443 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1444 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001445 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001446 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1447 ret = -EINVAL;
1448 goto out_unlock;
1449 }
1450
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001451 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001452 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1453 kref_get(&ns->kref);
1454 mutex_unlock(&ctrl->namespaces_mutex);
1455
1456 ret = nvme_user_cmd(ctrl, ns, argp);
1457 nvme_put_ns(ns);
1458 return ret;
1459
1460out_unlock:
1461 mutex_unlock(&ctrl->namespaces_mutex);
1462 return ret;
1463}
1464
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001465static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1466 unsigned long arg)
1467{
1468 struct nvme_ctrl *ctrl = file->private_data;
1469 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001470
1471 switch (cmd) {
1472 case NVME_IOCTL_ADMIN_CMD:
1473 return nvme_user_cmd(ctrl, NULL, argp);
1474 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001475 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001476 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001477 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001478 return ctrl->ops->reset_ctrl(ctrl);
1479 case NVME_IOCTL_SUBSYS_RESET:
1480 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001481 case NVME_IOCTL_RESCAN:
1482 nvme_queue_scan(ctrl);
1483 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001484 default:
1485 return -ENOTTY;
1486 }
1487}
1488
1489static const struct file_operations nvme_dev_fops = {
1490 .owner = THIS_MODULE,
1491 .open = nvme_dev_open,
1492 .release = nvme_dev_release,
1493 .unlocked_ioctl = nvme_dev_ioctl,
1494 .compat_ioctl = nvme_dev_ioctl,
1495};
1496
1497static ssize_t nvme_sysfs_reset(struct device *dev,
1498 struct device_attribute *attr, const char *buf,
1499 size_t count)
1500{
1501 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1502 int ret;
1503
1504 ret = ctrl->ops->reset_ctrl(ctrl);
1505 if (ret < 0)
1506 return ret;
1507 return count;
1508}
1509static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1510
Keith Busch9ec3bb22016-04-29 15:45:18 -06001511static ssize_t nvme_sysfs_rescan(struct device *dev,
1512 struct device_attribute *attr, const char *buf,
1513 size_t count)
1514{
1515 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1516
1517 nvme_queue_scan(ctrl);
1518 return count;
1519}
1520static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1521
Keith Busch118472a2016-02-18 09:57:48 -07001522static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1523 char *buf)
1524{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001525 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001526 struct nvme_ctrl *ctrl = ns->ctrl;
1527 int serial_len = sizeof(ctrl->serial);
1528 int model_len = sizeof(ctrl->model);
1529
1530 if (memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1531 return sprintf(buf, "eui.%16phN\n", ns->uuid);
1532
1533 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1534 return sprintf(buf, "eui.%8phN\n", ns->eui);
1535
1536 while (ctrl->serial[serial_len - 1] == ' ')
1537 serial_len--;
1538 while (ctrl->model[model_len - 1] == ' ')
1539 model_len--;
1540
1541 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1542 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1543}
1544static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1545
Keith Busch2b9b6e82015-12-22 10:10:45 -07001546static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1547 char *buf)
1548{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001549 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001550 return sprintf(buf, "%pU\n", ns->uuid);
1551}
1552static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1553
1554static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1555 char *buf)
1556{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001557 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001558 return sprintf(buf, "%8phd\n", ns->eui);
1559}
1560static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1561
1562static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1563 char *buf)
1564{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001565 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001566 return sprintf(buf, "%d\n", ns->ns_id);
1567}
1568static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1569
1570static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001571 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001572 &dev_attr_uuid.attr,
1573 &dev_attr_eui.attr,
1574 &dev_attr_nsid.attr,
1575 NULL,
1576};
1577
Ming Lin1a353d82016-06-13 16:45:24 +02001578static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001579 struct attribute *a, int n)
1580{
1581 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001582 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001583
1584 if (a == &dev_attr_uuid.attr) {
1585 if (!memchr_inv(ns->uuid, 0, sizeof(ns->uuid)))
1586 return 0;
1587 }
1588 if (a == &dev_attr_eui.attr) {
1589 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1590 return 0;
1591 }
1592 return a->mode;
1593}
1594
1595static const struct attribute_group nvme_ns_attr_group = {
1596 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001597 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001598};
1599
Ming Lin931e1c22016-02-26 13:24:19 -08001600#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001601static ssize_t field##_show(struct device *dev, \
1602 struct device_attribute *attr, char *buf) \
1603{ \
1604 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1605 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1606} \
1607static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1608
Ming Lin931e1c22016-02-26 13:24:19 -08001609#define nvme_show_int_function(field) \
1610static ssize_t field##_show(struct device *dev, \
1611 struct device_attribute *attr, char *buf) \
1612{ \
1613 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1614 return sprintf(buf, "%d\n", ctrl->field); \
1615} \
1616static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1617
1618nvme_show_str_function(model);
1619nvme_show_str_function(serial);
1620nvme_show_str_function(firmware_rev);
1621nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001622
Ming Lin1a353d82016-06-13 16:45:24 +02001623static ssize_t nvme_sysfs_delete(struct device *dev,
1624 struct device_attribute *attr, const char *buf,
1625 size_t count)
1626{
1627 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1628
1629 if (device_remove_file_self(dev, attr))
1630 ctrl->ops->delete_ctrl(ctrl);
1631 return count;
1632}
1633static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1634
1635static ssize_t nvme_sysfs_show_transport(struct device *dev,
1636 struct device_attribute *attr,
1637 char *buf)
1638{
1639 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1640
1641 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1642}
1643static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1644
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001645static ssize_t nvme_sysfs_show_state(struct device *dev,
1646 struct device_attribute *attr,
1647 char *buf)
1648{
1649 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1650 static const char *const state_name[] = {
1651 [NVME_CTRL_NEW] = "new",
1652 [NVME_CTRL_LIVE] = "live",
1653 [NVME_CTRL_RESETTING] = "resetting",
1654 [NVME_CTRL_RECONNECTING]= "reconnecting",
1655 [NVME_CTRL_DELETING] = "deleting",
1656 [NVME_CTRL_DEAD] = "dead",
1657 };
1658
1659 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1660 state_name[ctrl->state])
1661 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1662
1663 return sprintf(buf, "unknown state\n");
1664}
1665
1666static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
1667
Ming Lin1a353d82016-06-13 16:45:24 +02001668static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
1669 struct device_attribute *attr,
1670 char *buf)
1671{
1672 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1673
1674 return snprintf(buf, PAGE_SIZE, "%s\n",
1675 ctrl->ops->get_subsysnqn(ctrl));
1676}
1677static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
1678
1679static ssize_t nvme_sysfs_show_address(struct device *dev,
1680 struct device_attribute *attr,
1681 char *buf)
1682{
1683 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1684
1685 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
1686}
1687static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
1688
Keith Busch779ff7562016-01-12 15:09:31 -07001689static struct attribute *nvme_dev_attrs[] = {
1690 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06001691 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001692 &dev_attr_model.attr,
1693 &dev_attr_serial.attr,
1694 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08001695 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02001696 &dev_attr_delete_controller.attr,
1697 &dev_attr_transport.attr,
1698 &dev_attr_subsysnqn.attr,
1699 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001700 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07001701 NULL
1702};
1703
Ming Lin1a353d82016-06-13 16:45:24 +02001704#define CHECK_ATTR(ctrl, a, name) \
1705 if ((a) == &dev_attr_##name.attr && \
1706 !(ctrl)->ops->get_##name) \
1707 return 0
1708
1709static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
1710 struct attribute *a, int n)
1711{
1712 struct device *dev = container_of(kobj, struct device, kobj);
1713 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1714
1715 if (a == &dev_attr_delete_controller.attr) {
1716 if (!ctrl->ops->delete_ctrl)
1717 return 0;
1718 }
1719
1720 CHECK_ATTR(ctrl, a, subsysnqn);
1721 CHECK_ATTR(ctrl, a, address);
1722
1723 return a->mode;
1724}
1725
Keith Busch779ff7562016-01-12 15:09:31 -07001726static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02001727 .attrs = nvme_dev_attrs,
1728 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07001729};
1730
1731static const struct attribute_group *nvme_dev_attr_groups[] = {
1732 &nvme_dev_attrs_group,
1733 NULL,
1734};
1735
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001736static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
1737{
1738 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
1739 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
1740
1741 return nsa->ns_id - nsb->ns_id;
1742}
1743
Keith Busch32f0c4a2016-07-13 11:45:02 -06001744static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001745{
Keith Busch32f0c4a2016-07-13 11:45:02 -06001746 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001747
Keith Busch32f0c4a2016-07-13 11:45:02 -06001748 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001749 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001750 if (ns->ns_id == nsid) {
1751 kref_get(&ns->kref);
1752 ret = ns;
1753 break;
1754 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001755 if (ns->ns_id > nsid)
1756 break;
1757 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001758 mutex_unlock(&ctrl->namespaces_mutex);
1759 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001760}
1761
1762static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1763{
1764 struct nvme_ns *ns;
1765 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001766 struct nvme_id_ns *id;
1767 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001768 int node = dev_to_node(ctrl->dev);
1769
1770 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
1771 if (!ns)
1772 return;
1773
Keith Busch075790e2016-02-24 09:15:53 -07001774 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
1775 if (ns->instance < 0)
1776 goto out_free_ns;
1777
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001778 ns->queue = blk_mq_init_queue(ctrl->tagset);
1779 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07001780 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001781 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
1782 ns->queue->queuedata = ns;
1783 ns->ctrl = ctrl;
1784
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001785 kref_init(&ns->kref);
1786 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001787 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001788
1789 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01001790 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001791
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001792 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001793
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001794 if (nvme_revalidate_ns(ns, &id))
1795 goto out_free_queue;
1796
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001797 if (nvme_nvm_ns_supported(ns, id) &&
1798 nvme_nvm_register(ns, disk_name, node)) {
1799 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
1800 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001801 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001802
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001803 disk = alloc_disk_node(0, node);
1804 if (!disk)
1805 goto out_free_id;
1806
1807 disk->fops = &nvme_fops;
1808 disk->private_data = ns;
1809 disk->queue = ns->queue;
1810 disk->flags = GENHD_FL_EXT_DEVT;
1811 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
1812 ns->disk = disk;
1813
1814 __nvme_revalidate_disk(disk, id);
1815
Keith Busch32f0c4a2016-07-13 11:45:02 -06001816 mutex_lock(&ctrl->namespaces_mutex);
1817 list_add_tail(&ns->list, &ctrl->namespaces);
1818 mutex_unlock(&ctrl->namespaces_mutex);
1819
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001820 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001821
1822 kfree(id);
1823
Dan Williams0d52c7562016-06-15 19:44:20 -07001824 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001825 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
1826 &nvme_ns_attr_group))
1827 pr_warn("%s: failed to create sysfs group for identification\n",
1828 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001829 if (ns->ndev && nvme_nvm_register_sysfs(ns))
1830 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
1831 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001832 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001833 out_free_id:
1834 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001835 out_free_queue:
1836 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07001837 out_release_instance:
1838 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001839 out_free_ns:
1840 kfree(ns);
1841}
1842
1843static void nvme_ns_remove(struct nvme_ns *ns)
1844{
Keith Busch646017a2016-02-24 09:15:54 -07001845 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
1846 return;
1847
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001848 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001849 if (blk_get_integrity(ns->disk))
1850 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001851 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
1852 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001853 if (ns->ndev)
1854 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001855 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001856 blk_mq_abort_requeue_list(ns->queue);
1857 blk_cleanup_queue(ns->queue);
1858 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06001859
1860 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001861 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001862 mutex_unlock(&ns->ctrl->namespaces_mutex);
1863
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001864 nvme_put_ns(ns);
1865}
1866
Keith Busch540c8012015-10-22 15:45:06 -06001867static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
1868{
1869 struct nvme_ns *ns;
1870
Keith Busch32f0c4a2016-07-13 11:45:02 -06001871 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06001872 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001873 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06001874 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001875 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06001876 } else
1877 nvme_alloc_ns(ctrl, nsid);
1878}
1879
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301880static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
1881 unsigned nsid)
1882{
1883 struct nvme_ns *ns, *next;
1884
1885 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
1886 if (ns->ns_id > nsid)
1887 nvme_ns_remove(ns);
1888 }
1889}
1890
Keith Busch540c8012015-10-22 15:45:06 -06001891static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
1892{
1893 struct nvme_ns *ns;
1894 __le32 *ns_list;
1895 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
1896 int ret = 0;
1897
1898 ns_list = kzalloc(0x1000, GFP_KERNEL);
1899 if (!ns_list)
1900 return -ENOMEM;
1901
1902 for (i = 0; i < num_lists; i++) {
1903 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
1904 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301905 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06001906
1907 for (j = 0; j < min(nn, 1024U); j++) {
1908 nsid = le32_to_cpu(ns_list[j]);
1909 if (!nsid)
1910 goto out;
1911
1912 nvme_validate_ns(ctrl, nsid);
1913
1914 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06001915 ns = nvme_find_get_ns(ctrl, prev);
1916 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06001917 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06001918 nvme_put_ns(ns);
1919 }
Keith Busch540c8012015-10-22 15:45:06 -06001920 }
1921 }
1922 nn -= j;
1923 }
1924 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301925 nvme_remove_invalid_namespaces(ctrl, prev);
1926 free:
Keith Busch540c8012015-10-22 15:45:06 -06001927 kfree(ns_list);
1928 return ret;
1929}
1930
Christoph Hellwig5955be22016-04-26 13:51:59 +02001931static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001932{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001933 unsigned i;
1934
Keith Busch540c8012015-10-22 15:45:06 -06001935 for (i = 1; i <= nn; i++)
1936 nvme_validate_ns(ctrl, i);
1937
Sunad Bhandary47b0e502016-05-27 15:59:43 +05301938 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001939}
1940
Christoph Hellwig5955be22016-04-26 13:51:59 +02001941static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001942{
Christoph Hellwig5955be22016-04-26 13:51:59 +02001943 struct nvme_ctrl *ctrl =
1944 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001945 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06001946 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001947
Christoph Hellwig5955be22016-04-26 13:51:59 +02001948 if (ctrl->state != NVME_CTRL_LIVE)
1949 return;
1950
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001951 if (nvme_identify_ctrl(ctrl, &id))
1952 return;
Keith Busch540c8012015-10-22 15:45:06 -06001953
1954 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001955 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06001956 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
1957 if (!nvme_scan_ns_list(ctrl, nn))
1958 goto done;
1959 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02001960 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06001961 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06001962 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06001963 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01001964 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001965 kfree(id);
1966}
Christoph Hellwig5955be22016-04-26 13:51:59 +02001967
1968void nvme_queue_scan(struct nvme_ctrl *ctrl)
1969{
1970 /*
1971 * Do not queue new scan work when a controller is reset during
1972 * removal.
1973 */
1974 if (ctrl->state == NVME_CTRL_LIVE)
1975 schedule_work(&ctrl->scan_work);
1976}
1977EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001978
Keith Busch32f0c4a2016-07-13 11:45:02 -06001979/*
1980 * This function iterates the namespace list unlocked to allow recovery from
1981 * controller failure. It is up to the caller to ensure the namespace list is
1982 * not modified by scan work while this function is executing.
1983 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001984void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
1985{
1986 struct nvme_ns *ns, *next;
1987
Keith Busch0ff9d4e2016-05-12 08:37:14 -06001988 /*
1989 * The dead states indicates the controller was not gracefully
1990 * disconnected. In that case, we won't be able to flush any data while
1991 * removing the namespaces' disks; fail all the queues now to avoid
1992 * potentially having to clean up the failed sync later.
1993 */
1994 if (ctrl->state == NVME_CTRL_DEAD)
1995 nvme_kill_queues(ctrl);
1996
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001997 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
1998 nvme_ns_remove(ns);
1999}
Ming Lin576d55d2016-02-10 10:03:32 -08002000EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002001
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002002static void nvme_async_event_work(struct work_struct *work)
2003{
2004 struct nvme_ctrl *ctrl =
2005 container_of(work, struct nvme_ctrl, async_event_work);
2006
2007 spin_lock_irq(&ctrl->lock);
2008 while (ctrl->event_limit > 0) {
2009 int aer_idx = --ctrl->event_limit;
2010
2011 spin_unlock_irq(&ctrl->lock);
2012 ctrl->ops->submit_async_event(ctrl, aer_idx);
2013 spin_lock_irq(&ctrl->lock);
2014 }
2015 spin_unlock_irq(&ctrl->lock);
2016}
2017
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002018void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2019 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002020{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002021 u32 result = le32_to_cpu(res->u32);
2022 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002023
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002024 switch (le16_to_cpu(status) >> 1) {
2025 case NVME_SC_SUCCESS:
2026 done = false;
2027 /*FALLTHRU*/
2028 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002029 ++ctrl->event_limit;
2030 schedule_work(&ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002031 break;
2032 default:
2033 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002034 }
2035
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002036 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002037 return;
2038
2039 switch (result & 0xff07) {
2040 case NVME_AER_NOTICE_NS_CHANGED:
2041 dev_info(ctrl->device, "rescanning\n");
2042 nvme_queue_scan(ctrl);
2043 break;
2044 default:
2045 dev_warn(ctrl->device, "async event result %08x\n", result);
2046 }
2047}
2048EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2049
2050void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2051{
2052 ctrl->event_limit = NVME_NR_AERS;
2053 schedule_work(&ctrl->async_event_work);
2054}
2055EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2056
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002057static DEFINE_IDA(nvme_instance_ida);
2058
2059static int nvme_set_instance(struct nvme_ctrl *ctrl)
2060{
2061 int instance, error;
2062
2063 do {
2064 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2065 return -ENODEV;
2066
2067 spin_lock(&dev_list_lock);
2068 error = ida_get_new(&nvme_instance_ida, &instance);
2069 spin_unlock(&dev_list_lock);
2070 } while (error == -EAGAIN);
2071
2072 if (error)
2073 return -ENODEV;
2074
2075 ctrl->instance = instance;
2076 return 0;
2077}
2078
2079static void nvme_release_instance(struct nvme_ctrl *ctrl)
2080{
2081 spin_lock(&dev_list_lock);
2082 ida_remove(&nvme_instance_ida, ctrl->instance);
2083 spin_unlock(&dev_list_lock);
2084}
2085
Keith Busch53029b02015-11-28 15:41:02 +01002086void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002087{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002088 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002089 flush_work(&ctrl->scan_work);
2090 nvme_remove_namespaces(ctrl);
2091
Keith Busch53029b02015-11-28 15:41:02 +01002092 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002093
2094 spin_lock(&dev_list_lock);
2095 list_del(&ctrl->node);
2096 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002097}
Ming Lin576d55d2016-02-10 10:03:32 -08002098EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002099
2100static void nvme_free_ctrl(struct kref *kref)
2101{
2102 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002103
2104 put_device(ctrl->device);
2105 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002106 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002107
2108 ctrl->ops->free_ctrl(ctrl);
2109}
2110
2111void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2112{
2113 kref_put(&ctrl->kref, nvme_free_ctrl);
2114}
Ming Lin576d55d2016-02-10 10:03:32 -08002115EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002116
2117/*
2118 * Initialize a NVMe controller structures. This needs to be called during
2119 * earliest initialization so that we have the initialized structured around
2120 * during probing.
2121 */
2122int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2123 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2124{
2125 int ret;
2126
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002127 ctrl->state = NVME_CTRL_NEW;
2128 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002129 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002130 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002131 kref_init(&ctrl->kref);
2132 ctrl->dev = dev;
2133 ctrl->ops = ops;
2134 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002135 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002136 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002137
2138 ret = nvme_set_instance(ctrl);
2139 if (ret)
2140 goto out;
2141
Keith Busch779ff7562016-01-12 15:09:31 -07002142 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002143 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002144 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002145 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002146 if (IS_ERR(ctrl->device)) {
2147 ret = PTR_ERR(ctrl->device);
2148 goto out_release_instance;
2149 }
2150 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002151 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002152
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002153 spin_lock(&dev_list_lock);
2154 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2155 spin_unlock(&dev_list_lock);
2156
2157 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002158out_release_instance:
2159 nvme_release_instance(ctrl);
2160out:
2161 return ret;
2162}
Ming Lin576d55d2016-02-10 10:03:32 -08002163EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002164
Keith Busch69d9a992016-02-24 09:15:56 -07002165/**
2166 * nvme_kill_queues(): Ends all namespace queues
2167 * @ctrl: the dead controller that needs to end
2168 *
2169 * Call this function when the driver determines it is unable to get the
2170 * controller in a state capable of servicing IO.
2171 */
2172void nvme_kill_queues(struct nvme_ctrl *ctrl)
2173{
2174 struct nvme_ns *ns;
2175
Keith Busch32f0c4a2016-07-13 11:45:02 -06002176 mutex_lock(&ctrl->namespaces_mutex);
2177 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002178 /*
2179 * Revalidating a dead namespace sets capacity to 0. This will
2180 * end buffered writers dirtying pages that can't be synced.
2181 */
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002182 if (ns->disk && !test_and_set_bit(NVME_NS_DEAD, &ns->flags))
Keith Busch69d9a992016-02-24 09:15:56 -07002183 revalidate_disk(ns->disk);
2184
2185 blk_set_queue_dying(ns->queue);
2186 blk_mq_abort_requeue_list(ns->queue);
2187 blk_mq_start_stopped_hw_queues(ns->queue, true);
Keith Busch69d9a992016-02-24 09:15:56 -07002188 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002189 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002190}
Linus Torvalds237045f2016-03-18 17:13:31 -07002191EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002192
Keith Busch25646262016-01-04 09:10:57 -07002193void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002194{
2195 struct nvme_ns *ns;
2196
Keith Busch32f0c4a2016-07-13 11:45:02 -06002197 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002198 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002199 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002200 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002201}
Ming Lin576d55d2016-02-10 10:03:32 -08002202EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002203
Keith Busch25646262016-01-04 09:10:57 -07002204void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002205{
2206 struct nvme_ns *ns;
2207
Keith Busch32f0c4a2016-07-13 11:45:02 -06002208 mutex_lock(&ctrl->namespaces_mutex);
2209 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002210 blk_mq_start_stopped_hw_queues(ns->queue, true);
2211 blk_mq_kick_requeue_list(ns->queue);
2212 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002213 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002214}
Ming Lin576d55d2016-02-10 10:03:32 -08002215EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002216
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002217int __init nvme_core_init(void)
2218{
2219 int result;
2220
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002221 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2222 &nvme_dev_fops);
2223 if (result < 0)
NeilBrownb09dcf52016-07-13 11:03:58 -07002224 return result;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002225 else if (result > 0)
2226 nvme_char_major = result;
2227
2228 nvme_class = class_create(THIS_MODULE, "nvme");
2229 if (IS_ERR(nvme_class)) {
2230 result = PTR_ERR(nvme_class);
2231 goto unregister_chrdev;
2232 }
2233
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002234 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002235
2236 unregister_chrdev:
2237 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002238 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002239}
2240
2241void nvme_core_exit(void)
2242{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002243 class_destroy(nvme_class);
2244 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002245}
Ming Lin576d55d2016-02-10 10:03:32 -08002246
2247MODULE_LICENSE("GPL");
2248MODULE_VERSION("1.0");
2249module_init(nvme_core_init);
2250module_exit(nvme_core_exit);