blob: 17118ef63c592c594387de2ec304cf428b8bebd0 [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>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <scsi/sg.h>
31#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010032
33#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020034#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010035
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010036#define NVME_MINORS (1U << MINORBITS)
37
Ming Linba0ba7d2016-02-10 10:03:30 -080038unsigned char admin_timeout = 60;
39module_param(admin_timeout, byte, 0644);
40MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080041EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080042
43unsigned char nvme_io_timeout = 30;
44module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
45MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080046EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080047
48unsigned char shutdown_timeout = 5;
49module_param(shutdown_timeout, byte, 0644);
50MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
51
Christoph Hellwig44e44b22017-04-05 19:18:11 +020052static u8 nvme_max_retries = 5;
53module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070054MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
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
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080059static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080060module_param(default_ps_max_latency_us, ulong, 0644);
61MODULE_PARM_DESC(default_ps_max_latency_us,
62 "max power saving latency for new devices; use PM QOS to change per device");
63
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070064static bool force_apst;
65module_param(force_apst, bool, 0644);
66MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010071static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080072static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010073
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010074static struct class *nvme_class;
75
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020076static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020077{
78 switch (nvme_req(req)->status & 0x7ff) {
79 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020080 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020081 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020082 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +020083 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020084 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +020085 case NVME_SC_WRITE_FAULT:
86 case NVME_SC_READ_ERROR:
87 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +020088 return BLK_STS_MEDIUM;
89 default:
90 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020091 }
92}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020093
Christoph Hellwigf6324b12017-04-05 19:18:09 +020094static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +020095{
Christoph Hellwigf6324b12017-04-05 19:18:09 +020096 if (blk_noretry_request(req))
97 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +020098 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +020099 return false;
100 if (jiffies - req->start_time >= req->timeout)
101 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200102 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200103 return false;
104 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200105}
106
107void nvme_complete_rq(struct request *req)
108{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200109 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
110 nvme_req(req)->retries++;
111 blk_mq_requeue_request(req, !blk_mq_queue_stopped(req->q));
112 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200113 }
114
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200115 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200116}
117EXPORT_SYMBOL_GPL(nvme_complete_rq);
118
Ming Linc55a2fd2016-05-18 14:05:02 -0700119void nvme_cancel_request(struct request *req, void *data, bool reserved)
120{
121 int status;
122
123 if (!blk_mq_request_started(req))
124 return;
125
126 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
127 "Cancelling I/O %d", req->tag);
128
129 status = NVME_SC_ABORT_REQ;
130 if (blk_queue_dying(req->q))
131 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200132 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200133 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200134
Ming Linc55a2fd2016-05-18 14:05:02 -0700135}
136EXPORT_SYMBOL_GPL(nvme_cancel_request);
137
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200138bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
139 enum nvme_ctrl_state new_state)
140{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300141 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200142 bool changed = false;
143
144 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300145
146 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200147 switch (new_state) {
148 case NVME_CTRL_LIVE:
149 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200150 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200151 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900152 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200153 changed = true;
154 /* FALLTHRU */
155 default:
156 break;
157 }
158 break;
159 case NVME_CTRL_RESETTING:
160 switch (old_state) {
161 case NVME_CTRL_NEW:
162 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900163 changed = true;
164 /* FALLTHRU */
165 default:
166 break;
167 }
168 break;
169 case NVME_CTRL_RECONNECTING:
170 switch (old_state) {
171 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200172 changed = true;
173 /* FALLTHRU */
174 default:
175 break;
176 }
177 break;
178 case NVME_CTRL_DELETING:
179 switch (old_state) {
180 case NVME_CTRL_LIVE:
181 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900182 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200183 changed = true;
184 /* FALLTHRU */
185 default:
186 break;
187 }
188 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600189 case NVME_CTRL_DEAD:
190 switch (old_state) {
191 case NVME_CTRL_DELETING:
192 changed = true;
193 /* FALLTHRU */
194 default:
195 break;
196 }
197 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200198 default:
199 break;
200 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200201
202 if (changed)
203 ctrl->state = new_state;
204
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300205 spin_unlock_irq(&ctrl->lock);
206
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200207 return changed;
208}
209EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
210
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100211static void nvme_free_ns(struct kref *kref)
212{
213 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
214
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200215 if (ns->ndev)
216 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100217
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200218 if (ns->disk) {
219 spin_lock(&dev_list_lock);
220 ns->disk->private_data = NULL;
221 spin_unlock(&dev_list_lock);
222 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100223
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100224 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700225 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
226 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100227 kfree(ns);
228}
229
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100230static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100231{
232 kref_put(&ns->kref, nvme_free_ns);
233}
234
235static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
236{
237 struct nvme_ns *ns;
238
239 spin_lock(&dev_list_lock);
240 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800241 if (ns) {
242 if (!kref_get_unless_zero(&ns->kref))
243 goto fail;
244 if (!try_module_get(ns->ctrl->ops->module))
245 goto fail_put_ns;
246 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100247 spin_unlock(&dev_list_lock);
248
249 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800250
251fail_put_ns:
252 kref_put(&ns->kref, nvme_free_ns);
253fail:
254 spin_unlock(&dev_list_lock);
255 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100256}
257
Christoph Hellwig41609822015-11-20 09:00:02 +0100258struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200259 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100260{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100261 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100262 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100263
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200264 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100265 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200266 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100267 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200268 qid ? qid - 1 : 0);
269 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100270 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100271 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100272
Christoph Hellwig21d34712015-11-26 09:08:36 +0100273 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800274 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100275
Christoph Hellwig41609822015-11-20 09:00:02 +0100276 return req;
277}
Ming Lin576d55d2016-02-10 10:03:32 -0800278EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100279
Ming Lin8093f7c2016-04-12 13:10:14 -0600280static inline void nvme_setup_flush(struct nvme_ns *ns,
281 struct nvme_command *cmnd)
282{
283 memset(cmnd, 0, sizeof(*cmnd));
284 cmnd->common.opcode = nvme_cmd_flush;
285 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
286}
287
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200288static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600289 struct nvme_command *cmnd)
290{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100291 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600292 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100293 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600294
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100295 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600296 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200297 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600298
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100299 __rq_for_each_bio(bio, req) {
300 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
301 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
302
303 range[n].cattr = cpu_to_le32(0);
304 range[n].nlb = cpu_to_le32(nlb);
305 range[n].slba = cpu_to_le64(slba);
306 n++;
307 }
308
309 if (WARN_ON_ONCE(n != segments)) {
310 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200311 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100312 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600313
314 memset(cmnd, 0, sizeof(*cmnd));
315 cmnd->dsm.opcode = nvme_cmd_dsm;
316 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200317 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600318 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
319
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700320 req->special_vec.bv_page = virt_to_page(range);
321 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100322 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700323 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600324
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200325 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600326}
Ming Lin8093f7c2016-04-12 13:10:14 -0600327
Ming Lin8093f7c2016-04-12 13:10:14 -0600328static inline void nvme_setup_rw(struct nvme_ns *ns, struct request *req,
329 struct nvme_command *cmnd)
330{
331 u16 control = 0;
332 u32 dsmgmt = 0;
333
334 if (req->cmd_flags & REQ_FUA)
335 control |= NVME_RW_FUA;
336 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
337 control |= NVME_RW_LR;
338
339 if (req->cmd_flags & REQ_RAHEAD)
340 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
341
342 memset(cmnd, 0, sizeof(*cmnd));
343 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600344 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
345 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
346 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
347
348 if (ns->ms) {
349 switch (ns->pi_type) {
350 case NVME_NS_DPS_PI_TYPE3:
351 control |= NVME_RW_PRINFO_PRCHK_GUARD;
352 break;
353 case NVME_NS_DPS_PI_TYPE1:
354 case NVME_NS_DPS_PI_TYPE2:
355 control |= NVME_RW_PRINFO_PRCHK_GUARD |
356 NVME_RW_PRINFO_PRCHK_REF;
357 cmnd->rw.reftag = cpu_to_le32(
358 nvme_block_nr(ns, blk_rq_pos(req)));
359 break;
360 }
361 if (!blk_integrity_rq(req))
362 control |= NVME_RW_PRINFO_PRACT;
363 }
364
365 cmnd->rw.control = cpu_to_le16(control);
366 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
367}
368
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200369blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600370 struct nvme_command *cmd)
371{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200372 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600373
Christoph Hellwig987f6992017-04-05 19:18:08 +0200374 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200375 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200376 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200377 req->rq_flags |= RQF_DONTPREP;
378 }
379
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100380 switch (req_op(req)) {
381 case REQ_OP_DRV_IN:
382 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800383 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100384 break;
385 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600386 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100387 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200388 case REQ_OP_WRITE_ZEROES:
389 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100390 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600391 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100392 break;
393 case REQ_OP_READ:
394 case REQ_OP_WRITE:
Ming Lin8093f7c2016-04-12 13:10:14 -0600395 nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100396 break;
397 default:
398 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200399 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100400 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600401
James Smart721b3912016-10-21 23:33:34 +0300402 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600403 return ret;
404}
405EXPORT_SYMBOL_GPL(nvme_setup_cmd);
406
Christoph Hellwig41609822015-11-20 09:00:02 +0100407/*
408 * Returns 0 on success. If the result is negative, it's a Linux error code;
409 * if the result is positive, it's an NVM Express status code
410 */
411int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800412 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200413 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100414{
415 struct request *req;
416 int ret;
417
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200418 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100419 if (IS_ERR(req))
420 return PTR_ERR(req);
421
422 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
423
Christoph Hellwig21d34712015-11-26 09:08:36 +0100424 if (buffer && bufflen) {
425 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
426 if (ret)
427 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100428 }
429
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200430 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800431 if (result)
432 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200433 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
434 ret = -EINTR;
435 else
436 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100437 out:
438 blk_mq_free_request(req);
439 return ret;
440}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200441EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100442
443int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
444 void *buffer, unsigned bufflen)
445{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200446 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
447 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100448}
Ming Lin576d55d2016-02-10 10:03:32 -0800449EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100450
Keith Busch0b7f1f22015-10-23 09:47:28 -0600451int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
452 void __user *ubuffer, unsigned bufflen,
453 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
454 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100455{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200456 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600457 struct nvme_ns *ns = q->queuedata;
458 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100459 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600460 struct bio *bio = NULL;
461 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100462 int ret;
463
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200464 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100465 if (IS_ERR(req))
466 return PTR_ERR(req);
467
468 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
469
470 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100471 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
472 GFP_KERNEL);
473 if (ret)
474 goto out;
475 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100476
Keith Busch0b7f1f22015-10-23 09:47:28 -0600477 if (!disk)
478 goto submit;
479 bio->bi_bdev = bdget_disk(disk, 0);
480 if (!bio->bi_bdev) {
481 ret = -ENODEV;
482 goto out_unmap;
483 }
484
Keith Busche9fc63d2016-02-24 09:15:58 -0700485 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600486 struct bio_integrity_payload *bip;
487
488 meta = kmalloc(meta_len, GFP_KERNEL);
489 if (!meta) {
490 ret = -ENOMEM;
491 goto out_unmap;
492 }
493
494 if (write) {
495 if (copy_from_user(meta, meta_buffer,
496 meta_len)) {
497 ret = -EFAULT;
498 goto out_free_meta;
499 }
500 }
501
502 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700503 if (IS_ERR(bip)) {
504 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600505 goto out_free_meta;
506 }
507
508 bip->bip_iter.bi_size = meta_len;
509 bip->bip_iter.bi_sector = meta_seed;
510
511 ret = bio_integrity_add_page(bio, virt_to_page(meta),
512 meta_len, offset_in_page(meta));
513 if (ret != meta_len) {
514 ret = -ENOMEM;
515 goto out_free_meta;
516 }
517 }
518 }
519 submit:
520 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200521 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
522 ret = -EINTR;
523 else
524 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100525 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800526 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600527 if (meta && !ret && !write) {
528 if (copy_to_user(meta_buffer, meta, meta_len))
529 ret = -EFAULT;
530 }
531 out_free_meta:
532 kfree(meta);
533 out_unmap:
534 if (bio) {
535 if (disk && bio->bi_bdev)
536 bdput(bio->bi_bdev);
537 blk_rq_unmap_user(bio);
538 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100539 out:
540 blk_mq_free_request(req);
541 return ret;
542}
543
Keith Busch0b7f1f22015-10-23 09:47:28 -0600544int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
545 void __user *ubuffer, unsigned bufflen, u32 *result,
546 unsigned timeout)
547{
548 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
549 result, timeout);
550}
551
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200552static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200553{
554 struct nvme_ctrl *ctrl = rq->end_io_data;
555
556 blk_mq_free_request(rq);
557
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200558 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200559 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200560 "failed nvme_keep_alive_end_io error=%d\n",
561 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200562 return;
563 }
564
565 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
566}
567
568static int nvme_keep_alive(struct nvme_ctrl *ctrl)
569{
570 struct nvme_command c;
571 struct request *rq;
572
573 memset(&c, 0, sizeof(c));
574 c.common.opcode = nvme_admin_keep_alive;
575
576 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
577 NVME_QID_ANY);
578 if (IS_ERR(rq))
579 return PTR_ERR(rq);
580
581 rq->timeout = ctrl->kato * HZ;
582 rq->end_io_data = ctrl;
583
584 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
585
586 return 0;
587}
588
589static void nvme_keep_alive_work(struct work_struct *work)
590{
591 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
592 struct nvme_ctrl, ka_work);
593
594 if (nvme_keep_alive(ctrl)) {
595 /* allocation failure, reset the controller */
596 dev_err(ctrl->device, "keep-alive failed\n");
597 ctrl->ops->reset_ctrl(ctrl);
598 return;
599 }
600}
601
602void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
603{
604 if (unlikely(ctrl->kato == 0))
605 return;
606
607 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
608 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
609}
610EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
611
612void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
613{
614 if (unlikely(ctrl->kato == 0))
615 return;
616
617 cancel_delayed_work_sync(&ctrl->ka_work);
618}
619EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
620
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100621int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100622{
623 struct nvme_command c = { };
624 int error;
625
626 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
627 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200628 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100629
630 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
631 if (!*id)
632 return -ENOMEM;
633
634 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
635 sizeof(struct nvme_id_ctrl));
636 if (error)
637 kfree(*id);
638 return error;
639}
640
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200641static int nvme_identify_ns_descs(struct nvme_ns *ns, unsigned nsid)
642{
643 struct nvme_command c = { };
644 int status;
645 void *data;
646 int pos;
647 int len;
648
649 c.identify.opcode = nvme_admin_identify;
650 c.identify.nsid = cpu_to_le32(nsid);
651 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
652
653 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
654 if (!data)
655 return -ENOMEM;
656
657 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, data,
658 NVME_IDENTIFY_DATA_SIZE);
659 if (status)
660 goto free_data;
661
662 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
663 struct nvme_ns_id_desc *cur = data + pos;
664
665 if (cur->nidl == 0)
666 break;
667
668 switch (cur->nidt) {
669 case NVME_NIDT_EUI64:
670 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
671 dev_warn(ns->ctrl->device,
672 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
673 cur->nidl);
674 goto free_data;
675 }
676 len = NVME_NIDT_EUI64_LEN;
677 memcpy(ns->eui, data + pos + sizeof(*cur), len);
678 break;
679 case NVME_NIDT_NGUID:
680 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
681 dev_warn(ns->ctrl->device,
682 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
683 cur->nidl);
684 goto free_data;
685 }
686 len = NVME_NIDT_NGUID_LEN;
687 memcpy(ns->nguid, data + pos + sizeof(*cur), len);
688 break;
689 case NVME_NIDT_UUID:
690 if (cur->nidl != NVME_NIDT_UUID_LEN) {
691 dev_warn(ns->ctrl->device,
692 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
693 cur->nidl);
694 goto free_data;
695 }
696 len = NVME_NIDT_UUID_LEN;
697 uuid_copy(&ns->uuid, data + pos + sizeof(*cur));
698 break;
699 default:
700 /* Skip unnkown types */
701 len = cur->nidl;
702 break;
703 }
704
705 len += sizeof(*cur);
706 }
707free_data:
708 kfree(data);
709 return status;
710}
711
Keith Busch540c8012015-10-22 15:45:06 -0600712static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
713{
714 struct nvme_command c = { };
715
716 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200717 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600718 c.identify.nsid = cpu_to_le32(nsid);
719 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
720}
721
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100722int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100723 struct nvme_id_ns **id)
724{
725 struct nvme_command c = { };
726 int error;
727
728 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200729 c.identify.opcode = nvme_admin_identify;
730 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200731 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100732
733 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
734 if (!*id)
735 return -ENOMEM;
736
737 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
738 sizeof(struct nvme_id_ns));
739 if (error)
740 kfree(*id);
741 return error;
742}
743
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100744int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700745 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100746{
747 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800748 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100749 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100750
751 memset(&c, 0, sizeof(c));
752 c.features.opcode = nvme_admin_get_features;
753 c.features.nsid = cpu_to_le32(nsid);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100754 c.features.fid = cpu_to_le32(fid);
755
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800756 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res, buffer, buflen, 0,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200757 NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700758 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800759 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100760 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100761}
762
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100763int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700764 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100765{
766 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800767 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100768 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100769
770 memset(&c, 0, sizeof(c));
771 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100772 c.features.fid = cpu_to_le32(fid);
773 c.features.dword11 = cpu_to_le32(dword11);
774
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800775 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700776 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700777 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800778 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100779 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100780}
781
Christoph Hellwig1c63dc62015-11-26 10:06:56 +0100782int nvme_get_log_page(struct nvme_ctrl *dev, struct nvme_smart_log **log)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100783{
784 struct nvme_command c = { };
785 int error;
786
787 c.common.opcode = nvme_admin_get_log_page,
788 c.common.nsid = cpu_to_le32(0xFFFFFFFF),
789 c.common.cdw10[0] = cpu_to_le32(
790 (((sizeof(struct nvme_smart_log) / 4) - 1) << 16) |
791 NVME_LOG_SMART),
792
793 *log = kmalloc(sizeof(struct nvme_smart_log), GFP_KERNEL);
794 if (!*log)
795 return -ENOMEM;
796
797 error = nvme_submit_sync_cmd(dev->admin_q, &c, *log,
798 sizeof(struct nvme_smart_log));
799 if (error)
800 kfree(*log);
801 return error;
802}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100803
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100804int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
805{
806 u32 q_count = (*count - 1) | ((*count - 1) << 16);
807 u32 result;
808 int status, nr_io_queues;
809
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700810 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100811 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200812 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100813 return status;
814
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200815 /*
816 * Degraded controllers might return an error when setting the queue
817 * count. We still want to be able to bring them online and offer
818 * access to the admin queue, as that might be only way to fix them up.
819 */
820 if (status > 0) {
821 dev_err(ctrl->dev, "Could not set queue count (%d)\n", status);
822 *count = 0;
823 } else {
824 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
825 *count = min(*count, nr_io_queues);
826 }
827
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100828 return 0;
829}
Ming Lin576d55d2016-02-10 10:03:32 -0800830EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100831
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100832static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
833{
834 struct nvme_user_io io;
835 struct nvme_command c;
836 unsigned length, meta_len;
837 void __user *metadata;
838
839 if (copy_from_user(&io, uio, sizeof(io)))
840 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700841 if (io.flags)
842 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100843
844 switch (io.opcode) {
845 case nvme_cmd_write:
846 case nvme_cmd_read:
847 case nvme_cmd_compare:
848 break;
849 default:
850 return -EINVAL;
851 }
852
853 length = (io.nblocks + 1) << ns->lba_shift;
854 meta_len = (io.nblocks + 1) * ns->ms;
855 metadata = (void __user *)(uintptr_t)io.metadata;
856
857 if (ns->ext) {
858 length += meta_len;
859 meta_len = 0;
860 } else if (meta_len) {
861 if ((io.metadata & 3) || !io.metadata)
862 return -EINVAL;
863 }
864
865 memset(&c, 0, sizeof(c));
866 c.rw.opcode = io.opcode;
867 c.rw.flags = io.flags;
868 c.rw.nsid = cpu_to_le32(ns->ns_id);
869 c.rw.slba = cpu_to_le64(io.slba);
870 c.rw.length = cpu_to_le16(io.nblocks);
871 c.rw.control = cpu_to_le16(io.control);
872 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
873 c.rw.reftag = cpu_to_le32(io.reftag);
874 c.rw.apptag = cpu_to_le16(io.apptag);
875 c.rw.appmask = cpu_to_le16(io.appmask);
876
877 return __nvme_submit_user_cmd(ns->queue, &c,
878 (void __user *)(uintptr_t)io.addr, length,
879 metadata, meta_len, io.slba, NULL, 0);
880}
881
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100882static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100883 struct nvme_passthru_cmd __user *ucmd)
884{
885 struct nvme_passthru_cmd cmd;
886 struct nvme_command c;
887 unsigned timeout = 0;
888 int status;
889
890 if (!capable(CAP_SYS_ADMIN))
891 return -EACCES;
892 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
893 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700894 if (cmd.flags)
895 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100896
897 memset(&c, 0, sizeof(c));
898 c.common.opcode = cmd.opcode;
899 c.common.flags = cmd.flags;
900 c.common.nsid = cpu_to_le32(cmd.nsid);
901 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
902 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
903 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
904 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
905 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
906 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
907 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
908 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
909
910 if (cmd.timeout_ms)
911 timeout = msecs_to_jiffies(cmd.timeout_ms);
912
913 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +0100914 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100915 &cmd.result, timeout);
916 if (status >= 0) {
917 if (put_user(cmd.result, &ucmd->result))
918 return -EFAULT;
919 }
920
921 return status;
922}
923
924static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
925 unsigned int cmd, unsigned long arg)
926{
927 struct nvme_ns *ns = bdev->bd_disk->private_data;
928
929 switch (cmd) {
930 case NVME_IOCTL_ID:
931 force_successful_syscall_return();
932 return ns->ns_id;
933 case NVME_IOCTL_ADMIN_CMD:
934 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
935 case NVME_IOCTL_IO_CMD:
936 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
937 case NVME_IOCTL_SUBMIT_IO:
938 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100939#ifdef CONFIG_BLK_DEV_NVME_SCSI
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100940 case SG_GET_VERSION_NUM:
941 return nvme_sg_get_version_num((void __user *)arg);
942 case SG_IO:
943 return nvme_sg_io(ns, (void __user *)arg);
Christoph Hellwig44907332015-12-24 15:27:02 +0100944#endif
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100945 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +0100946#ifdef CONFIG_NVM
947 if (ns->ndev)
948 return nvme_nvm_ioctl(ns, cmd, arg);
949#endif
Scott Bauera98e58e52017-02-03 12:50:32 -0700950 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +0100951 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -0700952 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100953 return -ENOTTY;
954 }
955}
956
957#ifdef CONFIG_COMPAT
958static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
959 unsigned int cmd, unsigned long arg)
960{
961 switch (cmd) {
962 case SG_IO:
963 return -ENOIOCTLCMD;
964 }
965 return nvme_ioctl(bdev, mode, cmd, arg);
966}
967#else
968#define nvme_compat_ioctl NULL
969#endif
970
971static int nvme_open(struct block_device *bdev, fmode_t mode)
972{
973 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
974}
975
976static void nvme_release(struct gendisk *disk, fmode_t mode)
977{
Sagi Grimberge439bb12016-02-10 10:03:29 -0800978 struct nvme_ns *ns = disk->private_data;
979
980 module_put(ns->ctrl->ops->module);
981 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100982}
983
984static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
985{
986 /* some standard values */
987 geo->heads = 1 << 6;
988 geo->sectors = 1 << 5;
989 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
990 return 0;
991}
992
993#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +0200994static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
995 u16 bs)
996{
997 struct nvme_ns *ns = disk->private_data;
998 u16 old_ms = ns->ms;
999 u8 pi_type = 0;
1000
1001 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1002 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1003
1004 /* PI implementation requires metadata equal t10 pi tuple size */
1005 if (ns->ms == sizeof(struct t10_pi_tuple))
1006 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1007
1008 if (blk_get_integrity(disk) &&
1009 (ns->pi_type != pi_type || ns->ms != old_ms ||
1010 bs != queue_logical_block_size(disk->queue) ||
1011 (ns->ms && ns->ext)))
1012 blk_integrity_unregister(disk);
1013
1014 ns->pi_type = pi_type;
1015}
1016
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001017static void nvme_init_integrity(struct nvme_ns *ns)
1018{
1019 struct blk_integrity integrity;
1020
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001021 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001022 switch (ns->pi_type) {
1023 case NVME_NS_DPS_PI_TYPE3:
1024 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001025 integrity.tag_size = sizeof(u16) + sizeof(u32);
1026 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001027 break;
1028 case NVME_NS_DPS_PI_TYPE1:
1029 case NVME_NS_DPS_PI_TYPE2:
1030 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001031 integrity.tag_size = sizeof(u16);
1032 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001033 break;
1034 default:
1035 integrity.profile = NULL;
1036 break;
1037 }
1038 integrity.tuple_size = ns->ms;
1039 blk_integrity_register(ns->disk, &integrity);
1040 blk_queue_max_integrity_segments(ns->queue, 1);
1041}
1042#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001043static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1044 u16 bs)
1045{
1046}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001047static void nvme_init_integrity(struct nvme_ns *ns)
1048{
1049}
1050#endif /* CONFIG_BLK_DEV_INTEGRITY */
1051
1052static void nvme_config_discard(struct nvme_ns *ns)
1053{
Keith Busch08095e72016-03-04 13:15:17 -07001054 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001055 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001056
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001057 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1058 NVME_DSM_MAX_RANGES);
1059
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001060 ns->queue->limits.discard_alignment = logical_block_size;
1061 ns->queue->limits.discard_granularity = logical_block_size;
Minfei Huangbd0fc282016-05-17 15:58:41 +08001062 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001063 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001064 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001065
1066 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1067 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001068}
1069
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001070static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001071{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001072 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001073 dev_warn(ns->ctrl->dev, "%s: Identify failure\n", __func__);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001074 return -ENODEV;
1075 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001076
1077 if ((*id)->ncap == 0) {
1078 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001079 return -ENODEV;
1080 }
1081
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001082 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001083 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001084 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001085 memcpy(ns->nguid, (*id)->nguid, sizeof(ns->nguid));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001086 if (ns->ctrl->vs >= NVME_VS(1, 3, 0)) {
1087 /* Don't treat error as fatal we potentially
1088 * already have a NGUID or EUI-64
1089 */
1090 if (nvme_identify_ns_descs(ns, ns->ns_id))
1091 dev_warn(ns->ctrl->device,
1092 "%s: Identify Descriptors failed\n", __func__);
1093 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001094
1095 return 0;
1096}
1097
1098static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1099{
1100 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001101 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001102
1103 /*
1104 * If identify namespace failed, use default 512 byte block size so
1105 * block layer can use before failing read/write for 0 capacity.
1106 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001107 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001108 if (ns->lba_shift == 0)
1109 ns->lba_shift = 9;
1110 bs = 1 << ns->lba_shift;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111
1112 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001113
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001114 if (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
1115 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001116 blk_queue_logical_block_size(ns->queue, bs);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001117 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001118 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001119 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1120 set_capacity(disk, 0);
1121 else
1122 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1123
1124 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1125 nvme_config_discard(ns);
1126 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001127}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001128
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001129static int nvme_revalidate_disk(struct gendisk *disk)
1130{
1131 struct nvme_ns *ns = disk->private_data;
1132 struct nvme_id_ns *id = NULL;
1133 int ret;
1134
1135 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1136 set_capacity(disk, 0);
1137 return -ENODEV;
1138 }
1139
1140 ret = nvme_revalidate_ns(ns, &id);
1141 if (ret)
1142 return ret;
1143
1144 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001145 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001146
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001147 return 0;
1148}
1149
1150static char nvme_pr_type(enum pr_type type)
1151{
1152 switch (type) {
1153 case PR_WRITE_EXCLUSIVE:
1154 return 1;
1155 case PR_EXCLUSIVE_ACCESS:
1156 return 2;
1157 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1158 return 3;
1159 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1160 return 4;
1161 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1162 return 5;
1163 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1164 return 6;
1165 default:
1166 return 0;
1167 }
1168};
1169
1170static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1171 u64 key, u64 sa_key, u8 op)
1172{
1173 struct nvme_ns *ns = bdev->bd_disk->private_data;
1174 struct nvme_command c;
1175 u8 data[16] = { 0, };
1176
1177 put_unaligned_le64(key, &data[0]);
1178 put_unaligned_le64(sa_key, &data[8]);
1179
1180 memset(&c, 0, sizeof(c));
1181 c.common.opcode = op;
1182 c.common.nsid = cpu_to_le32(ns->ns_id);
1183 c.common.cdw10[0] = cpu_to_le32(cdw10);
1184
1185 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1186}
1187
1188static int nvme_pr_register(struct block_device *bdev, u64 old,
1189 u64 new, unsigned flags)
1190{
1191 u32 cdw10;
1192
1193 if (flags & ~PR_FL_IGNORE_KEY)
1194 return -EOPNOTSUPP;
1195
1196 cdw10 = old ? 2 : 0;
1197 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1198 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1199 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1200}
1201
1202static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1203 enum pr_type type, unsigned flags)
1204{
1205 u32 cdw10;
1206
1207 if (flags & ~PR_FL_IGNORE_KEY)
1208 return -EOPNOTSUPP;
1209
1210 cdw10 = nvme_pr_type(type) << 8;
1211 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1212 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1213}
1214
1215static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1216 enum pr_type type, bool abort)
1217{
1218 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1219 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1220}
1221
1222static int nvme_pr_clear(struct block_device *bdev, u64 key)
1223{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001224 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001225 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1226}
1227
1228static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1229{
1230 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1231 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1232}
1233
1234static const struct pr_ops nvme_pr_ops = {
1235 .pr_register = nvme_pr_register,
1236 .pr_reserve = nvme_pr_reserve,
1237 .pr_release = nvme_pr_release,
1238 .pr_preempt = nvme_pr_preempt,
1239 .pr_clear = nvme_pr_clear,
1240};
1241
Scott Bauera98e58e52017-02-03 12:50:32 -07001242#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001243int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1244 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001245{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001246 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001247 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001248
1249 memset(&cmd, 0, sizeof(cmd));
1250 if (send)
1251 cmd.common.opcode = nvme_admin_security_send;
1252 else
1253 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001254 cmd.common.nsid = 0;
1255 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1256 cmd.common.cdw10[1] = cpu_to_le32(len);
1257
1258 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1259 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1260}
1261EXPORT_SYMBOL_GPL(nvme_sec_submit);
1262#endif /* CONFIG_BLK_SED_OPAL */
1263
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001264static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001265 .owner = THIS_MODULE,
1266 .ioctl = nvme_ioctl,
1267 .compat_ioctl = nvme_compat_ioctl,
1268 .open = nvme_open,
1269 .release = nvme_release,
1270 .getgeo = nvme_getgeo,
1271 .revalidate_disk= nvme_revalidate_disk,
1272 .pr_ops = &nvme_pr_ops,
1273};
1274
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001275static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1276{
1277 unsigned long timeout =
1278 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1279 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1280 int ret;
1281
1282 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001283 if (csts == ~0)
1284 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001285 if ((csts & NVME_CSTS_RDY) == bit)
1286 break;
1287
1288 msleep(100);
1289 if (fatal_signal_pending(current))
1290 return -EINTR;
1291 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001292 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001293 "Device not ready; aborting %s\n", enabled ?
1294 "initialisation" : "reset");
1295 return -ENODEV;
1296 }
1297 }
1298
1299 return ret;
1300}
1301
1302/*
1303 * If the device has been passed off to us in an enabled state, just clear
1304 * the enabled bit. The spec says we should set the 'shutdown notification
1305 * bits', but doing so may cause the device to complete commands to the
1306 * admin queue ... and we don't know what memory that might be pointing at!
1307 */
1308int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1309{
1310 int ret;
1311
1312 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1313 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1314
1315 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1316 if (ret)
1317 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001318
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001319 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001320 msleep(NVME_QUIRK_DELAY_AMOUNT);
1321
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001322 return nvme_wait_ready(ctrl, cap, false);
1323}
Ming Lin576d55d2016-02-10 10:03:32 -08001324EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001325
1326int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1327{
1328 /*
1329 * Default to a 4K page size, with the intention to update this
1330 * path in the future to accomodate architectures with differing
1331 * kernel and IO page sizes.
1332 */
1333 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1334 int ret;
1335
1336 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001337 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001338 "Minimum device page size %u too large for host (%u)\n",
1339 1 << dev_page_min, 1 << page_shift);
1340 return -ENODEV;
1341 }
1342
1343 ctrl->page_size = 1 << page_shift;
1344
1345 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1346 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1347 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1348 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1349 ctrl->ctrl_config |= NVME_CC_ENABLE;
1350
1351 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1352 if (ret)
1353 return ret;
1354 return nvme_wait_ready(ctrl, cap, true);
1355}
Ming Lin576d55d2016-02-10 10:03:32 -08001356EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001357
1358int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1359{
1360 unsigned long timeout = SHUTDOWN_TIMEOUT + jiffies;
1361 u32 csts;
1362 int ret;
1363
1364 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1365 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1366
1367 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1368 if (ret)
1369 return ret;
1370
1371 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1372 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1373 break;
1374
1375 msleep(100);
1376 if (fatal_signal_pending(current))
1377 return -EINTR;
1378 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001379 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001380 "Device shutdown incomplete; abort shutdown\n");
1381 return -ENODEV;
1382 }
1383 }
1384
1385 return ret;
1386}
Ming Lin576d55d2016-02-10 10:03:32 -08001387EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001388
Christoph Hellwigda358252016-03-02 18:07:11 +01001389static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1390 struct request_queue *q)
1391{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001392 bool vwc = false;
1393
Christoph Hellwigda358252016-03-02 18:07:11 +01001394 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001395 u32 max_segments =
1396 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1397
Christoph Hellwigda358252016-03-02 18:07:11 +01001398 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001399 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001400 }
Keith Busche6282ae2016-12-19 11:37:50 -05001401 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1402 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001403 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001404 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1405 vwc = true;
1406 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001407}
1408
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001409static void nvme_configure_apst(struct nvme_ctrl *ctrl)
1410{
1411 /*
1412 * APST (Autonomous Power State Transition) lets us program a
1413 * table of power state transitions that the controller will
1414 * perform automatically. We configure it with a simple
1415 * heuristic: we are willing to spend at most 2% of the time
1416 * transitioning between power states. Therefore, when running
1417 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001418 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001419 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001420 * the requested maximum latency.
1421 *
1422 * We will not autonomously enter any non-operational state for
1423 * which the total latency exceeds ps_max_latency_us. Users
1424 * can set ps_max_latency_us to zero to turn off APST.
1425 */
1426
1427 unsigned apste;
1428 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001429 u64 max_lat_us = 0;
1430 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001431 int ret;
1432
1433 /*
1434 * If APST isn't supported or if we haven't been initialized yet,
1435 * then don't do anything.
1436 */
1437 if (!ctrl->apsta)
1438 return;
1439
1440 if (ctrl->npss > 31) {
1441 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
1442 return;
1443 }
1444
1445 table = kzalloc(sizeof(*table), GFP_KERNEL);
1446 if (!table)
1447 return;
1448
1449 if (ctrl->ps_max_latency_us == 0) {
1450 /* Turn off APST. */
1451 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001452 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001453 } else {
1454 __le64 target = cpu_to_le64(0);
1455 int state;
1456
1457 /*
1458 * Walk through all states from lowest- to highest-power.
1459 * According to the spec, lower-numbered states use more
1460 * power. NPSS, despite the name, is the index of the
1461 * lowest-power state, not the number of states.
1462 */
1463 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001464 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001465
1466 if (target)
1467 table->entries[state] = target;
1468
1469 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001470 * Don't allow transitions to the deepest state
1471 * if it's quirked off.
1472 */
1473 if (state == ctrl->npss &&
1474 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1475 continue;
1476
1477 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001478 * Is this state a useful non-operational state for
1479 * higher-power states to autonomously transition to?
1480 */
1481 if (!(ctrl->psd[state].flags &
1482 NVME_PS_FLAGS_NON_OP_STATE))
1483 continue;
1484
Kai-Heng Fengda875912017-06-07 15:25:42 +08001485 exit_latency_us =
1486 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1487 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001488 continue;
1489
Kai-Heng Fengda875912017-06-07 15:25:42 +08001490 total_latency_us =
1491 exit_latency_us +
1492 le32_to_cpu(ctrl->psd[state].entry_lat);
1493
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001494 /*
1495 * This state is good. Use it as the APST idle
1496 * target for higher power states.
1497 */
1498 transition_ms = total_latency_us + 19;
1499 do_div(transition_ms, 20);
1500 if (transition_ms > (1 << 24) - 1)
1501 transition_ms = (1 << 24) - 1;
1502
1503 target = cpu_to_le64((state << 3) |
1504 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001505
1506 if (max_ps == -1)
1507 max_ps = state;
1508
1509 if (total_latency_us > max_lat_us)
1510 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001511 }
1512
1513 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001514
1515 if (max_ps == -1) {
1516 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1517 } else {
1518 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1519 max_ps, max_lat_us, (int)sizeof(*table), table);
1520 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001521 }
1522
1523 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1524 table, sizeof(*table), NULL);
1525 if (ret)
1526 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1527
1528 kfree(table);
1529}
1530
1531static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1532{
1533 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1534 u64 latency;
1535
1536 switch (val) {
1537 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1538 case PM_QOS_LATENCY_ANY:
1539 latency = U64_MAX;
1540 break;
1541
1542 default:
1543 latency = val;
1544 }
1545
1546 if (ctrl->ps_max_latency_us != latency) {
1547 ctrl->ps_max_latency_us = latency;
1548 nvme_configure_apst(ctrl);
1549 }
1550}
1551
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001552struct nvme_core_quirk_entry {
1553 /*
1554 * NVMe model and firmware strings are padded with spaces. For
1555 * simplicity, strings in the quirk table are padded with NULLs
1556 * instead.
1557 */
1558 u16 vid;
1559 const char *mn;
1560 const char *fr;
1561 unsigned long quirks;
1562};
1563
1564static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001565 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001566 /*
1567 * This Toshiba device seems to die using any APST states. See:
1568 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1569 */
1570 .vid = 0x1179,
1571 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001572 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001573 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001574};
1575
1576/* match is null-terminated but idstr is space-padded. */
1577static bool string_matches(const char *idstr, const char *match, size_t len)
1578{
1579 size_t matchlen;
1580
1581 if (!match)
1582 return true;
1583
1584 matchlen = strlen(match);
1585 WARN_ON_ONCE(matchlen > len);
1586
1587 if (memcmp(idstr, match, matchlen))
1588 return false;
1589
1590 for (; matchlen < len; matchlen++)
1591 if (idstr[matchlen] != ' ')
1592 return false;
1593
1594 return true;
1595}
1596
1597static bool quirk_matches(const struct nvme_id_ctrl *id,
1598 const struct nvme_core_quirk_entry *q)
1599{
1600 return q->vid == le16_to_cpu(id->vid) &&
1601 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1602 string_matches(id->fr, q->fr, sizeof(id->fr));
1603}
1604
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001605/*
1606 * Initialize the cached copies of the Identify data and various controller
1607 * register in our nvme_ctrl structure. This should be called as soon as
1608 * the admin queue is fully up and running.
1609 */
1610int nvme_init_identify(struct nvme_ctrl *ctrl)
1611{
1612 struct nvme_id_ctrl *id;
1613 u64 cap;
1614 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001615 u32 max_hw_sectors;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001616 u8 prev_apsta;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001617
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001618 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1619 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001620 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001621 return ret;
1622 }
1623
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001624 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1625 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001626 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001627 return ret;
1628 }
1629 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1630
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001631 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001632 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1633
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001634 ret = nvme_identify_ctrl(ctrl, &id);
1635 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001636 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001637 return -EIO;
1638 }
1639
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001640 if (!ctrl->identified) {
1641 /*
1642 * Check for quirks. Quirk can depend on firmware version,
1643 * so, in principle, the set of quirks present can change
1644 * across a reset. As a possible future enhancement, we
1645 * could re-scan for quirks every time we reinitialize
1646 * the device, but we'd have to make sure that the driver
1647 * behaves intelligently if the quirks change.
1648 */
1649
1650 int i;
1651
1652 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1653 if (quirk_matches(id, &core_quirks[i]))
1654 ctrl->quirks |= core_quirks[i].quirks;
1655 }
1656 }
1657
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001658 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
1659 dev_warn(ctrl->dev, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
1660 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1661 }
1662
Scott Bauer8a9ae522017-02-17 13:59:40 +01001663 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001664 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001665 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001666 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001667 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001668 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001669 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1670 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1671 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1672 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001673 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001674 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001675 max_hw_sectors = UINT_MAX;
1676 ctrl->max_hw_sectors =
1677 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001678
Christoph Hellwigda358252016-03-02 18:07:11 +01001679 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001680 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001681 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001682
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001683 ctrl->npss = id->npss;
1684 prev_apsta = ctrl->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001685 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1686 if (force_apst && id->apsta) {
1687 dev_warn(ctrl->dev, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
1688 ctrl->apsta = 1;
1689 } else {
1690 ctrl->apsta = 0;
1691 }
1692 } else {
1693 ctrl->apsta = id->apsta;
1694 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001695 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1696
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001697 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001698 ctrl->icdoff = le16_to_cpu(id->icdoff);
1699 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1700 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1701 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1702
1703 /*
1704 * In fabrics we need to verify the cntlid matches the
1705 * admin connect
1706 */
1707 if (ctrl->cntlid != le16_to_cpu(id->cntlid))
1708 ret = -EINVAL;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001709
1710 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
1711 dev_err(ctrl->dev,
1712 "keep-alive support is mandatory for fabrics\n");
1713 ret = -EINVAL;
1714 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001715 } else {
1716 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001717 ctrl->hmpre = le32_to_cpu(id->hmpre);
1718 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001719 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001720
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001721 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001722
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001723 if (ctrl->apsta && !prev_apsta)
1724 dev_pm_qos_expose_latency_tolerance(ctrl->device);
1725 else if (!ctrl->apsta && prev_apsta)
1726 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1727
1728 nvme_configure_apst(ctrl);
1729
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001730 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001731
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001732 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001733}
Ming Lin576d55d2016-02-10 10:03:32 -08001734EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001735
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001736static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001737{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001738 struct nvme_ctrl *ctrl;
1739 int instance = iminor(inode);
1740 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001741
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001742 spin_lock(&dev_list_lock);
1743 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1744 if (ctrl->instance != instance)
1745 continue;
1746
1747 if (!ctrl->admin_q) {
1748 ret = -EWOULDBLOCK;
1749 break;
1750 }
1751 if (!kref_get_unless_zero(&ctrl->kref))
1752 break;
1753 file->private_data = ctrl;
1754 ret = 0;
1755 break;
1756 }
1757 spin_unlock(&dev_list_lock);
1758
1759 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001760}
1761
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001762static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001763{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001764 nvme_put_ctrl(file->private_data);
1765 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001766}
1767
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001768static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1769{
1770 struct nvme_ns *ns;
1771 int ret;
1772
1773 mutex_lock(&ctrl->namespaces_mutex);
1774 if (list_empty(&ctrl->namespaces)) {
1775 ret = -ENOTTY;
1776 goto out_unlock;
1777 }
1778
1779 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1780 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001781 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001782 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1783 ret = -EINVAL;
1784 goto out_unlock;
1785 }
1786
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001787 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001788 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1789 kref_get(&ns->kref);
1790 mutex_unlock(&ctrl->namespaces_mutex);
1791
1792 ret = nvme_user_cmd(ctrl, ns, argp);
1793 nvme_put_ns(ns);
1794 return ret;
1795
1796out_unlock:
1797 mutex_unlock(&ctrl->namespaces_mutex);
1798 return ret;
1799}
1800
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001801static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1802 unsigned long arg)
1803{
1804 struct nvme_ctrl *ctrl = file->private_data;
1805 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001806
1807 switch (cmd) {
1808 case NVME_IOCTL_ADMIN_CMD:
1809 return nvme_user_cmd(ctrl, NULL, argp);
1810 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001811 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001812 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001813 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001814 return ctrl->ops->reset_ctrl(ctrl);
1815 case NVME_IOCTL_SUBSYS_RESET:
1816 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001817 case NVME_IOCTL_RESCAN:
1818 nvme_queue_scan(ctrl);
1819 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001820 default:
1821 return -ENOTTY;
1822 }
1823}
1824
1825static const struct file_operations nvme_dev_fops = {
1826 .owner = THIS_MODULE,
1827 .open = nvme_dev_open,
1828 .release = nvme_dev_release,
1829 .unlocked_ioctl = nvme_dev_ioctl,
1830 .compat_ioctl = nvme_dev_ioctl,
1831};
1832
1833static ssize_t nvme_sysfs_reset(struct device *dev,
1834 struct device_attribute *attr, const char *buf,
1835 size_t count)
1836{
1837 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1838 int ret;
1839
1840 ret = ctrl->ops->reset_ctrl(ctrl);
1841 if (ret < 0)
1842 return ret;
1843 return count;
1844}
1845static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
1846
Keith Busch9ec3bb22016-04-29 15:45:18 -06001847static ssize_t nvme_sysfs_rescan(struct device *dev,
1848 struct device_attribute *attr, const char *buf,
1849 size_t count)
1850{
1851 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1852
1853 nvme_queue_scan(ctrl);
1854 return count;
1855}
1856static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
1857
Keith Busch118472a2016-02-18 09:57:48 -07001858static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
1859 char *buf)
1860{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001861 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07001862 struct nvme_ctrl *ctrl = ns->ctrl;
1863 int serial_len = sizeof(ctrl->serial);
1864 int model_len = sizeof(ctrl->model);
1865
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001866 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
1867 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07001868
1869 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1870 return sprintf(buf, "eui.%8phN\n", ns->eui);
1871
1872 while (ctrl->serial[serial_len - 1] == ' ')
1873 serial_len--;
1874 while (ctrl->model[model_len - 1] == ' ')
1875 model_len--;
1876
1877 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
1878 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
1879}
1880static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
1881
Keith Busch2b9b6e82015-12-22 10:10:45 -07001882static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
1883 char *buf)
1884{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001885 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001886 return sprintf(buf, "%pU\n", ns->nguid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001887}
1888static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
1889
1890static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
1891 char *buf)
1892{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001893 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001894 return sprintf(buf, "%8phd\n", ns->eui);
1895}
1896static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
1897
1898static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
1899 char *buf)
1900{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001901 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001902 return sprintf(buf, "%d\n", ns->ns_id);
1903}
1904static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
1905
1906static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07001907 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001908 &dev_attr_uuid.attr,
1909 &dev_attr_eui.attr,
1910 &dev_attr_nsid.attr,
1911 NULL,
1912};
1913
Ming Lin1a353d82016-06-13 16:45:24 +02001914static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001915 struct attribute *a, int n)
1916{
1917 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001918 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07001919
1920 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001921 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07001922 return 0;
1923 }
1924 if (a == &dev_attr_eui.attr) {
1925 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
1926 return 0;
1927 }
1928 return a->mode;
1929}
1930
1931static const struct attribute_group nvme_ns_attr_group = {
1932 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02001933 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07001934};
1935
Ming Lin931e1c22016-02-26 13:24:19 -08001936#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07001937static ssize_t field##_show(struct device *dev, \
1938 struct device_attribute *attr, char *buf) \
1939{ \
1940 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1941 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
1942} \
1943static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1944
Ming Lin931e1c22016-02-26 13:24:19 -08001945#define nvme_show_int_function(field) \
1946static ssize_t field##_show(struct device *dev, \
1947 struct device_attribute *attr, char *buf) \
1948{ \
1949 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
1950 return sprintf(buf, "%d\n", ctrl->field); \
1951} \
1952static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
1953
1954nvme_show_str_function(model);
1955nvme_show_str_function(serial);
1956nvme_show_str_function(firmware_rev);
1957nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07001958
Ming Lin1a353d82016-06-13 16:45:24 +02001959static ssize_t nvme_sysfs_delete(struct device *dev,
1960 struct device_attribute *attr, const char *buf,
1961 size_t count)
1962{
1963 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1964
1965 if (device_remove_file_self(dev, attr))
1966 ctrl->ops->delete_ctrl(ctrl);
1967 return count;
1968}
1969static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
1970
1971static ssize_t nvme_sysfs_show_transport(struct device *dev,
1972 struct device_attribute *attr,
1973 char *buf)
1974{
1975 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1976
1977 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
1978}
1979static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
1980
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02001981static ssize_t nvme_sysfs_show_state(struct device *dev,
1982 struct device_attribute *attr,
1983 char *buf)
1984{
1985 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1986 static const char *const state_name[] = {
1987 [NVME_CTRL_NEW] = "new",
1988 [NVME_CTRL_LIVE] = "live",
1989 [NVME_CTRL_RESETTING] = "resetting",
1990 [NVME_CTRL_RECONNECTING]= "reconnecting",
1991 [NVME_CTRL_DELETING] = "deleting",
1992 [NVME_CTRL_DEAD] = "dead",
1993 };
1994
1995 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
1996 state_name[ctrl->state])
1997 return sprintf(buf, "%s\n", state_name[ctrl->state]);
1998
1999 return sprintf(buf, "unknown state\n");
2000}
2001
2002static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2003
Ming Lin1a353d82016-06-13 16:45:24 +02002004static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2005 struct device_attribute *attr,
2006 char *buf)
2007{
2008 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2009
2010 return snprintf(buf, PAGE_SIZE, "%s\n",
2011 ctrl->ops->get_subsysnqn(ctrl));
2012}
2013static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2014
2015static ssize_t nvme_sysfs_show_address(struct device *dev,
2016 struct device_attribute *attr,
2017 char *buf)
2018{
2019 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2020
2021 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2022}
2023static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2024
Keith Busch779ff7562016-01-12 15:09:31 -07002025static struct attribute *nvme_dev_attrs[] = {
2026 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002027 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002028 &dev_attr_model.attr,
2029 &dev_attr_serial.attr,
2030 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002031 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002032 &dev_attr_delete_controller.attr,
2033 &dev_attr_transport.attr,
2034 &dev_attr_subsysnqn.attr,
2035 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002036 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002037 NULL
2038};
2039
Ming Lin1a353d82016-06-13 16:45:24 +02002040#define CHECK_ATTR(ctrl, a, name) \
2041 if ((a) == &dev_attr_##name.attr && \
2042 !(ctrl)->ops->get_##name) \
2043 return 0
2044
2045static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2046 struct attribute *a, int n)
2047{
2048 struct device *dev = container_of(kobj, struct device, kobj);
2049 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2050
2051 if (a == &dev_attr_delete_controller.attr) {
2052 if (!ctrl->ops->delete_ctrl)
2053 return 0;
2054 }
2055
2056 CHECK_ATTR(ctrl, a, subsysnqn);
2057 CHECK_ATTR(ctrl, a, address);
2058
2059 return a->mode;
2060}
2061
Keith Busch779ff7562016-01-12 15:09:31 -07002062static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002063 .attrs = nvme_dev_attrs,
2064 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002065};
2066
2067static const struct attribute_group *nvme_dev_attr_groups[] = {
2068 &nvme_dev_attrs_group,
2069 NULL,
2070};
2071
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002072static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2073{
2074 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2075 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2076
2077 return nsa->ns_id - nsb->ns_id;
2078}
2079
Keith Busch32f0c4a2016-07-13 11:45:02 -06002080static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002081{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002082 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002083
Keith Busch32f0c4a2016-07-13 11:45:02 -06002084 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002085 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002086 if (ns->ns_id == nsid) {
2087 kref_get(&ns->kref);
2088 ret = ns;
2089 break;
2090 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002091 if (ns->ns_id > nsid)
2092 break;
2093 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002094 mutex_unlock(&ctrl->namespaces_mutex);
2095 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002096}
2097
2098static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2099{
2100 struct nvme_ns *ns;
2101 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002102 struct nvme_id_ns *id;
2103 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002104 int node = dev_to_node(ctrl->dev);
2105
2106 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2107 if (!ns)
2108 return;
2109
Keith Busch075790e2016-02-24 09:15:53 -07002110 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2111 if (ns->instance < 0)
2112 goto out_free_ns;
2113
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002114 ns->queue = blk_mq_init_queue(ctrl->tagset);
2115 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002116 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002117 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2118 ns->queue->queuedata = ns;
2119 ns->ctrl = ctrl;
2120
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002121 kref_init(&ns->kref);
2122 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002123 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002124
2125 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002126 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002127
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002128 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002129
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002130 if (nvme_revalidate_ns(ns, &id))
2131 goto out_free_queue;
2132
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002133 if (nvme_nvm_ns_supported(ns, id) &&
2134 nvme_nvm_register(ns, disk_name, node)) {
2135 dev_warn(ctrl->dev, "%s: LightNVM init failure\n", __func__);
2136 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002137 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002138
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002139 disk = alloc_disk_node(0, node);
2140 if (!disk)
2141 goto out_free_id;
2142
2143 disk->fops = &nvme_fops;
2144 disk->private_data = ns;
2145 disk->queue = ns->queue;
2146 disk->flags = GENHD_FL_EXT_DEVT;
2147 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2148 ns->disk = disk;
2149
2150 __nvme_revalidate_disk(disk, id);
2151
Keith Busch32f0c4a2016-07-13 11:45:02 -06002152 mutex_lock(&ctrl->namespaces_mutex);
2153 list_add_tail(&ns->list, &ctrl->namespaces);
2154 mutex_unlock(&ctrl->namespaces_mutex);
2155
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002156 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002157
2158 kfree(id);
2159
Dan Williams0d52c7562016-06-15 19:44:20 -07002160 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002161 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2162 &nvme_ns_attr_group))
2163 pr_warn("%s: failed to create sysfs group for identification\n",
2164 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002165 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2166 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2167 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002168 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002169 out_free_id:
2170 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002171 out_free_queue:
2172 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002173 out_release_instance:
2174 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002175 out_free_ns:
2176 kfree(ns);
2177}
2178
2179static void nvme_ns_remove(struct nvme_ns *ns)
2180{
Keith Busch646017a2016-02-24 09:15:54 -07002181 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2182 return;
2183
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002184 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002185 if (blk_get_integrity(ns->disk))
2186 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002187 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2188 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002189 if (ns->ndev)
2190 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002191 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002192 blk_cleanup_queue(ns->queue);
2193 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002194
2195 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002196 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002197 mutex_unlock(&ns->ctrl->namespaces_mutex);
2198
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002199 nvme_put_ns(ns);
2200}
2201
Keith Busch540c8012015-10-22 15:45:06 -06002202static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2203{
2204 struct nvme_ns *ns;
2205
Keith Busch32f0c4a2016-07-13 11:45:02 -06002206 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002207 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002208 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002209 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002210 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002211 } else
2212 nvme_alloc_ns(ctrl, nsid);
2213}
2214
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302215static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2216 unsigned nsid)
2217{
2218 struct nvme_ns *ns, *next;
2219
2220 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2221 if (ns->ns_id > nsid)
2222 nvme_ns_remove(ns);
2223 }
2224}
2225
Keith Busch540c8012015-10-22 15:45:06 -06002226static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2227{
2228 struct nvme_ns *ns;
2229 __le32 *ns_list;
2230 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2231 int ret = 0;
2232
2233 ns_list = kzalloc(0x1000, GFP_KERNEL);
2234 if (!ns_list)
2235 return -ENOMEM;
2236
2237 for (i = 0; i < num_lists; i++) {
2238 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2239 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302240 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002241
2242 for (j = 0; j < min(nn, 1024U); j++) {
2243 nsid = le32_to_cpu(ns_list[j]);
2244 if (!nsid)
2245 goto out;
2246
2247 nvme_validate_ns(ctrl, nsid);
2248
2249 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002250 ns = nvme_find_get_ns(ctrl, prev);
2251 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002252 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002253 nvme_put_ns(ns);
2254 }
Keith Busch540c8012015-10-22 15:45:06 -06002255 }
2256 }
2257 nn -= j;
2258 }
2259 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302260 nvme_remove_invalid_namespaces(ctrl, prev);
2261 free:
Keith Busch540c8012015-10-22 15:45:06 -06002262 kfree(ns_list);
2263 return ret;
2264}
2265
Christoph Hellwig5955be22016-04-26 13:51:59 +02002266static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002267{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002268 unsigned i;
2269
Keith Busch540c8012015-10-22 15:45:06 -06002270 for (i = 1; i <= nn; i++)
2271 nvme_validate_ns(ctrl, i);
2272
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302273 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002274}
2275
Christoph Hellwig5955be22016-04-26 13:51:59 +02002276static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002277{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002278 struct nvme_ctrl *ctrl =
2279 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002280 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002281 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002282
Christoph Hellwig5955be22016-04-26 13:51:59 +02002283 if (ctrl->state != NVME_CTRL_LIVE)
2284 return;
2285
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002286 if (nvme_identify_ctrl(ctrl, &id))
2287 return;
Keith Busch540c8012015-10-22 15:45:06 -06002288
2289 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002290 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002291 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2292 if (!nvme_scan_ns_list(ctrl, nn))
2293 goto done;
2294 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002295 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002296 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002297 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002298 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002299 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002300 kfree(id);
2301}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002302
2303void nvme_queue_scan(struct nvme_ctrl *ctrl)
2304{
2305 /*
2306 * Do not queue new scan work when a controller is reset during
2307 * removal.
2308 */
2309 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002310 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002311}
2312EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002313
Keith Busch32f0c4a2016-07-13 11:45:02 -06002314/*
2315 * This function iterates the namespace list unlocked to allow recovery from
2316 * controller failure. It is up to the caller to ensure the namespace list is
2317 * not modified by scan work while this function is executing.
2318 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002319void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2320{
2321 struct nvme_ns *ns, *next;
2322
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002323 /*
2324 * The dead states indicates the controller was not gracefully
2325 * disconnected. In that case, we won't be able to flush any data while
2326 * removing the namespaces' disks; fail all the queues now to avoid
2327 * potentially having to clean up the failed sync later.
2328 */
2329 if (ctrl->state == NVME_CTRL_DEAD)
2330 nvme_kill_queues(ctrl);
2331
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002332 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2333 nvme_ns_remove(ns);
2334}
Ming Lin576d55d2016-02-10 10:03:32 -08002335EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002336
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002337static void nvme_async_event_work(struct work_struct *work)
2338{
2339 struct nvme_ctrl *ctrl =
2340 container_of(work, struct nvme_ctrl, async_event_work);
2341
2342 spin_lock_irq(&ctrl->lock);
2343 while (ctrl->event_limit > 0) {
2344 int aer_idx = --ctrl->event_limit;
2345
2346 spin_unlock_irq(&ctrl->lock);
2347 ctrl->ops->submit_async_event(ctrl, aer_idx);
2348 spin_lock_irq(&ctrl->lock);
2349 }
2350 spin_unlock_irq(&ctrl->lock);
2351}
2352
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002353void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2354 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002355{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002356 u32 result = le32_to_cpu(res->u32);
2357 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002358
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002359 switch (le16_to_cpu(status) >> 1) {
2360 case NVME_SC_SUCCESS:
2361 done = false;
2362 /*FALLTHRU*/
2363 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002364 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002365 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002366 break;
2367 default:
2368 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002369 }
2370
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002371 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002372 return;
2373
2374 switch (result & 0xff07) {
2375 case NVME_AER_NOTICE_NS_CHANGED:
2376 dev_info(ctrl->device, "rescanning\n");
2377 nvme_queue_scan(ctrl);
2378 break;
2379 default:
2380 dev_warn(ctrl->device, "async event result %08x\n", result);
2381 }
2382}
2383EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2384
2385void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2386{
2387 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002388 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002389}
2390EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2391
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002392static DEFINE_IDA(nvme_instance_ida);
2393
2394static int nvme_set_instance(struct nvme_ctrl *ctrl)
2395{
2396 int instance, error;
2397
2398 do {
2399 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2400 return -ENODEV;
2401
2402 spin_lock(&dev_list_lock);
2403 error = ida_get_new(&nvme_instance_ida, &instance);
2404 spin_unlock(&dev_list_lock);
2405 } while (error == -EAGAIN);
2406
2407 if (error)
2408 return -ENODEV;
2409
2410 ctrl->instance = instance;
2411 return 0;
2412}
2413
2414static void nvme_release_instance(struct nvme_ctrl *ctrl)
2415{
2416 spin_lock(&dev_list_lock);
2417 ida_remove(&nvme_instance_ida, ctrl->instance);
2418 spin_unlock(&dev_list_lock);
2419}
2420
Keith Busch53029b02015-11-28 15:41:02 +01002421void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002422{
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002423 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002424 flush_work(&ctrl->scan_work);
2425 nvme_remove_namespaces(ctrl);
2426
Keith Busch53029b02015-11-28 15:41:02 +01002427 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002428
2429 spin_lock(&dev_list_lock);
2430 list_del(&ctrl->node);
2431 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002432}
Ming Lin576d55d2016-02-10 10:03:32 -08002433EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002434
2435static void nvme_free_ctrl(struct kref *kref)
2436{
2437 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002438
2439 put_device(ctrl->device);
2440 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002441 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002442
2443 ctrl->ops->free_ctrl(ctrl);
2444}
2445
2446void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2447{
2448 kref_put(&ctrl->kref, nvme_free_ctrl);
2449}
Ming Lin576d55d2016-02-10 10:03:32 -08002450EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002451
2452/*
2453 * Initialize a NVMe controller structures. This needs to be called during
2454 * earliest initialization so that we have the initialized structured around
2455 * during probing.
2456 */
2457int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2458 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2459{
2460 int ret;
2461
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002462 ctrl->state = NVME_CTRL_NEW;
2463 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002464 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002465 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002466 kref_init(&ctrl->kref);
2467 ctrl->dev = dev;
2468 ctrl->ops = ops;
2469 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002470 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002471 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002472
2473 ret = nvme_set_instance(ctrl);
2474 if (ret)
2475 goto out;
2476
Keith Busch779ff7562016-01-12 15:09:31 -07002477 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002478 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002479 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002480 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002481 if (IS_ERR(ctrl->device)) {
2482 ret = PTR_ERR(ctrl->device);
2483 goto out_release_instance;
2484 }
2485 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002486 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002487
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002488 spin_lock(&dev_list_lock);
2489 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2490 spin_unlock(&dev_list_lock);
2491
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002492 /*
2493 * Initialize latency tolerance controls. The sysfs files won't
2494 * be visible to userspace unless the device actually supports APST.
2495 */
2496 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2497 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2498 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2499
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002500 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002501out_release_instance:
2502 nvme_release_instance(ctrl);
2503out:
2504 return ret;
2505}
Ming Lin576d55d2016-02-10 10:03:32 -08002506EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002507
Keith Busch69d9a992016-02-24 09:15:56 -07002508/**
2509 * nvme_kill_queues(): Ends all namespace queues
2510 * @ctrl: the dead controller that needs to end
2511 *
2512 * Call this function when the driver determines it is unable to get the
2513 * controller in a state capable of servicing IO.
2514 */
2515void nvme_kill_queues(struct nvme_ctrl *ctrl)
2516{
2517 struct nvme_ns *ns;
2518
Keith Busch32f0c4a2016-07-13 11:45:02 -06002519 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002520
2521 /* Forcibly start all queues to avoid having stuck requests */
2522 blk_mq_start_hw_queues(ctrl->admin_q);
2523
Keith Busch32f0c4a2016-07-13 11:45:02 -06002524 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002525 /*
2526 * Revalidating a dead namespace sets capacity to 0. This will
2527 * end buffered writers dirtying pages that can't be synced.
2528 */
Keith Buschf33447b2017-02-10 18:15:51 -05002529 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2530 continue;
2531 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002532 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002533
2534 /*
2535 * Forcibly start all queues to avoid having stuck requests.
2536 * Note that we must ensure the queues are not stopped
2537 * when the final removal happens.
2538 */
2539 blk_mq_start_hw_queues(ns->queue);
Ming Lei986f75c2017-05-22 23:05:04 +08002540
2541 /* draining requests in requeue list */
2542 blk_mq_kick_requeue_list(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002543 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002544 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002545}
Linus Torvalds237045f2016-03-18 17:13:31 -07002546EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002547
Keith Busch302ad8c2017-03-01 14:22:12 -05002548void nvme_unfreeze(struct nvme_ctrl *ctrl)
2549{
2550 struct nvme_ns *ns;
2551
2552 mutex_lock(&ctrl->namespaces_mutex);
2553 list_for_each_entry(ns, &ctrl->namespaces, list)
2554 blk_mq_unfreeze_queue(ns->queue);
2555 mutex_unlock(&ctrl->namespaces_mutex);
2556}
2557EXPORT_SYMBOL_GPL(nvme_unfreeze);
2558
2559void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2560{
2561 struct nvme_ns *ns;
2562
2563 mutex_lock(&ctrl->namespaces_mutex);
2564 list_for_each_entry(ns, &ctrl->namespaces, list) {
2565 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2566 if (timeout <= 0)
2567 break;
2568 }
2569 mutex_unlock(&ctrl->namespaces_mutex);
2570}
2571EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2572
2573void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2574{
2575 struct nvme_ns *ns;
2576
2577 mutex_lock(&ctrl->namespaces_mutex);
2578 list_for_each_entry(ns, &ctrl->namespaces, list)
2579 blk_mq_freeze_queue_wait(ns->queue);
2580 mutex_unlock(&ctrl->namespaces_mutex);
2581}
2582EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2583
2584void nvme_start_freeze(struct nvme_ctrl *ctrl)
2585{
2586 struct nvme_ns *ns;
2587
2588 mutex_lock(&ctrl->namespaces_mutex);
2589 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002590 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002591 mutex_unlock(&ctrl->namespaces_mutex);
2592}
2593EXPORT_SYMBOL_GPL(nvme_start_freeze);
2594
Keith Busch25646262016-01-04 09:10:57 -07002595void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002596{
2597 struct nvme_ns *ns;
2598
Keith Busch32f0c4a2016-07-13 11:45:02 -06002599 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002600 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002601 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002602 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002603}
Ming Lin576d55d2016-02-10 10:03:32 -08002604EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002605
Keith Busch25646262016-01-04 09:10:57 -07002606void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002607{
2608 struct nvme_ns *ns;
2609
Keith Busch32f0c4a2016-07-13 11:45:02 -06002610 mutex_lock(&ctrl->namespaces_mutex);
2611 list_for_each_entry(ns, &ctrl->namespaces, list) {
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002612 blk_mq_start_stopped_hw_queues(ns->queue, true);
2613 blk_mq_kick_requeue_list(ns->queue);
2614 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002615 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002616}
Ming Lin576d55d2016-02-10 10:03:32 -08002617EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002618
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002619int __init nvme_core_init(void)
2620{
2621 int result;
2622
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002623 nvme_wq = alloc_workqueue("nvme-wq",
2624 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2625 if (!nvme_wq)
2626 return -ENOMEM;
2627
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002628 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2629 &nvme_dev_fops);
2630 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002631 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002632 else if (result > 0)
2633 nvme_char_major = result;
2634
2635 nvme_class = class_create(THIS_MODULE, "nvme");
2636 if (IS_ERR(nvme_class)) {
2637 result = PTR_ERR(nvme_class);
2638 goto unregister_chrdev;
2639 }
2640
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002641 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002642
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002643unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002644 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002645destroy_wq:
2646 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002647 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002648}
2649
2650void nvme_core_exit(void)
2651{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002652 class_destroy(nvme_class);
2653 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002654 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002655}
Ming Lin576d55d2016-02-10 10:03:32 -08002656
2657MODULE_LICENSE("GPL");
2658MODULE_VERSION("1.0");
2659module_init(nvme_core_init);
2660module_exit(nvme_core_exit);