blob: 78dc6f624d52fe3ed0091a8fcf6dfb8772d932f4 [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 Hellwigab9e00c2017-11-09 13:48:55 +010071static DEFINE_IDA(nvme_subsystems_ida);
72static LIST_HEAD(nvme_subsystems);
73static DEFINE_MUTEX(nvme_subsystems_lock);
74
Christoph Hellwig9843f682017-10-18 13:10:01 +020075static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020076static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010077static struct class *nvme_class;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010078static struct class *nvme_subsys_class;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010079
Keith Busch84fef622017-11-07 10:28:32 -070080static void nvme_ns_remove(struct nvme_ns *ns);
81static int nvme_revalidate_disk(struct gendisk *disk);
82
Arnav Dawnb6dccf72017-07-12 16:10:40 +053083static __le32 nvme_get_log_dw10(u8 lid, size_t size)
84{
85 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
86}
87
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020088int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
89{
90 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
91 return -EBUSY;
92 if (!queue_work(nvme_wq, &ctrl->reset_work))
93 return -EBUSY;
94 return 0;
95}
96EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
97
98static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
99{
100 int ret;
101
102 ret = nvme_reset_ctrl(ctrl);
103 if (!ret)
104 flush_work(&ctrl->reset_work);
105 return ret;
106}
107
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200108static void nvme_delete_ctrl_work(struct work_struct *work)
109{
110 struct nvme_ctrl *ctrl =
111 container_of(work, struct nvme_ctrl, delete_work);
112
Sagi Grimberg40546372017-10-29 14:21:02 +0200113 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200114 nvme_stop_ctrl(ctrl);
115 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200116 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200117 nvme_uninit_ctrl(ctrl);
118 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200119}
120
121int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
122{
123 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
124 return -EBUSY;
125 if (!queue_work(nvme_wq, &ctrl->delete_work))
126 return -EBUSY;
127 return 0;
128}
129EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
130
131int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
132{
133 int ret = 0;
134
135 /*
136 * Keep a reference until the work is flushed since ->delete_ctrl
137 * can free the controller.
138 */
139 nvme_get_ctrl(ctrl);
140 ret = nvme_delete_ctrl(ctrl);
141 if (!ret)
142 flush_work(&ctrl->delete_work);
143 nvme_put_ctrl(ctrl);
144 return ret;
145}
146EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
147
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100148static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
149{
150 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
151}
152
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200153static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200154{
155 switch (nvme_req(req)->status & 0x7ff) {
156 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200157 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200158 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200159 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200160 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200161 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200162 case NVME_SC_WRITE_FAULT:
163 case NVME_SC_READ_ERROR:
164 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200165 case NVME_SC_ACCESS_DENIED:
166 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200167 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200168 case NVME_SC_GUARD_CHECK:
169 case NVME_SC_APPTAG_CHECK:
170 case NVME_SC_REFTAG_CHECK:
171 case NVME_SC_INVALID_PI:
172 return BLK_STS_PROTECTION;
173 case NVME_SC_RESERVATION_CONFLICT:
174 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200175 default:
176 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200177 }
178}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200179
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200180static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200181{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200182 if (blk_noretry_request(req))
183 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200184 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200185 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200186 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200187 return false;
Christoph Hellwige54b0642017-11-02 21:28:51 +0300188 if (blk_queue_dying(req->q))
189 return false;
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200190 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200191}
192
193void nvme_complete_rq(struct request *req)
194{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200195 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
196 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300197 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200198 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200199 }
200
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200201 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200202}
203EXPORT_SYMBOL_GPL(nvme_complete_rq);
204
Ming Linc55a2fd2016-05-18 14:05:02 -0700205void nvme_cancel_request(struct request *req, void *data, bool reserved)
206{
Ming Linc55a2fd2016-05-18 14:05:02 -0700207 if (!blk_mq_request_started(req))
208 return;
209
210 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
211 "Cancelling I/O %d", req->tag);
212
Christoph Hellwige54b0642017-11-02 21:28:51 +0300213 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200214 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200215
Ming Linc55a2fd2016-05-18 14:05:02 -0700216}
217EXPORT_SYMBOL_GPL(nvme_cancel_request);
218
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200219bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
220 enum nvme_ctrl_state new_state)
221{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300222 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200223 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200224 bool changed = false;
225
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200226 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300227
228 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200229 switch (new_state) {
230 case NVME_CTRL_LIVE:
231 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200232 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200233 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900234 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200235 changed = true;
236 /* FALLTHRU */
237 default:
238 break;
239 }
240 break;
241 case NVME_CTRL_RESETTING:
242 switch (old_state) {
243 case NVME_CTRL_NEW:
244 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900245 changed = true;
246 /* FALLTHRU */
247 default:
248 break;
249 }
250 break;
251 case NVME_CTRL_RECONNECTING:
252 switch (old_state) {
253 case NVME_CTRL_LIVE:
James Smart3cec7f92017-10-25 16:43:13 -0700254 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200255 changed = true;
256 /* FALLTHRU */
257 default:
258 break;
259 }
260 break;
261 case NVME_CTRL_DELETING:
262 switch (old_state) {
263 case NVME_CTRL_LIVE:
264 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900265 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200266 changed = true;
267 /* FALLTHRU */
268 default:
269 break;
270 }
271 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600272 case NVME_CTRL_DEAD:
273 switch (old_state) {
274 case NVME_CTRL_DELETING:
275 changed = true;
276 /* FALLTHRU */
277 default:
278 break;
279 }
280 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200281 default:
282 break;
283 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200284
285 if (changed)
286 ctrl->state = new_state;
287
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200288 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300289
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200290 return changed;
291}
292EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
293
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100294static void nvme_free_ns(struct kref *kref)
295{
296 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
297
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200298 if (ns->ndev)
299 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100300
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100301 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700302 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
303 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100304 kfree(ns);
305}
306
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100307static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100308{
309 kref_put(&ns->kref, nvme_free_ns);
310}
311
Christoph Hellwig41609822015-11-20 09:00:02 +0100312struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800313 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100314{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100315 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100316 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100317
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200318 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100319 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200320 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100321 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200322 qid ? qid - 1 : 0);
323 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100324 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100325 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100326
Christoph Hellwig21d34712015-11-26 09:08:36 +0100327 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800328 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100329
Christoph Hellwig41609822015-11-20 09:00:02 +0100330 return req;
331}
Ming Lin576d55d2016-02-10 10:03:32 -0800332EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100333
Jens Axboef5d11842017-06-27 12:03:06 -0600334static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
335{
336 struct nvme_command c;
337
338 memset(&c, 0, sizeof(c));
339
340 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530341 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600342 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
343 c.directive.dtype = NVME_DIR_IDENTIFY;
344 c.directive.tdtype = NVME_DIR_STREAMS;
345 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
346
347 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
348}
349
350static int nvme_disable_streams(struct nvme_ctrl *ctrl)
351{
352 return nvme_toggle_streams(ctrl, false);
353}
354
355static int nvme_enable_streams(struct nvme_ctrl *ctrl)
356{
357 return nvme_toggle_streams(ctrl, true);
358}
359
360static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
361 struct streams_directive_params *s, u32 nsid)
362{
363 struct nvme_command c;
364
365 memset(&c, 0, sizeof(c));
366 memset(s, 0, sizeof(*s));
367
368 c.directive.opcode = nvme_admin_directive_recv;
369 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700370 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600371 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
372 c.directive.dtype = NVME_DIR_STREAMS;
373
374 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
375}
376
377static int nvme_configure_directives(struct nvme_ctrl *ctrl)
378{
379 struct streams_directive_params s;
380 int ret;
381
382 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
383 return 0;
384 if (!streams)
385 return 0;
386
387 ret = nvme_enable_streams(ctrl);
388 if (ret)
389 return ret;
390
Arnav Dawn62346ea2017-07-12 16:11:53 +0530391 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600392 if (ret)
393 return ret;
394
395 ctrl->nssa = le16_to_cpu(s.nssa);
396 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
397 dev_info(ctrl->device, "too few streams (%u) available\n",
398 ctrl->nssa);
399 nvme_disable_streams(ctrl);
400 return 0;
401 }
402
403 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
404 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
405 return 0;
406}
407
408/*
409 * Check if 'req' has a write hint associated with it. If it does, assign
410 * a valid namespace stream to the write.
411 */
412static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
413 struct request *req, u16 *control,
414 u32 *dsmgmt)
415{
416 enum rw_hint streamid = req->write_hint;
417
418 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
419 streamid = 0;
420 else {
421 streamid--;
422 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
423 return;
424
425 *control |= NVME_RW_DTYPE_STREAMS;
426 *dsmgmt |= streamid << 16;
427 }
428
429 if (streamid < ARRAY_SIZE(req->q->write_hints))
430 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
431}
432
Ming Lin8093f7c2016-04-12 13:10:14 -0600433static inline void nvme_setup_flush(struct nvme_ns *ns,
434 struct nvme_command *cmnd)
435{
436 memset(cmnd, 0, sizeof(*cmnd));
437 cmnd->common.opcode = nvme_cmd_flush;
438 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
439}
440
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200441static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600442 struct nvme_command *cmnd)
443{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100444 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600445 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100446 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600447
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100448 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600449 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200450 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600451
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100452 __rq_for_each_bio(bio, req) {
453 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
454 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
455
456 range[n].cattr = cpu_to_le32(0);
457 range[n].nlb = cpu_to_le32(nlb);
458 range[n].slba = cpu_to_le64(slba);
459 n++;
460 }
461
462 if (WARN_ON_ONCE(n != segments)) {
463 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200464 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100465 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600466
467 memset(cmnd, 0, sizeof(*cmnd));
468 cmnd->dsm.opcode = nvme_cmd_dsm;
469 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200470 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600471 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
472
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700473 req->special_vec.bv_page = virt_to_page(range);
474 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100475 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700476 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600477
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200478 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600479}
Ming Lin8093f7c2016-04-12 13:10:14 -0600480
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200481static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
482 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600483{
Jens Axboef5d11842017-06-27 12:03:06 -0600484 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600485 u16 control = 0;
486 u32 dsmgmt = 0;
487
488 if (req->cmd_flags & REQ_FUA)
489 control |= NVME_RW_FUA;
490 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
491 control |= NVME_RW_LR;
492
493 if (req->cmd_flags & REQ_RAHEAD)
494 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
495
496 memset(cmnd, 0, sizeof(*cmnd));
497 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600498 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
499 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
500 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
501
Jens Axboef5d11842017-06-27 12:03:06 -0600502 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
503 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
504
Ming Lin8093f7c2016-04-12 13:10:14 -0600505 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100506 /*
507 * If formated with metadata, the block layer always provides a
508 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
509 * we enable the PRACT bit for protection information or set the
510 * namespace capacity to zero to prevent any I/O.
511 */
512 if (!blk_integrity_rq(req)) {
513 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
514 return BLK_STS_NOTSUPP;
515 control |= NVME_RW_PRINFO_PRACT;
516 }
517
Ming Lin8093f7c2016-04-12 13:10:14 -0600518 switch (ns->pi_type) {
519 case NVME_NS_DPS_PI_TYPE3:
520 control |= NVME_RW_PRINFO_PRCHK_GUARD;
521 break;
522 case NVME_NS_DPS_PI_TYPE1:
523 case NVME_NS_DPS_PI_TYPE2:
524 control |= NVME_RW_PRINFO_PRCHK_GUARD |
525 NVME_RW_PRINFO_PRCHK_REF;
526 cmnd->rw.reftag = cpu_to_le32(
527 nvme_block_nr(ns, blk_rq_pos(req)));
528 break;
529 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600530 }
531
532 cmnd->rw.control = cpu_to_le16(control);
533 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200534 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600535}
536
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200537blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600538 struct nvme_command *cmd)
539{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200540 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600541
Christoph Hellwig987f6992017-04-05 19:18:08 +0200542 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200543 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200544 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200545 req->rq_flags |= RQF_DONTPREP;
546 }
547
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100548 switch (req_op(req)) {
549 case REQ_OP_DRV_IN:
550 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800551 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100552 break;
553 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600554 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100555 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200556 case REQ_OP_WRITE_ZEROES:
557 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100558 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600559 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100560 break;
561 case REQ_OP_READ:
562 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200563 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100564 break;
565 default:
566 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200567 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100568 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600569
James Smart721b3912016-10-21 23:33:34 +0300570 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600571 return ret;
572}
573EXPORT_SYMBOL_GPL(nvme_setup_cmd);
574
Christoph Hellwig41609822015-11-20 09:00:02 +0100575/*
576 * Returns 0 on success. If the result is negative, it's a Linux error code;
577 * if the result is positive, it's an NVM Express status code
578 */
579int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800580 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800581 unsigned timeout, int qid, int at_head,
582 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100583{
584 struct request *req;
585 int ret;
586
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200587 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100588 if (IS_ERR(req))
589 return PTR_ERR(req);
590
591 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
592
Christoph Hellwig21d34712015-11-26 09:08:36 +0100593 if (buffer && bufflen) {
594 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
595 if (ret)
596 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100597 }
598
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200599 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800600 if (result)
601 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200602 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
603 ret = -EINTR;
604 else
605 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100606 out:
607 blk_mq_free_request(req);
608 return ret;
609}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200610EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100611
612int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
613 void *buffer, unsigned bufflen)
614{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200615 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
616 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100617}
Ming Lin576d55d2016-02-10 10:03:32 -0800618EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100619
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400620static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
621 unsigned len, u32 seed, bool write)
622{
623 struct bio_integrity_payload *bip;
624 int ret = -ENOMEM;
625 void *buf;
626
627 buf = kmalloc(len, GFP_KERNEL);
628 if (!buf)
629 goto out;
630
631 ret = -EFAULT;
632 if (write && copy_from_user(buf, ubuf, len))
633 goto out_free_meta;
634
635 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
636 if (IS_ERR(bip)) {
637 ret = PTR_ERR(bip);
638 goto out_free_meta;
639 }
640
641 bip->bip_iter.bi_size = len;
642 bip->bip_iter.bi_sector = seed;
643 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
644 offset_in_page(buf));
645 if (ret == len)
646 return buf;
647 ret = -ENOMEM;
648out_free_meta:
649 kfree(buf);
650out:
651 return ERR_PTR(ret);
652}
653
Keith Busch63263d62017-08-29 17:46:04 -0400654static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400655 struct nvme_command *cmd, void __user *ubuffer,
656 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
657 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100658{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200659 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600660 struct nvme_ns *ns = q->queuedata;
661 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100662 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600663 struct bio *bio = NULL;
664 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100665 int ret;
666
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200667 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100668 if (IS_ERR(req))
669 return PTR_ERR(req);
670
671 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
672
673 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100674 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
675 GFP_KERNEL);
676 if (ret)
677 goto out;
678 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200679 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400680 if (disk && meta_buffer && meta_len) {
681 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
682 meta_seed, write);
683 if (IS_ERR(meta)) {
684 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600685 goto out_unmap;
686 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600687 }
688 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400689
Keith Busch0b7f1f22015-10-23 09:47:28 -0600690 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200691 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
692 ret = -EINTR;
693 else
694 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100695 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800696 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600697 if (meta && !ret && !write) {
698 if (copy_to_user(meta_buffer, meta, meta_len))
699 ret = -EFAULT;
700 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600701 kfree(meta);
702 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200703 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600704 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100705 out:
706 blk_mq_free_request(req);
707 return ret;
708}
709
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200710static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200711{
712 struct nvme_ctrl *ctrl = rq->end_io_data;
713
714 blk_mq_free_request(rq);
715
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200716 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200717 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200718 "failed nvme_keep_alive_end_io error=%d\n",
719 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200720 return;
721 }
722
723 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
724}
725
726static int nvme_keep_alive(struct nvme_ctrl *ctrl)
727{
728 struct nvme_command c;
729 struct request *rq;
730
731 memset(&c, 0, sizeof(c));
732 c.common.opcode = nvme_admin_keep_alive;
733
734 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
735 NVME_QID_ANY);
736 if (IS_ERR(rq))
737 return PTR_ERR(rq);
738
739 rq->timeout = ctrl->kato * HZ;
740 rq->end_io_data = ctrl;
741
742 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
743
744 return 0;
745}
746
747static void nvme_keep_alive_work(struct work_struct *work)
748{
749 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
750 struct nvme_ctrl, ka_work);
751
752 if (nvme_keep_alive(ctrl)) {
753 /* allocation failure, reset the controller */
754 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200755 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200756 return;
757 }
758}
759
760void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
761{
762 if (unlikely(ctrl->kato == 0))
763 return;
764
765 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
766 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
767}
768EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
769
770void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
771{
772 if (unlikely(ctrl->kato == 0))
773 return;
774
775 cancel_delayed_work_sync(&ctrl->ka_work);
776}
777EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
778
Keith Busch3f7f25a92017-06-20 15:09:56 -0400779static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100780{
781 struct nvme_command c = { };
782 int error;
783
784 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
785 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200786 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100787
788 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
789 if (!*id)
790 return -ENOMEM;
791
792 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
793 sizeof(struct nvme_id_ctrl));
794 if (error)
795 kfree(*id);
796 return error;
797}
798
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200799static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
800 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200801{
802 struct nvme_command c = { };
803 int status;
804 void *data;
805 int pos;
806 int len;
807
808 c.identify.opcode = nvme_admin_identify;
809 c.identify.nsid = cpu_to_le32(nsid);
810 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
811
812 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
813 if (!data)
814 return -ENOMEM;
815
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200816 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200817 NVME_IDENTIFY_DATA_SIZE);
818 if (status)
819 goto free_data;
820
821 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
822 struct nvme_ns_id_desc *cur = data + pos;
823
824 if (cur->nidl == 0)
825 break;
826
827 switch (cur->nidt) {
828 case NVME_NIDT_EUI64:
829 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200830 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200831 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
832 cur->nidl);
833 goto free_data;
834 }
835 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200836 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200837 break;
838 case NVME_NIDT_NGUID:
839 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200840 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200841 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
842 cur->nidl);
843 goto free_data;
844 }
845 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200846 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200847 break;
848 case NVME_NIDT_UUID:
849 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200850 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200851 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
852 cur->nidl);
853 goto free_data;
854 }
855 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200856 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200857 break;
858 default:
859 /* Skip unnkown types */
860 len = cur->nidl;
861 break;
862 }
863
864 len += sizeof(*cur);
865 }
866free_data:
867 kfree(data);
868 return status;
869}
870
Keith Busch540c8012015-10-22 15:45:06 -0600871static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
872{
873 struct nvme_command c = { };
874
875 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200876 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600877 c.identify.nsid = cpu_to_le32(nsid);
878 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
879}
880
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200881static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
882 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100883{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200884 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100885 struct nvme_command c = { };
886 int error;
887
888 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200889 c.identify.opcode = nvme_admin_identify;
890 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200891 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100892
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200893 id = kmalloc(sizeof(*id), GFP_KERNEL);
894 if (!id)
895 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100896
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200897 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
898 if (error) {
899 dev_warn(ctrl->device, "Identify namespace failed\n");
900 kfree(id);
901 return NULL;
902 }
903
904 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100905}
906
Keith Busch3f7f25a92017-06-20 15:09:56 -0400907static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700908 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100909{
910 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800911 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100912 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100913
914 memset(&c, 0, sizeof(c));
915 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100916 c.features.fid = cpu_to_le32(fid);
917 c.features.dword11 = cpu_to_le32(dword11);
918
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800919 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700920 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700921 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800922 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100923 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100924}
925
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100926int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
927{
928 u32 q_count = (*count - 1) | ((*count - 1) << 16);
929 u32 result;
930 int status, nr_io_queues;
931
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700932 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100933 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200934 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100935 return status;
936
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200937 /*
938 * Degraded controllers might return an error when setting the queue
939 * count. We still want to be able to bring them online and offer
940 * access to the admin queue, as that might be only way to fix them up.
941 */
942 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200943 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200944 *count = 0;
945 } else {
946 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
947 *count = min(*count, nr_io_queues);
948 }
949
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100950 return 0;
951}
Ming Lin576d55d2016-02-10 10:03:32 -0800952EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100953
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100954static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
955{
956 struct nvme_user_io io;
957 struct nvme_command c;
958 unsigned length, meta_len;
959 void __user *metadata;
960
961 if (copy_from_user(&io, uio, sizeof(io)))
962 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700963 if (io.flags)
964 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100965
966 switch (io.opcode) {
967 case nvme_cmd_write:
968 case nvme_cmd_read:
969 case nvme_cmd_compare:
970 break;
971 default:
972 return -EINVAL;
973 }
974
975 length = (io.nblocks + 1) << ns->lba_shift;
976 meta_len = (io.nblocks + 1) * ns->ms;
977 metadata = (void __user *)(uintptr_t)io.metadata;
978
979 if (ns->ext) {
980 length += meta_len;
981 meta_len = 0;
982 } else if (meta_len) {
983 if ((io.metadata & 3) || !io.metadata)
984 return -EINVAL;
985 }
986
987 memset(&c, 0, sizeof(c));
988 c.rw.opcode = io.opcode;
989 c.rw.flags = io.flags;
990 c.rw.nsid = cpu_to_le32(ns->ns_id);
991 c.rw.slba = cpu_to_le64(io.slba);
992 c.rw.length = cpu_to_le16(io.nblocks);
993 c.rw.control = cpu_to_le16(io.control);
994 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
995 c.rw.reftag = cpu_to_le32(io.reftag);
996 c.rw.apptag = cpu_to_le16(io.apptag);
997 c.rw.appmask = cpu_to_le16(io.appmask);
998
Keith Busch63263d62017-08-29 17:46:04 -0400999 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001000 (void __user *)(uintptr_t)io.addr, length,
1001 metadata, meta_len, io.slba, NULL, 0);
1002}
1003
Keith Busch84fef622017-11-07 10:28:32 -07001004static u32 nvme_known_admin_effects(u8 opcode)
1005{
1006 switch (opcode) {
1007 case nvme_admin_format_nvm:
1008 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1009 NVME_CMD_EFFECTS_CSE_MASK;
1010 case nvme_admin_sanitize_nvm:
1011 return NVME_CMD_EFFECTS_CSE_MASK;
1012 default:
1013 break;
1014 }
1015 return 0;
1016}
1017
1018static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1019 u8 opcode)
1020{
1021 u32 effects = 0;
1022
1023 if (ns) {
1024 if (ctrl->effects)
1025 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1026 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1027 dev_warn(ctrl->device,
1028 "IO command:%02x has unhandled effects:%08x\n",
1029 opcode, effects);
1030 return 0;
1031 }
1032
1033 if (ctrl->effects)
1034 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1035 else
1036 effects = nvme_known_admin_effects(opcode);
1037
1038 /*
1039 * For simplicity, IO to all namespaces is quiesced even if the command
1040 * effects say only one namespace is affected.
1041 */
1042 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1043 nvme_start_freeze(ctrl);
1044 nvme_wait_freeze(ctrl);
1045 }
1046 return effects;
1047}
1048
1049static void nvme_update_formats(struct nvme_ctrl *ctrl)
1050{
1051 struct nvme_ns *ns;
1052
1053 mutex_lock(&ctrl->namespaces_mutex);
1054 list_for_each_entry(ns, &ctrl->namespaces, list) {
1055 if (ns->disk && nvme_revalidate_disk(ns->disk))
1056 nvme_ns_remove(ns);
1057 }
1058 mutex_unlock(&ctrl->namespaces_mutex);
1059}
1060
1061static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1062{
1063 /*
1064 * Revalidate LBA changes prior to unfreezing. This is necessary to
1065 * prevent memory corruption if a logical block size was changed by
1066 * this command.
1067 */
1068 if (effects & NVME_CMD_EFFECTS_LBCC)
1069 nvme_update_formats(ctrl);
1070 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1071 nvme_unfreeze(ctrl);
1072 if (effects & NVME_CMD_EFFECTS_CCC)
1073 nvme_init_identify(ctrl);
1074 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1075 nvme_queue_scan(ctrl);
1076}
1077
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001078static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001079 struct nvme_passthru_cmd __user *ucmd)
1080{
1081 struct nvme_passthru_cmd cmd;
1082 struct nvme_command c;
1083 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001084 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001085 int status;
1086
1087 if (!capable(CAP_SYS_ADMIN))
1088 return -EACCES;
1089 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1090 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001091 if (cmd.flags)
1092 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001093
1094 memset(&c, 0, sizeof(c));
1095 c.common.opcode = cmd.opcode;
1096 c.common.flags = cmd.flags;
1097 c.common.nsid = cpu_to_le32(cmd.nsid);
1098 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1099 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1100 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1101 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1102 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1103 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1104 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1105 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1106
1107 if (cmd.timeout_ms)
1108 timeout = msecs_to_jiffies(cmd.timeout_ms);
1109
Keith Busch84fef622017-11-07 10:28:32 -07001110 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001112 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001113 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1114 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001115 nvme_passthru_end(ctrl, effects);
1116
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001117 if (status >= 0) {
1118 if (put_user(cmd.result, &ucmd->result))
1119 return -EFAULT;
1120 }
1121
1122 return status;
1123}
1124
1125static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1126 unsigned int cmd, unsigned long arg)
1127{
1128 struct nvme_ns *ns = bdev->bd_disk->private_data;
1129
1130 switch (cmd) {
1131 case NVME_IOCTL_ID:
1132 force_successful_syscall_return();
1133 return ns->ns_id;
1134 case NVME_IOCTL_ADMIN_CMD:
1135 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1136 case NVME_IOCTL_IO_CMD:
1137 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1138 case NVME_IOCTL_SUBMIT_IO:
1139 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001140 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001141#ifdef CONFIG_NVM
1142 if (ns->ndev)
1143 return nvme_nvm_ioctl(ns, cmd, arg);
1144#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001145 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001146 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001147 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001148 return -ENOTTY;
1149 }
1150}
1151
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001152static int nvme_open(struct block_device *bdev, fmode_t mode)
1153{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001154 struct nvme_ns *ns = bdev->bd_disk->private_data;
1155
1156 if (!kref_get_unless_zero(&ns->kref))
1157 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001158 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001159}
1160
1161static void nvme_release(struct gendisk *disk, fmode_t mode)
1162{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001163 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001164}
1165
1166static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1167{
1168 /* some standard values */
1169 geo->heads = 1 << 6;
1170 geo->sectors = 1 << 5;
1171 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1172 return 0;
1173}
1174
1175#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001176static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001177{
1178 struct blk_integrity integrity;
1179
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001180 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001181 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001182 case NVME_NS_DPS_PI_TYPE3:
1183 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001184 integrity.tag_size = sizeof(u16) + sizeof(u32);
1185 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001186 break;
1187 case NVME_NS_DPS_PI_TYPE1:
1188 case NVME_NS_DPS_PI_TYPE2:
1189 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001190 integrity.tag_size = sizeof(u16);
1191 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001192 break;
1193 default:
1194 integrity.profile = NULL;
1195 break;
1196 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001197 integrity.tuple_size = ms;
1198 blk_integrity_register(disk, &integrity);
1199 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001200}
1201#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001202static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001203{
1204}
1205#endif /* CONFIG_BLK_DEV_INTEGRITY */
1206
Scott Bauer6b8190d2017-06-15 10:44:30 -06001207static void nvme_set_chunk_size(struct nvme_ns *ns)
1208{
1209 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1210 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1211}
1212
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001213static void nvme_config_discard(struct nvme_ctrl *ctrl,
1214 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001215{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001216 u32 size = queue_logical_block_size(queue);
1217
1218 if (stream_alignment)
1219 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001220
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001221 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1222 NVME_DSM_MAX_RANGES);
1223
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001224 queue->limits.discard_alignment = size;
1225 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001226
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001227 blk_queue_max_discard_sectors(queue, UINT_MAX);
1228 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1229 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001230
1231 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001232 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001233}
1234
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001235static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1236 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001237{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001238 if (ctrl->vs >= NVME_VS(1, 1, 0))
1239 memcpy(eui64, id->eui64, sizeof(id->eui64));
1240 if (ctrl->vs >= NVME_VS(1, 2, 0))
1241 memcpy(nguid, id->nguid, sizeof(id->nguid));
1242 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001243 /* Don't treat error as fatal we potentially
1244 * already have a NGUID or EUI-64
1245 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001246 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1247 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001248 "%s: Identify Descriptors failed\n", __func__);
1249 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001250}
1251
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001252static void nvme_update_disk_info(struct gendisk *disk,
1253 struct nvme_ns *ns, struct nvme_id_ns *id)
1254{
1255 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1256 unsigned stream_alignment = 0;
1257
1258 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1259 stream_alignment = ns->sws * ns->sgs;
1260
1261 blk_mq_freeze_queue(disk->queue);
1262 blk_integrity_unregister(disk);
1263
1264 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1265 if (ns->ms && !ns->ext &&
1266 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1267 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001268 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001269 capacity = 0;
1270 set_capacity(disk, capacity);
1271
1272 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1273 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1274 blk_mq_unfreeze_queue(disk->queue);
1275}
1276
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001277static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1278{
1279 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001280
1281 /*
1282 * If identify namespace failed, use default 512 byte block size so
1283 * block layer can use before failing read/write for 0 capacity.
1284 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001285 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001286 if (ns->lba_shift == 0)
1287 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001288 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001289 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1290 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1291 /* the PI implementation requires metadata equal t10 pi tuple size */
1292 if (ns->ms == sizeof(struct t10_pi_tuple))
1293 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1294 else
1295 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001296
Christoph Hellwig6e78f212017-11-02 21:28:55 +03001297 if (ns->noiob)
1298 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001299 nvme_update_disk_info(disk, ns, id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001300}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001301
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001302static int nvme_revalidate_disk(struct gendisk *disk)
1303{
1304 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001305 struct nvme_ctrl *ctrl = ns->ctrl;
1306 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001307 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1308 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001309 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001310
1311 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1312 set_capacity(disk, 0);
1313 return -ENODEV;
1314 }
1315
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001316 id = nvme_identify_ns(ctrl, ns->ns_id);
1317 if (!id)
1318 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001319
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001320 if (id->ncap == 0) {
1321 ret = -ENODEV;
1322 goto out;
1323 }
1324
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001325 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1326 if (!uuid_equal(&ns->uuid, &uuid) ||
1327 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1328 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1329 dev_err(ctrl->device,
1330 "identifiers changed for nsid %d\n", ns->ns_id);
1331 ret = -ENODEV;
1332 }
1333
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001334out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001335 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001336 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001337}
1338
1339static char nvme_pr_type(enum pr_type type)
1340{
1341 switch (type) {
1342 case PR_WRITE_EXCLUSIVE:
1343 return 1;
1344 case PR_EXCLUSIVE_ACCESS:
1345 return 2;
1346 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1347 return 3;
1348 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1349 return 4;
1350 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1351 return 5;
1352 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1353 return 6;
1354 default:
1355 return 0;
1356 }
1357};
1358
1359static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1360 u64 key, u64 sa_key, u8 op)
1361{
1362 struct nvme_ns *ns = bdev->bd_disk->private_data;
1363 struct nvme_command c;
1364 u8 data[16] = { 0, };
1365
1366 put_unaligned_le64(key, &data[0]);
1367 put_unaligned_le64(sa_key, &data[8]);
1368
1369 memset(&c, 0, sizeof(c));
1370 c.common.opcode = op;
1371 c.common.nsid = cpu_to_le32(ns->ns_id);
1372 c.common.cdw10[0] = cpu_to_le32(cdw10);
1373
1374 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1375}
1376
1377static int nvme_pr_register(struct block_device *bdev, u64 old,
1378 u64 new, unsigned flags)
1379{
1380 u32 cdw10;
1381
1382 if (flags & ~PR_FL_IGNORE_KEY)
1383 return -EOPNOTSUPP;
1384
1385 cdw10 = old ? 2 : 0;
1386 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1387 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1388 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1389}
1390
1391static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1392 enum pr_type type, unsigned flags)
1393{
1394 u32 cdw10;
1395
1396 if (flags & ~PR_FL_IGNORE_KEY)
1397 return -EOPNOTSUPP;
1398
1399 cdw10 = nvme_pr_type(type) << 8;
1400 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1401 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1402}
1403
1404static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1405 enum pr_type type, bool abort)
1406{
1407 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1408 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1409}
1410
1411static int nvme_pr_clear(struct block_device *bdev, u64 key)
1412{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001413 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001414 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1415}
1416
1417static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1418{
1419 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1420 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1421}
1422
1423static const struct pr_ops nvme_pr_ops = {
1424 .pr_register = nvme_pr_register,
1425 .pr_reserve = nvme_pr_reserve,
1426 .pr_release = nvme_pr_release,
1427 .pr_preempt = nvme_pr_preempt,
1428 .pr_clear = nvme_pr_clear,
1429};
1430
Scott Bauera98e58e52017-02-03 12:50:32 -07001431#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001432int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1433 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001434{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001435 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001436 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001437
1438 memset(&cmd, 0, sizeof(cmd));
1439 if (send)
1440 cmd.common.opcode = nvme_admin_security_send;
1441 else
1442 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001443 cmd.common.nsid = 0;
1444 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1445 cmd.common.cdw10[1] = cpu_to_le32(len);
1446
1447 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1448 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1449}
1450EXPORT_SYMBOL_GPL(nvme_sec_submit);
1451#endif /* CONFIG_BLK_SED_OPAL */
1452
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001453static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001454 .owner = THIS_MODULE,
1455 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001456 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001457 .open = nvme_open,
1458 .release = nvme_release,
1459 .getgeo = nvme_getgeo,
1460 .revalidate_disk= nvme_revalidate_disk,
1461 .pr_ops = &nvme_pr_ops,
1462};
1463
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001464static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1465{
1466 unsigned long timeout =
1467 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1468 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1469 int ret;
1470
1471 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001472 if (csts == ~0)
1473 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001474 if ((csts & NVME_CSTS_RDY) == bit)
1475 break;
1476
1477 msleep(100);
1478 if (fatal_signal_pending(current))
1479 return -EINTR;
1480 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001481 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001482 "Device not ready; aborting %s\n", enabled ?
1483 "initialisation" : "reset");
1484 return -ENODEV;
1485 }
1486 }
1487
1488 return ret;
1489}
1490
1491/*
1492 * If the device has been passed off to us in an enabled state, just clear
1493 * the enabled bit. The spec says we should set the 'shutdown notification
1494 * bits', but doing so may cause the device to complete commands to the
1495 * admin queue ... and we don't know what memory that might be pointing at!
1496 */
1497int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1498{
1499 int ret;
1500
1501 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1502 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1503
1504 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1505 if (ret)
1506 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001507
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001508 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001509 msleep(NVME_QUIRK_DELAY_AMOUNT);
1510
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001511 return nvme_wait_ready(ctrl, cap, false);
1512}
Ming Lin576d55d2016-02-10 10:03:32 -08001513EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001514
1515int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1516{
1517 /*
1518 * Default to a 4K page size, with the intention to update this
1519 * path in the future to accomodate architectures with differing
1520 * kernel and IO page sizes.
1521 */
1522 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1523 int ret;
1524
1525 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001526 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001527 "Minimum device page size %u too large for host (%u)\n",
1528 1 << dev_page_min, 1 << page_shift);
1529 return -ENODEV;
1530 }
1531
1532 ctrl->page_size = 1 << page_shift;
1533
1534 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1535 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001536 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001537 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1538 ctrl->ctrl_config |= NVME_CC_ENABLE;
1539
1540 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1541 if (ret)
1542 return ret;
1543 return nvme_wait_ready(ctrl, cap, true);
1544}
Ming Lin576d55d2016-02-10 10:03:32 -08001545EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001546
1547int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1548{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001549 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001550 u32 csts;
1551 int ret;
1552
1553 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1554 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1555
1556 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1557 if (ret)
1558 return ret;
1559
1560 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1561 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1562 break;
1563
1564 msleep(100);
1565 if (fatal_signal_pending(current))
1566 return -EINTR;
1567 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001568 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001569 "Device shutdown incomplete; abort shutdown\n");
1570 return -ENODEV;
1571 }
1572 }
1573
1574 return ret;
1575}
Ming Lin576d55d2016-02-10 10:03:32 -08001576EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001577
Christoph Hellwigda358252016-03-02 18:07:11 +01001578static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1579 struct request_queue *q)
1580{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001581 bool vwc = false;
1582
Christoph Hellwigda358252016-03-02 18:07:11 +01001583 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001584 u32 max_segments =
1585 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1586
Christoph Hellwigda358252016-03-02 18:07:11 +01001587 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001588 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001589 }
Keith Busche6282ae2016-12-19 11:37:50 -05001590 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1591 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001592 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001593 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1594 vwc = true;
1595 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001596}
1597
Jon Derrickdbf86b32017-08-16 09:51:29 +02001598static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1599{
1600 __le64 ts;
1601 int ret;
1602
1603 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1604 return 0;
1605
1606 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1607 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1608 NULL);
1609 if (ret)
1610 dev_warn_once(ctrl->device,
1611 "could not set timestamp (%d)\n", ret);
1612 return ret;
1613}
1614
Keith Busch634b8322017-08-10 11:23:31 +02001615static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001616{
1617 /*
1618 * APST (Autonomous Power State Transition) lets us program a
1619 * table of power state transitions that the controller will
1620 * perform automatically. We configure it with a simple
1621 * heuristic: we are willing to spend at most 2% of the time
1622 * transitioning between power states. Therefore, when running
1623 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001624 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001625 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001626 * the requested maximum latency.
1627 *
1628 * We will not autonomously enter any non-operational state for
1629 * which the total latency exceeds ps_max_latency_us. Users
1630 * can set ps_max_latency_us to zero to turn off APST.
1631 */
1632
1633 unsigned apste;
1634 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001635 u64 max_lat_us = 0;
1636 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001637 int ret;
1638
1639 /*
1640 * If APST isn't supported or if we haven't been initialized yet,
1641 * then don't do anything.
1642 */
1643 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001644 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001645
1646 if (ctrl->npss > 31) {
1647 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001648 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001649 }
1650
1651 table = kzalloc(sizeof(*table), GFP_KERNEL);
1652 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001653 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001654
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001655 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001656 /* Turn off APST. */
1657 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001658 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001659 } else {
1660 __le64 target = cpu_to_le64(0);
1661 int state;
1662
1663 /*
1664 * Walk through all states from lowest- to highest-power.
1665 * According to the spec, lower-numbered states use more
1666 * power. NPSS, despite the name, is the index of the
1667 * lowest-power state, not the number of states.
1668 */
1669 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001670 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001671
1672 if (target)
1673 table->entries[state] = target;
1674
1675 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001676 * Don't allow transitions to the deepest state
1677 * if it's quirked off.
1678 */
1679 if (state == ctrl->npss &&
1680 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1681 continue;
1682
1683 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001684 * Is this state a useful non-operational state for
1685 * higher-power states to autonomously transition to?
1686 */
1687 if (!(ctrl->psd[state].flags &
1688 NVME_PS_FLAGS_NON_OP_STATE))
1689 continue;
1690
Kai-Heng Fengda875912017-06-07 15:25:42 +08001691 exit_latency_us =
1692 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1693 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001694 continue;
1695
Kai-Heng Fengda875912017-06-07 15:25:42 +08001696 total_latency_us =
1697 exit_latency_us +
1698 le32_to_cpu(ctrl->psd[state].entry_lat);
1699
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001700 /*
1701 * This state is good. Use it as the APST idle
1702 * target for higher power states.
1703 */
1704 transition_ms = total_latency_us + 19;
1705 do_div(transition_ms, 20);
1706 if (transition_ms > (1 << 24) - 1)
1707 transition_ms = (1 << 24) - 1;
1708
1709 target = cpu_to_le64((state << 3) |
1710 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001711
1712 if (max_ps == -1)
1713 max_ps = state;
1714
1715 if (total_latency_us > max_lat_us)
1716 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001717 }
1718
1719 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001720
1721 if (max_ps == -1) {
1722 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1723 } else {
1724 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1725 max_ps, max_lat_us, (int)sizeof(*table), table);
1726 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001727 }
1728
1729 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1730 table, sizeof(*table), NULL);
1731 if (ret)
1732 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1733
1734 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001735 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001736}
1737
1738static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1739{
1740 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1741 u64 latency;
1742
1743 switch (val) {
1744 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1745 case PM_QOS_LATENCY_ANY:
1746 latency = U64_MAX;
1747 break;
1748
1749 default:
1750 latency = val;
1751 }
1752
1753 if (ctrl->ps_max_latency_us != latency) {
1754 ctrl->ps_max_latency_us = latency;
1755 nvme_configure_apst(ctrl);
1756 }
1757}
1758
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001759struct nvme_core_quirk_entry {
1760 /*
1761 * NVMe model and firmware strings are padded with spaces. For
1762 * simplicity, strings in the quirk table are padded with NULLs
1763 * instead.
1764 */
1765 u16 vid;
1766 const char *mn;
1767 const char *fr;
1768 unsigned long quirks;
1769};
1770
1771static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001772 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001773 /*
1774 * This Toshiba device seems to die using any APST states. See:
1775 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1776 */
1777 .vid = 0x1179,
1778 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001779 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001780 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001781};
1782
1783/* match is null-terminated but idstr is space-padded. */
1784static bool string_matches(const char *idstr, const char *match, size_t len)
1785{
1786 size_t matchlen;
1787
1788 if (!match)
1789 return true;
1790
1791 matchlen = strlen(match);
1792 WARN_ON_ONCE(matchlen > len);
1793
1794 if (memcmp(idstr, match, matchlen))
1795 return false;
1796
1797 for (; matchlen < len; matchlen++)
1798 if (idstr[matchlen] != ' ')
1799 return false;
1800
1801 return true;
1802}
1803
1804static bool quirk_matches(const struct nvme_id_ctrl *id,
1805 const struct nvme_core_quirk_entry *q)
1806{
1807 return q->vid == le16_to_cpu(id->vid) &&
1808 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1809 string_matches(id->fr, q->fr, sizeof(id->fr));
1810}
1811
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001812static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
1813 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02001814{
1815 size_t nqnlen;
1816 int off;
1817
1818 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1819 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001820 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02001821 return;
1822 }
1823
1824 if (ctrl->vs >= NVME_VS(1, 2, 1))
1825 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1826
1827 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001828 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02001829 "nqn.2014.08.org.nvmexpress:%4x%4x",
1830 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001831 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001832 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001833 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001834 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001835 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
1836}
1837
1838static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
1839{
1840 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
1841 kfree(subsys);
1842}
1843
1844static void nvme_release_subsystem(struct device *dev)
1845{
1846 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
1847}
1848
1849static void nvme_destroy_subsystem(struct kref *ref)
1850{
1851 struct nvme_subsystem *subsys =
1852 container_of(ref, struct nvme_subsystem, ref);
1853
1854 mutex_lock(&nvme_subsystems_lock);
1855 list_del(&subsys->entry);
1856 mutex_unlock(&nvme_subsystems_lock);
1857
1858 device_del(&subsys->dev);
1859 put_device(&subsys->dev);
1860}
1861
1862static void nvme_put_subsystem(struct nvme_subsystem *subsys)
1863{
1864 kref_put(&subsys->ref, nvme_destroy_subsystem);
1865}
1866
1867static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
1868{
1869 struct nvme_subsystem *subsys;
1870
1871 lockdep_assert_held(&nvme_subsystems_lock);
1872
1873 list_for_each_entry(subsys, &nvme_subsystems, entry) {
1874 if (strcmp(subsys->subnqn, subsysnqn))
1875 continue;
1876 if (!kref_get_unless_zero(&subsys->ref))
1877 continue;
1878 return subsys;
1879 }
1880
1881 return NULL;
1882}
1883
1884static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1885{
1886 struct nvme_subsystem *subsys, *found;
1887 int ret;
1888
1889 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1890 if (!subsys)
1891 return -ENOMEM;
1892 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
1893 if (ret < 0) {
1894 kfree(subsys);
1895 return ret;
1896 }
1897 subsys->instance = ret;
1898 mutex_init(&subsys->lock);
1899 kref_init(&subsys->ref);
1900 INIT_LIST_HEAD(&subsys->ctrls);
1901 nvme_init_subnqn(subsys, ctrl, id);
1902 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
1903 memcpy(subsys->model, id->mn, sizeof(subsys->model));
1904 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
1905 subsys->vendor_id = le16_to_cpu(id->vid);
1906 subsys->cmic = id->cmic;
1907
1908 subsys->dev.class = nvme_subsys_class;
1909 subsys->dev.release = nvme_release_subsystem;
1910 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
1911 device_initialize(&subsys->dev);
1912
1913 mutex_lock(&nvme_subsystems_lock);
1914 found = __nvme_find_get_subsystem(subsys->subnqn);
1915 if (found) {
1916 /*
1917 * Verify that the subsystem actually supports multiple
1918 * controllers, else bail out.
1919 */
1920 if (!(id->cmic & (1 << 1))) {
1921 dev_err(ctrl->device,
1922 "ignoring ctrl due to duplicate subnqn (%s).\n",
1923 found->subnqn);
1924 nvme_put_subsystem(found);
1925 ret = -EINVAL;
1926 goto out_unlock;
1927 }
1928
1929 __nvme_release_subsystem(subsys);
1930 subsys = found;
1931 } else {
1932 ret = device_add(&subsys->dev);
1933 if (ret) {
1934 dev_err(ctrl->device,
1935 "failed to register subsystem device.\n");
1936 goto out_unlock;
1937 }
1938 list_add_tail(&subsys->entry, &nvme_subsystems);
1939 }
1940
1941 ctrl->subsys = subsys;
1942 mutex_unlock(&nvme_subsystems_lock);
1943
1944 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
1945 dev_name(ctrl->device))) {
1946 dev_err(ctrl->device,
1947 "failed to create sysfs link from subsystem.\n");
1948 /* the transport driver will eventually put the subsystem */
1949 return -EINVAL;
1950 }
1951
1952 mutex_lock(&subsys->lock);
1953 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1954 mutex_unlock(&subsys->lock);
1955
1956 return 0;
1957
1958out_unlock:
1959 mutex_unlock(&nvme_subsystems_lock);
1960 put_device(&subsys->dev);
1961 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02001962}
1963
Keith Buschc627c482017-11-07 10:28:31 -07001964static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
1965 size_t size)
1966{
1967 struct nvme_command c = { };
1968
1969 c.common.opcode = nvme_admin_get_log_page;
1970 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
1971 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
1972
1973 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
1974}
1975
Keith Busch84fef622017-11-07 10:28:32 -07001976static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
1977{
1978 int ret;
1979
1980 if (!ctrl->effects)
1981 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
1982
1983 if (!ctrl->effects)
1984 return 0;
1985
1986 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
1987 sizeof(*ctrl->effects));
1988 if (ret) {
1989 kfree(ctrl->effects);
1990 ctrl->effects = NULL;
1991 }
1992 return ret;
1993}
1994
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001995/*
1996 * Initialize the cached copies of the Identify data and various controller
1997 * register in our nvme_ctrl structure. This should be called as soon as
1998 * the admin queue is fully up and running.
1999 */
2000int nvme_init_identify(struct nvme_ctrl *ctrl)
2001{
2002 struct nvme_id_ctrl *id;
2003 u64 cap;
2004 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002005 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002006 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002007
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002008 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2009 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002010 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002011 return ret;
2012 }
2013
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002014 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2015 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002016 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002017 return ret;
2018 }
2019 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2020
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002021 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002022 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2023
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002024 ret = nvme_identify_ctrl(ctrl, &id);
2025 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002026 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002027 return -EIO;
2028 }
2029
Keith Busch84fef622017-11-07 10:28:32 -07002030 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2031 ret = nvme_get_effects_log(ctrl);
2032 if (ret < 0)
2033 return ret;
2034 }
2035
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002036 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002037 int i;
2038
2039 ret = nvme_init_subsystem(ctrl, id);
2040 if (ret)
2041 goto out_free;
2042
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002043 /*
2044 * Check for quirks. Quirk can depend on firmware version,
2045 * so, in principle, the set of quirks present can change
2046 * across a reset. As a possible future enhancement, we
2047 * could re-scan for quirks every time we reinitialize
2048 * the device, but we'd have to make sure that the driver
2049 * behaves intelligently if the quirks change.
2050 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002051 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2052 if (quirk_matches(id, &core_quirks[i]))
2053 ctrl->quirks |= core_quirks[i].quirks;
2054 }
2055 }
2056
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002057 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002058 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 -07002059 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2060 }
2061
Scott Bauer8a9ae522017-02-17 13:59:40 +01002062 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002063 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002064 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002065 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002066 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002067 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002068 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002069 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002070 max_hw_sectors = UINT_MAX;
2071 ctrl->max_hw_sectors =
2072 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002073
Christoph Hellwigda358252016-03-02 18:07:11 +01002074 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002075 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002076 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002077
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002078 if (id->rtd3e) {
2079 /* us -> s */
2080 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2081
2082 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2083 shutdown_timeout, 60);
2084
2085 if (ctrl->shutdown_timeout != shutdown_timeout)
2086 dev_warn(ctrl->device,
2087 "Shutdown timeout set to %u seconds\n",
2088 ctrl->shutdown_timeout);
2089 } else
2090 ctrl->shutdown_timeout = shutdown_timeout;
2091
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002092 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002093 ctrl->apsta = id->apsta;
2094 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002095 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2096 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002097 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 -04002098 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002099 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002100 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002101 }
2102 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002103 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002104 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002105 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2106
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002107 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002108 ctrl->icdoff = le16_to_cpu(id->icdoff);
2109 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2110 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2111 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2112
2113 /*
2114 * In fabrics we need to verify the cntlid matches the
2115 * admin connect
2116 */
Keith Busch634b8322017-08-10 11:23:31 +02002117 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002118 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002119 goto out_free;
2120 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002121
2122 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002123 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002124 "keep-alive support is mandatory for fabrics\n");
2125 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002126 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002127 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002128 } else {
2129 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002130 ctrl->hmpre = le32_to_cpu(id->hmpre);
2131 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002132 ctrl->hmminds = le32_to_cpu(id->hmminds);
2133 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002134 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002135
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002136 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002137
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002138 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002139 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002140 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002141 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2142
Keith Busch634b8322017-08-10 11:23:31 +02002143 ret = nvme_configure_apst(ctrl);
2144 if (ret < 0)
2145 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002146
2147 ret = nvme_configure_timestamp(ctrl);
2148 if (ret < 0)
2149 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002150
2151 ret = nvme_configure_directives(ctrl);
2152 if (ret < 0)
2153 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002154
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002155 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002156
Keith Busch634b8322017-08-10 11:23:31 +02002157 return 0;
2158
2159out_free:
2160 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002161 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002162}
Ming Lin576d55d2016-02-10 10:03:32 -08002163EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002164
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002165static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002166{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002167 struct nvme_ctrl *ctrl =
2168 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002169
Christoph Hellwig999ada22017-10-18 17:09:31 +02002170 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002171 return -EWOULDBLOCK;
2172 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002173 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002174}
2175
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002176static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2177{
2178 struct nvme_ns *ns;
2179 int ret;
2180
2181 mutex_lock(&ctrl->namespaces_mutex);
2182 if (list_empty(&ctrl->namespaces)) {
2183 ret = -ENOTTY;
2184 goto out_unlock;
2185 }
2186
2187 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2188 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002189 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002190 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2191 ret = -EINVAL;
2192 goto out_unlock;
2193 }
2194
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002195 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002196 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2197 kref_get(&ns->kref);
2198 mutex_unlock(&ctrl->namespaces_mutex);
2199
2200 ret = nvme_user_cmd(ctrl, ns, argp);
2201 nvme_put_ns(ns);
2202 return ret;
2203
2204out_unlock:
2205 mutex_unlock(&ctrl->namespaces_mutex);
2206 return ret;
2207}
2208
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002209static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2210 unsigned long arg)
2211{
2212 struct nvme_ctrl *ctrl = file->private_data;
2213 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002214
2215 switch (cmd) {
2216 case NVME_IOCTL_ADMIN_CMD:
2217 return nvme_user_cmd(ctrl, NULL, argp);
2218 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002219 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002220 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002221 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002222 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002223 case NVME_IOCTL_SUBSYS_RESET:
2224 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002225 case NVME_IOCTL_RESCAN:
2226 nvme_queue_scan(ctrl);
2227 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002228 default:
2229 return -ENOTTY;
2230 }
2231}
2232
2233static const struct file_operations nvme_dev_fops = {
2234 .owner = THIS_MODULE,
2235 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002236 .unlocked_ioctl = nvme_dev_ioctl,
2237 .compat_ioctl = nvme_dev_ioctl,
2238};
2239
2240static ssize_t nvme_sysfs_reset(struct device *dev,
2241 struct device_attribute *attr, const char *buf,
2242 size_t count)
2243{
2244 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2245 int ret;
2246
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002247 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002248 if (ret < 0)
2249 return ret;
2250 return count;
2251}
2252static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2253
Keith Busch9ec3bb22016-04-29 15:45:18 -06002254static ssize_t nvme_sysfs_rescan(struct device *dev,
2255 struct device_attribute *attr, const char *buf,
2256 size_t count)
2257{
2258 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2259
2260 nvme_queue_scan(ctrl);
2261 return count;
2262}
2263static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2264
Keith Busch118472a2016-02-18 09:57:48 -07002265static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2266 char *buf)
2267{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002268 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002269 struct nvme_subsystem *subsys = ns->ctrl->subsys;
2270 int serial_len = sizeof(subsys->serial);
2271 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002272
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002273 if (!uuid_is_null(&ns->uuid))
2274 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2275
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002276 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2277 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002278
2279 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2280 return sprintf(buf, "eui.%8phN\n", ns->eui);
2281
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002282 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2283 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002284 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002285 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2286 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002287 model_len--;
2288
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002289 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2290 serial_len, subsys->serial, model_len, subsys->model,
2291 ns->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002292}
2293static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2294
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002295static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2296 char *buf)
2297{
2298 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2299 return sprintf(buf, "%pU\n", ns->nguid);
2300}
2301static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2302
Keith Busch2b9b6e82015-12-22 10:10:45 -07002303static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2304 char *buf)
2305{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002306 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002307
2308 /* For backward compatibility expose the NGUID to userspace if
2309 * we have no UUID set
2310 */
2311 if (uuid_is_null(&ns->uuid)) {
2312 printk_ratelimited(KERN_WARNING
2313 "No UUID available providing old NGUID\n");
2314 return sprintf(buf, "%pU\n", ns->nguid);
2315 }
2316 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002317}
2318static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2319
2320static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2321 char *buf)
2322{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002323 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Javier Gonzálezab083b12017-11-08 10:59:04 +01002324 return sprintf(buf, "%8ph\n", ns->eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002325}
2326static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2327
2328static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2329 char *buf)
2330{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002331 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002332 return sprintf(buf, "%d\n", ns->ns_id);
2333}
2334static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2335
2336static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002337 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002338 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002339 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002340 &dev_attr_eui.attr,
2341 &dev_attr_nsid.attr,
2342 NULL,
2343};
2344
Ming Lin1a353d82016-06-13 16:45:24 +02002345static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002346 struct attribute *a, int n)
2347{
2348 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002349 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002350
2351 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002352 if (uuid_is_null(&ns->uuid) ||
2353 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2354 return 0;
2355 }
2356 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002357 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002358 return 0;
2359 }
2360 if (a == &dev_attr_eui.attr) {
2361 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2362 return 0;
2363 }
2364 return a->mode;
2365}
2366
2367static const struct attribute_group nvme_ns_attr_group = {
2368 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002369 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002370};
2371
Ming Lin931e1c22016-02-26 13:24:19 -08002372#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002373static ssize_t field##_show(struct device *dev, \
2374 struct device_attribute *attr, char *buf) \
2375{ \
2376 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002377 return sprintf(buf, "%.*s\n", \
2378 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002379} \
2380static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2381
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002382nvme_show_str_function(model);
2383nvme_show_str_function(serial);
2384nvme_show_str_function(firmware_rev);
2385
Ming Lin931e1c22016-02-26 13:24:19 -08002386#define nvme_show_int_function(field) \
2387static ssize_t field##_show(struct device *dev, \
2388 struct device_attribute *attr, char *buf) \
2389{ \
2390 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2391 return sprintf(buf, "%d\n", ctrl->field); \
2392} \
2393static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2394
Ming Lin931e1c22016-02-26 13:24:19 -08002395nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002396
Ming Lin1a353d82016-06-13 16:45:24 +02002397static ssize_t nvme_sysfs_delete(struct device *dev,
2398 struct device_attribute *attr, const char *buf,
2399 size_t count)
2400{
2401 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2402
2403 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002404 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002405 return count;
2406}
2407static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2408
2409static ssize_t nvme_sysfs_show_transport(struct device *dev,
2410 struct device_attribute *attr,
2411 char *buf)
2412{
2413 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2414
2415 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2416}
2417static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2418
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002419static ssize_t nvme_sysfs_show_state(struct device *dev,
2420 struct device_attribute *attr,
2421 char *buf)
2422{
2423 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2424 static const char *const state_name[] = {
2425 [NVME_CTRL_NEW] = "new",
2426 [NVME_CTRL_LIVE] = "live",
2427 [NVME_CTRL_RESETTING] = "resetting",
2428 [NVME_CTRL_RECONNECTING]= "reconnecting",
2429 [NVME_CTRL_DELETING] = "deleting",
2430 [NVME_CTRL_DEAD] = "dead",
2431 };
2432
2433 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2434 state_name[ctrl->state])
2435 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2436
2437 return sprintf(buf, "unknown state\n");
2438}
2439
2440static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2441
Ming Lin1a353d82016-06-13 16:45:24 +02002442static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2443 struct device_attribute *attr,
2444 char *buf)
2445{
2446 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2447
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002448 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002449}
2450static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2451
2452static ssize_t nvme_sysfs_show_address(struct device *dev,
2453 struct device_attribute *attr,
2454 char *buf)
2455{
2456 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2457
2458 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2459}
2460static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2461
Keith Busch779ff7562016-01-12 15:09:31 -07002462static struct attribute *nvme_dev_attrs[] = {
2463 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002464 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002465 &dev_attr_model.attr,
2466 &dev_attr_serial.attr,
2467 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002468 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002469 &dev_attr_delete_controller.attr,
2470 &dev_attr_transport.attr,
2471 &dev_attr_subsysnqn.attr,
2472 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002473 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002474 NULL
2475};
2476
Ming Lin1a353d82016-06-13 16:45:24 +02002477static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2478 struct attribute *a, int n)
2479{
2480 struct device *dev = container_of(kobj, struct device, kobj);
2481 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2482
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002483 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2484 return 0;
2485 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2486 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002487
2488 return a->mode;
2489}
2490
Keith Busch779ff7562016-01-12 15:09:31 -07002491static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002492 .attrs = nvme_dev_attrs,
2493 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002494};
2495
2496static const struct attribute_group *nvme_dev_attr_groups[] = {
2497 &nvme_dev_attrs_group,
2498 NULL,
2499};
2500
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002501static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2502{
2503 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2504 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2505
2506 return nsa->ns_id - nsb->ns_id;
2507}
2508
Keith Busch32f0c4a2016-07-13 11:45:02 -06002509static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002510{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002511 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002512
Keith Busch32f0c4a2016-07-13 11:45:02 -06002513 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002514 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002515 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002516 if (!kref_get_unless_zero(&ns->kref))
2517 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002518 ret = ns;
2519 break;
2520 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002521 if (ns->ns_id > nsid)
2522 break;
2523 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002524 mutex_unlock(&ctrl->namespaces_mutex);
2525 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002526}
2527
Jens Axboef5d11842017-06-27 12:03:06 -06002528static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2529{
2530 struct streams_directive_params s;
2531 int ret;
2532
2533 if (!ctrl->nr_streams)
2534 return 0;
2535
2536 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2537 if (ret)
2538 return ret;
2539
2540 ns->sws = le32_to_cpu(s.sws);
2541 ns->sgs = le16_to_cpu(s.sgs);
2542
2543 if (ns->sws) {
2544 unsigned int bs = 1 << ns->lba_shift;
2545
2546 blk_queue_io_min(ns->queue, bs * ns->sws);
2547 if (ns->sgs)
2548 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2549 }
2550
2551 return 0;
2552}
2553
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002554static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2555{
2556 struct nvme_ns *ns;
2557 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002558 struct nvme_id_ns *id;
2559 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002560 int node = dev_to_node(ctrl->dev);
2561
2562 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2563 if (!ns)
2564 return;
2565
Keith Busch075790e2016-02-24 09:15:53 -07002566 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2567 if (ns->instance < 0)
2568 goto out_free_ns;
2569
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002570 ns->queue = blk_mq_init_queue(ctrl->tagset);
2571 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002572 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002573 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2574 ns->queue->queuedata = ns;
2575 ns->ctrl = ctrl;
2576
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002577 kref_init(&ns->kref);
2578 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002579 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002580
2581 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002582 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002583 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002584
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002585 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002586
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002587 id = nvme_identify_ns(ctrl, nsid);
2588 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002589 goto out_free_queue;
2590
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002591 if (id->ncap == 0)
2592 goto out_free_id;
2593
2594 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2595
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002596 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2597 if (nvme_nvm_register(ns, disk_name, node)) {
2598 dev_warn(ctrl->device, "LightNVM init failure\n");
2599 goto out_free_id;
2600 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002601 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002602
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002603 disk = alloc_disk_node(0, node);
2604 if (!disk)
2605 goto out_free_id;
2606
2607 disk->fops = &nvme_fops;
2608 disk->private_data = ns;
2609 disk->queue = ns->queue;
2610 disk->flags = GENHD_FL_EXT_DEVT;
2611 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2612 ns->disk = disk;
2613
2614 __nvme_revalidate_disk(disk, id);
2615
Keith Busch32f0c4a2016-07-13 11:45:02 -06002616 mutex_lock(&ctrl->namespaces_mutex);
2617 list_add_tail(&ns->list, &ctrl->namespaces);
2618 mutex_unlock(&ctrl->namespaces_mutex);
2619
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002620 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002621
2622 kfree(id);
2623
Dan Williams0d52c7562016-06-15 19:44:20 -07002624 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002625 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2626 &nvme_ns_attr_group))
2627 pr_warn("%s: failed to create sysfs group for identification\n",
2628 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002629 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2630 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2631 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002632 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002633 out_free_id:
2634 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002635 out_free_queue:
2636 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002637 out_release_instance:
2638 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002639 out_free_ns:
2640 kfree(ns);
2641}
2642
2643static void nvme_ns_remove(struct nvme_ns *ns)
2644{
Keith Busch646017a2016-02-24 09:15:54 -07002645 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2646 return;
2647
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002648 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002649 if (blk_get_integrity(ns->disk))
2650 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002651 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2652 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002653 if (ns->ndev)
2654 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002655 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002656 blk_cleanup_queue(ns->queue);
2657 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002658
2659 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002660 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002661 mutex_unlock(&ns->ctrl->namespaces_mutex);
2662
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002663 nvme_put_ns(ns);
2664}
2665
Keith Busch540c8012015-10-22 15:45:06 -06002666static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2667{
2668 struct nvme_ns *ns;
2669
Keith Busch32f0c4a2016-07-13 11:45:02 -06002670 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002671 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002672 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002673 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002674 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002675 } else
2676 nvme_alloc_ns(ctrl, nsid);
2677}
2678
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302679static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2680 unsigned nsid)
2681{
2682 struct nvme_ns *ns, *next;
2683
2684 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2685 if (ns->ns_id > nsid)
2686 nvme_ns_remove(ns);
2687 }
2688}
2689
Keith Busch540c8012015-10-22 15:45:06 -06002690static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2691{
2692 struct nvme_ns *ns;
2693 __le32 *ns_list;
2694 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2695 int ret = 0;
2696
2697 ns_list = kzalloc(0x1000, GFP_KERNEL);
2698 if (!ns_list)
2699 return -ENOMEM;
2700
2701 for (i = 0; i < num_lists; i++) {
2702 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2703 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302704 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002705
2706 for (j = 0; j < min(nn, 1024U); j++) {
2707 nsid = le32_to_cpu(ns_list[j]);
2708 if (!nsid)
2709 goto out;
2710
2711 nvme_validate_ns(ctrl, nsid);
2712
2713 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002714 ns = nvme_find_get_ns(ctrl, prev);
2715 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002716 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002717 nvme_put_ns(ns);
2718 }
Keith Busch540c8012015-10-22 15:45:06 -06002719 }
2720 }
2721 nn -= j;
2722 }
2723 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302724 nvme_remove_invalid_namespaces(ctrl, prev);
2725 free:
Keith Busch540c8012015-10-22 15:45:06 -06002726 kfree(ns_list);
2727 return ret;
2728}
2729
Christoph Hellwig5955be22016-04-26 13:51:59 +02002730static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002731{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002732 unsigned i;
2733
Keith Busch540c8012015-10-22 15:45:06 -06002734 for (i = 1; i <= nn; i++)
2735 nvme_validate_ns(ctrl, i);
2736
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302737 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002738}
2739
Christoph Hellwig5955be22016-04-26 13:51:59 +02002740static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002741{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002742 struct nvme_ctrl *ctrl =
2743 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002744 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002745 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002746
Christoph Hellwig5955be22016-04-26 13:51:59 +02002747 if (ctrl->state != NVME_CTRL_LIVE)
2748 return;
2749
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002750 if (nvme_identify_ctrl(ctrl, &id))
2751 return;
Keith Busch540c8012015-10-22 15:45:06 -06002752
2753 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002754 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002755 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2756 if (!nvme_scan_ns_list(ctrl, nn))
2757 goto done;
2758 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002759 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002760 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002761 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002762 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002763 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002764 kfree(id);
2765}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002766
2767void nvme_queue_scan(struct nvme_ctrl *ctrl)
2768{
2769 /*
2770 * Do not queue new scan work when a controller is reset during
2771 * removal.
2772 */
2773 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002774 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002775}
2776EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002777
Keith Busch32f0c4a2016-07-13 11:45:02 -06002778/*
2779 * This function iterates the namespace list unlocked to allow recovery from
2780 * controller failure. It is up to the caller to ensure the namespace list is
2781 * not modified by scan work while this function is executing.
2782 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002783void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2784{
2785 struct nvme_ns *ns, *next;
2786
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002787 /*
2788 * The dead states indicates the controller was not gracefully
2789 * disconnected. In that case, we won't be able to flush any data while
2790 * removing the namespaces' disks; fail all the queues now to avoid
2791 * potentially having to clean up the failed sync later.
2792 */
2793 if (ctrl->state == NVME_CTRL_DEAD)
2794 nvme_kill_queues(ctrl);
2795
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002796 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2797 nvme_ns_remove(ns);
2798}
Ming Lin576d55d2016-02-10 10:03:32 -08002799EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002800
Keith Busche3d78742017-11-07 15:13:14 -07002801static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
2802{
2803 char *envp[2] = { NULL, NULL };
2804 u32 aen_result = ctrl->aen_result;
2805
2806 ctrl->aen_result = 0;
2807 if (!aen_result)
2808 return;
2809
2810 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
2811 if (!envp[0])
2812 return;
2813 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
2814 kfree(envp[0]);
2815}
2816
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002817static void nvme_async_event_work(struct work_struct *work)
2818{
2819 struct nvme_ctrl *ctrl =
2820 container_of(work, struct nvme_ctrl, async_event_work);
2821
Keith Busche3d78742017-11-07 15:13:14 -07002822 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07002823 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002824}
2825
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302826static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2827{
2828
2829 u32 csts;
2830
2831 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2832 return false;
2833
2834 if (csts == ~0)
2835 return false;
2836
2837 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2838}
2839
2840static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2841{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302842 struct nvme_fw_slot_info_log *log;
2843
2844 log = kmalloc(sizeof(*log), GFP_KERNEL);
2845 if (!log)
2846 return;
2847
Keith Buschc627c482017-11-07 10:28:31 -07002848 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302849 dev_warn(ctrl->device,
2850 "Get FW SLOT INFO log error\n");
2851 kfree(log);
2852}
2853
2854static void nvme_fw_act_work(struct work_struct *work)
2855{
2856 struct nvme_ctrl *ctrl = container_of(work,
2857 struct nvme_ctrl, fw_act_work);
2858 unsigned long fw_act_timeout;
2859
2860 if (ctrl->mtfa)
2861 fw_act_timeout = jiffies +
2862 msecs_to_jiffies(ctrl->mtfa * 100);
2863 else
2864 fw_act_timeout = jiffies +
2865 msecs_to_jiffies(admin_timeout * 1000);
2866
2867 nvme_stop_queues(ctrl);
2868 while (nvme_ctrl_pp_status(ctrl)) {
2869 if (time_after(jiffies, fw_act_timeout)) {
2870 dev_warn(ctrl->device,
2871 "Fw activation timeout, reset controller\n");
2872 nvme_reset_ctrl(ctrl);
2873 break;
2874 }
2875 msleep(100);
2876 }
2877
2878 if (ctrl->state != NVME_CTRL_LIVE)
2879 return;
2880
2881 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09002882 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302883 nvme_get_fw_slot_info(ctrl);
2884}
2885
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002886void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2887 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002888{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002889 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002890
Keith Buschad22c352017-11-07 15:13:12 -07002891 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002892 return;
2893
Keith Busche3d78742017-11-07 15:13:14 -07002894 switch (result & 0x7) {
2895 case NVME_AER_ERROR:
2896 case NVME_AER_SMART:
2897 case NVME_AER_CSS:
2898 case NVME_AER_VS:
2899 ctrl->aen_result = result;
2900 break;
2901 default:
2902 break;
2903 }
2904
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002905 switch (result & 0xff07) {
2906 case NVME_AER_NOTICE_NS_CHANGED:
2907 dev_info(ctrl->device, "rescanning\n");
2908 nvme_queue_scan(ctrl);
2909 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302910 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002911 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302912 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002913 default:
2914 dev_warn(ctrl->device, "async event result %08x\n", result);
2915 }
Keith Buschad22c352017-11-07 15:13:12 -07002916 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002917}
2918EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2919
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002920void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002921{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002922 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002923 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002924 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302925 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002926}
2927EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002928
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002929void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2930{
2931 if (ctrl->kato)
2932 nvme_start_keep_alive(ctrl);
2933
2934 if (ctrl->queue_count > 1) {
2935 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07002936 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002937 nvme_start_queues(ctrl);
2938 }
2939}
2940EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2941
2942void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2943{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002944 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002945}
Ming Lin576d55d2016-02-10 10:03:32 -08002946EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002947
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002948static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002949{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002950 struct nvme_ctrl *ctrl =
2951 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002952 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002953
Christoph Hellwig9843f682017-10-18 13:10:01 +02002954 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002955 ida_destroy(&ctrl->ns_ida);
Keith Busch84fef622017-11-07 10:28:32 -07002956 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002957
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002958 if (subsys) {
2959 mutex_lock(&subsys->lock);
2960 list_del(&ctrl->subsys_entry);
2961 mutex_unlock(&subsys->lock);
2962 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
2963 }
2964
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002965 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002966
2967 if (subsys)
2968 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002969}
2970
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002971/*
2972 * Initialize a NVMe controller structures. This needs to be called during
2973 * earliest initialization so that we have the initialized structured around
2974 * during probing.
2975 */
2976int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2977 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2978{
2979 int ret;
2980
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002981 ctrl->state = NVME_CTRL_NEW;
2982 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002983 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002984 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002985 ctrl->dev = dev;
2986 ctrl->ops = ops;
2987 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002988 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002989 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302990 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002991 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002992
Christoph Hellwig9843f682017-10-18 13:10:01 +02002993 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2994 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002995 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002996 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002997
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002998 device_initialize(&ctrl->ctrl_device);
2999 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003000 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003001 ctrl->device->class = nvme_class;
3002 ctrl->device->parent = ctrl->dev;
3003 ctrl->device->groups = nvme_dev_attr_groups;
3004 ctrl->device->release = nvme_free_ctrl;
3005 dev_set_drvdata(ctrl->device, ctrl);
3006 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3007 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003008 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003009
3010 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3011 ctrl->cdev.owner = ops->module;
3012 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003013 if (ret)
3014 goto out_free_name;
3015
Keith Busch075790e2016-02-24 09:15:53 -07003016 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003017
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003018 /*
3019 * Initialize latency tolerance controls. The sysfs files won't
3020 * be visible to userspace unless the device actually supports APST.
3021 */
3022 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3023 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3024 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3025
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003026 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003027out_free_name:
3028 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003029out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003030 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003031out:
3032 return ret;
3033}
Ming Lin576d55d2016-02-10 10:03:32 -08003034EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003035
Keith Busch69d9a992016-02-24 09:15:56 -07003036/**
3037 * nvme_kill_queues(): Ends all namespace queues
3038 * @ctrl: the dead controller that needs to end
3039 *
3040 * Call this function when the driver determines it is unable to get the
3041 * controller in a state capable of servicing IO.
3042 */
3043void nvme_kill_queues(struct nvme_ctrl *ctrl)
3044{
3045 struct nvme_ns *ns;
3046
Keith Busch32f0c4a2016-07-13 11:45:02 -06003047 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08003048
Ming Lei443bd902017-06-19 10:21:08 +08003049 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003050 if (ctrl->admin_q)
3051 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003052
Keith Busch32f0c4a2016-07-13 11:45:02 -06003053 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003054 /*
3055 * Revalidating a dead namespace sets capacity to 0. This will
3056 * end buffered writers dirtying pages that can't be synced.
3057 */
Keith Buschf33447b2017-02-10 18:15:51 -05003058 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3059 continue;
3060 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003061 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003062
Ming Lei443bd902017-06-19 10:21:08 +08003063 /* Forcibly unquiesce queues to avoid blocking dispatch */
3064 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003065 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003066 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07003067}
Linus Torvalds237045f2016-03-18 17:13:31 -07003068EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003069
Keith Busch302ad8c2017-03-01 14:22:12 -05003070void nvme_unfreeze(struct nvme_ctrl *ctrl)
3071{
3072 struct nvme_ns *ns;
3073
3074 mutex_lock(&ctrl->namespaces_mutex);
3075 list_for_each_entry(ns, &ctrl->namespaces, list)
3076 blk_mq_unfreeze_queue(ns->queue);
3077 mutex_unlock(&ctrl->namespaces_mutex);
3078}
3079EXPORT_SYMBOL_GPL(nvme_unfreeze);
3080
3081void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3082{
3083 struct nvme_ns *ns;
3084
3085 mutex_lock(&ctrl->namespaces_mutex);
3086 list_for_each_entry(ns, &ctrl->namespaces, list) {
3087 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3088 if (timeout <= 0)
3089 break;
3090 }
3091 mutex_unlock(&ctrl->namespaces_mutex);
3092}
3093EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3094
3095void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3096{
3097 struct nvme_ns *ns;
3098
3099 mutex_lock(&ctrl->namespaces_mutex);
3100 list_for_each_entry(ns, &ctrl->namespaces, list)
3101 blk_mq_freeze_queue_wait(ns->queue);
3102 mutex_unlock(&ctrl->namespaces_mutex);
3103}
3104EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3105
3106void nvme_start_freeze(struct nvme_ctrl *ctrl)
3107{
3108 struct nvme_ns *ns;
3109
3110 mutex_lock(&ctrl->namespaces_mutex);
3111 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003112 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05003113 mutex_unlock(&ctrl->namespaces_mutex);
3114}
3115EXPORT_SYMBOL_GPL(nvme_start_freeze);
3116
Keith Busch25646262016-01-04 09:10:57 -07003117void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003118{
3119 struct nvme_ns *ns;
3120
Keith Busch32f0c4a2016-07-13 11:45:02 -06003121 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003122 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003123 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003124 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003125}
Ming Lin576d55d2016-02-10 10:03:32 -08003126EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003127
Keith Busch25646262016-01-04 09:10:57 -07003128void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003129{
3130 struct nvme_ns *ns;
3131
Keith Busch32f0c4a2016-07-13 11:45:02 -06003132 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003133 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003134 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003135 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003136}
Ming Lin576d55d2016-02-10 10:03:32 -08003137EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003138
Sagi Grimberg31b84462017-10-11 12:53:07 +03003139int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3140{
3141 if (!ctrl->ops->reinit_request)
3142 return 0;
3143
3144 return blk_mq_tagset_iter(set, set->driver_data,
3145 ctrl->ops->reinit_request);
3146}
3147EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3148
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003149int __init nvme_core_init(void)
3150{
3151 int result;
3152
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003153 nvme_wq = alloc_workqueue("nvme-wq",
3154 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3155 if (!nvme_wq)
3156 return -ENOMEM;
3157
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003158 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003159 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003160 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003161
3162 nvme_class = class_create(THIS_MODULE, "nvme");
3163 if (IS_ERR(nvme_class)) {
3164 result = PTR_ERR(nvme_class);
3165 goto unregister_chrdev;
3166 }
3167
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003168 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3169 if (IS_ERR(nvme_subsys_class)) {
3170 result = PTR_ERR(nvme_subsys_class);
3171 goto destroy_class;
3172 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003173 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003174
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003175destroy_class:
3176 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003177unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003178 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003179destroy_wq:
3180 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003181 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003182}
3183
3184void nvme_core_exit(void)
3185{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003186 ida_destroy(&nvme_subsystems_ida);
3187 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003188 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003189 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003190 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003191}
Ming Lin576d55d2016-02-10 10:03:32 -08003192
3193MODULE_LICENSE("GPL");
3194MODULE_VERSION("1.0");
3195module_init(nvme_core_init);
3196module_exit(nvme_core_exit);