blob: 21710a7460c823bbc4f84134d7ecce70d3f993ba [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 Hellwigab9e00cc2017-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 Hellwigab9e00cc2017-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);
Jianchao Wang12d9f072018-05-04 16:01:57 +0800102static void nvme_put_subsystem(struct nvme_subsystem *subsys);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100103
Christoph Hellwig50e8d8e2018-05-25 18:15:47 +0200104static void nvme_queue_scan(struct nvme_ctrl *ctrl)
105{
106 /*
107 * Only new queue scan work when admin and IO queues are both alive
108 */
109 if (ctrl->state == NVME_CTRL_LIVE)
110 queue_work(nvme_wq, &ctrl->scan_work);
111}
112
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200113int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
114{
115 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
116 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200117 if (!queue_work(nvme_reset_wq, &ctrl->reset_work))
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200118 return -EBUSY;
119 return 0;
120}
121EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
122
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200123int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200124{
125 int ret;
126
127 ret = nvme_reset_ctrl(ctrl);
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000128 if (!ret) {
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200129 flush_work(&ctrl->reset_work);
Charles Machalow4e50d9e2018-05-10 16:01:38 -0700130 if (ctrl->state != NVME_CTRL_LIVE &&
131 ctrl->state != NVME_CTRL_ADMIN_ONLY)
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000132 ret = -ENETRESET;
133 }
134
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200135 return ret;
136}
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200137EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200138
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200139static void nvme_delete_ctrl_work(struct work_struct *work)
140{
141 struct nvme_ctrl *ctrl =
142 container_of(work, struct nvme_ctrl, delete_work);
143
Max Gurtovoy77d06122018-03-11 17:46:06 +0200144 dev_info(ctrl->device,
145 "Removing ctrl: NQN \"%s\"\n", ctrl->opts->subsysnqn);
146
Sagi Grimberg40546372017-10-29 14:21:02 +0200147 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200148 nvme_stop_ctrl(ctrl);
149 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200150 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200151 nvme_uninit_ctrl(ctrl);
152 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200153}
154
155int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
156{
157 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
158 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200159 if (!queue_work(nvme_delete_wq, &ctrl->delete_work))
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200160 return -EBUSY;
161 return 0;
162}
163EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
164
165int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
166{
167 int ret = 0;
168
169 /*
170 * Keep a reference until the work is flushed since ->delete_ctrl
171 * can free the controller.
172 */
173 nvme_get_ctrl(ctrl);
174 ret = nvme_delete_ctrl(ctrl);
175 if (!ret)
176 flush_work(&ctrl->delete_work);
177 nvme_put_ctrl(ctrl);
178 return ret;
179}
180EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
181
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100182static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
183{
184 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
185}
186
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200187static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200188{
189 switch (nvme_req(req)->status & 0x7ff) {
190 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200191 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200192 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200193 return BLK_STS_NOSPC;
Keith Busche96fef22018-01-09 12:04:14 -0700194 case NVME_SC_LBA_RANGE:
195 return BLK_STS_TARGET;
196 case NVME_SC_BAD_ATTRIBUTES:
Junxiong Guane02ab022017-04-21 12:59:07 +0200197 case NVME_SC_ONCS_NOT_SUPPORTED:
Keith Busche96fef22018-01-09 12:04:14 -0700198 case NVME_SC_INVALID_OPCODE:
199 case NVME_SC_INVALID_FIELD:
200 case NVME_SC_INVALID_NS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200201 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200202 case NVME_SC_WRITE_FAULT:
203 case NVME_SC_READ_ERROR:
204 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200205 case NVME_SC_ACCESS_DENIED:
206 case NVME_SC_READ_ONLY:
Keith Busche96fef22018-01-09 12:04:14 -0700207 case NVME_SC_COMPARE_FAILED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200208 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200209 case NVME_SC_GUARD_CHECK:
210 case NVME_SC_APPTAG_CHECK:
211 case NVME_SC_REFTAG_CHECK:
212 case NVME_SC_INVALID_PI:
213 return BLK_STS_PROTECTION;
214 case NVME_SC_RESERVATION_CONFLICT:
215 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200216 default:
217 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200218 }
219}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200220
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200221static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200222{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200223 if (blk_noretry_request(req))
224 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200225 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200226 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200227 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200228 return false;
229 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200230}
231
232void nvme_complete_rq(struct request *req)
233{
Keith Busch908e4562018-01-09 12:04:15 -0700234 blk_status_t status = nvme_error_status(req);
235
Johannes Thumshirnca5554a2018-01-26 11:21:38 +0100236 trace_nvme_complete_rq(req);
237
Keith Busch908e4562018-01-09 12:04:15 -0700238 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
239 if (nvme_req_needs_failover(req, status)) {
Christoph Hellwig32acab32017-11-02 12:59:30 +0100240 nvme_failover_req(req);
241 return;
242 }
243
244 if (!blk_queue_dying(req->q)) {
245 nvme_req(req)->retries++;
246 blk_mq_requeue_request(req, true);
247 return;
248 }
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200249 }
Keith Busch908e4562018-01-09 12:04:15 -0700250 blk_mq_end_request(req, status);
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200251}
252EXPORT_SYMBOL_GPL(nvme_complete_rq);
253
Ming Linc55a2fd2016-05-18 14:05:02 -0700254void nvme_cancel_request(struct request *req, void *data, bool reserved)
255{
Ming Linc55a2fd2016-05-18 14:05:02 -0700256 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
257 "Cancelling I/O %d", req->tag);
258
Christoph Hellwige54b0642017-11-02 21:28:51 +0300259 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200260 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200261
Ming Linc55a2fd2016-05-18 14:05:02 -0700262}
263EXPORT_SYMBOL_GPL(nvme_cancel_request);
264
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200265bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
266 enum nvme_ctrl_state new_state)
267{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300268 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200269 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200270 bool changed = false;
271
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200272 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300273
274 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200275 switch (new_state) {
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800276 case NVME_CTRL_ADMIN_ONLY:
277 switch (old_state) {
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200278 case NVME_CTRL_CONNECTING:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800279 changed = true;
280 /* FALLTHRU */
281 default:
282 break;
283 }
284 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200285 case NVME_CTRL_LIVE:
286 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200287 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200288 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200289 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200290 changed = true;
291 /* FALLTHRU */
292 default:
293 break;
294 }
295 break;
296 case NVME_CTRL_RESETTING:
297 switch (old_state) {
298 case NVME_CTRL_NEW:
299 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800300 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900301 changed = true;
302 /* FALLTHRU */
303 default:
304 break;
305 }
306 break;
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200307 case NVME_CTRL_CONNECTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900308 switch (old_state) {
Max Gurtovoyb754a322018-01-31 18:31:25 +0200309 case NVME_CTRL_NEW:
James Smart3cec7f92017-10-25 16:43:13 -0700310 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200311 changed = true;
312 /* FALLTHRU */
313 default:
314 break;
315 }
316 break;
317 case NVME_CTRL_DELETING:
318 switch (old_state) {
319 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800320 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200321 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200322 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200323 changed = true;
324 /* FALLTHRU */
325 default:
326 break;
327 }
328 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600329 case NVME_CTRL_DEAD:
330 switch (old_state) {
331 case NVME_CTRL_DELETING:
332 changed = true;
333 /* FALLTHRU */
334 default:
335 break;
336 }
337 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200338 default:
339 break;
340 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200341
342 if (changed)
343 ctrl->state = new_state;
344
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200345 spin_unlock_irqrestore(&ctrl->lock, flags);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100346 if (changed && ctrl->state == NVME_CTRL_LIVE)
347 nvme_kick_requeue_lists(ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200348 return changed;
349}
350EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
351
Christoph Hellwiged754e52017-11-09 13:50:43 +0100352static void nvme_free_ns_head(struct kref *ref)
353{
354 struct nvme_ns_head *head =
355 container_of(ref, struct nvme_ns_head, ref);
356
Christoph Hellwig32acab32017-11-02 12:59:30 +0100357 nvme_mpath_remove_disk(head);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100358 ida_simple_remove(&head->subsys->ns_ida, head->instance);
359 list_del_init(&head->entry);
Nitzan Carmi43172282018-04-09 17:50:26 +0300360 cleanup_srcu_struct_quiesced(&head->srcu);
Jianchao Wang12d9f072018-05-04 16:01:57 +0800361 nvme_put_subsystem(head->subsys);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100362 kfree(head);
363}
364
365static void nvme_put_ns_head(struct nvme_ns_head *head)
366{
367 kref_put(&head->ref, nvme_free_ns_head);
368}
369
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100370static void nvme_free_ns(struct kref *kref)
371{
372 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
373
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200374 if (ns->ndev)
375 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100376
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100377 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100378 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700379 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100380 kfree(ns);
381}
382
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100383static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100384{
385 kref_put(&ns->kref, nvme_free_ns);
386}
387
James Smartbb06ec312018-04-12 09:16:15 -0600388static inline void nvme_clear_nvme_request(struct request *req)
389{
390 if (!(req->rq_flags & RQF_DONTPREP)) {
391 nvme_req(req)->retries = 0;
392 nvme_req(req)->flags = 0;
393 req->rq_flags |= RQF_DONTPREP;
394 }
395}
396
Christoph Hellwig41609822015-11-20 09:00:02 +0100397struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800398 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100399{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100400 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100401 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100402
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200403 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100404 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200405 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100406 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200407 qid ? qid - 1 : 0);
408 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100409 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100410 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100411
Christoph Hellwig21d34712015-11-26 09:08:36 +0100412 req->cmd_flags |= REQ_FAILFAST_DRIVER;
James Smartbb06ec312018-04-12 09:16:15 -0600413 nvme_clear_nvme_request(req);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800414 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100415
Christoph Hellwig41609822015-11-20 09:00:02 +0100416 return req;
417}
Ming Lin576d55d2016-02-10 10:03:32 -0800418EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100419
Jens Axboef5d11842017-06-27 12:03:06 -0600420static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
421{
422 struct nvme_command c;
423
424 memset(&c, 0, sizeof(c));
425
426 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530427 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600428 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
429 c.directive.dtype = NVME_DIR_IDENTIFY;
430 c.directive.tdtype = NVME_DIR_STREAMS;
431 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
432
433 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
434}
435
436static int nvme_disable_streams(struct nvme_ctrl *ctrl)
437{
438 return nvme_toggle_streams(ctrl, false);
439}
440
441static int nvme_enable_streams(struct nvme_ctrl *ctrl)
442{
443 return nvme_toggle_streams(ctrl, true);
444}
445
446static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
447 struct streams_directive_params *s, u32 nsid)
448{
449 struct nvme_command c;
450
451 memset(&c, 0, sizeof(c));
452 memset(s, 0, sizeof(*s));
453
454 c.directive.opcode = nvme_admin_directive_recv;
455 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700456 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600457 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
458 c.directive.dtype = NVME_DIR_STREAMS;
459
460 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
461}
462
463static int nvme_configure_directives(struct nvme_ctrl *ctrl)
464{
465 struct streams_directive_params s;
466 int ret;
467
468 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
469 return 0;
470 if (!streams)
471 return 0;
472
473 ret = nvme_enable_streams(ctrl);
474 if (ret)
475 return ret;
476
Arnav Dawn62346ea2017-07-12 16:11:53 +0530477 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600478 if (ret)
479 return ret;
480
481 ctrl->nssa = le16_to_cpu(s.nssa);
482 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
483 dev_info(ctrl->device, "too few streams (%u) available\n",
484 ctrl->nssa);
485 nvme_disable_streams(ctrl);
486 return 0;
487 }
488
489 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
490 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
491 return 0;
492}
493
494/*
495 * Check if 'req' has a write hint associated with it. If it does, assign
496 * a valid namespace stream to the write.
497 */
498static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
499 struct request *req, u16 *control,
500 u32 *dsmgmt)
501{
502 enum rw_hint streamid = req->write_hint;
503
504 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
505 streamid = 0;
506 else {
507 streamid--;
508 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
509 return;
510
511 *control |= NVME_RW_DTYPE_STREAMS;
512 *dsmgmt |= streamid << 16;
513 }
514
515 if (streamid < ARRAY_SIZE(req->q->write_hints))
516 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
517}
518
Ming Lin8093f7c2016-04-12 13:10:14 -0600519static inline void nvme_setup_flush(struct nvme_ns *ns,
520 struct nvme_command *cmnd)
521{
522 memset(cmnd, 0, sizeof(*cmnd));
523 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100524 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600525}
526
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200527static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600528 struct nvme_command *cmnd)
529{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100530 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600531 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100532 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600533
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100534 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600535 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200536 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600537
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100538 __rq_for_each_bio(bio, req) {
539 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
540 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
541
Keith Busch8cb6af72018-01-31 17:01:58 -0700542 if (n < segments) {
543 range[n].cattr = cpu_to_le32(0);
544 range[n].nlb = cpu_to_le32(nlb);
545 range[n].slba = cpu_to_le64(slba);
546 }
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100547 n++;
548 }
549
550 if (WARN_ON_ONCE(n != segments)) {
551 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200552 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100553 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600554
555 memset(cmnd, 0, sizeof(*cmnd));
556 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100557 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200558 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600559 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
560
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700561 req->special_vec.bv_page = virt_to_page(range);
562 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100563 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700564 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600565
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200566 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600567}
Ming Lin8093f7c2016-04-12 13:10:14 -0600568
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200569static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
570 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600571{
Jens Axboef5d11842017-06-27 12:03:06 -0600572 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600573 u16 control = 0;
574 u32 dsmgmt = 0;
575
576 if (req->cmd_flags & REQ_FUA)
577 control |= NVME_RW_FUA;
578 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
579 control |= NVME_RW_LR;
580
581 if (req->cmd_flags & REQ_RAHEAD)
582 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
583
584 memset(cmnd, 0, sizeof(*cmnd));
585 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100586 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600587 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
588 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
589
Jens Axboef5d11842017-06-27 12:03:06 -0600590 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
591 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
592
Ming Lin8093f7c2016-04-12 13:10:14 -0600593 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100594 /*
595 * If formated with metadata, the block layer always provides a
596 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
597 * we enable the PRACT bit for protection information or set the
598 * namespace capacity to zero to prevent any I/O.
599 */
600 if (!blk_integrity_rq(req)) {
601 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
602 return BLK_STS_NOTSUPP;
603 control |= NVME_RW_PRINFO_PRACT;
604 }
605
Ming Lin8093f7c2016-04-12 13:10:14 -0600606 switch (ns->pi_type) {
607 case NVME_NS_DPS_PI_TYPE3:
608 control |= NVME_RW_PRINFO_PRCHK_GUARD;
609 break;
610 case NVME_NS_DPS_PI_TYPE1:
611 case NVME_NS_DPS_PI_TYPE2:
612 control |= NVME_RW_PRINFO_PRCHK_GUARD |
613 NVME_RW_PRINFO_PRCHK_REF;
614 cmnd->rw.reftag = cpu_to_le32(
615 nvme_block_nr(ns, blk_rq_pos(req)));
616 break;
617 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600618 }
619
620 cmnd->rw.control = cpu_to_le16(control);
621 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200622 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600623}
624
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200625blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600626 struct nvme_command *cmd)
627{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200628 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600629
James Smartbb06ec312018-04-12 09:16:15 -0600630 nvme_clear_nvme_request(req);
Christoph Hellwig987f6992017-04-05 19:18:08 +0200631
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100632 switch (req_op(req)) {
633 case REQ_OP_DRV_IN:
634 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800635 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100636 break;
637 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600638 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100639 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200640 case REQ_OP_WRITE_ZEROES:
641 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100642 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600643 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100644 break;
645 case REQ_OP_READ:
646 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200647 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100648 break;
649 default:
650 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200651 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100652 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600653
James Smart721b3912016-10-21 23:33:34 +0300654 cmd->common.command_id = req->tag;
Johannes Thumshirn3d030e42018-01-26 11:21:37 +0100655 if (ns)
656 trace_nvme_setup_nvm_cmd(req->q->id, cmd);
657 else
658 trace_nvme_setup_admin_cmd(cmd);
Ming Lin8093f7c2016-04-12 13:10:14 -0600659 return ret;
660}
661EXPORT_SYMBOL_GPL(nvme_setup_cmd);
662
Christoph Hellwig41609822015-11-20 09:00:02 +0100663/*
664 * Returns 0 on success. If the result is negative, it's a Linux error code;
665 * if the result is positive, it's an NVM Express status code
666 */
667int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800668 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800669 unsigned timeout, int qid, int at_head,
670 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100671{
672 struct request *req;
673 int ret;
674
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200675 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100676 if (IS_ERR(req))
677 return PTR_ERR(req);
678
679 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
680
Christoph Hellwig21d34712015-11-26 09:08:36 +0100681 if (buffer && bufflen) {
682 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
683 if (ret)
684 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100685 }
686
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200687 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800688 if (result)
689 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200690 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
691 ret = -EINTR;
692 else
693 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100694 out:
695 blk_mq_free_request(req);
696 return ret;
697}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200698EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100699
700int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
701 void *buffer, unsigned bufflen)
702{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200703 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
704 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100705}
Ming Lin576d55d2016-02-10 10:03:32 -0800706EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100707
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400708static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
709 unsigned len, u32 seed, bool write)
710{
711 struct bio_integrity_payload *bip;
712 int ret = -ENOMEM;
713 void *buf;
714
715 buf = kmalloc(len, GFP_KERNEL);
716 if (!buf)
717 goto out;
718
719 ret = -EFAULT;
720 if (write && copy_from_user(buf, ubuf, len))
721 goto out_free_meta;
722
723 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
724 if (IS_ERR(bip)) {
725 ret = PTR_ERR(bip);
726 goto out_free_meta;
727 }
728
729 bip->bip_iter.bi_size = len;
730 bip->bip_iter.bi_sector = seed;
731 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
732 offset_in_page(buf));
733 if (ret == len)
734 return buf;
735 ret = -ENOMEM;
736out_free_meta:
737 kfree(buf);
738out:
739 return ERR_PTR(ret);
740}
741
Keith Busch63263d62017-08-29 17:46:04 -0400742static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400743 struct nvme_command *cmd, void __user *ubuffer,
744 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
745 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100746{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200747 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600748 struct nvme_ns *ns = q->queuedata;
749 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100750 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600751 struct bio *bio = NULL;
752 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100753 int ret;
754
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200755 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100756 if (IS_ERR(req))
757 return PTR_ERR(req);
758
759 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
James Smartbb06ec312018-04-12 09:16:15 -0600760 nvme_req(req)->flags |= NVME_REQ_USERCMD;
Christoph Hellwig41609822015-11-20 09:00:02 +0100761
762 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100763 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
764 GFP_KERNEL);
765 if (ret)
766 goto out;
767 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200768 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400769 if (disk && meta_buffer && meta_len) {
770 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
771 meta_seed, write);
772 if (IS_ERR(meta)) {
773 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600774 goto out_unmap;
775 }
Keith Buschf31a2112018-04-17 14:42:44 -0600776 req->cmd_flags |= REQ_INTEGRITY;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600777 }
778 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400779
Keith Busch0b7f1f22015-10-23 09:47:28 -0600780 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200781 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
782 ret = -EINTR;
783 else
784 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100785 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800786 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600787 if (meta && !ret && !write) {
788 if (copy_to_user(meta_buffer, meta, meta_len))
789 ret = -EFAULT;
790 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600791 kfree(meta);
792 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200793 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600794 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100795 out:
796 blk_mq_free_request(req);
797 return ret;
798}
799
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200800static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200801{
802 struct nvme_ctrl *ctrl = rq->end_io_data;
803
804 blk_mq_free_request(rq);
805
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200806 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200807 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200808 "failed nvme_keep_alive_end_io error=%d\n",
809 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200810 return;
811 }
812
813 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
814}
815
816static int nvme_keep_alive(struct nvme_ctrl *ctrl)
817{
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200818 struct request *rq;
819
Roland Dreier0a34e462018-01-11 13:38:15 -0800820 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200821 NVME_QID_ANY);
822 if (IS_ERR(rq))
823 return PTR_ERR(rq);
824
825 rq->timeout = ctrl->kato * HZ;
826 rq->end_io_data = ctrl;
827
828 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
829
830 return 0;
831}
832
833static void nvme_keep_alive_work(struct work_struct *work)
834{
835 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
836 struct nvme_ctrl, ka_work);
837
838 if (nvme_keep_alive(ctrl)) {
839 /* allocation failure, reset the controller */
840 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200841 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200842 return;
843 }
844}
845
Johannes Thumshirn00b683d2018-04-12 09:16:05 -0600846static void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200847{
848 if (unlikely(ctrl->kato == 0))
849 return;
850
851 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
Roland Dreier0a34e462018-01-11 13:38:15 -0800852 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd));
853 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200854 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
855}
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200856
857void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
858{
859 if (unlikely(ctrl->kato == 0))
860 return;
861
862 cancel_delayed_work_sync(&ctrl->ka_work);
863}
864EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
865
Keith Busch3f7f25a92017-06-20 15:09:56 -0400866static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100867{
868 struct nvme_command c = { };
869 int error;
870
871 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
872 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200873 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100874
875 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
876 if (!*id)
877 return -ENOMEM;
878
879 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
880 sizeof(struct nvme_id_ctrl));
881 if (error)
882 kfree(*id);
883 return error;
884}
885
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200886static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100887 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200888{
889 struct nvme_command c = { };
890 int status;
891 void *data;
892 int pos;
893 int len;
894
895 c.identify.opcode = nvme_admin_identify;
896 c.identify.nsid = cpu_to_le32(nsid);
897 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
898
899 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
900 if (!data)
901 return -ENOMEM;
902
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200903 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200904 NVME_IDENTIFY_DATA_SIZE);
905 if (status)
906 goto free_data;
907
908 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
909 struct nvme_ns_id_desc *cur = data + pos;
910
911 if (cur->nidl == 0)
912 break;
913
914 switch (cur->nidt) {
915 case NVME_NIDT_EUI64:
916 if (cur->nidl != NVME_NIDT_EUI64_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_EUI64\n",
919 cur->nidl);
920 goto free_data;
921 }
922 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100923 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200924 break;
925 case NVME_NIDT_NGUID:
926 if (cur->nidl != NVME_NIDT_NGUID_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_NGUID\n",
929 cur->nidl);
930 goto free_data;
931 }
932 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100933 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200934 break;
935 case NVME_NIDT_UUID:
936 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200937 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200938 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
939 cur->nidl);
940 goto free_data;
941 }
942 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100943 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200944 break;
945 default:
946 /* Skip unnkown types */
947 len = cur->nidl;
948 break;
949 }
950
951 len += sizeof(*cur);
952 }
953free_data:
954 kfree(data);
955 return status;
956}
957
Keith Busch540c8012015-10-22 15:45:06 -0600958static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
959{
960 struct nvme_command c = { };
961
962 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200963 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600964 c.identify.nsid = cpu_to_le32(nsid);
Minwoo Im42595eb2018-02-08 22:56:31 +0900965 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list,
966 NVME_IDENTIFY_DATA_SIZE);
Keith Busch540c8012015-10-22 15:45:06 -0600967}
968
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200969static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
970 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100971{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200972 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100973 struct nvme_command c = { };
974 int error;
975
976 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200977 c.identify.opcode = nvme_admin_identify;
978 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200979 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100980
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200981 id = kmalloc(sizeof(*id), GFP_KERNEL);
982 if (!id)
983 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100984
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200985 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
986 if (error) {
987 dev_warn(ctrl->device, "Identify namespace failed\n");
988 kfree(id);
989 return NULL;
990 }
991
992 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100993}
994
Keith Busch3f7f25a92017-06-20 15:09:56 -0400995static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700996 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100997{
998 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800999 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +01001000 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01001001
1002 memset(&c, 0, sizeof(c));
1003 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +01001004 c.features.fid = cpu_to_le32(fid);
1005 c.features.dword11 = cpu_to_le32(dword11);
1006
Christoph Hellwigd49187e2016-11-10 07:32:33 -08001007 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -07001008 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -07001009 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -08001010 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +01001011 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01001012}
1013
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001014int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
1015{
1016 u32 q_count = (*count - 1) | ((*count - 1) << 16);
1017 u32 result;
1018 int status, nr_io_queues;
1019
Andy Lutomirski1a6fe742016-09-16 11:16:10 -07001020 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001021 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001022 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001023 return status;
1024
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001025 /*
1026 * Degraded controllers might return an error when setting the queue
1027 * count. We still want to be able to bring them online and offer
1028 * access to the admin queue, as that might be only way to fix them up.
1029 */
1030 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001031 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001032 *count = 0;
1033 } else {
1034 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
1035 *count = min(*count, nr_io_queues);
1036 }
1037
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001038 return 0;
1039}
Ming Lin576d55d2016-02-10 10:03:32 -08001040EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001041
Hannes Reineckec0561f82018-05-22 11:09:55 +02001042#define NVME_AEN_SUPPORTED \
1043 (NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT)
1044
1045static void nvme_enable_aen(struct nvme_ctrl *ctrl)
1046{
1047 u32 result;
1048 int status;
1049
1050 status = nvme_set_features(ctrl, NVME_FEAT_ASYNC_EVENT,
1051 ctrl->oaes & NVME_AEN_SUPPORTED, NULL, 0, &result);
1052 if (status)
1053 dev_warn(ctrl->device, "Failed to configure AEN (cfg %x)\n",
1054 ctrl->oaes & NVME_AEN_SUPPORTED);
1055}
1056
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001057static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1058{
1059 struct nvme_user_io io;
1060 struct nvme_command c;
1061 unsigned length, meta_len;
1062 void __user *metadata;
1063
1064 if (copy_from_user(&io, uio, sizeof(io)))
1065 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001066 if (io.flags)
1067 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001068
1069 switch (io.opcode) {
1070 case nvme_cmd_write:
1071 case nvme_cmd_read:
1072 case nvme_cmd_compare:
1073 break;
1074 default:
1075 return -EINVAL;
1076 }
1077
1078 length = (io.nblocks + 1) << ns->lba_shift;
1079 meta_len = (io.nblocks + 1) * ns->ms;
1080 metadata = (void __user *)(uintptr_t)io.metadata;
1081
1082 if (ns->ext) {
1083 length += meta_len;
1084 meta_len = 0;
1085 } else if (meta_len) {
1086 if ((io.metadata & 3) || !io.metadata)
1087 return -EINVAL;
1088 }
1089
1090 memset(&c, 0, sizeof(c));
1091 c.rw.opcode = io.opcode;
1092 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001093 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001094 c.rw.slba = cpu_to_le64(io.slba);
1095 c.rw.length = cpu_to_le16(io.nblocks);
1096 c.rw.control = cpu_to_le16(io.control);
1097 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1098 c.rw.reftag = cpu_to_le32(io.reftag);
1099 c.rw.apptag = cpu_to_le16(io.apptag);
1100 c.rw.appmask = cpu_to_le16(io.appmask);
1101
Keith Busch63263d62017-08-29 17:46:04 -04001102 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001103 (void __user *)(uintptr_t)io.addr, length,
1104 metadata, meta_len, io.slba, NULL, 0);
1105}
1106
Keith Busch84fef622017-11-07 10:28:32 -07001107static u32 nvme_known_admin_effects(u8 opcode)
1108{
1109 switch (opcode) {
1110 case nvme_admin_format_nvm:
1111 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1112 NVME_CMD_EFFECTS_CSE_MASK;
1113 case nvme_admin_sanitize_nvm:
1114 return NVME_CMD_EFFECTS_CSE_MASK;
1115 default:
1116 break;
1117 }
1118 return 0;
1119}
1120
1121static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1122 u8 opcode)
1123{
1124 u32 effects = 0;
1125
1126 if (ns) {
1127 if (ctrl->effects)
1128 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1129 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1130 dev_warn(ctrl->device,
1131 "IO command:%02x has unhandled effects:%08x\n",
1132 opcode, effects);
1133 return 0;
1134 }
1135
1136 if (ctrl->effects)
Keith Busch62843c22018-04-12 09:16:14 -06001137 effects = le32_to_cpu(ctrl->effects->acs[opcode]);
Keith Busch84fef622017-11-07 10:28:32 -07001138 else
1139 effects = nvme_known_admin_effects(opcode);
1140
1141 /*
1142 * For simplicity, IO to all namespaces is quiesced even if the command
1143 * effects say only one namespace is affected.
1144 */
1145 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1146 nvme_start_freeze(ctrl);
1147 nvme_wait_freeze(ctrl);
1148 }
1149 return effects;
1150}
1151
1152static void nvme_update_formats(struct nvme_ctrl *ctrl)
1153{
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001154 struct nvme_ns *ns, *next;
1155 LIST_HEAD(rm_list);
Keith Busch84fef622017-11-07 10:28:32 -07001156
Jianchao Wang765cc032018-02-12 20:54:46 +08001157 down_write(&ctrl->namespaces_rwsem);
Keith Busch84fef622017-11-07 10:28:32 -07001158 list_for_each_entry(ns, &ctrl->namespaces, list) {
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001159 if (ns->disk && nvme_revalidate_disk(ns->disk)) {
1160 list_move_tail(&ns->list, &rm_list);
1161 }
Keith Busch84fef622017-11-07 10:28:32 -07001162 }
Jianchao Wang765cc032018-02-12 20:54:46 +08001163 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001164
1165 list_for_each_entry_safe(ns, next, &rm_list, list)
1166 nvme_ns_remove(ns);
Keith Busch84fef622017-11-07 10:28:32 -07001167}
1168
1169static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1170{
1171 /*
1172 * Revalidate LBA changes prior to unfreezing. This is necessary to
1173 * prevent memory corruption if a logical block size was changed by
1174 * this command.
1175 */
1176 if (effects & NVME_CMD_EFFECTS_LBCC)
1177 nvme_update_formats(ctrl);
1178 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1179 nvme_unfreeze(ctrl);
1180 if (effects & NVME_CMD_EFFECTS_CCC)
1181 nvme_init_identify(ctrl);
1182 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1183 nvme_queue_scan(ctrl);
1184}
1185
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001186static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001187 struct nvme_passthru_cmd __user *ucmd)
1188{
1189 struct nvme_passthru_cmd cmd;
1190 struct nvme_command c;
1191 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001192 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001193 int status;
1194
1195 if (!capable(CAP_SYS_ADMIN))
1196 return -EACCES;
1197 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1198 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001199 if (cmd.flags)
1200 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001201
1202 memset(&c, 0, sizeof(c));
1203 c.common.opcode = cmd.opcode;
1204 c.common.flags = cmd.flags;
1205 c.common.nsid = cpu_to_le32(cmd.nsid);
1206 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1207 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1208 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1209 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1210 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1211 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1212 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1213 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1214
1215 if (cmd.timeout_ms)
1216 timeout = msecs_to_jiffies(cmd.timeout_ms);
1217
Keith Busch84fef622017-11-07 10:28:32 -07001218 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001219 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001220 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001221 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1222 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001223 nvme_passthru_end(ctrl, effects);
1224
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001225 if (status >= 0) {
1226 if (put_user(cmd.result, &ucmd->result))
1227 return -EFAULT;
1228 }
1229
1230 return status;
1231}
1232
Christoph Hellwig32acab32017-11-02 12:59:30 +01001233/*
1234 * Issue ioctl requests on the first available path. Note that unlike normal
1235 * block layer requests we will not retry failed request on another controller.
1236 */
1237static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1238 struct nvme_ns_head **head, int *srcu_idx)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001239{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001240#ifdef CONFIG_NVME_MULTIPATH
1241 if (disk->fops == &nvme_ns_head_ops) {
1242 *head = disk->private_data;
1243 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1244 return nvme_find_path(*head);
1245 }
1246#endif
1247 *head = NULL;
1248 *srcu_idx = -1;
1249 return disk->private_data;
1250}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001251
Christoph Hellwig32acab32017-11-02 12:59:30 +01001252static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1253{
1254 if (head)
1255 srcu_read_unlock(&head->srcu, idx);
1256}
1257
1258static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
1259{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001260 switch (cmd) {
1261 case NVME_IOCTL_ID:
1262 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001263 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001264 case NVME_IOCTL_ADMIN_CMD:
1265 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1266 case NVME_IOCTL_IO_CMD:
1267 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1268 case NVME_IOCTL_SUBMIT_IO:
1269 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001270 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001271#ifdef CONFIG_NVM
1272 if (ns->ndev)
1273 return nvme_nvm_ioctl(ns, cmd, arg);
1274#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001275 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001276 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001277 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001278 return -ENOTTY;
1279 }
1280}
1281
Christoph Hellwig32acab32017-11-02 12:59:30 +01001282static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1283 unsigned int cmd, unsigned long arg)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001284{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001285 struct nvme_ns_head *head = NULL;
1286 struct nvme_ns *ns;
1287 int srcu_idx, ret;
1288
1289 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1290 if (unlikely(!ns))
1291 ret = -EWOULDBLOCK;
1292 else
1293 ret = nvme_ns_ioctl(ns, cmd, arg);
1294 nvme_put_ns_from_disk(head, srcu_idx);
1295 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001296}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001297
1298static int nvme_open(struct block_device *bdev, fmode_t mode)
1299{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001300 struct nvme_ns *ns = bdev->bd_disk->private_data;
1301
Christoph Hellwig32acab32017-11-02 12:59:30 +01001302#ifdef CONFIG_NVME_MULTIPATH
1303 /* should never be called due to GENHD_FL_HIDDEN */
1304 if (WARN_ON_ONCE(ns->head->disk))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001305 goto fail;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001306#endif
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001307 if (!kref_get_unless_zero(&ns->kref))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001308 goto fail;
1309 if (!try_module_get(ns->ctrl->ops->module))
1310 goto fail_put_ns;
1311
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001312 return 0;
Nitzan Carmi85088c42018-01-04 17:56:13 +02001313
1314fail_put_ns:
1315 nvme_put_ns(ns);
1316fail:
1317 return -ENXIO;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001318}
1319
1320static void nvme_release(struct gendisk *disk, fmode_t mode)
1321{
Nitzan Carmi85088c42018-01-04 17:56:13 +02001322 struct nvme_ns *ns = disk->private_data;
1323
1324 module_put(ns->ctrl->ops->module);
1325 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001326}
1327
1328static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1329{
1330 /* some standard values */
1331 geo->heads = 1 << 6;
1332 geo->sectors = 1 << 5;
1333 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1334 return 0;
1335}
1336
1337#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001338static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001339{
1340 struct blk_integrity integrity;
1341
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001342 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001343 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001344 case NVME_NS_DPS_PI_TYPE3:
1345 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001346 integrity.tag_size = sizeof(u16) + sizeof(u32);
1347 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001348 break;
1349 case NVME_NS_DPS_PI_TYPE1:
1350 case NVME_NS_DPS_PI_TYPE2:
1351 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001352 integrity.tag_size = sizeof(u16);
1353 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001354 break;
1355 default:
1356 integrity.profile = NULL;
1357 break;
1358 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001359 integrity.tuple_size = ms;
1360 blk_integrity_register(disk, &integrity);
1361 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001362}
1363#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001364static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001365{
1366}
1367#endif /* CONFIG_BLK_DEV_INTEGRITY */
1368
Scott Bauer6b8190d2017-06-15 10:44:30 -06001369static void nvme_set_chunk_size(struct nvme_ns *ns)
1370{
1371 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1372 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1373}
1374
Jens Axboe38317612018-05-02 11:06:54 -06001375static void nvme_config_discard(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001376{
Jens Axboe38317612018-05-02 11:06:54 -06001377 struct nvme_ctrl *ctrl = ns->ctrl;
1378 struct request_queue *queue = ns->queue;
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001379 u32 size = queue_logical_block_size(queue);
1380
Jens Axboe38317612018-05-02 11:06:54 -06001381 if (!(ctrl->oncs & NVME_CTRL_ONCS_DSM)) {
1382 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, queue);
1383 return;
1384 }
1385
1386 if (ctrl->nr_streams && ns->sws && ns->sgs)
1387 size *= ns->sws * ns->sgs;
Keith Busch08095e72016-03-04 13:15:17 -07001388
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001389 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1390 NVME_DSM_MAX_RANGES);
1391
David Disseldorpb224f612017-11-24 16:30:53 +01001392 queue->limits.discard_alignment = 0;
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001393 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001394
Jens Axboe38317612018-05-02 11:06:54 -06001395 /* If discard is already enabled, don't reset queue limits */
1396 if (blk_queue_flag_test_and_set(QUEUE_FLAG_DISCARD, queue))
1397 return;
1398
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001399 blk_queue_max_discard_sectors(queue, UINT_MAX);
1400 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001401
1402 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001403 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001404}
1405
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001406static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001407 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001408{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001409 memset(ids, 0, sizeof(*ids));
1410
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001411 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001412 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001413 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001414 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001415 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001416 /* Don't treat error as fatal we potentially
1417 * already have a NGUID or EUI-64
1418 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001419 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001420 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001421 "%s: Identify Descriptors failed\n", __func__);
1422 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001423}
1424
Christoph Hellwiged754e52017-11-09 13:50:43 +01001425static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1426{
1427 return !uuid_is_null(&ids->uuid) ||
1428 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1429 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1430}
1431
Christoph Hellwig002fab02017-11-09 13:50:16 +01001432static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1433{
1434 return uuid_equal(&a->uuid, &b->uuid) &&
1435 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1436 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1437}
1438
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001439static void nvme_update_disk_info(struct gendisk *disk,
1440 struct nvme_ns *ns, struct nvme_id_ns *id)
1441{
1442 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
Jeff Liencee160f2017-12-19 13:24:15 -06001443 unsigned short bs = 1 << ns->lba_shift;
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001444
1445 blk_mq_freeze_queue(disk->queue);
1446 blk_integrity_unregister(disk);
1447
Jeff Liencee160f2017-12-19 13:24:15 -06001448 blk_queue_logical_block_size(disk->queue, bs);
1449 blk_queue_physical_block_size(disk->queue, bs);
1450 blk_queue_io_min(disk->queue, bs);
1451
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001452 if (ns->ms && !ns->ext &&
1453 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1454 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001455 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001456 capacity = 0;
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001457
Jens Axboe38317612018-05-02 11:06:54 -06001458 set_capacity(disk, capacity);
1459 nvme_config_discard(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001460 blk_mq_unfreeze_queue(disk->queue);
1461}
1462
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001463static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1464{
1465 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001466
1467 /*
1468 * If identify namespace failed, use default 512 byte block size so
1469 * block layer can use before failing read/write for 0 capacity.
1470 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001471 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001472 if (ns->lba_shift == 0)
1473 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001474 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001475 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
Max Gurtovoyc97f4142018-05-27 18:50:10 +03001476 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001477 /* the PI implementation requires metadata equal t10 pi tuple size */
1478 if (ns->ms == sizeof(struct t10_pi_tuple))
1479 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1480 else
1481 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001482
Scott Bauer6b8190d2017-06-15 10:44:30 -06001483 if (ns->noiob)
1484 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001485 nvme_update_disk_info(disk, ns, id);
Matias Bjørling96257a82018-03-30 00:05:05 +02001486 if (ns->ndev)
1487 nvme_nvm_update_nvm_info(ns);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001488#ifdef CONFIG_NVME_MULTIPATH
1489 if (ns->head->disk)
1490 nvme_update_disk_info(ns->head->disk, ns, id);
1491#endif
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001492}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001493
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001494static int nvme_revalidate_disk(struct gendisk *disk)
1495{
1496 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001497 struct nvme_ctrl *ctrl = ns->ctrl;
1498 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001499 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001500 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001501
1502 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1503 set_capacity(disk, 0);
1504 return -ENODEV;
1505 }
1506
Christoph Hellwiged754e52017-11-09 13:50:43 +01001507 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001508 if (!id)
1509 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001510
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001511 if (id->ncap == 0) {
1512 ret = -ENODEV;
1513 goto out;
1514 }
1515
Keith Busch5e0fab52017-10-27 13:51:22 -06001516 __nvme_revalidate_disk(disk, id);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001517 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1518 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001519 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001520 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001521 ret = -ENODEV;
1522 }
1523
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001524out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001525 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001526 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001527}
1528
1529static char nvme_pr_type(enum pr_type type)
1530{
1531 switch (type) {
1532 case PR_WRITE_EXCLUSIVE:
1533 return 1;
1534 case PR_EXCLUSIVE_ACCESS:
1535 return 2;
1536 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1537 return 3;
1538 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1539 return 4;
1540 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1541 return 5;
1542 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1543 return 6;
1544 default:
1545 return 0;
1546 }
1547};
1548
1549static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1550 u64 key, u64 sa_key, u8 op)
1551{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001552 struct nvme_ns_head *head = NULL;
1553 struct nvme_ns *ns;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001554 struct nvme_command c;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001555 int srcu_idx, ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001556 u8 data[16] = { 0, };
1557
Keith Buschb0d61d52017-11-16 13:36:49 -07001558 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1559 if (unlikely(!ns))
1560 return -EWOULDBLOCK;
1561
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001562 put_unaligned_le64(key, &data[0]);
1563 put_unaligned_le64(sa_key, &data[8]);
1564
1565 memset(&c, 0, sizeof(c));
1566 c.common.opcode = op;
Keith Buschb0d61d52017-11-16 13:36:49 -07001567 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001568 c.common.cdw10[0] = cpu_to_le32(cdw10);
1569
Keith Buschb0d61d52017-11-16 13:36:49 -07001570 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001571 nvme_put_ns_from_disk(head, srcu_idx);
1572 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001573}
1574
1575static int nvme_pr_register(struct block_device *bdev, u64 old,
1576 u64 new, unsigned flags)
1577{
1578 u32 cdw10;
1579
1580 if (flags & ~PR_FL_IGNORE_KEY)
1581 return -EOPNOTSUPP;
1582
1583 cdw10 = old ? 2 : 0;
1584 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1585 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1586 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1587}
1588
1589static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1590 enum pr_type type, unsigned flags)
1591{
1592 u32 cdw10;
1593
1594 if (flags & ~PR_FL_IGNORE_KEY)
1595 return -EOPNOTSUPP;
1596
1597 cdw10 = nvme_pr_type(type) << 8;
1598 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1599 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1600}
1601
1602static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1603 enum pr_type type, bool abort)
1604{
Ivan Bornyakove9a98532018-05-23 17:56:11 +03001605 u32 cdw10 = nvme_pr_type(type) << 8 | (abort ? 2 : 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001606 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1607}
1608
1609static int nvme_pr_clear(struct block_device *bdev, u64 key)
1610{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001611 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001612 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1613}
1614
1615static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1616{
Ivan Bornyakove9a98532018-05-23 17:56:11 +03001617 u32 cdw10 = nvme_pr_type(type) << 8 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001618 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1619}
1620
1621static const struct pr_ops nvme_pr_ops = {
1622 .pr_register = nvme_pr_register,
1623 .pr_reserve = nvme_pr_reserve,
1624 .pr_release = nvme_pr_release,
1625 .pr_preempt = nvme_pr_preempt,
1626 .pr_clear = nvme_pr_clear,
1627};
1628
Scott Bauera98e58e52017-02-03 12:50:32 -07001629#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001630int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1631 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001632{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001633 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001634 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001635
1636 memset(&cmd, 0, sizeof(cmd));
1637 if (send)
1638 cmd.common.opcode = nvme_admin_security_send;
1639 else
1640 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001641 cmd.common.nsid = 0;
1642 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1643 cmd.common.cdw10[1] = cpu_to_le32(len);
1644
1645 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1646 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1647}
1648EXPORT_SYMBOL_GPL(nvme_sec_submit);
1649#endif /* CONFIG_BLK_SED_OPAL */
1650
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001651static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001652 .owner = THIS_MODULE,
1653 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001654 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001655 .open = nvme_open,
1656 .release = nvme_release,
1657 .getgeo = nvme_getgeo,
1658 .revalidate_disk= nvme_revalidate_disk,
1659 .pr_ops = &nvme_pr_ops,
1660};
1661
Christoph Hellwig32acab32017-11-02 12:59:30 +01001662#ifdef CONFIG_NVME_MULTIPATH
1663static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
1664{
1665 struct nvme_ns_head *head = bdev->bd_disk->private_data;
1666
1667 if (!kref_get_unless_zero(&head->ref))
1668 return -ENXIO;
1669 return 0;
1670}
1671
1672static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
1673{
1674 nvme_put_ns_head(disk->private_data);
1675}
1676
1677const struct block_device_operations nvme_ns_head_ops = {
1678 .owner = THIS_MODULE,
1679 .open = nvme_ns_head_open,
1680 .release = nvme_ns_head_release,
1681 .ioctl = nvme_ioctl,
1682 .compat_ioctl = nvme_ioctl,
1683 .getgeo = nvme_getgeo,
1684 .pr_ops = &nvme_pr_ops,
1685};
1686#endif /* CONFIG_NVME_MULTIPATH */
1687
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001688static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1689{
1690 unsigned long timeout =
1691 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1692 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1693 int ret;
1694
1695 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001696 if (csts == ~0)
1697 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001698 if ((csts & NVME_CSTS_RDY) == bit)
1699 break;
1700
1701 msleep(100);
1702 if (fatal_signal_pending(current))
1703 return -EINTR;
1704 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001705 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001706 "Device not ready; aborting %s\n", enabled ?
1707 "initialisation" : "reset");
1708 return -ENODEV;
1709 }
1710 }
1711
1712 return ret;
1713}
1714
1715/*
1716 * If the device has been passed off to us in an enabled state, just clear
1717 * the enabled bit. The spec says we should set the 'shutdown notification
1718 * bits', but doing so may cause the device to complete commands to the
1719 * admin queue ... and we don't know what memory that might be pointing at!
1720 */
1721int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1722{
1723 int ret;
1724
1725 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1726 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1727
1728 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1729 if (ret)
1730 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001731
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001732 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001733 msleep(NVME_QUIRK_DELAY_AMOUNT);
1734
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001735 return nvme_wait_ready(ctrl, cap, false);
1736}
Ming Lin576d55d2016-02-10 10:03:32 -08001737EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001738
1739int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1740{
1741 /*
1742 * Default to a 4K page size, with the intention to update this
1743 * path in the future to accomodate architectures with differing
1744 * kernel and IO page sizes.
1745 */
1746 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1747 int ret;
1748
1749 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001750 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001751 "Minimum device page size %u too large for host (%u)\n",
1752 1 << dev_page_min, 1 << page_shift);
1753 return -ENODEV;
1754 }
1755
1756 ctrl->page_size = 1 << page_shift;
1757
1758 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1759 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001760 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001761 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1762 ctrl->ctrl_config |= NVME_CC_ENABLE;
1763
1764 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1765 if (ret)
1766 return ret;
1767 return nvme_wait_ready(ctrl, cap, true);
1768}
Ming Lin576d55d2016-02-10 10:03:32 -08001769EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001770
1771int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1772{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001773 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001774 u32 csts;
1775 int ret;
1776
1777 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1778 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1779
1780 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1781 if (ret)
1782 return ret;
1783
1784 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1785 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1786 break;
1787
1788 msleep(100);
1789 if (fatal_signal_pending(current))
1790 return -EINTR;
1791 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001792 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001793 "Device shutdown incomplete; abort shutdown\n");
1794 return -ENODEV;
1795 }
1796 }
1797
1798 return ret;
1799}
Ming Lin576d55d2016-02-10 10:03:32 -08001800EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001801
Christoph Hellwigda358252016-03-02 18:07:11 +01001802static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1803 struct request_queue *q)
1804{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001805 bool vwc = false;
1806
Christoph Hellwigda358252016-03-02 18:07:11 +01001807 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001808 u32 max_segments =
1809 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1810
Christoph Hellwigda358252016-03-02 18:07:11 +01001811 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001812 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001813 }
Keith Busch249159c2017-12-14 11:20:14 -07001814 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
1815 is_power_of_2(ctrl->max_hw_sectors))
Keith Busche6282ae2016-12-19 11:37:50 -05001816 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001817 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001818 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1819 vwc = true;
1820 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001821}
1822
Jon Derrickdbf86b32017-08-16 09:51:29 +02001823static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1824{
1825 __le64 ts;
1826 int ret;
1827
1828 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1829 return 0;
1830
1831 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1832 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1833 NULL);
1834 if (ret)
1835 dev_warn_once(ctrl->device,
1836 "could not set timestamp (%d)\n", ret);
1837 return ret;
1838}
1839
Keith Busch634b8322017-08-10 11:23:31 +02001840static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001841{
1842 /*
1843 * APST (Autonomous Power State Transition) lets us program a
1844 * table of power state transitions that the controller will
1845 * perform automatically. We configure it with a simple
1846 * heuristic: we are willing to spend at most 2% of the time
1847 * transitioning between power states. Therefore, when running
1848 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001849 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001850 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001851 * the requested maximum latency.
1852 *
1853 * We will not autonomously enter any non-operational state for
1854 * which the total latency exceeds ps_max_latency_us. Users
1855 * can set ps_max_latency_us to zero to turn off APST.
1856 */
1857
1858 unsigned apste;
1859 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001860 u64 max_lat_us = 0;
1861 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001862 int ret;
1863
1864 /*
1865 * If APST isn't supported or if we haven't been initialized yet,
1866 * then don't do anything.
1867 */
1868 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001869 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001870
1871 if (ctrl->npss > 31) {
1872 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001873 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001874 }
1875
1876 table = kzalloc(sizeof(*table), GFP_KERNEL);
1877 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001878 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001879
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001880 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001881 /* Turn off APST. */
1882 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001883 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001884 } else {
1885 __le64 target = cpu_to_le64(0);
1886 int state;
1887
1888 /*
1889 * Walk through all states from lowest- to highest-power.
1890 * According to the spec, lower-numbered states use more
1891 * power. NPSS, despite the name, is the index of the
1892 * lowest-power state, not the number of states.
1893 */
1894 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001895 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001896
1897 if (target)
1898 table->entries[state] = target;
1899
1900 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001901 * Don't allow transitions to the deepest state
1902 * if it's quirked off.
1903 */
1904 if (state == ctrl->npss &&
1905 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1906 continue;
1907
1908 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001909 * Is this state a useful non-operational state for
1910 * higher-power states to autonomously transition to?
1911 */
1912 if (!(ctrl->psd[state].flags &
1913 NVME_PS_FLAGS_NON_OP_STATE))
1914 continue;
1915
Kai-Heng Fengda875912017-06-07 15:25:42 +08001916 exit_latency_us =
1917 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1918 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001919 continue;
1920
Kai-Heng Fengda875912017-06-07 15:25:42 +08001921 total_latency_us =
1922 exit_latency_us +
1923 le32_to_cpu(ctrl->psd[state].entry_lat);
1924
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001925 /*
1926 * This state is good. Use it as the APST idle
1927 * target for higher power states.
1928 */
1929 transition_ms = total_latency_us + 19;
1930 do_div(transition_ms, 20);
1931 if (transition_ms > (1 << 24) - 1)
1932 transition_ms = (1 << 24) - 1;
1933
1934 target = cpu_to_le64((state << 3) |
1935 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001936
1937 if (max_ps == -1)
1938 max_ps = state;
1939
1940 if (total_latency_us > max_lat_us)
1941 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001942 }
1943
1944 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001945
1946 if (max_ps == -1) {
1947 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1948 } else {
1949 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1950 max_ps, max_lat_us, (int)sizeof(*table), table);
1951 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001952 }
1953
1954 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1955 table, sizeof(*table), NULL);
1956 if (ret)
1957 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1958
1959 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001960 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001961}
1962
1963static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1964{
1965 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1966 u64 latency;
1967
1968 switch (val) {
1969 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1970 case PM_QOS_LATENCY_ANY:
1971 latency = U64_MAX;
1972 break;
1973
1974 default:
1975 latency = val;
1976 }
1977
1978 if (ctrl->ps_max_latency_us != latency) {
1979 ctrl->ps_max_latency_us = latency;
1980 nvme_configure_apst(ctrl);
1981 }
1982}
1983
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001984struct nvme_core_quirk_entry {
1985 /*
1986 * NVMe model and firmware strings are padded with spaces. For
1987 * simplicity, strings in the quirk table are padded with NULLs
1988 * instead.
1989 */
1990 u16 vid;
1991 const char *mn;
1992 const char *fr;
1993 unsigned long quirks;
1994};
1995
1996static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001997 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001998 /*
1999 * This Toshiba device seems to die using any APST states. See:
2000 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
2001 */
2002 .vid = 0x1179,
2003 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002004 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07002005 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002006};
2007
2008/* match is null-terminated but idstr is space-padded. */
2009static bool string_matches(const char *idstr, const char *match, size_t len)
2010{
2011 size_t matchlen;
2012
2013 if (!match)
2014 return true;
2015
2016 matchlen = strlen(match);
2017 WARN_ON_ONCE(matchlen > len);
2018
2019 if (memcmp(idstr, match, matchlen))
2020 return false;
2021
2022 for (; matchlen < len; matchlen++)
2023 if (idstr[matchlen] != ' ')
2024 return false;
2025
2026 return true;
2027}
2028
2029static bool quirk_matches(const struct nvme_id_ctrl *id,
2030 const struct nvme_core_quirk_entry *q)
2031{
2032 return q->vid == le16_to_cpu(id->vid) &&
2033 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
2034 string_matches(id->fr, q->fr, sizeof(id->fr));
2035}
2036
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002037static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
2038 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02002039{
2040 size_t nqnlen;
2041 int off;
2042
2043 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
2044 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002045 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02002046 return;
2047 }
2048
2049 if (ctrl->vs >= NVME_VS(1, 2, 1))
2050 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
2051
2052 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002053 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02002054 "nqn.2014.08.org.nvmexpress:%4x%4x",
2055 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002056 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002057 off += sizeof(id->sn);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002058 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002059 off += sizeof(id->mn);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002060 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
2061}
2062
2063static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
2064{
2065 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
2066 kfree(subsys);
2067}
2068
2069static void nvme_release_subsystem(struct device *dev)
2070{
2071 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
2072}
2073
2074static void nvme_destroy_subsystem(struct kref *ref)
2075{
2076 struct nvme_subsystem *subsys =
2077 container_of(ref, struct nvme_subsystem, ref);
2078
2079 mutex_lock(&nvme_subsystems_lock);
2080 list_del(&subsys->entry);
2081 mutex_unlock(&nvme_subsystems_lock);
2082
Christoph Hellwiged754e52017-11-09 13:50:43 +01002083 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002084 device_del(&subsys->dev);
2085 put_device(&subsys->dev);
2086}
2087
2088static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2089{
2090 kref_put(&subsys->ref, nvme_destroy_subsystem);
2091}
2092
2093static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2094{
2095 struct nvme_subsystem *subsys;
2096
2097 lockdep_assert_held(&nvme_subsystems_lock);
2098
2099 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2100 if (strcmp(subsys->subnqn, subsysnqn))
2101 continue;
2102 if (!kref_get_unless_zero(&subsys->ref))
2103 continue;
2104 return subsys;
2105 }
2106
2107 return NULL;
2108}
2109
Hannes Reinecke1e496932017-11-10 10:58:23 +01002110#define SUBSYS_ATTR_RO(_name, _mode, _show) \
2111 struct device_attribute subsys_attr_##_name = \
2112 __ATTR(_name, _mode, _show, NULL)
2113
2114static ssize_t nvme_subsys_show_nqn(struct device *dev,
2115 struct device_attribute *attr,
2116 char *buf)
2117{
2118 struct nvme_subsystem *subsys =
2119 container_of(dev, struct nvme_subsystem, dev);
2120
2121 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2122}
2123static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2124
2125#define nvme_subsys_show_str_function(field) \
2126static ssize_t subsys_##field##_show(struct device *dev, \
2127 struct device_attribute *attr, char *buf) \
2128{ \
2129 struct nvme_subsystem *subsys = \
2130 container_of(dev, struct nvme_subsystem, dev); \
2131 return sprintf(buf, "%.*s\n", \
2132 (int)sizeof(subsys->field), subsys->field); \
2133} \
2134static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2135
2136nvme_subsys_show_str_function(model);
2137nvme_subsys_show_str_function(serial);
2138nvme_subsys_show_str_function(firmware_rev);
2139
2140static struct attribute *nvme_subsys_attrs[] = {
2141 &subsys_attr_model.attr,
2142 &subsys_attr_serial.attr,
2143 &subsys_attr_firmware_rev.attr,
2144 &subsys_attr_subsysnqn.attr,
2145 NULL,
2146};
2147
2148static struct attribute_group nvme_subsys_attrs_group = {
2149 .attrs = nvme_subsys_attrs,
2150};
2151
2152static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2153 &nvme_subsys_attrs_group,
2154 NULL,
2155};
2156
Israel Rukshinb837b282018-01-04 17:56:14 +02002157static int nvme_active_ctrls(struct nvme_subsystem *subsys)
2158{
2159 int count = 0;
2160 struct nvme_ctrl *ctrl;
2161
2162 mutex_lock(&subsys->lock);
2163 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2164 if (ctrl->state != NVME_CTRL_DELETING &&
2165 ctrl->state != NVME_CTRL_DEAD)
2166 count++;
2167 }
2168 mutex_unlock(&subsys->lock);
2169
2170 return count;
2171}
2172
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002173static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2174{
2175 struct nvme_subsystem *subsys, *found;
2176 int ret;
2177
2178 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2179 if (!subsys)
2180 return -ENOMEM;
2181 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
2182 if (ret < 0) {
2183 kfree(subsys);
2184 return ret;
2185 }
2186 subsys->instance = ret;
2187 mutex_init(&subsys->lock);
2188 kref_init(&subsys->ref);
2189 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002190 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002191 nvme_init_subnqn(subsys, ctrl, id);
2192 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2193 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2194 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2195 subsys->vendor_id = le16_to_cpu(id->vid);
2196 subsys->cmic = id->cmic;
2197
2198 subsys->dev.class = nvme_subsys_class;
2199 subsys->dev.release = nvme_release_subsystem;
Hannes Reinecke1e496932017-11-10 10:58:23 +01002200 subsys->dev.groups = nvme_subsys_attrs_groups;
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002201 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
2202 device_initialize(&subsys->dev);
2203
2204 mutex_lock(&nvme_subsystems_lock);
2205 found = __nvme_find_get_subsystem(subsys->subnqn);
2206 if (found) {
2207 /*
2208 * Verify that the subsystem actually supports multiple
2209 * controllers, else bail out.
2210 */
Israel Rukshin16001c12018-06-10 10:31:10 +00002211 if (!(ctrl->opts && ctrl->opts->discovery_nqn) &&
Hannes Reinecke181303d2018-05-24 16:18:17 +02002212 nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002213 dev_err(ctrl->device,
2214 "ignoring ctrl due to duplicate subnqn (%s).\n",
2215 found->subnqn);
2216 nvme_put_subsystem(found);
2217 ret = -EINVAL;
2218 goto out_unlock;
2219 }
2220
2221 __nvme_release_subsystem(subsys);
2222 subsys = found;
2223 } else {
2224 ret = device_add(&subsys->dev);
2225 if (ret) {
2226 dev_err(ctrl->device,
2227 "failed to register subsystem device.\n");
2228 goto out_unlock;
2229 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002230 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002231 list_add_tail(&subsys->entry, &nvme_subsystems);
2232 }
2233
2234 ctrl->subsys = subsys;
2235 mutex_unlock(&nvme_subsystems_lock);
2236
2237 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2238 dev_name(ctrl->device))) {
2239 dev_err(ctrl->device,
2240 "failed to create sysfs link from subsystem.\n");
2241 /* the transport driver will eventually put the subsystem */
2242 return -EINVAL;
2243 }
2244
2245 mutex_lock(&subsys->lock);
2246 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2247 mutex_unlock(&subsys->lock);
2248
2249 return 0;
2250
2251out_unlock:
2252 mutex_unlock(&nvme_subsystems_lock);
2253 put_device(&subsys->dev);
2254 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02002255}
2256
Matias Bjørlingd558fb52018-03-21 20:27:07 +01002257int nvme_get_log_ext(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Javier Gonzáleza294c192018-03-30 00:05:17 +02002258 u8 log_page, void *log,
Matias Bjørling7ec60742018-04-12 09:16:03 -06002259 size_t size, u64 offset)
Matias Bjørling70da6092018-02-26 13:55:40 +01002260{
2261 struct nvme_command c = { };
2262 unsigned long dwlen = size / 4 - 1;
2263
2264 c.get_log_page.opcode = nvme_admin_get_log_page;
2265
2266 if (ns)
2267 c.get_log_page.nsid = cpu_to_le32(ns->head->ns_id);
2268 else
2269 c.get_log_page.nsid = cpu_to_le32(NVME_NSID_ALL);
2270
2271 c.get_log_page.lid = log_page;
2272 c.get_log_page.numdl = cpu_to_le16(dwlen & ((1 << 16) - 1));
2273 c.get_log_page.numdu = cpu_to_le16(dwlen >> 16);
Matias Bjørling7ec60742018-04-12 09:16:03 -06002274 c.get_log_page.lpol = cpu_to_le32(lower_32_bits(offset));
2275 c.get_log_page.lpou = cpu_to_le32(upper_32_bits(offset));
Matias Bjørling70da6092018-02-26 13:55:40 +01002276
2277 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2278}
2279
Keith Buschc627c482017-11-07 10:28:31 -07002280static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
2281 size_t size)
2282{
Matias Bjørling70da6092018-02-26 13:55:40 +01002283 return nvme_get_log_ext(ctrl, NULL, log_page, log, size, 0);
Keith Buschc627c482017-11-07 10:28:31 -07002284}
2285
Keith Busch84fef622017-11-07 10:28:32 -07002286static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2287{
2288 int ret;
2289
2290 if (!ctrl->effects)
2291 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2292
2293 if (!ctrl->effects)
2294 return 0;
2295
2296 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2297 sizeof(*ctrl->effects));
2298 if (ret) {
2299 kfree(ctrl->effects);
2300 ctrl->effects = NULL;
2301 }
2302 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002303}
2304
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002305/*
2306 * Initialize the cached copies of the Identify data and various controller
2307 * register in our nvme_ctrl structure. This should be called as soon as
2308 * the admin queue is fully up and running.
2309 */
2310int nvme_init_identify(struct nvme_ctrl *ctrl)
2311{
2312 struct nvme_id_ctrl *id;
2313 u64 cap;
2314 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002315 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002316 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002317
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002318 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2319 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002320 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002321 return ret;
2322 }
2323
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002324 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2325 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002326 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002327 return ret;
2328 }
2329 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2330
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002331 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002332 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2333
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002334 ret = nvme_identify_ctrl(ctrl, &id);
2335 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002336 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002337 return -EIO;
2338 }
2339
Keith Busch84fef622017-11-07 10:28:32 -07002340 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2341 ret = nvme_get_effects_log(ctrl);
2342 if (ret < 0)
Hannes Reinecke75c8b192018-05-25 11:06:27 +02002343 goto out_free;
Keith Busch84fef622017-11-07 10:28:32 -07002344 }
Christoph Hellwig180de0072017-06-26 12:39:02 +02002345
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002346 if (!ctrl->identified) {
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002347 int i;
2348
2349 ret = nvme_init_subsystem(ctrl, id);
2350 if (ret)
2351 goto out_free;
2352
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002353 /*
2354 * Check for quirks. Quirk can depend on firmware version,
2355 * so, in principle, the set of quirks present can change
2356 * across a reset. As a possible future enhancement, we
2357 * could re-scan for quirks every time we reinitialize
2358 * the device, but we'd have to make sure that the driver
2359 * behaves intelligently if the quirks change.
2360 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002361 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2362 if (quirk_matches(id, &core_quirks[i]))
2363 ctrl->quirks |= core_quirks[i].quirks;
2364 }
2365 }
2366
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002367 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002368 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 -07002369 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2370 }
2371
Scott Bauer8a9ae522017-02-17 13:59:40 +01002372 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002373 ctrl->oncs = le16_to_cpup(&id->oncs);
Hannes Reineckec0561f82018-05-22 11:09:55 +02002374 ctrl->oaes = le32_to_cpu(id->oaes);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002375 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002376 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002377 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002378 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002379 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002380 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002381 max_hw_sectors = UINT_MAX;
2382 ctrl->max_hw_sectors =
2383 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002384
Christoph Hellwigda358252016-03-02 18:07:11 +01002385 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002386 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002387 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002388
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002389 if (id->rtd3e) {
2390 /* us -> s */
2391 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2392
2393 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2394 shutdown_timeout, 60);
2395
2396 if (ctrl->shutdown_timeout != shutdown_timeout)
Max Gurtovoy1a3838d2017-12-31 15:33:27 +02002397 dev_info(ctrl->device,
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002398 "Shutdown timeout set to %u seconds\n",
2399 ctrl->shutdown_timeout);
2400 } else
2401 ctrl->shutdown_timeout = shutdown_timeout;
2402
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002403 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002404 ctrl->apsta = id->apsta;
2405 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002406 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2407 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002408 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 -04002409 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002410 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002411 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002412 }
2413 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002414 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002415 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002416 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2417
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002418 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002419 ctrl->icdoff = le16_to_cpu(id->icdoff);
2420 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2421 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2422 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2423
2424 /*
2425 * In fabrics we need to verify the cntlid matches the
2426 * admin connect
2427 */
Keith Busch634b8322017-08-10 11:23:31 +02002428 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002429 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002430 goto out_free;
2431 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002432
2433 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002434 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002435 "keep-alive support is mandatory for fabrics\n");
2436 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002437 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002438 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002439 } else {
2440 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002441 ctrl->hmpre = le32_to_cpu(id->hmpre);
2442 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002443 ctrl->hmminds = le32_to_cpu(id->hmminds);
2444 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002445 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002446
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002447 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002448
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002449 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002450 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002451 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002452 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2453
Keith Busch634b8322017-08-10 11:23:31 +02002454 ret = nvme_configure_apst(ctrl);
2455 if (ret < 0)
2456 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002457
2458 ret = nvme_configure_timestamp(ctrl);
2459 if (ret < 0)
2460 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002461
2462 ret = nvme_configure_directives(ctrl);
2463 if (ret < 0)
2464 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002465
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002466 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002467
Keith Busch634b8322017-08-10 11:23:31 +02002468 return 0;
2469
2470out_free:
2471 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002472 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002473}
Ming Lin576d55d2016-02-10 10:03:32 -08002474EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002475
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002476static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig21d34712015-11-26 09:08:36 +01002477{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002478 struct nvme_ctrl *ctrl =
2479 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig21d34712015-11-26 09:08:36 +01002480
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002481 switch (ctrl->state) {
2482 case NVME_CTRL_LIVE:
2483 case NVME_CTRL_ADMIN_ONLY:
2484 break;
2485 default:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002486 return -EWOULDBLOCK;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002487 }
2488
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002489 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002490 return 0;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002491}
2492
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002493static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2494{
2495 struct nvme_ns *ns;
2496 int ret;
2497
Jianchao Wang765cc032018-02-12 20:54:46 +08002498 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002499 if (list_empty(&ctrl->namespaces)) {
2500 ret = -ENOTTY;
2501 goto out_unlock;
2502 }
2503
2504 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2505 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002506 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002507 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2508 ret = -EINVAL;
2509 goto out_unlock;
2510 }
2511
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002512 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002513 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2514 kref_get(&ns->kref);
Jianchao Wang765cc032018-02-12 20:54:46 +08002515 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002516
2517 ret = nvme_user_cmd(ctrl, ns, argp);
2518 nvme_put_ns(ns);
2519 return ret;
2520
2521out_unlock:
Jianchao Wang765cc032018-02-12 20:54:46 +08002522 up_read(&ctrl->namespaces_rwsem);
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002523 return ret;
2524}
2525
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002526static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2527 unsigned long arg)
2528{
2529 struct nvme_ctrl *ctrl = file->private_data;
2530 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002531
2532 switch (cmd) {
2533 case NVME_IOCTL_ADMIN_CMD:
2534 return nvme_user_cmd(ctrl, NULL, argp);
2535 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002536 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002537 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002538 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002539 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002540 case NVME_IOCTL_SUBSYS_RESET:
2541 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002542 case NVME_IOCTL_RESCAN:
2543 nvme_queue_scan(ctrl);
2544 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002545 default:
2546 return -ENOTTY;
2547 }
2548}
2549
2550static const struct file_operations nvme_dev_fops = {
2551 .owner = THIS_MODULE,
2552 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002553 .unlocked_ioctl = nvme_dev_ioctl,
2554 .compat_ioctl = nvme_dev_ioctl,
2555};
2556
2557static ssize_t nvme_sysfs_reset(struct device *dev,
2558 struct device_attribute *attr, const char *buf,
2559 size_t count)
2560{
2561 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2562 int ret;
2563
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002564 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002565 if (ret < 0)
2566 return ret;
2567 return count;
2568}
2569static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2570
Keith Busch9ec3bb22016-04-29 15:45:18 -06002571static ssize_t nvme_sysfs_rescan(struct device *dev,
2572 struct device_attribute *attr, const char *buf,
2573 size_t count)
2574{
2575 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2576
2577 nvme_queue_scan(ctrl);
2578 return count;
2579}
2580static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2581
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002582static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
Keith Busch118472a2016-02-18 09:57:48 -07002583{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002584 struct gendisk *disk = dev_to_disk(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002585
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002586 if (disk->fops == &nvme_fops)
2587 return nvme_get_ns_from_dev(dev)->head;
2588 else
2589 return disk->private_data;
2590}
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002591
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002592static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2593 char *buf)
2594{
2595 struct nvme_ns_head *head = dev_to_ns_head(dev);
2596 struct nvme_ns_ids *ids = &head->ids;
2597 struct nvme_subsystem *subsys = head->subsys;
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002598 int serial_len = sizeof(subsys->serial);
2599 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002600
Christoph Hellwig002fab02017-11-09 13:50:16 +01002601 if (!uuid_is_null(&ids->uuid))
2602 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Keith Busch118472a2016-02-18 09:57:48 -07002603
Christoph Hellwig002fab02017-11-09 13:50:16 +01002604 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2605 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002606
Christoph Hellwig002fab02017-11-09 13:50:16 +01002607 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2608 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002609
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002610 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2611 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002612 serial_len--;
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002613 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2614 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002615 model_len--;
2616
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002617 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2618 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002619 head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002620}
Joe Perchesc828a892017-12-19 10:15:08 -08002621static DEVICE_ATTR_RO(wwid);
Keith Busch118472a2016-02-18 09:57:48 -07002622
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002623static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002624 char *buf)
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002625{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002626 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002627}
Joe Perchesc828a892017-12-19 10:15:08 -08002628static DEVICE_ATTR_RO(nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002629
Keith Busch2b9b6e82015-12-22 10:10:45 -07002630static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002631 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002632{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002633 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002634
2635 /* For backward compatibility expose the NGUID to userspace if
2636 * we have no UUID set
2637 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002638 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002639 printk_ratelimited(KERN_WARNING
2640 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002641 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002642 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002643 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002644}
Joe Perchesc828a892017-12-19 10:15:08 -08002645static DEVICE_ATTR_RO(uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002646
2647static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002648 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002649{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002650 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002651}
Joe Perchesc828a892017-12-19 10:15:08 -08002652static DEVICE_ATTR_RO(eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002653
2654static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002655 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002656{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002657 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002658}
Joe Perchesc828a892017-12-19 10:15:08 -08002659static DEVICE_ATTR_RO(nsid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002660
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002661static struct attribute *nvme_ns_id_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002662 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002663 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002664 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002665 &dev_attr_eui.attr,
2666 &dev_attr_nsid.attr,
2667 NULL,
2668};
2669
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002670static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002671 struct attribute *a, int n)
2672{
2673 struct device *dev = container_of(kobj, struct device, kobj);
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002674 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002675
2676 if (a == &dev_attr_uuid.attr) {
Martin Wilcka04b5de2017-09-28 21:33:23 +02002677 if (uuid_is_null(&ids->uuid) &&
Christoph Hellwig002fab02017-11-09 13:50:16 +01002678 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002679 return 0;
2680 }
2681 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002682 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002683 return 0;
2684 }
2685 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002686 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002687 return 0;
2688 }
2689 return a->mode;
2690}
2691
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002692const struct attribute_group nvme_ns_id_attr_group = {
2693 .attrs = nvme_ns_id_attrs,
2694 .is_visible = nvme_ns_id_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002695};
2696
Ming Lin931e1c22016-02-26 13:24:19 -08002697#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002698static ssize_t field##_show(struct device *dev, \
2699 struct device_attribute *attr, char *buf) \
2700{ \
2701 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002702 return sprintf(buf, "%.*s\n", \
2703 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002704} \
2705static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2706
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002707nvme_show_str_function(model);
2708nvme_show_str_function(serial);
2709nvme_show_str_function(firmware_rev);
2710
Ming Lin931e1c22016-02-26 13:24:19 -08002711#define nvme_show_int_function(field) \
2712static ssize_t field##_show(struct device *dev, \
2713 struct device_attribute *attr, char *buf) \
2714{ \
2715 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2716 return sprintf(buf, "%d\n", ctrl->field); \
2717} \
2718static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2719
Ming Lin931e1c22016-02-26 13:24:19 -08002720nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002721
Ming Lin1a353d82016-06-13 16:45:24 +02002722static ssize_t nvme_sysfs_delete(struct device *dev,
2723 struct device_attribute *attr, const char *buf,
2724 size_t count)
2725{
2726 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2727
2728 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002729 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002730 return count;
2731}
2732static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2733
2734static ssize_t nvme_sysfs_show_transport(struct device *dev,
2735 struct device_attribute *attr,
2736 char *buf)
2737{
2738 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2739
2740 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2741}
2742static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2743
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002744static ssize_t nvme_sysfs_show_state(struct device *dev,
2745 struct device_attribute *attr,
2746 char *buf)
2747{
2748 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2749 static const char *const state_name[] = {
2750 [NVME_CTRL_NEW] = "new",
2751 [NVME_CTRL_LIVE] = "live",
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002752 [NVME_CTRL_ADMIN_ONLY] = "only-admin",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002753 [NVME_CTRL_RESETTING] = "resetting",
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002754 [NVME_CTRL_CONNECTING] = "connecting",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002755 [NVME_CTRL_DELETING] = "deleting",
2756 [NVME_CTRL_DEAD] = "dead",
2757 };
2758
2759 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2760 state_name[ctrl->state])
2761 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2762
2763 return sprintf(buf, "unknown state\n");
2764}
2765
2766static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2767
Ming Lin1a353d82016-06-13 16:45:24 +02002768static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2769 struct device_attribute *attr,
2770 char *buf)
2771{
2772 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2773
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01002774 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002775}
2776static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2777
2778static ssize_t nvme_sysfs_show_address(struct device *dev,
2779 struct device_attribute *attr,
2780 char *buf)
2781{
2782 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2783
2784 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2785}
2786static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2787
Keith Busch779ff7562016-01-12 15:09:31 -07002788static struct attribute *nvme_dev_attrs[] = {
2789 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002790 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002791 &dev_attr_model.attr,
2792 &dev_attr_serial.attr,
2793 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002794 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002795 &dev_attr_delete_controller.attr,
2796 &dev_attr_transport.attr,
2797 &dev_attr_subsysnqn.attr,
2798 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002799 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002800 NULL
2801};
2802
Ming Lin1a353d82016-06-13 16:45:24 +02002803static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2804 struct attribute *a, int n)
2805{
2806 struct device *dev = container_of(kobj, struct device, kobj);
2807 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2808
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002809 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2810 return 0;
2811 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2812 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002813
2814 return a->mode;
2815}
2816
Keith Busch779ff7562016-01-12 15:09:31 -07002817static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002818 .attrs = nvme_dev_attrs,
2819 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002820};
2821
2822static const struct attribute_group *nvme_dev_attr_groups[] = {
2823 &nvme_dev_attrs_group,
2824 NULL,
2825};
2826
Christoph Hellwiged754e52017-11-09 13:50:43 +01002827static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2828 unsigned nsid)
2829{
2830 struct nvme_ns_head *h;
2831
2832 lockdep_assert_held(&subsys->lock);
2833
2834 list_for_each_entry(h, &subsys->nsheads, entry) {
2835 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2836 return h;
2837 }
2838
2839 return NULL;
2840}
2841
2842static int __nvme_check_ids(struct nvme_subsystem *subsys,
2843 struct nvme_ns_head *new)
2844{
2845 struct nvme_ns_head *h;
2846
2847 lockdep_assert_held(&subsys->lock);
2848
2849 list_for_each_entry(h, &subsys->nsheads, entry) {
2850 if (nvme_ns_ids_valid(&new->ids) &&
Keith Busch20796992018-03-19 10:53:50 -06002851 !list_empty(&h->list) &&
Christoph Hellwiged754e52017-11-09 13:50:43 +01002852 nvme_ns_ids_equal(&new->ids, &h->ids))
2853 return -EINVAL;
2854 }
2855
2856 return 0;
2857}
2858
2859static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2860 unsigned nsid, struct nvme_id_ns *id)
2861{
2862 struct nvme_ns_head *head;
2863 int ret = -ENOMEM;
2864
2865 head = kzalloc(sizeof(*head), GFP_KERNEL);
2866 if (!head)
2867 goto out;
2868 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2869 if (ret < 0)
2870 goto out_free_head;
2871 head->instance = ret;
2872 INIT_LIST_HEAD(&head->list);
Max Gurtovoyfd92c772018-04-12 09:16:12 -06002873 ret = init_srcu_struct(&head->srcu);
2874 if (ret)
2875 goto out_ida_remove;
Christoph Hellwiged754e52017-11-09 13:50:43 +01002876 head->subsys = ctrl->subsys;
2877 head->ns_id = nsid;
2878 kref_init(&head->ref);
2879
2880 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2881
2882 ret = __nvme_check_ids(ctrl->subsys, head);
2883 if (ret) {
2884 dev_err(ctrl->device,
2885 "duplicate IDs for nsid %d\n", nsid);
2886 goto out_cleanup_srcu;
2887 }
2888
Christoph Hellwig32acab32017-11-02 12:59:30 +01002889 ret = nvme_mpath_alloc_disk(ctrl, head);
2890 if (ret)
2891 goto out_cleanup_srcu;
2892
Christoph Hellwiged754e52017-11-09 13:50:43 +01002893 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
Jianchao Wang12d9f072018-05-04 16:01:57 +08002894
2895 kref_get(&ctrl->subsys->ref);
2896
Christoph Hellwiged754e52017-11-09 13:50:43 +01002897 return head;
2898out_cleanup_srcu:
2899 cleanup_srcu_struct(&head->srcu);
Max Gurtovoyfd92c772018-04-12 09:16:12 -06002900out_ida_remove:
Christoph Hellwiged754e52017-11-09 13:50:43 +01002901 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2902out_free_head:
2903 kfree(head);
2904out:
2905 return ERR_PTR(ret);
2906}
2907
2908static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002909 struct nvme_id_ns *id)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002910{
2911 struct nvme_ctrl *ctrl = ns->ctrl;
2912 bool is_shared = id->nmic & (1 << 0);
2913 struct nvme_ns_head *head = NULL;
2914 int ret = 0;
2915
2916 mutex_lock(&ctrl->subsys->lock);
2917 if (is_shared)
2918 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2919 if (!head) {
2920 head = nvme_alloc_ns_head(ctrl, nsid, id);
2921 if (IS_ERR(head)) {
2922 ret = PTR_ERR(head);
2923 goto out_unlock;
2924 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002925 } else {
2926 struct nvme_ns_ids ids;
2927
2928 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2929 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2930 dev_err(ctrl->device,
2931 "IDs don't match for shared namespace %d\n",
2932 nsid);
2933 ret = -EINVAL;
2934 goto out_unlock;
2935 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002936 }
2937
2938 list_add_tail(&ns->siblings, &head->list);
2939 ns->head = head;
2940
2941out_unlock:
2942 mutex_unlock(&ctrl->subsys->lock);
2943 return ret;
2944}
2945
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002946static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2947{
2948 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2949 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2950
Christoph Hellwiged754e52017-11-09 13:50:43 +01002951 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002952}
2953
Keith Busch32f0c4a2016-07-13 11:45:02 -06002954static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002955{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002956 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002957
Jianchao Wang765cc032018-02-12 20:54:46 +08002958 down_read(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002959 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002960 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002961 if (!kref_get_unless_zero(&ns->kref))
2962 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002963 ret = ns;
2964 break;
2965 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002966 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002967 break;
2968 }
Jianchao Wang765cc032018-02-12 20:54:46 +08002969 up_read(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06002970 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002971}
2972
Jens Axboef5d11842017-06-27 12:03:06 -06002973static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2974{
2975 struct streams_directive_params s;
2976 int ret;
2977
2978 if (!ctrl->nr_streams)
2979 return 0;
2980
Christoph Hellwiged754e52017-11-09 13:50:43 +01002981 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002982 if (ret)
2983 return ret;
2984
2985 ns->sws = le32_to_cpu(s.sws);
2986 ns->sgs = le16_to_cpu(s.sgs);
2987
2988 if (ns->sws) {
2989 unsigned int bs = 1 << ns->lba_shift;
2990
2991 blk_queue_io_min(ns->queue, bs * ns->sws);
2992 if (ns->sgs)
2993 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2994 }
2995
2996 return 0;
2997}
2998
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002999static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3000{
3001 struct nvme_ns *ns;
3002 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003003 struct nvme_id_ns *id;
3004 char disk_name[DISK_NAME_LEN];
Christoph Hellwig32acab32017-11-02 12:59:30 +01003005 int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003006
3007 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
3008 if (!ns)
3009 return;
3010
3011 ns->queue = blk_mq_init_queue(ctrl->tagset);
3012 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01003013 goto out_free_ns;
Bart Van Assche8b904b52018-03-07 17:10:10 -08003014 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003015 ns->queue->queuedata = ns;
3016 ns->ctrl = ctrl;
3017
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003018 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003019 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003020
3021 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01003022 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003023
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02003024 id = nvme_identify_ns(ctrl, nsid);
3025 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003026 goto out_free_queue;
3027
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02003028 if (id->ncap == 0)
3029 goto out_free_id;
3030
Baegjae Sung9bd82b12018-02-28 16:06:04 +09003031 if (nvme_init_ns_head(ns, nsid, id))
Christoph Hellwiged754e52017-11-09 13:50:43 +01003032 goto out_free_id;
Keith Busch654b4a42017-12-14 11:20:32 -07003033 nvme_setup_streams_ns(ctrl, ns);
Keith Buscha785dbc2018-04-26 14:22:41 -06003034 nvme_set_disk_name(disk_name, ns, ctrl, &flags);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02003035
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003036 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
3037 if (nvme_nvm_register(ns, disk_name, node)) {
3038 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01003039 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003040 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003041 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003042
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003043 disk = alloc_disk_node(0, node);
3044 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01003045 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003046
3047 disk->fops = &nvme_fops;
3048 disk->private_data = ns;
3049 disk->queue = ns->queue;
Christoph Hellwig32acab32017-11-02 12:59:30 +01003050 disk->flags = flags;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003051 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
3052 ns->disk = disk;
3053
3054 __nvme_revalidate_disk(disk, id);
3055
Jianchao Wang765cc032018-02-12 20:54:46 +08003056 down_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003057 list_add_tail(&ns->list, &ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003058 up_write(&ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003059
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003060 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003061
3062 kfree(id);
3063
Dan Williams0d52c7562016-06-15 19:44:20 -07003064 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003065 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003066 &nvme_ns_id_attr_group))
Keith Busch2b9b6e82015-12-22 10:10:45 -07003067 pr_warn("%s: failed to create sysfs group for identification\n",
3068 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003069 if (ns->ndev && nvme_nvm_register_sysfs(ns))
3070 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
3071 ns->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003072
Baegjae Sung9bd82b12018-02-28 16:06:04 +09003073 nvme_mpath_add_disk(ns->head);
Thomas Taib9e03852018-02-08 13:38:29 -05003074 nvme_fault_inject_init(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003075 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01003076 out_unlink_ns:
3077 mutex_lock(&ctrl->subsys->lock);
3078 list_del_rcu(&ns->siblings);
3079 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003080 out_free_id:
3081 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003082 out_free_queue:
3083 blk_cleanup_queue(ns->queue);
3084 out_free_ns:
3085 kfree(ns);
3086}
3087
3088static void nvme_ns_remove(struct nvme_ns *ns)
3089{
Keith Busch646017a2016-02-24 09:15:54 -07003090 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3091 return;
3092
Thomas Taib9e03852018-02-08 13:38:29 -05003093 nvme_fault_inject_fini(ns);
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003094 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Keith Busch2b9b6e82015-12-22 10:10:45 -07003095 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003096 &nvme_ns_id_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003097 if (ns->ndev)
3098 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003099 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003100 blk_cleanup_queue(ns->queue);
Ming Leibd9f5d62017-12-06 18:30:09 +08003101 if (blk_get_integrity(ns->disk))
3102 blk_integrity_unregister(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003103 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003104
Christoph Hellwiged754e52017-11-09 13:50:43 +01003105 mutex_lock(&ns->ctrl->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003106 nvme_mpath_clear_current_path(ns);
Keith Busch9941a862017-11-16 13:36:50 -07003107 list_del_rcu(&ns->siblings);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003108 mutex_unlock(&ns->ctrl->subsys->lock);
3109
Jianchao Wang765cc032018-02-12 20:54:46 +08003110 down_write(&ns->ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003111 list_del_init(&ns->list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003112 up_write(&ns->ctrl->namespaces_rwsem);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003113
Keith Busch9941a862017-11-16 13:36:50 -07003114 synchronize_srcu(&ns->head->srcu);
Sagi Grimberg479a3222017-12-21 15:07:27 +02003115 nvme_mpath_check_last_path(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003116 nvme_put_ns(ns);
3117}
3118
Keith Busch540c8012015-10-22 15:45:06 -06003119static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3120{
3121 struct nvme_ns *ns;
3122
Keith Busch32f0c4a2016-07-13 11:45:02 -06003123 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06003124 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003125 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06003126 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003127 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06003128 } else
3129 nvme_alloc_ns(ctrl, nsid);
3130}
3131
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303132static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3133 unsigned nsid)
3134{
3135 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003136 LIST_HEAD(rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303137
Jianchao Wang765cc032018-02-12 20:54:46 +08003138 down_write(&ctrl->namespaces_rwsem);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303139 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01003140 if (ns->head->ns_id > nsid)
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003141 list_move_tail(&ns->list, &rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303142 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003143 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003144
3145 list_for_each_entry_safe(ns, next, &rm_list, list)
3146 nvme_ns_remove(ns);
3147
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303148}
3149
Keith Busch540c8012015-10-22 15:45:06 -06003150static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3151{
3152 struct nvme_ns *ns;
3153 __le32 *ns_list;
3154 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
3155 int ret = 0;
3156
Minwoo Im42595eb2018-02-08 22:56:31 +09003157 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
Keith Busch540c8012015-10-22 15:45:06 -06003158 if (!ns_list)
3159 return -ENOMEM;
3160
3161 for (i = 0; i < num_lists; i++) {
3162 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3163 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303164 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06003165
3166 for (j = 0; j < min(nn, 1024U); j++) {
3167 nsid = le32_to_cpu(ns_list[j]);
3168 if (!nsid)
3169 goto out;
3170
3171 nvme_validate_ns(ctrl, nsid);
3172
3173 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06003174 ns = nvme_find_get_ns(ctrl, prev);
3175 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06003176 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003177 nvme_put_ns(ns);
3178 }
Keith Busch540c8012015-10-22 15:45:06 -06003179 }
3180 }
3181 nn -= j;
3182 }
3183 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303184 nvme_remove_invalid_namespaces(ctrl, prev);
3185 free:
Keith Busch540c8012015-10-22 15:45:06 -06003186 kfree(ns_list);
3187 return ret;
3188}
3189
Christoph Hellwig5955be22016-04-26 13:51:59 +02003190static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003191{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003192 unsigned i;
3193
Keith Busch540c8012015-10-22 15:45:06 -06003194 for (i = 1; i <= nn; i++)
3195 nvme_validate_ns(ctrl, i);
3196
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303197 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003198}
3199
Christoph Hellwigf493af32018-06-07 13:47:33 +02003200static void nvme_clear_changed_ns_log(struct nvme_ctrl *ctrl)
Christoph Hellwig30d90962018-05-25 18:17:41 +02003201{
3202 size_t log_size = NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32);
3203 __le32 *log;
Christoph Hellwigf493af32018-06-07 13:47:33 +02003204 int error;
Christoph Hellwig30d90962018-05-25 18:17:41 +02003205
3206 log = kzalloc(log_size, GFP_KERNEL);
3207 if (!log)
Christoph Hellwigf493af32018-06-07 13:47:33 +02003208 return;
Christoph Hellwig30d90962018-05-25 18:17:41 +02003209
Christoph Hellwigf493af32018-06-07 13:47:33 +02003210 /*
3211 * We need to read the log to clear the AEN, but we don't want to rely
3212 * on it for the changed namespace information as userspace could have
3213 * raced with us in reading the log page, which could cause us to miss
3214 * updates.
3215 */
Christoph Hellwig30d90962018-05-25 18:17:41 +02003216 error = nvme_get_log(ctrl, NVME_LOG_CHANGED_NS, log, log_size);
Christoph Hellwigf493af32018-06-07 13:47:33 +02003217 if (error)
Christoph Hellwig30d90962018-05-25 18:17:41 +02003218 dev_warn(ctrl->device,
3219 "reading changed ns log failed: %d\n", error);
Christoph Hellwig30d90962018-05-25 18:17:41 +02003220
Christoph Hellwig30d90962018-05-25 18:17:41 +02003221 kfree(log);
Christoph Hellwig30d90962018-05-25 18:17:41 +02003222}
3223
Christoph Hellwig5955be22016-04-26 13:51:59 +02003224static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003225{
Christoph Hellwig5955be22016-04-26 13:51:59 +02003226 struct nvme_ctrl *ctrl =
3227 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003228 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06003229 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003230
Christoph Hellwig5955be22016-04-26 13:51:59 +02003231 if (ctrl->state != NVME_CTRL_LIVE)
3232 return;
3233
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003234 WARN_ON_ONCE(!ctrl->tagset);
3235
Dan Carpenter77016192018-06-07 11:27:41 +03003236 if (test_and_clear_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events)) {
Christoph Hellwig30d90962018-05-25 18:17:41 +02003237 dev_info(ctrl->device, "rescanning namespaces.\n");
Christoph Hellwigf493af32018-06-07 13:47:33 +02003238 nvme_clear_changed_ns_log(ctrl);
Christoph Hellwig30d90962018-05-25 18:17:41 +02003239 }
3240
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003241 if (nvme_identify_ctrl(ctrl, &id))
3242 return;
Keith Busch540c8012015-10-22 15:45:06 -06003243
3244 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06003245 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06003246 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3247 if (!nvme_scan_ns_list(ctrl, nn))
Christoph Hellwig30d90962018-05-25 18:17:41 +02003248 goto out_free_id;
Keith Busch540c8012015-10-22 15:45:06 -06003249 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02003250 nvme_scan_ns_sequential(ctrl, nn);
Christoph Hellwig30d90962018-05-25 18:17:41 +02003251out_free_id:
3252 kfree(id);
Jianchao Wang765cc032018-02-12 20:54:46 +08003253 down_write(&ctrl->namespaces_rwsem);
Keith Busch540c8012015-10-22 15:45:06 -06003254 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Jianchao Wang765cc032018-02-12 20:54:46 +08003255 up_write(&ctrl->namespaces_rwsem);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003256}
Christoph Hellwig5955be22016-04-26 13:51:59 +02003257
Keith Busch32f0c4a2016-07-13 11:45:02 -06003258/*
3259 * This function iterates the namespace list unlocked to allow recovery from
3260 * controller failure. It is up to the caller to ensure the namespace list is
3261 * not modified by scan work while this function is executing.
3262 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003263void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3264{
3265 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003266 LIST_HEAD(ns_list);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003267
Keith Busch0ff9d4e2016-05-12 08:37:14 -06003268 /*
3269 * The dead states indicates the controller was not gracefully
3270 * disconnected. In that case, we won't be able to flush any data while
3271 * removing the namespaces' disks; fail all the queues now to avoid
3272 * potentially having to clean up the failed sync later.
3273 */
3274 if (ctrl->state == NVME_CTRL_DEAD)
3275 nvme_kill_queues(ctrl);
3276
Jianchao Wang765cc032018-02-12 20:54:46 +08003277 down_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003278 list_splice_init(&ctrl->namespaces, &ns_list);
Jianchao Wang765cc032018-02-12 20:54:46 +08003279 up_write(&ctrl->namespaces_rwsem);
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003280
3281 list_for_each_entry_safe(ns, next, &ns_list, list)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003282 nvme_ns_remove(ns);
3283}
Ming Lin576d55d2016-02-10 10:03:32 -08003284EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003285
Keith Busche3d78742017-11-07 15:13:14 -07003286static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3287{
3288 char *envp[2] = { NULL, NULL };
3289 u32 aen_result = ctrl->aen_result;
3290
3291 ctrl->aen_result = 0;
3292 if (!aen_result)
3293 return;
3294
3295 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3296 if (!envp[0])
3297 return;
3298 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3299 kfree(envp[0]);
3300}
3301
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003302static void nvme_async_event_work(struct work_struct *work)
3303{
3304 struct nvme_ctrl *ctrl =
3305 container_of(work, struct nvme_ctrl, async_event_work);
3306
Keith Busche3d78742017-11-07 15:13:14 -07003307 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07003308 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003309}
3310
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303311static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3312{
3313
3314 u32 csts;
3315
3316 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3317 return false;
3318
3319 if (csts == ~0)
3320 return false;
3321
3322 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3323}
3324
3325static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3326{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303327 struct nvme_fw_slot_info_log *log;
3328
3329 log = kmalloc(sizeof(*log), GFP_KERNEL);
3330 if (!log)
3331 return;
3332
Keith Buschc627c482017-11-07 10:28:31 -07003333 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303334 dev_warn(ctrl->device,
3335 "Get FW SLOT INFO log error\n");
3336 kfree(log);
3337}
3338
3339static void nvme_fw_act_work(struct work_struct *work)
3340{
3341 struct nvme_ctrl *ctrl = container_of(work,
3342 struct nvme_ctrl, fw_act_work);
3343 unsigned long fw_act_timeout;
3344
3345 if (ctrl->mtfa)
3346 fw_act_timeout = jiffies +
3347 msecs_to_jiffies(ctrl->mtfa * 100);
3348 else
3349 fw_act_timeout = jiffies +
3350 msecs_to_jiffies(admin_timeout * 1000);
3351
3352 nvme_stop_queues(ctrl);
3353 while (nvme_ctrl_pp_status(ctrl)) {
3354 if (time_after(jiffies, fw_act_timeout)) {
3355 dev_warn(ctrl->device,
3356 "Fw activation timeout, reset controller\n");
3357 nvme_reset_ctrl(ctrl);
3358 break;
3359 }
3360 msleep(100);
3361 }
3362
3363 if (ctrl->state != NVME_CTRL_LIVE)
3364 return;
3365
3366 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003367 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303368 nvme_get_fw_slot_info(ctrl);
3369}
3370
Christoph Hellwig868c2392018-05-22 11:09:54 +02003371static void nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003372{
Christoph Hellwig868c2392018-05-22 11:09:54 +02003373 switch ((result & 0xff00) >> 8) {
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003374 case NVME_AER_NOTICE_NS_CHANGED:
Dan Carpenter77016192018-06-07 11:27:41 +03003375 set_bit(NVME_AER_NOTICE_NS_CHANGED, &ctrl->events);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003376 nvme_queue_scan(ctrl);
3377 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303378 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003379 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303380 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003381 default:
3382 dev_warn(ctrl->device, "async event result %08x\n", result);
3383 }
Christoph Hellwig868c2392018-05-22 11:09:54 +02003384}
3385
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003386void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003387 volatile union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003388{
3389 u32 result = le32_to_cpu(res->u32);
3390
3391 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
3392 return;
3393
3394 switch (result & 0x7) {
Christoph Hellwig868c2392018-05-22 11:09:54 +02003395 case NVME_AER_NOTICE:
3396 nvme_handle_aen_notice(ctrl, result);
3397 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003398 case NVME_AER_ERROR:
3399 case NVME_AER_SMART:
3400 case NVME_AER_CSS:
3401 case NVME_AER_VS:
3402 ctrl->aen_result = result;
3403 break;
3404 default:
3405 break;
3406 }
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003407 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003408}
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003409EXPORT_SYMBOL_GPL(nvme_complete_async_event);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003410
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003411void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003412{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003413 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003414 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003415 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303416 cancel_work_sync(&ctrl->fw_act_work);
Nitzan Carmib435ece2018-03-20 11:07:30 +00003417 if (ctrl->ops->stop_ctrl)
3418 ctrl->ops->stop_ctrl(ctrl);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003419}
3420EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003421
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003422void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3423{
3424 if (ctrl->kato)
3425 nvme_start_keep_alive(ctrl);
3426
3427 if (ctrl->queue_count > 1) {
3428 nvme_queue_scan(ctrl);
Hannes Reineckec0561f82018-05-22 11:09:55 +02003429 nvme_enable_aen(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003430 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003431 nvme_start_queues(ctrl);
3432 }
3433}
3434EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3435
3436void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3437{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003438 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003439}
Ming Lin576d55d2016-02-10 10:03:32 -08003440EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003441
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003442static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003443{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003444 struct nvme_ctrl *ctrl =
3445 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003446 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003447
Christoph Hellwig9843f682017-10-18 13:10:01 +02003448 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003449 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003450
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003451 if (subsys) {
3452 mutex_lock(&subsys->lock);
3453 list_del(&ctrl->subsys_entry);
3454 mutex_unlock(&subsys->lock);
3455 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3456 }
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003457
3458 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003459
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003460 if (subsys)
3461 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003462}
3463
3464/*
3465 * Initialize a NVMe controller structures. This needs to be called during
3466 * earliest initialization so that we have the initialized structured around
3467 * during probing.
3468 */
3469int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3470 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3471{
3472 int ret;
3473
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003474 ctrl->state = NVME_CTRL_NEW;
3475 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003476 INIT_LIST_HEAD(&ctrl->namespaces);
Jianchao Wang765cc032018-02-12 20:54:46 +08003477 init_rwsem(&ctrl->namespaces_rwsem);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003478 ctrl->dev = dev;
3479 ctrl->ops = ops;
3480 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003481 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003482 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303483 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003484 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003485
Christoph Hellwig9843f682017-10-18 13:10:01 +02003486 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3487 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003488 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003489 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003490
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003491 device_initialize(&ctrl->ctrl_device);
3492 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003493 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003494 ctrl->device->class = nvme_class;
3495 ctrl->device->parent = ctrl->dev;
3496 ctrl->device->groups = nvme_dev_attr_groups;
3497 ctrl->device->release = nvme_free_ctrl;
3498 dev_set_drvdata(ctrl->device, ctrl);
3499 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3500 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003501 goto out_release_instance;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003502
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003503 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3504 ctrl->cdev.owner = ops->module;
3505 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003506 if (ret)
3507 goto out_free_name;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003508
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003509 /*
3510 * Initialize latency tolerance controls. The sysfs files won't
3511 * be visible to userspace unless the device actually supports APST.
3512 */
3513 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3514 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3515 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3516
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003517 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003518out_free_name:
3519 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003520out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003521 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003522out:
3523 return ret;
3524}
Ming Lin576d55d2016-02-10 10:03:32 -08003525EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003526
Keith Busch69d9a992016-02-24 09:15:56 -07003527/**
3528 * nvme_kill_queues(): Ends all namespace queues
3529 * @ctrl: the dead controller that needs to end
3530 *
3531 * Call this function when the driver determines it is unable to get the
3532 * controller in a state capable of servicing IO.
3533 */
3534void nvme_kill_queues(struct nvme_ctrl *ctrl)
3535{
3536 struct nvme_ns *ns;
3537
Jianchao Wang765cc032018-02-12 20:54:46 +08003538 down_read(&ctrl->namespaces_rwsem);
Ming Lei82654b62017-06-02 16:32:08 +08003539
Ming Lei443bd902017-06-19 10:21:08 +08003540 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003541 if (ctrl->admin_q)
3542 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003543
Keith Busch32f0c4a2016-07-13 11:45:02 -06003544 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003545 /*
3546 * Revalidating a dead namespace sets capacity to 0. This will
3547 * end buffered writers dirtying pages that can't be synced.
3548 */
Keith Buschf33447b2017-02-10 18:15:51 -05003549 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3550 continue;
3551 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003552 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003553
Ming Lei443bd902017-06-19 10:21:08 +08003554 /* Forcibly unquiesce queues to avoid blocking dispatch */
3555 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003556 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003557 up_read(&ctrl->namespaces_rwsem);
Keith Busch69d9a992016-02-24 09:15:56 -07003558}
Linus Torvalds237045f2016-03-18 17:13:31 -07003559EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003560
Keith Busch302ad8c2017-03-01 14:22:12 -05003561void nvme_unfreeze(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)
3567 blk_mq_unfreeze_queue(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_unfreeze);
3571
3572void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3573{
3574 struct nvme_ns *ns;
3575
Jianchao Wang765cc032018-02-12 20:54:46 +08003576 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003577 list_for_each_entry(ns, &ctrl->namespaces, list) {
3578 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3579 if (timeout <= 0)
3580 break;
3581 }
Jianchao Wang765cc032018-02-12 20:54:46 +08003582 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003583}
3584EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3585
3586void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3587{
3588 struct nvme_ns *ns;
3589
Jianchao Wang765cc032018-02-12 20:54:46 +08003590 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003591 list_for_each_entry(ns, &ctrl->namespaces, list)
3592 blk_mq_freeze_queue_wait(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003593 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003594}
3595EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3596
3597void nvme_start_freeze(struct nvme_ctrl *ctrl)
3598{
3599 struct nvme_ns *ns;
3600
Jianchao Wang765cc032018-02-12 20:54:46 +08003601 down_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003602 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003603 blk_freeze_queue_start(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003604 up_read(&ctrl->namespaces_rwsem);
Keith Busch302ad8c2017-03-01 14:22:12 -05003605}
3606EXPORT_SYMBOL_GPL(nvme_start_freeze);
3607
Keith Busch25646262016-01-04 09:10:57 -07003608void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003609{
3610 struct nvme_ns *ns;
3611
Jianchao Wang765cc032018-02-12 20:54:46 +08003612 down_read(&ctrl->namespaces_rwsem);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003613 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003614 blk_mq_quiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003615 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003616}
Ming Lin576d55d2016-02-10 10:03:32 -08003617EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003618
Keith Busch25646262016-01-04 09:10:57 -07003619void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003620{
3621 struct nvme_ns *ns;
3622
Jianchao Wang765cc032018-02-12 20:54:46 +08003623 down_read(&ctrl->namespaces_rwsem);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003624 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003625 blk_mq_unquiesce_queue(ns->queue);
Jianchao Wang765cc032018-02-12 20:54:46 +08003626 up_read(&ctrl->namespaces_rwsem);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003627}
Ming Lin576d55d2016-02-10 10:03:32 -08003628EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003629
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003630int __init nvme_core_init(void)
3631{
Roy Shtermanb227c592018-01-14 12:39:02 +02003632 int result = -ENOMEM;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003633
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003634 nvme_wq = alloc_workqueue("nvme-wq",
3635 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3636 if (!nvme_wq)
Roy Shtermanb227c592018-01-14 12:39:02 +02003637 goto out;
3638
3639 nvme_reset_wq = alloc_workqueue("nvme-reset-wq",
3640 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3641 if (!nvme_reset_wq)
3642 goto destroy_wq;
3643
3644 nvme_delete_wq = alloc_workqueue("nvme-delete-wq",
3645 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3646 if (!nvme_delete_wq)
3647 goto destroy_reset_wq;
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003648
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003649 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003650 if (result < 0)
Roy Shtermanb227c592018-01-14 12:39:02 +02003651 goto destroy_delete_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003652
3653 nvme_class = class_create(THIS_MODULE, "nvme");
3654 if (IS_ERR(nvme_class)) {
3655 result = PTR_ERR(nvme_class);
3656 goto unregister_chrdev;
3657 }
3658
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003659 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3660 if (IS_ERR(nvme_subsys_class)) {
3661 result = PTR_ERR(nvme_subsys_class);
3662 goto destroy_class;
3663 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003664 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003665
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003666destroy_class:
3667 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003668unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003669 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003670destroy_delete_wq:
3671 destroy_workqueue(nvme_delete_wq);
3672destroy_reset_wq:
3673 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003674destroy_wq:
3675 destroy_workqueue(nvme_wq);
Roy Shtermanb227c592018-01-14 12:39:02 +02003676out:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003677 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003678}
3679
3680void nvme_core_exit(void)
3681{
Christoph Hellwigab9e00cc2017-11-09 13:48:55 +01003682 ida_destroy(&nvme_subsystems_ida);
3683 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003684 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003685 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003686 destroy_workqueue(nvme_delete_wq);
3687 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003688 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003689}
Ming Lin576d55d2016-02-10 10:03:32 -08003690
3691MODULE_LICENSE("GPL");
3692MODULE_VERSION("1.0");
3693module_init(nvme_core_init);
3694module_exit(nvme_core_exit);