blob: c95155696741b72a745de6e83dbc9c0ab410e115 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
32#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020033#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010034
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010035#define NVME_MINORS (1U << MINORBITS)
36
Marc Olson8ae4e442017-09-06 17:23:56 -070037unsigned int admin_timeout = 60;
38module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080039MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080040EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080041
Marc Olson8ae4e442017-09-06 17:23:56 -070042unsigned int nvme_io_timeout = 30;
43module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080044MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080045EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080046
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020047static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080048module_param(shutdown_timeout, byte, 0644);
49MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
50
Christoph Hellwig44e44b22017-04-05 19:18:11 +020051static u8 nvme_max_retries = 5;
52module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070053MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010054
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;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200137 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200138 return false;
139 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200140}
141
142void nvme_complete_rq(struct request *req)
143{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200144 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
145 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300146 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200147 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200148 }
149
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200150 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200151}
152EXPORT_SYMBOL_GPL(nvme_complete_rq);
153
Ming Linc55a2fd2016-05-18 14:05:02 -0700154void nvme_cancel_request(struct request *req, void *data, bool reserved)
155{
156 int status;
157
158 if (!blk_mq_request_started(req))
159 return;
160
161 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
162 "Cancelling I/O %d", req->tag);
163
164 status = NVME_SC_ABORT_REQ;
165 if (blk_queue_dying(req->q))
166 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200167 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200168 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200169
Ming Linc55a2fd2016-05-18 14:05:02 -0700170}
171EXPORT_SYMBOL_GPL(nvme_cancel_request);
172
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200173bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
174 enum nvme_ctrl_state new_state)
175{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300176 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200177 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200178 bool changed = false;
179
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200180 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300181
182 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200183 switch (new_state) {
184 case NVME_CTRL_LIVE:
185 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200186 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200187 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900188 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200189 changed = true;
190 /* FALLTHRU */
191 default:
192 break;
193 }
194 break;
195 case NVME_CTRL_RESETTING:
196 switch (old_state) {
197 case NVME_CTRL_NEW:
198 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900199 changed = true;
200 /* FALLTHRU */
201 default:
202 break;
203 }
204 break;
205 case NVME_CTRL_RECONNECTING:
206 switch (old_state) {
207 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200208 changed = true;
209 /* FALLTHRU */
210 default:
211 break;
212 }
213 break;
214 case NVME_CTRL_DELETING:
215 switch (old_state) {
216 case NVME_CTRL_LIVE:
217 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900218 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200219 changed = true;
220 /* FALLTHRU */
221 default:
222 break;
223 }
224 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600225 case NVME_CTRL_DEAD:
226 switch (old_state) {
227 case NVME_CTRL_DELETING:
228 changed = true;
229 /* FALLTHRU */
230 default:
231 break;
232 }
233 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200234 default:
235 break;
236 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200237
238 if (changed)
239 ctrl->state = new_state;
240
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200241 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300242
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200243 return changed;
244}
245EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
246
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100247static void nvme_free_ns(struct kref *kref)
248{
249 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
250
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200251 if (ns->ndev)
252 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100253
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200254 if (ns->disk) {
255 spin_lock(&dev_list_lock);
256 ns->disk->private_data = NULL;
257 spin_unlock(&dev_list_lock);
258 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100259
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100260 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700261 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
262 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100263 kfree(ns);
264}
265
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100266static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100267{
268 kref_put(&ns->kref, nvme_free_ns);
269}
270
271static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk)
272{
273 struct nvme_ns *ns;
274
275 spin_lock(&dev_list_lock);
276 ns = disk->private_data;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800277 if (ns) {
278 if (!kref_get_unless_zero(&ns->kref))
279 goto fail;
280 if (!try_module_get(ns->ctrl->ops->module))
281 goto fail_put_ns;
282 }
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100283 spin_unlock(&dev_list_lock);
284
285 return ns;
Sagi Grimberge439bb12016-02-10 10:03:29 -0800286
287fail_put_ns:
288 kref_put(&ns->kref, nvme_free_ns);
289fail:
290 spin_unlock(&dev_list_lock);
291 return NULL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100292}
293
Christoph Hellwig41609822015-11-20 09:00:02 +0100294struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200295 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100296{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100297 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100298 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100299
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200300 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100301 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200302 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100303 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200304 qid ? qid - 1 : 0);
305 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100306 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100307 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100308
Christoph Hellwig21d34712015-11-26 09:08:36 +0100309 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800310 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100311
Christoph Hellwig41609822015-11-20 09:00:02 +0100312 return req;
313}
Ming Lin576d55d2016-02-10 10:03:32 -0800314EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100315
Jens Axboef5d11842017-06-27 12:03:06 -0600316static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
317{
318 struct nvme_command c;
319
320 memset(&c, 0, sizeof(c));
321
322 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530323 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600324 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
325 c.directive.dtype = NVME_DIR_IDENTIFY;
326 c.directive.tdtype = NVME_DIR_STREAMS;
327 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
328
329 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
330}
331
332static int nvme_disable_streams(struct nvme_ctrl *ctrl)
333{
334 return nvme_toggle_streams(ctrl, false);
335}
336
337static int nvme_enable_streams(struct nvme_ctrl *ctrl)
338{
339 return nvme_toggle_streams(ctrl, true);
340}
341
342static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
343 struct streams_directive_params *s, u32 nsid)
344{
345 struct nvme_command c;
346
347 memset(&c, 0, sizeof(c));
348 memset(s, 0, sizeof(*s));
349
350 c.directive.opcode = nvme_admin_directive_recv;
351 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700352 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600353 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
354 c.directive.dtype = NVME_DIR_STREAMS;
355
356 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
357}
358
359static int nvme_configure_directives(struct nvme_ctrl *ctrl)
360{
361 struct streams_directive_params s;
362 int ret;
363
364 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
365 return 0;
366 if (!streams)
367 return 0;
368
369 ret = nvme_enable_streams(ctrl);
370 if (ret)
371 return ret;
372
Arnav Dawn62346ea2017-07-12 16:11:53 +0530373 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600374 if (ret)
375 return ret;
376
377 ctrl->nssa = le16_to_cpu(s.nssa);
378 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
379 dev_info(ctrl->device, "too few streams (%u) available\n",
380 ctrl->nssa);
381 nvme_disable_streams(ctrl);
382 return 0;
383 }
384
385 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
386 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
387 return 0;
388}
389
390/*
391 * Check if 'req' has a write hint associated with it. If it does, assign
392 * a valid namespace stream to the write.
393 */
394static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
395 struct request *req, u16 *control,
396 u32 *dsmgmt)
397{
398 enum rw_hint streamid = req->write_hint;
399
400 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
401 streamid = 0;
402 else {
403 streamid--;
404 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
405 return;
406
407 *control |= NVME_RW_DTYPE_STREAMS;
408 *dsmgmt |= streamid << 16;
409 }
410
411 if (streamid < ARRAY_SIZE(req->q->write_hints))
412 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
413}
414
Ming Lin8093f7c2016-04-12 13:10:14 -0600415static inline void nvme_setup_flush(struct nvme_ns *ns,
416 struct nvme_command *cmnd)
417{
418 memset(cmnd, 0, sizeof(*cmnd));
419 cmnd->common.opcode = nvme_cmd_flush;
420 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
421}
422
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200423static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600424 struct nvme_command *cmnd)
425{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100426 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600427 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100428 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600429
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100430 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600431 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200432 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600433
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100434 __rq_for_each_bio(bio, req) {
435 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
436 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
437
438 range[n].cattr = cpu_to_le32(0);
439 range[n].nlb = cpu_to_le32(nlb);
440 range[n].slba = cpu_to_le64(slba);
441 n++;
442 }
443
444 if (WARN_ON_ONCE(n != segments)) {
445 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200446 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100447 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600448
449 memset(cmnd, 0, sizeof(*cmnd));
450 cmnd->dsm.opcode = nvme_cmd_dsm;
451 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200452 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600453 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
454
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700455 req->special_vec.bv_page = virt_to_page(range);
456 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100457 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700458 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600459
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200460 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600461}
Ming Lin8093f7c2016-04-12 13:10:14 -0600462
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200463static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
464 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600465{
Jens Axboef5d11842017-06-27 12:03:06 -0600466 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600467 u16 control = 0;
468 u32 dsmgmt = 0;
469
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200470 /*
471 * If formated with metadata, require the block layer provide a buffer
472 * unless this namespace is formated such that the metadata can be
473 * stripped/generated by the controller with PRACT=1.
474 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300475 if (ns && ns->ms &&
476 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200477 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
478 return BLK_STS_NOTSUPP;
479
Ming Lin8093f7c2016-04-12 13:10:14 -0600480 if (req->cmd_flags & REQ_FUA)
481 control |= NVME_RW_FUA;
482 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
483 control |= NVME_RW_LR;
484
485 if (req->cmd_flags & REQ_RAHEAD)
486 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
487
488 memset(cmnd, 0, sizeof(*cmnd));
489 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600490 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
491 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
492 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
493
Jens Axboef5d11842017-06-27 12:03:06 -0600494 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
495 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
496
Ming Lin8093f7c2016-04-12 13:10:14 -0600497 if (ns->ms) {
498 switch (ns->pi_type) {
499 case NVME_NS_DPS_PI_TYPE3:
500 control |= NVME_RW_PRINFO_PRCHK_GUARD;
501 break;
502 case NVME_NS_DPS_PI_TYPE1:
503 case NVME_NS_DPS_PI_TYPE2:
504 control |= NVME_RW_PRINFO_PRCHK_GUARD |
505 NVME_RW_PRINFO_PRCHK_REF;
506 cmnd->rw.reftag = cpu_to_le32(
507 nvme_block_nr(ns, blk_rq_pos(req)));
508 break;
509 }
510 if (!blk_integrity_rq(req))
511 control |= NVME_RW_PRINFO_PRACT;
512 }
513
514 cmnd->rw.control = cpu_to_le16(control);
515 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200516 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600517}
518
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200519blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600520 struct nvme_command *cmd)
521{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200522 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600523
Christoph Hellwig987f6992017-04-05 19:18:08 +0200524 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200525 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200526 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200527 req->rq_flags |= RQF_DONTPREP;
528 }
529
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100530 switch (req_op(req)) {
531 case REQ_OP_DRV_IN:
532 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800533 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100534 break;
535 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600536 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100537 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200538 case REQ_OP_WRITE_ZEROES:
539 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100540 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600541 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100542 break;
543 case REQ_OP_READ:
544 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200545 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100546 break;
547 default:
548 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200549 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100550 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600551
James Smart721b3912016-10-21 23:33:34 +0300552 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600553 return ret;
554}
555EXPORT_SYMBOL_GPL(nvme_setup_cmd);
556
Christoph Hellwig41609822015-11-20 09:00:02 +0100557/*
558 * Returns 0 on success. If the result is negative, it's a Linux error code;
559 * if the result is positive, it's an NVM Express status code
560 */
561int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800562 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200563 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100564{
565 struct request *req;
566 int ret;
567
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200568 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100569 if (IS_ERR(req))
570 return PTR_ERR(req);
571
572 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
573
Christoph Hellwig21d34712015-11-26 09:08:36 +0100574 if (buffer && bufflen) {
575 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
576 if (ret)
577 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100578 }
579
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200580 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800581 if (result)
582 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200583 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
584 ret = -EINTR;
585 else
586 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100587 out:
588 blk_mq_free_request(req);
589 return ret;
590}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200591EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100592
593int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
594 void *buffer, unsigned bufflen)
595{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200596 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
597 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100598}
Ming Lin576d55d2016-02-10 10:03:32 -0800599EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100600
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400601static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
602 unsigned len, u32 seed, bool write)
603{
604 struct bio_integrity_payload *bip;
605 int ret = -ENOMEM;
606 void *buf;
607
608 buf = kmalloc(len, GFP_KERNEL);
609 if (!buf)
610 goto out;
611
612 ret = -EFAULT;
613 if (write && copy_from_user(buf, ubuf, len))
614 goto out_free_meta;
615
616 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
617 if (IS_ERR(bip)) {
618 ret = PTR_ERR(bip);
619 goto out_free_meta;
620 }
621
622 bip->bip_iter.bi_size = len;
623 bip->bip_iter.bi_sector = seed;
624 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
625 offset_in_page(buf));
626 if (ret == len)
627 return buf;
628 ret = -ENOMEM;
629out_free_meta:
630 kfree(buf);
631out:
632 return ERR_PTR(ret);
633}
634
Keith Busch63263d62017-08-29 17:46:04 -0400635static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400636 struct nvme_command *cmd, void __user *ubuffer,
637 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
638 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100639{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200640 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600641 struct nvme_ns *ns = q->queuedata;
642 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100643 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600644 struct bio *bio = NULL;
645 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100646 int ret;
647
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200648 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100649 if (IS_ERR(req))
650 return PTR_ERR(req);
651
652 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
653
654 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100655 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
656 GFP_KERNEL);
657 if (ret)
658 goto out;
659 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200660 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400661 if (disk && meta_buffer && meta_len) {
662 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
663 meta_seed, write);
664 if (IS_ERR(meta)) {
665 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600666 goto out_unmap;
667 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600668 }
669 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400670
Keith Busch0b7f1f22015-10-23 09:47:28 -0600671 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200672 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
673 ret = -EINTR;
674 else
675 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100676 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800677 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600678 if (meta && !ret && !write) {
679 if (copy_to_user(meta_buffer, meta, meta_len))
680 ret = -EFAULT;
681 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600682 kfree(meta);
683 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200684 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600685 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100686 out:
687 blk_mq_free_request(req);
688 return ret;
689}
690
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200691static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200692{
693 struct nvme_ctrl *ctrl = rq->end_io_data;
694
695 blk_mq_free_request(rq);
696
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200697 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200698 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200699 "failed nvme_keep_alive_end_io error=%d\n",
700 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200701 return;
702 }
703
704 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
705}
706
707static int nvme_keep_alive(struct nvme_ctrl *ctrl)
708{
709 struct nvme_command c;
710 struct request *rq;
711
712 memset(&c, 0, sizeof(c));
713 c.common.opcode = nvme_admin_keep_alive;
714
715 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
716 NVME_QID_ANY);
717 if (IS_ERR(rq))
718 return PTR_ERR(rq);
719
720 rq->timeout = ctrl->kato * HZ;
721 rq->end_io_data = ctrl;
722
723 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
724
725 return 0;
726}
727
728static void nvme_keep_alive_work(struct work_struct *work)
729{
730 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
731 struct nvme_ctrl, ka_work);
732
733 if (nvme_keep_alive(ctrl)) {
734 /* allocation failure, reset the controller */
735 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200736 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200737 return;
738 }
739}
740
741void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
742{
743 if (unlikely(ctrl->kato == 0))
744 return;
745
746 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
747 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
748}
749EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
750
751void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
752{
753 if (unlikely(ctrl->kato == 0))
754 return;
755
756 cancel_delayed_work_sync(&ctrl->ka_work);
757}
758EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
759
Keith Busch3f7f25a92017-06-20 15:09:56 -0400760static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100761{
762 struct nvme_command c = { };
763 int error;
764
765 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
766 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200767 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100768
769 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
770 if (!*id)
771 return -ENOMEM;
772
773 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
774 sizeof(struct nvme_id_ctrl));
775 if (error)
776 kfree(*id);
777 return error;
778}
779
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200780static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
781 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200782{
783 struct nvme_command c = { };
784 int status;
785 void *data;
786 int pos;
787 int len;
788
789 c.identify.opcode = nvme_admin_identify;
790 c.identify.nsid = cpu_to_le32(nsid);
791 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
792
793 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
794 if (!data)
795 return -ENOMEM;
796
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200797 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200798 NVME_IDENTIFY_DATA_SIZE);
799 if (status)
800 goto free_data;
801
802 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
803 struct nvme_ns_id_desc *cur = data + pos;
804
805 if (cur->nidl == 0)
806 break;
807
808 switch (cur->nidt) {
809 case NVME_NIDT_EUI64:
810 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200811 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200812 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
813 cur->nidl);
814 goto free_data;
815 }
816 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200817 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200818 break;
819 case NVME_NIDT_NGUID:
820 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200821 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200822 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
823 cur->nidl);
824 goto free_data;
825 }
826 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200827 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200828 break;
829 case NVME_NIDT_UUID:
830 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200831 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200832 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
833 cur->nidl);
834 goto free_data;
835 }
836 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200837 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200838 break;
839 default:
840 /* Skip unnkown types */
841 len = cur->nidl;
842 break;
843 }
844
845 len += sizeof(*cur);
846 }
847free_data:
848 kfree(data);
849 return status;
850}
851
Keith Busch540c8012015-10-22 15:45:06 -0600852static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
853{
854 struct nvme_command c = { };
855
856 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200857 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600858 c.identify.nsid = cpu_to_le32(nsid);
859 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
860}
861
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200862static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
863 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100864{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200865 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100866 struct nvme_command c = { };
867 int error;
868
869 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200870 c.identify.opcode = nvme_admin_identify;
871 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200872 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100873
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200874 id = kmalloc(sizeof(*id), GFP_KERNEL);
875 if (!id)
876 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100877
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200878 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
879 if (error) {
880 dev_warn(ctrl->device, "Identify namespace failed\n");
881 kfree(id);
882 return NULL;
883 }
884
885 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100886}
887
Keith Busch3f7f25a92017-06-20 15:09:56 -0400888static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700889 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100890{
891 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800892 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100893 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100894
895 memset(&c, 0, sizeof(c));
896 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100897 c.features.fid = cpu_to_le32(fid);
898 c.features.dword11 = cpu_to_le32(dword11);
899
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800900 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700901 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700902 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800903 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100904 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100905}
906
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100907int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
908{
909 u32 q_count = (*count - 1) | ((*count - 1) << 16);
910 u32 result;
911 int status, nr_io_queues;
912
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700913 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100914 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200915 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100916 return status;
917
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200918 /*
919 * Degraded controllers might return an error when setting the queue
920 * count. We still want to be able to bring them online and offer
921 * access to the admin queue, as that might be only way to fix them up.
922 */
923 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200924 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200925 *count = 0;
926 } else {
927 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
928 *count = min(*count, nr_io_queues);
929 }
930
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100931 return 0;
932}
Ming Lin576d55d2016-02-10 10:03:32 -0800933EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100934
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100935static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
936{
937 struct nvme_user_io io;
938 struct nvme_command c;
939 unsigned length, meta_len;
940 void __user *metadata;
941
942 if (copy_from_user(&io, uio, sizeof(io)))
943 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700944 if (io.flags)
945 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100946
947 switch (io.opcode) {
948 case nvme_cmd_write:
949 case nvme_cmd_read:
950 case nvme_cmd_compare:
951 break;
952 default:
953 return -EINVAL;
954 }
955
956 length = (io.nblocks + 1) << ns->lba_shift;
957 meta_len = (io.nblocks + 1) * ns->ms;
958 metadata = (void __user *)(uintptr_t)io.metadata;
959
960 if (ns->ext) {
961 length += meta_len;
962 meta_len = 0;
963 } else if (meta_len) {
964 if ((io.metadata & 3) || !io.metadata)
965 return -EINVAL;
966 }
967
968 memset(&c, 0, sizeof(c));
969 c.rw.opcode = io.opcode;
970 c.rw.flags = io.flags;
971 c.rw.nsid = cpu_to_le32(ns->ns_id);
972 c.rw.slba = cpu_to_le64(io.slba);
973 c.rw.length = cpu_to_le16(io.nblocks);
974 c.rw.control = cpu_to_le16(io.control);
975 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
976 c.rw.reftag = cpu_to_le32(io.reftag);
977 c.rw.apptag = cpu_to_le16(io.apptag);
978 c.rw.appmask = cpu_to_le16(io.appmask);
979
Keith Busch63263d62017-08-29 17:46:04 -0400980 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100981 (void __user *)(uintptr_t)io.addr, length,
982 metadata, meta_len, io.slba, NULL, 0);
983}
984
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100985static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100986 struct nvme_passthru_cmd __user *ucmd)
987{
988 struct nvme_passthru_cmd cmd;
989 struct nvme_command c;
990 unsigned timeout = 0;
991 int status;
992
993 if (!capable(CAP_SYS_ADMIN))
994 return -EACCES;
995 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
996 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700997 if (cmd.flags)
998 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100999
1000 memset(&c, 0, sizeof(c));
1001 c.common.opcode = cmd.opcode;
1002 c.common.flags = cmd.flags;
1003 c.common.nsid = cpu_to_le32(cmd.nsid);
1004 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1005 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1006 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1007 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1008 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1009 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1010 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1011 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1012
1013 if (cmd.timeout_ms)
1014 timeout = msecs_to_jiffies(cmd.timeout_ms);
1015
1016 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001017 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001018 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1019 0, &cmd.result, timeout);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001020 if (status >= 0) {
1021 if (put_user(cmd.result, &ucmd->result))
1022 return -EFAULT;
1023 }
1024
1025 return status;
1026}
1027
1028static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1029 unsigned int cmd, unsigned long arg)
1030{
1031 struct nvme_ns *ns = bdev->bd_disk->private_data;
1032
1033 switch (cmd) {
1034 case NVME_IOCTL_ID:
1035 force_successful_syscall_return();
1036 return ns->ns_id;
1037 case NVME_IOCTL_ADMIN_CMD:
1038 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1039 case NVME_IOCTL_IO_CMD:
1040 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1041 case NVME_IOCTL_SUBMIT_IO:
1042 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001043 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001044#ifdef CONFIG_NVM
1045 if (ns->ndev)
1046 return nvme_nvm_ioctl(ns, cmd, arg);
1047#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001048 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001049 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001050 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001051 return -ENOTTY;
1052 }
1053}
1054
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001055static int nvme_open(struct block_device *bdev, fmode_t mode)
1056{
1057 return nvme_get_ns_from_disk(bdev->bd_disk) ? 0 : -ENXIO;
1058}
1059
1060static void nvme_release(struct gendisk *disk, fmode_t mode)
1061{
Sagi Grimberge439bb12016-02-10 10:03:29 -08001062 struct nvme_ns *ns = disk->private_data;
1063
1064 module_put(ns->ctrl->ops->module);
1065 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001066}
1067
1068static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1069{
1070 /* some standard values */
1071 geo->heads = 1 << 6;
1072 geo->sectors = 1 << 5;
1073 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1074 return 0;
1075}
1076
1077#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001078static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1079 u16 bs)
1080{
1081 struct nvme_ns *ns = disk->private_data;
1082 u16 old_ms = ns->ms;
1083 u8 pi_type = 0;
1084
1085 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1086 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1087
1088 /* PI implementation requires metadata equal t10 pi tuple size */
1089 if (ns->ms == sizeof(struct t10_pi_tuple))
1090 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1091
1092 if (blk_get_integrity(disk) &&
1093 (ns->pi_type != pi_type || ns->ms != old_ms ||
1094 bs != queue_logical_block_size(disk->queue) ||
1095 (ns->ms && ns->ext)))
1096 blk_integrity_unregister(disk);
1097
1098 ns->pi_type = pi_type;
1099}
1100
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001101static void nvme_init_integrity(struct nvme_ns *ns)
1102{
1103 struct blk_integrity integrity;
1104
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001105 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001106 switch (ns->pi_type) {
1107 case NVME_NS_DPS_PI_TYPE3:
1108 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001109 integrity.tag_size = sizeof(u16) + sizeof(u32);
1110 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001111 break;
1112 case NVME_NS_DPS_PI_TYPE1:
1113 case NVME_NS_DPS_PI_TYPE2:
1114 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001115 integrity.tag_size = sizeof(u16);
1116 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001117 break;
1118 default:
1119 integrity.profile = NULL;
1120 break;
1121 }
1122 integrity.tuple_size = ns->ms;
1123 blk_integrity_register(ns->disk, &integrity);
1124 blk_queue_max_integrity_segments(ns->queue, 1);
1125}
1126#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001127static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1128 u16 bs)
1129{
1130}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001131static void nvme_init_integrity(struct nvme_ns *ns)
1132{
1133}
1134#endif /* CONFIG_BLK_DEV_INTEGRITY */
1135
Scott Bauer6b8190d2017-06-15 10:44:30 -06001136static void nvme_set_chunk_size(struct nvme_ns *ns)
1137{
1138 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1139 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1140}
1141
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001142static void nvme_config_discard(struct nvme_ns *ns)
1143{
Keith Busch08095e72016-03-04 13:15:17 -07001144 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001145 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001146
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001147 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1148 NVME_DSM_MAX_RANGES);
1149
Jens Axboef5d11842017-06-27 12:03:06 -06001150 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1151 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1152
1153 ns->queue->limits.discard_alignment = sz;
1154 ns->queue->limits.discard_granularity = sz;
1155 } else {
1156 ns->queue->limits.discard_alignment = logical_block_size;
1157 ns->queue->limits.discard_granularity = logical_block_size;
1158 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001159 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001160 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001161 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001162
1163 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1164 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001165}
1166
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001167static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1168 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001169{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001170 if (ctrl->vs >= NVME_VS(1, 1, 0))
1171 memcpy(eui64, id->eui64, sizeof(id->eui64));
1172 if (ctrl->vs >= NVME_VS(1, 2, 0))
1173 memcpy(nguid, id->nguid, sizeof(id->nguid));
1174 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001175 /* Don't treat error as fatal we potentially
1176 * already have a NGUID or EUI-64
1177 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001178 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1179 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001180 "%s: Identify Descriptors failed\n", __func__);
1181 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001182}
1183
1184static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1185{
1186 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001187 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001188 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001189
1190 /*
1191 * If identify namespace failed, use default 512 byte block size so
1192 * block layer can use before failing read/write for 0 capacity.
1193 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001194 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001195 if (ns->lba_shift == 0)
1196 ns->lba_shift = 9;
1197 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001198 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001199
1200 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001201
Jens Axboef5d11842017-06-27 12:03:06 -06001202 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001203 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001204 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001205 if (ns->noiob)
1206 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001207 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001208 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001209 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1210 set_capacity(disk, 0);
1211 else
1212 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1213
Jens Axboef5d11842017-06-27 12:03:06 -06001214 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001215 nvme_config_discard(ns);
1216 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001217}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001218
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001219static int nvme_revalidate_disk(struct gendisk *disk)
1220{
1221 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001222 struct nvme_ctrl *ctrl = ns->ctrl;
1223 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001224 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1225 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001226 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001227
1228 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1229 set_capacity(disk, 0);
1230 return -ENODEV;
1231 }
1232
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001233 id = nvme_identify_ns(ctrl, ns->ns_id);
1234 if (!id)
1235 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001236
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001237 if (id->ncap == 0) {
1238 ret = -ENODEV;
1239 goto out;
1240 }
1241
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001242 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1243 if (!uuid_equal(&ns->uuid, &uuid) ||
1244 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1245 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1246 dev_err(ctrl->device,
1247 "identifiers changed for nsid %d\n", ns->ns_id);
1248 ret = -ENODEV;
1249 }
1250
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001251out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001252 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001253 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001254}
1255
1256static char nvme_pr_type(enum pr_type type)
1257{
1258 switch (type) {
1259 case PR_WRITE_EXCLUSIVE:
1260 return 1;
1261 case PR_EXCLUSIVE_ACCESS:
1262 return 2;
1263 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1264 return 3;
1265 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1266 return 4;
1267 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1268 return 5;
1269 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1270 return 6;
1271 default:
1272 return 0;
1273 }
1274};
1275
1276static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1277 u64 key, u64 sa_key, u8 op)
1278{
1279 struct nvme_ns *ns = bdev->bd_disk->private_data;
1280 struct nvme_command c;
1281 u8 data[16] = { 0, };
1282
1283 put_unaligned_le64(key, &data[0]);
1284 put_unaligned_le64(sa_key, &data[8]);
1285
1286 memset(&c, 0, sizeof(c));
1287 c.common.opcode = op;
1288 c.common.nsid = cpu_to_le32(ns->ns_id);
1289 c.common.cdw10[0] = cpu_to_le32(cdw10);
1290
1291 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1292}
1293
1294static int nvme_pr_register(struct block_device *bdev, u64 old,
1295 u64 new, unsigned flags)
1296{
1297 u32 cdw10;
1298
1299 if (flags & ~PR_FL_IGNORE_KEY)
1300 return -EOPNOTSUPP;
1301
1302 cdw10 = old ? 2 : 0;
1303 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1304 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1305 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1306}
1307
1308static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1309 enum pr_type type, unsigned flags)
1310{
1311 u32 cdw10;
1312
1313 if (flags & ~PR_FL_IGNORE_KEY)
1314 return -EOPNOTSUPP;
1315
1316 cdw10 = nvme_pr_type(type) << 8;
1317 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1318 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1319}
1320
1321static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1322 enum pr_type type, bool abort)
1323{
1324 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1325 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1326}
1327
1328static int nvme_pr_clear(struct block_device *bdev, u64 key)
1329{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001330 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001331 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1332}
1333
1334static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1335{
1336 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1337 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1338}
1339
1340static const struct pr_ops nvme_pr_ops = {
1341 .pr_register = nvme_pr_register,
1342 .pr_reserve = nvme_pr_reserve,
1343 .pr_release = nvme_pr_release,
1344 .pr_preempt = nvme_pr_preempt,
1345 .pr_clear = nvme_pr_clear,
1346};
1347
Scott Bauera98e58e52017-02-03 12:50:32 -07001348#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001349int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1350 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001351{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001352 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001353 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001354
1355 memset(&cmd, 0, sizeof(cmd));
1356 if (send)
1357 cmd.common.opcode = nvme_admin_security_send;
1358 else
1359 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001360 cmd.common.nsid = 0;
1361 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1362 cmd.common.cdw10[1] = cpu_to_le32(len);
1363
1364 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1365 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1366}
1367EXPORT_SYMBOL_GPL(nvme_sec_submit);
1368#endif /* CONFIG_BLK_SED_OPAL */
1369
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001370static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001371 .owner = THIS_MODULE,
1372 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001373 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001374 .open = nvme_open,
1375 .release = nvme_release,
1376 .getgeo = nvme_getgeo,
1377 .revalidate_disk= nvme_revalidate_disk,
1378 .pr_ops = &nvme_pr_ops,
1379};
1380
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001381static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1382{
1383 unsigned long timeout =
1384 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1385 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1386 int ret;
1387
1388 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001389 if (csts == ~0)
1390 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001391 if ((csts & NVME_CSTS_RDY) == bit)
1392 break;
1393
1394 msleep(100);
1395 if (fatal_signal_pending(current))
1396 return -EINTR;
1397 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001398 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001399 "Device not ready; aborting %s\n", enabled ?
1400 "initialisation" : "reset");
1401 return -ENODEV;
1402 }
1403 }
1404
1405 return ret;
1406}
1407
1408/*
1409 * If the device has been passed off to us in an enabled state, just clear
1410 * the enabled bit. The spec says we should set the 'shutdown notification
1411 * bits', but doing so may cause the device to complete commands to the
1412 * admin queue ... and we don't know what memory that might be pointing at!
1413 */
1414int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1415{
1416 int ret;
1417
1418 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1419 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1420
1421 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1422 if (ret)
1423 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001424
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001425 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001426 msleep(NVME_QUIRK_DELAY_AMOUNT);
1427
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001428 return nvme_wait_ready(ctrl, cap, false);
1429}
Ming Lin576d55d2016-02-10 10:03:32 -08001430EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001431
1432int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1433{
1434 /*
1435 * Default to a 4K page size, with the intention to update this
1436 * path in the future to accomodate architectures with differing
1437 * kernel and IO page sizes.
1438 */
1439 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1440 int ret;
1441
1442 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001443 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001444 "Minimum device page size %u too large for host (%u)\n",
1445 1 << dev_page_min, 1 << page_shift);
1446 return -ENODEV;
1447 }
1448
1449 ctrl->page_size = 1 << page_shift;
1450
1451 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1452 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001453 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001454 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1455 ctrl->ctrl_config |= NVME_CC_ENABLE;
1456
1457 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1458 if (ret)
1459 return ret;
1460 return nvme_wait_ready(ctrl, cap, true);
1461}
Ming Lin576d55d2016-02-10 10:03:32 -08001462EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001463
1464int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1465{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001466 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001467 u32 csts;
1468 int ret;
1469
1470 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1471 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1472
1473 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1474 if (ret)
1475 return ret;
1476
1477 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1478 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1479 break;
1480
1481 msleep(100);
1482 if (fatal_signal_pending(current))
1483 return -EINTR;
1484 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001485 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001486 "Device shutdown incomplete; abort shutdown\n");
1487 return -ENODEV;
1488 }
1489 }
1490
1491 return ret;
1492}
Ming Lin576d55d2016-02-10 10:03:32 -08001493EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001494
Christoph Hellwigda358252016-03-02 18:07:11 +01001495static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1496 struct request_queue *q)
1497{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001498 bool vwc = false;
1499
Christoph Hellwigda358252016-03-02 18:07:11 +01001500 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001501 u32 max_segments =
1502 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1503
Christoph Hellwigda358252016-03-02 18:07:11 +01001504 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001505 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001506 }
Keith Busche6282ae2016-12-19 11:37:50 -05001507 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1508 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001509 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001510 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1511 vwc = true;
1512 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001513}
1514
Jon Derrickdbf86b32017-08-16 09:51:29 +02001515static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1516{
1517 __le64 ts;
1518 int ret;
1519
1520 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1521 return 0;
1522
1523 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1524 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1525 NULL);
1526 if (ret)
1527 dev_warn_once(ctrl->device,
1528 "could not set timestamp (%d)\n", ret);
1529 return ret;
1530}
1531
Keith Busch634b8322017-08-10 11:23:31 +02001532static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001533{
1534 /*
1535 * APST (Autonomous Power State Transition) lets us program a
1536 * table of power state transitions that the controller will
1537 * perform automatically. We configure it with a simple
1538 * heuristic: we are willing to spend at most 2% of the time
1539 * transitioning between power states. Therefore, when running
1540 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001541 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001542 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001543 * the requested maximum latency.
1544 *
1545 * We will not autonomously enter any non-operational state for
1546 * which the total latency exceeds ps_max_latency_us. Users
1547 * can set ps_max_latency_us to zero to turn off APST.
1548 */
1549
1550 unsigned apste;
1551 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001552 u64 max_lat_us = 0;
1553 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001554 int ret;
1555
1556 /*
1557 * If APST isn't supported or if we haven't been initialized yet,
1558 * then don't do anything.
1559 */
1560 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001561 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001562
1563 if (ctrl->npss > 31) {
1564 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001565 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001566 }
1567
1568 table = kzalloc(sizeof(*table), GFP_KERNEL);
1569 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001570 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001571
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001572 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001573 /* Turn off APST. */
1574 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001575 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001576 } else {
1577 __le64 target = cpu_to_le64(0);
1578 int state;
1579
1580 /*
1581 * Walk through all states from lowest- to highest-power.
1582 * According to the spec, lower-numbered states use more
1583 * power. NPSS, despite the name, is the index of the
1584 * lowest-power state, not the number of states.
1585 */
1586 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001587 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001588
1589 if (target)
1590 table->entries[state] = target;
1591
1592 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001593 * Don't allow transitions to the deepest state
1594 * if it's quirked off.
1595 */
1596 if (state == ctrl->npss &&
1597 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1598 continue;
1599
1600 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001601 * Is this state a useful non-operational state for
1602 * higher-power states to autonomously transition to?
1603 */
1604 if (!(ctrl->psd[state].flags &
1605 NVME_PS_FLAGS_NON_OP_STATE))
1606 continue;
1607
Kai-Heng Fengda875912017-06-07 15:25:42 +08001608 exit_latency_us =
1609 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1610 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001611 continue;
1612
Kai-Heng Fengda875912017-06-07 15:25:42 +08001613 total_latency_us =
1614 exit_latency_us +
1615 le32_to_cpu(ctrl->psd[state].entry_lat);
1616
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001617 /*
1618 * This state is good. Use it as the APST idle
1619 * target for higher power states.
1620 */
1621 transition_ms = total_latency_us + 19;
1622 do_div(transition_ms, 20);
1623 if (transition_ms > (1 << 24) - 1)
1624 transition_ms = (1 << 24) - 1;
1625
1626 target = cpu_to_le64((state << 3) |
1627 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001628
1629 if (max_ps == -1)
1630 max_ps = state;
1631
1632 if (total_latency_us > max_lat_us)
1633 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001634 }
1635
1636 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001637
1638 if (max_ps == -1) {
1639 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1640 } else {
1641 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1642 max_ps, max_lat_us, (int)sizeof(*table), table);
1643 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001644 }
1645
1646 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1647 table, sizeof(*table), NULL);
1648 if (ret)
1649 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1650
1651 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001652 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001653}
1654
1655static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1656{
1657 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1658 u64 latency;
1659
1660 switch (val) {
1661 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1662 case PM_QOS_LATENCY_ANY:
1663 latency = U64_MAX;
1664 break;
1665
1666 default:
1667 latency = val;
1668 }
1669
1670 if (ctrl->ps_max_latency_us != latency) {
1671 ctrl->ps_max_latency_us = latency;
1672 nvme_configure_apst(ctrl);
1673 }
1674}
1675
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001676struct nvme_core_quirk_entry {
1677 /*
1678 * NVMe model and firmware strings are padded with spaces. For
1679 * simplicity, strings in the quirk table are padded with NULLs
1680 * instead.
1681 */
1682 u16 vid;
1683 const char *mn;
1684 const char *fr;
1685 unsigned long quirks;
1686};
1687
1688static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001689 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001690 /*
1691 * This Toshiba device seems to die using any APST states. See:
1692 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1693 */
1694 .vid = 0x1179,
1695 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001696 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001697 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001698};
1699
1700/* match is null-terminated but idstr is space-padded. */
1701static bool string_matches(const char *idstr, const char *match, size_t len)
1702{
1703 size_t matchlen;
1704
1705 if (!match)
1706 return true;
1707
1708 matchlen = strlen(match);
1709 WARN_ON_ONCE(matchlen > len);
1710
1711 if (memcmp(idstr, match, matchlen))
1712 return false;
1713
1714 for (; matchlen < len; matchlen++)
1715 if (idstr[matchlen] != ' ')
1716 return false;
1717
1718 return true;
1719}
1720
1721static bool quirk_matches(const struct nvme_id_ctrl *id,
1722 const struct nvme_core_quirk_entry *q)
1723{
1724 return q->vid == le16_to_cpu(id->vid) &&
1725 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1726 string_matches(id->fr, q->fr, sizeof(id->fr));
1727}
1728
Christoph Hellwig180de0072017-06-26 12:39:02 +02001729static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1730{
1731 size_t nqnlen;
1732 int off;
1733
1734 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1735 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1736 strcpy(ctrl->subnqn, id->subnqn);
1737 return;
1738 }
1739
1740 if (ctrl->vs >= NVME_VS(1, 2, 1))
1741 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1742
1743 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1744 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1745 "nqn.2014.08.org.nvmexpress:%4x%4x",
1746 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1747 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1748 off += sizeof(id->sn);
1749 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1750 off += sizeof(id->mn);
1751 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1752}
1753
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001754/*
1755 * Initialize the cached copies of the Identify data and various controller
1756 * register in our nvme_ctrl structure. This should be called as soon as
1757 * the admin queue is fully up and running.
1758 */
1759int nvme_init_identify(struct nvme_ctrl *ctrl)
1760{
1761 struct nvme_id_ctrl *id;
1762 u64 cap;
1763 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001764 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001765 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001766
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001767 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1768 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001769 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001770 return ret;
1771 }
1772
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001773 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1774 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001775 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001776 return ret;
1777 }
1778 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1779
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001780 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001781 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1782
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001783 ret = nvme_identify_ctrl(ctrl, &id);
1784 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001785 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001786 return -EIO;
1787 }
1788
Christoph Hellwig180de0072017-06-26 12:39:02 +02001789 nvme_init_subnqn(ctrl, id);
1790
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001791 if (!ctrl->identified) {
1792 /*
1793 * Check for quirks. Quirk can depend on firmware version,
1794 * so, in principle, the set of quirks present can change
1795 * across a reset. As a possible future enhancement, we
1796 * could re-scan for quirks every time we reinitialize
1797 * the device, but we'd have to make sure that the driver
1798 * behaves intelligently if the quirks change.
1799 */
1800
1801 int i;
1802
1803 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1804 if (quirk_matches(id, &core_quirks[i]))
1805 ctrl->quirks |= core_quirks[i].quirks;
1806 }
1807 }
1808
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001809 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001810 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 -07001811 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1812 }
1813
Scott Bauer8a9ae522017-02-17 13:59:40 +01001814 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001815 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001816 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001817 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001818 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001819 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001820 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1821 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1822 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1823 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001824 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001825 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001826 max_hw_sectors = UINT_MAX;
1827 ctrl->max_hw_sectors =
1828 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001829
Christoph Hellwigda358252016-03-02 18:07:11 +01001830 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001831 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001832 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001833
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001834 if (id->rtd3e) {
1835 /* us -> s */
1836 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1837
1838 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1839 shutdown_timeout, 60);
1840
1841 if (ctrl->shutdown_timeout != shutdown_timeout)
1842 dev_warn(ctrl->device,
1843 "Shutdown timeout set to %u seconds\n",
1844 ctrl->shutdown_timeout);
1845 } else
1846 ctrl->shutdown_timeout = shutdown_timeout;
1847
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001848 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001849 ctrl->apsta = id->apsta;
1850 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001851 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1852 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001853 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 -04001854 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001855 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001856 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001857 }
1858 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001859 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001860 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001861 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1862
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001863 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001864 ctrl->icdoff = le16_to_cpu(id->icdoff);
1865 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1866 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1867 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1868
1869 /*
1870 * In fabrics we need to verify the cntlid matches the
1871 * admin connect
1872 */
Keith Busch634b8322017-08-10 11:23:31 +02001873 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001874 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001875 goto out_free;
1876 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001877
1878 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001879 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001880 "keep-alive support is mandatory for fabrics\n");
1881 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001882 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001883 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001884 } else {
1885 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001886 ctrl->hmpre = le32_to_cpu(id->hmpre);
1887 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001888 ctrl->hmminds = le32_to_cpu(id->hmminds);
1889 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001890 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001891
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001892 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001893
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001894 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001895 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001896 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001897 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1898
Keith Busch634b8322017-08-10 11:23:31 +02001899 ret = nvme_configure_apst(ctrl);
1900 if (ret < 0)
1901 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001902
1903 ret = nvme_configure_timestamp(ctrl);
1904 if (ret < 0)
1905 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001906
1907 ret = nvme_configure_directives(ctrl);
1908 if (ret < 0)
1909 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001910
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001911 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001912
Keith Busch634b8322017-08-10 11:23:31 +02001913 return 0;
1914
1915out_free:
1916 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001917 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001918}
Ming Lin576d55d2016-02-10 10:03:32 -08001919EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001920
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001921static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001922{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001923 struct nvme_ctrl *ctrl;
1924 int instance = iminor(inode);
1925 int ret = -ENODEV;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001926
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001927 spin_lock(&dev_list_lock);
1928 list_for_each_entry(ctrl, &nvme_ctrl_list, node) {
1929 if (ctrl->instance != instance)
1930 continue;
1931
1932 if (!ctrl->admin_q) {
1933 ret = -EWOULDBLOCK;
1934 break;
1935 }
1936 if (!kref_get_unless_zero(&ctrl->kref))
1937 break;
1938 file->private_data = ctrl;
1939 ret = 0;
1940 break;
1941 }
1942 spin_unlock(&dev_list_lock);
1943
1944 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001945}
1946
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001947static int nvme_dev_release(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001948{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001949 nvme_put_ctrl(file->private_data);
1950 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001951}
1952
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001953static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1954{
1955 struct nvme_ns *ns;
1956 int ret;
1957
1958 mutex_lock(&ctrl->namespaces_mutex);
1959 if (list_empty(&ctrl->namespaces)) {
1960 ret = -ENOTTY;
1961 goto out_unlock;
1962 }
1963
1964 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1965 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001966 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001967 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1968 ret = -EINVAL;
1969 goto out_unlock;
1970 }
1971
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001972 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001973 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1974 kref_get(&ns->kref);
1975 mutex_unlock(&ctrl->namespaces_mutex);
1976
1977 ret = nvme_user_cmd(ctrl, ns, argp);
1978 nvme_put_ns(ns);
1979 return ret;
1980
1981out_unlock:
1982 mutex_unlock(&ctrl->namespaces_mutex);
1983 return ret;
1984}
1985
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001986static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1987 unsigned long arg)
1988{
1989 struct nvme_ctrl *ctrl = file->private_data;
1990 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001991
1992 switch (cmd) {
1993 case NVME_IOCTL_ADMIN_CMD:
1994 return nvme_user_cmd(ctrl, NULL, argp);
1995 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001996 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001997 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001998 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001999 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002000 case NVME_IOCTL_SUBSYS_RESET:
2001 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002002 case NVME_IOCTL_RESCAN:
2003 nvme_queue_scan(ctrl);
2004 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002005 default:
2006 return -ENOTTY;
2007 }
2008}
2009
2010static const struct file_operations nvme_dev_fops = {
2011 .owner = THIS_MODULE,
2012 .open = nvme_dev_open,
2013 .release = nvme_dev_release,
2014 .unlocked_ioctl = nvme_dev_ioctl,
2015 .compat_ioctl = nvme_dev_ioctl,
2016};
2017
2018static ssize_t nvme_sysfs_reset(struct device *dev,
2019 struct device_attribute *attr, const char *buf,
2020 size_t count)
2021{
2022 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2023 int ret;
2024
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002025 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002026 if (ret < 0)
2027 return ret;
2028 return count;
2029}
2030static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2031
Keith Busch9ec3bb22016-04-29 15:45:18 -06002032static ssize_t nvme_sysfs_rescan(struct device *dev,
2033 struct device_attribute *attr, const char *buf,
2034 size_t count)
2035{
2036 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2037
2038 nvme_queue_scan(ctrl);
2039 return count;
2040}
2041static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2042
Keith Busch118472a2016-02-18 09:57:48 -07002043static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2044 char *buf)
2045{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002046 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002047 struct nvme_ctrl *ctrl = ns->ctrl;
2048 int serial_len = sizeof(ctrl->serial);
2049 int model_len = sizeof(ctrl->model);
2050
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002051 if (!uuid_is_null(&ns->uuid))
2052 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2053
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002054 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2055 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002056
2057 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2058 return sprintf(buf, "eui.%8phN\n", ns->eui);
2059
Martin Wilck758f3732017-07-20 18:34:02 +02002060 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2061 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002062 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002063 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2064 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002065 model_len--;
2066
2067 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2068 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2069}
2070static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2071
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002072static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2073 char *buf)
2074{
2075 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2076 return sprintf(buf, "%pU\n", ns->nguid);
2077}
2078static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2079
Keith Busch2b9b6e82015-12-22 10:10:45 -07002080static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2081 char *buf)
2082{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002083 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002084
2085 /* For backward compatibility expose the NGUID to userspace if
2086 * we have no UUID set
2087 */
2088 if (uuid_is_null(&ns->uuid)) {
2089 printk_ratelimited(KERN_WARNING
2090 "No UUID available providing old NGUID\n");
2091 return sprintf(buf, "%pU\n", ns->nguid);
2092 }
2093 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002094}
2095static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2096
2097static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2098 char *buf)
2099{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002100 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002101 return sprintf(buf, "%8phd\n", ns->eui);
2102}
2103static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2104
2105static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2106 char *buf)
2107{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002108 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002109 return sprintf(buf, "%d\n", ns->ns_id);
2110}
2111static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2112
2113static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002114 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002115 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002116 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002117 &dev_attr_eui.attr,
2118 &dev_attr_nsid.attr,
2119 NULL,
2120};
2121
Ming Lin1a353d82016-06-13 16:45:24 +02002122static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002123 struct attribute *a, int n)
2124{
2125 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002126 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002127
2128 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002129 if (uuid_is_null(&ns->uuid) ||
2130 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2131 return 0;
2132 }
2133 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002134 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002135 return 0;
2136 }
2137 if (a == &dev_attr_eui.attr) {
2138 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2139 return 0;
2140 }
2141 return a->mode;
2142}
2143
2144static const struct attribute_group nvme_ns_attr_group = {
2145 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002146 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002147};
2148
Ming Lin931e1c22016-02-26 13:24:19 -08002149#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002150static ssize_t field##_show(struct device *dev, \
2151 struct device_attribute *attr, char *buf) \
2152{ \
2153 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2154 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2155} \
2156static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2157
Ming Lin931e1c22016-02-26 13:24:19 -08002158#define nvme_show_int_function(field) \
2159static ssize_t field##_show(struct device *dev, \
2160 struct device_attribute *attr, char *buf) \
2161{ \
2162 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2163 return sprintf(buf, "%d\n", ctrl->field); \
2164} \
2165static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2166
2167nvme_show_str_function(model);
2168nvme_show_str_function(serial);
2169nvme_show_str_function(firmware_rev);
2170nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002171
Ming Lin1a353d82016-06-13 16:45:24 +02002172static ssize_t nvme_sysfs_delete(struct device *dev,
2173 struct device_attribute *attr, const char *buf,
2174 size_t count)
2175{
2176 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2177
2178 if (device_remove_file_self(dev, attr))
2179 ctrl->ops->delete_ctrl(ctrl);
2180 return count;
2181}
2182static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2183
2184static ssize_t nvme_sysfs_show_transport(struct device *dev,
2185 struct device_attribute *attr,
2186 char *buf)
2187{
2188 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2189
2190 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2191}
2192static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2193
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002194static ssize_t nvme_sysfs_show_state(struct device *dev,
2195 struct device_attribute *attr,
2196 char *buf)
2197{
2198 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2199 static const char *const state_name[] = {
2200 [NVME_CTRL_NEW] = "new",
2201 [NVME_CTRL_LIVE] = "live",
2202 [NVME_CTRL_RESETTING] = "resetting",
2203 [NVME_CTRL_RECONNECTING]= "reconnecting",
2204 [NVME_CTRL_DELETING] = "deleting",
2205 [NVME_CTRL_DEAD] = "dead",
2206 };
2207
2208 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2209 state_name[ctrl->state])
2210 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2211
2212 return sprintf(buf, "unknown state\n");
2213}
2214
2215static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2216
Ming Lin1a353d82016-06-13 16:45:24 +02002217static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2218 struct device_attribute *attr,
2219 char *buf)
2220{
2221 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2222
Christoph Hellwig180de0072017-06-26 12:39:02 +02002223 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002224}
2225static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2226
2227static ssize_t nvme_sysfs_show_address(struct device *dev,
2228 struct device_attribute *attr,
2229 char *buf)
2230{
2231 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2232
2233 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2234}
2235static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2236
Keith Busch779ff7562016-01-12 15:09:31 -07002237static struct attribute *nvme_dev_attrs[] = {
2238 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002239 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002240 &dev_attr_model.attr,
2241 &dev_attr_serial.attr,
2242 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002243 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002244 &dev_attr_delete_controller.attr,
2245 &dev_attr_transport.attr,
2246 &dev_attr_subsysnqn.attr,
2247 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002248 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002249 NULL
2250};
2251
Ming Lin1a353d82016-06-13 16:45:24 +02002252static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2253 struct attribute *a, int n)
2254{
2255 struct device *dev = container_of(kobj, struct device, kobj);
2256 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2257
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002258 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2259 return 0;
2260 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2261 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002262
2263 return a->mode;
2264}
2265
Keith Busch779ff7562016-01-12 15:09:31 -07002266static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002267 .attrs = nvme_dev_attrs,
2268 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002269};
2270
2271static const struct attribute_group *nvme_dev_attr_groups[] = {
2272 &nvme_dev_attrs_group,
2273 NULL,
2274};
2275
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002276static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2277{
2278 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2279 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2280
2281 return nsa->ns_id - nsb->ns_id;
2282}
2283
Keith Busch32f0c4a2016-07-13 11:45:02 -06002284static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002285{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002286 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002287
Keith Busch32f0c4a2016-07-13 11:45:02 -06002288 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002289 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002290 if (ns->ns_id == nsid) {
2291 kref_get(&ns->kref);
2292 ret = ns;
2293 break;
2294 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002295 if (ns->ns_id > nsid)
2296 break;
2297 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002298 mutex_unlock(&ctrl->namespaces_mutex);
2299 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002300}
2301
Jens Axboef5d11842017-06-27 12:03:06 -06002302static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2303{
2304 struct streams_directive_params s;
2305 int ret;
2306
2307 if (!ctrl->nr_streams)
2308 return 0;
2309
2310 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2311 if (ret)
2312 return ret;
2313
2314 ns->sws = le32_to_cpu(s.sws);
2315 ns->sgs = le16_to_cpu(s.sgs);
2316
2317 if (ns->sws) {
2318 unsigned int bs = 1 << ns->lba_shift;
2319
2320 blk_queue_io_min(ns->queue, bs * ns->sws);
2321 if (ns->sgs)
2322 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2323 }
2324
2325 return 0;
2326}
2327
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002328static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2329{
2330 struct nvme_ns *ns;
2331 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002332 struct nvme_id_ns *id;
2333 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002334 int node = dev_to_node(ctrl->dev);
2335
2336 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2337 if (!ns)
2338 return;
2339
Keith Busch075790e2016-02-24 09:15:53 -07002340 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2341 if (ns->instance < 0)
2342 goto out_free_ns;
2343
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002344 ns->queue = blk_mq_init_queue(ctrl->tagset);
2345 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002346 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002347 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2348 ns->queue->queuedata = ns;
2349 ns->ctrl = ctrl;
2350
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002351 kref_init(&ns->kref);
2352 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002353 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002354
2355 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002356 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002357 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002358
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002359 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002360
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002361 id = nvme_identify_ns(ctrl, nsid);
2362 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002363 goto out_free_queue;
2364
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002365 if (id->ncap == 0)
2366 goto out_free_id;
2367
2368 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2369
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002370 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2371 if (nvme_nvm_register(ns, disk_name, node)) {
2372 dev_warn(ctrl->device, "LightNVM init failure\n");
2373 goto out_free_id;
2374 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002375 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002376
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002377 disk = alloc_disk_node(0, node);
2378 if (!disk)
2379 goto out_free_id;
2380
2381 disk->fops = &nvme_fops;
2382 disk->private_data = ns;
2383 disk->queue = ns->queue;
2384 disk->flags = GENHD_FL_EXT_DEVT;
2385 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2386 ns->disk = disk;
2387
2388 __nvme_revalidate_disk(disk, id);
2389
Keith Busch32f0c4a2016-07-13 11:45:02 -06002390 mutex_lock(&ctrl->namespaces_mutex);
2391 list_add_tail(&ns->list, &ctrl->namespaces);
2392 mutex_unlock(&ctrl->namespaces_mutex);
2393
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002394 kref_get(&ctrl->kref);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002395
2396 kfree(id);
2397
Dan Williams0d52c7562016-06-15 19:44:20 -07002398 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002399 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2400 &nvme_ns_attr_group))
2401 pr_warn("%s: failed to create sysfs group for identification\n",
2402 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002403 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2404 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2405 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002406 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002407 out_free_id:
2408 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002409 out_free_queue:
2410 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002411 out_release_instance:
2412 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002413 out_free_ns:
2414 kfree(ns);
2415}
2416
2417static void nvme_ns_remove(struct nvme_ns *ns)
2418{
Keith Busch646017a2016-02-24 09:15:54 -07002419 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2420 return;
2421
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002422 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002423 if (blk_get_integrity(ns->disk))
2424 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002425 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2426 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002427 if (ns->ndev)
2428 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002429 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002430 blk_cleanup_queue(ns->queue);
2431 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002432
2433 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002434 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002435 mutex_unlock(&ns->ctrl->namespaces_mutex);
2436
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002437 nvme_put_ns(ns);
2438}
2439
Keith Busch540c8012015-10-22 15:45:06 -06002440static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2441{
2442 struct nvme_ns *ns;
2443
Keith Busch32f0c4a2016-07-13 11:45:02 -06002444 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002445 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002446 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002447 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002448 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002449 } else
2450 nvme_alloc_ns(ctrl, nsid);
2451}
2452
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302453static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2454 unsigned nsid)
2455{
2456 struct nvme_ns *ns, *next;
2457
2458 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2459 if (ns->ns_id > nsid)
2460 nvme_ns_remove(ns);
2461 }
2462}
2463
Keith Busch540c8012015-10-22 15:45:06 -06002464static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2465{
2466 struct nvme_ns *ns;
2467 __le32 *ns_list;
2468 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2469 int ret = 0;
2470
2471 ns_list = kzalloc(0x1000, GFP_KERNEL);
2472 if (!ns_list)
2473 return -ENOMEM;
2474
2475 for (i = 0; i < num_lists; i++) {
2476 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2477 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302478 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002479
2480 for (j = 0; j < min(nn, 1024U); j++) {
2481 nsid = le32_to_cpu(ns_list[j]);
2482 if (!nsid)
2483 goto out;
2484
2485 nvme_validate_ns(ctrl, nsid);
2486
2487 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002488 ns = nvme_find_get_ns(ctrl, prev);
2489 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002490 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002491 nvme_put_ns(ns);
2492 }
Keith Busch540c8012015-10-22 15:45:06 -06002493 }
2494 }
2495 nn -= j;
2496 }
2497 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302498 nvme_remove_invalid_namespaces(ctrl, prev);
2499 free:
Keith Busch540c8012015-10-22 15:45:06 -06002500 kfree(ns_list);
2501 return ret;
2502}
2503
Christoph Hellwig5955be22016-04-26 13:51:59 +02002504static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002505{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002506 unsigned i;
2507
Keith Busch540c8012015-10-22 15:45:06 -06002508 for (i = 1; i <= nn; i++)
2509 nvme_validate_ns(ctrl, i);
2510
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302511 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002512}
2513
Christoph Hellwig5955be22016-04-26 13:51:59 +02002514static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002515{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002516 struct nvme_ctrl *ctrl =
2517 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002518 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002519 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002520
Christoph Hellwig5955be22016-04-26 13:51:59 +02002521 if (ctrl->state != NVME_CTRL_LIVE)
2522 return;
2523
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002524 if (nvme_identify_ctrl(ctrl, &id))
2525 return;
Keith Busch540c8012015-10-22 15:45:06 -06002526
2527 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002528 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002529 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2530 if (!nvme_scan_ns_list(ctrl, nn))
2531 goto done;
2532 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002533 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002534 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002535 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002536 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002537 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002538 kfree(id);
2539}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002540
2541void nvme_queue_scan(struct nvme_ctrl *ctrl)
2542{
2543 /*
2544 * Do not queue new scan work when a controller is reset during
2545 * removal.
2546 */
2547 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002548 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002549}
2550EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002551
Keith Busch32f0c4a2016-07-13 11:45:02 -06002552/*
2553 * This function iterates the namespace list unlocked to allow recovery from
2554 * controller failure. It is up to the caller to ensure the namespace list is
2555 * not modified by scan work while this function is executing.
2556 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002557void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2558{
2559 struct nvme_ns *ns, *next;
2560
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002561 /*
2562 * The dead states indicates the controller was not gracefully
2563 * disconnected. In that case, we won't be able to flush any data while
2564 * removing the namespaces' disks; fail all the queues now to avoid
2565 * potentially having to clean up the failed sync later.
2566 */
2567 if (ctrl->state == NVME_CTRL_DEAD)
2568 nvme_kill_queues(ctrl);
2569
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002570 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2571 nvme_ns_remove(ns);
2572}
Ming Lin576d55d2016-02-10 10:03:32 -08002573EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002574
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002575static void nvme_async_event_work(struct work_struct *work)
2576{
2577 struct nvme_ctrl *ctrl =
2578 container_of(work, struct nvme_ctrl, async_event_work);
2579
2580 spin_lock_irq(&ctrl->lock);
James Smartcd482822017-09-14 11:03:09 -07002581 while (ctrl->state == NVME_CTRL_LIVE && ctrl->event_limit > 0) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002582 int aer_idx = --ctrl->event_limit;
2583
2584 spin_unlock_irq(&ctrl->lock);
2585 ctrl->ops->submit_async_event(ctrl, aer_idx);
2586 spin_lock_irq(&ctrl->lock);
2587 }
2588 spin_unlock_irq(&ctrl->lock);
2589}
2590
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302591static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2592{
2593
2594 u32 csts;
2595
2596 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2597 return false;
2598
2599 if (csts == ~0)
2600 return false;
2601
2602 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2603}
2604
2605static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2606{
2607 struct nvme_command c = { };
2608 struct nvme_fw_slot_info_log *log;
2609
2610 log = kmalloc(sizeof(*log), GFP_KERNEL);
2611 if (!log)
2612 return;
2613
2614 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302615 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302616 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2617
2618 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2619 dev_warn(ctrl->device,
2620 "Get FW SLOT INFO log error\n");
2621 kfree(log);
2622}
2623
2624static void nvme_fw_act_work(struct work_struct *work)
2625{
2626 struct nvme_ctrl *ctrl = container_of(work,
2627 struct nvme_ctrl, fw_act_work);
2628 unsigned long fw_act_timeout;
2629
2630 if (ctrl->mtfa)
2631 fw_act_timeout = jiffies +
2632 msecs_to_jiffies(ctrl->mtfa * 100);
2633 else
2634 fw_act_timeout = jiffies +
2635 msecs_to_jiffies(admin_timeout * 1000);
2636
2637 nvme_stop_queues(ctrl);
2638 while (nvme_ctrl_pp_status(ctrl)) {
2639 if (time_after(jiffies, fw_act_timeout)) {
2640 dev_warn(ctrl->device,
2641 "Fw activation timeout, reset controller\n");
2642 nvme_reset_ctrl(ctrl);
2643 break;
2644 }
2645 msleep(100);
2646 }
2647
2648 if (ctrl->state != NVME_CTRL_LIVE)
2649 return;
2650
2651 nvme_start_queues(ctrl);
2652 /* read FW slot informationi to clear the AER*/
2653 nvme_get_fw_slot_info(ctrl);
2654}
2655
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002656void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2657 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002658{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002659 u32 result = le32_to_cpu(res->u32);
2660 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002661
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002662 switch (le16_to_cpu(status) >> 1) {
2663 case NVME_SC_SUCCESS:
2664 done = false;
2665 /*FALLTHRU*/
2666 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002667 ++ctrl->event_limit;
James Smartcd482822017-09-14 11:03:09 -07002668 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002669 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002670 break;
2671 default:
2672 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002673 }
2674
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002675 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002676 return;
2677
2678 switch (result & 0xff07) {
2679 case NVME_AER_NOTICE_NS_CHANGED:
2680 dev_info(ctrl->device, "rescanning\n");
2681 nvme_queue_scan(ctrl);
2682 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302683 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002684 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302685 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002686 default:
2687 dev_warn(ctrl->device, "async event result %08x\n", result);
2688 }
2689}
2690EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2691
2692void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2693{
2694 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002695 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002696}
2697EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2698
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002699static DEFINE_IDA(nvme_instance_ida);
2700
2701static int nvme_set_instance(struct nvme_ctrl *ctrl)
2702{
2703 int instance, error;
2704
2705 do {
2706 if (!ida_pre_get(&nvme_instance_ida, GFP_KERNEL))
2707 return -ENODEV;
2708
2709 spin_lock(&dev_list_lock);
2710 error = ida_get_new(&nvme_instance_ida, &instance);
2711 spin_unlock(&dev_list_lock);
2712 } while (error == -EAGAIN);
2713
2714 if (error)
2715 return -ENODEV;
2716
2717 ctrl->instance = instance;
2718 return 0;
2719}
2720
2721static void nvme_release_instance(struct nvme_ctrl *ctrl)
2722{
2723 spin_lock(&dev_list_lock);
2724 ida_remove(&nvme_instance_ida, ctrl->instance);
2725 spin_unlock(&dev_list_lock);
2726}
2727
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002728void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002729{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002730 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002731 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002732 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302733 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002734}
2735EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002736
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002737void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2738{
2739 if (ctrl->kato)
2740 nvme_start_keep_alive(ctrl);
2741
2742 if (ctrl->queue_count > 1) {
2743 nvme_queue_scan(ctrl);
2744 nvme_queue_async_events(ctrl);
2745 nvme_start_queues(ctrl);
2746 }
2747}
2748EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2749
2750void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2751{
Keith Busch53029b02015-11-28 15:41:02 +01002752 device_destroy(nvme_class, MKDEV(nvme_char_major, ctrl->instance));
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002753
2754 spin_lock(&dev_list_lock);
2755 list_del(&ctrl->node);
2756 spin_unlock(&dev_list_lock);
Keith Busch53029b02015-11-28 15:41:02 +01002757}
Ming Lin576d55d2016-02-10 10:03:32 -08002758EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002759
2760static void nvme_free_ctrl(struct kref *kref)
2761{
2762 struct nvme_ctrl *ctrl = container_of(kref, struct nvme_ctrl, kref);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002763
2764 put_device(ctrl->device);
2765 nvme_release_instance(ctrl);
Keith Busch075790e2016-02-24 09:15:53 -07002766 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002767
2768 ctrl->ops->free_ctrl(ctrl);
2769}
2770
2771void nvme_put_ctrl(struct nvme_ctrl *ctrl)
2772{
2773 kref_put(&ctrl->kref, nvme_free_ctrl);
2774}
Ming Lin576d55d2016-02-10 10:03:32 -08002775EXPORT_SYMBOL_GPL(nvme_put_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002776
2777/*
2778 * Initialize a NVMe controller structures. This needs to be called during
2779 * earliest initialization so that we have the initialized structured around
2780 * during probing.
2781 */
2782int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2783 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2784{
2785 int ret;
2786
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002787 ctrl->state = NVME_CTRL_NEW;
2788 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002789 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002790 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002791 kref_init(&ctrl->kref);
2792 ctrl->dev = dev;
2793 ctrl->ops = ops;
2794 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002795 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002796 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302797 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002798
2799 ret = nvme_set_instance(ctrl);
2800 if (ret)
2801 goto out;
2802
Keith Busch779ff7562016-01-12 15:09:31 -07002803 ctrl->device = device_create_with_groups(nvme_class, ctrl->dev,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002804 MKDEV(nvme_char_major, ctrl->instance),
Christoph Hellwigf4f0f632016-02-09 12:44:03 -07002805 ctrl, nvme_dev_attr_groups,
Keith Busch779ff7562016-01-12 15:09:31 -07002806 "nvme%d", ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002807 if (IS_ERR(ctrl->device)) {
2808 ret = PTR_ERR(ctrl->device);
2809 goto out_release_instance;
2810 }
2811 get_device(ctrl->device);
Keith Busch075790e2016-02-24 09:15:53 -07002812 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002813
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002814 spin_lock(&dev_list_lock);
2815 list_add_tail(&ctrl->node, &nvme_ctrl_list);
2816 spin_unlock(&dev_list_lock);
2817
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002818 /*
2819 * Initialize latency tolerance controls. The sysfs files won't
2820 * be visible to userspace unless the device actually supports APST.
2821 */
2822 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2823 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2824 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2825
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002826 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002827out_release_instance:
2828 nvme_release_instance(ctrl);
2829out:
2830 return ret;
2831}
Ming Lin576d55d2016-02-10 10:03:32 -08002832EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002833
Keith Busch69d9a992016-02-24 09:15:56 -07002834/**
2835 * nvme_kill_queues(): Ends all namespace queues
2836 * @ctrl: the dead controller that needs to end
2837 *
2838 * Call this function when the driver determines it is unable to get the
2839 * controller in a state capable of servicing IO.
2840 */
2841void nvme_kill_queues(struct nvme_ctrl *ctrl)
2842{
2843 struct nvme_ns *ns;
2844
Keith Busch32f0c4a2016-07-13 11:45:02 -06002845 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002846
Ming Lei443bd902017-06-19 10:21:08 +08002847 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002848 if (ctrl->admin_q)
2849 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002850
Keith Busch32f0c4a2016-07-13 11:45:02 -06002851 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002852 /*
2853 * Revalidating a dead namespace sets capacity to 0. This will
2854 * end buffered writers dirtying pages that can't be synced.
2855 */
Keith Buschf33447b2017-02-10 18:15:51 -05002856 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2857 continue;
2858 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002859 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002860
Ming Lei443bd902017-06-19 10:21:08 +08002861 /* Forcibly unquiesce queues to avoid blocking dispatch */
2862 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002863 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002864 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002865}
Linus Torvalds237045f2016-03-18 17:13:31 -07002866EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002867
Keith Busch302ad8c2017-03-01 14:22:12 -05002868void nvme_unfreeze(struct nvme_ctrl *ctrl)
2869{
2870 struct nvme_ns *ns;
2871
2872 mutex_lock(&ctrl->namespaces_mutex);
2873 list_for_each_entry(ns, &ctrl->namespaces, list)
2874 blk_mq_unfreeze_queue(ns->queue);
2875 mutex_unlock(&ctrl->namespaces_mutex);
2876}
2877EXPORT_SYMBOL_GPL(nvme_unfreeze);
2878
2879void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2880{
2881 struct nvme_ns *ns;
2882
2883 mutex_lock(&ctrl->namespaces_mutex);
2884 list_for_each_entry(ns, &ctrl->namespaces, list) {
2885 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2886 if (timeout <= 0)
2887 break;
2888 }
2889 mutex_unlock(&ctrl->namespaces_mutex);
2890}
2891EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2892
2893void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2894{
2895 struct nvme_ns *ns;
2896
2897 mutex_lock(&ctrl->namespaces_mutex);
2898 list_for_each_entry(ns, &ctrl->namespaces, list)
2899 blk_mq_freeze_queue_wait(ns->queue);
2900 mutex_unlock(&ctrl->namespaces_mutex);
2901}
2902EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2903
2904void nvme_start_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)
Ming Lei1671d522017-03-27 20:06:57 +08002910 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002911 mutex_unlock(&ctrl->namespaces_mutex);
2912}
2913EXPORT_SYMBOL_GPL(nvme_start_freeze);
2914
Keith Busch25646262016-01-04 09:10:57 -07002915void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002916{
2917 struct nvme_ns *ns;
2918
Keith Busch32f0c4a2016-07-13 11:45:02 -06002919 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002920 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002921 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002922 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002923}
Ming Lin576d55d2016-02-10 10:03:32 -08002924EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002925
Keith Busch25646262016-01-04 09:10:57 -07002926void nvme_start_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);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002931 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002932 blk_mq_unquiesce_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_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002936
Sagi Grimberg31b84462017-10-11 12:53:07 +03002937int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2938{
2939 if (!ctrl->ops->reinit_request)
2940 return 0;
2941
2942 return blk_mq_tagset_iter(set, set->driver_data,
2943 ctrl->ops->reinit_request);
2944}
2945EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
2946
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002947int __init nvme_core_init(void)
2948{
2949 int result;
2950
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002951 nvme_wq = alloc_workqueue("nvme-wq",
2952 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2953 if (!nvme_wq)
2954 return -ENOMEM;
2955
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002956 result = __register_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme",
2957 &nvme_dev_fops);
2958 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002959 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002960 else if (result > 0)
2961 nvme_char_major = result;
2962
2963 nvme_class = class_create(THIS_MODULE, "nvme");
2964 if (IS_ERR(nvme_class)) {
2965 result = PTR_ERR(nvme_class);
2966 goto unregister_chrdev;
2967 }
2968
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002969 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002970
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002971unregister_chrdev:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002972 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002973destroy_wq:
2974 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002975 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002976}
2977
2978void nvme_core_exit(void)
2979{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002980 class_destroy(nvme_class);
2981 __unregister_chrdev(nvme_char_major, 0, NVME_MINORS, "nvme");
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002982 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002983}
Ming Lin576d55d2016-02-10 10:03:32 -08002984
2985MODULE_LICENSE("GPL");
2986MODULE_VERSION("1.0");
2987module_init(nvme_core_init);
2988module_exit(nvme_core_exit);