blob: 28d58d353a420a71c8dc73924e1ef67a0c9a2b61 [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
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080055static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080056module_param(default_ps_max_latency_us, ulong, 0644);
57MODULE_PARM_DESC(default_ps_max_latency_us,
58 "max power saving latency for new devices; use PM QOS to change per device");
59
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070060static bool force_apst;
61module_param(force_apst, bool, 0644);
62MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
63
Jens Axboef5d11842017-06-27 12:03:06 -060064static bool streams;
65module_param(streams, bool, 0644);
66MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwig9843f682017-10-18 13:10:01 +020071static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020072static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010073static struct class *nvme_class;
74
Arnav Dawnb6dccf72017-07-12 16:10:40 +053075static __le32 nvme_get_log_dw10(u8 lid, size_t size)
76{
77 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
78}
79
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020080int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
81{
82 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
83 return -EBUSY;
84 if (!queue_work(nvme_wq, &ctrl->reset_work))
85 return -EBUSY;
86 return 0;
87}
88EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
89
90static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
91{
92 int ret;
93
94 ret = nvme_reset_ctrl(ctrl);
95 if (!ret)
96 flush_work(&ctrl->reset_work);
97 return ret;
98}
99
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200100static void nvme_delete_ctrl_work(struct work_struct *work)
101{
102 struct nvme_ctrl *ctrl =
103 container_of(work, struct nvme_ctrl, delete_work);
104
Sagi Grimberg40546372017-10-29 14:21:02 +0200105 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200106 nvme_stop_ctrl(ctrl);
107 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200108 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200109 nvme_uninit_ctrl(ctrl);
110 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200111}
112
113int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
114{
115 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
116 return -EBUSY;
117 if (!queue_work(nvme_wq, &ctrl->delete_work))
118 return -EBUSY;
119 return 0;
120}
121EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
122
123int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
124{
125 int ret = 0;
126
127 /*
128 * Keep a reference until the work is flushed since ->delete_ctrl
129 * can free the controller.
130 */
131 nvme_get_ctrl(ctrl);
132 ret = nvme_delete_ctrl(ctrl);
133 if (!ret)
134 flush_work(&ctrl->delete_work);
135 nvme_put_ctrl(ctrl);
136 return ret;
137}
138EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
139
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200140static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200141{
142 switch (nvme_req(req)->status & 0x7ff) {
143 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200144 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200145 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200146 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200147 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200148 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200149 case NVME_SC_WRITE_FAULT:
150 case NVME_SC_READ_ERROR:
151 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200152 case NVME_SC_ACCESS_DENIED:
153 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200154 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200155 case NVME_SC_GUARD_CHECK:
156 case NVME_SC_APPTAG_CHECK:
157 case NVME_SC_REFTAG_CHECK:
158 case NVME_SC_INVALID_PI:
159 return BLK_STS_PROTECTION;
160 case NVME_SC_RESERVATION_CONFLICT:
161 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200162 default:
163 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200164 }
165}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200166
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200167static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200168{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200169 if (blk_noretry_request(req))
170 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200171 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200172 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200173 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200174 return false;
Christoph Hellwige54b0642017-11-02 21:28:51 +0300175 if (blk_queue_dying(req->q))
176 return false;
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200177 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200178}
179
180void nvme_complete_rq(struct request *req)
181{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200182 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
183 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300184 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200185 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200186 }
187
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200188 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200189}
190EXPORT_SYMBOL_GPL(nvme_complete_rq);
191
Ming Linc55a2fd2016-05-18 14:05:02 -0700192void nvme_cancel_request(struct request *req, void *data, bool reserved)
193{
Ming Linc55a2fd2016-05-18 14:05:02 -0700194 if (!blk_mq_request_started(req))
195 return;
196
197 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
198 "Cancelling I/O %d", req->tag);
199
Christoph Hellwige54b0642017-11-02 21:28:51 +0300200 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200201 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200202
Ming Linc55a2fd2016-05-18 14:05:02 -0700203}
204EXPORT_SYMBOL_GPL(nvme_cancel_request);
205
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200206bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
207 enum nvme_ctrl_state new_state)
208{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300209 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200210 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200211 bool changed = false;
212
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200213 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300214
215 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200216 switch (new_state) {
217 case NVME_CTRL_LIVE:
218 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200219 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200220 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900221 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200222 changed = true;
223 /* FALLTHRU */
224 default:
225 break;
226 }
227 break;
228 case NVME_CTRL_RESETTING:
229 switch (old_state) {
230 case NVME_CTRL_NEW:
231 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900232 changed = true;
233 /* FALLTHRU */
234 default:
235 break;
236 }
237 break;
238 case NVME_CTRL_RECONNECTING:
239 switch (old_state) {
240 case NVME_CTRL_LIVE:
James Smart3cec7f92017-10-25 16:43:13 -0700241 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200242 changed = true;
243 /* FALLTHRU */
244 default:
245 break;
246 }
247 break;
248 case NVME_CTRL_DELETING:
249 switch (old_state) {
250 case NVME_CTRL_LIVE:
251 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900252 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200253 changed = true;
254 /* FALLTHRU */
255 default:
256 break;
257 }
258 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600259 case NVME_CTRL_DEAD:
260 switch (old_state) {
261 case NVME_CTRL_DELETING:
262 changed = true;
263 /* FALLTHRU */
264 default:
265 break;
266 }
267 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200268 default:
269 break;
270 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200271
272 if (changed)
273 ctrl->state = new_state;
274
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200275 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300276
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200277 return changed;
278}
279EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
280
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100281static void nvme_free_ns(struct kref *kref)
282{
283 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
284
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200285 if (ns->ndev)
286 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100287
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100288 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700289 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
290 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100291 kfree(ns);
292}
293
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100294static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100295{
296 kref_put(&ns->kref, nvme_free_ns);
297}
298
Christoph Hellwig41609822015-11-20 09:00:02 +0100299struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200300 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100301{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100302 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100303 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100304
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200305 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100306 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200307 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100308 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200309 qid ? qid - 1 : 0);
310 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100312 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100313
Christoph Hellwig21d34712015-11-26 09:08:36 +0100314 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800315 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100316
Christoph Hellwig41609822015-11-20 09:00:02 +0100317 return req;
318}
Ming Lin576d55d2016-02-10 10:03:32 -0800319EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100320
Jens Axboef5d11842017-06-27 12:03:06 -0600321static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
322{
323 struct nvme_command c;
324
325 memset(&c, 0, sizeof(c));
326
327 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530328 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600329 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
330 c.directive.dtype = NVME_DIR_IDENTIFY;
331 c.directive.tdtype = NVME_DIR_STREAMS;
332 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
333
334 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
335}
336
337static int nvme_disable_streams(struct nvme_ctrl *ctrl)
338{
339 return nvme_toggle_streams(ctrl, false);
340}
341
342static int nvme_enable_streams(struct nvme_ctrl *ctrl)
343{
344 return nvme_toggle_streams(ctrl, true);
345}
346
347static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
348 struct streams_directive_params *s, u32 nsid)
349{
350 struct nvme_command c;
351
352 memset(&c, 0, sizeof(c));
353 memset(s, 0, sizeof(*s));
354
355 c.directive.opcode = nvme_admin_directive_recv;
356 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700357 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600358 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
359 c.directive.dtype = NVME_DIR_STREAMS;
360
361 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
362}
363
364static int nvme_configure_directives(struct nvme_ctrl *ctrl)
365{
366 struct streams_directive_params s;
367 int ret;
368
369 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
370 return 0;
371 if (!streams)
372 return 0;
373
374 ret = nvme_enable_streams(ctrl);
375 if (ret)
376 return ret;
377
Arnav Dawn62346ea2017-07-12 16:11:53 +0530378 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600379 if (ret)
380 return ret;
381
382 ctrl->nssa = le16_to_cpu(s.nssa);
383 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
384 dev_info(ctrl->device, "too few streams (%u) available\n",
385 ctrl->nssa);
386 nvme_disable_streams(ctrl);
387 return 0;
388 }
389
390 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
391 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
392 return 0;
393}
394
395/*
396 * Check if 'req' has a write hint associated with it. If it does, assign
397 * a valid namespace stream to the write.
398 */
399static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
400 struct request *req, u16 *control,
401 u32 *dsmgmt)
402{
403 enum rw_hint streamid = req->write_hint;
404
405 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
406 streamid = 0;
407 else {
408 streamid--;
409 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
410 return;
411
412 *control |= NVME_RW_DTYPE_STREAMS;
413 *dsmgmt |= streamid << 16;
414 }
415
416 if (streamid < ARRAY_SIZE(req->q->write_hints))
417 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
418}
419
Ming Lin8093f7c2016-04-12 13:10:14 -0600420static inline void nvme_setup_flush(struct nvme_ns *ns,
421 struct nvme_command *cmnd)
422{
423 memset(cmnd, 0, sizeof(*cmnd));
424 cmnd->common.opcode = nvme_cmd_flush;
425 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
426}
427
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200428static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600429 struct nvme_command *cmnd)
430{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100431 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600432 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100433 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600434
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100435 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600436 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200437 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600438
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100439 __rq_for_each_bio(bio, req) {
440 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
441 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
442
443 range[n].cattr = cpu_to_le32(0);
444 range[n].nlb = cpu_to_le32(nlb);
445 range[n].slba = cpu_to_le64(slba);
446 n++;
447 }
448
449 if (WARN_ON_ONCE(n != segments)) {
450 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200451 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100452 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600453
454 memset(cmnd, 0, sizeof(*cmnd));
455 cmnd->dsm.opcode = nvme_cmd_dsm;
456 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200457 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600458 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
459
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700460 req->special_vec.bv_page = virt_to_page(range);
461 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100462 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700463 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600464
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200465 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600466}
Ming Lin8093f7c2016-04-12 13:10:14 -0600467
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200468static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
469 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600470{
Jens Axboef5d11842017-06-27 12:03:06 -0600471 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600472 u16 control = 0;
473 u32 dsmgmt = 0;
474
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200475 /*
476 * If formated with metadata, require the block layer provide a buffer
477 * unless this namespace is formated such that the metadata can be
478 * stripped/generated by the controller with PRACT=1.
479 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300480 if (ns && ns->ms &&
481 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200482 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
483 return BLK_STS_NOTSUPP;
484
Ming Lin8093f7c2016-04-12 13:10:14 -0600485 if (req->cmd_flags & REQ_FUA)
486 control |= NVME_RW_FUA;
487 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
488 control |= NVME_RW_LR;
489
490 if (req->cmd_flags & REQ_RAHEAD)
491 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
492
493 memset(cmnd, 0, sizeof(*cmnd));
494 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600495 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
496 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
497 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
498
Jens Axboef5d11842017-06-27 12:03:06 -0600499 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
500 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
501
Ming Lin8093f7c2016-04-12 13:10:14 -0600502 if (ns->ms) {
503 switch (ns->pi_type) {
504 case NVME_NS_DPS_PI_TYPE3:
505 control |= NVME_RW_PRINFO_PRCHK_GUARD;
506 break;
507 case NVME_NS_DPS_PI_TYPE1:
508 case NVME_NS_DPS_PI_TYPE2:
509 control |= NVME_RW_PRINFO_PRCHK_GUARD |
510 NVME_RW_PRINFO_PRCHK_REF;
511 cmnd->rw.reftag = cpu_to_le32(
512 nvme_block_nr(ns, blk_rq_pos(req)));
513 break;
514 }
515 if (!blk_integrity_rq(req))
516 control |= NVME_RW_PRINFO_PRACT;
517 }
518
519 cmnd->rw.control = cpu_to_le16(control);
520 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200521 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600522}
523
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200524blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600525 struct nvme_command *cmd)
526{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200527 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600528
Christoph Hellwig987f6992017-04-05 19:18:08 +0200529 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200530 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200531 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200532 req->rq_flags |= RQF_DONTPREP;
533 }
534
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100535 switch (req_op(req)) {
536 case REQ_OP_DRV_IN:
537 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800538 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100539 break;
540 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600541 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200543 case REQ_OP_WRITE_ZEROES:
544 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100545 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600546 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100547 break;
548 case REQ_OP_READ:
549 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200550 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100551 break;
552 default:
553 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200554 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100555 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600556
James Smart721b3912016-10-21 23:33:34 +0300557 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600558 return ret;
559}
560EXPORT_SYMBOL_GPL(nvme_setup_cmd);
561
Christoph Hellwig41609822015-11-20 09:00:02 +0100562/*
563 * Returns 0 on success. If the result is negative, it's a Linux error code;
564 * if the result is positive, it's an NVM Express status code
565 */
566int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800567 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200568 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100569{
570 struct request *req;
571 int ret;
572
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200573 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100574 if (IS_ERR(req))
575 return PTR_ERR(req);
576
577 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
578
Christoph Hellwig21d34712015-11-26 09:08:36 +0100579 if (buffer && bufflen) {
580 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
581 if (ret)
582 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100583 }
584
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200585 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800586 if (result)
587 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200588 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
589 ret = -EINTR;
590 else
591 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100592 out:
593 blk_mq_free_request(req);
594 return ret;
595}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200596EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100597
598int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
599 void *buffer, unsigned bufflen)
600{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200601 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
602 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100603}
Ming Lin576d55d2016-02-10 10:03:32 -0800604EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100605
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400606static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
607 unsigned len, u32 seed, bool write)
608{
609 struct bio_integrity_payload *bip;
610 int ret = -ENOMEM;
611 void *buf;
612
613 buf = kmalloc(len, GFP_KERNEL);
614 if (!buf)
615 goto out;
616
617 ret = -EFAULT;
618 if (write && copy_from_user(buf, ubuf, len))
619 goto out_free_meta;
620
621 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
622 if (IS_ERR(bip)) {
623 ret = PTR_ERR(bip);
624 goto out_free_meta;
625 }
626
627 bip->bip_iter.bi_size = len;
628 bip->bip_iter.bi_sector = seed;
629 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
630 offset_in_page(buf));
631 if (ret == len)
632 return buf;
633 ret = -ENOMEM;
634out_free_meta:
635 kfree(buf);
636out:
637 return ERR_PTR(ret);
638}
639
Keith Busch63263d62017-08-29 17:46:04 -0400640static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400641 struct nvme_command *cmd, void __user *ubuffer,
642 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
643 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100644{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200645 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600646 struct nvme_ns *ns = q->queuedata;
647 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100648 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600649 struct bio *bio = NULL;
650 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100651 int ret;
652
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200653 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100654 if (IS_ERR(req))
655 return PTR_ERR(req);
656
657 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
658
659 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100660 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
661 GFP_KERNEL);
662 if (ret)
663 goto out;
664 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200665 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400666 if (disk && meta_buffer && meta_len) {
667 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
668 meta_seed, write);
669 if (IS_ERR(meta)) {
670 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600671 goto out_unmap;
672 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600673 }
674 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400675
Keith Busch0b7f1f22015-10-23 09:47:28 -0600676 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200677 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
678 ret = -EINTR;
679 else
680 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100681 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800682 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600683 if (meta && !ret && !write) {
684 if (copy_to_user(meta_buffer, meta, meta_len))
685 ret = -EFAULT;
686 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600687 kfree(meta);
688 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200689 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600690 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100691 out:
692 blk_mq_free_request(req);
693 return ret;
694}
695
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200696static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200697{
698 struct nvme_ctrl *ctrl = rq->end_io_data;
699
700 blk_mq_free_request(rq);
701
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200702 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200703 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200704 "failed nvme_keep_alive_end_io error=%d\n",
705 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200706 return;
707 }
708
709 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
710}
711
712static int nvme_keep_alive(struct nvme_ctrl *ctrl)
713{
714 struct nvme_command c;
715 struct request *rq;
716
717 memset(&c, 0, sizeof(c));
718 c.common.opcode = nvme_admin_keep_alive;
719
720 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
721 NVME_QID_ANY);
722 if (IS_ERR(rq))
723 return PTR_ERR(rq);
724
725 rq->timeout = ctrl->kato * HZ;
726 rq->end_io_data = ctrl;
727
728 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
729
730 return 0;
731}
732
733static void nvme_keep_alive_work(struct work_struct *work)
734{
735 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
736 struct nvme_ctrl, ka_work);
737
738 if (nvme_keep_alive(ctrl)) {
739 /* allocation failure, reset the controller */
740 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200741 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200742 return;
743 }
744}
745
746void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
747{
748 if (unlikely(ctrl->kato == 0))
749 return;
750
751 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
752 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
753}
754EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
755
756void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
757{
758 if (unlikely(ctrl->kato == 0))
759 return;
760
761 cancel_delayed_work_sync(&ctrl->ka_work);
762}
763EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
764
Keith Busch3f7f25a92017-06-20 15:09:56 -0400765static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100766{
767 struct nvme_command c = { };
768 int error;
769
770 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
771 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200772 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100773
774 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
775 if (!*id)
776 return -ENOMEM;
777
778 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
779 sizeof(struct nvme_id_ctrl));
780 if (error)
781 kfree(*id);
782 return error;
783}
784
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200785static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
786 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200787{
788 struct nvme_command c = { };
789 int status;
790 void *data;
791 int pos;
792 int len;
793
794 c.identify.opcode = nvme_admin_identify;
795 c.identify.nsid = cpu_to_le32(nsid);
796 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
797
798 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
799 if (!data)
800 return -ENOMEM;
801
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200802 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200803 NVME_IDENTIFY_DATA_SIZE);
804 if (status)
805 goto free_data;
806
807 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
808 struct nvme_ns_id_desc *cur = data + pos;
809
810 if (cur->nidl == 0)
811 break;
812
813 switch (cur->nidt) {
814 case NVME_NIDT_EUI64:
815 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200816 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200817 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
818 cur->nidl);
819 goto free_data;
820 }
821 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200822 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200823 break;
824 case NVME_NIDT_NGUID:
825 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200826 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200827 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
828 cur->nidl);
829 goto free_data;
830 }
831 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200832 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200833 break;
834 case NVME_NIDT_UUID:
835 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200836 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200837 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
838 cur->nidl);
839 goto free_data;
840 }
841 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200842 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200843 break;
844 default:
845 /* Skip unnkown types */
846 len = cur->nidl;
847 break;
848 }
849
850 len += sizeof(*cur);
851 }
852free_data:
853 kfree(data);
854 return status;
855}
856
Keith Busch540c8012015-10-22 15:45:06 -0600857static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
858{
859 struct nvme_command c = { };
860
861 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200862 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600863 c.identify.nsid = cpu_to_le32(nsid);
864 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
865}
866
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200867static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
868 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100869{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200870 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100871 struct nvme_command c = { };
872 int error;
873
874 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200875 c.identify.opcode = nvme_admin_identify;
876 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200877 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100878
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200879 id = kmalloc(sizeof(*id), GFP_KERNEL);
880 if (!id)
881 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100882
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200883 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
884 if (error) {
885 dev_warn(ctrl->device, "Identify namespace failed\n");
886 kfree(id);
887 return NULL;
888 }
889
890 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100891}
892
Keith Busch3f7f25a92017-06-20 15:09:56 -0400893static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700894 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100895{
896 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800897 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100898 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100899
900 memset(&c, 0, sizeof(c));
901 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100902 c.features.fid = cpu_to_le32(fid);
903 c.features.dword11 = cpu_to_le32(dword11);
904
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800905 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700906 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700907 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800908 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100909 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100910}
911
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100912int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
913{
914 u32 q_count = (*count - 1) | ((*count - 1) << 16);
915 u32 result;
916 int status, nr_io_queues;
917
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700918 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100919 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200920 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100921 return status;
922
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200923 /*
924 * Degraded controllers might return an error when setting the queue
925 * count. We still want to be able to bring them online and offer
926 * access to the admin queue, as that might be only way to fix them up.
927 */
928 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200929 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200930 *count = 0;
931 } else {
932 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
933 *count = min(*count, nr_io_queues);
934 }
935
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100936 return 0;
937}
Ming Lin576d55d2016-02-10 10:03:32 -0800938EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100939
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100940static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
941{
942 struct nvme_user_io io;
943 struct nvme_command c;
944 unsigned length, meta_len;
945 void __user *metadata;
946
947 if (copy_from_user(&io, uio, sizeof(io)))
948 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700949 if (io.flags)
950 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100951
952 switch (io.opcode) {
953 case nvme_cmd_write:
954 case nvme_cmd_read:
955 case nvme_cmd_compare:
956 break;
957 default:
958 return -EINVAL;
959 }
960
961 length = (io.nblocks + 1) << ns->lba_shift;
962 meta_len = (io.nblocks + 1) * ns->ms;
963 metadata = (void __user *)(uintptr_t)io.metadata;
964
965 if (ns->ext) {
966 length += meta_len;
967 meta_len = 0;
968 } else if (meta_len) {
969 if ((io.metadata & 3) || !io.metadata)
970 return -EINVAL;
971 }
972
973 memset(&c, 0, sizeof(c));
974 c.rw.opcode = io.opcode;
975 c.rw.flags = io.flags;
976 c.rw.nsid = cpu_to_le32(ns->ns_id);
977 c.rw.slba = cpu_to_le64(io.slba);
978 c.rw.length = cpu_to_le16(io.nblocks);
979 c.rw.control = cpu_to_le16(io.control);
980 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
981 c.rw.reftag = cpu_to_le32(io.reftag);
982 c.rw.apptag = cpu_to_le16(io.apptag);
983 c.rw.appmask = cpu_to_le16(io.appmask);
984
Keith Busch63263d62017-08-29 17:46:04 -0400985 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100986 (void __user *)(uintptr_t)io.addr, length,
987 metadata, meta_len, io.slba, NULL, 0);
988}
989
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100990static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100991 struct nvme_passthru_cmd __user *ucmd)
992{
993 struct nvme_passthru_cmd cmd;
994 struct nvme_command c;
995 unsigned timeout = 0;
996 int status;
997
998 if (!capable(CAP_SYS_ADMIN))
999 return -EACCES;
1000 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1001 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001002 if (cmd.flags)
1003 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001004
1005 memset(&c, 0, sizeof(c));
1006 c.common.opcode = cmd.opcode;
1007 c.common.flags = cmd.flags;
1008 c.common.nsid = cpu_to_le32(cmd.nsid);
1009 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1010 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1011 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1012 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1013 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1014 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1015 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1016 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1017
1018 if (cmd.timeout_ms)
1019 timeout = msecs_to_jiffies(cmd.timeout_ms);
1020
1021 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001022 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001023 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1024 0, &cmd.result, timeout);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001025 if (status >= 0) {
1026 if (put_user(cmd.result, &ucmd->result))
1027 return -EFAULT;
1028 }
1029
1030 return status;
1031}
1032
1033static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1034 unsigned int cmd, unsigned long arg)
1035{
1036 struct nvme_ns *ns = bdev->bd_disk->private_data;
1037
1038 switch (cmd) {
1039 case NVME_IOCTL_ID:
1040 force_successful_syscall_return();
1041 return ns->ns_id;
1042 case NVME_IOCTL_ADMIN_CMD:
1043 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1044 case NVME_IOCTL_IO_CMD:
1045 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1046 case NVME_IOCTL_SUBMIT_IO:
1047 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001048 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001049#ifdef CONFIG_NVM
1050 if (ns->ndev)
1051 return nvme_nvm_ioctl(ns, cmd, arg);
1052#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001053 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001054 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001055 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001056 return -ENOTTY;
1057 }
1058}
1059
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001060static int nvme_open(struct block_device *bdev, fmode_t mode)
1061{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001062 struct nvme_ns *ns = bdev->bd_disk->private_data;
1063
1064 if (!kref_get_unless_zero(&ns->kref))
1065 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001066 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001067}
1068
1069static void nvme_release(struct gendisk *disk, fmode_t mode)
1070{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001071 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001072}
1073
1074static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1075{
1076 /* some standard values */
1077 geo->heads = 1 << 6;
1078 geo->sectors = 1 << 5;
1079 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1080 return 0;
1081}
1082
1083#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001084static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001085{
1086 struct blk_integrity integrity;
1087
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001088 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001089 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001090 case NVME_NS_DPS_PI_TYPE3:
1091 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001092 integrity.tag_size = sizeof(u16) + sizeof(u32);
1093 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001094 break;
1095 case NVME_NS_DPS_PI_TYPE1:
1096 case NVME_NS_DPS_PI_TYPE2:
1097 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001098 integrity.tag_size = sizeof(u16);
1099 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001100 break;
1101 default:
1102 integrity.profile = NULL;
1103 break;
1104 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001105 integrity.tuple_size = ms;
1106 blk_integrity_register(disk, &integrity);
1107 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001108}
1109#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001110static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111{
1112}
1113#endif /* CONFIG_BLK_DEV_INTEGRITY */
1114
Scott Bauer6b8190d2017-06-15 10:44:30 -06001115static void nvme_set_chunk_size(struct nvme_ns *ns)
1116{
1117 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1118 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1119}
1120
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001121static void nvme_config_discard(struct nvme_ctrl *ctrl,
1122 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001123{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001124 u32 size = queue_logical_block_size(queue);
1125
1126 if (stream_alignment)
1127 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001128
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001129 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1130 NVME_DSM_MAX_RANGES);
1131
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001132 queue->limits.discard_alignment = size;
1133 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001134
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001135 blk_queue_max_discard_sectors(queue, UINT_MAX);
1136 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1137 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001138
1139 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001140 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001141}
1142
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001143static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1144 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001145{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001146 if (ctrl->vs >= NVME_VS(1, 1, 0))
1147 memcpy(eui64, id->eui64, sizeof(id->eui64));
1148 if (ctrl->vs >= NVME_VS(1, 2, 0))
1149 memcpy(nguid, id->nguid, sizeof(id->nguid));
1150 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001151 /* Don't treat error as fatal we potentially
1152 * already have a NGUID or EUI-64
1153 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001154 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1155 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001156 "%s: Identify Descriptors failed\n", __func__);
1157 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001158}
1159
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001160static void nvme_update_disk_info(struct gendisk *disk,
1161 struct nvme_ns *ns, struct nvme_id_ns *id)
1162{
1163 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1164 unsigned stream_alignment = 0;
1165
1166 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1167 stream_alignment = ns->sws * ns->sgs;
1168
1169 blk_mq_freeze_queue(disk->queue);
1170 blk_integrity_unregister(disk);
1171
1172 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1173 if (ns->ms && !ns->ext &&
1174 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1175 nvme_init_integrity(disk, ns->ms, ns->pi_type);
1176 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1177 capacity = 0;
1178 set_capacity(disk, capacity);
1179
1180 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1181 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1182 blk_mq_unfreeze_queue(disk->queue);
1183}
1184
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001185static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1186{
1187 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001188
1189 /*
1190 * If identify namespace failed, use default 512 byte block size so
1191 * block layer can use before failing read/write for 0 capacity.
1192 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001193 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001194 if (ns->lba_shift == 0)
1195 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001196 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001197 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1198 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1199 /* the PI implementation requires metadata equal t10 pi tuple size */
1200 if (ns->ms == sizeof(struct t10_pi_tuple))
1201 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1202 else
1203 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001204
Christoph Hellwig6e78f212017-11-02 21:28:55 +03001205 if (ns->noiob)
1206 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001207 nvme_update_disk_info(disk, ns, id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001208}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001209
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001210static int nvme_revalidate_disk(struct gendisk *disk)
1211{
1212 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001213 struct nvme_ctrl *ctrl = ns->ctrl;
1214 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001215 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1216 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001217 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001218
1219 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1220 set_capacity(disk, 0);
1221 return -ENODEV;
1222 }
1223
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001224 id = nvme_identify_ns(ctrl, ns->ns_id);
1225 if (!id)
1226 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001227
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001228 if (id->ncap == 0) {
1229 ret = -ENODEV;
1230 goto out;
1231 }
1232
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001233 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1234 if (!uuid_equal(&ns->uuid, &uuid) ||
1235 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1236 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1237 dev_err(ctrl->device,
1238 "identifiers changed for nsid %d\n", ns->ns_id);
1239 ret = -ENODEV;
1240 }
1241
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001242out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001243 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001244 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001245}
1246
1247static char nvme_pr_type(enum pr_type type)
1248{
1249 switch (type) {
1250 case PR_WRITE_EXCLUSIVE:
1251 return 1;
1252 case PR_EXCLUSIVE_ACCESS:
1253 return 2;
1254 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1255 return 3;
1256 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1257 return 4;
1258 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1259 return 5;
1260 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1261 return 6;
1262 default:
1263 return 0;
1264 }
1265};
1266
1267static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1268 u64 key, u64 sa_key, u8 op)
1269{
1270 struct nvme_ns *ns = bdev->bd_disk->private_data;
1271 struct nvme_command c;
1272 u8 data[16] = { 0, };
1273
1274 put_unaligned_le64(key, &data[0]);
1275 put_unaligned_le64(sa_key, &data[8]);
1276
1277 memset(&c, 0, sizeof(c));
1278 c.common.opcode = op;
1279 c.common.nsid = cpu_to_le32(ns->ns_id);
1280 c.common.cdw10[0] = cpu_to_le32(cdw10);
1281
1282 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1283}
1284
1285static int nvme_pr_register(struct block_device *bdev, u64 old,
1286 u64 new, unsigned flags)
1287{
1288 u32 cdw10;
1289
1290 if (flags & ~PR_FL_IGNORE_KEY)
1291 return -EOPNOTSUPP;
1292
1293 cdw10 = old ? 2 : 0;
1294 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1295 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1296 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1297}
1298
1299static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1300 enum pr_type type, unsigned flags)
1301{
1302 u32 cdw10;
1303
1304 if (flags & ~PR_FL_IGNORE_KEY)
1305 return -EOPNOTSUPP;
1306
1307 cdw10 = nvme_pr_type(type) << 8;
1308 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1309 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1310}
1311
1312static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1313 enum pr_type type, bool abort)
1314{
1315 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1316 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1317}
1318
1319static int nvme_pr_clear(struct block_device *bdev, u64 key)
1320{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001321 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001322 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1323}
1324
1325static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1326{
1327 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1328 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1329}
1330
1331static const struct pr_ops nvme_pr_ops = {
1332 .pr_register = nvme_pr_register,
1333 .pr_reserve = nvme_pr_reserve,
1334 .pr_release = nvme_pr_release,
1335 .pr_preempt = nvme_pr_preempt,
1336 .pr_clear = nvme_pr_clear,
1337};
1338
Scott Bauera98e58e52017-02-03 12:50:32 -07001339#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001340int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1341 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001342{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001343 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001344 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001345
1346 memset(&cmd, 0, sizeof(cmd));
1347 if (send)
1348 cmd.common.opcode = nvme_admin_security_send;
1349 else
1350 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001351 cmd.common.nsid = 0;
1352 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1353 cmd.common.cdw10[1] = cpu_to_le32(len);
1354
1355 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1356 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1357}
1358EXPORT_SYMBOL_GPL(nvme_sec_submit);
1359#endif /* CONFIG_BLK_SED_OPAL */
1360
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001361static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001362 .owner = THIS_MODULE,
1363 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001364 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001365 .open = nvme_open,
1366 .release = nvme_release,
1367 .getgeo = nvme_getgeo,
1368 .revalidate_disk= nvme_revalidate_disk,
1369 .pr_ops = &nvme_pr_ops,
1370};
1371
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001372static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1373{
1374 unsigned long timeout =
1375 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1376 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1377 int ret;
1378
1379 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001380 if (csts == ~0)
1381 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001382 if ((csts & NVME_CSTS_RDY) == bit)
1383 break;
1384
1385 msleep(100);
1386 if (fatal_signal_pending(current))
1387 return -EINTR;
1388 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001389 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001390 "Device not ready; aborting %s\n", enabled ?
1391 "initialisation" : "reset");
1392 return -ENODEV;
1393 }
1394 }
1395
1396 return ret;
1397}
1398
1399/*
1400 * If the device has been passed off to us in an enabled state, just clear
1401 * the enabled bit. The spec says we should set the 'shutdown notification
1402 * bits', but doing so may cause the device to complete commands to the
1403 * admin queue ... and we don't know what memory that might be pointing at!
1404 */
1405int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1406{
1407 int ret;
1408
1409 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1410 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1411
1412 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1413 if (ret)
1414 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001415
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001416 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001417 msleep(NVME_QUIRK_DELAY_AMOUNT);
1418
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001419 return nvme_wait_ready(ctrl, cap, false);
1420}
Ming Lin576d55d2016-02-10 10:03:32 -08001421EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001422
1423int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1424{
1425 /*
1426 * Default to a 4K page size, with the intention to update this
1427 * path in the future to accomodate architectures with differing
1428 * kernel and IO page sizes.
1429 */
1430 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1431 int ret;
1432
1433 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001434 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001435 "Minimum device page size %u too large for host (%u)\n",
1436 1 << dev_page_min, 1 << page_shift);
1437 return -ENODEV;
1438 }
1439
1440 ctrl->page_size = 1 << page_shift;
1441
1442 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1443 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001444 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001445 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1446 ctrl->ctrl_config |= NVME_CC_ENABLE;
1447
1448 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1449 if (ret)
1450 return ret;
1451 return nvme_wait_ready(ctrl, cap, true);
1452}
Ming Lin576d55d2016-02-10 10:03:32 -08001453EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001454
1455int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1456{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001457 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001458 u32 csts;
1459 int ret;
1460
1461 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1462 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1463
1464 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1465 if (ret)
1466 return ret;
1467
1468 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1469 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1470 break;
1471
1472 msleep(100);
1473 if (fatal_signal_pending(current))
1474 return -EINTR;
1475 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001476 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001477 "Device shutdown incomplete; abort shutdown\n");
1478 return -ENODEV;
1479 }
1480 }
1481
1482 return ret;
1483}
Ming Lin576d55d2016-02-10 10:03:32 -08001484EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001485
Christoph Hellwigda358252016-03-02 18:07:11 +01001486static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1487 struct request_queue *q)
1488{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001489 bool vwc = false;
1490
Christoph Hellwigda358252016-03-02 18:07:11 +01001491 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001492 u32 max_segments =
1493 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1494
Christoph Hellwigda358252016-03-02 18:07:11 +01001495 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001496 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001497 }
Keith Busche6282ae2016-12-19 11:37:50 -05001498 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1499 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001500 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001501 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1502 vwc = true;
1503 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001504}
1505
Jon Derrickdbf86b32017-08-16 09:51:29 +02001506static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1507{
1508 __le64 ts;
1509 int ret;
1510
1511 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1512 return 0;
1513
1514 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1515 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1516 NULL);
1517 if (ret)
1518 dev_warn_once(ctrl->device,
1519 "could not set timestamp (%d)\n", ret);
1520 return ret;
1521}
1522
Keith Busch634b8322017-08-10 11:23:31 +02001523static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001524{
1525 /*
1526 * APST (Autonomous Power State Transition) lets us program a
1527 * table of power state transitions that the controller will
1528 * perform automatically. We configure it with a simple
1529 * heuristic: we are willing to spend at most 2% of the time
1530 * transitioning between power states. Therefore, when running
1531 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001532 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001533 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001534 * the requested maximum latency.
1535 *
1536 * We will not autonomously enter any non-operational state for
1537 * which the total latency exceeds ps_max_latency_us. Users
1538 * can set ps_max_latency_us to zero to turn off APST.
1539 */
1540
1541 unsigned apste;
1542 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001543 u64 max_lat_us = 0;
1544 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001545 int ret;
1546
1547 /*
1548 * If APST isn't supported or if we haven't been initialized yet,
1549 * then don't do anything.
1550 */
1551 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001552 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001553
1554 if (ctrl->npss > 31) {
1555 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001556 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001557 }
1558
1559 table = kzalloc(sizeof(*table), GFP_KERNEL);
1560 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001561 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001562
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001563 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001564 /* Turn off APST. */
1565 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001566 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001567 } else {
1568 __le64 target = cpu_to_le64(0);
1569 int state;
1570
1571 /*
1572 * Walk through all states from lowest- to highest-power.
1573 * According to the spec, lower-numbered states use more
1574 * power. NPSS, despite the name, is the index of the
1575 * lowest-power state, not the number of states.
1576 */
1577 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001578 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001579
1580 if (target)
1581 table->entries[state] = target;
1582
1583 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001584 * Don't allow transitions to the deepest state
1585 * if it's quirked off.
1586 */
1587 if (state == ctrl->npss &&
1588 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1589 continue;
1590
1591 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001592 * Is this state a useful non-operational state for
1593 * higher-power states to autonomously transition to?
1594 */
1595 if (!(ctrl->psd[state].flags &
1596 NVME_PS_FLAGS_NON_OP_STATE))
1597 continue;
1598
Kai-Heng Fengda875912017-06-07 15:25:42 +08001599 exit_latency_us =
1600 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1601 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001602 continue;
1603
Kai-Heng Fengda875912017-06-07 15:25:42 +08001604 total_latency_us =
1605 exit_latency_us +
1606 le32_to_cpu(ctrl->psd[state].entry_lat);
1607
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001608 /*
1609 * This state is good. Use it as the APST idle
1610 * target for higher power states.
1611 */
1612 transition_ms = total_latency_us + 19;
1613 do_div(transition_ms, 20);
1614 if (transition_ms > (1 << 24) - 1)
1615 transition_ms = (1 << 24) - 1;
1616
1617 target = cpu_to_le64((state << 3) |
1618 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001619
1620 if (max_ps == -1)
1621 max_ps = state;
1622
1623 if (total_latency_us > max_lat_us)
1624 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001625 }
1626
1627 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001628
1629 if (max_ps == -1) {
1630 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1631 } else {
1632 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1633 max_ps, max_lat_us, (int)sizeof(*table), table);
1634 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001635 }
1636
1637 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1638 table, sizeof(*table), NULL);
1639 if (ret)
1640 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1641
1642 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001643 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001644}
1645
1646static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1647{
1648 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1649 u64 latency;
1650
1651 switch (val) {
1652 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1653 case PM_QOS_LATENCY_ANY:
1654 latency = U64_MAX;
1655 break;
1656
1657 default:
1658 latency = val;
1659 }
1660
1661 if (ctrl->ps_max_latency_us != latency) {
1662 ctrl->ps_max_latency_us = latency;
1663 nvme_configure_apst(ctrl);
1664 }
1665}
1666
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001667struct nvme_core_quirk_entry {
1668 /*
1669 * NVMe model and firmware strings are padded with spaces. For
1670 * simplicity, strings in the quirk table are padded with NULLs
1671 * instead.
1672 */
1673 u16 vid;
1674 const char *mn;
1675 const char *fr;
1676 unsigned long quirks;
1677};
1678
1679static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001680 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001681 /*
1682 * This Toshiba device seems to die using any APST states. See:
1683 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1684 */
1685 .vid = 0x1179,
1686 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001687 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001688 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001689};
1690
1691/* match is null-terminated but idstr is space-padded. */
1692static bool string_matches(const char *idstr, const char *match, size_t len)
1693{
1694 size_t matchlen;
1695
1696 if (!match)
1697 return true;
1698
1699 matchlen = strlen(match);
1700 WARN_ON_ONCE(matchlen > len);
1701
1702 if (memcmp(idstr, match, matchlen))
1703 return false;
1704
1705 for (; matchlen < len; matchlen++)
1706 if (idstr[matchlen] != ' ')
1707 return false;
1708
1709 return true;
1710}
1711
1712static bool quirk_matches(const struct nvme_id_ctrl *id,
1713 const struct nvme_core_quirk_entry *q)
1714{
1715 return q->vid == le16_to_cpu(id->vid) &&
1716 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1717 string_matches(id->fr, q->fr, sizeof(id->fr));
1718}
1719
Christoph Hellwig180de0072017-06-26 12:39:02 +02001720static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1721{
1722 size_t nqnlen;
1723 int off;
1724
1725 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1726 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1727 strcpy(ctrl->subnqn, id->subnqn);
1728 return;
1729 }
1730
1731 if (ctrl->vs >= NVME_VS(1, 2, 1))
1732 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1733
1734 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1735 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1736 "nqn.2014.08.org.nvmexpress:%4x%4x",
1737 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1738 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1739 off += sizeof(id->sn);
1740 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1741 off += sizeof(id->mn);
1742 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1743}
1744
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001745/*
1746 * Initialize the cached copies of the Identify data and various controller
1747 * register in our nvme_ctrl structure. This should be called as soon as
1748 * the admin queue is fully up and running.
1749 */
1750int nvme_init_identify(struct nvme_ctrl *ctrl)
1751{
1752 struct nvme_id_ctrl *id;
1753 u64 cap;
1754 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001755 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001756 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001757
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001758 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1759 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001760 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001761 return ret;
1762 }
1763
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001764 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1765 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001766 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001767 return ret;
1768 }
1769 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1770
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001771 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001772 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1773
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001774 ret = nvme_identify_ctrl(ctrl, &id);
1775 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001776 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001777 return -EIO;
1778 }
1779
Christoph Hellwig180de0072017-06-26 12:39:02 +02001780 nvme_init_subnqn(ctrl, id);
1781
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001782 if (!ctrl->identified) {
1783 /*
1784 * Check for quirks. Quirk can depend on firmware version,
1785 * so, in principle, the set of quirks present can change
1786 * across a reset. As a possible future enhancement, we
1787 * could re-scan for quirks every time we reinitialize
1788 * the device, but we'd have to make sure that the driver
1789 * behaves intelligently if the quirks change.
1790 */
1791
1792 int i;
1793
1794 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1795 if (quirk_matches(id, &core_quirks[i]))
1796 ctrl->quirks |= core_quirks[i].quirks;
1797 }
1798 }
1799
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001800 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001801 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 -07001802 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1803 }
1804
Scott Bauer8a9ae522017-02-17 13:59:40 +01001805 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001806 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001807 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001808 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001809 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001810 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001811 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1812 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1813 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1814 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001815 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001816 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001817 max_hw_sectors = UINT_MAX;
1818 ctrl->max_hw_sectors =
1819 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001820
Christoph Hellwigda358252016-03-02 18:07:11 +01001821 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001822 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001823 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001824
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001825 if (id->rtd3e) {
1826 /* us -> s */
1827 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1828
1829 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1830 shutdown_timeout, 60);
1831
1832 if (ctrl->shutdown_timeout != shutdown_timeout)
1833 dev_warn(ctrl->device,
1834 "Shutdown timeout set to %u seconds\n",
1835 ctrl->shutdown_timeout);
1836 } else
1837 ctrl->shutdown_timeout = shutdown_timeout;
1838
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001839 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001840 ctrl->apsta = id->apsta;
1841 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001842 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1843 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001844 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 -04001845 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001846 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001847 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001848 }
1849 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001850 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001851 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001852 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1853
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001854 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001855 ctrl->icdoff = le16_to_cpu(id->icdoff);
1856 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1857 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1858 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1859
1860 /*
1861 * In fabrics we need to verify the cntlid matches the
1862 * admin connect
1863 */
Keith Busch634b8322017-08-10 11:23:31 +02001864 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001865 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001866 goto out_free;
1867 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001868
1869 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001870 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001871 "keep-alive support is mandatory for fabrics\n");
1872 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001873 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001874 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001875 } else {
1876 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001877 ctrl->hmpre = le32_to_cpu(id->hmpre);
1878 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001879 ctrl->hmminds = le32_to_cpu(id->hmminds);
1880 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001881 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001882
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001883 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001884
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001885 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001886 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001887 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001888 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1889
Keith Busch634b8322017-08-10 11:23:31 +02001890 ret = nvme_configure_apst(ctrl);
1891 if (ret < 0)
1892 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001893
1894 ret = nvme_configure_timestamp(ctrl);
1895 if (ret < 0)
1896 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001897
1898 ret = nvme_configure_directives(ctrl);
1899 if (ret < 0)
1900 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001901
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001902 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001903
Keith Busch634b8322017-08-10 11:23:31 +02001904 return 0;
1905
1906out_free:
1907 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001908 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001909}
Ming Lin576d55d2016-02-10 10:03:32 -08001910EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001911
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001912static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001913{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001914 struct nvme_ctrl *ctrl =
1915 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001916
Christoph Hellwig999ada22017-10-18 17:09:31 +02001917 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001918 return -EWOULDBLOCK;
1919 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001920 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001921}
1922
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001923static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1924{
1925 struct nvme_ns *ns;
1926 int ret;
1927
1928 mutex_lock(&ctrl->namespaces_mutex);
1929 if (list_empty(&ctrl->namespaces)) {
1930 ret = -ENOTTY;
1931 goto out_unlock;
1932 }
1933
1934 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1935 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001936 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001937 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1938 ret = -EINVAL;
1939 goto out_unlock;
1940 }
1941
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001942 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001943 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1944 kref_get(&ns->kref);
1945 mutex_unlock(&ctrl->namespaces_mutex);
1946
1947 ret = nvme_user_cmd(ctrl, ns, argp);
1948 nvme_put_ns(ns);
1949 return ret;
1950
1951out_unlock:
1952 mutex_unlock(&ctrl->namespaces_mutex);
1953 return ret;
1954}
1955
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001956static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1957 unsigned long arg)
1958{
1959 struct nvme_ctrl *ctrl = file->private_data;
1960 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001961
1962 switch (cmd) {
1963 case NVME_IOCTL_ADMIN_CMD:
1964 return nvme_user_cmd(ctrl, NULL, argp);
1965 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001966 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001967 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001968 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001969 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001970 case NVME_IOCTL_SUBSYS_RESET:
1971 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001972 case NVME_IOCTL_RESCAN:
1973 nvme_queue_scan(ctrl);
1974 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001975 default:
1976 return -ENOTTY;
1977 }
1978}
1979
1980static const struct file_operations nvme_dev_fops = {
1981 .owner = THIS_MODULE,
1982 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001983 .unlocked_ioctl = nvme_dev_ioctl,
1984 .compat_ioctl = nvme_dev_ioctl,
1985};
1986
1987static ssize_t nvme_sysfs_reset(struct device *dev,
1988 struct device_attribute *attr, const char *buf,
1989 size_t count)
1990{
1991 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1992 int ret;
1993
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001994 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001995 if (ret < 0)
1996 return ret;
1997 return count;
1998}
1999static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2000
Keith Busch9ec3bb22016-04-29 15:45:18 -06002001static ssize_t nvme_sysfs_rescan(struct device *dev,
2002 struct device_attribute *attr, const char *buf,
2003 size_t count)
2004{
2005 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2006
2007 nvme_queue_scan(ctrl);
2008 return count;
2009}
2010static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2011
Keith Busch118472a2016-02-18 09:57:48 -07002012static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2013 char *buf)
2014{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002015 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002016 struct nvme_ctrl *ctrl = ns->ctrl;
2017 int serial_len = sizeof(ctrl->serial);
2018 int model_len = sizeof(ctrl->model);
2019
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002020 if (!uuid_is_null(&ns->uuid))
2021 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2022
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002023 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2024 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002025
2026 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2027 return sprintf(buf, "eui.%8phN\n", ns->eui);
2028
Martin Wilck758f3732017-07-20 18:34:02 +02002029 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2030 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002031 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002032 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2033 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002034 model_len--;
2035
2036 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2037 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2038}
2039static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2040
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002041static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2042 char *buf)
2043{
2044 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2045 return sprintf(buf, "%pU\n", ns->nguid);
2046}
2047static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2048
Keith Busch2b9b6e82015-12-22 10:10:45 -07002049static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2050 char *buf)
2051{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002052 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002053
2054 /* For backward compatibility expose the NGUID to userspace if
2055 * we have no UUID set
2056 */
2057 if (uuid_is_null(&ns->uuid)) {
2058 printk_ratelimited(KERN_WARNING
2059 "No UUID available providing old NGUID\n");
2060 return sprintf(buf, "%pU\n", ns->nguid);
2061 }
2062 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002063}
2064static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2065
2066static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2067 char *buf)
2068{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002069 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002070 return sprintf(buf, "%8phd\n", ns->eui);
2071}
2072static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2073
2074static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2075 char *buf)
2076{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002077 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002078 return sprintf(buf, "%d\n", ns->ns_id);
2079}
2080static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2081
2082static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002083 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002084 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002085 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002086 &dev_attr_eui.attr,
2087 &dev_attr_nsid.attr,
2088 NULL,
2089};
2090
Ming Lin1a353d82016-06-13 16:45:24 +02002091static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002092 struct attribute *a, int n)
2093{
2094 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002095 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002096
2097 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002098 if (uuid_is_null(&ns->uuid) ||
2099 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2100 return 0;
2101 }
2102 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002103 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002104 return 0;
2105 }
2106 if (a == &dev_attr_eui.attr) {
2107 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2108 return 0;
2109 }
2110 return a->mode;
2111}
2112
2113static const struct attribute_group nvme_ns_attr_group = {
2114 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002115 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002116};
2117
Ming Lin931e1c22016-02-26 13:24:19 -08002118#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002119static ssize_t field##_show(struct device *dev, \
2120 struct device_attribute *attr, char *buf) \
2121{ \
2122 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2123 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2124} \
2125static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2126
Ming Lin931e1c22016-02-26 13:24:19 -08002127#define nvme_show_int_function(field) \
2128static ssize_t field##_show(struct device *dev, \
2129 struct device_attribute *attr, char *buf) \
2130{ \
2131 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2132 return sprintf(buf, "%d\n", ctrl->field); \
2133} \
2134static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2135
2136nvme_show_str_function(model);
2137nvme_show_str_function(serial);
2138nvme_show_str_function(firmware_rev);
2139nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002140
Ming Lin1a353d82016-06-13 16:45:24 +02002141static ssize_t nvme_sysfs_delete(struct device *dev,
2142 struct device_attribute *attr, const char *buf,
2143 size_t count)
2144{
2145 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2146
2147 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002148 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002149 return count;
2150}
2151static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2152
2153static ssize_t nvme_sysfs_show_transport(struct device *dev,
2154 struct device_attribute *attr,
2155 char *buf)
2156{
2157 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2158
2159 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2160}
2161static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2162
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002163static ssize_t nvme_sysfs_show_state(struct device *dev,
2164 struct device_attribute *attr,
2165 char *buf)
2166{
2167 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2168 static const char *const state_name[] = {
2169 [NVME_CTRL_NEW] = "new",
2170 [NVME_CTRL_LIVE] = "live",
2171 [NVME_CTRL_RESETTING] = "resetting",
2172 [NVME_CTRL_RECONNECTING]= "reconnecting",
2173 [NVME_CTRL_DELETING] = "deleting",
2174 [NVME_CTRL_DEAD] = "dead",
2175 };
2176
2177 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2178 state_name[ctrl->state])
2179 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2180
2181 return sprintf(buf, "unknown state\n");
2182}
2183
2184static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2185
Ming Lin1a353d82016-06-13 16:45:24 +02002186static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2187 struct device_attribute *attr,
2188 char *buf)
2189{
2190 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2191
Christoph Hellwig180de0072017-06-26 12:39:02 +02002192 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002193}
2194static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2195
2196static ssize_t nvme_sysfs_show_address(struct device *dev,
2197 struct device_attribute *attr,
2198 char *buf)
2199{
2200 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2201
2202 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2203}
2204static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2205
Keith Busch779ff7562016-01-12 15:09:31 -07002206static struct attribute *nvme_dev_attrs[] = {
2207 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002208 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002209 &dev_attr_model.attr,
2210 &dev_attr_serial.attr,
2211 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002212 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002213 &dev_attr_delete_controller.attr,
2214 &dev_attr_transport.attr,
2215 &dev_attr_subsysnqn.attr,
2216 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002217 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002218 NULL
2219};
2220
Ming Lin1a353d82016-06-13 16:45:24 +02002221static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2222 struct attribute *a, int n)
2223{
2224 struct device *dev = container_of(kobj, struct device, kobj);
2225 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2226
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002227 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2228 return 0;
2229 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2230 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002231
2232 return a->mode;
2233}
2234
Keith Busch779ff7562016-01-12 15:09:31 -07002235static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002236 .attrs = nvme_dev_attrs,
2237 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002238};
2239
2240static const struct attribute_group *nvme_dev_attr_groups[] = {
2241 &nvme_dev_attrs_group,
2242 NULL,
2243};
2244
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002245static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2246{
2247 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2248 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2249
2250 return nsa->ns_id - nsb->ns_id;
2251}
2252
Keith Busch32f0c4a2016-07-13 11:45:02 -06002253static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002254{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002255 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002256
Keith Busch32f0c4a2016-07-13 11:45:02 -06002257 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002258 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002259 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002260 if (!kref_get_unless_zero(&ns->kref))
2261 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002262 ret = ns;
2263 break;
2264 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002265 if (ns->ns_id > nsid)
2266 break;
2267 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002268 mutex_unlock(&ctrl->namespaces_mutex);
2269 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002270}
2271
Jens Axboef5d11842017-06-27 12:03:06 -06002272static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2273{
2274 struct streams_directive_params s;
2275 int ret;
2276
2277 if (!ctrl->nr_streams)
2278 return 0;
2279
2280 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2281 if (ret)
2282 return ret;
2283
2284 ns->sws = le32_to_cpu(s.sws);
2285 ns->sgs = le16_to_cpu(s.sgs);
2286
2287 if (ns->sws) {
2288 unsigned int bs = 1 << ns->lba_shift;
2289
2290 blk_queue_io_min(ns->queue, bs * ns->sws);
2291 if (ns->sgs)
2292 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2293 }
2294
2295 return 0;
2296}
2297
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002298static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2299{
2300 struct nvme_ns *ns;
2301 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002302 struct nvme_id_ns *id;
2303 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002304 int node = dev_to_node(ctrl->dev);
2305
2306 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2307 if (!ns)
2308 return;
2309
Keith Busch075790e2016-02-24 09:15:53 -07002310 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2311 if (ns->instance < 0)
2312 goto out_free_ns;
2313
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002314 ns->queue = blk_mq_init_queue(ctrl->tagset);
2315 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002316 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002317 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2318 ns->queue->queuedata = ns;
2319 ns->ctrl = ctrl;
2320
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002321 kref_init(&ns->kref);
2322 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002323 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002324
2325 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002326 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002327 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002328
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002329 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002330
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002331 id = nvme_identify_ns(ctrl, nsid);
2332 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002333 goto out_free_queue;
2334
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002335 if (id->ncap == 0)
2336 goto out_free_id;
2337
2338 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2339
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002340 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2341 if (nvme_nvm_register(ns, disk_name, node)) {
2342 dev_warn(ctrl->device, "LightNVM init failure\n");
2343 goto out_free_id;
2344 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002345 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002346
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002347 disk = alloc_disk_node(0, node);
2348 if (!disk)
2349 goto out_free_id;
2350
2351 disk->fops = &nvme_fops;
2352 disk->private_data = ns;
2353 disk->queue = ns->queue;
2354 disk->flags = GENHD_FL_EXT_DEVT;
2355 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2356 ns->disk = disk;
2357
2358 __nvme_revalidate_disk(disk, id);
2359
Keith Busch32f0c4a2016-07-13 11:45:02 -06002360 mutex_lock(&ctrl->namespaces_mutex);
2361 list_add_tail(&ns->list, &ctrl->namespaces);
2362 mutex_unlock(&ctrl->namespaces_mutex);
2363
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002364 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002365
2366 kfree(id);
2367
Dan Williams0d52c7562016-06-15 19:44:20 -07002368 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002369 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2370 &nvme_ns_attr_group))
2371 pr_warn("%s: failed to create sysfs group for identification\n",
2372 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002373 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2374 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2375 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002376 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002377 out_free_id:
2378 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002379 out_free_queue:
2380 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002381 out_release_instance:
2382 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002383 out_free_ns:
2384 kfree(ns);
2385}
2386
2387static void nvme_ns_remove(struct nvme_ns *ns)
2388{
Keith Busch646017a2016-02-24 09:15:54 -07002389 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2390 return;
2391
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002392 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002393 if (blk_get_integrity(ns->disk))
2394 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002395 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2396 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002397 if (ns->ndev)
2398 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002399 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002400 blk_cleanup_queue(ns->queue);
2401 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002402
2403 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002404 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002405 mutex_unlock(&ns->ctrl->namespaces_mutex);
2406
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002407 nvme_put_ns(ns);
2408}
2409
Keith Busch540c8012015-10-22 15:45:06 -06002410static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2411{
2412 struct nvme_ns *ns;
2413
Keith Busch32f0c4a2016-07-13 11:45:02 -06002414 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002415 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002416 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002417 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002418 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002419 } else
2420 nvme_alloc_ns(ctrl, nsid);
2421}
2422
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302423static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2424 unsigned nsid)
2425{
2426 struct nvme_ns *ns, *next;
2427
2428 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2429 if (ns->ns_id > nsid)
2430 nvme_ns_remove(ns);
2431 }
2432}
2433
Keith Busch540c8012015-10-22 15:45:06 -06002434static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2435{
2436 struct nvme_ns *ns;
2437 __le32 *ns_list;
2438 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2439 int ret = 0;
2440
2441 ns_list = kzalloc(0x1000, GFP_KERNEL);
2442 if (!ns_list)
2443 return -ENOMEM;
2444
2445 for (i = 0; i < num_lists; i++) {
2446 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2447 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302448 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002449
2450 for (j = 0; j < min(nn, 1024U); j++) {
2451 nsid = le32_to_cpu(ns_list[j]);
2452 if (!nsid)
2453 goto out;
2454
2455 nvme_validate_ns(ctrl, nsid);
2456
2457 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002458 ns = nvme_find_get_ns(ctrl, prev);
2459 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002460 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002461 nvme_put_ns(ns);
2462 }
Keith Busch540c8012015-10-22 15:45:06 -06002463 }
2464 }
2465 nn -= j;
2466 }
2467 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302468 nvme_remove_invalid_namespaces(ctrl, prev);
2469 free:
Keith Busch540c8012015-10-22 15:45:06 -06002470 kfree(ns_list);
2471 return ret;
2472}
2473
Christoph Hellwig5955be22016-04-26 13:51:59 +02002474static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002475{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002476 unsigned i;
2477
Keith Busch540c8012015-10-22 15:45:06 -06002478 for (i = 1; i <= nn; i++)
2479 nvme_validate_ns(ctrl, i);
2480
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302481 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002482}
2483
Christoph Hellwig5955be22016-04-26 13:51:59 +02002484static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002485{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002486 struct nvme_ctrl *ctrl =
2487 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002488 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002489 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002490
Christoph Hellwig5955be22016-04-26 13:51:59 +02002491 if (ctrl->state != NVME_CTRL_LIVE)
2492 return;
2493
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002494 if (nvme_identify_ctrl(ctrl, &id))
2495 return;
Keith Busch540c8012015-10-22 15:45:06 -06002496
2497 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002498 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002499 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2500 if (!nvme_scan_ns_list(ctrl, nn))
2501 goto done;
2502 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002503 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002504 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002505 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002506 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002507 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002508 kfree(id);
2509}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002510
2511void nvme_queue_scan(struct nvme_ctrl *ctrl)
2512{
2513 /*
2514 * Do not queue new scan work when a controller is reset during
2515 * removal.
2516 */
2517 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002518 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002519}
2520EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002521
Keith Busch32f0c4a2016-07-13 11:45:02 -06002522/*
2523 * This function iterates the namespace list unlocked to allow recovery from
2524 * controller failure. It is up to the caller to ensure the namespace list is
2525 * not modified by scan work while this function is executing.
2526 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002527void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2528{
2529 struct nvme_ns *ns, *next;
2530
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002531 /*
2532 * The dead states indicates the controller was not gracefully
2533 * disconnected. In that case, we won't be able to flush any data while
2534 * removing the namespaces' disks; fail all the queues now to avoid
2535 * potentially having to clean up the failed sync later.
2536 */
2537 if (ctrl->state == NVME_CTRL_DEAD)
2538 nvme_kill_queues(ctrl);
2539
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002540 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2541 nvme_ns_remove(ns);
2542}
Ming Lin576d55d2016-02-10 10:03:32 -08002543EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002544
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002545static void nvme_async_event_work(struct work_struct *work)
2546{
2547 struct nvme_ctrl *ctrl =
2548 container_of(work, struct nvme_ctrl, async_event_work);
2549
2550 spin_lock_irq(&ctrl->lock);
James Smartcd482822017-09-14 11:03:09 -07002551 while (ctrl->state == NVME_CTRL_LIVE && ctrl->event_limit > 0) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002552 int aer_idx = --ctrl->event_limit;
2553
2554 spin_unlock_irq(&ctrl->lock);
2555 ctrl->ops->submit_async_event(ctrl, aer_idx);
2556 spin_lock_irq(&ctrl->lock);
2557 }
2558 spin_unlock_irq(&ctrl->lock);
2559}
2560
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302561static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2562{
2563
2564 u32 csts;
2565
2566 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2567 return false;
2568
2569 if (csts == ~0)
2570 return false;
2571
2572 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2573}
2574
2575static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2576{
2577 struct nvme_command c = { };
2578 struct nvme_fw_slot_info_log *log;
2579
2580 log = kmalloc(sizeof(*log), GFP_KERNEL);
2581 if (!log)
2582 return;
2583
2584 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302585 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302586 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2587
2588 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2589 dev_warn(ctrl->device,
2590 "Get FW SLOT INFO log error\n");
2591 kfree(log);
2592}
2593
2594static void nvme_fw_act_work(struct work_struct *work)
2595{
2596 struct nvme_ctrl *ctrl = container_of(work,
2597 struct nvme_ctrl, fw_act_work);
2598 unsigned long fw_act_timeout;
2599
2600 if (ctrl->mtfa)
2601 fw_act_timeout = jiffies +
2602 msecs_to_jiffies(ctrl->mtfa * 100);
2603 else
2604 fw_act_timeout = jiffies +
2605 msecs_to_jiffies(admin_timeout * 1000);
2606
2607 nvme_stop_queues(ctrl);
2608 while (nvme_ctrl_pp_status(ctrl)) {
2609 if (time_after(jiffies, fw_act_timeout)) {
2610 dev_warn(ctrl->device,
2611 "Fw activation timeout, reset controller\n");
2612 nvme_reset_ctrl(ctrl);
2613 break;
2614 }
2615 msleep(100);
2616 }
2617
2618 if (ctrl->state != NVME_CTRL_LIVE)
2619 return;
2620
2621 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09002622 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302623 nvme_get_fw_slot_info(ctrl);
2624}
2625
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002626void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2627 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002628{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002629 u32 result = le32_to_cpu(res->u32);
2630 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002631
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002632 switch (le16_to_cpu(status) >> 1) {
2633 case NVME_SC_SUCCESS:
2634 done = false;
2635 /*FALLTHRU*/
2636 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002637 ++ctrl->event_limit;
James Smartcd482822017-09-14 11:03:09 -07002638 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002639 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002640 break;
2641 default:
2642 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002643 }
2644
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002645 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002646 return;
2647
2648 switch (result & 0xff07) {
2649 case NVME_AER_NOTICE_NS_CHANGED:
2650 dev_info(ctrl->device, "rescanning\n");
2651 nvme_queue_scan(ctrl);
2652 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302653 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002654 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302655 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002656 default:
2657 dev_warn(ctrl->device, "async event result %08x\n", result);
2658 }
2659}
2660EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2661
2662void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2663{
2664 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002665 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002666}
2667EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2668
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002669void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002670{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002671 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002672 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002673 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302674 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002675}
2676EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002677
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002678void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2679{
2680 if (ctrl->kato)
2681 nvme_start_keep_alive(ctrl);
2682
2683 if (ctrl->queue_count > 1) {
2684 nvme_queue_scan(ctrl);
2685 nvme_queue_async_events(ctrl);
2686 nvme_start_queues(ctrl);
2687 }
2688}
2689EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2690
2691void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2692{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002693 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002694}
Ming Lin576d55d2016-02-10 10:03:32 -08002695EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002696
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002697static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002698{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002699 struct nvme_ctrl *ctrl =
2700 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002701
Christoph Hellwig9843f682017-10-18 13:10:01 +02002702 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002703 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002704
2705 ctrl->ops->free_ctrl(ctrl);
2706}
2707
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002708/*
2709 * Initialize a NVMe controller structures. This needs to be called during
2710 * earliest initialization so that we have the initialized structured around
2711 * during probing.
2712 */
2713int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2714 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2715{
2716 int ret;
2717
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002718 ctrl->state = NVME_CTRL_NEW;
2719 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002720 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002721 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002722 ctrl->dev = dev;
2723 ctrl->ops = ops;
2724 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002725 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002726 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302727 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002728 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002729
Christoph Hellwig9843f682017-10-18 13:10:01 +02002730 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2731 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002732 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002733 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002734
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002735 device_initialize(&ctrl->ctrl_device);
2736 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002737 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002738 ctrl->device->class = nvme_class;
2739 ctrl->device->parent = ctrl->dev;
2740 ctrl->device->groups = nvme_dev_attr_groups;
2741 ctrl->device->release = nvme_free_ctrl;
2742 dev_set_drvdata(ctrl->device, ctrl);
2743 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
2744 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002745 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002746
2747 cdev_init(&ctrl->cdev, &nvme_dev_fops);
2748 ctrl->cdev.owner = ops->module;
2749 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002750 if (ret)
2751 goto out_free_name;
2752
Keith Busch075790e2016-02-24 09:15:53 -07002753 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002754
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002755 /*
2756 * Initialize latency tolerance controls. The sysfs files won't
2757 * be visible to userspace unless the device actually supports APST.
2758 */
2759 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2760 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2761 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2762
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002763 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002764out_free_name:
2765 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002766out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02002767 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002768out:
2769 return ret;
2770}
Ming Lin576d55d2016-02-10 10:03:32 -08002771EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002772
Keith Busch69d9a992016-02-24 09:15:56 -07002773/**
2774 * nvme_kill_queues(): Ends all namespace queues
2775 * @ctrl: the dead controller that needs to end
2776 *
2777 * Call this function when the driver determines it is unable to get the
2778 * controller in a state capable of servicing IO.
2779 */
2780void nvme_kill_queues(struct nvme_ctrl *ctrl)
2781{
2782 struct nvme_ns *ns;
2783
Keith Busch32f0c4a2016-07-13 11:45:02 -06002784 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002785
Ming Lei443bd902017-06-19 10:21:08 +08002786 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002787 if (ctrl->admin_q)
2788 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002789
Keith Busch32f0c4a2016-07-13 11:45:02 -06002790 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002791 /*
2792 * Revalidating a dead namespace sets capacity to 0. This will
2793 * end buffered writers dirtying pages that can't be synced.
2794 */
Keith Buschf33447b2017-02-10 18:15:51 -05002795 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2796 continue;
2797 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002798 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002799
Ming Lei443bd902017-06-19 10:21:08 +08002800 /* Forcibly unquiesce queues to avoid blocking dispatch */
2801 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002802 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002803 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002804}
Linus Torvalds237045f2016-03-18 17:13:31 -07002805EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002806
Keith Busch302ad8c2017-03-01 14:22:12 -05002807void nvme_unfreeze(struct nvme_ctrl *ctrl)
2808{
2809 struct nvme_ns *ns;
2810
2811 mutex_lock(&ctrl->namespaces_mutex);
2812 list_for_each_entry(ns, &ctrl->namespaces, list)
2813 blk_mq_unfreeze_queue(ns->queue);
2814 mutex_unlock(&ctrl->namespaces_mutex);
2815}
2816EXPORT_SYMBOL_GPL(nvme_unfreeze);
2817
2818void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2819{
2820 struct nvme_ns *ns;
2821
2822 mutex_lock(&ctrl->namespaces_mutex);
2823 list_for_each_entry(ns, &ctrl->namespaces, list) {
2824 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2825 if (timeout <= 0)
2826 break;
2827 }
2828 mutex_unlock(&ctrl->namespaces_mutex);
2829}
2830EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2831
2832void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2833{
2834 struct nvme_ns *ns;
2835
2836 mutex_lock(&ctrl->namespaces_mutex);
2837 list_for_each_entry(ns, &ctrl->namespaces, list)
2838 blk_mq_freeze_queue_wait(ns->queue);
2839 mutex_unlock(&ctrl->namespaces_mutex);
2840}
2841EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2842
2843void nvme_start_freeze(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)
Ming Lei1671d522017-03-27 20:06:57 +08002849 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002850 mutex_unlock(&ctrl->namespaces_mutex);
2851}
2852EXPORT_SYMBOL_GPL(nvme_start_freeze);
2853
Keith Busch25646262016-01-04 09:10:57 -07002854void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002855{
2856 struct nvme_ns *ns;
2857
Keith Busch32f0c4a2016-07-13 11:45:02 -06002858 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002859 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002860 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002861 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002862}
Ming Lin576d55d2016-02-10 10:03:32 -08002863EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002864
Keith Busch25646262016-01-04 09:10:57 -07002865void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002866{
2867 struct nvme_ns *ns;
2868
Keith Busch32f0c4a2016-07-13 11:45:02 -06002869 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002870 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002871 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002872 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002873}
Ming Lin576d55d2016-02-10 10:03:32 -08002874EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002875
Sagi Grimberg31b84462017-10-11 12:53:07 +03002876int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2877{
2878 if (!ctrl->ops->reinit_request)
2879 return 0;
2880
2881 return blk_mq_tagset_iter(set, set->driver_data,
2882 ctrl->ops->reinit_request);
2883}
2884EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
2885
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002886int __init nvme_core_init(void)
2887{
2888 int result;
2889
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002890 nvme_wq = alloc_workqueue("nvme-wq",
2891 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2892 if (!nvme_wq)
2893 return -ENOMEM;
2894
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002895 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002896 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002897 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002898
2899 nvme_class = class_create(THIS_MODULE, "nvme");
2900 if (IS_ERR(nvme_class)) {
2901 result = PTR_ERR(nvme_class);
2902 goto unregister_chrdev;
2903 }
2904
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002905 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002906
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002907unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002908 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002909destroy_wq:
2910 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002911 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002912}
2913
2914void nvme_core_exit(void)
2915{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002916 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002917 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002918 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002919}
Ming Lin576d55d2016-02-10 10:03:32 -08002920
2921MODULE_LICENSE("GPL");
2922MODULE_VERSION("1.0");
2923module_init(nvme_core_init);
2924module_exit(nvme_core_exit);