blob: 9df4f71e58caa0f93e8a6175cb48ec8d78f164e0 [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
Max Gurtovoy77d06122018-03-11 17:46:06 +0200133 dev_info(ctrl->device,
134 "Removing ctrl: NQN \"%s\"\n", ctrl->opts->subsysnqn);
135
Sagi Grimberg40546372017-10-29 14:21:02 +0200136 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200137 nvme_stop_ctrl(ctrl);
138 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200139 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200140 nvme_uninit_ctrl(ctrl);
141 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200142}
143
144int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
145{
146 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
147 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200148 if (!queue_work(nvme_delete_wq, &ctrl->delete_work))
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200149 return -EBUSY;
150 return 0;
151}
152EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
153
154int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
155{
156 int ret = 0;
157
158 /*
159 * Keep a reference until the work is flushed since ->delete_ctrl
160 * can free the controller.
161 */
162 nvme_get_ctrl(ctrl);
163 ret = nvme_delete_ctrl(ctrl);
164 if (!ret)
165 flush_work(&ctrl->delete_work);
166 nvme_put_ctrl(ctrl);
167 return ret;
168}
169EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
170
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100171static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
172{
173 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
174}
175
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200176static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200177{
178 switch (nvme_req(req)->status & 0x7ff) {
179 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200180 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200181 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200182 return BLK_STS_NOSPC;
Keith Busche96fef22018-01-09 12:04:14 -0700183 case NVME_SC_LBA_RANGE:
184 return BLK_STS_TARGET;
185 case NVME_SC_BAD_ATTRIBUTES:
Junxiong Guane02ab022017-04-21 12:59:07 +0200186 case NVME_SC_ONCS_NOT_SUPPORTED:
Keith Busche96fef22018-01-09 12:04:14 -0700187 case NVME_SC_INVALID_OPCODE:
188 case NVME_SC_INVALID_FIELD:
189 case NVME_SC_INVALID_NS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200190 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200191 case NVME_SC_WRITE_FAULT:
192 case NVME_SC_READ_ERROR:
193 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200194 case NVME_SC_ACCESS_DENIED:
195 case NVME_SC_READ_ONLY:
Keith Busche96fef22018-01-09 12:04:14 -0700196 case NVME_SC_COMPARE_FAILED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200197 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200198 case NVME_SC_GUARD_CHECK:
199 case NVME_SC_APPTAG_CHECK:
200 case NVME_SC_REFTAG_CHECK:
201 case NVME_SC_INVALID_PI:
202 return BLK_STS_PROTECTION;
203 case NVME_SC_RESERVATION_CONFLICT:
204 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200205 default:
206 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200207 }
208}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200209
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200210static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200211{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200212 if (blk_noretry_request(req))
213 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200214 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200215 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200216 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200217 return false;
218 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200219}
220
221void nvme_complete_rq(struct request *req)
222{
Keith Busch908e4562018-01-09 12:04:15 -0700223 blk_status_t status = nvme_error_status(req);
224
Johannes Thumshirnca5554a2018-01-26 11:21:38 +0100225 trace_nvme_complete_rq(req);
226
Keith Busch908e4562018-01-09 12:04:15 -0700227 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
228 if (nvme_req_needs_failover(req, status)) {
Christoph Hellwig32acab32017-11-02 12:59:30 +0100229 nvme_failover_req(req);
230 return;
231 }
232
233 if (!blk_queue_dying(req->q)) {
234 nvme_req(req)->retries++;
235 blk_mq_requeue_request(req, true);
236 return;
237 }
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200238 }
Keith Busch908e4562018-01-09 12:04:15 -0700239 blk_mq_end_request(req, status);
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200240}
241EXPORT_SYMBOL_GPL(nvme_complete_rq);
242
Ming Linc55a2fd2016-05-18 14:05:02 -0700243void nvme_cancel_request(struct request *req, void *data, bool reserved)
244{
Ming Linc55a2fd2016-05-18 14:05:02 -0700245 if (!blk_mq_request_started(req))
246 return;
247
248 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
249 "Cancelling I/O %d", req->tag);
250
Christoph Hellwige54b0642017-11-02 21:28:51 +0300251 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200252 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200253
Ming Linc55a2fd2016-05-18 14:05:02 -0700254}
255EXPORT_SYMBOL_GPL(nvme_cancel_request);
256
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200257bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
258 enum nvme_ctrl_state new_state)
259{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300260 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200261 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200262 bool changed = false;
263
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200264 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300265
266 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200267 switch (new_state) {
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800268 case NVME_CTRL_ADMIN_ONLY:
269 switch (old_state) {
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200270 case NVME_CTRL_CONNECTING:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800271 changed = true;
272 /* FALLTHRU */
273 default:
274 break;
275 }
276 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200277 case NVME_CTRL_LIVE:
278 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200279 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200280 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200281 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200282 changed = true;
283 /* FALLTHRU */
284 default:
285 break;
286 }
287 break;
288 case NVME_CTRL_RESETTING:
289 switch (old_state) {
290 case NVME_CTRL_NEW:
291 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800292 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900293 changed = true;
294 /* FALLTHRU */
295 default:
296 break;
297 }
298 break;
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200299 case NVME_CTRL_CONNECTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900300 switch (old_state) {
Max Gurtovoyb754a322018-01-31 18:31:25 +0200301 case NVME_CTRL_NEW:
James Smart3cec7f92017-10-25 16:43:13 -0700302 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200303 changed = true;
304 /* FALLTHRU */
305 default:
306 break;
307 }
308 break;
309 case NVME_CTRL_DELETING:
310 switch (old_state) {
311 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800312 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200313 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200314 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200315 changed = true;
316 /* FALLTHRU */
317 default:
318 break;
319 }
320 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600321 case NVME_CTRL_DEAD:
322 switch (old_state) {
323 case NVME_CTRL_DELETING:
324 changed = true;
325 /* FALLTHRU */
326 default:
327 break;
328 }
329 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200330 default:
331 break;
332 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200333
334 if (changed)
335 ctrl->state = new_state;
336
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200337 spin_unlock_irqrestore(&ctrl->lock, flags);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100338 if (changed && ctrl->state == NVME_CTRL_LIVE)
339 nvme_kick_requeue_lists(ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200340 return changed;
341}
342EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
343
Christoph Hellwiged754e52017-11-09 13:50:43 +0100344static void nvme_free_ns_head(struct kref *ref)
345{
346 struct nvme_ns_head *head =
347 container_of(ref, struct nvme_ns_head, ref);
348
Christoph Hellwig32acab32017-11-02 12:59:30 +0100349 nvme_mpath_remove_disk(head);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100350 ida_simple_remove(&head->subsys->ns_ida, head->instance);
351 list_del_init(&head->entry);
352 cleanup_srcu_struct(&head->srcu);
353 kfree(head);
354}
355
356static void nvme_put_ns_head(struct nvme_ns_head *head)
357{
358 kref_put(&head->ref, nvme_free_ns_head);
359}
360
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100361static void nvme_free_ns(struct kref *kref)
362{
363 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
364
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200365 if (ns->ndev)
366 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100367
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100368 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100369 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700370 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100371 kfree(ns);
372}
373
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100374static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100375{
376 kref_put(&ns->kref, nvme_free_ns);
377}
378
James Smartbb06ec312018-04-12 09:16:15 -0600379static inline void nvme_clear_nvme_request(struct request *req)
380{
381 if (!(req->rq_flags & RQF_DONTPREP)) {
382 nvme_req(req)->retries = 0;
383 nvme_req(req)->flags = 0;
384 req->rq_flags |= RQF_DONTPREP;
385 }
386}
387
Christoph Hellwig41609822015-11-20 09:00:02 +0100388struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800389 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100390{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100391 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100392 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100393
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200394 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100395 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200396 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100397 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200398 qid ? qid - 1 : 0);
399 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100400 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100401 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100402
Christoph Hellwig21d34712015-11-26 09:08:36 +0100403 req->cmd_flags |= REQ_FAILFAST_DRIVER;
James Smartbb06ec312018-04-12 09:16:15 -0600404 nvme_clear_nvme_request(req);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800405 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100406
Christoph Hellwig41609822015-11-20 09:00:02 +0100407 return req;
408}
Ming Lin576d55d2016-02-10 10:03:32 -0800409EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100410
Jens Axboef5d11842017-06-27 12:03:06 -0600411static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
412{
413 struct nvme_command c;
414
415 memset(&c, 0, sizeof(c));
416
417 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530418 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600419 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
420 c.directive.dtype = NVME_DIR_IDENTIFY;
421 c.directive.tdtype = NVME_DIR_STREAMS;
422 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
423
424 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
425}
426
427static int nvme_disable_streams(struct nvme_ctrl *ctrl)
428{
429 return nvme_toggle_streams(ctrl, false);
430}
431
432static int nvme_enable_streams(struct nvme_ctrl *ctrl)
433{
434 return nvme_toggle_streams(ctrl, true);
435}
436
437static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
438 struct streams_directive_params *s, u32 nsid)
439{
440 struct nvme_command c;
441
442 memset(&c, 0, sizeof(c));
443 memset(s, 0, sizeof(*s));
444
445 c.directive.opcode = nvme_admin_directive_recv;
446 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700447 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600448 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
449 c.directive.dtype = NVME_DIR_STREAMS;
450
451 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
452}
453
454static int nvme_configure_directives(struct nvme_ctrl *ctrl)
455{
456 struct streams_directive_params s;
457 int ret;
458
459 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
460 return 0;
461 if (!streams)
462 return 0;
463
464 ret = nvme_enable_streams(ctrl);
465 if (ret)
466 return ret;
467
Arnav Dawn62346ea2017-07-12 16:11:53 +0530468 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600469 if (ret)
470 return ret;
471
472 ctrl->nssa = le16_to_cpu(s.nssa);
473 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
474 dev_info(ctrl->device, "too few streams (%u) available\n",
475 ctrl->nssa);
476 nvme_disable_streams(ctrl);
477 return 0;
478 }
479
480 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
481 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
482 return 0;
483}
484
485/*
486 * Check if 'req' has a write hint associated with it. If it does, assign
487 * a valid namespace stream to the write.
488 */
489static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
490 struct request *req, u16 *control,
491 u32 *dsmgmt)
492{
493 enum rw_hint streamid = req->write_hint;
494
495 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
496 streamid = 0;
497 else {
498 streamid--;
499 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
500 return;
501
502 *control |= NVME_RW_DTYPE_STREAMS;
503 *dsmgmt |= streamid << 16;
504 }
505
506 if (streamid < ARRAY_SIZE(req->q->write_hints))
507 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
508}
509
Ming Lin8093f7c2016-04-12 13:10:14 -0600510static inline void nvme_setup_flush(struct nvme_ns *ns,
511 struct nvme_command *cmnd)
512{
513 memset(cmnd, 0, sizeof(*cmnd));
514 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100515 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600516}
517
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200518static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600519 struct nvme_command *cmnd)
520{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100521 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600522 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100523 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600524
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100525 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600526 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200527 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600528
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100529 __rq_for_each_bio(bio, req) {
530 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
531 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
532
Keith Busch8cb6af72018-01-31 17:01:58 -0700533 if (n < segments) {
534 range[n].cattr = cpu_to_le32(0);
535 range[n].nlb = cpu_to_le32(nlb);
536 range[n].slba = cpu_to_le64(slba);
537 }
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100538 n++;
539 }
540
541 if (WARN_ON_ONCE(n != segments)) {
542 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200543 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100544 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600545
546 memset(cmnd, 0, sizeof(*cmnd));
547 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100548 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200549 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600550 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
551
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700552 req->special_vec.bv_page = virt_to_page(range);
553 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100554 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700555 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600556
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200557 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600558}
Ming Lin8093f7c2016-04-12 13:10:14 -0600559
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200560static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
561 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600562{
Jens Axboef5d11842017-06-27 12:03:06 -0600563 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600564 u16 control = 0;
565 u32 dsmgmt = 0;
566
567 if (req->cmd_flags & REQ_FUA)
568 control |= NVME_RW_FUA;
569 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
570 control |= NVME_RW_LR;
571
572 if (req->cmd_flags & REQ_RAHEAD)
573 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
574
575 memset(cmnd, 0, sizeof(*cmnd));
576 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100577 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600578 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
579 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
580
Jens Axboef5d11842017-06-27 12:03:06 -0600581 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
582 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
583
Ming Lin8093f7c2016-04-12 13:10:14 -0600584 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100585 /*
586 * If formated with metadata, the block layer always provides a
587 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
588 * we enable the PRACT bit for protection information or set the
589 * namespace capacity to zero to prevent any I/O.
590 */
591 if (!blk_integrity_rq(req)) {
592 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
593 return BLK_STS_NOTSUPP;
594 control |= NVME_RW_PRINFO_PRACT;
595 }
596
Ming Lin8093f7c2016-04-12 13:10:14 -0600597 switch (ns->pi_type) {
598 case NVME_NS_DPS_PI_TYPE3:
599 control |= NVME_RW_PRINFO_PRCHK_GUARD;
600 break;
601 case NVME_NS_DPS_PI_TYPE1:
602 case NVME_NS_DPS_PI_TYPE2:
603 control |= NVME_RW_PRINFO_PRCHK_GUARD |
604 NVME_RW_PRINFO_PRCHK_REF;
605 cmnd->rw.reftag = cpu_to_le32(
606 nvme_block_nr(ns, blk_rq_pos(req)));
607 break;
608 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600609 }
610
611 cmnd->rw.control = cpu_to_le16(control);
612 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200613 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600614}
615
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200616blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600617 struct nvme_command *cmd)
618{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200619 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600620
James Smartbb06ec312018-04-12 09:16:15 -0600621 nvme_clear_nvme_request(req);
Christoph Hellwig987f6992017-04-05 19:18:08 +0200622
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100623 switch (req_op(req)) {
624 case REQ_OP_DRV_IN:
625 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800626 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100627 break;
628 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600629 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100630 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200631 case REQ_OP_WRITE_ZEROES:
632 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100633 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600634 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100635 break;
636 case REQ_OP_READ:
637 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200638 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100639 break;
640 default:
641 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200642 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100643 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600644
James Smart721b3912016-10-21 23:33:34 +0300645 cmd->common.command_id = req->tag;
Johannes Thumshirn3d030e42018-01-26 11:21:37 +0100646 if (ns)
647 trace_nvme_setup_nvm_cmd(req->q->id, cmd);
648 else
649 trace_nvme_setup_admin_cmd(cmd);
Ming Lin8093f7c2016-04-12 13:10:14 -0600650 return ret;
651}
652EXPORT_SYMBOL_GPL(nvme_setup_cmd);
653
Christoph Hellwig41609822015-11-20 09:00:02 +0100654/*
655 * Returns 0 on success. If the result is negative, it's a Linux error code;
656 * if the result is positive, it's an NVM Express status code
657 */
658int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800659 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800660 unsigned timeout, int qid, int at_head,
661 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100662{
663 struct request *req;
664 int ret;
665
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200666 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100667 if (IS_ERR(req))
668 return PTR_ERR(req);
669
670 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
671
Christoph Hellwig21d34712015-11-26 09:08:36 +0100672 if (buffer && bufflen) {
673 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
674 if (ret)
675 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100676 }
677
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200678 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800679 if (result)
680 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200681 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
682 ret = -EINTR;
683 else
684 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100685 out:
686 blk_mq_free_request(req);
687 return ret;
688}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200689EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100690
691int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
692 void *buffer, unsigned bufflen)
693{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200694 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
695 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100696}
Ming Lin576d55d2016-02-10 10:03:32 -0800697EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100698
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400699static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
700 unsigned len, u32 seed, bool write)
701{
702 struct bio_integrity_payload *bip;
703 int ret = -ENOMEM;
704 void *buf;
705
706 buf = kmalloc(len, GFP_KERNEL);
707 if (!buf)
708 goto out;
709
710 ret = -EFAULT;
711 if (write && copy_from_user(buf, ubuf, len))
712 goto out_free_meta;
713
714 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
715 if (IS_ERR(bip)) {
716 ret = PTR_ERR(bip);
717 goto out_free_meta;
718 }
719
720 bip->bip_iter.bi_size = len;
721 bip->bip_iter.bi_sector = seed;
722 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
723 offset_in_page(buf));
724 if (ret == len)
725 return buf;
726 ret = -ENOMEM;
727out_free_meta:
728 kfree(buf);
729out:
730 return ERR_PTR(ret);
731}
732
Keith Busch63263d62017-08-29 17:46:04 -0400733static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400734 struct nvme_command *cmd, void __user *ubuffer,
735 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
736 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100737{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200738 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600739 struct nvme_ns *ns = q->queuedata;
740 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100741 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600742 struct bio *bio = NULL;
743 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100744 int ret;
745
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200746 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100747 if (IS_ERR(req))
748 return PTR_ERR(req);
749
750 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
James Smartbb06ec312018-04-12 09:16:15 -0600751 nvme_req(req)->flags |= NVME_REQ_USERCMD;
Christoph Hellwig41609822015-11-20 09:00:02 +0100752
753 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100754 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
755 GFP_KERNEL);
756 if (ret)
757 goto out;
758 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200759 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400760 if (disk && meta_buffer && meta_len) {
761 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
762 meta_seed, write);
763 if (IS_ERR(meta)) {
764 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600765 goto out_unmap;
766 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600767 }
768 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400769
Keith Busch0b7f1f22015-10-23 09:47:28 -0600770 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200771 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
772 ret = -EINTR;
773 else
774 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100775 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800776 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600777 if (meta && !ret && !write) {
778 if (copy_to_user(meta_buffer, meta, meta_len))
779 ret = -EFAULT;
780 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600781 kfree(meta);
782 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200783 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600784 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100785 out:
786 blk_mq_free_request(req);
787 return ret;
788}
789
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200790static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200791{
792 struct nvme_ctrl *ctrl = rq->end_io_data;
793
794 blk_mq_free_request(rq);
795
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200796 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200797 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200798 "failed nvme_keep_alive_end_io error=%d\n",
799 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200800 return;
801 }
802
803 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
804}
805
806static int nvme_keep_alive(struct nvme_ctrl *ctrl)
807{
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200808 struct request *rq;
809
Roland Dreier0a34e462018-01-11 13:38:15 -0800810 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200811 NVME_QID_ANY);
812 if (IS_ERR(rq))
813 return PTR_ERR(rq);
814
815 rq->timeout = ctrl->kato * HZ;
816 rq->end_io_data = ctrl;
817
818 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
819
820 return 0;
821}
822
823static void nvme_keep_alive_work(struct work_struct *work)
824{
825 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
826 struct nvme_ctrl, ka_work);
827
828 if (nvme_keep_alive(ctrl)) {
829 /* allocation failure, reset the controller */
830 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200831 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200832 return;
833 }
834}
835
Johannes Thumshirn00b683d2018-04-12 09:16:05 -0600836static void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200837{
838 if (unlikely(ctrl->kato == 0))
839 return;
840
841 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
Roland Dreier0a34e462018-01-11 13:38:15 -0800842 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd));
843 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200844 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
845}
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200846
847void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
848{
849 if (unlikely(ctrl->kato == 0))
850 return;
851
852 cancel_delayed_work_sync(&ctrl->ka_work);
853}
854EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
855
Keith Busch3f7f25a92017-06-20 15:09:56 -0400856static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100857{
858 struct nvme_command c = { };
859 int error;
860
861 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
862 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200863 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100864
865 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
866 if (!*id)
867 return -ENOMEM;
868
869 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
870 sizeof(struct nvme_id_ctrl));
871 if (error)
872 kfree(*id);
873 return error;
874}
875
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200876static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100877 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200878{
879 struct nvme_command c = { };
880 int status;
881 void *data;
882 int pos;
883 int len;
884
885 c.identify.opcode = nvme_admin_identify;
886 c.identify.nsid = cpu_to_le32(nsid);
887 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
888
889 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
890 if (!data)
891 return -ENOMEM;
892
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200893 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200894 NVME_IDENTIFY_DATA_SIZE);
895 if (status)
896 goto free_data;
897
898 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
899 struct nvme_ns_id_desc *cur = data + pos;
900
901 if (cur->nidl == 0)
902 break;
903
904 switch (cur->nidt) {
905 case NVME_NIDT_EUI64:
906 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200907 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200908 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
909 cur->nidl);
910 goto free_data;
911 }
912 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100913 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200914 break;
915 case NVME_NIDT_NGUID:
916 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200917 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200918 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
919 cur->nidl);
920 goto free_data;
921 }
922 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100923 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200924 break;
925 case NVME_NIDT_UUID:
926 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200927 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200928 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
929 cur->nidl);
930 goto free_data;
931 }
932 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100933 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200934 break;
935 default:
936 /* Skip unnkown types */
937 len = cur->nidl;
938 break;
939 }
940
941 len += sizeof(*cur);
942 }
943free_data:
944 kfree(data);
945 return status;
946}
947
Keith Busch540c8012015-10-22 15:45:06 -0600948static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
949{
950 struct nvme_command c = { };
951
952 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200953 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600954 c.identify.nsid = cpu_to_le32(nsid);
Minwoo Im42595eb2018-02-08 22:56:31 +0900955 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list,
956 NVME_IDENTIFY_DATA_SIZE);
Keith Busch540c8012015-10-22 15:45:06 -0600957}
958
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200959static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
960 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100961{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200962 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100963 struct nvme_command c = { };
964 int error;
965
966 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200967 c.identify.opcode = nvme_admin_identify;
968 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200969 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100970
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200971 id = kmalloc(sizeof(*id), GFP_KERNEL);
972 if (!id)
973 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100974
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200975 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
976 if (error) {
977 dev_warn(ctrl->device, "Identify namespace failed\n");
978 kfree(id);
979 return NULL;
980 }
981
982 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100983}
984
Keith Busch3f7f25a92017-06-20 15:09:56 -0400985static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700986 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100987{
988 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800989 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100990 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100991
992 memset(&c, 0, sizeof(c));
993 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100994 c.features.fid = cpu_to_le32(fid);
995 c.features.dword11 = cpu_to_le32(dword11);
996
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800997 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700998 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700999 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -08001000 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +01001001 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01001002}
1003
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001004int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
1005{
1006 u32 q_count = (*count - 1) | ((*count - 1) << 16);
1007 u32 result;
1008 int status, nr_io_queues;
1009
Andy Lutomirski1a6fe742016-09-16 11:16:10 -07001010 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001011 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001012 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001013 return status;
1014
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001015 /*
1016 * Degraded controllers might return an error when setting the queue
1017 * count. We still want to be able to bring them online and offer
1018 * access to the admin queue, as that might be only way to fix them up.
1019 */
1020 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001021 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001022 *count = 0;
1023 } else {
1024 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
1025 *count = min(*count, nr_io_queues);
1026 }
1027
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001028 return 0;
1029}
Ming Lin576d55d2016-02-10 10:03:32 -08001030EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001031
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001032static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1033{
1034 struct nvme_user_io io;
1035 struct nvme_command c;
1036 unsigned length, meta_len;
1037 void __user *metadata;
1038
1039 if (copy_from_user(&io, uio, sizeof(io)))
1040 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001041 if (io.flags)
1042 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001043
1044 switch (io.opcode) {
1045 case nvme_cmd_write:
1046 case nvme_cmd_read:
1047 case nvme_cmd_compare:
1048 break;
1049 default:
1050 return -EINVAL;
1051 }
1052
1053 length = (io.nblocks + 1) << ns->lba_shift;
1054 meta_len = (io.nblocks + 1) * ns->ms;
1055 metadata = (void __user *)(uintptr_t)io.metadata;
1056
1057 if (ns->ext) {
1058 length += meta_len;
1059 meta_len = 0;
1060 } else if (meta_len) {
1061 if ((io.metadata & 3) || !io.metadata)
1062 return -EINVAL;
1063 }
1064
1065 memset(&c, 0, sizeof(c));
1066 c.rw.opcode = io.opcode;
1067 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001068 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001069 c.rw.slba = cpu_to_le64(io.slba);
1070 c.rw.length = cpu_to_le16(io.nblocks);
1071 c.rw.control = cpu_to_le16(io.control);
1072 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1073 c.rw.reftag = cpu_to_le32(io.reftag);
1074 c.rw.apptag = cpu_to_le16(io.apptag);
1075 c.rw.appmask = cpu_to_le16(io.appmask);
1076
Keith Busch63263d62017-08-29 17:46:04 -04001077 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001078 (void __user *)(uintptr_t)io.addr, length,
1079 metadata, meta_len, io.slba, NULL, 0);
1080}
1081
Keith Busch84fef622017-11-07 10:28:32 -07001082static u32 nvme_known_admin_effects(u8 opcode)
1083{
1084 switch (opcode) {
1085 case nvme_admin_format_nvm:
1086 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1087 NVME_CMD_EFFECTS_CSE_MASK;
1088 case nvme_admin_sanitize_nvm:
1089 return NVME_CMD_EFFECTS_CSE_MASK;
1090 default:
1091 break;
1092 }
1093 return 0;
1094}
1095
1096static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1097 u8 opcode)
1098{
1099 u32 effects = 0;
1100
1101 if (ns) {
1102 if (ctrl->effects)
1103 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1104 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1105 dev_warn(ctrl->device,
1106 "IO command:%02x has unhandled effects:%08x\n",
1107 opcode, effects);
1108 return 0;
1109 }
1110
1111 if (ctrl->effects)
Keith Busch62843c22018-04-12 09:16:14 -06001112 effects = le32_to_cpu(ctrl->effects->acs[opcode]);
Keith Busch84fef622017-11-07 10:28:32 -07001113 else
1114 effects = nvme_known_admin_effects(opcode);
1115
1116 /*
1117 * For simplicity, IO to all namespaces is quiesced even if the command
1118 * effects say only one namespace is affected.
1119 */
1120 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1121 nvme_start_freeze(ctrl);
1122 nvme_wait_freeze(ctrl);
1123 }
1124 return effects;
1125}
1126
1127static void nvme_update_formats(struct nvme_ctrl *ctrl)
1128{
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001129 struct nvme_ns *ns, *next;
1130 LIST_HEAD(rm_list);
Keith Busch84fef622017-11-07 10:28:32 -07001131
Jianchao Wang765cc032018-02-12 20:54:46 +08001132 down_write(&ctrl->namespaces_rwsem);
Keith Busch84fef622017-11-07 10:28:32 -07001133 list_for_each_entry(ns, &ctrl->namespaces, list) {
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001134 if (ns->disk && nvme_revalidate_disk(ns->disk)) {
1135 list_move_tail(&ns->list, &rm_list);
1136 }
Keith Busch84fef622017-11-07 10:28:32 -07001137 }
Jianchao Wang765cc032018-02-12 20:54:46 +08001138 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001139
1140 list_for_each_entry_safe(ns, next, &rm_list, list)
1141 nvme_ns_remove(ns);
Keith Busch84fef622017-11-07 10:28:32 -07001142}
1143
1144static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1145{
1146 /*
1147 * Revalidate LBA changes prior to unfreezing. This is necessary to
1148 * prevent memory corruption if a logical block size was changed by
1149 * this command.
1150 */
1151 if (effects & NVME_CMD_EFFECTS_LBCC)
1152 nvme_update_formats(ctrl);
1153 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1154 nvme_unfreeze(ctrl);
1155 if (effects & NVME_CMD_EFFECTS_CCC)
1156 nvme_init_identify(ctrl);
1157 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1158 nvme_queue_scan(ctrl);
1159}
1160
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001161static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001162 struct nvme_passthru_cmd __user *ucmd)
1163{
1164 struct nvme_passthru_cmd cmd;
1165 struct nvme_command c;
1166 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001167 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001168 int status;
1169
1170 if (!capable(CAP_SYS_ADMIN))
1171 return -EACCES;
1172 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1173 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001174 if (cmd.flags)
1175 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001176
1177 memset(&c, 0, sizeof(c));
1178 c.common.opcode = cmd.opcode;
1179 c.common.flags = cmd.flags;
1180 c.common.nsid = cpu_to_le32(cmd.nsid);
1181 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1182 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1183 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1184 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1185 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1186 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1187 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1188 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1189
1190 if (cmd.timeout_ms)
1191 timeout = msecs_to_jiffies(cmd.timeout_ms);
1192
Keith Busch84fef622017-11-07 10:28:32 -07001193 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001194 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001195 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001196 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1197 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001198 nvme_passthru_end(ctrl, effects);
1199
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001200 if (status >= 0) {
1201 if (put_user(cmd.result, &ucmd->result))
1202 return -EFAULT;
1203 }
1204
1205 return status;
1206}
1207
Christoph Hellwig32acab32017-11-02 12:59:30 +01001208/*
1209 * Issue ioctl requests on the first available path. Note that unlike normal
1210 * block layer requests we will not retry failed request on another controller.
1211 */
1212static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1213 struct nvme_ns_head **head, int *srcu_idx)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001214{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001215#ifdef CONFIG_NVME_MULTIPATH
1216 if (disk->fops == &nvme_ns_head_ops) {
1217 *head = disk->private_data;
1218 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1219 return nvme_find_path(*head);
1220 }
1221#endif
1222 *head = NULL;
1223 *srcu_idx = -1;
1224 return disk->private_data;
1225}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001226
Christoph Hellwig32acab32017-11-02 12:59:30 +01001227static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1228{
1229 if (head)
1230 srcu_read_unlock(&head->srcu, idx);
1231}
1232
1233static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
1234{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001235 switch (cmd) {
1236 case NVME_IOCTL_ID:
1237 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001238 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001239 case NVME_IOCTL_ADMIN_CMD:
1240 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1241 case NVME_IOCTL_IO_CMD:
1242 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1243 case NVME_IOCTL_SUBMIT_IO:
1244 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001245 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001246#ifdef CONFIG_NVM
1247 if (ns->ndev)
1248 return nvme_nvm_ioctl(ns, cmd, arg);
1249#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001250 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001251 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001252 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001253 return -ENOTTY;
1254 }
1255}
1256
Christoph Hellwig32acab32017-11-02 12:59:30 +01001257static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1258 unsigned int cmd, unsigned long arg)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001259{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001260 struct nvme_ns_head *head = NULL;
1261 struct nvme_ns *ns;
1262 int srcu_idx, ret;
1263
1264 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1265 if (unlikely(!ns))
1266 ret = -EWOULDBLOCK;
1267 else
1268 ret = nvme_ns_ioctl(ns, cmd, arg);
1269 nvme_put_ns_from_disk(head, srcu_idx);
1270 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001271}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001272
1273static int nvme_open(struct block_device *bdev, fmode_t mode)
1274{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001275 struct nvme_ns *ns = bdev->bd_disk->private_data;
1276
Christoph Hellwig32acab32017-11-02 12:59:30 +01001277#ifdef CONFIG_NVME_MULTIPATH
1278 /* should never be called due to GENHD_FL_HIDDEN */
1279 if (WARN_ON_ONCE(ns->head->disk))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001280 goto fail;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001281#endif
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001282 if (!kref_get_unless_zero(&ns->kref))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001283 goto fail;
1284 if (!try_module_get(ns->ctrl->ops->module))
1285 goto fail_put_ns;
1286
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001287 return 0;
Nitzan Carmi85088c42018-01-04 17:56:13 +02001288
1289fail_put_ns:
1290 nvme_put_ns(ns);
1291fail:
1292 return -ENXIO;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001293}
1294
1295static void nvme_release(struct gendisk *disk, fmode_t mode)
1296{
Nitzan Carmi85088c42018-01-04 17:56:13 +02001297 struct nvme_ns *ns = disk->private_data;
1298
1299 module_put(ns->ctrl->ops->module);
1300 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001301}
1302
1303static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1304{
1305 /* some standard values */
1306 geo->heads = 1 << 6;
1307 geo->sectors = 1 << 5;
1308 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1309 return 0;
1310}
1311
1312#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001313static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001314{
1315 struct blk_integrity integrity;
1316
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001317 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001318 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001319 case NVME_NS_DPS_PI_TYPE3:
1320 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001321 integrity.tag_size = sizeof(u16) + sizeof(u32);
1322 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001323 break;
1324 case NVME_NS_DPS_PI_TYPE1:
1325 case NVME_NS_DPS_PI_TYPE2:
1326 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001327 integrity.tag_size = sizeof(u16);
1328 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001329 break;
1330 default:
1331 integrity.profile = NULL;
1332 break;
1333 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001334 integrity.tuple_size = ms;
1335 blk_integrity_register(disk, &integrity);
1336 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001337}
1338#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001339static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001340{
1341}
1342#endif /* CONFIG_BLK_DEV_INTEGRITY */
1343
Scott Bauer6b8190d2017-06-15 10:44:30 -06001344static void nvme_set_chunk_size(struct nvme_ns *ns)
1345{
1346 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1347 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1348}
1349
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001350static void nvme_config_discard(struct nvme_ctrl *ctrl,
1351 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001352{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001353 u32 size = queue_logical_block_size(queue);
1354
1355 if (stream_alignment)
1356 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001357
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001358 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1359 NVME_DSM_MAX_RANGES);
1360
David Disseldorpb224f612017-11-24 16:30:53 +01001361 queue->limits.discard_alignment = 0;
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001362 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001363
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001364 blk_queue_max_discard_sectors(queue, UINT_MAX);
1365 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001366 blk_queue_flag_set(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001367
1368 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001369 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001370}
1371
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001372static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001373 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001374{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001375 memset(ids, 0, sizeof(*ids));
1376
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001377 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001378 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001379 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001380 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001381 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001382 /* Don't treat error as fatal we potentially
1383 * already have a NGUID or EUI-64
1384 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001385 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001386 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001387 "%s: Identify Descriptors failed\n", __func__);
1388 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001389}
1390
Christoph Hellwiged754e52017-11-09 13:50:43 +01001391static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1392{
1393 return !uuid_is_null(&ids->uuid) ||
1394 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1395 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1396}
1397
Christoph Hellwig002fab02017-11-09 13:50:16 +01001398static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1399{
1400 return uuid_equal(&a->uuid, &b->uuid) &&
1401 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1402 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1403}
1404
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001405static void nvme_update_disk_info(struct gendisk *disk,
1406 struct nvme_ns *ns, struct nvme_id_ns *id)
1407{
1408 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
Jeff Liencee160f2017-12-19 13:24:15 -06001409 unsigned short bs = 1 << ns->lba_shift;
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001410 unsigned stream_alignment = 0;
1411
1412 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1413 stream_alignment = ns->sws * ns->sgs;
1414
1415 blk_mq_freeze_queue(disk->queue);
1416 blk_integrity_unregister(disk);
1417
Jeff Liencee160f2017-12-19 13:24:15 -06001418 blk_queue_logical_block_size(disk->queue, bs);
1419 blk_queue_physical_block_size(disk->queue, bs);
1420 blk_queue_io_min(disk->queue, bs);
1421
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001422 if (ns->ms && !ns->ext &&
1423 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1424 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001425 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001426 capacity = 0;
1427 set_capacity(disk, capacity);
1428
1429 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1430 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1431 blk_mq_unfreeze_queue(disk->queue);
1432}
1433
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001434static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1435{
1436 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001437
1438 /*
1439 * If identify namespace failed, use default 512 byte block size so
1440 * block layer can use before failing read/write for 0 capacity.
1441 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001442 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001443 if (ns->lba_shift == 0)
1444 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001445 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001446 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1447 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1448 /* the PI implementation requires metadata equal t10 pi tuple size */
1449 if (ns->ms == sizeof(struct t10_pi_tuple))
1450 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1451 else
1452 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001453
Scott Bauer6b8190d2017-06-15 10:44:30 -06001454 if (ns->noiob)
1455 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001456 nvme_update_disk_info(disk, ns, id);
Matias Bjørling96257a82018-03-30 00:05:05 +02001457 if (ns->ndev)
1458 nvme_nvm_update_nvm_info(ns);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001459#ifdef CONFIG_NVME_MULTIPATH
1460 if (ns->head->disk)
1461 nvme_update_disk_info(ns->head->disk, ns, id);
1462#endif
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001463}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001464
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001465static int nvme_revalidate_disk(struct gendisk *disk)
1466{
1467 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001468 struct nvme_ctrl *ctrl = ns->ctrl;
1469 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001470 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001471 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001472
1473 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1474 set_capacity(disk, 0);
1475 return -ENODEV;
1476 }
1477
Christoph Hellwiged754e52017-11-09 13:50:43 +01001478 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001479 if (!id)
1480 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001481
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001482 if (id->ncap == 0) {
1483 ret = -ENODEV;
1484 goto out;
1485 }
1486
Keith Busch5e0fab52017-10-27 13:51:22 -06001487 __nvme_revalidate_disk(disk, id);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001488 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1489 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001490 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001491 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001492 ret = -ENODEV;
1493 }
1494
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001495out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001496 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001497 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001498}
1499
1500static char nvme_pr_type(enum pr_type type)
1501{
1502 switch (type) {
1503 case PR_WRITE_EXCLUSIVE:
1504 return 1;
1505 case PR_EXCLUSIVE_ACCESS:
1506 return 2;
1507 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1508 return 3;
1509 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1510 return 4;
1511 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1512 return 5;
1513 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1514 return 6;
1515 default:
1516 return 0;
1517 }
1518};
1519
1520static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1521 u64 key, u64 sa_key, u8 op)
1522{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001523 struct nvme_ns_head *head = NULL;
1524 struct nvme_ns *ns;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001525 struct nvme_command c;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001526 int srcu_idx, ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001527 u8 data[16] = { 0, };
1528
Keith Buschb0d61d52017-11-16 13:36:49 -07001529 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1530 if (unlikely(!ns))
1531 return -EWOULDBLOCK;
1532
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001533 put_unaligned_le64(key, &data[0]);
1534 put_unaligned_le64(sa_key, &data[8]);
1535
1536 memset(&c, 0, sizeof(c));
1537 c.common.opcode = op;
Keith Buschb0d61d52017-11-16 13:36:49 -07001538 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001539 c.common.cdw10[0] = cpu_to_le32(cdw10);
1540
Keith Buschb0d61d52017-11-16 13:36:49 -07001541 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001542 nvme_put_ns_from_disk(head, srcu_idx);
1543 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001544}
1545
1546static int nvme_pr_register(struct block_device *bdev, u64 old,
1547 u64 new, unsigned flags)
1548{
1549 u32 cdw10;
1550
1551 if (flags & ~PR_FL_IGNORE_KEY)
1552 return -EOPNOTSUPP;
1553
1554 cdw10 = old ? 2 : 0;
1555 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1556 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1557 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1558}
1559
1560static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1561 enum pr_type type, unsigned flags)
1562{
1563 u32 cdw10;
1564
1565 if (flags & ~PR_FL_IGNORE_KEY)
1566 return -EOPNOTSUPP;
1567
1568 cdw10 = nvme_pr_type(type) << 8;
1569 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1570 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1571}
1572
1573static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1574 enum pr_type type, bool abort)
1575{
1576 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1577 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1578}
1579
1580static int nvme_pr_clear(struct block_device *bdev, u64 key)
1581{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001582 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001583 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1584}
1585
1586static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1587{
1588 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1589 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1590}
1591
1592static const struct pr_ops nvme_pr_ops = {
1593 .pr_register = nvme_pr_register,
1594 .pr_reserve = nvme_pr_reserve,
1595 .pr_release = nvme_pr_release,
1596 .pr_preempt = nvme_pr_preempt,
1597 .pr_clear = nvme_pr_clear,
1598};
1599
Scott Bauera98e58e52017-02-03 12:50:32 -07001600#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001601int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1602 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001603{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001604 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001605 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001606
1607 memset(&cmd, 0, sizeof(cmd));
1608 if (send)
1609 cmd.common.opcode = nvme_admin_security_send;
1610 else
1611 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001612 cmd.common.nsid = 0;
1613 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1614 cmd.common.cdw10[1] = cpu_to_le32(len);
1615
1616 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1617 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1618}
1619EXPORT_SYMBOL_GPL(nvme_sec_submit);
1620#endif /* CONFIG_BLK_SED_OPAL */
1621
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001622static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001623 .owner = THIS_MODULE,
1624 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001625 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001626 .open = nvme_open,
1627 .release = nvme_release,
1628 .getgeo = nvme_getgeo,
1629 .revalidate_disk= nvme_revalidate_disk,
1630 .pr_ops = &nvme_pr_ops,
1631};
1632
Christoph Hellwig32acab32017-11-02 12:59:30 +01001633#ifdef CONFIG_NVME_MULTIPATH
1634static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
1635{
1636 struct nvme_ns_head *head = bdev->bd_disk->private_data;
1637
1638 if (!kref_get_unless_zero(&head->ref))
1639 return -ENXIO;
1640 return 0;
1641}
1642
1643static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
1644{
1645 nvme_put_ns_head(disk->private_data);
1646}
1647
1648const struct block_device_operations nvme_ns_head_ops = {
1649 .owner = THIS_MODULE,
1650 .open = nvme_ns_head_open,
1651 .release = nvme_ns_head_release,
1652 .ioctl = nvme_ioctl,
1653 .compat_ioctl = nvme_ioctl,
1654 .getgeo = nvme_getgeo,
1655 .pr_ops = &nvme_pr_ops,
1656};
1657#endif /* CONFIG_NVME_MULTIPATH */
1658
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001659static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1660{
1661 unsigned long timeout =
1662 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1663 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1664 int ret;
1665
1666 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001667 if (csts == ~0)
1668 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001669 if ((csts & NVME_CSTS_RDY) == bit)
1670 break;
1671
1672 msleep(100);
1673 if (fatal_signal_pending(current))
1674 return -EINTR;
1675 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001676 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001677 "Device not ready; aborting %s\n", enabled ?
1678 "initialisation" : "reset");
1679 return -ENODEV;
1680 }
1681 }
1682
1683 return ret;
1684}
1685
1686/*
1687 * If the device has been passed off to us in an enabled state, just clear
1688 * the enabled bit. The spec says we should set the 'shutdown notification
1689 * bits', but doing so may cause the device to complete commands to the
1690 * admin queue ... and we don't know what memory that might be pointing at!
1691 */
1692int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1693{
1694 int ret;
1695
1696 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1697 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1698
1699 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1700 if (ret)
1701 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001702
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001703 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001704 msleep(NVME_QUIRK_DELAY_AMOUNT);
1705
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001706 return nvme_wait_ready(ctrl, cap, false);
1707}
Ming Lin576d55d2016-02-10 10:03:32 -08001708EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001709
1710int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1711{
1712 /*
1713 * Default to a 4K page size, with the intention to update this
1714 * path in the future to accomodate architectures with differing
1715 * kernel and IO page sizes.
1716 */
1717 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1718 int ret;
1719
1720 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001721 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001722 "Minimum device page size %u too large for host (%u)\n",
1723 1 << dev_page_min, 1 << page_shift);
1724 return -ENODEV;
1725 }
1726
1727 ctrl->page_size = 1 << page_shift;
1728
1729 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1730 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001731 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001732 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1733 ctrl->ctrl_config |= NVME_CC_ENABLE;
1734
1735 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1736 if (ret)
1737 return ret;
1738 return nvme_wait_ready(ctrl, cap, true);
1739}
Ming Lin576d55d2016-02-10 10:03:32 -08001740EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001741
1742int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1743{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001744 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001745 u32 csts;
1746 int ret;
1747
1748 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1749 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1750
1751 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1752 if (ret)
1753 return ret;
1754
1755 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1756 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1757 break;
1758
1759 msleep(100);
1760 if (fatal_signal_pending(current))
1761 return -EINTR;
1762 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001763 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001764 "Device shutdown incomplete; abort shutdown\n");
1765 return -ENODEV;
1766 }
1767 }
1768
1769 return ret;
1770}
Ming Lin576d55d2016-02-10 10:03:32 -08001771EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001772
Christoph Hellwigda358252016-03-02 18:07:11 +01001773static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1774 struct request_queue *q)
1775{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001776 bool vwc = false;
1777
Christoph Hellwigda358252016-03-02 18:07:11 +01001778 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001779 u32 max_segments =
1780 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1781
Christoph Hellwigda358252016-03-02 18:07:11 +01001782 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001783 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001784 }
Keith Busch249159c2017-12-14 11:20:14 -07001785 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
1786 is_power_of_2(ctrl->max_hw_sectors))
Keith Busche6282ae2016-12-19 11:37:50 -05001787 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001788 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001789 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1790 vwc = true;
1791 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001792}
1793
Jon Derrickdbf86b32017-08-16 09:51:29 +02001794static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1795{
1796 __le64 ts;
1797 int ret;
1798
1799 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1800 return 0;
1801
1802 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1803 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1804 NULL);
1805 if (ret)
1806 dev_warn_once(ctrl->device,
1807 "could not set timestamp (%d)\n", ret);
1808 return ret;
1809}
1810
Keith Busch634b8322017-08-10 11:23:31 +02001811static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001812{
1813 /*
1814 * APST (Autonomous Power State Transition) lets us program a
1815 * table of power state transitions that the controller will
1816 * perform automatically. We configure it with a simple
1817 * heuristic: we are willing to spend at most 2% of the time
1818 * transitioning between power states. Therefore, when running
1819 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001820 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001821 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001822 * the requested maximum latency.
1823 *
1824 * We will not autonomously enter any non-operational state for
1825 * which the total latency exceeds ps_max_latency_us. Users
1826 * can set ps_max_latency_us to zero to turn off APST.
1827 */
1828
1829 unsigned apste;
1830 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001831 u64 max_lat_us = 0;
1832 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001833 int ret;
1834
1835 /*
1836 * If APST isn't supported or if we haven't been initialized yet,
1837 * then don't do anything.
1838 */
1839 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001840 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001841
1842 if (ctrl->npss > 31) {
1843 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001844 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001845 }
1846
1847 table = kzalloc(sizeof(*table), GFP_KERNEL);
1848 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001849 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001850
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001851 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001852 /* Turn off APST. */
1853 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001854 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001855 } else {
1856 __le64 target = cpu_to_le64(0);
1857 int state;
1858
1859 /*
1860 * Walk through all states from lowest- to highest-power.
1861 * According to the spec, lower-numbered states use more
1862 * power. NPSS, despite the name, is the index of the
1863 * lowest-power state, not the number of states.
1864 */
1865 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001866 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001867
1868 if (target)
1869 table->entries[state] = target;
1870
1871 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001872 * Don't allow transitions to the deepest state
1873 * if it's quirked off.
1874 */
1875 if (state == ctrl->npss &&
1876 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1877 continue;
1878
1879 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001880 * Is this state a useful non-operational state for
1881 * higher-power states to autonomously transition to?
1882 */
1883 if (!(ctrl->psd[state].flags &
1884 NVME_PS_FLAGS_NON_OP_STATE))
1885 continue;
1886
Kai-Heng Fengda875912017-06-07 15:25:42 +08001887 exit_latency_us =
1888 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1889 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001890 continue;
1891
Kai-Heng Fengda875912017-06-07 15:25:42 +08001892 total_latency_us =
1893 exit_latency_us +
1894 le32_to_cpu(ctrl->psd[state].entry_lat);
1895
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001896 /*
1897 * This state is good. Use it as the APST idle
1898 * target for higher power states.
1899 */
1900 transition_ms = total_latency_us + 19;
1901 do_div(transition_ms, 20);
1902 if (transition_ms > (1 << 24) - 1)
1903 transition_ms = (1 << 24) - 1;
1904
1905 target = cpu_to_le64((state << 3) |
1906 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001907
1908 if (max_ps == -1)
1909 max_ps = state;
1910
1911 if (total_latency_us > max_lat_us)
1912 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001913 }
1914
1915 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001916
1917 if (max_ps == -1) {
1918 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1919 } else {
1920 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1921 max_ps, max_lat_us, (int)sizeof(*table), table);
1922 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001923 }
1924
1925 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1926 table, sizeof(*table), NULL);
1927 if (ret)
1928 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1929
1930 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001931 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001932}
1933
1934static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1935{
1936 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1937 u64 latency;
1938
1939 switch (val) {
1940 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1941 case PM_QOS_LATENCY_ANY:
1942 latency = U64_MAX;
1943 break;
1944
1945 default:
1946 latency = val;
1947 }
1948
1949 if (ctrl->ps_max_latency_us != latency) {
1950 ctrl->ps_max_latency_us = latency;
1951 nvme_configure_apst(ctrl);
1952 }
1953}
1954
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001955struct nvme_core_quirk_entry {
1956 /*
1957 * NVMe model and firmware strings are padded with spaces. For
1958 * simplicity, strings in the quirk table are padded with NULLs
1959 * instead.
1960 */
1961 u16 vid;
1962 const char *mn;
1963 const char *fr;
1964 unsigned long quirks;
1965};
1966
1967static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001968 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001969 /*
1970 * This Toshiba device seems to die using any APST states. See:
1971 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1972 */
1973 .vid = 0x1179,
1974 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001975 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001976 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001977};
1978
1979/* match is null-terminated but idstr is space-padded. */
1980static bool string_matches(const char *idstr, const char *match, size_t len)
1981{
1982 size_t matchlen;
1983
1984 if (!match)
1985 return true;
1986
1987 matchlen = strlen(match);
1988 WARN_ON_ONCE(matchlen > len);
1989
1990 if (memcmp(idstr, match, matchlen))
1991 return false;
1992
1993 for (; matchlen < len; matchlen++)
1994 if (idstr[matchlen] != ' ')
1995 return false;
1996
1997 return true;
1998}
1999
2000static bool quirk_matches(const struct nvme_id_ctrl *id,
2001 const struct nvme_core_quirk_entry *q)
2002{
2003 return q->vid == le16_to_cpu(id->vid) &&
2004 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
2005 string_matches(id->fr, q->fr, sizeof(id->fr));
2006}
2007
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002008static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
2009 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02002010{
2011 size_t nqnlen;
2012 int off;
2013
2014 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
2015 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002016 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02002017 return;
2018 }
2019
2020 if (ctrl->vs >= NVME_VS(1, 2, 1))
2021 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
2022
2023 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002024 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02002025 "nqn.2014.08.org.nvmexpress:%4x%4x",
2026 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002027 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002028 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002029 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002030 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002031 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
2032}
2033
2034static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
2035{
2036 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
2037 kfree(subsys);
2038}
2039
2040static void nvme_release_subsystem(struct device *dev)
2041{
2042 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
2043}
2044
2045static void nvme_destroy_subsystem(struct kref *ref)
2046{
2047 struct nvme_subsystem *subsys =
2048 container_of(ref, struct nvme_subsystem, ref);
2049
2050 mutex_lock(&nvme_subsystems_lock);
2051 list_del(&subsys->entry);
2052 mutex_unlock(&nvme_subsystems_lock);
2053
Christoph Hellwiged754e52017-11-09 13:50:43 +01002054 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002055 device_del(&subsys->dev);
2056 put_device(&subsys->dev);
2057}
2058
2059static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2060{
2061 kref_put(&subsys->ref, nvme_destroy_subsystem);
2062}
2063
2064static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2065{
2066 struct nvme_subsystem *subsys;
2067
2068 lockdep_assert_held(&nvme_subsystems_lock);
2069
2070 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2071 if (strcmp(subsys->subnqn, subsysnqn))
2072 continue;
2073 if (!kref_get_unless_zero(&subsys->ref))
2074 continue;
2075 return subsys;
2076 }
2077
2078 return NULL;
2079}
2080
Hannes Reinecke1e496932017-11-10 10:58:23 +01002081#define SUBSYS_ATTR_RO(_name, _mode, _show) \
2082 struct device_attribute subsys_attr_##_name = \
2083 __ATTR(_name, _mode, _show, NULL)
2084
2085static ssize_t nvme_subsys_show_nqn(struct device *dev,
2086 struct device_attribute *attr,
2087 char *buf)
2088{
2089 struct nvme_subsystem *subsys =
2090 container_of(dev, struct nvme_subsystem, dev);
2091
2092 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2093}
2094static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2095
2096#define nvme_subsys_show_str_function(field) \
2097static ssize_t subsys_##field##_show(struct device *dev, \
2098 struct device_attribute *attr, char *buf) \
2099{ \
2100 struct nvme_subsystem *subsys = \
2101 container_of(dev, struct nvme_subsystem, dev); \
2102 return sprintf(buf, "%.*s\n", \
2103 (int)sizeof(subsys->field), subsys->field); \
2104} \
2105static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2106
2107nvme_subsys_show_str_function(model);
2108nvme_subsys_show_str_function(serial);
2109nvme_subsys_show_str_function(firmware_rev);
2110
2111static struct attribute *nvme_subsys_attrs[] = {
2112 &subsys_attr_model.attr,
2113 &subsys_attr_serial.attr,
2114 &subsys_attr_firmware_rev.attr,
2115 &subsys_attr_subsysnqn.attr,
2116 NULL,
2117};
2118
2119static struct attribute_group nvme_subsys_attrs_group = {
2120 .attrs = nvme_subsys_attrs,
2121};
2122
2123static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2124 &nvme_subsys_attrs_group,
2125 NULL,
2126};
2127
Israel Rukshinb837b282018-01-04 17:56:14 +02002128static int nvme_active_ctrls(struct nvme_subsystem *subsys)
2129{
2130 int count = 0;
2131 struct nvme_ctrl *ctrl;
2132
2133 mutex_lock(&subsys->lock);
2134 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2135 if (ctrl->state != NVME_CTRL_DELETING &&
2136 ctrl->state != NVME_CTRL_DEAD)
2137 count++;
2138 }
2139 mutex_unlock(&subsys->lock);
2140
2141 return count;
2142}
2143
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002144static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2145{
2146 struct nvme_subsystem *subsys, *found;
2147 int ret;
2148
2149 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2150 if (!subsys)
2151 return -ENOMEM;
2152 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
2153 if (ret < 0) {
2154 kfree(subsys);
2155 return ret;
2156 }
2157 subsys->instance = ret;
2158 mutex_init(&subsys->lock);
2159 kref_init(&subsys->ref);
2160 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002161 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002162 nvme_init_subnqn(subsys, ctrl, id);
2163 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2164 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2165 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2166 subsys->vendor_id = le16_to_cpu(id->vid);
2167 subsys->cmic = id->cmic;
2168
2169 subsys->dev.class = nvme_subsys_class;
2170 subsys->dev.release = nvme_release_subsystem;
Hannes Reinecke1e496932017-11-10 10:58:23 +01002171 subsys->dev.groups = nvme_subsys_attrs_groups;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002172 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
2173 device_initialize(&subsys->dev);
2174
2175 mutex_lock(&nvme_subsystems_lock);
2176 found = __nvme_find_get_subsystem(subsys->subnqn);
2177 if (found) {
2178 /*
2179 * Verify that the subsystem actually supports multiple
2180 * controllers, else bail out.
2181 */
Israel Rukshinb837b282018-01-04 17:56:14 +02002182 if (nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002183 dev_err(ctrl->device,
2184 "ignoring ctrl due to duplicate subnqn (%s).\n",
2185 found->subnqn);
2186 nvme_put_subsystem(found);
2187 ret = -EINVAL;
2188 goto out_unlock;
2189 }
2190
2191 __nvme_release_subsystem(subsys);
2192 subsys = found;
2193 } else {
2194 ret = device_add(&subsys->dev);
2195 if (ret) {
2196 dev_err(ctrl->device,
2197 "failed to register subsystem device.\n");
2198 goto out_unlock;
2199 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002200 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002201 list_add_tail(&subsys->entry, &nvme_subsystems);
2202 }
2203
2204 ctrl->subsys = subsys;
2205 mutex_unlock(&nvme_subsystems_lock);
2206
2207 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2208 dev_name(ctrl->device))) {
2209 dev_err(ctrl->device,
2210 "failed to create sysfs link from subsystem.\n");
2211 /* the transport driver will eventually put the subsystem */
2212 return -EINVAL;
2213 }
2214
2215 mutex_lock(&subsys->lock);
2216 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2217 mutex_unlock(&subsys->lock);
2218
2219 return 0;
2220
2221out_unlock:
2222 mutex_unlock(&nvme_subsystems_lock);
2223 put_device(&subsys->dev);
2224 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02002225}
2226
Matias Bjørlingd558fb52018-03-21 20:27:07 +01002227int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Javier Gonzáleza294c192018-03-30 00:05:17 +02002228 u8 log_page, void *log,
Matias Bjørling7ec60742018-04-12 09:16:03 -06002229 size_t size, u64 offset)
Matias Bjørling70da6092018-02-26 13:55:40 +01002230{
2231 struct nvme_command c = { };
2232 unsigned long dwlen = size / 4 - 1;
2233
2234 c.get_log_page.opcode = nvme_admin_get_log_page;
2235
2236 if (ns)
2237 c.get_log_page.nsid = cpu_to_le32(ns->head->ns_id);
2238 else
2239 c.get_log_page.nsid = cpu_to_le32(NVME_NSID_ALL);
2240
2241 c.get_log_page.lid = log_page;
2242 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1));
2243 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16);
Matias Bjørling7ec60742018-04-12 09:16:03 -06002244 c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset));
2245 c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset));
Matias Bjørling70da6092018-02-26 13:55:40 +01002246
2247 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2248}
2249
Keith Buschc627c482017-11-07 10:28:31 -07002250static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
2251 size_t size)
2252{
Matias Bjørling70da6092018-02-26 13:55:40 +01002253 return nvme_get_log_ext(ctrl, NULL, log_page, log, size, 0);
Keith Buschc627c482017-11-07 10:28:31 -07002254}
2255
Keith Busch84fef622017-11-07 10:28:32 -07002256static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2257{
2258 int ret;
2259
2260 if (!ctrl->effects)
2261 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2262
2263 if (!ctrl->effects)
2264 return 0;
2265
2266 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2267 sizeof(*ctrl->effects));
2268 if (ret) {
2269 kfree(ctrl->effects);
2270 ctrl->effects = NULL;
2271 }
2272 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002273}
2274
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002275/*
2276 * Initialize the cached copies of the Identify data and various controller
2277 * register in our nvme_ctrl structure. This should be called as soon as
2278 * the admin queue is fully up and running.
2279 */
2280int nvme_init_identify(struct nvme_ctrl *ctrl)
2281{
2282 struct nvme_id_ctrl *id;
2283 u64 cap;
2284 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002285 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002286 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002287
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002288 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2289 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002290 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002291 return ret;
2292 }
2293
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002294 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2295 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002296 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002297 return ret;
2298 }
2299 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2300
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002301 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002302 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2303
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002304 ret = nvme_identify_ctrl(ctrl, &id);
2305 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002306 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002307 return -EIO;
2308 }
2309
Keith Busch84fef622017-11-07 10:28:32 -07002310 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2311 ret = nvme_get_effects_log(ctrl);
2312 if (ret < 0)
2313 return ret;
2314 }
Christoph Hellwig180de0072017-06-26 12:39:02 +02002315
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002316 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002317 int i;
2318
2319 ret = nvme_init_subsystem(ctrl, id);
2320 if (ret)
2321 goto out_free;
2322
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002323 /*
2324 * Check for quirks. Quirk can depend on firmware version,
2325 * so, in principle, the set of quirks present can change
2326 * across a reset. As a possible future enhancement, we
2327 * could re-scan for quirks every time we reinitialize
2328 * the device, but we'd have to make sure that the driver
2329 * behaves intelligently if the quirks change.
2330 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002331 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2332 if (quirk_matches(id, &core_quirks[i]))
2333 ctrl->quirks |= core_quirks[i].quirks;
2334 }
2335 }
2336
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002337 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002338 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 -07002339 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2340 }
2341
Scott Bauer8a9ae522017-02-17 13:59:40 +01002342 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002343 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002344 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002345 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002346 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002347 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002348 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002349 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002350 max_hw_sectors = UINT_MAX;
2351 ctrl->max_hw_sectors =
2352 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002353
Christoph Hellwigda358252016-03-02 18:07:11 +01002354 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002355 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002356 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002357
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002358 if (id->rtd3e) {
2359 /* us -> s */
2360 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2361
2362 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2363 shutdown_timeout, 60);
2364
2365 if (ctrl->shutdown_timeout != shutdown_timeout)
Max Gurtovoy1a3838d2017-12-31 15:33:27 +02002366 dev_info(ctrl->device,
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002367 "Shutdown timeout set to %u seconds\n",
2368 ctrl->shutdown_timeout);
2369 } else
2370 ctrl->shutdown_timeout = shutdown_timeout;
2371
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002372 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002373 ctrl->apsta = id->apsta;
2374 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002375 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2376 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002377 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 -04002378 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002379 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002380 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002381 }
2382 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002383 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002384 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002385 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2386
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002387 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002388 ctrl->icdoff = le16_to_cpu(id->icdoff);
2389 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2390 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2391 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2392
2393 /*
2394 * In fabrics we need to verify the cntlid matches the
2395 * admin connect
2396 */
Keith Busch634b8322017-08-10 11:23:31 +02002397 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002398 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002399 goto out_free;
2400 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002401
2402 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002403 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002404 "keep-alive support is mandatory for fabrics\n");
2405 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002406 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002407 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002408 } else {
2409 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002410 ctrl->hmpre = le32_to_cpu(id->hmpre);
2411 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002412 ctrl->hmminds = le32_to_cpu(id->hmminds);
2413 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002414 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002415
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002416 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002417
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002418 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002419 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002420 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002421 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2422
Keith Busch634b8322017-08-10 11:23:31 +02002423 ret = nvme_configure_apst(ctrl);
2424 if (ret < 0)
2425 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002426
2427 ret = nvme_configure_timestamp(ctrl);
2428 if (ret < 0)
2429 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002430
2431 ret = nvme_configure_directives(ctrl);
2432 if (ret < 0)
2433 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002434
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002435 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002436
Keith Busch634b8322017-08-10 11:23:31 +02002437 return 0;
2438
2439out_free:
2440 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002441 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002442}
Ming Lin576d55d2016-02-10 10:03:32 -08002443EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002444
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002445static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig21d34712015-11-26 09:08:36 +01002446{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002447 struct nvme_ctrl *ctrl =
2448 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig21d34712015-11-26 09:08:36 +01002449
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002450 switch (ctrl->state) {
2451 case NVME_CTRL_LIVE:
2452 case NVME_CTRL_ADMIN_ONLY:
2453 break;
2454 default:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002455 return -EWOULDBLOCK;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002456 }
2457
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002458 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002459 return 0;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002460}
2461
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002462static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2463{
2464 struct nvme_ns *ns;
2465 int ret;
2466
Jianchao Wang765cc032018-02-12 20:54:46 +08002467 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002468 if (list_empty(&ctrl->namespaces)) {
2469 ret = -ENOTTY;
2470 goto out_unlock;
2471 }
2472
2473 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2474 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002475 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002476 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2477 ret = -EINVAL;
2478 goto out_unlock;
2479 }
2480
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002481 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002482 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2483 kref_get(&ns->kref);
Jianchao Wang765cc032018-02-12 20:54:46 +08002484 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002485
2486 ret = nvme_user_cmd(ctrl, ns, argp);
2487 nvme_put_ns(ns);
2488 return ret;
2489
2490out_unlock:
Jianchao Wang765cc032018-02-12 20:54:46 +08002491 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002492 return ret;
2493}
2494
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002495static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2496 unsigned long arg)
2497{
2498 struct nvme_ctrl *ctrl = file->private_data;
2499 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002500
2501 switch (cmd) {
2502 case NVME_IOCTL_ADMIN_CMD:
2503 return nvme_user_cmd(ctrl, NULL, argp);
2504 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002505 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002506 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002507 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002508 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002509 case NVME_IOCTL_SUBSYS_RESET:
2510 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002511 case NVME_IOCTL_RESCAN:
2512 nvme_queue_scan(ctrl);
2513 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002514 default:
2515 return -ENOTTY;
2516 }
2517}
2518
2519static const struct file_operations nvme_dev_fops = {
2520 .owner = THIS_MODULE,
2521 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002522 .unlocked_ioctl = nvme_dev_ioctl,
2523 .compat_ioctl = nvme_dev_ioctl,
2524};
2525
2526static ssize_t nvme_sysfs_reset(struct device *dev,
2527 struct device_attribute *attr, const char *buf,
2528 size_t count)
2529{
2530 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2531 int ret;
2532
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002533 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002534 if (ret < 0)
2535 return ret;
2536 return count;
2537}
2538static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2539
Keith Busch9ec3bb22016-04-29 15:45:18 -06002540static ssize_t nvme_sysfs_rescan(struct device *dev,
2541 struct device_attribute *attr, const char *buf,
2542 size_t count)
2543{
2544 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2545
2546 nvme_queue_scan(ctrl);
2547 return count;
2548}
2549static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2550
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002551static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
Keith Busch118472a2016-02-18 09:57:48 -07002552{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002553 struct gendisk *disk = dev_to_disk(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002554
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002555 if (disk->fops == &nvme_fops)
2556 return nvme_get_ns_from_dev(dev)->head;
2557 else
2558 return disk->private_data;
2559}
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002560
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002561static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2562 char *buf)
2563{
2564 struct nvme_ns_head *head = dev_to_ns_head(dev);
2565 struct nvme_ns_ids *ids = &head->ids;
2566 struct nvme_subsystem *subsys = head->subsys;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002567 int serial_len = sizeof(subsys->serial);
2568 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002569
Christoph Hellwig002fab02017-11-09 13:50:16 +01002570 if (!uuid_is_null(&ids->uuid))
2571 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Keith Busch118472a2016-02-18 09:57:48 -07002572
Christoph Hellwig002fab02017-11-09 13:50:16 +01002573 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2574 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002575
Christoph Hellwig002fab02017-11-09 13:50:16 +01002576 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2577 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002578
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002579 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2580 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002581 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002582 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2583 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002584 model_len--;
2585
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002586 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2587 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002588 head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002589}
Joe Perchesc828a892017-12-19 10:15:08 -08002590static DEVICE_ATTR_RO(wwid);
Keith Busch118472a2016-02-18 09:57:48 -07002591
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002592static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002593 char *buf)
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002594{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002595 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002596}
Joe Perchesc828a892017-12-19 10:15:08 -08002597static DEVICE_ATTR_RO(nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002598
Keith Busch2b9b6e82015-12-22 10:10:45 -07002599static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002600 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002601{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002602 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002603
2604 /* For backward compatibility expose the NGUID to userspace if
2605 * we have no UUID set
2606 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002607 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002608 printk_ratelimited(KERN_WARNING
2609 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002610 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002611 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002612 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002613}
Joe Perchesc828a892017-12-19 10:15:08 -08002614static DEVICE_ATTR_RO(uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002615
2616static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002617 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002618{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002619 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002620}
Joe Perchesc828a892017-12-19 10:15:08 -08002621static DEVICE_ATTR_RO(eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002622
2623static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002624 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002625{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002626 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002627}
Joe Perchesc828a892017-12-19 10:15:08 -08002628static DEVICE_ATTR_RO(nsid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002629
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002630static struct attribute *nvme_ns_id_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002631 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002632 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002633 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002634 &dev_attr_eui.attr,
2635 &dev_attr_nsid.attr,
2636 NULL,
2637};
2638
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002639static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002640 struct attribute *a, int n)
2641{
2642 struct device *dev = container_of(kobj, struct device, kobj);
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002643 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002644
2645 if (a == &dev_attr_uuid.attr) {
Martin Wilcka04b5de2017-09-28 21:33:23 +02002646 if (uuid_is_null(&ids->uuid) &&
Christoph Hellwig002fab02017-11-09 13:50:16 +01002647 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002648 return 0;
2649 }
2650 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002651 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002652 return 0;
2653 }
2654 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002655 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002656 return 0;
2657 }
2658 return a->mode;
2659}
2660
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002661const struct attribute_group nvme_ns_id_attr_group = {
2662 .attrs = nvme_ns_id_attrs,
2663 .is_visible = nvme_ns_id_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002664};
2665
Ming Lin931e1c22016-02-26 13:24:19 -08002666#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002667static ssize_t field##_show(struct device *dev, \
2668 struct device_attribute *attr, char *buf) \
2669{ \
2670 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002671 return sprintf(buf, "%.*s\n", \
2672 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002673} \
2674static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2675
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002676nvme_show_str_function(model);
2677nvme_show_str_function(serial);
2678nvme_show_str_function(firmware_rev);
2679
Ming Lin931e1c22016-02-26 13:24:19 -08002680#define nvme_show_int_function(field) \
2681static ssize_t field##_show(struct device *dev, \
2682 struct device_attribute *attr, char *buf) \
2683{ \
2684 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2685 return sprintf(buf, "%d\n", ctrl->field); \
2686} \
2687static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2688
Ming Lin931e1c22016-02-26 13:24:19 -08002689nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002690
Ming Lin1a353d82016-06-13 16:45:24 +02002691static ssize_t nvme_sysfs_delete(struct device *dev,
2692 struct device_attribute *attr, const char *buf,
2693 size_t count)
2694{
2695 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2696
2697 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002698 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002699 return count;
2700}
2701static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2702
2703static ssize_t nvme_sysfs_show_transport(struct device *dev,
2704 struct device_attribute *attr,
2705 char *buf)
2706{
2707 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2708
2709 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2710}
2711static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2712
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002713static ssize_t nvme_sysfs_show_state(struct device *dev,
2714 struct device_attribute *attr,
2715 char *buf)
2716{
2717 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2718 static const char *const state_name[] = {
2719 [NVME_CTRL_NEW] = "new",
2720 [NVME_CTRL_LIVE] = "live",
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002721 [NVME_CTRL_ADMIN_ONLY] = "only-admin",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002722 [NVME_CTRL_RESETTING] = "resetting",
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002723 [NVME_CTRL_CONNECTING] = "connecting",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002724 [NVME_CTRL_DELETING] = "deleting",
2725 [NVME_CTRL_DEAD] = "dead",
2726 };
2727
2728 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2729 state_name[ctrl->state])
2730 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2731
2732 return sprintf(buf, "unknown state\n");
2733}
2734
2735static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2736
Ming Lin1a353d82016-06-13 16:45:24 +02002737static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2740{
2741 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2742
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002743 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002744}
2745static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2746
2747static ssize_t nvme_sysfs_show_address(struct device *dev,
2748 struct device_attribute *attr,
2749 char *buf)
2750{
2751 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2752
2753 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2754}
2755static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2756
Keith Busch779ff7562016-01-12 15:09:31 -07002757static struct attribute *nvme_dev_attrs[] = {
2758 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002759 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002760 &dev_attr_model.attr,
2761 &dev_attr_serial.attr,
2762 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002763 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002764 &dev_attr_delete_controller.attr,
2765 &dev_attr_transport.attr,
2766 &dev_attr_subsysnqn.attr,
2767 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002768 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002769 NULL
2770};
2771
Ming Lin1a353d82016-06-13 16:45:24 +02002772static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2773 struct attribute *a, int n)
2774{
2775 struct device *dev = container_of(kobj, struct device, kobj);
2776 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2777
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002778 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2779 return 0;
2780 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2781 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002782
2783 return a->mode;
2784}
2785
Keith Busch779ff7562016-01-12 15:09:31 -07002786static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002787 .attrs = nvme_dev_attrs,
2788 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002789};
2790
2791static const struct attribute_group *nvme_dev_attr_groups[] = {
2792 &nvme_dev_attrs_group,
2793 NULL,
2794};
2795
Christoph Hellwiged754e52017-11-09 13:50:43 +01002796static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2797 unsigned nsid)
2798{
2799 struct nvme_ns_head *h;
2800
2801 lockdep_assert_held(&subsys->lock);
2802
2803 list_for_each_entry(h, &subsys->nsheads, entry) {
2804 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2805 return h;
2806 }
2807
2808 return NULL;
2809}
2810
2811static int __nvme_check_ids(struct nvme_subsystem *subsys,
2812 struct nvme_ns_head *new)
2813{
2814 struct nvme_ns_head *h;
2815
2816 lockdep_assert_held(&subsys->lock);
2817
2818 list_for_each_entry(h, &subsys->nsheads, entry) {
2819 if (nvme_ns_ids_valid(&new->ids) &&
Keith Busch20796992018-03-19 10:53:50 -06002820 !list_empty(&h->list) &&
Christoph Hellwiged754e52017-11-09 13:50:43 +01002821 nvme_ns_ids_equal(&new->ids, &h->ids))
2822 return -EINVAL;
2823 }
2824
2825 return 0;
2826}
2827
2828static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2829 unsigned nsid, struct nvme_id_ns *id)
2830{
2831 struct nvme_ns_head *head;
2832 int ret = -ENOMEM;
2833
2834 head = kzalloc(sizeof(*head), GFP_KERNEL);
2835 if (!head)
2836 goto out;
2837 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2838 if (ret < 0)
2839 goto out_free_head;
2840 head->instance = ret;
2841 INIT_LIST_HEAD(&head->list);
Max Gurtovoyfd92c772018-04-12 09:16:12 -06002842 ret = init_srcu_struct(&head->srcu);
2843 if (ret)
2844 goto out_ida_remove;
Christoph Hellwiged754e52017-11-09 13:50:43 +01002845 head->subsys = ctrl->subsys;
2846 head->ns_id = nsid;
2847 kref_init(&head->ref);
2848
2849 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2850
2851 ret = __nvme_check_ids(ctrl->subsys, head);
2852 if (ret) {
2853 dev_err(ctrl->device,
2854 "duplicate IDs for nsid %d\n", nsid);
2855 goto out_cleanup_srcu;
2856 }
2857
Christoph Hellwig32acab32017-11-02 12:59:30 +01002858 ret = nvme_mpath_alloc_disk(ctrl, head);
2859 if (ret)
2860 goto out_cleanup_srcu;
2861
Christoph Hellwiged754e52017-11-09 13:50:43 +01002862 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
2863 return head;
2864out_cleanup_srcu:
2865 cleanup_srcu_struct(&head->srcu);
Max Gurtovoyfd92c772018-04-12 09:16:12 -06002866out_ida_remove:
Christoph Hellwiged754e52017-11-09 13:50:43 +01002867 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2868out_free_head:
2869 kfree(head);
2870out:
2871 return ERR_PTR(ret);
2872}
2873
2874static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002875 struct nvme_id_ns *id)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002876{
2877 struct nvme_ctrl *ctrl = ns->ctrl;
2878 bool is_shared = id->nmic & (1 << 0);
2879 struct nvme_ns_head *head = NULL;
2880 int ret = 0;
2881
2882 mutex_lock(&ctrl->subsys->lock);
2883 if (is_shared)
2884 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2885 if (!head) {
2886 head = nvme_alloc_ns_head(ctrl, nsid, id);
2887 if (IS_ERR(head)) {
2888 ret = PTR_ERR(head);
2889 goto out_unlock;
2890 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002891 } else {
2892 struct nvme_ns_ids ids;
2893
2894 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2895 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2896 dev_err(ctrl->device,
2897 "IDs don't match for shared namespace %d\n",
2898 nsid);
2899 ret = -EINVAL;
2900 goto out_unlock;
2901 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002902 }
2903
2904 list_add_tail(&ns->siblings, &head->list);
2905 ns->head = head;
2906
2907out_unlock:
2908 mutex_unlock(&ctrl->subsys->lock);
2909 return ret;
2910}
2911
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002912static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2913{
2914 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2915 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2916
Christoph Hellwiged754e52017-11-09 13:50:43 +01002917 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002918}
2919
Keith Busch32f0c4a2016-07-13 11:45:02 -06002920static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002921{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002922 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002923
Jianchao Wang765cc032018-02-12 20:54:46 +08002924 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002925 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002926 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002927 if (!kref_get_unless_zero(&ns->kref))
2928 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002929 ret = ns;
2930 break;
2931 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002932 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002933 break;
2934 }
Jianchao Wang765cc032018-02-12 20:54:46 +08002935 up_read(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002936 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002937}
2938
Jens Axboef5d11842017-06-27 12:03:06 -06002939static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2940{
2941 struct streams_directive_params s;
2942 int ret;
2943
2944 if (!ctrl->nr_streams)
2945 return 0;
2946
Christoph Hellwiged754e52017-11-09 13:50:43 +01002947 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002948 if (ret)
2949 return ret;
2950
2951 ns->sws = le32_to_cpu(s.sws);
2952 ns->sgs = le16_to_cpu(s.sgs);
2953
2954 if (ns->sws) {
2955 unsigned int bs = 1 << ns->lba_shift;
2956
2957 blk_queue_io_min(ns->queue, bs * ns->sws);
2958 if (ns->sgs)
2959 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2960 }
2961
2962 return 0;
2963}
2964
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002965static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2966{
2967 struct nvme_ns *ns;
2968 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002969 struct nvme_id_ns *id;
2970 char disk_name[DISK_NAME_LEN];
Christoph Hellwig32acab32017-11-02 12:59:30 +01002971 int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002972
2973 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2974 if (!ns)
2975 return;
2976
2977 ns->queue = blk_mq_init_queue(ctrl->tagset);
2978 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002979 goto out_free_ns;
Bart Van Assche8b904b52018-03-07 17:10:10 -08002980 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002981 ns->queue->queuedata = ns;
2982 ns->ctrl = ctrl;
2983
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002984 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002985 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002986
2987 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002988 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002989
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002990 id = nvme_identify_ns(ctrl, nsid);
2991 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002992 goto out_free_queue;
2993
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002994 if (id->ncap == 0)
2995 goto out_free_id;
2996
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002997 if (nvme_init_ns_head(ns, nsid, id))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002998 goto out_free_id;
Keith Busch654b4a42017-12-14 11:20:32 -07002999 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003000
Christoph Hellwig32acab32017-11-02 12:59:30 +01003001#ifdef CONFIG_NVME_MULTIPATH
3002 /*
3003 * If multipathing is enabled we need to always use the subsystem
3004 * instance number for numbering our devices to avoid conflicts
3005 * between subsystems that have multiple controllers and thus use
3006 * the multipath-aware subsystem node and those that have a single
3007 * controller and use the controller node directly.
3008 */
3009 if (ns->head->disk) {
3010 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
3011 ctrl->cntlid, ns->head->instance);
3012 flags = GENHD_FL_HIDDEN;
3013 } else {
3014 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
3015 ns->head->instance);
3016 }
3017#else
3018 /*
3019 * But without the multipath code enabled, multiple controller per
3020 * subsystems are visible as devices and thus we cannot use the
3021 * subsystem instance.
3022 */
Christoph Hellwiged754e52017-11-09 13:50:43 +01003023 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003024#endif
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02003025
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003026 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
3027 if (nvme_nvm_register(ns, disk_name, node)) {
3028 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01003029 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003030 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003031 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003032
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003033 disk = alloc_disk_node(0, node);
3034 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01003035 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003036
3037 disk->fops = &nvme_fops;
3038 disk->private_data = ns;
3039 disk->queue = ns->queue;
Christoph Hellwig32acab32017-11-02 12:59:30 +01003040 disk->flags = flags;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003041 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
3042 ns->disk = disk;
3043
3044 __nvme_revalidate_disk(disk, id);
3045
Jianchao Wang765cc032018-02-12 20:54:46 +08003046 down_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003047 list_add_tail(&ns->list, &ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003048 up_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003049
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003050 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003051
3052 kfree(id);
3053
Dan Williams0d52c7562016-06-15 19:44:20 -07003054 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003055 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003056 &nvme_ns_id_attr_group))
Keith Busch2b9b6e82015-12-22 10:10:45 -07003057 pr_warn("%s: failed to create sysfs group for identification\n",
3058 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003059 if (ns->ndev && nvme_nvm_register_sysfs(ns))
3060 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
3061 ns->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003062
Baegjae Sung9bd82b12018-02-28 16:06:04 +09003063 nvme_mpath_add_disk(ns->head);
Thomas Taib9e03852018-02-08 13:38:29 -05003064 nvme_fault_inject_init(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003065 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01003066 out_unlink_ns:
3067 mutex_lock(&ctrl->subsys->lock);
3068 list_del_rcu(&ns->siblings);
3069 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003070 out_free_id:
3071 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003072 out_free_queue:
3073 blk_cleanup_queue(ns->queue);
3074 out_free_ns:
3075 kfree(ns);
3076}
3077
3078static void nvme_ns_remove(struct nvme_ns *ns)
3079{
Keith Busch646017a2016-02-24 09:15:54 -07003080 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3081 return;
3082
Thomas Taib9e03852018-02-08 13:38:29 -05003083 nvme_fault_inject_fini(ns);
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003084 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Keith Busch2b9b6e82015-12-22 10:10:45 -07003085 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003086 &nvme_ns_id_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003087 if (ns->ndev)
3088 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003089 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003090 blk_cleanup_queue(ns->queue);
Ming Leibd9f5d62017-12-06 18:30:09 +08003091 if (blk_get_integrity(ns->disk))
3092 blk_integrity_unregister(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003093 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003094
Christoph Hellwiged754e52017-11-09 13:50:43 +01003095 mutex_lock(&ns->ctrl->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003096 nvme_mpath_clear_current_path(ns);
Keith Busch9941a862017-11-16 13:36:50 -07003097 list_del_rcu(&ns->siblings);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003098 mutex_unlock(&ns->ctrl->subsys->lock);
3099
Jianchao Wang765cc032018-02-12 20:54:46 +08003100 down_write(&ns->ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003101 list_del_init(&ns->list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003102 up_write(&ns->ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003103
Keith Busch9941a862017-11-16 13:36:50 -07003104 synchronize_srcu(&ns->head->srcu);
Sagi Grimberg479a3222017-12-21 15:07:27 +02003105 nvme_mpath_check_last_path(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003106 nvme_put_ns(ns);
3107}
3108
Keith Busch540c8012015-10-22 15:45:06 -06003109static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3110{
3111 struct nvme_ns *ns;
3112
Keith Busch32f0c4a2016-07-13 11:45:02 -06003113 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06003114 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003115 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06003116 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003117 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06003118 } else
3119 nvme_alloc_ns(ctrl, nsid);
3120}
3121
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303122static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3123 unsigned nsid)
3124{
3125 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003126 LIST_HEAD(rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303127
Jianchao Wang765cc032018-02-12 20:54:46 +08003128 down_write(&ctrl->namespaces_rwsem);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303129 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01003130 if (ns->head->ns_id > nsid)
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003131 list_move_tail(&ns->list, &rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303132 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003133 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003134
3135 list_for_each_entry_safe(ns, next, &rm_list, list)
3136 nvme_ns_remove(ns);
3137
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303138}
3139
Keith Busch540c8012015-10-22 15:45:06 -06003140static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3141{
3142 struct nvme_ns *ns;
3143 __le32 *ns_list;
3144 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
3145 int ret = 0;
3146
Minwoo Im42595eb2018-02-08 22:56:31 +09003147 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
Keith Busch540c8012015-10-22 15:45:06 -06003148 if (!ns_list)
3149 return -ENOMEM;
3150
3151 for (i = 0; i < num_lists; i++) {
3152 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3153 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303154 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06003155
3156 for (j = 0; j < min(nn, 1024U); j++) {
3157 nsid = le32_to_cpu(ns_list[j]);
3158 if (!nsid)
3159 goto out;
3160
3161 nvme_validate_ns(ctrl, nsid);
3162
3163 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06003164 ns = nvme_find_get_ns(ctrl, prev);
3165 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06003166 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003167 nvme_put_ns(ns);
3168 }
Keith Busch540c8012015-10-22 15:45:06 -06003169 }
3170 }
3171 nn -= j;
3172 }
3173 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303174 nvme_remove_invalid_namespaces(ctrl, prev);
3175 free:
Keith Busch540c8012015-10-22 15:45:06 -06003176 kfree(ns_list);
3177 return ret;
3178}
3179
Christoph Hellwig5955be22016-04-26 13:51:59 +02003180static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003181{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003182 unsigned i;
3183
Keith Busch540c8012015-10-22 15:45:06 -06003184 for (i = 1; i <= nn; i++)
3185 nvme_validate_ns(ctrl, i);
3186
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303187 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003188}
3189
Christoph Hellwig5955be22016-04-26 13:51:59 +02003190static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003191{
Christoph Hellwig5955be22016-04-26 13:51:59 +02003192 struct nvme_ctrl *ctrl =
3193 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003194 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06003195 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003196
Christoph Hellwig5955be22016-04-26 13:51:59 +02003197 if (ctrl->state != NVME_CTRL_LIVE)
3198 return;
3199
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003200 WARN_ON_ONCE(!ctrl->tagset);
3201
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003202 if (nvme_identify_ctrl(ctrl, &id))
3203 return;
Keith Busch540c8012015-10-22 15:45:06 -06003204
3205 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06003206 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06003207 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3208 if (!nvme_scan_ns_list(ctrl, nn))
3209 goto done;
3210 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02003211 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06003212 done:
Jianchao Wang765cc032018-02-12 20:54:46 +08003213 down_write(&ctrl->namespaces_rwsem);
Keith Busch540c8012015-10-22 15:45:06 -06003214 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Jianchao Wang765cc032018-02-12 20:54:46 +08003215 up_write(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003216 kfree(id);
3217}
Christoph Hellwig5955be22016-04-26 13:51:59 +02003218
3219void nvme_queue_scan(struct nvme_ctrl *ctrl)
3220{
3221 /*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003222 * Only new queue scan work when admin and IO queues are both alive
Christoph Hellwig5955be22016-04-26 13:51:59 +02003223 */
3224 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003225 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003226}
3227EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003228
Keith Busch32f0c4a2016-07-13 11:45:02 -06003229/*
3230 * This function iterates the namespace list unlocked to allow recovery from
3231 * controller failure. It is up to the caller to ensure the namespace list is
3232 * not modified by scan work while this function is executing.
3233 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003234void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3235{
3236 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003237 LIST_HEAD(ns_list);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003238
Keith Busch0ff9d4e2016-05-12 08:37:14 -06003239 /*
3240 * The dead states indicates the controller was not gracefully
3241 * disconnected. In that case, we won't be able to flush any data while
3242 * removing the namespaces' disks; fail all the queues now to avoid
3243 * potentially having to clean up the failed sync later.
3244 */
3245 if (ctrl->state == NVME_CTRL_DEAD)
3246 nvme_kill_queues(ctrl);
3247
Jianchao Wang765cc032018-02-12 20:54:46 +08003248 down_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003249 list_splice_init(&ctrl->namespaces, &ns_list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003250 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003251
3252 list_for_each_entry_safe(ns, next, &ns_list, list)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003253 nvme_ns_remove(ns);
3254}
Ming Lin576d55d2016-02-10 10:03:32 -08003255EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003256
Keith Busche3d78742017-11-07 15:13:14 -07003257static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3258{
3259 char *envp[2] = { NULL, NULL };
3260 u32 aen_result = ctrl->aen_result;
3261
3262 ctrl->aen_result = 0;
3263 if (!aen_result)
3264 return;
3265
3266 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3267 if (!envp[0])
3268 return;
3269 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3270 kfree(envp[0]);
3271}
3272
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003273static void nvme_async_event_work(struct work_struct *work)
3274{
3275 struct nvme_ctrl *ctrl =
3276 container_of(work, struct nvme_ctrl, async_event_work);
3277
Keith Busche3d78742017-11-07 15:13:14 -07003278 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07003279 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003280}
3281
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303282static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3283{
3284
3285 u32 csts;
3286
3287 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3288 return false;
3289
3290 if (csts == ~0)
3291 return false;
3292
3293 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3294}
3295
3296static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3297{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303298 struct nvme_fw_slot_info_log *log;
3299
3300 log = kmalloc(sizeof(*log), GFP_KERNEL);
3301 if (!log)
3302 return;
3303
Keith Buschc627c482017-11-07 10:28:31 -07003304 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303305 dev_warn(ctrl->device,
3306 "Get FW SLOT INFO log error\n");
3307 kfree(log);
3308}
3309
3310static void nvme_fw_act_work(struct work_struct *work)
3311{
3312 struct nvme_ctrl *ctrl = container_of(work,
3313 struct nvme_ctrl, fw_act_work);
3314 unsigned long fw_act_timeout;
3315
3316 if (ctrl->mtfa)
3317 fw_act_timeout = jiffies +
3318 msecs_to_jiffies(ctrl->mtfa * 100);
3319 else
3320 fw_act_timeout = jiffies +
3321 msecs_to_jiffies(admin_timeout * 1000);
3322
3323 nvme_stop_queues(ctrl);
3324 while (nvme_ctrl_pp_status(ctrl)) {
3325 if (time_after(jiffies, fw_act_timeout)) {
3326 dev_warn(ctrl->device,
3327 "Fw activation timeout, reset controller\n");
3328 nvme_reset_ctrl(ctrl);
3329 break;
3330 }
3331 msleep(100);
3332 }
3333
3334 if (ctrl->state != NVME_CTRL_LIVE)
3335 return;
3336
3337 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003338 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303339 nvme_get_fw_slot_info(ctrl);
3340}
3341
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003342void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3343 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003344{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003345 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003346
Keith Buschad22c352017-11-07 15:13:12 -07003347 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003348 return;
3349
Keith Busche3d78742017-11-07 15:13:14 -07003350 switch (result & 0x7) {
3351 case NVME_AER_ERROR:
3352 case NVME_AER_SMART:
3353 case NVME_AER_CSS:
3354 case NVME_AER_VS:
3355 ctrl->aen_result = result;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003356 break;
3357 default:
3358 break;
3359 }
3360
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003361 switch (result & 0xff07) {
3362 case NVME_AER_NOTICE_NS_CHANGED:
3363 dev_info(ctrl->device, "rescanning\n");
3364 nvme_queue_scan(ctrl);
3365 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303366 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003367 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303368 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003369 default:
3370 dev_warn(ctrl->device, "async event result %08x\n", result);
3371 }
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003372 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003373}
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003374EXPORT_SYMBOL_GPL(nvme_complete_async_event);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003375
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003376void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003377{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003378 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003379 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003380 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303381 cancel_work_sync(&ctrl->fw_act_work);
Nitzan Carmib435ece2018-03-20 11:07:30 +00003382 if (ctrl->ops->stop_ctrl)
3383 ctrl->ops->stop_ctrl(ctrl);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003384}
3385EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003386
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003387void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3388{
3389 if (ctrl->kato)
3390 nvme_start_keep_alive(ctrl);
3391
3392 if (ctrl->queue_count > 1) {
3393 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003394 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003395 nvme_start_queues(ctrl);
3396 }
3397}
3398EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3399
3400void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3401{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003402 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003403}
Ming Lin576d55d2016-02-10 10:03:32 -08003404EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003405
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003406static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003407{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003408 struct nvme_ctrl *ctrl =
3409 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003410 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003411
Christoph Hellwig9843f682017-10-18 13:10:01 +02003412 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003413 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003414
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003415 if (subsys) {
3416 mutex_lock(&subsys->lock);
3417 list_del(&ctrl->subsys_entry);
3418 mutex_unlock(&subsys->lock);
3419 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3420 }
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003421
3422 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003423
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003424 if (subsys)
3425 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003426}
3427
3428/*
3429 * Initialize a NVMe controller structures. This needs to be called during
3430 * earliest initialization so that we have the initialized structured around
3431 * during probing.
3432 */
3433int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3434 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3435{
3436 int ret;
3437
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003438 ctrl->state = NVME_CTRL_NEW;
3439 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003440 INIT_LIST_HEAD(&ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003441 init_rwsem(&ctrl->namespaces_rwsem);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003442 ctrl->dev = dev;
3443 ctrl->ops = ops;
3444 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003445 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003446 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303447 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003448 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003449
Christoph Hellwig9843f682017-10-18 13:10:01 +02003450 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3451 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003452 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003453 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003454
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003455 device_initialize(&ctrl->ctrl_device);
3456 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003457 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003458 ctrl->device->class = nvme_class;
3459 ctrl->device->parent = ctrl->dev;
3460 ctrl->device->groups = nvme_dev_attr_groups;
3461 ctrl->device->release = nvme_free_ctrl;
3462 dev_set_drvdata(ctrl->device, ctrl);
3463 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3464 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003465 goto out_release_instance;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003466
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003467 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3468 ctrl->cdev.owner = ops->module;
3469 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003470 if (ret)
3471 goto out_free_name;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003472
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003473 /*
3474 * Initialize latency tolerance controls. The sysfs files won't
3475 * be visible to userspace unless the device actually supports APST.
3476 */
3477 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3478 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3479 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3480
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003481 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003482out_free_name:
3483 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003484out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003485 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003486out:
3487 return ret;
3488}
Ming Lin576d55d2016-02-10 10:03:32 -08003489EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003490
Keith Busch69d9a992016-02-24 09:15:56 -07003491/**
3492 * nvme_kill_queues(): Ends all namespace queues
3493 * @ctrl: the dead controller that needs to end
3494 *
3495 * Call this function when the driver determines it is unable to get the
3496 * controller in a state capable of servicing IO.
3497 */
3498void nvme_kill_queues(struct nvme_ctrl *ctrl)
3499{
3500 struct nvme_ns *ns;
3501
Jianchao Wang765cc032018-02-12 20:54:46 +08003502 down_read(&ctrl->namespaces_rwsem);
Ming Lei82654b62017-06-02 16:32:08 +08003503
Ming Lei443bd902017-06-19 10:21:08 +08003504 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003505 if (ctrl->admin_q)
3506 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003507
Keith Busch32f0c4a2016-07-13 11:45:02 -06003508 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003509 /*
3510 * Revalidating a dead namespace sets capacity to 0. This will
3511 * end buffered writers dirtying pages that can't be synced.
3512 */
Keith Buschf33447b2017-02-10 18:15:51 -05003513 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3514 continue;
3515 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003516 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003517
Ming Lei443bd902017-06-19 10:21:08 +08003518 /* Forcibly unquiesce queues to avoid blocking dispatch */
3519 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003520 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003521 up_read(&ctrl->namespaces_rwsem);
Keith Busch69d9a992016-02-24 09:15:56 -07003522}
Linus Torvalds237045f2016-03-18 17:13:31 -07003523EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003524
Keith Busch302ad8c2017-03-01 14:22:12 -05003525void nvme_unfreeze(struct nvme_ctrl *ctrl)
3526{
3527 struct nvme_ns *ns;
3528
Jianchao Wang765cc032018-02-12 20:54:46 +08003529 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003530 list_for_each_entry(ns, &ctrl->namespaces, list)
3531 blk_mq_unfreeze_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003532 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003533}
3534EXPORT_SYMBOL_GPL(nvme_unfreeze);
3535
3536void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3537{
3538 struct nvme_ns *ns;
3539
Jianchao Wang765cc032018-02-12 20:54:46 +08003540 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003541 list_for_each_entry(ns, &ctrl->namespaces, list) {
3542 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3543 if (timeout <= 0)
3544 break;
3545 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003546 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003547}
3548EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3549
3550void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3551{
3552 struct nvme_ns *ns;
3553
Jianchao Wang765cc032018-02-12 20:54:46 +08003554 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003555 list_for_each_entry(ns, &ctrl->namespaces, list)
3556 blk_mq_freeze_queue_wait(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003557 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003558}
3559EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3560
3561void nvme_start_freeze(struct nvme_ctrl *ctrl)
3562{
3563 struct nvme_ns *ns;
3564
Jianchao Wang765cc032018-02-12 20:54:46 +08003565 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003566 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003567 blk_freeze_queue_start(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003568 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003569}
3570EXPORT_SYMBOL_GPL(nvme_start_freeze);
3571
Keith Busch25646262016-01-04 09:10:57 -07003572void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003573{
3574 struct nvme_ns *ns;
3575
Jianchao Wang765cc032018-02-12 20:54:46 +08003576 down_read(&ctrl->namespaces_rwsem);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003577 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003578 blk_mq_quiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003579 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003580}
Ming Lin576d55d2016-02-10 10:03:32 -08003581EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003582
Keith Busch25646262016-01-04 09:10:57 -07003583void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003584{
3585 struct nvme_ns *ns;
3586
Jianchao Wang765cc032018-02-12 20:54:46 +08003587 down_read(&ctrl->namespaces_rwsem);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003588 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003589 blk_mq_unquiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003590 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003591}
Ming Lin576d55d2016-02-10 10:03:32 -08003592EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003593
Sagi Grimberg31b84462017-10-11 12:53:07 +03003594int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3595{
3596 if (!ctrl->ops->reinit_request)
3597 return 0;
3598
3599 return blk_mq_tagset_iter(set, set->driver_data,
3600 ctrl->ops->reinit_request);
3601}
3602EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3603
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003604int __init nvme_core_init(void)
3605{
Roy Shtermanb227c592018-01-14 12:39:02 +02003606 int result = -ENOMEM;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003607
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003608 nvme_wq = alloc_workqueue("nvme-wq",
3609 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3610 if (!nvme_wq)
Roy Shtermanb227c592018-01-14 12:39:02 +02003611 goto out;
3612
3613 nvme_reset_wq = alloc_workqueue("nvme-reset-wq",
3614 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3615 if (!nvme_reset_wq)
3616 goto destroy_wq;
3617
3618 nvme_delete_wq = alloc_workqueue("nvme-delete-wq",
3619 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3620 if (!nvme_delete_wq)
3621 goto destroy_reset_wq;
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003622
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003623 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003624 if (result < 0)
Roy Shtermanb227c592018-01-14 12:39:02 +02003625 goto destroy_delete_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003626
3627 nvme_class = class_create(THIS_MODULE, "nvme");
3628 if (IS_ERR(nvme_class)) {
3629 result = PTR_ERR(nvme_class);
3630 goto unregister_chrdev;
3631 }
3632
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003633 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3634 if (IS_ERR(nvme_subsys_class)) {
3635 result = PTR_ERR(nvme_subsys_class);
3636 goto destroy_class;
3637 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003638 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003639
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003640destroy_class:
3641 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003642unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003643 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003644destroy_delete_wq:
3645 destroy_workqueue(nvme_delete_wq);
3646destroy_reset_wq:
3647 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003648destroy_wq:
3649 destroy_workqueue(nvme_wq);
Roy Shtermanb227c592018-01-14 12:39:02 +02003650out:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003651 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003652}
3653
3654void nvme_core_exit(void)
3655{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003656 ida_destroy(&nvme_subsystems_ida);
3657 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003658 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003659 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003660 destroy_workqueue(nvme_delete_wq);
3661 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003662 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003663}
Ming Lin576d55d2016-02-10 10:03:32 -08003664
3665MODULE_LICENSE("GPL");
3666MODULE_VERSION("1.0");
3667module_init(nvme_core_init);
3668module_exit(nvme_core_exit);