blob: 4fa748c9a3f6de3c98dfaa02f65c366ed3d19eb2 [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
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080055static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080056module_param(default_ps_max_latency_us, ulong, 0644);
57MODULE_PARM_DESC(default_ps_max_latency_us,
58 "max power saving latency for new devices; use PM QOS to change per device");
59
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070060static bool force_apst;
61module_param(force_apst, bool, 0644);
62MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
63
Jens Axboef5d11842017-06-27 12:03:06 -060064static bool streams;
65module_param(streams, bool, 0644);
66MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
67
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020068struct workqueue_struct *nvme_wq;
69EXPORT_SYMBOL_GPL(nvme_wq);
70
Christoph Hellwig9843f682017-10-18 13:10:01 +020071static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020072static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010073static struct class *nvme_class;
74
Arnav Dawnb6dccf72017-07-12 16:10:40 +053075static __le32 nvme_get_log_dw10(u8 lid, size_t size)
76{
77 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
78}
79
Christoph Hellwigd86c4d82017-06-15 15:41:08 +020080int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
81{
82 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
83 return -EBUSY;
84 if (!queue_work(nvme_wq, &ctrl->reset_work))
85 return -EBUSY;
86 return 0;
87}
88EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
89
90static int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
91{
92 int ret;
93
94 ret = nvme_reset_ctrl(ctrl);
95 if (!ret)
96 flush_work(&ctrl->reset_work);
97 return ret;
98}
99
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200100static void nvme_delete_ctrl_work(struct work_struct *work)
101{
102 struct nvme_ctrl *ctrl =
103 container_of(work, struct nvme_ctrl, delete_work);
104
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200105 nvme_stop_ctrl(ctrl);
106 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200107 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200108 nvme_uninit_ctrl(ctrl);
109 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200110}
111
112int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
113{
114 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
115 return -EBUSY;
116 if (!queue_work(nvme_wq, &ctrl->delete_work))
117 return -EBUSY;
118 return 0;
119}
120EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
121
122int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
123{
124 int ret = 0;
125
126 /*
127 * Keep a reference until the work is flushed since ->delete_ctrl
128 * can free the controller.
129 */
130 nvme_get_ctrl(ctrl);
131 ret = nvme_delete_ctrl(ctrl);
132 if (!ret)
133 flush_work(&ctrl->delete_work);
134 nvme_put_ctrl(ctrl);
135 return ret;
136}
137EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
138
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200139static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200140{
141 switch (nvme_req(req)->status & 0x7ff) {
142 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200143 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200144 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200145 return BLK_STS_NOSPC;
Junxiong Guane02ab022017-04-21 12:59:07 +0200146 case NVME_SC_ONCS_NOT_SUPPORTED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200147 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200148 case NVME_SC_WRITE_FAULT:
149 case NVME_SC_READ_ERROR:
150 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200151 case NVME_SC_ACCESS_DENIED:
152 case NVME_SC_READ_ONLY:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200153 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200154 case NVME_SC_GUARD_CHECK:
155 case NVME_SC_APPTAG_CHECK:
156 case NVME_SC_REFTAG_CHECK:
157 case NVME_SC_INVALID_PI:
158 return BLK_STS_PROTECTION;
159 case NVME_SC_RESERVATION_CONFLICT:
160 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200161 default:
162 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200163 }
164}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200165
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200166static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200167{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200168 if (blk_noretry_request(req))
169 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200170 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200171 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200172 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200173 return false;
174 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200175}
176
177void nvme_complete_rq(struct request *req)
178{
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200179 if (unlikely(nvme_req(req)->status && nvme_req_needs_retry(req))) {
180 nvme_req(req)->retries++;
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +0300181 blk_mq_requeue_request(req, true);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200182 return;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200183 }
184
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200185 blk_mq_end_request(req, nvme_error_status(req));
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200186}
187EXPORT_SYMBOL_GPL(nvme_complete_rq);
188
Ming Linc55a2fd2016-05-18 14:05:02 -0700189void nvme_cancel_request(struct request *req, void *data, bool reserved)
190{
191 int status;
192
193 if (!blk_mq_request_started(req))
194 return;
195
196 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
197 "Cancelling I/O %d", req->tag);
198
199 status = NVME_SC_ABORT_REQ;
200 if (blk_queue_dying(req->q))
201 status |= NVME_SC_DNR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200202 nvme_req(req)->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200203 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200204
Ming Linc55a2fd2016-05-18 14:05:02 -0700205}
206EXPORT_SYMBOL_GPL(nvme_cancel_request);
207
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200208bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
209 enum nvme_ctrl_state new_state)
210{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300211 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200212 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200213 bool changed = false;
214
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200215 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300216
217 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200218 switch (new_state) {
219 case NVME_CTRL_LIVE:
220 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200221 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200222 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900223 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200224 changed = true;
225 /* FALLTHRU */
226 default:
227 break;
228 }
229 break;
230 case NVME_CTRL_RESETTING:
231 switch (old_state) {
232 case NVME_CTRL_NEW:
233 case NVME_CTRL_LIVE:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900234 changed = true;
235 /* FALLTHRU */
236 default:
237 break;
238 }
239 break;
240 case NVME_CTRL_RECONNECTING:
241 switch (old_state) {
242 case NVME_CTRL_LIVE:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200243 changed = true;
244 /* FALLTHRU */
245 default:
246 break;
247 }
248 break;
249 case NVME_CTRL_DELETING:
250 switch (old_state) {
251 case NVME_CTRL_LIVE:
252 case NVME_CTRL_RESETTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900253 case NVME_CTRL_RECONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200254 changed = true;
255 /* FALLTHRU */
256 default:
257 break;
258 }
259 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600260 case NVME_CTRL_DEAD:
261 switch (old_state) {
262 case NVME_CTRL_DELETING:
263 changed = true;
264 /* FALLTHRU */
265 default:
266 break;
267 }
268 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200269 default:
270 break;
271 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200272
273 if (changed)
274 ctrl->state = new_state;
275
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200276 spin_unlock_irqrestore(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300277
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200278 return changed;
279}
280EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
281
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100282static void nvme_free_ns(struct kref *kref)
283{
284 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
285
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200286 if (ns->ndev)
287 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100288
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100289 put_disk(ns->disk);
Keith Busch075790e2016-02-24 09:15:53 -0700290 ida_simple_remove(&ns->ctrl->ns_ida, ns->instance);
291 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100292 kfree(ns);
293}
294
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100295static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100296{
297 kref_put(&ns->kref, nvme_free_ns);
298}
299
Christoph Hellwig41609822015-11-20 09:00:02 +0100300struct request *nvme_alloc_request(struct request_queue *q,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200301 struct nvme_command *cmd, unsigned int flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100302{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100303 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100304 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100305
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200306 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100307 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200308 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100309 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200310 qid ? qid - 1 : 0);
311 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100312 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100313 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100314
Christoph Hellwig21d34712015-11-26 09:08:36 +0100315 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800316 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100317
Christoph Hellwig41609822015-11-20 09:00:02 +0100318 return req;
319}
Ming Lin576d55d2016-02-10 10:03:32 -0800320EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100321
Jens Axboef5d11842017-06-27 12:03:06 -0600322static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
323{
324 struct nvme_command c;
325
326 memset(&c, 0, sizeof(c));
327
328 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530329 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600330 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
331 c.directive.dtype = NVME_DIR_IDENTIFY;
332 c.directive.tdtype = NVME_DIR_STREAMS;
333 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
334
335 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
336}
337
338static int nvme_disable_streams(struct nvme_ctrl *ctrl)
339{
340 return nvme_toggle_streams(ctrl, false);
341}
342
343static int nvme_enable_streams(struct nvme_ctrl *ctrl)
344{
345 return nvme_toggle_streams(ctrl, true);
346}
347
348static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
349 struct streams_directive_params *s, u32 nsid)
350{
351 struct nvme_command c;
352
353 memset(&c, 0, sizeof(c));
354 memset(s, 0, sizeof(*s));
355
356 c.directive.opcode = nvme_admin_directive_recv;
357 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700358 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600359 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
360 c.directive.dtype = NVME_DIR_STREAMS;
361
362 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
363}
364
365static int nvme_configure_directives(struct nvme_ctrl *ctrl)
366{
367 struct streams_directive_params s;
368 int ret;
369
370 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
371 return 0;
372 if (!streams)
373 return 0;
374
375 ret = nvme_enable_streams(ctrl);
376 if (ret)
377 return ret;
378
Arnav Dawn62346ea2017-07-12 16:11:53 +0530379 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600380 if (ret)
381 return ret;
382
383 ctrl->nssa = le16_to_cpu(s.nssa);
384 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
385 dev_info(ctrl->device, "too few streams (%u) available\n",
386 ctrl->nssa);
387 nvme_disable_streams(ctrl);
388 return 0;
389 }
390
391 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
392 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
393 return 0;
394}
395
396/*
397 * Check if 'req' has a write hint associated with it. If it does, assign
398 * a valid namespace stream to the write.
399 */
400static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
401 struct request *req, u16 *control,
402 u32 *dsmgmt)
403{
404 enum rw_hint streamid = req->write_hint;
405
406 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
407 streamid = 0;
408 else {
409 streamid--;
410 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
411 return;
412
413 *control |= NVME_RW_DTYPE_STREAMS;
414 *dsmgmt |= streamid << 16;
415 }
416
417 if (streamid < ARRAY_SIZE(req->q->write_hints))
418 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
419}
420
Ming Lin8093f7c2016-04-12 13:10:14 -0600421static inline void nvme_setup_flush(struct nvme_ns *ns,
422 struct nvme_command *cmnd)
423{
424 memset(cmnd, 0, sizeof(*cmnd));
425 cmnd->common.opcode = nvme_cmd_flush;
426 cmnd->common.nsid = cpu_to_le32(ns->ns_id);
427}
428
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200429static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600430 struct nvme_command *cmnd)
431{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100432 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600433 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100434 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600435
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100436 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600437 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200438 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600439
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100440 __rq_for_each_bio(bio, req) {
441 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
442 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
443
444 range[n].cattr = cpu_to_le32(0);
445 range[n].nlb = cpu_to_le32(nlb);
446 range[n].slba = cpu_to_le64(slba);
447 n++;
448 }
449
450 if (WARN_ON_ONCE(n != segments)) {
451 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200452 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100453 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600454
455 memset(cmnd, 0, sizeof(*cmnd));
456 cmnd->dsm.opcode = nvme_cmd_dsm;
457 cmnd->dsm.nsid = cpu_to_le32(ns->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200458 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600459 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
460
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700461 req->special_vec.bv_page = virt_to_page(range);
462 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100463 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700464 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600465
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200466 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600467}
Ming Lin8093f7c2016-04-12 13:10:14 -0600468
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200469static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
470 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600471{
Jens Axboef5d11842017-06-27 12:03:06 -0600472 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600473 u16 control = 0;
474 u32 dsmgmt = 0;
475
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200476 /*
477 * If formated with metadata, require the block layer provide a buffer
478 * unless this namespace is formated such that the metadata can be
479 * stripped/generated by the controller with PRACT=1.
480 */
Sagi Grimberg8fa61122017-06-15 16:31:29 +0300481 if (ns && ns->ms &&
482 (!ns->pi_type || ns->ms != sizeof(struct t10_pi_tuple)) &&
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200483 !blk_integrity_rq(req) && !blk_rq_is_passthrough(req))
484 return BLK_STS_NOTSUPP;
485
Ming Lin8093f7c2016-04-12 13:10:14 -0600486 if (req->cmd_flags & REQ_FUA)
487 control |= NVME_RW_FUA;
488 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
489 control |= NVME_RW_LR;
490
491 if (req->cmd_flags & REQ_RAHEAD)
492 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
493
494 memset(cmnd, 0, sizeof(*cmnd));
495 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Ming Lin8093f7c2016-04-12 13:10:14 -0600496 cmnd->rw.nsid = cpu_to_le32(ns->ns_id);
497 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
498 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
499
Jens Axboef5d11842017-06-27 12:03:06 -0600500 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
501 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
502
Ming Lin8093f7c2016-04-12 13:10:14 -0600503 if (ns->ms) {
504 switch (ns->pi_type) {
505 case NVME_NS_DPS_PI_TYPE3:
506 control |= NVME_RW_PRINFO_PRCHK_GUARD;
507 break;
508 case NVME_NS_DPS_PI_TYPE1:
509 case NVME_NS_DPS_PI_TYPE2:
510 control |= NVME_RW_PRINFO_PRCHK_GUARD |
511 NVME_RW_PRINFO_PRCHK_REF;
512 cmnd->rw.reftag = cpu_to_le32(
513 nvme_block_nr(ns, blk_rq_pos(req)));
514 break;
515 }
516 if (!blk_integrity_rq(req))
517 control |= NVME_RW_PRINFO_PRACT;
518 }
519
520 cmnd->rw.control = cpu_to_le16(control);
521 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200522 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600523}
524
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200525blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600526 struct nvme_command *cmd)
527{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200528 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600529
Christoph Hellwig987f6992017-04-05 19:18:08 +0200530 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200531 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200532 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200533 req->rq_flags |= RQF_DONTPREP;
534 }
535
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100536 switch (req_op(req)) {
537 case REQ_OP_DRV_IN:
538 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800539 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100540 break;
541 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600542 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100543 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200544 case REQ_OP_WRITE_ZEROES:
545 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100546 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600547 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100548 break;
549 case REQ_OP_READ:
550 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200551 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100552 break;
553 default:
554 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200555 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100556 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600557
James Smart721b3912016-10-21 23:33:34 +0300558 cmd->common.command_id = req->tag;
Ming Lin8093f7c2016-04-12 13:10:14 -0600559 return ret;
560}
561EXPORT_SYMBOL_GPL(nvme_setup_cmd);
562
Christoph Hellwig41609822015-11-20 09:00:02 +0100563/*
564 * Returns 0 on success. If the result is negative, it's a Linux error code;
565 * if the result is positive, it's an NVM Express status code
566 */
567int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800568 union nvme_result *result, void *buffer, unsigned bufflen,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200569 unsigned timeout, int qid, int at_head, int flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100570{
571 struct request *req;
572 int ret;
573
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200574 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100575 if (IS_ERR(req))
576 return PTR_ERR(req);
577
578 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
579
Christoph Hellwig21d34712015-11-26 09:08:36 +0100580 if (buffer && bufflen) {
581 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
582 if (ret)
583 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100584 }
585
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200586 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800587 if (result)
588 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200589 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
590 ret = -EINTR;
591 else
592 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100593 out:
594 blk_mq_free_request(req);
595 return ret;
596}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200597EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100598
599int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
600 void *buffer, unsigned bufflen)
601{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200602 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
603 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100604}
Ming Lin576d55d2016-02-10 10:03:32 -0800605EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100606
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400607static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
608 unsigned len, u32 seed, bool write)
609{
610 struct bio_integrity_payload *bip;
611 int ret = -ENOMEM;
612 void *buf;
613
614 buf = kmalloc(len, GFP_KERNEL);
615 if (!buf)
616 goto out;
617
618 ret = -EFAULT;
619 if (write && copy_from_user(buf, ubuf, len))
620 goto out_free_meta;
621
622 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
623 if (IS_ERR(bip)) {
624 ret = PTR_ERR(bip);
625 goto out_free_meta;
626 }
627
628 bip->bip_iter.bi_size = len;
629 bip->bip_iter.bi_sector = seed;
630 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
631 offset_in_page(buf));
632 if (ret == len)
633 return buf;
634 ret = -ENOMEM;
635out_free_meta:
636 kfree(buf);
637out:
638 return ERR_PTR(ret);
639}
640
Keith Busch63263d62017-08-29 17:46:04 -0400641static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400642 struct nvme_command *cmd, void __user *ubuffer,
643 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
644 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100645{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200646 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600647 struct nvme_ns *ns = q->queuedata;
648 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100649 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600650 struct bio *bio = NULL;
651 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100652 int ret;
653
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200654 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100655 if (IS_ERR(req))
656 return PTR_ERR(req);
657
658 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
659
660 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100661 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
662 GFP_KERNEL);
663 if (ret)
664 goto out;
665 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200666 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400667 if (disk && meta_buffer && meta_len) {
668 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
669 meta_seed, write);
670 if (IS_ERR(meta)) {
671 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600672 goto out_unmap;
673 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600674 }
675 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400676
Keith Busch0b7f1f22015-10-23 09:47:28 -0600677 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200678 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
679 ret = -EINTR;
680 else
681 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100682 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800683 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600684 if (meta && !ret && !write) {
685 if (copy_to_user(meta_buffer, meta, meta_len))
686 ret = -EFAULT;
687 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600688 kfree(meta);
689 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200690 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600691 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100692 out:
693 blk_mq_free_request(req);
694 return ret;
695}
696
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200697static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200698{
699 struct nvme_ctrl *ctrl = rq->end_io_data;
700
701 blk_mq_free_request(rq);
702
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200703 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200704 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200705 "failed nvme_keep_alive_end_io error=%d\n",
706 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200707 return;
708 }
709
710 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
711}
712
713static int nvme_keep_alive(struct nvme_ctrl *ctrl)
714{
715 struct nvme_command c;
716 struct request *rq;
717
718 memset(&c, 0, sizeof(c));
719 c.common.opcode = nvme_admin_keep_alive;
720
721 rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
722 NVME_QID_ANY);
723 if (IS_ERR(rq))
724 return PTR_ERR(rq);
725
726 rq->timeout = ctrl->kato * HZ;
727 rq->end_io_data = ctrl;
728
729 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
730
731 return 0;
732}
733
734static void nvme_keep_alive_work(struct work_struct *work)
735{
736 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
737 struct nvme_ctrl, ka_work);
738
739 if (nvme_keep_alive(ctrl)) {
740 /* allocation failure, reset the controller */
741 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200742 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200743 return;
744 }
745}
746
747void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
748{
749 if (unlikely(ctrl->kato == 0))
750 return;
751
752 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
753 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
754}
755EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
756
757void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
758{
759 if (unlikely(ctrl->kato == 0))
760 return;
761
762 cancel_delayed_work_sync(&ctrl->ka_work);
763}
764EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
765
Keith Busch3f7f25a92017-06-20 15:09:56 -0400766static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100767{
768 struct nvme_command c = { };
769 int error;
770
771 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
772 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200773 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100774
775 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
776 if (!*id)
777 return -ENOMEM;
778
779 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
780 sizeof(struct nvme_id_ctrl));
781 if (error)
782 kfree(*id);
783 return error;
784}
785
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200786static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
787 u8 *eui64, u8 *nguid, uuid_t *uuid)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200788{
789 struct nvme_command c = { };
790 int status;
791 void *data;
792 int pos;
793 int len;
794
795 c.identify.opcode = nvme_admin_identify;
796 c.identify.nsid = cpu_to_le32(nsid);
797 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
798
799 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
800 if (!data)
801 return -ENOMEM;
802
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200803 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200804 NVME_IDENTIFY_DATA_SIZE);
805 if (status)
806 goto free_data;
807
808 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
809 struct nvme_ns_id_desc *cur = data + pos;
810
811 if (cur->nidl == 0)
812 break;
813
814 switch (cur->nidt) {
815 case NVME_NIDT_EUI64:
816 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200817 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200818 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
819 cur->nidl);
820 goto free_data;
821 }
822 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200823 memcpy(eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200824 break;
825 case NVME_NIDT_NGUID:
826 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200827 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200828 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
829 cur->nidl);
830 goto free_data;
831 }
832 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200833 memcpy(nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200834 break;
835 case NVME_NIDT_UUID:
836 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200837 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200838 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
839 cur->nidl);
840 goto free_data;
841 }
842 len = NVME_NIDT_UUID_LEN;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200843 uuid_copy(uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200844 break;
845 default:
846 /* Skip unnkown types */
847 len = cur->nidl;
848 break;
849 }
850
851 len += sizeof(*cur);
852 }
853free_data:
854 kfree(data);
855 return status;
856}
857
Keith Busch540c8012015-10-22 15:45:06 -0600858static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
859{
860 struct nvme_command c = { };
861
862 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200863 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600864 c.identify.nsid = cpu_to_le32(nsid);
865 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list, 0x1000);
866}
867
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200868static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
869 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100870{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200871 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100872 struct nvme_command c = { };
873 int error;
874
875 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200876 c.identify.opcode = nvme_admin_identify;
877 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200878 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100879
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200880 id = kmalloc(sizeof(*id), GFP_KERNEL);
881 if (!id)
882 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100883
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200884 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
885 if (error) {
886 dev_warn(ctrl->device, "Identify namespace failed\n");
887 kfree(id);
888 return NULL;
889 }
890
891 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100892}
893
Keith Busch3f7f25a92017-06-20 15:09:56 -0400894static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700895 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100896{
897 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800898 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100899 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100900
901 memset(&c, 0, sizeof(c));
902 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100903 c.features.fid = cpu_to_le32(fid);
904 c.features.dword11 = cpu_to_le32(dword11);
905
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800906 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700907 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700908 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800909 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100910 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100911}
912
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100913int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
914{
915 u32 q_count = (*count - 1) | ((*count - 1) << 16);
916 u32 result;
917 int status, nr_io_queues;
918
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700919 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100920 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200921 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100922 return status;
923
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200924 /*
925 * Degraded controllers might return an error when setting the queue
926 * count. We still want to be able to bring them online and offer
927 * access to the admin queue, as that might be only way to fix them up.
928 */
929 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +0200930 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +0200931 *count = 0;
932 } else {
933 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
934 *count = min(*count, nr_io_queues);
935 }
936
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100937 return 0;
938}
Ming Lin576d55d2016-02-10 10:03:32 -0800939EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100940
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100941static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
942{
943 struct nvme_user_io io;
944 struct nvme_command c;
945 unsigned length, meta_len;
946 void __user *metadata;
947
948 if (copy_from_user(&io, uio, sizeof(io)))
949 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -0700950 if (io.flags)
951 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100952
953 switch (io.opcode) {
954 case nvme_cmd_write:
955 case nvme_cmd_read:
956 case nvme_cmd_compare:
957 break;
958 default:
959 return -EINVAL;
960 }
961
962 length = (io.nblocks + 1) << ns->lba_shift;
963 meta_len = (io.nblocks + 1) * ns->ms;
964 metadata = (void __user *)(uintptr_t)io.metadata;
965
966 if (ns->ext) {
967 length += meta_len;
968 meta_len = 0;
969 } else if (meta_len) {
970 if ((io.metadata & 3) || !io.metadata)
971 return -EINVAL;
972 }
973
974 memset(&c, 0, sizeof(c));
975 c.rw.opcode = io.opcode;
976 c.rw.flags = io.flags;
977 c.rw.nsid = cpu_to_le32(ns->ns_id);
978 c.rw.slba = cpu_to_le64(io.slba);
979 c.rw.length = cpu_to_le16(io.nblocks);
980 c.rw.control = cpu_to_le16(io.control);
981 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
982 c.rw.reftag = cpu_to_le32(io.reftag);
983 c.rw.apptag = cpu_to_le16(io.apptag);
984 c.rw.appmask = cpu_to_le16(io.appmask);
985
Keith Busch63263d62017-08-29 17:46:04 -0400986 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100987 (void __user *)(uintptr_t)io.addr, length,
988 metadata, meta_len, io.slba, NULL, 0);
989}
990
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +0100991static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100992 struct nvme_passthru_cmd __user *ucmd)
993{
994 struct nvme_passthru_cmd cmd;
995 struct nvme_command c;
996 unsigned timeout = 0;
997 int status;
998
999 if (!capable(CAP_SYS_ADMIN))
1000 return -EACCES;
1001 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1002 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001003 if (cmd.flags)
1004 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001005
1006 memset(&c, 0, sizeof(c));
1007 c.common.opcode = cmd.opcode;
1008 c.common.flags = cmd.flags;
1009 c.common.nsid = cpu_to_le32(cmd.nsid);
1010 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1011 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1012 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1013 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1014 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1015 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1016 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1017 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1018
1019 if (cmd.timeout_ms)
1020 timeout = msecs_to_jiffies(cmd.timeout_ms);
1021
1022 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001023 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001024 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1025 0, &cmd.result, timeout);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001026 if (status >= 0) {
1027 if (put_user(cmd.result, &ucmd->result))
1028 return -EFAULT;
1029 }
1030
1031 return status;
1032}
1033
1034static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1035 unsigned int cmd, unsigned long arg)
1036{
1037 struct nvme_ns *ns = bdev->bd_disk->private_data;
1038
1039 switch (cmd) {
1040 case NVME_IOCTL_ID:
1041 force_successful_syscall_return();
1042 return ns->ns_id;
1043 case NVME_IOCTL_ADMIN_CMD:
1044 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1045 case NVME_IOCTL_IO_CMD:
1046 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1047 case NVME_IOCTL_SUBMIT_IO:
1048 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001049 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001050#ifdef CONFIG_NVM
1051 if (ns->ndev)
1052 return nvme_nvm_ioctl(ns, cmd, arg);
1053#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001054 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001055 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001056 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001057 return -ENOTTY;
1058 }
1059}
1060
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001061static int nvme_open(struct block_device *bdev, fmode_t mode)
1062{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001063 struct nvme_ns *ns = bdev->bd_disk->private_data;
1064
1065 if (!kref_get_unless_zero(&ns->kref))
1066 return -ENXIO;
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001067 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001068}
1069
1070static void nvme_release(struct gendisk *disk, fmode_t mode)
1071{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001072 nvme_put_ns(disk->private_data);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001073}
1074
1075static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1076{
1077 /* some standard values */
1078 geo->heads = 1 << 6;
1079 geo->sectors = 1 << 5;
1080 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1081 return 0;
1082}
1083
1084#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001085static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1086 u16 bs)
1087{
1088 struct nvme_ns *ns = disk->private_data;
1089 u16 old_ms = ns->ms;
1090 u8 pi_type = 0;
1091
1092 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1093 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1094
1095 /* PI implementation requires metadata equal t10 pi tuple size */
1096 if (ns->ms == sizeof(struct t10_pi_tuple))
1097 pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1098
1099 if (blk_get_integrity(disk) &&
1100 (ns->pi_type != pi_type || ns->ms != old_ms ||
1101 bs != queue_logical_block_size(disk->queue) ||
1102 (ns->ms && ns->ext)))
1103 blk_integrity_unregister(disk);
1104
1105 ns->pi_type = pi_type;
1106}
1107
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001108static void nvme_init_integrity(struct nvme_ns *ns)
1109{
1110 struct blk_integrity integrity;
1111
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001112 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001113 switch (ns->pi_type) {
1114 case NVME_NS_DPS_PI_TYPE3:
1115 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001116 integrity.tag_size = sizeof(u16) + sizeof(u32);
1117 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001118 break;
1119 case NVME_NS_DPS_PI_TYPE1:
1120 case NVME_NS_DPS_PI_TYPE2:
1121 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001122 integrity.tag_size = sizeof(u16);
1123 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001124 break;
1125 default:
1126 integrity.profile = NULL;
1127 break;
1128 }
1129 integrity.tuple_size = ns->ms;
1130 blk_integrity_register(ns->disk, &integrity);
1131 blk_queue_max_integrity_segments(ns->queue, 1);
1132}
1133#else
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001134static void nvme_prep_integrity(struct gendisk *disk, struct nvme_id_ns *id,
1135 u16 bs)
1136{
1137}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001138static void nvme_init_integrity(struct nvme_ns *ns)
1139{
1140}
1141#endif /* CONFIG_BLK_DEV_INTEGRITY */
1142
Scott Bauer6b8190d2017-06-15 10:44:30 -06001143static void nvme_set_chunk_size(struct nvme_ns *ns)
1144{
1145 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1146 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1147}
1148
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001149static void nvme_config_discard(struct nvme_ns *ns)
1150{
Keith Busch08095e72016-03-04 13:15:17 -07001151 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001152 u32 logical_block_size = queue_logical_block_size(ns->queue);
Keith Busch08095e72016-03-04 13:15:17 -07001153
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001154 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1155 NVME_DSM_MAX_RANGES);
1156
Jens Axboef5d11842017-06-27 12:03:06 -06001157 if (ctrl->nr_streams && ns->sws && ns->sgs) {
1158 unsigned int sz = logical_block_size * ns->sws * ns->sgs;
1159
1160 ns->queue->limits.discard_alignment = sz;
1161 ns->queue->limits.discard_granularity = sz;
1162 } else {
1163 ns->queue->limits.discard_alignment = logical_block_size;
1164 ns->queue->limits.discard_granularity = logical_block_size;
1165 }
Minfei Huangbd0fc282016-05-17 15:58:41 +08001166 blk_queue_max_discard_sectors(ns->queue, UINT_MAX);
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001167 blk_queue_max_discard_segments(ns->queue, NVME_DSM_MAX_RANGES);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001168 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, ns->queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001169
1170 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
1171 blk_queue_max_write_zeroes_sectors(ns->queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001172}
1173
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001174static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
1175 struct nvme_id_ns *id, u8 *eui64, u8 *nguid, uuid_t *uuid)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001176{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001177 if (ctrl->vs >= NVME_VS(1, 1, 0))
1178 memcpy(eui64, id->eui64, sizeof(id->eui64));
1179 if (ctrl->vs >= NVME_VS(1, 2, 0))
1180 memcpy(nguid, id->nguid, sizeof(id->nguid));
1181 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001182 /* Don't treat error as fatal we potentially
1183 * already have a NGUID or EUI-64
1184 */
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001185 if (nvme_identify_ns_descs(ctrl, nsid, eui64, nguid, uuid))
1186 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001187 "%s: Identify Descriptors failed\n", __func__);
1188 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001189}
1190
1191static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1192{
1193 struct nvme_ns *ns = disk->private_data;
Jens Axboef5d11842017-06-27 12:03:06 -06001194 struct nvme_ctrl *ctrl = ns->ctrl;
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001195 u16 bs;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001196
1197 /*
1198 * If identify namespace failed, use default 512 byte block size so
1199 * block layer can use before failing read/write for 0 capacity.
1200 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001201 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001202 if (ns->lba_shift == 0)
1203 ns->lba_shift = 9;
1204 bs = 1 << ns->lba_shift;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001205 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001206
1207 blk_mq_freeze_queue(disk->queue);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001208
Jens Axboef5d11842017-06-27 12:03:06 -06001209 if (ctrl->ops->flags & NVME_F_METADATA_SUPPORTED)
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001210 nvme_prep_integrity(disk, id, bs);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001211 blk_queue_logical_block_size(ns->queue, bs);
Scott Bauer6b8190d2017-06-15 10:44:30 -06001212 if (ns->noiob)
1213 nvme_set_chunk_size(ns);
Keith Busch4b9d5b12015-11-20 09:13:30 +01001214 if (ns->ms && !blk_get_integrity(disk) && !ns->ext)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001215 nvme_init_integrity(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001216 if (ns->ms && !(ns->ms == 8 && ns->pi_type) && !blk_get_integrity(disk))
1217 set_capacity(disk, 0);
1218 else
1219 set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
1220
Jens Axboef5d11842017-06-27 12:03:06 -06001221 if (ctrl->oncs & NVME_CTRL_ONCS_DSM)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001222 nvme_config_discard(ns);
1223 blk_mq_unfreeze_queue(disk->queue);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001224}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001225
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001226static int nvme_revalidate_disk(struct gendisk *disk)
1227{
1228 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001229 struct nvme_ctrl *ctrl = ns->ctrl;
1230 struct nvme_id_ns *id;
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001231 u8 eui64[8] = { 0 }, nguid[16] = { 0 };
1232 uuid_t uuid = uuid_null;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001233 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001234
1235 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1236 set_capacity(disk, 0);
1237 return -ENODEV;
1238 }
1239
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001240 id = nvme_identify_ns(ctrl, ns->ns_id);
1241 if (!id)
1242 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001243
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001244 if (id->ncap == 0) {
1245 ret = -ENODEV;
1246 goto out;
1247 }
1248
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001249 nvme_report_ns_ids(ctrl, ns->ns_id, id, eui64, nguid, &uuid);
1250 if (!uuid_equal(&ns->uuid, &uuid) ||
1251 memcmp(&ns->nguid, &nguid, sizeof(ns->nguid)) ||
1252 memcmp(&ns->eui, &eui64, sizeof(ns->eui))) {
1253 dev_err(ctrl->device,
1254 "identifiers changed for nsid %d\n", ns->ns_id);
1255 ret = -ENODEV;
1256 }
1257
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001258out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001259 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001260 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001261}
1262
1263static char nvme_pr_type(enum pr_type type)
1264{
1265 switch (type) {
1266 case PR_WRITE_EXCLUSIVE:
1267 return 1;
1268 case PR_EXCLUSIVE_ACCESS:
1269 return 2;
1270 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1271 return 3;
1272 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1273 return 4;
1274 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1275 return 5;
1276 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1277 return 6;
1278 default:
1279 return 0;
1280 }
1281};
1282
1283static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1284 u64 key, u64 sa_key, u8 op)
1285{
1286 struct nvme_ns *ns = bdev->bd_disk->private_data;
1287 struct nvme_command c;
1288 u8 data[16] = { 0, };
1289
1290 put_unaligned_le64(key, &data[0]);
1291 put_unaligned_le64(sa_key, &data[8]);
1292
1293 memset(&c, 0, sizeof(c));
1294 c.common.opcode = op;
1295 c.common.nsid = cpu_to_le32(ns->ns_id);
1296 c.common.cdw10[0] = cpu_to_le32(cdw10);
1297
1298 return nvme_submit_sync_cmd(ns->queue, &c, data, 16);
1299}
1300
1301static int nvme_pr_register(struct block_device *bdev, u64 old,
1302 u64 new, unsigned flags)
1303{
1304 u32 cdw10;
1305
1306 if (flags & ~PR_FL_IGNORE_KEY)
1307 return -EOPNOTSUPP;
1308
1309 cdw10 = old ? 2 : 0;
1310 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1311 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1312 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1313}
1314
1315static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1316 enum pr_type type, unsigned flags)
1317{
1318 u32 cdw10;
1319
1320 if (flags & ~PR_FL_IGNORE_KEY)
1321 return -EOPNOTSUPP;
1322
1323 cdw10 = nvme_pr_type(type) << 8;
1324 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1325 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1326}
1327
1328static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1329 enum pr_type type, bool abort)
1330{
1331 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1332 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1333}
1334
1335static int nvme_pr_clear(struct block_device *bdev, u64 key)
1336{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001337 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001338 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1339}
1340
1341static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1342{
1343 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1344 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1345}
1346
1347static const struct pr_ops nvme_pr_ops = {
1348 .pr_register = nvme_pr_register,
1349 .pr_reserve = nvme_pr_reserve,
1350 .pr_release = nvme_pr_release,
1351 .pr_preempt = nvme_pr_preempt,
1352 .pr_clear = nvme_pr_clear,
1353};
1354
Scott Bauera98e58e52017-02-03 12:50:32 -07001355#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001356int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1357 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001358{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001359 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001360 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001361
1362 memset(&cmd, 0, sizeof(cmd));
1363 if (send)
1364 cmd.common.opcode = nvme_admin_security_send;
1365 else
1366 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001367 cmd.common.nsid = 0;
1368 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1369 cmd.common.cdw10[1] = cpu_to_le32(len);
1370
1371 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1372 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1373}
1374EXPORT_SYMBOL_GPL(nvme_sec_submit);
1375#endif /* CONFIG_BLK_SED_OPAL */
1376
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001377static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001378 .owner = THIS_MODULE,
1379 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001380 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001381 .open = nvme_open,
1382 .release = nvme_release,
1383 .getgeo = nvme_getgeo,
1384 .revalidate_disk= nvme_revalidate_disk,
1385 .pr_ops = &nvme_pr_ops,
1386};
1387
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001388static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1389{
1390 unsigned long timeout =
1391 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1392 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1393 int ret;
1394
1395 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001396 if (csts == ~0)
1397 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001398 if ((csts & NVME_CSTS_RDY) == bit)
1399 break;
1400
1401 msleep(100);
1402 if (fatal_signal_pending(current))
1403 return -EINTR;
1404 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001405 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001406 "Device not ready; aborting %s\n", enabled ?
1407 "initialisation" : "reset");
1408 return -ENODEV;
1409 }
1410 }
1411
1412 return ret;
1413}
1414
1415/*
1416 * If the device has been passed off to us in an enabled state, just clear
1417 * the enabled bit. The spec says we should set the 'shutdown notification
1418 * bits', but doing so may cause the device to complete commands to the
1419 * admin queue ... and we don't know what memory that might be pointing at!
1420 */
1421int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1422{
1423 int ret;
1424
1425 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1426 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1427
1428 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1429 if (ret)
1430 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001431
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001432 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001433 msleep(NVME_QUIRK_DELAY_AMOUNT);
1434
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001435 return nvme_wait_ready(ctrl, cap, false);
1436}
Ming Lin576d55d2016-02-10 10:03:32 -08001437EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001438
1439int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1440{
1441 /*
1442 * Default to a 4K page size, with the intention to update this
1443 * path in the future to accomodate architectures with differing
1444 * kernel and IO page sizes.
1445 */
1446 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1447 int ret;
1448
1449 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001450 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001451 "Minimum device page size %u too large for host (%u)\n",
1452 1 << dev_page_min, 1 << page_shift);
1453 return -ENODEV;
1454 }
1455
1456 ctrl->page_size = 1 << page_shift;
1457
1458 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1459 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001460 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001461 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1462 ctrl->ctrl_config |= NVME_CC_ENABLE;
1463
1464 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1465 if (ret)
1466 return ret;
1467 return nvme_wait_ready(ctrl, cap, true);
1468}
Ming Lin576d55d2016-02-10 10:03:32 -08001469EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001470
1471int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1472{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001473 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001474 u32 csts;
1475 int ret;
1476
1477 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1478 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1479
1480 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1481 if (ret)
1482 return ret;
1483
1484 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1485 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1486 break;
1487
1488 msleep(100);
1489 if (fatal_signal_pending(current))
1490 return -EINTR;
1491 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001492 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001493 "Device shutdown incomplete; abort shutdown\n");
1494 return -ENODEV;
1495 }
1496 }
1497
1498 return ret;
1499}
Ming Lin576d55d2016-02-10 10:03:32 -08001500EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001501
Christoph Hellwigda358252016-03-02 18:07:11 +01001502static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1503 struct request_queue *q)
1504{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001505 bool vwc = false;
1506
Christoph Hellwigda358252016-03-02 18:07:11 +01001507 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001508 u32 max_segments =
1509 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1510
Christoph Hellwigda358252016-03-02 18:07:11 +01001511 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001512 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001513 }
Keith Busche6282ae2016-12-19 11:37:50 -05001514 if (ctrl->quirks & NVME_QUIRK_STRIPE_SIZE)
1515 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001516 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001517 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1518 vwc = true;
1519 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001520}
1521
Jon Derrickdbf86b32017-08-16 09:51:29 +02001522static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1523{
1524 __le64 ts;
1525 int ret;
1526
1527 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1528 return 0;
1529
1530 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1531 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1532 NULL);
1533 if (ret)
1534 dev_warn_once(ctrl->device,
1535 "could not set timestamp (%d)\n", ret);
1536 return ret;
1537}
1538
Keith Busch634b8322017-08-10 11:23:31 +02001539static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001540{
1541 /*
1542 * APST (Autonomous Power State Transition) lets us program a
1543 * table of power state transitions that the controller will
1544 * perform automatically. We configure it with a simple
1545 * heuristic: we are willing to spend at most 2% of the time
1546 * transitioning between power states. Therefore, when running
1547 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001548 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001549 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001550 * the requested maximum latency.
1551 *
1552 * We will not autonomously enter any non-operational state for
1553 * which the total latency exceeds ps_max_latency_us. Users
1554 * can set ps_max_latency_us to zero to turn off APST.
1555 */
1556
1557 unsigned apste;
1558 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001559 u64 max_lat_us = 0;
1560 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001561 int ret;
1562
1563 /*
1564 * If APST isn't supported or if we haven't been initialized yet,
1565 * then don't do anything.
1566 */
1567 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001568 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001569
1570 if (ctrl->npss > 31) {
1571 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001572 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001573 }
1574
1575 table = kzalloc(sizeof(*table), GFP_KERNEL);
1576 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001577 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001578
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001579 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001580 /* Turn off APST. */
1581 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001582 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001583 } else {
1584 __le64 target = cpu_to_le64(0);
1585 int state;
1586
1587 /*
1588 * Walk through all states from lowest- to highest-power.
1589 * According to the spec, lower-numbered states use more
1590 * power. NPSS, despite the name, is the index of the
1591 * lowest-power state, not the number of states.
1592 */
1593 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001594 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001595
1596 if (target)
1597 table->entries[state] = target;
1598
1599 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001600 * Don't allow transitions to the deepest state
1601 * if it's quirked off.
1602 */
1603 if (state == ctrl->npss &&
1604 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1605 continue;
1606
1607 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001608 * Is this state a useful non-operational state for
1609 * higher-power states to autonomously transition to?
1610 */
1611 if (!(ctrl->psd[state].flags &
1612 NVME_PS_FLAGS_NON_OP_STATE))
1613 continue;
1614
Kai-Heng Fengda875912017-06-07 15:25:42 +08001615 exit_latency_us =
1616 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1617 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001618 continue;
1619
Kai-Heng Fengda875912017-06-07 15:25:42 +08001620 total_latency_us =
1621 exit_latency_us +
1622 le32_to_cpu(ctrl->psd[state].entry_lat);
1623
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001624 /*
1625 * This state is good. Use it as the APST idle
1626 * target for higher power states.
1627 */
1628 transition_ms = total_latency_us + 19;
1629 do_div(transition_ms, 20);
1630 if (transition_ms > (1 << 24) - 1)
1631 transition_ms = (1 << 24) - 1;
1632
1633 target = cpu_to_le64((state << 3) |
1634 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001635
1636 if (max_ps == -1)
1637 max_ps = state;
1638
1639 if (total_latency_us > max_lat_us)
1640 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001641 }
1642
1643 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001644
1645 if (max_ps == -1) {
1646 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1647 } else {
1648 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1649 max_ps, max_lat_us, (int)sizeof(*table), table);
1650 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001651 }
1652
1653 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1654 table, sizeof(*table), NULL);
1655 if (ret)
1656 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1657
1658 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001659 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001660}
1661
1662static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1663{
1664 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1665 u64 latency;
1666
1667 switch (val) {
1668 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1669 case PM_QOS_LATENCY_ANY:
1670 latency = U64_MAX;
1671 break;
1672
1673 default:
1674 latency = val;
1675 }
1676
1677 if (ctrl->ps_max_latency_us != latency) {
1678 ctrl->ps_max_latency_us = latency;
1679 nvme_configure_apst(ctrl);
1680 }
1681}
1682
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001683struct nvme_core_quirk_entry {
1684 /*
1685 * NVMe model and firmware strings are padded with spaces. For
1686 * simplicity, strings in the quirk table are padded with NULLs
1687 * instead.
1688 */
1689 u16 vid;
1690 const char *mn;
1691 const char *fr;
1692 unsigned long quirks;
1693};
1694
1695static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001696 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001697 /*
1698 * This Toshiba device seems to die using any APST states. See:
1699 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1700 */
1701 .vid = 0x1179,
1702 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001703 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001704 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001705};
1706
1707/* match is null-terminated but idstr is space-padded. */
1708static bool string_matches(const char *idstr, const char *match, size_t len)
1709{
1710 size_t matchlen;
1711
1712 if (!match)
1713 return true;
1714
1715 matchlen = strlen(match);
1716 WARN_ON_ONCE(matchlen > len);
1717
1718 if (memcmp(idstr, match, matchlen))
1719 return false;
1720
1721 for (; matchlen < len; matchlen++)
1722 if (idstr[matchlen] != ' ')
1723 return false;
1724
1725 return true;
1726}
1727
1728static bool quirk_matches(const struct nvme_id_ctrl *id,
1729 const struct nvme_core_quirk_entry *q)
1730{
1731 return q->vid == le16_to_cpu(id->vid) &&
1732 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1733 string_matches(id->fr, q->fr, sizeof(id->fr));
1734}
1735
Christoph Hellwig180de0072017-06-26 12:39:02 +02001736static void nvme_init_subnqn(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
1737{
1738 size_t nqnlen;
1739 int off;
1740
1741 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
1742 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
1743 strcpy(ctrl->subnqn, id->subnqn);
1744 return;
1745 }
1746
1747 if (ctrl->vs >= NVME_VS(1, 2, 1))
1748 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
1749
1750 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
1751 off = snprintf(ctrl->subnqn, NVMF_NQN_SIZE,
1752 "nqn.2014.08.org.nvmexpress:%4x%4x",
1753 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
1754 memcpy(ctrl->subnqn + off, id->sn, sizeof(id->sn));
1755 off += sizeof(id->sn);
1756 memcpy(ctrl->subnqn + off, id->mn, sizeof(id->mn));
1757 off += sizeof(id->mn);
1758 memset(ctrl->subnqn + off, 0, sizeof(ctrl->subnqn) - off);
1759}
1760
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001761/*
1762 * Initialize the cached copies of the Identify data and various controller
1763 * register in our nvme_ctrl structure. This should be called as soon as
1764 * the admin queue is fully up and running.
1765 */
1766int nvme_init_identify(struct nvme_ctrl *ctrl)
1767{
1768 struct nvme_id_ctrl *id;
1769 u64 cap;
1770 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001771 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001772 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001773
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001774 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
1775 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001776 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001777 return ret;
1778 }
1779
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001780 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
1781 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001782 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001783 return ret;
1784 }
1785 page_shift = NVME_CAP_MPSMIN(cap) + 12;
1786
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06001787 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001788 ctrl->subsystem = NVME_CAP_NSSRC(cap);
1789
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001790 ret = nvme_identify_ctrl(ctrl, &id);
1791 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001792 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001793 return -EIO;
1794 }
1795
Christoph Hellwig180de0072017-06-26 12:39:02 +02001796 nvme_init_subnqn(ctrl, id);
1797
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001798 if (!ctrl->identified) {
1799 /*
1800 * Check for quirks. Quirk can depend on firmware version,
1801 * so, in principle, the set of quirks present can change
1802 * across a reset. As a possible future enhancement, we
1803 * could re-scan for quirks every time we reinitialize
1804 * the device, but we'd have to make sure that the driver
1805 * behaves intelligently if the quirks change.
1806 */
1807
1808 int i;
1809
1810 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
1811 if (quirk_matches(id, &core_quirks[i]))
1812 ctrl->quirks |= core_quirks[i].quirks;
1813 }
1814 }
1815
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001816 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001817 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 -07001818 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
1819 }
1820
Scott Bauer8a9ae522017-02-17 13:59:40 +01001821 ctrl->oacs = le16_to_cpu(id->oacs);
Keith Busch118472a2016-02-18 09:57:48 -07001822 ctrl->vid = le16_to_cpu(id->vid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001823 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01001824 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001825 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08001826 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001827 memcpy(ctrl->serial, id->sn, sizeof(id->sn));
1828 memcpy(ctrl->model, id->mn, sizeof(id->mn));
1829 memcpy(ctrl->firmware_rev, id->fr, sizeof(id->fr));
1830 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001831 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001832 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02001833 max_hw_sectors = UINT_MAX;
1834 ctrl->max_hw_sectors =
1835 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001836
Christoph Hellwigda358252016-03-02 18:07:11 +01001837 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001838 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001839 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001840
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001841 if (id->rtd3e) {
1842 /* us -> s */
1843 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
1844
1845 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
1846 shutdown_timeout, 60);
1847
1848 if (ctrl->shutdown_timeout != shutdown_timeout)
1849 dev_warn(ctrl->device,
1850 "Shutdown timeout set to %u seconds\n",
1851 ctrl->shutdown_timeout);
1852 } else
1853 ctrl->shutdown_timeout = shutdown_timeout;
1854
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001855 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001856 ctrl->apsta = id->apsta;
1857 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001858 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
1859 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001860 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 -04001861 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001862 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001863 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001864 }
1865 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001866 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07001867 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001868 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
1869
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02001870 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001871 ctrl->icdoff = le16_to_cpu(id->icdoff);
1872 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
1873 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
1874 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
1875
1876 /*
1877 * In fabrics we need to verify the cntlid matches the
1878 * admin connect
1879 */
Keith Busch634b8322017-08-10 11:23:31 +02001880 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001881 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001882 goto out_free;
1883 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001884
1885 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001886 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001887 "keep-alive support is mandatory for fabrics\n");
1888 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02001889 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02001890 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001891 } else {
1892 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02001893 ctrl->hmpre = le32_to_cpu(id->hmpre);
1894 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04001895 ctrl->hmminds = le32_to_cpu(id->hmminds);
1896 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001897 }
Christoph Hellwigda358252016-03-02 18:07:11 +01001898
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001899 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001900
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001901 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001902 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001903 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001904 dev_pm_qos_hide_latency_tolerance(ctrl->device);
1905
Keith Busch634b8322017-08-10 11:23:31 +02001906 ret = nvme_configure_apst(ctrl);
1907 if (ret < 0)
1908 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02001909
1910 ret = nvme_configure_timestamp(ctrl);
1911 if (ret < 0)
1912 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02001913
1914 ret = nvme_configure_directives(ctrl);
1915 if (ret < 0)
1916 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001917
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001918 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001919
Keith Busch634b8322017-08-10 11:23:31 +02001920 return 0;
1921
1922out_free:
1923 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02001924 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001925}
Ming Lin576d55d2016-02-10 10:03:32 -08001926EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01001927
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001928static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001929{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001930 struct nvme_ctrl *ctrl =
1931 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001932
Christoph Hellwig999ada22017-10-18 17:09:31 +02001933 if (ctrl->state != NVME_CTRL_LIVE)
Christoph Hellwiga6a51492017-10-18 16:59:25 +02001934 return -EWOULDBLOCK;
1935 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001936 return 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001937}
1938
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001939static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
1940{
1941 struct nvme_ns *ns;
1942 int ret;
1943
1944 mutex_lock(&ctrl->namespaces_mutex);
1945 if (list_empty(&ctrl->namespaces)) {
1946 ret = -ENOTTY;
1947 goto out_unlock;
1948 }
1949
1950 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
1951 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001952 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001953 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
1954 ret = -EINVAL;
1955 goto out_unlock;
1956 }
1957
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001958 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001959 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
1960 kref_get(&ns->kref);
1961 mutex_unlock(&ctrl->namespaces_mutex);
1962
1963 ret = nvme_user_cmd(ctrl, ns, argp);
1964 nvme_put_ns(ns);
1965 return ret;
1966
1967out_unlock:
1968 mutex_unlock(&ctrl->namespaces_mutex);
1969 return ret;
1970}
1971
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001972static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
1973 unsigned long arg)
1974{
1975 struct nvme_ctrl *ctrl = file->private_data;
1976 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001977
1978 switch (cmd) {
1979 case NVME_IOCTL_ADMIN_CMD:
1980 return nvme_user_cmd(ctrl, NULL, argp);
1981 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01001982 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001983 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001984 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02001985 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001986 case NVME_IOCTL_SUBSYS_RESET:
1987 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06001988 case NVME_IOCTL_RESCAN:
1989 nvme_queue_scan(ctrl);
1990 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001991 default:
1992 return -ENOTTY;
1993 }
1994}
1995
1996static const struct file_operations nvme_dev_fops = {
1997 .owner = THIS_MODULE,
1998 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001999 .unlocked_ioctl = nvme_dev_ioctl,
2000 .compat_ioctl = nvme_dev_ioctl,
2001};
2002
2003static ssize_t nvme_sysfs_reset(struct device *dev,
2004 struct device_attribute *attr, const char *buf,
2005 size_t count)
2006{
2007 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2008 int ret;
2009
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002010 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002011 if (ret < 0)
2012 return ret;
2013 return count;
2014}
2015static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2016
Keith Busch9ec3bb22016-04-29 15:45:18 -06002017static ssize_t nvme_sysfs_rescan(struct device *dev,
2018 struct device_attribute *attr, const char *buf,
2019 size_t count)
2020{
2021 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2022
2023 nvme_queue_scan(ctrl);
2024 return count;
2025}
2026static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2027
Keith Busch118472a2016-02-18 09:57:48 -07002028static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2029 char *buf)
2030{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002031 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002032 struct nvme_ctrl *ctrl = ns->ctrl;
2033 int serial_len = sizeof(ctrl->serial);
2034 int model_len = sizeof(ctrl->model);
2035
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002036 if (!uuid_is_null(&ns->uuid))
2037 return sprintf(buf, "uuid.%pU\n", &ns->uuid);
2038
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002039 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2040 return sprintf(buf, "eui.%16phN\n", ns->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002041
2042 if (memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2043 return sprintf(buf, "eui.%8phN\n", ns->eui);
2044
Martin Wilck758f3732017-07-20 18:34:02 +02002045 while (serial_len > 0 && (ctrl->serial[serial_len - 1] == ' ' ||
2046 ctrl->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002047 serial_len--;
Martin Wilck758f3732017-07-20 18:34:02 +02002048 while (model_len > 0 && (ctrl->model[model_len - 1] == ' ' ||
2049 ctrl->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002050 model_len--;
2051
2052 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
2053 serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
2054}
2055static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
2056
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002057static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
2058 char *buf)
2059{
2060 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
2061 return sprintf(buf, "%pU\n", ns->nguid);
2062}
2063static DEVICE_ATTR(nguid, S_IRUGO, nguid_show, NULL);
2064
Keith Busch2b9b6e82015-12-22 10:10:45 -07002065static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
2066 char *buf)
2067{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002068 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002069
2070 /* For backward compatibility expose the NGUID to userspace if
2071 * we have no UUID set
2072 */
2073 if (uuid_is_null(&ns->uuid)) {
2074 printk_ratelimited(KERN_WARNING
2075 "No UUID available providing old NGUID\n");
2076 return sprintf(buf, "%pU\n", ns->nguid);
2077 }
2078 return sprintf(buf, "%pU\n", &ns->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002079}
2080static DEVICE_ATTR(uuid, S_IRUGO, uuid_show, NULL);
2081
2082static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
2083 char *buf)
2084{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002085 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002086 return sprintf(buf, "%8phd\n", ns->eui);
2087}
2088static DEVICE_ATTR(eui, S_IRUGO, eui_show, NULL);
2089
2090static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
2091 char *buf)
2092{
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002093 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002094 return sprintf(buf, "%d\n", ns->ns_id);
2095}
2096static DEVICE_ATTR(nsid, S_IRUGO, nsid_show, NULL);
2097
2098static struct attribute *nvme_ns_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002099 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002100 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002101 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002102 &dev_attr_eui.attr,
2103 &dev_attr_nsid.attr,
2104 NULL,
2105};
2106
Ming Lin1a353d82016-06-13 16:45:24 +02002107static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002108 struct attribute *a, int n)
2109{
2110 struct device *dev = container_of(kobj, struct device, kobj);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02002111 struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002112
2113 if (a == &dev_attr_uuid.attr) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002114 if (uuid_is_null(&ns->uuid) ||
2115 !memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
2116 return 0;
2117 }
2118 if (a == &dev_attr_nguid.attr) {
Johannes Thumshirn90985b82017-06-07 11:45:31 +02002119 if (!memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002120 return 0;
2121 }
2122 if (a == &dev_attr_eui.attr) {
2123 if (!memchr_inv(ns->eui, 0, sizeof(ns->eui)))
2124 return 0;
2125 }
2126 return a->mode;
2127}
2128
2129static const struct attribute_group nvme_ns_attr_group = {
2130 .attrs = nvme_ns_attrs,
Ming Lin1a353d82016-06-13 16:45:24 +02002131 .is_visible = nvme_ns_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002132};
2133
Ming Lin931e1c22016-02-26 13:24:19 -08002134#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002135static ssize_t field##_show(struct device *dev, \
2136 struct device_attribute *attr, char *buf) \
2137{ \
2138 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2139 return sprintf(buf, "%.*s\n", (int)sizeof(ctrl->field), ctrl->field); \
2140} \
2141static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2142
Ming Lin931e1c22016-02-26 13:24:19 -08002143#define nvme_show_int_function(field) \
2144static ssize_t field##_show(struct device *dev, \
2145 struct device_attribute *attr, char *buf) \
2146{ \
2147 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2148 return sprintf(buf, "%d\n", ctrl->field); \
2149} \
2150static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2151
2152nvme_show_str_function(model);
2153nvme_show_str_function(serial);
2154nvme_show_str_function(firmware_rev);
2155nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002156
Ming Lin1a353d82016-06-13 16:45:24 +02002157static ssize_t nvme_sysfs_delete(struct device *dev,
2158 struct device_attribute *attr, const char *buf,
2159 size_t count)
2160{
2161 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2162
2163 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002164 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002165 return count;
2166}
2167static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2168
2169static ssize_t nvme_sysfs_show_transport(struct device *dev,
2170 struct device_attribute *attr,
2171 char *buf)
2172{
2173 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2174
2175 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2176}
2177static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2178
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002179static ssize_t nvme_sysfs_show_state(struct device *dev,
2180 struct device_attribute *attr,
2181 char *buf)
2182{
2183 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2184 static const char *const state_name[] = {
2185 [NVME_CTRL_NEW] = "new",
2186 [NVME_CTRL_LIVE] = "live",
2187 [NVME_CTRL_RESETTING] = "resetting",
2188 [NVME_CTRL_RECONNECTING]= "reconnecting",
2189 [NVME_CTRL_DELETING] = "deleting",
2190 [NVME_CTRL_DEAD] = "dead",
2191 };
2192
2193 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2194 state_name[ctrl->state])
2195 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2196
2197 return sprintf(buf, "unknown state\n");
2198}
2199
2200static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2201
Ming Lin1a353d82016-06-13 16:45:24 +02002202static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2203 struct device_attribute *attr,
2204 char *buf)
2205{
2206 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2207
Christoph Hellwig180de0072017-06-26 12:39:02 +02002208 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002209}
2210static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2211
2212static ssize_t nvme_sysfs_show_address(struct device *dev,
2213 struct device_attribute *attr,
2214 char *buf)
2215{
2216 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2217
2218 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2219}
2220static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2221
Keith Busch779ff7562016-01-12 15:09:31 -07002222static struct attribute *nvme_dev_attrs[] = {
2223 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002224 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002225 &dev_attr_model.attr,
2226 &dev_attr_serial.attr,
2227 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002228 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002229 &dev_attr_delete_controller.attr,
2230 &dev_attr_transport.attr,
2231 &dev_attr_subsysnqn.attr,
2232 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002233 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002234 NULL
2235};
2236
Ming Lin1a353d82016-06-13 16:45:24 +02002237static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2238 struct attribute *a, int n)
2239{
2240 struct device *dev = container_of(kobj, struct device, kobj);
2241 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2242
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002243 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2244 return 0;
2245 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2246 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002247
2248 return a->mode;
2249}
2250
Keith Busch779ff7562016-01-12 15:09:31 -07002251static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002252 .attrs = nvme_dev_attrs,
2253 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002254};
2255
2256static const struct attribute_group *nvme_dev_attr_groups[] = {
2257 &nvme_dev_attrs_group,
2258 NULL,
2259};
2260
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002261static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2262{
2263 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2264 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2265
2266 return nsa->ns_id - nsb->ns_id;
2267}
2268
Keith Busch32f0c4a2016-07-13 11:45:02 -06002269static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002270{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002271 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002272
Keith Busch32f0c4a2016-07-13 11:45:02 -06002273 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002274 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002275 if (ns->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002276 if (!kref_get_unless_zero(&ns->kref))
2277 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002278 ret = ns;
2279 break;
2280 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002281 if (ns->ns_id > nsid)
2282 break;
2283 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002284 mutex_unlock(&ctrl->namespaces_mutex);
2285 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002286}
2287
Jens Axboef5d11842017-06-27 12:03:06 -06002288static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2289{
2290 struct streams_directive_params s;
2291 int ret;
2292
2293 if (!ctrl->nr_streams)
2294 return 0;
2295
2296 ret = nvme_get_stream_params(ctrl, &s, ns->ns_id);
2297 if (ret)
2298 return ret;
2299
2300 ns->sws = le32_to_cpu(s.sws);
2301 ns->sgs = le16_to_cpu(s.sgs);
2302
2303 if (ns->sws) {
2304 unsigned int bs = 1 << ns->lba_shift;
2305
2306 blk_queue_io_min(ns->queue, bs * ns->sws);
2307 if (ns->sgs)
2308 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2309 }
2310
2311 return 0;
2312}
2313
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002314static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2315{
2316 struct nvme_ns *ns;
2317 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002318 struct nvme_id_ns *id;
2319 char disk_name[DISK_NAME_LEN];
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002320 int node = dev_to_node(ctrl->dev);
2321
2322 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2323 if (!ns)
2324 return;
2325
Keith Busch075790e2016-02-24 09:15:53 -07002326 ns->instance = ida_simple_get(&ctrl->ns_ida, 1, 0, GFP_KERNEL);
2327 if (ns->instance < 0)
2328 goto out_free_ns;
2329
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002330 ns->queue = blk_mq_init_queue(ctrl->tagset);
2331 if (IS_ERR(ns->queue))
Keith Busch075790e2016-02-24 09:15:53 -07002332 goto out_release_instance;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002333 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, ns->queue);
2334 ns->queue->queuedata = ns;
2335 ns->ctrl = ctrl;
2336
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002337 kref_init(&ns->kref);
2338 ns->ns_id = nsid;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002339 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002340
2341 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002342 nvme_set_queue_limits(ctrl, ns->queue);
Jens Axboef5d11842017-06-27 12:03:06 -06002343 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002344
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002345 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002346
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002347 id = nvme_identify_ns(ctrl, nsid);
2348 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002349 goto out_free_queue;
2350
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002351 if (id->ncap == 0)
2352 goto out_free_id;
2353
2354 nvme_report_ns_ids(ctrl, ns->ns_id, id, ns->eui, ns->nguid, &ns->uuid);
2355
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002356 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
2357 if (nvme_nvm_register(ns, disk_name, node)) {
2358 dev_warn(ctrl->device, "LightNVM init failure\n");
2359 goto out_free_id;
2360 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002361 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002362
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002363 disk = alloc_disk_node(0, node);
2364 if (!disk)
2365 goto out_free_id;
2366
2367 disk->fops = &nvme_fops;
2368 disk->private_data = ns;
2369 disk->queue = ns->queue;
2370 disk->flags = GENHD_FL_EXT_DEVT;
2371 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
2372 ns->disk = disk;
2373
2374 __nvme_revalidate_disk(disk, id);
2375
Keith Busch32f0c4a2016-07-13 11:45:02 -06002376 mutex_lock(&ctrl->namespaces_mutex);
2377 list_add_tail(&ns->list, &ctrl->namespaces);
2378 mutex_unlock(&ctrl->namespaces_mutex);
2379
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002380 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002381
2382 kfree(id);
2383
Dan Williams0d52c7562016-06-15 19:44:20 -07002384 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002385 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
2386 &nvme_ns_attr_group))
2387 pr_warn("%s: failed to create sysfs group for identification\n",
2388 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002389 if (ns->ndev && nvme_nvm_register_sysfs(ns))
2390 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
2391 ns->disk->disk_name);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002392 return;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002393 out_free_id:
2394 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002395 out_free_queue:
2396 blk_cleanup_queue(ns->queue);
Keith Busch075790e2016-02-24 09:15:53 -07002397 out_release_instance:
2398 ida_simple_remove(&ctrl->ns_ida, ns->instance);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002399 out_free_ns:
2400 kfree(ns);
2401}
2402
2403static void nvme_ns_remove(struct nvme_ns *ns)
2404{
Keith Busch646017a2016-02-24 09:15:54 -07002405 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
2406 return;
2407
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002408 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002409 if (blk_get_integrity(ns->disk))
2410 blk_integrity_unregister(ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002411 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
2412 &nvme_ns_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01002413 if (ns->ndev)
2414 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002415 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002416 blk_cleanup_queue(ns->queue);
2417 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002418
2419 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002420 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002421 mutex_unlock(&ns->ctrl->namespaces_mutex);
2422
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002423 nvme_put_ns(ns);
2424}
2425
Keith Busch540c8012015-10-22 15:45:06 -06002426static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2427{
2428 struct nvme_ns *ns;
2429
Keith Busch32f0c4a2016-07-13 11:45:02 -06002430 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06002431 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02002432 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06002433 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002434 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06002435 } else
2436 nvme_alloc_ns(ctrl, nsid);
2437}
2438
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302439static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
2440 unsigned nsid)
2441{
2442 struct nvme_ns *ns, *next;
2443
2444 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
2445 if (ns->ns_id > nsid)
2446 nvme_ns_remove(ns);
2447 }
2448}
2449
Keith Busch540c8012015-10-22 15:45:06 -06002450static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
2451{
2452 struct nvme_ns *ns;
2453 __le32 *ns_list;
2454 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
2455 int ret = 0;
2456
2457 ns_list = kzalloc(0x1000, GFP_KERNEL);
2458 if (!ns_list)
2459 return -ENOMEM;
2460
2461 for (i = 0; i < num_lists; i++) {
2462 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
2463 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302464 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06002465
2466 for (j = 0; j < min(nn, 1024U); j++) {
2467 nsid = le32_to_cpu(ns_list[j]);
2468 if (!nsid)
2469 goto out;
2470
2471 nvme_validate_ns(ctrl, nsid);
2472
2473 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06002474 ns = nvme_find_get_ns(ctrl, prev);
2475 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06002476 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002477 nvme_put_ns(ns);
2478 }
Keith Busch540c8012015-10-22 15:45:06 -06002479 }
2480 }
2481 nn -= j;
2482 }
2483 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302484 nvme_remove_invalid_namespaces(ctrl, prev);
2485 free:
Keith Busch540c8012015-10-22 15:45:06 -06002486 kfree(ns_list);
2487 return ret;
2488}
2489
Christoph Hellwig5955be22016-04-26 13:51:59 +02002490static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002491{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002492 unsigned i;
2493
Keith Busch540c8012015-10-22 15:45:06 -06002494 for (i = 1; i <= nn; i++)
2495 nvme_validate_ns(ctrl, i);
2496
Sunad Bhandary47b0e502016-05-27 15:59:43 +05302497 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002498}
2499
Christoph Hellwig5955be22016-04-26 13:51:59 +02002500static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002501{
Christoph Hellwig5955be22016-04-26 13:51:59 +02002502 struct nvme_ctrl *ctrl =
2503 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002504 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06002505 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002506
Christoph Hellwig5955be22016-04-26 13:51:59 +02002507 if (ctrl->state != NVME_CTRL_LIVE)
2508 return;
2509
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002510 if (nvme_identify_ctrl(ctrl, &id))
2511 return;
Keith Busch540c8012015-10-22 15:45:06 -06002512
2513 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002514 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06002515 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
2516 if (!nvme_scan_ns_list(ctrl, nn))
2517 goto done;
2518 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02002519 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06002520 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06002521 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06002522 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002523 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002524 kfree(id);
2525}
Christoph Hellwig5955be22016-04-26 13:51:59 +02002526
2527void nvme_queue_scan(struct nvme_ctrl *ctrl)
2528{
2529 /*
2530 * Do not queue new scan work when a controller is reset during
2531 * removal.
2532 */
2533 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002534 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002535}
2536EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002537
Keith Busch32f0c4a2016-07-13 11:45:02 -06002538/*
2539 * This function iterates the namespace list unlocked to allow recovery from
2540 * controller failure. It is up to the caller to ensure the namespace list is
2541 * not modified by scan work while this function is executing.
2542 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002543void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
2544{
2545 struct nvme_ns *ns, *next;
2546
Keith Busch0ff9d4e2016-05-12 08:37:14 -06002547 /*
2548 * The dead states indicates the controller was not gracefully
2549 * disconnected. In that case, we won't be able to flush any data while
2550 * removing the namespaces' disks; fail all the queues now to avoid
2551 * potentially having to clean up the failed sync later.
2552 */
2553 if (ctrl->state == NVME_CTRL_DEAD)
2554 nvme_kill_queues(ctrl);
2555
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002556 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list)
2557 nvme_ns_remove(ns);
2558}
Ming Lin576d55d2016-02-10 10:03:32 -08002559EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002560
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002561static void nvme_async_event_work(struct work_struct *work)
2562{
2563 struct nvme_ctrl *ctrl =
2564 container_of(work, struct nvme_ctrl, async_event_work);
2565
2566 spin_lock_irq(&ctrl->lock);
James Smartcd482822017-09-14 11:03:09 -07002567 while (ctrl->state == NVME_CTRL_LIVE && ctrl->event_limit > 0) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002568 int aer_idx = --ctrl->event_limit;
2569
2570 spin_unlock_irq(&ctrl->lock);
2571 ctrl->ops->submit_async_event(ctrl, aer_idx);
2572 spin_lock_irq(&ctrl->lock);
2573 }
2574 spin_unlock_irq(&ctrl->lock);
2575}
2576
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302577static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
2578{
2579
2580 u32 csts;
2581
2582 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
2583 return false;
2584
2585 if (csts == ~0)
2586 return false;
2587
2588 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
2589}
2590
2591static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
2592{
2593 struct nvme_command c = { };
2594 struct nvme_fw_slot_info_log *log;
2595
2596 log = kmalloc(sizeof(*log), GFP_KERNEL);
2597 if (!log)
2598 return;
2599
2600 c.common.opcode = nvme_admin_get_log_page;
Arnav Dawn62346ea2017-07-12 16:11:53 +05302601 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302602 c.common.cdw10[0] = nvme_get_log_dw10(NVME_LOG_FW_SLOT, sizeof(*log));
2603
2604 if (!nvme_submit_sync_cmd(ctrl->admin_q, &c, log, sizeof(*log)))
2605 dev_warn(ctrl->device,
2606 "Get FW SLOT INFO log error\n");
2607 kfree(log);
2608}
2609
2610static void nvme_fw_act_work(struct work_struct *work)
2611{
2612 struct nvme_ctrl *ctrl = container_of(work,
2613 struct nvme_ctrl, fw_act_work);
2614 unsigned long fw_act_timeout;
2615
2616 if (ctrl->mtfa)
2617 fw_act_timeout = jiffies +
2618 msecs_to_jiffies(ctrl->mtfa * 100);
2619 else
2620 fw_act_timeout = jiffies +
2621 msecs_to_jiffies(admin_timeout * 1000);
2622
2623 nvme_stop_queues(ctrl);
2624 while (nvme_ctrl_pp_status(ctrl)) {
2625 if (time_after(jiffies, fw_act_timeout)) {
2626 dev_warn(ctrl->device,
2627 "Fw activation timeout, reset controller\n");
2628 nvme_reset_ctrl(ctrl);
2629 break;
2630 }
2631 msleep(100);
2632 }
2633
2634 if (ctrl->state != NVME_CTRL_LIVE)
2635 return;
2636
2637 nvme_start_queues(ctrl);
2638 /* read FW slot informationi to clear the AER*/
2639 nvme_get_fw_slot_info(ctrl);
2640}
2641
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002642void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
2643 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002644{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002645 u32 result = le32_to_cpu(res->u32);
2646 bool done = true;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002647
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002648 switch (le16_to_cpu(status) >> 1) {
2649 case NVME_SC_SUCCESS:
2650 done = false;
2651 /*FALLTHRU*/
2652 case NVME_SC_ABORT_REQ:
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002653 ++ctrl->event_limit;
James Smartcd482822017-09-14 11:03:09 -07002654 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002655 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002656 break;
2657 default:
2658 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002659 }
2660
Christoph Hellwig7bf58532016-11-10 07:32:34 -08002661 if (done)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002662 return;
2663
2664 switch (result & 0xff07) {
2665 case NVME_AER_NOTICE_NS_CHANGED:
2666 dev_info(ctrl->device, "rescanning\n");
2667 nvme_queue_scan(ctrl);
2668 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302669 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03002670 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302671 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002672 default:
2673 dev_warn(ctrl->device, "async event result %08x\n", result);
2674 }
2675}
2676EXPORT_SYMBOL_GPL(nvme_complete_async_event);
2677
2678void nvme_queue_async_events(struct nvme_ctrl *ctrl)
2679{
2680 ctrl->event_limit = NVME_NR_AERS;
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03002681 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002682}
2683EXPORT_SYMBOL_GPL(nvme_queue_async_events);
2684
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002685void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08002686{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002687 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002688 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002689 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302690 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002691}
2692EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02002693
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03002694void nvme_start_ctrl(struct nvme_ctrl *ctrl)
2695{
2696 if (ctrl->kato)
2697 nvme_start_keep_alive(ctrl);
2698
2699 if (ctrl->queue_count > 1) {
2700 nvme_queue_scan(ctrl);
2701 nvme_queue_async_events(ctrl);
2702 nvme_start_queues(ctrl);
2703 }
2704}
2705EXPORT_SYMBOL_GPL(nvme_start_ctrl);
2706
2707void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
2708{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002709 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01002710}
Ming Lin576d55d2016-02-10 10:03:32 -08002711EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01002712
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002713static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01002714{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002715 struct nvme_ctrl *ctrl =
2716 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002717
Christoph Hellwig9843f682017-10-18 13:10:01 +02002718 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch075790e2016-02-24 09:15:53 -07002719 ida_destroy(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002720
2721 ctrl->ops->free_ctrl(ctrl);
2722}
2723
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002724/*
2725 * Initialize a NVMe controller structures. This needs to be called during
2726 * earliest initialization so that we have the initialized structured around
2727 * during probing.
2728 */
2729int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
2730 const struct nvme_ctrl_ops *ops, unsigned long quirks)
2731{
2732 int ret;
2733
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02002734 ctrl->state = NVME_CTRL_NEW;
2735 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002736 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01002737 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002738 ctrl->dev = dev;
2739 ctrl->ops = ops;
2740 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02002741 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02002742 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05302743 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002744 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002745
Christoph Hellwig9843f682017-10-18 13:10:01 +02002746 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
2747 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002748 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02002749 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002750
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002751 device_initialize(&ctrl->ctrl_device);
2752 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002753 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002754 ctrl->device->class = nvme_class;
2755 ctrl->device->parent = ctrl->dev;
2756 ctrl->device->groups = nvme_dev_attr_groups;
2757 ctrl->device->release = nvme_free_ctrl;
2758 dev_set_drvdata(ctrl->device, ctrl);
2759 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
2760 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002761 goto out_release_instance;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002762
2763 cdev_init(&ctrl->cdev, &nvme_dev_fops);
2764 ctrl->cdev.owner = ops->module;
2765 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002766 if (ret)
2767 goto out_free_name;
2768
Keith Busch075790e2016-02-24 09:15:53 -07002769 ida_init(&ctrl->ns_ida);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002770
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002771 /*
2772 * Initialize latency tolerance controls. The sysfs files won't
2773 * be visible to userspace unless the device actually supports APST.
2774 */
2775 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
2776 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
2777 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
2778
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002779 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02002780out_free_name:
2781 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002782out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02002783 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002784out:
2785 return ret;
2786}
Ming Lin576d55d2016-02-10 10:03:32 -08002787EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002788
Keith Busch69d9a992016-02-24 09:15:56 -07002789/**
2790 * nvme_kill_queues(): Ends all namespace queues
2791 * @ctrl: the dead controller that needs to end
2792 *
2793 * Call this function when the driver determines it is unable to get the
2794 * controller in a state capable of servicing IO.
2795 */
2796void nvme_kill_queues(struct nvme_ctrl *ctrl)
2797{
2798 struct nvme_ns *ns;
2799
Keith Busch32f0c4a2016-07-13 11:45:02 -06002800 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08002801
Ming Lei443bd902017-06-19 10:21:08 +08002802 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06002803 if (ctrl->admin_q)
2804 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08002805
Keith Busch32f0c4a2016-07-13 11:45:02 -06002806 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07002807 /*
2808 * Revalidating a dead namespace sets capacity to 0. This will
2809 * end buffered writers dirtying pages that can't be synced.
2810 */
Keith Buschf33447b2017-02-10 18:15:51 -05002811 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
2812 continue;
2813 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07002814 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08002815
Ming Lei443bd902017-06-19 10:21:08 +08002816 /* Forcibly unquiesce queues to avoid blocking dispatch */
2817 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07002818 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002819 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07002820}
Linus Torvalds237045f2016-03-18 17:13:31 -07002821EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07002822
Keith Busch302ad8c2017-03-01 14:22:12 -05002823void nvme_unfreeze(struct nvme_ctrl *ctrl)
2824{
2825 struct nvme_ns *ns;
2826
2827 mutex_lock(&ctrl->namespaces_mutex);
2828 list_for_each_entry(ns, &ctrl->namespaces, list)
2829 blk_mq_unfreeze_queue(ns->queue);
2830 mutex_unlock(&ctrl->namespaces_mutex);
2831}
2832EXPORT_SYMBOL_GPL(nvme_unfreeze);
2833
2834void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
2835{
2836 struct nvme_ns *ns;
2837
2838 mutex_lock(&ctrl->namespaces_mutex);
2839 list_for_each_entry(ns, &ctrl->namespaces, list) {
2840 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
2841 if (timeout <= 0)
2842 break;
2843 }
2844 mutex_unlock(&ctrl->namespaces_mutex);
2845}
2846EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
2847
2848void nvme_wait_freeze(struct nvme_ctrl *ctrl)
2849{
2850 struct nvme_ns *ns;
2851
2852 mutex_lock(&ctrl->namespaces_mutex);
2853 list_for_each_entry(ns, &ctrl->namespaces, list)
2854 blk_mq_freeze_queue_wait(ns->queue);
2855 mutex_unlock(&ctrl->namespaces_mutex);
2856}
2857EXPORT_SYMBOL_GPL(nvme_wait_freeze);
2858
2859void nvme_start_freeze(struct nvme_ctrl *ctrl)
2860{
2861 struct nvme_ns *ns;
2862
2863 mutex_lock(&ctrl->namespaces_mutex);
2864 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08002865 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05002866 mutex_unlock(&ctrl->namespaces_mutex);
2867}
2868EXPORT_SYMBOL_GPL(nvme_start_freeze);
2869
Keith Busch25646262016-01-04 09:10:57 -07002870void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002871{
2872 struct nvme_ns *ns;
2873
Keith Busch32f0c4a2016-07-13 11:45:02 -06002874 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07002875 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07002876 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002877 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002878}
Ming Lin576d55d2016-02-10 10:03:32 -08002879EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002880
Keith Busch25646262016-01-04 09:10:57 -07002881void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002882{
2883 struct nvme_ns *ns;
2884
Keith Busch32f0c4a2016-07-13 11:45:02 -06002885 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03002886 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08002887 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002888 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002889}
Ming Lin576d55d2016-02-10 10:03:32 -08002890EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01002891
Sagi Grimberg31b84462017-10-11 12:53:07 +03002892int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
2893{
2894 if (!ctrl->ops->reinit_request)
2895 return 0;
2896
2897 return blk_mq_tagset_iter(set, set->driver_data,
2898 ctrl->ops->reinit_request);
2899}
2900EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
2901
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002902int __init nvme_core_init(void)
2903{
2904 int result;
2905
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002906 nvme_wq = alloc_workqueue("nvme-wq",
2907 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
2908 if (!nvme_wq)
2909 return -ENOMEM;
2910
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002911 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002912 if (result < 0)
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002913 goto destroy_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002914
2915 nvme_class = class_create(THIS_MODULE, "nvme");
2916 if (IS_ERR(nvme_class)) {
2917 result = PTR_ERR(nvme_class);
2918 goto unregister_chrdev;
2919 }
2920
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002921 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002922
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002923unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002924 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002925destroy_wq:
2926 destroy_workqueue(nvme_wq);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002927 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002928}
2929
2930void nvme_core_exit(void)
2931{
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002932 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002933 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02002934 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002935}
Ming Lin576d55d2016-02-10 10:03:32 -08002936
2937MODULE_LICENSE("GPL");
2938MODULE_VERSION("1.0");
2939module_init(nvme_core_init);
2940module_exit(nvme_core_exit);