blob: 5c729ab519114747e0a48aea036c02b3d9f60a6c [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
Johannes Thumshirn3d030e42018-01-26 11:21:37 +010032#define CREATE_TRACE_POINTS
33#include "trace.h"
34
Christoph Hellwig21d34712015-11-26 09:08:36 +010035#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020036#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010037
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010038#define NVME_MINORS (1U << MINORBITS)
39
Marc Olson8ae4e442017-09-06 17:23:56 -070040unsigned int admin_timeout = 60;
41module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080042MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080043EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080044
Marc Olson8ae4e442017-09-06 17:23:56 -070045unsigned int nvme_io_timeout = 30;
46module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080047MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080048EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080049
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020050static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080051module_param(shutdown_timeout, byte, 0644);
52MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
53
Christoph Hellwig44e44b22017-04-05 19:18:11 +020054static u8 nvme_max_retries = 5;
55module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070056MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010057
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080058static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059module_param(default_ps_max_latency_us, ulong, 0644);
60MODULE_PARM_DESC(default_ps_max_latency_us,
61 "max power saving latency for new devices; use PM QOS to change per device");
62
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070063static bool force_apst;
64module_param(force_apst, bool, 0644);
65MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
66
Jens Axboef5d11842017-06-27 12:03:06 -060067static bool streams;
68module_param(streams, bool, 0644);
69MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
70
Roy Shtermanb227c592018-01-14 12:39:02 +020071/*
72 * nvme_wq - hosts nvme related works that are not reset or delete
73 * nvme_reset_wq - hosts nvme reset works
74 * nvme_delete_wq - hosts nvme delete works
75 *
76 * nvme_wq will host works such are scan, aen handling, fw activation,
77 * keep-alive error recovery, periodic reconnects etc. nvme_reset_wq
78 * runs reset works which also flush works hosted on nvme_wq for
79 * serialization purposes. nvme_delete_wq host controller deletion
80 * works which flush reset works for serialization.
81 */
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020082struct workqueue_struct *nvme_wq;
83EXPORT_SYMBOL_GPL(nvme_wq);
84
Roy Shtermanb227c592018-01-14 12:39:02 +020085struct workqueue_struct *nvme_reset_wq;
86EXPORT_SYMBOL_GPL(nvme_reset_wq);
87
88struct workqueue_struct *nvme_delete_wq;
89EXPORT_SYMBOL_GPL(nvme_delete_wq);
90
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010091static DEFINE_IDA(nvme_subsystems_ida);
92static LIST_HEAD(nvme_subsystems);
93static DEFINE_MUTEX(nvme_subsystems_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010094
Christoph Hellwig9843f682017-10-18 13:10:01 +020095static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020096static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010097static struct class *nvme_class;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010098static struct class *nvme_subsys_class;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010099
Keith Busch84fef622017-11-07 10:28:32 -0700100static void nvme_ns_remove(struct nvme_ns *ns);
101static int nvme_revalidate_disk(struct gendisk *disk);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100102
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200103int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
104{
105 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
106 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200107 if (!queue_work(nvme_reset_wq, &ctrl->reset_work))
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200108 return -EBUSY;
109 return 0;
110}
111EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
112
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200113int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200114{
115 int ret;
116
117 ret = nvme_reset_ctrl(ctrl);
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000118 if (!ret) {
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200119 flush_work(&ctrl->reset_work);
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000120 if (ctrl->state != NVME_CTRL_LIVE)
121 ret = -ENETRESET;
122 }
123
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200124 return ret;
125}
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200126EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200127
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200128static void nvme_delete_ctrl_work(struct work_struct *work)
129{
130 struct nvme_ctrl *ctrl =
131 container_of(work, struct nvme_ctrl, delete_work);
132
Sagi Grimberg40546372017-10-29 14:21:02 +0200133 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200134 nvme_stop_ctrl(ctrl);
135 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200136 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200137 nvme_uninit_ctrl(ctrl);
138 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200139}
140
141int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
142{
143 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
144 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200145 if (!queue_work(nvme_delete_wq, &ctrl->delete_work))
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200146 return -EBUSY;
147 return 0;
148}
149EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
150
151int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
152{
153 int ret = 0;
154
155 /*
156 * Keep a reference until the work is flushed since ->delete_ctrl
157 * can free the controller.
158 */
159 nvme_get_ctrl(ctrl);
160 ret = nvme_delete_ctrl(ctrl);
161 if (!ret)
162 flush_work(&ctrl->delete_work);
163 nvme_put_ctrl(ctrl);
164 return ret;
165}
166EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
167
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100168static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
169{
170 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
171}
172
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200173static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200174{
175 switch (nvme_req(req)->status & 0x7ff) {
176 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200177 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200178 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200179 return BLK_STS_NOSPC;
Keith Busche96fef22018-01-09 12:04:14 -0700180 case NVME_SC_LBA_RANGE:
181 return BLK_STS_TARGET;
182 case NVME_SC_BAD_ATTRIBUTES:
Junxiong Guane02ab022017-04-21 12:59:07 +0200183 case NVME_SC_ONCS_NOT_SUPPORTED:
Keith Busche96fef22018-01-09 12:04:14 -0700184 case NVME_SC_INVALID_OPCODE:
185 case NVME_SC_INVALID_FIELD:
186 case NVME_SC_INVALID_NS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200187 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200188 case NVME_SC_WRITE_FAULT:
189 case NVME_SC_READ_ERROR:
190 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200191 case NVME_SC_ACCESS_DENIED:
192 case NVME_SC_READ_ONLY:
Keith Busche96fef22018-01-09 12:04:14 -0700193 case NVME_SC_COMPARE_FAILED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200194 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200195 case NVME_SC_GUARD_CHECK:
196 case NVME_SC_APPTAG_CHECK:
197 case NVME_SC_REFTAG_CHECK:
198 case NVME_SC_INVALID_PI:
199 return BLK_STS_PROTECTION;
200 case NVME_SC_RESERVATION_CONFLICT:
201 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200202 default:
203 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200204 }
205}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200206
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200207static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200208{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200209 if (blk_noretry_request(req))
210 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200211 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200212 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200213 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200214 return false;
215 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200216}
217
218void nvme_complete_rq(struct request *req)
219{
Keith Busch908e4562018-01-09 12:04:15 -0700220 blk_status_t status = nvme_error_status(req);
221
Johannes Thumshirnca5554a2018-01-26 11:21:38 +0100222 trace_nvme_complete_rq(req);
223
Keith Busch908e4562018-01-09 12:04:15 -0700224 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
225 if (nvme_req_needs_failover(req, status)) {
Christoph Hellwig32acab32017-11-02 12:59:30 +0100226 nvme_failover_req(req);
227 return;
228 }
229
230 if (!blk_queue_dying(req->q)) {
231 nvme_req(req)->retries++;
232 blk_mq_requeue_request(req, true);
233 return;
234 }
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200235 }
Keith Busch908e4562018-01-09 12:04:15 -0700236 blk_mq_end_request(req, status);
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200237}
238EXPORT_SYMBOL_GPL(nvme_complete_rq);
239
Ming Linc55a2fd2016-05-18 14:05:02 -0700240void nvme_cancel_request(struct request *req, void *data, bool reserved)
241{
Ming Linc55a2fd2016-05-18 14:05:02 -0700242 if (!blk_mq_request_started(req))
243 return;
244
245 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
246 "Cancelling I/O %d", req->tag);
247
Christoph Hellwige54b0642017-11-02 21:28:51 +0300248 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200249 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200250
Ming Linc55a2fd2016-05-18 14:05:02 -0700251}
252EXPORT_SYMBOL_GPL(nvme_cancel_request);
253
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200254bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
255 enum nvme_ctrl_state new_state)
256{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300257 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200258 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200259 bool changed = false;
260
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200261 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300262
263 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200264 switch (new_state) {
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800265 case NVME_CTRL_ADMIN_ONLY:
266 switch (old_state) {
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200267 case NVME_CTRL_CONNECTING:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800268 changed = true;
269 /* FALLTHRU */
270 default:
271 break;
272 }
273 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200274 case NVME_CTRL_LIVE:
275 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200276 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200277 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200278 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200279 changed = true;
280 /* FALLTHRU */
281 default:
282 break;
283 }
284 break;
285 case NVME_CTRL_RESETTING:
286 switch (old_state) {
287 case NVME_CTRL_NEW:
288 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800289 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900290 changed = true;
291 /* FALLTHRU */
292 default:
293 break;
294 }
295 break;
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200296 case NVME_CTRL_CONNECTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900297 switch (old_state) {
Max Gurtovoyb754a322018-01-31 18:31:25 +0200298 case NVME_CTRL_NEW:
James Smart3cec7f92017-10-25 16:43:13 -0700299 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200300 changed = true;
301 /* FALLTHRU */
302 default:
303 break;
304 }
305 break;
306 case NVME_CTRL_DELETING:
307 switch (old_state) {
308 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800309 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200310 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200311 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200312 changed = true;
313 /* FALLTHRU */
314 default:
315 break;
316 }
317 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600318 case NVME_CTRL_DEAD:
319 switch (old_state) {
320 case NVME_CTRL_DELETING:
321 changed = true;
322 /* FALLTHRU */
323 default:
324 break;
325 }
326 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200327 default:
328 break;
329 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200330
331 if (changed)
332 ctrl->state = new_state;
333
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200334 spin_unlock_irqrestore(&ctrl->lock, flags);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100335 if (changed && ctrl->state == NVME_CTRL_LIVE)
336 nvme_kick_requeue_lists(ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200337 return changed;
338}
339EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
340
Christoph Hellwiged754e52017-11-09 13:50:43 +0100341static void nvme_free_ns_head(struct kref *ref)
342{
343 struct nvme_ns_head *head =
344 container_of(ref, struct nvme_ns_head, ref);
345
Christoph Hellwig32acab32017-11-02 12:59:30 +0100346 nvme_mpath_remove_disk(head);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100347 ida_simple_remove(&head->subsys->ns_ida, head->instance);
348 list_del_init(&head->entry);
349 cleanup_srcu_struct(&head->srcu);
350 kfree(head);
351}
352
353static void nvme_put_ns_head(struct nvme_ns_head *head)
354{
355 kref_put(&head->ref, nvme_free_ns_head);
356}
357
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100358static void nvme_free_ns(struct kref *kref)
359{
360 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
361
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200362 if (ns->ndev)
363 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100364
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100365 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100366 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700367 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100368 kfree(ns);
369}
370
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100371static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100372{
373 kref_put(&ns->kref, nvme_free_ns);
374}
375
Christoph Hellwig41609822015-11-20 09:00:02 +0100376struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800377 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100378{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100379 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100380 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100381
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200382 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100383 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200384 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100385 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200386 qid ? qid - 1 : 0);
387 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100388 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100389 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100390
Christoph Hellwig21d34712015-11-26 09:08:36 +0100391 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800392 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100393
Christoph Hellwig41609822015-11-20 09:00:02 +0100394 return req;
395}
Ming Lin576d55d2016-02-10 10:03:32 -0800396EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100397
Jens Axboef5d11842017-06-27 12:03:06 -0600398static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
399{
400 struct nvme_command c;
401
402 memset(&c, 0, sizeof(c));
403
404 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530405 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600406 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
407 c.directive.dtype = NVME_DIR_IDENTIFY;
408 c.directive.tdtype = NVME_DIR_STREAMS;
409 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
410
411 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
412}
413
414static int nvme_disable_streams(struct nvme_ctrl *ctrl)
415{
416 return nvme_toggle_streams(ctrl, false);
417}
418
419static int nvme_enable_streams(struct nvme_ctrl *ctrl)
420{
421 return nvme_toggle_streams(ctrl, true);
422}
423
424static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
425 struct streams_directive_params *s, u32 nsid)
426{
427 struct nvme_command c;
428
429 memset(&c, 0, sizeof(c));
430 memset(s, 0, sizeof(*s));
431
432 c.directive.opcode = nvme_admin_directive_recv;
433 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700434 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600435 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
436 c.directive.dtype = NVME_DIR_STREAMS;
437
438 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
439}
440
441static int nvme_configure_directives(struct nvme_ctrl *ctrl)
442{
443 struct streams_directive_params s;
444 int ret;
445
446 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
447 return 0;
448 if (!streams)
449 return 0;
450
451 ret = nvme_enable_streams(ctrl);
452 if (ret)
453 return ret;
454
Arnav Dawn62346ea2017-07-12 16:11:53 +0530455 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600456 if (ret)
457 return ret;
458
459 ctrl->nssa = le16_to_cpu(s.nssa);
460 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
461 dev_info(ctrl->device, "too few streams (%u) available\n",
462 ctrl->nssa);
463 nvme_disable_streams(ctrl);
464 return 0;
465 }
466
467 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
468 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
469 return 0;
470}
471
472/*
473 * Check if 'req' has a write hint associated with it. If it does, assign
474 * a valid namespace stream to the write.
475 */
476static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
477 struct request *req, u16 *control,
478 u32 *dsmgmt)
479{
480 enum rw_hint streamid = req->write_hint;
481
482 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
483 streamid = 0;
484 else {
485 streamid--;
486 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
487 return;
488
489 *control |= NVME_RW_DTYPE_STREAMS;
490 *dsmgmt |= streamid << 16;
491 }
492
493 if (streamid < ARRAY_SIZE(req->q->write_hints))
494 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
495}
496
Ming Lin8093f7c2016-04-12 13:10:14 -0600497static inline void nvme_setup_flush(struct nvme_ns *ns,
498 struct nvme_command *cmnd)
499{
500 memset(cmnd, 0, sizeof(*cmnd));
501 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100502 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600503}
504
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200505static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600506 struct nvme_command *cmnd)
507{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100508 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600509 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100510 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600511
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100512 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600513 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200514 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600515
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100516 __rq_for_each_bio(bio, req) {
517 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
518 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
519
Keith Busch8cb6af72018-01-31 17:01:58 -0700520 if (n < segments) {
521 range[n].cattr = cpu_to_le32(0);
522 range[n].nlb = cpu_to_le32(nlb);
523 range[n].slba = cpu_to_le64(slba);
524 }
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100525 n++;
526 }
527
528 if (WARN_ON_ONCE(n != segments)) {
529 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200530 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100531 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600532
533 memset(cmnd, 0, sizeof(*cmnd));
534 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100535 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200536 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600537 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
538
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700539 req->special_vec.bv_page = virt_to_page(range);
540 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100541 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700542 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600543
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200544 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600545}
Ming Lin8093f7c2016-04-12 13:10:14 -0600546
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200547static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
548 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600549{
Jens Axboef5d11842017-06-27 12:03:06 -0600550 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600551 u16 control = 0;
552 u32 dsmgmt = 0;
553
554 if (req->cmd_flags & REQ_FUA)
555 control |= NVME_RW_FUA;
556 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
557 control |= NVME_RW_LR;
558
559 if (req->cmd_flags & REQ_RAHEAD)
560 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
561
562 memset(cmnd, 0, sizeof(*cmnd));
563 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100564 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600565 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
566 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
567
Jens Axboef5d11842017-06-27 12:03:06 -0600568 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
569 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
570
Ming Lin8093f7c2016-04-12 13:10:14 -0600571 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100572 /*
573 * If formated with metadata, the block layer always provides a
574 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
575 * we enable the PRACT bit for protection information or set the
576 * namespace capacity to zero to prevent any I/O.
577 */
578 if (!blk_integrity_rq(req)) {
579 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
580 return BLK_STS_NOTSUPP;
581 control |= NVME_RW_PRINFO_PRACT;
582 }
583
Ming Lin8093f7c2016-04-12 13:10:14 -0600584 switch (ns->pi_type) {
585 case NVME_NS_DPS_PI_TYPE3:
586 control |= NVME_RW_PRINFO_PRCHK_GUARD;
587 break;
588 case NVME_NS_DPS_PI_TYPE1:
589 case NVME_NS_DPS_PI_TYPE2:
590 control |= NVME_RW_PRINFO_PRCHK_GUARD |
591 NVME_RW_PRINFO_PRCHK_REF;
592 cmnd->rw.reftag = cpu_to_le32(
593 nvme_block_nr(ns, blk_rq_pos(req)));
594 break;
595 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600596 }
597
598 cmnd->rw.control = cpu_to_le16(control);
599 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200600 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600601}
602
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200603blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600604 struct nvme_command *cmd)
605{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200606 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600607
Christoph Hellwig987f6992017-04-05 19:18:08 +0200608 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200609 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200610 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200611 req->rq_flags |= RQF_DONTPREP;
612 }
613
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100614 switch (req_op(req)) {
615 case REQ_OP_DRV_IN:
616 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800617 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100618 break;
619 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600620 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100621 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200622 case REQ_OP_WRITE_ZEROES:
623 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100624 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600625 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100626 break;
627 case REQ_OP_READ:
628 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200629 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100630 break;
631 default:
632 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200633 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100634 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600635
James Smart721b3912016-10-21 23:33:34 +0300636 cmd->common.command_id = req->tag;
Johannes Thumshirn3d030e42018-01-26 11:21:37 +0100637 if (ns)
638 trace_nvme_setup_nvm_cmd(req->q->id, cmd);
639 else
640 trace_nvme_setup_admin_cmd(cmd);
Ming Lin8093f7c2016-04-12 13:10:14 -0600641 return ret;
642}
643EXPORT_SYMBOL_GPL(nvme_setup_cmd);
644
Christoph Hellwig41609822015-11-20 09:00:02 +0100645/*
646 * Returns 0 on success. If the result is negative, it's a Linux error code;
647 * if the result is positive, it's an NVM Express status code
648 */
649int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800650 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800651 unsigned timeout, int qid, int at_head,
652 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100653{
654 struct request *req;
655 int ret;
656
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200657 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100658 if (IS_ERR(req))
659 return PTR_ERR(req);
660
661 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
662
Christoph Hellwig21d34712015-11-26 09:08:36 +0100663 if (buffer && bufflen) {
664 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
665 if (ret)
666 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100667 }
668
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200669 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800670 if (result)
671 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200672 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
673 ret = -EINTR;
674 else
675 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100676 out:
677 blk_mq_free_request(req);
678 return ret;
679}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200680EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100681
682int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
683 void *buffer, unsigned bufflen)
684{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200685 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
686 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100687}
Ming Lin576d55d2016-02-10 10:03:32 -0800688EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100689
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400690static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
691 unsigned len, u32 seed, bool write)
692{
693 struct bio_integrity_payload *bip;
694 int ret = -ENOMEM;
695 void *buf;
696
697 buf = kmalloc(len, GFP_KERNEL);
698 if (!buf)
699 goto out;
700
701 ret = -EFAULT;
702 if (write && copy_from_user(buf, ubuf, len))
703 goto out_free_meta;
704
705 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
706 if (IS_ERR(bip)) {
707 ret = PTR_ERR(bip);
708 goto out_free_meta;
709 }
710
711 bip->bip_iter.bi_size = len;
712 bip->bip_iter.bi_sector = seed;
713 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
714 offset_in_page(buf));
715 if (ret == len)
716 return buf;
717 ret = -ENOMEM;
718out_free_meta:
719 kfree(buf);
720out:
721 return ERR_PTR(ret);
722}
723
Keith Busch63263d62017-08-29 17:46:04 -0400724static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400725 struct nvme_command *cmd, void __user *ubuffer,
726 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
727 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100728{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200729 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600730 struct nvme_ns *ns = q->queuedata;
731 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100732 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600733 struct bio *bio = NULL;
734 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100735 int ret;
736
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200737 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100738 if (IS_ERR(req))
739 return PTR_ERR(req);
740
741 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
742
743 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100744 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
745 GFP_KERNEL);
746 if (ret)
747 goto out;
748 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200749 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400750 if (disk && meta_buffer && meta_len) {
751 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
752 meta_seed, write);
753 if (IS_ERR(meta)) {
754 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600755 goto out_unmap;
756 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600757 }
758 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400759
Keith Busch0b7f1f22015-10-23 09:47:28 -0600760 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200761 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
762 ret = -EINTR;
763 else
764 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100765 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800766 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600767 if (meta && !ret && !write) {
768 if (copy_to_user(meta_buffer, meta, meta_len))
769 ret = -EFAULT;
770 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600771 kfree(meta);
772 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200773 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600774 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100775 out:
776 blk_mq_free_request(req);
777 return ret;
778}
779
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200780static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200781{
782 struct nvme_ctrl *ctrl = rq->end_io_data;
783
784 blk_mq_free_request(rq);
785
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200786 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200787 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200788 "failed nvme_keep_alive_end_io error=%d\n",
789 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200790 return;
791 }
792
793 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
794}
795
796static int nvme_keep_alive(struct nvme_ctrl *ctrl)
797{
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200798 struct request *rq;
799
Roland Dreier0a34e462018-01-11 13:38:15 -0800800 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200801 NVME_QID_ANY);
802 if (IS_ERR(rq))
803 return PTR_ERR(rq);
804
805 rq->timeout = ctrl->kato * HZ;
806 rq->end_io_data = ctrl;
807
808 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
809
810 return 0;
811}
812
813static void nvme_keep_alive_work(struct work_struct *work)
814{
815 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
816 struct nvme_ctrl, ka_work);
817
818 if (nvme_keep_alive(ctrl)) {
819 /* allocation failure, reset the controller */
820 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200821 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200822 return;
823 }
824}
825
826void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
827{
828 if (unlikely(ctrl->kato == 0))
829 return;
830
831 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
Roland Dreier0a34e462018-01-11 13:38:15 -0800832 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd));
833 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200834 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
835}
836EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
837
838void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
839{
840 if (unlikely(ctrl->kato == 0))
841 return;
842
843 cancel_delayed_work_sync(&ctrl->ka_work);
844}
845EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
846
Keith Busch3f7f25a92017-06-20 15:09:56 -0400847static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100848{
849 struct nvme_command c = { };
850 int error;
851
852 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
853 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200854 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100855
856 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
857 if (!*id)
858 return -ENOMEM;
859
860 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
861 sizeof(struct nvme_id_ctrl));
862 if (error)
863 kfree(*id);
864 return error;
865}
866
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200867static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100868 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200869{
870 struct nvme_command c = { };
871 int status;
872 void *data;
873 int pos;
874 int len;
875
876 c.identify.opcode = nvme_admin_identify;
877 c.identify.nsid = cpu_to_le32(nsid);
878 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
879
880 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
881 if (!data)
882 return -ENOMEM;
883
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200884 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200885 NVME_IDENTIFY_DATA_SIZE);
886 if (status)
887 goto free_data;
888
889 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
890 struct nvme_ns_id_desc *cur = data + pos;
891
892 if (cur->nidl == 0)
893 break;
894
895 switch (cur->nidt) {
896 case NVME_NIDT_EUI64:
897 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200898 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200899 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
900 cur->nidl);
901 goto free_data;
902 }
903 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100904 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200905 break;
906 case NVME_NIDT_NGUID:
907 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200908 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200909 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
910 cur->nidl);
911 goto free_data;
912 }
913 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100914 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200915 break;
916 case NVME_NIDT_UUID:
917 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200918 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200919 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
920 cur->nidl);
921 goto free_data;
922 }
923 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100924 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200925 break;
926 default:
927 /* Skip unnkown types */
928 len = cur->nidl;
929 break;
930 }
931
932 len += sizeof(*cur);
933 }
934free_data:
935 kfree(data);
936 return status;
937}
938
Keith Busch540c8012015-10-22 15:45:06 -0600939static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
940{
941 struct nvme_command c = { };
942
943 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200944 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600945 c.identify.nsid = cpu_to_le32(nsid);
Minwoo Im42595eb2018-02-08 22:56:31 +0900946 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list,
947 NVME_IDENTIFY_DATA_SIZE);
Keith Busch540c8012015-10-22 15:45:06 -0600948}
949
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200950static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
951 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100952{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200953 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100954 struct nvme_command c = { };
955 int error;
956
957 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200958 c.identify.opcode = nvme_admin_identify;
959 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200960 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100961
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200962 id = kmalloc(sizeof(*id), GFP_KERNEL);
963 if (!id)
964 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100965
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200966 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
967 if (error) {
968 dev_warn(ctrl->device, "Identify namespace failed\n");
969 kfree(id);
970 return NULL;
971 }
972
973 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100974}
975
Keith Busch3f7f25a92017-06-20 15:09:56 -0400976static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700977 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100978{
979 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800980 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100981 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100982
983 memset(&c, 0, sizeof(c));
984 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100985 c.features.fid = cpu_to_le32(fid);
986 c.features.dword11 = cpu_to_le32(dword11);
987
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800988 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700989 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700990 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800991 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100992 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100993}
994
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +0100995int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
996{
997 u32 q_count = (*count - 1) | ((*count - 1) << 16);
998 u32 result;
999 int status, nr_io_queues;
1000
Andy Lutomirski1a6fe742016-09-16 11:16:10 -07001001 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001002 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001003 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001004 return status;
1005
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001006 /*
1007 * Degraded controllers might return an error when setting the queue
1008 * count. We still want to be able to bring them online and offer
1009 * access to the admin queue, as that might be only way to fix them up.
1010 */
1011 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001012 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001013 *count = 0;
1014 } else {
1015 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
1016 *count = min(*count, nr_io_queues);
1017 }
1018
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001019 return 0;
1020}
Ming Lin576d55d2016-02-10 10:03:32 -08001021EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001022
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001023static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1024{
1025 struct nvme_user_io io;
1026 struct nvme_command c;
1027 unsigned length, meta_len;
1028 void __user *metadata;
1029
1030 if (copy_from_user(&io, uio, sizeof(io)))
1031 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001032 if (io.flags)
1033 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001034
1035 switch (io.opcode) {
1036 case nvme_cmd_write:
1037 case nvme_cmd_read:
1038 case nvme_cmd_compare:
1039 break;
1040 default:
1041 return -EINVAL;
1042 }
1043
1044 length = (io.nblocks + 1) << ns->lba_shift;
1045 meta_len = (io.nblocks + 1) * ns->ms;
1046 metadata = (void __user *)(uintptr_t)io.metadata;
1047
1048 if (ns->ext) {
1049 length += meta_len;
1050 meta_len = 0;
1051 } else if (meta_len) {
1052 if ((io.metadata & 3) || !io.metadata)
1053 return -EINVAL;
1054 }
1055
1056 memset(&c, 0, sizeof(c));
1057 c.rw.opcode = io.opcode;
1058 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001059 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001060 c.rw.slba = cpu_to_le64(io.slba);
1061 c.rw.length = cpu_to_le16(io.nblocks);
1062 c.rw.control = cpu_to_le16(io.control);
1063 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1064 c.rw.reftag = cpu_to_le32(io.reftag);
1065 c.rw.apptag = cpu_to_le16(io.apptag);
1066 c.rw.appmask = cpu_to_le16(io.appmask);
1067
Keith Busch63263d62017-08-29 17:46:04 -04001068 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001069 (void __user *)(uintptr_t)io.addr, length,
1070 metadata, meta_len, io.slba, NULL, 0);
1071}
1072
Keith Busch84fef622017-11-07 10:28:32 -07001073static u32 nvme_known_admin_effects(u8 opcode)
1074{
1075 switch (opcode) {
1076 case nvme_admin_format_nvm:
1077 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1078 NVME_CMD_EFFECTS_CSE_MASK;
1079 case nvme_admin_sanitize_nvm:
1080 return NVME_CMD_EFFECTS_CSE_MASK;
1081 default:
1082 break;
1083 }
1084 return 0;
1085}
1086
1087static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1088 u8 opcode)
1089{
1090 u32 effects = 0;
1091
1092 if (ns) {
1093 if (ctrl->effects)
1094 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1095 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1096 dev_warn(ctrl->device,
1097 "IO command:%02x has unhandled effects:%08x\n",
1098 opcode, effects);
1099 return 0;
1100 }
1101
1102 if (ctrl->effects)
1103 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1104 else
1105 effects = nvme_known_admin_effects(opcode);
1106
1107 /*
1108 * For simplicity, IO to all namespaces is quiesced even if the command
1109 * effects say only one namespace is affected.
1110 */
1111 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1112 nvme_start_freeze(ctrl);
1113 nvme_wait_freeze(ctrl);
1114 }
1115 return effects;
1116}
1117
1118static void nvme_update_formats(struct nvme_ctrl *ctrl)
1119{
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001120 struct nvme_ns *ns, *next;
1121 LIST_HEAD(rm_list);
Keith Busch84fef622017-11-07 10:28:32 -07001122
Jianchao Wang765cc032018-02-12 20:54:46 +08001123 down_write(&ctrl->namespaces_rwsem);
Keith Busch84fef622017-11-07 10:28:32 -07001124 list_for_each_entry(ns, &ctrl->namespaces, list) {
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001125 if (ns->disk && nvme_revalidate_disk(ns->disk)) {
1126 list_move_tail(&ns->list, &rm_list);
1127 }
Keith Busch84fef622017-11-07 10:28:32 -07001128 }
Jianchao Wang765cc032018-02-12 20:54:46 +08001129 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001130
1131 list_for_each_entry_safe(ns, next, &rm_list, list)
1132 nvme_ns_remove(ns);
Keith Busch84fef622017-11-07 10:28:32 -07001133}
1134
1135static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1136{
1137 /*
1138 * Revalidate LBA changes prior to unfreezing. This is necessary to
1139 * prevent memory corruption if a logical block size was changed by
1140 * this command.
1141 */
1142 if (effects & NVME_CMD_EFFECTS_LBCC)
1143 nvme_update_formats(ctrl);
1144 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1145 nvme_unfreeze(ctrl);
1146 if (effects & NVME_CMD_EFFECTS_CCC)
1147 nvme_init_identify(ctrl);
1148 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1149 nvme_queue_scan(ctrl);
1150}
1151
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001152static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001153 struct nvme_passthru_cmd __user *ucmd)
1154{
1155 struct nvme_passthru_cmd cmd;
1156 struct nvme_command c;
1157 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001158 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001159 int status;
1160
1161 if (!capable(CAP_SYS_ADMIN))
1162 return -EACCES;
1163 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1164 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001165 if (cmd.flags)
1166 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001167
1168 memset(&c, 0, sizeof(c));
1169 c.common.opcode = cmd.opcode;
1170 c.common.flags = cmd.flags;
1171 c.common.nsid = cpu_to_le32(cmd.nsid);
1172 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1173 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1174 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1175 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1176 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1177 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1178 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1179 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1180
1181 if (cmd.timeout_ms)
1182 timeout = msecs_to_jiffies(cmd.timeout_ms);
1183
Keith Busch84fef622017-11-07 10:28:32 -07001184 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001185 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001186 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001187 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1188 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001189 nvme_passthru_end(ctrl, effects);
1190
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001191 if (status >= 0) {
1192 if (put_user(cmd.result, &ucmd->result))
1193 return -EFAULT;
1194 }
1195
1196 return status;
1197}
1198
Christoph Hellwig32acab32017-11-02 12:59:30 +01001199/*
1200 * Issue ioctl requests on the first available path. Note that unlike normal
1201 * block layer requests we will not retry failed request on another controller.
1202 */
1203static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1204 struct nvme_ns_head **head, int *srcu_idx)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001205{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001206#ifdef CONFIG_NVME_MULTIPATH
1207 if (disk->fops == &nvme_ns_head_ops) {
1208 *head = disk->private_data;
1209 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1210 return nvme_find_path(*head);
1211 }
1212#endif
1213 *head = NULL;
1214 *srcu_idx = -1;
1215 return disk->private_data;
1216}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001217
Christoph Hellwig32acab32017-11-02 12:59:30 +01001218static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1219{
1220 if (head)
1221 srcu_read_unlock(&head->srcu, idx);
1222}
1223
1224static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
1225{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001226 switch (cmd) {
1227 case NVME_IOCTL_ID:
1228 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001229 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001230 case NVME_IOCTL_ADMIN_CMD:
1231 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1232 case NVME_IOCTL_IO_CMD:
1233 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1234 case NVME_IOCTL_SUBMIT_IO:
1235 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001236 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001237#ifdef CONFIG_NVM
1238 if (ns->ndev)
1239 return nvme_nvm_ioctl(ns, cmd, arg);
1240#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001241 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001242 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001243 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001244 return -ENOTTY;
1245 }
1246}
1247
Christoph Hellwig32acab32017-11-02 12:59:30 +01001248static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1249 unsigned int cmd, unsigned long arg)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001250{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001251 struct nvme_ns_head *head = NULL;
1252 struct nvme_ns *ns;
1253 int srcu_idx, ret;
1254
1255 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1256 if (unlikely(!ns))
1257 ret = -EWOULDBLOCK;
1258 else
1259 ret = nvme_ns_ioctl(ns, cmd, arg);
1260 nvme_put_ns_from_disk(head, srcu_idx);
1261 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001262}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001263
1264static int nvme_open(struct block_device *bdev, fmode_t mode)
1265{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001266 struct nvme_ns *ns = bdev->bd_disk->private_data;
1267
Christoph Hellwig32acab32017-11-02 12:59:30 +01001268#ifdef CONFIG_NVME_MULTIPATH
1269 /* should never be called due to GENHD_FL_HIDDEN */
1270 if (WARN_ON_ONCE(ns->head->disk))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001271 goto fail;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001272#endif
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001273 if (!kref_get_unless_zero(&ns->kref))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001274 goto fail;
1275 if (!try_module_get(ns->ctrl->ops->module))
1276 goto fail_put_ns;
1277
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001278 return 0;
Nitzan Carmi85088c42018-01-04 17:56:13 +02001279
1280fail_put_ns:
1281 nvme_put_ns(ns);
1282fail:
1283 return -ENXIO;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001284}
1285
1286static void nvme_release(struct gendisk *disk, fmode_t mode)
1287{
Nitzan Carmi85088c42018-01-04 17:56:13 +02001288 struct nvme_ns *ns = disk->private_data;
1289
1290 module_put(ns->ctrl->ops->module);
1291 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001292}
1293
1294static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1295{
1296 /* some standard values */
1297 geo->heads = 1 << 6;
1298 geo->sectors = 1 << 5;
1299 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1300 return 0;
1301}
1302
1303#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001304static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001305{
1306 struct blk_integrity integrity;
1307
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001308 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001309 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001310 case NVME_NS_DPS_PI_TYPE3:
1311 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001312 integrity.tag_size = sizeof(u16) + sizeof(u32);
1313 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001314 break;
1315 case NVME_NS_DPS_PI_TYPE1:
1316 case NVME_NS_DPS_PI_TYPE2:
1317 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001318 integrity.tag_size = sizeof(u16);
1319 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001320 break;
1321 default:
1322 integrity.profile = NULL;
1323 break;
1324 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001325 integrity.tuple_size = ms;
1326 blk_integrity_register(disk, &integrity);
1327 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001328}
1329#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001330static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001331{
1332}
1333#endif /* CONFIG_BLK_DEV_INTEGRITY */
1334
Scott Bauer6b8190d2017-06-15 10:44:30 -06001335static void nvme_set_chunk_size(struct nvme_ns *ns)
1336{
1337 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1338 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1339}
1340
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001341static void nvme_config_discard(struct nvme_ctrl *ctrl,
1342 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001343{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001344 u32 size = queue_logical_block_size(queue);
1345
1346 if (stream_alignment)
1347 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001348
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001349 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1350 NVME_DSM_MAX_RANGES);
1351
David Disseldorpb224f612017-11-24 16:30:53 +01001352 queue->limits.discard_alignment = 0;
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001353 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001354
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001355 blk_queue_max_discard_sectors(queue, UINT_MAX);
1356 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001357 blk_queue_flag_set(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001358
1359 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001360 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001361}
1362
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001363static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001364 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001365{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001366 memset(ids, 0, sizeof(*ids));
1367
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001368 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001369 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001370 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001371 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001372 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001373 /* Don't treat error as fatal we potentially
1374 * already have a NGUID or EUI-64
1375 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001376 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001377 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001378 "%s: Identify Descriptors failed\n", __func__);
1379 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001380}
1381
Christoph Hellwiged754e52017-11-09 13:50:43 +01001382static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1383{
1384 return !uuid_is_null(&ids->uuid) ||
1385 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1386 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1387}
1388
Christoph Hellwig002fab02017-11-09 13:50:16 +01001389static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1390{
1391 return uuid_equal(&a->uuid, &b->uuid) &&
1392 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1393 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1394}
1395
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001396static void nvme_update_disk_info(struct gendisk *disk,
1397 struct nvme_ns *ns, struct nvme_id_ns *id)
1398{
1399 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
Jeff Liencee160f2017-12-19 13:24:15 -06001400 unsigned short bs = 1 << ns->lba_shift;
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001401 unsigned stream_alignment = 0;
1402
1403 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1404 stream_alignment = ns->sws * ns->sgs;
1405
1406 blk_mq_freeze_queue(disk->queue);
1407 blk_integrity_unregister(disk);
1408
Jeff Liencee160f2017-12-19 13:24:15 -06001409 blk_queue_logical_block_size(disk->queue, bs);
1410 blk_queue_physical_block_size(disk->queue, bs);
1411 blk_queue_io_min(disk->queue, bs);
1412
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001413 if (ns->ms && !ns->ext &&
1414 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1415 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001416 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001417 capacity = 0;
1418 set_capacity(disk, capacity);
1419
1420 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1421 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1422 blk_mq_unfreeze_queue(disk->queue);
1423}
1424
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001425static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1426{
1427 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001428
1429 /*
1430 * If identify namespace failed, use default 512 byte block size so
1431 * block layer can use before failing read/write for 0 capacity.
1432 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001433 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001434 if (ns->lba_shift == 0)
1435 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001436 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001437 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1438 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1439 /* the PI implementation requires metadata equal t10 pi tuple size */
1440 if (ns->ms == sizeof(struct t10_pi_tuple))
1441 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1442 else
1443 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001444
Scott Bauer6b8190d2017-06-15 10:44:30 -06001445 if (ns->noiob)
1446 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001447 nvme_update_disk_info(disk, ns, id);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001448#ifdef CONFIG_NVME_MULTIPATH
1449 if (ns->head->disk)
1450 nvme_update_disk_info(ns->head->disk, ns, id);
1451#endif
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001452}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001453
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001454static int nvme_revalidate_disk(struct gendisk *disk)
1455{
1456 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001457 struct nvme_ctrl *ctrl = ns->ctrl;
1458 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001459 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001460 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001461
1462 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1463 set_capacity(disk, 0);
1464 return -ENODEV;
1465 }
1466
Christoph Hellwiged754e52017-11-09 13:50:43 +01001467 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001468 if (!id)
1469 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001470
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001471 if (id->ncap == 0) {
1472 ret = -ENODEV;
1473 goto out;
1474 }
1475
Keith Busch5e0fab52017-10-27 13:51:22 -06001476 __nvme_revalidate_disk(disk, id);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001477 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1478 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001479 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001480 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001481 ret = -ENODEV;
1482 }
1483
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001484out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001485 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001486 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001487}
1488
1489static char nvme_pr_type(enum pr_type type)
1490{
1491 switch (type) {
1492 case PR_WRITE_EXCLUSIVE:
1493 return 1;
1494 case PR_EXCLUSIVE_ACCESS:
1495 return 2;
1496 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1497 return 3;
1498 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1499 return 4;
1500 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1501 return 5;
1502 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1503 return 6;
1504 default:
1505 return 0;
1506 }
1507};
1508
1509static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1510 u64 key, u64 sa_key, u8 op)
1511{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001512 struct nvme_ns_head *head = NULL;
1513 struct nvme_ns *ns;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001514 struct nvme_command c;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001515 int srcu_idx, ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001516 u8 data[16] = { 0, };
1517
Keith Buschb0d61d52017-11-16 13:36:49 -07001518 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1519 if (unlikely(!ns))
1520 return -EWOULDBLOCK;
1521
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001522 put_unaligned_le64(key, &data[0]);
1523 put_unaligned_le64(sa_key, &data[8]);
1524
1525 memset(&c, 0, sizeof(c));
1526 c.common.opcode = op;
Keith Buschb0d61d52017-11-16 13:36:49 -07001527 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001528 c.common.cdw10[0] = cpu_to_le32(cdw10);
1529
Keith Buschb0d61d52017-11-16 13:36:49 -07001530 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001531 nvme_put_ns_from_disk(head, srcu_idx);
1532 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001533}
1534
1535static int nvme_pr_register(struct block_device *bdev, u64 old,
1536 u64 new, unsigned flags)
1537{
1538 u32 cdw10;
1539
1540 if (flags & ~PR_FL_IGNORE_KEY)
1541 return -EOPNOTSUPP;
1542
1543 cdw10 = old ? 2 : 0;
1544 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1545 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1546 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1547}
1548
1549static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1550 enum pr_type type, unsigned flags)
1551{
1552 u32 cdw10;
1553
1554 if (flags & ~PR_FL_IGNORE_KEY)
1555 return -EOPNOTSUPP;
1556
1557 cdw10 = nvme_pr_type(type) << 8;
1558 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1559 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1560}
1561
1562static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1563 enum pr_type type, bool abort)
1564{
1565 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1566 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1567}
1568
1569static int nvme_pr_clear(struct block_device *bdev, u64 key)
1570{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001571 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001572 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1573}
1574
1575static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1576{
1577 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1578 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1579}
1580
1581static const struct pr_ops nvme_pr_ops = {
1582 .pr_register = nvme_pr_register,
1583 .pr_reserve = nvme_pr_reserve,
1584 .pr_release = nvme_pr_release,
1585 .pr_preempt = nvme_pr_preempt,
1586 .pr_clear = nvme_pr_clear,
1587};
1588
Scott Bauera98e58e52017-02-03 12:50:32 -07001589#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001590int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1591 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001592{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001593 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001594 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001595
1596 memset(&cmd, 0, sizeof(cmd));
1597 if (send)
1598 cmd.common.opcode = nvme_admin_security_send;
1599 else
1600 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001601 cmd.common.nsid = 0;
1602 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1603 cmd.common.cdw10[1] = cpu_to_le32(len);
1604
1605 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1606 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1607}
1608EXPORT_SYMBOL_GPL(nvme_sec_submit);
1609#endif /* CONFIG_BLK_SED_OPAL */
1610
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001611static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001612 .owner = THIS_MODULE,
1613 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001614 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001615 .open = nvme_open,
1616 .release = nvme_release,
1617 .getgeo = nvme_getgeo,
1618 .revalidate_disk= nvme_revalidate_disk,
1619 .pr_ops = &nvme_pr_ops,
1620};
1621
Christoph Hellwig32acab32017-11-02 12:59:30 +01001622#ifdef CONFIG_NVME_MULTIPATH
1623static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
1624{
1625 struct nvme_ns_head *head = bdev->bd_disk->private_data;
1626
1627 if (!kref_get_unless_zero(&head->ref))
1628 return -ENXIO;
1629 return 0;
1630}
1631
1632static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
1633{
1634 nvme_put_ns_head(disk->private_data);
1635}
1636
1637const struct block_device_operations nvme_ns_head_ops = {
1638 .owner = THIS_MODULE,
1639 .open = nvme_ns_head_open,
1640 .release = nvme_ns_head_release,
1641 .ioctl = nvme_ioctl,
1642 .compat_ioctl = nvme_ioctl,
1643 .getgeo = nvme_getgeo,
1644 .pr_ops = &nvme_pr_ops,
1645};
1646#endif /* CONFIG_NVME_MULTIPATH */
1647
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001648static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1649{
1650 unsigned long timeout =
1651 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1652 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1653 int ret;
1654
1655 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001656 if (csts == ~0)
1657 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001658 if ((csts & NVME_CSTS_RDY) == bit)
1659 break;
1660
1661 msleep(100);
1662 if (fatal_signal_pending(current))
1663 return -EINTR;
1664 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001665 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001666 "Device not ready; aborting %s\n", enabled ?
1667 "initialisation" : "reset");
1668 return -ENODEV;
1669 }
1670 }
1671
1672 return ret;
1673}
1674
1675/*
1676 * If the device has been passed off to us in an enabled state, just clear
1677 * the enabled bit. The spec says we should set the 'shutdown notification
1678 * bits', but doing so may cause the device to complete commands to the
1679 * admin queue ... and we don't know what memory that might be pointing at!
1680 */
1681int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1682{
1683 int ret;
1684
1685 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1686 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1687
1688 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1689 if (ret)
1690 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001691
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001692 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001693 msleep(NVME_QUIRK_DELAY_AMOUNT);
1694
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001695 return nvme_wait_ready(ctrl, cap, false);
1696}
Ming Lin576d55d2016-02-10 10:03:32 -08001697EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001698
1699int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1700{
1701 /*
1702 * Default to a 4K page size, with the intention to update this
1703 * path in the future to accomodate architectures with differing
1704 * kernel and IO page sizes.
1705 */
1706 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1707 int ret;
1708
1709 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001710 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001711 "Minimum device page size %u too large for host (%u)\n",
1712 1 << dev_page_min, 1 << page_shift);
1713 return -ENODEV;
1714 }
1715
1716 ctrl->page_size = 1 << page_shift;
1717
1718 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1719 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001720 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001721 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1722 ctrl->ctrl_config |= NVME_CC_ENABLE;
1723
1724 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1725 if (ret)
1726 return ret;
1727 return nvme_wait_ready(ctrl, cap, true);
1728}
Ming Lin576d55d2016-02-10 10:03:32 -08001729EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001730
1731int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1732{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001733 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001734 u32 csts;
1735 int ret;
1736
1737 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1738 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1739
1740 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1741 if (ret)
1742 return ret;
1743
1744 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1745 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1746 break;
1747
1748 msleep(100);
1749 if (fatal_signal_pending(current))
1750 return -EINTR;
1751 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001752 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001753 "Device shutdown incomplete; abort shutdown\n");
1754 return -ENODEV;
1755 }
1756 }
1757
1758 return ret;
1759}
Ming Lin576d55d2016-02-10 10:03:32 -08001760EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001761
Christoph Hellwigda358252016-03-02 18:07:11 +01001762static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1763 struct request_queue *q)
1764{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001765 bool vwc = false;
1766
Christoph Hellwigda358252016-03-02 18:07:11 +01001767 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001768 u32 max_segments =
1769 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1770
Christoph Hellwigda358252016-03-02 18:07:11 +01001771 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001772 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001773 }
Keith Busch249159c2017-12-14 11:20:14 -07001774 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
1775 is_power_of_2(ctrl->max_hw_sectors))
Keith Busche6282ae2016-12-19 11:37:50 -05001776 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001777 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001778 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1779 vwc = true;
1780 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001781}
1782
Jon Derrickdbf86b32017-08-16 09:51:29 +02001783static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1784{
1785 __le64 ts;
1786 int ret;
1787
1788 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1789 return 0;
1790
1791 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1792 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1793 NULL);
1794 if (ret)
1795 dev_warn_once(ctrl->device,
1796 "could not set timestamp (%d)\n", ret);
1797 return ret;
1798}
1799
Keith Busch634b8322017-08-10 11:23:31 +02001800static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001801{
1802 /*
1803 * APST (Autonomous Power State Transition) lets us program a
1804 * table of power state transitions that the controller will
1805 * perform automatically. We configure it with a simple
1806 * heuristic: we are willing to spend at most 2% of the time
1807 * transitioning between power states. Therefore, when running
1808 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001809 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001810 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001811 * the requested maximum latency.
1812 *
1813 * We will not autonomously enter any non-operational state for
1814 * which the total latency exceeds ps_max_latency_us. Users
1815 * can set ps_max_latency_us to zero to turn off APST.
1816 */
1817
1818 unsigned apste;
1819 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001820 u64 max_lat_us = 0;
1821 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001822 int ret;
1823
1824 /*
1825 * If APST isn't supported or if we haven't been initialized yet,
1826 * then don't do anything.
1827 */
1828 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001829 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001830
1831 if (ctrl->npss > 31) {
1832 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001833 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001834 }
1835
1836 table = kzalloc(sizeof(*table), GFP_KERNEL);
1837 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001838 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001839
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001840 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001841 /* Turn off APST. */
1842 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001843 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001844 } else {
1845 __le64 target = cpu_to_le64(0);
1846 int state;
1847
1848 /*
1849 * Walk through all states from lowest- to highest-power.
1850 * According to the spec, lower-numbered states use more
1851 * power. NPSS, despite the name, is the index of the
1852 * lowest-power state, not the number of states.
1853 */
1854 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001855 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001856
1857 if (target)
1858 table->entries[state] = target;
1859
1860 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001861 * Don't allow transitions to the deepest state
1862 * if it's quirked off.
1863 */
1864 if (state == ctrl->npss &&
1865 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1866 continue;
1867
1868 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001869 * Is this state a useful non-operational state for
1870 * higher-power states to autonomously transition to?
1871 */
1872 if (!(ctrl->psd[state].flags &
1873 NVME_PS_FLAGS_NON_OP_STATE))
1874 continue;
1875
Kai-Heng Fengda875912017-06-07 15:25:42 +08001876 exit_latency_us =
1877 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1878 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001879 continue;
1880
Kai-Heng Fengda875912017-06-07 15:25:42 +08001881 total_latency_us =
1882 exit_latency_us +
1883 le32_to_cpu(ctrl->psd[state].entry_lat);
1884
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001885 /*
1886 * This state is good. Use it as the APST idle
1887 * target for higher power states.
1888 */
1889 transition_ms = total_latency_us + 19;
1890 do_div(transition_ms, 20);
1891 if (transition_ms > (1 << 24) - 1)
1892 transition_ms = (1 << 24) - 1;
1893
1894 target = cpu_to_le64((state << 3) |
1895 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001896
1897 if (max_ps == -1)
1898 max_ps = state;
1899
1900 if (total_latency_us > max_lat_us)
1901 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001902 }
1903
1904 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001905
1906 if (max_ps == -1) {
1907 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1908 } else {
1909 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1910 max_ps, max_lat_us, (int)sizeof(*table), table);
1911 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001912 }
1913
1914 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1915 table, sizeof(*table), NULL);
1916 if (ret)
1917 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1918
1919 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001920 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001921}
1922
1923static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1924{
1925 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1926 u64 latency;
1927
1928 switch (val) {
1929 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1930 case PM_QOS_LATENCY_ANY:
1931 latency = U64_MAX;
1932 break;
1933
1934 default:
1935 latency = val;
1936 }
1937
1938 if (ctrl->ps_max_latency_us != latency) {
1939 ctrl->ps_max_latency_us = latency;
1940 nvme_configure_apst(ctrl);
1941 }
1942}
1943
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001944struct nvme_core_quirk_entry {
1945 /*
1946 * NVMe model and firmware strings are padded with spaces. For
1947 * simplicity, strings in the quirk table are padded with NULLs
1948 * instead.
1949 */
1950 u16 vid;
1951 const char *mn;
1952 const char *fr;
1953 unsigned long quirks;
1954};
1955
1956static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001957 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001958 /*
1959 * This Toshiba device seems to die using any APST states. See:
1960 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1961 */
1962 .vid = 0x1179,
1963 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001964 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001965 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001966};
1967
1968/* match is null-terminated but idstr is space-padded. */
1969static bool string_matches(const char *idstr, const char *match, size_t len)
1970{
1971 size_t matchlen;
1972
1973 if (!match)
1974 return true;
1975
1976 matchlen = strlen(match);
1977 WARN_ON_ONCE(matchlen > len);
1978
1979 if (memcmp(idstr, match, matchlen))
1980 return false;
1981
1982 for (; matchlen < len; matchlen++)
1983 if (idstr[matchlen] != ' ')
1984 return false;
1985
1986 return true;
1987}
1988
1989static bool quirk_matches(const struct nvme_id_ctrl *id,
1990 const struct nvme_core_quirk_entry *q)
1991{
1992 return q->vid == le16_to_cpu(id->vid) &&
1993 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1994 string_matches(id->fr, q->fr, sizeof(id->fr));
1995}
1996
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01001997static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
1998 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02001999{
2000 size_t nqnlen;
2001 int off;
2002
2003 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
2004 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002005 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02002006 return;
2007 }
2008
2009 if (ctrl->vs >= NVME_VS(1, 2, 1))
2010 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
2011
2012 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002013 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02002014 "nqn.2014.08.org.nvmexpress:%4x%4x",
2015 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002016 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002017 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002018 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002019 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002020 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
2021}
2022
2023static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
2024{
2025 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
2026 kfree(subsys);
2027}
2028
2029static void nvme_release_subsystem(struct device *dev)
2030{
2031 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
2032}
2033
2034static void nvme_destroy_subsystem(struct kref *ref)
2035{
2036 struct nvme_subsystem *subsys =
2037 container_of(ref, struct nvme_subsystem, ref);
2038
2039 mutex_lock(&nvme_subsystems_lock);
2040 list_del(&subsys->entry);
2041 mutex_unlock(&nvme_subsystems_lock);
2042
Christoph Hellwiged754e52017-11-09 13:50:43 +01002043 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002044 device_del(&subsys->dev);
2045 put_device(&subsys->dev);
2046}
2047
2048static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2049{
2050 kref_put(&subsys->ref, nvme_destroy_subsystem);
2051}
2052
2053static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2054{
2055 struct nvme_subsystem *subsys;
2056
2057 lockdep_assert_held(&nvme_subsystems_lock);
2058
2059 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2060 if (strcmp(subsys->subnqn, subsysnqn))
2061 continue;
2062 if (!kref_get_unless_zero(&subsys->ref))
2063 continue;
2064 return subsys;
2065 }
2066
2067 return NULL;
2068}
2069
Hannes Reinecke1e496932017-11-10 10:58:23 +01002070#define SUBSYS_ATTR_RO(_name, _mode, _show) \
2071 struct device_attribute subsys_attr_##_name = \
2072 __ATTR(_name, _mode, _show, NULL)
2073
2074static ssize_t nvme_subsys_show_nqn(struct device *dev,
2075 struct device_attribute *attr,
2076 char *buf)
2077{
2078 struct nvme_subsystem *subsys =
2079 container_of(dev, struct nvme_subsystem, dev);
2080
2081 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2082}
2083static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2084
2085#define nvme_subsys_show_str_function(field) \
2086static ssize_t subsys_##field##_show(struct device *dev, \
2087 struct device_attribute *attr, char *buf) \
2088{ \
2089 struct nvme_subsystem *subsys = \
2090 container_of(dev, struct nvme_subsystem, dev); \
2091 return sprintf(buf, "%.*s\n", \
2092 (int)sizeof(subsys->field), subsys->field); \
2093} \
2094static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2095
2096nvme_subsys_show_str_function(model);
2097nvme_subsys_show_str_function(serial);
2098nvme_subsys_show_str_function(firmware_rev);
2099
2100static struct attribute *nvme_subsys_attrs[] = {
2101 &subsys_attr_model.attr,
2102 &subsys_attr_serial.attr,
2103 &subsys_attr_firmware_rev.attr,
2104 &subsys_attr_subsysnqn.attr,
2105 NULL,
2106};
2107
2108static struct attribute_group nvme_subsys_attrs_group = {
2109 .attrs = nvme_subsys_attrs,
2110};
2111
2112static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2113 &nvme_subsys_attrs_group,
2114 NULL,
2115};
2116
Israel Rukshinb837b282018-01-04 17:56:14 +02002117static int nvme_active_ctrls(struct nvme_subsystem *subsys)
2118{
2119 int count = 0;
2120 struct nvme_ctrl *ctrl;
2121
2122 mutex_lock(&subsys->lock);
2123 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2124 if (ctrl->state != NVME_CTRL_DELETING &&
2125 ctrl->state != NVME_CTRL_DEAD)
2126 count++;
2127 }
2128 mutex_unlock(&subsys->lock);
2129
2130 return count;
2131}
2132
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002133static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2134{
2135 struct nvme_subsystem *subsys, *found;
2136 int ret;
2137
2138 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2139 if (!subsys)
2140 return -ENOMEM;
2141 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
2142 if (ret < 0) {
2143 kfree(subsys);
2144 return ret;
2145 }
2146 subsys->instance = ret;
2147 mutex_init(&subsys->lock);
2148 kref_init(&subsys->ref);
2149 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002150 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002151 nvme_init_subnqn(subsys, ctrl, id);
2152 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2153 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2154 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2155 subsys->vendor_id = le16_to_cpu(id->vid);
2156 subsys->cmic = id->cmic;
2157
2158 subsys->dev.class = nvme_subsys_class;
2159 subsys->dev.release = nvme_release_subsystem;
Hannes Reinecke1e496932017-11-10 10:58:23 +01002160 subsys->dev.groups = nvme_subsys_attrs_groups;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002161 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
2162 device_initialize(&subsys->dev);
2163
2164 mutex_lock(&nvme_subsystems_lock);
2165 found = __nvme_find_get_subsystem(subsys->subnqn);
2166 if (found) {
2167 /*
2168 * Verify that the subsystem actually supports multiple
2169 * controllers, else bail out.
2170 */
Israel Rukshinb837b282018-01-04 17:56:14 +02002171 if (nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002172 dev_err(ctrl->device,
2173 "ignoring ctrl due to duplicate subnqn (%s).\n",
2174 found->subnqn);
2175 nvme_put_subsystem(found);
2176 ret = -EINVAL;
2177 goto out_unlock;
2178 }
2179
2180 __nvme_release_subsystem(subsys);
2181 subsys = found;
2182 } else {
2183 ret = device_add(&subsys->dev);
2184 if (ret) {
2185 dev_err(ctrl->device,
2186 "failed to register subsystem device.\n");
2187 goto out_unlock;
2188 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002189 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002190 list_add_tail(&subsys->entry, &nvme_subsystems);
2191 }
2192
2193 ctrl->subsys = subsys;
2194 mutex_unlock(&nvme_subsystems_lock);
2195
2196 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2197 dev_name(ctrl->device))) {
2198 dev_err(ctrl->device,
2199 "failed to create sysfs link from subsystem.\n");
2200 /* the transport driver will eventually put the subsystem */
2201 return -EINVAL;
2202 }
2203
2204 mutex_lock(&subsys->lock);
2205 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2206 mutex_unlock(&subsys->lock);
2207
2208 return 0;
2209
2210out_unlock:
2211 mutex_unlock(&nvme_subsystems_lock);
2212 put_device(&subsys->dev);
2213 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02002214}
2215
Matias Bjørling70da6092018-02-26 13:55:40 +01002216static int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
2217 u8 log_page, void *log,
2218 size_t size, size_t offset)
2219{
2220 struct nvme_command c = { };
2221 unsigned long dwlen = size / 4 - 1;
2222
2223 c.get_log_page.opcode = nvme_admin_get_log_page;
2224
2225 if (ns)
2226 c.get_log_page.nsid = cpu_to_le32(ns->head->ns_id);
2227 else
2228 c.get_log_page.nsid = cpu_to_le32(NVME_NSID_ALL);
2229
2230 c.get_log_page.lid = log_page;
2231 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1));
2232 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16);
2233 c.get_log_page.lpol = cpu_to_le32(offset & ((1ULL << 32) - 1));
2234 c.get_log_page.lpou = cpu_to_le32(offset >> 32ULL);
2235
2236 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2237}
2238
Keith Buschc627c482017-11-07 10:28:31 -07002239static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
2240 size_t size)
2241{
Matias Bjørling70da6092018-02-26 13:55:40 +01002242 return nvme_get_log_ext(ctrl, NULL, log_page, log, size, 0);
Keith Buschc627c482017-11-07 10:28:31 -07002243}
2244
Keith Busch84fef622017-11-07 10:28:32 -07002245static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2246{
2247 int ret;
2248
2249 if (!ctrl->effects)
2250 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2251
2252 if (!ctrl->effects)
2253 return 0;
2254
2255 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2256 sizeof(*ctrl->effects));
2257 if (ret) {
2258 kfree(ctrl->effects);
2259 ctrl->effects = NULL;
2260 }
2261 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002262}
2263
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002264/*
2265 * Initialize the cached copies of the Identify data and various controller
2266 * register in our nvme_ctrl structure. This should be called as soon as
2267 * the admin queue is fully up and running.
2268 */
2269int nvme_init_identify(struct nvme_ctrl *ctrl)
2270{
2271 struct nvme_id_ctrl *id;
2272 u64 cap;
2273 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002274 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002275 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002276
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002277 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2278 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002279 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002280 return ret;
2281 }
2282
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002283 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2284 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002285 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002286 return ret;
2287 }
2288 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2289
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002290 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002291 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2292
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002293 ret = nvme_identify_ctrl(ctrl, &id);
2294 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002295 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002296 return -EIO;
2297 }
2298
Keith Busch84fef622017-11-07 10:28:32 -07002299 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2300 ret = nvme_get_effects_log(ctrl);
2301 if (ret < 0)
2302 return ret;
2303 }
Christoph Hellwig180de0072017-06-26 12:39:02 +02002304
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002305 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002306 int i;
2307
2308 ret = nvme_init_subsystem(ctrl, id);
2309 if (ret)
2310 goto out_free;
2311
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002312 /*
2313 * Check for quirks. Quirk can depend on firmware version,
2314 * so, in principle, the set of quirks present can change
2315 * across a reset. As a possible future enhancement, we
2316 * could re-scan for quirks every time we reinitialize
2317 * the device, but we'd have to make sure that the driver
2318 * behaves intelligently if the quirks change.
2319 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002320 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2321 if (quirk_matches(id, &core_quirks[i]))
2322 ctrl->quirks |= core_quirks[i].quirks;
2323 }
2324 }
2325
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002326 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002327 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 -07002328 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2329 }
2330
Scott Bauer8a9ae522017-02-17 13:59:40 +01002331 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002332 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002333 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002334 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002335 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002336 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002337 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002338 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002339 max_hw_sectors = UINT_MAX;
2340 ctrl->max_hw_sectors =
2341 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002342
Christoph Hellwigda358252016-03-02 18:07:11 +01002343 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002344 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002345 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002346
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002347 if (id->rtd3e) {
2348 /* us -> s */
2349 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2350
2351 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2352 shutdown_timeout, 60);
2353
2354 if (ctrl->shutdown_timeout != shutdown_timeout)
Max Gurtovoy1a3838d2017-12-31 15:33:27 +02002355 dev_info(ctrl->device,
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002356 "Shutdown timeout set to %u seconds\n",
2357 ctrl->shutdown_timeout);
2358 } else
2359 ctrl->shutdown_timeout = shutdown_timeout;
2360
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002361 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002362 ctrl->apsta = id->apsta;
2363 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002364 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2365 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002366 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 -04002367 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002368 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002369 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002370 }
2371 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002372 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002373 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002374 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2375
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002376 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002377 ctrl->icdoff = le16_to_cpu(id->icdoff);
2378 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2379 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2380 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2381
2382 /*
2383 * In fabrics we need to verify the cntlid matches the
2384 * admin connect
2385 */
Keith Busch634b8322017-08-10 11:23:31 +02002386 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002387 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002388 goto out_free;
2389 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002390
2391 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002392 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002393 "keep-alive support is mandatory for fabrics\n");
2394 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002395 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002396 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002397 } else {
2398 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002399 ctrl->hmpre = le32_to_cpu(id->hmpre);
2400 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002401 ctrl->hmminds = le32_to_cpu(id->hmminds);
2402 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002403 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002404
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002405 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002406
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002407 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002408 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002409 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002410 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2411
Keith Busch634b8322017-08-10 11:23:31 +02002412 ret = nvme_configure_apst(ctrl);
2413 if (ret < 0)
2414 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002415
2416 ret = nvme_configure_timestamp(ctrl);
2417 if (ret < 0)
2418 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002419
2420 ret = nvme_configure_directives(ctrl);
2421 if (ret < 0)
2422 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002423
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002424 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002425
Keith Busch634b8322017-08-10 11:23:31 +02002426 return 0;
2427
2428out_free:
2429 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002430 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002431}
Ming Lin576d55d2016-02-10 10:03:32 -08002432EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002433
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002434static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig21d34712015-11-26 09:08:36 +01002435{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002436 struct nvme_ctrl *ctrl =
2437 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig21d34712015-11-26 09:08:36 +01002438
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002439 switch (ctrl->state) {
2440 case NVME_CTRL_LIVE:
2441 case NVME_CTRL_ADMIN_ONLY:
2442 break;
2443 default:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002444 return -EWOULDBLOCK;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002445 }
2446
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002447 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002448 return 0;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002449}
2450
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002451static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2452{
2453 struct nvme_ns *ns;
2454 int ret;
2455
Jianchao Wang765cc032018-02-12 20:54:46 +08002456 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002457 if (list_empty(&ctrl->namespaces)) {
2458 ret = -ENOTTY;
2459 goto out_unlock;
2460 }
2461
2462 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2463 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002464 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002465 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2466 ret = -EINVAL;
2467 goto out_unlock;
2468 }
2469
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002470 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002471 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2472 kref_get(&ns->kref);
Jianchao Wang765cc032018-02-12 20:54:46 +08002473 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002474
2475 ret = nvme_user_cmd(ctrl, ns, argp);
2476 nvme_put_ns(ns);
2477 return ret;
2478
2479out_unlock:
Jianchao Wang765cc032018-02-12 20:54:46 +08002480 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002481 return ret;
2482}
2483
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002484static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2485 unsigned long arg)
2486{
2487 struct nvme_ctrl *ctrl = file->private_data;
2488 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002489
2490 switch (cmd) {
2491 case NVME_IOCTL_ADMIN_CMD:
2492 return nvme_user_cmd(ctrl, NULL, argp);
2493 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002494 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002495 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002496 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002497 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002498 case NVME_IOCTL_SUBSYS_RESET:
2499 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002500 case NVME_IOCTL_RESCAN:
2501 nvme_queue_scan(ctrl);
2502 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002503 default:
2504 return -ENOTTY;
2505 }
2506}
2507
2508static const struct file_operations nvme_dev_fops = {
2509 .owner = THIS_MODULE,
2510 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002511 .unlocked_ioctl = nvme_dev_ioctl,
2512 .compat_ioctl = nvme_dev_ioctl,
2513};
2514
2515static ssize_t nvme_sysfs_reset(struct device *dev,
2516 struct device_attribute *attr, const char *buf,
2517 size_t count)
2518{
2519 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2520 int ret;
2521
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002522 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002523 if (ret < 0)
2524 return ret;
2525 return count;
2526}
2527static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2528
Keith Busch9ec3bb22016-04-29 15:45:18 -06002529static ssize_t nvme_sysfs_rescan(struct device *dev,
2530 struct device_attribute *attr, const char *buf,
2531 size_t count)
2532{
2533 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2534
2535 nvme_queue_scan(ctrl);
2536 return count;
2537}
2538static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2539
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002540static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
Keith Busch118472a2016-02-18 09:57:48 -07002541{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002542 struct gendisk *disk = dev_to_disk(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002543
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002544 if (disk->fops == &nvme_fops)
2545 return nvme_get_ns_from_dev(dev)->head;
2546 else
2547 return disk->private_data;
2548}
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002549
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002550static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2551 char *buf)
2552{
2553 struct nvme_ns_head *head = dev_to_ns_head(dev);
2554 struct nvme_ns_ids *ids = &head->ids;
2555 struct nvme_subsystem *subsys = head->subsys;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002556 int serial_len = sizeof(subsys->serial);
2557 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002558
Christoph Hellwig002fab02017-11-09 13:50:16 +01002559 if (!uuid_is_null(&ids->uuid))
2560 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Keith Busch118472a2016-02-18 09:57:48 -07002561
Christoph Hellwig002fab02017-11-09 13:50:16 +01002562 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2563 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002564
Christoph Hellwig002fab02017-11-09 13:50:16 +01002565 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2566 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002567
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002568 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2569 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002570 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002571 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2572 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002573 model_len--;
2574
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002575 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2576 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002577 head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002578}
Joe Perchesc828a892017-12-19 10:15:08 -08002579static DEVICE_ATTR_RO(wwid);
Keith Busch118472a2016-02-18 09:57:48 -07002580
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002581static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002582 char *buf)
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002583{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002584 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002585}
Joe Perchesc828a892017-12-19 10:15:08 -08002586static DEVICE_ATTR_RO(nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002587
Keith Busch2b9b6e82015-12-22 10:10:45 -07002588static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002589 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002590{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002591 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002592
2593 /* For backward compatibility expose the NGUID to userspace if
2594 * we have no UUID set
2595 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002596 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002597 printk_ratelimited(KERN_WARNING
2598 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002599 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002600 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002601 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002602}
Joe Perchesc828a892017-12-19 10:15:08 -08002603static DEVICE_ATTR_RO(uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002604
2605static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002606 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002607{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002608 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002609}
Joe Perchesc828a892017-12-19 10:15:08 -08002610static DEVICE_ATTR_RO(eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002611
2612static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002613 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002614{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002615 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002616}
Joe Perchesc828a892017-12-19 10:15:08 -08002617static DEVICE_ATTR_RO(nsid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002618
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002619static struct attribute *nvme_ns_id_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002620 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002621 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002622 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002623 &dev_attr_eui.attr,
2624 &dev_attr_nsid.attr,
2625 NULL,
2626};
2627
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002628static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002629 struct attribute *a, int n)
2630{
2631 struct device *dev = container_of(kobj, struct device, kobj);
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002632 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002633
2634 if (a == &dev_attr_uuid.attr) {
Martin Wilcka04b5de2017-09-28 21:33:23 +02002635 if (uuid_is_null(&ids->uuid) &&
Christoph Hellwig002fab02017-11-09 13:50:16 +01002636 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002637 return 0;
2638 }
2639 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002640 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002641 return 0;
2642 }
2643 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002644 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002645 return 0;
2646 }
2647 return a->mode;
2648}
2649
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002650const struct attribute_group nvme_ns_id_attr_group = {
2651 .attrs = nvme_ns_id_attrs,
2652 .is_visible = nvme_ns_id_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002653};
2654
Ming Lin931e1c22016-02-26 13:24:19 -08002655#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002656static ssize_t field##_show(struct device *dev, \
2657 struct device_attribute *attr, char *buf) \
2658{ \
2659 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002660 return sprintf(buf, "%.*s\n", \
2661 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002662} \
2663static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2664
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002665nvme_show_str_function(model);
2666nvme_show_str_function(serial);
2667nvme_show_str_function(firmware_rev);
2668
Ming Lin931e1c22016-02-26 13:24:19 -08002669#define nvme_show_int_function(field) \
2670static ssize_t field##_show(struct device *dev, \
2671 struct device_attribute *attr, char *buf) \
2672{ \
2673 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2674 return sprintf(buf, "%d\n", ctrl->field); \
2675} \
2676static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2677
Ming Lin931e1c22016-02-26 13:24:19 -08002678nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002679
Ming Lin1a353d82016-06-13 16:45:24 +02002680static ssize_t nvme_sysfs_delete(struct device *dev,
2681 struct device_attribute *attr, const char *buf,
2682 size_t count)
2683{
2684 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2685
2686 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002687 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002688 return count;
2689}
2690static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2691
2692static ssize_t nvme_sysfs_show_transport(struct device *dev,
2693 struct device_attribute *attr,
2694 char *buf)
2695{
2696 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2697
2698 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2699}
2700static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2701
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002702static ssize_t nvme_sysfs_show_state(struct device *dev,
2703 struct device_attribute *attr,
2704 char *buf)
2705{
2706 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2707 static const char *const state_name[] = {
2708 [NVME_CTRL_NEW] = "new",
2709 [NVME_CTRL_LIVE] = "live",
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002710 [NVME_CTRL_ADMIN_ONLY] = "only-admin",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002711 [NVME_CTRL_RESETTING] = "resetting",
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002712 [NVME_CTRL_CONNECTING] = "connecting",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002713 [NVME_CTRL_DELETING] = "deleting",
2714 [NVME_CTRL_DEAD] = "dead",
2715 };
2716
2717 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2718 state_name[ctrl->state])
2719 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2720
2721 return sprintf(buf, "unknown state\n");
2722}
2723
2724static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2725
Ming Lin1a353d82016-06-13 16:45:24 +02002726static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2727 struct device_attribute *attr,
2728 char *buf)
2729{
2730 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2731
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002732 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002733}
2734static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2735
2736static ssize_t nvme_sysfs_show_address(struct device *dev,
2737 struct device_attribute *attr,
2738 char *buf)
2739{
2740 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2741
2742 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2743}
2744static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2745
Keith Busch779ff7562016-01-12 15:09:31 -07002746static struct attribute *nvme_dev_attrs[] = {
2747 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002748 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002749 &dev_attr_model.attr,
2750 &dev_attr_serial.attr,
2751 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002752 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002753 &dev_attr_delete_controller.attr,
2754 &dev_attr_transport.attr,
2755 &dev_attr_subsysnqn.attr,
2756 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002757 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002758 NULL
2759};
2760
Ming Lin1a353d82016-06-13 16:45:24 +02002761static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2762 struct attribute *a, int n)
2763{
2764 struct device *dev = container_of(kobj, struct device, kobj);
2765 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2766
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002767 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2768 return 0;
2769 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2770 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002771
2772 return a->mode;
2773}
2774
Keith Busch779ff7562016-01-12 15:09:31 -07002775static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002776 .attrs = nvme_dev_attrs,
2777 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002778};
2779
2780static const struct attribute_group *nvme_dev_attr_groups[] = {
2781 &nvme_dev_attrs_group,
2782 NULL,
2783};
2784
Christoph Hellwiged754e52017-11-09 13:50:43 +01002785static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2786 unsigned nsid)
2787{
2788 struct nvme_ns_head *h;
2789
2790 lockdep_assert_held(&subsys->lock);
2791
2792 list_for_each_entry(h, &subsys->nsheads, entry) {
2793 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2794 return h;
2795 }
2796
2797 return NULL;
2798}
2799
2800static int __nvme_check_ids(struct nvme_subsystem *subsys,
2801 struct nvme_ns_head *new)
2802{
2803 struct nvme_ns_head *h;
2804
2805 lockdep_assert_held(&subsys->lock);
2806
2807 list_for_each_entry(h, &subsys->nsheads, entry) {
2808 if (nvme_ns_ids_valid(&new->ids) &&
2809 nvme_ns_ids_equal(&new->ids, &h->ids))
2810 return -EINVAL;
2811 }
2812
2813 return 0;
2814}
2815
2816static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2817 unsigned nsid, struct nvme_id_ns *id)
2818{
2819 struct nvme_ns_head *head;
2820 int ret = -ENOMEM;
2821
2822 head = kzalloc(sizeof(*head), GFP_KERNEL);
2823 if (!head)
2824 goto out;
2825 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2826 if (ret < 0)
2827 goto out_free_head;
2828 head->instance = ret;
2829 INIT_LIST_HEAD(&head->list);
2830 init_srcu_struct(&head->srcu);
2831 head->subsys = ctrl->subsys;
2832 head->ns_id = nsid;
2833 kref_init(&head->ref);
2834
2835 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2836
2837 ret = __nvme_check_ids(ctrl->subsys, head);
2838 if (ret) {
2839 dev_err(ctrl->device,
2840 "duplicate IDs for nsid %d\n", nsid);
2841 goto out_cleanup_srcu;
2842 }
2843
Christoph Hellwig32acab32017-11-02 12:59:30 +01002844 ret = nvme_mpath_alloc_disk(ctrl, head);
2845 if (ret)
2846 goto out_cleanup_srcu;
2847
Christoph Hellwiged754e52017-11-09 13:50:43 +01002848 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
2849 return head;
2850out_cleanup_srcu:
2851 cleanup_srcu_struct(&head->srcu);
2852 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2853out_free_head:
2854 kfree(head);
2855out:
2856 return ERR_PTR(ret);
2857}
2858
2859static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002860 struct nvme_id_ns *id)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002861{
2862 struct nvme_ctrl *ctrl = ns->ctrl;
2863 bool is_shared = id->nmic & (1 << 0);
2864 struct nvme_ns_head *head = NULL;
2865 int ret = 0;
2866
2867 mutex_lock(&ctrl->subsys->lock);
2868 if (is_shared)
2869 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2870 if (!head) {
2871 head = nvme_alloc_ns_head(ctrl, nsid, id);
2872 if (IS_ERR(head)) {
2873 ret = PTR_ERR(head);
2874 goto out_unlock;
2875 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002876 } else {
2877 struct nvme_ns_ids ids;
2878
2879 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2880 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2881 dev_err(ctrl->device,
2882 "IDs don't match for shared namespace %d\n",
2883 nsid);
2884 ret = -EINVAL;
2885 goto out_unlock;
2886 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002887 }
2888
2889 list_add_tail(&ns->siblings, &head->list);
2890 ns->head = head;
2891
2892out_unlock:
2893 mutex_unlock(&ctrl->subsys->lock);
2894 return ret;
2895}
2896
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002897static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2898{
2899 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2900 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2901
Christoph Hellwiged754e52017-11-09 13:50:43 +01002902 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002903}
2904
Keith Busch32f0c4a2016-07-13 11:45:02 -06002905static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002906{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002907 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002908
Jianchao Wang765cc032018-02-12 20:54:46 +08002909 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002910 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002911 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002912 if (!kref_get_unless_zero(&ns->kref))
2913 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002914 ret = ns;
2915 break;
2916 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002917 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002918 break;
2919 }
Jianchao Wang765cc032018-02-12 20:54:46 +08002920 up_read(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002921 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002922}
2923
Jens Axboef5d11842017-06-27 12:03:06 -06002924static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2925{
2926 struct streams_directive_params s;
2927 int ret;
2928
2929 if (!ctrl->nr_streams)
2930 return 0;
2931
Christoph Hellwiged754e52017-11-09 13:50:43 +01002932 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002933 if (ret)
2934 return ret;
2935
2936 ns->sws = le32_to_cpu(s.sws);
2937 ns->sgs = le16_to_cpu(s.sgs);
2938
2939 if (ns->sws) {
2940 unsigned int bs = 1 << ns->lba_shift;
2941
2942 blk_queue_io_min(ns->queue, bs * ns->sws);
2943 if (ns->sgs)
2944 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2945 }
2946
2947 return 0;
2948}
2949
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002950static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2951{
2952 struct nvme_ns *ns;
2953 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002954 struct nvme_id_ns *id;
2955 char disk_name[DISK_NAME_LEN];
Christoph Hellwig32acab32017-11-02 12:59:30 +01002956 int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002957
2958 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2959 if (!ns)
2960 return;
2961
2962 ns->queue = blk_mq_init_queue(ctrl->tagset);
2963 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002964 goto out_free_ns;
Bart Van Assche8b904b52018-03-07 17:10:10 -08002965 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002966 ns->queue->queuedata = ns;
2967 ns->ctrl = ctrl;
2968
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002969 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002970 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002971
2972 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002973 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002974
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002975 id = nvme_identify_ns(ctrl, nsid);
2976 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002977 goto out_free_queue;
2978
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002979 if (id->ncap == 0)
2980 goto out_free_id;
2981
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002982 if (nvme_init_ns_head(ns, nsid, id))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002983 goto out_free_id;
Keith Busch654b4a42017-12-14 11:20:32 -07002984 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002985
Christoph Hellwig32acab32017-11-02 12:59:30 +01002986#ifdef CONFIG_NVME_MULTIPATH
2987 /*
2988 * If multipathing is enabled we need to always use the subsystem
2989 * instance number for numbering our devices to avoid conflicts
2990 * between subsystems that have multiple controllers and thus use
2991 * the multipath-aware subsystem node and those that have a single
2992 * controller and use the controller node directly.
2993 */
2994 if (ns->head->disk) {
2995 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
2996 ctrl->cntlid, ns->head->instance);
2997 flags = GENHD_FL_HIDDEN;
2998 } else {
2999 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
3000 ns->head->instance);
3001 }
3002#else
3003 /*
3004 * But without the multipath code enabled, multiple controller per
3005 * subsystems are visible as devices and thus we cannot use the
3006 * subsystem instance.
3007 */
Christoph Hellwiged754e52017-11-09 13:50:43 +01003008 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003009#endif
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02003010
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003011 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
3012 if (nvme_nvm_register(ns, disk_name, node)) {
3013 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01003014 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003015 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003016 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003017
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003018 disk = alloc_disk_node(0, node);
3019 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01003020 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003021
3022 disk->fops = &nvme_fops;
3023 disk->private_data = ns;
3024 disk->queue = ns->queue;
Christoph Hellwig32acab32017-11-02 12:59:30 +01003025 disk->flags = flags;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003026 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
3027 ns->disk = disk;
3028
3029 __nvme_revalidate_disk(disk, id);
3030
Jianchao Wang765cc032018-02-12 20:54:46 +08003031 down_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003032 list_add_tail(&ns->list, &ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003033 up_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003034
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003035 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003036
3037 kfree(id);
3038
Dan Williams0d52c7562016-06-15 19:44:20 -07003039 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003040 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003041 &nvme_ns_id_attr_group))
Keith Busch2b9b6e82015-12-22 10:10:45 -07003042 pr_warn("%s: failed to create sysfs group for identification\n",
3043 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003044 if (ns->ndev && nvme_nvm_register_sysfs(ns))
3045 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
3046 ns->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003047
Baegjae Sung9bd82b12018-02-28 16:06:04 +09003048 nvme_mpath_add_disk(ns->head);
Hannes Reineckee9a48032017-11-09 17:57:06 +01003049 nvme_mpath_add_disk_links(ns);
Thomas Taib9e03852018-02-08 13:38:29 -05003050 nvme_fault_inject_init(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003051 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01003052 out_unlink_ns:
3053 mutex_lock(&ctrl->subsys->lock);
3054 list_del_rcu(&ns->siblings);
3055 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003056 out_free_id:
3057 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003058 out_free_queue:
3059 blk_cleanup_queue(ns->queue);
3060 out_free_ns:
3061 kfree(ns);
3062}
3063
3064static void nvme_ns_remove(struct nvme_ns *ns)
3065{
Keith Busch646017a2016-02-24 09:15:54 -07003066 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3067 return;
3068
Thomas Taib9e03852018-02-08 13:38:29 -05003069 nvme_fault_inject_fini(ns);
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003070 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Hannes Reineckee9a48032017-11-09 17:57:06 +01003071 nvme_mpath_remove_disk_links(ns);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003072 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003073 &nvme_ns_id_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003074 if (ns->ndev)
3075 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003076 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003077 blk_cleanup_queue(ns->queue);
Ming Leibd9f5d62017-12-06 18:30:09 +08003078 if (blk_get_integrity(ns->disk))
3079 blk_integrity_unregister(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003080 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003081
Christoph Hellwiged754e52017-11-09 13:50:43 +01003082 mutex_lock(&ns->ctrl->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003083 nvme_mpath_clear_current_path(ns);
Keith Busch9941a862017-11-16 13:36:50 -07003084 list_del_rcu(&ns->siblings);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003085 mutex_unlock(&ns->ctrl->subsys->lock);
3086
Jianchao Wang765cc032018-02-12 20:54:46 +08003087 down_write(&ns->ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003088 list_del_init(&ns->list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003089 up_write(&ns->ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003090
Keith Busch9941a862017-11-16 13:36:50 -07003091 synchronize_srcu(&ns->head->srcu);
Sagi Grimberg479a3222017-12-21 15:07:27 +02003092 nvme_mpath_check_last_path(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003093 nvme_put_ns(ns);
3094}
3095
Keith Busch540c8012015-10-22 15:45:06 -06003096static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3097{
3098 struct nvme_ns *ns;
3099
Keith Busch32f0c4a2016-07-13 11:45:02 -06003100 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06003101 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003102 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06003103 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003104 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06003105 } else
3106 nvme_alloc_ns(ctrl, nsid);
3107}
3108
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303109static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3110 unsigned nsid)
3111{
3112 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003113 LIST_HEAD(rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303114
Jianchao Wang765cc032018-02-12 20:54:46 +08003115 down_write(&ctrl->namespaces_rwsem);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303116 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01003117 if (ns->head->ns_id > nsid)
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003118 list_move_tail(&ns->list, &rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303119 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003120 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003121
3122 list_for_each_entry_safe(ns, next, &rm_list, list)
3123 nvme_ns_remove(ns);
3124
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303125}
3126
Keith Busch540c8012015-10-22 15:45:06 -06003127static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3128{
3129 struct nvme_ns *ns;
3130 __le32 *ns_list;
3131 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
3132 int ret = 0;
3133
Minwoo Im42595eb2018-02-08 22:56:31 +09003134 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
Keith Busch540c8012015-10-22 15:45:06 -06003135 if (!ns_list)
3136 return -ENOMEM;
3137
3138 for (i = 0; i < num_lists; i++) {
3139 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3140 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303141 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06003142
3143 for (j = 0; j < min(nn, 1024U); j++) {
3144 nsid = le32_to_cpu(ns_list[j]);
3145 if (!nsid)
3146 goto out;
3147
3148 nvme_validate_ns(ctrl, nsid);
3149
3150 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06003151 ns = nvme_find_get_ns(ctrl, prev);
3152 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06003153 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003154 nvme_put_ns(ns);
3155 }
Keith Busch540c8012015-10-22 15:45:06 -06003156 }
3157 }
3158 nn -= j;
3159 }
3160 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303161 nvme_remove_invalid_namespaces(ctrl, prev);
3162 free:
Keith Busch540c8012015-10-22 15:45:06 -06003163 kfree(ns_list);
3164 return ret;
3165}
3166
Christoph Hellwig5955be22016-04-26 13:51:59 +02003167static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003168{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003169 unsigned i;
3170
Keith Busch540c8012015-10-22 15:45:06 -06003171 for (i = 1; i <= nn; i++)
3172 nvme_validate_ns(ctrl, i);
3173
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303174 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003175}
3176
Christoph Hellwig5955be22016-04-26 13:51:59 +02003177static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003178{
Christoph Hellwig5955be22016-04-26 13:51:59 +02003179 struct nvme_ctrl *ctrl =
3180 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003181 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06003182 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003183
Christoph Hellwig5955be22016-04-26 13:51:59 +02003184 if (ctrl->state != NVME_CTRL_LIVE)
3185 return;
3186
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003187 WARN_ON_ONCE(!ctrl->tagset);
3188
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003189 if (nvme_identify_ctrl(ctrl, &id))
3190 return;
Keith Busch540c8012015-10-22 15:45:06 -06003191
3192 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06003193 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06003194 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3195 if (!nvme_scan_ns_list(ctrl, nn))
3196 goto done;
3197 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02003198 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06003199 done:
Jianchao Wang765cc032018-02-12 20:54:46 +08003200 down_write(&ctrl->namespaces_rwsem);
Keith Busch540c8012015-10-22 15:45:06 -06003201 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Jianchao Wang765cc032018-02-12 20:54:46 +08003202 up_write(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003203 kfree(id);
3204}
Christoph Hellwig5955be22016-04-26 13:51:59 +02003205
3206void nvme_queue_scan(struct nvme_ctrl *ctrl)
3207{
3208 /*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003209 * Only new queue scan work when admin and IO queues are both alive
Christoph Hellwig5955be22016-04-26 13:51:59 +02003210 */
3211 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003212 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003213}
3214EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003215
Keith Busch32f0c4a2016-07-13 11:45:02 -06003216/*
3217 * This function iterates the namespace list unlocked to allow recovery from
3218 * controller failure. It is up to the caller to ensure the namespace list is
3219 * not modified by scan work while this function is executing.
3220 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003221void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3222{
3223 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003224 LIST_HEAD(ns_list);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003225
Keith Busch0ff9d4e2016-05-12 08:37:14 -06003226 /*
3227 * The dead states indicates the controller was not gracefully
3228 * disconnected. In that case, we won't be able to flush any data while
3229 * removing the namespaces' disks; fail all the queues now to avoid
3230 * potentially having to clean up the failed sync later.
3231 */
3232 if (ctrl->state == NVME_CTRL_DEAD)
3233 nvme_kill_queues(ctrl);
3234
Jianchao Wang765cc032018-02-12 20:54:46 +08003235 down_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003236 list_splice_init(&ctrl->namespaces, &ns_list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003237 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003238
3239 list_for_each_entry_safe(ns, next, &ns_list, list)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003240 nvme_ns_remove(ns);
3241}
Ming Lin576d55d2016-02-10 10:03:32 -08003242EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003243
Keith Busche3d78742017-11-07 15:13:14 -07003244static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3245{
3246 char *envp[2] = { NULL, NULL };
3247 u32 aen_result = ctrl->aen_result;
3248
3249 ctrl->aen_result = 0;
3250 if (!aen_result)
3251 return;
3252
3253 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3254 if (!envp[0])
3255 return;
3256 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3257 kfree(envp[0]);
3258}
3259
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003260static void nvme_async_event_work(struct work_struct *work)
3261{
3262 struct nvme_ctrl *ctrl =
3263 container_of(work, struct nvme_ctrl, async_event_work);
3264
Keith Busche3d78742017-11-07 15:13:14 -07003265 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07003266 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003267}
3268
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303269static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3270{
3271
3272 u32 csts;
3273
3274 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3275 return false;
3276
3277 if (csts == ~0)
3278 return false;
3279
3280 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3281}
3282
3283static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3284{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303285 struct nvme_fw_slot_info_log *log;
3286
3287 log = kmalloc(sizeof(*log), GFP_KERNEL);
3288 if (!log)
3289 return;
3290
Keith Buschc627c482017-11-07 10:28:31 -07003291 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303292 dev_warn(ctrl->device,
3293 "Get FW SLOT INFO log error\n");
3294 kfree(log);
3295}
3296
3297static void nvme_fw_act_work(struct work_struct *work)
3298{
3299 struct nvme_ctrl *ctrl = container_of(work,
3300 struct nvme_ctrl, fw_act_work);
3301 unsigned long fw_act_timeout;
3302
3303 if (ctrl->mtfa)
3304 fw_act_timeout = jiffies +
3305 msecs_to_jiffies(ctrl->mtfa * 100);
3306 else
3307 fw_act_timeout = jiffies +
3308 msecs_to_jiffies(admin_timeout * 1000);
3309
3310 nvme_stop_queues(ctrl);
3311 while (nvme_ctrl_pp_status(ctrl)) {
3312 if (time_after(jiffies, fw_act_timeout)) {
3313 dev_warn(ctrl->device,
3314 "Fw activation timeout, reset controller\n");
3315 nvme_reset_ctrl(ctrl);
3316 break;
3317 }
3318 msleep(100);
3319 }
3320
3321 if (ctrl->state != NVME_CTRL_LIVE)
3322 return;
3323
3324 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003325 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303326 nvme_get_fw_slot_info(ctrl);
3327}
3328
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003329void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3330 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003331{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003332 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003333
Keith Buschad22c352017-11-07 15:13:12 -07003334 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003335 return;
3336
Keith Busche3d78742017-11-07 15:13:14 -07003337 switch (result & 0x7) {
3338 case NVME_AER_ERROR:
3339 case NVME_AER_SMART:
3340 case NVME_AER_CSS:
3341 case NVME_AER_VS:
3342 ctrl->aen_result = result;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003343 break;
3344 default:
3345 break;
3346 }
3347
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003348 switch (result & 0xff07) {
3349 case NVME_AER_NOTICE_NS_CHANGED:
3350 dev_info(ctrl->device, "rescanning\n");
3351 nvme_queue_scan(ctrl);
3352 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303353 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003354 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303355 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003356 default:
3357 dev_warn(ctrl->device, "async event result %08x\n", result);
3358 }
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003359 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003360}
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003361EXPORT_SYMBOL_GPL(nvme_complete_async_event);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003362
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003363void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003364{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003365 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003366 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003367 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303368 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003369}
3370EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003371
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003372void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3373{
3374 if (ctrl->kato)
3375 nvme_start_keep_alive(ctrl);
3376
3377 if (ctrl->queue_count > 1) {
3378 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003379 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003380 nvme_start_queues(ctrl);
3381 }
3382}
3383EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3384
3385void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3386{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003387 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003388}
Ming Lin576d55d2016-02-10 10:03:32 -08003389EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003390
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003391static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003392{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003393 struct nvme_ctrl *ctrl =
3394 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003395 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003396
Christoph Hellwig9843f682017-10-18 13:10:01 +02003397 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003398 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003399
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003400 if (subsys) {
3401 mutex_lock(&subsys->lock);
3402 list_del(&ctrl->subsys_entry);
3403 mutex_unlock(&subsys->lock);
3404 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3405 }
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003406
3407 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003408
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003409 if (subsys)
3410 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003411}
3412
3413/*
3414 * Initialize a NVMe controller structures. This needs to be called during
3415 * earliest initialization so that we have the initialized structured around
3416 * during probing.
3417 */
3418int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3419 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3420{
3421 int ret;
3422
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003423 ctrl->state = NVME_CTRL_NEW;
3424 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003425 INIT_LIST_HEAD(&ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003426 init_rwsem(&ctrl->namespaces_rwsem);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003427 ctrl->dev = dev;
3428 ctrl->ops = ops;
3429 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003430 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003431 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303432 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003433 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003434
Christoph Hellwig9843f682017-10-18 13:10:01 +02003435 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3436 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003437 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003438 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003439
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003440 device_initialize(&ctrl->ctrl_device);
3441 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003442 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003443 ctrl->device->class = nvme_class;
3444 ctrl->device->parent = ctrl->dev;
3445 ctrl->device->groups = nvme_dev_attr_groups;
3446 ctrl->device->release = nvme_free_ctrl;
3447 dev_set_drvdata(ctrl->device, ctrl);
3448 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3449 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003450 goto out_release_instance;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003451
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003452 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3453 ctrl->cdev.owner = ops->module;
3454 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003455 if (ret)
3456 goto out_free_name;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003457
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003458 /*
3459 * Initialize latency tolerance controls. The sysfs files won't
3460 * be visible to userspace unless the device actually supports APST.
3461 */
3462 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3463 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3464 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3465
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003466 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003467out_free_name:
3468 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003469out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003470 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003471out:
3472 return ret;
3473}
Ming Lin576d55d2016-02-10 10:03:32 -08003474EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003475
Keith Busch69d9a992016-02-24 09:15:56 -07003476/**
3477 * nvme_kill_queues(): Ends all namespace queues
3478 * @ctrl: the dead controller that needs to end
3479 *
3480 * Call this function when the driver determines it is unable to get the
3481 * controller in a state capable of servicing IO.
3482 */
3483void nvme_kill_queues(struct nvme_ctrl *ctrl)
3484{
3485 struct nvme_ns *ns;
3486
Jianchao Wang765cc032018-02-12 20:54:46 +08003487 down_read(&ctrl->namespaces_rwsem);
Ming Lei82654b62017-06-02 16:32:08 +08003488
Ming Lei443bd902017-06-19 10:21:08 +08003489 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003490 if (ctrl->admin_q)
3491 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003492
Keith Busch32f0c4a2016-07-13 11:45:02 -06003493 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003494 /*
3495 * Revalidating a dead namespace sets capacity to 0. This will
3496 * end buffered writers dirtying pages that can't be synced.
3497 */
Keith Buschf33447b2017-02-10 18:15:51 -05003498 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3499 continue;
3500 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003501 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003502
Ming Lei443bd902017-06-19 10:21:08 +08003503 /* Forcibly unquiesce queues to avoid blocking dispatch */
3504 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003505 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003506 up_read(&ctrl->namespaces_rwsem);
Keith Busch69d9a992016-02-24 09:15:56 -07003507}
Linus Torvalds237045f2016-03-18 17:13:31 -07003508EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003509
Keith Busch302ad8c2017-03-01 14:22:12 -05003510void nvme_unfreeze(struct nvme_ctrl *ctrl)
3511{
3512 struct nvme_ns *ns;
3513
Jianchao Wang765cc032018-02-12 20:54:46 +08003514 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003515 list_for_each_entry(ns, &ctrl->namespaces, list)
3516 blk_mq_unfreeze_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003517 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003518}
3519EXPORT_SYMBOL_GPL(nvme_unfreeze);
3520
3521void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3522{
3523 struct nvme_ns *ns;
3524
Jianchao Wang765cc032018-02-12 20:54:46 +08003525 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003526 list_for_each_entry(ns, &ctrl->namespaces, list) {
3527 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3528 if (timeout <= 0)
3529 break;
3530 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003531 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003532}
3533EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3534
3535void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3536{
3537 struct nvme_ns *ns;
3538
Jianchao Wang765cc032018-02-12 20:54:46 +08003539 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003540 list_for_each_entry(ns, &ctrl->namespaces, list)
3541 blk_mq_freeze_queue_wait(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003542 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003543}
3544EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3545
3546void nvme_start_freeze(struct nvme_ctrl *ctrl)
3547{
3548 struct nvme_ns *ns;
3549
Jianchao Wang765cc032018-02-12 20:54:46 +08003550 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003551 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003552 blk_freeze_queue_start(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003553 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003554}
3555EXPORT_SYMBOL_GPL(nvme_start_freeze);
3556
Keith Busch25646262016-01-04 09:10:57 -07003557void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003558{
3559 struct nvme_ns *ns;
3560
Jianchao Wang765cc032018-02-12 20:54:46 +08003561 down_read(&ctrl->namespaces_rwsem);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003562 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003563 blk_mq_quiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003564 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003565}
Ming Lin576d55d2016-02-10 10:03:32 -08003566EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003567
Keith Busch25646262016-01-04 09:10:57 -07003568void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003569{
3570 struct nvme_ns *ns;
3571
Jianchao Wang765cc032018-02-12 20:54:46 +08003572 down_read(&ctrl->namespaces_rwsem);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003573 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003574 blk_mq_unquiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003575 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003576}
Ming Lin576d55d2016-02-10 10:03:32 -08003577EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003578
Sagi Grimberg31b84462017-10-11 12:53:07 +03003579int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3580{
3581 if (!ctrl->ops->reinit_request)
3582 return 0;
3583
3584 return blk_mq_tagset_iter(set, set->driver_data,
3585 ctrl->ops->reinit_request);
3586}
3587EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3588
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003589int __init nvme_core_init(void)
3590{
Roy Shtermanb227c592018-01-14 12:39:02 +02003591 int result = -ENOMEM;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003592
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003593 nvme_wq = alloc_workqueue("nvme-wq",
3594 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3595 if (!nvme_wq)
Roy Shtermanb227c592018-01-14 12:39:02 +02003596 goto out;
3597
3598 nvme_reset_wq = alloc_workqueue("nvme-reset-wq",
3599 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3600 if (!nvme_reset_wq)
3601 goto destroy_wq;
3602
3603 nvme_delete_wq = alloc_workqueue("nvme-delete-wq",
3604 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3605 if (!nvme_delete_wq)
3606 goto destroy_reset_wq;
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003607
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003608 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003609 if (result < 0)
Roy Shtermanb227c592018-01-14 12:39:02 +02003610 goto destroy_delete_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003611
3612 nvme_class = class_create(THIS_MODULE, "nvme");
3613 if (IS_ERR(nvme_class)) {
3614 result = PTR_ERR(nvme_class);
3615 goto unregister_chrdev;
3616 }
3617
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003618 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3619 if (IS_ERR(nvme_subsys_class)) {
3620 result = PTR_ERR(nvme_subsys_class);
3621 goto destroy_class;
3622 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003623 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003624
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003625destroy_class:
3626 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003627unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003628 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003629destroy_delete_wq:
3630 destroy_workqueue(nvme_delete_wq);
3631destroy_reset_wq:
3632 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003633destroy_wq:
3634 destroy_workqueue(nvme_wq);
Roy Shtermanb227c592018-01-14 12:39:02 +02003635out:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003636 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003637}
3638
3639void nvme_core_exit(void)
3640{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003641 ida_destroy(&nvme_subsystems_ida);
3642 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003643 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003644 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003645 destroy_workqueue(nvme_delete_wq);
3646 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003647 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003648}
Ming Lin576d55d2016-02-10 10:03:32 -08003649
3650MODULE_LICENSE("GPL");
3651MODULE_VERSION("1.0");
3652module_init(nvme_core_init);
3653module_exit(nvme_core_exit);