blob: 13676f6cd4f67a2e96e27f796e87c6d16201e173 [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 Hellwiged754e52017-11-09 13:50:43 +0100294static void nvme_free_ns_head(struct kref *ref)
295{
296 struct nvme_ns_head *head =
297 container_of(ref, struct nvme_ns_head, ref);
298
299 ida_simple_remove(&head->subsys->ns_ida, head->instance);
300 list_del_init(&head->entry);
301 cleanup_srcu_struct(&head->srcu);
302 kfree(head);
303}
304
305static void nvme_put_ns_head(struct nvme_ns_head *head)
306{
307 kref_put(&head->ref, nvme_free_ns_head);
308}
309
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100310static void nvme_free_ns(struct kref *kref)
311{
312 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
313
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200314 if (ns->ndev)
315 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100316
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100317 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100318 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700319 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100320 kfree(ns);
321}
322
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100323static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100324{
325 kref_put(&ns->kref, nvme_free_ns);
326}
327
Christoph Hellwig41609822015-11-20 09:00:02 +0100328struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800329 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100330{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100331 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100332 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100333
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200334 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100335 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200336 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100337 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200338 qid ? qid - 1 : 0);
339 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100340 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100341 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100342
Christoph Hellwig21d34712015-11-26 09:08:36 +0100343 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800344 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100345
Christoph Hellwig41609822015-11-20 09:00:02 +0100346 return req;
347}
Ming Lin576d55d2016-02-10 10:03:32 -0800348EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100349
Jens Axboef5d11842017-06-27 12:03:06 -0600350static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
351{
352 struct nvme_command c;
353
354 memset(&c, 0, sizeof(c));
355
356 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530357 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600358 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
359 c.directive.dtype = NVME_DIR_IDENTIFY;
360 c.directive.tdtype = NVME_DIR_STREAMS;
361 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
362
363 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
364}
365
366static int nvme_disable_streams(struct nvme_ctrl *ctrl)
367{
368 return nvme_toggle_streams(ctrl, false);
369}
370
371static int nvme_enable_streams(struct nvme_ctrl *ctrl)
372{
373 return nvme_toggle_streams(ctrl, true);
374}
375
376static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
377 struct streams_directive_params *s, u32 nsid)
378{
379 struct nvme_command c;
380
381 memset(&c, 0, sizeof(c));
382 memset(s, 0, sizeof(*s));
383
384 c.directive.opcode = nvme_admin_directive_recv;
385 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700386 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600387 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
388 c.directive.dtype = NVME_DIR_STREAMS;
389
390 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
391}
392
393static int nvme_configure_directives(struct nvme_ctrl *ctrl)
394{
395 struct streams_directive_params s;
396 int ret;
397
398 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
399 return 0;
400 if (!streams)
401 return 0;
402
403 ret = nvme_enable_streams(ctrl);
404 if (ret)
405 return ret;
406
Arnav Dawn62346ea2017-07-12 16:11:53 +0530407 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600408 if (ret)
409 return ret;
410
411 ctrl->nssa = le16_to_cpu(s.nssa);
412 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
413 dev_info(ctrl->device, "too few streams (%u) available\n",
414 ctrl->nssa);
415 nvme_disable_streams(ctrl);
416 return 0;
417 }
418
419 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
420 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
421 return 0;
422}
423
424/*
425 * Check if 'req' has a write hint associated with it. If it does, assign
426 * a valid namespace stream to the write.
427 */
428static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
429 struct request *req, u16 *control,
430 u32 *dsmgmt)
431{
432 enum rw_hint streamid = req->write_hint;
433
434 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
435 streamid = 0;
436 else {
437 streamid--;
438 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
439 return;
440
441 *control |= NVME_RW_DTYPE_STREAMS;
442 *dsmgmt |= streamid << 16;
443 }
444
445 if (streamid < ARRAY_SIZE(req->q->write_hints))
446 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
447}
448
Ming Lin8093f7c2016-04-12 13:10:14 -0600449static inline void nvme_setup_flush(struct nvme_ns *ns,
450 struct nvme_command *cmnd)
451{
452 memset(cmnd, 0, sizeof(*cmnd));
453 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100454 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600455}
456
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200457static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600458 struct nvme_command *cmnd)
459{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100460 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600461 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100462 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600463
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100464 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600465 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200466 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600467
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100468 __rq_for_each_bio(bio, req) {
469 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
470 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
471
472 range[n].cattr = cpu_to_le32(0);
473 range[n].nlb = cpu_to_le32(nlb);
474 range[n].slba = cpu_to_le64(slba);
475 n++;
476 }
477
478 if (WARN_ON_ONCE(n != segments)) {
479 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200480 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100481 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600482
483 memset(cmnd, 0, sizeof(*cmnd));
484 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100485 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200486 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600487 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
488
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700489 req->special_vec.bv_page = virt_to_page(range);
490 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100491 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700492 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600493
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200494 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600495}
Ming Lin8093f7c2016-04-12 13:10:14 -0600496
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200497static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
498 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600499{
Jens Axboef5d11842017-06-27 12:03:06 -0600500 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600501 u16 control = 0;
502 u32 dsmgmt = 0;
503
504 if (req->cmd_flags & REQ_FUA)
505 control |= NVME_RW_FUA;
506 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
507 control |= NVME_RW_LR;
508
509 if (req->cmd_flags & REQ_RAHEAD)
510 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
511
512 memset(cmnd, 0, sizeof(*cmnd));
513 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100514 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600515 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
516 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
517
Jens Axboef5d11842017-06-27 12:03:06 -0600518 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
519 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
520
Ming Lin8093f7c2016-04-12 13:10:14 -0600521 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100522 /*
523 * If formated with metadata, the block layer always provides a
524 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
525 * we enable the PRACT bit for protection information or set the
526 * namespace capacity to zero to prevent any I/O.
527 */
528 if (!blk_integrity_rq(req)) {
529 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
530 return BLK_STS_NOTSUPP;
531 control |= NVME_RW_PRINFO_PRACT;
532 }
533
Ming Lin8093f7c2016-04-12 13:10:14 -0600534 switch (ns->pi_type) {
535 case NVME_NS_DPS_PI_TYPE3:
536 control |= NVME_RW_PRINFO_PRCHK_GUARD;
537 break;
538 case NVME_NS_DPS_PI_TYPE1:
539 case NVME_NS_DPS_PI_TYPE2:
540 control |= NVME_RW_PRINFO_PRCHK_GUARD |
541 NVME_RW_PRINFO_PRCHK_REF;
542 cmnd->rw.reftag = cpu_to_le32(
543 nvme_block_nr(ns, blk_rq_pos(req)));
544 break;
545 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600546 }
547
548 cmnd->rw.control = cpu_to_le16(control);
549 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200550 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600551}
552
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200553blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600554 struct nvme_command *cmd)
555{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200556 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600557
Christoph Hellwig987f6992017-04-05 19:18:08 +0200558 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200559 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200560 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200561 req->rq_flags |= RQF_DONTPREP;
562 }
563
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100564 switch (req_op(req)) {
565 case REQ_OP_DRV_IN:
566 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800567 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100568 break;
569 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600570 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100571 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200572 case REQ_OP_WRITE_ZEROES:
573 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100574 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600575 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100576 break;
577 case REQ_OP_READ:
578 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200579 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100580 break;
581 default:
582 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200583 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100584 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600585
James Smart721b3912016-10-21 23:33:34 +0300586 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600587 return ret;
588}
589EXPORT_SYMBOL_GPL(nvme_setup_cmd);
590
Christoph Hellwig41609822015-11-20 09:00:02 +0100591/*
592 * Returns 0 on success. If the result is negative, it's a Linux error code;
593 * if the result is positive, it's an NVM Express status code
594 */
595int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800596 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800597 unsigned timeout, int qid, int at_head,
598 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100599{
600 struct request *req;
601 int ret;
602
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200603 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100604 if (IS_ERR(req))
605 return PTR_ERR(req);
606
607 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
608
Christoph Hellwig21d34712015-11-26 09:08:36 +0100609 if (buffer && bufflen) {
610 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
611 if (ret)
612 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100613 }
614
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200615 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800616 if (result)
617 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200618 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
619 ret = -EINTR;
620 else
621 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100622 out:
623 blk_mq_free_request(req);
624 return ret;
625}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200626EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100627
628int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
629 void *buffer, unsigned bufflen)
630{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200631 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
632 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100633}
Ming Lin576d55d2016-02-10 10:03:32 -0800634EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100635
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400636static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
637 unsigned len, u32 seed, bool write)
638{
639 struct bio_integrity_payload *bip;
640 int ret = -ENOMEM;
641 void *buf;
642
643 buf = kmalloc(len, GFP_KERNEL);
644 if (!buf)
645 goto out;
646
647 ret = -EFAULT;
648 if (write && copy_from_user(buf, ubuf, len))
649 goto out_free_meta;
650
651 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
652 if (IS_ERR(bip)) {
653 ret = PTR_ERR(bip);
654 goto out_free_meta;
655 }
656
657 bip->bip_iter.bi_size = len;
658 bip->bip_iter.bi_sector = seed;
659 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
660 offset_in_page(buf));
661 if (ret == len)
662 return buf;
663 ret = -ENOMEM;
664out_free_meta:
665 kfree(buf);
666out:
667 return ERR_PTR(ret);
668}
669
Keith Busch63263d62017-08-29 17:46:04 -0400670static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400671 struct nvme_command *cmd, void __user *ubuffer,
672 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
673 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100674{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200675 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600676 struct nvme_ns *ns = q->queuedata;
677 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100678 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600679 struct bio *bio = NULL;
680 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100681 int ret;
682
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200683 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100684 if (IS_ERR(req))
685 return PTR_ERR(req);
686
687 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
688
689 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100690 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
691 GFP_KERNEL);
692 if (ret)
693 goto out;
694 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200695 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400696 if (disk && meta_buffer && meta_len) {
697 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
698 meta_seed, write);
699 if (IS_ERR(meta)) {
700 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600701 goto out_unmap;
702 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600703 }
704 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400705
Keith Busch0b7f1f22015-10-23 09:47:28 -0600706 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200707 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
708 ret = -EINTR;
709 else
710 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100711 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800712 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600713 if (meta && !ret && !write) {
714 if (copy_to_user(meta_buffer, meta, meta_len))
715 ret = -EFAULT;
716 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600717 kfree(meta);
718 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200719 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600720 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100721 out:
722 blk_mq_free_request(req);
723 return ret;
724}
725
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200726static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200727{
728 struct nvme_ctrl *ctrl = rq->end_io_data;
729
730 blk_mq_free_request(rq);
731
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200732 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200733 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200734 "failed nvme_keep_alive_end_io error=%d\n",
735 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200736 return;
737 }
738
739 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
740}
741
742static int nvme_keep_alive(struct nvme_ctrl *ctrl)
743{
744 struct nvme_command c;
745 struct request *rq;
746
747 memset(&c, 0, sizeof(c));
748 c.common.opcode = nvme_admin_keep_alive;
749
750 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
751 NVME_QID_ANY);
752 if (IS_ERR(rq))
753 return PTR_ERR(rq);
754
755 rq->timeout = ctrl->kato * HZ;
756 rq->end_io_data = ctrl;
757
758 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
759
760 return 0;
761}
762
763static void nvme_keep_alive_work(struct work_struct *work)
764{
765 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
766 struct nvme_ctrl, ka_work);
767
768 if (nvme_keep_alive(ctrl)) {
769 /* allocation failure, reset the controller */
770 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200771 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200772 return;
773 }
774}
775
776void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
777{
778 if (unlikely(ctrl->kato == 0))
779 return;
780
781 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
782 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
783}
784EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
785
786void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
787{
788 if (unlikely(ctrl->kato == 0))
789 return;
790
791 cancel_delayed_work_sync(&ctrl->ka_work);
792}
793EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
794
Keith Busch3f7f25a92017-06-20 15:09:56 -0400795static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100796{
797 struct nvme_command c = { };
798 int error;
799
800 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
801 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200802 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100803
804 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
805 if (!*id)
806 return -ENOMEM;
807
808 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
809 sizeof(struct nvme_id_ctrl));
810 if (error)
811 kfree(*id);
812 return error;
813}
814
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200815static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100816 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200817{
818 struct nvme_command c = { };
819 int status;
820 void *data;
821 int pos;
822 int len;
823
824 c.identify.opcode = nvme_admin_identify;
825 c.identify.nsid = cpu_to_le32(nsid);
826 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
827
828 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
829 if (!data)
830 return -ENOMEM;
831
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200832 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200833 NVME_IDENTIFY_DATA_SIZE);
834 if (status)
835 goto free_data;
836
837 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
838 struct nvme_ns_id_desc *cur = data + pos;
839
840 if (cur->nidl == 0)
841 break;
842
843 switch (cur->nidt) {
844 case NVME_NIDT_EUI64:
845 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200846 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200847 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
848 cur->nidl);
849 goto free_data;
850 }
851 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100852 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200853 break;
854 case NVME_NIDT_NGUID:
855 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200856 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200857 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
858 cur->nidl);
859 goto free_data;
860 }
861 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100862 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200863 break;
864 case NVME_NIDT_UUID:
865 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200866 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200867 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
868 cur->nidl);
869 goto free_data;
870 }
871 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100872 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200873 break;
874 default:
875 /* Skip unnkown types */
876 len = cur->nidl;
877 break;
878 }
879
880 len += sizeof(*cur);
881 }
882free_data:
883 kfree(data);
884 return status;
885}
886
Keith Busch540c8012015-10-22 15:45:06 -0600887static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
888{
889 struct nvme_command c = { };
890
891 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200892 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600893 c.identify.nsid = cpu_to_le32(nsid);
894 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
895}
896
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200897static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
898 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100899{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200900 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100901 struct nvme_command c = { };
902 int error;
903
904 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200905 c.identify.opcode = nvme_admin_identify;
906 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200907 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100908
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200909 id = kmalloc(sizeof(*id), GFP_KERNEL);
910 if (!id)
911 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100912
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200913 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
914 if (error) {
915 dev_warn(ctrl->device, "Identify namespace failed\n");
916 kfree(id);
917 return NULL;
918 }
919
920 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100921}
922
Keith Busch3f7f25a92017-06-20 15:09:56 -0400923static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700924 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100925{
926 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800927 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100928 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100929
930 memset(&c, 0, sizeof(c));
931 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100932 c.features.fid = cpu_to_le32(fid);
933 c.features.dword11 = cpu_to_le32(dword11);
934
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800935 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700936 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700937 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800938 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100939 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100940}
941
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100942int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
943{
944 u32 q_count = (*count - 1) | ((*count - 1) << 16);
945 u32 result;
946 int status, nr_io_queues;
947
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700948 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100949 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200950 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100951 return status;
952
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200953 /*
954 * Degraded controllers might return an error when setting the queue
955 * count. We still want to be able to bring them online and offer
956 * access to the admin queue, as that might be only way to fix them up.
957 */
958 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200959 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200960 *count = 0;
961 } else {
962 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
963 *count = min(*count, nr_io_queues);
964 }
965
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100966 return 0;
967}
Ming Lin576d55d2016-02-10 10:03:32 -0800968EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100969
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100970static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
971{
972 struct nvme_user_io io;
973 struct nvme_command c;
974 unsigned length, meta_len;
975 void __user *metadata;
976
977 if (copy_from_user(&io, uio, sizeof(io)))
978 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700979 if (io.flags)
980 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100981
982 switch (io.opcode) {
983 case nvme_cmd_write:
984 case nvme_cmd_read:
985 case nvme_cmd_compare:
986 break;
987 default:
988 return -EINVAL;
989 }
990
991 length = (io.nblocks + 1) << ns->lba_shift;
992 meta_len = (io.nblocks + 1) * ns->ms;
993 metadata = (void __user *)(uintptr_t)io.metadata;
994
995 if (ns->ext) {
996 length += meta_len;
997 meta_len = 0;
998 } else if (meta_len) {
999 if ((io.metadata & 3) || !io.metadata)
1000 return -EINVAL;
1001 }
1002
1003 memset(&c, 0, sizeof(c));
1004 c.rw.opcode = io.opcode;
1005 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001006 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001007 c.rw.slba = cpu_to_le64(io.slba);
1008 c.rw.length = cpu_to_le16(io.nblocks);
1009 c.rw.control = cpu_to_le16(io.control);
1010 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1011 c.rw.reftag = cpu_to_le32(io.reftag);
1012 c.rw.apptag = cpu_to_le16(io.apptag);
1013 c.rw.appmask = cpu_to_le16(io.appmask);
1014
Keith Busch63263d62017-08-29 17:46:04 -04001015 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001016 (void __user *)(uintptr_t)io.addr, length,
1017 metadata, meta_len, io.slba, NULL, 0);
1018}
1019
Keith Busch84fef622017-11-07 10:28:32 -07001020static u32 nvme_known_admin_effects(u8 opcode)
1021{
1022 switch (opcode) {
1023 case nvme_admin_format_nvm:
1024 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1025 NVME_CMD_EFFECTS_CSE_MASK;
1026 case nvme_admin_sanitize_nvm:
1027 return NVME_CMD_EFFECTS_CSE_MASK;
1028 default:
1029 break;
1030 }
1031 return 0;
1032}
1033
1034static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1035 u8 opcode)
1036{
1037 u32 effects = 0;
1038
1039 if (ns) {
1040 if (ctrl->effects)
1041 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1042 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1043 dev_warn(ctrl->device,
1044 "IO command:%02x has unhandled effects:%08x\n",
1045 opcode, effects);
1046 return 0;
1047 }
1048
1049 if (ctrl->effects)
1050 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1051 else
1052 effects = nvme_known_admin_effects(opcode);
1053
1054 /*
1055 * For simplicity, IO to all namespaces is quiesced even if the command
1056 * effects say only one namespace is affected.
1057 */
1058 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1059 nvme_start_freeze(ctrl);
1060 nvme_wait_freeze(ctrl);
1061 }
1062 return effects;
1063}
1064
1065static void nvme_update_formats(struct nvme_ctrl *ctrl)
1066{
1067 struct nvme_ns *ns;
1068
1069 mutex_lock(&ctrl->namespaces_mutex);
1070 list_for_each_entry(ns, &ctrl->namespaces, list) {
1071 if (ns->disk && nvme_revalidate_disk(ns->disk))
1072 nvme_ns_remove(ns);
1073 }
1074 mutex_unlock(&ctrl->namespaces_mutex);
1075}
1076
1077static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1078{
1079 /*
1080 * Revalidate LBA changes prior to unfreezing. This is necessary to
1081 * prevent memory corruption if a logical block size was changed by
1082 * this command.
1083 */
1084 if (effects & NVME_CMD_EFFECTS_LBCC)
1085 nvme_update_formats(ctrl);
1086 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1087 nvme_unfreeze(ctrl);
1088 if (effects & NVME_CMD_EFFECTS_CCC)
1089 nvme_init_identify(ctrl);
1090 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1091 nvme_queue_scan(ctrl);
1092}
1093
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001094static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001095 struct nvme_passthru_cmd __user *ucmd)
1096{
1097 struct nvme_passthru_cmd cmd;
1098 struct nvme_command c;
1099 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001100 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001101 int status;
1102
1103 if (!capable(CAP_SYS_ADMIN))
1104 return -EACCES;
1105 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1106 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001107 if (cmd.flags)
1108 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001109
1110 memset(&c, 0, sizeof(c));
1111 c.common.opcode = cmd.opcode;
1112 c.common.flags = cmd.flags;
1113 c.common.nsid = cpu_to_le32(cmd.nsid);
1114 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1115 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1116 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1117 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1118 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1119 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1120 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1121 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1122
1123 if (cmd.timeout_ms)
1124 timeout = msecs_to_jiffies(cmd.timeout_ms);
1125
Keith Busch84fef622017-11-07 10:28:32 -07001126 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001127 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001128 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001129 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1130 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001131 nvme_passthru_end(ctrl, effects);
1132
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001133 if (status >= 0) {
1134 if (put_user(cmd.result, &ucmd->result))
1135 return -EFAULT;
1136 }
1137
1138 return status;
1139}
1140
1141static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1142 unsigned int cmd, unsigned long arg)
1143{
1144 struct nvme_ns *ns = bdev->bd_disk->private_data;
1145
1146 switch (cmd) {
1147 case NVME_IOCTL_ID:
1148 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001149 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001150 case NVME_IOCTL_ADMIN_CMD:
1151 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1152 case NVME_IOCTL_IO_CMD:
1153 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1154 case NVME_IOCTL_SUBMIT_IO:
1155 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001156 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001157#ifdef CONFIG_NVM
1158 if (ns->ndev)
1159 return nvme_nvm_ioctl(ns, cmd, arg);
1160#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001161 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001162 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001163 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001164 return -ENOTTY;
1165 }
1166}
1167
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001168static int nvme_open(struct block_device *bdev, fmode_t mode)
1169{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001170 struct nvme_ns *ns = bdev->bd_disk->private_data;
1171
1172 if (!kref_get_unless_zero(&ns->kref))
1173 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001174 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001175}
1176
1177static void nvme_release(struct gendisk *disk, fmode_t mode)
1178{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001179 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001180}
1181
1182static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1183{
1184 /* some standard values */
1185 geo->heads = 1 << 6;
1186 geo->sectors = 1 << 5;
1187 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1188 return 0;
1189}
1190
1191#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001192static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001193{
1194 struct blk_integrity integrity;
1195
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001196 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001197 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001198 case NVME_NS_DPS_PI_TYPE3:
1199 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001200 integrity.tag_size = sizeof(u16) + sizeof(u32);
1201 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001202 break;
1203 case NVME_NS_DPS_PI_TYPE1:
1204 case NVME_NS_DPS_PI_TYPE2:
1205 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001206 integrity.tag_size = sizeof(u16);
1207 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001208 break;
1209 default:
1210 integrity.profile = NULL;
1211 break;
1212 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001213 integrity.tuple_size = ms;
1214 blk_integrity_register(disk, &integrity);
1215 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001216}
1217#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001218static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001219{
1220}
1221#endif /* CONFIG_BLK_DEV_INTEGRITY */
1222
Scott Bauer6b8190d2017-06-15 10:44:30 -06001223static void nvme_set_chunk_size(struct nvme_ns *ns)
1224{
1225 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1226 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1227}
1228
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001229static void nvme_config_discard(struct nvme_ctrl *ctrl,
1230 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001231{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001232 u32 size = queue_logical_block_size(queue);
1233
1234 if (stream_alignment)
1235 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001236
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001237 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1238 NVME_DSM_MAX_RANGES);
1239
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001240 queue->limits.discard_alignment = size;
1241 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001242
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001243 blk_queue_max_discard_sectors(queue, UINT_MAX);
1244 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1245 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001246
1247 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001248 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001249}
1250
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001251static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001252 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001253{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001254 memset(ids, 0, sizeof(*ids));
1255
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001256 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001257 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001258 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001259 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001260 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001261 /* Don't treat error as fatal we potentially
1262 * already have a NGUID or EUI-64
1263 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001264 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001265 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001266 "%s: Identify Descriptors failed\n", __func__);
1267 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001268}
1269
Christoph Hellwiged754e52017-11-09 13:50:43 +01001270static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1271{
1272 return !uuid_is_null(&ids->uuid) ||
1273 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1274 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1275}
1276
Christoph Hellwig002fab02017-11-09 13:50:16 +01001277static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1278{
1279 return uuid_equal(&a->uuid, &b->uuid) &&
1280 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1281 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1282}
1283
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001284static void nvme_update_disk_info(struct gendisk *disk,
1285 struct nvme_ns *ns, struct nvme_id_ns *id)
1286{
1287 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1288 unsigned stream_alignment = 0;
1289
1290 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1291 stream_alignment = ns->sws * ns->sgs;
1292
1293 blk_mq_freeze_queue(disk->queue);
1294 blk_integrity_unregister(disk);
1295
1296 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1297 if (ns->ms && !ns->ext &&
1298 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1299 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001300 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001301 capacity = 0;
1302 set_capacity(disk, capacity);
1303
1304 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1305 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1306 blk_mq_unfreeze_queue(disk->queue);
1307}
1308
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001309static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1310{
1311 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001312
1313 /*
1314 * If identify namespace failed, use default 512 byte block size so
1315 * block layer can use before failing read/write for 0 capacity.
1316 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001317 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001318 if (ns->lba_shift == 0)
1319 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001320 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001321 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1322 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1323 /* the PI implementation requires metadata equal t10 pi tuple size */
1324 if (ns->ms == sizeof(struct t10_pi_tuple))
1325 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1326 else
1327 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001328
Christoph Hellwig6e78f212017-11-02 21:28:55 +03001329 if (ns->noiob)
1330 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001331 nvme_update_disk_info(disk, ns, id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001332}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001333
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001334static int nvme_revalidate_disk(struct gendisk *disk)
1335{
1336 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001337 struct nvme_ctrl *ctrl = ns->ctrl;
1338 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001339 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001340 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001341
1342 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1343 set_capacity(disk, 0);
1344 return -ENODEV;
1345 }
1346
Christoph Hellwiged754e52017-11-09 13:50:43 +01001347 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001348 if (!id)
1349 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001350
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001351 if (id->ncap == 0) {
1352 ret = -ENODEV;
1353 goto out;
1354 }
1355
Christoph Hellwiged754e52017-11-09 13:50:43 +01001356 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1357 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001358 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001359 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001360 ret = -ENODEV;
1361 }
1362
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001363out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001364 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001365 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001366}
1367
1368static char nvme_pr_type(enum pr_type type)
1369{
1370 switch (type) {
1371 case PR_WRITE_EXCLUSIVE:
1372 return 1;
1373 case PR_EXCLUSIVE_ACCESS:
1374 return 2;
1375 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1376 return 3;
1377 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1378 return 4;
1379 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1380 return 5;
1381 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1382 return 6;
1383 default:
1384 return 0;
1385 }
1386};
1387
1388static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1389 u64 key, u64 sa_key, u8 op)
1390{
1391 struct nvme_ns *ns = bdev->bd_disk->private_data;
1392 struct nvme_command c;
1393 u8 data[16] = { 0, };
1394
1395 put_unaligned_le64(key, &data[0]);
1396 put_unaligned_le64(sa_key, &data[8]);
1397
1398 memset(&c, 0, sizeof(c));
1399 c.common.opcode = op;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001400 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001401 c.common.cdw10[0] = cpu_to_le32(cdw10);
1402
1403 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1404}
1405
1406static int nvme_pr_register(struct block_device *bdev, u64 old,
1407 u64 new, unsigned flags)
1408{
1409 u32 cdw10;
1410
1411 if (flags & ~PR_FL_IGNORE_KEY)
1412 return -EOPNOTSUPP;
1413
1414 cdw10 = old ? 2 : 0;
1415 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1416 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1417 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1418}
1419
1420static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1421 enum pr_type type, unsigned flags)
1422{
1423 u32 cdw10;
1424
1425 if (flags & ~PR_FL_IGNORE_KEY)
1426 return -EOPNOTSUPP;
1427
1428 cdw10 = nvme_pr_type(type) << 8;
1429 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1430 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1431}
1432
1433static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1434 enum pr_type type, bool abort)
1435{
1436 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1437 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1438}
1439
1440static int nvme_pr_clear(struct block_device *bdev, u64 key)
1441{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001442 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001443 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1444}
1445
1446static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1447{
1448 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1449 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1450}
1451
1452static const struct pr_ops nvme_pr_ops = {
1453 .pr_register = nvme_pr_register,
1454 .pr_reserve = nvme_pr_reserve,
1455 .pr_release = nvme_pr_release,
1456 .pr_preempt = nvme_pr_preempt,
1457 .pr_clear = nvme_pr_clear,
1458};
1459
Scott Bauera98e58e52017-02-03 12:50:32 -07001460#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001461int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1462 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001463{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001464 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001465 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001466
1467 memset(&cmd, 0, sizeof(cmd));
1468 if (send)
1469 cmd.common.opcode = nvme_admin_security_send;
1470 else
1471 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001472 cmd.common.nsid = 0;
1473 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1474 cmd.common.cdw10[1] = cpu_to_le32(len);
1475
1476 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1477 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1478}
1479EXPORT_SYMBOL_GPL(nvme_sec_submit);
1480#endif /* CONFIG_BLK_SED_OPAL */
1481
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001482static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001483 .owner = THIS_MODULE,
1484 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001485 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001486 .open = nvme_open,
1487 .release = nvme_release,
1488 .getgeo = nvme_getgeo,
1489 .revalidate_disk= nvme_revalidate_disk,
1490 .pr_ops = &nvme_pr_ops,
1491};
1492
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001493static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1494{
1495 unsigned long timeout =
1496 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1497 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1498 int ret;
1499
1500 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001501 if (csts == ~0)
1502 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001503 if ((csts & NVME_CSTS_RDY) == bit)
1504 break;
1505
1506 msleep(100);
1507 if (fatal_signal_pending(current))
1508 return -EINTR;
1509 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001510 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001511 "Device not ready; aborting %s\n", enabled ?
1512 "initialisation" : "reset");
1513 return -ENODEV;
1514 }
1515 }
1516
1517 return ret;
1518}
1519
1520/*
1521 * If the device has been passed off to us in an enabled state, just clear
1522 * the enabled bit. The spec says we should set the 'shutdown notification
1523 * bits', but doing so may cause the device to complete commands to the
1524 * admin queue ... and we don't know what memory that might be pointing at!
1525 */
1526int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1527{
1528 int ret;
1529
1530 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1531 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1532
1533 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1534 if (ret)
1535 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001536
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001537 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001538 msleep(NVME_QUIRK_DELAY_AMOUNT);
1539
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001540 return nvme_wait_ready(ctrl, cap, false);
1541}
Ming Lin576d55d2016-02-10 10:03:32 -08001542EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001543
1544int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1545{
1546 /*
1547 * Default to a 4K page size, with the intention to update this
1548 * path in the future to accomodate architectures with differing
1549 * kernel and IO page sizes.
1550 */
1551 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1552 int ret;
1553
1554 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001555 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001556 "Minimum device page size %u too large for host (%u)\n",
1557 1 << dev_page_min, 1 << page_shift);
1558 return -ENODEV;
1559 }
1560
1561 ctrl->page_size = 1 << page_shift;
1562
1563 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1564 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001565 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001566 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1567 ctrl->ctrl_config |= NVME_CC_ENABLE;
1568
1569 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1570 if (ret)
1571 return ret;
1572 return nvme_wait_ready(ctrl, cap, true);
1573}
Ming Lin576d55d2016-02-10 10:03:32 -08001574EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001575
1576int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1577{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001578 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001579 u32 csts;
1580 int ret;
1581
1582 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1583 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1584
1585 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1586 if (ret)
1587 return ret;
1588
1589 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1590 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1591 break;
1592
1593 msleep(100);
1594 if (fatal_signal_pending(current))
1595 return -EINTR;
1596 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001597 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001598 "Device shutdown incomplete; abort shutdown\n");
1599 return -ENODEV;
1600 }
1601 }
1602
1603 return ret;
1604}
Ming Lin576d55d2016-02-10 10:03:32 -08001605EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001606
Christoph Hellwigda358252016-03-02 18:07:11 +01001607static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1608 struct request_queue *q)
1609{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001610 bool vwc = false;
1611
Christoph Hellwigda358252016-03-02 18:07:11 +01001612 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001613 u32 max_segments =
1614 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1615
Christoph Hellwigda358252016-03-02 18:07:11 +01001616 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001617 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001618 }
Keith Busche6282ae2016-12-19 11:37:50 -05001619 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1620 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001621 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001622 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1623 vwc = true;
1624 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001625}
1626
Jon Derrickdbf86b32017-08-16 09:51:29 +02001627static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1628{
1629 __le64 ts;
1630 int ret;
1631
1632 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1633 return 0;
1634
1635 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1636 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1637 NULL);
1638 if (ret)
1639 dev_warn_once(ctrl->device,
1640 "could not set timestamp (%d)\n", ret);
1641 return ret;
1642}
1643
Keith Busch634b8322017-08-10 11:23:31 +02001644static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001645{
1646 /*
1647 * APST (Autonomous Power State Transition) lets us program a
1648 * table of power state transitions that the controller will
1649 * perform automatically. We configure it with a simple
1650 * heuristic: we are willing to spend at most 2% of the time
1651 * transitioning between power states. Therefore, when running
1652 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001653 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001654 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001655 * the requested maximum latency.
1656 *
1657 * We will not autonomously enter any non-operational state for
1658 * which the total latency exceeds ps_max_latency_us. Users
1659 * can set ps_max_latency_us to zero to turn off APST.
1660 */
1661
1662 unsigned apste;
1663 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001664 u64 max_lat_us = 0;
1665 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001666 int ret;
1667
1668 /*
1669 * If APST isn't supported or if we haven't been initialized yet,
1670 * then don't do anything.
1671 */
1672 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001673 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001674
1675 if (ctrl->npss > 31) {
1676 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001677 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001678 }
1679
1680 table = kzalloc(sizeof(*table), GFP_KERNEL);
1681 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001682 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001683
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001684 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001685 /* Turn off APST. */
1686 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001687 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001688 } else {
1689 __le64 target = cpu_to_le64(0);
1690 int state;
1691
1692 /*
1693 * Walk through all states from lowest- to highest-power.
1694 * According to the spec, lower-numbered states use more
1695 * power. NPSS, despite the name, is the index of the
1696 * lowest-power state, not the number of states.
1697 */
1698 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001699 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001700
1701 if (target)
1702 table->entries[state] = target;
1703
1704 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001705 * Don't allow transitions to the deepest state
1706 * if it's quirked off.
1707 */
1708 if (state == ctrl->npss &&
1709 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1710 continue;
1711
1712 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001713 * Is this state a useful non-operational state for
1714 * higher-power states to autonomously transition to?
1715 */
1716 if (!(ctrl->psd[state].flags &
1717 NVME_PS_FLAGS_NON_OP_STATE))
1718 continue;
1719
Kai-Heng Fengda875912017-06-07 15:25:42 +08001720 exit_latency_us =
1721 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1722 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001723 continue;
1724
Kai-Heng Fengda875912017-06-07 15:25:42 +08001725 total_latency_us =
1726 exit_latency_us +
1727 le32_to_cpu(ctrl->psd[state].entry_lat);
1728
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001729 /*
1730 * This state is good. Use it as the APST idle
1731 * target for higher power states.
1732 */
1733 transition_ms = total_latency_us + 19;
1734 do_div(transition_ms, 20);
1735 if (transition_ms > (1 << 24) - 1)
1736 transition_ms = (1 << 24) - 1;
1737
1738 target = cpu_to_le64((state << 3) |
1739 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001740
1741 if (max_ps == -1)
1742 max_ps = state;
1743
1744 if (total_latency_us > max_lat_us)
1745 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001746 }
1747
1748 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001749
1750 if (max_ps == -1) {
1751 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1752 } else {
1753 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1754 max_ps, max_lat_us, (int)sizeof(*table), table);
1755 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001756 }
1757
1758 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1759 table, sizeof(*table), NULL);
1760 if (ret)
1761 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1762
1763 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001764 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001765}
1766
1767static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1768{
1769 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1770 u64 latency;
1771
1772 switch (val) {
1773 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1774 case PM_QOS_LATENCY_ANY:
1775 latency = U64_MAX;
1776 break;
1777
1778 default:
1779 latency = val;
1780 }
1781
1782 if (ctrl->ps_max_latency_us != latency) {
1783 ctrl->ps_max_latency_us = latency;
1784 nvme_configure_apst(ctrl);
1785 }
1786}
1787
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001788struct nvme_core_quirk_entry {
1789 /*
1790 * NVMe model and firmware strings are padded with spaces. For
1791 * simplicity, strings in the quirk table are padded with NULLs
1792 * instead.
1793 */
1794 u16 vid;
1795 const char *mn;
1796 const char *fr;
1797 unsigned long quirks;
1798};
1799
1800static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001801 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001802 /*
1803 * This Toshiba device seems to die using any APST states. See:
1804 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1805 */
1806 .vid = 0x1179,
1807 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001808 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001809 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001810};
1811
1812/* match is null-terminated but idstr is space-padded. */
1813static bool string_matches(const char *idstr, const char *match, size_t len)
1814{
1815 size_t matchlen;
1816
1817 if (!match)
1818 return true;
1819
1820 matchlen = strlen(match);
1821 WARN_ON_ONCE(matchlen > len);
1822
1823 if (memcmp(idstr, match, matchlen))
1824 return false;
1825
1826 for (; matchlen < len; matchlen++)
1827 if (idstr[matchlen] != ' ')
1828 return false;
1829
1830 return true;
1831}
1832
1833static bool quirk_matches(const struct nvme_id_ctrl *id,
1834 const struct nvme_core_quirk_entry *q)
1835{
1836 return q->vid == le16_to_cpu(id->vid) &&
1837 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1838 string_matches(id->fr, q->fr, sizeof(id->fr));
1839}
1840
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001841static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
1842 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02001843{
1844 size_t nqnlen;
1845 int off;
1846
1847 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1848 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001849 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02001850 return;
1851 }
1852
1853 if (ctrl->vs >= NVME_VS(1, 2, 1))
1854 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1855
1856 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001857 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02001858 "nqn.2014.08.org.nvmexpress:%4x%4x",
1859 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001860 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001861 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001862 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02001863 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001864 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
1865}
1866
1867static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
1868{
1869 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
1870 kfree(subsys);
1871}
1872
1873static void nvme_release_subsystem(struct device *dev)
1874{
1875 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
1876}
1877
1878static void nvme_destroy_subsystem(struct kref *ref)
1879{
1880 struct nvme_subsystem *subsys =
1881 container_of(ref, struct nvme_subsystem, ref);
1882
1883 mutex_lock(&nvme_subsystems_lock);
1884 list_del(&subsys->entry);
1885 mutex_unlock(&nvme_subsystems_lock);
1886
Christoph Hellwiged754e52017-11-09 13:50:43 +01001887 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001888 device_del(&subsys->dev);
1889 put_device(&subsys->dev);
1890}
1891
1892static void nvme_put_subsystem(struct nvme_subsystem *subsys)
1893{
1894 kref_put(&subsys->ref, nvme_destroy_subsystem);
1895}
1896
1897static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
1898{
1899 struct nvme_subsystem *subsys;
1900
1901 lockdep_assert_held(&nvme_subsystems_lock);
1902
1903 list_for_each_entry(subsys, &nvme_subsystems, entry) {
1904 if (strcmp(subsys->subnqn, subsysnqn))
1905 continue;
1906 if (!kref_get_unless_zero(&subsys->ref))
1907 continue;
1908 return subsys;
1909 }
1910
1911 return NULL;
1912}
1913
1914static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1915{
1916 struct nvme_subsystem *subsys, *found;
1917 int ret;
1918
1919 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
1920 if (!subsys)
1921 return -ENOMEM;
1922 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
1923 if (ret < 0) {
1924 kfree(subsys);
1925 return ret;
1926 }
1927 subsys->instance = ret;
1928 mutex_init(&subsys->lock);
1929 kref_init(&subsys->ref);
1930 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001931 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001932 nvme_init_subnqn(subsys, ctrl, id);
1933 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
1934 memcpy(subsys->model, id->mn, sizeof(subsys->model));
1935 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
1936 subsys->vendor_id = le16_to_cpu(id->vid);
1937 subsys->cmic = id->cmic;
1938
1939 subsys->dev.class = nvme_subsys_class;
1940 subsys->dev.release = nvme_release_subsystem;
1941 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
1942 device_initialize(&subsys->dev);
1943
1944 mutex_lock(&nvme_subsystems_lock);
1945 found = __nvme_find_get_subsystem(subsys->subnqn);
1946 if (found) {
1947 /*
1948 * Verify that the subsystem actually supports multiple
1949 * controllers, else bail out.
1950 */
1951 if (!(id->cmic & (1 << 1))) {
1952 dev_err(ctrl->device,
1953 "ignoring ctrl due to duplicate subnqn (%s).\n",
1954 found->subnqn);
1955 nvme_put_subsystem(found);
1956 ret = -EINVAL;
1957 goto out_unlock;
1958 }
1959
1960 __nvme_release_subsystem(subsys);
1961 subsys = found;
1962 } else {
1963 ret = device_add(&subsys->dev);
1964 if (ret) {
1965 dev_err(ctrl->device,
1966 "failed to register subsystem device.\n");
1967 goto out_unlock;
1968 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01001969 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001970 list_add_tail(&subsys->entry, &nvme_subsystems);
1971 }
1972
1973 ctrl->subsys = subsys;
1974 mutex_unlock(&nvme_subsystems_lock);
1975
1976 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
1977 dev_name(ctrl->device))) {
1978 dev_err(ctrl->device,
1979 "failed to create sysfs link from subsystem.\n");
1980 /* the transport driver will eventually put the subsystem */
1981 return -EINVAL;
1982 }
1983
1984 mutex_lock(&subsys->lock);
1985 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
1986 mutex_unlock(&subsys->lock);
1987
1988 return 0;
1989
1990out_unlock:
1991 mutex_unlock(&nvme_subsystems_lock);
1992 put_device(&subsys->dev);
1993 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02001994}
1995
Keith Buschc627c482017-11-07 10:28:31 -07001996static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
1997 size_t size)
1998{
1999 struct nvme_command c = { };
2000
2001 c.common.opcode = nvme_admin_get_log_page;
2002 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
2003 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
2004
2005 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2006}
2007
Keith Busch84fef622017-11-07 10:28:32 -07002008static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2009{
2010 int ret;
2011
2012 if (!ctrl->effects)
2013 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2014
2015 if (!ctrl->effects)
2016 return 0;
2017
2018 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2019 sizeof(*ctrl->effects));
2020 if (ret) {
2021 kfree(ctrl->effects);
2022 ctrl->effects = NULL;
2023 }
2024 return ret;
2025}
2026
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002027/*
2028 * Initialize the cached copies of the Identify data and various controller
2029 * register in our nvme_ctrl structure. This should be called as soon as
2030 * the admin queue is fully up and running.
2031 */
2032int nvme_init_identify(struct nvme_ctrl *ctrl)
2033{
2034 struct nvme_id_ctrl *id;
2035 u64 cap;
2036 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002037 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002038 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002039
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002040 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2041 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002042 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002043 return ret;
2044 }
2045
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002046 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2047 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002048 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002049 return ret;
2050 }
2051 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2052
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002053 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002054 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2055
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002056 ret = nvme_identify_ctrl(ctrl, &id);
2057 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002058 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002059 return -EIO;
2060 }
2061
Keith Busch84fef622017-11-07 10:28:32 -07002062 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2063 ret = nvme_get_effects_log(ctrl);
2064 if (ret < 0)
2065 return ret;
2066 }
2067
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002068 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002069 int i;
2070
2071 ret = nvme_init_subsystem(ctrl, id);
2072 if (ret)
2073 goto out_free;
2074
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002075 /*
2076 * Check for quirks. Quirk can depend on firmware version,
2077 * so, in principle, the set of quirks present can change
2078 * across a reset. As a possible future enhancement, we
2079 * could re-scan for quirks every time we reinitialize
2080 * the device, but we'd have to make sure that the driver
2081 * behaves intelligently if the quirks change.
2082 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002083 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2084 if (quirk_matches(id, &core_quirks[i]))
2085 ctrl->quirks |= core_quirks[i].quirks;
2086 }
2087 }
2088
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002089 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002090 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 -07002091 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2092 }
2093
Scott Bauer8a9ae522017-02-17 13:59:40 +01002094 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002095 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002096 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002097 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002098 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002099 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002100 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002101 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002102 max_hw_sectors = UINT_MAX;
2103 ctrl->max_hw_sectors =
2104 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002105
Christoph Hellwigda358252016-03-02 18:07:11 +01002106 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002107 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002108 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002109
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002110 if (id->rtd3e) {
2111 /* us -> s */
2112 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2113
2114 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2115 shutdown_timeout, 60);
2116
2117 if (ctrl->shutdown_timeout != shutdown_timeout)
2118 dev_warn(ctrl->device,
2119 "Shutdown timeout set to %u seconds\n",
2120 ctrl->shutdown_timeout);
2121 } else
2122 ctrl->shutdown_timeout = shutdown_timeout;
2123
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002124 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002125 ctrl->apsta = id->apsta;
2126 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002127 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2128 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002129 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 -04002130 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002131 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002132 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002133 }
2134 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002135 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002136 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002137 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2138
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002139 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002140 ctrl->icdoff = le16_to_cpu(id->icdoff);
2141 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2142 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2143 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2144
2145 /*
2146 * In fabrics we need to verify the cntlid matches the
2147 * admin connect
2148 */
Keith Busch634b8322017-08-10 11:23:31 +02002149 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002150 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002151 goto out_free;
2152 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002153
2154 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002155 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002156 "keep-alive support is mandatory for fabrics\n");
2157 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002158 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002159 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002160 } else {
2161 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002162 ctrl->hmpre = le32_to_cpu(id->hmpre);
2163 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002164 ctrl->hmminds = le32_to_cpu(id->hmminds);
2165 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002166 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002167
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002168 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002169
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002170 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002171 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002172 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002173 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2174
Keith Busch634b8322017-08-10 11:23:31 +02002175 ret = nvme_configure_apst(ctrl);
2176 if (ret < 0)
2177 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002178
2179 ret = nvme_configure_timestamp(ctrl);
2180 if (ret < 0)
2181 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002182
2183 ret = nvme_configure_directives(ctrl);
2184 if (ret < 0)
2185 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002186
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002187 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002188
Keith Busch634b8322017-08-10 11:23:31 +02002189 return 0;
2190
2191out_free:
2192 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002193 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002194}
Ming Lin576d55d2016-02-10 10:03:32 -08002195EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002196
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002197static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002198{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002199 struct nvme_ctrl *ctrl =
2200 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002201
Christoph Hellwig999ada22017-10-18 17:09:31 +02002202 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002203 return -EWOULDBLOCK;
2204 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002205 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002206}
2207
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002208static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2209{
2210 struct nvme_ns *ns;
2211 int ret;
2212
2213 mutex_lock(&ctrl->namespaces_mutex);
2214 if (list_empty(&ctrl->namespaces)) {
2215 ret = -ENOTTY;
2216 goto out_unlock;
2217 }
2218
2219 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2220 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002221 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002222 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2223 ret = -EINVAL;
2224 goto out_unlock;
2225 }
2226
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002227 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002228 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2229 kref_get(&ns->kref);
2230 mutex_unlock(&ctrl->namespaces_mutex);
2231
2232 ret = nvme_user_cmd(ctrl, ns, argp);
2233 nvme_put_ns(ns);
2234 return ret;
2235
2236out_unlock:
2237 mutex_unlock(&ctrl->namespaces_mutex);
2238 return ret;
2239}
2240
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002241static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2242 unsigned long arg)
2243{
2244 struct nvme_ctrl *ctrl = file->private_data;
2245 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002246
2247 switch (cmd) {
2248 case NVME_IOCTL_ADMIN_CMD:
2249 return nvme_user_cmd(ctrl, NULL, argp);
2250 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002251 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002252 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002253 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002254 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002255 case NVME_IOCTL_SUBSYS_RESET:
2256 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002257 case NVME_IOCTL_RESCAN:
2258 nvme_queue_scan(ctrl);
2259 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002260 default:
2261 return -ENOTTY;
2262 }
2263}
2264
2265static const struct file_operations nvme_dev_fops = {
2266 .owner = THIS_MODULE,
2267 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002268 .unlocked_ioctl = nvme_dev_ioctl,
2269 .compat_ioctl = nvme_dev_ioctl,
2270};
2271
2272static ssize_t nvme_sysfs_reset(struct device *dev,
2273 struct device_attribute *attr, const char *buf,
2274 size_t count)
2275{
2276 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2277 int ret;
2278
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002279 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002280 if (ret < 0)
2281 return ret;
2282 return count;
2283}
2284static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2285
Keith Busch9ec3bb22016-04-29 15:45:18 -06002286static ssize_t nvme_sysfs_rescan(struct device *dev,
2287 struct device_attribute *attr, const char *buf,
2288 size_t count)
2289{
2290 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2291
2292 nvme_queue_scan(ctrl);
2293 return count;
2294}
2295static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2296
Keith Busch118472a2016-02-18 09:57:48 -07002297static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2298 char *buf)
2299{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002300 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002301 struct nvme_ns_ids *ids = &ns->head->ids;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002302 struct nvme_subsystem *subsys = ns->ctrl->subsys;
2303 int serial_len = sizeof(subsys->serial);
2304 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002305
Christoph Hellwig002fab02017-11-09 13:50:16 +01002306 if (!uuid_is_null(&ids->uuid))
2307 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002308
Christoph Hellwig002fab02017-11-09 13:50:16 +01002309 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2310 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002311
Christoph Hellwig002fab02017-11-09 13:50:16 +01002312 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2313 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002314
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002315 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2316 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002317 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002318 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2319 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002320 model_len--;
2321
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002322 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2323 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwiged754e52017-11-09 13:50:43 +01002324 ns->head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002325}
2326static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2327
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002328static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2329 char *buf)
2330{
2331 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002332 return sprintf(buf, "%pU\n", ns->head->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002333}
2334static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2335
Keith Busch2b9b6e82015-12-22 10:10:45 -07002336static ssize_t uuid_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);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002340 struct nvme_ns_ids *ids = &ns->head->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002341
2342 /* For backward compatibility expose the NGUID to userspace if
2343 * we have no UUID set
2344 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002345 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002346 printk_ratelimited(KERN_WARNING
2347 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002348 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002349 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002350 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002351}
2352static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2353
2354static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2355 char *buf)
2356{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002357 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002358 return sprintf(buf, "%8ph\n", ns->head->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002359}
2360static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2361
2362static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2363 char *buf)
2364{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002365 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002366 return sprintf(buf, "%d\n", ns->head->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002367}
2368static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2369
2370static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002371 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002372 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002373 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002374 &dev_attr_eui.attr,
2375 &dev_attr_nsid.attr,
2376 NULL,
2377};
2378
Ming Lin1a353d82016-06-13 16:45:24 +02002379static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002380 struct attribute *a, int n)
2381{
2382 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002383 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002384 struct nvme_ns_ids *ids = &ns->head->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002385
2386 if (a == &dev_attr_uuid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002387 if (uuid_is_null(&ids->uuid) ||
2388 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002389 return 0;
2390 }
2391 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002392 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002393 return 0;
2394 }
2395 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002396 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002397 return 0;
2398 }
2399 return a->mode;
2400}
2401
2402static const struct attribute_group nvme_ns_attr_group = {
2403 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002404 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002405};
2406
Ming Lin931e1c22016-02-26 13:24:19 -08002407#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002408static ssize_t field##_show(struct device *dev, \
2409 struct device_attribute *attr, char *buf) \
2410{ \
2411 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002412 return sprintf(buf, "%.*s\n", \
2413 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002414} \
2415static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2416
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002417nvme_show_str_function(model);
2418nvme_show_str_function(serial);
2419nvme_show_str_function(firmware_rev);
2420
Ming Lin931e1c22016-02-26 13:24:19 -08002421#define nvme_show_int_function(field) \
2422static ssize_t field##_show(struct device *dev, \
2423 struct device_attribute *attr, char *buf) \
2424{ \
2425 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2426 return sprintf(buf, "%d\n", ctrl->field); \
2427} \
2428static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2429
Ming Lin931e1c22016-02-26 13:24:19 -08002430nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002431
Ming Lin1a353d82016-06-13 16:45:24 +02002432static ssize_t nvme_sysfs_delete(struct device *dev,
2433 struct device_attribute *attr, const char *buf,
2434 size_t count)
2435{
2436 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2437
2438 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002439 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002440 return count;
2441}
2442static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2443
2444static ssize_t nvme_sysfs_show_transport(struct device *dev,
2445 struct device_attribute *attr,
2446 char *buf)
2447{
2448 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2449
2450 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2451}
2452static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2453
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002454static ssize_t nvme_sysfs_show_state(struct device *dev,
2455 struct device_attribute *attr,
2456 char *buf)
2457{
2458 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2459 static const char *const state_name[] = {
2460 [NVME_CTRL_NEW] = "new",
2461 [NVME_CTRL_LIVE] = "live",
2462 [NVME_CTRL_RESETTING] = "resetting",
2463 [NVME_CTRL_RECONNECTING]= "reconnecting",
2464 [NVME_CTRL_DELETING] = "deleting",
2465 [NVME_CTRL_DEAD] = "dead",
2466 };
2467
2468 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2469 state_name[ctrl->state])
2470 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2471
2472 return sprintf(buf, "unknown state\n");
2473}
2474
2475static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2476
Ming Lin1a353d82016-06-13 16:45:24 +02002477static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2478 struct device_attribute *attr,
2479 char *buf)
2480{
2481 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2482
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002483 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002484}
2485static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2486
2487static ssize_t nvme_sysfs_show_address(struct device *dev,
2488 struct device_attribute *attr,
2489 char *buf)
2490{
2491 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2492
2493 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2494}
2495static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2496
Keith Busch779ff7562016-01-12 15:09:31 -07002497static struct attribute *nvme_dev_attrs[] = {
2498 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002499 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002500 &dev_attr_model.attr,
2501 &dev_attr_serial.attr,
2502 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002503 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002504 &dev_attr_delete_controller.attr,
2505 &dev_attr_transport.attr,
2506 &dev_attr_subsysnqn.attr,
2507 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002508 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002509 NULL
2510};
2511
Ming Lin1a353d82016-06-13 16:45:24 +02002512static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2513 struct attribute *a, int n)
2514{
2515 struct device *dev = container_of(kobj, struct device, kobj);
2516 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2517
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002518 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2519 return 0;
2520 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2521 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002522
2523 return a->mode;
2524}
2525
Keith Busch779ff7562016-01-12 15:09:31 -07002526static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002527 .attrs = nvme_dev_attrs,
2528 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002529};
2530
2531static const struct attribute_group *nvme_dev_attr_groups[] = {
2532 &nvme_dev_attrs_group,
2533 NULL,
2534};
2535
Christoph Hellwiged754e52017-11-09 13:50:43 +01002536static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2537 unsigned nsid)
2538{
2539 struct nvme_ns_head *h;
2540
2541 lockdep_assert_held(&subsys->lock);
2542
2543 list_for_each_entry(h, &subsys->nsheads, entry) {
2544 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2545 return h;
2546 }
2547
2548 return NULL;
2549}
2550
2551static int __nvme_check_ids(struct nvme_subsystem *subsys,
2552 struct nvme_ns_head *new)
2553{
2554 struct nvme_ns_head *h;
2555
2556 lockdep_assert_held(&subsys->lock);
2557
2558 list_for_each_entry(h, &subsys->nsheads, entry) {
2559 if (nvme_ns_ids_valid(&new->ids) &&
2560 nvme_ns_ids_equal(&new->ids, &h->ids))
2561 return -EINVAL;
2562 }
2563
2564 return 0;
2565}
2566
2567static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2568 unsigned nsid, struct nvme_id_ns *id)
2569{
2570 struct nvme_ns_head *head;
2571 int ret = -ENOMEM;
2572
2573 head = kzalloc(sizeof(*head), GFP_KERNEL);
2574 if (!head)
2575 goto out;
2576 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2577 if (ret < 0)
2578 goto out_free_head;
2579 head->instance = ret;
2580 INIT_LIST_HEAD(&head->list);
2581 init_srcu_struct(&head->srcu);
2582 head->subsys = ctrl->subsys;
2583 head->ns_id = nsid;
2584 kref_init(&head->ref);
2585
2586 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2587
2588 ret = __nvme_check_ids(ctrl->subsys, head);
2589 if (ret) {
2590 dev_err(ctrl->device,
2591 "duplicate IDs for nsid %d\n", nsid);
2592 goto out_cleanup_srcu;
2593 }
2594
2595 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
2596 return head;
2597out_cleanup_srcu:
2598 cleanup_srcu_struct(&head->srcu);
2599 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2600out_free_head:
2601 kfree(head);
2602out:
2603 return ERR_PTR(ret);
2604}
2605
2606static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
2607 struct nvme_id_ns *id, bool *new)
2608{
2609 struct nvme_ctrl *ctrl = ns->ctrl;
2610 bool is_shared = id->nmic & (1 << 0);
2611 struct nvme_ns_head *head = NULL;
2612 int ret = 0;
2613
2614 mutex_lock(&ctrl->subsys->lock);
2615 if (is_shared)
2616 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2617 if (!head) {
2618 head = nvme_alloc_ns_head(ctrl, nsid, id);
2619 if (IS_ERR(head)) {
2620 ret = PTR_ERR(head);
2621 goto out_unlock;
2622 }
2623
2624 *new = true;
2625 } else {
2626 struct nvme_ns_ids ids;
2627
2628 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2629 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2630 dev_err(ctrl->device,
2631 "IDs don't match for shared namespace %d\n",
2632 nsid);
2633 ret = -EINVAL;
2634 goto out_unlock;
2635 }
2636
2637 *new = false;
2638 }
2639
2640 list_add_tail(&ns->siblings, &head->list);
2641 ns->head = head;
2642
2643out_unlock:
2644 mutex_unlock(&ctrl->subsys->lock);
2645 return ret;
2646}
2647
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002648static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2649{
2650 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2651 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2652
Christoph Hellwiged754e52017-11-09 13:50:43 +01002653 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002654}
2655
Keith Busch32f0c4a2016-07-13 11:45:02 -06002656static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002657{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002658 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002659
Keith Busch32f0c4a2016-07-13 11:45:02 -06002660 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002661 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002662 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002663 if (!kref_get_unless_zero(&ns->kref))
2664 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002665 ret = ns;
2666 break;
2667 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002668 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002669 break;
2670 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002671 mutex_unlock(&ctrl->namespaces_mutex);
2672 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002673}
2674
Jens Axboef5d11842017-06-27 12:03:06 -06002675static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2676{
2677 struct streams_directive_params s;
2678 int ret;
2679
2680 if (!ctrl->nr_streams)
2681 return 0;
2682
Christoph Hellwiged754e52017-11-09 13:50:43 +01002683 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002684 if (ret)
2685 return ret;
2686
2687 ns->sws = le32_to_cpu(s.sws);
2688 ns->sgs = le16_to_cpu(s.sgs);
2689
2690 if (ns->sws) {
2691 unsigned int bs = 1 << ns->lba_shift;
2692
2693 blk_queue_io_min(ns->queue, bs * ns->sws);
2694 if (ns->sgs)
2695 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2696 }
2697
2698 return 0;
2699}
2700
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002701static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2702{
2703 struct nvme_ns *ns;
2704 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002705 struct nvme_id_ns *id;
2706 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002707 int node = dev_to_node(ctrl->dev);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002708 bool new = true;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002709
2710 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2711 if (!ns)
2712 return;
2713
2714 ns->queue = blk_mq_init_queue(ctrl->tagset);
2715 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002716 goto out_free_ns;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002717 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2718 ns->queue->queuedata = ns;
2719 ns->ctrl = ctrl;
2720
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002721 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002722 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002723
2724 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002725 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002726 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002727
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002728 id = nvme_identify_ns(ctrl, nsid);
2729 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002730 goto out_free_queue;
2731
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002732 if (id->ncap == 0)
2733 goto out_free_id;
2734
Christoph Hellwiged754e52017-11-09 13:50:43 +01002735 if (nvme_init_ns_head(ns, nsid, id, &new))
2736 goto out_free_id;
2737
2738 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002739
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002740 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2741 if (nvme_nvm_register(ns, disk_name, node)) {
2742 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01002743 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002744 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002745 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002746
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002747 disk = alloc_disk_node(0, node);
2748 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002749 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002750
2751 disk->fops = &nvme_fops;
2752 disk->private_data = ns;
2753 disk->queue = ns->queue;
2754 disk->flags = GENHD_FL_EXT_DEVT;
2755 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2756 ns->disk = disk;
2757
2758 __nvme_revalidate_disk(disk, id);
2759
Keith Busch32f0c4a2016-07-13 11:45:02 -06002760 mutex_lock(&ctrl->namespaces_mutex);
2761 list_add_tail(&ns->list, &ctrl->namespaces);
2762 mutex_unlock(&ctrl->namespaces_mutex);
2763
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002764 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002765
2766 kfree(id);
2767
Dan Williams0d52c7562016-06-15 19:44:20 -07002768 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002769 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2770 &nvme_ns_attr_group))
2771 pr_warn("%s: failed to create sysfs group for identification\n",
2772 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002773 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2774 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2775 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002776 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01002777 out_unlink_ns:
2778 mutex_lock(&ctrl->subsys->lock);
2779 list_del_rcu(&ns->siblings);
2780 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002781 out_free_id:
2782 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002783 out_free_queue:
2784 blk_cleanup_queue(ns->queue);
2785 out_free_ns:
2786 kfree(ns);
2787}
2788
2789static void nvme_ns_remove(struct nvme_ns *ns)
2790{
Christoph Hellwiged754e52017-11-09 13:50:43 +01002791 struct nvme_ns_head *head = ns->head;
2792
Keith Busch646017a2016-02-24 09:15:54 -07002793 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2794 return;
2795
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002796 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002797 if (blk_get_integrity(ns->disk))
2798 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002799 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2800 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002801 if (ns->ndev)
2802 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002803 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002804 blk_cleanup_queue(ns->queue);
2805 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002806
Christoph Hellwiged754e52017-11-09 13:50:43 +01002807 mutex_lock(&ns->ctrl->subsys->lock);
2808 if (head)
2809 list_del_rcu(&ns->siblings);
2810 mutex_unlock(&ns->ctrl->subsys->lock);
2811
Keith Busch32f0c4a2016-07-13 11:45:02 -06002812 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002813 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002814 mutex_unlock(&ns->ctrl->namespaces_mutex);
2815
Christoph Hellwiged754e52017-11-09 13:50:43 +01002816 synchronize_srcu(&head->srcu);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002817 nvme_put_ns(ns);
2818}
2819
Keith Busch540c8012015-10-22 15:45:06 -06002820static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2821{
2822 struct nvme_ns *ns;
2823
Keith Busch32f0c4a2016-07-13 11:45:02 -06002824 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002825 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002826 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002827 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002828 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002829 } else
2830 nvme_alloc_ns(ctrl, nsid);
2831}
2832
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302833static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2834 unsigned nsid)
2835{
2836 struct nvme_ns *ns, *next;
2837
2838 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002839 if (ns->head->ns_id > nsid)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302840 nvme_ns_remove(ns);
2841 }
2842}
2843
Keith Busch540c8012015-10-22 15:45:06 -06002844static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2845{
2846 struct nvme_ns *ns;
2847 __le32 *ns_list;
2848 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2849 int ret = 0;
2850
2851 ns_list = kzalloc(0x1000, GFP_KERNEL);
2852 if (!ns_list)
2853 return -ENOMEM;
2854
2855 for (i = 0; i < num_lists; i++) {
2856 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2857 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302858 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002859
2860 for (j = 0; j < min(nn, 1024U); j++) {
2861 nsid = le32_to_cpu(ns_list[j]);
2862 if (!nsid)
2863 goto out;
2864
2865 nvme_validate_ns(ctrl, nsid);
2866
2867 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002868 ns = nvme_find_get_ns(ctrl, prev);
2869 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002870 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002871 nvme_put_ns(ns);
2872 }
Keith Busch540c8012015-10-22 15:45:06 -06002873 }
2874 }
2875 nn -= j;
2876 }
2877 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302878 nvme_remove_invalid_namespaces(ctrl, prev);
2879 free:
Keith Busch540c8012015-10-22 15:45:06 -06002880 kfree(ns_list);
2881 return ret;
2882}
2883
Christoph Hellwig5955be22016-04-26 13:51:59 +02002884static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002885{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002886 unsigned i;
2887
Keith Busch540c8012015-10-22 15:45:06 -06002888 for (i = 1; i <= nn; i++)
2889 nvme_validate_ns(ctrl, i);
2890
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302891 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002892}
2893
Christoph Hellwig5955be22016-04-26 13:51:59 +02002894static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002895{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002896 struct nvme_ctrl *ctrl =
2897 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002898 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002899 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002900
Christoph Hellwig5955be22016-04-26 13:51:59 +02002901 if (ctrl->state != NVME_CTRL_LIVE)
2902 return;
2903
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002904 if (nvme_identify_ctrl(ctrl, &id))
2905 return;
Keith Busch540c8012015-10-22 15:45:06 -06002906
2907 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002908 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002909 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2910 if (!nvme_scan_ns_list(ctrl, nn))
2911 goto done;
2912 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002913 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002914 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002915 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002916 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002917 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002918 kfree(id);
2919}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002920
2921void nvme_queue_scan(struct nvme_ctrl *ctrl)
2922{
2923 /*
2924 * Do not queue new scan work when a controller is reset during
2925 * removal.
2926 */
2927 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002928 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002929}
2930EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002931
Keith Busch32f0c4a2016-07-13 11:45:02 -06002932/*
2933 * This function iterates the namespace list unlocked to allow recovery from
2934 * controller failure. It is up to the caller to ensure the namespace list is
2935 * not modified by scan work while this function is executing.
2936 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002937void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2938{
2939 struct nvme_ns *ns, *next;
2940
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002941 /*
2942 * The dead states indicates the controller was not gracefully
2943 * disconnected. In that case, we won't be able to flush any data while
2944 * removing the namespaces' disks; fail all the queues now to avoid
2945 * potentially having to clean up the failed sync later.
2946 */
2947 if (ctrl->state == NVME_CTRL_DEAD)
2948 nvme_kill_queues(ctrl);
2949
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002950 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2951 nvme_ns_remove(ns);
2952}
Ming Lin576d55d2016-02-10 10:03:32 -08002953EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002954
Keith Busche3d78742017-11-07 15:13:14 -07002955static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
2956{
2957 char *envp[2] = { NULL, NULL };
2958 u32 aen_result = ctrl->aen_result;
2959
2960 ctrl->aen_result = 0;
2961 if (!aen_result)
2962 return;
2963
2964 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
2965 if (!envp[0])
2966 return;
2967 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
2968 kfree(envp[0]);
2969}
2970
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002971static void nvme_async_event_work(struct work_struct *work)
2972{
2973 struct nvme_ctrl *ctrl =
2974 container_of(work, struct nvme_ctrl, async_event_work);
2975
Keith Busche3d78742017-11-07 15:13:14 -07002976 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07002977 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002978}
2979
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302980static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2981{
2982
2983 u32 csts;
2984
2985 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2986 return false;
2987
2988 if (csts == ~0)
2989 return false;
2990
2991 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2992}
2993
2994static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2995{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302996 struct nvme_fw_slot_info_log *log;
2997
2998 log = kmalloc(sizeof(*log), GFP_KERNEL);
2999 if (!log)
3000 return;
3001
Keith Buschc627c482017-11-07 10:28:31 -07003002 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303003 dev_warn(ctrl->device,
3004 "Get FW SLOT INFO log error\n");
3005 kfree(log);
3006}
3007
3008static void nvme_fw_act_work(struct work_struct *work)
3009{
3010 struct nvme_ctrl *ctrl = container_of(work,
3011 struct nvme_ctrl, fw_act_work);
3012 unsigned long fw_act_timeout;
3013
3014 if (ctrl->mtfa)
3015 fw_act_timeout = jiffies +
3016 msecs_to_jiffies(ctrl->mtfa * 100);
3017 else
3018 fw_act_timeout = jiffies +
3019 msecs_to_jiffies(admin_timeout * 1000);
3020
3021 nvme_stop_queues(ctrl);
3022 while (nvme_ctrl_pp_status(ctrl)) {
3023 if (time_after(jiffies, fw_act_timeout)) {
3024 dev_warn(ctrl->device,
3025 "Fw activation timeout, reset controller\n");
3026 nvme_reset_ctrl(ctrl);
3027 break;
3028 }
3029 msleep(100);
3030 }
3031
3032 if (ctrl->state != NVME_CTRL_LIVE)
3033 return;
3034
3035 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003036 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303037 nvme_get_fw_slot_info(ctrl);
3038}
3039
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003040void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3041 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003042{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003043 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003044
Keith Buschad22c352017-11-07 15:13:12 -07003045 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003046 return;
3047
Keith Busche3d78742017-11-07 15:13:14 -07003048 switch (result & 0x7) {
3049 case NVME_AER_ERROR:
3050 case NVME_AER_SMART:
3051 case NVME_AER_CSS:
3052 case NVME_AER_VS:
3053 ctrl->aen_result = result;
3054 break;
3055 default:
3056 break;
3057 }
3058
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003059 switch (result & 0xff07) {
3060 case NVME_AER_NOTICE_NS_CHANGED:
3061 dev_info(ctrl->device, "rescanning\n");
3062 nvme_queue_scan(ctrl);
3063 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303064 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003065 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303066 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003067 default:
3068 dev_warn(ctrl->device, "async event result %08x\n", result);
3069 }
Keith Buschad22c352017-11-07 15:13:12 -07003070 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003071}
3072EXPORT_SYMBOL_GPL(nvme_complete_async_event);
3073
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003074void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003075{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003076 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003077 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003078 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303079 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003080}
3081EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003082
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003083void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3084{
3085 if (ctrl->kato)
3086 nvme_start_keep_alive(ctrl);
3087
3088 if (ctrl->queue_count > 1) {
3089 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003090 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003091 nvme_start_queues(ctrl);
3092 }
3093}
3094EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3095
3096void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3097{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003098 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003099}
Ming Lin576d55d2016-02-10 10:03:32 -08003100EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003101
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003102static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003103{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003104 struct nvme_ctrl *ctrl =
3105 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003106 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003107
Christoph Hellwig9843f682017-10-18 13:10:01 +02003108 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003109 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003110
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003111 if (subsys) {
3112 mutex_lock(&subsys->lock);
3113 list_del(&ctrl->subsys_entry);
3114 mutex_unlock(&subsys->lock);
3115 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3116 }
3117
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003118 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003119
3120 if (subsys)
3121 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003122}
3123
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003124/*
3125 * Initialize a NVMe controller structures. This needs to be called during
3126 * earliest initialization so that we have the initialized structured around
3127 * during probing.
3128 */
3129int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3130 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3131{
3132 int ret;
3133
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003134 ctrl->state = NVME_CTRL_NEW;
3135 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003136 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01003137 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003138 ctrl->dev = dev;
3139 ctrl->ops = ops;
3140 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003141 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003142 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303143 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003144 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003145
Christoph Hellwig9843f682017-10-18 13:10:01 +02003146 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3147 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003148 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003149 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003150
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003151 device_initialize(&ctrl->ctrl_device);
3152 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003153 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003154 ctrl->device->class = nvme_class;
3155 ctrl->device->parent = ctrl->dev;
3156 ctrl->device->groups = nvme_dev_attr_groups;
3157 ctrl->device->release = nvme_free_ctrl;
3158 dev_set_drvdata(ctrl->device, ctrl);
3159 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3160 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003161 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003162
3163 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3164 ctrl->cdev.owner = ops->module;
3165 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003166 if (ret)
3167 goto out_free_name;
3168
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003169 /*
3170 * Initialize latency tolerance controls. The sysfs files won't
3171 * be visible to userspace unless the device actually supports APST.
3172 */
3173 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3174 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3175 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3176
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003177 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003178out_free_name:
3179 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003180out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003181 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003182out:
3183 return ret;
3184}
Ming Lin576d55d2016-02-10 10:03:32 -08003185EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003186
Keith Busch69d9a992016-02-24 09:15:56 -07003187/**
3188 * nvme_kill_queues(): Ends all namespace queues
3189 * @ctrl: the dead controller that needs to end
3190 *
3191 * Call this function when the driver determines it is unable to get the
3192 * controller in a state capable of servicing IO.
3193 */
3194void nvme_kill_queues(struct nvme_ctrl *ctrl)
3195{
3196 struct nvme_ns *ns;
3197
Keith Busch32f0c4a2016-07-13 11:45:02 -06003198 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08003199
Ming Lei443bd902017-06-19 10:21:08 +08003200 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003201 if (ctrl->admin_q)
3202 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003203
Keith Busch32f0c4a2016-07-13 11:45:02 -06003204 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003205 /*
3206 * Revalidating a dead namespace sets capacity to 0. This will
3207 * end buffered writers dirtying pages that can't be synced.
3208 */
Keith Buschf33447b2017-02-10 18:15:51 -05003209 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3210 continue;
3211 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003212 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003213
Ming Lei443bd902017-06-19 10:21:08 +08003214 /* Forcibly unquiesce queues to avoid blocking dispatch */
3215 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003216 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003217 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07003218}
Linus Torvalds237045f2016-03-18 17:13:31 -07003219EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003220
Keith Busch302ad8c2017-03-01 14:22:12 -05003221void nvme_unfreeze(struct nvme_ctrl *ctrl)
3222{
3223 struct nvme_ns *ns;
3224
3225 mutex_lock(&ctrl->namespaces_mutex);
3226 list_for_each_entry(ns, &ctrl->namespaces, list)
3227 blk_mq_unfreeze_queue(ns->queue);
3228 mutex_unlock(&ctrl->namespaces_mutex);
3229}
3230EXPORT_SYMBOL_GPL(nvme_unfreeze);
3231
3232void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3233{
3234 struct nvme_ns *ns;
3235
3236 mutex_lock(&ctrl->namespaces_mutex);
3237 list_for_each_entry(ns, &ctrl->namespaces, list) {
3238 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3239 if (timeout <= 0)
3240 break;
3241 }
3242 mutex_unlock(&ctrl->namespaces_mutex);
3243}
3244EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3245
3246void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3247{
3248 struct nvme_ns *ns;
3249
3250 mutex_lock(&ctrl->namespaces_mutex);
3251 list_for_each_entry(ns, &ctrl->namespaces, list)
3252 blk_mq_freeze_queue_wait(ns->queue);
3253 mutex_unlock(&ctrl->namespaces_mutex);
3254}
3255EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3256
3257void nvme_start_freeze(struct nvme_ctrl *ctrl)
3258{
3259 struct nvme_ns *ns;
3260
3261 mutex_lock(&ctrl->namespaces_mutex);
3262 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003263 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05003264 mutex_unlock(&ctrl->namespaces_mutex);
3265}
3266EXPORT_SYMBOL_GPL(nvme_start_freeze);
3267
Keith Busch25646262016-01-04 09:10:57 -07003268void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003269{
3270 struct nvme_ns *ns;
3271
Keith Busch32f0c4a2016-07-13 11:45:02 -06003272 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003273 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003274 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003275 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003276}
Ming Lin576d55d2016-02-10 10:03:32 -08003277EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003278
Keith Busch25646262016-01-04 09:10:57 -07003279void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003280{
3281 struct nvme_ns *ns;
3282
Keith Busch32f0c4a2016-07-13 11:45:02 -06003283 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003284 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003285 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003286 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003287}
Ming Lin576d55d2016-02-10 10:03:32 -08003288EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003289
Sagi Grimberg31b84462017-10-11 12:53:07 +03003290int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3291{
3292 if (!ctrl->ops->reinit_request)
3293 return 0;
3294
3295 return blk_mq_tagset_iter(set, set->driver_data,
3296 ctrl->ops->reinit_request);
3297}
3298EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3299
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003300int __init nvme_core_init(void)
3301{
3302 int result;
3303
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003304 nvme_wq = alloc_workqueue("nvme-wq",
3305 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3306 if (!nvme_wq)
3307 return -ENOMEM;
3308
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003309 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003310 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003311 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003312
3313 nvme_class = class_create(THIS_MODULE, "nvme");
3314 if (IS_ERR(nvme_class)) {
3315 result = PTR_ERR(nvme_class);
3316 goto unregister_chrdev;
3317 }
3318
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003319 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3320 if (IS_ERR(nvme_subsys_class)) {
3321 result = PTR_ERR(nvme_subsys_class);
3322 goto destroy_class;
3323 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003324 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003325
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003326destroy_class:
3327 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003328unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003329 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003330destroy_wq:
3331 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003332 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003333}
3334
3335void nvme_core_exit(void)
3336{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003337 ida_destroy(&nvme_subsystems_ida);
3338 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003339 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003340 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003341 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003342}
Ming Lin576d55d2016-02-10 10:03:32 -08003343
3344MODULE_LICENSE("GPL");
3345MODULE_VERSION("1.0");
3346module_init(nvme_core_init);
3347module_exit(nvme_core_exit);