blob: b2daeafbeadd7252dd9055ef9d5d4909faabb5bc [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
Ming Linba0ba7d2016-02-10 10:03:30 -080037unsigned char admin_timeout = 60;
38module_param(admin_timeout, byte, 0644);
39MODULE_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
42unsigned char nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
44MODULE_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
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010055static int nvme_char_major;
56module_param(nvme_char_major, int, 0);
57
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080058static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059module_param(default_ps_max_latency_us, ulong, 0644);
60MODULE_PARM_DESC(default_ps_max_latency_us,
61 "max power saving latency for new devices; use PM QOS to change per device");
62
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070063static bool force_apst;
64module_param(force_apst, bool, 0644);
65MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
66
Jens Axboef5d11842017-06-27 12:03:06 -060067static bool streams;
68module_param(streams, bool, 0644);
69MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
70
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020071struct workqueue_struct *nvme_wq;
72EXPORT_SYMBOL_GPL(nvme_wq);
73
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010074static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080075static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010076
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010077static struct class *nvme_class;
78
Arnav Dawnb6dccf72017-07-12 16:10:40 +053079static __le32 nvme_get_log_dw10(u8 lid, size_t size)
80{
81 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
82}
83
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020084int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
85{
86 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
87 return -EBUSY;
88 if (!queue_work(nvme_wq, &ctrl->reset_work))
89 return -EBUSY;
90 return 0;
91}
92EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
93
94static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
95{
96 int ret;
97
98 ret = nvme_reset_ctrl(ctrl);
99 if (!ret)
100 flush_work(&ctrl->reset_work);
101 return ret;
102}
103
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200104static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200105{
106 switch (nvme_req(req)->status & 0x7ff) {
107 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200108 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200109 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200110 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200111 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200112 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200113 case NVME_SC_WRITE_FAULT:
114 case NVME_SC_READ_ERROR:
115 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200116 return BLK_STS_MEDIUM;
117 default:
118 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200119 }
120}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200121
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200122static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200123{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200124 if (blk_noretry_request(req))
125 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200126 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200127 return false;
128 if (jiffies - req->start_time >= req->timeout)
129 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200130 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200131 return false;
132 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200133}
134
135void nvme_complete_rq(struct request *req)
136{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200137 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
138 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300139 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200140 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200141 }
142
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200143 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200144}
145EXPORT_SYMBOL_GPL(nvme_complete_rq);
146
Ming Linc55a2fd2016-05-18 14:05:02 -0700147void nvme_cancel_request(struct request *req, void *data, bool reserved)
148{
149 int status;
150
151 if (!blk_mq_request_started(req))
152 return;
153
154 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
155 "Cancelling I/O %d", req->tag);
156
157 status = NVME_SC_ABORT_REQ;
158 if (blk_queue_dying(req->q))
159 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200160 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200161 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200162
Ming Linc55a2fd2016-05-18 14:05:02 -0700163}
164EXPORT_SYMBOL_GPL(nvme_cancel_request);
165
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200166bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
167 enum nvme_ctrl_state new_state)
168{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300169 enum nvme_ctrl_state old_state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200170 bool changed = false;
171
172 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300173
174 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200175 switch (new_state) {
176 case NVME_CTRL_LIVE:
177 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200178 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200179 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900180 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200181 changed = true;
182 /* FALLTHRU */
183 default:
184 break;
185 }
186 break;
187 case NVME_CTRL_RESETTING:
188 switch (old_state) {
189 case NVME_CTRL_NEW:
190 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900191 changed = true;
192 /* FALLTHRU */
193 default:
194 break;
195 }
196 break;
197 case NVME_CTRL_RECONNECTING:
198 switch (old_state) {
199 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200200 changed = true;
201 /* FALLTHRU */
202 default:
203 break;
204 }
205 break;
206 case NVME_CTRL_DELETING:
207 switch (old_state) {
208 case NVME_CTRL_LIVE:
209 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900210 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200211 changed = true;
212 /* FALLTHRU */
213 default:
214 break;
215 }
216 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600217 case NVME_CTRL_DEAD:
218 switch (old_state) {
219 case NVME_CTRL_DELETING:
220 changed = true;
221 /* FALLTHRU */
222 default:
223 break;
224 }
225 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200226 default:
227 break;
228 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200229
230 if (changed)
231 ctrl->state = new_state;
232
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300233 spin_unlock_irq(&ctrl->lock);
234
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200235 return changed;
236}
237EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
238
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100239static void nvme_free_ns(struct kref *kref)
240{
241 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
242
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200243 if (ns->ndev)
244 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100245
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200246 if (ns->disk) {
247 spin_lock(&dev_list_lock);
248 ns->disk->private_data = NULL;
249 spin_unlock(&dev_list_lock);
250 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100251
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100252 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700253 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
254 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100255 kfree(ns);
256}
257
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100258static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100259{
260 kref_put(&ns->kref, nvme_free_ns);
261}
262
263static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
264{
265 struct nvme_ns *ns;
266
267 spin_lock(&dev_list_lock);
268 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800269 if (ns) {
270 if (!kref_get_unless_zero(&ns->kref))
271 goto fail;
272 if (!try_module_get(ns->ctrl->ops->module))
273 goto fail_put_ns;
274 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100275 spin_unlock(&dev_list_lock);
276
277 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800278
279fail_put_ns:
280 kref_put(&ns->kref, nvme_free_ns);
281fail:
282 spin_unlock(&dev_list_lock);
283 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100284}
285
Christoph Hellwig41609822015-11-20 09:00:02 +0100286struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200287 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100288{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100289 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100290 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100291
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200292 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100293 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200294 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100295 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200296 qid ? qid - 1 : 0);
297 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100298 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100299 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100300
Christoph Hellwig21d34712015-11-26 09:08:36 +0100301 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800302 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100303
Christoph Hellwig41609822015-11-20 09:00:02 +0100304 return req;
305}
Ming Lin576d55d2016-02-10 10:03:32 -0800306EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100307
Jens Axboef5d11842017-06-27 12:03:06 -0600308static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
309{
310 struct nvme_command c;
311
312 memset(&c, 0, sizeof(c));
313
314 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530315 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600316 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
317 c.directive.dtype = NVME_DIR_IDENTIFY;
318 c.directive.tdtype = NVME_DIR_STREAMS;
319 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
320
321 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
322}
323
324static int nvme_disable_streams(struct nvme_ctrl *ctrl)
325{
326 return nvme_toggle_streams(ctrl, false);
327}
328
329static int nvme_enable_streams(struct nvme_ctrl *ctrl)
330{
331 return nvme_toggle_streams(ctrl, true);
332}
333
334static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
335 struct streams_directive_params *s, u32 nsid)
336{
337 struct nvme_command c;
338
339 memset(&c, 0, sizeof(c));
340 memset(s, 0, sizeof(*s));
341
342 c.directive.opcode = nvme_admin_directive_recv;
343 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700344 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600345 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
346 c.directive.dtype = NVME_DIR_STREAMS;
347
348 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
349}
350
351static int nvme_configure_directives(struct nvme_ctrl *ctrl)
352{
353 struct streams_directive_params s;
354 int ret;
355
356 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
357 return 0;
358 if (!streams)
359 return 0;
360
361 ret = nvme_enable_streams(ctrl);
362 if (ret)
363 return ret;
364
Arnav Dawn62346ea2017-07-12 16:11:53 +0530365 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600366 if (ret)
367 return ret;
368
369 ctrl->nssa = le16_to_cpu(s.nssa);
370 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
371 dev_info(ctrl->device, "too few streams (%u) available\n",
372 ctrl->nssa);
373 nvme_disable_streams(ctrl);
374 return 0;
375 }
376
377 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
378 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
379 return 0;
380}
381
382/*
383 * Check if 'req' has a write hint associated with it. If it does, assign
384 * a valid namespace stream to the write.
385 */
386static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
387 struct request *req, u16 *control,
388 u32 *dsmgmt)
389{
390 enum rw_hint streamid = req->write_hint;
391
392 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
393 streamid = 0;
394 else {
395 streamid--;
396 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
397 return;
398
399 *control |= NVME_RW_DTYPE_STREAMS;
400 *dsmgmt |= streamid << 16;
401 }
402
403 if (streamid < ARRAY_SIZE(req->q->write_hints))
404 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
405}
406
Ming Lin8093f7c2016-04-12 13:10:14 -0600407static inline void nvme_setup_flush(struct nvme_ns *ns,
408 struct nvme_command *cmnd)
409{
410 memset(cmnd, 0, sizeof(*cmnd));
411 cmnd->common.opcode = nvme_cmd_flush;
412 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
413}
414
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200415static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600416 struct nvme_command *cmnd)
417{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100418 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600419 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100420 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600421
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100422 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600423 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200424 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600425
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100426 __rq_for_each_bio(bio, req) {
427 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
428 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
429
430 range[n].cattr = cpu_to_le32(0);
431 range[n].nlb = cpu_to_le32(nlb);
432 range[n].slba = cpu_to_le64(slba);
433 n++;
434 }
435
436 if (WARN_ON_ONCE(n != segments)) {
437 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200438 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100439 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600440
441 memset(cmnd, 0, sizeof(*cmnd));
442 cmnd->dsm.opcode = nvme_cmd_dsm;
443 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200444 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600445 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
446
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700447 req->special_vec.bv_page = virt_to_page(range);
448 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100449 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700450 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600451
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200452 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600453}
Ming Lin8093f7c2016-04-12 13:10:14 -0600454
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200455static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
456 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600457{
Jens Axboef5d11842017-06-27 12:03:06 -0600458 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600459 u16 control = 0;
460 u32 dsmgmt = 0;
461
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200462 /*
463 * If formated with metadata, require the block layer provide a buffer
464 * unless this namespace is formated such that the metadata can be
465 * stripped/generated by the controller with PRACT=1.
466 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300467 if (ns && ns->ms &&
468 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200469 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
470 return BLK_STS_NOTSUPP;
471
Ming Lin8093f7c2016-04-12 13:10:14 -0600472 if (req->cmd_flags & REQ_FUA)
473 control |= NVME_RW_FUA;
474 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
475 control |= NVME_RW_LR;
476
477 if (req->cmd_flags & REQ_RAHEAD)
478 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
479
480 memset(cmnd, 0, sizeof(*cmnd));
481 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600482 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
483 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
484 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
485
Jens Axboef5d11842017-06-27 12:03:06 -0600486 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
487 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
488
Ming Lin8093f7c2016-04-12 13:10:14 -0600489 if (ns->ms) {
490 switch (ns->pi_type) {
491 case NVME_NS_DPS_PI_TYPE3:
492 control |= NVME_RW_PRINFO_PRCHK_GUARD;
493 break;
494 case NVME_NS_DPS_PI_TYPE1:
495 case NVME_NS_DPS_PI_TYPE2:
496 control |= NVME_RW_PRINFO_PRCHK_GUARD |
497 NVME_RW_PRINFO_PRCHK_REF;
498 cmnd->rw.reftag = cpu_to_le32(
499 nvme_block_nr(ns, blk_rq_pos(req)));
500 break;
501 }
502 if (!blk_integrity_rq(req))
503 control |= NVME_RW_PRINFO_PRACT;
504 }
505
506 cmnd->rw.control = cpu_to_le16(control);
507 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200508 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600509}
510
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200511blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600512 struct nvme_command *cmd)
513{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200514 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600515
Christoph Hellwig987f6992017-04-05 19:18:08 +0200516 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200517 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200518 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200519 req->rq_flags |= RQF_DONTPREP;
520 }
521
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100522 switch (req_op(req)) {
523 case REQ_OP_DRV_IN:
524 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800525 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100526 break;
527 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600528 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100529 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200530 case REQ_OP_WRITE_ZEROES:
531 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100532 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600533 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100534 break;
535 case REQ_OP_READ:
536 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200537 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100538 break;
539 default:
540 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200541 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600543
James Smart721b3912016-10-21 23:33:34 +0300544 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600545 return ret;
546}
547EXPORT_SYMBOL_GPL(nvme_setup_cmd);
548
Christoph Hellwig41609822015-11-20 09:00:02 +0100549/*
550 * Returns 0 on success. If the result is negative, it's a Linux error code;
551 * if the result is positive, it's an NVM Express status code
552 */
553int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800554 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200555 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100556{
557 struct request *req;
558 int ret;
559
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200560 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100561 if (IS_ERR(req))
562 return PTR_ERR(req);
563
564 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
565
Christoph Hellwig21d34712015-11-26 09:08:36 +0100566 if (buffer && bufflen) {
567 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
568 if (ret)
569 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100570 }
571
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200572 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800573 if (result)
574 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200575 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
576 ret = -EINTR;
577 else
578 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100579 out:
580 blk_mq_free_request(req);
581 return ret;
582}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200583EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100584
585int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
586 void *buffer, unsigned bufflen)
587{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200588 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
589 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100590}
Ming Lin576d55d2016-02-10 10:03:32 -0800591EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100592
Keith Busch0b7f1f22015-10-23 09:47:28 -0600593int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
594 void __user *ubuffer, unsigned bufflen,
595 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
596 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100597{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200598 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600599 struct nvme_ns *ns = q->queuedata;
600 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100601 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600602 struct bio *bio = NULL;
603 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100604 int ret;
605
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200606 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100607 if (IS_ERR(req))
608 return PTR_ERR(req);
609
610 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
611
612 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100613 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
614 GFP_KERNEL);
615 if (ret)
616 goto out;
617 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100618
Keith Busch0b7f1f22015-10-23 09:47:28 -0600619 if (!disk)
620 goto submit;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200621 bio->bi_disk = disk;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600622
Keith Busche9fc63d2016-02-24 09:15:58 -0700623 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600624 struct bio_integrity_payload *bip;
625
626 meta = kmalloc(meta_len, GFP_KERNEL);
627 if (!meta) {
628 ret = -ENOMEM;
629 goto out_unmap;
630 }
631
632 if (write) {
633 if (copy_from_user(meta, meta_buffer,
634 meta_len)) {
635 ret = -EFAULT;
636 goto out_free_meta;
637 }
638 }
639
640 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700641 if (IS_ERR(bip)) {
642 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600643 goto out_free_meta;
644 }
645
646 bip->bip_iter.bi_size = meta_len;
647 bip->bip_iter.bi_sector = meta_seed;
648
649 ret = bio_integrity_add_page(bio, virt_to_page(meta),
650 meta_len, offset_in_page(meta));
651 if (ret != meta_len) {
652 ret = -ENOMEM;
653 goto out_free_meta;
654 }
655 }
656 }
657 submit:
658 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200659 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
660 ret = -EINTR;
661 else
662 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100663 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800664 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600665 if (meta && !ret && !write) {
666 if (copy_to_user(meta_buffer, meta, meta_len))
667 ret = -EFAULT;
668 }
669 out_free_meta:
670 kfree(meta);
671 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200672 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600673 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100674 out:
675 blk_mq_free_request(req);
676 return ret;
677}
678
Keith Busch0b7f1f22015-10-23 09:47:28 -0600679int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
680 void __user *ubuffer, unsigned bufflen, u32 *result,
681 unsigned timeout)
682{
683 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
684 result, timeout);
685}
686
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200687static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200688{
689 struct nvme_ctrl *ctrl = rq->end_io_data;
690
691 blk_mq_free_request(rq);
692
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200693 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200694 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200695 "failed nvme_keep_alive_end_io error=%d\n",
696 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200697 return;
698 }
699
700 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
701}
702
703static int nvme_keep_alive(struct nvme_ctrl *ctrl)
704{
705 struct nvme_command c;
706 struct request *rq;
707
708 memset(&c, 0, sizeof(c));
709 c.common.opcode = nvme_admin_keep_alive;
710
711 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
712 NVME_QID_ANY);
713 if (IS_ERR(rq))
714 return PTR_ERR(rq);
715
716 rq->timeout = ctrl->kato * HZ;
717 rq->end_io_data = ctrl;
718
719 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
720
721 return 0;
722}
723
724static void nvme_keep_alive_work(struct work_struct *work)
725{
726 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
727 struct nvme_ctrl, ka_work);
728
729 if (nvme_keep_alive(ctrl)) {
730 /* allocation failure, reset the controller */
731 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200732 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200733 return;
734 }
735}
736
737void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
738{
739 if (unlikely(ctrl->kato == 0))
740 return;
741
742 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
743 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
744}
745EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
746
747void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
748{
749 if (unlikely(ctrl->kato == 0))
750 return;
751
752 cancel_delayed_work_sync(&ctrl->ka_work);
753}
754EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
755
Keith Busch3f7f25a92017-06-20 15:09:56 -0400756static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100757{
758 struct nvme_command c = { };
759 int error;
760
761 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
762 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200763 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100764
765 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
766 if (!*id)
767 return -ENOMEM;
768
769 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
770 sizeof(struct nvme_id_ctrl));
771 if (error)
772 kfree(*id);
773 return error;
774}
775
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200776static int nvme_identify_ns_descs(struct nvme_ns *ns, unsigned nsid)
777{
778 struct nvme_command c = { };
779 int status;
780 void *data;
781 int pos;
782 int len;
783
784 c.identify.opcode = nvme_admin_identify;
785 c.identify.nsid = cpu_to_le32(nsid);
786 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
787
788 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
789 if (!data)
790 return -ENOMEM;
791
792 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, data,
793 NVME_IDENTIFY_DATA_SIZE);
794 if (status)
795 goto free_data;
796
797 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
798 struct nvme_ns_id_desc *cur = data + pos;
799
800 if (cur->nidl == 0)
801 break;
802
803 switch (cur->nidt) {
804 case NVME_NIDT_EUI64:
805 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
806 dev_warn(ns->ctrl->device,
807 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
808 cur->nidl);
809 goto free_data;
810 }
811 len = NVME_NIDT_EUI64_LEN;
812 memcpy(ns->eui, data + pos + sizeof(*cur), len);
813 break;
814 case NVME_NIDT_NGUID:
815 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
816 dev_warn(ns->ctrl->device,
817 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
818 cur->nidl);
819 goto free_data;
820 }
821 len = NVME_NIDT_NGUID_LEN;
822 memcpy(ns->nguid, data + pos + sizeof(*cur), len);
823 break;
824 case NVME_NIDT_UUID:
825 if (cur->nidl != NVME_NIDT_UUID_LEN) {
826 dev_warn(ns->ctrl->device,
827 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
828 cur->nidl);
829 goto free_data;
830 }
831 len = NVME_NIDT_UUID_LEN;
832 uuid_copy(&ns->uuid, data + pos + sizeof(*cur));
833 break;
834 default:
835 /* Skip unnkown types */
836 len = cur->nidl;
837 break;
838 }
839
840 len += sizeof(*cur);
841 }
842free_data:
843 kfree(data);
844 return status;
845}
846
Keith Busch540c8012015-10-22 15:45:06 -0600847static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
848{
849 struct nvme_command c = { };
850
851 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200852 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600853 c.identify.nsid = cpu_to_le32(nsid);
854 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
855}
856
Keith Busch3f7f25a92017-06-20 15:09:56 -0400857static int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100858 struct nvme_id_ns **id)
859{
860 struct nvme_command c = { };
861 int error;
862
863 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200864 c.identify.opcode = nvme_admin_identify;
865 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200866 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100867
868 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
869 if (!*id)
870 return -ENOMEM;
871
872 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
873 sizeof(struct nvme_id_ns));
874 if (error)
875 kfree(*id);
876 return error;
877}
878
Keith Busch3f7f25a92017-06-20 15:09:56 -0400879static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700880 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100881{
882 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800883 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100884 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100885
886 memset(&c, 0, sizeof(c));
887 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100888 c.features.fid = cpu_to_le32(fid);
889 c.features.dword11 = cpu_to_le32(dword11);
890
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800891 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700892 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700893 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800894 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100895 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100896}
897
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100898int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
899{
900 u32 q_count = (*count - 1) | ((*count - 1) << 16);
901 u32 result;
902 int status, nr_io_queues;
903
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700904 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100905 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200906 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100907 return status;
908
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200909 /*
910 * Degraded controllers might return an error when setting the queue
911 * count. We still want to be able to bring them online and offer
912 * access to the admin queue, as that might be only way to fix them up.
913 */
914 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200915 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200916 *count = 0;
917 } else {
918 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
919 *count = min(*count, nr_io_queues);
920 }
921
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100922 return 0;
923}
Ming Lin576d55d2016-02-10 10:03:32 -0800924EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100925
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100926static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
927{
928 struct nvme_user_io io;
929 struct nvme_command c;
930 unsigned length, meta_len;
931 void __user *metadata;
932
933 if (copy_from_user(&io, uio, sizeof(io)))
934 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700935 if (io.flags)
936 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100937
938 switch (io.opcode) {
939 case nvme_cmd_write:
940 case nvme_cmd_read:
941 case nvme_cmd_compare:
942 break;
943 default:
944 return -EINVAL;
945 }
946
947 length = (io.nblocks + 1) << ns->lba_shift;
948 meta_len = (io.nblocks + 1) * ns->ms;
949 metadata = (void __user *)(uintptr_t)io.metadata;
950
951 if (ns->ext) {
952 length += meta_len;
953 meta_len = 0;
954 } else if (meta_len) {
955 if ((io.metadata & 3) || !io.metadata)
956 return -EINVAL;
957 }
958
959 memset(&c, 0, sizeof(c));
960 c.rw.opcode = io.opcode;
961 c.rw.flags = io.flags;
962 c.rw.nsid = cpu_to_le32(ns->ns_id);
963 c.rw.slba = cpu_to_le64(io.slba);
964 c.rw.length = cpu_to_le16(io.nblocks);
965 c.rw.control = cpu_to_le16(io.control);
966 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
967 c.rw.reftag = cpu_to_le32(io.reftag);
968 c.rw.apptag = cpu_to_le16(io.apptag);
969 c.rw.appmask = cpu_to_le16(io.appmask);
970
971 return __nvme_submit_user_cmd(ns->queue, &c,
972 (void __user *)(uintptr_t)io.addr, length,
973 metadata, meta_len, io.slba, NULL, 0);
974}
975
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100976static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100977 struct nvme_passthru_cmd __user *ucmd)
978{
979 struct nvme_passthru_cmd cmd;
980 struct nvme_command c;
981 unsigned timeout = 0;
982 int status;
983
984 if (!capable(CAP_SYS_ADMIN))
985 return -EACCES;
986 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
987 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700988 if (cmd.flags)
989 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100990
991 memset(&c, 0, sizeof(c));
992 c.common.opcode = cmd.opcode;
993 c.common.flags = cmd.flags;
994 c.common.nsid = cpu_to_le32(cmd.nsid);
995 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
996 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
997 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
998 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
999 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1000 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1001 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1002 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1003
1004 if (cmd.timeout_ms)
1005 timeout = msecs_to_jiffies(cmd.timeout_ms);
1006
1007 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001008 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001009 &cmd.result, timeout);
1010 if (status >= 0) {
1011 if (put_user(cmd.result, &ucmd->result))
1012 return -EFAULT;
1013 }
1014
1015 return status;
1016}
1017
1018static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1019 unsigned int cmd, unsigned long arg)
1020{
1021 struct nvme_ns *ns = bdev->bd_disk->private_data;
1022
1023 switch (cmd) {
1024 case NVME_IOCTL_ID:
1025 force_successful_syscall_return();
1026 return ns->ns_id;
1027 case NVME_IOCTL_ADMIN_CMD:
1028 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1029 case NVME_IOCTL_IO_CMD:
1030 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1031 case NVME_IOCTL_SUBMIT_IO:
1032 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001033 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001034#ifdef CONFIG_NVM
1035 if (ns->ndev)
1036 return nvme_nvm_ioctl(ns, cmd, arg);
1037#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001038 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001039 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001040 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001041 return -ENOTTY;
1042 }
1043}
1044
1045#ifdef CONFIG_COMPAT
1046static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1047 unsigned int cmd, unsigned long arg)
1048{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001049 return nvme_ioctl(bdev, mode, cmd, arg);
1050}
1051#else
1052#define nvme_compat_ioctl NULL
1053#endif
1054
1055static int nvme_open(struct block_device *bdev, fmode_t mode)
1056{
1057 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1058}
1059
1060static void nvme_release(struct gendisk *disk, fmode_t mode)
1061{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001062 struct nvme_ns *ns = disk->private_data;
1063
1064 module_put(ns->ctrl->ops->module);
1065 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001066}
1067
1068static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1069{
1070 /* some standard values */
1071 geo->heads = 1 << 6;
1072 geo->sectors = 1 << 5;
1073 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1074 return 0;
1075}
1076
1077#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001078static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1079 u16 bs)
1080{
1081 struct nvme_ns *ns = disk->private_data;
1082 u16 old_ms = ns->ms;
1083 u8 pi_type = 0;
1084
1085 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1086 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1087
1088 /* PI implementation requires metadata equal t10 pi tuple size */
1089 if (ns->ms == sizeof(struct t10_pi_tuple))
1090 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1091
1092 if (blk_get_integrity(disk) &&
1093 (ns->pi_type != pi_type || ns->ms != old_ms ||
1094 bs != queue_logical_block_size(disk->queue) ||
1095 (ns->ms && ns->ext)))
1096 blk_integrity_unregister(disk);
1097
1098 ns->pi_type = pi_type;
1099}
1100
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001101static void nvme_init_integrity(struct nvme_ns *ns)
1102{
1103 struct blk_integrity integrity;
1104
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001105 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001106 switch (ns->pi_type) {
1107 case NVME_NS_DPS_PI_TYPE3:
1108 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001109 integrity.tag_size = sizeof(u16) + sizeof(u32);
1110 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111 break;
1112 case NVME_NS_DPS_PI_TYPE1:
1113 case NVME_NS_DPS_PI_TYPE2:
1114 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001115 integrity.tag_size = sizeof(u16);
1116 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001117 break;
1118 default:
1119 integrity.profile = NULL;
1120 break;
1121 }
1122 integrity.tuple_size = ns->ms;
1123 blk_integrity_register(ns->disk, &integrity);
1124 blk_queue_max_integrity_segments(ns->queue, 1);
1125}
1126#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001127static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1128 u16 bs)
1129{
1130}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001131static void nvme_init_integrity(struct nvme_ns *ns)
1132{
1133}
1134#endif /* CONFIG_BLK_DEV_INTEGRITY */
1135
Scott Bauer6b8190d2017-06-15 10:44:30 -06001136static void nvme_set_chunk_size(struct nvme_ns *ns)
1137{
1138 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1139 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1140}
1141
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001142static void nvme_config_discard(struct nvme_ns *ns)
1143{
Keith Busch08095e72016-03-04 13:15:17 -07001144 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001145 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001146
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001147 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1148 NVME_DSM_MAX_RANGES);
1149
Jens Axboef5d11842017-06-27 12:03:06 -06001150 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1151 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1152
1153 ns->queue->limits.discard_alignment = sz;
1154 ns->queue->limits.discard_granularity = sz;
1155 } else {
1156 ns->queue->limits.discard_alignment = logical_block_size;
1157 ns->queue->limits.discard_granularity = logical_block_size;
1158 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001159 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001160 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001161 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001162
1163 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1164 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001165}
1166
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001167static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001168{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001169 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Sagi Grimbergcaaa15c2017-08-15 12:24:05 +03001170 dev_warn(ns->ctrl->device, "Identify namespace failed\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001171 return -ENODEV;
1172 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001173
1174 if ((*id)->ncap == 0) {
1175 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001176 return -ENODEV;
1177 }
1178
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001179 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001180 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001181 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001182 memcpy(ns->nguid, (*id)->nguid, sizeof(ns->nguid));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001183 if (ns->ctrl->vs >= NVME_VS(1, 3, 0)) {
1184 /* Don't treat error as fatal we potentially
1185 * already have a NGUID or EUI-64
1186 */
1187 if (nvme_identify_ns_descs(ns, ns->ns_id))
1188 dev_warn(ns->ctrl->device,
1189 "%s: Identify Descriptors failed\n", __func__);
1190 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001191
1192 return 0;
1193}
1194
1195static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1196{
1197 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001198 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001199 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001200
1201 /*
1202 * If identify namespace failed, use default 512 byte block size so
1203 * block layer can use before failing read/write for 0 capacity.
1204 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001205 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001206 if (ns->lba_shift == 0)
1207 ns->lba_shift = 9;
1208 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001209 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210
1211 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001212
Jens Axboef5d11842017-06-27 12:03:06 -06001213 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001214 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001215 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001216 if (ns->noiob)
1217 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001218 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001219 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001220 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1221 set_capacity(disk, 0);
1222 else
1223 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1224
Jens Axboef5d11842017-06-27 12:03:06 -06001225 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001226 nvme_config_discard(ns);
1227 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001228}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001229
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001230static int nvme_revalidate_disk(struct gendisk *disk)
1231{
1232 struct nvme_ns *ns = disk->private_data;
1233 struct nvme_id_ns *id = NULL;
1234 int ret;
1235
1236 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1237 set_capacity(disk, 0);
1238 return -ENODEV;
1239 }
1240
1241 ret = nvme_revalidate_ns(ns, &id);
1242 if (ret)
1243 return ret;
1244
1245 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001246 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001247
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001248 return 0;
1249}
1250
1251static char nvme_pr_type(enum pr_type type)
1252{
1253 switch (type) {
1254 case PR_WRITE_EXCLUSIVE:
1255 return 1;
1256 case PR_EXCLUSIVE_ACCESS:
1257 return 2;
1258 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1259 return 3;
1260 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1261 return 4;
1262 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1263 return 5;
1264 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1265 return 6;
1266 default:
1267 return 0;
1268 }
1269};
1270
1271static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1272 u64 key, u64 sa_key, u8 op)
1273{
1274 struct nvme_ns *ns = bdev->bd_disk->private_data;
1275 struct nvme_command c;
1276 u8 data[16] = { 0, };
1277
1278 put_unaligned_le64(key, &data[0]);
1279 put_unaligned_le64(sa_key, &data[8]);
1280
1281 memset(&c, 0, sizeof(c));
1282 c.common.opcode = op;
1283 c.common.nsid = cpu_to_le32(ns->ns_id);
1284 c.common.cdw10[0] = cpu_to_le32(cdw10);
1285
1286 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1287}
1288
1289static int nvme_pr_register(struct block_device *bdev, u64 old,
1290 u64 new, unsigned flags)
1291{
1292 u32 cdw10;
1293
1294 if (flags & ~PR_FL_IGNORE_KEY)
1295 return -EOPNOTSUPP;
1296
1297 cdw10 = old ? 2 : 0;
1298 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1299 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1300 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1301}
1302
1303static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1304 enum pr_type type, unsigned flags)
1305{
1306 u32 cdw10;
1307
1308 if (flags & ~PR_FL_IGNORE_KEY)
1309 return -EOPNOTSUPP;
1310
1311 cdw10 = nvme_pr_type(type) << 8;
1312 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1313 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1314}
1315
1316static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1317 enum pr_type type, bool abort)
1318{
1319 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1320 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1321}
1322
1323static int nvme_pr_clear(struct block_device *bdev, u64 key)
1324{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001325 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001326 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1327}
1328
1329static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1330{
1331 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1332 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1333}
1334
1335static const struct pr_ops nvme_pr_ops = {
1336 .pr_register = nvme_pr_register,
1337 .pr_reserve = nvme_pr_reserve,
1338 .pr_release = nvme_pr_release,
1339 .pr_preempt = nvme_pr_preempt,
1340 .pr_clear = nvme_pr_clear,
1341};
1342
Scott Bauera98e58e52017-02-03 12:50:32 -07001343#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001344int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1345 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001346{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001347 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001348 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001349
1350 memset(&cmd, 0, sizeof(cmd));
1351 if (send)
1352 cmd.common.opcode = nvme_admin_security_send;
1353 else
1354 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001355 cmd.common.nsid = 0;
1356 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1357 cmd.common.cdw10[1] = cpu_to_le32(len);
1358
1359 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1360 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1361}
1362EXPORT_SYMBOL_GPL(nvme_sec_submit);
1363#endif /* CONFIG_BLK_SED_OPAL */
1364
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001365static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001366 .owner = THIS_MODULE,
1367 .ioctl = nvme_ioctl,
1368 .compat_ioctl = nvme_compat_ioctl,
1369 .open = nvme_open,
1370 .release = nvme_release,
1371 .getgeo = nvme_getgeo,
1372 .revalidate_disk= nvme_revalidate_disk,
1373 .pr_ops = &nvme_pr_ops,
1374};
1375
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001376static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1377{
1378 unsigned long timeout =
1379 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1380 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1381 int ret;
1382
1383 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001384 if (csts == ~0)
1385 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001386 if ((csts & NVME_CSTS_RDY) == bit)
1387 break;
1388
1389 msleep(100);
1390 if (fatal_signal_pending(current))
1391 return -EINTR;
1392 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001393 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001394 "Device not ready; aborting %s\n", enabled ?
1395 "initialisation" : "reset");
1396 return -ENODEV;
1397 }
1398 }
1399
1400 return ret;
1401}
1402
1403/*
1404 * If the device has been passed off to us in an enabled state, just clear
1405 * the enabled bit. The spec says we should set the 'shutdown notification
1406 * bits', but doing so may cause the device to complete commands to the
1407 * admin queue ... and we don't know what memory that might be pointing at!
1408 */
1409int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1410{
1411 int ret;
1412
1413 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1414 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1415
1416 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1417 if (ret)
1418 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001419
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001420 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001421 msleep(NVME_QUIRK_DELAY_AMOUNT);
1422
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001423 return nvme_wait_ready(ctrl, cap, false);
1424}
Ming Lin576d55d2016-02-10 10:03:32 -08001425EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001426
1427int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1428{
1429 /*
1430 * Default to a 4K page size, with the intention to update this
1431 * path in the future to accomodate architectures with differing
1432 * kernel and IO page sizes.
1433 */
1434 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1435 int ret;
1436
1437 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001438 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001439 "Minimum device page size %u too large for host (%u)\n",
1440 1 << dev_page_min, 1 << page_shift);
1441 return -ENODEV;
1442 }
1443
1444 ctrl->page_size = 1 << page_shift;
1445
1446 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1447 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
1448 ctrl->ctrl_config |= NVME_CC_ARB_RR | NVME_CC_SHN_NONE;
1449 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1450 ctrl->ctrl_config |= NVME_CC_ENABLE;
1451
1452 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1453 if (ret)
1454 return ret;
1455 return nvme_wait_ready(ctrl, cap, true);
1456}
Ming Lin576d55d2016-02-10 10:03:32 -08001457EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001458
1459int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1460{
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +02001461 unsigned long timeout = jiffies + (shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001462 u32 csts;
1463 int ret;
1464
1465 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1466 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1467
1468 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1469 if (ret)
1470 return ret;
1471
1472 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1473 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1474 break;
1475
1476 msleep(100);
1477 if (fatal_signal_pending(current))
1478 return -EINTR;
1479 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001480 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001481 "Device shutdown incomplete; abort shutdown\n");
1482 return -ENODEV;
1483 }
1484 }
1485
1486 return ret;
1487}
Ming Lin576d55d2016-02-10 10:03:32 -08001488EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001489
Christoph Hellwigda358252016-03-02 18:07:11 +01001490static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1491 struct request_queue *q)
1492{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001493 bool vwc = false;
1494
Christoph Hellwigda358252016-03-02 18:07:11 +01001495 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001496 u32 max_segments =
1497 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1498
Christoph Hellwigda358252016-03-02 18:07:11 +01001499 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001500 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001501 }
Keith Busche6282ae2016-12-19 11:37:50 -05001502 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1503 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001504 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001505 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1506 vwc = true;
1507 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001508}
1509
Jon Derrickdbf86b32017-08-16 09:51:29 +02001510static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1511{
1512 __le64 ts;
1513 int ret;
1514
1515 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1516 return 0;
1517
1518 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1519 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1520 NULL);
1521 if (ret)
1522 dev_warn_once(ctrl->device,
1523 "could not set timestamp (%d)\n", ret);
1524 return ret;
1525}
1526
Keith Busch634b8322017-08-10 11:23:31 +02001527static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001528{
1529 /*
1530 * APST (Autonomous Power State Transition) lets us program a
1531 * table of power state transitions that the controller will
1532 * perform automatically. We configure it with a simple
1533 * heuristic: we are willing to spend at most 2% of the time
1534 * transitioning between power states. Therefore, when running
1535 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001536 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001537 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001538 * the requested maximum latency.
1539 *
1540 * We will not autonomously enter any non-operational state for
1541 * which the total latency exceeds ps_max_latency_us. Users
1542 * can set ps_max_latency_us to zero to turn off APST.
1543 */
1544
1545 unsigned apste;
1546 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001547 u64 max_lat_us = 0;
1548 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001549 int ret;
1550
1551 /*
1552 * If APST isn't supported or if we haven't been initialized yet,
1553 * then don't do anything.
1554 */
1555 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001556 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001557
1558 if (ctrl->npss > 31) {
1559 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001560 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001561 }
1562
1563 table = kzalloc(sizeof(*table), GFP_KERNEL);
1564 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001565 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001566
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001567 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001568 /* Turn off APST. */
1569 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001570 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001571 } else {
1572 __le64 target = cpu_to_le64(0);
1573 int state;
1574
1575 /*
1576 * Walk through all states from lowest- to highest-power.
1577 * According to the spec, lower-numbered states use more
1578 * power. NPSS, despite the name, is the index of the
1579 * lowest-power state, not the number of states.
1580 */
1581 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001582 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001583
1584 if (target)
1585 table->entries[state] = target;
1586
1587 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001588 * Don't allow transitions to the deepest state
1589 * if it's quirked off.
1590 */
1591 if (state == ctrl->npss &&
1592 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1593 continue;
1594
1595 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001596 * Is this state a useful non-operational state for
1597 * higher-power states to autonomously transition to?
1598 */
1599 if (!(ctrl->psd[state].flags &
1600 NVME_PS_FLAGS_NON_OP_STATE))
1601 continue;
1602
Kai-Heng Fengda875912017-06-07 15:25:42 +08001603 exit_latency_us =
1604 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1605 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001606 continue;
1607
Kai-Heng Fengda875912017-06-07 15:25:42 +08001608 total_latency_us =
1609 exit_latency_us +
1610 le32_to_cpu(ctrl->psd[state].entry_lat);
1611
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001612 /*
1613 * This state is good. Use it as the APST idle
1614 * target for higher power states.
1615 */
1616 transition_ms = total_latency_us + 19;
1617 do_div(transition_ms, 20);
1618 if (transition_ms > (1 << 24) - 1)
1619 transition_ms = (1 << 24) - 1;
1620
1621 target = cpu_to_le64((state << 3) |
1622 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001623
1624 if (max_ps == -1)
1625 max_ps = state;
1626
1627 if (total_latency_us > max_lat_us)
1628 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001629 }
1630
1631 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001632
1633 if (max_ps == -1) {
1634 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1635 } else {
1636 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1637 max_ps, max_lat_us, (int)sizeof(*table), table);
1638 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001639 }
1640
1641 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1642 table, sizeof(*table), NULL);
1643 if (ret)
1644 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1645
1646 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001647 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001648}
1649
1650static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1651{
1652 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1653 u64 latency;
1654
1655 switch (val) {
1656 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1657 case PM_QOS_LATENCY_ANY:
1658 latency = U64_MAX;
1659 break;
1660
1661 default:
1662 latency = val;
1663 }
1664
1665 if (ctrl->ps_max_latency_us != latency) {
1666 ctrl->ps_max_latency_us = latency;
1667 nvme_configure_apst(ctrl);
1668 }
1669}
1670
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001671struct nvme_core_quirk_entry {
1672 /*
1673 * NVMe model and firmware strings are padded with spaces. For
1674 * simplicity, strings in the quirk table are padded with NULLs
1675 * instead.
1676 */
1677 u16 vid;
1678 const char *mn;
1679 const char *fr;
1680 unsigned long quirks;
1681};
1682
1683static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001684 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001685 /*
1686 * This Toshiba device seems to die using any APST states. See:
1687 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1688 */
1689 .vid = 0x1179,
1690 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001691 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001692 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001693};
1694
1695/* match is null-terminated but idstr is space-padded. */
1696static bool string_matches(const char *idstr, const char *match, size_t len)
1697{
1698 size_t matchlen;
1699
1700 if (!match)
1701 return true;
1702
1703 matchlen = strlen(match);
1704 WARN_ON_ONCE(matchlen > len);
1705
1706 if (memcmp(idstr, match, matchlen))
1707 return false;
1708
1709 for (; matchlen < len; matchlen++)
1710 if (idstr[matchlen] != ' ')
1711 return false;
1712
1713 return true;
1714}
1715
1716static bool quirk_matches(const struct nvme_id_ctrl *id,
1717 const struct nvme_core_quirk_entry *q)
1718{
1719 return q->vid == le16_to_cpu(id->vid) &&
1720 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1721 string_matches(id->fr, q->fr, sizeof(id->fr));
1722}
1723
Christoph Hellwig180de0072017-06-26 12:39:02 +02001724static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1725{
1726 size_t nqnlen;
1727 int off;
1728
1729 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1730 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1731 strcpy(ctrl->subnqn, id->subnqn);
1732 return;
1733 }
1734
1735 if (ctrl->vs >= NVME_VS(1, 2, 1))
1736 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1737
1738 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1739 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1740 "nqn.2014.08.org.nvmexpress:%4x%4x",
1741 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1742 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1743 off += sizeof(id->sn);
1744 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1745 off += sizeof(id->mn);
1746 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1747}
1748
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001749/*
1750 * Initialize the cached copies of the Identify data and various controller
1751 * register in our nvme_ctrl structure. This should be called as soon as
1752 * the admin queue is fully up and running.
1753 */
1754int nvme_init_identify(struct nvme_ctrl *ctrl)
1755{
1756 struct nvme_id_ctrl *id;
1757 u64 cap;
1758 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001759 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001760 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001761
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001762 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1763 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001764 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001765 return ret;
1766 }
1767
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001768 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1769 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001770 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001771 return ret;
1772 }
1773 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1774
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001775 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001776 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1777
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001778 ret = nvme_identify_ctrl(ctrl, &id);
1779 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001780 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001781 return -EIO;
1782 }
1783
Christoph Hellwig180de0072017-06-26 12:39:02 +02001784 nvme_init_subnqn(ctrl, id);
1785
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001786 if (!ctrl->identified) {
1787 /*
1788 * Check for quirks. Quirk can depend on firmware version,
1789 * so, in principle, the set of quirks present can change
1790 * across a reset. As a possible future enhancement, we
1791 * could re-scan for quirks every time we reinitialize
1792 * the device, but we'd have to make sure that the driver
1793 * behaves intelligently if the quirks change.
1794 */
1795
1796 int i;
1797
1798 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1799 if (quirk_matches(id, &core_quirks[i]))
1800 ctrl->quirks |= core_quirks[i].quirks;
1801 }
1802 }
1803
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001804 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001805 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 -07001806 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1807 }
1808
Scott Bauer8a9ae522017-02-17 13:59:40 +01001809 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001810 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001811 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001812 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001813 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001814 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001815 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1816 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1817 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1818 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001819 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001820 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001821 max_hw_sectors = UINT_MAX;
1822 ctrl->max_hw_sectors =
1823 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001824
Christoph Hellwigda358252016-03-02 18:07:11 +01001825 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001826 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001827 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001828
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001829 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001830 ctrl->apsta = id->apsta;
1831 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001832 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1833 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001834 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 -04001835 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001836 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001837 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001838 }
1839 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001840 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001841 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001842 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1843
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001844 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001845 ctrl->icdoff = le16_to_cpu(id->icdoff);
1846 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1847 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1848 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1849
1850 /*
1851 * In fabrics we need to verify the cntlid matches the
1852 * admin connect
1853 */
Keith Busch634b8322017-08-10 11:23:31 +02001854 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001855 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001856 goto out_free;
1857 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001858
1859 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001860 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001861 "keep-alive support is mandatory for fabrics\n");
1862 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001863 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001864 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001865 } else {
1866 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001867 ctrl->hmpre = le32_to_cpu(id->hmpre);
1868 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001869 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001870
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001871 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001872
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001873 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001874 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001875 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001876 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1877
Keith Busch634b8322017-08-10 11:23:31 +02001878 ret = nvme_configure_apst(ctrl);
1879 if (ret < 0)
1880 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001881
1882 ret = nvme_configure_timestamp(ctrl);
1883 if (ret < 0)
1884 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001885
1886 ret = nvme_configure_directives(ctrl);
1887 if (ret < 0)
1888 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001889
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001890 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001891
Keith Busch634b8322017-08-10 11:23:31 +02001892 return 0;
1893
1894out_free:
1895 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001896 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001897}
Ming Lin576d55d2016-02-10 10:03:32 -08001898EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001899
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001900static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001901{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001902 struct nvme_ctrl *ctrl;
1903 int instance = iminor(inode);
1904 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001905
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001906 spin_lock(&dev_list_lock);
1907 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1908 if (ctrl->instance != instance)
1909 continue;
1910
1911 if (!ctrl->admin_q) {
1912 ret = -EWOULDBLOCK;
1913 break;
1914 }
1915 if (!kref_get_unless_zero(&ctrl->kref))
1916 break;
1917 file->private_data = ctrl;
1918 ret = 0;
1919 break;
1920 }
1921 spin_unlock(&dev_list_lock);
1922
1923 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001924}
1925
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001926static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001927{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001928 nvme_put_ctrl(file->private_data);
1929 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001930}
1931
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001932static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1933{
1934 struct nvme_ns *ns;
1935 int ret;
1936
1937 mutex_lock(&ctrl->namespaces_mutex);
1938 if (list_empty(&ctrl->namespaces)) {
1939 ret = -ENOTTY;
1940 goto out_unlock;
1941 }
1942
1943 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1944 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001945 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001946 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1947 ret = -EINVAL;
1948 goto out_unlock;
1949 }
1950
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001951 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001952 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1953 kref_get(&ns->kref);
1954 mutex_unlock(&ctrl->namespaces_mutex);
1955
1956 ret = nvme_user_cmd(ctrl, ns, argp);
1957 nvme_put_ns(ns);
1958 return ret;
1959
1960out_unlock:
1961 mutex_unlock(&ctrl->namespaces_mutex);
1962 return ret;
1963}
1964
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001965static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1966 unsigned long arg)
1967{
1968 struct nvme_ctrl *ctrl = file->private_data;
1969 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001970
1971 switch (cmd) {
1972 case NVME_IOCTL_ADMIN_CMD:
1973 return nvme_user_cmd(ctrl, NULL, argp);
1974 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001975 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001976 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001977 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001978 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001979 case NVME_IOCTL_SUBSYS_RESET:
1980 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001981 case NVME_IOCTL_RESCAN:
1982 nvme_queue_scan(ctrl);
1983 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001984 default:
1985 return -ENOTTY;
1986 }
1987}
1988
1989static const struct file_operations nvme_dev_fops = {
1990 .owner = THIS_MODULE,
1991 .open = nvme_dev_open,
1992 .release = nvme_dev_release,
1993 .unlocked_ioctl = nvme_dev_ioctl,
1994 .compat_ioctl = nvme_dev_ioctl,
1995};
1996
1997static ssize_t nvme_sysfs_reset(struct device *dev,
1998 struct device_attribute *attr, const char *buf,
1999 size_t count)
2000{
2001 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2002 int ret;
2003
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002004 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002005 if (ret < 0)
2006 return ret;
2007 return count;
2008}
2009static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2010
Keith Busch9ec3bb22016-04-29 15:45:18 -06002011static ssize_t nvme_sysfs_rescan(struct device *dev,
2012 struct device_attribute *attr, const char *buf,
2013 size_t count)
2014{
2015 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2016
2017 nvme_queue_scan(ctrl);
2018 return count;
2019}
2020static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2021
Keith Busch118472a2016-02-18 09:57:48 -07002022static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2023 char *buf)
2024{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002025 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002026 struct nvme_ctrl *ctrl = ns->ctrl;
2027 int serial_len = sizeof(ctrl->serial);
2028 int model_len = sizeof(ctrl->model);
2029
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002030 if (!uuid_is_null(&ns->uuid))
2031 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2032
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002033 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2034 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002035
2036 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2037 return sprintf(buf, "eui.%8phN\n", ns->eui);
2038
Martin Wilck758f3732017-07-20 18:34:02 +02002039 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2040 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002041 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002042 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2043 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002044 model_len--;
2045
2046 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2047 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2048}
2049static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2050
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002051static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2052 char *buf)
2053{
2054 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2055 return sprintf(buf, "%pU\n", ns->nguid);
2056}
2057static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2058
Keith Busch2b9b6e82015-12-22 10:10:45 -07002059static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2060 char *buf)
2061{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002062 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002063
2064 /* For backward compatibility expose the NGUID to userspace if
2065 * we have no UUID set
2066 */
2067 if (uuid_is_null(&ns->uuid)) {
2068 printk_ratelimited(KERN_WARNING
2069 "No UUID available providing old NGUID\n");
2070 return sprintf(buf, "%pU\n", ns->nguid);
2071 }
2072 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002073}
2074static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2075
2076static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2077 char *buf)
2078{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002079 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002080 return sprintf(buf, "%8phd\n", ns->eui);
2081}
2082static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2083
2084static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2085 char *buf)
2086{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002087 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002088 return sprintf(buf, "%d\n", ns->ns_id);
2089}
2090static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2091
2092static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002093 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002094 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002095 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002096 &dev_attr_eui.attr,
2097 &dev_attr_nsid.attr,
2098 NULL,
2099};
2100
Ming Lin1a353d82016-06-13 16:45:24 +02002101static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002102 struct attribute *a, int n)
2103{
2104 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002105 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002106
2107 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002108 if (uuid_is_null(&ns->uuid) ||
2109 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2110 return 0;
2111 }
2112 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002113 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002114 return 0;
2115 }
2116 if (a == &dev_attr_eui.attr) {
2117 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2118 return 0;
2119 }
2120 return a->mode;
2121}
2122
2123static const struct attribute_group nvme_ns_attr_group = {
2124 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002125 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002126};
2127
Ming Lin931e1c22016-02-26 13:24:19 -08002128#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002129static ssize_t field##_show(struct device *dev, \
2130 struct device_attribute *attr, char *buf) \
2131{ \
2132 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2133 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2134} \
2135static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2136
Ming Lin931e1c22016-02-26 13:24:19 -08002137#define nvme_show_int_function(field) \
2138static ssize_t field##_show(struct device *dev, \
2139 struct device_attribute *attr, char *buf) \
2140{ \
2141 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2142 return sprintf(buf, "%d\n", ctrl->field); \
2143} \
2144static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2145
2146nvme_show_str_function(model);
2147nvme_show_str_function(serial);
2148nvme_show_str_function(firmware_rev);
2149nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002150
Ming Lin1a353d82016-06-13 16:45:24 +02002151static ssize_t nvme_sysfs_delete(struct device *dev,
2152 struct device_attribute *attr, const char *buf,
2153 size_t count)
2154{
2155 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2156
2157 if (device_remove_file_self(dev, attr))
2158 ctrl->ops->delete_ctrl(ctrl);
2159 return count;
2160}
2161static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2162
2163static ssize_t nvme_sysfs_show_transport(struct device *dev,
2164 struct device_attribute *attr,
2165 char *buf)
2166{
2167 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2168
2169 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2170}
2171static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2172
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002173static ssize_t nvme_sysfs_show_state(struct device *dev,
2174 struct device_attribute *attr,
2175 char *buf)
2176{
2177 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2178 static const char *const state_name[] = {
2179 [NVME_CTRL_NEW] = "new",
2180 [NVME_CTRL_LIVE] = "live",
2181 [NVME_CTRL_RESETTING] = "resetting",
2182 [NVME_CTRL_RECONNECTING]= "reconnecting",
2183 [NVME_CTRL_DELETING] = "deleting",
2184 [NVME_CTRL_DEAD] = "dead",
2185 };
2186
2187 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2188 state_name[ctrl->state])
2189 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2190
2191 return sprintf(buf, "unknown state\n");
2192}
2193
2194static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2195
Ming Lin1a353d82016-06-13 16:45:24 +02002196static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2197 struct device_attribute *attr,
2198 char *buf)
2199{
2200 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2201
Christoph Hellwig180de0072017-06-26 12:39:02 +02002202 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002203}
2204static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2205
2206static ssize_t nvme_sysfs_show_address(struct device *dev,
2207 struct device_attribute *attr,
2208 char *buf)
2209{
2210 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2211
2212 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2213}
2214static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2215
Keith Busch779ff7562016-01-12 15:09:31 -07002216static struct attribute *nvme_dev_attrs[] = {
2217 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002218 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002219 &dev_attr_model.attr,
2220 &dev_attr_serial.attr,
2221 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002222 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002223 &dev_attr_delete_controller.attr,
2224 &dev_attr_transport.attr,
2225 &dev_attr_subsysnqn.attr,
2226 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002227 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002228 NULL
2229};
2230
Ming Lin1a353d82016-06-13 16:45:24 +02002231static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2232 struct attribute *a, int n)
2233{
2234 struct device *dev = container_of(kobj, struct device, kobj);
2235 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2236
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002237 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2238 return 0;
2239 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2240 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002241
2242 return a->mode;
2243}
2244
Keith Busch779ff7562016-01-12 15:09:31 -07002245static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002246 .attrs = nvme_dev_attrs,
2247 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002248};
2249
2250static const struct attribute_group *nvme_dev_attr_groups[] = {
2251 &nvme_dev_attrs_group,
2252 NULL,
2253};
2254
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002255static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2256{
2257 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2258 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2259
2260 return nsa->ns_id - nsb->ns_id;
2261}
2262
Keith Busch32f0c4a2016-07-13 11:45:02 -06002263static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002264{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002265 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002266
Keith Busch32f0c4a2016-07-13 11:45:02 -06002267 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002268 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002269 if (ns->ns_id == nsid) {
2270 kref_get(&ns->kref);
2271 ret = ns;
2272 break;
2273 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002274 if (ns->ns_id > nsid)
2275 break;
2276 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002277 mutex_unlock(&ctrl->namespaces_mutex);
2278 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002279}
2280
Jens Axboef5d11842017-06-27 12:03:06 -06002281static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2282{
2283 struct streams_directive_params s;
2284 int ret;
2285
2286 if (!ctrl->nr_streams)
2287 return 0;
2288
2289 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2290 if (ret)
2291 return ret;
2292
2293 ns->sws = le32_to_cpu(s.sws);
2294 ns->sgs = le16_to_cpu(s.sgs);
2295
2296 if (ns->sws) {
2297 unsigned int bs = 1 << ns->lba_shift;
2298
2299 blk_queue_io_min(ns->queue, bs * ns->sws);
2300 if (ns->sgs)
2301 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2302 }
2303
2304 return 0;
2305}
2306
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002307static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2308{
2309 struct nvme_ns *ns;
2310 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002311 struct nvme_id_ns *id;
2312 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002313 int node = dev_to_node(ctrl->dev);
2314
2315 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2316 if (!ns)
2317 return;
2318
Keith Busch075790e2016-02-24 09:15:53 -07002319 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2320 if (ns->instance < 0)
2321 goto out_free_ns;
2322
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002323 ns->queue = blk_mq_init_queue(ctrl->tagset);
2324 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002325 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002326 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2327 ns->queue->queuedata = ns;
2328 ns->ctrl = ctrl;
2329
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002330 kref_init(&ns->kref);
2331 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002332 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002333
2334 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002335 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002336 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002337
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002338 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002339
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002340 if (nvme_revalidate_ns(ns, &id))
2341 goto out_free_queue;
2342
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002343 if (nvme_nvm_ns_supported(ns, id) &&
2344 nvme_nvm_register(ns, disk_name, node)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002345 dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002346 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002347 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002348
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002349 disk = alloc_disk_node(0, node);
2350 if (!disk)
2351 goto out_free_id;
2352
2353 disk->fops = &nvme_fops;
2354 disk->private_data = ns;
2355 disk->queue = ns->queue;
2356 disk->flags = GENHD_FL_EXT_DEVT;
2357 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2358 ns->disk = disk;
2359
2360 __nvme_revalidate_disk(disk, id);
2361
Keith Busch32f0c4a2016-07-13 11:45:02 -06002362 mutex_lock(&ctrl->namespaces_mutex);
2363 list_add_tail(&ns->list, &ctrl->namespaces);
2364 mutex_unlock(&ctrl->namespaces_mutex);
2365
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002366 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002367
2368 kfree(id);
2369
Dan Williams0d52c7562016-06-15 19:44:20 -07002370 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002371 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2372 &nvme_ns_attr_group))
2373 pr_warn("%s: failed to create sysfs group for identification\n",
2374 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002375 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2376 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2377 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002378 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002379 out_free_id:
2380 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002381 out_free_queue:
2382 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002383 out_release_instance:
2384 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002385 out_free_ns:
2386 kfree(ns);
2387}
2388
2389static void nvme_ns_remove(struct nvme_ns *ns)
2390{
Keith Busch646017a2016-02-24 09:15:54 -07002391 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2392 return;
2393
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002394 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002395 if (blk_get_integrity(ns->disk))
2396 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002397 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2398 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002399 if (ns->ndev)
2400 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002401 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002402 blk_cleanup_queue(ns->queue);
2403 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002404
2405 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002406 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002407 mutex_unlock(&ns->ctrl->namespaces_mutex);
2408
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002409 nvme_put_ns(ns);
2410}
2411
Keith Busch540c8012015-10-22 15:45:06 -06002412static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2413{
2414 struct nvme_ns *ns;
2415
Keith Busch32f0c4a2016-07-13 11:45:02 -06002416 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002417 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002418 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002419 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002420 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002421 } else
2422 nvme_alloc_ns(ctrl, nsid);
2423}
2424
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302425static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2426 unsigned nsid)
2427{
2428 struct nvme_ns *ns, *next;
2429
2430 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2431 if (ns->ns_id > nsid)
2432 nvme_ns_remove(ns);
2433 }
2434}
2435
Keith Busch540c8012015-10-22 15:45:06 -06002436static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2437{
2438 struct nvme_ns *ns;
2439 __le32 *ns_list;
2440 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2441 int ret = 0;
2442
2443 ns_list = kzalloc(0x1000, GFP_KERNEL);
2444 if (!ns_list)
2445 return -ENOMEM;
2446
2447 for (i = 0; i < num_lists; i++) {
2448 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2449 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302450 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002451
2452 for (j = 0; j < min(nn, 1024U); j++) {
2453 nsid = le32_to_cpu(ns_list[j]);
2454 if (!nsid)
2455 goto out;
2456
2457 nvme_validate_ns(ctrl, nsid);
2458
2459 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002460 ns = nvme_find_get_ns(ctrl, prev);
2461 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002462 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002463 nvme_put_ns(ns);
2464 }
Keith Busch540c8012015-10-22 15:45:06 -06002465 }
2466 }
2467 nn -= j;
2468 }
2469 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302470 nvme_remove_invalid_namespaces(ctrl, prev);
2471 free:
Keith Busch540c8012015-10-22 15:45:06 -06002472 kfree(ns_list);
2473 return ret;
2474}
2475
Christoph Hellwig5955be22016-04-26 13:51:59 +02002476static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002477{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002478 unsigned i;
2479
Keith Busch540c8012015-10-22 15:45:06 -06002480 for (i = 1; i <= nn; i++)
2481 nvme_validate_ns(ctrl, i);
2482
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302483 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002484}
2485
Christoph Hellwig5955be22016-04-26 13:51:59 +02002486static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002487{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002488 struct nvme_ctrl *ctrl =
2489 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002490 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002491 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002492
Christoph Hellwig5955be22016-04-26 13:51:59 +02002493 if (ctrl->state != NVME_CTRL_LIVE)
2494 return;
2495
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002496 if (nvme_identify_ctrl(ctrl, &id))
2497 return;
Keith Busch540c8012015-10-22 15:45:06 -06002498
2499 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002500 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002501 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2502 if (!nvme_scan_ns_list(ctrl, nn))
2503 goto done;
2504 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002505 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002506 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002507 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002508 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002509 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002510 kfree(id);
2511}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002512
2513void nvme_queue_scan(struct nvme_ctrl *ctrl)
2514{
2515 /*
2516 * Do not queue new scan work when a controller is reset during
2517 * removal.
2518 */
2519 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002520 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002521}
2522EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002523
Keith Busch32f0c4a2016-07-13 11:45:02 -06002524/*
2525 * This function iterates the namespace list unlocked to allow recovery from
2526 * controller failure. It is up to the caller to ensure the namespace list is
2527 * not modified by scan work while this function is executing.
2528 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002529void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2530{
2531 struct nvme_ns *ns, *next;
2532
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002533 /*
2534 * The dead states indicates the controller was not gracefully
2535 * disconnected. In that case, we won't be able to flush any data while
2536 * removing the namespaces' disks; fail all the queues now to avoid
2537 * potentially having to clean up the failed sync later.
2538 */
2539 if (ctrl->state == NVME_CTRL_DEAD)
2540 nvme_kill_queues(ctrl);
2541
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002542 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2543 nvme_ns_remove(ns);
2544}
Ming Lin576d55d2016-02-10 10:03:32 -08002545EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002546
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002547static void nvme_async_event_work(struct work_struct *work)
2548{
2549 struct nvme_ctrl *ctrl =
2550 container_of(work, struct nvme_ctrl, async_event_work);
2551
2552 spin_lock_irq(&ctrl->lock);
2553 while (ctrl->event_limit > 0) {
2554 int aer_idx = --ctrl->event_limit;
2555
2556 spin_unlock_irq(&ctrl->lock);
2557 ctrl->ops->submit_async_event(ctrl, aer_idx);
2558 spin_lock_irq(&ctrl->lock);
2559 }
2560 spin_unlock_irq(&ctrl->lock);
2561}
2562
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302563static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2564{
2565
2566 u32 csts;
2567
2568 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2569 return false;
2570
2571 if (csts == ~0)
2572 return false;
2573
2574 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2575}
2576
2577static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2578{
2579 struct nvme_command c = { };
2580 struct nvme_fw_slot_info_log *log;
2581
2582 log = kmalloc(sizeof(*log), GFP_KERNEL);
2583 if (!log)
2584 return;
2585
2586 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302587 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302588 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2589
2590 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2591 dev_warn(ctrl->device,
2592 "Get FW SLOT INFO log error\n");
2593 kfree(log);
2594}
2595
2596static void nvme_fw_act_work(struct work_struct *work)
2597{
2598 struct nvme_ctrl *ctrl = container_of(work,
2599 struct nvme_ctrl, fw_act_work);
2600 unsigned long fw_act_timeout;
2601
2602 if (ctrl->mtfa)
2603 fw_act_timeout = jiffies +
2604 msecs_to_jiffies(ctrl->mtfa * 100);
2605 else
2606 fw_act_timeout = jiffies +
2607 msecs_to_jiffies(admin_timeout * 1000);
2608
2609 nvme_stop_queues(ctrl);
2610 while (nvme_ctrl_pp_status(ctrl)) {
2611 if (time_after(jiffies, fw_act_timeout)) {
2612 dev_warn(ctrl->device,
2613 "Fw activation timeout, reset controller\n");
2614 nvme_reset_ctrl(ctrl);
2615 break;
2616 }
2617 msleep(100);
2618 }
2619
2620 if (ctrl->state != NVME_CTRL_LIVE)
2621 return;
2622
2623 nvme_start_queues(ctrl);
2624 /* read FW slot informationi to clear the AER*/
2625 nvme_get_fw_slot_info(ctrl);
2626}
2627
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002628void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2629 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002630{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002631 u32 result = le32_to_cpu(res->u32);
2632 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002633
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002634 switch (le16_to_cpu(status) >> 1) {
2635 case NVME_SC_SUCCESS:
2636 done = false;
2637 /*FALLTHRU*/
2638 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002639 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002640 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002641 break;
2642 default:
2643 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002644 }
2645
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002646 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002647 return;
2648
2649 switch (result & 0xff07) {
2650 case NVME_AER_NOTICE_NS_CHANGED:
2651 dev_info(ctrl->device, "rescanning\n");
2652 nvme_queue_scan(ctrl);
2653 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302654 case NVME_AER_NOTICE_FW_ACT_STARTING:
2655 schedule_work(&ctrl->fw_act_work);
2656 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002657 default:
2658 dev_warn(ctrl->device, "async event result %08x\n", result);
2659 }
2660}
2661EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2662
2663void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2664{
2665 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002666 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002667}
2668EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2669
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002670static DEFINE_IDA(nvme_instance_ida);
2671
2672static int nvme_set_instance(struct nvme_ctrl *ctrl)
2673{
2674 int instance, error;
2675
2676 do {
2677 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2678 return -ENODEV;
2679
2680 spin_lock(&dev_list_lock);
2681 error = ida_get_new(&nvme_instance_ida, &instance);
2682 spin_unlock(&dev_list_lock);
2683 } while (error == -EAGAIN);
2684
2685 if (error)
2686 return -ENODEV;
2687
2688 ctrl->instance = instance;
2689 return 0;
2690}
2691
2692static void nvme_release_instance(struct nvme_ctrl *ctrl)
2693{
2694 spin_lock(&dev_list_lock);
2695 ida_remove(&nvme_instance_ida, ctrl->instance);
2696 spin_unlock(&dev_list_lock);
2697}
2698
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002699void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002700{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002701 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002702 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002703 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302704 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002705}
2706EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002707
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002708void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2709{
2710 if (ctrl->kato)
2711 nvme_start_keep_alive(ctrl);
2712
2713 if (ctrl->queue_count > 1) {
2714 nvme_queue_scan(ctrl);
2715 nvme_queue_async_events(ctrl);
2716 nvme_start_queues(ctrl);
2717 }
2718}
2719EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2720
2721void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2722{
Keith Busch53029b02015-11-28 15:41:02 +01002723 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002724
2725 spin_lock(&dev_list_lock);
2726 list_del(&ctrl->node);
2727 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002728}
Ming Lin576d55d2016-02-10 10:03:32 -08002729EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002730
2731static void nvme_free_ctrl(struct kref *kref)
2732{
2733 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002734
2735 put_device(ctrl->device);
2736 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002737 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002738
2739 ctrl->ops->free_ctrl(ctrl);
2740}
2741
2742void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2743{
2744 kref_put(&ctrl->kref, nvme_free_ctrl);
2745}
Ming Lin576d55d2016-02-10 10:03:32 -08002746EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002747
2748/*
2749 * Initialize a NVMe controller structures. This needs to be called during
2750 * earliest initialization so that we have the initialized structured around
2751 * during probing.
2752 */
2753int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2754 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2755{
2756 int ret;
2757
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002758 ctrl->state = NVME_CTRL_NEW;
2759 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002760 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002761 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002762 kref_init(&ctrl->kref);
2763 ctrl->dev = dev;
2764 ctrl->ops = ops;
2765 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002766 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002767 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302768 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002769
2770 ret = nvme_set_instance(ctrl);
2771 if (ret)
2772 goto out;
2773
Keith Busch779ff7562016-01-12 15:09:31 -07002774 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002775 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002776 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002777 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002778 if (IS_ERR(ctrl->device)) {
2779 ret = PTR_ERR(ctrl->device);
2780 goto out_release_instance;
2781 }
2782 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002783 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002784
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002785 spin_lock(&dev_list_lock);
2786 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2787 spin_unlock(&dev_list_lock);
2788
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002789 /*
2790 * Initialize latency tolerance controls. The sysfs files won't
2791 * be visible to userspace unless the device actually supports APST.
2792 */
2793 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2794 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2795 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2796
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002797 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002798out_release_instance:
2799 nvme_release_instance(ctrl);
2800out:
2801 return ret;
2802}
Ming Lin576d55d2016-02-10 10:03:32 -08002803EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002804
Keith Busch69d9a992016-02-24 09:15:56 -07002805/**
2806 * nvme_kill_queues(): Ends all namespace queues
2807 * @ctrl: the dead controller that needs to end
2808 *
2809 * Call this function when the driver determines it is unable to get the
2810 * controller in a state capable of servicing IO.
2811 */
2812void nvme_kill_queues(struct nvme_ctrl *ctrl)
2813{
2814 struct nvme_ns *ns;
2815
Keith Busch32f0c4a2016-07-13 11:45:02 -06002816 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002817
Ming Lei443bd902017-06-19 10:21:08 +08002818 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002819 if (ctrl->admin_q)
2820 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002821
Keith Busch32f0c4a2016-07-13 11:45:02 -06002822 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002823 /*
2824 * Revalidating a dead namespace sets capacity to 0. This will
2825 * end buffered writers dirtying pages that can't be synced.
2826 */
Keith Buschf33447b2017-02-10 18:15:51 -05002827 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2828 continue;
2829 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002830 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002831
Ming Lei443bd902017-06-19 10:21:08 +08002832 /* Forcibly unquiesce queues to avoid blocking dispatch */
2833 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002834 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002835 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002836}
Linus Torvalds237045f2016-03-18 17:13:31 -07002837EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002838
Keith Busch302ad8c2017-03-01 14:22:12 -05002839void nvme_unfreeze(struct nvme_ctrl *ctrl)
2840{
2841 struct nvme_ns *ns;
2842
2843 mutex_lock(&ctrl->namespaces_mutex);
2844 list_for_each_entry(ns, &ctrl->namespaces, list)
2845 blk_mq_unfreeze_queue(ns->queue);
2846 mutex_unlock(&ctrl->namespaces_mutex);
2847}
2848EXPORT_SYMBOL_GPL(nvme_unfreeze);
2849
2850void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2851{
2852 struct nvme_ns *ns;
2853
2854 mutex_lock(&ctrl->namespaces_mutex);
2855 list_for_each_entry(ns, &ctrl->namespaces, list) {
2856 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2857 if (timeout <= 0)
2858 break;
2859 }
2860 mutex_unlock(&ctrl->namespaces_mutex);
2861}
2862EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2863
2864void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2865{
2866 struct nvme_ns *ns;
2867
2868 mutex_lock(&ctrl->namespaces_mutex);
2869 list_for_each_entry(ns, &ctrl->namespaces, list)
2870 blk_mq_freeze_queue_wait(ns->queue);
2871 mutex_unlock(&ctrl->namespaces_mutex);
2872}
2873EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2874
2875void nvme_start_freeze(struct nvme_ctrl *ctrl)
2876{
2877 struct nvme_ns *ns;
2878
2879 mutex_lock(&ctrl->namespaces_mutex);
2880 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002881 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002882 mutex_unlock(&ctrl->namespaces_mutex);
2883}
2884EXPORT_SYMBOL_GPL(nvme_start_freeze);
2885
Keith Busch25646262016-01-04 09:10:57 -07002886void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002887{
2888 struct nvme_ns *ns;
2889
Keith Busch32f0c4a2016-07-13 11:45:02 -06002890 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002891 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002892 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002893 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002894}
Ming Lin576d55d2016-02-10 10:03:32 -08002895EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002896
Keith Busch25646262016-01-04 09:10:57 -07002897void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002898{
2899 struct nvme_ns *ns;
2900
Keith Busch32f0c4a2016-07-13 11:45:02 -06002901 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002902 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002903 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002904 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002905}
Ming Lin576d55d2016-02-10 10:03:32 -08002906EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002907
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002908int __init nvme_core_init(void)
2909{
2910 int result;
2911
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002912 nvme_wq = alloc_workqueue("nvme-wq",
2913 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2914 if (!nvme_wq)
2915 return -ENOMEM;
2916
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002917 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2918 &nvme_dev_fops);
2919 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002920 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002921 else if (result > 0)
2922 nvme_char_major = result;
2923
2924 nvme_class = class_create(THIS_MODULE, "nvme");
2925 if (IS_ERR(nvme_class)) {
2926 result = PTR_ERR(nvme_class);
2927 goto unregister_chrdev;
2928 }
2929
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002930 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002931
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002932unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002933 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002934destroy_wq:
2935 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002936 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002937}
2938
2939void nvme_core_exit(void)
2940{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002941 class_destroy(nvme_class);
2942 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002943 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002944}
Ming Lin576d55d2016-02-10 10:03:32 -08002945
2946MODULE_LICENSE("GPL");
2947MODULE_VERSION("1.0");
2948module_init(nvme_core_init);
2949module_exit(nvme_core_exit);