blob: 1d931deac83bc5eb37f3961195e9f550e47d6c03 [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 <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Marc Olson8ae4e442017-09-06 17:23:56 -070037unsigned int admin_timeout = 60;
38module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080039MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
Marc Olson8ae4e442017-09-06 17:23:56 -070042unsigned int nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080044MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020047static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080048module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Christoph Hellwig44e44b22017-04-05 19:18:11 +020051static u8 nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070053MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010054
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010055static int nvme_char_major;
56module_param(nvme_char_major, int, 0);
57
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080058static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059module_param(default_ps_max_latency_us, ulong, 0644);
60MODULE_PARM_DESC(default_ps_max_latency_us,
61 "max power saving latency for new devices; use PM QOS to change per device");
62
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070063static bool force_apst;
64module_param(force_apst, bool, 0644);
65MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
66
Jens Axboef5d11842017-06-27 12:03:06 -060067static bool streams;
68module_param(streams, bool, 0644);
69MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
70
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020071struct workqueue_struct *nvme_wq;
72EXPORT_SYMBOL_GPL(nvme_wq);
73
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010074static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080075static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010076
Christoph Hellwig9843f682017-10-18 13:10:01 +020077static DEFINE_IDA(nvme_instance_ida);
78
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010079static struct class *nvme_class;
80
Arnav Dawnb6dccf72017-07-12 16:10:40 +053081static __le32 nvme_get_log_dw10(u8 lid, size_t size)
82{
83 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
84}
85
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020086int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
87{
88 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
89 return -EBUSY;
90 if (!queue_work(nvme_wq, &ctrl->reset_work))
91 return -EBUSY;
92 return 0;
93}
94EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
95
96static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
97{
98 int ret;
99
100 ret = nvme_reset_ctrl(ctrl);
101 if (!ret)
102 flush_work(&ctrl->reset_work);
103 return ret;
104}
105
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200106static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200107{
108 switch (nvme_req(req)->status & 0x7ff) {
109 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200110 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200111 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200112 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200113 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200114 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200115 case NVME_SC_WRITE_FAULT:
116 case NVME_SC_READ_ERROR:
117 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200118 case NVME_SC_ACCESS_DENIED:
119 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200120 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200121 case NVME_SC_GUARD_CHECK:
122 case NVME_SC_APPTAG_CHECK:
123 case NVME_SC_REFTAG_CHECK:
124 case NVME_SC_INVALID_PI:
125 return BLK_STS_PROTECTION;
126 case NVME_SC_RESERVATION_CONFLICT:
127 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200128 default:
129 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200130 }
131}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200132
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200133static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200134{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200135 if (blk_noretry_request(req))
136 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200137 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200138 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200139 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200140 return false;
141 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200142}
143
144void nvme_complete_rq(struct request *req)
145{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200146 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
147 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300148 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200149 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200150 }
151
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200152 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200153}
154EXPORT_SYMBOL_GPL(nvme_complete_rq);
155
Ming Linc55a2fd2016-05-18 14:05:02 -0700156void nvme_cancel_request(struct request *req, void *data, bool reserved)
157{
158 int status;
159
160 if (!blk_mq_request_started(req))
161 return;
162
163 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
164 "Cancelling I/O %d", req->tag);
165
166 status = NVME_SC_ABORT_REQ;
167 if (blk_queue_dying(req->q))
168 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200169 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200170 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200171
Ming Linc55a2fd2016-05-18 14:05:02 -0700172}
173EXPORT_SYMBOL_GPL(nvme_cancel_request);
174
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200175bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
176 enum nvme_ctrl_state new_state)
177{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300178 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200179 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200180 bool changed = false;
181
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200182 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300183
184 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200185 switch (new_state) {
186 case NVME_CTRL_LIVE:
187 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200188 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200189 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900190 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200191 changed = true;
192 /* FALLTHRU */
193 default:
194 break;
195 }
196 break;
197 case NVME_CTRL_RESETTING:
198 switch (old_state) {
199 case NVME_CTRL_NEW:
200 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900201 changed = true;
202 /* FALLTHRU */
203 default:
204 break;
205 }
206 break;
207 case NVME_CTRL_RECONNECTING:
208 switch (old_state) {
209 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200210 changed = true;
211 /* FALLTHRU */
212 default:
213 break;
214 }
215 break;
216 case NVME_CTRL_DELETING:
217 switch (old_state) {
218 case NVME_CTRL_LIVE:
219 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900220 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200221 changed = true;
222 /* FALLTHRU */
223 default:
224 break;
225 }
226 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600227 case NVME_CTRL_DEAD:
228 switch (old_state) {
229 case NVME_CTRL_DELETING:
230 changed = true;
231 /* FALLTHRU */
232 default:
233 break;
234 }
235 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200236 default:
237 break;
238 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200239
240 if (changed)
241 ctrl->state = new_state;
242
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200243 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300244
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200245 return changed;
246}
247EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
248
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100249static void nvme_free_ns(struct kref *kref)
250{
251 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
252
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200253 if (ns->ndev)
254 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100255
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200256 if (ns->disk) {
257 spin_lock(&dev_list_lock);
258 ns->disk->private_data = NULL;
259 spin_unlock(&dev_list_lock);
260 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100261
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100262 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700263 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
264 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100265 kfree(ns);
266}
267
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100268static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100269{
270 kref_put(&ns->kref, nvme_free_ns);
271}
272
273static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
274{
275 struct nvme_ns *ns;
276
277 spin_lock(&dev_list_lock);
278 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800279 if (ns) {
280 if (!kref_get_unless_zero(&ns->kref))
281 goto fail;
282 if (!try_module_get(ns->ctrl->ops->module))
283 goto fail_put_ns;
284 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100285 spin_unlock(&dev_list_lock);
286
287 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800288
289fail_put_ns:
290 kref_put(&ns->kref, nvme_free_ns);
291fail:
292 spin_unlock(&dev_list_lock);
293 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100294}
295
Christoph Hellwig41609822015-11-20 09:00:02 +0100296struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200297 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100298{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100299 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100300 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100301
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200302 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100303 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200304 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100305 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200306 qid ? qid - 1 : 0);
307 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100308 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100309 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100310
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800312 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100313
Christoph Hellwig41609822015-11-20 09:00:02 +0100314 return req;
315}
Ming Lin576d55d2016-02-10 10:03:32 -0800316EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100317
Jens Axboef5d11842017-06-27 12:03:06 -0600318static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
319{
320 struct nvme_command c;
321
322 memset(&c, 0, sizeof(c));
323
324 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530325 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600326 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
327 c.directive.dtype = NVME_DIR_IDENTIFY;
328 c.directive.tdtype = NVME_DIR_STREAMS;
329 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
330
331 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
332}
333
334static int nvme_disable_streams(struct nvme_ctrl *ctrl)
335{
336 return nvme_toggle_streams(ctrl, false);
337}
338
339static int nvme_enable_streams(struct nvme_ctrl *ctrl)
340{
341 return nvme_toggle_streams(ctrl, true);
342}
343
344static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
345 struct streams_directive_params *s, u32 nsid)
346{
347 struct nvme_command c;
348
349 memset(&c, 0, sizeof(c));
350 memset(s, 0, sizeof(*s));
351
352 c.directive.opcode = nvme_admin_directive_recv;
353 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700354 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600355 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
356 c.directive.dtype = NVME_DIR_STREAMS;
357
358 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
359}
360
361static int nvme_configure_directives(struct nvme_ctrl *ctrl)
362{
363 struct streams_directive_params s;
364 int ret;
365
366 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
367 return 0;
368 if (!streams)
369 return 0;
370
371 ret = nvme_enable_streams(ctrl);
372 if (ret)
373 return ret;
374
Arnav Dawn62346ea2017-07-12 16:11:53 +0530375 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600376 if (ret)
377 return ret;
378
379 ctrl->nssa = le16_to_cpu(s.nssa);
380 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
381 dev_info(ctrl->device, "too few streams (%u) available\n",
382 ctrl->nssa);
383 nvme_disable_streams(ctrl);
384 return 0;
385 }
386
387 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
388 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
389 return 0;
390}
391
392/*
393 * Check if 'req' has a write hint associated with it. If it does, assign
394 * a valid namespace stream to the write.
395 */
396static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
397 struct request *req, u16 *control,
398 u32 *dsmgmt)
399{
400 enum rw_hint streamid = req->write_hint;
401
402 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
403 streamid = 0;
404 else {
405 streamid--;
406 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
407 return;
408
409 *control |= NVME_RW_DTYPE_STREAMS;
410 *dsmgmt |= streamid << 16;
411 }
412
413 if (streamid < ARRAY_SIZE(req->q->write_hints))
414 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
415}
416
Ming Lin8093f7c2016-04-12 13:10:14 -0600417static inline void nvme_setup_flush(struct nvme_ns *ns,
418 struct nvme_command *cmnd)
419{
420 memset(cmnd, 0, sizeof(*cmnd));
421 cmnd->common.opcode = nvme_cmd_flush;
422 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
423}
424
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200425static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600426 struct nvme_command *cmnd)
427{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100428 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600429 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100430 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600431
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100432 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600433 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200434 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600435
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100436 __rq_for_each_bio(bio, req) {
437 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
438 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
439
440 range[n].cattr = cpu_to_le32(0);
441 range[n].nlb = cpu_to_le32(nlb);
442 range[n].slba = cpu_to_le64(slba);
443 n++;
444 }
445
446 if (WARN_ON_ONCE(n != segments)) {
447 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200448 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100449 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600450
451 memset(cmnd, 0, sizeof(*cmnd));
452 cmnd->dsm.opcode = nvme_cmd_dsm;
453 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200454 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600455 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
456
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700457 req->special_vec.bv_page = virt_to_page(range);
458 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100459 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700460 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600461
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200462 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600463}
Ming Lin8093f7c2016-04-12 13:10:14 -0600464
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200465static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
466 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600467{
Jens Axboef5d11842017-06-27 12:03:06 -0600468 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600469 u16 control = 0;
470 u32 dsmgmt = 0;
471
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200472 /*
473 * If formated with metadata, require the block layer provide a buffer
474 * unless this namespace is formated such that the metadata can be
475 * stripped/generated by the controller with PRACT=1.
476 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300477 if (ns && ns->ms &&
478 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200479 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
480 return BLK_STS_NOTSUPP;
481
Ming Lin8093f7c2016-04-12 13:10:14 -0600482 if (req->cmd_flags & REQ_FUA)
483 control |= NVME_RW_FUA;
484 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
485 control |= NVME_RW_LR;
486
487 if (req->cmd_flags & REQ_RAHEAD)
488 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
489
490 memset(cmnd, 0, sizeof(*cmnd));
491 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600492 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
493 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
494 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
495
Jens Axboef5d11842017-06-27 12:03:06 -0600496 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
497 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
498
Ming Lin8093f7c2016-04-12 13:10:14 -0600499 if (ns->ms) {
500 switch (ns->pi_type) {
501 case NVME_NS_DPS_PI_TYPE3:
502 control |= NVME_RW_PRINFO_PRCHK_GUARD;
503 break;
504 case NVME_NS_DPS_PI_TYPE1:
505 case NVME_NS_DPS_PI_TYPE2:
506 control |= NVME_RW_PRINFO_PRCHK_GUARD |
507 NVME_RW_PRINFO_PRCHK_REF;
508 cmnd->rw.reftag = cpu_to_le32(
509 nvme_block_nr(ns, blk_rq_pos(req)));
510 break;
511 }
512 if (!blk_integrity_rq(req))
513 control |= NVME_RW_PRINFO_PRACT;
514 }
515
516 cmnd->rw.control = cpu_to_le16(control);
517 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200518 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600519}
520
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200521blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600522 struct nvme_command *cmd)
523{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200524 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600525
Christoph Hellwig987f6992017-04-05 19:18:08 +0200526 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200527 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200528 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200529 req->rq_flags |= RQF_DONTPREP;
530 }
531
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100532 switch (req_op(req)) {
533 case REQ_OP_DRV_IN:
534 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800535 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100536 break;
537 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600538 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100539 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200540 case REQ_OP_WRITE_ZEROES:
541 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600543 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100544 break;
545 case REQ_OP_READ:
546 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200547 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100548 break;
549 default:
550 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200551 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100552 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600553
James Smart721b3912016-10-21 23:33:34 +0300554 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600555 return ret;
556}
557EXPORT_SYMBOL_GPL(nvme_setup_cmd);
558
Christoph Hellwig41609822015-11-20 09:00:02 +0100559/*
560 * Returns 0 on success. If the result is negative, it's a Linux error code;
561 * if the result is positive, it's an NVM Express status code
562 */
563int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800564 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200565 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100566{
567 struct request *req;
568 int ret;
569
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200570 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100571 if (IS_ERR(req))
572 return PTR_ERR(req);
573
574 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
575
Christoph Hellwig21d34712015-11-26 09:08:36 +0100576 if (buffer && bufflen) {
577 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
578 if (ret)
579 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100580 }
581
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200582 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800583 if (result)
584 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200585 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
586 ret = -EINTR;
587 else
588 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100589 out:
590 blk_mq_free_request(req);
591 return ret;
592}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200593EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100594
595int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
596 void *buffer, unsigned bufflen)
597{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200598 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
599 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100600}
Ming Lin576d55d2016-02-10 10:03:32 -0800601EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100602
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400603static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
604 unsigned len, u32 seed, bool write)
605{
606 struct bio_integrity_payload *bip;
607 int ret = -ENOMEM;
608 void *buf;
609
610 buf = kmalloc(len, GFP_KERNEL);
611 if (!buf)
612 goto out;
613
614 ret = -EFAULT;
615 if (write && copy_from_user(buf, ubuf, len))
616 goto out_free_meta;
617
618 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
619 if (IS_ERR(bip)) {
620 ret = PTR_ERR(bip);
621 goto out_free_meta;
622 }
623
624 bip->bip_iter.bi_size = len;
625 bip->bip_iter.bi_sector = seed;
626 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
627 offset_in_page(buf));
628 if (ret == len)
629 return buf;
630 ret = -ENOMEM;
631out_free_meta:
632 kfree(buf);
633out:
634 return ERR_PTR(ret);
635}
636
Keith Busch63263d62017-08-29 17:46:04 -0400637static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400638 struct nvme_command *cmd, void __user *ubuffer,
639 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
640 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100641{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200642 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600643 struct nvme_ns *ns = q->queuedata;
644 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100645 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600646 struct bio *bio = NULL;
647 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100648 int ret;
649
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200650 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100651 if (IS_ERR(req))
652 return PTR_ERR(req);
653
654 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
655
656 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100657 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
658 GFP_KERNEL);
659 if (ret)
660 goto out;
661 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200662 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400663 if (disk && meta_buffer && meta_len) {
664 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
665 meta_seed, write);
666 if (IS_ERR(meta)) {
667 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600668 goto out_unmap;
669 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600670 }
671 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400672
Keith Busch0b7f1f22015-10-23 09:47:28 -0600673 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200674 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
675 ret = -EINTR;
676 else
677 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100678 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800679 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600680 if (meta && !ret && !write) {
681 if (copy_to_user(meta_buffer, meta, meta_len))
682 ret = -EFAULT;
683 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600684 kfree(meta);
685 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200686 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600687 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100688 out:
689 blk_mq_free_request(req);
690 return ret;
691}
692
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200693static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200694{
695 struct nvme_ctrl *ctrl = rq->end_io_data;
696
697 blk_mq_free_request(rq);
698
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200699 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200700 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200701 "failed nvme_keep_alive_end_io error=%d\n",
702 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200703 return;
704 }
705
706 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
707}
708
709static int nvme_keep_alive(struct nvme_ctrl *ctrl)
710{
711 struct nvme_command c;
712 struct request *rq;
713
714 memset(&c, 0, sizeof(c));
715 c.common.opcode = nvme_admin_keep_alive;
716
717 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
718 NVME_QID_ANY);
719 if (IS_ERR(rq))
720 return PTR_ERR(rq);
721
722 rq->timeout = ctrl->kato * HZ;
723 rq->end_io_data = ctrl;
724
725 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
726
727 return 0;
728}
729
730static void nvme_keep_alive_work(struct work_struct *work)
731{
732 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
733 struct nvme_ctrl, ka_work);
734
735 if (nvme_keep_alive(ctrl)) {
736 /* allocation failure, reset the controller */
737 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200738 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200739 return;
740 }
741}
742
743void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
744{
745 if (unlikely(ctrl->kato == 0))
746 return;
747
748 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
749 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
750}
751EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
752
753void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
754{
755 if (unlikely(ctrl->kato == 0))
756 return;
757
758 cancel_delayed_work_sync(&ctrl->ka_work);
759}
760EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
761
Keith Busch3f7f25a92017-06-20 15:09:56 -0400762static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100763{
764 struct nvme_command c = { };
765 int error;
766
767 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
768 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200769 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100770
771 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
772 if (!*id)
773 return -ENOMEM;
774
775 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
776 sizeof(struct nvme_id_ctrl));
777 if (error)
778 kfree(*id);
779 return error;
780}
781
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200782static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
783 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200784{
785 struct nvme_command c = { };
786 int status;
787 void *data;
788 int pos;
789 int len;
790
791 c.identify.opcode = nvme_admin_identify;
792 c.identify.nsid = cpu_to_le32(nsid);
793 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
794
795 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
796 if (!data)
797 return -ENOMEM;
798
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200799 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200800 NVME_IDENTIFY_DATA_SIZE);
801 if (status)
802 goto free_data;
803
804 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
805 struct nvme_ns_id_desc *cur = data + pos;
806
807 if (cur->nidl == 0)
808 break;
809
810 switch (cur->nidt) {
811 case NVME_NIDT_EUI64:
812 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200813 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200814 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
815 cur->nidl);
816 goto free_data;
817 }
818 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200819 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200820 break;
821 case NVME_NIDT_NGUID:
822 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200823 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200824 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
825 cur->nidl);
826 goto free_data;
827 }
828 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200829 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200830 break;
831 case NVME_NIDT_UUID:
832 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200833 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200834 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
835 cur->nidl);
836 goto free_data;
837 }
838 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200839 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200840 break;
841 default:
842 /* Skip unnkown types */
843 len = cur->nidl;
844 break;
845 }
846
847 len += sizeof(*cur);
848 }
849free_data:
850 kfree(data);
851 return status;
852}
853
Keith Busch540c8012015-10-22 15:45:06 -0600854static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
855{
856 struct nvme_command c = { };
857
858 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200859 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600860 c.identify.nsid = cpu_to_le32(nsid);
861 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
862}
863
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200864static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
865 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100866{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200867 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100868 struct nvme_command c = { };
869 int error;
870
871 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200872 c.identify.opcode = nvme_admin_identify;
873 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200874 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100875
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200876 id = kmalloc(sizeof(*id), GFP_KERNEL);
877 if (!id)
878 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100879
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200880 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
881 if (error) {
882 dev_warn(ctrl->device, "Identify namespace failed\n");
883 kfree(id);
884 return NULL;
885 }
886
887 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100888}
889
Keith Busch3f7f25a92017-06-20 15:09:56 -0400890static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700891 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100892{
893 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800894 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100895 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100896
897 memset(&c, 0, sizeof(c));
898 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100899 c.features.fid = cpu_to_le32(fid);
900 c.features.dword11 = cpu_to_le32(dword11);
901
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800902 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700903 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700904 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800905 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100906 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100907}
908
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100909int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
910{
911 u32 q_count = (*count - 1) | ((*count - 1) << 16);
912 u32 result;
913 int status, nr_io_queues;
914
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700915 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100916 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200917 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100918 return status;
919
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200920 /*
921 * Degraded controllers might return an error when setting the queue
922 * count. We still want to be able to bring them online and offer
923 * access to the admin queue, as that might be only way to fix them up.
924 */
925 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200926 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200927 *count = 0;
928 } else {
929 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
930 *count = min(*count, nr_io_queues);
931 }
932
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100933 return 0;
934}
Ming Lin576d55d2016-02-10 10:03:32 -0800935EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100936
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100937static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
938{
939 struct nvme_user_io io;
940 struct nvme_command c;
941 unsigned length, meta_len;
942 void __user *metadata;
943
944 if (copy_from_user(&io, uio, sizeof(io)))
945 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700946 if (io.flags)
947 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100948
949 switch (io.opcode) {
950 case nvme_cmd_write:
951 case nvme_cmd_read:
952 case nvme_cmd_compare:
953 break;
954 default:
955 return -EINVAL;
956 }
957
958 length = (io.nblocks + 1) << ns->lba_shift;
959 meta_len = (io.nblocks + 1) * ns->ms;
960 metadata = (void __user *)(uintptr_t)io.metadata;
961
962 if (ns->ext) {
963 length += meta_len;
964 meta_len = 0;
965 } else if (meta_len) {
966 if ((io.metadata & 3) || !io.metadata)
967 return -EINVAL;
968 }
969
970 memset(&c, 0, sizeof(c));
971 c.rw.opcode = io.opcode;
972 c.rw.flags = io.flags;
973 c.rw.nsid = cpu_to_le32(ns->ns_id);
974 c.rw.slba = cpu_to_le64(io.slba);
975 c.rw.length = cpu_to_le16(io.nblocks);
976 c.rw.control = cpu_to_le16(io.control);
977 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
978 c.rw.reftag = cpu_to_le32(io.reftag);
979 c.rw.apptag = cpu_to_le16(io.apptag);
980 c.rw.appmask = cpu_to_le16(io.appmask);
981
Keith Busch63263d62017-08-29 17:46:04 -0400982 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100983 (void __user *)(uintptr_t)io.addr, length,
984 metadata, meta_len, io.slba, NULL, 0);
985}
986
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100987static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100988 struct nvme_passthru_cmd __user *ucmd)
989{
990 struct nvme_passthru_cmd cmd;
991 struct nvme_command c;
992 unsigned timeout = 0;
993 int status;
994
995 if (!capable(CAP_SYS_ADMIN))
996 return -EACCES;
997 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
998 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700999 if (cmd.flags)
1000 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001001
1002 memset(&c, 0, sizeof(c));
1003 c.common.opcode = cmd.opcode;
1004 c.common.flags = cmd.flags;
1005 c.common.nsid = cpu_to_le32(cmd.nsid);
1006 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1007 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1008 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1009 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1010 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1011 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1012 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1013 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1014
1015 if (cmd.timeout_ms)
1016 timeout = msecs_to_jiffies(cmd.timeout_ms);
1017
1018 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001019 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001020 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1021 0, &cmd.result, timeout);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001022 if (status >= 0) {
1023 if (put_user(cmd.result, &ucmd->result))
1024 return -EFAULT;
1025 }
1026
1027 return status;
1028}
1029
1030static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1031 unsigned int cmd, unsigned long arg)
1032{
1033 struct nvme_ns *ns = bdev->bd_disk->private_data;
1034
1035 switch (cmd) {
1036 case NVME_IOCTL_ID:
1037 force_successful_syscall_return();
1038 return ns->ns_id;
1039 case NVME_IOCTL_ADMIN_CMD:
1040 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1041 case NVME_IOCTL_IO_CMD:
1042 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1043 case NVME_IOCTL_SUBMIT_IO:
1044 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001045 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001046#ifdef CONFIG_NVM
1047 if (ns->ndev)
1048 return nvme_nvm_ioctl(ns, cmd, arg);
1049#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001050 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001051 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001052 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001053 return -ENOTTY;
1054 }
1055}
1056
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001057static int nvme_open(struct block_device *bdev, fmode_t mode)
1058{
1059 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1060}
1061
1062static void nvme_release(struct gendisk *disk, fmode_t mode)
1063{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001064 struct nvme_ns *ns = disk->private_data;
1065
1066 module_put(ns->ctrl->ops->module);
1067 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001068}
1069
1070static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1071{
1072 /* some standard values */
1073 geo->heads = 1 << 6;
1074 geo->sectors = 1 << 5;
1075 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1076 return 0;
1077}
1078
1079#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001080static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1081 u16 bs)
1082{
1083 struct nvme_ns *ns = disk->private_data;
1084 u16 old_ms = ns->ms;
1085 u8 pi_type = 0;
1086
1087 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1088 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1089
1090 /* PI implementation requires metadata equal t10 pi tuple size */
1091 if (ns->ms == sizeof(struct t10_pi_tuple))
1092 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1093
1094 if (blk_get_integrity(disk) &&
1095 (ns->pi_type != pi_type || ns->ms != old_ms ||
1096 bs != queue_logical_block_size(disk->queue) ||
1097 (ns->ms && ns->ext)))
1098 blk_integrity_unregister(disk);
1099
1100 ns->pi_type = pi_type;
1101}
1102
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001103static void nvme_init_integrity(struct nvme_ns *ns)
1104{
1105 struct blk_integrity integrity;
1106
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001107 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001108 switch (ns->pi_type) {
1109 case NVME_NS_DPS_PI_TYPE3:
1110 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001111 integrity.tag_size = sizeof(u16) + sizeof(u32);
1112 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001113 break;
1114 case NVME_NS_DPS_PI_TYPE1:
1115 case NVME_NS_DPS_PI_TYPE2:
1116 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001117 integrity.tag_size = sizeof(u16);
1118 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001119 break;
1120 default:
1121 integrity.profile = NULL;
1122 break;
1123 }
1124 integrity.tuple_size = ns->ms;
1125 blk_integrity_register(ns->disk, &integrity);
1126 blk_queue_max_integrity_segments(ns->queue, 1);
1127}
1128#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001129static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1130 u16 bs)
1131{
1132}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001133static void nvme_init_integrity(struct nvme_ns *ns)
1134{
1135}
1136#endif /* CONFIG_BLK_DEV_INTEGRITY */
1137
Scott Bauer6b8190d2017-06-15 10:44:30 -06001138static void nvme_set_chunk_size(struct nvme_ns *ns)
1139{
1140 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1141 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1142}
1143
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001144static void nvme_config_discard(struct nvme_ns *ns)
1145{
Keith Busch08095e72016-03-04 13:15:17 -07001146 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001147 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001148
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001149 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1150 NVME_DSM_MAX_RANGES);
1151
Jens Axboef5d11842017-06-27 12:03:06 -06001152 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1153 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1154
1155 ns->queue->limits.discard_alignment = sz;
1156 ns->queue->limits.discard_granularity = sz;
1157 } else {
1158 ns->queue->limits.discard_alignment = logical_block_size;
1159 ns->queue->limits.discard_granularity = logical_block_size;
1160 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001161 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001162 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001163 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001164
1165 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1166 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001167}
1168
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001169static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1170 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001171{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001172 if (ctrl->vs >= NVME_VS(1, 1, 0))
1173 memcpy(eui64, id->eui64, sizeof(id->eui64));
1174 if (ctrl->vs >= NVME_VS(1, 2, 0))
1175 memcpy(nguid, id->nguid, sizeof(id->nguid));
1176 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001177 /* Don't treat error as fatal we potentially
1178 * already have a NGUID or EUI-64
1179 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001180 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1181 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001182 "%s: Identify Descriptors failed\n", __func__);
1183 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001184}
1185
1186static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1187{
1188 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001189 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001190 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001191
1192 /*
1193 * If identify namespace failed, use default 512 byte block size so
1194 * block layer can use before failing read/write for 0 capacity.
1195 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001196 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001197 if (ns->lba_shift == 0)
1198 ns->lba_shift = 9;
1199 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001200 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001201
1202 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001203
Jens Axboef5d11842017-06-27 12:03:06 -06001204 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001205 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001206 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001207 if (ns->noiob)
1208 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001209 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001211 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1212 set_capacity(disk, 0);
1213 else
1214 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1215
Jens Axboef5d11842017-06-27 12:03:06 -06001216 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001217 nvme_config_discard(ns);
1218 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001219}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001220
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001221static int nvme_revalidate_disk(struct gendisk *disk)
1222{
1223 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001224 struct nvme_ctrl *ctrl = ns->ctrl;
1225 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001226 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1227 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001228 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001229
1230 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1231 set_capacity(disk, 0);
1232 return -ENODEV;
1233 }
1234
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001235 id = nvme_identify_ns(ctrl, ns->ns_id);
1236 if (!id)
1237 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001238
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001239 if (id->ncap == 0) {
1240 ret = -ENODEV;
1241 goto out;
1242 }
1243
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001244 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1245 if (!uuid_equal(&ns->uuid, &uuid) ||
1246 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1247 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1248 dev_err(ctrl->device,
1249 "identifiers changed for nsid %d\n", ns->ns_id);
1250 ret = -ENODEV;
1251 }
1252
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001253out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001254 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001255 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001256}
1257
1258static char nvme_pr_type(enum pr_type type)
1259{
1260 switch (type) {
1261 case PR_WRITE_EXCLUSIVE:
1262 return 1;
1263 case PR_EXCLUSIVE_ACCESS:
1264 return 2;
1265 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1266 return 3;
1267 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1268 return 4;
1269 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1270 return 5;
1271 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1272 return 6;
1273 default:
1274 return 0;
1275 }
1276};
1277
1278static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1279 u64 key, u64 sa_key, u8 op)
1280{
1281 struct nvme_ns *ns = bdev->bd_disk->private_data;
1282 struct nvme_command c;
1283 u8 data[16] = { 0, };
1284
1285 put_unaligned_le64(key, &data[0]);
1286 put_unaligned_le64(sa_key, &data[8]);
1287
1288 memset(&c, 0, sizeof(c));
1289 c.common.opcode = op;
1290 c.common.nsid = cpu_to_le32(ns->ns_id);
1291 c.common.cdw10[0] = cpu_to_le32(cdw10);
1292
1293 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1294}
1295
1296static int nvme_pr_register(struct block_device *bdev, u64 old,
1297 u64 new, unsigned flags)
1298{
1299 u32 cdw10;
1300
1301 if (flags & ~PR_FL_IGNORE_KEY)
1302 return -EOPNOTSUPP;
1303
1304 cdw10 = old ? 2 : 0;
1305 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1306 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1307 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1308}
1309
1310static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1311 enum pr_type type, unsigned flags)
1312{
1313 u32 cdw10;
1314
1315 if (flags & ~PR_FL_IGNORE_KEY)
1316 return -EOPNOTSUPP;
1317
1318 cdw10 = nvme_pr_type(type) << 8;
1319 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1320 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1321}
1322
1323static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1324 enum pr_type type, bool abort)
1325{
1326 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1327 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1328}
1329
1330static int nvme_pr_clear(struct block_device *bdev, u64 key)
1331{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001332 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001333 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1334}
1335
1336static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1337{
1338 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1339 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1340}
1341
1342static const struct pr_ops nvme_pr_ops = {
1343 .pr_register = nvme_pr_register,
1344 .pr_reserve = nvme_pr_reserve,
1345 .pr_release = nvme_pr_release,
1346 .pr_preempt = nvme_pr_preempt,
1347 .pr_clear = nvme_pr_clear,
1348};
1349
Scott Bauera98e58e52017-02-03 12:50:32 -07001350#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001351int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1352 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001353{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001354 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001355 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001356
1357 memset(&cmd, 0, sizeof(cmd));
1358 if (send)
1359 cmd.common.opcode = nvme_admin_security_send;
1360 else
1361 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001362 cmd.common.nsid = 0;
1363 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1364 cmd.common.cdw10[1] = cpu_to_le32(len);
1365
1366 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1367 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1368}
1369EXPORT_SYMBOL_GPL(nvme_sec_submit);
1370#endif /* CONFIG_BLK_SED_OPAL */
1371
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001372static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001373 .owner = THIS_MODULE,
1374 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001375 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001376 .open = nvme_open,
1377 .release = nvme_release,
1378 .getgeo = nvme_getgeo,
1379 .revalidate_disk= nvme_revalidate_disk,
1380 .pr_ops = &nvme_pr_ops,
1381};
1382
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001383static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1384{
1385 unsigned long timeout =
1386 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1387 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1388 int ret;
1389
1390 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001391 if (csts == ~0)
1392 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001393 if ((csts & NVME_CSTS_RDY) == bit)
1394 break;
1395
1396 msleep(100);
1397 if (fatal_signal_pending(current))
1398 return -EINTR;
1399 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001400 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001401 "Device not ready; aborting %s\n", enabled ?
1402 "initialisation" : "reset");
1403 return -ENODEV;
1404 }
1405 }
1406
1407 return ret;
1408}
1409
1410/*
1411 * If the device has been passed off to us in an enabled state, just clear
1412 * the enabled bit. The spec says we should set the 'shutdown notification
1413 * bits', but doing so may cause the device to complete commands to the
1414 * admin queue ... and we don't know what memory that might be pointing at!
1415 */
1416int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1417{
1418 int ret;
1419
1420 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1421 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1422
1423 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1424 if (ret)
1425 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001426
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001427 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001428 msleep(NVME_QUIRK_DELAY_AMOUNT);
1429
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001430 return nvme_wait_ready(ctrl, cap, false);
1431}
Ming Lin576d55d2016-02-10 10:03:32 -08001432EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001433
1434int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1435{
1436 /*
1437 * Default to a 4K page size, with the intention to update this
1438 * path in the future to accomodate architectures with differing
1439 * kernel and IO page sizes.
1440 */
1441 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1442 int ret;
1443
1444 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001445 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001446 "Minimum device page size %u too large for host (%u)\n",
1447 1 << dev_page_min, 1 << page_shift);
1448 return -ENODEV;
1449 }
1450
1451 ctrl->page_size = 1 << page_shift;
1452
1453 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1454 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001455 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001456 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1457 ctrl->ctrl_config |= NVME_CC_ENABLE;
1458
1459 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1460 if (ret)
1461 return ret;
1462 return nvme_wait_ready(ctrl, cap, true);
1463}
Ming Lin576d55d2016-02-10 10:03:32 -08001464EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001465
1466int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1467{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001468 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001469 u32 csts;
1470 int ret;
1471
1472 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1473 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1474
1475 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1476 if (ret)
1477 return ret;
1478
1479 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1480 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1481 break;
1482
1483 msleep(100);
1484 if (fatal_signal_pending(current))
1485 return -EINTR;
1486 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001487 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001488 "Device shutdown incomplete; abort shutdown\n");
1489 return -ENODEV;
1490 }
1491 }
1492
1493 return ret;
1494}
Ming Lin576d55d2016-02-10 10:03:32 -08001495EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001496
Christoph Hellwigda358252016-03-02 18:07:11 +01001497static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1498 struct request_queue *q)
1499{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001500 bool vwc = false;
1501
Christoph Hellwigda358252016-03-02 18:07:11 +01001502 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001503 u32 max_segments =
1504 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1505
Christoph Hellwigda358252016-03-02 18:07:11 +01001506 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001507 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001508 }
Keith Busche6282ae2016-12-19 11:37:50 -05001509 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1510 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001511 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001512 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1513 vwc = true;
1514 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001515}
1516
Jon Derrickdbf86b32017-08-16 09:51:29 +02001517static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1518{
1519 __le64 ts;
1520 int ret;
1521
1522 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1523 return 0;
1524
1525 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1526 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1527 NULL);
1528 if (ret)
1529 dev_warn_once(ctrl->device,
1530 "could not set timestamp (%d)\n", ret);
1531 return ret;
1532}
1533
Keith Busch634b8322017-08-10 11:23:31 +02001534static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001535{
1536 /*
1537 * APST (Autonomous Power State Transition) lets us program a
1538 * table of power state transitions that the controller will
1539 * perform automatically. We configure it with a simple
1540 * heuristic: we are willing to spend at most 2% of the time
1541 * transitioning between power states. Therefore, when running
1542 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001543 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001544 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001545 * the requested maximum latency.
1546 *
1547 * We will not autonomously enter any non-operational state for
1548 * which the total latency exceeds ps_max_latency_us. Users
1549 * can set ps_max_latency_us to zero to turn off APST.
1550 */
1551
1552 unsigned apste;
1553 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001554 u64 max_lat_us = 0;
1555 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001556 int ret;
1557
1558 /*
1559 * If APST isn't supported or if we haven't been initialized yet,
1560 * then don't do anything.
1561 */
1562 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001563 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001564
1565 if (ctrl->npss > 31) {
1566 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001567 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001568 }
1569
1570 table = kzalloc(sizeof(*table), GFP_KERNEL);
1571 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001572 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001573
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001574 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001575 /* Turn off APST. */
1576 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001577 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001578 } else {
1579 __le64 target = cpu_to_le64(0);
1580 int state;
1581
1582 /*
1583 * Walk through all states from lowest- to highest-power.
1584 * According to the spec, lower-numbered states use more
1585 * power. NPSS, despite the name, is the index of the
1586 * lowest-power state, not the number of states.
1587 */
1588 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001589 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001590
1591 if (target)
1592 table->entries[state] = target;
1593
1594 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001595 * Don't allow transitions to the deepest state
1596 * if it's quirked off.
1597 */
1598 if (state == ctrl->npss &&
1599 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1600 continue;
1601
1602 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001603 * Is this state a useful non-operational state for
1604 * higher-power states to autonomously transition to?
1605 */
1606 if (!(ctrl->psd[state].flags &
1607 NVME_PS_FLAGS_NON_OP_STATE))
1608 continue;
1609
Kai-Heng Fengda875912017-06-07 15:25:42 +08001610 exit_latency_us =
1611 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1612 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001613 continue;
1614
Kai-Heng Fengda875912017-06-07 15:25:42 +08001615 total_latency_us =
1616 exit_latency_us +
1617 le32_to_cpu(ctrl->psd[state].entry_lat);
1618
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001619 /*
1620 * This state is good. Use it as the APST idle
1621 * target for higher power states.
1622 */
1623 transition_ms = total_latency_us + 19;
1624 do_div(transition_ms, 20);
1625 if (transition_ms > (1 << 24) - 1)
1626 transition_ms = (1 << 24) - 1;
1627
1628 target = cpu_to_le64((state << 3) |
1629 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001630
1631 if (max_ps == -1)
1632 max_ps = state;
1633
1634 if (total_latency_us > max_lat_us)
1635 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001636 }
1637
1638 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001639
1640 if (max_ps == -1) {
1641 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1642 } else {
1643 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1644 max_ps, max_lat_us, (int)sizeof(*table), table);
1645 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001646 }
1647
1648 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1649 table, sizeof(*table), NULL);
1650 if (ret)
1651 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1652
1653 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001654 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001655}
1656
1657static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1658{
1659 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1660 u64 latency;
1661
1662 switch (val) {
1663 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1664 case PM_QOS_LATENCY_ANY:
1665 latency = U64_MAX;
1666 break;
1667
1668 default:
1669 latency = val;
1670 }
1671
1672 if (ctrl->ps_max_latency_us != latency) {
1673 ctrl->ps_max_latency_us = latency;
1674 nvme_configure_apst(ctrl);
1675 }
1676}
1677
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001678struct nvme_core_quirk_entry {
1679 /*
1680 * NVMe model and firmware strings are padded with spaces. For
1681 * simplicity, strings in the quirk table are padded with NULLs
1682 * instead.
1683 */
1684 u16 vid;
1685 const char *mn;
1686 const char *fr;
1687 unsigned long quirks;
1688};
1689
1690static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001691 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001692 /*
1693 * This Toshiba device seems to die using any APST states. See:
1694 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1695 */
1696 .vid = 0x1179,
1697 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001698 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001699 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001700};
1701
1702/* match is null-terminated but idstr is space-padded. */
1703static bool string_matches(const char *idstr, const char *match, size_t len)
1704{
1705 size_t matchlen;
1706
1707 if (!match)
1708 return true;
1709
1710 matchlen = strlen(match);
1711 WARN_ON_ONCE(matchlen > len);
1712
1713 if (memcmp(idstr, match, matchlen))
1714 return false;
1715
1716 for (; matchlen < len; matchlen++)
1717 if (idstr[matchlen] != ' ')
1718 return false;
1719
1720 return true;
1721}
1722
1723static bool quirk_matches(const struct nvme_id_ctrl *id,
1724 const struct nvme_core_quirk_entry *q)
1725{
1726 return q->vid == le16_to_cpu(id->vid) &&
1727 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1728 string_matches(id->fr, q->fr, sizeof(id->fr));
1729}
1730
Christoph Hellwig180de0072017-06-26 12:39:02 +02001731static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1732{
1733 size_t nqnlen;
1734 int off;
1735
1736 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1737 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1738 strcpy(ctrl->subnqn, id->subnqn);
1739 return;
1740 }
1741
1742 if (ctrl->vs >= NVME_VS(1, 2, 1))
1743 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1744
1745 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1746 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1747 "nqn.2014.08.org.nvmexpress:%4x%4x",
1748 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1749 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1750 off += sizeof(id->sn);
1751 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1752 off += sizeof(id->mn);
1753 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1754}
1755
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001756/*
1757 * Initialize the cached copies of the Identify data and various controller
1758 * register in our nvme_ctrl structure. This should be called as soon as
1759 * the admin queue is fully up and running.
1760 */
1761int nvme_init_identify(struct nvme_ctrl *ctrl)
1762{
1763 struct nvme_id_ctrl *id;
1764 u64 cap;
1765 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001766 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001767 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001768
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001769 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1770 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001771 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001772 return ret;
1773 }
1774
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001775 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1776 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001777 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001778 return ret;
1779 }
1780 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1781
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001782 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001783 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1784
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001785 ret = nvme_identify_ctrl(ctrl, &id);
1786 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001787 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001788 return -EIO;
1789 }
1790
Christoph Hellwig180de0072017-06-26 12:39:02 +02001791 nvme_init_subnqn(ctrl, id);
1792
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001793 if (!ctrl->identified) {
1794 /*
1795 * Check for quirks. Quirk can depend on firmware version,
1796 * so, in principle, the set of quirks present can change
1797 * across a reset. As a possible future enhancement, we
1798 * could re-scan for quirks every time we reinitialize
1799 * the device, but we'd have to make sure that the driver
1800 * behaves intelligently if the quirks change.
1801 */
1802
1803 int i;
1804
1805 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1806 if (quirk_matches(id, &core_quirks[i]))
1807 ctrl->quirks |= core_quirks[i].quirks;
1808 }
1809 }
1810
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001811 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001812 dev_warn(ctrl->device, "forcibly allowing all power states due to nvme_core.force_apst -- use at your own risk\n");
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001813 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1814 }
1815
Scott Bauer8a9ae522017-02-17 13:59:40 +01001816 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001817 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001818 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001819 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001820 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001821 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001822 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1823 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1824 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1825 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001826 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001827 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001828 max_hw_sectors = UINT_MAX;
1829 ctrl->max_hw_sectors =
1830 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001831
Christoph Hellwigda358252016-03-02 18:07:11 +01001832 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001833 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001834 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001835
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001836 if (id->rtd3e) {
1837 /* us -> s */
1838 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1839
1840 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1841 shutdown_timeout, 60);
1842
1843 if (ctrl->shutdown_timeout != shutdown_timeout)
1844 dev_warn(ctrl->device,
1845 "Shutdown timeout set to %u seconds\n",
1846 ctrl->shutdown_timeout);
1847 } else
1848 ctrl->shutdown_timeout = shutdown_timeout;
1849
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001850 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001851 ctrl->apsta = id->apsta;
1852 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001853 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1854 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001855 dev_warn(ctrl->device, "forcibly allowing APST due to nvme_core.force_apst -- use at your own risk\n");
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001856 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001857 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001858 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001859 }
1860 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001861 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001862 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001863 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1864
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001865 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001866 ctrl->icdoff = le16_to_cpu(id->icdoff);
1867 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1868 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1869 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1870
1871 /*
1872 * In fabrics we need to verify the cntlid matches the
1873 * admin connect
1874 */
Keith Busch634b8322017-08-10 11:23:31 +02001875 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001876 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001877 goto out_free;
1878 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001879
1880 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001881 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001882 "keep-alive support is mandatory for fabrics\n");
1883 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001884 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001885 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001886 } else {
1887 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001888 ctrl->hmpre = le32_to_cpu(id->hmpre);
1889 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001890 ctrl->hmminds = le32_to_cpu(id->hmminds);
1891 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001892 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001893
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001894 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001895
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001896 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001897 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001898 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001899 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1900
Keith Busch634b8322017-08-10 11:23:31 +02001901 ret = nvme_configure_apst(ctrl);
1902 if (ret < 0)
1903 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001904
1905 ret = nvme_configure_timestamp(ctrl);
1906 if (ret < 0)
1907 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001908
1909 ret = nvme_configure_directives(ctrl);
1910 if (ret < 0)
1911 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001912
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001913 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001914
Keith Busch634b8322017-08-10 11:23:31 +02001915 return 0;
1916
1917out_free:
1918 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001919 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001920}
Ming Lin576d55d2016-02-10 10:03:32 -08001921EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001922
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001923static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001924{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001925 struct nvme_ctrl *ctrl;
1926 int instance = iminor(inode);
1927 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001928
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001929 spin_lock(&dev_list_lock);
1930 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1931 if (ctrl->instance != instance)
1932 continue;
1933
1934 if (!ctrl->admin_q) {
1935 ret = -EWOULDBLOCK;
1936 break;
1937 }
1938 if (!kref_get_unless_zero(&ctrl->kref))
1939 break;
1940 file->private_data = ctrl;
1941 ret = 0;
1942 break;
1943 }
1944 spin_unlock(&dev_list_lock);
1945
1946 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001947}
1948
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001949static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001950{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001951 nvme_put_ctrl(file->private_data);
1952 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001953}
1954
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001955static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1956{
1957 struct nvme_ns *ns;
1958 int ret;
1959
1960 mutex_lock(&ctrl->namespaces_mutex);
1961 if (list_empty(&ctrl->namespaces)) {
1962 ret = -ENOTTY;
1963 goto out_unlock;
1964 }
1965
1966 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1967 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001968 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001969 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1970 ret = -EINVAL;
1971 goto out_unlock;
1972 }
1973
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001974 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001975 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1976 kref_get(&ns->kref);
1977 mutex_unlock(&ctrl->namespaces_mutex);
1978
1979 ret = nvme_user_cmd(ctrl, ns, argp);
1980 nvme_put_ns(ns);
1981 return ret;
1982
1983out_unlock:
1984 mutex_unlock(&ctrl->namespaces_mutex);
1985 return ret;
1986}
1987
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001988static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1989 unsigned long arg)
1990{
1991 struct nvme_ctrl *ctrl = file->private_data;
1992 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001993
1994 switch (cmd) {
1995 case NVME_IOCTL_ADMIN_CMD:
1996 return nvme_user_cmd(ctrl, NULL, argp);
1997 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001998 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001999 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002000 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002001 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002002 case NVME_IOCTL_SUBSYS_RESET:
2003 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002004 case NVME_IOCTL_RESCAN:
2005 nvme_queue_scan(ctrl);
2006 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002007 default:
2008 return -ENOTTY;
2009 }
2010}
2011
2012static const struct file_operations nvme_dev_fops = {
2013 .owner = THIS_MODULE,
2014 .open = nvme_dev_open,
2015 .release = nvme_dev_release,
2016 .unlocked_ioctl = nvme_dev_ioctl,
2017 .compat_ioctl = nvme_dev_ioctl,
2018};
2019
2020static ssize_t nvme_sysfs_reset(struct device *dev,
2021 struct device_attribute *attr, const char *buf,
2022 size_t count)
2023{
2024 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2025 int ret;
2026
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002027 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002028 if (ret < 0)
2029 return ret;
2030 return count;
2031}
2032static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2033
Keith Busch9ec3bb22016-04-29 15:45:18 -06002034static ssize_t nvme_sysfs_rescan(struct device *dev,
2035 struct device_attribute *attr, const char *buf,
2036 size_t count)
2037{
2038 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2039
2040 nvme_queue_scan(ctrl);
2041 return count;
2042}
2043static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2044
Keith Busch118472a2016-02-18 09:57:48 -07002045static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2046 char *buf)
2047{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002048 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002049 struct nvme_ctrl *ctrl = ns->ctrl;
2050 int serial_len = sizeof(ctrl->serial);
2051 int model_len = sizeof(ctrl->model);
2052
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002053 if (!uuid_is_null(&ns->uuid))
2054 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2055
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002056 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2057 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002058
2059 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2060 return sprintf(buf, "eui.%8phN\n", ns->eui);
2061
Martin Wilck758f3732017-07-20 18:34:02 +02002062 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2063 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002064 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002065 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2066 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002067 model_len--;
2068
2069 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2070 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2071}
2072static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2073
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002074static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2075 char *buf)
2076{
2077 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2078 return sprintf(buf, "%pU\n", ns->nguid);
2079}
2080static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2081
Keith Busch2b9b6e82015-12-22 10:10:45 -07002082static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2083 char *buf)
2084{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002085 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002086
2087 /* For backward compatibility expose the NGUID to userspace if
2088 * we have no UUID set
2089 */
2090 if (uuid_is_null(&ns->uuid)) {
2091 printk_ratelimited(KERN_WARNING
2092 "No UUID available providing old NGUID\n");
2093 return sprintf(buf, "%pU\n", ns->nguid);
2094 }
2095 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002096}
2097static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2098
2099static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2100 char *buf)
2101{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002102 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002103 return sprintf(buf, "%8phd\n", ns->eui);
2104}
2105static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2106
2107static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2108 char *buf)
2109{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002110 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002111 return sprintf(buf, "%d\n", ns->ns_id);
2112}
2113static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2114
2115static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002116 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002117 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002118 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002119 &dev_attr_eui.attr,
2120 &dev_attr_nsid.attr,
2121 NULL,
2122};
2123
Ming Lin1a353d82016-06-13 16:45:24 +02002124static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002125 struct attribute *a, int n)
2126{
2127 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002128 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002129
2130 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002131 if (uuid_is_null(&ns->uuid) ||
2132 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2133 return 0;
2134 }
2135 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002136 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002137 return 0;
2138 }
2139 if (a == &dev_attr_eui.attr) {
2140 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2141 return 0;
2142 }
2143 return a->mode;
2144}
2145
2146static const struct attribute_group nvme_ns_attr_group = {
2147 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002148 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002149};
2150
Ming Lin931e1c22016-02-26 13:24:19 -08002151#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002152static ssize_t field##_show(struct device *dev, \
2153 struct device_attribute *attr, char *buf) \
2154{ \
2155 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2156 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2157} \
2158static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2159
Ming Lin931e1c22016-02-26 13:24:19 -08002160#define nvme_show_int_function(field) \
2161static ssize_t field##_show(struct device *dev, \
2162 struct device_attribute *attr, char *buf) \
2163{ \
2164 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2165 return sprintf(buf, "%d\n", ctrl->field); \
2166} \
2167static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2168
2169nvme_show_str_function(model);
2170nvme_show_str_function(serial);
2171nvme_show_str_function(firmware_rev);
2172nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002173
Ming Lin1a353d82016-06-13 16:45:24 +02002174static ssize_t nvme_sysfs_delete(struct device *dev,
2175 struct device_attribute *attr, const char *buf,
2176 size_t count)
2177{
2178 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2179
2180 if (device_remove_file_self(dev, attr))
2181 ctrl->ops->delete_ctrl(ctrl);
2182 return count;
2183}
2184static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2185
2186static ssize_t nvme_sysfs_show_transport(struct device *dev,
2187 struct device_attribute *attr,
2188 char *buf)
2189{
2190 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2191
2192 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2193}
2194static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2195
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002196static ssize_t nvme_sysfs_show_state(struct device *dev,
2197 struct device_attribute *attr,
2198 char *buf)
2199{
2200 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2201 static const char *const state_name[] = {
2202 [NVME_CTRL_NEW] = "new",
2203 [NVME_CTRL_LIVE] = "live",
2204 [NVME_CTRL_RESETTING] = "resetting",
2205 [NVME_CTRL_RECONNECTING]= "reconnecting",
2206 [NVME_CTRL_DELETING] = "deleting",
2207 [NVME_CTRL_DEAD] = "dead",
2208 };
2209
2210 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2211 state_name[ctrl->state])
2212 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2213
2214 return sprintf(buf, "unknown state\n");
2215}
2216
2217static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2218
Ming Lin1a353d82016-06-13 16:45:24 +02002219static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2220 struct device_attribute *attr,
2221 char *buf)
2222{
2223 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2224
Christoph Hellwig180de0072017-06-26 12:39:02 +02002225 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002226}
2227static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2228
2229static ssize_t nvme_sysfs_show_address(struct device *dev,
2230 struct device_attribute *attr,
2231 char *buf)
2232{
2233 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2234
2235 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2236}
2237static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2238
Keith Busch779ff7562016-01-12 15:09:31 -07002239static struct attribute *nvme_dev_attrs[] = {
2240 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002241 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002242 &dev_attr_model.attr,
2243 &dev_attr_serial.attr,
2244 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002245 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002246 &dev_attr_delete_controller.attr,
2247 &dev_attr_transport.attr,
2248 &dev_attr_subsysnqn.attr,
2249 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002250 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002251 NULL
2252};
2253
Ming Lin1a353d82016-06-13 16:45:24 +02002254static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2255 struct attribute *a, int n)
2256{
2257 struct device *dev = container_of(kobj, struct device, kobj);
2258 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2259
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002260 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2261 return 0;
2262 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2263 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002264
2265 return a->mode;
2266}
2267
Keith Busch779ff7562016-01-12 15:09:31 -07002268static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002269 .attrs = nvme_dev_attrs,
2270 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002271};
2272
2273static const struct attribute_group *nvme_dev_attr_groups[] = {
2274 &nvme_dev_attrs_group,
2275 NULL,
2276};
2277
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002278static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2279{
2280 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2281 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2282
2283 return nsa->ns_id - nsb->ns_id;
2284}
2285
Keith Busch32f0c4a2016-07-13 11:45:02 -06002286static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002287{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002288 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002289
Keith Busch32f0c4a2016-07-13 11:45:02 -06002290 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002291 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002292 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002293 if (!kref_get_unless_zero(&ns->kref))
2294 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002295 ret = ns;
2296 break;
2297 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002298 if (ns->ns_id > nsid)
2299 break;
2300 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002301 mutex_unlock(&ctrl->namespaces_mutex);
2302 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002303}
2304
Jens Axboef5d11842017-06-27 12:03:06 -06002305static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2306{
2307 struct streams_directive_params s;
2308 int ret;
2309
2310 if (!ctrl->nr_streams)
2311 return 0;
2312
2313 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2314 if (ret)
2315 return ret;
2316
2317 ns->sws = le32_to_cpu(s.sws);
2318 ns->sgs = le16_to_cpu(s.sgs);
2319
2320 if (ns->sws) {
2321 unsigned int bs = 1 << ns->lba_shift;
2322
2323 blk_queue_io_min(ns->queue, bs * ns->sws);
2324 if (ns->sgs)
2325 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2326 }
2327
2328 return 0;
2329}
2330
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002331static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2332{
2333 struct nvme_ns *ns;
2334 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002335 struct nvme_id_ns *id;
2336 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002337 int node = dev_to_node(ctrl->dev);
2338
2339 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2340 if (!ns)
2341 return;
2342
Keith Busch075790e2016-02-24 09:15:53 -07002343 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2344 if (ns->instance < 0)
2345 goto out_free_ns;
2346
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002347 ns->queue = blk_mq_init_queue(ctrl->tagset);
2348 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002349 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002350 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2351 ns->queue->queuedata = ns;
2352 ns->ctrl = ctrl;
2353
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002354 kref_init(&ns->kref);
2355 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002356 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002357
2358 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002359 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002360 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002361
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002362 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002363
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002364 id = nvme_identify_ns(ctrl, nsid);
2365 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002366 goto out_free_queue;
2367
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002368 if (id->ncap == 0)
2369 goto out_free_id;
2370
2371 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2372
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002373 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2374 if (nvme_nvm_register(ns, disk_name, node)) {
2375 dev_warn(ctrl->device, "LightNVM init failure\n");
2376 goto out_free_id;
2377 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002378 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002379
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002380 disk = alloc_disk_node(0, node);
2381 if (!disk)
2382 goto out_free_id;
2383
2384 disk->fops = &nvme_fops;
2385 disk->private_data = ns;
2386 disk->queue = ns->queue;
2387 disk->flags = GENHD_FL_EXT_DEVT;
2388 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2389 ns->disk = disk;
2390
2391 __nvme_revalidate_disk(disk, id);
2392
Keith Busch32f0c4a2016-07-13 11:45:02 -06002393 mutex_lock(&ctrl->namespaces_mutex);
2394 list_add_tail(&ns->list, &ctrl->namespaces);
2395 mutex_unlock(&ctrl->namespaces_mutex);
2396
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002397 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002398
2399 kfree(id);
2400
Dan Williams0d52c7562016-06-15 19:44:20 -07002401 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002402 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2403 &nvme_ns_attr_group))
2404 pr_warn("%s: failed to create sysfs group for identification\n",
2405 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002406 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2407 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2408 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002409 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002410 out_free_id:
2411 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002412 out_free_queue:
2413 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002414 out_release_instance:
2415 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002416 out_free_ns:
2417 kfree(ns);
2418}
2419
2420static void nvme_ns_remove(struct nvme_ns *ns)
2421{
Keith Busch646017a2016-02-24 09:15:54 -07002422 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2423 return;
2424
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002425 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002426 if (blk_get_integrity(ns->disk))
2427 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002428 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2429 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002430 if (ns->ndev)
2431 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002432 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002433 blk_cleanup_queue(ns->queue);
2434 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002435
2436 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002437 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002438 mutex_unlock(&ns->ctrl->namespaces_mutex);
2439
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002440 nvme_put_ns(ns);
2441}
2442
Keith Busch540c8012015-10-22 15:45:06 -06002443static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2444{
2445 struct nvme_ns *ns;
2446
Keith Busch32f0c4a2016-07-13 11:45:02 -06002447 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002448 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002449 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002450 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002451 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002452 } else
2453 nvme_alloc_ns(ctrl, nsid);
2454}
2455
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302456static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2457 unsigned nsid)
2458{
2459 struct nvme_ns *ns, *next;
2460
2461 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2462 if (ns->ns_id > nsid)
2463 nvme_ns_remove(ns);
2464 }
2465}
2466
Keith Busch540c8012015-10-22 15:45:06 -06002467static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2468{
2469 struct nvme_ns *ns;
2470 __le32 *ns_list;
2471 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2472 int ret = 0;
2473
2474 ns_list = kzalloc(0x1000, GFP_KERNEL);
2475 if (!ns_list)
2476 return -ENOMEM;
2477
2478 for (i = 0; i < num_lists; i++) {
2479 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2480 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302481 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002482
2483 for (j = 0; j < min(nn, 1024U); j++) {
2484 nsid = le32_to_cpu(ns_list[j]);
2485 if (!nsid)
2486 goto out;
2487
2488 nvme_validate_ns(ctrl, nsid);
2489
2490 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002491 ns = nvme_find_get_ns(ctrl, prev);
2492 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002493 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002494 nvme_put_ns(ns);
2495 }
Keith Busch540c8012015-10-22 15:45:06 -06002496 }
2497 }
2498 nn -= j;
2499 }
2500 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302501 nvme_remove_invalid_namespaces(ctrl, prev);
2502 free:
Keith Busch540c8012015-10-22 15:45:06 -06002503 kfree(ns_list);
2504 return ret;
2505}
2506
Christoph Hellwig5955be22016-04-26 13:51:59 +02002507static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002508{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002509 unsigned i;
2510
Keith Busch540c8012015-10-22 15:45:06 -06002511 for (i = 1; i <= nn; i++)
2512 nvme_validate_ns(ctrl, i);
2513
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302514 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002515}
2516
Christoph Hellwig5955be22016-04-26 13:51:59 +02002517static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002518{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002519 struct nvme_ctrl *ctrl =
2520 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002521 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002522 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002523
Christoph Hellwig5955be22016-04-26 13:51:59 +02002524 if (ctrl->state != NVME_CTRL_LIVE)
2525 return;
2526
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002527 if (nvme_identify_ctrl(ctrl, &id))
2528 return;
Keith Busch540c8012015-10-22 15:45:06 -06002529
2530 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002531 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002532 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2533 if (!nvme_scan_ns_list(ctrl, nn))
2534 goto done;
2535 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002536 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002537 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002538 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002539 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002540 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002541 kfree(id);
2542}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002543
2544void nvme_queue_scan(struct nvme_ctrl *ctrl)
2545{
2546 /*
2547 * Do not queue new scan work when a controller is reset during
2548 * removal.
2549 */
2550 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002551 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002552}
2553EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002554
Keith Busch32f0c4a2016-07-13 11:45:02 -06002555/*
2556 * This function iterates the namespace list unlocked to allow recovery from
2557 * controller failure. It is up to the caller to ensure the namespace list is
2558 * not modified by scan work while this function is executing.
2559 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002560void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2561{
2562 struct nvme_ns *ns, *next;
2563
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002564 /*
2565 * The dead states indicates the controller was not gracefully
2566 * disconnected. In that case, we won't be able to flush any data while
2567 * removing the namespaces' disks; fail all the queues now to avoid
2568 * potentially having to clean up the failed sync later.
2569 */
2570 if (ctrl->state == NVME_CTRL_DEAD)
2571 nvme_kill_queues(ctrl);
2572
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002573 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2574 nvme_ns_remove(ns);
2575}
Ming Lin576d55d2016-02-10 10:03:32 -08002576EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002577
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002578static void nvme_async_event_work(struct work_struct *work)
2579{
2580 struct nvme_ctrl *ctrl =
2581 container_of(work, struct nvme_ctrl, async_event_work);
2582
2583 spin_lock_irq(&ctrl->lock);
James Smartcd482822017-09-14 11:03:09 -07002584 while (ctrl->state == NVME_CTRL_LIVE && ctrl->event_limit > 0) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002585 int aer_idx = --ctrl->event_limit;
2586
2587 spin_unlock_irq(&ctrl->lock);
2588 ctrl->ops->submit_async_event(ctrl, aer_idx);
2589 spin_lock_irq(&ctrl->lock);
2590 }
2591 spin_unlock_irq(&ctrl->lock);
2592}
2593
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302594static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2595{
2596
2597 u32 csts;
2598
2599 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2600 return false;
2601
2602 if (csts == ~0)
2603 return false;
2604
2605 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2606}
2607
2608static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2609{
2610 struct nvme_command c = { };
2611 struct nvme_fw_slot_info_log *log;
2612
2613 log = kmalloc(sizeof(*log), GFP_KERNEL);
2614 if (!log)
2615 return;
2616
2617 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302618 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302619 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2620
2621 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2622 dev_warn(ctrl->device,
2623 "Get FW SLOT INFO log error\n");
2624 kfree(log);
2625}
2626
2627static void nvme_fw_act_work(struct work_struct *work)
2628{
2629 struct nvme_ctrl *ctrl = container_of(work,
2630 struct nvme_ctrl, fw_act_work);
2631 unsigned long fw_act_timeout;
2632
2633 if (ctrl->mtfa)
2634 fw_act_timeout = jiffies +
2635 msecs_to_jiffies(ctrl->mtfa * 100);
2636 else
2637 fw_act_timeout = jiffies +
2638 msecs_to_jiffies(admin_timeout * 1000);
2639
2640 nvme_stop_queues(ctrl);
2641 while (nvme_ctrl_pp_status(ctrl)) {
2642 if (time_after(jiffies, fw_act_timeout)) {
2643 dev_warn(ctrl->device,
2644 "Fw activation timeout, reset controller\n");
2645 nvme_reset_ctrl(ctrl);
2646 break;
2647 }
2648 msleep(100);
2649 }
2650
2651 if (ctrl->state != NVME_CTRL_LIVE)
2652 return;
2653
2654 nvme_start_queues(ctrl);
2655 /* read FW slot informationi to clear the AER*/
2656 nvme_get_fw_slot_info(ctrl);
2657}
2658
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002659void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2660 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002661{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002662 u32 result = le32_to_cpu(res->u32);
2663 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002664
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002665 switch (le16_to_cpu(status) >> 1) {
2666 case NVME_SC_SUCCESS:
2667 done = false;
2668 /*FALLTHRU*/
2669 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002670 ++ctrl->event_limit;
James Smartcd482822017-09-14 11:03:09 -07002671 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002672 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002673 break;
2674 default:
2675 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002676 }
2677
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002678 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002679 return;
2680
2681 switch (result & 0xff07) {
2682 case NVME_AER_NOTICE_NS_CHANGED:
2683 dev_info(ctrl->device, "rescanning\n");
2684 nvme_queue_scan(ctrl);
2685 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302686 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002687 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302688 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002689 default:
2690 dev_warn(ctrl->device, "async event result %08x\n", result);
2691 }
2692}
2693EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2694
2695void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2696{
2697 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002698 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002699}
2700EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2701
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002702void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002703{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002704 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002705 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002706 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302707 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002708}
2709EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002710
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002711void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2712{
2713 if (ctrl->kato)
2714 nvme_start_keep_alive(ctrl);
2715
2716 if (ctrl->queue_count > 1) {
2717 nvme_queue_scan(ctrl);
2718 nvme_queue_async_events(ctrl);
2719 nvme_start_queues(ctrl);
2720 }
2721}
2722EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2723
2724void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2725{
Keith Busch53029b02015-11-28 15:41:02 +01002726 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002727
2728 spin_lock(&dev_list_lock);
2729 list_del(&ctrl->node);
2730 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002731}
Ming Lin576d55d2016-02-10 10:03:32 -08002732EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002733
2734static void nvme_free_ctrl(struct kref *kref)
2735{
2736 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002737
2738 put_device(ctrl->device);
Christoph Hellwig9843f682017-10-18 13:10:01 +02002739 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002740 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002741
2742 ctrl->ops->free_ctrl(ctrl);
2743}
2744
2745void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2746{
2747 kref_put(&ctrl->kref, nvme_free_ctrl);
2748}
Ming Lin576d55d2016-02-10 10:03:32 -08002749EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002750
2751/*
2752 * Initialize a NVMe controller structures. This needs to be called during
2753 * earliest initialization so that we have the initialized structured around
2754 * during probing.
2755 */
2756int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2757 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2758{
2759 int ret;
2760
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002761 ctrl->state = NVME_CTRL_NEW;
2762 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002763 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002764 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002765 kref_init(&ctrl->kref);
2766 ctrl->dev = dev;
2767 ctrl->ops = ops;
2768 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002769 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002770 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302771 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002772
Christoph Hellwig9843f682017-10-18 13:10:01 +02002773 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2774 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002775 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002776 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002777
Keith Busch779ff7562016-01-12 15:09:31 -07002778 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002779 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002780 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002781 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002782 if (IS_ERR(ctrl->device)) {
2783 ret = PTR_ERR(ctrl->device);
2784 goto out_release_instance;
2785 }
2786 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002787 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002788
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002789 spin_lock(&dev_list_lock);
2790 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2791 spin_unlock(&dev_list_lock);
2792
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002793 /*
2794 * Initialize latency tolerance controls. The sysfs files won't
2795 * be visible to userspace unless the device actually supports APST.
2796 */
2797 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2798 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2799 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2800
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002801 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002802out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02002803 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002804out:
2805 return ret;
2806}
Ming Lin576d55d2016-02-10 10:03:32 -08002807EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002808
Keith Busch69d9a992016-02-24 09:15:56 -07002809/**
2810 * nvme_kill_queues(): Ends all namespace queues
2811 * @ctrl: the dead controller that needs to end
2812 *
2813 * Call this function when the driver determines it is unable to get the
2814 * controller in a state capable of servicing IO.
2815 */
2816void nvme_kill_queues(struct nvme_ctrl *ctrl)
2817{
2818 struct nvme_ns *ns;
2819
Keith Busch32f0c4a2016-07-13 11:45:02 -06002820 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002821
Ming Lei443bd902017-06-19 10:21:08 +08002822 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002823 if (ctrl->admin_q)
2824 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002825
Keith Busch32f0c4a2016-07-13 11:45:02 -06002826 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002827 /*
2828 * Revalidating a dead namespace sets capacity to 0. This will
2829 * end buffered writers dirtying pages that can't be synced.
2830 */
Keith Buschf33447b2017-02-10 18:15:51 -05002831 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2832 continue;
2833 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002834 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002835
Ming Lei443bd902017-06-19 10:21:08 +08002836 /* Forcibly unquiesce queues to avoid blocking dispatch */
2837 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002838 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002839 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002840}
Linus Torvalds237045f2016-03-18 17:13:31 -07002841EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002842
Keith Busch302ad8c2017-03-01 14:22:12 -05002843void nvme_unfreeze(struct nvme_ctrl *ctrl)
2844{
2845 struct nvme_ns *ns;
2846
2847 mutex_lock(&ctrl->namespaces_mutex);
2848 list_for_each_entry(ns, &ctrl->namespaces, list)
2849 blk_mq_unfreeze_queue(ns->queue);
2850 mutex_unlock(&ctrl->namespaces_mutex);
2851}
2852EXPORT_SYMBOL_GPL(nvme_unfreeze);
2853
2854void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2855{
2856 struct nvme_ns *ns;
2857
2858 mutex_lock(&ctrl->namespaces_mutex);
2859 list_for_each_entry(ns, &ctrl->namespaces, list) {
2860 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2861 if (timeout <= 0)
2862 break;
2863 }
2864 mutex_unlock(&ctrl->namespaces_mutex);
2865}
2866EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2867
2868void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2869{
2870 struct nvme_ns *ns;
2871
2872 mutex_lock(&ctrl->namespaces_mutex);
2873 list_for_each_entry(ns, &ctrl->namespaces, list)
2874 blk_mq_freeze_queue_wait(ns->queue);
2875 mutex_unlock(&ctrl->namespaces_mutex);
2876}
2877EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2878
2879void nvme_start_freeze(struct nvme_ctrl *ctrl)
2880{
2881 struct nvme_ns *ns;
2882
2883 mutex_lock(&ctrl->namespaces_mutex);
2884 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002885 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002886 mutex_unlock(&ctrl->namespaces_mutex);
2887}
2888EXPORT_SYMBOL_GPL(nvme_start_freeze);
2889
Keith Busch25646262016-01-04 09:10:57 -07002890void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002891{
2892 struct nvme_ns *ns;
2893
Keith Busch32f0c4a2016-07-13 11:45:02 -06002894 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002895 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002896 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002897 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002898}
Ming Lin576d55d2016-02-10 10:03:32 -08002899EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002900
Keith Busch25646262016-01-04 09:10:57 -07002901void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002902{
2903 struct nvme_ns *ns;
2904
Keith Busch32f0c4a2016-07-13 11:45:02 -06002905 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002906 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002907 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002908 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002909}
Ming Lin576d55d2016-02-10 10:03:32 -08002910EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002911
Sagi Grimberg31b84462017-10-11 12:53:07 +03002912int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2913{
2914 if (!ctrl->ops->reinit_request)
2915 return 0;
2916
2917 return blk_mq_tagset_iter(set, set->driver_data,
2918 ctrl->ops->reinit_request);
2919}
2920EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
2921
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002922int __init nvme_core_init(void)
2923{
2924 int result;
2925
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002926 nvme_wq = alloc_workqueue("nvme-wq",
2927 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2928 if (!nvme_wq)
2929 return -ENOMEM;
2930
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002931 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2932 &nvme_dev_fops);
2933 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002934 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002935 else if (result > 0)
2936 nvme_char_major = result;
2937
2938 nvme_class = class_create(THIS_MODULE, "nvme");
2939 if (IS_ERR(nvme_class)) {
2940 result = PTR_ERR(nvme_class);
2941 goto unregister_chrdev;
2942 }
2943
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002944 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002945
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002946unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002947 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002948destroy_wq:
2949 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002950 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002951}
2952
2953void nvme_core_exit(void)
2954{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002955 class_destroy(nvme_class);
2956 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002957 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002958}
Ming Lin576d55d2016-02-10 10:03:32 -08002959
2960MODULE_LICENSE("GPL");
2961MODULE_VERSION("1.0");
2962module_init(nvme_core_init);
2963module_exit(nvme_core_exit);