blob: b0dd58db110ed3c4ceabe4c4444c36641ccd6f4b [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Ming Linba0ba7d2016-02-10 10:03:30 -080037unsigned char admin_timeout = 60;
38module_param(admin_timeout, byte, 0644);
39MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
42unsigned char nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, byte, 0644);
44MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020047static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080048module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Christoph Hellwig44e44b22017-04-05 19:18:11 +020051static u8 nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070053MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010054
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010055static int nvme_char_major;
56module_param(nvme_char_major, int, 0);
57
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080058static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059module_param(default_ps_max_latency_us, ulong, 0644);
60MODULE_PARM_DESC(default_ps_max_latency_us,
61 "max power saving latency for new devices; use PM QOS to change per device");
62
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070063static bool force_apst;
64module_param(force_apst, bool, 0644);
65MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
66
Jens Axboef5d11842017-06-27 12:03:06 -060067static bool streams;
68module_param(streams, bool, 0644);
69MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
70
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020071struct workqueue_struct *nvme_wq;
72EXPORT_SYMBOL_GPL(nvme_wq);
73
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010074static LIST_HEAD(nvme_ctrl_list);
Ming Lin9f2482b2016-02-10 10:03:31 -080075static DEFINE_SPINLOCK(dev_list_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010076
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010077static struct class *nvme_class;
78
Arnav Dawnb6dccf72017-07-12 16:10:40 +053079static __le32 nvme_get_log_dw10(u8 lid, size_t size)
80{
81 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
82}
83
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020084int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
85{
86 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
87 return -EBUSY;
88 if (!queue_work(nvme_wq, &ctrl->reset_work))
89 return -EBUSY;
90 return 0;
91}
92EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
93
94static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
95{
96 int ret;
97
98 ret = nvme_reset_ctrl(ctrl);
99 if (!ret)
100 flush_work(&ctrl->reset_work);
101 return ret;
102}
103
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200104static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200105{
106 switch (nvme_req(req)->status & 0x7ff) {
107 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200108 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200109 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200110 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200111 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200112 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200113 case NVME_SC_WRITE_FAULT:
114 case NVME_SC_READ_ERROR:
115 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200116 case NVME_SC_ACCESS_DENIED:
117 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200118 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200119 case NVME_SC_GUARD_CHECK:
120 case NVME_SC_APPTAG_CHECK:
121 case NVME_SC_REFTAG_CHECK:
122 case NVME_SC_INVALID_PI:
123 return BLK_STS_PROTECTION;
124 case NVME_SC_RESERVATION_CONFLICT:
125 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200126 default:
127 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200128 }
129}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200130
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200131static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200132{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200133 if (blk_noretry_request(req))
134 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200135 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200136 return false;
137 if (jiffies - req->start_time >= req->timeout)
138 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200139 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200140 return false;
141 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200142}
143
144void nvme_complete_rq(struct request *req)
145{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200146 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
147 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300148 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200149 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200150 }
151
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200152 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200153}
154EXPORT_SYMBOL_GPL(nvme_complete_rq);
155
Ming Linc55a2fd2016-05-18 14:05:02 -0700156void nvme_cancel_request(struct request *req, void *data, bool reserved)
157{
158 int status;
159
160 if (!blk_mq_request_started(req))
161 return;
162
163 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
164 "Cancelling I/O %d", req->tag);
165
166 status = NVME_SC_ABORT_REQ;
167 if (blk_queue_dying(req->q))
168 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200169 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200170 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200171
Ming Linc55a2fd2016-05-18 14:05:02 -0700172}
173EXPORT_SYMBOL_GPL(nvme_cancel_request);
174
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200175bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
176 enum nvme_ctrl_state new_state)
177{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300178 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200179 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200180 bool changed = false;
181
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200182 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300183
184 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200185 switch (new_state) {
186 case NVME_CTRL_LIVE:
187 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200188 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200189 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900190 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200191 changed = true;
192 /* FALLTHRU */
193 default:
194 break;
195 }
196 break;
197 case NVME_CTRL_RESETTING:
198 switch (old_state) {
199 case NVME_CTRL_NEW:
200 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900201 changed = true;
202 /* FALLTHRU */
203 default:
204 break;
205 }
206 break;
207 case NVME_CTRL_RECONNECTING:
208 switch (old_state) {
209 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200210 changed = true;
211 /* FALLTHRU */
212 default:
213 break;
214 }
215 break;
216 case NVME_CTRL_DELETING:
217 switch (old_state) {
218 case NVME_CTRL_LIVE:
219 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900220 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200221 changed = true;
222 /* FALLTHRU */
223 default:
224 break;
225 }
226 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600227 case NVME_CTRL_DEAD:
228 switch (old_state) {
229 case NVME_CTRL_DELETING:
230 changed = true;
231 /* FALLTHRU */
232 default:
233 break;
234 }
235 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200236 default:
237 break;
238 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200239
240 if (changed)
241 ctrl->state = new_state;
242
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200243 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300244
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200245 return changed;
246}
247EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
248
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100249static void nvme_free_ns(struct kref *kref)
250{
251 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
252
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200253 if (ns->ndev)
254 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100255
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200256 if (ns->disk) {
257 spin_lock(&dev_list_lock);
258 ns->disk->private_data = NULL;
259 spin_unlock(&dev_list_lock);
260 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100261
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100262 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700263 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
264 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100265 kfree(ns);
266}
267
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100268static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100269{
270 kref_put(&ns->kref, nvme_free_ns);
271}
272
273static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
274{
275 struct nvme_ns *ns;
276
277 spin_lock(&dev_list_lock);
278 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800279 if (ns) {
280 if (!kref_get_unless_zero(&ns->kref))
281 goto fail;
282 if (!try_module_get(ns->ctrl->ops->module))
283 goto fail_put_ns;
284 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100285 spin_unlock(&dev_list_lock);
286
287 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800288
289fail_put_ns:
290 kref_put(&ns->kref, nvme_free_ns);
291fail:
292 spin_unlock(&dev_list_lock);
293 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100294}
295
Christoph Hellwig41609822015-11-20 09:00:02 +0100296struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200297 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100298{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100299 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100300 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100301
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200302 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100303 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200304 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100305 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200306 qid ? qid - 1 : 0);
307 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100308 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100309 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100310
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800312 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100313
Christoph Hellwig41609822015-11-20 09:00:02 +0100314 return req;
315}
Ming Lin576d55d2016-02-10 10:03:32 -0800316EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100317
Jens Axboef5d11842017-06-27 12:03:06 -0600318static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
319{
320 struct nvme_command c;
321
322 memset(&c, 0, sizeof(c));
323
324 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530325 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600326 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
327 c.directive.dtype = NVME_DIR_IDENTIFY;
328 c.directive.tdtype = NVME_DIR_STREAMS;
329 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
330
331 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
332}
333
334static int nvme_disable_streams(struct nvme_ctrl *ctrl)
335{
336 return nvme_toggle_streams(ctrl, false);
337}
338
339static int nvme_enable_streams(struct nvme_ctrl *ctrl)
340{
341 return nvme_toggle_streams(ctrl, true);
342}
343
344static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
345 struct streams_directive_params *s, u32 nsid)
346{
347 struct nvme_command c;
348
349 memset(&c, 0, sizeof(c));
350 memset(s, 0, sizeof(*s));
351
352 c.directive.opcode = nvme_admin_directive_recv;
353 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700354 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600355 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
356 c.directive.dtype = NVME_DIR_STREAMS;
357
358 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
359}
360
361static int nvme_configure_directives(struct nvme_ctrl *ctrl)
362{
363 struct streams_directive_params s;
364 int ret;
365
366 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
367 return 0;
368 if (!streams)
369 return 0;
370
371 ret = nvme_enable_streams(ctrl);
372 if (ret)
373 return ret;
374
Arnav Dawn62346ea2017-07-12 16:11:53 +0530375 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600376 if (ret)
377 return ret;
378
379 ctrl->nssa = le16_to_cpu(s.nssa);
380 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
381 dev_info(ctrl->device, "too few streams (%u) available\n",
382 ctrl->nssa);
383 nvme_disable_streams(ctrl);
384 return 0;
385 }
386
387 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
388 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
389 return 0;
390}
391
392/*
393 * Check if 'req' has a write hint associated with it. If it does, assign
394 * a valid namespace stream to the write.
395 */
396static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
397 struct request *req, u16 *control,
398 u32 *dsmgmt)
399{
400 enum rw_hint streamid = req->write_hint;
401
402 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
403 streamid = 0;
404 else {
405 streamid--;
406 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
407 return;
408
409 *control |= NVME_RW_DTYPE_STREAMS;
410 *dsmgmt |= streamid << 16;
411 }
412
413 if (streamid < ARRAY_SIZE(req->q->write_hints))
414 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
415}
416
Ming Lin8093f7c2016-04-12 13:10:14 -0600417static inline void nvme_setup_flush(struct nvme_ns *ns,
418 struct nvme_command *cmnd)
419{
420 memset(cmnd, 0, sizeof(*cmnd));
421 cmnd->common.opcode = nvme_cmd_flush;
422 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
423}
424
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200425static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600426 struct nvme_command *cmnd)
427{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100428 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600429 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100430 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600431
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100432 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600433 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200434 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600435
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100436 __rq_for_each_bio(bio, req) {
437 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
438 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
439
440 range[n].cattr = cpu_to_le32(0);
441 range[n].nlb = cpu_to_le32(nlb);
442 range[n].slba = cpu_to_le64(slba);
443 n++;
444 }
445
446 if (WARN_ON_ONCE(n != segments)) {
447 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200448 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100449 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600450
451 memset(cmnd, 0, sizeof(*cmnd));
452 cmnd->dsm.opcode = nvme_cmd_dsm;
453 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200454 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600455 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
456
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700457 req->special_vec.bv_page = virt_to_page(range);
458 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100459 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700460 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600461
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200462 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600463}
Ming Lin8093f7c2016-04-12 13:10:14 -0600464
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200465static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
466 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600467{
Jens Axboef5d11842017-06-27 12:03:06 -0600468 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600469 u16 control = 0;
470 u32 dsmgmt = 0;
471
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200472 /*
473 * If formated with metadata, require the block layer provide a buffer
474 * unless this namespace is formated such that the metadata can be
475 * stripped/generated by the controller with PRACT=1.
476 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300477 if (ns && ns->ms &&
478 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200479 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
480 return BLK_STS_NOTSUPP;
481
Ming Lin8093f7c2016-04-12 13:10:14 -0600482 if (req->cmd_flags & REQ_FUA)
483 control |= NVME_RW_FUA;
484 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
485 control |= NVME_RW_LR;
486
487 if (req->cmd_flags & REQ_RAHEAD)
488 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
489
490 memset(cmnd, 0, sizeof(*cmnd));
491 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600492 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
493 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
494 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
495
Jens Axboef5d11842017-06-27 12:03:06 -0600496 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
497 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
498
Ming Lin8093f7c2016-04-12 13:10:14 -0600499 if (ns->ms) {
500 switch (ns->pi_type) {
501 case NVME_NS_DPS_PI_TYPE3:
502 control |= NVME_RW_PRINFO_PRCHK_GUARD;
503 break;
504 case NVME_NS_DPS_PI_TYPE1:
505 case NVME_NS_DPS_PI_TYPE2:
506 control |= NVME_RW_PRINFO_PRCHK_GUARD |
507 NVME_RW_PRINFO_PRCHK_REF;
508 cmnd->rw.reftag = cpu_to_le32(
509 nvme_block_nr(ns, blk_rq_pos(req)));
510 break;
511 }
512 if (!blk_integrity_rq(req))
513 control |= NVME_RW_PRINFO_PRACT;
514 }
515
516 cmnd->rw.control = cpu_to_le16(control);
517 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200518 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600519}
520
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200521blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600522 struct nvme_command *cmd)
523{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200524 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600525
Christoph Hellwig987f6992017-04-05 19:18:08 +0200526 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200527 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200528 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200529 req->rq_flags |= RQF_DONTPREP;
530 }
531
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100532 switch (req_op(req)) {
533 case REQ_OP_DRV_IN:
534 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800535 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100536 break;
537 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600538 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100539 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200540 case REQ_OP_WRITE_ZEROES:
541 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600543 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100544 break;
545 case REQ_OP_READ:
546 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200547 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100548 break;
549 default:
550 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200551 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100552 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600553
James Smart721b3912016-10-21 23:33:34 +0300554 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600555 return ret;
556}
557EXPORT_SYMBOL_GPL(nvme_setup_cmd);
558
Christoph Hellwig41609822015-11-20 09:00:02 +0100559/*
560 * Returns 0 on success. If the result is negative, it's a Linux error code;
561 * if the result is positive, it's an NVM Express status code
562 */
563int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800564 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200565 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100566{
567 struct request *req;
568 int ret;
569
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200570 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100571 if (IS_ERR(req))
572 return PTR_ERR(req);
573
574 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
575
Christoph Hellwig21d34712015-11-26 09:08:36 +0100576 if (buffer && bufflen) {
577 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
578 if (ret)
579 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100580 }
581
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200582 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800583 if (result)
584 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200585 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
586 ret = -EINTR;
587 else
588 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100589 out:
590 blk_mq_free_request(req);
591 return ret;
592}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200593EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100594
595int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
596 void *buffer, unsigned bufflen)
597{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200598 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
599 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100600}
Ming Lin576d55d2016-02-10 10:03:32 -0800601EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100602
Keith Busch0b7f1f22015-10-23 09:47:28 -0600603int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
604 void __user *ubuffer, unsigned bufflen,
605 void __user *meta_buffer, unsigned meta_len, u32 meta_seed,
606 u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100607{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200608 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600609 struct nvme_ns *ns = q->queuedata;
610 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100611 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600612 struct bio *bio = NULL;
613 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100614 int ret;
615
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200616 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100617 if (IS_ERR(req))
618 return PTR_ERR(req);
619
620 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
621
622 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100623 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
624 GFP_KERNEL);
625 if (ret)
626 goto out;
627 bio = req->bio;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100628
Keith Busch0b7f1f22015-10-23 09:47:28 -0600629 if (!disk)
630 goto submit;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200631 bio->bi_disk = disk;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600632
Keith Busche9fc63d2016-02-24 09:15:58 -0700633 if (meta_buffer && meta_len) {
Keith Busch0b7f1f22015-10-23 09:47:28 -0600634 struct bio_integrity_payload *bip;
635
636 meta = kmalloc(meta_len, GFP_KERNEL);
637 if (!meta) {
638 ret = -ENOMEM;
639 goto out_unmap;
640 }
641
642 if (write) {
643 if (copy_from_user(meta, meta_buffer,
644 meta_len)) {
645 ret = -EFAULT;
646 goto out_free_meta;
647 }
648 }
649
650 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
Keith Busch06c1e392015-12-03 09:32:21 -0700651 if (IS_ERR(bip)) {
652 ret = PTR_ERR(bip);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600653 goto out_free_meta;
654 }
655
656 bip->bip_iter.bi_size = meta_len;
657 bip->bip_iter.bi_sector = meta_seed;
658
659 ret = bio_integrity_add_page(bio, virt_to_page(meta),
660 meta_len, offset_in_page(meta));
661 if (ret != meta_len) {
662 ret = -ENOMEM;
663 goto out_free_meta;
664 }
665 }
666 }
667 submit:
668 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200669 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
670 ret = -EINTR;
671 else
672 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100673 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800674 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600675 if (meta && !ret && !write) {
676 if (copy_to_user(meta_buffer, meta, meta_len))
677 ret = -EFAULT;
678 }
679 out_free_meta:
680 kfree(meta);
681 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200682 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600683 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100684 out:
685 blk_mq_free_request(req);
686 return ret;
687}
688
Keith Busch0b7f1f22015-10-23 09:47:28 -0600689int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
690 void __user *ubuffer, unsigned bufflen, u32 *result,
691 unsigned timeout)
692{
693 return __nvme_submit_user_cmd(q, cmd, ubuffer, bufflen, NULL, 0, 0,
694 result, timeout);
695}
696
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200697static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200698{
699 struct nvme_ctrl *ctrl = rq->end_io_data;
700
701 blk_mq_free_request(rq);
702
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200703 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200704 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705 "failed nvme_keep_alive_end_io error=%d\n",
706 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200707 return;
708 }
709
710 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
711}
712
713static int nvme_keep_alive(struct nvme_ctrl *ctrl)
714{
715 struct nvme_command c;
716 struct request *rq;
717
718 memset(&c, 0, sizeof(c));
719 c.common.opcode = nvme_admin_keep_alive;
720
721 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
722 NVME_QID_ANY);
723 if (IS_ERR(rq))
724 return PTR_ERR(rq);
725
726 rq->timeout = ctrl->kato * HZ;
727 rq->end_io_data = ctrl;
728
729 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
730
731 return 0;
732}
733
734static void nvme_keep_alive_work(struct work_struct *work)
735{
736 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
737 struct nvme_ctrl, ka_work);
738
739 if (nvme_keep_alive(ctrl)) {
740 /* allocation failure, reset the controller */
741 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200742 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200743 return;
744 }
745}
746
747void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
748{
749 if (unlikely(ctrl->kato == 0))
750 return;
751
752 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
753 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
754}
755EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
756
757void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
758{
759 if (unlikely(ctrl->kato == 0))
760 return;
761
762 cancel_delayed_work_sync(&ctrl->ka_work);
763}
764EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
765
Keith Busch3f7f25a92017-06-20 15:09:56 -0400766static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100767{
768 struct nvme_command c = { };
769 int error;
770
771 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
772 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200773 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100774
775 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
776 if (!*id)
777 return -ENOMEM;
778
779 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
780 sizeof(struct nvme_id_ctrl));
781 if (error)
782 kfree(*id);
783 return error;
784}
785
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200786static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
787 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200788{
789 struct nvme_command c = { };
790 int status;
791 void *data;
792 int pos;
793 int len;
794
795 c.identify.opcode = nvme_admin_identify;
796 c.identify.nsid = cpu_to_le32(nsid);
797 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
798
799 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
800 if (!data)
801 return -ENOMEM;
802
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200803 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200804 NVME_IDENTIFY_DATA_SIZE);
805 if (status)
806 goto free_data;
807
808 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
809 struct nvme_ns_id_desc *cur = data + pos;
810
811 if (cur->nidl == 0)
812 break;
813
814 switch (cur->nidt) {
815 case NVME_NIDT_EUI64:
816 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200817 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200818 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
819 cur->nidl);
820 goto free_data;
821 }
822 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200823 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200824 break;
825 case NVME_NIDT_NGUID:
826 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200827 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200828 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
829 cur->nidl);
830 goto free_data;
831 }
832 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200833 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200834 break;
835 case NVME_NIDT_UUID:
836 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200837 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200838 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
839 cur->nidl);
840 goto free_data;
841 }
842 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200843 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200844 break;
845 default:
846 /* Skip unnkown types */
847 len = cur->nidl;
848 break;
849 }
850
851 len += sizeof(*cur);
852 }
853free_data:
854 kfree(data);
855 return status;
856}
857
Keith Busch540c8012015-10-22 15:45:06 -0600858static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
859{
860 struct nvme_command c = { };
861
862 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200863 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600864 c.identify.nsid = cpu_to_le32(nsid);
865 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
866}
867
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200868static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
869 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100870{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200871 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100872 struct nvme_command c = { };
873 int error;
874
875 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200876 c.identify.opcode = nvme_admin_identify;
877 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200878 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100879
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200880 id = kmalloc(sizeof(*id), GFP_KERNEL);
881 if (!id)
882 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100883
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200884 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
885 if (error) {
886 dev_warn(ctrl->device, "Identify namespace failed\n");
887 kfree(id);
888 return NULL;
889 }
890
891 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100892}
893
Keith Busch3f7f25a92017-06-20 15:09:56 -0400894static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700895 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100896{
897 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800898 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100899 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100900
901 memset(&c, 0, sizeof(c));
902 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100903 c.features.fid = cpu_to_le32(fid);
904 c.features.dword11 = cpu_to_le32(dword11);
905
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800906 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700907 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700908 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800909 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100910 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100911}
912
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100913int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
914{
915 u32 q_count = (*count - 1) | ((*count - 1) << 16);
916 u32 result;
917 int status, nr_io_queues;
918
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700919 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100920 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200921 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100922 return status;
923
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200924 /*
925 * Degraded controllers might return an error when setting the queue
926 * count. We still want to be able to bring them online and offer
927 * access to the admin queue, as that might be only way to fix them up.
928 */
929 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200930 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200931 *count = 0;
932 } else {
933 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
934 *count = min(*count, nr_io_queues);
935 }
936
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100937 return 0;
938}
Ming Lin576d55d2016-02-10 10:03:32 -0800939EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100940
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100941static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
942{
943 struct nvme_user_io io;
944 struct nvme_command c;
945 unsigned length, meta_len;
946 void __user *metadata;
947
948 if (copy_from_user(&io, uio, sizeof(io)))
949 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700950 if (io.flags)
951 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100952
953 switch (io.opcode) {
954 case nvme_cmd_write:
955 case nvme_cmd_read:
956 case nvme_cmd_compare:
957 break;
958 default:
959 return -EINVAL;
960 }
961
962 length = (io.nblocks + 1) << ns->lba_shift;
963 meta_len = (io.nblocks + 1) * ns->ms;
964 metadata = (void __user *)(uintptr_t)io.metadata;
965
966 if (ns->ext) {
967 length += meta_len;
968 meta_len = 0;
969 } else if (meta_len) {
970 if ((io.metadata & 3) || !io.metadata)
971 return -EINVAL;
972 }
973
974 memset(&c, 0, sizeof(c));
975 c.rw.opcode = io.opcode;
976 c.rw.flags = io.flags;
977 c.rw.nsid = cpu_to_le32(ns->ns_id);
978 c.rw.slba = cpu_to_le64(io.slba);
979 c.rw.length = cpu_to_le16(io.nblocks);
980 c.rw.control = cpu_to_le16(io.control);
981 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
982 c.rw.reftag = cpu_to_le32(io.reftag);
983 c.rw.apptag = cpu_to_le16(io.apptag);
984 c.rw.appmask = cpu_to_le16(io.appmask);
985
986 return __nvme_submit_user_cmd(ns->queue, &c,
987 (void __user *)(uintptr_t)io.addr, length,
988 metadata, meta_len, io.slba, NULL, 0);
989}
990
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100991static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100992 struct nvme_passthru_cmd __user *ucmd)
993{
994 struct nvme_passthru_cmd cmd;
995 struct nvme_command c;
996 unsigned timeout = 0;
997 int status;
998
999 if (!capable(CAP_SYS_ADMIN))
1000 return -EACCES;
1001 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1002 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001003 if (cmd.flags)
1004 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001005
1006 memset(&c, 0, sizeof(c));
1007 c.common.opcode = cmd.opcode;
1008 c.common.flags = cmd.flags;
1009 c.common.nsid = cpu_to_le32(cmd.nsid);
1010 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1011 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1012 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1013 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1014 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1015 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1016 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1017 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1018
1019 if (cmd.timeout_ms)
1020 timeout = msecs_to_jiffies(cmd.timeout_ms);
1021
1022 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001023 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001024 &cmd.result, timeout);
1025 if (status >= 0) {
1026 if (put_user(cmd.result, &ucmd->result))
1027 return -EFAULT;
1028 }
1029
1030 return status;
1031}
1032
1033static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1034 unsigned int cmd, unsigned long arg)
1035{
1036 struct nvme_ns *ns = bdev->bd_disk->private_data;
1037
1038 switch (cmd) {
1039 case NVME_IOCTL_ID:
1040 force_successful_syscall_return();
1041 return ns->ns_id;
1042 case NVME_IOCTL_ADMIN_CMD:
1043 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1044 case NVME_IOCTL_IO_CMD:
1045 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1046 case NVME_IOCTL_SUBMIT_IO:
1047 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001048 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001049#ifdef CONFIG_NVM
1050 if (ns->ndev)
1051 return nvme_nvm_ioctl(ns, cmd, arg);
1052#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001053 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001054 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001055 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001056 return -ENOTTY;
1057 }
1058}
1059
1060#ifdef CONFIG_COMPAT
1061static int nvme_compat_ioctl(struct block_device *bdev, fmode_t mode,
1062 unsigned int cmd, unsigned long arg)
1063{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001064 return nvme_ioctl(bdev, mode, cmd, arg);
1065}
1066#else
1067#define nvme_compat_ioctl NULL
1068#endif
1069
1070static int nvme_open(struct block_device *bdev, fmode_t mode)
1071{
1072 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1073}
1074
1075static void nvme_release(struct gendisk *disk, fmode_t mode)
1076{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001077 struct nvme_ns *ns = disk->private_data;
1078
1079 module_put(ns->ctrl->ops->module);
1080 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001081}
1082
1083static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1084{
1085 /* some standard values */
1086 geo->heads = 1 << 6;
1087 geo->sectors = 1 << 5;
1088 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1089 return 0;
1090}
1091
1092#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001093static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1094 u16 bs)
1095{
1096 struct nvme_ns *ns = disk->private_data;
1097 u16 old_ms = ns->ms;
1098 u8 pi_type = 0;
1099
1100 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1101 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1102
1103 /* PI implementation requires metadata equal t10 pi tuple size */
1104 if (ns->ms == sizeof(struct t10_pi_tuple))
1105 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1106
1107 if (blk_get_integrity(disk) &&
1108 (ns->pi_type != pi_type || ns->ms != old_ms ||
1109 bs != queue_logical_block_size(disk->queue) ||
1110 (ns->ms && ns->ext)))
1111 blk_integrity_unregister(disk);
1112
1113 ns->pi_type = pi_type;
1114}
1115
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001116static void nvme_init_integrity(struct nvme_ns *ns)
1117{
1118 struct blk_integrity integrity;
1119
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001120 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001121 switch (ns->pi_type) {
1122 case NVME_NS_DPS_PI_TYPE3:
1123 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001124 integrity.tag_size = sizeof(u16) + sizeof(u32);
1125 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001126 break;
1127 case NVME_NS_DPS_PI_TYPE1:
1128 case NVME_NS_DPS_PI_TYPE2:
1129 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001130 integrity.tag_size = sizeof(u16);
1131 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001132 break;
1133 default:
1134 integrity.profile = NULL;
1135 break;
1136 }
1137 integrity.tuple_size = ns->ms;
1138 blk_integrity_register(ns->disk, &integrity);
1139 blk_queue_max_integrity_segments(ns->queue, 1);
1140}
1141#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001142static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1143 u16 bs)
1144{
1145}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001146static void nvme_init_integrity(struct nvme_ns *ns)
1147{
1148}
1149#endif /* CONFIG_BLK_DEV_INTEGRITY */
1150
Scott Bauer6b8190d2017-06-15 10:44:30 -06001151static void nvme_set_chunk_size(struct nvme_ns *ns)
1152{
1153 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1154 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1155}
1156
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001157static void nvme_config_discard(struct nvme_ns *ns)
1158{
Keith Busch08095e72016-03-04 13:15:17 -07001159 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001160 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001161
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001162 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1163 NVME_DSM_MAX_RANGES);
1164
Jens Axboef5d11842017-06-27 12:03:06 -06001165 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1166 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1167
1168 ns->queue->limits.discard_alignment = sz;
1169 ns->queue->limits.discard_granularity = sz;
1170 } else {
1171 ns->queue->limits.discard_alignment = logical_block_size;
1172 ns->queue->limits.discard_granularity = logical_block_size;
1173 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001174 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001175 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001176 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001177
1178 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1179 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001180}
1181
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001182static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1183 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001184{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001185 if (ctrl->vs >= NVME_VS(1, 1, 0))
1186 memcpy(eui64, id->eui64, sizeof(id->eui64));
1187 if (ctrl->vs >= NVME_VS(1, 2, 0))
1188 memcpy(nguid, id->nguid, sizeof(id->nguid));
1189 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001190 /* Don't treat error as fatal we potentially
1191 * already have a NGUID or EUI-64
1192 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001193 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1194 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001195 "%s: Identify Descriptors failed\n", __func__);
1196 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001197}
1198
1199static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1200{
1201 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001202 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001203 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001204
1205 /*
1206 * If identify namespace failed, use default 512 byte block size so
1207 * block layer can use before failing read/write for 0 capacity.
1208 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001209 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210 if (ns->lba_shift == 0)
1211 ns->lba_shift = 9;
1212 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001213 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001214
1215 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001216
Jens Axboef5d11842017-06-27 12:03:06 -06001217 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001218 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001219 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001220 if (ns->noiob)
1221 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001222 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001223 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001224 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1225 set_capacity(disk, 0);
1226 else
1227 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1228
Jens Axboef5d11842017-06-27 12:03:06 -06001229 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001230 nvme_config_discard(ns);
1231 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001232}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001233
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001234static int nvme_revalidate_disk(struct gendisk *disk)
1235{
1236 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001237 struct nvme_ctrl *ctrl = ns->ctrl;
1238 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001239 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1240 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001241 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001242
1243 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1244 set_capacity(disk, 0);
1245 return -ENODEV;
1246 }
1247
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001248 id = nvme_identify_ns(ctrl, ns->ns_id);
1249 if (!id)
1250 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001251
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001252 if (id->ncap == 0) {
1253 ret = -ENODEV;
1254 goto out;
1255 }
1256
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001257 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1258 if (!uuid_equal(&ns->uuid, &uuid) ||
1259 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1260 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1261 dev_err(ctrl->device,
1262 "identifiers changed for nsid %d\n", ns->ns_id);
1263 ret = -ENODEV;
1264 }
1265
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001266out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001267 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001268 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001269}
1270
1271static char nvme_pr_type(enum pr_type type)
1272{
1273 switch (type) {
1274 case PR_WRITE_EXCLUSIVE:
1275 return 1;
1276 case PR_EXCLUSIVE_ACCESS:
1277 return 2;
1278 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1279 return 3;
1280 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1281 return 4;
1282 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1283 return 5;
1284 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1285 return 6;
1286 default:
1287 return 0;
1288 }
1289};
1290
1291static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1292 u64 key, u64 sa_key, u8 op)
1293{
1294 struct nvme_ns *ns = bdev->bd_disk->private_data;
1295 struct nvme_command c;
1296 u8 data[16] = { 0, };
1297
1298 put_unaligned_le64(key, &data[0]);
1299 put_unaligned_le64(sa_key, &data[8]);
1300
1301 memset(&c, 0, sizeof(c));
1302 c.common.opcode = op;
1303 c.common.nsid = cpu_to_le32(ns->ns_id);
1304 c.common.cdw10[0] = cpu_to_le32(cdw10);
1305
1306 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1307}
1308
1309static int nvme_pr_register(struct block_device *bdev, u64 old,
1310 u64 new, unsigned flags)
1311{
1312 u32 cdw10;
1313
1314 if (flags & ~PR_FL_IGNORE_KEY)
1315 return -EOPNOTSUPP;
1316
1317 cdw10 = old ? 2 : 0;
1318 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1319 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1320 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1321}
1322
1323static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1324 enum pr_type type, unsigned flags)
1325{
1326 u32 cdw10;
1327
1328 if (flags & ~PR_FL_IGNORE_KEY)
1329 return -EOPNOTSUPP;
1330
1331 cdw10 = nvme_pr_type(type) << 8;
1332 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1333 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1334}
1335
1336static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1337 enum pr_type type, bool abort)
1338{
1339 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1340 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1341}
1342
1343static int nvme_pr_clear(struct block_device *bdev, u64 key)
1344{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001345 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001346 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1347}
1348
1349static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1350{
1351 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1352 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1353}
1354
1355static const struct pr_ops nvme_pr_ops = {
1356 .pr_register = nvme_pr_register,
1357 .pr_reserve = nvme_pr_reserve,
1358 .pr_release = nvme_pr_release,
1359 .pr_preempt = nvme_pr_preempt,
1360 .pr_clear = nvme_pr_clear,
1361};
1362
Scott Bauera98e58e52017-02-03 12:50:32 -07001363#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001364int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1365 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001366{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001367 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001368 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001369
1370 memset(&cmd, 0, sizeof(cmd));
1371 if (send)
1372 cmd.common.opcode = nvme_admin_security_send;
1373 else
1374 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001375 cmd.common.nsid = 0;
1376 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1377 cmd.common.cdw10[1] = cpu_to_le32(len);
1378
1379 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1380 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1381}
1382EXPORT_SYMBOL_GPL(nvme_sec_submit);
1383#endif /* CONFIG_BLK_SED_OPAL */
1384
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001385static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001386 .owner = THIS_MODULE,
1387 .ioctl = nvme_ioctl,
1388 .compat_ioctl = nvme_compat_ioctl,
1389 .open = nvme_open,
1390 .release = nvme_release,
1391 .getgeo = nvme_getgeo,
1392 .revalidate_disk= nvme_revalidate_disk,
1393 .pr_ops = &nvme_pr_ops,
1394};
1395
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001396static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1397{
1398 unsigned long timeout =
1399 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1400 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1401 int ret;
1402
1403 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001404 if (csts == ~0)
1405 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001406 if ((csts & NVME_CSTS_RDY) == bit)
1407 break;
1408
1409 msleep(100);
1410 if (fatal_signal_pending(current))
1411 return -EINTR;
1412 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001413 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001414 "Device not ready; aborting %s\n", enabled ?
1415 "initialisation" : "reset");
1416 return -ENODEV;
1417 }
1418 }
1419
1420 return ret;
1421}
1422
1423/*
1424 * If the device has been passed off to us in an enabled state, just clear
1425 * the enabled bit. The spec says we should set the 'shutdown notification
1426 * bits', but doing so may cause the device to complete commands to the
1427 * admin queue ... and we don't know what memory that might be pointing at!
1428 */
1429int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1430{
1431 int ret;
1432
1433 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1434 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1435
1436 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1437 if (ret)
1438 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001439
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001440 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001441 msleep(NVME_QUIRK_DELAY_AMOUNT);
1442
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001443 return nvme_wait_ready(ctrl, cap, false);
1444}
Ming Lin576d55d2016-02-10 10:03:32 -08001445EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001446
1447int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1448{
1449 /*
1450 * Default to a 4K page size, with the intention to update this
1451 * path in the future to accomodate architectures with differing
1452 * kernel and IO page sizes.
1453 */
1454 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1455 int ret;
1456
1457 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001458 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001459 "Minimum device page size %u too large for host (%u)\n",
1460 1 << dev_page_min, 1 << page_shift);
1461 return -ENODEV;
1462 }
1463
1464 ctrl->page_size = 1 << page_shift;
1465
1466 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1467 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001468 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001469 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1470 ctrl->ctrl_config |= NVME_CC_ENABLE;
1471
1472 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1473 if (ret)
1474 return ret;
1475 return nvme_wait_ready(ctrl, cap, true);
1476}
Ming Lin576d55d2016-02-10 10:03:32 -08001477EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001478
1479int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1480{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001481 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001482 u32 csts;
1483 int ret;
1484
1485 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1486 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1487
1488 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1489 if (ret)
1490 return ret;
1491
1492 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1493 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1494 break;
1495
1496 msleep(100);
1497 if (fatal_signal_pending(current))
1498 return -EINTR;
1499 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001500 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001501 "Device shutdown incomplete; abort shutdown\n");
1502 return -ENODEV;
1503 }
1504 }
1505
1506 return ret;
1507}
Ming Lin576d55d2016-02-10 10:03:32 -08001508EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001509
Christoph Hellwigda358252016-03-02 18:07:11 +01001510static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1511 struct request_queue *q)
1512{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001513 bool vwc = false;
1514
Christoph Hellwigda358252016-03-02 18:07:11 +01001515 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001516 u32 max_segments =
1517 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1518
Christoph Hellwigda358252016-03-02 18:07:11 +01001519 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001520 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001521 }
Keith Busche6282ae2016-12-19 11:37:50 -05001522 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1523 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001524 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001525 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1526 vwc = true;
1527 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001528}
1529
Jon Derrickdbf86b32017-08-16 09:51:29 +02001530static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1531{
1532 __le64 ts;
1533 int ret;
1534
1535 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1536 return 0;
1537
1538 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1539 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1540 NULL);
1541 if (ret)
1542 dev_warn_once(ctrl->device,
1543 "could not set timestamp (%d)\n", ret);
1544 return ret;
1545}
1546
Keith Busch634b8322017-08-10 11:23:31 +02001547static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001548{
1549 /*
1550 * APST (Autonomous Power State Transition) lets us program a
1551 * table of power state transitions that the controller will
1552 * perform automatically. We configure it with a simple
1553 * heuristic: we are willing to spend at most 2% of the time
1554 * transitioning between power states. Therefore, when running
1555 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001556 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001557 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001558 * the requested maximum latency.
1559 *
1560 * We will not autonomously enter any non-operational state for
1561 * which the total latency exceeds ps_max_latency_us. Users
1562 * can set ps_max_latency_us to zero to turn off APST.
1563 */
1564
1565 unsigned apste;
1566 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001567 u64 max_lat_us = 0;
1568 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001569 int ret;
1570
1571 /*
1572 * If APST isn't supported or if we haven't been initialized yet,
1573 * then don't do anything.
1574 */
1575 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001576 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001577
1578 if (ctrl->npss > 31) {
1579 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001580 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001581 }
1582
1583 table = kzalloc(sizeof(*table), GFP_KERNEL);
1584 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001585 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001586
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001587 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001588 /* Turn off APST. */
1589 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001590 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001591 } else {
1592 __le64 target = cpu_to_le64(0);
1593 int state;
1594
1595 /*
1596 * Walk through all states from lowest- to highest-power.
1597 * According to the spec, lower-numbered states use more
1598 * power. NPSS, despite the name, is the index of the
1599 * lowest-power state, not the number of states.
1600 */
1601 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001602 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001603
1604 if (target)
1605 table->entries[state] = target;
1606
1607 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001608 * Don't allow transitions to the deepest state
1609 * if it's quirked off.
1610 */
1611 if (state == ctrl->npss &&
1612 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1613 continue;
1614
1615 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001616 * Is this state a useful non-operational state for
1617 * higher-power states to autonomously transition to?
1618 */
1619 if (!(ctrl->psd[state].flags &
1620 NVME_PS_FLAGS_NON_OP_STATE))
1621 continue;
1622
Kai-Heng Fengda875912017-06-07 15:25:42 +08001623 exit_latency_us =
1624 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1625 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001626 continue;
1627
Kai-Heng Fengda875912017-06-07 15:25:42 +08001628 total_latency_us =
1629 exit_latency_us +
1630 le32_to_cpu(ctrl->psd[state].entry_lat);
1631
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001632 /*
1633 * This state is good. Use it as the APST idle
1634 * target for higher power states.
1635 */
1636 transition_ms = total_latency_us + 19;
1637 do_div(transition_ms, 20);
1638 if (transition_ms > (1 << 24) - 1)
1639 transition_ms = (1 << 24) - 1;
1640
1641 target = cpu_to_le64((state << 3) |
1642 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001643
1644 if (max_ps == -1)
1645 max_ps = state;
1646
1647 if (total_latency_us > max_lat_us)
1648 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001649 }
1650
1651 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001652
1653 if (max_ps == -1) {
1654 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1655 } else {
1656 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1657 max_ps, max_lat_us, (int)sizeof(*table), table);
1658 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001659 }
1660
1661 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1662 table, sizeof(*table), NULL);
1663 if (ret)
1664 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1665
1666 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001667 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001668}
1669
1670static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1671{
1672 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1673 u64 latency;
1674
1675 switch (val) {
1676 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1677 case PM_QOS_LATENCY_ANY:
1678 latency = U64_MAX;
1679 break;
1680
1681 default:
1682 latency = val;
1683 }
1684
1685 if (ctrl->ps_max_latency_us != latency) {
1686 ctrl->ps_max_latency_us = latency;
1687 nvme_configure_apst(ctrl);
1688 }
1689}
1690
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001691struct nvme_core_quirk_entry {
1692 /*
1693 * NVMe model and firmware strings are padded with spaces. For
1694 * simplicity, strings in the quirk table are padded with NULLs
1695 * instead.
1696 */
1697 u16 vid;
1698 const char *mn;
1699 const char *fr;
1700 unsigned long quirks;
1701};
1702
1703static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001704 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001705 /*
1706 * This Toshiba device seems to die using any APST states. See:
1707 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1708 */
1709 .vid = 0x1179,
1710 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001711 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001712 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001713};
1714
1715/* match is null-terminated but idstr is space-padded. */
1716static bool string_matches(const char *idstr, const char *match, size_t len)
1717{
1718 size_t matchlen;
1719
1720 if (!match)
1721 return true;
1722
1723 matchlen = strlen(match);
1724 WARN_ON_ONCE(matchlen > len);
1725
1726 if (memcmp(idstr, match, matchlen))
1727 return false;
1728
1729 for (; matchlen < len; matchlen++)
1730 if (idstr[matchlen] != ' ')
1731 return false;
1732
1733 return true;
1734}
1735
1736static bool quirk_matches(const struct nvme_id_ctrl *id,
1737 const struct nvme_core_quirk_entry *q)
1738{
1739 return q->vid == le16_to_cpu(id->vid) &&
1740 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1741 string_matches(id->fr, q->fr, sizeof(id->fr));
1742}
1743
Christoph Hellwig180de0072017-06-26 12:39:02 +02001744static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1745{
1746 size_t nqnlen;
1747 int off;
1748
1749 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1750 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1751 strcpy(ctrl->subnqn, id->subnqn);
1752 return;
1753 }
1754
1755 if (ctrl->vs >= NVME_VS(1, 2, 1))
1756 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1757
1758 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1759 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1760 "nqn.2014.08.org.nvmexpress:%4x%4x",
1761 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1762 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1763 off += sizeof(id->sn);
1764 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1765 off += sizeof(id->mn);
1766 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1767}
1768
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001769/*
1770 * Initialize the cached copies of the Identify data and various controller
1771 * register in our nvme_ctrl structure. This should be called as soon as
1772 * the admin queue is fully up and running.
1773 */
1774int nvme_init_identify(struct nvme_ctrl *ctrl)
1775{
1776 struct nvme_id_ctrl *id;
1777 u64 cap;
1778 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001779 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001780 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001781
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001782 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1783 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001784 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001785 return ret;
1786 }
1787
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001788 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1789 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001790 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001791 return ret;
1792 }
1793 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1794
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001795 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001796 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1797
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001798 ret = nvme_identify_ctrl(ctrl, &id);
1799 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001800 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001801 return -EIO;
1802 }
1803
Christoph Hellwig180de0072017-06-26 12:39:02 +02001804 nvme_init_subnqn(ctrl, id);
1805
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001806 if (!ctrl->identified) {
1807 /*
1808 * Check for quirks. Quirk can depend on firmware version,
1809 * so, in principle, the set of quirks present can change
1810 * across a reset. As a possible future enhancement, we
1811 * could re-scan for quirks every time we reinitialize
1812 * the device, but we'd have to make sure that the driver
1813 * behaves intelligently if the quirks change.
1814 */
1815
1816 int i;
1817
1818 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1819 if (quirk_matches(id, &core_quirks[i]))
1820 ctrl->quirks |= core_quirks[i].quirks;
1821 }
1822 }
1823
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001824 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001825 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 -07001826 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1827 }
1828
Scott Bauer8a9ae522017-02-17 13:59:40 +01001829 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001830 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001831 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001832 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001833 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001834 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001835 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1836 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1837 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1838 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001839 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001840 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001841 max_hw_sectors = UINT_MAX;
1842 ctrl->max_hw_sectors =
1843 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001844
Christoph Hellwigda358252016-03-02 18:07:11 +01001845 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001846 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001847 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001848
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001849 if (id->rtd3e) {
1850 /* us -> s */
1851 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1852
1853 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1854 shutdown_timeout, 60);
1855
1856 if (ctrl->shutdown_timeout != shutdown_timeout)
1857 dev_warn(ctrl->device,
1858 "Shutdown timeout set to %u seconds\n",
1859 ctrl->shutdown_timeout);
1860 } else
1861 ctrl->shutdown_timeout = shutdown_timeout;
1862
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001863 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001864 ctrl->apsta = id->apsta;
1865 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001866 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1867 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001868 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 -04001869 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001870 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001871 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001872 }
1873 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001874 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001875 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001876 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1877
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001878 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001879 ctrl->icdoff = le16_to_cpu(id->icdoff);
1880 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1881 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1882 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1883
1884 /*
1885 * In fabrics we need to verify the cntlid matches the
1886 * admin connect
1887 */
Keith Busch634b8322017-08-10 11:23:31 +02001888 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001889 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001890 goto out_free;
1891 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001892
1893 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001894 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001895 "keep-alive support is mandatory for fabrics\n");
1896 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001897 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001898 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001899 } else {
1900 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001901 ctrl->hmpre = le32_to_cpu(id->hmpre);
1902 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001903 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001904
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001905 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001906
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001907 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001908 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001909 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001910 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1911
Keith Busch634b8322017-08-10 11:23:31 +02001912 ret = nvme_configure_apst(ctrl);
1913 if (ret < 0)
1914 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001915
1916 ret = nvme_configure_timestamp(ctrl);
1917 if (ret < 0)
1918 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001919
1920 ret = nvme_configure_directives(ctrl);
1921 if (ret < 0)
1922 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001923
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001924 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001925
Keith Busch634b8322017-08-10 11:23:31 +02001926 return 0;
1927
1928out_free:
1929 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001930 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001931}
Ming Lin576d55d2016-02-10 10:03:32 -08001932EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001933
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001934static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001935{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001936 struct nvme_ctrl *ctrl;
1937 int instance = iminor(inode);
1938 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001939
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001940 spin_lock(&dev_list_lock);
1941 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1942 if (ctrl->instance != instance)
1943 continue;
1944
1945 if (!ctrl->admin_q) {
1946 ret = -EWOULDBLOCK;
1947 break;
1948 }
1949 if (!kref_get_unless_zero(&ctrl->kref))
1950 break;
1951 file->private_data = ctrl;
1952 ret = 0;
1953 break;
1954 }
1955 spin_unlock(&dev_list_lock);
1956
1957 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001958}
1959
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001960static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001961{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001962 nvme_put_ctrl(file->private_data);
1963 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001964}
1965
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001966static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1967{
1968 struct nvme_ns *ns;
1969 int ret;
1970
1971 mutex_lock(&ctrl->namespaces_mutex);
1972 if (list_empty(&ctrl->namespaces)) {
1973 ret = -ENOTTY;
1974 goto out_unlock;
1975 }
1976
1977 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1978 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001979 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001980 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1981 ret = -EINVAL;
1982 goto out_unlock;
1983 }
1984
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001985 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001986 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1987 kref_get(&ns->kref);
1988 mutex_unlock(&ctrl->namespaces_mutex);
1989
1990 ret = nvme_user_cmd(ctrl, ns, argp);
1991 nvme_put_ns(ns);
1992 return ret;
1993
1994out_unlock:
1995 mutex_unlock(&ctrl->namespaces_mutex);
1996 return ret;
1997}
1998
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001999static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2000 unsigned long arg)
2001{
2002 struct nvme_ctrl *ctrl = file->private_data;
2003 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002004
2005 switch (cmd) {
2006 case NVME_IOCTL_ADMIN_CMD:
2007 return nvme_user_cmd(ctrl, NULL, argp);
2008 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002009 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002010 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002011 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002012 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002013 case NVME_IOCTL_SUBSYS_RESET:
2014 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002015 case NVME_IOCTL_RESCAN:
2016 nvme_queue_scan(ctrl);
2017 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002018 default:
2019 return -ENOTTY;
2020 }
2021}
2022
2023static const struct file_operations nvme_dev_fops = {
2024 .owner = THIS_MODULE,
2025 .open = nvme_dev_open,
2026 .release = nvme_dev_release,
2027 .unlocked_ioctl = nvme_dev_ioctl,
2028 .compat_ioctl = nvme_dev_ioctl,
2029};
2030
2031static ssize_t nvme_sysfs_reset(struct device *dev,
2032 struct device_attribute *attr, const char *buf,
2033 size_t count)
2034{
2035 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2036 int ret;
2037
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002038 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002039 if (ret < 0)
2040 return ret;
2041 return count;
2042}
2043static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2044
Keith Busch9ec3bb22016-04-29 15:45:18 -06002045static ssize_t nvme_sysfs_rescan(struct device *dev,
2046 struct device_attribute *attr, const char *buf,
2047 size_t count)
2048{
2049 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2050
2051 nvme_queue_scan(ctrl);
2052 return count;
2053}
2054static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2055
Keith Busch118472a2016-02-18 09:57:48 -07002056static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2057 char *buf)
2058{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002059 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002060 struct nvme_ctrl *ctrl = ns->ctrl;
2061 int serial_len = sizeof(ctrl->serial);
2062 int model_len = sizeof(ctrl->model);
2063
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002064 if (!uuid_is_null(&ns->uuid))
2065 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2066
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002067 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2068 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002069
2070 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2071 return sprintf(buf, "eui.%8phN\n", ns->eui);
2072
Martin Wilck758f3732017-07-20 18:34:02 +02002073 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2074 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002075 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002076 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2077 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002078 model_len--;
2079
2080 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2081 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2082}
2083static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2084
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002085static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2086 char *buf)
2087{
2088 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2089 return sprintf(buf, "%pU\n", ns->nguid);
2090}
2091static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2092
Keith Busch2b9b6e82015-12-22 10:10:45 -07002093static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2094 char *buf)
2095{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002096 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002097
2098 /* For backward compatibility expose the NGUID to userspace if
2099 * we have no UUID set
2100 */
2101 if (uuid_is_null(&ns->uuid)) {
2102 printk_ratelimited(KERN_WARNING
2103 "No UUID available providing old NGUID\n");
2104 return sprintf(buf, "%pU\n", ns->nguid);
2105 }
2106 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002107}
2108static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2109
2110static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2111 char *buf)
2112{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002113 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002114 return sprintf(buf, "%8phd\n", ns->eui);
2115}
2116static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2117
2118static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2119 char *buf)
2120{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002121 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002122 return sprintf(buf, "%d\n", ns->ns_id);
2123}
2124static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2125
2126static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002127 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002128 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002129 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002130 &dev_attr_eui.attr,
2131 &dev_attr_nsid.attr,
2132 NULL,
2133};
2134
Ming Lin1a353d82016-06-13 16:45:24 +02002135static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002136 struct attribute *a, int n)
2137{
2138 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002139 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002140
2141 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002142 if (uuid_is_null(&ns->uuid) ||
2143 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2144 return 0;
2145 }
2146 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002147 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002148 return 0;
2149 }
2150 if (a == &dev_attr_eui.attr) {
2151 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2152 return 0;
2153 }
2154 return a->mode;
2155}
2156
2157static const struct attribute_group nvme_ns_attr_group = {
2158 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002159 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002160};
2161
Ming Lin931e1c22016-02-26 13:24:19 -08002162#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002163static ssize_t field##_show(struct device *dev, \
2164 struct device_attribute *attr, char *buf) \
2165{ \
2166 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2167 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2168} \
2169static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2170
Ming Lin931e1c22016-02-26 13:24:19 -08002171#define nvme_show_int_function(field) \
2172static ssize_t field##_show(struct device *dev, \
2173 struct device_attribute *attr, char *buf) \
2174{ \
2175 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2176 return sprintf(buf, "%d\n", ctrl->field); \
2177} \
2178static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2179
2180nvme_show_str_function(model);
2181nvme_show_str_function(serial);
2182nvme_show_str_function(firmware_rev);
2183nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002184
Ming Lin1a353d82016-06-13 16:45:24 +02002185static ssize_t nvme_sysfs_delete(struct device *dev,
2186 struct device_attribute *attr, const char *buf,
2187 size_t count)
2188{
2189 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2190
2191 if (device_remove_file_self(dev, attr))
2192 ctrl->ops->delete_ctrl(ctrl);
2193 return count;
2194}
2195static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2196
2197static ssize_t nvme_sysfs_show_transport(struct device *dev,
2198 struct device_attribute *attr,
2199 char *buf)
2200{
2201 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2202
2203 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2204}
2205static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2206
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002207static ssize_t nvme_sysfs_show_state(struct device *dev,
2208 struct device_attribute *attr,
2209 char *buf)
2210{
2211 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2212 static const char *const state_name[] = {
2213 [NVME_CTRL_NEW] = "new",
2214 [NVME_CTRL_LIVE] = "live",
2215 [NVME_CTRL_RESETTING] = "resetting",
2216 [NVME_CTRL_RECONNECTING]= "reconnecting",
2217 [NVME_CTRL_DELETING] = "deleting",
2218 [NVME_CTRL_DEAD] = "dead",
2219 };
2220
2221 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2222 state_name[ctrl->state])
2223 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2224
2225 return sprintf(buf, "unknown state\n");
2226}
2227
2228static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2229
Ming Lin1a353d82016-06-13 16:45:24 +02002230static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2231 struct device_attribute *attr,
2232 char *buf)
2233{
2234 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2235
Christoph Hellwig180de0072017-06-26 12:39:02 +02002236 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002237}
2238static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2239
2240static ssize_t nvme_sysfs_show_address(struct device *dev,
2241 struct device_attribute *attr,
2242 char *buf)
2243{
2244 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2245
2246 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2247}
2248static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2249
Keith Busch779ff7562016-01-12 15:09:31 -07002250static struct attribute *nvme_dev_attrs[] = {
2251 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002252 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002253 &dev_attr_model.attr,
2254 &dev_attr_serial.attr,
2255 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002256 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002257 &dev_attr_delete_controller.attr,
2258 &dev_attr_transport.attr,
2259 &dev_attr_subsysnqn.attr,
2260 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002261 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002262 NULL
2263};
2264
Ming Lin1a353d82016-06-13 16:45:24 +02002265static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2266 struct attribute *a, int n)
2267{
2268 struct device *dev = container_of(kobj, struct device, kobj);
2269 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2270
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002271 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2272 return 0;
2273 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2274 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002275
2276 return a->mode;
2277}
2278
Keith Busch779ff7562016-01-12 15:09:31 -07002279static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002280 .attrs = nvme_dev_attrs,
2281 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002282};
2283
2284static const struct attribute_group *nvme_dev_attr_groups[] = {
2285 &nvme_dev_attrs_group,
2286 NULL,
2287};
2288
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002289static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2290{
2291 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2292 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2293
2294 return nsa->ns_id - nsb->ns_id;
2295}
2296
Keith Busch32f0c4a2016-07-13 11:45:02 -06002297static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002298{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002299 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002300
Keith Busch32f0c4a2016-07-13 11:45:02 -06002301 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002302 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002303 if (ns->ns_id == nsid) {
2304 kref_get(&ns->kref);
2305 ret = ns;
2306 break;
2307 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002308 if (ns->ns_id > nsid)
2309 break;
2310 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002311 mutex_unlock(&ctrl->namespaces_mutex);
2312 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002313}
2314
Jens Axboef5d11842017-06-27 12:03:06 -06002315static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2316{
2317 struct streams_directive_params s;
2318 int ret;
2319
2320 if (!ctrl->nr_streams)
2321 return 0;
2322
2323 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2324 if (ret)
2325 return ret;
2326
2327 ns->sws = le32_to_cpu(s.sws);
2328 ns->sgs = le16_to_cpu(s.sgs);
2329
2330 if (ns->sws) {
2331 unsigned int bs = 1 << ns->lba_shift;
2332
2333 blk_queue_io_min(ns->queue, bs * ns->sws);
2334 if (ns->sgs)
2335 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2336 }
2337
2338 return 0;
2339}
2340
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002341static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2342{
2343 struct nvme_ns *ns;
2344 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002345 struct nvme_id_ns *id;
2346 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002347 int node = dev_to_node(ctrl->dev);
2348
2349 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2350 if (!ns)
2351 return;
2352
Keith Busch075790e2016-02-24 09:15:53 -07002353 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2354 if (ns->instance < 0)
2355 goto out_free_ns;
2356
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002357 ns->queue = blk_mq_init_queue(ctrl->tagset);
2358 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002359 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002360 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2361 ns->queue->queuedata = ns;
2362 ns->ctrl = ctrl;
2363
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002364 kref_init(&ns->kref);
2365 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002366 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002367
2368 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002369 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002370 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002371
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002372 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002373
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002374 id = nvme_identify_ns(ctrl, nsid);
2375 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002376 goto out_free_queue;
2377
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002378 if (id->ncap == 0)
2379 goto out_free_id;
2380
2381 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2382
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002383 if (nvme_nvm_ns_supported(ns, id) &&
2384 nvme_nvm_register(ns, disk_name, node)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002385 dev_warn(ctrl->device, "%s: LightNVM init failure\n", __func__);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002386 goto out_free_id;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002387 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002388
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002389 disk = alloc_disk_node(0, node);
2390 if (!disk)
2391 goto out_free_id;
2392
2393 disk->fops = &nvme_fops;
2394 disk->private_data = ns;
2395 disk->queue = ns->queue;
2396 disk->flags = GENHD_FL_EXT_DEVT;
2397 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2398 ns->disk = disk;
2399
2400 __nvme_revalidate_disk(disk, id);
2401
Keith Busch32f0c4a2016-07-13 11:45:02 -06002402 mutex_lock(&ctrl->namespaces_mutex);
2403 list_add_tail(&ns->list, &ctrl->namespaces);
2404 mutex_unlock(&ctrl->namespaces_mutex);
2405
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002406 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002407
2408 kfree(id);
2409
Dan Williams0d52c7562016-06-15 19:44:20 -07002410 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002411 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2412 &nvme_ns_attr_group))
2413 pr_warn("%s: failed to create sysfs group for identification\n",
2414 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002415 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2416 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2417 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002418 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002419 out_free_id:
2420 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002421 out_free_queue:
2422 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002423 out_release_instance:
2424 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002425 out_free_ns:
2426 kfree(ns);
2427}
2428
2429static void nvme_ns_remove(struct nvme_ns *ns)
2430{
Keith Busch646017a2016-02-24 09:15:54 -07002431 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2432 return;
2433
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002434 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002435 if (blk_get_integrity(ns->disk))
2436 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002437 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2438 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002439 if (ns->ndev)
2440 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002441 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002442 blk_cleanup_queue(ns->queue);
2443 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002444
2445 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002446 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002447 mutex_unlock(&ns->ctrl->namespaces_mutex);
2448
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002449 nvme_put_ns(ns);
2450}
2451
Keith Busch540c8012015-10-22 15:45:06 -06002452static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2453{
2454 struct nvme_ns *ns;
2455
Keith Busch32f0c4a2016-07-13 11:45:02 -06002456 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002457 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002458 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002459 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002460 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002461 } else
2462 nvme_alloc_ns(ctrl, nsid);
2463}
2464
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302465static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2466 unsigned nsid)
2467{
2468 struct nvme_ns *ns, *next;
2469
2470 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2471 if (ns->ns_id > nsid)
2472 nvme_ns_remove(ns);
2473 }
2474}
2475
Keith Busch540c8012015-10-22 15:45:06 -06002476static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2477{
2478 struct nvme_ns *ns;
2479 __le32 *ns_list;
2480 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2481 int ret = 0;
2482
2483 ns_list = kzalloc(0x1000, GFP_KERNEL);
2484 if (!ns_list)
2485 return -ENOMEM;
2486
2487 for (i = 0; i < num_lists; i++) {
2488 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2489 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302490 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002491
2492 for (j = 0; j < min(nn, 1024U); j++) {
2493 nsid = le32_to_cpu(ns_list[j]);
2494 if (!nsid)
2495 goto out;
2496
2497 nvme_validate_ns(ctrl, nsid);
2498
2499 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002500 ns = nvme_find_get_ns(ctrl, prev);
2501 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002502 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002503 nvme_put_ns(ns);
2504 }
Keith Busch540c8012015-10-22 15:45:06 -06002505 }
2506 }
2507 nn -= j;
2508 }
2509 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302510 nvme_remove_invalid_namespaces(ctrl, prev);
2511 free:
Keith Busch540c8012015-10-22 15:45:06 -06002512 kfree(ns_list);
2513 return ret;
2514}
2515
Christoph Hellwig5955be22016-04-26 13:51:59 +02002516static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002517{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002518 unsigned i;
2519
Keith Busch540c8012015-10-22 15:45:06 -06002520 for (i = 1; i <= nn; i++)
2521 nvme_validate_ns(ctrl, i);
2522
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302523 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002524}
2525
Christoph Hellwig5955be22016-04-26 13:51:59 +02002526static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002527{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002528 struct nvme_ctrl *ctrl =
2529 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002530 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002531 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002532
Christoph Hellwig5955be22016-04-26 13:51:59 +02002533 if (ctrl->state != NVME_CTRL_LIVE)
2534 return;
2535
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002536 if (nvme_identify_ctrl(ctrl, &id))
2537 return;
Keith Busch540c8012015-10-22 15:45:06 -06002538
2539 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002540 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002541 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2542 if (!nvme_scan_ns_list(ctrl, nn))
2543 goto done;
2544 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002545 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002546 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002547 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002548 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002549 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002550 kfree(id);
2551}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002552
2553void nvme_queue_scan(struct nvme_ctrl *ctrl)
2554{
2555 /*
2556 * Do not queue new scan work when a controller is reset during
2557 * removal.
2558 */
2559 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002560 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002561}
2562EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002563
Keith Busch32f0c4a2016-07-13 11:45:02 -06002564/*
2565 * This function iterates the namespace list unlocked to allow recovery from
2566 * controller failure. It is up to the caller to ensure the namespace list is
2567 * not modified by scan work while this function is executing.
2568 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002569void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2570{
2571 struct nvme_ns *ns, *next;
2572
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002573 /*
2574 * The dead states indicates the controller was not gracefully
2575 * disconnected. In that case, we won't be able to flush any data while
2576 * removing the namespaces' disks; fail all the queues now to avoid
2577 * potentially having to clean up the failed sync later.
2578 */
2579 if (ctrl->state == NVME_CTRL_DEAD)
2580 nvme_kill_queues(ctrl);
2581
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002582 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2583 nvme_ns_remove(ns);
2584}
Ming Lin576d55d2016-02-10 10:03:32 -08002585EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002586
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002587static void nvme_async_event_work(struct work_struct *work)
2588{
2589 struct nvme_ctrl *ctrl =
2590 container_of(work, struct nvme_ctrl, async_event_work);
2591
2592 spin_lock_irq(&ctrl->lock);
2593 while (ctrl->event_limit > 0) {
2594 int aer_idx = --ctrl->event_limit;
2595
2596 spin_unlock_irq(&ctrl->lock);
2597 ctrl->ops->submit_async_event(ctrl, aer_idx);
2598 spin_lock_irq(&ctrl->lock);
2599 }
2600 spin_unlock_irq(&ctrl->lock);
2601}
2602
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302603static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2604{
2605
2606 u32 csts;
2607
2608 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2609 return false;
2610
2611 if (csts == ~0)
2612 return false;
2613
2614 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2615}
2616
2617static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2618{
2619 struct nvme_command c = { };
2620 struct nvme_fw_slot_info_log *log;
2621
2622 log = kmalloc(sizeof(*log), GFP_KERNEL);
2623 if (!log)
2624 return;
2625
2626 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302627 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302628 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2629
2630 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2631 dev_warn(ctrl->device,
2632 "Get FW SLOT INFO log error\n");
2633 kfree(log);
2634}
2635
2636static void nvme_fw_act_work(struct work_struct *work)
2637{
2638 struct nvme_ctrl *ctrl = container_of(work,
2639 struct nvme_ctrl, fw_act_work);
2640 unsigned long fw_act_timeout;
2641
2642 if (ctrl->mtfa)
2643 fw_act_timeout = jiffies +
2644 msecs_to_jiffies(ctrl->mtfa * 100);
2645 else
2646 fw_act_timeout = jiffies +
2647 msecs_to_jiffies(admin_timeout * 1000);
2648
2649 nvme_stop_queues(ctrl);
2650 while (nvme_ctrl_pp_status(ctrl)) {
2651 if (time_after(jiffies, fw_act_timeout)) {
2652 dev_warn(ctrl->device,
2653 "Fw activation timeout, reset controller\n");
2654 nvme_reset_ctrl(ctrl);
2655 break;
2656 }
2657 msleep(100);
2658 }
2659
2660 if (ctrl->state != NVME_CTRL_LIVE)
2661 return;
2662
2663 nvme_start_queues(ctrl);
2664 /* read FW slot informationi to clear the AER*/
2665 nvme_get_fw_slot_info(ctrl);
2666}
2667
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002668void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2669 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002670{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002671 u32 result = le32_to_cpu(res->u32);
2672 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002673
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002674 switch (le16_to_cpu(status) >> 1) {
2675 case NVME_SC_SUCCESS:
2676 done = false;
2677 /*FALLTHRU*/
2678 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002679 ++ctrl->event_limit;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002680 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002681 break;
2682 default:
2683 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002684 }
2685
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002686 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002687 return;
2688
2689 switch (result & 0xff07) {
2690 case NVME_AER_NOTICE_NS_CHANGED:
2691 dev_info(ctrl->device, "rescanning\n");
2692 nvme_queue_scan(ctrl);
2693 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302694 case NVME_AER_NOTICE_FW_ACT_STARTING:
2695 schedule_work(&ctrl->fw_act_work);
2696 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002697 default:
2698 dev_warn(ctrl->device, "async event result %08x\n", result);
2699 }
2700}
2701EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2702
2703void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2704{
2705 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002706 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002707}
2708EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2709
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002710static DEFINE_IDA(nvme_instance_ida);
2711
2712static int nvme_set_instance(struct nvme_ctrl *ctrl)
2713{
2714 int instance, error;
2715
2716 do {
2717 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2718 return -ENODEV;
2719
2720 spin_lock(&dev_list_lock);
2721 error = ida_get_new(&nvme_instance_ida, &instance);
2722 spin_unlock(&dev_list_lock);
2723 } while (error == -EAGAIN);
2724
2725 if (error)
2726 return -ENODEV;
2727
2728 ctrl->instance = instance;
2729 return 0;
2730}
2731
2732static void nvme_release_instance(struct nvme_ctrl *ctrl)
2733{
2734 spin_lock(&dev_list_lock);
2735 ida_remove(&nvme_instance_ida, ctrl->instance);
2736 spin_unlock(&dev_list_lock);
2737}
2738
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002739void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002740{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002741 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002742 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002743 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302744 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002745}
2746EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002747
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002748void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2749{
2750 if (ctrl->kato)
2751 nvme_start_keep_alive(ctrl);
2752
2753 if (ctrl->queue_count > 1) {
2754 nvme_queue_scan(ctrl);
2755 nvme_queue_async_events(ctrl);
2756 nvme_start_queues(ctrl);
2757 }
2758}
2759EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2760
2761void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2762{
Keith Busch53029b02015-11-28 15:41:02 +01002763 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002764
2765 spin_lock(&dev_list_lock);
2766 list_del(&ctrl->node);
2767 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002768}
Ming Lin576d55d2016-02-10 10:03:32 -08002769EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002770
2771static void nvme_free_ctrl(struct kref *kref)
2772{
2773 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002774
2775 put_device(ctrl->device);
2776 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002777 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002778
2779 ctrl->ops->free_ctrl(ctrl);
2780}
2781
2782void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2783{
2784 kref_put(&ctrl->kref, nvme_free_ctrl);
2785}
Ming Lin576d55d2016-02-10 10:03:32 -08002786EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002787
2788/*
2789 * Initialize a NVMe controller structures. This needs to be called during
2790 * earliest initialization so that we have the initialized structured around
2791 * during probing.
2792 */
2793int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2794 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2795{
2796 int ret;
2797
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002798 ctrl->state = NVME_CTRL_NEW;
2799 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002800 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002801 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002802 kref_init(&ctrl->kref);
2803 ctrl->dev = dev;
2804 ctrl->ops = ops;
2805 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002806 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002807 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302808 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002809
2810 ret = nvme_set_instance(ctrl);
2811 if (ret)
2812 goto out;
2813
Keith Busch779ff7562016-01-12 15:09:31 -07002814 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002815 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002816 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002817 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002818 if (IS_ERR(ctrl->device)) {
2819 ret = PTR_ERR(ctrl->device);
2820 goto out_release_instance;
2821 }
2822 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002823 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002824
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002825 spin_lock(&dev_list_lock);
2826 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2827 spin_unlock(&dev_list_lock);
2828
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002829 /*
2830 * Initialize latency tolerance controls. The sysfs files won't
2831 * be visible to userspace unless the device actually supports APST.
2832 */
2833 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2834 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2835 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2836
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002837 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002838out_release_instance:
2839 nvme_release_instance(ctrl);
2840out:
2841 return ret;
2842}
Ming Lin576d55d2016-02-10 10:03:32 -08002843EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002844
Keith Busch69d9a992016-02-24 09:15:56 -07002845/**
2846 * nvme_kill_queues(): Ends all namespace queues
2847 * @ctrl: the dead controller that needs to end
2848 *
2849 * Call this function when the driver determines it is unable to get the
2850 * controller in a state capable of servicing IO.
2851 */
2852void nvme_kill_queues(struct nvme_ctrl *ctrl)
2853{
2854 struct nvme_ns *ns;
2855
Keith Busch32f0c4a2016-07-13 11:45:02 -06002856 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002857
Ming Lei443bd902017-06-19 10:21:08 +08002858 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002859 if (ctrl->admin_q)
2860 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002861
Keith Busch32f0c4a2016-07-13 11:45:02 -06002862 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002863 /*
2864 * Revalidating a dead namespace sets capacity to 0. This will
2865 * end buffered writers dirtying pages that can't be synced.
2866 */
Keith Buschf33447b2017-02-10 18:15:51 -05002867 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2868 continue;
2869 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002870 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002871
Ming Lei443bd902017-06-19 10:21:08 +08002872 /* Forcibly unquiesce queues to avoid blocking dispatch */
2873 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002874 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002875 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002876}
Linus Torvalds237045f2016-03-18 17:13:31 -07002877EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002878
Keith Busch302ad8c2017-03-01 14:22:12 -05002879void nvme_unfreeze(struct nvme_ctrl *ctrl)
2880{
2881 struct nvme_ns *ns;
2882
2883 mutex_lock(&ctrl->namespaces_mutex);
2884 list_for_each_entry(ns, &ctrl->namespaces, list)
2885 blk_mq_unfreeze_queue(ns->queue);
2886 mutex_unlock(&ctrl->namespaces_mutex);
2887}
2888EXPORT_SYMBOL_GPL(nvme_unfreeze);
2889
2890void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2891{
2892 struct nvme_ns *ns;
2893
2894 mutex_lock(&ctrl->namespaces_mutex);
2895 list_for_each_entry(ns, &ctrl->namespaces, list) {
2896 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2897 if (timeout <= 0)
2898 break;
2899 }
2900 mutex_unlock(&ctrl->namespaces_mutex);
2901}
2902EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2903
2904void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2905{
2906 struct nvme_ns *ns;
2907
2908 mutex_lock(&ctrl->namespaces_mutex);
2909 list_for_each_entry(ns, &ctrl->namespaces, list)
2910 blk_mq_freeze_queue_wait(ns->queue);
2911 mutex_unlock(&ctrl->namespaces_mutex);
2912}
2913EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2914
2915void nvme_start_freeze(struct nvme_ctrl *ctrl)
2916{
2917 struct nvme_ns *ns;
2918
2919 mutex_lock(&ctrl->namespaces_mutex);
2920 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002921 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002922 mutex_unlock(&ctrl->namespaces_mutex);
2923}
2924EXPORT_SYMBOL_GPL(nvme_start_freeze);
2925
Keith Busch25646262016-01-04 09:10:57 -07002926void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002927{
2928 struct nvme_ns *ns;
2929
Keith Busch32f0c4a2016-07-13 11:45:02 -06002930 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002931 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002932 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002933 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002934}
Ming Lin576d55d2016-02-10 10:03:32 -08002935EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002936
Keith Busch25646262016-01-04 09:10:57 -07002937void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002938{
2939 struct nvme_ns *ns;
2940
Keith Busch32f0c4a2016-07-13 11:45:02 -06002941 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002942 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002943 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002944 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002945}
Ming Lin576d55d2016-02-10 10:03:32 -08002946EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002947
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002948int __init nvme_core_init(void)
2949{
2950 int result;
2951
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002952 nvme_wq = alloc_workqueue("nvme-wq",
2953 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2954 if (!nvme_wq)
2955 return -ENOMEM;
2956
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002957 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2958 &nvme_dev_fops);
2959 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002960 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002961 else if (result > 0)
2962 nvme_char_major = result;
2963
2964 nvme_class = class_create(THIS_MODULE, "nvme");
2965 if (IS_ERR(nvme_class)) {
2966 result = PTR_ERR(nvme_class);
2967 goto unregister_chrdev;
2968 }
2969
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002970 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002971
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002972unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002973 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002974destroy_wq:
2975 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002976 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002977}
2978
2979void nvme_core_exit(void)
2980{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002981 class_destroy(nvme_class);
2982 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002983 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002984}
Ming Lin576d55d2016-02-10 10:03:32 -08002985
2986MODULE_LICENSE("GPL");
2987MODULE_VERSION("1.0");
2988module_init(nvme_core_init);
2989module_exit(nvme_core_exit);