blob: d63e1fcf4437f296231c9a27942d823a22cb8fee [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 Hellwiga751da32017-08-22 10:17:03 +0200116 case NVME_SC_ACCESS_DENIED:
117 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200118 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200119 case NVME_SC_GUARD_CHECK:
120 case NVME_SC_APPTAG_CHECK:
121 case NVME_SC_REFTAG_CHECK:
122 case NVME_SC_INVALID_PI:
123 return BLK_STS_PROTECTION;
124 case NVME_SC_RESERVATION_CONFLICT:
125 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200126 default:
127 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200128 }
129}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200130
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200131static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200132{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200133 if (blk_noretry_request(req))
134 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200135 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200136 return false;
137 if (jiffies - req->start_time >= req->timeout)
138 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200139 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200140 return false;
141 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200142}
143
144void nvme_complete_rq(struct request *req)
145{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200146 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
147 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300148 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200149 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200150 }
151
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200152 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200153}
154EXPORT_SYMBOL_GPL(nvme_complete_rq);
155
Ming Linc55a2fd2016-05-18 14:05:02 -0700156void nvme_cancel_request(struct request *req, void *data, bool reserved)
157{
158 int status;
159
160 if (!blk_mq_request_started(req))
161 return;
162
163 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
164 "Cancelling I/O %d", req->tag);
165
166 status = NVME_SC_ABORT_REQ;
167 if (blk_queue_dying(req->q))
168 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200169 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200170 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200171
Ming Linc55a2fd2016-05-18 14:05:02 -0700172}
173EXPORT_SYMBOL_GPL(nvme_cancel_request);
174
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200175bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
176 enum nvme_ctrl_state new_state)
177{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300178 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200179 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200180 bool changed = false;
181
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200182 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300183
184 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200185 switch (new_state) {
186 case NVME_CTRL_LIVE:
187 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200188 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200189 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900190 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200191 changed = true;
192 /* FALLTHRU */
193 default:
194 break;
195 }
196 break;
197 case NVME_CTRL_RESETTING:
198 switch (old_state) {
199 case NVME_CTRL_NEW:
200 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900201 changed = true;
202 /* FALLTHRU */
203 default:
204 break;
205 }
206 break;
207 case NVME_CTRL_RECONNECTING:
208 switch (old_state) {
209 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200210 changed = true;
211 /* FALLTHRU */
212 default:
213 break;
214 }
215 break;
216 case NVME_CTRL_DELETING:
217 switch (old_state) {
218 case NVME_CTRL_LIVE:
219 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900220 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200221 changed = true;
222 /* FALLTHRU */
223 default:
224 break;
225 }
226 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600227 case NVME_CTRL_DEAD:
228 switch (old_state) {
229 case NVME_CTRL_DELETING:
230 changed = true;
231 /* FALLTHRU */
232 default:
233 break;
234 }
235 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200236 default:
237 break;
238 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200239
240 if (changed)
241 ctrl->state = new_state;
242
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200243 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300244
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200245 return changed;
246}
247EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
248
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100249static void nvme_free_ns(struct kref *kref)
250{
251 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
252
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200253 if (ns->ndev)
254 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100255
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200256 if (ns->disk) {
257 spin_lock(&dev_list_lock);
258 ns->disk->private_data = NULL;
259 spin_unlock(&dev_list_lock);
260 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100261
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100262 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700263 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
264 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100265 kfree(ns);
266}
267
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100268static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100269{
270 kref_put(&ns->kref, nvme_free_ns);
271}
272
273static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
274{
275 struct nvme_ns *ns;
276
277 spin_lock(&dev_list_lock);
278 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800279 if (ns) {
280 if (!kref_get_unless_zero(&ns->kref))
281 goto fail;
282 if (!try_module_get(ns->ctrl->ops->module))
283 goto fail_put_ns;
284 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100285 spin_unlock(&dev_list_lock);
286
287 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800288
289fail_put_ns:
290 kref_put(&ns->kref, nvme_free_ns);
291fail:
292 spin_unlock(&dev_list_lock);
293 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100294}
295
Christoph Hellwig41609822015-11-20 09:00:02 +0100296struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200297 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100298{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100299 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100300 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100301
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200302 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100303 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200304 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100305 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200306 qid ? qid - 1 : 0);
307 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100308 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100309 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100310
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800312 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100313
Christoph Hellwig41609822015-11-20 09:00:02 +0100314 return req;
315}
Ming Lin576d55d2016-02-10 10:03:32 -0800316EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100317
Jens Axboef5d11842017-06-27 12:03:06 -0600318static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
319{
320 struct nvme_command c;
321
322 memset(&c, 0, sizeof(c));
323
324 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530325 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600326 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
327 c.directive.dtype = NVME_DIR_IDENTIFY;
328 c.directive.tdtype = NVME_DIR_STREAMS;
329 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
330
331 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
332}
333
334static int nvme_disable_streams(struct nvme_ctrl *ctrl)
335{
336 return nvme_toggle_streams(ctrl, false);
337}
338
339static int nvme_enable_streams(struct nvme_ctrl *ctrl)
340{
341 return nvme_toggle_streams(ctrl, true);
342}
343
344static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
345 struct streams_directive_params *s, u32 nsid)
346{
347 struct nvme_command c;
348
349 memset(&c, 0, sizeof(c));
350 memset(s, 0, sizeof(*s));
351
352 c.directive.opcode = nvme_admin_directive_recv;
353 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700354 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600355 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
356 c.directive.dtype = NVME_DIR_STREAMS;
357
358 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
359}
360
361static int nvme_configure_directives(struct nvme_ctrl *ctrl)
362{
363 struct streams_directive_params s;
364 int ret;
365
366 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
367 return 0;
368 if (!streams)
369 return 0;
370
371 ret = nvme_enable_streams(ctrl);
372 if (ret)
373 return ret;
374
Arnav Dawn62346ea2017-07-12 16:11:53 +0530375 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600376 if (ret)
377 return ret;
378
379 ctrl->nssa = le16_to_cpu(s.nssa);
380 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
381 dev_info(ctrl->device, "too few streams (%u) available\n",
382 ctrl->nssa);
383 nvme_disable_streams(ctrl);
384 return 0;
385 }
386
387 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
388 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
389 return 0;
390}
391
392/*
393 * Check if 'req' has a write hint associated with it. If it does, assign
394 * a valid namespace stream to the write.
395 */
396static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
397 struct request *req, u16 *control,
398 u32 *dsmgmt)
399{
400 enum rw_hint streamid = req->write_hint;
401
402 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
403 streamid = 0;
404 else {
405 streamid--;
406 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
407 return;
408
409 *control |= NVME_RW_DTYPE_STREAMS;
410 *dsmgmt |= streamid << 16;
411 }
412
413 if (streamid < ARRAY_SIZE(req->q->write_hints))
414 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
415}
416
Ming Lin8093f7c2016-04-12 13:10:14 -0600417static inline void nvme_setup_flush(struct nvme_ns *ns,
418 struct nvme_command *cmnd)
419{
420 memset(cmnd, 0, sizeof(*cmnd));
421 cmnd->common.opcode = nvme_cmd_flush;
422 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
423}
424
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200425static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600426 struct nvme_command *cmnd)
427{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100428 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600429 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100430 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600431
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100432 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600433 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200434 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600435
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100436 __rq_for_each_bio(bio, req) {
437 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
438 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
439
440 range[n].cattr = cpu_to_le32(0);
441 range[n].nlb = cpu_to_le32(nlb);
442 range[n].slba = cpu_to_le64(slba);
443 n++;
444 }
445
446 if (WARN_ON_ONCE(n != segments)) {
447 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200448 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100449 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600450
451 memset(cmnd, 0, sizeof(*cmnd));
452 cmnd->dsm.opcode = nvme_cmd_dsm;
453 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200454 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600455 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
456
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700457 req->special_vec.bv_page = virt_to_page(range);
458 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100459 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700460 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600461
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200462 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600463}
Ming Lin8093f7c2016-04-12 13:10:14 -0600464
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200465static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
466 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600467{
Jens Axboef5d11842017-06-27 12:03:06 -0600468 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600469 u16 control = 0;
470 u32 dsmgmt = 0;
471
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200472 /*
473 * If formated with metadata, require the block layer provide a buffer
474 * unless this namespace is formated such that the metadata can be
475 * stripped/generated by the controller with PRACT=1.
476 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300477 if (ns && ns->ms &&
478 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200479 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
480 return BLK_STS_NOTSUPP;
481
Ming Lin8093f7c2016-04-12 13:10:14 -0600482 if (req->cmd_flags & REQ_FUA)
483 control |= NVME_RW_FUA;
484 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
485 control |= NVME_RW_LR;
486
487 if (req->cmd_flags & REQ_RAHEAD)
488 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
489
490 memset(cmnd, 0, sizeof(*cmnd));
491 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600492 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
493 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
494 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
495
Jens Axboef5d11842017-06-27 12:03:06 -0600496 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
497 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
498
Ming Lin8093f7c2016-04-12 13:10:14 -0600499 if (ns->ms) {
500 switch (ns->pi_type) {
501 case NVME_NS_DPS_PI_TYPE3:
502 control |= NVME_RW_PRINFO_PRCHK_GUARD;
503 break;
504 case NVME_NS_DPS_PI_TYPE1:
505 case NVME_NS_DPS_PI_TYPE2:
506 control |= NVME_RW_PRINFO_PRCHK_GUARD |
507 NVME_RW_PRINFO_PRCHK_REF;
508 cmnd->rw.reftag = cpu_to_le32(
509 nvme_block_nr(ns, blk_rq_pos(req)));
510 break;
511 }
512 if (!blk_integrity_rq(req))
513 control |= NVME_RW_PRINFO_PRACT;
514 }
515
516 cmnd->rw.control = cpu_to_le16(control);
517 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200518 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600519}
520
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200521blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600522 struct nvme_command *cmd)
523{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200524 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600525
Christoph Hellwig987f6992017-04-05 19:18:08 +0200526 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200527 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200528 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200529 req->rq_flags |= RQF_DONTPREP;
530 }
531
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100532 switch (req_op(req)) {
533 case REQ_OP_DRV_IN:
534 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800535 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100536 break;
537 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600538 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100539 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200540 case REQ_OP_WRITE_ZEROES:
541 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600543 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100544 break;
545 case REQ_OP_READ:
546 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200547 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100548 break;
549 default:
550 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200551 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100552 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600553
James Smart721b3912016-10-21 23:33:34 +0300554 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600555 return ret;
556}
557EXPORT_SYMBOL_GPL(nvme_setup_cmd);
558
Christoph Hellwig41609822015-11-20 09:00:02 +0100559/*
560 * Returns 0 on success. If the result is negative, it's a Linux error code;
561 * if the result is positive, it's an NVM Express status code
562 */
563int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800564 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200565 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100566{
567 struct request *req;
568 int ret;
569
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200570 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100571 if (IS_ERR(req))
572 return PTR_ERR(req);
573
574 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
575
Christoph Hellwig21d34712015-11-26 09:08:36 +0100576 if (buffer && bufflen) {
577 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
578 if (ret)
579 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100580 }
581
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200582 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800583 if (result)
584 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200585 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
586 ret = -EINTR;
587 else
588 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100589 out:
590 blk_mq_free_request(req);
591 return ret;
592}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200593EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100594
595int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
596 void *buffer, unsigned bufflen)
597{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200598 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
599 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100600}
Ming Lin576d55d2016-02-10 10:03:32 -0800601EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100602
Keith Busch0b7f1f22015-10-23 09:47:28 -0600603int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
604 void __user *ubuffer, unsigned bufflen,
605 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
606 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100607{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200608 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600609 struct nvme_ns *ns = q->queuedata;
610 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100611 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600612 struct bio *bio = NULL;
613 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100614 int ret;
615
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200616 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100617 if (IS_ERR(req))
618 return PTR_ERR(req);
619
620 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
621
622 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100623 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
624 GFP_KERNEL);
625 if (ret)
626 goto out;
627 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100628
Keith Busch0b7f1f22015-10-23 09:47:28 -0600629 if (!disk)
630 goto submit;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200631 bio->bi_disk = disk;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600632
Keith Busche9fc63d2016-02-24 09:15:58 -0700633 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600634 struct bio_integrity_payload *bip;
635
636 meta = kmalloc(meta_len, GFP_KERNEL);
637 if (!meta) {
638 ret = -ENOMEM;
639 goto out_unmap;
640 }
641
642 if (write) {
643 if (copy_from_user(meta, meta_buffer,
644 meta_len)) {
645 ret = -EFAULT;
646 goto out_free_meta;
647 }
648 }
649
650 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700651 if (IS_ERR(bip)) {
652 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600653 goto out_free_meta;
654 }
655
656 bip->bip_iter.bi_size = meta_len;
657 bip->bip_iter.bi_sector = meta_seed;
658
659 ret = bio_integrity_add_page(bio, virt_to_page(meta),
660 meta_len, offset_in_page(meta));
661 if (ret != meta_len) {
662 ret = -ENOMEM;
663 goto out_free_meta;
664 }
665 }
666 }
667 submit:
668 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200669 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
670 ret = -EINTR;
671 else
672 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100673 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800674 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600675 if (meta && !ret && !write) {
676 if (copy_to_user(meta_buffer, meta, meta_len))
677 ret = -EFAULT;
678 }
679 out_free_meta:
680 kfree(meta);
681 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200682 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600683 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100684 out:
685 blk_mq_free_request(req);
686 return ret;
687}
688
Keith Busch0b7f1f22015-10-23 09:47:28 -0600689int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
690 void __user *ubuffer, unsigned bufflen, u32 *result,
691 unsigned timeout)
692{
693 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
694 result, timeout);
695}
696
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200697static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200698{
699 struct nvme_ctrl *ctrl = rq->end_io_data;
700
701 blk_mq_free_request(rq);
702
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200703 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200704 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705 "failed nvme_keep_alive_end_io error=%d\n",
706 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200707 return;
708 }
709
710 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
711}
712
713static int nvme_keep_alive(struct nvme_ctrl *ctrl)
714{
715 struct nvme_command c;
716 struct request *rq;
717
718 memset(&c, 0, sizeof(c));
719 c.common.opcode = nvme_admin_keep_alive;
720
721 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
722 NVME_QID_ANY);
723 if (IS_ERR(rq))
724 return PTR_ERR(rq);
725
726 rq->timeout = ctrl->kato * HZ;
727 rq->end_io_data = ctrl;
728
729 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
730
731 return 0;
732}
733
734static void nvme_keep_alive_work(struct work_struct *work)
735{
736 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
737 struct nvme_ctrl, ka_work);
738
739 if (nvme_keep_alive(ctrl)) {
740 /* allocation failure, reset the controller */
741 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200742 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200743 return;
744 }
745}
746
747void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
748{
749 if (unlikely(ctrl->kato == 0))
750 return;
751
752 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
753 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
754}
755EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
756
757void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
758{
759 if (unlikely(ctrl->kato == 0))
760 return;
761
762 cancel_delayed_work_sync(&ctrl->ka_work);
763}
764EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
765
Keith Busch3f7f25a92017-06-20 15:09:56 -0400766static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100767{
768 struct nvme_command c = { };
769 int error;
770
771 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
772 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200773 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100774
775 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
776 if (!*id)
777 return -ENOMEM;
778
779 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
780 sizeof(struct nvme_id_ctrl));
781 if (error)
782 kfree(*id);
783 return error;
784}
785
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200786static int nvme_identify_ns_descs(struct nvme_ns *ns, unsigned nsid)
787{
788 struct nvme_command c = { };
789 int status;
790 void *data;
791 int pos;
792 int len;
793
794 c.identify.opcode = nvme_admin_identify;
795 c.identify.nsid = cpu_to_le32(nsid);
796 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
797
798 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
799 if (!data)
800 return -ENOMEM;
801
802 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, data,
803 NVME_IDENTIFY_DATA_SIZE);
804 if (status)
805 goto free_data;
806
807 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
808 struct nvme_ns_id_desc *cur = data + pos;
809
810 if (cur->nidl == 0)
811 break;
812
813 switch (cur->nidt) {
814 case NVME_NIDT_EUI64:
815 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
816 dev_warn(ns->ctrl->device,
817 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
818 cur->nidl);
819 goto free_data;
820 }
821 len = NVME_NIDT_EUI64_LEN;
822 memcpy(ns->eui, data + pos + sizeof(*cur), len);
823 break;
824 case NVME_NIDT_NGUID:
825 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
826 dev_warn(ns->ctrl->device,
827 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
828 cur->nidl);
829 goto free_data;
830 }
831 len = NVME_NIDT_NGUID_LEN;
832 memcpy(ns->nguid, data + pos + sizeof(*cur), len);
833 break;
834 case NVME_NIDT_UUID:
835 if (cur->nidl != NVME_NIDT_UUID_LEN) {
836 dev_warn(ns->ctrl->device,
837 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
838 cur->nidl);
839 goto free_data;
840 }
841 len = NVME_NIDT_UUID_LEN;
842 uuid_copy(&ns->uuid, data + pos + sizeof(*cur));
843 break;
844 default:
845 /* Skip unnkown types */
846 len = cur->nidl;
847 break;
848 }
849
850 len += sizeof(*cur);
851 }
852free_data:
853 kfree(data);
854 return status;
855}
856
Keith Busch540c8012015-10-22 15:45:06 -0600857static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
858{
859 struct nvme_command c = { };
860
861 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200862 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600863 c.identify.nsid = cpu_to_le32(nsid);
864 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
865}
866
Keith Busch3f7f25a92017-06-20 15:09:56 -0400867static int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100868 struct nvme_id_ns **id)
869{
870 struct nvme_command c = { };
871 int error;
872
873 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200874 c.identify.opcode = nvme_admin_identify;
875 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200876 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100877
878 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
879 if (!*id)
880 return -ENOMEM;
881
882 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
883 sizeof(struct nvme_id_ns));
884 if (error)
885 kfree(*id);
886 return error;
887}
888
Keith Busch3f7f25a92017-06-20 15:09:56 -0400889static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700890 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100891{
892 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800893 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100894 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100895
896 memset(&c, 0, sizeof(c));
897 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100898 c.features.fid = cpu_to_le32(fid);
899 c.features.dword11 = cpu_to_le32(dword11);
900
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800901 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700902 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700903 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800904 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100905 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100906}
907
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100908int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
909{
910 u32 q_count = (*count - 1) | ((*count - 1) << 16);
911 u32 result;
912 int status, nr_io_queues;
913
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700914 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100915 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200916 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100917 return status;
918
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200919 /*
920 * Degraded controllers might return an error when setting the queue
921 * count. We still want to be able to bring them online and offer
922 * access to the admin queue, as that might be only way to fix them up.
923 */
924 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200925 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200926 *count = 0;
927 } else {
928 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
929 *count = min(*count, nr_io_queues);
930 }
931
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100932 return 0;
933}
Ming Lin576d55d2016-02-10 10:03:32 -0800934EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100935
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100936static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
937{
938 struct nvme_user_io io;
939 struct nvme_command c;
940 unsigned length, meta_len;
941 void __user *metadata;
942
943 if (copy_from_user(&io, uio, sizeof(io)))
944 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700945 if (io.flags)
946 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100947
948 switch (io.opcode) {
949 case nvme_cmd_write:
950 case nvme_cmd_read:
951 case nvme_cmd_compare:
952 break;
953 default:
954 return -EINVAL;
955 }
956
957 length = (io.nblocks + 1) << ns->lba_shift;
958 meta_len = (io.nblocks + 1) * ns->ms;
959 metadata = (void __user *)(uintptr_t)io.metadata;
960
961 if (ns->ext) {
962 length += meta_len;
963 meta_len = 0;
964 } else if (meta_len) {
965 if ((io.metadata & 3) || !io.metadata)
966 return -EINVAL;
967 }
968
969 memset(&c, 0, sizeof(c));
970 c.rw.opcode = io.opcode;
971 c.rw.flags = io.flags;
972 c.rw.nsid = cpu_to_le32(ns->ns_id);
973 c.rw.slba = cpu_to_le64(io.slba);
974 c.rw.length = cpu_to_le16(io.nblocks);
975 c.rw.control = cpu_to_le16(io.control);
976 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
977 c.rw.reftag = cpu_to_le32(io.reftag);
978 c.rw.apptag = cpu_to_le16(io.apptag);
979 c.rw.appmask = cpu_to_le16(io.appmask);
980
981 return __nvme_submit_user_cmd(ns->queue, &c,
982 (void __user *)(uintptr_t)io.addr, length,
983 metadata, meta_len, io.slba, NULL, 0);
984}
985
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100986static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100987 struct nvme_passthru_cmd __user *ucmd)
988{
989 struct nvme_passthru_cmd cmd;
990 struct nvme_command c;
991 unsigned timeout = 0;
992 int status;
993
994 if (!capable(CAP_SYS_ADMIN))
995 return -EACCES;
996 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
997 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700998 if (cmd.flags)
999 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001000
1001 memset(&c, 0, sizeof(c));
1002 c.common.opcode = cmd.opcode;
1003 c.common.flags = cmd.flags;
1004 c.common.nsid = cpu_to_le32(cmd.nsid);
1005 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1006 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1007 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1008 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1009 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1010 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1011 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1012 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1013
1014 if (cmd.timeout_ms)
1015 timeout = msecs_to_jiffies(cmd.timeout_ms);
1016
1017 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001018 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001019 &cmd.result, timeout);
1020 if (status >= 0) {
1021 if (put_user(cmd.result, &ucmd->result))
1022 return -EFAULT;
1023 }
1024
1025 return status;
1026}
1027
1028static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1029 unsigned int cmd, unsigned long arg)
1030{
1031 struct nvme_ns *ns = bdev->bd_disk->private_data;
1032
1033 switch (cmd) {
1034 case NVME_IOCTL_ID:
1035 force_successful_syscall_return();
1036 return ns->ns_id;
1037 case NVME_IOCTL_ADMIN_CMD:
1038 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1039 case NVME_IOCTL_IO_CMD:
1040 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1041 case NVME_IOCTL_SUBMIT_IO:
1042 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001043 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001044#ifdef CONFIG_NVM
1045 if (ns->ndev)
1046 return nvme_nvm_ioctl(ns, cmd, arg);
1047#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001048 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001049 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001050 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001051 return -ENOTTY;
1052 }
1053}
1054
1055#ifdef CONFIG_COMPAT
1056static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1057 unsigned int cmd, unsigned long arg)
1058{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001059 return nvme_ioctl(bdev, mode, cmd, arg);
1060}
1061#else
1062#define nvme_compat_ioctl NULL
1063#endif
1064
1065static int nvme_open(struct block_device *bdev, fmode_t mode)
1066{
1067 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1068}
1069
1070static void nvme_release(struct gendisk *disk, fmode_t mode)
1071{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001072 struct nvme_ns *ns = disk->private_data;
1073
1074 module_put(ns->ctrl->ops->module);
1075 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001076}
1077
1078static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1079{
1080 /* some standard values */
1081 geo->heads = 1 << 6;
1082 geo->sectors = 1 << 5;
1083 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1084 return 0;
1085}
1086
1087#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001088static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1089 u16 bs)
1090{
1091 struct nvme_ns *ns = disk->private_data;
1092 u16 old_ms = ns->ms;
1093 u8 pi_type = 0;
1094
1095 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1096 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1097
1098 /* PI implementation requires metadata equal t10 pi tuple size */
1099 if (ns->ms == sizeof(struct t10_pi_tuple))
1100 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1101
1102 if (blk_get_integrity(disk) &&
1103 (ns->pi_type != pi_type || ns->ms != old_ms ||
1104 bs != queue_logical_block_size(disk->queue) ||
1105 (ns->ms && ns->ext)))
1106 blk_integrity_unregister(disk);
1107
1108 ns->pi_type = pi_type;
1109}
1110
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111static void nvme_init_integrity(struct nvme_ns *ns)
1112{
1113 struct blk_integrity integrity;
1114
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001115 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001116 switch (ns->pi_type) {
1117 case NVME_NS_DPS_PI_TYPE3:
1118 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001119 integrity.tag_size = sizeof(u16) + sizeof(u32);
1120 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001121 break;
1122 case NVME_NS_DPS_PI_TYPE1:
1123 case NVME_NS_DPS_PI_TYPE2:
1124 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001125 integrity.tag_size = sizeof(u16);
1126 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001127 break;
1128 default:
1129 integrity.profile = NULL;
1130 break;
1131 }
1132 integrity.tuple_size = ns->ms;
1133 blk_integrity_register(ns->disk, &integrity);
1134 blk_queue_max_integrity_segments(ns->queue, 1);
1135}
1136#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001137static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1138 u16 bs)
1139{
1140}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001141static void nvme_init_integrity(struct nvme_ns *ns)
1142{
1143}
1144#endif /* CONFIG_BLK_DEV_INTEGRITY */
1145
Scott Bauer6b8190d2017-06-15 10:44:30 -06001146static void nvme_set_chunk_size(struct nvme_ns *ns)
1147{
1148 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1149 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1150}
1151
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001152static void nvme_config_discard(struct nvme_ns *ns)
1153{
Keith Busch08095e72016-03-04 13:15:17 -07001154 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001155 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001156
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001157 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1158 NVME_DSM_MAX_RANGES);
1159
Jens Axboef5d11842017-06-27 12:03:06 -06001160 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1161 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1162
1163 ns->queue->limits.discard_alignment = sz;
1164 ns->queue->limits.discard_granularity = sz;
1165 } else {
1166 ns->queue->limits.discard_alignment = logical_block_size;
1167 ns->queue->limits.discard_granularity = logical_block_size;
1168 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001169 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001170 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001171 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001172
1173 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1174 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001175}
1176
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001177static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001178{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001179 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Sagi Grimbergcaaa15c2017-08-15 12:24:05 +03001180 dev_warn(ns->ctrl->device, "Identify namespace failed\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001181 return -ENODEV;
1182 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001183
1184 if ((*id)->ncap == 0) {
1185 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001186 return -ENODEV;
1187 }
1188
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001189 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001190 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001191 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001192 memcpy(ns->nguid, (*id)->nguid, sizeof(ns->nguid));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001193 if (ns->ctrl->vs >= NVME_VS(1, 3, 0)) {
1194 /* Don't treat error as fatal we potentially
1195 * already have a NGUID or EUI-64
1196 */
1197 if (nvme_identify_ns_descs(ns, ns->ns_id))
1198 dev_warn(ns->ctrl->device,
1199 "%s: Identify Descriptors failed\n", __func__);
1200 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001201
1202 return 0;
1203}
1204
1205static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1206{
1207 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001208 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001209 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210
1211 /*
1212 * If identify namespace failed, use default 512 byte block size so
1213 * block layer can use before failing read/write for 0 capacity.
1214 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001215 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001216 if (ns->lba_shift == 0)
1217 ns->lba_shift = 9;
1218 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001219 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001220
1221 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001222
Jens Axboef5d11842017-06-27 12:03:06 -06001223 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001224 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001225 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001226 if (ns->noiob)
1227 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001228 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001229 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001230 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1231 set_capacity(disk, 0);
1232 else
1233 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1234
Jens Axboef5d11842017-06-27 12:03:06 -06001235 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001236 nvme_config_discard(ns);
1237 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001238}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001239
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001240static int nvme_revalidate_disk(struct gendisk *disk)
1241{
1242 struct nvme_ns *ns = disk->private_data;
1243 struct nvme_id_ns *id = NULL;
1244 int ret;
1245
1246 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1247 set_capacity(disk, 0);
1248 return -ENODEV;
1249 }
1250
1251 ret = nvme_revalidate_ns(ns, &id);
1252 if (ret)
1253 return ret;
1254
1255 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001256 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001257
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001258 return 0;
1259}
1260
1261static char nvme_pr_type(enum pr_type type)
1262{
1263 switch (type) {
1264 case PR_WRITE_EXCLUSIVE:
1265 return 1;
1266 case PR_EXCLUSIVE_ACCESS:
1267 return 2;
1268 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1269 return 3;
1270 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1271 return 4;
1272 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1273 return 5;
1274 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1275 return 6;
1276 default:
1277 return 0;
1278 }
1279};
1280
1281static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1282 u64 key, u64 sa_key, u8 op)
1283{
1284 struct nvme_ns *ns = bdev->bd_disk->private_data;
1285 struct nvme_command c;
1286 u8 data[16] = { 0, };
1287
1288 put_unaligned_le64(key, &data[0]);
1289 put_unaligned_le64(sa_key, &data[8]);
1290
1291 memset(&c, 0, sizeof(c));
1292 c.common.opcode = op;
1293 c.common.nsid = cpu_to_le32(ns->ns_id);
1294 c.common.cdw10[0] = cpu_to_le32(cdw10);
1295
1296 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1297}
1298
1299static int nvme_pr_register(struct block_device *bdev, u64 old,
1300 u64 new, unsigned flags)
1301{
1302 u32 cdw10;
1303
1304 if (flags & ~PR_FL_IGNORE_KEY)
1305 return -EOPNOTSUPP;
1306
1307 cdw10 = old ? 2 : 0;
1308 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1309 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1310 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1311}
1312
1313static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1314 enum pr_type type, unsigned flags)
1315{
1316 u32 cdw10;
1317
1318 if (flags & ~PR_FL_IGNORE_KEY)
1319 return -EOPNOTSUPP;
1320
1321 cdw10 = nvme_pr_type(type) << 8;
1322 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1323 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1324}
1325
1326static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1327 enum pr_type type, bool abort)
1328{
1329 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1330 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1331}
1332
1333static int nvme_pr_clear(struct block_device *bdev, u64 key)
1334{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001335 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001336 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1337}
1338
1339static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1340{
1341 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1342 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1343}
1344
1345static const struct pr_ops nvme_pr_ops = {
1346 .pr_register = nvme_pr_register,
1347 .pr_reserve = nvme_pr_reserve,
1348 .pr_release = nvme_pr_release,
1349 .pr_preempt = nvme_pr_preempt,
1350 .pr_clear = nvme_pr_clear,
1351};
1352
Scott Bauera98e58e52017-02-03 12:50:32 -07001353#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001354int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1355 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001356{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001357 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001358 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001359
1360 memset(&cmd, 0, sizeof(cmd));
1361 if (send)
1362 cmd.common.opcode = nvme_admin_security_send;
1363 else
1364 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001365 cmd.common.nsid = 0;
1366 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1367 cmd.common.cdw10[1] = cpu_to_le32(len);
1368
1369 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1370 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1371}
1372EXPORT_SYMBOL_GPL(nvme_sec_submit);
1373#endif /* CONFIG_BLK_SED_OPAL */
1374
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001375static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001376 .owner = THIS_MODULE,
1377 .ioctl = nvme_ioctl,
1378 .compat_ioctl = nvme_compat_ioctl,
1379 .open = nvme_open,
1380 .release = nvme_release,
1381 .getgeo = nvme_getgeo,
1382 .revalidate_disk= nvme_revalidate_disk,
1383 .pr_ops = &nvme_pr_ops,
1384};
1385
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001386static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1387{
1388 unsigned long timeout =
1389 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1390 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1391 int ret;
1392
1393 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001394 if (csts == ~0)
1395 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001396 if ((csts & NVME_CSTS_RDY) == bit)
1397 break;
1398
1399 msleep(100);
1400 if (fatal_signal_pending(current))
1401 return -EINTR;
1402 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001403 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001404 "Device not ready; aborting %s\n", enabled ?
1405 "initialisation" : "reset");
1406 return -ENODEV;
1407 }
1408 }
1409
1410 return ret;
1411}
1412
1413/*
1414 * If the device has been passed off to us in an enabled state, just clear
1415 * the enabled bit. The spec says we should set the 'shutdown notification
1416 * bits', but doing so may cause the device to complete commands to the
1417 * admin queue ... and we don't know what memory that might be pointing at!
1418 */
1419int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1420{
1421 int ret;
1422
1423 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1424 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1425
1426 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1427 if (ret)
1428 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001429
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001430 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001431 msleep(NVME_QUIRK_DELAY_AMOUNT);
1432
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001433 return nvme_wait_ready(ctrl, cap, false);
1434}
Ming Lin576d55d2016-02-10 10:03:32 -08001435EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001436
1437int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1438{
1439 /*
1440 * Default to a 4K page size, with the intention to update this
1441 * path in the future to accomodate architectures with differing
1442 * kernel and IO page sizes.
1443 */
1444 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1445 int ret;
1446
1447 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001448 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001449 "Minimum device page size %u too large for host (%u)\n",
1450 1 << dev_page_min, 1 << page_shift);
1451 return -ENODEV;
1452 }
1453
1454 ctrl->page_size = 1 << page_shift;
1455
1456 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1457 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001458 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001459 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1460 ctrl->ctrl_config |= NVME_CC_ENABLE;
1461
1462 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1463 if (ret)
1464 return ret;
1465 return nvme_wait_ready(ctrl, cap, true);
1466}
Ming Lin576d55d2016-02-10 10:03:32 -08001467EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001468
1469int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1470{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001471 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001472 u32 csts;
1473 int ret;
1474
1475 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1476 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1477
1478 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1479 if (ret)
1480 return ret;
1481
1482 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1483 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1484 break;
1485
1486 msleep(100);
1487 if (fatal_signal_pending(current))
1488 return -EINTR;
1489 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001490 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001491 "Device shutdown incomplete; abort shutdown\n");
1492 return -ENODEV;
1493 }
1494 }
1495
1496 return ret;
1497}
Ming Lin576d55d2016-02-10 10:03:32 -08001498EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001499
Christoph Hellwigda358252016-03-02 18:07:11 +01001500static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1501 struct request_queue *q)
1502{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001503 bool vwc = false;
1504
Christoph Hellwigda358252016-03-02 18:07:11 +01001505 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001506 u32 max_segments =
1507 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1508
Christoph Hellwigda358252016-03-02 18:07:11 +01001509 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001510 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001511 }
Keith Busche6282ae2016-12-19 11:37:50 -05001512 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1513 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001514 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001515 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1516 vwc = true;
1517 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001518}
1519
Jon Derrickdbf86b32017-08-16 09:51:29 +02001520static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1521{
1522 __le64 ts;
1523 int ret;
1524
1525 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1526 return 0;
1527
1528 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1529 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1530 NULL);
1531 if (ret)
1532 dev_warn_once(ctrl->device,
1533 "could not set timestamp (%d)\n", ret);
1534 return ret;
1535}
1536
Keith Busch634b8322017-08-10 11:23:31 +02001537static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001538{
1539 /*
1540 * APST (Autonomous Power State Transition) lets us program a
1541 * table of power state transitions that the controller will
1542 * perform automatically. We configure it with a simple
1543 * heuristic: we are willing to spend at most 2% of the time
1544 * transitioning between power states. Therefore, when running
1545 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001546 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001547 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001548 * the requested maximum latency.
1549 *
1550 * We will not autonomously enter any non-operational state for
1551 * which the total latency exceeds ps_max_latency_us. Users
1552 * can set ps_max_latency_us to zero to turn off APST.
1553 */
1554
1555 unsigned apste;
1556 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001557 u64 max_lat_us = 0;
1558 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001559 int ret;
1560
1561 /*
1562 * If APST isn't supported or if we haven't been initialized yet,
1563 * then don't do anything.
1564 */
1565 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001566 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001567
1568 if (ctrl->npss > 31) {
1569 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001570 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001571 }
1572
1573 table = kzalloc(sizeof(*table), GFP_KERNEL);
1574 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001575 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001576
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001577 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001578 /* Turn off APST. */
1579 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001580 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001581 } else {
1582 __le64 target = cpu_to_le64(0);
1583 int state;
1584
1585 /*
1586 * Walk through all states from lowest- to highest-power.
1587 * According to the spec, lower-numbered states use more
1588 * power. NPSS, despite the name, is the index of the
1589 * lowest-power state, not the number of states.
1590 */
1591 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001592 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001593
1594 if (target)
1595 table->entries[state] = target;
1596
1597 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001598 * Don't allow transitions to the deepest state
1599 * if it's quirked off.
1600 */
1601 if (state == ctrl->npss &&
1602 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1603 continue;
1604
1605 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001606 * Is this state a useful non-operational state for
1607 * higher-power states to autonomously transition to?
1608 */
1609 if (!(ctrl->psd[state].flags &
1610 NVME_PS_FLAGS_NON_OP_STATE))
1611 continue;
1612
Kai-Heng Fengda875912017-06-07 15:25:42 +08001613 exit_latency_us =
1614 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1615 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001616 continue;
1617
Kai-Heng Fengda875912017-06-07 15:25:42 +08001618 total_latency_us =
1619 exit_latency_us +
1620 le32_to_cpu(ctrl->psd[state].entry_lat);
1621
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001622 /*
1623 * This state is good. Use it as the APST idle
1624 * target for higher power states.
1625 */
1626 transition_ms = total_latency_us + 19;
1627 do_div(transition_ms, 20);
1628 if (transition_ms > (1 << 24) - 1)
1629 transition_ms = (1 << 24) - 1;
1630
1631 target = cpu_to_le64((state << 3) |
1632 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001633
1634 if (max_ps == -1)
1635 max_ps = state;
1636
1637 if (total_latency_us > max_lat_us)
1638 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001639 }
1640
1641 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001642
1643 if (max_ps == -1) {
1644 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1645 } else {
1646 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1647 max_ps, max_lat_us, (int)sizeof(*table), table);
1648 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001649 }
1650
1651 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1652 table, sizeof(*table), NULL);
1653 if (ret)
1654 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1655
1656 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001657 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001658}
1659
1660static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1661{
1662 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1663 u64 latency;
1664
1665 switch (val) {
1666 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1667 case PM_QOS_LATENCY_ANY:
1668 latency = U64_MAX;
1669 break;
1670
1671 default:
1672 latency = val;
1673 }
1674
1675 if (ctrl->ps_max_latency_us != latency) {
1676 ctrl->ps_max_latency_us = latency;
1677 nvme_configure_apst(ctrl);
1678 }
1679}
1680
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001681struct nvme_core_quirk_entry {
1682 /*
1683 * NVMe model and firmware strings are padded with spaces. For
1684 * simplicity, strings in the quirk table are padded with NULLs
1685 * instead.
1686 */
1687 u16 vid;
1688 const char *mn;
1689 const char *fr;
1690 unsigned long quirks;
1691};
1692
1693static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001694 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001695 /*
1696 * This Toshiba device seems to die using any APST states. See:
1697 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1698 */
1699 .vid = 0x1179,
1700 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001701 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001702 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001703};
1704
1705/* match is null-terminated but idstr is space-padded. */
1706static bool string_matches(const char *idstr, const char *match, size_t len)
1707{
1708 size_t matchlen;
1709
1710 if (!match)
1711 return true;
1712
1713 matchlen = strlen(match);
1714 WARN_ON_ONCE(matchlen > len);
1715
1716 if (memcmp(idstr, match, matchlen))
1717 return false;
1718
1719 for (; matchlen < len; matchlen++)
1720 if (idstr[matchlen] != ' ')
1721 return false;
1722
1723 return true;
1724}
1725
1726static bool quirk_matches(const struct nvme_id_ctrl *id,
1727 const struct nvme_core_quirk_entry *q)
1728{
1729 return q->vid == le16_to_cpu(id->vid) &&
1730 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1731 string_matches(id->fr, q->fr, sizeof(id->fr));
1732}
1733
Christoph Hellwig180de0072017-06-26 12:39:02 +02001734static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1735{
1736 size_t nqnlen;
1737 int off;
1738
1739 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1740 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1741 strcpy(ctrl->subnqn, id->subnqn);
1742 return;
1743 }
1744
1745 if (ctrl->vs >= NVME_VS(1, 2, 1))
1746 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1747
1748 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1749 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1750 "nqn.2014.08.org.nvmexpress:%4x%4x",
1751 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1752 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1753 off += sizeof(id->sn);
1754 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1755 off += sizeof(id->mn);
1756 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1757}
1758
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001759/*
1760 * Initialize the cached copies of the Identify data and various controller
1761 * register in our nvme_ctrl structure. This should be called as soon as
1762 * the admin queue is fully up and running.
1763 */
1764int nvme_init_identify(struct nvme_ctrl *ctrl)
1765{
1766 struct nvme_id_ctrl *id;
1767 u64 cap;
1768 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001769 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001770 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001771
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001772 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1773 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001774 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001775 return ret;
1776 }
1777
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001778 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1779 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001780 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001781 return ret;
1782 }
1783 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1784
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001785 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001786 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1787
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001788 ret = nvme_identify_ctrl(ctrl, &id);
1789 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001790 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001791 return -EIO;
1792 }
1793
Christoph Hellwig180de0072017-06-26 12:39:02 +02001794 nvme_init_subnqn(ctrl, id);
1795
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001796 if (!ctrl->identified) {
1797 /*
1798 * Check for quirks. Quirk can depend on firmware version,
1799 * so, in principle, the set of quirks present can change
1800 * across a reset. As a possible future enhancement, we
1801 * could re-scan for quirks every time we reinitialize
1802 * the device, but we'd have to make sure that the driver
1803 * behaves intelligently if the quirks change.
1804 */
1805
1806 int i;
1807
1808 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1809 if (quirk_matches(id, &core_quirks[i]))
1810 ctrl->quirks |= core_quirks[i].quirks;
1811 }
1812 }
1813
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001814 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001815 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 -07001816 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1817 }
1818
Scott Bauer8a9ae522017-02-17 13:59:40 +01001819 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001820 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001821 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001822 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001823 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001824 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001825 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1826 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1827 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1828 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001829 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001830 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001831 max_hw_sectors = UINT_MAX;
1832 ctrl->max_hw_sectors =
1833 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001834
Christoph Hellwigda358252016-03-02 18:07:11 +01001835 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001836 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001837 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001838
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001839 if (id->rtd3e) {
1840 /* us -> s */
1841 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1842
1843 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1844 shutdown_timeout, 60);
1845
1846 if (ctrl->shutdown_timeout != shutdown_timeout)
1847 dev_warn(ctrl->device,
1848 "Shutdown timeout set to %u seconds\n",
1849 ctrl->shutdown_timeout);
1850 } else
1851 ctrl->shutdown_timeout = shutdown_timeout;
1852
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001853 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001854 ctrl->apsta = id->apsta;
1855 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001856 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1857 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001858 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 -04001859 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001860 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001861 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001862 }
1863 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001864 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001865 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001866 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1867
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001868 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001869 ctrl->icdoff = le16_to_cpu(id->icdoff);
1870 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1871 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1872 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1873
1874 /*
1875 * In fabrics we need to verify the cntlid matches the
1876 * admin connect
1877 */
Keith Busch634b8322017-08-10 11:23:31 +02001878 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001879 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001880 goto out_free;
1881 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001882
1883 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001884 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001885 "keep-alive support is mandatory for fabrics\n");
1886 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001887 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001888 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001889 } else {
1890 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001891 ctrl->hmpre = le32_to_cpu(id->hmpre);
1892 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001893 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001894
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001895 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001896
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001897 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001898 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001899 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001900 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1901
Keith Busch634b8322017-08-10 11:23:31 +02001902 ret = nvme_configure_apst(ctrl);
1903 if (ret < 0)
1904 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001905
1906 ret = nvme_configure_timestamp(ctrl);
1907 if (ret < 0)
1908 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001909
1910 ret = nvme_configure_directives(ctrl);
1911 if (ret < 0)
1912 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001913
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001914 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001915
Keith Busch634b8322017-08-10 11:23:31 +02001916 return 0;
1917
1918out_free:
1919 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001920 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001921}
Ming Lin576d55d2016-02-10 10:03:32 -08001922EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001923
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001924static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001925{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001926 struct nvme_ctrl *ctrl;
1927 int instance = iminor(inode);
1928 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001929
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001930 spin_lock(&dev_list_lock);
1931 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1932 if (ctrl->instance != instance)
1933 continue;
1934
1935 if (!ctrl->admin_q) {
1936 ret = -EWOULDBLOCK;
1937 break;
1938 }
1939 if (!kref_get_unless_zero(&ctrl->kref))
1940 break;
1941 file->private_data = ctrl;
1942 ret = 0;
1943 break;
1944 }
1945 spin_unlock(&dev_list_lock);
1946
1947 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001948}
1949
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001950static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001951{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001952 nvme_put_ctrl(file->private_data);
1953 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001954}
1955
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001956static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1957{
1958 struct nvme_ns *ns;
1959 int ret;
1960
1961 mutex_lock(&ctrl->namespaces_mutex);
1962 if (list_empty(&ctrl->namespaces)) {
1963 ret = -ENOTTY;
1964 goto out_unlock;
1965 }
1966
1967 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1968 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001969 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001970 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1971 ret = -EINVAL;
1972 goto out_unlock;
1973 }
1974
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001975 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001976 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1977 kref_get(&ns->kref);
1978 mutex_unlock(&ctrl->namespaces_mutex);
1979
1980 ret = nvme_user_cmd(ctrl, ns, argp);
1981 nvme_put_ns(ns);
1982 return ret;
1983
1984out_unlock:
1985 mutex_unlock(&ctrl->namespaces_mutex);
1986 return ret;
1987}
1988
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001989static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1990 unsigned long arg)
1991{
1992 struct nvme_ctrl *ctrl = file->private_data;
1993 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001994
1995 switch (cmd) {
1996 case NVME_IOCTL_ADMIN_CMD:
1997 return nvme_user_cmd(ctrl, NULL, argp);
1998 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001999 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002000 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002001 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002002 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002003 case NVME_IOCTL_SUBSYS_RESET:
2004 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002005 case NVME_IOCTL_RESCAN:
2006 nvme_queue_scan(ctrl);
2007 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002008 default:
2009 return -ENOTTY;
2010 }
2011}
2012
2013static const struct file_operations nvme_dev_fops = {
2014 .owner = THIS_MODULE,
2015 .open = nvme_dev_open,
2016 .release = nvme_dev_release,
2017 .unlocked_ioctl = nvme_dev_ioctl,
2018 .compat_ioctl = nvme_dev_ioctl,
2019};
2020
2021static ssize_t nvme_sysfs_reset(struct device *dev,
2022 struct device_attribute *attr, const char *buf,
2023 size_t count)
2024{
2025 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2026 int ret;
2027
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002028 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002029 if (ret < 0)
2030 return ret;
2031 return count;
2032}
2033static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2034
Keith Busch9ec3bb22016-04-29 15:45:18 -06002035static ssize_t nvme_sysfs_rescan(struct device *dev,
2036 struct device_attribute *attr, const char *buf,
2037 size_t count)
2038{
2039 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2040
2041 nvme_queue_scan(ctrl);
2042 return count;
2043}
2044static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2045
Keith Busch118472a2016-02-18 09:57:48 -07002046static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2047 char *buf)
2048{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002049 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002050 struct nvme_ctrl *ctrl = ns->ctrl;
2051 int serial_len = sizeof(ctrl->serial);
2052 int model_len = sizeof(ctrl->model);
2053
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002054 if (!uuid_is_null(&ns->uuid))
2055 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2056
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002057 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2058 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002059
2060 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2061 return sprintf(buf, "eui.%8phN\n", ns->eui);
2062
Martin Wilck758f3732017-07-20 18:34:02 +02002063 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2064 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002065 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002066 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2067 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002068 model_len--;
2069
2070 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2071 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2072}
2073static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2074
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002075static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2076 char *buf)
2077{
2078 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2079 return sprintf(buf, "%pU\n", ns->nguid);
2080}
2081static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2082
Keith Busch2b9b6e82015-12-22 10:10:45 -07002083static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2084 char *buf)
2085{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002086 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002087
2088 /* For backward compatibility expose the NGUID to userspace if
2089 * we have no UUID set
2090 */
2091 if (uuid_is_null(&ns->uuid)) {
2092 printk_ratelimited(KERN_WARNING
2093 "No UUID available providing old NGUID\n");
2094 return sprintf(buf, "%pU\n", ns->nguid);
2095 }
2096 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002097}
2098static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2099
2100static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2101 char *buf)
2102{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002103 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002104 return sprintf(buf, "%8phd\n", ns->eui);
2105}
2106static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2107
2108static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2109 char *buf)
2110{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002111 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002112 return sprintf(buf, "%d\n", ns->ns_id);
2113}
2114static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2115
2116static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002117 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002118 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002119 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002120 &dev_attr_eui.attr,
2121 &dev_attr_nsid.attr,
2122 NULL,
2123};
2124
Ming Lin1a353d82016-06-13 16:45:24 +02002125static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002126 struct attribute *a, int n)
2127{
2128 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002129 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002130
2131 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002132 if (uuid_is_null(&ns->uuid) ||
2133 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2134 return 0;
2135 }
2136 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002137 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002138 return 0;
2139 }
2140 if (a == &dev_attr_eui.attr) {
2141 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2142 return 0;
2143 }
2144 return a->mode;
2145}
2146
2147static const struct attribute_group nvme_ns_attr_group = {
2148 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002149 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002150};
2151
Ming Lin931e1c22016-02-26 13:24:19 -08002152#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002153static ssize_t field##_show(struct device *dev, \
2154 struct device_attribute *attr, char *buf) \
2155{ \
2156 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2157 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2158} \
2159static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2160
Ming Lin931e1c22016-02-26 13:24:19 -08002161#define nvme_show_int_function(field) \
2162static ssize_t field##_show(struct device *dev, \
2163 struct device_attribute *attr, char *buf) \
2164{ \
2165 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2166 return sprintf(buf, "%d\n", ctrl->field); \
2167} \
2168static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2169
2170nvme_show_str_function(model);
2171nvme_show_str_function(serial);
2172nvme_show_str_function(firmware_rev);
2173nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002174
Ming Lin1a353d82016-06-13 16:45:24 +02002175static ssize_t nvme_sysfs_delete(struct device *dev,
2176 struct device_attribute *attr, const char *buf,
2177 size_t count)
2178{
2179 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2180
2181 if (device_remove_file_self(dev, attr))
2182 ctrl->ops->delete_ctrl(ctrl);
2183 return count;
2184}
2185static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2186
2187static ssize_t nvme_sysfs_show_transport(struct device *dev,
2188 struct device_attribute *attr,
2189 char *buf)
2190{
2191 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2192
2193 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2194}
2195static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2196
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002197static ssize_t nvme_sysfs_show_state(struct device *dev,
2198 struct device_attribute *attr,
2199 char *buf)
2200{
2201 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2202 static const char *const state_name[] = {
2203 [NVME_CTRL_NEW] = "new",
2204 [NVME_CTRL_LIVE] = "live",
2205 [NVME_CTRL_RESETTING] = "resetting",
2206 [NVME_CTRL_RECONNECTING]= "reconnecting",
2207 [NVME_CTRL_DELETING] = "deleting",
2208 [NVME_CTRL_DEAD] = "dead",
2209 };
2210
2211 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2212 state_name[ctrl->state])
2213 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2214
2215 return sprintf(buf, "unknown state\n");
2216}
2217
2218static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2219
Ming Lin1a353d82016-06-13 16:45:24 +02002220static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2221 struct device_attribute *attr,
2222 char *buf)
2223{
2224 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2225
Christoph Hellwig180de0072017-06-26 12:39:02 +02002226 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002227}
2228static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2229
2230static ssize_t nvme_sysfs_show_address(struct device *dev,
2231 struct device_attribute *attr,
2232 char *buf)
2233{
2234 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2235
2236 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2237}
2238static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2239
Keith Busch779ff7562016-01-12 15:09:31 -07002240static struct attribute *nvme_dev_attrs[] = {
2241 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002242 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002243 &dev_attr_model.attr,
2244 &dev_attr_serial.attr,
2245 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002246 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002247 &dev_attr_delete_controller.attr,
2248 &dev_attr_transport.attr,
2249 &dev_attr_subsysnqn.attr,
2250 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002251 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002252 NULL
2253};
2254
Ming Lin1a353d82016-06-13 16:45:24 +02002255static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2256 struct attribute *a, int n)
2257{
2258 struct device *dev = container_of(kobj, struct device, kobj);
2259 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2260
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002261 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2262 return 0;
2263 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2264 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002265
2266 return a->mode;
2267}
2268
Keith Busch779ff7562016-01-12 15:09:31 -07002269static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002270 .attrs = nvme_dev_attrs,
2271 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002272};
2273
2274static const struct attribute_group *nvme_dev_attr_groups[] = {
2275 &nvme_dev_attrs_group,
2276 NULL,
2277};
2278
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002279static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2280{
2281 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2282 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2283
2284 return nsa->ns_id - nsb->ns_id;
2285}
2286
Keith Busch32f0c4a2016-07-13 11:45:02 -06002287static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002288{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002289 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002290
Keith Busch32f0c4a2016-07-13 11:45:02 -06002291 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002292 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002293 if (ns->ns_id == nsid) {
2294 kref_get(&ns->kref);
2295 ret = ns;
2296 break;
2297 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002298 if (ns->ns_id > nsid)
2299 break;
2300 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002301 mutex_unlock(&ctrl->namespaces_mutex);
2302 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002303}
2304
Jens Axboef5d11842017-06-27 12:03:06 -06002305static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2306{
2307 struct streams_directive_params s;
2308 int ret;
2309
2310 if (!ctrl->nr_streams)
2311 return 0;
2312
2313 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2314 if (ret)
2315 return ret;
2316
2317 ns->sws = le32_to_cpu(s.sws);
2318 ns->sgs = le16_to_cpu(s.sgs);
2319
2320 if (ns->sws) {
2321 unsigned int bs = 1 << ns->lba_shift;
2322
2323 blk_queue_io_min(ns->queue, bs * ns->sws);
2324 if (ns->sgs)
2325 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2326 }
2327
2328 return 0;
2329}
2330
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002331static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2332{
2333 struct nvme_ns *ns;
2334 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002335 struct nvme_id_ns *id;
2336 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002337 int node = dev_to_node(ctrl->dev);
2338
2339 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2340 if (!ns)
2341 return;
2342
Keith Busch075790e2016-02-24 09:15:53 -07002343 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2344 if (ns->instance < 0)
2345 goto out_free_ns;
2346
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002347 ns->queue = blk_mq_init_queue(ctrl->tagset);
2348 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002349 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002350 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2351 ns->queue->queuedata = ns;
2352 ns->ctrl = ctrl;
2353
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002354 kref_init(&ns->kref);
2355 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002356 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002357
2358 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002359 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002360 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002361
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002362 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002363
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002364 if (nvme_revalidate_ns(ns, &id))
2365 goto out_free_queue;
2366
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002367 if (nvme_nvm_ns_supported(ns, id) &&
2368 nvme_nvm_register(ns, disk_name, node)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002369 dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002370 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002371 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002372
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002373 disk = alloc_disk_node(0, node);
2374 if (!disk)
2375 goto out_free_id;
2376
2377 disk->fops = &nvme_fops;
2378 disk->private_data = ns;
2379 disk->queue = ns->queue;
2380 disk->flags = GENHD_FL_EXT_DEVT;
2381 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2382 ns->disk = disk;
2383
2384 __nvme_revalidate_disk(disk, id);
2385
Keith Busch32f0c4a2016-07-13 11:45:02 -06002386 mutex_lock(&ctrl->namespaces_mutex);
2387 list_add_tail(&ns->list, &ctrl->namespaces);
2388 mutex_unlock(&ctrl->namespaces_mutex);
2389
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002390 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002391
2392 kfree(id);
2393
Dan Williams0d52c7562016-06-15 19:44:20 -07002394 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002395 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2396 &nvme_ns_attr_group))
2397 pr_warn("%s: failed to create sysfs group for identification\n",
2398 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002399 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2400 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2401 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002402 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002403 out_free_id:
2404 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002405 out_free_queue:
2406 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002407 out_release_instance:
2408 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002409 out_free_ns:
2410 kfree(ns);
2411}
2412
2413static void nvme_ns_remove(struct nvme_ns *ns)
2414{
Keith Busch646017a2016-02-24 09:15:54 -07002415 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2416 return;
2417
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002418 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002419 if (blk_get_integrity(ns->disk))
2420 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002421 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2422 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002423 if (ns->ndev)
2424 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002425 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002426 blk_cleanup_queue(ns->queue);
2427 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002428
2429 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002430 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002431 mutex_unlock(&ns->ctrl->namespaces_mutex);
2432
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002433 nvme_put_ns(ns);
2434}
2435
Keith Busch540c8012015-10-22 15:45:06 -06002436static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2437{
2438 struct nvme_ns *ns;
2439
Keith Busch32f0c4a2016-07-13 11:45:02 -06002440 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002441 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002442 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002443 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002444 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002445 } else
2446 nvme_alloc_ns(ctrl, nsid);
2447}
2448
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302449static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2450 unsigned nsid)
2451{
2452 struct nvme_ns *ns, *next;
2453
2454 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2455 if (ns->ns_id > nsid)
2456 nvme_ns_remove(ns);
2457 }
2458}
2459
Keith Busch540c8012015-10-22 15:45:06 -06002460static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2461{
2462 struct nvme_ns *ns;
2463 __le32 *ns_list;
2464 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2465 int ret = 0;
2466
2467 ns_list = kzalloc(0x1000, GFP_KERNEL);
2468 if (!ns_list)
2469 return -ENOMEM;
2470
2471 for (i = 0; i < num_lists; i++) {
2472 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2473 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302474 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002475
2476 for (j = 0; j < min(nn, 1024U); j++) {
2477 nsid = le32_to_cpu(ns_list[j]);
2478 if (!nsid)
2479 goto out;
2480
2481 nvme_validate_ns(ctrl, nsid);
2482
2483 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002484 ns = nvme_find_get_ns(ctrl, prev);
2485 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002486 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002487 nvme_put_ns(ns);
2488 }
Keith Busch540c8012015-10-22 15:45:06 -06002489 }
2490 }
2491 nn -= j;
2492 }
2493 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302494 nvme_remove_invalid_namespaces(ctrl, prev);
2495 free:
Keith Busch540c8012015-10-22 15:45:06 -06002496 kfree(ns_list);
2497 return ret;
2498}
2499
Christoph Hellwig5955be22016-04-26 13:51:59 +02002500static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002501{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002502 unsigned i;
2503
Keith Busch540c8012015-10-22 15:45:06 -06002504 for (i = 1; i <= nn; i++)
2505 nvme_validate_ns(ctrl, i);
2506
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302507 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002508}
2509
Christoph Hellwig5955be22016-04-26 13:51:59 +02002510static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002511{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002512 struct nvme_ctrl *ctrl =
2513 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002514 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002515 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002516
Christoph Hellwig5955be22016-04-26 13:51:59 +02002517 if (ctrl->state != NVME_CTRL_LIVE)
2518 return;
2519
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002520 if (nvme_identify_ctrl(ctrl, &id))
2521 return;
Keith Busch540c8012015-10-22 15:45:06 -06002522
2523 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002524 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002525 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2526 if (!nvme_scan_ns_list(ctrl, nn))
2527 goto done;
2528 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002529 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002530 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002531 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002532 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002533 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002534 kfree(id);
2535}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002536
2537void nvme_queue_scan(struct nvme_ctrl *ctrl)
2538{
2539 /*
2540 * Do not queue new scan work when a controller is reset during
2541 * removal.
2542 */
2543 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002544 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002545}
2546EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002547
Keith Busch32f0c4a2016-07-13 11:45:02 -06002548/*
2549 * This function iterates the namespace list unlocked to allow recovery from
2550 * controller failure. It is up to the caller to ensure the namespace list is
2551 * not modified by scan work while this function is executing.
2552 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002553void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2554{
2555 struct nvme_ns *ns, *next;
2556
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002557 /*
2558 * The dead states indicates the controller was not gracefully
2559 * disconnected. In that case, we won't be able to flush any data while
2560 * removing the namespaces' disks; fail all the queues now to avoid
2561 * potentially having to clean up the failed sync later.
2562 */
2563 if (ctrl->state == NVME_CTRL_DEAD)
2564 nvme_kill_queues(ctrl);
2565
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002566 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2567 nvme_ns_remove(ns);
2568}
Ming Lin576d55d2016-02-10 10:03:32 -08002569EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002570
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002571static void nvme_async_event_work(struct work_struct *work)
2572{
2573 struct nvme_ctrl *ctrl =
2574 container_of(work, struct nvme_ctrl, async_event_work);
2575
2576 spin_lock_irq(&ctrl->lock);
2577 while (ctrl->event_limit > 0) {
2578 int aer_idx = --ctrl->event_limit;
2579
2580 spin_unlock_irq(&ctrl->lock);
2581 ctrl->ops->submit_async_event(ctrl, aer_idx);
2582 spin_lock_irq(&ctrl->lock);
2583 }
2584 spin_unlock_irq(&ctrl->lock);
2585}
2586
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302587static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2588{
2589
2590 u32 csts;
2591
2592 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2593 return false;
2594
2595 if (csts == ~0)
2596 return false;
2597
2598 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2599}
2600
2601static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2602{
2603 struct nvme_command c = { };
2604 struct nvme_fw_slot_info_log *log;
2605
2606 log = kmalloc(sizeof(*log), GFP_KERNEL);
2607 if (!log)
2608 return;
2609
2610 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302611 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302612 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2613
2614 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2615 dev_warn(ctrl->device,
2616 "Get FW SLOT INFO log error\n");
2617 kfree(log);
2618}
2619
2620static void nvme_fw_act_work(struct work_struct *work)
2621{
2622 struct nvme_ctrl *ctrl = container_of(work,
2623 struct nvme_ctrl, fw_act_work);
2624 unsigned long fw_act_timeout;
2625
2626 if (ctrl->mtfa)
2627 fw_act_timeout = jiffies +
2628 msecs_to_jiffies(ctrl->mtfa * 100);
2629 else
2630 fw_act_timeout = jiffies +
2631 msecs_to_jiffies(admin_timeout * 1000);
2632
2633 nvme_stop_queues(ctrl);
2634 while (nvme_ctrl_pp_status(ctrl)) {
2635 if (time_after(jiffies, fw_act_timeout)) {
2636 dev_warn(ctrl->device,
2637 "Fw activation timeout, reset controller\n");
2638 nvme_reset_ctrl(ctrl);
2639 break;
2640 }
2641 msleep(100);
2642 }
2643
2644 if (ctrl->state != NVME_CTRL_LIVE)
2645 return;
2646
2647 nvme_start_queues(ctrl);
2648 /* read FW slot informationi to clear the AER*/
2649 nvme_get_fw_slot_info(ctrl);
2650}
2651
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002652void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2653 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002654{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002655 u32 result = le32_to_cpu(res->u32);
2656 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002657
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002658 switch (le16_to_cpu(status) >> 1) {
2659 case NVME_SC_SUCCESS:
2660 done = false;
2661 /*FALLTHRU*/
2662 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002663 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002664 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002665 break;
2666 default:
2667 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002668 }
2669
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002670 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002671 return;
2672
2673 switch (result & 0xff07) {
2674 case NVME_AER_NOTICE_NS_CHANGED:
2675 dev_info(ctrl->device, "rescanning\n");
2676 nvme_queue_scan(ctrl);
2677 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302678 case NVME_AER_NOTICE_FW_ACT_STARTING:
2679 schedule_work(&ctrl->fw_act_work);
2680 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002681 default:
2682 dev_warn(ctrl->device, "async event result %08x\n", result);
2683 }
2684}
2685EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2686
2687void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2688{
2689 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002690 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002691}
2692EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2693
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002694static DEFINE_IDA(nvme_instance_ida);
2695
2696static int nvme_set_instance(struct nvme_ctrl *ctrl)
2697{
2698 int instance, error;
2699
2700 do {
2701 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2702 return -ENODEV;
2703
2704 spin_lock(&dev_list_lock);
2705 error = ida_get_new(&nvme_instance_ida, &instance);
2706 spin_unlock(&dev_list_lock);
2707 } while (error == -EAGAIN);
2708
2709 if (error)
2710 return -ENODEV;
2711
2712 ctrl->instance = instance;
2713 return 0;
2714}
2715
2716static void nvme_release_instance(struct nvme_ctrl *ctrl)
2717{
2718 spin_lock(&dev_list_lock);
2719 ida_remove(&nvme_instance_ida, ctrl->instance);
2720 spin_unlock(&dev_list_lock);
2721}
2722
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002723void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002724{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002725 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002726 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002727 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302728 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002729}
2730EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002731
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002732void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2733{
2734 if (ctrl->kato)
2735 nvme_start_keep_alive(ctrl);
2736
2737 if (ctrl->queue_count > 1) {
2738 nvme_queue_scan(ctrl);
2739 nvme_queue_async_events(ctrl);
2740 nvme_start_queues(ctrl);
2741 }
2742}
2743EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2744
2745void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2746{
Keith Busch53029b02015-11-28 15:41:02 +01002747 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002748
2749 spin_lock(&dev_list_lock);
2750 list_del(&ctrl->node);
2751 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002752}
Ming Lin576d55d2016-02-10 10:03:32 -08002753EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002754
2755static void nvme_free_ctrl(struct kref *kref)
2756{
2757 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002758
2759 put_device(ctrl->device);
2760 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002761 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002762
2763 ctrl->ops->free_ctrl(ctrl);
2764}
2765
2766void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2767{
2768 kref_put(&ctrl->kref, nvme_free_ctrl);
2769}
Ming Lin576d55d2016-02-10 10:03:32 -08002770EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002771
2772/*
2773 * Initialize a NVMe controller structures. This needs to be called during
2774 * earliest initialization so that we have the initialized structured around
2775 * during probing.
2776 */
2777int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2778 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2779{
2780 int ret;
2781
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002782 ctrl->state = NVME_CTRL_NEW;
2783 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002784 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002785 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002786 kref_init(&ctrl->kref);
2787 ctrl->dev = dev;
2788 ctrl->ops = ops;
2789 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002790 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002791 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302792 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002793
2794 ret = nvme_set_instance(ctrl);
2795 if (ret)
2796 goto out;
2797
Keith Busch779ff7562016-01-12 15:09:31 -07002798 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002799 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002800 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002801 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002802 if (IS_ERR(ctrl->device)) {
2803 ret = PTR_ERR(ctrl->device);
2804 goto out_release_instance;
2805 }
2806 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002807 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002808
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002809 spin_lock(&dev_list_lock);
2810 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2811 spin_unlock(&dev_list_lock);
2812
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002813 /*
2814 * Initialize latency tolerance controls. The sysfs files won't
2815 * be visible to userspace unless the device actually supports APST.
2816 */
2817 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2818 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2819 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2820
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002821 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002822out_release_instance:
2823 nvme_release_instance(ctrl);
2824out:
2825 return ret;
2826}
Ming Lin576d55d2016-02-10 10:03:32 -08002827EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002828
Keith Busch69d9a992016-02-24 09:15:56 -07002829/**
2830 * nvme_kill_queues(): Ends all namespace queues
2831 * @ctrl: the dead controller that needs to end
2832 *
2833 * Call this function when the driver determines it is unable to get the
2834 * controller in a state capable of servicing IO.
2835 */
2836void nvme_kill_queues(struct nvme_ctrl *ctrl)
2837{
2838 struct nvme_ns *ns;
2839
Keith Busch32f0c4a2016-07-13 11:45:02 -06002840 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002841
Ming Lei443bd902017-06-19 10:21:08 +08002842 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002843 if (ctrl->admin_q)
2844 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002845
Keith Busch32f0c4a2016-07-13 11:45:02 -06002846 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002847 /*
2848 * Revalidating a dead namespace sets capacity to 0. This will
2849 * end buffered writers dirtying pages that can't be synced.
2850 */
Keith Buschf33447b2017-02-10 18:15:51 -05002851 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2852 continue;
2853 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002854 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002855
Ming Lei443bd902017-06-19 10:21:08 +08002856 /* Forcibly unquiesce queues to avoid blocking dispatch */
2857 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002858 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002859 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002860}
Linus Torvalds237045f2016-03-18 17:13:31 -07002861EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002862
Keith Busch302ad8c2017-03-01 14:22:12 -05002863void nvme_unfreeze(struct nvme_ctrl *ctrl)
2864{
2865 struct nvme_ns *ns;
2866
2867 mutex_lock(&ctrl->namespaces_mutex);
2868 list_for_each_entry(ns, &ctrl->namespaces, list)
2869 blk_mq_unfreeze_queue(ns->queue);
2870 mutex_unlock(&ctrl->namespaces_mutex);
2871}
2872EXPORT_SYMBOL_GPL(nvme_unfreeze);
2873
2874void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2875{
2876 struct nvme_ns *ns;
2877
2878 mutex_lock(&ctrl->namespaces_mutex);
2879 list_for_each_entry(ns, &ctrl->namespaces, list) {
2880 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2881 if (timeout <= 0)
2882 break;
2883 }
2884 mutex_unlock(&ctrl->namespaces_mutex);
2885}
2886EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2887
2888void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2889{
2890 struct nvme_ns *ns;
2891
2892 mutex_lock(&ctrl->namespaces_mutex);
2893 list_for_each_entry(ns, &ctrl->namespaces, list)
2894 blk_mq_freeze_queue_wait(ns->queue);
2895 mutex_unlock(&ctrl->namespaces_mutex);
2896}
2897EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2898
2899void nvme_start_freeze(struct nvme_ctrl *ctrl)
2900{
2901 struct nvme_ns *ns;
2902
2903 mutex_lock(&ctrl->namespaces_mutex);
2904 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002905 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002906 mutex_unlock(&ctrl->namespaces_mutex);
2907}
2908EXPORT_SYMBOL_GPL(nvme_start_freeze);
2909
Keith Busch25646262016-01-04 09:10:57 -07002910void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002911{
2912 struct nvme_ns *ns;
2913
Keith Busch32f0c4a2016-07-13 11:45:02 -06002914 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002915 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002916 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002917 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002918}
Ming Lin576d55d2016-02-10 10:03:32 -08002919EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002920
Keith Busch25646262016-01-04 09:10:57 -07002921void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002922{
2923 struct nvme_ns *ns;
2924
Keith Busch32f0c4a2016-07-13 11:45:02 -06002925 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002926 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002927 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002928 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002929}
Ming Lin576d55d2016-02-10 10:03:32 -08002930EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002931
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002932int __init nvme_core_init(void)
2933{
2934 int result;
2935
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002936 nvme_wq = alloc_workqueue("nvme-wq",
2937 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2938 if (!nvme_wq)
2939 return -ENOMEM;
2940
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002941 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2942 &nvme_dev_fops);
2943 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002944 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002945 else if (result > 0)
2946 nvme_char_major = result;
2947
2948 nvme_class = class_create(THIS_MODULE, "nvme");
2949 if (IS_ERR(nvme_class)) {
2950 result = PTR_ERR(nvme_class);
2951 goto unregister_chrdev;
2952 }
2953
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002954 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002955
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002956unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002957 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002958destroy_wq:
2959 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002960 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002961}
2962
2963void nvme_core_exit(void)
2964{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002965 class_destroy(nvme_class);
2966 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002967 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002968}
Ming Lin576d55d2016-02-10 10:03:32 -08002969
2970MODULE_LICENSE("GPL");
2971MODULE_VERSION("1.0");
2972module_init(nvme_core_init);
2973module_exit(nvme_core_exit);