blob: 4c49ec4349bc0aeda92af67621fae309b08393a8 [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 Hellwigbb8d2612016-04-26 13:51:57 +0200179 bool changed = false;
180
181 spin_lock_irq(&ctrl->lock);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300182
183 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200184 switch (new_state) {
185 case NVME_CTRL_LIVE:
186 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200187 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200188 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900189 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200190 changed = true;
191 /* FALLTHRU */
192 default:
193 break;
194 }
195 break;
196 case NVME_CTRL_RESETTING:
197 switch (old_state) {
198 case NVME_CTRL_NEW:
199 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900200 changed = true;
201 /* FALLTHRU */
202 default:
203 break;
204 }
205 break;
206 case NVME_CTRL_RECONNECTING:
207 switch (old_state) {
208 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200209 changed = true;
210 /* FALLTHRU */
211 default:
212 break;
213 }
214 break;
215 case NVME_CTRL_DELETING:
216 switch (old_state) {
217 case NVME_CTRL_LIVE:
218 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900219 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200220 changed = true;
221 /* FALLTHRU */
222 default:
223 break;
224 }
225 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600226 case NVME_CTRL_DEAD:
227 switch (old_state) {
228 case NVME_CTRL_DELETING:
229 changed = true;
230 /* FALLTHRU */
231 default:
232 break;
233 }
234 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200235 default:
236 break;
237 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200238
239 if (changed)
240 ctrl->state = new_state;
241
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300242 spin_unlock_irq(&ctrl->lock);
243
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200244 return changed;
245}
246EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
247
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100248static void nvme_free_ns(struct kref *kref)
249{
250 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
251
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200252 if (ns->ndev)
253 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100254
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200255 if (ns->disk) {
256 spin_lock(&dev_list_lock);
257 ns->disk->private_data = NULL;
258 spin_unlock(&dev_list_lock);
259 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100260
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100261 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700262 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
263 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100264 kfree(ns);
265}
266
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100267static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100268{
269 kref_put(&ns->kref, nvme_free_ns);
270}
271
272static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
273{
274 struct nvme_ns *ns;
275
276 spin_lock(&dev_list_lock);
277 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800278 if (ns) {
279 if (!kref_get_unless_zero(&ns->kref))
280 goto fail;
281 if (!try_module_get(ns->ctrl->ops->module))
282 goto fail_put_ns;
283 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100284 spin_unlock(&dev_list_lock);
285
286 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800287
288fail_put_ns:
289 kref_put(&ns->kref, nvme_free_ns);
290fail:
291 spin_unlock(&dev_list_lock);
292 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100293}
294
Christoph Hellwig41609822015-11-20 09:00:02 +0100295struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200296 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100297{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100298 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100299 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100300
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200301 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100302 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200303 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100304 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200305 qid ? qid - 1 : 0);
306 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100307 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100308 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100309
Christoph Hellwig21d34712015-11-26 09:08:36 +0100310 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800311 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100312
Christoph Hellwig41609822015-11-20 09:00:02 +0100313 return req;
314}
Ming Lin576d55d2016-02-10 10:03:32 -0800315EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100316
Jens Axboef5d11842017-06-27 12:03:06 -0600317static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
318{
319 struct nvme_command c;
320
321 memset(&c, 0, sizeof(c));
322
323 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530324 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600325 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
326 c.directive.dtype = NVME_DIR_IDENTIFY;
327 c.directive.tdtype = NVME_DIR_STREAMS;
328 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
329
330 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
331}
332
333static int nvme_disable_streams(struct nvme_ctrl *ctrl)
334{
335 return nvme_toggle_streams(ctrl, false);
336}
337
338static int nvme_enable_streams(struct nvme_ctrl *ctrl)
339{
340 return nvme_toggle_streams(ctrl, true);
341}
342
343static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
344 struct streams_directive_params *s, u32 nsid)
345{
346 struct nvme_command c;
347
348 memset(&c, 0, sizeof(c));
349 memset(s, 0, sizeof(*s));
350
351 c.directive.opcode = nvme_admin_directive_recv;
352 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700353 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600354 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
355 c.directive.dtype = NVME_DIR_STREAMS;
356
357 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
358}
359
360static int nvme_configure_directives(struct nvme_ctrl *ctrl)
361{
362 struct streams_directive_params s;
363 int ret;
364
365 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
366 return 0;
367 if (!streams)
368 return 0;
369
370 ret = nvme_enable_streams(ctrl);
371 if (ret)
372 return ret;
373
Arnav Dawn62346ea2017-07-12 16:11:53 +0530374 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600375 if (ret)
376 return ret;
377
378 ctrl->nssa = le16_to_cpu(s.nssa);
379 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
380 dev_info(ctrl->device, "too few streams (%u) available\n",
381 ctrl->nssa);
382 nvme_disable_streams(ctrl);
383 return 0;
384 }
385
386 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
387 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
388 return 0;
389}
390
391/*
392 * Check if 'req' has a write hint associated with it. If it does, assign
393 * a valid namespace stream to the write.
394 */
395static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
396 struct request *req, u16 *control,
397 u32 *dsmgmt)
398{
399 enum rw_hint streamid = req->write_hint;
400
401 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
402 streamid = 0;
403 else {
404 streamid--;
405 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
406 return;
407
408 *control |= NVME_RW_DTYPE_STREAMS;
409 *dsmgmt |= streamid << 16;
410 }
411
412 if (streamid < ARRAY_SIZE(req->q->write_hints))
413 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
414}
415
Ming Lin8093f7c2016-04-12 13:10:14 -0600416static inline void nvme_setup_flush(struct nvme_ns *ns,
417 struct nvme_command *cmnd)
418{
419 memset(cmnd, 0, sizeof(*cmnd));
420 cmnd->common.opcode = nvme_cmd_flush;
421 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
422}
423
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200424static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600425 struct nvme_command *cmnd)
426{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100427 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600428 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100429 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600430
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100431 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600432 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200433 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600434
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100435 __rq_for_each_bio(bio, req) {
436 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
437 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
438
439 range[n].cattr = cpu_to_le32(0);
440 range[n].nlb = cpu_to_le32(nlb);
441 range[n].slba = cpu_to_le64(slba);
442 n++;
443 }
444
445 if (WARN_ON_ONCE(n != segments)) {
446 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200447 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100448 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600449
450 memset(cmnd, 0, sizeof(*cmnd));
451 cmnd->dsm.opcode = nvme_cmd_dsm;
452 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200453 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600454 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
455
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700456 req->special_vec.bv_page = virt_to_page(range);
457 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100458 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700459 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600460
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200461 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600462}
Ming Lin8093f7c2016-04-12 13:10:14 -0600463
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200464static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
465 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600466{
Jens Axboef5d11842017-06-27 12:03:06 -0600467 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600468 u16 control = 0;
469 u32 dsmgmt = 0;
470
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200471 /*
472 * If formated with metadata, require the block layer provide a buffer
473 * unless this namespace is formated such that the metadata can be
474 * stripped/generated by the controller with PRACT=1.
475 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300476 if (ns && ns->ms &&
477 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200478 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
479 return BLK_STS_NOTSUPP;
480
Ming Lin8093f7c2016-04-12 13:10:14 -0600481 if (req->cmd_flags & REQ_FUA)
482 control |= NVME_RW_FUA;
483 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
484 control |= NVME_RW_LR;
485
486 if (req->cmd_flags & REQ_RAHEAD)
487 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
488
489 memset(cmnd, 0, sizeof(*cmnd));
490 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600491 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
492 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
493 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
494
Jens Axboef5d11842017-06-27 12:03:06 -0600495 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
496 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
497
Ming Lin8093f7c2016-04-12 13:10:14 -0600498 if (ns->ms) {
499 switch (ns->pi_type) {
500 case NVME_NS_DPS_PI_TYPE3:
501 control |= NVME_RW_PRINFO_PRCHK_GUARD;
502 break;
503 case NVME_NS_DPS_PI_TYPE1:
504 case NVME_NS_DPS_PI_TYPE2:
505 control |= NVME_RW_PRINFO_PRCHK_GUARD |
506 NVME_RW_PRINFO_PRCHK_REF;
507 cmnd->rw.reftag = cpu_to_le32(
508 nvme_block_nr(ns, blk_rq_pos(req)));
509 break;
510 }
511 if (!blk_integrity_rq(req))
512 control |= NVME_RW_PRINFO_PRACT;
513 }
514
515 cmnd->rw.control = cpu_to_le16(control);
516 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200517 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600518}
519
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200520blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600521 struct nvme_command *cmd)
522{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200523 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600524
Christoph Hellwig987f6992017-04-05 19:18:08 +0200525 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200526 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200527 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200528 req->rq_flags |= RQF_DONTPREP;
529 }
530
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100531 switch (req_op(req)) {
532 case REQ_OP_DRV_IN:
533 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800534 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100535 break;
536 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600537 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100538 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200539 case REQ_OP_WRITE_ZEROES:
540 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100541 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600542 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100543 break;
544 case REQ_OP_READ:
545 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200546 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100547 break;
548 default:
549 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200550 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100551 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600552
James Smart721b3912016-10-21 23:33:34 +0300553 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600554 return ret;
555}
556EXPORT_SYMBOL_GPL(nvme_setup_cmd);
557
Christoph Hellwig41609822015-11-20 09:00:02 +0100558/*
559 * Returns 0 on success. If the result is negative, it's a Linux error code;
560 * if the result is positive, it's an NVM Express status code
561 */
562int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800563 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200564 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100565{
566 struct request *req;
567 int ret;
568
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200569 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100570 if (IS_ERR(req))
571 return PTR_ERR(req);
572
573 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
574
Christoph Hellwig21d34712015-11-26 09:08:36 +0100575 if (buffer && bufflen) {
576 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
577 if (ret)
578 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100579 }
580
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200581 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800582 if (result)
583 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200584 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
585 ret = -EINTR;
586 else
587 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100588 out:
589 blk_mq_free_request(req);
590 return ret;
591}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200592EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100593
594int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
595 void *buffer, unsigned bufflen)
596{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200597 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
598 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100599}
Ming Lin576d55d2016-02-10 10:03:32 -0800600EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100601
Keith Busch0b7f1f22015-10-23 09:47:28 -0600602int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
603 void __user *ubuffer, unsigned bufflen,
604 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
605 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100606{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200607 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600608 struct nvme_ns *ns = q->queuedata;
609 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100610 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600611 struct bio *bio = NULL;
612 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100613 int ret;
614
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200615 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100616 if (IS_ERR(req))
617 return PTR_ERR(req);
618
619 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
620
621 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100622 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
623 GFP_KERNEL);
624 if (ret)
625 goto out;
626 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100627
Keith Busch0b7f1f22015-10-23 09:47:28 -0600628 if (!disk)
629 goto submit;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200630 bio->bi_disk = disk;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600631
Keith Busche9fc63d2016-02-24 09:15:58 -0700632 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600633 struct bio_integrity_payload *bip;
634
635 meta = kmalloc(meta_len, GFP_KERNEL);
636 if (!meta) {
637 ret = -ENOMEM;
638 goto out_unmap;
639 }
640
641 if (write) {
642 if (copy_from_user(meta, meta_buffer,
643 meta_len)) {
644 ret = -EFAULT;
645 goto out_free_meta;
646 }
647 }
648
649 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700650 if (IS_ERR(bip)) {
651 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600652 goto out_free_meta;
653 }
654
655 bip->bip_iter.bi_size = meta_len;
656 bip->bip_iter.bi_sector = meta_seed;
657
658 ret = bio_integrity_add_page(bio, virt_to_page(meta),
659 meta_len, offset_in_page(meta));
660 if (ret != meta_len) {
661 ret = -ENOMEM;
662 goto out_free_meta;
663 }
664 }
665 }
666 submit:
667 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200668 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
669 ret = -EINTR;
670 else
671 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100672 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800673 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600674 if (meta && !ret && !write) {
675 if (copy_to_user(meta_buffer, meta, meta_len))
676 ret = -EFAULT;
677 }
678 out_free_meta:
679 kfree(meta);
680 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200681 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600682 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100683 out:
684 blk_mq_free_request(req);
685 return ret;
686}
687
Keith Busch0b7f1f22015-10-23 09:47:28 -0600688int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
689 void __user *ubuffer, unsigned bufflen, u32 *result,
690 unsigned timeout)
691{
692 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
693 result, timeout);
694}
695
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200696static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200697{
698 struct nvme_ctrl *ctrl = rq->end_io_data;
699
700 blk_mq_free_request(rq);
701
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200702 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200703 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200704 "failed nvme_keep_alive_end_io error=%d\n",
705 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200706 return;
707 }
708
709 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
710}
711
712static int nvme_keep_alive(struct nvme_ctrl *ctrl)
713{
714 struct nvme_command c;
715 struct request *rq;
716
717 memset(&c, 0, sizeof(c));
718 c.common.opcode = nvme_admin_keep_alive;
719
720 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
721 NVME_QID_ANY);
722 if (IS_ERR(rq))
723 return PTR_ERR(rq);
724
725 rq->timeout = ctrl->kato * HZ;
726 rq->end_io_data = ctrl;
727
728 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
729
730 return 0;
731}
732
733static void nvme_keep_alive_work(struct work_struct *work)
734{
735 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
736 struct nvme_ctrl, ka_work);
737
738 if (nvme_keep_alive(ctrl)) {
739 /* allocation failure, reset the controller */
740 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200741 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200742 return;
743 }
744}
745
746void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
747{
748 if (unlikely(ctrl->kato == 0))
749 return;
750
751 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
752 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
753}
754EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
755
756void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
757{
758 if (unlikely(ctrl->kato == 0))
759 return;
760
761 cancel_delayed_work_sync(&ctrl->ka_work);
762}
763EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
764
Keith Busch3f7f25a92017-06-20 15:09:56 -0400765static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100766{
767 struct nvme_command c = { };
768 int error;
769
770 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
771 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200772 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100773
774 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
775 if (!*id)
776 return -ENOMEM;
777
778 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
779 sizeof(struct nvme_id_ctrl));
780 if (error)
781 kfree(*id);
782 return error;
783}
784
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200785static int nvme_identify_ns_descs(struct nvme_ns *ns, unsigned nsid)
786{
787 struct nvme_command c = { };
788 int status;
789 void *data;
790 int pos;
791 int len;
792
793 c.identify.opcode = nvme_admin_identify;
794 c.identify.nsid = cpu_to_le32(nsid);
795 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
796
797 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
798 if (!data)
799 return -ENOMEM;
800
801 status = nvme_submit_sync_cmd(ns->ctrl->admin_q, &c, data,
802 NVME_IDENTIFY_DATA_SIZE);
803 if (status)
804 goto free_data;
805
806 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
807 struct nvme_ns_id_desc *cur = data + pos;
808
809 if (cur->nidl == 0)
810 break;
811
812 switch (cur->nidt) {
813 case NVME_NIDT_EUI64:
814 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
815 dev_warn(ns->ctrl->device,
816 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
817 cur->nidl);
818 goto free_data;
819 }
820 len = NVME_NIDT_EUI64_LEN;
821 memcpy(ns->eui, data + pos + sizeof(*cur), len);
822 break;
823 case NVME_NIDT_NGUID:
824 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
825 dev_warn(ns->ctrl->device,
826 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
827 cur->nidl);
828 goto free_data;
829 }
830 len = NVME_NIDT_NGUID_LEN;
831 memcpy(ns->nguid, data + pos + sizeof(*cur), len);
832 break;
833 case NVME_NIDT_UUID:
834 if (cur->nidl != NVME_NIDT_UUID_LEN) {
835 dev_warn(ns->ctrl->device,
836 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
837 cur->nidl);
838 goto free_data;
839 }
840 len = NVME_NIDT_UUID_LEN;
841 uuid_copy(&ns->uuid, data + pos + sizeof(*cur));
842 break;
843 default:
844 /* Skip unnkown types */
845 len = cur->nidl;
846 break;
847 }
848
849 len += sizeof(*cur);
850 }
851free_data:
852 kfree(data);
853 return status;
854}
855
Keith Busch540c8012015-10-22 15:45:06 -0600856static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
857{
858 struct nvme_command c = { };
859
860 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200861 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600862 c.identify.nsid = cpu_to_le32(nsid);
863 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
864}
865
Keith Busch3f7f25a92017-06-20 15:09:56 -0400866static int nvme_identify_ns(struct nvme_ctrl *dev, unsigned nsid,
Christoph Hellwig21d34712015-11-26 09:08:36 +0100867 struct nvme_id_ns **id)
868{
869 struct nvme_command c = { };
870 int error;
871
872 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200873 c.identify.opcode = nvme_admin_identify;
874 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200875 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100876
877 *id = kmalloc(sizeof(struct nvme_id_ns), GFP_KERNEL);
878 if (!*id)
879 return -ENOMEM;
880
881 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
882 sizeof(struct nvme_id_ns));
883 if (error)
884 kfree(*id);
885 return error;
886}
887
Keith Busch3f7f25a92017-06-20 15:09:56 -0400888static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700889 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100890{
891 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800892 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100893 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100894
895 memset(&c, 0, sizeof(c));
896 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100897 c.features.fid = cpu_to_le32(fid);
898 c.features.dword11 = cpu_to_le32(dword11);
899
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800900 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700901 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700902 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800903 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100904 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100905}
906
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100907int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
908{
909 u32 q_count = (*count - 1) | ((*count - 1) << 16);
910 u32 result;
911 int status, nr_io_queues;
912
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700913 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100914 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200915 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100916 return status;
917
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200918 /*
919 * Degraded controllers might return an error when setting the queue
920 * count. We still want to be able to bring them online and offer
921 * access to the admin queue, as that might be only way to fix them up.
922 */
923 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200924 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200925 *count = 0;
926 } else {
927 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
928 *count = min(*count, nr_io_queues);
929 }
930
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100931 return 0;
932}
Ming Lin576d55d2016-02-10 10:03:32 -0800933EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100934
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100935static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
936{
937 struct nvme_user_io io;
938 struct nvme_command c;
939 unsigned length, meta_len;
940 void __user *metadata;
941
942 if (copy_from_user(&io, uio, sizeof(io)))
943 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700944 if (io.flags)
945 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100946
947 switch (io.opcode) {
948 case nvme_cmd_write:
949 case nvme_cmd_read:
950 case nvme_cmd_compare:
951 break;
952 default:
953 return -EINVAL;
954 }
955
956 length = (io.nblocks + 1) << ns->lba_shift;
957 meta_len = (io.nblocks + 1) * ns->ms;
958 metadata = (void __user *)(uintptr_t)io.metadata;
959
960 if (ns->ext) {
961 length += meta_len;
962 meta_len = 0;
963 } else if (meta_len) {
964 if ((io.metadata & 3) || !io.metadata)
965 return -EINVAL;
966 }
967
968 memset(&c, 0, sizeof(c));
969 c.rw.opcode = io.opcode;
970 c.rw.flags = io.flags;
971 c.rw.nsid = cpu_to_le32(ns->ns_id);
972 c.rw.slba = cpu_to_le64(io.slba);
973 c.rw.length = cpu_to_le16(io.nblocks);
974 c.rw.control = cpu_to_le16(io.control);
975 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
976 c.rw.reftag = cpu_to_le32(io.reftag);
977 c.rw.apptag = cpu_to_le16(io.apptag);
978 c.rw.appmask = cpu_to_le16(io.appmask);
979
980 return __nvme_submit_user_cmd(ns->queue, &c,
981 (void __user *)(uintptr_t)io.addr, length,
982 metadata, meta_len, io.slba, NULL, 0);
983}
984
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100985static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100986 struct nvme_passthru_cmd __user *ucmd)
987{
988 struct nvme_passthru_cmd cmd;
989 struct nvme_command c;
990 unsigned timeout = 0;
991 int status;
992
993 if (!capable(CAP_SYS_ADMIN))
994 return -EACCES;
995 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
996 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700997 if (cmd.flags)
998 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100999
1000 memset(&c, 0, sizeof(c));
1001 c.common.opcode = cmd.opcode;
1002 c.common.flags = cmd.flags;
1003 c.common.nsid = cpu_to_le32(cmd.nsid);
1004 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1005 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1006 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1007 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1008 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1009 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1010 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1011 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1012
1013 if (cmd.timeout_ms)
1014 timeout = msecs_to_jiffies(cmd.timeout_ms);
1015
1016 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001017 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001018 &cmd.result, timeout);
1019 if (status >= 0) {
1020 if (put_user(cmd.result, &ucmd->result))
1021 return -EFAULT;
1022 }
1023
1024 return status;
1025}
1026
1027static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1028 unsigned int cmd, unsigned long arg)
1029{
1030 struct nvme_ns *ns = bdev->bd_disk->private_data;
1031
1032 switch (cmd) {
1033 case NVME_IOCTL_ID:
1034 force_successful_syscall_return();
1035 return ns->ns_id;
1036 case NVME_IOCTL_ADMIN_CMD:
1037 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1038 case NVME_IOCTL_IO_CMD:
1039 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1040 case NVME_IOCTL_SUBMIT_IO:
1041 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001042 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001043#ifdef CONFIG_NVM
1044 if (ns->ndev)
1045 return nvme_nvm_ioctl(ns, cmd, arg);
1046#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001047 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001048 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001049 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001050 return -ENOTTY;
1051 }
1052}
1053
1054#ifdef CONFIG_COMPAT
1055static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1056 unsigned int cmd, unsigned long arg)
1057{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001058 return nvme_ioctl(bdev, mode, cmd, arg);
1059}
1060#else
1061#define nvme_compat_ioctl NULL
1062#endif
1063
1064static int nvme_open(struct block_device *bdev, fmode_t mode)
1065{
1066 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1067}
1068
1069static void nvme_release(struct gendisk *disk, fmode_t mode)
1070{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001071 struct nvme_ns *ns = disk->private_data;
1072
1073 module_put(ns->ctrl->ops->module);
1074 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001075}
1076
1077static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1078{
1079 /* some standard values */
1080 geo->heads = 1 << 6;
1081 geo->sectors = 1 << 5;
1082 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1083 return 0;
1084}
1085
1086#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001087static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1088 u16 bs)
1089{
1090 struct nvme_ns *ns = disk->private_data;
1091 u16 old_ms = ns->ms;
1092 u8 pi_type = 0;
1093
1094 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1095 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1096
1097 /* PI implementation requires metadata equal t10 pi tuple size */
1098 if (ns->ms == sizeof(struct t10_pi_tuple))
1099 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1100
1101 if (blk_get_integrity(disk) &&
1102 (ns->pi_type != pi_type || ns->ms != old_ms ||
1103 bs != queue_logical_block_size(disk->queue) ||
1104 (ns->ms && ns->ext)))
1105 blk_integrity_unregister(disk);
1106
1107 ns->pi_type = pi_type;
1108}
1109
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001110static void nvme_init_integrity(struct nvme_ns *ns)
1111{
1112 struct blk_integrity integrity;
1113
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001114 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001115 switch (ns->pi_type) {
1116 case NVME_NS_DPS_PI_TYPE3:
1117 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001118 integrity.tag_size = sizeof(u16) + sizeof(u32);
1119 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001120 break;
1121 case NVME_NS_DPS_PI_TYPE1:
1122 case NVME_NS_DPS_PI_TYPE2:
1123 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001124 integrity.tag_size = sizeof(u16);
1125 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001126 break;
1127 default:
1128 integrity.profile = NULL;
1129 break;
1130 }
1131 integrity.tuple_size = ns->ms;
1132 blk_integrity_register(ns->disk, &integrity);
1133 blk_queue_max_integrity_segments(ns->queue, 1);
1134}
1135#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001136static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1137 u16 bs)
1138{
1139}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001140static void nvme_init_integrity(struct nvme_ns *ns)
1141{
1142}
1143#endif /* CONFIG_BLK_DEV_INTEGRITY */
1144
Scott Bauer6b8190d2017-06-15 10:44:30 -06001145static void nvme_set_chunk_size(struct nvme_ns *ns)
1146{
1147 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1148 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1149}
1150
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001151static void nvme_config_discard(struct nvme_ns *ns)
1152{
Keith Busch08095e72016-03-04 13:15:17 -07001153 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001154 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001155
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001156 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1157 NVME_DSM_MAX_RANGES);
1158
Jens Axboef5d11842017-06-27 12:03:06 -06001159 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1160 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1161
1162 ns->queue->limits.discard_alignment = sz;
1163 ns->queue->limits.discard_granularity = sz;
1164 } else {
1165 ns->queue->limits.discard_alignment = logical_block_size;
1166 ns->queue->limits.discard_granularity = logical_block_size;
1167 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001168 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001169 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001170 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001171
1172 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1173 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001174}
1175
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001176static int nvme_revalidate_ns(struct nvme_ns *ns, struct nvme_id_ns **id)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001177{
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001178 if (nvme_identify_ns(ns->ctrl, ns->ns_id, id)) {
Sagi Grimbergcaaa15c2017-08-15 12:24:05 +03001179 dev_warn(ns->ctrl->device, "Identify namespace failed\n");
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001180 return -ENODEV;
1181 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001182
1183 if ((*id)->ncap == 0) {
1184 kfree(*id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001185 return -ENODEV;
1186 }
1187
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001188 if (ns->ctrl->vs >= NVME_VS(1, 1, 0))
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001189 memcpy(ns->eui, (*id)->eui64, sizeof(ns->eui));
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001190 if (ns->ctrl->vs >= NVME_VS(1, 2, 0))
Johannes Thumshirn90985b82017-06-07 11:45:31 +02001191 memcpy(ns->nguid, (*id)->nguid, sizeof(ns->nguid));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001192 if (ns->ctrl->vs >= NVME_VS(1, 3, 0)) {
1193 /* Don't treat error as fatal we potentially
1194 * already have a NGUID or EUI-64
1195 */
1196 if (nvme_identify_ns_descs(ns, ns->ns_id))
1197 dev_warn(ns->ctrl->device,
1198 "%s: Identify Descriptors failed\n", __func__);
1199 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001200
1201 return 0;
1202}
1203
1204static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1205{
1206 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001207 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001208 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001209
1210 /*
1211 * If identify namespace failed, use default 512 byte block size so
1212 * block layer can use before failing read/write for 0 capacity.
1213 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001214 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001215 if (ns->lba_shift == 0)
1216 ns->lba_shift = 9;
1217 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001218 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001219
1220 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001221
Jens Axboef5d11842017-06-27 12:03:06 -06001222 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001223 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001224 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001225 if (ns->noiob)
1226 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001227 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001228 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001229 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1230 set_capacity(disk, 0);
1231 else
1232 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1233
Jens Axboef5d11842017-06-27 12:03:06 -06001234 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001235 nvme_config_discard(ns);
1236 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001237}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001238
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001239static int nvme_revalidate_disk(struct gendisk *disk)
1240{
1241 struct nvme_ns *ns = disk->private_data;
1242 struct nvme_id_ns *id = NULL;
1243 int ret;
1244
1245 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1246 set_capacity(disk, 0);
1247 return -ENODEV;
1248 }
1249
1250 ret = nvme_revalidate_ns(ns, &id);
1251 if (ret)
1252 return ret;
1253
1254 __nvme_revalidate_disk(disk, id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001255 kfree(id);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001256
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001257 return 0;
1258}
1259
1260static char nvme_pr_type(enum pr_type type)
1261{
1262 switch (type) {
1263 case PR_WRITE_EXCLUSIVE:
1264 return 1;
1265 case PR_EXCLUSIVE_ACCESS:
1266 return 2;
1267 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1268 return 3;
1269 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1270 return 4;
1271 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1272 return 5;
1273 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1274 return 6;
1275 default:
1276 return 0;
1277 }
1278};
1279
1280static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1281 u64 key, u64 sa_key, u8 op)
1282{
1283 struct nvme_ns *ns = bdev->bd_disk->private_data;
1284 struct nvme_command c;
1285 u8 data[16] = { 0, };
1286
1287 put_unaligned_le64(key, &data[0]);
1288 put_unaligned_le64(sa_key, &data[8]);
1289
1290 memset(&c, 0, sizeof(c));
1291 c.common.opcode = op;
1292 c.common.nsid = cpu_to_le32(ns->ns_id);
1293 c.common.cdw10[0] = cpu_to_le32(cdw10);
1294
1295 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1296}
1297
1298static int nvme_pr_register(struct block_device *bdev, u64 old,
1299 u64 new, unsigned flags)
1300{
1301 u32 cdw10;
1302
1303 if (flags & ~PR_FL_IGNORE_KEY)
1304 return -EOPNOTSUPP;
1305
1306 cdw10 = old ? 2 : 0;
1307 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1308 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1309 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1310}
1311
1312static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1313 enum pr_type type, unsigned flags)
1314{
1315 u32 cdw10;
1316
1317 if (flags & ~PR_FL_IGNORE_KEY)
1318 return -EOPNOTSUPP;
1319
1320 cdw10 = nvme_pr_type(type) << 8;
1321 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1322 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1323}
1324
1325static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1326 enum pr_type type, bool abort)
1327{
1328 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1329 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1330}
1331
1332static int nvme_pr_clear(struct block_device *bdev, u64 key)
1333{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001334 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001335 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1336}
1337
1338static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1339{
1340 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1341 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1342}
1343
1344static const struct pr_ops nvme_pr_ops = {
1345 .pr_register = nvme_pr_register,
1346 .pr_reserve = nvme_pr_reserve,
1347 .pr_release = nvme_pr_release,
1348 .pr_preempt = nvme_pr_preempt,
1349 .pr_clear = nvme_pr_clear,
1350};
1351
Scott Bauera98e58e52017-02-03 12:50:32 -07001352#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001353int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1354 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001355{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001356 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001357 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001358
1359 memset(&cmd, 0, sizeof(cmd));
1360 if (send)
1361 cmd.common.opcode = nvme_admin_security_send;
1362 else
1363 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001364 cmd.common.nsid = 0;
1365 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1366 cmd.common.cdw10[1] = cpu_to_le32(len);
1367
1368 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1369 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1370}
1371EXPORT_SYMBOL_GPL(nvme_sec_submit);
1372#endif /* CONFIG_BLK_SED_OPAL */
1373
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001374static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001375 .owner = THIS_MODULE,
1376 .ioctl = nvme_ioctl,
1377 .compat_ioctl = nvme_compat_ioctl,
1378 .open = nvme_open,
1379 .release = nvme_release,
1380 .getgeo = nvme_getgeo,
1381 .revalidate_disk= nvme_revalidate_disk,
1382 .pr_ops = &nvme_pr_ops,
1383};
1384
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001385static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1386{
1387 unsigned long timeout =
1388 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1389 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1390 int ret;
1391
1392 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001393 if (csts == ~0)
1394 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001395 if ((csts & NVME_CSTS_RDY) == bit)
1396 break;
1397
1398 msleep(100);
1399 if (fatal_signal_pending(current))
1400 return -EINTR;
1401 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001402 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001403 "Device not ready; aborting %s\n", enabled ?
1404 "initialisation" : "reset");
1405 return -ENODEV;
1406 }
1407 }
1408
1409 return ret;
1410}
1411
1412/*
1413 * If the device has been passed off to us in an enabled state, just clear
1414 * the enabled bit. The spec says we should set the 'shutdown notification
1415 * bits', but doing so may cause the device to complete commands to the
1416 * admin queue ... and we don't know what memory that might be pointing at!
1417 */
1418int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1419{
1420 int ret;
1421
1422 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1423 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1424
1425 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1426 if (ret)
1427 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001428
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001429 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001430 msleep(NVME_QUIRK_DELAY_AMOUNT);
1431
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001432 return nvme_wait_ready(ctrl, cap, false);
1433}
Ming Lin576d55d2016-02-10 10:03:32 -08001434EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001435
1436int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1437{
1438 /*
1439 * Default to a 4K page size, with the intention to update this
1440 * path in the future to accomodate architectures with differing
1441 * kernel and IO page sizes.
1442 */
1443 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1444 int ret;
1445
1446 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001447 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001448 "Minimum device page size %u too large for host (%u)\n",
1449 1 << dev_page_min, 1 << page_shift);
1450 return -ENODEV;
1451 }
1452
1453 ctrl->page_size = 1 << page_shift;
1454
1455 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1456 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001457 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001458 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1459 ctrl->ctrl_config |= NVME_CC_ENABLE;
1460
1461 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1462 if (ret)
1463 return ret;
1464 return nvme_wait_ready(ctrl, cap, true);
1465}
Ming Lin576d55d2016-02-10 10:03:32 -08001466EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001467
1468int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1469{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001470 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001471 u32 csts;
1472 int ret;
1473
1474 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1475 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1476
1477 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1478 if (ret)
1479 return ret;
1480
1481 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1482 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1483 break;
1484
1485 msleep(100);
1486 if (fatal_signal_pending(current))
1487 return -EINTR;
1488 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001489 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001490 "Device shutdown incomplete; abort shutdown\n");
1491 return -ENODEV;
1492 }
1493 }
1494
1495 return ret;
1496}
Ming Lin576d55d2016-02-10 10:03:32 -08001497EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001498
Christoph Hellwigda358252016-03-02 18:07:11 +01001499static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1500 struct request_queue *q)
1501{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001502 bool vwc = false;
1503
Christoph Hellwigda358252016-03-02 18:07:11 +01001504 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001505 u32 max_segments =
1506 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1507
Christoph Hellwigda358252016-03-02 18:07:11 +01001508 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001509 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001510 }
Keith Busche6282ae2016-12-19 11:37:50 -05001511 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1512 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001513 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001514 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1515 vwc = true;
1516 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001517}
1518
Jon Derrickdbf86b32017-08-16 09:51:29 +02001519static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1520{
1521 __le64 ts;
1522 int ret;
1523
1524 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1525 return 0;
1526
1527 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1528 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1529 NULL);
1530 if (ret)
1531 dev_warn_once(ctrl->device,
1532 "could not set timestamp (%d)\n", ret);
1533 return ret;
1534}
1535
Keith Busch634b8322017-08-10 11:23:31 +02001536static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001537{
1538 /*
1539 * APST (Autonomous Power State Transition) lets us program a
1540 * table of power state transitions that the controller will
1541 * perform automatically. We configure it with a simple
1542 * heuristic: we are willing to spend at most 2% of the time
1543 * transitioning between power states. Therefore, when running
1544 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001545 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001546 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001547 * the requested maximum latency.
1548 *
1549 * We will not autonomously enter any non-operational state for
1550 * which the total latency exceeds ps_max_latency_us. Users
1551 * can set ps_max_latency_us to zero to turn off APST.
1552 */
1553
1554 unsigned apste;
1555 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001556 u64 max_lat_us = 0;
1557 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001558 int ret;
1559
1560 /*
1561 * If APST isn't supported or if we haven't been initialized yet,
1562 * then don't do anything.
1563 */
1564 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001565 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001566
1567 if (ctrl->npss > 31) {
1568 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001569 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001570 }
1571
1572 table = kzalloc(sizeof(*table), GFP_KERNEL);
1573 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001574 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001575
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001576 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001577 /* Turn off APST. */
1578 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001579 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001580 } else {
1581 __le64 target = cpu_to_le64(0);
1582 int state;
1583
1584 /*
1585 * Walk through all states from lowest- to highest-power.
1586 * According to the spec, lower-numbered states use more
1587 * power. NPSS, despite the name, is the index of the
1588 * lowest-power state, not the number of states.
1589 */
1590 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001591 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001592
1593 if (target)
1594 table->entries[state] = target;
1595
1596 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001597 * Don't allow transitions to the deepest state
1598 * if it's quirked off.
1599 */
1600 if (state == ctrl->npss &&
1601 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1602 continue;
1603
1604 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001605 * Is this state a useful non-operational state for
1606 * higher-power states to autonomously transition to?
1607 */
1608 if (!(ctrl->psd[state].flags &
1609 NVME_PS_FLAGS_NON_OP_STATE))
1610 continue;
1611
Kai-Heng Fengda875912017-06-07 15:25:42 +08001612 exit_latency_us =
1613 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1614 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001615 continue;
1616
Kai-Heng Fengda875912017-06-07 15:25:42 +08001617 total_latency_us =
1618 exit_latency_us +
1619 le32_to_cpu(ctrl->psd[state].entry_lat);
1620
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001621 /*
1622 * This state is good. Use it as the APST idle
1623 * target for higher power states.
1624 */
1625 transition_ms = total_latency_us + 19;
1626 do_div(transition_ms, 20);
1627 if (transition_ms > (1 << 24) - 1)
1628 transition_ms = (1 << 24) - 1;
1629
1630 target = cpu_to_le64((state << 3) |
1631 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001632
1633 if (max_ps == -1)
1634 max_ps = state;
1635
1636 if (total_latency_us > max_lat_us)
1637 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001638 }
1639
1640 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001641
1642 if (max_ps == -1) {
1643 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1644 } else {
1645 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1646 max_ps, max_lat_us, (int)sizeof(*table), table);
1647 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001648 }
1649
1650 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1651 table, sizeof(*table), NULL);
1652 if (ret)
1653 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1654
1655 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001656 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001657}
1658
1659static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1660{
1661 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1662 u64 latency;
1663
1664 switch (val) {
1665 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1666 case PM_QOS_LATENCY_ANY:
1667 latency = U64_MAX;
1668 break;
1669
1670 default:
1671 latency = val;
1672 }
1673
1674 if (ctrl->ps_max_latency_us != latency) {
1675 ctrl->ps_max_latency_us = latency;
1676 nvme_configure_apst(ctrl);
1677 }
1678}
1679
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001680struct nvme_core_quirk_entry {
1681 /*
1682 * NVMe model and firmware strings are padded with spaces. For
1683 * simplicity, strings in the quirk table are padded with NULLs
1684 * instead.
1685 */
1686 u16 vid;
1687 const char *mn;
1688 const char *fr;
1689 unsigned long quirks;
1690};
1691
1692static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001693 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001694 /*
1695 * This Toshiba device seems to die using any APST states. See:
1696 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1697 */
1698 .vid = 0x1179,
1699 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001700 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001701 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001702};
1703
1704/* match is null-terminated but idstr is space-padded. */
1705static bool string_matches(const char *idstr, const char *match, size_t len)
1706{
1707 size_t matchlen;
1708
1709 if (!match)
1710 return true;
1711
1712 matchlen = strlen(match);
1713 WARN_ON_ONCE(matchlen > len);
1714
1715 if (memcmp(idstr, match, matchlen))
1716 return false;
1717
1718 for (; matchlen < len; matchlen++)
1719 if (idstr[matchlen] != ' ')
1720 return false;
1721
1722 return true;
1723}
1724
1725static bool quirk_matches(const struct nvme_id_ctrl *id,
1726 const struct nvme_core_quirk_entry *q)
1727{
1728 return q->vid == le16_to_cpu(id->vid) &&
1729 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1730 string_matches(id->fr, q->fr, sizeof(id->fr));
1731}
1732
Christoph Hellwig180de0072017-06-26 12:39:02 +02001733static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1734{
1735 size_t nqnlen;
1736 int off;
1737
1738 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1739 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1740 strcpy(ctrl->subnqn, id->subnqn);
1741 return;
1742 }
1743
1744 if (ctrl->vs >= NVME_VS(1, 2, 1))
1745 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1746
1747 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1748 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1749 "nqn.2014.08.org.nvmexpress:%4x%4x",
1750 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1751 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1752 off += sizeof(id->sn);
1753 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1754 off += sizeof(id->mn);
1755 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1756}
1757
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001758/*
1759 * Initialize the cached copies of the Identify data and various controller
1760 * register in our nvme_ctrl structure. This should be called as soon as
1761 * the admin queue is fully up and running.
1762 */
1763int nvme_init_identify(struct nvme_ctrl *ctrl)
1764{
1765 struct nvme_id_ctrl *id;
1766 u64 cap;
1767 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001768 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001769 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001770
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001771 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1772 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001773 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001774 return ret;
1775 }
1776
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001777 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1778 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001779 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001780 return ret;
1781 }
1782 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1783
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001784 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001785 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1786
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001787 ret = nvme_identify_ctrl(ctrl, &id);
1788 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001789 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001790 return -EIO;
1791 }
1792
Christoph Hellwig180de0072017-06-26 12:39:02 +02001793 nvme_init_subnqn(ctrl, id);
1794
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001795 if (!ctrl->identified) {
1796 /*
1797 * Check for quirks. Quirk can depend on firmware version,
1798 * so, in principle, the set of quirks present can change
1799 * across a reset. As a possible future enhancement, we
1800 * could re-scan for quirks every time we reinitialize
1801 * the device, but we'd have to make sure that the driver
1802 * behaves intelligently if the quirks change.
1803 */
1804
1805 int i;
1806
1807 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1808 if (quirk_matches(id, &core_quirks[i]))
1809 ctrl->quirks |= core_quirks[i].quirks;
1810 }
1811 }
1812
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001813 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001814 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 -07001815 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1816 }
1817
Scott Bauer8a9ae522017-02-17 13:59:40 +01001818 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001819 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001820 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001821 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001822 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001823 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001824 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1825 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1826 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1827 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001828 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001829 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001830 max_hw_sectors = UINT_MAX;
1831 ctrl->max_hw_sectors =
1832 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001833
Christoph Hellwigda358252016-03-02 18:07:11 +01001834 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001835 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001836 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001837
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001838 if (id->rtd3e) {
1839 /* us -> s */
1840 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1841
1842 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1843 shutdown_timeout, 60);
1844
1845 if (ctrl->shutdown_timeout != shutdown_timeout)
1846 dev_warn(ctrl->device,
1847 "Shutdown timeout set to %u seconds\n",
1848 ctrl->shutdown_timeout);
1849 } else
1850 ctrl->shutdown_timeout = shutdown_timeout;
1851
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001852 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001853 ctrl->apsta = id->apsta;
1854 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001855 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1856 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001857 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 -04001858 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001859 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001860 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001861 }
1862 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001863 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001864 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001865 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1866
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001867 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001868 ctrl->icdoff = le16_to_cpu(id->icdoff);
1869 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1870 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1871 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1872
1873 /*
1874 * In fabrics we need to verify the cntlid matches the
1875 * admin connect
1876 */
Keith Busch634b8322017-08-10 11:23:31 +02001877 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001878 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001879 goto out_free;
1880 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001881
1882 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001883 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001884 "keep-alive support is mandatory for fabrics\n");
1885 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001886 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001887 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001888 } else {
1889 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001890 ctrl->hmpre = le32_to_cpu(id->hmpre);
1891 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001892 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001893
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001894 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001895
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001896 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001897 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001898 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001899 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1900
Keith Busch634b8322017-08-10 11:23:31 +02001901 ret = nvme_configure_apst(ctrl);
1902 if (ret < 0)
1903 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001904
1905 ret = nvme_configure_timestamp(ctrl);
1906 if (ret < 0)
1907 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001908
1909 ret = nvme_configure_directives(ctrl);
1910 if (ret < 0)
1911 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001912
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001913 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001914
Keith Busch634b8322017-08-10 11:23:31 +02001915 return 0;
1916
1917out_free:
1918 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001919 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001920}
Ming Lin576d55d2016-02-10 10:03:32 -08001921EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001922
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001923static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001924{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001925 struct nvme_ctrl *ctrl;
1926 int instance = iminor(inode);
1927 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001928
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001929 spin_lock(&dev_list_lock);
1930 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1931 if (ctrl->instance != instance)
1932 continue;
1933
1934 if (!ctrl->admin_q) {
1935 ret = -EWOULDBLOCK;
1936 break;
1937 }
1938 if (!kref_get_unless_zero(&ctrl->kref))
1939 break;
1940 file->private_data = ctrl;
1941 ret = 0;
1942 break;
1943 }
1944 spin_unlock(&dev_list_lock);
1945
1946 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001947}
1948
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001949static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001950{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001951 nvme_put_ctrl(file->private_data);
1952 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001953}
1954
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001955static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1956{
1957 struct nvme_ns *ns;
1958 int ret;
1959
1960 mutex_lock(&ctrl->namespaces_mutex);
1961 if (list_empty(&ctrl->namespaces)) {
1962 ret = -ENOTTY;
1963 goto out_unlock;
1964 }
1965
1966 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1967 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001968 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001969 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1970 ret = -EINVAL;
1971 goto out_unlock;
1972 }
1973
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001974 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001975 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1976 kref_get(&ns->kref);
1977 mutex_unlock(&ctrl->namespaces_mutex);
1978
1979 ret = nvme_user_cmd(ctrl, ns, argp);
1980 nvme_put_ns(ns);
1981 return ret;
1982
1983out_unlock:
1984 mutex_unlock(&ctrl->namespaces_mutex);
1985 return ret;
1986}
1987
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001988static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1989 unsigned long arg)
1990{
1991 struct nvme_ctrl *ctrl = file->private_data;
1992 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001993
1994 switch (cmd) {
1995 case NVME_IOCTL_ADMIN_CMD:
1996 return nvme_user_cmd(ctrl, NULL, argp);
1997 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001998 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001999 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002000 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002001 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002002 case NVME_IOCTL_SUBSYS_RESET:
2003 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002004 case NVME_IOCTL_RESCAN:
2005 nvme_queue_scan(ctrl);
2006 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002007 default:
2008 return -ENOTTY;
2009 }
2010}
2011
2012static const struct file_operations nvme_dev_fops = {
2013 .owner = THIS_MODULE,
2014 .open = nvme_dev_open,
2015 .release = nvme_dev_release,
2016 .unlocked_ioctl = nvme_dev_ioctl,
2017 .compat_ioctl = nvme_dev_ioctl,
2018};
2019
2020static ssize_t nvme_sysfs_reset(struct device *dev,
2021 struct device_attribute *attr, const char *buf,
2022 size_t count)
2023{
2024 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2025 int ret;
2026
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002027 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002028 if (ret < 0)
2029 return ret;
2030 return count;
2031}
2032static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2033
Keith Busch9ec3bb22016-04-29 15:45:18 -06002034static ssize_t nvme_sysfs_rescan(struct device *dev,
2035 struct device_attribute *attr, const char *buf,
2036 size_t count)
2037{
2038 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2039
2040 nvme_queue_scan(ctrl);
2041 return count;
2042}
2043static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2044
Keith Busch118472a2016-02-18 09:57:48 -07002045static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2046 char *buf)
2047{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002048 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002049 struct nvme_ctrl *ctrl = ns->ctrl;
2050 int serial_len = sizeof(ctrl->serial);
2051 int model_len = sizeof(ctrl->model);
2052
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002053 if (!uuid_is_null(&ns->uuid))
2054 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2055
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002056 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2057 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002058
2059 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2060 return sprintf(buf, "eui.%8phN\n", ns->eui);
2061
Martin Wilck758f3732017-07-20 18:34:02 +02002062 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2063 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002064 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002065 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2066 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002067 model_len--;
2068
2069 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2070 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2071}
2072static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2073
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002074static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2075 char *buf)
2076{
2077 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2078 return sprintf(buf, "%pU\n", ns->nguid);
2079}
2080static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2081
Keith Busch2b9b6e82015-12-22 10:10:45 -07002082static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2083 char *buf)
2084{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002085 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002086
2087 /* For backward compatibility expose the NGUID to userspace if
2088 * we have no UUID set
2089 */
2090 if (uuid_is_null(&ns->uuid)) {
2091 printk_ratelimited(KERN_WARNING
2092 "No UUID available providing old NGUID\n");
2093 return sprintf(buf, "%pU\n", ns->nguid);
2094 }
2095 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002096}
2097static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2098
2099static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2100 char *buf)
2101{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002102 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002103 return sprintf(buf, "%8phd\n", ns->eui);
2104}
2105static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2106
2107static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2108 char *buf)
2109{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002110 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002111 return sprintf(buf, "%d\n", ns->ns_id);
2112}
2113static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2114
2115static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002116 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002117 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002118 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002119 &dev_attr_eui.attr,
2120 &dev_attr_nsid.attr,
2121 NULL,
2122};
2123
Ming Lin1a353d82016-06-13 16:45:24 +02002124static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002125 struct attribute *a, int n)
2126{
2127 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002128 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002129
2130 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002131 if (uuid_is_null(&ns->uuid) ||
2132 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2133 return 0;
2134 }
2135 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002136 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002137 return 0;
2138 }
2139 if (a == &dev_attr_eui.attr) {
2140 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2141 return 0;
2142 }
2143 return a->mode;
2144}
2145
2146static const struct attribute_group nvme_ns_attr_group = {
2147 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002148 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002149};
2150
Ming Lin931e1c22016-02-26 13:24:19 -08002151#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002152static ssize_t field##_show(struct device *dev, \
2153 struct device_attribute *attr, char *buf) \
2154{ \
2155 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2156 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2157} \
2158static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2159
Ming Lin931e1c22016-02-26 13:24:19 -08002160#define nvme_show_int_function(field) \
2161static ssize_t field##_show(struct device *dev, \
2162 struct device_attribute *attr, char *buf) \
2163{ \
2164 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2165 return sprintf(buf, "%d\n", ctrl->field); \
2166} \
2167static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2168
2169nvme_show_str_function(model);
2170nvme_show_str_function(serial);
2171nvme_show_str_function(firmware_rev);
2172nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002173
Ming Lin1a353d82016-06-13 16:45:24 +02002174static ssize_t nvme_sysfs_delete(struct device *dev,
2175 struct device_attribute *attr, const char *buf,
2176 size_t count)
2177{
2178 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2179
2180 if (device_remove_file_self(dev, attr))
2181 ctrl->ops->delete_ctrl(ctrl);
2182 return count;
2183}
2184static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2185
2186static ssize_t nvme_sysfs_show_transport(struct device *dev,
2187 struct device_attribute *attr,
2188 char *buf)
2189{
2190 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2191
2192 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2193}
2194static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2195
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002196static ssize_t nvme_sysfs_show_state(struct device *dev,
2197 struct device_attribute *attr,
2198 char *buf)
2199{
2200 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2201 static const char *const state_name[] = {
2202 [NVME_CTRL_NEW] = "new",
2203 [NVME_CTRL_LIVE] = "live",
2204 [NVME_CTRL_RESETTING] = "resetting",
2205 [NVME_CTRL_RECONNECTING]= "reconnecting",
2206 [NVME_CTRL_DELETING] = "deleting",
2207 [NVME_CTRL_DEAD] = "dead",
2208 };
2209
2210 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2211 state_name[ctrl->state])
2212 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2213
2214 return sprintf(buf, "unknown state\n");
2215}
2216
2217static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2218
Ming Lin1a353d82016-06-13 16:45:24 +02002219static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2220 struct device_attribute *attr,
2221 char *buf)
2222{
2223 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2224
Christoph Hellwig180de0072017-06-26 12:39:02 +02002225 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002226}
2227static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2228
2229static ssize_t nvme_sysfs_show_address(struct device *dev,
2230 struct device_attribute *attr,
2231 char *buf)
2232{
2233 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2234
2235 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2236}
2237static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2238
Keith Busch779ff7562016-01-12 15:09:31 -07002239static struct attribute *nvme_dev_attrs[] = {
2240 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002241 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002242 &dev_attr_model.attr,
2243 &dev_attr_serial.attr,
2244 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002245 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002246 &dev_attr_delete_controller.attr,
2247 &dev_attr_transport.attr,
2248 &dev_attr_subsysnqn.attr,
2249 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002250 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002251 NULL
2252};
2253
Ming Lin1a353d82016-06-13 16:45:24 +02002254static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2255 struct attribute *a, int n)
2256{
2257 struct device *dev = container_of(kobj, struct device, kobj);
2258 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2259
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002260 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2261 return 0;
2262 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2263 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002264
2265 return a->mode;
2266}
2267
Keith Busch779ff7562016-01-12 15:09:31 -07002268static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002269 .attrs = nvme_dev_attrs,
2270 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002271};
2272
2273static const struct attribute_group *nvme_dev_attr_groups[] = {
2274 &nvme_dev_attrs_group,
2275 NULL,
2276};
2277
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002278static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2279{
2280 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2281 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2282
2283 return nsa->ns_id - nsb->ns_id;
2284}
2285
Keith Busch32f0c4a2016-07-13 11:45:02 -06002286static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002287{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002288 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002289
Keith Busch32f0c4a2016-07-13 11:45:02 -06002290 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002291 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002292 if (ns->ns_id == nsid) {
2293 kref_get(&ns->kref);
2294 ret = ns;
2295 break;
2296 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002297 if (ns->ns_id > nsid)
2298 break;
2299 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002300 mutex_unlock(&ctrl->namespaces_mutex);
2301 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002302}
2303
Jens Axboef5d11842017-06-27 12:03:06 -06002304static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2305{
2306 struct streams_directive_params s;
2307 int ret;
2308
2309 if (!ctrl->nr_streams)
2310 return 0;
2311
2312 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2313 if (ret)
2314 return ret;
2315
2316 ns->sws = le32_to_cpu(s.sws);
2317 ns->sgs = le16_to_cpu(s.sgs);
2318
2319 if (ns->sws) {
2320 unsigned int bs = 1 << ns->lba_shift;
2321
2322 blk_queue_io_min(ns->queue, bs * ns->sws);
2323 if (ns->sgs)
2324 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2325 }
2326
2327 return 0;
2328}
2329
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002330static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2331{
2332 struct nvme_ns *ns;
2333 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002334 struct nvme_id_ns *id;
2335 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002336 int node = dev_to_node(ctrl->dev);
2337
2338 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2339 if (!ns)
2340 return;
2341
Keith Busch075790e2016-02-24 09:15:53 -07002342 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2343 if (ns->instance < 0)
2344 goto out_free_ns;
2345
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002346 ns->queue = blk_mq_init_queue(ctrl->tagset);
2347 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002348 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002349 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2350 ns->queue->queuedata = ns;
2351 ns->ctrl = ctrl;
2352
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002353 kref_init(&ns->kref);
2354 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002355 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002356
2357 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002358 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002359 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002360
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002361 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002362
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002363 if (nvme_revalidate_ns(ns, &id))
2364 goto out_free_queue;
2365
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002366 if (nvme_nvm_ns_supported(ns, id) &&
2367 nvme_nvm_register(ns, disk_name, node)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002368 dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002369 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002370 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002371
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002372 disk = alloc_disk_node(0, node);
2373 if (!disk)
2374 goto out_free_id;
2375
2376 disk->fops = &nvme_fops;
2377 disk->private_data = ns;
2378 disk->queue = ns->queue;
2379 disk->flags = GENHD_FL_EXT_DEVT;
2380 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2381 ns->disk = disk;
2382
2383 __nvme_revalidate_disk(disk, id);
2384
Keith Busch32f0c4a2016-07-13 11:45:02 -06002385 mutex_lock(&ctrl->namespaces_mutex);
2386 list_add_tail(&ns->list, &ctrl->namespaces);
2387 mutex_unlock(&ctrl->namespaces_mutex);
2388
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002389 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002390
2391 kfree(id);
2392
Dan Williams0d52c7562016-06-15 19:44:20 -07002393 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002394 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2395 &nvme_ns_attr_group))
2396 pr_warn("%s: failed to create sysfs group for identification\n",
2397 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002398 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2399 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2400 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002401 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002402 out_free_id:
2403 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002404 out_free_queue:
2405 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002406 out_release_instance:
2407 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002408 out_free_ns:
2409 kfree(ns);
2410}
2411
2412static void nvme_ns_remove(struct nvme_ns *ns)
2413{
Keith Busch646017a2016-02-24 09:15:54 -07002414 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2415 return;
2416
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002417 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002418 if (blk_get_integrity(ns->disk))
2419 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002420 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2421 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002422 if (ns->ndev)
2423 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002424 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002425 blk_cleanup_queue(ns->queue);
2426 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002427
2428 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002429 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002430 mutex_unlock(&ns->ctrl->namespaces_mutex);
2431
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002432 nvme_put_ns(ns);
2433}
2434
Keith Busch540c8012015-10-22 15:45:06 -06002435static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2436{
2437 struct nvme_ns *ns;
2438
Keith Busch32f0c4a2016-07-13 11:45:02 -06002439 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002440 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002441 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002442 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002443 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002444 } else
2445 nvme_alloc_ns(ctrl, nsid);
2446}
2447
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302448static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2449 unsigned nsid)
2450{
2451 struct nvme_ns *ns, *next;
2452
2453 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2454 if (ns->ns_id > nsid)
2455 nvme_ns_remove(ns);
2456 }
2457}
2458
Keith Busch540c8012015-10-22 15:45:06 -06002459static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2460{
2461 struct nvme_ns *ns;
2462 __le32 *ns_list;
2463 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2464 int ret = 0;
2465
2466 ns_list = kzalloc(0x1000, GFP_KERNEL);
2467 if (!ns_list)
2468 return -ENOMEM;
2469
2470 for (i = 0; i < num_lists; i++) {
2471 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2472 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302473 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002474
2475 for (j = 0; j < min(nn, 1024U); j++) {
2476 nsid = le32_to_cpu(ns_list[j]);
2477 if (!nsid)
2478 goto out;
2479
2480 nvme_validate_ns(ctrl, nsid);
2481
2482 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002483 ns = nvme_find_get_ns(ctrl, prev);
2484 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002485 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002486 nvme_put_ns(ns);
2487 }
Keith Busch540c8012015-10-22 15:45:06 -06002488 }
2489 }
2490 nn -= j;
2491 }
2492 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302493 nvme_remove_invalid_namespaces(ctrl, prev);
2494 free:
Keith Busch540c8012015-10-22 15:45:06 -06002495 kfree(ns_list);
2496 return ret;
2497}
2498
Christoph Hellwig5955be22016-04-26 13:51:59 +02002499static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002500{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002501 unsigned i;
2502
Keith Busch540c8012015-10-22 15:45:06 -06002503 for (i = 1; i <= nn; i++)
2504 nvme_validate_ns(ctrl, i);
2505
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302506 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002507}
2508
Christoph Hellwig5955be22016-04-26 13:51:59 +02002509static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002510{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002511 struct nvme_ctrl *ctrl =
2512 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002513 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002514 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002515
Christoph Hellwig5955be22016-04-26 13:51:59 +02002516 if (ctrl->state != NVME_CTRL_LIVE)
2517 return;
2518
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002519 if (nvme_identify_ctrl(ctrl, &id))
2520 return;
Keith Busch540c8012015-10-22 15:45:06 -06002521
2522 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002523 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002524 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2525 if (!nvme_scan_ns_list(ctrl, nn))
2526 goto done;
2527 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002528 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002529 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002530 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002531 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002532 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002533 kfree(id);
2534}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002535
2536void nvme_queue_scan(struct nvme_ctrl *ctrl)
2537{
2538 /*
2539 * Do not queue new scan work when a controller is reset during
2540 * removal.
2541 */
2542 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002543 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002544}
2545EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002546
Keith Busch32f0c4a2016-07-13 11:45:02 -06002547/*
2548 * This function iterates the namespace list unlocked to allow recovery from
2549 * controller failure. It is up to the caller to ensure the namespace list is
2550 * not modified by scan work while this function is executing.
2551 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002552void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2553{
2554 struct nvme_ns *ns, *next;
2555
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002556 /*
2557 * The dead states indicates the controller was not gracefully
2558 * disconnected. In that case, we won't be able to flush any data while
2559 * removing the namespaces' disks; fail all the queues now to avoid
2560 * potentially having to clean up the failed sync later.
2561 */
2562 if (ctrl->state == NVME_CTRL_DEAD)
2563 nvme_kill_queues(ctrl);
2564
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002565 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2566 nvme_ns_remove(ns);
2567}
Ming Lin576d55d2016-02-10 10:03:32 -08002568EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002569
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002570static void nvme_async_event_work(struct work_struct *work)
2571{
2572 struct nvme_ctrl *ctrl =
2573 container_of(work, struct nvme_ctrl, async_event_work);
2574
2575 spin_lock_irq(&ctrl->lock);
2576 while (ctrl->event_limit > 0) {
2577 int aer_idx = --ctrl->event_limit;
2578
2579 spin_unlock_irq(&ctrl->lock);
2580 ctrl->ops->submit_async_event(ctrl, aer_idx);
2581 spin_lock_irq(&ctrl->lock);
2582 }
2583 spin_unlock_irq(&ctrl->lock);
2584}
2585
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302586static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2587{
2588
2589 u32 csts;
2590
2591 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2592 return false;
2593
2594 if (csts == ~0)
2595 return false;
2596
2597 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2598}
2599
2600static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2601{
2602 struct nvme_command c = { };
2603 struct nvme_fw_slot_info_log *log;
2604
2605 log = kmalloc(sizeof(*log), GFP_KERNEL);
2606 if (!log)
2607 return;
2608
2609 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302610 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302611 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2612
2613 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2614 dev_warn(ctrl->device,
2615 "Get FW SLOT INFO log error\n");
2616 kfree(log);
2617}
2618
2619static void nvme_fw_act_work(struct work_struct *work)
2620{
2621 struct nvme_ctrl *ctrl = container_of(work,
2622 struct nvme_ctrl, fw_act_work);
2623 unsigned long fw_act_timeout;
2624
2625 if (ctrl->mtfa)
2626 fw_act_timeout = jiffies +
2627 msecs_to_jiffies(ctrl->mtfa * 100);
2628 else
2629 fw_act_timeout = jiffies +
2630 msecs_to_jiffies(admin_timeout * 1000);
2631
2632 nvme_stop_queues(ctrl);
2633 while (nvme_ctrl_pp_status(ctrl)) {
2634 if (time_after(jiffies, fw_act_timeout)) {
2635 dev_warn(ctrl->device,
2636 "Fw activation timeout, reset controller\n");
2637 nvme_reset_ctrl(ctrl);
2638 break;
2639 }
2640 msleep(100);
2641 }
2642
2643 if (ctrl->state != NVME_CTRL_LIVE)
2644 return;
2645
2646 nvme_start_queues(ctrl);
2647 /* read FW slot informationi to clear the AER*/
2648 nvme_get_fw_slot_info(ctrl);
2649}
2650
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002651void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2652 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002653{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002654 u32 result = le32_to_cpu(res->u32);
2655 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002656
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002657 switch (le16_to_cpu(status) >> 1) {
2658 case NVME_SC_SUCCESS:
2659 done = false;
2660 /*FALLTHRU*/
2661 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002662 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002663 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002664 break;
2665 default:
2666 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002667 }
2668
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002669 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002670 return;
2671
2672 switch (result & 0xff07) {
2673 case NVME_AER_NOTICE_NS_CHANGED:
2674 dev_info(ctrl->device, "rescanning\n");
2675 nvme_queue_scan(ctrl);
2676 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302677 case NVME_AER_NOTICE_FW_ACT_STARTING:
2678 schedule_work(&ctrl->fw_act_work);
2679 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002680 default:
2681 dev_warn(ctrl->device, "async event result %08x\n", result);
2682 }
2683}
2684EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2685
2686void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2687{
2688 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002689 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002690}
2691EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2692
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002693static DEFINE_IDA(nvme_instance_ida);
2694
2695static int nvme_set_instance(struct nvme_ctrl *ctrl)
2696{
2697 int instance, error;
2698
2699 do {
2700 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2701 return -ENODEV;
2702
2703 spin_lock(&dev_list_lock);
2704 error = ida_get_new(&nvme_instance_ida, &instance);
2705 spin_unlock(&dev_list_lock);
2706 } while (error == -EAGAIN);
2707
2708 if (error)
2709 return -ENODEV;
2710
2711 ctrl->instance = instance;
2712 return 0;
2713}
2714
2715static void nvme_release_instance(struct nvme_ctrl *ctrl)
2716{
2717 spin_lock(&dev_list_lock);
2718 ida_remove(&nvme_instance_ida, ctrl->instance);
2719 spin_unlock(&dev_list_lock);
2720}
2721
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002722void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002723{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002724 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002725 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002726 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302727 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002728}
2729EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002730
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002731void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2732{
2733 if (ctrl->kato)
2734 nvme_start_keep_alive(ctrl);
2735
2736 if (ctrl->queue_count > 1) {
2737 nvme_queue_scan(ctrl);
2738 nvme_queue_async_events(ctrl);
2739 nvme_start_queues(ctrl);
2740 }
2741}
2742EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2743
2744void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2745{
Keith Busch53029b02015-11-28 15:41:02 +01002746 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002747
2748 spin_lock(&dev_list_lock);
2749 list_del(&ctrl->node);
2750 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002751}
Ming Lin576d55d2016-02-10 10:03:32 -08002752EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002753
2754static void nvme_free_ctrl(struct kref *kref)
2755{
2756 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002757
2758 put_device(ctrl->device);
2759 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002760 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002761
2762 ctrl->ops->free_ctrl(ctrl);
2763}
2764
2765void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2766{
2767 kref_put(&ctrl->kref, nvme_free_ctrl);
2768}
Ming Lin576d55d2016-02-10 10:03:32 -08002769EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002770
2771/*
2772 * Initialize a NVMe controller structures. This needs to be called during
2773 * earliest initialization so that we have the initialized structured around
2774 * during probing.
2775 */
2776int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2777 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2778{
2779 int ret;
2780
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002781 ctrl->state = NVME_CTRL_NEW;
2782 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002783 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002784 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002785 kref_init(&ctrl->kref);
2786 ctrl->dev = dev;
2787 ctrl->ops = ops;
2788 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002789 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002790 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302791 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002792
2793 ret = nvme_set_instance(ctrl);
2794 if (ret)
2795 goto out;
2796
Keith Busch779ff7562016-01-12 15:09:31 -07002797 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002798 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002799 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002800 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002801 if (IS_ERR(ctrl->device)) {
2802 ret = PTR_ERR(ctrl->device);
2803 goto out_release_instance;
2804 }
2805 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002806 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002807
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002808 spin_lock(&dev_list_lock);
2809 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2810 spin_unlock(&dev_list_lock);
2811
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002812 /*
2813 * Initialize latency tolerance controls. The sysfs files won't
2814 * be visible to userspace unless the device actually supports APST.
2815 */
2816 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2817 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2818 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2819
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002820 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002821out_release_instance:
2822 nvme_release_instance(ctrl);
2823out:
2824 return ret;
2825}
Ming Lin576d55d2016-02-10 10:03:32 -08002826EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002827
Keith Busch69d9a992016-02-24 09:15:56 -07002828/**
2829 * nvme_kill_queues(): Ends all namespace queues
2830 * @ctrl: the dead controller that needs to end
2831 *
2832 * Call this function when the driver determines it is unable to get the
2833 * controller in a state capable of servicing IO.
2834 */
2835void nvme_kill_queues(struct nvme_ctrl *ctrl)
2836{
2837 struct nvme_ns *ns;
2838
Keith Busch32f0c4a2016-07-13 11:45:02 -06002839 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002840
Ming Lei443bd902017-06-19 10:21:08 +08002841 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002842 if (ctrl->admin_q)
2843 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002844
Keith Busch32f0c4a2016-07-13 11:45:02 -06002845 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002846 /*
2847 * Revalidating a dead namespace sets capacity to 0. This will
2848 * end buffered writers dirtying pages that can't be synced.
2849 */
Keith Buschf33447b2017-02-10 18:15:51 -05002850 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2851 continue;
2852 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002853 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002854
Ming Lei443bd902017-06-19 10:21:08 +08002855 /* Forcibly unquiesce queues to avoid blocking dispatch */
2856 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002857 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002858 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002859}
Linus Torvalds237045f2016-03-18 17:13:31 -07002860EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002861
Keith Busch302ad8c2017-03-01 14:22:12 -05002862void nvme_unfreeze(struct nvme_ctrl *ctrl)
2863{
2864 struct nvme_ns *ns;
2865
2866 mutex_lock(&ctrl->namespaces_mutex);
2867 list_for_each_entry(ns, &ctrl->namespaces, list)
2868 blk_mq_unfreeze_queue(ns->queue);
2869 mutex_unlock(&ctrl->namespaces_mutex);
2870}
2871EXPORT_SYMBOL_GPL(nvme_unfreeze);
2872
2873void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2874{
2875 struct nvme_ns *ns;
2876
2877 mutex_lock(&ctrl->namespaces_mutex);
2878 list_for_each_entry(ns, &ctrl->namespaces, list) {
2879 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2880 if (timeout <= 0)
2881 break;
2882 }
2883 mutex_unlock(&ctrl->namespaces_mutex);
2884}
2885EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2886
2887void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2888{
2889 struct nvme_ns *ns;
2890
2891 mutex_lock(&ctrl->namespaces_mutex);
2892 list_for_each_entry(ns, &ctrl->namespaces, list)
2893 blk_mq_freeze_queue_wait(ns->queue);
2894 mutex_unlock(&ctrl->namespaces_mutex);
2895}
2896EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2897
2898void nvme_start_freeze(struct nvme_ctrl *ctrl)
2899{
2900 struct nvme_ns *ns;
2901
2902 mutex_lock(&ctrl->namespaces_mutex);
2903 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002904 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002905 mutex_unlock(&ctrl->namespaces_mutex);
2906}
2907EXPORT_SYMBOL_GPL(nvme_start_freeze);
2908
Keith Busch25646262016-01-04 09:10:57 -07002909void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002910{
2911 struct nvme_ns *ns;
2912
Keith Busch32f0c4a2016-07-13 11:45:02 -06002913 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002914 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002915 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002916 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002917}
Ming Lin576d55d2016-02-10 10:03:32 -08002918EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002919
Keith Busch25646262016-01-04 09:10:57 -07002920void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002921{
2922 struct nvme_ns *ns;
2923
Keith Busch32f0c4a2016-07-13 11:45:02 -06002924 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002925 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002926 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002927 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002928}
Ming Lin576d55d2016-02-10 10:03:32 -08002929EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002930
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002931int __init nvme_core_init(void)
2932{
2933 int result;
2934
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002935 nvme_wq = alloc_workqueue("nvme-wq",
2936 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2937 if (!nvme_wq)
2938 return -ENOMEM;
2939
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002940 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2941 &nvme_dev_fops);
2942 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002943 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002944 else if (result > 0)
2945 nvme_char_major = result;
2946
2947 nvme_class = class_create(THIS_MODULE, "nvme");
2948 if (IS_ERR(nvme_class)) {
2949 result = PTR_ERR(nvme_class);
2950 goto unregister_chrdev;
2951 }
2952
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002953 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002954
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002955unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002956 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002957destroy_wq:
2958 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002959 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002960}
2961
2962void nvme_core_exit(void)
2963{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002964 class_destroy(nvme_class);
2965 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002966 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002967}
Ming Lin576d55d2016-02-10 10:03:32 -08002968
2969MODULE_LICENSE("GPL");
2970MODULE_VERSION("1.0");
2971module_init(nvme_core_init);
2972module_exit(nvme_core_exit);