blob: 04c0949c754ac9dbbf3f7ade3ec242b5129ef466 [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,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100800 struct nvme_ns_ids *ids)
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 Hellwig002fab02017-11-09 13:50:16 +0100836 memcpy(ids->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 Hellwig002fab02017-11-09 13:50:16 +0100846 memcpy(ids->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 Hellwig002fab02017-11-09 13:50:16 +0100856 uuid_copy(&ids->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,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001236 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001237{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001238 memset(ids, 0, sizeof(*ids));
1239
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001240 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001241 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001242 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001243 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001244 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001245 /* Don't treat error as fatal we potentially
1246 * already have a NGUID or EUI-64
1247 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001248 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001249 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001250 "%s: Identify Descriptors failed\n", __func__);
1251 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001252}
1253
Christoph Hellwig002fab02017-11-09 13:50:16 +01001254static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1255{
1256 return uuid_equal(&a->uuid, &b->uuid) &&
1257 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1258 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1259}
1260
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001261static void nvme_update_disk_info(struct gendisk *disk,
1262 struct nvme_ns *ns, struct nvme_id_ns *id)
1263{
1264 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1265 unsigned stream_alignment = 0;
1266
1267 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1268 stream_alignment = ns->sws * ns->sgs;
1269
1270 blk_mq_freeze_queue(disk->queue);
1271 blk_integrity_unregister(disk);
1272
1273 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1274 if (ns->ms && !ns->ext &&
1275 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1276 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001277 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001278 capacity = 0;
1279 set_capacity(disk, capacity);
1280
1281 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1282 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1283 blk_mq_unfreeze_queue(disk->queue);
1284}
1285
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001286static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1287{
1288 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001289
1290 /*
1291 * If identify namespace failed, use default 512 byte block size so
1292 * block layer can use before failing read/write for 0 capacity.
1293 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001294 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001295 if (ns->lba_shift == 0)
1296 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001297 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001298 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1299 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1300 /* the PI implementation requires metadata equal t10 pi tuple size */
1301 if (ns->ms == sizeof(struct t10_pi_tuple))
1302 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1303 else
1304 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001305
Christoph Hellwig6e78f212017-11-02 21:28:55 +03001306 if (ns->noiob)
1307 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001308 nvme_update_disk_info(disk, ns, id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001309}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001310
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001311static int nvme_revalidate_disk(struct gendisk *disk)
1312{
1313 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001314 struct nvme_ctrl *ctrl = ns->ctrl;
1315 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001316 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001317 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001318
1319 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1320 set_capacity(disk, 0);
1321 return -ENODEV;
1322 }
1323
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001324 id = nvme_identify_ns(ctrl, ns->ns_id);
1325 if (!id)
1326 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001327
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001328 if (id->ncap == 0) {
1329 ret = -ENODEV;
1330 goto out;
1331 }
1332
Christoph Hellwig002fab02017-11-09 13:50:16 +01001333 nvme_report_ns_ids(ctrl, ns->ns_id, id, &ids);
1334 if (!nvme_ns_ids_equal(&ns->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001335 dev_err(ctrl->device,
1336 "identifiers changed for nsid %d\n", ns->ns_id);
1337 ret = -ENODEV;
1338 }
1339
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001340out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001341 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001342 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001343}
1344
1345static char nvme_pr_type(enum pr_type type)
1346{
1347 switch (type) {
1348 case PR_WRITE_EXCLUSIVE:
1349 return 1;
1350 case PR_EXCLUSIVE_ACCESS:
1351 return 2;
1352 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1353 return 3;
1354 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1355 return 4;
1356 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1357 return 5;
1358 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1359 return 6;
1360 default:
1361 return 0;
1362 }
1363};
1364
1365static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1366 u64 key, u64 sa_key, u8 op)
1367{
1368 struct nvme_ns *ns = bdev->bd_disk->private_data;
1369 struct nvme_command c;
1370 u8 data[16] = { 0, };
1371
1372 put_unaligned_le64(key, &data[0]);
1373 put_unaligned_le64(sa_key, &data[8]);
1374
1375 memset(&c, 0, sizeof(c));
1376 c.common.opcode = op;
1377 c.common.nsid = cpu_to_le32(ns->ns_id);
1378 c.common.cdw10[0] = cpu_to_le32(cdw10);
1379
1380 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1381}
1382
1383static int nvme_pr_register(struct block_device *bdev, u64 old,
1384 u64 new, unsigned flags)
1385{
1386 u32 cdw10;
1387
1388 if (flags & ~PR_FL_IGNORE_KEY)
1389 return -EOPNOTSUPP;
1390
1391 cdw10 = old ? 2 : 0;
1392 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1393 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1394 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1395}
1396
1397static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1398 enum pr_type type, unsigned flags)
1399{
1400 u32 cdw10;
1401
1402 if (flags & ~PR_FL_IGNORE_KEY)
1403 return -EOPNOTSUPP;
1404
1405 cdw10 = nvme_pr_type(type) << 8;
1406 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1407 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1408}
1409
1410static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1411 enum pr_type type, bool abort)
1412{
1413 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1414 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1415}
1416
1417static int nvme_pr_clear(struct block_device *bdev, u64 key)
1418{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001419 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001420 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1421}
1422
1423static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1424{
1425 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1426 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1427}
1428
1429static const struct pr_ops nvme_pr_ops = {
1430 .pr_register = nvme_pr_register,
1431 .pr_reserve = nvme_pr_reserve,
1432 .pr_release = nvme_pr_release,
1433 .pr_preempt = nvme_pr_preempt,
1434 .pr_clear = nvme_pr_clear,
1435};
1436
Scott Bauera98e58e52017-02-03 12:50:32 -07001437#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001438int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1439 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001440{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001441 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001442 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001443
1444 memset(&cmd, 0, sizeof(cmd));
1445 if (send)
1446 cmd.common.opcode = nvme_admin_security_send;
1447 else
1448 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001449 cmd.common.nsid = 0;
1450 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1451 cmd.common.cdw10[1] = cpu_to_le32(len);
1452
1453 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1454 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1455}
1456EXPORT_SYMBOL_GPL(nvme_sec_submit);
1457#endif /* CONFIG_BLK_SED_OPAL */
1458
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001459static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001460 .owner = THIS_MODULE,
1461 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001462 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001463 .open = nvme_open,
1464 .release = nvme_release,
1465 .getgeo = nvme_getgeo,
1466 .revalidate_disk= nvme_revalidate_disk,
1467 .pr_ops = &nvme_pr_ops,
1468};
1469
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001470static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1471{
1472 unsigned long timeout =
1473 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1474 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1475 int ret;
1476
1477 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001478 if (csts == ~0)
1479 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001480 if ((csts & NVME_CSTS_RDY) == bit)
1481 break;
1482
1483 msleep(100);
1484 if (fatal_signal_pending(current))
1485 return -EINTR;
1486 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001487 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001488 "Device not ready; aborting %s\n", enabled ?
1489 "initialisation" : "reset");
1490 return -ENODEV;
1491 }
1492 }
1493
1494 return ret;
1495}
1496
1497/*
1498 * If the device has been passed off to us in an enabled state, just clear
1499 * the enabled bit. The spec says we should set the 'shutdown notification
1500 * bits', but doing so may cause the device to complete commands to the
1501 * admin queue ... and we don't know what memory that might be pointing at!
1502 */
1503int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1504{
1505 int ret;
1506
1507 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1508 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1509
1510 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1511 if (ret)
1512 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001513
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001514 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001515 msleep(NVME_QUIRK_DELAY_AMOUNT);
1516
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001517 return nvme_wait_ready(ctrl, cap, false);
1518}
Ming Lin576d55d2016-02-10 10:03:32 -08001519EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001520
1521int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1522{
1523 /*
1524 * Default to a 4K page size, with the intention to update this
1525 * path in the future to accomodate architectures with differing
1526 * kernel and IO page sizes.
1527 */
1528 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1529 int ret;
1530
1531 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001532 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001533 "Minimum device page size %u too large for host (%u)\n",
1534 1 << dev_page_min, 1 << page_shift);
1535 return -ENODEV;
1536 }
1537
1538 ctrl->page_size = 1 << page_shift;
1539
1540 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1541 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001542 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001543 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1544 ctrl->ctrl_config |= NVME_CC_ENABLE;
1545
1546 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1547 if (ret)
1548 return ret;
1549 return nvme_wait_ready(ctrl, cap, true);
1550}
Ming Lin576d55d2016-02-10 10:03:32 -08001551EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001552
1553int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1554{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001555 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001556 u32 csts;
1557 int ret;
1558
1559 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1560 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1561
1562 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1563 if (ret)
1564 return ret;
1565
1566 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1567 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1568 break;
1569
1570 msleep(100);
1571 if (fatal_signal_pending(current))
1572 return -EINTR;
1573 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001574 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001575 "Device shutdown incomplete; abort shutdown\n");
1576 return -ENODEV;
1577 }
1578 }
1579
1580 return ret;
1581}
Ming Lin576d55d2016-02-10 10:03:32 -08001582EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001583
Christoph Hellwigda358252016-03-02 18:07:11 +01001584static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1585 struct request_queue *q)
1586{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001587 bool vwc = false;
1588
Christoph Hellwigda358252016-03-02 18:07:11 +01001589 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001590 u32 max_segments =
1591 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1592
Christoph Hellwigda358252016-03-02 18:07:11 +01001593 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001594 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001595 }
Keith Busche6282ae2016-12-19 11:37:50 -05001596 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1597 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001598 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001599 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1600 vwc = true;
1601 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001602}
1603
Jon Derrickdbf86b32017-08-16 09:51:29 +02001604static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1605{
1606 __le64 ts;
1607 int ret;
1608
1609 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1610 return 0;
1611
1612 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1613 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1614 NULL);
1615 if (ret)
1616 dev_warn_once(ctrl->device,
1617 "could not set timestamp (%d)\n", ret);
1618 return ret;
1619}
1620
Keith Busch634b8322017-08-10 11:23:31 +02001621static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001622{
1623 /*
1624 * APST (Autonomous Power State Transition) lets us program a
1625 * table of power state transitions that the controller will
1626 * perform automatically. We configure it with a simple
1627 * heuristic: we are willing to spend at most 2% of the time
1628 * transitioning between power states. Therefore, when running
1629 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001630 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001631 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001632 * the requested maximum latency.
1633 *
1634 * We will not autonomously enter any non-operational state for
1635 * which the total latency exceeds ps_max_latency_us. Users
1636 * can set ps_max_latency_us to zero to turn off APST.
1637 */
1638
1639 unsigned apste;
1640 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001641 u64 max_lat_us = 0;
1642 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001643 int ret;
1644
1645 /*
1646 * If APST isn't supported or if we haven't been initialized yet,
1647 * then don't do anything.
1648 */
1649 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001650 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001651
1652 if (ctrl->npss > 31) {
1653 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001654 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001655 }
1656
1657 table = kzalloc(sizeof(*table), GFP_KERNEL);
1658 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001659 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001660
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001661 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001662 /* Turn off APST. */
1663 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001664 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001665 } else {
1666 __le64 target = cpu_to_le64(0);
1667 int state;
1668
1669 /*
1670 * Walk through all states from lowest- to highest-power.
1671 * According to the spec, lower-numbered states use more
1672 * power. NPSS, despite the name, is the index of the
1673 * lowest-power state, not the number of states.
1674 */
1675 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001676 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001677
1678 if (target)
1679 table->entries[state] = target;
1680
1681 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001682 * Don't allow transitions to the deepest state
1683 * if it's quirked off.
1684 */
1685 if (state == ctrl->npss &&
1686 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1687 continue;
1688
1689 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001690 * Is this state a useful non-operational state for
1691 * higher-power states to autonomously transition to?
1692 */
1693 if (!(ctrl->psd[state].flags &
1694 NVME_PS_FLAGS_NON_OP_STATE))
1695 continue;
1696
Kai-Heng Fengda875912017-06-07 15:25:42 +08001697 exit_latency_us =
1698 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1699 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001700 continue;
1701
Kai-Heng Fengda875912017-06-07 15:25:42 +08001702 total_latency_us =
1703 exit_latency_us +
1704 le32_to_cpu(ctrl->psd[state].entry_lat);
1705
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001706 /*
1707 * This state is good. Use it as the APST idle
1708 * target for higher power states.
1709 */
1710 transition_ms = total_latency_us + 19;
1711 do_div(transition_ms, 20);
1712 if (transition_ms > (1 << 24) - 1)
1713 transition_ms = (1 << 24) - 1;
1714
1715 target = cpu_to_le64((state << 3) |
1716 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001717
1718 if (max_ps == -1)
1719 max_ps = state;
1720
1721 if (total_latency_us > max_lat_us)
1722 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001723 }
1724
1725 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001726
1727 if (max_ps == -1) {
1728 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1729 } else {
1730 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1731 max_ps, max_lat_us, (int)sizeof(*table), table);
1732 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001733 }
1734
1735 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1736 table, sizeof(*table), NULL);
1737 if (ret)
1738 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1739
1740 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001741 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001742}
1743
1744static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1745{
1746 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1747 u64 latency;
1748
1749 switch (val) {
1750 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1751 case PM_QOS_LATENCY_ANY:
1752 latency = U64_MAX;
1753 break;
1754
1755 default:
1756 latency = val;
1757 }
1758
1759 if (ctrl->ps_max_latency_us != latency) {
1760 ctrl->ps_max_latency_us = latency;
1761 nvme_configure_apst(ctrl);
1762 }
1763}
1764
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001765struct nvme_core_quirk_entry {
1766 /*
1767 * NVMe model and firmware strings are padded with spaces. For
1768 * simplicity, strings in the quirk table are padded with NULLs
1769 * instead.
1770 */
1771 u16 vid;
1772 const char *mn;
1773 const char *fr;
1774 unsigned long quirks;
1775};
1776
1777static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001778 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001779 /*
1780 * This Toshiba device seems to die using any APST states. See:
1781 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1782 */
1783 .vid = 0x1179,
1784 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001785 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001786 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001787};
1788
1789/* match is null-terminated but idstr is space-padded. */
1790static bool string_matches(const char *idstr, const char *match, size_t len)
1791{
1792 size_t matchlen;
1793
1794 if (!match)
1795 return true;
1796
1797 matchlen = strlen(match);
1798 WARN_ON_ONCE(matchlen > len);
1799
1800 if (memcmp(idstr, match, matchlen))
1801 return false;
1802
1803 for (; matchlen < len; matchlen++)
1804 if (idstr[matchlen] != ' ')
1805 return false;
1806
1807 return true;
1808}
1809
1810static bool quirk_matches(const struct nvme_id_ctrl *id,
1811 const struct nvme_core_quirk_entry *q)
1812{
1813 return q->vid == le16_to_cpu(id->vid) &&
1814 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1815 string_matches(id->fr, q->fr, sizeof(id->fr));
1816}
1817
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001818static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
1819 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02001820{
1821 size_t nqnlen;
1822 int off;
1823
1824 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1825 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001826 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02001827 return;
1828 }
1829
1830 if (ctrl->vs >= NVME_VS(1, 2, 1))
1831 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1832
1833 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001834 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02001835 "nqn.2014.08.org.nvmexpress:%4x%4x",
1836 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001837 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001838 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001839 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001840 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001841 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
1842}
1843
1844static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
1845{
1846 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
1847 kfree(subsys);
1848}
1849
1850static void nvme_release_subsystem(struct device *dev)
1851{
1852 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
1853}
1854
1855static void nvme_destroy_subsystem(struct kref *ref)
1856{
1857 struct nvme_subsystem *subsys =
1858 container_of(ref, struct nvme_subsystem, ref);
1859
1860 mutex_lock(&nvme_subsystems_lock);
1861 list_del(&subsys->entry);
1862 mutex_unlock(&nvme_subsystems_lock);
1863
1864 device_del(&subsys->dev);
1865 put_device(&subsys->dev);
1866}
1867
1868static void nvme_put_subsystem(struct nvme_subsystem *subsys)
1869{
1870 kref_put(&subsys->ref, nvme_destroy_subsystem);
1871}
1872
1873static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
1874{
1875 struct nvme_subsystem *subsys;
1876
1877 lockdep_assert_held(&nvme_subsystems_lock);
1878
1879 list_for_each_entry(subsys, &nvme_subsystems, entry) {
1880 if (strcmp(subsys->subnqn, subsysnqn))
1881 continue;
1882 if (!kref_get_unless_zero(&subsys->ref))
1883 continue;
1884 return subsys;
1885 }
1886
1887 return NULL;
1888}
1889
1890static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1891{
1892 struct nvme_subsystem *subsys, *found;
1893 int ret;
1894
1895 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1896 if (!subsys)
1897 return -ENOMEM;
1898 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
1899 if (ret < 0) {
1900 kfree(subsys);
1901 return ret;
1902 }
1903 subsys->instance = ret;
1904 mutex_init(&subsys->lock);
1905 kref_init(&subsys->ref);
1906 INIT_LIST_HEAD(&subsys->ctrls);
1907 nvme_init_subnqn(subsys, ctrl, id);
1908 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
1909 memcpy(subsys->model, id->mn, sizeof(subsys->model));
1910 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
1911 subsys->vendor_id = le16_to_cpu(id->vid);
1912 subsys->cmic = id->cmic;
1913
1914 subsys->dev.class = nvme_subsys_class;
1915 subsys->dev.release = nvme_release_subsystem;
1916 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
1917 device_initialize(&subsys->dev);
1918
1919 mutex_lock(&nvme_subsystems_lock);
1920 found = __nvme_find_get_subsystem(subsys->subnqn);
1921 if (found) {
1922 /*
1923 * Verify that the subsystem actually supports multiple
1924 * controllers, else bail out.
1925 */
1926 if (!(id->cmic & (1 << 1))) {
1927 dev_err(ctrl->device,
1928 "ignoring ctrl due to duplicate subnqn (%s).\n",
1929 found->subnqn);
1930 nvme_put_subsystem(found);
1931 ret = -EINVAL;
1932 goto out_unlock;
1933 }
1934
1935 __nvme_release_subsystem(subsys);
1936 subsys = found;
1937 } else {
1938 ret = device_add(&subsys->dev);
1939 if (ret) {
1940 dev_err(ctrl->device,
1941 "failed to register subsystem device.\n");
1942 goto out_unlock;
1943 }
1944 list_add_tail(&subsys->entry, &nvme_subsystems);
1945 }
1946
1947 ctrl->subsys = subsys;
1948 mutex_unlock(&nvme_subsystems_lock);
1949
1950 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
1951 dev_name(ctrl->device))) {
1952 dev_err(ctrl->device,
1953 "failed to create sysfs link from subsystem.\n");
1954 /* the transport driver will eventually put the subsystem */
1955 return -EINVAL;
1956 }
1957
1958 mutex_lock(&subsys->lock);
1959 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1960 mutex_unlock(&subsys->lock);
1961
1962 return 0;
1963
1964out_unlock:
1965 mutex_unlock(&nvme_subsystems_lock);
1966 put_device(&subsys->dev);
1967 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02001968}
1969
Keith Buschc627c482017-11-07 10:28:31 -07001970static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
1971 size_t size)
1972{
1973 struct nvme_command c = { };
1974
1975 c.common.opcode = nvme_admin_get_log_page;
1976 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
1977 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
1978
1979 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
1980}
1981
Keith Busch84fef622017-11-07 10:28:32 -07001982static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
1983{
1984 int ret;
1985
1986 if (!ctrl->effects)
1987 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
1988
1989 if (!ctrl->effects)
1990 return 0;
1991
1992 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
1993 sizeof(*ctrl->effects));
1994 if (ret) {
1995 kfree(ctrl->effects);
1996 ctrl->effects = NULL;
1997 }
1998 return ret;
1999}
2000
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002001/*
2002 * Initialize the cached copies of the Identify data and various controller
2003 * register in our nvme_ctrl structure. This should be called as soon as
2004 * the admin queue is fully up and running.
2005 */
2006int nvme_init_identify(struct nvme_ctrl *ctrl)
2007{
2008 struct nvme_id_ctrl *id;
2009 u64 cap;
2010 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002011 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002012 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002013
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002014 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2015 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002016 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002017 return ret;
2018 }
2019
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002020 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2021 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002022 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002023 return ret;
2024 }
2025 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2026
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002027 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002028 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2029
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002030 ret = nvme_identify_ctrl(ctrl, &id);
2031 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002032 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002033 return -EIO;
2034 }
2035
Keith Busch84fef622017-11-07 10:28:32 -07002036 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2037 ret = nvme_get_effects_log(ctrl);
2038 if (ret < 0)
2039 return ret;
2040 }
2041
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002042 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002043 int i;
2044
2045 ret = nvme_init_subsystem(ctrl, id);
2046 if (ret)
2047 goto out_free;
2048
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002049 /*
2050 * Check for quirks. Quirk can depend on firmware version,
2051 * so, in principle, the set of quirks present can change
2052 * across a reset. As a possible future enhancement, we
2053 * could re-scan for quirks every time we reinitialize
2054 * the device, but we'd have to make sure that the driver
2055 * behaves intelligently if the quirks change.
2056 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002057 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2058 if (quirk_matches(id, &core_quirks[i]))
2059 ctrl->quirks |= core_quirks[i].quirks;
2060 }
2061 }
2062
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002063 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002064 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 -07002065 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2066 }
2067
Scott Bauer8a9ae522017-02-17 13:59:40 +01002068 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002069 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002070 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002071 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002072 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002073 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002074 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002075 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002076 max_hw_sectors = UINT_MAX;
2077 ctrl->max_hw_sectors =
2078 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002079
Christoph Hellwigda358252016-03-02 18:07:11 +01002080 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002081 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002082 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002083
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002084 if (id->rtd3e) {
2085 /* us -> s */
2086 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2087
2088 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2089 shutdown_timeout, 60);
2090
2091 if (ctrl->shutdown_timeout != shutdown_timeout)
2092 dev_warn(ctrl->device,
2093 "Shutdown timeout set to %u seconds\n",
2094 ctrl->shutdown_timeout);
2095 } else
2096 ctrl->shutdown_timeout = shutdown_timeout;
2097
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002098 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002099 ctrl->apsta = id->apsta;
2100 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002101 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2102 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002103 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 -04002104 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002105 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002106 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002107 }
2108 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002109 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002110 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002111 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2112
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002113 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002114 ctrl->icdoff = le16_to_cpu(id->icdoff);
2115 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2116 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2117 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2118
2119 /*
2120 * In fabrics we need to verify the cntlid matches the
2121 * admin connect
2122 */
Keith Busch634b8322017-08-10 11:23:31 +02002123 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002124 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002125 goto out_free;
2126 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002127
2128 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002129 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002130 "keep-alive support is mandatory for fabrics\n");
2131 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002132 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002133 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002134 } else {
2135 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002136 ctrl->hmpre = le32_to_cpu(id->hmpre);
2137 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002138 ctrl->hmminds = le32_to_cpu(id->hmminds);
2139 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002140 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002141
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002142 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002143
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002144 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002145 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002146 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002147 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2148
Keith Busch634b8322017-08-10 11:23:31 +02002149 ret = nvme_configure_apst(ctrl);
2150 if (ret < 0)
2151 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002152
2153 ret = nvme_configure_timestamp(ctrl);
2154 if (ret < 0)
2155 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002156
2157 ret = nvme_configure_directives(ctrl);
2158 if (ret < 0)
2159 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002160
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002161 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002162
Keith Busch634b8322017-08-10 11:23:31 +02002163 return 0;
2164
2165out_free:
2166 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002167 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002168}
Ming Lin576d55d2016-02-10 10:03:32 -08002169EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002170
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002171static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002172{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002173 struct nvme_ctrl *ctrl =
2174 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002175
Christoph Hellwig999ada22017-10-18 17:09:31 +02002176 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002177 return -EWOULDBLOCK;
2178 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002179 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002180}
2181
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002182static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2183{
2184 struct nvme_ns *ns;
2185 int ret;
2186
2187 mutex_lock(&ctrl->namespaces_mutex);
2188 if (list_empty(&ctrl->namespaces)) {
2189 ret = -ENOTTY;
2190 goto out_unlock;
2191 }
2192
2193 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2194 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002195 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002196 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2197 ret = -EINVAL;
2198 goto out_unlock;
2199 }
2200
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002201 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002202 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2203 kref_get(&ns->kref);
2204 mutex_unlock(&ctrl->namespaces_mutex);
2205
2206 ret = nvme_user_cmd(ctrl, ns, argp);
2207 nvme_put_ns(ns);
2208 return ret;
2209
2210out_unlock:
2211 mutex_unlock(&ctrl->namespaces_mutex);
2212 return ret;
2213}
2214
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002215static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2216 unsigned long arg)
2217{
2218 struct nvme_ctrl *ctrl = file->private_data;
2219 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002220
2221 switch (cmd) {
2222 case NVME_IOCTL_ADMIN_CMD:
2223 return nvme_user_cmd(ctrl, NULL, argp);
2224 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002225 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002226 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002227 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002228 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002229 case NVME_IOCTL_SUBSYS_RESET:
2230 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002231 case NVME_IOCTL_RESCAN:
2232 nvme_queue_scan(ctrl);
2233 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002234 default:
2235 return -ENOTTY;
2236 }
2237}
2238
2239static const struct file_operations nvme_dev_fops = {
2240 .owner = THIS_MODULE,
2241 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002242 .unlocked_ioctl = nvme_dev_ioctl,
2243 .compat_ioctl = nvme_dev_ioctl,
2244};
2245
2246static ssize_t nvme_sysfs_reset(struct device *dev,
2247 struct device_attribute *attr, const char *buf,
2248 size_t count)
2249{
2250 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2251 int ret;
2252
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002253 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002254 if (ret < 0)
2255 return ret;
2256 return count;
2257}
2258static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2259
Keith Busch9ec3bb22016-04-29 15:45:18 -06002260static ssize_t nvme_sysfs_rescan(struct device *dev,
2261 struct device_attribute *attr, const char *buf,
2262 size_t count)
2263{
2264 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2265
2266 nvme_queue_scan(ctrl);
2267 return count;
2268}
2269static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2270
Keith Busch118472a2016-02-18 09:57:48 -07002271static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2272 char *buf)
2273{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002274 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwig002fab02017-11-09 13:50:16 +01002275 struct nvme_ns_ids *ids = &ns->ids;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002276 struct nvme_subsystem *subsys = ns->ctrl->subsys;
2277 int serial_len = sizeof(subsys->serial);
2278 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002279
Christoph Hellwig002fab02017-11-09 13:50:16 +01002280 if (!uuid_is_null(&ids->uuid))
2281 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002282
Christoph Hellwig002fab02017-11-09 13:50:16 +01002283 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2284 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002285
Christoph Hellwig002fab02017-11-09 13:50:16 +01002286 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2287 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002288
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002289 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2290 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002291 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002292 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2293 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002294 model_len--;
2295
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002296 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2297 serial_len, subsys->serial, model_len, subsys->model,
2298 ns->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002299}
2300static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2301
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002302static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2303 char *buf)
2304{
2305 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwig002fab02017-11-09 13:50:16 +01002306 return sprintf(buf, "%pU\n", ns->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002307}
2308static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2309
Keith Busch2b9b6e82015-12-22 10:10:45 -07002310static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2311 char *buf)
2312{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002313 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwig002fab02017-11-09 13:50:16 +01002314 struct nvme_ns_ids *ids = &ns->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002315
2316 /* For backward compatibility expose the NGUID to userspace if
2317 * we have no UUID set
2318 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002319 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002320 printk_ratelimited(KERN_WARNING
2321 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002322 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002323 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002324 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002325}
2326static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2327
2328static ssize_t eui_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);
Christoph Hellwig002fab02017-11-09 13:50:16 +01002332 return sprintf(buf, "%8ph\n", ns->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002333}
2334static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2335
2336static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2337 char *buf)
2338{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002339 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002340 return sprintf(buf, "%d\n", ns->ns_id);
2341}
2342static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2343
2344static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002345 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002346 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002347 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002348 &dev_attr_eui.attr,
2349 &dev_attr_nsid.attr,
2350 NULL,
2351};
2352
Ming Lin1a353d82016-06-13 16:45:24 +02002353static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002354 struct attribute *a, int n)
2355{
2356 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002357 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwig002fab02017-11-09 13:50:16 +01002358 struct nvme_ns_ids *ids = &ns->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002359
2360 if (a == &dev_attr_uuid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002361 if (uuid_is_null(&ids->uuid) ||
2362 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002363 return 0;
2364 }
2365 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002366 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002367 return 0;
2368 }
2369 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002370 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002371 return 0;
2372 }
2373 return a->mode;
2374}
2375
2376static const struct attribute_group nvme_ns_attr_group = {
2377 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002378 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002379};
2380
Ming Lin931e1c22016-02-26 13:24:19 -08002381#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002382static ssize_t field##_show(struct device *dev, \
2383 struct device_attribute *attr, char *buf) \
2384{ \
2385 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002386 return sprintf(buf, "%.*s\n", \
2387 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002388} \
2389static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2390
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002391nvme_show_str_function(model);
2392nvme_show_str_function(serial);
2393nvme_show_str_function(firmware_rev);
2394
Ming Lin931e1c22016-02-26 13:24:19 -08002395#define nvme_show_int_function(field) \
2396static ssize_t field##_show(struct device *dev, \
2397 struct device_attribute *attr, char *buf) \
2398{ \
2399 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2400 return sprintf(buf, "%d\n", ctrl->field); \
2401} \
2402static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2403
Ming Lin931e1c22016-02-26 13:24:19 -08002404nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002405
Ming Lin1a353d82016-06-13 16:45:24 +02002406static ssize_t nvme_sysfs_delete(struct device *dev,
2407 struct device_attribute *attr, const char *buf,
2408 size_t count)
2409{
2410 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2411
2412 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002413 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002414 return count;
2415}
2416static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2417
2418static ssize_t nvme_sysfs_show_transport(struct device *dev,
2419 struct device_attribute *attr,
2420 char *buf)
2421{
2422 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2423
2424 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2425}
2426static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2427
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002428static ssize_t nvme_sysfs_show_state(struct device *dev,
2429 struct device_attribute *attr,
2430 char *buf)
2431{
2432 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2433 static const char *const state_name[] = {
2434 [NVME_CTRL_NEW] = "new",
2435 [NVME_CTRL_LIVE] = "live",
2436 [NVME_CTRL_RESETTING] = "resetting",
2437 [NVME_CTRL_RECONNECTING]= "reconnecting",
2438 [NVME_CTRL_DELETING] = "deleting",
2439 [NVME_CTRL_DEAD] = "dead",
2440 };
2441
2442 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2443 state_name[ctrl->state])
2444 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2445
2446 return sprintf(buf, "unknown state\n");
2447}
2448
2449static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2450
Ming Lin1a353d82016-06-13 16:45:24 +02002451static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2452 struct device_attribute *attr,
2453 char *buf)
2454{
2455 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2456
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002457 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002458}
2459static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2460
2461static ssize_t nvme_sysfs_show_address(struct device *dev,
2462 struct device_attribute *attr,
2463 char *buf)
2464{
2465 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2466
2467 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2468}
2469static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2470
Keith Busch779ff7562016-01-12 15:09:31 -07002471static struct attribute *nvme_dev_attrs[] = {
2472 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002473 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002474 &dev_attr_model.attr,
2475 &dev_attr_serial.attr,
2476 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002477 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002478 &dev_attr_delete_controller.attr,
2479 &dev_attr_transport.attr,
2480 &dev_attr_subsysnqn.attr,
2481 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002482 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002483 NULL
2484};
2485
Ming Lin1a353d82016-06-13 16:45:24 +02002486static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2487 struct attribute *a, int n)
2488{
2489 struct device *dev = container_of(kobj, struct device, kobj);
2490 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2491
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002492 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2493 return 0;
2494 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2495 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002496
2497 return a->mode;
2498}
2499
Keith Busch779ff7562016-01-12 15:09:31 -07002500static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002501 .attrs = nvme_dev_attrs,
2502 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002503};
2504
2505static const struct attribute_group *nvme_dev_attr_groups[] = {
2506 &nvme_dev_attrs_group,
2507 NULL,
2508};
2509
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002510static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2511{
2512 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2513 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2514
2515 return nsa->ns_id - nsb->ns_id;
2516}
2517
Keith Busch32f0c4a2016-07-13 11:45:02 -06002518static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002519{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002520 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002521
Keith Busch32f0c4a2016-07-13 11:45:02 -06002522 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002523 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002524 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002525 if (!kref_get_unless_zero(&ns->kref))
2526 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002527 ret = ns;
2528 break;
2529 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002530 if (ns->ns_id > nsid)
2531 break;
2532 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002533 mutex_unlock(&ctrl->namespaces_mutex);
2534 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002535}
2536
Jens Axboef5d11842017-06-27 12:03:06 -06002537static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2538{
2539 struct streams_directive_params s;
2540 int ret;
2541
2542 if (!ctrl->nr_streams)
2543 return 0;
2544
2545 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2546 if (ret)
2547 return ret;
2548
2549 ns->sws = le32_to_cpu(s.sws);
2550 ns->sgs = le16_to_cpu(s.sgs);
2551
2552 if (ns->sws) {
2553 unsigned int bs = 1 << ns->lba_shift;
2554
2555 blk_queue_io_min(ns->queue, bs * ns->sws);
2556 if (ns->sgs)
2557 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2558 }
2559
2560 return 0;
2561}
2562
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002563static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2564{
2565 struct nvme_ns *ns;
2566 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002567 struct nvme_id_ns *id;
2568 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002569 int node = dev_to_node(ctrl->dev);
2570
2571 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2572 if (!ns)
2573 return;
2574
Keith Busch075790e2016-02-24 09:15:53 -07002575 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2576 if (ns->instance < 0)
2577 goto out_free_ns;
2578
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002579 ns->queue = blk_mq_init_queue(ctrl->tagset);
2580 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002581 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002582 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2583 ns->queue->queuedata = ns;
2584 ns->ctrl = ctrl;
2585
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002586 kref_init(&ns->kref);
2587 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002588 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002589
2590 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002591 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002592 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002593
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002594 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002595
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002596 id = nvme_identify_ns(ctrl, nsid);
2597 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002598 goto out_free_queue;
2599
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002600 if (id->ncap == 0)
2601 goto out_free_id;
2602
Christoph Hellwig002fab02017-11-09 13:50:16 +01002603 nvme_report_ns_ids(ctrl, ns->ns_id, id, &ns->ids);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002604
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002605 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2606 if (nvme_nvm_register(ns, disk_name, node)) {
2607 dev_warn(ctrl->device, "LightNVM init failure\n");
2608 goto out_free_id;
2609 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002610 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002611
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002612 disk = alloc_disk_node(0, node);
2613 if (!disk)
2614 goto out_free_id;
2615
2616 disk->fops = &nvme_fops;
2617 disk->private_data = ns;
2618 disk->queue = ns->queue;
2619 disk->flags = GENHD_FL_EXT_DEVT;
2620 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2621 ns->disk = disk;
2622
2623 __nvme_revalidate_disk(disk, id);
2624
Keith Busch32f0c4a2016-07-13 11:45:02 -06002625 mutex_lock(&ctrl->namespaces_mutex);
2626 list_add_tail(&ns->list, &ctrl->namespaces);
2627 mutex_unlock(&ctrl->namespaces_mutex);
2628
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002629 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002630
2631 kfree(id);
2632
Dan Williams0d52c7562016-06-15 19:44:20 -07002633 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002634 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2635 &nvme_ns_attr_group))
2636 pr_warn("%s: failed to create sysfs group for identification\n",
2637 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002638 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2639 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2640 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002641 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002642 out_free_id:
2643 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002644 out_free_queue:
2645 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002646 out_release_instance:
2647 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002648 out_free_ns:
2649 kfree(ns);
2650}
2651
2652static void nvme_ns_remove(struct nvme_ns *ns)
2653{
Keith Busch646017a2016-02-24 09:15:54 -07002654 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2655 return;
2656
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002657 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002658 if (blk_get_integrity(ns->disk))
2659 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002660 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2661 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002662 if (ns->ndev)
2663 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002664 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002665 blk_cleanup_queue(ns->queue);
2666 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002667
2668 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002669 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002670 mutex_unlock(&ns->ctrl->namespaces_mutex);
2671
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002672 nvme_put_ns(ns);
2673}
2674
Keith Busch540c8012015-10-22 15:45:06 -06002675static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2676{
2677 struct nvme_ns *ns;
2678
Keith Busch32f0c4a2016-07-13 11:45:02 -06002679 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002680 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002681 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002682 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002683 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002684 } else
2685 nvme_alloc_ns(ctrl, nsid);
2686}
2687
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302688static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2689 unsigned nsid)
2690{
2691 struct nvme_ns *ns, *next;
2692
2693 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2694 if (ns->ns_id > nsid)
2695 nvme_ns_remove(ns);
2696 }
2697}
2698
Keith Busch540c8012015-10-22 15:45:06 -06002699static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2700{
2701 struct nvme_ns *ns;
2702 __le32 *ns_list;
2703 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2704 int ret = 0;
2705
2706 ns_list = kzalloc(0x1000, GFP_KERNEL);
2707 if (!ns_list)
2708 return -ENOMEM;
2709
2710 for (i = 0; i < num_lists; i++) {
2711 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2712 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302713 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002714
2715 for (j = 0; j < min(nn, 1024U); j++) {
2716 nsid = le32_to_cpu(ns_list[j]);
2717 if (!nsid)
2718 goto out;
2719
2720 nvme_validate_ns(ctrl, nsid);
2721
2722 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002723 ns = nvme_find_get_ns(ctrl, prev);
2724 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002725 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002726 nvme_put_ns(ns);
2727 }
Keith Busch540c8012015-10-22 15:45:06 -06002728 }
2729 }
2730 nn -= j;
2731 }
2732 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302733 nvme_remove_invalid_namespaces(ctrl, prev);
2734 free:
Keith Busch540c8012015-10-22 15:45:06 -06002735 kfree(ns_list);
2736 return ret;
2737}
2738
Christoph Hellwig5955be22016-04-26 13:51:59 +02002739static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002740{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002741 unsigned i;
2742
Keith Busch540c8012015-10-22 15:45:06 -06002743 for (i = 1; i <= nn; i++)
2744 nvme_validate_ns(ctrl, i);
2745
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302746 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002747}
2748
Christoph Hellwig5955be22016-04-26 13:51:59 +02002749static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002750{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002751 struct nvme_ctrl *ctrl =
2752 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002753 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002754 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002755
Christoph Hellwig5955be22016-04-26 13:51:59 +02002756 if (ctrl->state != NVME_CTRL_LIVE)
2757 return;
2758
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002759 if (nvme_identify_ctrl(ctrl, &id))
2760 return;
Keith Busch540c8012015-10-22 15:45:06 -06002761
2762 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002763 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002764 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2765 if (!nvme_scan_ns_list(ctrl, nn))
2766 goto done;
2767 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002768 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002769 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002770 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002771 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002772 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002773 kfree(id);
2774}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002775
2776void nvme_queue_scan(struct nvme_ctrl *ctrl)
2777{
2778 /*
2779 * Do not queue new scan work when a controller is reset during
2780 * removal.
2781 */
2782 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002783 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002784}
2785EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002786
Keith Busch32f0c4a2016-07-13 11:45:02 -06002787/*
2788 * This function iterates the namespace list unlocked to allow recovery from
2789 * controller failure. It is up to the caller to ensure the namespace list is
2790 * not modified by scan work while this function is executing.
2791 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002792void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2793{
2794 struct nvme_ns *ns, *next;
2795
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002796 /*
2797 * The dead states indicates the controller was not gracefully
2798 * disconnected. In that case, we won't be able to flush any data while
2799 * removing the namespaces' disks; fail all the queues now to avoid
2800 * potentially having to clean up the failed sync later.
2801 */
2802 if (ctrl->state == NVME_CTRL_DEAD)
2803 nvme_kill_queues(ctrl);
2804
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002805 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2806 nvme_ns_remove(ns);
2807}
Ming Lin576d55d2016-02-10 10:03:32 -08002808EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002809
Keith Busche3d78742017-11-07 15:13:14 -07002810static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
2811{
2812 char *envp[2] = { NULL, NULL };
2813 u32 aen_result = ctrl->aen_result;
2814
2815 ctrl->aen_result = 0;
2816 if (!aen_result)
2817 return;
2818
2819 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
2820 if (!envp[0])
2821 return;
2822 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
2823 kfree(envp[0]);
2824}
2825
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002826static void nvme_async_event_work(struct work_struct *work)
2827{
2828 struct nvme_ctrl *ctrl =
2829 container_of(work, struct nvme_ctrl, async_event_work);
2830
Keith Busche3d78742017-11-07 15:13:14 -07002831 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07002832 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002833}
2834
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302835static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2836{
2837
2838 u32 csts;
2839
2840 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2841 return false;
2842
2843 if (csts == ~0)
2844 return false;
2845
2846 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2847}
2848
2849static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2850{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302851 struct nvme_fw_slot_info_log *log;
2852
2853 log = kmalloc(sizeof(*log), GFP_KERNEL);
2854 if (!log)
2855 return;
2856
Keith Buschc627c482017-11-07 10:28:31 -07002857 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302858 dev_warn(ctrl->device,
2859 "Get FW SLOT INFO log error\n");
2860 kfree(log);
2861}
2862
2863static void nvme_fw_act_work(struct work_struct *work)
2864{
2865 struct nvme_ctrl *ctrl = container_of(work,
2866 struct nvme_ctrl, fw_act_work);
2867 unsigned long fw_act_timeout;
2868
2869 if (ctrl->mtfa)
2870 fw_act_timeout = jiffies +
2871 msecs_to_jiffies(ctrl->mtfa * 100);
2872 else
2873 fw_act_timeout = jiffies +
2874 msecs_to_jiffies(admin_timeout * 1000);
2875
2876 nvme_stop_queues(ctrl);
2877 while (nvme_ctrl_pp_status(ctrl)) {
2878 if (time_after(jiffies, fw_act_timeout)) {
2879 dev_warn(ctrl->device,
2880 "Fw activation timeout, reset controller\n");
2881 nvme_reset_ctrl(ctrl);
2882 break;
2883 }
2884 msleep(100);
2885 }
2886
2887 if (ctrl->state != NVME_CTRL_LIVE)
2888 return;
2889
2890 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09002891 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302892 nvme_get_fw_slot_info(ctrl);
2893}
2894
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002895void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2896 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002897{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002898 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002899
Keith Buschad22c352017-11-07 15:13:12 -07002900 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002901 return;
2902
Keith Busche3d78742017-11-07 15:13:14 -07002903 switch (result & 0x7) {
2904 case NVME_AER_ERROR:
2905 case NVME_AER_SMART:
2906 case NVME_AER_CSS:
2907 case NVME_AER_VS:
2908 ctrl->aen_result = result;
2909 break;
2910 default:
2911 break;
2912 }
2913
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002914 switch (result & 0xff07) {
2915 case NVME_AER_NOTICE_NS_CHANGED:
2916 dev_info(ctrl->device, "rescanning\n");
2917 nvme_queue_scan(ctrl);
2918 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302919 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002920 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302921 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002922 default:
2923 dev_warn(ctrl->device, "async event result %08x\n", result);
2924 }
Keith Buschad22c352017-11-07 15:13:12 -07002925 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002926}
2927EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2928
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002929void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002930{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002931 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002932 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002933 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302934 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002935}
2936EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002937
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002938void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2939{
2940 if (ctrl->kato)
2941 nvme_start_keep_alive(ctrl);
2942
2943 if (ctrl->queue_count > 1) {
2944 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07002945 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002946 nvme_start_queues(ctrl);
2947 }
2948}
2949EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2950
2951void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2952{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002953 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002954}
Ming Lin576d55d2016-02-10 10:03:32 -08002955EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002956
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002957static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002958{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002959 struct nvme_ctrl *ctrl =
2960 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002961 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002962
Christoph Hellwig9843f682017-10-18 13:10:01 +02002963 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002964 ida_destroy(&ctrl->ns_ida);
Keith Busch84fef622017-11-07 10:28:32 -07002965 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002966
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002967 if (subsys) {
2968 mutex_lock(&subsys->lock);
2969 list_del(&ctrl->subsys_entry);
2970 mutex_unlock(&subsys->lock);
2971 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
2972 }
2973
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002974 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002975
2976 if (subsys)
2977 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002978}
2979
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002980/*
2981 * Initialize a NVMe controller structures. This needs to be called during
2982 * earliest initialization so that we have the initialized structured around
2983 * during probing.
2984 */
2985int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2986 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2987{
2988 int ret;
2989
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002990 ctrl->state = NVME_CTRL_NEW;
2991 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002992 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002993 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002994 ctrl->dev = dev;
2995 ctrl->ops = ops;
2996 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002997 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002998 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302999 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003000 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003001
Christoph Hellwig9843f682017-10-18 13:10:01 +02003002 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3003 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003004 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003005 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003006
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003007 device_initialize(&ctrl->ctrl_device);
3008 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003009 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003010 ctrl->device->class = nvme_class;
3011 ctrl->device->parent = ctrl->dev;
3012 ctrl->device->groups = nvme_dev_attr_groups;
3013 ctrl->device->release = nvme_free_ctrl;
3014 dev_set_drvdata(ctrl->device, ctrl);
3015 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3016 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003017 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003018
3019 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3020 ctrl->cdev.owner = ops->module;
3021 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003022 if (ret)
3023 goto out_free_name;
3024
Keith Busch075790e2016-02-24 09:15:53 -07003025 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003026
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003027 /*
3028 * Initialize latency tolerance controls. The sysfs files won't
3029 * be visible to userspace unless the device actually supports APST.
3030 */
3031 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3032 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3033 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003035 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003036out_free_name:
3037 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003038out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003039 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003040out:
3041 return ret;
3042}
Ming Lin576d55d2016-02-10 10:03:32 -08003043EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003044
Keith Busch69d9a992016-02-24 09:15:56 -07003045/**
3046 * nvme_kill_queues(): Ends all namespace queues
3047 * @ctrl: the dead controller that needs to end
3048 *
3049 * Call this function when the driver determines it is unable to get the
3050 * controller in a state capable of servicing IO.
3051 */
3052void nvme_kill_queues(struct nvme_ctrl *ctrl)
3053{
3054 struct nvme_ns *ns;
3055
Keith Busch32f0c4a2016-07-13 11:45:02 -06003056 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08003057
Ming Lei443bd902017-06-19 10:21:08 +08003058 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003059 if (ctrl->admin_q)
3060 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003061
Keith Busch32f0c4a2016-07-13 11:45:02 -06003062 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003063 /*
3064 * Revalidating a dead namespace sets capacity to 0. This will
3065 * end buffered writers dirtying pages that can't be synced.
3066 */
Keith Buschf33447b2017-02-10 18:15:51 -05003067 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3068 continue;
3069 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003070 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003071
Ming Lei443bd902017-06-19 10:21:08 +08003072 /* Forcibly unquiesce queues to avoid blocking dispatch */
3073 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003074 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003075 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07003076}
Linus Torvalds237045f2016-03-18 17:13:31 -07003077EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003078
Keith Busch302ad8c2017-03-01 14:22:12 -05003079void nvme_unfreeze(struct nvme_ctrl *ctrl)
3080{
3081 struct nvme_ns *ns;
3082
3083 mutex_lock(&ctrl->namespaces_mutex);
3084 list_for_each_entry(ns, &ctrl->namespaces, list)
3085 blk_mq_unfreeze_queue(ns->queue);
3086 mutex_unlock(&ctrl->namespaces_mutex);
3087}
3088EXPORT_SYMBOL_GPL(nvme_unfreeze);
3089
3090void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3091{
3092 struct nvme_ns *ns;
3093
3094 mutex_lock(&ctrl->namespaces_mutex);
3095 list_for_each_entry(ns, &ctrl->namespaces, list) {
3096 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3097 if (timeout <= 0)
3098 break;
3099 }
3100 mutex_unlock(&ctrl->namespaces_mutex);
3101}
3102EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3103
3104void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3105{
3106 struct nvme_ns *ns;
3107
3108 mutex_lock(&ctrl->namespaces_mutex);
3109 list_for_each_entry(ns, &ctrl->namespaces, list)
3110 blk_mq_freeze_queue_wait(ns->queue);
3111 mutex_unlock(&ctrl->namespaces_mutex);
3112}
3113EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3114
3115void nvme_start_freeze(struct nvme_ctrl *ctrl)
3116{
3117 struct nvme_ns *ns;
3118
3119 mutex_lock(&ctrl->namespaces_mutex);
3120 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003121 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05003122 mutex_unlock(&ctrl->namespaces_mutex);
3123}
3124EXPORT_SYMBOL_GPL(nvme_start_freeze);
3125
Keith Busch25646262016-01-04 09:10:57 -07003126void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003127{
3128 struct nvme_ns *ns;
3129
Keith Busch32f0c4a2016-07-13 11:45:02 -06003130 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003131 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003132 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003133 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003134}
Ming Lin576d55d2016-02-10 10:03:32 -08003135EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003136
Keith Busch25646262016-01-04 09:10:57 -07003137void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003138{
3139 struct nvme_ns *ns;
3140
Keith Busch32f0c4a2016-07-13 11:45:02 -06003141 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003142 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003143 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003144 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003145}
Ming Lin576d55d2016-02-10 10:03:32 -08003146EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003147
Sagi Grimberg31b84462017-10-11 12:53:07 +03003148int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3149{
3150 if (!ctrl->ops->reinit_request)
3151 return 0;
3152
3153 return blk_mq_tagset_iter(set, set->driver_data,
3154 ctrl->ops->reinit_request);
3155}
3156EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3157
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003158int __init nvme_core_init(void)
3159{
3160 int result;
3161
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003162 nvme_wq = alloc_workqueue("nvme-wq",
3163 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3164 if (!nvme_wq)
3165 return -ENOMEM;
3166
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003167 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003168 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003169 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003170
3171 nvme_class = class_create(THIS_MODULE, "nvme");
3172 if (IS_ERR(nvme_class)) {
3173 result = PTR_ERR(nvme_class);
3174 goto unregister_chrdev;
3175 }
3176
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003177 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3178 if (IS_ERR(nvme_subsys_class)) {
3179 result = PTR_ERR(nvme_subsys_class);
3180 goto destroy_class;
3181 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003182 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003183
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003184destroy_class:
3185 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003186unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003187 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003188destroy_wq:
3189 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003190 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003191}
3192
3193void nvme_core_exit(void)
3194{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003195 ida_destroy(&nvme_subsystems_ida);
3196 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003197 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003198 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003199 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003200}
Ming Lin576d55d2016-02-10 10:03:32 -08003201
3202MODULE_LICENSE("GPL");
3203MODULE_VERSION("1.0");
3204module_init(nvme_core_init);
3205module_exit(nvme_core_exit);