blob: 003314eb6341ad205fc505190219bded9f1376d1 [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;
175 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200176}
177
178void nvme_complete_rq(struct request *req)
179{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200180 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
181 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300182 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200183 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200184 }
185
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200186 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200187}
188EXPORT_SYMBOL_GPL(nvme_complete_rq);
189
Ming Linc55a2fd2016-05-18 14:05:02 -0700190void nvme_cancel_request(struct request *req, void *data, bool reserved)
191{
192 int status;
193
194 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
200 status = NVME_SC_ABORT_REQ;
201 if (blk_queue_dying(req->q))
202 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200203 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200204 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200205
Ming Linc55a2fd2016-05-18 14:05:02 -0700206}
207EXPORT_SYMBOL_GPL(nvme_cancel_request);
208
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200209bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
210 enum nvme_ctrl_state new_state)
211{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300212 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200213 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200214 bool changed = false;
215
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200216 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300217
218 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200219 switch (new_state) {
220 case NVME_CTRL_LIVE:
221 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200222 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200223 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900224 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200225 changed = true;
226 /* FALLTHRU */
227 default:
228 break;
229 }
230 break;
231 case NVME_CTRL_RESETTING:
232 switch (old_state) {
233 case NVME_CTRL_NEW:
234 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900235 changed = true;
236 /* FALLTHRU */
237 default:
238 break;
239 }
240 break;
241 case NVME_CTRL_RECONNECTING:
242 switch (old_state) {
243 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200244 changed = true;
245 /* FALLTHRU */
246 default:
247 break;
248 }
249 break;
250 case NVME_CTRL_DELETING:
251 switch (old_state) {
252 case NVME_CTRL_LIVE:
253 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900254 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200255 changed = true;
256 /* FALLTHRU */
257 default:
258 break;
259 }
260 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600261 case NVME_CTRL_DEAD:
262 switch (old_state) {
263 case NVME_CTRL_DELETING:
264 changed = true;
265 /* FALLTHRU */
266 default:
267 break;
268 }
269 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200270 default:
271 break;
272 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200273
274 if (changed)
275 ctrl->state = new_state;
276
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200277 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300278
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200279 return changed;
280}
281EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
282
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100283static void nvme_free_ns(struct kref *kref)
284{
285 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
286
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200287 if (ns->ndev)
288 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100289
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100290 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700291 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
292 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100293 kfree(ns);
294}
295
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100296static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100297{
298 kref_put(&ns->kref, nvme_free_ns);
299}
300
Christoph Hellwig41609822015-11-20 09:00:02 +0100301struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200302 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100303{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100304 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100305 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100306
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200307 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100308 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200309 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100310 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200311 qid ? qid - 1 : 0);
312 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100313 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100314 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100315
Christoph Hellwig21d34712015-11-26 09:08:36 +0100316 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800317 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100318
Christoph Hellwig41609822015-11-20 09:00:02 +0100319 return req;
320}
Ming Lin576d55d2016-02-10 10:03:32 -0800321EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100322
Jens Axboef5d11842017-06-27 12:03:06 -0600323static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
324{
325 struct nvme_command c;
326
327 memset(&c, 0, sizeof(c));
328
329 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530330 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600331 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
332 c.directive.dtype = NVME_DIR_IDENTIFY;
333 c.directive.tdtype = NVME_DIR_STREAMS;
334 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
335
336 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
337}
338
339static int nvme_disable_streams(struct nvme_ctrl *ctrl)
340{
341 return nvme_toggle_streams(ctrl, false);
342}
343
344static int nvme_enable_streams(struct nvme_ctrl *ctrl)
345{
346 return nvme_toggle_streams(ctrl, true);
347}
348
349static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
350 struct streams_directive_params *s, u32 nsid)
351{
352 struct nvme_command c;
353
354 memset(&c, 0, sizeof(c));
355 memset(s, 0, sizeof(*s));
356
357 c.directive.opcode = nvme_admin_directive_recv;
358 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700359 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600360 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
361 c.directive.dtype = NVME_DIR_STREAMS;
362
363 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
364}
365
366static int nvme_configure_directives(struct nvme_ctrl *ctrl)
367{
368 struct streams_directive_params s;
369 int ret;
370
371 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
372 return 0;
373 if (!streams)
374 return 0;
375
376 ret = nvme_enable_streams(ctrl);
377 if (ret)
378 return ret;
379
Arnav Dawn62346ea2017-07-12 16:11:53 +0530380 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600381 if (ret)
382 return ret;
383
384 ctrl->nssa = le16_to_cpu(s.nssa);
385 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
386 dev_info(ctrl->device, "too few streams (%u) available\n",
387 ctrl->nssa);
388 nvme_disable_streams(ctrl);
389 return 0;
390 }
391
392 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
393 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
394 return 0;
395}
396
397/*
398 * Check if 'req' has a write hint associated with it. If it does, assign
399 * a valid namespace stream to the write.
400 */
401static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
402 struct request *req, u16 *control,
403 u32 *dsmgmt)
404{
405 enum rw_hint streamid = req->write_hint;
406
407 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
408 streamid = 0;
409 else {
410 streamid--;
411 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
412 return;
413
414 *control |= NVME_RW_DTYPE_STREAMS;
415 *dsmgmt |= streamid << 16;
416 }
417
418 if (streamid < ARRAY_SIZE(req->q->write_hints))
419 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
420}
421
Ming Lin8093f7c2016-04-12 13:10:14 -0600422static inline void nvme_setup_flush(struct nvme_ns *ns,
423 struct nvme_command *cmnd)
424{
425 memset(cmnd, 0, sizeof(*cmnd));
426 cmnd->common.opcode = nvme_cmd_flush;
427 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
428}
429
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200430static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600431 struct nvme_command *cmnd)
432{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100433 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600434 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100435 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600436
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100437 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600438 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200439 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600440
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100441 __rq_for_each_bio(bio, req) {
442 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
443 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
444
445 range[n].cattr = cpu_to_le32(0);
446 range[n].nlb = cpu_to_le32(nlb);
447 range[n].slba = cpu_to_le64(slba);
448 n++;
449 }
450
451 if (WARN_ON_ONCE(n != segments)) {
452 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200453 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100454 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600455
456 memset(cmnd, 0, sizeof(*cmnd));
457 cmnd->dsm.opcode = nvme_cmd_dsm;
458 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200459 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600460 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
461
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700462 req->special_vec.bv_page = virt_to_page(range);
463 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100464 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700465 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600466
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200467 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600468}
Ming Lin8093f7c2016-04-12 13:10:14 -0600469
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200470static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
471 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600472{
Jens Axboef5d11842017-06-27 12:03:06 -0600473 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600474 u16 control = 0;
475 u32 dsmgmt = 0;
476
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200477 /*
478 * If formated with metadata, require the block layer provide a buffer
479 * unless this namespace is formated such that the metadata can be
480 * stripped/generated by the controller with PRACT=1.
481 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300482 if (ns && ns->ms &&
483 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200484 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
485 return BLK_STS_NOTSUPP;
486
Ming Lin8093f7c2016-04-12 13:10:14 -0600487 if (req->cmd_flags & REQ_FUA)
488 control |= NVME_RW_FUA;
489 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
490 control |= NVME_RW_LR;
491
492 if (req->cmd_flags & REQ_RAHEAD)
493 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
494
495 memset(cmnd, 0, sizeof(*cmnd));
496 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600497 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
498 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
499 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
500
Jens Axboef5d11842017-06-27 12:03:06 -0600501 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
502 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
503
Ming Lin8093f7c2016-04-12 13:10:14 -0600504 if (ns->ms) {
505 switch (ns->pi_type) {
506 case NVME_NS_DPS_PI_TYPE3:
507 control |= NVME_RW_PRINFO_PRCHK_GUARD;
508 break;
509 case NVME_NS_DPS_PI_TYPE1:
510 case NVME_NS_DPS_PI_TYPE2:
511 control |= NVME_RW_PRINFO_PRCHK_GUARD |
512 NVME_RW_PRINFO_PRCHK_REF;
513 cmnd->rw.reftag = cpu_to_le32(
514 nvme_block_nr(ns, blk_rq_pos(req)));
515 break;
516 }
517 if (!blk_integrity_rq(req))
518 control |= NVME_RW_PRINFO_PRACT;
519 }
520
521 cmnd->rw.control = cpu_to_le16(control);
522 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200523 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600524}
525
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200526blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600527 struct nvme_command *cmd)
528{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200529 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600530
Christoph Hellwig987f6992017-04-05 19:18:08 +0200531 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200532 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200533 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200534 req->rq_flags |= RQF_DONTPREP;
535 }
536
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100537 switch (req_op(req)) {
538 case REQ_OP_DRV_IN:
539 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800540 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100541 break;
542 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600543 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100544 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200545 case REQ_OP_WRITE_ZEROES:
546 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100547 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600548 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100549 break;
550 case REQ_OP_READ:
551 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200552 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100553 break;
554 default:
555 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200556 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100557 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600558
James Smart721b3912016-10-21 23:33:34 +0300559 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600560 return ret;
561}
562EXPORT_SYMBOL_GPL(nvme_setup_cmd);
563
Christoph Hellwig41609822015-11-20 09:00:02 +0100564/*
565 * Returns 0 on success. If the result is negative, it's a Linux error code;
566 * if the result is positive, it's an NVM Express status code
567 */
568int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800569 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200570 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100571{
572 struct request *req;
573 int ret;
574
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200575 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100576 if (IS_ERR(req))
577 return PTR_ERR(req);
578
579 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
580
Christoph Hellwig21d34712015-11-26 09:08:36 +0100581 if (buffer && bufflen) {
582 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
583 if (ret)
584 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100585 }
586
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200587 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800588 if (result)
589 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200590 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
591 ret = -EINTR;
592 else
593 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100594 out:
595 blk_mq_free_request(req);
596 return ret;
597}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200598EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100599
600int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
601 void *buffer, unsigned bufflen)
602{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200603 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
604 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100605}
Ming Lin576d55d2016-02-10 10:03:32 -0800606EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100607
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400608static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
609 unsigned len, u32 seed, bool write)
610{
611 struct bio_integrity_payload *bip;
612 int ret = -ENOMEM;
613 void *buf;
614
615 buf = kmalloc(len, GFP_KERNEL);
616 if (!buf)
617 goto out;
618
619 ret = -EFAULT;
620 if (write && copy_from_user(buf, ubuf, len))
621 goto out_free_meta;
622
623 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
624 if (IS_ERR(bip)) {
625 ret = PTR_ERR(bip);
626 goto out_free_meta;
627 }
628
629 bip->bip_iter.bi_size = len;
630 bip->bip_iter.bi_sector = seed;
631 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
632 offset_in_page(buf));
633 if (ret == len)
634 return buf;
635 ret = -ENOMEM;
636out_free_meta:
637 kfree(buf);
638out:
639 return ERR_PTR(ret);
640}
641
Keith Busch63263d62017-08-29 17:46:04 -0400642static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400643 struct nvme_command *cmd, void __user *ubuffer,
644 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
645 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100646{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200647 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600648 struct nvme_ns *ns = q->queuedata;
649 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100650 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600651 struct bio *bio = NULL;
652 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100653 int ret;
654
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200655 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100656 if (IS_ERR(req))
657 return PTR_ERR(req);
658
659 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
660
661 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100662 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
663 GFP_KERNEL);
664 if (ret)
665 goto out;
666 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200667 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400668 if (disk && meta_buffer && meta_len) {
669 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
670 meta_seed, write);
671 if (IS_ERR(meta)) {
672 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600673 goto out_unmap;
674 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600675 }
676 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400677
Keith Busch0b7f1f22015-10-23 09:47:28 -0600678 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200679 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
680 ret = -EINTR;
681 else
682 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100683 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800684 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600685 if (meta && !ret && !write) {
686 if (copy_to_user(meta_buffer, meta, meta_len))
687 ret = -EFAULT;
688 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600689 kfree(meta);
690 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200691 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600692 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100693 out:
694 blk_mq_free_request(req);
695 return ret;
696}
697
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200698static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200699{
700 struct nvme_ctrl *ctrl = rq->end_io_data;
701
702 blk_mq_free_request(rq);
703
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200704 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200705 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200706 "failed nvme_keep_alive_end_io error=%d\n",
707 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200708 return;
709 }
710
711 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
712}
713
714static int nvme_keep_alive(struct nvme_ctrl *ctrl)
715{
716 struct nvme_command c;
717 struct request *rq;
718
719 memset(&c, 0, sizeof(c));
720 c.common.opcode = nvme_admin_keep_alive;
721
722 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
723 NVME_QID_ANY);
724 if (IS_ERR(rq))
725 return PTR_ERR(rq);
726
727 rq->timeout = ctrl->kato * HZ;
728 rq->end_io_data = ctrl;
729
730 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
731
732 return 0;
733}
734
735static void nvme_keep_alive_work(struct work_struct *work)
736{
737 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
738 struct nvme_ctrl, ka_work);
739
740 if (nvme_keep_alive(ctrl)) {
741 /* allocation failure, reset the controller */
742 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200743 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200744 return;
745 }
746}
747
748void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
749{
750 if (unlikely(ctrl->kato == 0))
751 return;
752
753 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
754 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
755}
756EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
757
758void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
759{
760 if (unlikely(ctrl->kato == 0))
761 return;
762
763 cancel_delayed_work_sync(&ctrl->ka_work);
764}
765EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
766
Keith Busch3f7f25a92017-06-20 15:09:56 -0400767static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100768{
769 struct nvme_command c = { };
770 int error;
771
772 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
773 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200774 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100775
776 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
777 if (!*id)
778 return -ENOMEM;
779
780 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
781 sizeof(struct nvme_id_ctrl));
782 if (error)
783 kfree(*id);
784 return error;
785}
786
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200787static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
788 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200789{
790 struct nvme_command c = { };
791 int status;
792 void *data;
793 int pos;
794 int len;
795
796 c.identify.opcode = nvme_admin_identify;
797 c.identify.nsid = cpu_to_le32(nsid);
798 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
799
800 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
801 if (!data)
802 return -ENOMEM;
803
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200804 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200805 NVME_IDENTIFY_DATA_SIZE);
806 if (status)
807 goto free_data;
808
809 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
810 struct nvme_ns_id_desc *cur = data + pos;
811
812 if (cur->nidl == 0)
813 break;
814
815 switch (cur->nidt) {
816 case NVME_NIDT_EUI64:
817 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200818 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200819 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
820 cur->nidl);
821 goto free_data;
822 }
823 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200824 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200825 break;
826 case NVME_NIDT_NGUID:
827 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200828 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200829 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
830 cur->nidl);
831 goto free_data;
832 }
833 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200834 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200835 break;
836 case NVME_NIDT_UUID:
837 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200838 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200839 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
840 cur->nidl);
841 goto free_data;
842 }
843 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200844 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200845 break;
846 default:
847 /* Skip unnkown types */
848 len = cur->nidl;
849 break;
850 }
851
852 len += sizeof(*cur);
853 }
854free_data:
855 kfree(data);
856 return status;
857}
858
Keith Busch540c8012015-10-22 15:45:06 -0600859static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
860{
861 struct nvme_command c = { };
862
863 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200864 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600865 c.identify.nsid = cpu_to_le32(nsid);
866 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
867}
868
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200869static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
870 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100871{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200872 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100873 struct nvme_command c = { };
874 int error;
875
876 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200877 c.identify.opcode = nvme_admin_identify;
878 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200879 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100880
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200881 id = kmalloc(sizeof(*id), GFP_KERNEL);
882 if (!id)
883 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100884
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200885 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
886 if (error) {
887 dev_warn(ctrl->device, "Identify namespace failed\n");
888 kfree(id);
889 return NULL;
890 }
891
892 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100893}
894
Keith Busch3f7f25a92017-06-20 15:09:56 -0400895static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700896 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100897{
898 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800899 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100900 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100901
902 memset(&c, 0, sizeof(c));
903 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100904 c.features.fid = cpu_to_le32(fid);
905 c.features.dword11 = cpu_to_le32(dword11);
906
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800907 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700908 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700909 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800910 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100911 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100912}
913
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100914int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
915{
916 u32 q_count = (*count - 1) | ((*count - 1) << 16);
917 u32 result;
918 int status, nr_io_queues;
919
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700920 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100921 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200922 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100923 return status;
924
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200925 /*
926 * Degraded controllers might return an error when setting the queue
927 * count. We still want to be able to bring them online and offer
928 * access to the admin queue, as that might be only way to fix them up.
929 */
930 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200931 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200932 *count = 0;
933 } else {
934 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
935 *count = min(*count, nr_io_queues);
936 }
937
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100938 return 0;
939}
Ming Lin576d55d2016-02-10 10:03:32 -0800940EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100941
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100942static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
943{
944 struct nvme_user_io io;
945 struct nvme_command c;
946 unsigned length, meta_len;
947 void __user *metadata;
948
949 if (copy_from_user(&io, uio, sizeof(io)))
950 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700951 if (io.flags)
952 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100953
954 switch (io.opcode) {
955 case nvme_cmd_write:
956 case nvme_cmd_read:
957 case nvme_cmd_compare:
958 break;
959 default:
960 return -EINVAL;
961 }
962
963 length = (io.nblocks + 1) << ns->lba_shift;
964 meta_len = (io.nblocks + 1) * ns->ms;
965 metadata = (void __user *)(uintptr_t)io.metadata;
966
967 if (ns->ext) {
968 length += meta_len;
969 meta_len = 0;
970 } else if (meta_len) {
971 if ((io.metadata & 3) || !io.metadata)
972 return -EINVAL;
973 }
974
975 memset(&c, 0, sizeof(c));
976 c.rw.opcode = io.opcode;
977 c.rw.flags = io.flags;
978 c.rw.nsid = cpu_to_le32(ns->ns_id);
979 c.rw.slba = cpu_to_le64(io.slba);
980 c.rw.length = cpu_to_le16(io.nblocks);
981 c.rw.control = cpu_to_le16(io.control);
982 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
983 c.rw.reftag = cpu_to_le32(io.reftag);
984 c.rw.apptag = cpu_to_le16(io.apptag);
985 c.rw.appmask = cpu_to_le16(io.appmask);
986
Keith Busch63263d62017-08-29 17:46:04 -0400987 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100988 (void __user *)(uintptr_t)io.addr, length,
989 metadata, meta_len, io.slba, NULL, 0);
990}
991
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100992static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100993 struct nvme_passthru_cmd __user *ucmd)
994{
995 struct nvme_passthru_cmd cmd;
996 struct nvme_command c;
997 unsigned timeout = 0;
998 int status;
999
1000 if (!capable(CAP_SYS_ADMIN))
1001 return -EACCES;
1002 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1003 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001004 if (cmd.flags)
1005 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001006
1007 memset(&c, 0, sizeof(c));
1008 c.common.opcode = cmd.opcode;
1009 c.common.flags = cmd.flags;
1010 c.common.nsid = cpu_to_le32(cmd.nsid);
1011 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1012 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1013 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1014 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1015 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1016 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1017 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1018 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1019
1020 if (cmd.timeout_ms)
1021 timeout = msecs_to_jiffies(cmd.timeout_ms);
1022
1023 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001024 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001025 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1026 0, &cmd.result, timeout);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001027 if (status >= 0) {
1028 if (put_user(cmd.result, &ucmd->result))
1029 return -EFAULT;
1030 }
1031
1032 return status;
1033}
1034
1035static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1036 unsigned int cmd, unsigned long arg)
1037{
1038 struct nvme_ns *ns = bdev->bd_disk->private_data;
1039
1040 switch (cmd) {
1041 case NVME_IOCTL_ID:
1042 force_successful_syscall_return();
1043 return ns->ns_id;
1044 case NVME_IOCTL_ADMIN_CMD:
1045 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1046 case NVME_IOCTL_IO_CMD:
1047 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1048 case NVME_IOCTL_SUBMIT_IO:
1049 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001050 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001051#ifdef CONFIG_NVM
1052 if (ns->ndev)
1053 return nvme_nvm_ioctl(ns, cmd, arg);
1054#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001055 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001056 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001057 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001058 return -ENOTTY;
1059 }
1060}
1061
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001062static int nvme_open(struct block_device *bdev, fmode_t mode)
1063{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001064 struct nvme_ns *ns = bdev->bd_disk->private_data;
1065
1066 if (!kref_get_unless_zero(&ns->kref))
1067 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001068 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001069}
1070
1071static void nvme_release(struct gendisk *disk, fmode_t mode)
1072{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001073 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001074}
1075
1076static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1077{
1078 /* some standard values */
1079 geo->heads = 1 << 6;
1080 geo->sectors = 1 << 5;
1081 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1082 return 0;
1083}
1084
1085#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001086static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1087 u16 bs)
1088{
1089 struct nvme_ns *ns = disk->private_data;
1090 u16 old_ms = ns->ms;
1091 u8 pi_type = 0;
1092
1093 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1094 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1095
1096 /* PI implementation requires metadata equal t10 pi tuple size */
1097 if (ns->ms == sizeof(struct t10_pi_tuple))
1098 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1099
1100 if (blk_get_integrity(disk) &&
1101 (ns->pi_type != pi_type || ns->ms != old_ms ||
1102 bs != queue_logical_block_size(disk->queue) ||
1103 (ns->ms && ns->ext)))
1104 blk_integrity_unregister(disk);
1105
1106 ns->pi_type = pi_type;
1107}
1108
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001109static void nvme_init_integrity(struct nvme_ns *ns)
1110{
1111 struct blk_integrity integrity;
1112
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001113 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001114 switch (ns->pi_type) {
1115 case NVME_NS_DPS_PI_TYPE3:
1116 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001117 integrity.tag_size = sizeof(u16) + sizeof(u32);
1118 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001119 break;
1120 case NVME_NS_DPS_PI_TYPE1:
1121 case NVME_NS_DPS_PI_TYPE2:
1122 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001123 integrity.tag_size = sizeof(u16);
1124 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001125 break;
1126 default:
1127 integrity.profile = NULL;
1128 break;
1129 }
1130 integrity.tuple_size = ns->ms;
1131 blk_integrity_register(ns->disk, &integrity);
1132 blk_queue_max_integrity_segments(ns->queue, 1);
1133}
1134#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001135static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1136 u16 bs)
1137{
1138}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001139static void nvme_init_integrity(struct nvme_ns *ns)
1140{
1141}
1142#endif /* CONFIG_BLK_DEV_INTEGRITY */
1143
Scott Bauer6b8190d2017-06-15 10:44:30 -06001144static void nvme_set_chunk_size(struct nvme_ns *ns)
1145{
1146 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1147 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1148}
1149
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001150static void nvme_config_discard(struct nvme_ns *ns)
1151{
Keith Busch08095e72016-03-04 13:15:17 -07001152 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001153 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001154
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001155 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1156 NVME_DSM_MAX_RANGES);
1157
Jens Axboef5d11842017-06-27 12:03:06 -06001158 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1159 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1160
1161 ns->queue->limits.discard_alignment = sz;
1162 ns->queue->limits.discard_granularity = sz;
1163 } else {
1164 ns->queue->limits.discard_alignment = logical_block_size;
1165 ns->queue->limits.discard_granularity = logical_block_size;
1166 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001167 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001168 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001169 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001170
1171 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1172 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001173}
1174
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001175static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1176 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001177{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001178 if (ctrl->vs >= NVME_VS(1, 1, 0))
1179 memcpy(eui64, id->eui64, sizeof(id->eui64));
1180 if (ctrl->vs >= NVME_VS(1, 2, 0))
1181 memcpy(nguid, id->nguid, sizeof(id->nguid));
1182 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001183 /* Don't treat error as fatal we potentially
1184 * already have a NGUID or EUI-64
1185 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001186 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1187 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001188 "%s: Identify Descriptors failed\n", __func__);
1189 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001190}
1191
1192static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1193{
1194 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001195 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001196 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001197
1198 /*
1199 * If identify namespace failed, use default 512 byte block size so
1200 * block layer can use before failing read/write for 0 capacity.
1201 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001202 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001203 if (ns->lba_shift == 0)
1204 ns->lba_shift = 9;
1205 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001206 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001207
1208 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001209
Jens Axboef5d11842017-06-27 12:03:06 -06001210 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001211 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001212 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001213 if (ns->noiob)
1214 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001215 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001216 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001217 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1218 set_capacity(disk, 0);
1219 else
1220 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1221
Jens Axboef5d11842017-06-27 12:03:06 -06001222 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001223 nvme_config_discard(ns);
1224 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001225}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001226
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001227static int nvme_revalidate_disk(struct gendisk *disk)
1228{
1229 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001230 struct nvme_ctrl *ctrl = ns->ctrl;
1231 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001232 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1233 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001234 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001235
1236 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1237 set_capacity(disk, 0);
1238 return -ENODEV;
1239 }
1240
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001241 id = nvme_identify_ns(ctrl, ns->ns_id);
1242 if (!id)
1243 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001244
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001245 if (id->ncap == 0) {
1246 ret = -ENODEV;
1247 goto out;
1248 }
1249
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001250 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1251 if (!uuid_equal(&ns->uuid, &uuid) ||
1252 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1253 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1254 dev_err(ctrl->device,
1255 "identifiers changed for nsid %d\n", ns->ns_id);
1256 ret = -ENODEV;
1257 }
1258
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001259out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001260 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001261 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001262}
1263
1264static char nvme_pr_type(enum pr_type type)
1265{
1266 switch (type) {
1267 case PR_WRITE_EXCLUSIVE:
1268 return 1;
1269 case PR_EXCLUSIVE_ACCESS:
1270 return 2;
1271 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1272 return 3;
1273 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1274 return 4;
1275 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1276 return 5;
1277 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1278 return 6;
1279 default:
1280 return 0;
1281 }
1282};
1283
1284static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1285 u64 key, u64 sa_key, u8 op)
1286{
1287 struct nvme_ns *ns = bdev->bd_disk->private_data;
1288 struct nvme_command c;
1289 u8 data[16] = { 0, };
1290
1291 put_unaligned_le64(key, &data[0]);
1292 put_unaligned_le64(sa_key, &data[8]);
1293
1294 memset(&c, 0, sizeof(c));
1295 c.common.opcode = op;
1296 c.common.nsid = cpu_to_le32(ns->ns_id);
1297 c.common.cdw10[0] = cpu_to_le32(cdw10);
1298
1299 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1300}
1301
1302static int nvme_pr_register(struct block_device *bdev, u64 old,
1303 u64 new, unsigned flags)
1304{
1305 u32 cdw10;
1306
1307 if (flags & ~PR_FL_IGNORE_KEY)
1308 return -EOPNOTSUPP;
1309
1310 cdw10 = old ? 2 : 0;
1311 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1312 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1313 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1314}
1315
1316static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1317 enum pr_type type, unsigned flags)
1318{
1319 u32 cdw10;
1320
1321 if (flags & ~PR_FL_IGNORE_KEY)
1322 return -EOPNOTSUPP;
1323
1324 cdw10 = nvme_pr_type(type) << 8;
1325 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1326 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1327}
1328
1329static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1330 enum pr_type type, bool abort)
1331{
1332 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1333 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1334}
1335
1336static int nvme_pr_clear(struct block_device *bdev, u64 key)
1337{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001338 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001339 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1340}
1341
1342static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1343{
1344 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1345 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1346}
1347
1348static const struct pr_ops nvme_pr_ops = {
1349 .pr_register = nvme_pr_register,
1350 .pr_reserve = nvme_pr_reserve,
1351 .pr_release = nvme_pr_release,
1352 .pr_preempt = nvme_pr_preempt,
1353 .pr_clear = nvme_pr_clear,
1354};
1355
Scott Bauera98e58e52017-02-03 12:50:32 -07001356#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001357int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1358 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001359{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001360 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001361 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001362
1363 memset(&cmd, 0, sizeof(cmd));
1364 if (send)
1365 cmd.common.opcode = nvme_admin_security_send;
1366 else
1367 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001368 cmd.common.nsid = 0;
1369 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1370 cmd.common.cdw10[1] = cpu_to_le32(len);
1371
1372 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1373 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1374}
1375EXPORT_SYMBOL_GPL(nvme_sec_submit);
1376#endif /* CONFIG_BLK_SED_OPAL */
1377
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001378static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001379 .owner = THIS_MODULE,
1380 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001381 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001382 .open = nvme_open,
1383 .release = nvme_release,
1384 .getgeo = nvme_getgeo,
1385 .revalidate_disk= nvme_revalidate_disk,
1386 .pr_ops = &nvme_pr_ops,
1387};
1388
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001389static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1390{
1391 unsigned long timeout =
1392 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1393 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1394 int ret;
1395
1396 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001397 if (csts == ~0)
1398 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001399 if ((csts & NVME_CSTS_RDY) == bit)
1400 break;
1401
1402 msleep(100);
1403 if (fatal_signal_pending(current))
1404 return -EINTR;
1405 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001406 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001407 "Device not ready; aborting %s\n", enabled ?
1408 "initialisation" : "reset");
1409 return -ENODEV;
1410 }
1411 }
1412
1413 return ret;
1414}
1415
1416/*
1417 * If the device has been passed off to us in an enabled state, just clear
1418 * the enabled bit. The spec says we should set the 'shutdown notification
1419 * bits', but doing so may cause the device to complete commands to the
1420 * admin queue ... and we don't know what memory that might be pointing at!
1421 */
1422int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1423{
1424 int ret;
1425
1426 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1427 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1428
1429 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1430 if (ret)
1431 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001432
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001433 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001434 msleep(NVME_QUIRK_DELAY_AMOUNT);
1435
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001436 return nvme_wait_ready(ctrl, cap, false);
1437}
Ming Lin576d55d2016-02-10 10:03:32 -08001438EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001439
1440int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1441{
1442 /*
1443 * Default to a 4K page size, with the intention to update this
1444 * path in the future to accomodate architectures with differing
1445 * kernel and IO page sizes.
1446 */
1447 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1448 int ret;
1449
1450 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001451 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001452 "Minimum device page size %u too large for host (%u)\n",
1453 1 << dev_page_min, 1 << page_shift);
1454 return -ENODEV;
1455 }
1456
1457 ctrl->page_size = 1 << page_shift;
1458
1459 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1460 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001461 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001462 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1463 ctrl->ctrl_config |= NVME_CC_ENABLE;
1464
1465 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1466 if (ret)
1467 return ret;
1468 return nvme_wait_ready(ctrl, cap, true);
1469}
Ming Lin576d55d2016-02-10 10:03:32 -08001470EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001471
1472int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1473{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001474 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001475 u32 csts;
1476 int ret;
1477
1478 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1479 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1480
1481 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1482 if (ret)
1483 return ret;
1484
1485 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1486 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1487 break;
1488
1489 msleep(100);
1490 if (fatal_signal_pending(current))
1491 return -EINTR;
1492 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001493 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001494 "Device shutdown incomplete; abort shutdown\n");
1495 return -ENODEV;
1496 }
1497 }
1498
1499 return ret;
1500}
Ming Lin576d55d2016-02-10 10:03:32 -08001501EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001502
Christoph Hellwigda358252016-03-02 18:07:11 +01001503static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1504 struct request_queue *q)
1505{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001506 bool vwc = false;
1507
Christoph Hellwigda358252016-03-02 18:07:11 +01001508 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001509 u32 max_segments =
1510 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1511
Christoph Hellwigda358252016-03-02 18:07:11 +01001512 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001513 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001514 }
Keith Busche6282ae2016-12-19 11:37:50 -05001515 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1516 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001517 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001518 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1519 vwc = true;
1520 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001521}
1522
Jon Derrickdbf86b32017-08-16 09:51:29 +02001523static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1524{
1525 __le64 ts;
1526 int ret;
1527
1528 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1529 return 0;
1530
1531 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1532 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1533 NULL);
1534 if (ret)
1535 dev_warn_once(ctrl->device,
1536 "could not set timestamp (%d)\n", ret);
1537 return ret;
1538}
1539
Keith Busch634b8322017-08-10 11:23:31 +02001540static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001541{
1542 /*
1543 * APST (Autonomous Power State Transition) lets us program a
1544 * table of power state transitions that the controller will
1545 * perform automatically. We configure it with a simple
1546 * heuristic: we are willing to spend at most 2% of the time
1547 * transitioning between power states. Therefore, when running
1548 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001549 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001550 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001551 * the requested maximum latency.
1552 *
1553 * We will not autonomously enter any non-operational state for
1554 * which the total latency exceeds ps_max_latency_us. Users
1555 * can set ps_max_latency_us to zero to turn off APST.
1556 */
1557
1558 unsigned apste;
1559 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001560 u64 max_lat_us = 0;
1561 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001562 int ret;
1563
1564 /*
1565 * If APST isn't supported or if we haven't been initialized yet,
1566 * then don't do anything.
1567 */
1568 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001569 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001570
1571 if (ctrl->npss > 31) {
1572 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001573 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001574 }
1575
1576 table = kzalloc(sizeof(*table), GFP_KERNEL);
1577 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001578 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001579
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001580 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001581 /* Turn off APST. */
1582 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001583 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001584 } else {
1585 __le64 target = cpu_to_le64(0);
1586 int state;
1587
1588 /*
1589 * Walk through all states from lowest- to highest-power.
1590 * According to the spec, lower-numbered states use more
1591 * power. NPSS, despite the name, is the index of the
1592 * lowest-power state, not the number of states.
1593 */
1594 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001595 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001596
1597 if (target)
1598 table->entries[state] = target;
1599
1600 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001601 * Don't allow transitions to the deepest state
1602 * if it's quirked off.
1603 */
1604 if (state == ctrl->npss &&
1605 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1606 continue;
1607
1608 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001609 * Is this state a useful non-operational state for
1610 * higher-power states to autonomously transition to?
1611 */
1612 if (!(ctrl->psd[state].flags &
1613 NVME_PS_FLAGS_NON_OP_STATE))
1614 continue;
1615
Kai-Heng Fengda875912017-06-07 15:25:42 +08001616 exit_latency_us =
1617 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1618 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001619 continue;
1620
Kai-Heng Fengda875912017-06-07 15:25:42 +08001621 total_latency_us =
1622 exit_latency_us +
1623 le32_to_cpu(ctrl->psd[state].entry_lat);
1624
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001625 /*
1626 * This state is good. Use it as the APST idle
1627 * target for higher power states.
1628 */
1629 transition_ms = total_latency_us + 19;
1630 do_div(transition_ms, 20);
1631 if (transition_ms > (1 << 24) - 1)
1632 transition_ms = (1 << 24) - 1;
1633
1634 target = cpu_to_le64((state << 3) |
1635 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001636
1637 if (max_ps == -1)
1638 max_ps = state;
1639
1640 if (total_latency_us > max_lat_us)
1641 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001642 }
1643
1644 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001645
1646 if (max_ps == -1) {
1647 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1648 } else {
1649 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1650 max_ps, max_lat_us, (int)sizeof(*table), table);
1651 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001652 }
1653
1654 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1655 table, sizeof(*table), NULL);
1656 if (ret)
1657 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1658
1659 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001660 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001661}
1662
1663static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1664{
1665 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1666 u64 latency;
1667
1668 switch (val) {
1669 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1670 case PM_QOS_LATENCY_ANY:
1671 latency = U64_MAX;
1672 break;
1673
1674 default:
1675 latency = val;
1676 }
1677
1678 if (ctrl->ps_max_latency_us != latency) {
1679 ctrl->ps_max_latency_us = latency;
1680 nvme_configure_apst(ctrl);
1681 }
1682}
1683
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001684struct nvme_core_quirk_entry {
1685 /*
1686 * NVMe model and firmware strings are padded with spaces. For
1687 * simplicity, strings in the quirk table are padded with NULLs
1688 * instead.
1689 */
1690 u16 vid;
1691 const char *mn;
1692 const char *fr;
1693 unsigned long quirks;
1694};
1695
1696static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001697 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001698 /*
1699 * This Toshiba device seems to die using any APST states. See:
1700 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1701 */
1702 .vid = 0x1179,
1703 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001704 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001705 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001706};
1707
1708/* match is null-terminated but idstr is space-padded. */
1709static bool string_matches(const char *idstr, const char *match, size_t len)
1710{
1711 size_t matchlen;
1712
1713 if (!match)
1714 return true;
1715
1716 matchlen = strlen(match);
1717 WARN_ON_ONCE(matchlen > len);
1718
1719 if (memcmp(idstr, match, matchlen))
1720 return false;
1721
1722 for (; matchlen < len; matchlen++)
1723 if (idstr[matchlen] != ' ')
1724 return false;
1725
1726 return true;
1727}
1728
1729static bool quirk_matches(const struct nvme_id_ctrl *id,
1730 const struct nvme_core_quirk_entry *q)
1731{
1732 return q->vid == le16_to_cpu(id->vid) &&
1733 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1734 string_matches(id->fr, q->fr, sizeof(id->fr));
1735}
1736
Christoph Hellwig180de0072017-06-26 12:39:02 +02001737static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1738{
1739 size_t nqnlen;
1740 int off;
1741
1742 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1743 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1744 strcpy(ctrl->subnqn, id->subnqn);
1745 return;
1746 }
1747
1748 if (ctrl->vs >= NVME_VS(1, 2, 1))
1749 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1750
1751 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1752 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1753 "nqn.2014.08.org.nvmexpress:%4x%4x",
1754 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1755 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1756 off += sizeof(id->sn);
1757 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1758 off += sizeof(id->mn);
1759 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1760}
1761
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001762/*
1763 * Initialize the cached copies of the Identify data and various controller
1764 * register in our nvme_ctrl structure. This should be called as soon as
1765 * the admin queue is fully up and running.
1766 */
1767int nvme_init_identify(struct nvme_ctrl *ctrl)
1768{
1769 struct nvme_id_ctrl *id;
1770 u64 cap;
1771 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001772 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001773 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001774
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001775 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1776 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001777 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001778 return ret;
1779 }
1780
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001781 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1782 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001783 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001784 return ret;
1785 }
1786 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1787
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001788 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001789 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1790
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001791 ret = nvme_identify_ctrl(ctrl, &id);
1792 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001793 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001794 return -EIO;
1795 }
1796
Christoph Hellwig180de0072017-06-26 12:39:02 +02001797 nvme_init_subnqn(ctrl, id);
1798
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001799 if (!ctrl->identified) {
1800 /*
1801 * Check for quirks. Quirk can depend on firmware version,
1802 * so, in principle, the set of quirks present can change
1803 * across a reset. As a possible future enhancement, we
1804 * could re-scan for quirks every time we reinitialize
1805 * the device, but we'd have to make sure that the driver
1806 * behaves intelligently if the quirks change.
1807 */
1808
1809 int i;
1810
1811 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1812 if (quirk_matches(id, &core_quirks[i]))
1813 ctrl->quirks |= core_quirks[i].quirks;
1814 }
1815 }
1816
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001817 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001818 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 -07001819 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1820 }
1821
Scott Bauer8a9ae522017-02-17 13:59:40 +01001822 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001823 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001824 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001825 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001826 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001827 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001828 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1829 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1830 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1831 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001832 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001833 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001834 max_hw_sectors = UINT_MAX;
1835 ctrl->max_hw_sectors =
1836 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001837
Christoph Hellwigda358252016-03-02 18:07:11 +01001838 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001839 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001840 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001841
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001842 if (id->rtd3e) {
1843 /* us -> s */
1844 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1845
1846 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1847 shutdown_timeout, 60);
1848
1849 if (ctrl->shutdown_timeout != shutdown_timeout)
1850 dev_warn(ctrl->device,
1851 "Shutdown timeout set to %u seconds\n",
1852 ctrl->shutdown_timeout);
1853 } else
1854 ctrl->shutdown_timeout = shutdown_timeout;
1855
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001856 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001857 ctrl->apsta = id->apsta;
1858 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001859 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1860 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001861 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 -04001862 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001863 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001864 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001865 }
1866 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001867 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001868 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001869 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1870
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001871 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001872 ctrl->icdoff = le16_to_cpu(id->icdoff);
1873 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1874 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1875 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1876
1877 /*
1878 * In fabrics we need to verify the cntlid matches the
1879 * admin connect
1880 */
Keith Busch634b8322017-08-10 11:23:31 +02001881 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001882 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001883 goto out_free;
1884 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001885
1886 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001887 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001888 "keep-alive support is mandatory for fabrics\n");
1889 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001890 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001891 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001892 } else {
1893 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001894 ctrl->hmpre = le32_to_cpu(id->hmpre);
1895 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001896 ctrl->hmminds = le32_to_cpu(id->hmminds);
1897 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001898 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001899
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001900 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001901
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001902 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001903 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001904 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001905 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1906
Keith Busch634b8322017-08-10 11:23:31 +02001907 ret = nvme_configure_apst(ctrl);
1908 if (ret < 0)
1909 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001910
1911 ret = nvme_configure_timestamp(ctrl);
1912 if (ret < 0)
1913 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001914
1915 ret = nvme_configure_directives(ctrl);
1916 if (ret < 0)
1917 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001918
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001919 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001920
Keith Busch634b8322017-08-10 11:23:31 +02001921 return 0;
1922
1923out_free:
1924 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001925 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001926}
Ming Lin576d55d2016-02-10 10:03:32 -08001927EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001928
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001929static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001930{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001931 struct nvme_ctrl *ctrl =
1932 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001933
Christoph Hellwig999ada22017-10-18 17:09:31 +02001934 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001935 return -EWOULDBLOCK;
1936 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001937 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001938}
1939
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001940static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1941{
1942 struct nvme_ns *ns;
1943 int ret;
1944
1945 mutex_lock(&ctrl->namespaces_mutex);
1946 if (list_empty(&ctrl->namespaces)) {
1947 ret = -ENOTTY;
1948 goto out_unlock;
1949 }
1950
1951 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1952 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001953 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001954 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1955 ret = -EINVAL;
1956 goto out_unlock;
1957 }
1958
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001959 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001960 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1961 kref_get(&ns->kref);
1962 mutex_unlock(&ctrl->namespaces_mutex);
1963
1964 ret = nvme_user_cmd(ctrl, ns, argp);
1965 nvme_put_ns(ns);
1966 return ret;
1967
1968out_unlock:
1969 mutex_unlock(&ctrl->namespaces_mutex);
1970 return ret;
1971}
1972
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001973static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1974 unsigned long arg)
1975{
1976 struct nvme_ctrl *ctrl = file->private_data;
1977 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001978
1979 switch (cmd) {
1980 case NVME_IOCTL_ADMIN_CMD:
1981 return nvme_user_cmd(ctrl, NULL, argp);
1982 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001983 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001984 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001985 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001986 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001987 case NVME_IOCTL_SUBSYS_RESET:
1988 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001989 case NVME_IOCTL_RESCAN:
1990 nvme_queue_scan(ctrl);
1991 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001992 default:
1993 return -ENOTTY;
1994 }
1995}
1996
1997static const struct file_operations nvme_dev_fops = {
1998 .owner = THIS_MODULE,
1999 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002000 .unlocked_ioctl = nvme_dev_ioctl,
2001 .compat_ioctl = nvme_dev_ioctl,
2002};
2003
2004static ssize_t nvme_sysfs_reset(struct device *dev,
2005 struct device_attribute *attr, const char *buf,
2006 size_t count)
2007{
2008 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2009 int ret;
2010
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002011 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002012 if (ret < 0)
2013 return ret;
2014 return count;
2015}
2016static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2017
Keith Busch9ec3bb22016-04-29 15:45:18 -06002018static ssize_t nvme_sysfs_rescan(struct device *dev,
2019 struct device_attribute *attr, const char *buf,
2020 size_t count)
2021{
2022 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2023
2024 nvme_queue_scan(ctrl);
2025 return count;
2026}
2027static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2028
Keith Busch118472a2016-02-18 09:57:48 -07002029static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2030 char *buf)
2031{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002032 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002033 struct nvme_ctrl *ctrl = ns->ctrl;
2034 int serial_len = sizeof(ctrl->serial);
2035 int model_len = sizeof(ctrl->model);
2036
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002037 if (!uuid_is_null(&ns->uuid))
2038 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2039
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002040 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2041 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002042
2043 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2044 return sprintf(buf, "eui.%8phN\n", ns->eui);
2045
Martin Wilck758f3732017-07-20 18:34:02 +02002046 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2047 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002048 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002049 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2050 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002051 model_len--;
2052
2053 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2054 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2055}
2056static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2057
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002058static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2059 char *buf)
2060{
2061 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2062 return sprintf(buf, "%pU\n", ns->nguid);
2063}
2064static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2065
Keith Busch2b9b6e82015-12-22 10:10:45 -07002066static ssize_t uuid_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);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002070
2071 /* For backward compatibility expose the NGUID to userspace if
2072 * we have no UUID set
2073 */
2074 if (uuid_is_null(&ns->uuid)) {
2075 printk_ratelimited(KERN_WARNING
2076 "No UUID available providing old NGUID\n");
2077 return sprintf(buf, "%pU\n", ns->nguid);
2078 }
2079 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002080}
2081static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2082
2083static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2084 char *buf)
2085{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002086 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002087 return sprintf(buf, "%8phd\n", ns->eui);
2088}
2089static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2090
2091static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2092 char *buf)
2093{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002094 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002095 return sprintf(buf, "%d\n", ns->ns_id);
2096}
2097static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2098
2099static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002100 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002101 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002102 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002103 &dev_attr_eui.attr,
2104 &dev_attr_nsid.attr,
2105 NULL,
2106};
2107
Ming Lin1a353d82016-06-13 16:45:24 +02002108static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002109 struct attribute *a, int n)
2110{
2111 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002112 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002113
2114 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002115 if (uuid_is_null(&ns->uuid) ||
2116 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2117 return 0;
2118 }
2119 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002120 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002121 return 0;
2122 }
2123 if (a == &dev_attr_eui.attr) {
2124 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2125 return 0;
2126 }
2127 return a->mode;
2128}
2129
2130static const struct attribute_group nvme_ns_attr_group = {
2131 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002132 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002133};
2134
Ming Lin931e1c22016-02-26 13:24:19 -08002135#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002136static ssize_t field##_show(struct device *dev, \
2137 struct device_attribute *attr, char *buf) \
2138{ \
2139 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2140 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2141} \
2142static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2143
Ming Lin931e1c22016-02-26 13:24:19 -08002144#define nvme_show_int_function(field) \
2145static ssize_t field##_show(struct device *dev, \
2146 struct device_attribute *attr, char *buf) \
2147{ \
2148 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2149 return sprintf(buf, "%d\n", ctrl->field); \
2150} \
2151static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2152
2153nvme_show_str_function(model);
2154nvme_show_str_function(serial);
2155nvme_show_str_function(firmware_rev);
2156nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002157
Ming Lin1a353d82016-06-13 16:45:24 +02002158static ssize_t nvme_sysfs_delete(struct device *dev,
2159 struct device_attribute *attr, const char *buf,
2160 size_t count)
2161{
2162 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2163
2164 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002165 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002166 return count;
2167}
2168static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2169
2170static ssize_t nvme_sysfs_show_transport(struct device *dev,
2171 struct device_attribute *attr,
2172 char *buf)
2173{
2174 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2175
2176 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2177}
2178static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2179
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002180static ssize_t nvme_sysfs_show_state(struct device *dev,
2181 struct device_attribute *attr,
2182 char *buf)
2183{
2184 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2185 static const char *const state_name[] = {
2186 [NVME_CTRL_NEW] = "new",
2187 [NVME_CTRL_LIVE] = "live",
2188 [NVME_CTRL_RESETTING] = "resetting",
2189 [NVME_CTRL_RECONNECTING]= "reconnecting",
2190 [NVME_CTRL_DELETING] = "deleting",
2191 [NVME_CTRL_DEAD] = "dead",
2192 };
2193
2194 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2195 state_name[ctrl->state])
2196 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2197
2198 return sprintf(buf, "unknown state\n");
2199}
2200
2201static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2202
Ming Lin1a353d82016-06-13 16:45:24 +02002203static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2204 struct device_attribute *attr,
2205 char *buf)
2206{
2207 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2208
Christoph Hellwig180de0072017-06-26 12:39:02 +02002209 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002210}
2211static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2212
2213static ssize_t nvme_sysfs_show_address(struct device *dev,
2214 struct device_attribute *attr,
2215 char *buf)
2216{
2217 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2218
2219 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2220}
2221static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2222
Keith Busch779ff7562016-01-12 15:09:31 -07002223static struct attribute *nvme_dev_attrs[] = {
2224 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002225 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002226 &dev_attr_model.attr,
2227 &dev_attr_serial.attr,
2228 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002229 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002230 &dev_attr_delete_controller.attr,
2231 &dev_attr_transport.attr,
2232 &dev_attr_subsysnqn.attr,
2233 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002234 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002235 NULL
2236};
2237
Ming Lin1a353d82016-06-13 16:45:24 +02002238static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2239 struct attribute *a, int n)
2240{
2241 struct device *dev = container_of(kobj, struct device, kobj);
2242 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2243
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002244 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2245 return 0;
2246 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2247 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002248
2249 return a->mode;
2250}
2251
Keith Busch779ff7562016-01-12 15:09:31 -07002252static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002253 .attrs = nvme_dev_attrs,
2254 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002255};
2256
2257static const struct attribute_group *nvme_dev_attr_groups[] = {
2258 &nvme_dev_attrs_group,
2259 NULL,
2260};
2261
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002262static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2263{
2264 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2265 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2266
2267 return nsa->ns_id - nsb->ns_id;
2268}
2269
Keith Busch32f0c4a2016-07-13 11:45:02 -06002270static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002271{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002272 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002273
Keith Busch32f0c4a2016-07-13 11:45:02 -06002274 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002275 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002276 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002277 if (!kref_get_unless_zero(&ns->kref))
2278 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002279 ret = ns;
2280 break;
2281 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002282 if (ns->ns_id > nsid)
2283 break;
2284 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002285 mutex_unlock(&ctrl->namespaces_mutex);
2286 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002287}
2288
Jens Axboef5d11842017-06-27 12:03:06 -06002289static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2290{
2291 struct streams_directive_params s;
2292 int ret;
2293
2294 if (!ctrl->nr_streams)
2295 return 0;
2296
2297 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2298 if (ret)
2299 return ret;
2300
2301 ns->sws = le32_to_cpu(s.sws);
2302 ns->sgs = le16_to_cpu(s.sgs);
2303
2304 if (ns->sws) {
2305 unsigned int bs = 1 << ns->lba_shift;
2306
2307 blk_queue_io_min(ns->queue, bs * ns->sws);
2308 if (ns->sgs)
2309 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2310 }
2311
2312 return 0;
2313}
2314
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002315static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2316{
2317 struct nvme_ns *ns;
2318 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002319 struct nvme_id_ns *id;
2320 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002321 int node = dev_to_node(ctrl->dev);
2322
2323 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2324 if (!ns)
2325 return;
2326
Keith Busch075790e2016-02-24 09:15:53 -07002327 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2328 if (ns->instance < 0)
2329 goto out_free_ns;
2330
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002331 ns->queue = blk_mq_init_queue(ctrl->tagset);
2332 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002333 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002334 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2335 ns->queue->queuedata = ns;
2336 ns->ctrl = ctrl;
2337
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002338 kref_init(&ns->kref);
2339 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002340 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002341
2342 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002343 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002344 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002345
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002346 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002347
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002348 id = nvme_identify_ns(ctrl, nsid);
2349 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002350 goto out_free_queue;
2351
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002352 if (id->ncap == 0)
2353 goto out_free_id;
2354
2355 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2356
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002357 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2358 if (nvme_nvm_register(ns, disk_name, node)) {
2359 dev_warn(ctrl->device, "LightNVM init failure\n");
2360 goto out_free_id;
2361 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002362 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002363
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002364 disk = alloc_disk_node(0, node);
2365 if (!disk)
2366 goto out_free_id;
2367
2368 disk->fops = &nvme_fops;
2369 disk->private_data = ns;
2370 disk->queue = ns->queue;
2371 disk->flags = GENHD_FL_EXT_DEVT;
2372 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2373 ns->disk = disk;
2374
2375 __nvme_revalidate_disk(disk, id);
2376
Keith Busch32f0c4a2016-07-13 11:45:02 -06002377 mutex_lock(&ctrl->namespaces_mutex);
2378 list_add_tail(&ns->list, &ctrl->namespaces);
2379 mutex_unlock(&ctrl->namespaces_mutex);
2380
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002381 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002382
2383 kfree(id);
2384
Dan Williams0d52c7562016-06-15 19:44:20 -07002385 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002386 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2387 &nvme_ns_attr_group))
2388 pr_warn("%s: failed to create sysfs group for identification\n",
2389 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002390 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2391 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2392 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002393 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002394 out_free_id:
2395 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002396 out_free_queue:
2397 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002398 out_release_instance:
2399 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002400 out_free_ns:
2401 kfree(ns);
2402}
2403
2404static void nvme_ns_remove(struct nvme_ns *ns)
2405{
Keith Busch646017a2016-02-24 09:15:54 -07002406 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2407 return;
2408
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002409 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002410 if (blk_get_integrity(ns->disk))
2411 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002412 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2413 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002414 if (ns->ndev)
2415 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002416 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002417 blk_cleanup_queue(ns->queue);
2418 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002419
2420 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002421 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002422 mutex_unlock(&ns->ctrl->namespaces_mutex);
2423
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002424 nvme_put_ns(ns);
2425}
2426
Keith Busch540c8012015-10-22 15:45:06 -06002427static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2428{
2429 struct nvme_ns *ns;
2430
Keith Busch32f0c4a2016-07-13 11:45:02 -06002431 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002432 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002433 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002434 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002435 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002436 } else
2437 nvme_alloc_ns(ctrl, nsid);
2438}
2439
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302440static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2441 unsigned nsid)
2442{
2443 struct nvme_ns *ns, *next;
2444
2445 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2446 if (ns->ns_id > nsid)
2447 nvme_ns_remove(ns);
2448 }
2449}
2450
Keith Busch540c8012015-10-22 15:45:06 -06002451static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2452{
2453 struct nvme_ns *ns;
2454 __le32 *ns_list;
2455 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2456 int ret = 0;
2457
2458 ns_list = kzalloc(0x1000, GFP_KERNEL);
2459 if (!ns_list)
2460 return -ENOMEM;
2461
2462 for (i = 0; i < num_lists; i++) {
2463 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2464 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302465 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002466
2467 for (j = 0; j < min(nn, 1024U); j++) {
2468 nsid = le32_to_cpu(ns_list[j]);
2469 if (!nsid)
2470 goto out;
2471
2472 nvme_validate_ns(ctrl, nsid);
2473
2474 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002475 ns = nvme_find_get_ns(ctrl, prev);
2476 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002477 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002478 nvme_put_ns(ns);
2479 }
Keith Busch540c8012015-10-22 15:45:06 -06002480 }
2481 }
2482 nn -= j;
2483 }
2484 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302485 nvme_remove_invalid_namespaces(ctrl, prev);
2486 free:
Keith Busch540c8012015-10-22 15:45:06 -06002487 kfree(ns_list);
2488 return ret;
2489}
2490
Christoph Hellwig5955be22016-04-26 13:51:59 +02002491static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002492{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002493 unsigned i;
2494
Keith Busch540c8012015-10-22 15:45:06 -06002495 for (i = 1; i <= nn; i++)
2496 nvme_validate_ns(ctrl, i);
2497
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302498 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002499}
2500
Christoph Hellwig5955be22016-04-26 13:51:59 +02002501static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002502{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002503 struct nvme_ctrl *ctrl =
2504 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002505 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002506 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002507
Christoph Hellwig5955be22016-04-26 13:51:59 +02002508 if (ctrl->state != NVME_CTRL_LIVE)
2509 return;
2510
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002511 if (nvme_identify_ctrl(ctrl, &id))
2512 return;
Keith Busch540c8012015-10-22 15:45:06 -06002513
2514 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002515 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002516 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2517 if (!nvme_scan_ns_list(ctrl, nn))
2518 goto done;
2519 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002520 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002521 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002522 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002523 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002524 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002525 kfree(id);
2526}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002527
2528void nvme_queue_scan(struct nvme_ctrl *ctrl)
2529{
2530 /*
2531 * Do not queue new scan work when a controller is reset during
2532 * removal.
2533 */
2534 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002535 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002536}
2537EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002538
Keith Busch32f0c4a2016-07-13 11:45:02 -06002539/*
2540 * This function iterates the namespace list unlocked to allow recovery from
2541 * controller failure. It is up to the caller to ensure the namespace list is
2542 * not modified by scan work while this function is executing.
2543 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002544void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2545{
2546 struct nvme_ns *ns, *next;
2547
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002548 /*
2549 * The dead states indicates the controller was not gracefully
2550 * disconnected. In that case, we won't be able to flush any data while
2551 * removing the namespaces' disks; fail all the queues now to avoid
2552 * potentially having to clean up the failed sync later.
2553 */
2554 if (ctrl->state == NVME_CTRL_DEAD)
2555 nvme_kill_queues(ctrl);
2556
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002557 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2558 nvme_ns_remove(ns);
2559}
Ming Lin576d55d2016-02-10 10:03:32 -08002560EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002561
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002562static void nvme_async_event_work(struct work_struct *work)
2563{
2564 struct nvme_ctrl *ctrl =
2565 container_of(work, struct nvme_ctrl, async_event_work);
2566
2567 spin_lock_irq(&ctrl->lock);
James Smartcd482822017-09-14 11:03:09 -07002568 while (ctrl->state == NVME_CTRL_LIVE && ctrl->event_limit > 0) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002569 int aer_idx = --ctrl->event_limit;
2570
2571 spin_unlock_irq(&ctrl->lock);
2572 ctrl->ops->submit_async_event(ctrl, aer_idx);
2573 spin_lock_irq(&ctrl->lock);
2574 }
2575 spin_unlock_irq(&ctrl->lock);
2576}
2577
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302578static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2579{
2580
2581 u32 csts;
2582
2583 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2584 return false;
2585
2586 if (csts == ~0)
2587 return false;
2588
2589 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2590}
2591
2592static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2593{
2594 struct nvme_command c = { };
2595 struct nvme_fw_slot_info_log *log;
2596
2597 log = kmalloc(sizeof(*log), GFP_KERNEL);
2598 if (!log)
2599 return;
2600
2601 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302602 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302603 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2604
2605 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2606 dev_warn(ctrl->device,
2607 "Get FW SLOT INFO log error\n");
2608 kfree(log);
2609}
2610
2611static void nvme_fw_act_work(struct work_struct *work)
2612{
2613 struct nvme_ctrl *ctrl = container_of(work,
2614 struct nvme_ctrl, fw_act_work);
2615 unsigned long fw_act_timeout;
2616
2617 if (ctrl->mtfa)
2618 fw_act_timeout = jiffies +
2619 msecs_to_jiffies(ctrl->mtfa * 100);
2620 else
2621 fw_act_timeout = jiffies +
2622 msecs_to_jiffies(admin_timeout * 1000);
2623
2624 nvme_stop_queues(ctrl);
2625 while (nvme_ctrl_pp_status(ctrl)) {
2626 if (time_after(jiffies, fw_act_timeout)) {
2627 dev_warn(ctrl->device,
2628 "Fw activation timeout, reset controller\n");
2629 nvme_reset_ctrl(ctrl);
2630 break;
2631 }
2632 msleep(100);
2633 }
2634
2635 if (ctrl->state != NVME_CTRL_LIVE)
2636 return;
2637
2638 nvme_start_queues(ctrl);
2639 /* read FW slot informationi to clear the AER*/
2640 nvme_get_fw_slot_info(ctrl);
2641}
2642
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002643void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2644 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002645{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002646 u32 result = le32_to_cpu(res->u32);
2647 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002648
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002649 switch (le16_to_cpu(status) >> 1) {
2650 case NVME_SC_SUCCESS:
2651 done = false;
2652 /*FALLTHRU*/
2653 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002654 ++ctrl->event_limit;
James Smartcd482822017-09-14 11:03:09 -07002655 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002656 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002657 break;
2658 default:
2659 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002660 }
2661
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002662 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002663 return;
2664
2665 switch (result & 0xff07) {
2666 case NVME_AER_NOTICE_NS_CHANGED:
2667 dev_info(ctrl->device, "rescanning\n");
2668 nvme_queue_scan(ctrl);
2669 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302670 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002671 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302672 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002673 default:
2674 dev_warn(ctrl->device, "async event result %08x\n", result);
2675 }
2676}
2677EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2678
2679void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2680{
2681 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002682 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002683}
2684EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2685
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002686void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002687{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002688 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002689 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002690 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302691 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002692}
2693EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002694
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002695void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2696{
2697 if (ctrl->kato)
2698 nvme_start_keep_alive(ctrl);
2699
2700 if (ctrl->queue_count > 1) {
2701 nvme_queue_scan(ctrl);
2702 nvme_queue_async_events(ctrl);
2703 nvme_start_queues(ctrl);
2704 }
2705}
2706EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2707
2708void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2709{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002710 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002711}
Ming Lin576d55d2016-02-10 10:03:32 -08002712EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002713
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002714static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002715{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002716 struct nvme_ctrl *ctrl =
2717 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002718
Christoph Hellwig9843f682017-10-18 13:10:01 +02002719 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002720 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002721
2722 ctrl->ops->free_ctrl(ctrl);
2723}
2724
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002725/*
2726 * Initialize a NVMe controller structures. This needs to be called during
2727 * earliest initialization so that we have the initialized structured around
2728 * during probing.
2729 */
2730int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2731 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2732{
2733 int ret;
2734
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002735 ctrl->state = NVME_CTRL_NEW;
2736 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002737 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002738 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002739 ctrl->dev = dev;
2740 ctrl->ops = ops;
2741 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002742 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002743 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302744 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002745 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002746
Christoph Hellwig9843f682017-10-18 13:10:01 +02002747 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2748 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002749 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002750 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002751
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002752 device_initialize(&ctrl->ctrl_device);
2753 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002754 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002755 ctrl->device->class = nvme_class;
2756 ctrl->device->parent = ctrl->dev;
2757 ctrl->device->groups = nvme_dev_attr_groups;
2758 ctrl->device->release = nvme_free_ctrl;
2759 dev_set_drvdata(ctrl->device, ctrl);
2760 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
2761 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002762 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002763
2764 cdev_init(&ctrl->cdev, &nvme_dev_fops);
2765 ctrl->cdev.owner = ops->module;
2766 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002767 if (ret)
2768 goto out_free_name;
2769
Keith Busch075790e2016-02-24 09:15:53 -07002770 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002771
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002772 /*
2773 * Initialize latency tolerance controls. The sysfs files won't
2774 * be visible to userspace unless the device actually supports APST.
2775 */
2776 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2777 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2778 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2779
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002780 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002781out_free_name:
2782 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002783out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02002784 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002785out:
2786 return ret;
2787}
Ming Lin576d55d2016-02-10 10:03:32 -08002788EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002789
Keith Busch69d9a992016-02-24 09:15:56 -07002790/**
2791 * nvme_kill_queues(): Ends all namespace queues
2792 * @ctrl: the dead controller that needs to end
2793 *
2794 * Call this function when the driver determines it is unable to get the
2795 * controller in a state capable of servicing IO.
2796 */
2797void nvme_kill_queues(struct nvme_ctrl *ctrl)
2798{
2799 struct nvme_ns *ns;
2800
Keith Busch32f0c4a2016-07-13 11:45:02 -06002801 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002802
Ming Lei443bd902017-06-19 10:21:08 +08002803 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002804 if (ctrl->admin_q)
2805 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002806
Keith Busch32f0c4a2016-07-13 11:45:02 -06002807 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002808 /*
2809 * Revalidating a dead namespace sets capacity to 0. This will
2810 * end buffered writers dirtying pages that can't be synced.
2811 */
Keith Buschf33447b2017-02-10 18:15:51 -05002812 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2813 continue;
2814 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002815 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002816
Ming Lei443bd902017-06-19 10:21:08 +08002817 /* Forcibly unquiesce queues to avoid blocking dispatch */
2818 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002819 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002820 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002821}
Linus Torvalds237045f2016-03-18 17:13:31 -07002822EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002823
Keith Busch302ad8c2017-03-01 14:22:12 -05002824void nvme_unfreeze(struct nvme_ctrl *ctrl)
2825{
2826 struct nvme_ns *ns;
2827
2828 mutex_lock(&ctrl->namespaces_mutex);
2829 list_for_each_entry(ns, &ctrl->namespaces, list)
2830 blk_mq_unfreeze_queue(ns->queue);
2831 mutex_unlock(&ctrl->namespaces_mutex);
2832}
2833EXPORT_SYMBOL_GPL(nvme_unfreeze);
2834
2835void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2836{
2837 struct nvme_ns *ns;
2838
2839 mutex_lock(&ctrl->namespaces_mutex);
2840 list_for_each_entry(ns, &ctrl->namespaces, list) {
2841 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2842 if (timeout <= 0)
2843 break;
2844 }
2845 mutex_unlock(&ctrl->namespaces_mutex);
2846}
2847EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2848
2849void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2850{
2851 struct nvme_ns *ns;
2852
2853 mutex_lock(&ctrl->namespaces_mutex);
2854 list_for_each_entry(ns, &ctrl->namespaces, list)
2855 blk_mq_freeze_queue_wait(ns->queue);
2856 mutex_unlock(&ctrl->namespaces_mutex);
2857}
2858EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2859
2860void nvme_start_freeze(struct nvme_ctrl *ctrl)
2861{
2862 struct nvme_ns *ns;
2863
2864 mutex_lock(&ctrl->namespaces_mutex);
2865 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002866 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002867 mutex_unlock(&ctrl->namespaces_mutex);
2868}
2869EXPORT_SYMBOL_GPL(nvme_start_freeze);
2870
Keith Busch25646262016-01-04 09:10:57 -07002871void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002872{
2873 struct nvme_ns *ns;
2874
Keith Busch32f0c4a2016-07-13 11:45:02 -06002875 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002876 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002877 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002878 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002879}
Ming Lin576d55d2016-02-10 10:03:32 -08002880EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002881
Keith Busch25646262016-01-04 09:10:57 -07002882void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002883{
2884 struct nvme_ns *ns;
2885
Keith Busch32f0c4a2016-07-13 11:45:02 -06002886 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002887 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002888 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002889 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002890}
Ming Lin576d55d2016-02-10 10:03:32 -08002891EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002892
Sagi Grimberg31b84462017-10-11 12:53:07 +03002893int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2894{
2895 if (!ctrl->ops->reinit_request)
2896 return 0;
2897
2898 return blk_mq_tagset_iter(set, set->driver_data,
2899 ctrl->ops->reinit_request);
2900}
2901EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
2902
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002903int __init nvme_core_init(void)
2904{
2905 int result;
2906
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002907 nvme_wq = alloc_workqueue("nvme-wq",
2908 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2909 if (!nvme_wq)
2910 return -ENOMEM;
2911
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002912 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002913 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002914 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002915
2916 nvme_class = class_create(THIS_MODULE, "nvme");
2917 if (IS_ERR(nvme_class)) {
2918 result = PTR_ERR(nvme_class);
2919 goto unregister_chrdev;
2920 }
2921
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002922 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002923
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002924unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002925 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002926destroy_wq:
2927 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002928 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002929}
2930
2931void nvme_core_exit(void)
2932{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002933 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002934 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002935 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002936}
Ming Lin576d55d2016-02-10 10:03:32 -08002937
2938MODULE_LICENSE("GPL");
2939MODULE_VERSION("1.0");
2940module_init(nvme_core_init);
2941module_exit(nvme_core_exit);