blob: 878d5c09d15b2905fa1ce576085fa5ecf2019cf1 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Marc Olson8ae4e442017-09-06 17:23:56 -070037unsigned int admin_timeout = 60;
38module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080039MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
Marc Olson8ae4e442017-09-06 17:23:56 -070042unsigned int nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080044MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020047static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080048module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Christoph Hellwig44e44b22017-04-05 19:18:11 +020051static u8 nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070053MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010054
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080055static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080056module_param(default_ps_max_latency_us, ulong, 0644);
57MODULE_PARM_DESC(default_ps_max_latency_us,
58 "max power saving latency for new devices; use PM QOS to change per device");
59
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070060static bool force_apst;
61module_param(force_apst, bool, 0644);
62MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
63
Jens Axboef5d11842017-06-27 12:03:06 -060064static bool streams;
65module_param(streams, bool, 0644);
66MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwig9843f682017-10-18 13:10:01 +020071static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020072static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010073static struct class *nvme_class;
74
Keith Busch84fef622017-11-07 10:28:32 -070075static void nvme_ns_remove(struct nvme_ns *ns);
76static int nvme_revalidate_disk(struct gendisk *disk);
77
Arnav Dawnb6dccf72017-07-12 16:10:40 +053078static __le32 nvme_get_log_dw10(u8 lid, size_t size)
79{
80 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
81}
82
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020083int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
84{
85 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
86 return -EBUSY;
87 if (!queue_work(nvme_wq, &ctrl->reset_work))
88 return -EBUSY;
89 return 0;
90}
91EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
92
93static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
94{
95 int ret;
96
97 ret = nvme_reset_ctrl(ctrl);
98 if (!ret)
99 flush_work(&ctrl->reset_work);
100 return ret;
101}
102
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200103static void nvme_delete_ctrl_work(struct work_struct *work)
104{
105 struct nvme_ctrl *ctrl =
106 container_of(work, struct nvme_ctrl, delete_work);
107
Sagi Grimberg40546372017-10-29 14:21:02 +0200108 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200109 nvme_stop_ctrl(ctrl);
110 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200111 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200112 nvme_uninit_ctrl(ctrl);
113 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200114}
115
116int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
117{
118 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
119 return -EBUSY;
120 if (!queue_work(nvme_wq, &ctrl->delete_work))
121 return -EBUSY;
122 return 0;
123}
124EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
125
126int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
127{
128 int ret = 0;
129
130 /*
131 * Keep a reference until the work is flushed since ->delete_ctrl
132 * can free the controller.
133 */
134 nvme_get_ctrl(ctrl);
135 ret = nvme_delete_ctrl(ctrl);
136 if (!ret)
137 flush_work(&ctrl->delete_work);
138 nvme_put_ctrl(ctrl);
139 return ret;
140}
141EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
142
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100143static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
144{
145 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
146}
147
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200148static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200149{
150 switch (nvme_req(req)->status & 0x7ff) {
151 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200152 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200153 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200154 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200155 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200156 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200157 case NVME_SC_WRITE_FAULT:
158 case NVME_SC_READ_ERROR:
159 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200160 case NVME_SC_ACCESS_DENIED:
161 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200162 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200163 case NVME_SC_GUARD_CHECK:
164 case NVME_SC_APPTAG_CHECK:
165 case NVME_SC_REFTAG_CHECK:
166 case NVME_SC_INVALID_PI:
167 return BLK_STS_PROTECTION;
168 case NVME_SC_RESERVATION_CONFLICT:
169 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200170 default:
171 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200172 }
173}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200174
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200175static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200176{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200177 if (blk_noretry_request(req))
178 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200179 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200180 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200181 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200182 return false;
Christoph Hellwige54b0642017-11-02 21:28:51 +0300183 if (blk_queue_dying(req->q))
184 return false;
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200185 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200186}
187
188void nvme_complete_rq(struct request *req)
189{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200190 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
191 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300192 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200193 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200194 }
195
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200196 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200197}
198EXPORT_SYMBOL_GPL(nvme_complete_rq);
199
Ming Linc55a2fd2016-05-18 14:05:02 -0700200void nvme_cancel_request(struct request *req, void *data, bool reserved)
201{
Ming Linc55a2fd2016-05-18 14:05:02 -0700202 if (!blk_mq_request_started(req))
203 return;
204
205 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
206 "Cancelling I/O %d", req->tag);
207
Christoph Hellwige54b0642017-11-02 21:28:51 +0300208 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200209 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200210
Ming Linc55a2fd2016-05-18 14:05:02 -0700211}
212EXPORT_SYMBOL_GPL(nvme_cancel_request);
213
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200214bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
215 enum nvme_ctrl_state new_state)
216{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300217 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200218 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200219 bool changed = false;
220
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200221 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300222
223 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200224 switch (new_state) {
225 case NVME_CTRL_LIVE:
226 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200227 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200228 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900229 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200230 changed = true;
231 /* FALLTHRU */
232 default:
233 break;
234 }
235 break;
236 case NVME_CTRL_RESETTING:
237 switch (old_state) {
238 case NVME_CTRL_NEW:
239 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900240 changed = true;
241 /* FALLTHRU */
242 default:
243 break;
244 }
245 break;
246 case NVME_CTRL_RECONNECTING:
247 switch (old_state) {
248 case NVME_CTRL_LIVE:
James Smart3cec7f92017-10-25 16:43:13 -0700249 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200250 changed = true;
251 /* FALLTHRU */
252 default:
253 break;
254 }
255 break;
256 case NVME_CTRL_DELETING:
257 switch (old_state) {
258 case NVME_CTRL_LIVE:
259 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900260 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200261 changed = true;
262 /* FALLTHRU */
263 default:
264 break;
265 }
266 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600267 case NVME_CTRL_DEAD:
268 switch (old_state) {
269 case NVME_CTRL_DELETING:
270 changed = true;
271 /* FALLTHRU */
272 default:
273 break;
274 }
275 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200276 default:
277 break;
278 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200279
280 if (changed)
281 ctrl->state = new_state;
282
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200283 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300284
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200285 return changed;
286}
287EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
288
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100289static void nvme_free_ns(struct kref *kref)
290{
291 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
292
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200293 if (ns->ndev)
294 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100295
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100296 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700297 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
298 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100299 kfree(ns);
300}
301
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100302static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100303{
304 kref_put(&ns->kref, nvme_free_ns);
305}
306
Christoph Hellwig41609822015-11-20 09:00:02 +0100307struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800308 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100309{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100310 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100312
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200313 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100314 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200315 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100316 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200317 qid ? qid - 1 : 0);
318 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100319 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100320 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100321
Christoph Hellwig21d34712015-11-26 09:08:36 +0100322 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800323 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100324
Christoph Hellwig41609822015-11-20 09:00:02 +0100325 return req;
326}
Ming Lin576d55d2016-02-10 10:03:32 -0800327EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100328
Jens Axboef5d11842017-06-27 12:03:06 -0600329static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
330{
331 struct nvme_command c;
332
333 memset(&c, 0, sizeof(c));
334
335 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530336 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600337 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
338 c.directive.dtype = NVME_DIR_IDENTIFY;
339 c.directive.tdtype = NVME_DIR_STREAMS;
340 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
341
342 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
343}
344
345static int nvme_disable_streams(struct nvme_ctrl *ctrl)
346{
347 return nvme_toggle_streams(ctrl, false);
348}
349
350static int nvme_enable_streams(struct nvme_ctrl *ctrl)
351{
352 return nvme_toggle_streams(ctrl, true);
353}
354
355static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
356 struct streams_directive_params *s, u32 nsid)
357{
358 struct nvme_command c;
359
360 memset(&c, 0, sizeof(c));
361 memset(s, 0, sizeof(*s));
362
363 c.directive.opcode = nvme_admin_directive_recv;
364 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700365 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600366 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
367 c.directive.dtype = NVME_DIR_STREAMS;
368
369 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
370}
371
372static int nvme_configure_directives(struct nvme_ctrl *ctrl)
373{
374 struct streams_directive_params s;
375 int ret;
376
377 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
378 return 0;
379 if (!streams)
380 return 0;
381
382 ret = nvme_enable_streams(ctrl);
383 if (ret)
384 return ret;
385
Arnav Dawn62346ea2017-07-12 16:11:53 +0530386 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600387 if (ret)
388 return ret;
389
390 ctrl->nssa = le16_to_cpu(s.nssa);
391 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
392 dev_info(ctrl->device, "too few streams (%u) available\n",
393 ctrl->nssa);
394 nvme_disable_streams(ctrl);
395 return 0;
396 }
397
398 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
399 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
400 return 0;
401}
402
403/*
404 * Check if 'req' has a write hint associated with it. If it does, assign
405 * a valid namespace stream to the write.
406 */
407static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
408 struct request *req, u16 *control,
409 u32 *dsmgmt)
410{
411 enum rw_hint streamid = req->write_hint;
412
413 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
414 streamid = 0;
415 else {
416 streamid--;
417 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
418 return;
419
420 *control |= NVME_RW_DTYPE_STREAMS;
421 *dsmgmt |= streamid << 16;
422 }
423
424 if (streamid < ARRAY_SIZE(req->q->write_hints))
425 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
426}
427
Ming Lin8093f7c2016-04-12 13:10:14 -0600428static inline void nvme_setup_flush(struct nvme_ns *ns,
429 struct nvme_command *cmnd)
430{
431 memset(cmnd, 0, sizeof(*cmnd));
432 cmnd->common.opcode = nvme_cmd_flush;
433 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
434}
435
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200436static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600437 struct nvme_command *cmnd)
438{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100439 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600440 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100441 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600442
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100443 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600444 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200445 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600446
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100447 __rq_for_each_bio(bio, req) {
448 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
449 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
450
451 range[n].cattr = cpu_to_le32(0);
452 range[n].nlb = cpu_to_le32(nlb);
453 range[n].slba = cpu_to_le64(slba);
454 n++;
455 }
456
457 if (WARN_ON_ONCE(n != segments)) {
458 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200459 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100460 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600461
462 memset(cmnd, 0, sizeof(*cmnd));
463 cmnd->dsm.opcode = nvme_cmd_dsm;
464 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200465 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600466 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
467
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700468 req->special_vec.bv_page = virt_to_page(range);
469 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100470 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700471 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600472
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200473 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600474}
Ming Lin8093f7c2016-04-12 13:10:14 -0600475
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200476static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
477 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600478{
Jens Axboef5d11842017-06-27 12:03:06 -0600479 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600480 u16 control = 0;
481 u32 dsmgmt = 0;
482
483 if (req->cmd_flags & REQ_FUA)
484 control |= NVME_RW_FUA;
485 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
486 control |= NVME_RW_LR;
487
488 if (req->cmd_flags & REQ_RAHEAD)
489 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
490
491 memset(cmnd, 0, sizeof(*cmnd));
492 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600493 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
494 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
495 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
496
Jens Axboef5d11842017-06-27 12:03:06 -0600497 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
498 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
499
Ming Lin8093f7c2016-04-12 13:10:14 -0600500 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100501 /*
502 * If formated with metadata, the block layer always provides a
503 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
504 * we enable the PRACT bit for protection information or set the
505 * namespace capacity to zero to prevent any I/O.
506 */
507 if (!blk_integrity_rq(req)) {
508 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
509 return BLK_STS_NOTSUPP;
510 control |= NVME_RW_PRINFO_PRACT;
511 }
512
Ming Lin8093f7c2016-04-12 13:10:14 -0600513 switch (ns->pi_type) {
514 case NVME_NS_DPS_PI_TYPE3:
515 control |= NVME_RW_PRINFO_PRCHK_GUARD;
516 break;
517 case NVME_NS_DPS_PI_TYPE1:
518 case NVME_NS_DPS_PI_TYPE2:
519 control |= NVME_RW_PRINFO_PRCHK_GUARD |
520 NVME_RW_PRINFO_PRCHK_REF;
521 cmnd->rw.reftag = cpu_to_le32(
522 nvme_block_nr(ns, blk_rq_pos(req)));
523 break;
524 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600525 }
526
527 cmnd->rw.control = cpu_to_le16(control);
528 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200529 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600530}
531
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200532blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600533 struct nvme_command *cmd)
534{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200535 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600536
Christoph Hellwig987f6992017-04-05 19:18:08 +0200537 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200538 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200539 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200540 req->rq_flags |= RQF_DONTPREP;
541 }
542
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100543 switch (req_op(req)) {
544 case REQ_OP_DRV_IN:
545 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800546 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100547 break;
548 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600549 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100550 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200551 case REQ_OP_WRITE_ZEROES:
552 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100553 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600554 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100555 break;
556 case REQ_OP_READ:
557 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200558 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100559 break;
560 default:
561 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200562 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100563 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600564
James Smart721b3912016-10-21 23:33:34 +0300565 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600566 return ret;
567}
568EXPORT_SYMBOL_GPL(nvme_setup_cmd);
569
Christoph Hellwig41609822015-11-20 09:00:02 +0100570/*
571 * Returns 0 on success. If the result is negative, it's a Linux error code;
572 * if the result is positive, it's an NVM Express status code
573 */
574int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800575 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800576 unsigned timeout, int qid, int at_head,
577 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100578{
579 struct request *req;
580 int ret;
581
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200582 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100583 if (IS_ERR(req))
584 return PTR_ERR(req);
585
586 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
587
Christoph Hellwig21d34712015-11-26 09:08:36 +0100588 if (buffer && bufflen) {
589 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
590 if (ret)
591 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100592 }
593
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200594 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800595 if (result)
596 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200597 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
598 ret = -EINTR;
599 else
600 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100601 out:
602 blk_mq_free_request(req);
603 return ret;
604}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200605EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100606
607int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
608 void *buffer, unsigned bufflen)
609{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200610 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
611 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100612}
Ming Lin576d55d2016-02-10 10:03:32 -0800613EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100614
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400615static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
616 unsigned len, u32 seed, bool write)
617{
618 struct bio_integrity_payload *bip;
619 int ret = -ENOMEM;
620 void *buf;
621
622 buf = kmalloc(len, GFP_KERNEL);
623 if (!buf)
624 goto out;
625
626 ret = -EFAULT;
627 if (write && copy_from_user(buf, ubuf, len))
628 goto out_free_meta;
629
630 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
631 if (IS_ERR(bip)) {
632 ret = PTR_ERR(bip);
633 goto out_free_meta;
634 }
635
636 bip->bip_iter.bi_size = len;
637 bip->bip_iter.bi_sector = seed;
638 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
639 offset_in_page(buf));
640 if (ret == len)
641 return buf;
642 ret = -ENOMEM;
643out_free_meta:
644 kfree(buf);
645out:
646 return ERR_PTR(ret);
647}
648
Keith Busch63263d62017-08-29 17:46:04 -0400649static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400650 struct nvme_command *cmd, void __user *ubuffer,
651 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
652 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100653{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200654 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600655 struct nvme_ns *ns = q->queuedata;
656 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100657 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600658 struct bio *bio = NULL;
659 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100660 int ret;
661
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200662 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100663 if (IS_ERR(req))
664 return PTR_ERR(req);
665
666 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
667
668 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100669 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
670 GFP_KERNEL);
671 if (ret)
672 goto out;
673 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200674 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400675 if (disk && meta_buffer && meta_len) {
676 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
677 meta_seed, write);
678 if (IS_ERR(meta)) {
679 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600680 goto out_unmap;
681 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600682 }
683 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400684
Keith Busch0b7f1f22015-10-23 09:47:28 -0600685 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200686 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
687 ret = -EINTR;
688 else
689 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100690 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800691 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600692 if (meta && !ret && !write) {
693 if (copy_to_user(meta_buffer, meta, meta_len))
694 ret = -EFAULT;
695 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600696 kfree(meta);
697 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200698 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600699 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100700 out:
701 blk_mq_free_request(req);
702 return ret;
703}
704
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200706{
707 struct nvme_ctrl *ctrl = rq->end_io_data;
708
709 blk_mq_free_request(rq);
710
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200711 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200712 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200713 "failed nvme_keep_alive_end_io error=%d\n",
714 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200715 return;
716 }
717
718 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
719}
720
721static int nvme_keep_alive(struct nvme_ctrl *ctrl)
722{
723 struct nvme_command c;
724 struct request *rq;
725
726 memset(&c, 0, sizeof(c));
727 c.common.opcode = nvme_admin_keep_alive;
728
729 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
730 NVME_QID_ANY);
731 if (IS_ERR(rq))
732 return PTR_ERR(rq);
733
734 rq->timeout = ctrl->kato * HZ;
735 rq->end_io_data = ctrl;
736
737 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
738
739 return 0;
740}
741
742static void nvme_keep_alive_work(struct work_struct *work)
743{
744 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
745 struct nvme_ctrl, ka_work);
746
747 if (nvme_keep_alive(ctrl)) {
748 /* allocation failure, reset the controller */
749 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200750 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200751 return;
752 }
753}
754
755void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
756{
757 if (unlikely(ctrl->kato == 0))
758 return;
759
760 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
761 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
762}
763EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
764
765void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
766{
767 if (unlikely(ctrl->kato == 0))
768 return;
769
770 cancel_delayed_work_sync(&ctrl->ka_work);
771}
772EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
773
Keith Busch3f7f25a92017-06-20 15:09:56 -0400774static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100775{
776 struct nvme_command c = { };
777 int error;
778
779 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
780 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200781 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100782
783 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
784 if (!*id)
785 return -ENOMEM;
786
787 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
788 sizeof(struct nvme_id_ctrl));
789 if (error)
790 kfree(*id);
791 return error;
792}
793
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200794static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
795 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200796{
797 struct nvme_command c = { };
798 int status;
799 void *data;
800 int pos;
801 int len;
802
803 c.identify.opcode = nvme_admin_identify;
804 c.identify.nsid = cpu_to_le32(nsid);
805 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
806
807 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
808 if (!data)
809 return -ENOMEM;
810
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200811 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200812 NVME_IDENTIFY_DATA_SIZE);
813 if (status)
814 goto free_data;
815
816 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
817 struct nvme_ns_id_desc *cur = data + pos;
818
819 if (cur->nidl == 0)
820 break;
821
822 switch (cur->nidt) {
823 case NVME_NIDT_EUI64:
824 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200825 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200826 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
827 cur->nidl);
828 goto free_data;
829 }
830 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200831 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200832 break;
833 case NVME_NIDT_NGUID:
834 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200835 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200836 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
837 cur->nidl);
838 goto free_data;
839 }
840 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200841 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200842 break;
843 case NVME_NIDT_UUID:
844 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200845 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200846 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
847 cur->nidl);
848 goto free_data;
849 }
850 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200851 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200852 break;
853 default:
854 /* Skip unnkown types */
855 len = cur->nidl;
856 break;
857 }
858
859 len += sizeof(*cur);
860 }
861free_data:
862 kfree(data);
863 return status;
864}
865
Keith Busch540c8012015-10-22 15:45:06 -0600866static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
867{
868 struct nvme_command c = { };
869
870 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200871 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600872 c.identify.nsid = cpu_to_le32(nsid);
873 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
874}
875
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200876static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
877 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100878{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200879 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100880 struct nvme_command c = { };
881 int error;
882
883 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200884 c.identify.opcode = nvme_admin_identify;
885 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200886 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100887
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200888 id = kmalloc(sizeof(*id), GFP_KERNEL);
889 if (!id)
890 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100891
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200892 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
893 if (error) {
894 dev_warn(ctrl->device, "Identify namespace failed\n");
895 kfree(id);
896 return NULL;
897 }
898
899 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100900}
901
Keith Busch3f7f25a92017-06-20 15:09:56 -0400902static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700903 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100904{
905 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800906 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100907 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100908
909 memset(&c, 0, sizeof(c));
910 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100911 c.features.fid = cpu_to_le32(fid);
912 c.features.dword11 = cpu_to_le32(dword11);
913
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800914 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700915 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700916 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800917 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100918 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100919}
920
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100921int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
922{
923 u32 q_count = (*count - 1) | ((*count - 1) << 16);
924 u32 result;
925 int status, nr_io_queues;
926
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700927 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100928 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200929 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100930 return status;
931
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200932 /*
933 * Degraded controllers might return an error when setting the queue
934 * count. We still want to be able to bring them online and offer
935 * access to the admin queue, as that might be only way to fix them up.
936 */
937 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200938 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200939 *count = 0;
940 } else {
941 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
942 *count = min(*count, nr_io_queues);
943 }
944
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100945 return 0;
946}
Ming Lin576d55d2016-02-10 10:03:32 -0800947EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100948
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100949static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
950{
951 struct nvme_user_io io;
952 struct nvme_command c;
953 unsigned length, meta_len;
954 void __user *metadata;
955
956 if (copy_from_user(&io, uio, sizeof(io)))
957 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700958 if (io.flags)
959 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100960
961 switch (io.opcode) {
962 case nvme_cmd_write:
963 case nvme_cmd_read:
964 case nvme_cmd_compare:
965 break;
966 default:
967 return -EINVAL;
968 }
969
970 length = (io.nblocks + 1) << ns->lba_shift;
971 meta_len = (io.nblocks + 1) * ns->ms;
972 metadata = (void __user *)(uintptr_t)io.metadata;
973
974 if (ns->ext) {
975 length += meta_len;
976 meta_len = 0;
977 } else if (meta_len) {
978 if ((io.metadata & 3) || !io.metadata)
979 return -EINVAL;
980 }
981
982 memset(&c, 0, sizeof(c));
983 c.rw.opcode = io.opcode;
984 c.rw.flags = io.flags;
985 c.rw.nsid = cpu_to_le32(ns->ns_id);
986 c.rw.slba = cpu_to_le64(io.slba);
987 c.rw.length = cpu_to_le16(io.nblocks);
988 c.rw.control = cpu_to_le16(io.control);
989 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
990 c.rw.reftag = cpu_to_le32(io.reftag);
991 c.rw.apptag = cpu_to_le16(io.apptag);
992 c.rw.appmask = cpu_to_le16(io.appmask);
993
Keith Busch63263d62017-08-29 17:46:04 -0400994 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100995 (void __user *)(uintptr_t)io.addr, length,
996 metadata, meta_len, io.slba, NULL, 0);
997}
998
Keith Busch84fef622017-11-07 10:28:32 -0700999static u32 nvme_known_admin_effects(u8 opcode)
1000{
1001 switch (opcode) {
1002 case nvme_admin_format_nvm:
1003 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1004 NVME_CMD_EFFECTS_CSE_MASK;
1005 case nvme_admin_sanitize_nvm:
1006 return NVME_CMD_EFFECTS_CSE_MASK;
1007 default:
1008 break;
1009 }
1010 return 0;
1011}
1012
1013static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1014 u8 opcode)
1015{
1016 u32 effects = 0;
1017
1018 if (ns) {
1019 if (ctrl->effects)
1020 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1021 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1022 dev_warn(ctrl->device,
1023 "IO command:%02x has unhandled effects:%08x\n",
1024 opcode, effects);
1025 return 0;
1026 }
1027
1028 if (ctrl->effects)
1029 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1030 else
1031 effects = nvme_known_admin_effects(opcode);
1032
1033 /*
1034 * For simplicity, IO to all namespaces is quiesced even if the command
1035 * effects say only one namespace is affected.
1036 */
1037 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1038 nvme_start_freeze(ctrl);
1039 nvme_wait_freeze(ctrl);
1040 }
1041 return effects;
1042}
1043
1044static void nvme_update_formats(struct nvme_ctrl *ctrl)
1045{
1046 struct nvme_ns *ns;
1047
1048 mutex_lock(&ctrl->namespaces_mutex);
1049 list_for_each_entry(ns, &ctrl->namespaces, list) {
1050 if (ns->disk && nvme_revalidate_disk(ns->disk))
1051 nvme_ns_remove(ns);
1052 }
1053 mutex_unlock(&ctrl->namespaces_mutex);
1054}
1055
1056static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1057{
1058 /*
1059 * Revalidate LBA changes prior to unfreezing. This is necessary to
1060 * prevent memory corruption if a logical block size was changed by
1061 * this command.
1062 */
1063 if (effects & NVME_CMD_EFFECTS_LBCC)
1064 nvme_update_formats(ctrl);
1065 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1066 nvme_unfreeze(ctrl);
1067 if (effects & NVME_CMD_EFFECTS_CCC)
1068 nvme_init_identify(ctrl);
1069 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1070 nvme_queue_scan(ctrl);
1071}
1072
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001073static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001074 struct nvme_passthru_cmd __user *ucmd)
1075{
1076 struct nvme_passthru_cmd cmd;
1077 struct nvme_command c;
1078 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001079 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001080 int status;
1081
1082 if (!capable(CAP_SYS_ADMIN))
1083 return -EACCES;
1084 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1085 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001086 if (cmd.flags)
1087 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001088
1089 memset(&c, 0, sizeof(c));
1090 c.common.opcode = cmd.opcode;
1091 c.common.flags = cmd.flags;
1092 c.common.nsid = cpu_to_le32(cmd.nsid);
1093 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1094 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1095 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1096 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1097 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1098 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1099 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1100 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1101
1102 if (cmd.timeout_ms)
1103 timeout = msecs_to_jiffies(cmd.timeout_ms);
1104
Keith Busch84fef622017-11-07 10:28:32 -07001105 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001106 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001107 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001108 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1109 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001110 nvme_passthru_end(ctrl, effects);
1111
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001112 if (status >= 0) {
1113 if (put_user(cmd.result, &ucmd->result))
1114 return -EFAULT;
1115 }
1116
1117 return status;
1118}
1119
1120static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1121 unsigned int cmd, unsigned long arg)
1122{
1123 struct nvme_ns *ns = bdev->bd_disk->private_data;
1124
1125 switch (cmd) {
1126 case NVME_IOCTL_ID:
1127 force_successful_syscall_return();
1128 return ns->ns_id;
1129 case NVME_IOCTL_ADMIN_CMD:
1130 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1131 case NVME_IOCTL_IO_CMD:
1132 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1133 case NVME_IOCTL_SUBMIT_IO:
1134 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001135 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001136#ifdef CONFIG_NVM
1137 if (ns->ndev)
1138 return nvme_nvm_ioctl(ns, cmd, arg);
1139#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001140 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001141 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001142 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001143 return -ENOTTY;
1144 }
1145}
1146
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001147static int nvme_open(struct block_device *bdev, fmode_t mode)
1148{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001149 struct nvme_ns *ns = bdev->bd_disk->private_data;
1150
1151 if (!kref_get_unless_zero(&ns->kref))
1152 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001153 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001154}
1155
1156static void nvme_release(struct gendisk *disk, fmode_t mode)
1157{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001158 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001159}
1160
1161static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1162{
1163 /* some standard values */
1164 geo->heads = 1 << 6;
1165 geo->sectors = 1 << 5;
1166 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1167 return 0;
1168}
1169
1170#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001171static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001172{
1173 struct blk_integrity integrity;
1174
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001175 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001176 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001177 case NVME_NS_DPS_PI_TYPE3:
1178 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001179 integrity.tag_size = sizeof(u16) + sizeof(u32);
1180 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001181 break;
1182 case NVME_NS_DPS_PI_TYPE1:
1183 case NVME_NS_DPS_PI_TYPE2:
1184 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001185 integrity.tag_size = sizeof(u16);
1186 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001187 break;
1188 default:
1189 integrity.profile = NULL;
1190 break;
1191 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001192 integrity.tuple_size = ms;
1193 blk_integrity_register(disk, &integrity);
1194 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001195}
1196#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001197static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001198{
1199}
1200#endif /* CONFIG_BLK_DEV_INTEGRITY */
1201
Scott Bauer6b8190d2017-06-15 10:44:30 -06001202static void nvme_set_chunk_size(struct nvme_ns *ns)
1203{
1204 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1205 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1206}
1207
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001208static void nvme_config_discard(struct nvme_ctrl *ctrl,
1209 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001211 u32 size = queue_logical_block_size(queue);
1212
1213 if (stream_alignment)
1214 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001215
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001216 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1217 NVME_DSM_MAX_RANGES);
1218
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001219 queue->limits.discard_alignment = size;
1220 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001221
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001222 blk_queue_max_discard_sectors(queue, UINT_MAX);
1223 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
1224 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001225
1226 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001227 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001228}
1229
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001230static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1231 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001232{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001233 if (ctrl->vs >= NVME_VS(1, 1, 0))
1234 memcpy(eui64, id->eui64, sizeof(id->eui64));
1235 if (ctrl->vs >= NVME_VS(1, 2, 0))
1236 memcpy(nguid, id->nguid, sizeof(id->nguid));
1237 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001238 /* Don't treat error as fatal we potentially
1239 * already have a NGUID or EUI-64
1240 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001241 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1242 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001243 "%s: Identify Descriptors failed\n", __func__);
1244 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001245}
1246
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001247static void nvme_update_disk_info(struct gendisk *disk,
1248 struct nvme_ns *ns, struct nvme_id_ns *id)
1249{
1250 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
1251 unsigned stream_alignment = 0;
1252
1253 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1254 stream_alignment = ns->sws * ns->sgs;
1255
1256 blk_mq_freeze_queue(disk->queue);
1257 blk_integrity_unregister(disk);
1258
1259 blk_queue_logical_block_size(disk->queue, 1 << ns->lba_shift);
1260 if (ns->ms && !ns->ext &&
1261 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1262 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001263 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001264 capacity = 0;
1265 set_capacity(disk, capacity);
1266
1267 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1268 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1269 blk_mq_unfreeze_queue(disk->queue);
1270}
1271
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001272static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1273{
1274 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001275
1276 /*
1277 * If identify namespace failed, use default 512 byte block size so
1278 * block layer can use before failing read/write for 0 capacity.
1279 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001280 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001281 if (ns->lba_shift == 0)
1282 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001283 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001284 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1285 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1286 /* the PI implementation requires metadata equal t10 pi tuple size */
1287 if (ns->ms == sizeof(struct t10_pi_tuple))
1288 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1289 else
1290 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001291
Christoph Hellwig6e78f212017-11-02 21:28:55 +03001292 if (ns->noiob)
1293 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001294 nvme_update_disk_info(disk, ns, id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001295}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001296
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001297static int nvme_revalidate_disk(struct gendisk *disk)
1298{
1299 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001300 struct nvme_ctrl *ctrl = ns->ctrl;
1301 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001302 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1303 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001304 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001305
1306 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1307 set_capacity(disk, 0);
1308 return -ENODEV;
1309 }
1310
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001311 id = nvme_identify_ns(ctrl, ns->ns_id);
1312 if (!id)
1313 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001314
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001315 if (id->ncap == 0) {
1316 ret = -ENODEV;
1317 goto out;
1318 }
1319
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001320 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1321 if (!uuid_equal(&ns->uuid, &uuid) ||
1322 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1323 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1324 dev_err(ctrl->device,
1325 "identifiers changed for nsid %d\n", ns->ns_id);
1326 ret = -ENODEV;
1327 }
1328
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001329out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001330 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001331 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001332}
1333
1334static char nvme_pr_type(enum pr_type type)
1335{
1336 switch (type) {
1337 case PR_WRITE_EXCLUSIVE:
1338 return 1;
1339 case PR_EXCLUSIVE_ACCESS:
1340 return 2;
1341 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1342 return 3;
1343 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1344 return 4;
1345 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1346 return 5;
1347 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1348 return 6;
1349 default:
1350 return 0;
1351 }
1352};
1353
1354static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1355 u64 key, u64 sa_key, u8 op)
1356{
1357 struct nvme_ns *ns = bdev->bd_disk->private_data;
1358 struct nvme_command c;
1359 u8 data[16] = { 0, };
1360
1361 put_unaligned_le64(key, &data[0]);
1362 put_unaligned_le64(sa_key, &data[8]);
1363
1364 memset(&c, 0, sizeof(c));
1365 c.common.opcode = op;
1366 c.common.nsid = cpu_to_le32(ns->ns_id);
1367 c.common.cdw10[0] = cpu_to_le32(cdw10);
1368
1369 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1370}
1371
1372static int nvme_pr_register(struct block_device *bdev, u64 old,
1373 u64 new, unsigned flags)
1374{
1375 u32 cdw10;
1376
1377 if (flags & ~PR_FL_IGNORE_KEY)
1378 return -EOPNOTSUPP;
1379
1380 cdw10 = old ? 2 : 0;
1381 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1382 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1383 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1384}
1385
1386static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1387 enum pr_type type, unsigned flags)
1388{
1389 u32 cdw10;
1390
1391 if (flags & ~PR_FL_IGNORE_KEY)
1392 return -EOPNOTSUPP;
1393
1394 cdw10 = nvme_pr_type(type) << 8;
1395 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1396 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1397}
1398
1399static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1400 enum pr_type type, bool abort)
1401{
1402 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1403 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1404}
1405
1406static int nvme_pr_clear(struct block_device *bdev, u64 key)
1407{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001408 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001409 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1410}
1411
1412static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1413{
1414 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1415 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1416}
1417
1418static const struct pr_ops nvme_pr_ops = {
1419 .pr_register = nvme_pr_register,
1420 .pr_reserve = nvme_pr_reserve,
1421 .pr_release = nvme_pr_release,
1422 .pr_preempt = nvme_pr_preempt,
1423 .pr_clear = nvme_pr_clear,
1424};
1425
Scott Bauera98e58e52017-02-03 12:50:32 -07001426#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001427int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1428 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001429{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001430 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001431 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001432
1433 memset(&cmd, 0, sizeof(cmd));
1434 if (send)
1435 cmd.common.opcode = nvme_admin_security_send;
1436 else
1437 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001438 cmd.common.nsid = 0;
1439 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1440 cmd.common.cdw10[1] = cpu_to_le32(len);
1441
1442 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1443 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1444}
1445EXPORT_SYMBOL_GPL(nvme_sec_submit);
1446#endif /* CONFIG_BLK_SED_OPAL */
1447
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001448static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001449 .owner = THIS_MODULE,
1450 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001451 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001452 .open = nvme_open,
1453 .release = nvme_release,
1454 .getgeo = nvme_getgeo,
1455 .revalidate_disk= nvme_revalidate_disk,
1456 .pr_ops = &nvme_pr_ops,
1457};
1458
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001459static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1460{
1461 unsigned long timeout =
1462 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1463 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1464 int ret;
1465
1466 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001467 if (csts == ~0)
1468 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001469 if ((csts & NVME_CSTS_RDY) == bit)
1470 break;
1471
1472 msleep(100);
1473 if (fatal_signal_pending(current))
1474 return -EINTR;
1475 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001476 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001477 "Device not ready; aborting %s\n", enabled ?
1478 "initialisation" : "reset");
1479 return -ENODEV;
1480 }
1481 }
1482
1483 return ret;
1484}
1485
1486/*
1487 * If the device has been passed off to us in an enabled state, just clear
1488 * the enabled bit. The spec says we should set the 'shutdown notification
1489 * bits', but doing so may cause the device to complete commands to the
1490 * admin queue ... and we don't know what memory that might be pointing at!
1491 */
1492int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1493{
1494 int ret;
1495
1496 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1497 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1498
1499 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1500 if (ret)
1501 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001502
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001503 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001504 msleep(NVME_QUIRK_DELAY_AMOUNT);
1505
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001506 return nvme_wait_ready(ctrl, cap, false);
1507}
Ming Lin576d55d2016-02-10 10:03:32 -08001508EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001509
1510int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1511{
1512 /*
1513 * Default to a 4K page size, with the intention to update this
1514 * path in the future to accomodate architectures with differing
1515 * kernel and IO page sizes.
1516 */
1517 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1518 int ret;
1519
1520 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001521 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001522 "Minimum device page size %u too large for host (%u)\n",
1523 1 << dev_page_min, 1 << page_shift);
1524 return -ENODEV;
1525 }
1526
1527 ctrl->page_size = 1 << page_shift;
1528
1529 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1530 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001531 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001532 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1533 ctrl->ctrl_config |= NVME_CC_ENABLE;
1534
1535 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1536 if (ret)
1537 return ret;
1538 return nvme_wait_ready(ctrl, cap, true);
1539}
Ming Lin576d55d2016-02-10 10:03:32 -08001540EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001541
1542int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1543{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001544 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001545 u32 csts;
1546 int ret;
1547
1548 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1549 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1550
1551 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1552 if (ret)
1553 return ret;
1554
1555 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1556 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1557 break;
1558
1559 msleep(100);
1560 if (fatal_signal_pending(current))
1561 return -EINTR;
1562 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001563 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001564 "Device shutdown incomplete; abort shutdown\n");
1565 return -ENODEV;
1566 }
1567 }
1568
1569 return ret;
1570}
Ming Lin576d55d2016-02-10 10:03:32 -08001571EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001572
Christoph Hellwigda358252016-03-02 18:07:11 +01001573static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1574 struct request_queue *q)
1575{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001576 bool vwc = false;
1577
Christoph Hellwigda358252016-03-02 18:07:11 +01001578 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001579 u32 max_segments =
1580 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1581
Christoph Hellwigda358252016-03-02 18:07:11 +01001582 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001583 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001584 }
Keith Busche6282ae2016-12-19 11:37:50 -05001585 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1586 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001587 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001588 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1589 vwc = true;
1590 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001591}
1592
Jon Derrickdbf86b32017-08-16 09:51:29 +02001593static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1594{
1595 __le64 ts;
1596 int ret;
1597
1598 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1599 return 0;
1600
1601 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1602 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1603 NULL);
1604 if (ret)
1605 dev_warn_once(ctrl->device,
1606 "could not set timestamp (%d)\n", ret);
1607 return ret;
1608}
1609
Keith Busch634b8322017-08-10 11:23:31 +02001610static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001611{
1612 /*
1613 * APST (Autonomous Power State Transition) lets us program a
1614 * table of power state transitions that the controller will
1615 * perform automatically. We configure it with a simple
1616 * heuristic: we are willing to spend at most 2% of the time
1617 * transitioning between power states. Therefore, when running
1618 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001619 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001620 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001621 * the requested maximum latency.
1622 *
1623 * We will not autonomously enter any non-operational state for
1624 * which the total latency exceeds ps_max_latency_us. Users
1625 * can set ps_max_latency_us to zero to turn off APST.
1626 */
1627
1628 unsigned apste;
1629 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001630 u64 max_lat_us = 0;
1631 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001632 int ret;
1633
1634 /*
1635 * If APST isn't supported or if we haven't been initialized yet,
1636 * then don't do anything.
1637 */
1638 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001639 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001640
1641 if (ctrl->npss > 31) {
1642 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001643 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001644 }
1645
1646 table = kzalloc(sizeof(*table), GFP_KERNEL);
1647 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001648 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001649
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001650 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001651 /* Turn off APST. */
1652 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001653 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001654 } else {
1655 __le64 target = cpu_to_le64(0);
1656 int state;
1657
1658 /*
1659 * Walk through all states from lowest- to highest-power.
1660 * According to the spec, lower-numbered states use more
1661 * power. NPSS, despite the name, is the index of the
1662 * lowest-power state, not the number of states.
1663 */
1664 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001665 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001666
1667 if (target)
1668 table->entries[state] = target;
1669
1670 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001671 * Don't allow transitions to the deepest state
1672 * if it's quirked off.
1673 */
1674 if (state == ctrl->npss &&
1675 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1676 continue;
1677
1678 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001679 * Is this state a useful non-operational state for
1680 * higher-power states to autonomously transition to?
1681 */
1682 if (!(ctrl->psd[state].flags &
1683 NVME_PS_FLAGS_NON_OP_STATE))
1684 continue;
1685
Kai-Heng Fengda875912017-06-07 15:25:42 +08001686 exit_latency_us =
1687 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1688 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001689 continue;
1690
Kai-Heng Fengda875912017-06-07 15:25:42 +08001691 total_latency_us =
1692 exit_latency_us +
1693 le32_to_cpu(ctrl->psd[state].entry_lat);
1694
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001695 /*
1696 * This state is good. Use it as the APST idle
1697 * target for higher power states.
1698 */
1699 transition_ms = total_latency_us + 19;
1700 do_div(transition_ms, 20);
1701 if (transition_ms > (1 << 24) - 1)
1702 transition_ms = (1 << 24) - 1;
1703
1704 target = cpu_to_le64((state << 3) |
1705 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001706
1707 if (max_ps == -1)
1708 max_ps = state;
1709
1710 if (total_latency_us > max_lat_us)
1711 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001712 }
1713
1714 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001715
1716 if (max_ps == -1) {
1717 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1718 } else {
1719 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1720 max_ps, max_lat_us, (int)sizeof(*table), table);
1721 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001722 }
1723
1724 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1725 table, sizeof(*table), NULL);
1726 if (ret)
1727 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1728
1729 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001730 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001731}
1732
1733static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1734{
1735 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1736 u64 latency;
1737
1738 switch (val) {
1739 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1740 case PM_QOS_LATENCY_ANY:
1741 latency = U64_MAX;
1742 break;
1743
1744 default:
1745 latency = val;
1746 }
1747
1748 if (ctrl->ps_max_latency_us != latency) {
1749 ctrl->ps_max_latency_us = latency;
1750 nvme_configure_apst(ctrl);
1751 }
1752}
1753
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001754struct nvme_core_quirk_entry {
1755 /*
1756 * NVMe model and firmware strings are padded with spaces. For
1757 * simplicity, strings in the quirk table are padded with NULLs
1758 * instead.
1759 */
1760 u16 vid;
1761 const char *mn;
1762 const char *fr;
1763 unsigned long quirks;
1764};
1765
1766static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001767 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001768 /*
1769 * This Toshiba device seems to die using any APST states. See:
1770 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1771 */
1772 .vid = 0x1179,
1773 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001774 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001775 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001776};
1777
1778/* match is null-terminated but idstr is space-padded. */
1779static bool string_matches(const char *idstr, const char *match, size_t len)
1780{
1781 size_t matchlen;
1782
1783 if (!match)
1784 return true;
1785
1786 matchlen = strlen(match);
1787 WARN_ON_ONCE(matchlen > len);
1788
1789 if (memcmp(idstr, match, matchlen))
1790 return false;
1791
1792 for (; matchlen < len; matchlen++)
1793 if (idstr[matchlen] != ' ')
1794 return false;
1795
1796 return true;
1797}
1798
1799static bool quirk_matches(const struct nvme_id_ctrl *id,
1800 const struct nvme_core_quirk_entry *q)
1801{
1802 return q->vid == le16_to_cpu(id->vid) &&
1803 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1804 string_matches(id->fr, q->fr, sizeof(id->fr));
1805}
1806
Christoph Hellwig180de0072017-06-26 12:39:02 +02001807static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1808{
1809 size_t nqnlen;
1810 int off;
1811
1812 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1813 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Javier Gonzáleza47619b2017-11-08 10:59:03 +01001814 strncpy(ctrl->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02001815 return;
1816 }
1817
1818 if (ctrl->vs >= NVME_VS(1, 2, 1))
1819 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1820
1821 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1822 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1823 "nqn.2014.08.org.nvmexpress:%4x%4x",
1824 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1825 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1826 off += sizeof(id->sn);
1827 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1828 off += sizeof(id->mn);
1829 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1830}
1831
Keith Buschc627c482017-11-07 10:28:31 -07001832static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
1833 size_t size)
1834{
1835 struct nvme_command c = { };
1836
1837 c.common.opcode = nvme_admin_get_log_page;
1838 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
1839 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
1840
1841 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
1842}
1843
Keith Busch84fef622017-11-07 10:28:32 -07001844static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
1845{
1846 int ret;
1847
1848 if (!ctrl->effects)
1849 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
1850
1851 if (!ctrl->effects)
1852 return 0;
1853
1854 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
1855 sizeof(*ctrl->effects));
1856 if (ret) {
1857 kfree(ctrl->effects);
1858 ctrl->effects = NULL;
1859 }
1860 return ret;
1861}
1862
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001863/*
1864 * Initialize the cached copies of the Identify data and various controller
1865 * register in our nvme_ctrl structure. This should be called as soon as
1866 * the admin queue is fully up and running.
1867 */
1868int nvme_init_identify(struct nvme_ctrl *ctrl)
1869{
1870 struct nvme_id_ctrl *id;
1871 u64 cap;
1872 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001873 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001874 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001875
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001876 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1877 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001878 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001879 return ret;
1880 }
1881
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001882 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1883 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001884 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001885 return ret;
1886 }
1887 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1888
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001889 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001890 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1891
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001892 ret = nvme_identify_ctrl(ctrl, &id);
1893 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001894 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001895 return -EIO;
1896 }
1897
Keith Busch84fef622017-11-07 10:28:32 -07001898 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
1899 ret = nvme_get_effects_log(ctrl);
1900 if (ret < 0)
1901 return ret;
1902 }
1903
Christoph Hellwig180de0072017-06-26 12:39:02 +02001904 nvme_init_subnqn(ctrl, id);
1905
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001906 if (!ctrl->identified) {
1907 /*
1908 * Check for quirks. Quirk can depend on firmware version,
1909 * so, in principle, the set of quirks present can change
1910 * across a reset. As a possible future enhancement, we
1911 * could re-scan for quirks every time we reinitialize
1912 * the device, but we'd have to make sure that the driver
1913 * behaves intelligently if the quirks change.
1914 */
1915
1916 int i;
1917
1918 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1919 if (quirk_matches(id, &core_quirks[i]))
1920 ctrl->quirks |= core_quirks[i].quirks;
1921 }
1922 }
1923
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001924 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001925 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 -07001926 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1927 }
1928
Scott Bauer8a9ae522017-02-17 13:59:40 +01001929 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001930 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001931 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001932 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001933 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001934 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001935 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1936 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1937 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1938 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001939 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001940 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001941 max_hw_sectors = UINT_MAX;
1942 ctrl->max_hw_sectors =
1943 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001944
Christoph Hellwigda358252016-03-02 18:07:11 +01001945 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001946 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001947 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001948
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001949 if (id->rtd3e) {
1950 /* us -> s */
1951 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1952
1953 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1954 shutdown_timeout, 60);
1955
1956 if (ctrl->shutdown_timeout != shutdown_timeout)
1957 dev_warn(ctrl->device,
1958 "Shutdown timeout set to %u seconds\n",
1959 ctrl->shutdown_timeout);
1960 } else
1961 ctrl->shutdown_timeout = shutdown_timeout;
1962
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001963 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001964 ctrl->apsta = id->apsta;
1965 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001966 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1967 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001968 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 -04001969 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001970 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001971 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001972 }
1973 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001974 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001975 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001976 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1977
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001978 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001979 ctrl->icdoff = le16_to_cpu(id->icdoff);
1980 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1981 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1982 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1983
1984 /*
1985 * In fabrics we need to verify the cntlid matches the
1986 * admin connect
1987 */
Keith Busch634b8322017-08-10 11:23:31 +02001988 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001989 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001990 goto out_free;
1991 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001992
1993 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001994 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001995 "keep-alive support is mandatory for fabrics\n");
1996 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001997 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001998 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001999 } else {
2000 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002001 ctrl->hmpre = le32_to_cpu(id->hmpre);
2002 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002003 ctrl->hmminds = le32_to_cpu(id->hmminds);
2004 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002005 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002006
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002007 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002008
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002009 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002010 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002011 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002012 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2013
Keith Busch634b8322017-08-10 11:23:31 +02002014 ret = nvme_configure_apst(ctrl);
2015 if (ret < 0)
2016 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002017
2018 ret = nvme_configure_timestamp(ctrl);
2019 if (ret < 0)
2020 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002021
2022 ret = nvme_configure_directives(ctrl);
2023 if (ret < 0)
2024 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002025
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002026 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002027
Keith Busch634b8322017-08-10 11:23:31 +02002028 return 0;
2029
2030out_free:
2031 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002032 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002033}
Ming Lin576d55d2016-02-10 10:03:32 -08002034EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002035
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002036static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002037{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002038 struct nvme_ctrl *ctrl =
2039 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002040
Christoph Hellwig999ada22017-10-18 17:09:31 +02002041 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002042 return -EWOULDBLOCK;
2043 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002044 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01002045}
2046
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002047static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2048{
2049 struct nvme_ns *ns;
2050 int ret;
2051
2052 mutex_lock(&ctrl->namespaces_mutex);
2053 if (list_empty(&ctrl->namespaces)) {
2054 ret = -ENOTTY;
2055 goto out_unlock;
2056 }
2057
2058 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2059 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002060 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002061 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2062 ret = -EINVAL;
2063 goto out_unlock;
2064 }
2065
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002066 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002067 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2068 kref_get(&ns->kref);
2069 mutex_unlock(&ctrl->namespaces_mutex);
2070
2071 ret = nvme_user_cmd(ctrl, ns, argp);
2072 nvme_put_ns(ns);
2073 return ret;
2074
2075out_unlock:
2076 mutex_unlock(&ctrl->namespaces_mutex);
2077 return ret;
2078}
2079
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002080static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2081 unsigned long arg)
2082{
2083 struct nvme_ctrl *ctrl = file->private_data;
2084 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002085
2086 switch (cmd) {
2087 case NVME_IOCTL_ADMIN_CMD:
2088 return nvme_user_cmd(ctrl, NULL, argp);
2089 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002090 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002091 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002092 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002093 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002094 case NVME_IOCTL_SUBSYS_RESET:
2095 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002096 case NVME_IOCTL_RESCAN:
2097 nvme_queue_scan(ctrl);
2098 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002099 default:
2100 return -ENOTTY;
2101 }
2102}
2103
2104static const struct file_operations nvme_dev_fops = {
2105 .owner = THIS_MODULE,
2106 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002107 .unlocked_ioctl = nvme_dev_ioctl,
2108 .compat_ioctl = nvme_dev_ioctl,
2109};
2110
2111static ssize_t nvme_sysfs_reset(struct device *dev,
2112 struct device_attribute *attr, const char *buf,
2113 size_t count)
2114{
2115 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2116 int ret;
2117
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002118 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002119 if (ret < 0)
2120 return ret;
2121 return count;
2122}
2123static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2124
Keith Busch9ec3bb22016-04-29 15:45:18 -06002125static ssize_t nvme_sysfs_rescan(struct device *dev,
2126 struct device_attribute *attr, const char *buf,
2127 size_t count)
2128{
2129 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2130
2131 nvme_queue_scan(ctrl);
2132 return count;
2133}
2134static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2135
Keith Busch118472a2016-02-18 09:57:48 -07002136static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2137 char *buf)
2138{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002139 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002140 struct nvme_ctrl *ctrl = ns->ctrl;
2141 int serial_len = sizeof(ctrl->serial);
2142 int model_len = sizeof(ctrl->model);
2143
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002144 if (!uuid_is_null(&ns->uuid))
2145 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2146
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002147 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2148 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002149
2150 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2151 return sprintf(buf, "eui.%8phN\n", ns->eui);
2152
Martin Wilck758f3732017-07-20 18:34:02 +02002153 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2154 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002155 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002156 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2157 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002158 model_len--;
2159
2160 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2161 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2162}
2163static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2164
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002165static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2166 char *buf)
2167{
2168 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2169 return sprintf(buf, "%pU\n", ns->nguid);
2170}
2171static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2172
Keith Busch2b9b6e82015-12-22 10:10:45 -07002173static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2174 char *buf)
2175{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002176 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002177
2178 /* For backward compatibility expose the NGUID to userspace if
2179 * we have no UUID set
2180 */
2181 if (uuid_is_null(&ns->uuid)) {
2182 printk_ratelimited(KERN_WARNING
2183 "No UUID available providing old NGUID\n");
2184 return sprintf(buf, "%pU\n", ns->nguid);
2185 }
2186 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002187}
2188static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2189
2190static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2191 char *buf)
2192{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002193 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Javier Gonzálezab083b12017-11-08 10:59:04 +01002194 return sprintf(buf, "%8ph\n", ns->eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002195}
2196static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2197
2198static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2199 char *buf)
2200{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002201 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002202 return sprintf(buf, "%d\n", ns->ns_id);
2203}
2204static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2205
2206static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002207 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002208 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002209 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002210 &dev_attr_eui.attr,
2211 &dev_attr_nsid.attr,
2212 NULL,
2213};
2214
Ming Lin1a353d82016-06-13 16:45:24 +02002215static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002216 struct attribute *a, int n)
2217{
2218 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002219 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002220
2221 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002222 if (uuid_is_null(&ns->uuid) ||
2223 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2224 return 0;
2225 }
2226 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002227 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002228 return 0;
2229 }
2230 if (a == &dev_attr_eui.attr) {
2231 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2232 return 0;
2233 }
2234 return a->mode;
2235}
2236
2237static const struct attribute_group nvme_ns_attr_group = {
2238 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002239 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002240};
2241
Ming Lin931e1c22016-02-26 13:24:19 -08002242#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002243static ssize_t field##_show(struct device *dev, \
2244 struct device_attribute *attr, char *buf) \
2245{ \
2246 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2247 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2248} \
2249static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2250
Ming Lin931e1c22016-02-26 13:24:19 -08002251#define nvme_show_int_function(field) \
2252static ssize_t field##_show(struct device *dev, \
2253 struct device_attribute *attr, char *buf) \
2254{ \
2255 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2256 return sprintf(buf, "%d\n", ctrl->field); \
2257} \
2258static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2259
2260nvme_show_str_function(model);
2261nvme_show_str_function(serial);
2262nvme_show_str_function(firmware_rev);
2263nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002264
Ming Lin1a353d82016-06-13 16:45:24 +02002265static ssize_t nvme_sysfs_delete(struct device *dev,
2266 struct device_attribute *attr, const char *buf,
2267 size_t count)
2268{
2269 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2270
2271 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002272 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002273 return count;
2274}
2275static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2276
2277static ssize_t nvme_sysfs_show_transport(struct device *dev,
2278 struct device_attribute *attr,
2279 char *buf)
2280{
2281 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2282
2283 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2284}
2285static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2286
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002287static ssize_t nvme_sysfs_show_state(struct device *dev,
2288 struct device_attribute *attr,
2289 char *buf)
2290{
2291 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2292 static const char *const state_name[] = {
2293 [NVME_CTRL_NEW] = "new",
2294 [NVME_CTRL_LIVE] = "live",
2295 [NVME_CTRL_RESETTING] = "resetting",
2296 [NVME_CTRL_RECONNECTING]= "reconnecting",
2297 [NVME_CTRL_DELETING] = "deleting",
2298 [NVME_CTRL_DEAD] = "dead",
2299 };
2300
2301 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2302 state_name[ctrl->state])
2303 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2304
2305 return sprintf(buf, "unknown state\n");
2306}
2307
2308static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2309
Ming Lin1a353d82016-06-13 16:45:24 +02002310static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2311 struct device_attribute *attr,
2312 char *buf)
2313{
2314 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2315
Christoph Hellwig180de0072017-06-26 12:39:02 +02002316 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002317}
2318static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2319
2320static ssize_t nvme_sysfs_show_address(struct device *dev,
2321 struct device_attribute *attr,
2322 char *buf)
2323{
2324 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2325
2326 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2327}
2328static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2329
Keith Busch779ff7562016-01-12 15:09:31 -07002330static struct attribute *nvme_dev_attrs[] = {
2331 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002332 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002333 &dev_attr_model.attr,
2334 &dev_attr_serial.attr,
2335 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002336 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002337 &dev_attr_delete_controller.attr,
2338 &dev_attr_transport.attr,
2339 &dev_attr_subsysnqn.attr,
2340 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002341 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002342 NULL
2343};
2344
Ming Lin1a353d82016-06-13 16:45:24 +02002345static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2346 struct attribute *a, int n)
2347{
2348 struct device *dev = container_of(kobj, struct device, kobj);
2349 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2350
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002351 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2352 return 0;
2353 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2354 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002355
2356 return a->mode;
2357}
2358
Keith Busch779ff7562016-01-12 15:09:31 -07002359static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002360 .attrs = nvme_dev_attrs,
2361 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002362};
2363
2364static const struct attribute_group *nvme_dev_attr_groups[] = {
2365 &nvme_dev_attrs_group,
2366 NULL,
2367};
2368
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002369static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2370{
2371 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2372 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2373
2374 return nsa->ns_id - nsb->ns_id;
2375}
2376
Keith Busch32f0c4a2016-07-13 11:45:02 -06002377static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002378{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002379 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002380
Keith Busch32f0c4a2016-07-13 11:45:02 -06002381 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002382 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002383 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002384 if (!kref_get_unless_zero(&ns->kref))
2385 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002386 ret = ns;
2387 break;
2388 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002389 if (ns->ns_id > nsid)
2390 break;
2391 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002392 mutex_unlock(&ctrl->namespaces_mutex);
2393 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002394}
2395
Jens Axboef5d11842017-06-27 12:03:06 -06002396static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2397{
2398 struct streams_directive_params s;
2399 int ret;
2400
2401 if (!ctrl->nr_streams)
2402 return 0;
2403
2404 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2405 if (ret)
2406 return ret;
2407
2408 ns->sws = le32_to_cpu(s.sws);
2409 ns->sgs = le16_to_cpu(s.sgs);
2410
2411 if (ns->sws) {
2412 unsigned int bs = 1 << ns->lba_shift;
2413
2414 blk_queue_io_min(ns->queue, bs * ns->sws);
2415 if (ns->sgs)
2416 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2417 }
2418
2419 return 0;
2420}
2421
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002422static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2423{
2424 struct nvme_ns *ns;
2425 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002426 struct nvme_id_ns *id;
2427 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002428 int node = dev_to_node(ctrl->dev);
2429
2430 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2431 if (!ns)
2432 return;
2433
Keith Busch075790e2016-02-24 09:15:53 -07002434 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2435 if (ns->instance < 0)
2436 goto out_free_ns;
2437
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002438 ns->queue = blk_mq_init_queue(ctrl->tagset);
2439 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002440 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002441 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2442 ns->queue->queuedata = ns;
2443 ns->ctrl = ctrl;
2444
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002445 kref_init(&ns->kref);
2446 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002447 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002448
2449 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002450 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002451 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002452
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002453 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002454
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002455 id = nvme_identify_ns(ctrl, nsid);
2456 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002457 goto out_free_queue;
2458
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002459 if (id->ncap == 0)
2460 goto out_free_id;
2461
2462 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2463
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002464 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2465 if (nvme_nvm_register(ns, disk_name, node)) {
2466 dev_warn(ctrl->device, "LightNVM init failure\n");
2467 goto out_free_id;
2468 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002469 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002470
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002471 disk = alloc_disk_node(0, node);
2472 if (!disk)
2473 goto out_free_id;
2474
2475 disk->fops = &nvme_fops;
2476 disk->private_data = ns;
2477 disk->queue = ns->queue;
2478 disk->flags = GENHD_FL_EXT_DEVT;
2479 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2480 ns->disk = disk;
2481
2482 __nvme_revalidate_disk(disk, id);
2483
Keith Busch32f0c4a2016-07-13 11:45:02 -06002484 mutex_lock(&ctrl->namespaces_mutex);
2485 list_add_tail(&ns->list, &ctrl->namespaces);
2486 mutex_unlock(&ctrl->namespaces_mutex);
2487
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002488 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002489
2490 kfree(id);
2491
Dan Williams0d52c7562016-06-15 19:44:20 -07002492 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002493 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2494 &nvme_ns_attr_group))
2495 pr_warn("%s: failed to create sysfs group for identification\n",
2496 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002497 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2498 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2499 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002500 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002501 out_free_id:
2502 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002503 out_free_queue:
2504 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002505 out_release_instance:
2506 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002507 out_free_ns:
2508 kfree(ns);
2509}
2510
2511static void nvme_ns_remove(struct nvme_ns *ns)
2512{
Keith Busch646017a2016-02-24 09:15:54 -07002513 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2514 return;
2515
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002516 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002517 if (blk_get_integrity(ns->disk))
2518 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002519 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2520 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002521 if (ns->ndev)
2522 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002523 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002524 blk_cleanup_queue(ns->queue);
2525 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002526
2527 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002528 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002529 mutex_unlock(&ns->ctrl->namespaces_mutex);
2530
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002531 nvme_put_ns(ns);
2532}
2533
Keith Busch540c8012015-10-22 15:45:06 -06002534static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2535{
2536 struct nvme_ns *ns;
2537
Keith Busch32f0c4a2016-07-13 11:45:02 -06002538 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002539 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002540 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002541 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002542 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002543 } else
2544 nvme_alloc_ns(ctrl, nsid);
2545}
2546
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302547static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2548 unsigned nsid)
2549{
2550 struct nvme_ns *ns, *next;
2551
2552 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2553 if (ns->ns_id > nsid)
2554 nvme_ns_remove(ns);
2555 }
2556}
2557
Keith Busch540c8012015-10-22 15:45:06 -06002558static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2559{
2560 struct nvme_ns *ns;
2561 __le32 *ns_list;
2562 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2563 int ret = 0;
2564
2565 ns_list = kzalloc(0x1000, GFP_KERNEL);
2566 if (!ns_list)
2567 return -ENOMEM;
2568
2569 for (i = 0; i < num_lists; i++) {
2570 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2571 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302572 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002573
2574 for (j = 0; j < min(nn, 1024U); j++) {
2575 nsid = le32_to_cpu(ns_list[j]);
2576 if (!nsid)
2577 goto out;
2578
2579 nvme_validate_ns(ctrl, nsid);
2580
2581 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002582 ns = nvme_find_get_ns(ctrl, prev);
2583 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002584 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002585 nvme_put_ns(ns);
2586 }
Keith Busch540c8012015-10-22 15:45:06 -06002587 }
2588 }
2589 nn -= j;
2590 }
2591 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302592 nvme_remove_invalid_namespaces(ctrl, prev);
2593 free:
Keith Busch540c8012015-10-22 15:45:06 -06002594 kfree(ns_list);
2595 return ret;
2596}
2597
Christoph Hellwig5955be22016-04-26 13:51:59 +02002598static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002599{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002600 unsigned i;
2601
Keith Busch540c8012015-10-22 15:45:06 -06002602 for (i = 1; i <= nn; i++)
2603 nvme_validate_ns(ctrl, i);
2604
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302605 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002606}
2607
Christoph Hellwig5955be22016-04-26 13:51:59 +02002608static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002609{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002610 struct nvme_ctrl *ctrl =
2611 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002612 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002613 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002614
Christoph Hellwig5955be22016-04-26 13:51:59 +02002615 if (ctrl->state != NVME_CTRL_LIVE)
2616 return;
2617
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002618 if (nvme_identify_ctrl(ctrl, &id))
2619 return;
Keith Busch540c8012015-10-22 15:45:06 -06002620
2621 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002622 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002623 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2624 if (!nvme_scan_ns_list(ctrl, nn))
2625 goto done;
2626 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002627 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002628 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002629 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002630 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002631 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002632 kfree(id);
2633}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002634
2635void nvme_queue_scan(struct nvme_ctrl *ctrl)
2636{
2637 /*
2638 * Do not queue new scan work when a controller is reset during
2639 * removal.
2640 */
2641 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002642 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002643}
2644EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002645
Keith Busch32f0c4a2016-07-13 11:45:02 -06002646/*
2647 * This function iterates the namespace list unlocked to allow recovery from
2648 * controller failure. It is up to the caller to ensure the namespace list is
2649 * not modified by scan work while this function is executing.
2650 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002651void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2652{
2653 struct nvme_ns *ns, *next;
2654
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002655 /*
2656 * The dead states indicates the controller was not gracefully
2657 * disconnected. In that case, we won't be able to flush any data while
2658 * removing the namespaces' disks; fail all the queues now to avoid
2659 * potentially having to clean up the failed sync later.
2660 */
2661 if (ctrl->state == NVME_CTRL_DEAD)
2662 nvme_kill_queues(ctrl);
2663
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002664 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2665 nvme_ns_remove(ns);
2666}
Ming Lin576d55d2016-02-10 10:03:32 -08002667EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002668
Keith Busche3d78742017-11-07 15:13:14 -07002669static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
2670{
2671 char *envp[2] = { NULL, NULL };
2672 u32 aen_result = ctrl->aen_result;
2673
2674 ctrl->aen_result = 0;
2675 if (!aen_result)
2676 return;
2677
2678 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
2679 if (!envp[0])
2680 return;
2681 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
2682 kfree(envp[0]);
2683}
2684
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002685static void nvme_async_event_work(struct work_struct *work)
2686{
2687 struct nvme_ctrl *ctrl =
2688 container_of(work, struct nvme_ctrl, async_event_work);
2689
Keith Busche3d78742017-11-07 15:13:14 -07002690 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07002691 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002692}
2693
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302694static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2695{
2696
2697 u32 csts;
2698
2699 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2700 return false;
2701
2702 if (csts == ~0)
2703 return false;
2704
2705 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2706}
2707
2708static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2709{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302710 struct nvme_fw_slot_info_log *log;
2711
2712 log = kmalloc(sizeof(*log), GFP_KERNEL);
2713 if (!log)
2714 return;
2715
Keith Buschc627c482017-11-07 10:28:31 -07002716 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302717 dev_warn(ctrl->device,
2718 "Get FW SLOT INFO log error\n");
2719 kfree(log);
2720}
2721
2722static void nvme_fw_act_work(struct work_struct *work)
2723{
2724 struct nvme_ctrl *ctrl = container_of(work,
2725 struct nvme_ctrl, fw_act_work);
2726 unsigned long fw_act_timeout;
2727
2728 if (ctrl->mtfa)
2729 fw_act_timeout = jiffies +
2730 msecs_to_jiffies(ctrl->mtfa * 100);
2731 else
2732 fw_act_timeout = jiffies +
2733 msecs_to_jiffies(admin_timeout * 1000);
2734
2735 nvme_stop_queues(ctrl);
2736 while (nvme_ctrl_pp_status(ctrl)) {
2737 if (time_after(jiffies, fw_act_timeout)) {
2738 dev_warn(ctrl->device,
2739 "Fw activation timeout, reset controller\n");
2740 nvme_reset_ctrl(ctrl);
2741 break;
2742 }
2743 msleep(100);
2744 }
2745
2746 if (ctrl->state != NVME_CTRL_LIVE)
2747 return;
2748
2749 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09002750 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302751 nvme_get_fw_slot_info(ctrl);
2752}
2753
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002754void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2755 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002756{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002757 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002758
Keith Buschad22c352017-11-07 15:13:12 -07002759 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002760 return;
2761
Keith Busche3d78742017-11-07 15:13:14 -07002762 switch (result & 0x7) {
2763 case NVME_AER_ERROR:
2764 case NVME_AER_SMART:
2765 case NVME_AER_CSS:
2766 case NVME_AER_VS:
2767 ctrl->aen_result = result;
2768 break;
2769 default:
2770 break;
2771 }
2772
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002773 switch (result & 0xff07) {
2774 case NVME_AER_NOTICE_NS_CHANGED:
2775 dev_info(ctrl->device, "rescanning\n");
2776 nvme_queue_scan(ctrl);
2777 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302778 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002779 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302780 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002781 default:
2782 dev_warn(ctrl->device, "async event result %08x\n", result);
2783 }
Keith Buschad22c352017-11-07 15:13:12 -07002784 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002785}
2786EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2787
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002788void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002789{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002790 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002791 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002792 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302793 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002794}
2795EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002796
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002797void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2798{
2799 if (ctrl->kato)
2800 nvme_start_keep_alive(ctrl);
2801
2802 if (ctrl->queue_count > 1) {
2803 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07002804 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002805 nvme_start_queues(ctrl);
2806 }
2807}
2808EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2809
2810void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2811{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002812 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002813}
Ming Lin576d55d2016-02-10 10:03:32 -08002814EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002815
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002816static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002817{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002818 struct nvme_ctrl *ctrl =
2819 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002820
Christoph Hellwig9843f682017-10-18 13:10:01 +02002821 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002822 ida_destroy(&ctrl->ns_ida);
Keith Busch84fef622017-11-07 10:28:32 -07002823 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002824
2825 ctrl->ops->free_ctrl(ctrl);
2826}
2827
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002828/*
2829 * Initialize a NVMe controller structures. This needs to be called during
2830 * earliest initialization so that we have the initialized structured around
2831 * during probing.
2832 */
2833int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2834 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2835{
2836 int ret;
2837
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002838 ctrl->state = NVME_CTRL_NEW;
2839 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002840 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002841 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002842 ctrl->dev = dev;
2843 ctrl->ops = ops;
2844 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002845 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002846 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302847 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002848 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002849
Christoph Hellwig9843f682017-10-18 13:10:01 +02002850 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2851 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002852 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002853 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002854
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002855 device_initialize(&ctrl->ctrl_device);
2856 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002857 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002858 ctrl->device->class = nvme_class;
2859 ctrl->device->parent = ctrl->dev;
2860 ctrl->device->groups = nvme_dev_attr_groups;
2861 ctrl->device->release = nvme_free_ctrl;
2862 dev_set_drvdata(ctrl->device, ctrl);
2863 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
2864 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002865 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002866
2867 cdev_init(&ctrl->cdev, &nvme_dev_fops);
2868 ctrl->cdev.owner = ops->module;
2869 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002870 if (ret)
2871 goto out_free_name;
2872
Keith Busch075790e2016-02-24 09:15:53 -07002873 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002874
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002875 /*
2876 * Initialize latency tolerance controls. The sysfs files won't
2877 * be visible to userspace unless the device actually supports APST.
2878 */
2879 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2880 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2881 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2882
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002883 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002884out_free_name:
2885 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002886out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02002887 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002888out:
2889 return ret;
2890}
Ming Lin576d55d2016-02-10 10:03:32 -08002891EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002892
Keith Busch69d9a992016-02-24 09:15:56 -07002893/**
2894 * nvme_kill_queues(): Ends all namespace queues
2895 * @ctrl: the dead controller that needs to end
2896 *
2897 * Call this function when the driver determines it is unable to get the
2898 * controller in a state capable of servicing IO.
2899 */
2900void nvme_kill_queues(struct nvme_ctrl *ctrl)
2901{
2902 struct nvme_ns *ns;
2903
Keith Busch32f0c4a2016-07-13 11:45:02 -06002904 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002905
Ming Lei443bd902017-06-19 10:21:08 +08002906 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002907 if (ctrl->admin_q)
2908 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002909
Keith Busch32f0c4a2016-07-13 11:45:02 -06002910 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002911 /*
2912 * Revalidating a dead namespace sets capacity to 0. This will
2913 * end buffered writers dirtying pages that can't be synced.
2914 */
Keith Buschf33447b2017-02-10 18:15:51 -05002915 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2916 continue;
2917 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002918 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002919
Ming Lei443bd902017-06-19 10:21:08 +08002920 /* Forcibly unquiesce queues to avoid blocking dispatch */
2921 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002922 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002923 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002924}
Linus Torvalds237045f2016-03-18 17:13:31 -07002925EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002926
Keith Busch302ad8c2017-03-01 14:22:12 -05002927void nvme_unfreeze(struct nvme_ctrl *ctrl)
2928{
2929 struct nvme_ns *ns;
2930
2931 mutex_lock(&ctrl->namespaces_mutex);
2932 list_for_each_entry(ns, &ctrl->namespaces, list)
2933 blk_mq_unfreeze_queue(ns->queue);
2934 mutex_unlock(&ctrl->namespaces_mutex);
2935}
2936EXPORT_SYMBOL_GPL(nvme_unfreeze);
2937
2938void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2939{
2940 struct nvme_ns *ns;
2941
2942 mutex_lock(&ctrl->namespaces_mutex);
2943 list_for_each_entry(ns, &ctrl->namespaces, list) {
2944 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2945 if (timeout <= 0)
2946 break;
2947 }
2948 mutex_unlock(&ctrl->namespaces_mutex);
2949}
2950EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2951
2952void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2953{
2954 struct nvme_ns *ns;
2955
2956 mutex_lock(&ctrl->namespaces_mutex);
2957 list_for_each_entry(ns, &ctrl->namespaces, list)
2958 blk_mq_freeze_queue_wait(ns->queue);
2959 mutex_unlock(&ctrl->namespaces_mutex);
2960}
2961EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2962
2963void nvme_start_freeze(struct nvme_ctrl *ctrl)
2964{
2965 struct nvme_ns *ns;
2966
2967 mutex_lock(&ctrl->namespaces_mutex);
2968 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002969 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002970 mutex_unlock(&ctrl->namespaces_mutex);
2971}
2972EXPORT_SYMBOL_GPL(nvme_start_freeze);
2973
Keith Busch25646262016-01-04 09:10:57 -07002974void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002975{
2976 struct nvme_ns *ns;
2977
Keith Busch32f0c4a2016-07-13 11:45:02 -06002978 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002979 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002980 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002981 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002982}
Ming Lin576d55d2016-02-10 10:03:32 -08002983EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002984
Keith Busch25646262016-01-04 09:10:57 -07002985void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002986{
2987 struct nvme_ns *ns;
2988
Keith Busch32f0c4a2016-07-13 11:45:02 -06002989 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002990 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002991 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002992 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002993}
Ming Lin576d55d2016-02-10 10:03:32 -08002994EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002995
Sagi Grimberg31b84462017-10-11 12:53:07 +03002996int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2997{
2998 if (!ctrl->ops->reinit_request)
2999 return 0;
3000
3001 return blk_mq_tagset_iter(set, set->driver_data,
3002 ctrl->ops->reinit_request);
3003}
3004EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3005
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003006int __init nvme_core_init(void)
3007{
3008 int result;
3009
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003010 nvme_wq = alloc_workqueue("nvme-wq",
3011 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3012 if (!nvme_wq)
3013 return -ENOMEM;
3014
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003015 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003016 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003017 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003018
3019 nvme_class = class_create(THIS_MODULE, "nvme");
3020 if (IS_ERR(nvme_class)) {
3021 result = PTR_ERR(nvme_class);
3022 goto unregister_chrdev;
3023 }
3024
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003025 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003026
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003027unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003028 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003029destroy_wq:
3030 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003031 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003032}
3033
3034void nvme_core_exit(void)
3035{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003036 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003037 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003038 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003039}
Ming Lin576d55d2016-02-10 10:03:32 -08003040
3041MODULE_LICENSE("GPL");
3042MODULE_VERSION("1.0");
3043module_init(nvme_core_init);
3044module_exit(nvme_core_exit);