blob: 31f20f4643cfe4e4ee4568f07ecf504d2c3637f3 [file] [log] [blame]
Christoph Hellwig21d34712015-11-26 09:08:36 +01001/*
2 * NVM Express device driver
3 * Copyright (c) 2011-2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/blkdev.h>
16#include <linux/blk-mq.h>
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +010017#include <linux/delay.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010018#include <linux/errno.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010019#include <linux/hdreg.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010020#include <linux/kernel.h>
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010021#include <linux/module.h>
22#include <linux/list_sort.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010023#include <linux/slab.h>
24#include <linux/types.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010025#include <linux/pr.h>
26#include <linux/ptrace.h>
27#include <linux/nvme_ioctl.h>
28#include <linux/t10-pi.h>
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080029#include <linux/pm_qos.h>
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010030#include <asm/unaligned.h>
Christoph Hellwig21d34712015-11-26 09:08:36 +010031
Johannes Thumshirn3d030e42018-01-26 11:21:37 +010032#define CREATE_TRACE_POINTS
33#include "trace.h"
34
Christoph Hellwig21d34712015-11-26 09:08:36 +010035#include "nvme.h"
Sagi Grimberg038bd4c2016-06-13 16:45:28 +020036#include "fabrics.h"
Christoph Hellwig21d34712015-11-26 09:08:36 +010037
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010038#define NVME_MINORS (1U << MINORBITS)
39
Marc Olson8ae4e442017-09-06 17:23:56 -070040unsigned int admin_timeout = 60;
41module_param(admin_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080042MODULE_PARM_DESC(admin_timeout, "timeout in seconds for admin commands");
Ming Lin576d55d2016-02-10 10:03:32 -080043EXPORT_SYMBOL_GPL(admin_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080044
Marc Olson8ae4e442017-09-06 17:23:56 -070045unsigned int nvme_io_timeout = 30;
46module_param_named(io_timeout, nvme_io_timeout, uint, 0644);
Ming Linba0ba7d2016-02-10 10:03:30 -080047MODULE_PARM_DESC(io_timeout, "timeout in seconds for I/O");
Ming Lin576d55d2016-02-10 10:03:32 -080048EXPORT_SYMBOL_GPL(nvme_io_timeout);
Ming Linba0ba7d2016-02-10 10:03:30 -080049
Christoph Hellwigb3b1b0b2017-06-12 18:30:51 +020050static unsigned char shutdown_timeout = 5;
Ming Linba0ba7d2016-02-10 10:03:30 -080051module_param(shutdown_timeout, byte, 0644);
52MODULE_PARM_DESC(shutdown_timeout, "timeout in seconds for controller shutdown");
53
Christoph Hellwig44e44b22017-04-05 19:18:11 +020054static u8 nvme_max_retries = 5;
55module_param_named(max_retries, nvme_max_retries, byte, 0644);
Keith Buschf80ec962016-07-12 16:20:31 -070056MODULE_PARM_DESC(max_retries, "max number of retries a command may have");
Christoph Hellwig5bae7f72015-11-28 15:39:07 +010057
Kai-Heng Feng9947d6a2017-06-07 15:25:43 +080058static unsigned long default_ps_max_latency_us = 100000;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -080059module_param(default_ps_max_latency_us, ulong, 0644);
60MODULE_PARM_DESC(default_ps_max_latency_us,
61 "max power saving latency for new devices; use PM QOS to change per device");
62
Andy Lutomirskic35e30b2017-04-21 16:19:24 -070063static bool force_apst;
64module_param(force_apst, bool, 0644);
65MODULE_PARM_DESC(force_apst, "allow APST for newly enumerated devices even if quirked off");
66
Jens Axboef5d11842017-06-27 12:03:06 -060067static bool streams;
68module_param(streams, bool, 0644);
69MODULE_PARM_DESC(streams, "turn on support for Streams write directives");
70
Roy Shtermanb227c592018-01-14 12:39:02 +020071/*
72 * nvme_wq - hosts nvme related works that are not reset or delete
73 * nvme_reset_wq - hosts nvme reset works
74 * nvme_delete_wq - hosts nvme delete works
75 *
76 * nvme_wq will host works such are scan, aen handling, fw activation,
77 * keep-alive error recovery, periodic reconnects etc. nvme_reset_wq
78 * runs reset works which also flush works hosted on nvme_wq for
79 * serialization purposes. nvme_delete_wq host controller deletion
80 * works which flush reset works for serialization.
81 */
Sagi Grimberg9a6327d2017-06-07 20:31:55 +020082struct workqueue_struct *nvme_wq;
83EXPORT_SYMBOL_GPL(nvme_wq);
84
Roy Shtermanb227c592018-01-14 12:39:02 +020085struct workqueue_struct *nvme_reset_wq;
86EXPORT_SYMBOL_GPL(nvme_reset_wq);
87
88struct workqueue_struct *nvme_delete_wq;
89EXPORT_SYMBOL_GPL(nvme_delete_wq);
90
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010091static DEFINE_IDA(nvme_subsystems_ida);
92static LIST_HEAD(nvme_subsystems);
93static DEFINE_MUTEX(nvme_subsystems_lock);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +010094
Christoph Hellwig9843f682017-10-18 13:10:01 +020095static DEFINE_IDA(nvme_instance_ida);
Christoph Hellwiga6a51492017-10-18 16:59:25 +020096static dev_t nvme_chr_devt;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010097static struct class *nvme_class;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +010098static struct class *nvme_subsys_class;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +010099
Keith Busch84fef622017-11-07 10:28:32 -0700100static void nvme_ns_remove(struct nvme_ns *ns);
101static int nvme_revalidate_disk(struct gendisk *disk);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100102
Arnav Dawnb6dccf72017-07-12 16:10:40 +0530103static __le32 nvme_get_log_dw10(u8 lid, size_t size)
104{
105 return cpu_to_le32((((size / 4) - 1) << 16) | lid);
106}
107
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200108int nvme_reset_ctrl(struct nvme_ctrl *ctrl)
109{
110 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
111 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200112 if (!queue_work(nvme_reset_wq, &ctrl->reset_work))
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200113 return -EBUSY;
114 return 0;
115}
116EXPORT_SYMBOL_GPL(nvme_reset_ctrl);
117
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200118int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl)
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200119{
120 int ret;
121
122 ret = nvme_reset_ctrl(ctrl);
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000123 if (!ret) {
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200124 flush_work(&ctrl->reset_work);
Nitzan Carmi8000d1f2018-01-17 11:01:14 +0000125 if (ctrl->state != NVME_CTRL_LIVE)
126 ret = -ENETRESET;
127 }
128
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200129 return ret;
130}
Sagi Grimberg79c48cc2018-01-14 12:39:00 +0200131EXPORT_SYMBOL_GPL(nvme_reset_ctrl_sync);
Christoph Hellwigd86c4d82017-06-15 15:41:08 +0200132
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200133static void nvme_delete_ctrl_work(struct work_struct *work)
134{
135 struct nvme_ctrl *ctrl =
136 container_of(work, struct nvme_ctrl, delete_work);
137
Sagi Grimberg40546372017-10-29 14:21:02 +0200138 flush_work(&ctrl->reset_work);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200139 nvme_stop_ctrl(ctrl);
140 nvme_remove_namespaces(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200141 ctrl->ops->delete_ctrl(ctrl);
Christoph Hellwig6cd53d12017-10-29 10:44:31 +0200142 nvme_uninit_ctrl(ctrl);
143 nvme_put_ctrl(ctrl);
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200144}
145
146int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
147{
148 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
149 return -EBUSY;
Roy Shtermanb227c592018-01-14 12:39:02 +0200150 if (!queue_work(nvme_delete_wq, &ctrl->delete_work))
Christoph Hellwigc5017e82017-10-29 10:44:29 +0200151 return -EBUSY;
152 return 0;
153}
154EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
155
156int nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
157{
158 int ret = 0;
159
160 /*
161 * Keep a reference until the work is flushed since ->delete_ctrl
162 * can free the controller.
163 */
164 nvme_get_ctrl(ctrl);
165 ret = nvme_delete_ctrl(ctrl);
166 if (!ret)
167 flush_work(&ctrl->delete_work);
168 nvme_put_ctrl(ctrl);
169 return ret;
170}
171EXPORT_SYMBOL_GPL(nvme_delete_ctrl_sync);
172
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100173static inline bool nvme_ns_has_pi(struct nvme_ns *ns)
174{
175 return ns->pi_type && ns->ms == sizeof(struct t10_pi_tuple);
176}
177
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200178static blk_status_t nvme_error_status(struct request *req)
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200179{
180 switch (nvme_req(req)->status & 0x7ff) {
181 case NVME_SC_SUCCESS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200182 return BLK_STS_OK;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200183 case NVME_SC_CAP_EXCEEDED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200184 return BLK_STS_NOSPC;
Keith Busche96fef22018-01-09 12:04:14 -0700185 case NVME_SC_LBA_RANGE:
186 return BLK_STS_TARGET;
187 case NVME_SC_BAD_ATTRIBUTES:
Junxiong Guane02ab022017-04-21 12:59:07 +0200188 case NVME_SC_ONCS_NOT_SUPPORTED:
Keith Busche96fef22018-01-09 12:04:14 -0700189 case NVME_SC_INVALID_OPCODE:
190 case NVME_SC_INVALID_FIELD:
191 case NVME_SC_INVALID_NS:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200192 return BLK_STS_NOTSUPP;
Junxiong Guane02ab022017-04-21 12:59:07 +0200193 case NVME_SC_WRITE_FAULT:
194 case NVME_SC_READ_ERROR:
195 case NVME_SC_UNWRITTEN_BLOCK:
Christoph Hellwiga751da32017-08-22 10:17:03 +0200196 case NVME_SC_ACCESS_DENIED:
197 case NVME_SC_READ_ONLY:
Keith Busche96fef22018-01-09 12:04:14 -0700198 case NVME_SC_COMPARE_FAILED:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200199 return BLK_STS_MEDIUM;
Christoph Hellwiga751da32017-08-22 10:17:03 +0200200 case NVME_SC_GUARD_CHECK:
201 case NVME_SC_APPTAG_CHECK:
202 case NVME_SC_REFTAG_CHECK:
203 case NVME_SC_INVALID_PI:
204 return BLK_STS_PROTECTION;
205 case NVME_SC_RESERVATION_CONFLICT:
206 return BLK_STS_NEXUS;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200207 default:
208 return BLK_STS_IOERR;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200209 }
210}
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200211
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200212static inline bool nvme_req_needs_retry(struct request *req)
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200213{
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200214 if (blk_noretry_request(req))
215 return false;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200216 if (nvme_req(req)->status & NVME_SC_DNR)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200217 return false;
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200218 if (nvme_req(req)->retries >= nvme_max_retries)
Christoph Hellwigf6324b12017-04-05 19:18:09 +0200219 return false;
220 return true;
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200221}
222
223void nvme_complete_rq(struct request *req)
224{
Keith Busch908e4562018-01-09 12:04:15 -0700225 blk_status_t status = nvme_error_status(req);
226
Johannes Thumshirnca5554a2018-01-26 11:21:38 +0100227 trace_nvme_complete_rq(req);
228
Keith Busch908e4562018-01-09 12:04:15 -0700229 if (unlikely(status != BLK_STS_OK && nvme_req_needs_retry(req))) {
230 if (nvme_req_needs_failover(req, status)) {
Christoph Hellwig32acab32017-11-02 12:59:30 +0100231 nvme_failover_req(req);
232 return;
233 }
234
235 if (!blk_queue_dying(req->q)) {
236 nvme_req(req)->retries++;
237 blk_mq_requeue_request(req, true);
238 return;
239 }
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200240 }
Keith Busch908e4562018-01-09 12:04:15 -0700241 blk_mq_end_request(req, status);
Christoph Hellwig77f02a72017-03-30 13:41:32 +0200242}
243EXPORT_SYMBOL_GPL(nvme_complete_rq);
244
Ming Linc55a2fd2016-05-18 14:05:02 -0700245void nvme_cancel_request(struct request *req, void *data, bool reserved)
246{
Ming Linc55a2fd2016-05-18 14:05:02 -0700247 if (!blk_mq_request_started(req))
248 return;
249
250 dev_dbg_ratelimited(((struct nvme_ctrl *) data)->device,
251 "Cancelling I/O %d", req->tag);
252
Christoph Hellwige54b0642017-11-02 21:28:51 +0300253 nvme_req(req)->status = NVME_SC_ABORT_REQ;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200254 blk_mq_complete_request(req);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200255
Ming Linc55a2fd2016-05-18 14:05:02 -0700256}
257EXPORT_SYMBOL_GPL(nvme_cancel_request);
258
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200259bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
260 enum nvme_ctrl_state new_state)
261{
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300262 enum nvme_ctrl_state old_state;
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200263 unsigned long flags;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200264 bool changed = false;
265
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200266 spin_lock_irqsave(&ctrl->lock, flags);
Gabriel Krisman Bertazif6b6a282016-07-29 16:15:18 -0300267
268 old_state = ctrl->state;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200269 switch (new_state) {
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800270 case NVME_CTRL_ADMIN_ONLY:
271 switch (old_state) {
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200272 case NVME_CTRL_CONNECTING:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800273 changed = true;
274 /* FALLTHRU */
275 default:
276 break;
277 }
278 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200279 case NVME_CTRL_LIVE:
280 switch (old_state) {
Christoph Hellwig7d2e8002016-06-13 16:45:22 +0200281 case NVME_CTRL_NEW:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200282 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200283 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200284 changed = true;
285 /* FALLTHRU */
286 default:
287 break;
288 }
289 break;
290 case NVME_CTRL_RESETTING:
291 switch (old_state) {
292 case NVME_CTRL_NEW:
293 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800294 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900295 changed = true;
296 /* FALLTHRU */
297 default:
298 break;
299 }
300 break;
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200301 case NVME_CTRL_CONNECTING:
Christoph Hellwigdef61ec2016-07-06 21:55:49 +0900302 switch (old_state) {
Max Gurtovoyb754a322018-01-31 18:31:25 +0200303 case NVME_CTRL_NEW:
James Smart3cec7f92017-10-25 16:43:13 -0700304 case NVME_CTRL_RESETTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200305 changed = true;
306 /* FALLTHRU */
307 default:
308 break;
309 }
310 break;
311 case NVME_CTRL_DELETING:
312 switch (old_state) {
313 case NVME_CTRL_LIVE:
Jianchao Wang2b1b7e72018-01-06 08:01:58 +0800314 case NVME_CTRL_ADMIN_ONLY:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200315 case NVME_CTRL_RESETTING:
Max Gurtovoyad6a0a52018-01-31 18:31:24 +0200316 case NVME_CTRL_CONNECTING:
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200317 changed = true;
318 /* FALLTHRU */
319 default:
320 break;
321 }
322 break;
Keith Busch0ff9d4e2016-05-12 08:37:14 -0600323 case NVME_CTRL_DEAD:
324 switch (old_state) {
325 case NVME_CTRL_DELETING:
326 changed = true;
327 /* FALLTHRU */
328 default:
329 break;
330 }
331 break;
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200332 default:
333 break;
334 }
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200335
336 if (changed)
337 ctrl->state = new_state;
338
Christoph Hellwig0a72bbb2017-08-22 11:42:24 +0200339 spin_unlock_irqrestore(&ctrl->lock, flags);
Christoph Hellwig32acab32017-11-02 12:59:30 +0100340 if (changed && ctrl->state == NVME_CTRL_LIVE)
341 nvme_kick_requeue_lists(ctrl);
Christoph Hellwigbb8d2612016-04-26 13:51:57 +0200342 return changed;
343}
344EXPORT_SYMBOL_GPL(nvme_change_ctrl_state);
345
Christoph Hellwiged754e52017-11-09 13:50:43 +0100346static void nvme_free_ns_head(struct kref *ref)
347{
348 struct nvme_ns_head *head =
349 container_of(ref, struct nvme_ns_head, ref);
350
Christoph Hellwig32acab32017-11-02 12:59:30 +0100351 nvme_mpath_remove_disk(head);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100352 ida_simple_remove(&head->subsys->ns_ida, head->instance);
353 list_del_init(&head->entry);
354 cleanup_srcu_struct(&head->srcu);
355 kfree(head);
356}
357
358static void nvme_put_ns_head(struct nvme_ns_head *head)
359{
360 kref_put(&head->ref, nvme_free_ns_head);
361}
362
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100363static void nvme_free_ns(struct kref *kref)
364{
365 struct nvme_ns *ns = container_of(kref, struct nvme_ns, kref);
366
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200367 if (ns->ndev)
368 nvme_nvm_unregister(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100369
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100370 put_disk(ns->disk);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100371 nvme_put_ns_head(ns->head);
Keith Busch075790e2016-02-24 09:15:53 -0700372 nvme_put_ctrl(ns->ctrl);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100373 kfree(ns);
374}
375
Christoph Hellwig5bae7f72015-11-28 15:39:07 +0100376static void nvme_put_ns(struct nvme_ns *ns)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +0100377{
378 kref_put(&ns->kref, nvme_free_ns);
379}
380
Christoph Hellwig41609822015-11-20 09:00:02 +0100381struct request *nvme_alloc_request(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800382 struct nvme_command *cmd, blk_mq_req_flags_t flags, int qid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100383{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100384 unsigned op = nvme_is_write(cmd) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100385 struct request *req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100386
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200387 if (qid == NVME_QID_ANY) {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100388 req = blk_mq_alloc_request(q, op, flags);
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200389 } else {
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100390 req = blk_mq_alloc_request_hctx(q, op, flags,
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200391 qid ? qid - 1 : 0);
392 }
Christoph Hellwig21d34712015-11-26 09:08:36 +0100393 if (IS_ERR(req))
Christoph Hellwig41609822015-11-20 09:00:02 +0100394 return req;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100395
Christoph Hellwig21d34712015-11-26 09:08:36 +0100396 req->cmd_flags |= REQ_FAILFAST_DRIVER;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800397 nvme_req(req)->cmd = cmd;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100398
Christoph Hellwig41609822015-11-20 09:00:02 +0100399 return req;
400}
Ming Lin576d55d2016-02-10 10:03:32 -0800401EXPORT_SYMBOL_GPL(nvme_alloc_request);
Christoph Hellwig41609822015-11-20 09:00:02 +0100402
Jens Axboef5d11842017-06-27 12:03:06 -0600403static int nvme_toggle_streams(struct nvme_ctrl *ctrl, bool enable)
404{
405 struct nvme_command c;
406
407 memset(&c, 0, sizeof(c));
408
409 c.directive.opcode = nvme_admin_directive_send;
Arnav Dawn62346ea2017-07-12 16:11:53 +0530410 c.directive.nsid = cpu_to_le32(NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600411 c.directive.doper = NVME_DIR_SND_ID_OP_ENABLE;
412 c.directive.dtype = NVME_DIR_IDENTIFY;
413 c.directive.tdtype = NVME_DIR_STREAMS;
414 c.directive.endir = enable ? NVME_DIR_ENDIR : 0;
415
416 return nvme_submit_sync_cmd(ctrl->admin_q, &c, NULL, 0);
417}
418
419static int nvme_disable_streams(struct nvme_ctrl *ctrl)
420{
421 return nvme_toggle_streams(ctrl, false);
422}
423
424static int nvme_enable_streams(struct nvme_ctrl *ctrl)
425{
426 return nvme_toggle_streams(ctrl, true);
427}
428
429static int nvme_get_stream_params(struct nvme_ctrl *ctrl,
430 struct streams_directive_params *s, u32 nsid)
431{
432 struct nvme_command c;
433
434 memset(&c, 0, sizeof(c));
435 memset(s, 0, sizeof(*s));
436
437 c.directive.opcode = nvme_admin_directive_recv;
438 c.directive.nsid = cpu_to_le32(nsid);
Kwan (Hingkwan) Huen-SSIa082b422017-08-09 11:26:29 -0700439 c.directive.numd = cpu_to_le32((sizeof(*s) >> 2) - 1);
Jens Axboef5d11842017-06-27 12:03:06 -0600440 c.directive.doper = NVME_DIR_RCV_ST_OP_PARAM;
441 c.directive.dtype = NVME_DIR_STREAMS;
442
443 return nvme_submit_sync_cmd(ctrl->admin_q, &c, s, sizeof(*s));
444}
445
446static int nvme_configure_directives(struct nvme_ctrl *ctrl)
447{
448 struct streams_directive_params s;
449 int ret;
450
451 if (!(ctrl->oacs & NVME_CTRL_OACS_DIRECTIVES))
452 return 0;
453 if (!streams)
454 return 0;
455
456 ret = nvme_enable_streams(ctrl);
457 if (ret)
458 return ret;
459
Arnav Dawn62346ea2017-07-12 16:11:53 +0530460 ret = nvme_get_stream_params(ctrl, &s, NVME_NSID_ALL);
Jens Axboef5d11842017-06-27 12:03:06 -0600461 if (ret)
462 return ret;
463
464 ctrl->nssa = le16_to_cpu(s.nssa);
465 if (ctrl->nssa < BLK_MAX_WRITE_HINTS - 1) {
466 dev_info(ctrl->device, "too few streams (%u) available\n",
467 ctrl->nssa);
468 nvme_disable_streams(ctrl);
469 return 0;
470 }
471
472 ctrl->nr_streams = min_t(unsigned, ctrl->nssa, BLK_MAX_WRITE_HINTS - 1);
473 dev_info(ctrl->device, "Using %u streams\n", ctrl->nr_streams);
474 return 0;
475}
476
477/*
478 * Check if 'req' has a write hint associated with it. If it does, assign
479 * a valid namespace stream to the write.
480 */
481static void nvme_assign_write_stream(struct nvme_ctrl *ctrl,
482 struct request *req, u16 *control,
483 u32 *dsmgmt)
484{
485 enum rw_hint streamid = req->write_hint;
486
487 if (streamid == WRITE_LIFE_NOT_SET || streamid == WRITE_LIFE_NONE)
488 streamid = 0;
489 else {
490 streamid--;
491 if (WARN_ON_ONCE(streamid > ctrl->nr_streams))
492 return;
493
494 *control |= NVME_RW_DTYPE_STREAMS;
495 *dsmgmt |= streamid << 16;
496 }
497
498 if (streamid < ARRAY_SIZE(req->q->write_hints))
499 req->q->write_hints[streamid] += blk_rq_bytes(req) >> 9;
500}
501
Ming Lin8093f7c2016-04-12 13:10:14 -0600502static inline void nvme_setup_flush(struct nvme_ns *ns,
503 struct nvme_command *cmnd)
504{
505 memset(cmnd, 0, sizeof(*cmnd));
506 cmnd->common.opcode = nvme_cmd_flush;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100507 cmnd->common.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600508}
509
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200510static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600511 struct nvme_command *cmnd)
512{
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100513 unsigned short segments = blk_rq_nr_discard_segments(req), n = 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600514 struct nvme_dsm_range *range;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100515 struct bio *bio;
Ming Lin8093f7c2016-04-12 13:10:14 -0600516
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100517 range = kmalloc_array(segments, sizeof(*range), GFP_ATOMIC);
Ming Lin8093f7c2016-04-12 13:10:14 -0600518 if (!range)
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200519 return BLK_STS_RESOURCE;
Ming Lin8093f7c2016-04-12 13:10:14 -0600520
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100521 __rq_for_each_bio(bio, req) {
522 u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
523 u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
524
Keith Busch8cb6af72018-01-31 17:01:58 -0700525 if (n < segments) {
526 range[n].cattr = cpu_to_le32(0);
527 range[n].nlb = cpu_to_le32(nlb);
528 range[n].slba = cpu_to_le64(slba);
529 }
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100530 n++;
531 }
532
533 if (WARN_ON_ONCE(n != segments)) {
534 kfree(range);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200535 return BLK_STS_IOERR;
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100536 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600537
538 memset(cmnd, 0, sizeof(*cmnd));
539 cmnd->dsm.opcode = nvme_cmd_dsm;
Christoph Hellwiged754e52017-11-09 13:50:43 +0100540 cmnd->dsm.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwigf1dd03a2017-03-31 17:00:05 +0200541 cmnd->dsm.nr = cpu_to_le32(segments - 1);
Ming Lin8093f7c2016-04-12 13:10:14 -0600542 cmnd->dsm.attributes = cpu_to_le32(NVME_DSMGMT_AD);
543
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700544 req->special_vec.bv_page = virt_to_page(range);
545 req->special_vec.bv_offset = offset_in_page(range);
Christoph Hellwigb35ba012017-02-08 14:46:50 +0100546 req->special_vec.bv_len = sizeof(*range) * segments;
Christoph Hellwigf9d03f92016-12-08 15:20:32 -0700547 req->rq_flags |= RQF_SPECIAL_PAYLOAD;
Ming Lin8093f7c2016-04-12 13:10:14 -0600548
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200549 return BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600550}
Ming Lin8093f7c2016-04-12 13:10:14 -0600551
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200552static inline blk_status_t nvme_setup_rw(struct nvme_ns *ns,
553 struct request *req, struct nvme_command *cmnd)
Ming Lin8093f7c2016-04-12 13:10:14 -0600554{
Jens Axboef5d11842017-06-27 12:03:06 -0600555 struct nvme_ctrl *ctrl = ns->ctrl;
Ming Lin8093f7c2016-04-12 13:10:14 -0600556 u16 control = 0;
557 u32 dsmgmt = 0;
558
559 if (req->cmd_flags & REQ_FUA)
560 control |= NVME_RW_FUA;
561 if (req->cmd_flags & (REQ_FAILFAST_DEV | REQ_RAHEAD))
562 control |= NVME_RW_LR;
563
564 if (req->cmd_flags & REQ_RAHEAD)
565 dsmgmt |= NVME_RW_DSM_FREQ_PREFETCH;
566
567 memset(cmnd, 0, sizeof(*cmnd));
568 cmnd->rw.opcode = (rq_data_dir(req) ? nvme_cmd_write : nvme_cmd_read);
Christoph Hellwiged754e52017-11-09 13:50:43 +0100569 cmnd->rw.nsid = cpu_to_le32(ns->head->ns_id);
Ming Lin8093f7c2016-04-12 13:10:14 -0600570 cmnd->rw.slba = cpu_to_le64(nvme_block_nr(ns, blk_rq_pos(req)));
571 cmnd->rw.length = cpu_to_le16((blk_rq_bytes(req) >> ns->lba_shift) - 1);
572
Jens Axboef5d11842017-06-27 12:03:06 -0600573 if (req_op(req) == REQ_OP_WRITE && ctrl->nr_streams)
574 nvme_assign_write_stream(ctrl, req, &control, &dsmgmt);
575
Ming Lin8093f7c2016-04-12 13:10:14 -0600576 if (ns->ms) {
Christoph Hellwig715ea9e2017-11-07 17:27:34 +0100577 /*
578 * If formated with metadata, the block layer always provides a
579 * metadata buffer if CONFIG_BLK_DEV_INTEGRITY is enabled. Else
580 * we enable the PRACT bit for protection information or set the
581 * namespace capacity to zero to prevent any I/O.
582 */
583 if (!blk_integrity_rq(req)) {
584 if (WARN_ON_ONCE(!nvme_ns_has_pi(ns)))
585 return BLK_STS_NOTSUPP;
586 control |= NVME_RW_PRINFO_PRACT;
587 }
588
Ming Lin8093f7c2016-04-12 13:10:14 -0600589 switch (ns->pi_type) {
590 case NVME_NS_DPS_PI_TYPE3:
591 control |= NVME_RW_PRINFO_PRCHK_GUARD;
592 break;
593 case NVME_NS_DPS_PI_TYPE1:
594 case NVME_NS_DPS_PI_TYPE2:
595 control |= NVME_RW_PRINFO_PRCHK_GUARD |
596 NVME_RW_PRINFO_PRCHK_REF;
597 cmnd->rw.reftag = cpu_to_le32(
598 nvme_block_nr(ns, blk_rq_pos(req)));
599 break;
600 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600601 }
602
603 cmnd->rw.control = cpu_to_le16(control);
604 cmnd->rw.dsmgmt = cpu_to_le32(dsmgmt);
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200605 return 0;
Ming Lin8093f7c2016-04-12 13:10:14 -0600606}
607
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200608blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req,
Ming Lin8093f7c2016-04-12 13:10:14 -0600609 struct nvme_command *cmd)
610{
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200611 blk_status_t ret = BLK_STS_OK;
Ming Lin8093f7c2016-04-12 13:10:14 -0600612
Christoph Hellwig987f6992017-04-05 19:18:08 +0200613 if (!(req->rq_flags & RQF_DONTPREP)) {
Christoph Hellwig44e44b22017-04-05 19:18:11 +0200614 nvme_req(req)->retries = 0;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200615 nvme_req(req)->flags = 0;
Christoph Hellwig987f6992017-04-05 19:18:08 +0200616 req->rq_flags |= RQF_DONTPREP;
617 }
618
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100619 switch (req_op(req)) {
620 case REQ_OP_DRV_IN:
621 case REQ_OP_DRV_OUT:
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800622 memcpy(cmd, nvme_req(req)->cmd, sizeof(*cmd));
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100623 break;
624 case REQ_OP_FLUSH:
Ming Lin8093f7c2016-04-12 13:10:14 -0600625 nvme_setup_flush(ns, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100626 break;
Christoph Hellwige850fd12017-04-05 19:21:13 +0200627 case REQ_OP_WRITE_ZEROES:
628 /* currently only aliased to deallocate for a few ctrls: */
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100629 case REQ_OP_DISCARD:
Ming Lin8093f7c2016-04-12 13:10:14 -0600630 ret = nvme_setup_discard(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100631 break;
632 case REQ_OP_READ:
633 case REQ_OP_WRITE:
Christoph Hellwigebe6d872017-06-12 18:36:32 +0200634 ret = nvme_setup_rw(ns, req, cmd);
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100635 break;
636 default:
637 WARN_ON_ONCE(1);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200638 return BLK_STS_IOERR;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100639 }
Ming Lin8093f7c2016-04-12 13:10:14 -0600640
James Smart721b3912016-10-21 23:33:34 +0300641 cmd->common.command_id = req->tag;
Johannes Thumshirn3d030e42018-01-26 11:21:37 +0100642 if (ns)
643 trace_nvme_setup_nvm_cmd(req->q->id, cmd);
644 else
645 trace_nvme_setup_admin_cmd(cmd);
Ming Lin8093f7c2016-04-12 13:10:14 -0600646 return ret;
647}
648EXPORT_SYMBOL_GPL(nvme_setup_cmd);
649
Christoph Hellwig41609822015-11-20 09:00:02 +0100650/*
651 * Returns 0 on success. If the result is negative, it's a Linux error code;
652 * if the result is positive, it's an NVM Express status code
653 */
654int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800655 union nvme_result *result, void *buffer, unsigned bufflen,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800656 unsigned timeout, int qid, int at_head,
657 blk_mq_req_flags_t flags)
Christoph Hellwig41609822015-11-20 09:00:02 +0100658{
659 struct request *req;
660 int ret;
661
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200662 req = nvme_alloc_request(q, cmd, flags, qid);
Christoph Hellwig41609822015-11-20 09:00:02 +0100663 if (IS_ERR(req))
664 return PTR_ERR(req);
665
666 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
667
Christoph Hellwig21d34712015-11-26 09:08:36 +0100668 if (buffer && bufflen) {
669 ret = blk_rq_map_kern(q, req, buffer, bufflen, GFP_KERNEL);
670 if (ret)
671 goto out;
Christoph Hellwig41609822015-11-20 09:00:02 +0100672 }
673
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200674 blk_execute_rq(req->q, NULL, req, at_head);
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800675 if (result)
676 *result = nvme_req(req)->result;
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200677 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
678 ret = -EINTR;
679 else
680 ret = nvme_req(req)->status;
Christoph Hellwig41609822015-11-20 09:00:02 +0100681 out:
682 blk_mq_free_request(req);
683 return ret;
684}
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200685EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100686
687int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
688 void *buffer, unsigned bufflen)
689{
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200690 return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
691 NVME_QID_ANY, 0, 0);
Christoph Hellwig41609822015-11-20 09:00:02 +0100692}
Ming Lin576d55d2016-02-10 10:03:32 -0800693EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
Christoph Hellwig41609822015-11-20 09:00:02 +0100694
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400695static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
696 unsigned len, u32 seed, bool write)
697{
698 struct bio_integrity_payload *bip;
699 int ret = -ENOMEM;
700 void *buf;
701
702 buf = kmalloc(len, GFP_KERNEL);
703 if (!buf)
704 goto out;
705
706 ret = -EFAULT;
707 if (write && copy_from_user(buf, ubuf, len))
708 goto out_free_meta;
709
710 bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
711 if (IS_ERR(bip)) {
712 ret = PTR_ERR(bip);
713 goto out_free_meta;
714 }
715
716 bip->bip_iter.bi_size = len;
717 bip->bip_iter.bi_sector = seed;
718 ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
719 offset_in_page(buf));
720 if (ret == len)
721 return buf;
722 ret = -ENOMEM;
723out_free_meta:
724 kfree(buf);
725out:
726 return ERR_PTR(ret);
727}
728
Keith Busch63263d62017-08-29 17:46:04 -0400729static int nvme_submit_user_cmd(struct request_queue *q,
Keith Busch485783c2017-08-29 17:46:03 -0400730 struct nvme_command *cmd, void __user *ubuffer,
731 unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
732 u32 meta_seed, u32 *result, unsigned timeout)
Christoph Hellwig41609822015-11-20 09:00:02 +0100733{
Christoph Hellwig7a5abb42016-06-06 23:20:49 +0200734 bool write = nvme_is_write(cmd);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600735 struct nvme_ns *ns = q->queuedata;
736 struct gendisk *disk = ns ? ns->disk : NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100737 struct request *req;
Keith Busch0b7f1f22015-10-23 09:47:28 -0600738 struct bio *bio = NULL;
739 void *meta = NULL;
Christoph Hellwig41609822015-11-20 09:00:02 +0100740 int ret;
741
Christoph Hellwigeb71f432016-06-13 16:45:23 +0200742 req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
Christoph Hellwig41609822015-11-20 09:00:02 +0100743 if (IS_ERR(req))
744 return PTR_ERR(req);
745
746 req->timeout = timeout ? timeout : ADMIN_TIMEOUT;
747
748 if (ubuffer && bufflen) {
Christoph Hellwig21d34712015-11-26 09:08:36 +0100749 ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
750 GFP_KERNEL);
751 if (ret)
752 goto out;
753 bio = req->bio;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200754 bio->bi_disk = disk;
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400755 if (disk && meta_buffer && meta_len) {
756 meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
757 meta_seed, write);
758 if (IS_ERR(meta)) {
759 ret = PTR_ERR(meta);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600760 goto out_unmap;
761 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600762 }
763 }
Christoph Hellwig1cad6562017-08-29 17:46:01 -0400764
Keith Busch0b7f1f22015-10-23 09:47:28 -0600765 blk_execute_rq(req->q, disk, req, 0);
Christoph Hellwig27fa9bc2017-04-20 16:02:57 +0200766 if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
767 ret = -EINTR;
768 else
769 ret = nvme_req(req)->status;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100770 if (result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800771 *result = le32_to_cpu(nvme_req(req)->result.u32);
Keith Busch0b7f1f22015-10-23 09:47:28 -0600772 if (meta && !ret && !write) {
773 if (copy_to_user(meta_buffer, meta, meta_len))
774 ret = -EFAULT;
775 }
Keith Busch0b7f1f22015-10-23 09:47:28 -0600776 kfree(meta);
777 out_unmap:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200778 if (bio)
Keith Busch0b7f1f22015-10-23 09:47:28 -0600779 blk_rq_unmap_user(bio);
Christoph Hellwig21d34712015-11-26 09:08:36 +0100780 out:
781 blk_mq_free_request(req);
782 return ret;
783}
784
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200785static void nvme_keep_alive_end_io(struct request *rq, blk_status_t status)
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200786{
787 struct nvme_ctrl *ctrl = rq->end_io_data;
788
789 blk_mq_free_request(rq);
790
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200791 if (status) {
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200792 dev_err(ctrl->device,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200793 "failed nvme_keep_alive_end_io error=%d\n",
794 status);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200795 return;
796 }
797
798 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
799}
800
801static int nvme_keep_alive(struct nvme_ctrl *ctrl)
802{
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200803 struct request *rq;
804
Roland Dreier0a34e462018-01-11 13:38:15 -0800805 rq = nvme_alloc_request(ctrl->admin_q, &ctrl->ka_cmd, BLK_MQ_REQ_RESERVED,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200806 NVME_QID_ANY);
807 if (IS_ERR(rq))
808 return PTR_ERR(rq);
809
810 rq->timeout = ctrl->kato * HZ;
811 rq->end_io_data = ctrl;
812
813 blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
814
815 return 0;
816}
817
818static void nvme_keep_alive_work(struct work_struct *work)
819{
820 struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
821 struct nvme_ctrl, ka_work);
822
823 if (nvme_keep_alive(ctrl)) {
824 /* allocation failure, reset the controller */
825 dev_err(ctrl->device, "keep-alive failed\n");
Christoph Hellwig39bdc592017-06-12 18:21:19 +0200826 nvme_reset_ctrl(ctrl);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200827 return;
828 }
829}
830
831void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
832{
833 if (unlikely(ctrl->kato == 0))
834 return;
835
836 INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
Roland Dreier0a34e462018-01-11 13:38:15 -0800837 memset(&ctrl->ka_cmd, 0, sizeof(ctrl->ka_cmd));
838 ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +0200839 schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
840}
841EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
842
843void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
844{
845 if (unlikely(ctrl->kato == 0))
846 return;
847
848 cancel_delayed_work_sync(&ctrl->ka_work);
849}
850EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
851
Keith Busch3f7f25a92017-06-20 15:09:56 -0400852static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100853{
854 struct nvme_command c = { };
855 int error;
856
857 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
858 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200859 c.identify.cns = NVME_ID_CNS_CTRL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100860
861 *id = kmalloc(sizeof(struct nvme_id_ctrl), GFP_KERNEL);
862 if (!*id)
863 return -ENOMEM;
864
865 error = nvme_submit_sync_cmd(dev->admin_q, &c, *id,
866 sizeof(struct nvme_id_ctrl));
867 if (error)
868 kfree(*id);
869 return error;
870}
871
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200872static int nvme_identify_ns_descs(struct nvme_ctrl *ctrl, unsigned nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +0100873 struct nvme_ns_ids *ids)
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200874{
875 struct nvme_command c = { };
876 int status;
877 void *data;
878 int pos;
879 int len;
880
881 c.identify.opcode = nvme_admin_identify;
882 c.identify.nsid = cpu_to_le32(nsid);
883 c.identify.cns = NVME_ID_CNS_NS_DESC_LIST;
884
885 data = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
886 if (!data)
887 return -ENOMEM;
888
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200889 status = nvme_submit_sync_cmd(ctrl->admin_q, &c, data,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200890 NVME_IDENTIFY_DATA_SIZE);
891 if (status)
892 goto free_data;
893
894 for (pos = 0; pos < NVME_IDENTIFY_DATA_SIZE; pos += len) {
895 struct nvme_ns_id_desc *cur = data + pos;
896
897 if (cur->nidl == 0)
898 break;
899
900 switch (cur->nidt) {
901 case NVME_NIDT_EUI64:
902 if (cur->nidl != NVME_NIDT_EUI64_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200903 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200904 "ctrl returned bogus length: %d for NVME_NIDT_EUI64\n",
905 cur->nidl);
906 goto free_data;
907 }
908 len = NVME_NIDT_EUI64_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100909 memcpy(ids->eui64, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200910 break;
911 case NVME_NIDT_NGUID:
912 if (cur->nidl != NVME_NIDT_NGUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200913 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200914 "ctrl returned bogus length: %d for NVME_NIDT_NGUID\n",
915 cur->nidl);
916 goto free_data;
917 }
918 len = NVME_NIDT_NGUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100919 memcpy(ids->nguid, data + pos + sizeof(*cur), len);
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200920 break;
921 case NVME_NIDT_UUID:
922 if (cur->nidl != NVME_NIDT_UUID_LEN) {
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200923 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200924 "ctrl returned bogus length: %d for NVME_NIDT_UUID\n",
925 cur->nidl);
926 goto free_data;
927 }
928 len = NVME_NIDT_UUID_LEN;
Christoph Hellwig002fab02017-11-09 13:50:16 +0100929 uuid_copy(&ids->uuid, data + pos + sizeof(*cur));
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +0200930 break;
931 default:
932 /* Skip unnkown types */
933 len = cur->nidl;
934 break;
935 }
936
937 len += sizeof(*cur);
938 }
939free_data:
940 kfree(data);
941 return status;
942}
943
Keith Busch540c8012015-10-22 15:45:06 -0600944static int nvme_identify_ns_list(struct nvme_ctrl *dev, unsigned nsid, __le32 *ns_list)
945{
946 struct nvme_command c = { };
947
948 c.identify.opcode = nvme_admin_identify;
Parav Pandit986994a2017-01-26 17:17:28 +0200949 c.identify.cns = NVME_ID_CNS_NS_ACTIVE_LIST;
Keith Busch540c8012015-10-22 15:45:06 -0600950 c.identify.nsid = cpu_to_le32(nsid);
Minwoo Im42595eb2018-02-08 22:56:31 +0900951 return nvme_submit_sync_cmd(dev->admin_q, &c, ns_list,
952 NVME_IDENTIFY_DATA_SIZE);
Keith Busch540c8012015-10-22 15:45:06 -0600953}
954
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200955static struct nvme_id_ns *nvme_identify_ns(struct nvme_ctrl *ctrl,
956 unsigned nsid)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100957{
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200958 struct nvme_id_ns *id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100959 struct nvme_command c = { };
960 int error;
961
962 /* gcc-4.4.4 (at least) has issues with initializers and anon unions */
Max Gurtovoy778f0672017-01-26 17:17:27 +0200963 c.identify.opcode = nvme_admin_identify;
964 c.identify.nsid = cpu_to_le32(nsid);
Parav Pandit986994a2017-01-26 17:17:28 +0200965 c.identify.cns = NVME_ID_CNS_NS;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100966
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200967 id = kmalloc(sizeof(*id), GFP_KERNEL);
968 if (!id)
969 return NULL;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100970
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +0200971 error = nvme_submit_sync_cmd(ctrl->admin_q, &c, id, sizeof(*id));
972 if (error) {
973 dev_warn(ctrl->device, "Identify namespace failed\n");
974 kfree(id);
975 return NULL;
976 }
977
978 return id;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100979}
980
Keith Busch3f7f25a92017-06-20 15:09:56 -0400981static int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700982 void *buffer, size_t buflen, u32 *result)
Christoph Hellwig21d34712015-11-26 09:08:36 +0100983{
984 struct nvme_command c;
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800985 union nvme_result res;
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100986 int ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100987
988 memset(&c, 0, sizeof(c));
989 c.features.opcode = nvme_admin_set_features;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100990 c.features.fid = cpu_to_le32(fid);
991 c.features.dword11 = cpu_to_le32(dword11);
992
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800993 ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &res,
Andy Lutomirski1a6fe742016-09-16 11:16:10 -0700994 buffer, buflen, 0, NVME_QID_ANY, 0, 0);
Andy Lutomirski9b47f77a2016-08-24 03:52:12 -0700995 if (ret >= 0 && result)
Christoph Hellwigd49187e2016-11-10 07:32:33 -0800996 *result = le32_to_cpu(res.u32);
Christoph Hellwig1cb3cce2016-02-29 15:59:47 +0100997 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +0100998}
999
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001000int nvme_set_queue_count(struct nvme_ctrl *ctrl, int *count)
1001{
1002 u32 q_count = (*count - 1) | ((*count - 1) << 16);
1003 u32 result;
1004 int status, nr_io_queues;
1005
Andy Lutomirski1a6fe742016-09-16 11:16:10 -07001006 status = nvme_set_features(ctrl, NVME_FEAT_NUM_QUEUES, q_count, NULL, 0,
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001007 &result);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001008 if (status < 0)
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001009 return status;
1010
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001011 /*
1012 * Degraded controllers might return an error when setting the queue
1013 * count. We still want to be able to bring them online and offer
1014 * access to the admin queue, as that might be only way to fix them up.
1015 */
1016 if (status > 0) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02001017 dev_err(ctrl->device, "Could not set queue count (%d)\n", status);
Christoph Hellwigf5fa90d2016-06-06 23:20:50 +02001018 *count = 0;
1019 } else {
1020 nr_io_queues = min(result & 0xffff, result >> 16) + 1;
1021 *count = min(*count, nr_io_queues);
1022 }
1023
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001024 return 0;
1025}
Ming Lin576d55d2016-02-10 10:03:32 -08001026EXPORT_SYMBOL_GPL(nvme_set_queue_count);
Christoph Hellwig9a0be7a2015-11-26 11:09:06 +01001027
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001028static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
1029{
1030 struct nvme_user_io io;
1031 struct nvme_command c;
1032 unsigned length, meta_len;
1033 void __user *metadata;
1034
1035 if (copy_from_user(&io, uio, sizeof(io)))
1036 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001037 if (io.flags)
1038 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001039
1040 switch (io.opcode) {
1041 case nvme_cmd_write:
1042 case nvme_cmd_read:
1043 case nvme_cmd_compare:
1044 break;
1045 default:
1046 return -EINVAL;
1047 }
1048
1049 length = (io.nblocks + 1) << ns->lba_shift;
1050 meta_len = (io.nblocks + 1) * ns->ms;
1051 metadata = (void __user *)(uintptr_t)io.metadata;
1052
1053 if (ns->ext) {
1054 length += meta_len;
1055 meta_len = 0;
1056 } else if (meta_len) {
1057 if ((io.metadata & 3) || !io.metadata)
1058 return -EINVAL;
1059 }
1060
1061 memset(&c, 0, sizeof(c));
1062 c.rw.opcode = io.opcode;
1063 c.rw.flags = io.flags;
Christoph Hellwiged754e52017-11-09 13:50:43 +01001064 c.rw.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001065 c.rw.slba = cpu_to_le64(io.slba);
1066 c.rw.length = cpu_to_le16(io.nblocks);
1067 c.rw.control = cpu_to_le16(io.control);
1068 c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
1069 c.rw.reftag = cpu_to_le32(io.reftag);
1070 c.rw.apptag = cpu_to_le16(io.apptag);
1071 c.rw.appmask = cpu_to_le16(io.appmask);
1072
Keith Busch63263d62017-08-29 17:46:04 -04001073 return nvme_submit_user_cmd(ns->queue, &c,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001074 (void __user *)(uintptr_t)io.addr, length,
1075 metadata, meta_len, io.slba, NULL, 0);
1076}
1077
Keith Busch84fef622017-11-07 10:28:32 -07001078static u32 nvme_known_admin_effects(u8 opcode)
1079{
1080 switch (opcode) {
1081 case nvme_admin_format_nvm:
1082 return NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
1083 NVME_CMD_EFFECTS_CSE_MASK;
1084 case nvme_admin_sanitize_nvm:
1085 return NVME_CMD_EFFECTS_CSE_MASK;
1086 default:
1087 break;
1088 }
1089 return 0;
1090}
1091
1092static u32 nvme_passthru_start(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
1093 u8 opcode)
1094{
1095 u32 effects = 0;
1096
1097 if (ns) {
1098 if (ctrl->effects)
1099 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1100 if (effects & ~NVME_CMD_EFFECTS_CSUPP)
1101 dev_warn(ctrl->device,
1102 "IO command:%02x has unhandled effects:%08x\n",
1103 opcode, effects);
1104 return 0;
1105 }
1106
1107 if (ctrl->effects)
1108 effects = le32_to_cpu(ctrl->effects->iocs[opcode]);
1109 else
1110 effects = nvme_known_admin_effects(opcode);
1111
1112 /*
1113 * For simplicity, IO to all namespaces is quiesced even if the command
1114 * effects say only one namespace is affected.
1115 */
1116 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK)) {
1117 nvme_start_freeze(ctrl);
1118 nvme_wait_freeze(ctrl);
1119 }
1120 return effects;
1121}
1122
1123static void nvme_update_formats(struct nvme_ctrl *ctrl)
1124{
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001125 struct nvme_ns *ns, *next;
1126 LIST_HEAD(rm_list);
Keith Busch84fef622017-11-07 10:28:32 -07001127
1128 mutex_lock(&ctrl->namespaces_mutex);
1129 list_for_each_entry(ns, &ctrl->namespaces, list) {
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001130 if (ns->disk && nvme_revalidate_disk(ns->disk)) {
1131 list_move_tail(&ns->list, &rm_list);
1132 }
Keith Busch84fef622017-11-07 10:28:32 -07001133 }
1134 mutex_unlock(&ctrl->namespaces_mutex);
Jianchao Wang3fd176b2018-02-12 20:54:45 +08001135
1136 list_for_each_entry_safe(ns, next, &rm_list, list)
1137 nvme_ns_remove(ns);
Keith Busch84fef622017-11-07 10:28:32 -07001138}
1139
1140static void nvme_passthru_end(struct nvme_ctrl *ctrl, u32 effects)
1141{
1142 /*
1143 * Revalidate LBA changes prior to unfreezing. This is necessary to
1144 * prevent memory corruption if a logical block size was changed by
1145 * this command.
1146 */
1147 if (effects & NVME_CMD_EFFECTS_LBCC)
1148 nvme_update_formats(ctrl);
1149 if (effects & (NVME_CMD_EFFECTS_LBCC | NVME_CMD_EFFECTS_CSE_MASK))
1150 nvme_unfreeze(ctrl);
1151 if (effects & NVME_CMD_EFFECTS_CCC)
1152 nvme_init_identify(ctrl);
1153 if (effects & (NVME_CMD_EFFECTS_NIC | NVME_CMD_EFFECTS_NCC))
1154 nvme_queue_scan(ctrl);
1155}
1156
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01001157static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001158 struct nvme_passthru_cmd __user *ucmd)
1159{
1160 struct nvme_passthru_cmd cmd;
1161 struct nvme_command c;
1162 unsigned timeout = 0;
Keith Busch84fef622017-11-07 10:28:32 -07001163 u32 effects;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001164 int status;
1165
1166 if (!capable(CAP_SYS_ADMIN))
1167 return -EACCES;
1168 if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
1169 return -EFAULT;
Keith Busch63088ec2016-02-24 09:15:57 -07001170 if (cmd.flags)
1171 return -EINVAL;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001172
1173 memset(&c, 0, sizeof(c));
1174 c.common.opcode = cmd.opcode;
1175 c.common.flags = cmd.flags;
1176 c.common.nsid = cpu_to_le32(cmd.nsid);
1177 c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
1178 c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
1179 c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
1180 c.common.cdw10[1] = cpu_to_le32(cmd.cdw11);
1181 c.common.cdw10[2] = cpu_to_le32(cmd.cdw12);
1182 c.common.cdw10[3] = cpu_to_le32(cmd.cdw13);
1183 c.common.cdw10[4] = cpu_to_le32(cmd.cdw14);
1184 c.common.cdw10[5] = cpu_to_le32(cmd.cdw15);
1185
1186 if (cmd.timeout_ms)
1187 timeout = msecs_to_jiffies(cmd.timeout_ms);
1188
Keith Busch84fef622017-11-07 10:28:32 -07001189 effects = nvme_passthru_start(ctrl, ns, cmd.opcode);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001190 status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
Arnd Bergmannd1ea7be2015-12-08 16:22:17 +01001191 (void __user *)(uintptr_t)cmd.addr, cmd.data_len,
Keith Busch63263d62017-08-29 17:46:04 -04001192 (void __user *)(uintptr_t)cmd.metadata, cmd.metadata,
1193 0, &cmd.result, timeout);
Keith Busch84fef622017-11-07 10:28:32 -07001194 nvme_passthru_end(ctrl, effects);
1195
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001196 if (status >= 0) {
1197 if (put_user(cmd.result, &ucmd->result))
1198 return -EFAULT;
1199 }
1200
1201 return status;
1202}
1203
Christoph Hellwig32acab32017-11-02 12:59:30 +01001204/*
1205 * Issue ioctl requests on the first available path. Note that unlike normal
1206 * block layer requests we will not retry failed request on another controller.
1207 */
1208static struct nvme_ns *nvme_get_ns_from_disk(struct gendisk *disk,
1209 struct nvme_ns_head **head, int *srcu_idx)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001210{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001211#ifdef CONFIG_NVME_MULTIPATH
1212 if (disk->fops == &nvme_ns_head_ops) {
1213 *head = disk->private_data;
1214 *srcu_idx = srcu_read_lock(&(*head)->srcu);
1215 return nvme_find_path(*head);
1216 }
1217#endif
1218 *head = NULL;
1219 *srcu_idx = -1;
1220 return disk->private_data;
1221}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001222
Christoph Hellwig32acab32017-11-02 12:59:30 +01001223static void nvme_put_ns_from_disk(struct nvme_ns_head *head, int idx)
1224{
1225 if (head)
1226 srcu_read_unlock(&head->srcu, idx);
1227}
1228
1229static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned cmd, unsigned long arg)
1230{
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001231 switch (cmd) {
1232 case NVME_IOCTL_ID:
1233 force_successful_syscall_return();
Christoph Hellwiged754e52017-11-09 13:50:43 +01001234 return ns->head->ns_id;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001235 case NVME_IOCTL_ADMIN_CMD:
1236 return nvme_user_cmd(ns->ctrl, NULL, (void __user *)arg);
1237 case NVME_IOCTL_IO_CMD:
1238 return nvme_user_cmd(ns->ctrl, ns, (void __user *)arg);
1239 case NVME_IOCTL_SUBMIT_IO:
1240 return nvme_submit_io(ns, (void __user *)arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001241 default:
Matias Bjørling84d4add2017-01-31 13:17:16 +01001242#ifdef CONFIG_NVM
1243 if (ns->ndev)
1244 return nvme_nvm_ioctl(ns, cmd, arg);
1245#endif
Scott Bauera98e58e52017-02-03 12:50:32 -07001246 if (is_sed_ioctl(cmd))
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001247 return sed_ioctl(ns->ctrl->opal_dev, cmd,
Scott Bauere225c202017-02-14 17:29:36 -07001248 (void __user *) arg);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001249 return -ENOTTY;
1250 }
1251}
1252
Christoph Hellwig32acab32017-11-02 12:59:30 +01001253static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
1254 unsigned int cmd, unsigned long arg)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001255{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001256 struct nvme_ns_head *head = NULL;
1257 struct nvme_ns *ns;
1258 int srcu_idx, ret;
1259
1260 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1261 if (unlikely(!ns))
1262 ret = -EWOULDBLOCK;
1263 else
1264 ret = nvme_ns_ioctl(ns, cmd, arg);
1265 nvme_put_ns_from_disk(head, srcu_idx);
1266 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001267}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001268
1269static int nvme_open(struct block_device *bdev, fmode_t mode)
1270{
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001271 struct nvme_ns *ns = bdev->bd_disk->private_data;
1272
Christoph Hellwig32acab32017-11-02 12:59:30 +01001273#ifdef CONFIG_NVME_MULTIPATH
1274 /* should never be called due to GENHD_FL_HIDDEN */
1275 if (WARN_ON_ONCE(ns->head->disk))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001276 goto fail;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001277#endif
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001278 if (!kref_get_unless_zero(&ns->kref))
Nitzan Carmi85088c42018-01-04 17:56:13 +02001279 goto fail;
1280 if (!try_module_get(ns->ctrl->ops->module))
1281 goto fail_put_ns;
1282
Christoph Hellwigc6424a92017-10-18 13:22:00 +02001283 return 0;
Nitzan Carmi85088c42018-01-04 17:56:13 +02001284
1285fail_put_ns:
1286 nvme_put_ns(ns);
1287fail:
1288 return -ENXIO;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001289}
1290
1291static void nvme_release(struct gendisk *disk, fmode_t mode)
1292{
Nitzan Carmi85088c42018-01-04 17:56:13 +02001293 struct nvme_ns *ns = disk->private_data;
1294
1295 module_put(ns->ctrl->ops->module);
1296 nvme_put_ns(ns);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001297}
1298
1299static int nvme_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1300{
1301 /* some standard values */
1302 geo->heads = 1 << 6;
1303 geo->sectors = 1 << 5;
1304 geo->cylinders = get_capacity(bdev->bd_disk) >> 11;
1305 return 0;
1306}
1307
1308#ifdef CONFIG_BLK_DEV_INTEGRITY
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001309static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001310{
1311 struct blk_integrity integrity;
1312
Jay Freyenseefa9a89f2016-07-20 21:26:16 -06001313 memset(&integrity, 0, sizeof(integrity));
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001314 switch (pi_type) {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001315 case NVME_NS_DPS_PI_TYPE3:
1316 integrity.profile = &t10_pi_type3_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001317 integrity.tag_size = sizeof(u16) + sizeof(u32);
1318 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001319 break;
1320 case NVME_NS_DPS_PI_TYPE1:
1321 case NVME_NS_DPS_PI_TYPE2:
1322 integrity.profile = &t10_pi_type1_crc;
Nicholas Bellingerba36c212016-04-09 03:04:42 +00001323 integrity.tag_size = sizeof(u16);
1324 integrity.flags |= BLK_INTEGRITY_DEVICE_CAPABLE;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001325 break;
1326 default:
1327 integrity.profile = NULL;
1328 break;
1329 }
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001330 integrity.tuple_size = ms;
1331 blk_integrity_register(disk, &integrity);
1332 blk_queue_max_integrity_segments(disk->queue, 1);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001333}
1334#else
Christoph Hellwig39b7baa2017-11-02 21:28:53 +03001335static void nvme_init_integrity(struct gendisk *disk, u16 ms, u8 pi_type)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001336{
1337}
1338#endif /* CONFIG_BLK_DEV_INTEGRITY */
1339
Scott Bauer6b8190d2017-06-15 10:44:30 -06001340static void nvme_set_chunk_size(struct nvme_ns *ns)
1341{
1342 u32 chunk_size = (((u32)ns->noiob) << (ns->lba_shift - 9));
1343 blk_queue_chunk_sectors(ns->queue, rounddown_pow_of_two(chunk_size));
1344}
1345
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001346static void nvme_config_discard(struct nvme_ctrl *ctrl,
1347 unsigned stream_alignment, struct request_queue *queue)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001348{
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001349 u32 size = queue_logical_block_size(queue);
1350
1351 if (stream_alignment)
1352 size *= stream_alignment;
Keith Busch08095e72016-03-04 13:15:17 -07001353
Christoph Hellwigb35ba012017-02-08 14:46:50 +01001354 BUILD_BUG_ON(PAGE_SIZE / sizeof(struct nvme_dsm_range) <
1355 NVME_DSM_MAX_RANGES);
1356
David Disseldorpb224f612017-11-24 16:30:53 +01001357 queue->limits.discard_alignment = 0;
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001358 queue->limits.discard_granularity = size;
Jens Axboef5d11842017-06-27 12:03:06 -06001359
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001360 blk_queue_max_discard_sectors(queue, UINT_MAX);
1361 blk_queue_max_discard_segments(queue, NVME_DSM_MAX_RANGES);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001362 blk_queue_flag_set(QUEUE_FLAG_DISCARD, queue);
Christoph Hellwige850fd12017-04-05 19:21:13 +02001363
1364 if (ctrl->quirks & NVME_QUIRK_DEALLOCATE_ZEROES)
Christoph Hellwig30e5e922017-11-02 21:28:54 +03001365 blk_queue_max_write_zeroes_sectors(queue, UINT_MAX);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001366}
1367
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001368static void nvme_report_ns_ids(struct nvme_ctrl *ctrl, unsigned int nsid,
Christoph Hellwig002fab02017-11-09 13:50:16 +01001369 struct nvme_id_ns *id, struct nvme_ns_ids *ids)
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001370{
Christoph Hellwig002fab02017-11-09 13:50:16 +01001371 memset(ids, 0, sizeof(*ids));
1372
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001373 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001374 memcpy(ids->eui64, id->eui64, sizeof(id->eui64));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001375 if (ctrl->vs >= NVME_VS(1, 2, 0))
Christoph Hellwig002fab02017-11-09 13:50:16 +01001376 memcpy(ids->nguid, id->nguid, sizeof(id->nguid));
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001377 if (ctrl->vs >= NVME_VS(1, 3, 0)) {
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001378 /* Don't treat error as fatal we potentially
1379 * already have a NGUID or EUI-64
1380 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01001381 if (nvme_identify_ns_descs(ctrl, nsid, ids))
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001382 dev_warn(ctrl->device,
Johannes Thumshirn3b22ba22017-06-07 11:45:34 +02001383 "%s: Identify Descriptors failed\n", __func__);
1384 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001385}
1386
Christoph Hellwiged754e52017-11-09 13:50:43 +01001387static bool nvme_ns_ids_valid(struct nvme_ns_ids *ids)
1388{
1389 return !uuid_is_null(&ids->uuid) ||
1390 memchr_inv(ids->nguid, 0, sizeof(ids->nguid)) ||
1391 memchr_inv(ids->eui64, 0, sizeof(ids->eui64));
1392}
1393
Christoph Hellwig002fab02017-11-09 13:50:16 +01001394static bool nvme_ns_ids_equal(struct nvme_ns_ids *a, struct nvme_ns_ids *b)
1395{
1396 return uuid_equal(&a->uuid, &b->uuid) &&
1397 memcmp(&a->nguid, &b->nguid, sizeof(a->nguid)) == 0 &&
1398 memcmp(&a->eui64, &b->eui64, sizeof(a->eui64)) == 0;
1399}
1400
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001401static void nvme_update_disk_info(struct gendisk *disk,
1402 struct nvme_ns *ns, struct nvme_id_ns *id)
1403{
1404 sector_t capacity = le64_to_cpup(&id->nsze) << (ns->lba_shift - 9);
Jeff Liencee160f2017-12-19 13:24:15 -06001405 unsigned short bs = 1 << ns->lba_shift;
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001406 unsigned stream_alignment = 0;
1407
1408 if (ns->ctrl->nr_streams && ns->sws && ns->sgs)
1409 stream_alignment = ns->sws * ns->sgs;
1410
1411 blk_mq_freeze_queue(disk->queue);
1412 blk_integrity_unregister(disk);
1413
Jeff Liencee160f2017-12-19 13:24:15 -06001414 blk_queue_logical_block_size(disk->queue, bs);
1415 blk_queue_physical_block_size(disk->queue, bs);
1416 blk_queue_io_min(disk->queue, bs);
1417
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001418 if (ns->ms && !ns->ext &&
1419 (ns->ctrl->ops->flags & NVME_F_METADATA_SUPPORTED))
1420 nvme_init_integrity(disk, ns->ms, ns->pi_type);
Christoph Hellwig715ea9e2017-11-07 17:27:34 +01001421 if (ns->ms && !nvme_ns_has_pi(ns) && !blk_get_integrity(disk))
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001422 capacity = 0;
1423 set_capacity(disk, capacity);
1424
1425 if (ns->ctrl->oncs & NVME_CTRL_ONCS_DSM)
1426 nvme_config_discard(ns->ctrl, stream_alignment, disk->queue);
1427 blk_mq_unfreeze_queue(disk->queue);
1428}
1429
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001430static void __nvme_revalidate_disk(struct gendisk *disk, struct nvme_id_ns *id)
1431{
1432 struct nvme_ns *ns = disk->private_data;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001433
1434 /*
1435 * If identify namespace failed, use default 512 byte block size so
1436 * block layer can use before failing read/write for 0 capacity.
1437 */
Christoph Hellwigc81bfba2017-05-20 15:14:45 +02001438 ns->lba_shift = id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ds;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001439 if (ns->lba_shift == 0)
1440 ns->lba_shift = 9;
Scott Bauer6b8190d2017-06-15 10:44:30 -06001441 ns->noiob = le16_to_cpu(id->noiob);
Christoph Hellwigb5be3b32017-11-02 21:28:52 +03001442 ns->ext = ns->ms && (id->flbas & NVME_NS_FLBAS_META_EXT);
1443 ns->ms = le16_to_cpu(id->lbaf[id->flbas & NVME_NS_FLBAS_LBA_MASK].ms);
1444 /* the PI implementation requires metadata equal t10 pi tuple size */
1445 if (ns->ms == sizeof(struct t10_pi_tuple))
1446 ns->pi_type = id->dps & NVME_NS_DPS_PI_MASK;
1447 else
1448 ns->pi_type = 0;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001449
Scott Bauer6b8190d2017-06-15 10:44:30 -06001450 if (ns->noiob)
1451 nvme_set_chunk_size(ns);
Christoph Hellwig24b0b582017-11-02 21:28:56 +03001452 nvme_update_disk_info(disk, ns, id);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001453#ifdef CONFIG_NVME_MULTIPATH
1454 if (ns->head->disk)
1455 nvme_update_disk_info(ns->head->disk, ns, id);
1456#endif
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001457}
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001458
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001459static int nvme_revalidate_disk(struct gendisk *disk)
1460{
1461 struct nvme_ns *ns = disk->private_data;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001462 struct nvme_ctrl *ctrl = ns->ctrl;
1463 struct nvme_id_ns *id;
Christoph Hellwig002fab02017-11-09 13:50:16 +01001464 struct nvme_ns_ids ids;
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001465 int ret = 0;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001466
1467 if (test_bit(NVME_NS_DEAD, &ns->flags)) {
1468 set_capacity(disk, 0);
1469 return -ENODEV;
1470 }
1471
Christoph Hellwiged754e52017-11-09 13:50:43 +01001472 id = nvme_identify_ns(ctrl, ns->head->ns_id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001473 if (!id)
1474 return -ENODEV;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001475
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001476 if (id->ncap == 0) {
1477 ret = -ENODEV;
1478 goto out;
1479 }
1480
Keith Busch5e0fab52017-10-27 13:51:22 -06001481 __nvme_revalidate_disk(disk, id);
Christoph Hellwiged754e52017-11-09 13:50:43 +01001482 nvme_report_ns_ids(ctrl, ns->head->ns_id, id, &ids);
1483 if (!nvme_ns_ids_equal(&ns->head->ids, &ids)) {
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001484 dev_err(ctrl->device,
Christoph Hellwiged754e52017-11-09 13:50:43 +01001485 "identifiers changed for nsid %d\n", ns->head->ns_id);
Christoph Hellwig1d5df6a2017-08-17 14:10:00 +02001486 ret = -ENODEV;
1487 }
1488
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001489out:
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001490 kfree(id);
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02001491 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001492}
1493
1494static char nvme_pr_type(enum pr_type type)
1495{
1496 switch (type) {
1497 case PR_WRITE_EXCLUSIVE:
1498 return 1;
1499 case PR_EXCLUSIVE_ACCESS:
1500 return 2;
1501 case PR_WRITE_EXCLUSIVE_REG_ONLY:
1502 return 3;
1503 case PR_EXCLUSIVE_ACCESS_REG_ONLY:
1504 return 4;
1505 case PR_WRITE_EXCLUSIVE_ALL_REGS:
1506 return 5;
1507 case PR_EXCLUSIVE_ACCESS_ALL_REGS:
1508 return 6;
1509 default:
1510 return 0;
1511 }
1512};
1513
1514static int nvme_pr_command(struct block_device *bdev, u32 cdw10,
1515 u64 key, u64 sa_key, u8 op)
1516{
Christoph Hellwig32acab32017-11-02 12:59:30 +01001517 struct nvme_ns_head *head = NULL;
1518 struct nvme_ns *ns;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001519 struct nvme_command c;
Christoph Hellwig32acab32017-11-02 12:59:30 +01001520 int srcu_idx, ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001521 u8 data[16] = { 0, };
1522
Keith Buschb0d61d52017-11-16 13:36:49 -07001523 ns = nvme_get_ns_from_disk(bdev->bd_disk, &head, &srcu_idx);
1524 if (unlikely(!ns))
1525 return -EWOULDBLOCK;
1526
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001527 put_unaligned_le64(key, &data[0]);
1528 put_unaligned_le64(sa_key, &data[8]);
1529
1530 memset(&c, 0, sizeof(c));
1531 c.common.opcode = op;
Keith Buschb0d61d52017-11-16 13:36:49 -07001532 c.common.nsid = cpu_to_le32(ns->head->ns_id);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001533 c.common.cdw10[0] = cpu_to_le32(cdw10);
1534
Keith Buschb0d61d52017-11-16 13:36:49 -07001535 ret = nvme_submit_sync_cmd(ns->queue, &c, data, 16);
Christoph Hellwig32acab32017-11-02 12:59:30 +01001536 nvme_put_ns_from_disk(head, srcu_idx);
1537 return ret;
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001538}
1539
1540static int nvme_pr_register(struct block_device *bdev, u64 old,
1541 u64 new, unsigned flags)
1542{
1543 u32 cdw10;
1544
1545 if (flags & ~PR_FL_IGNORE_KEY)
1546 return -EOPNOTSUPP;
1547
1548 cdw10 = old ? 2 : 0;
1549 cdw10 |= (flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0;
1550 cdw10 |= (1 << 30) | (1 << 31); /* PTPL=1 */
1551 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_register);
1552}
1553
1554static int nvme_pr_reserve(struct block_device *bdev, u64 key,
1555 enum pr_type type, unsigned flags)
1556{
1557 u32 cdw10;
1558
1559 if (flags & ~PR_FL_IGNORE_KEY)
1560 return -EOPNOTSUPP;
1561
1562 cdw10 = nvme_pr_type(type) << 8;
1563 cdw10 |= ((flags & PR_FL_IGNORE_KEY) ? 1 << 3 : 0);
1564 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_acquire);
1565}
1566
1567static int nvme_pr_preempt(struct block_device *bdev, u64 old, u64 new,
1568 enum pr_type type, bool abort)
1569{
1570 u32 cdw10 = nvme_pr_type(type) << 8 | abort ? 2 : 1;
1571 return nvme_pr_command(bdev, cdw10, old, new, nvme_cmd_resv_acquire);
1572}
1573
1574static int nvme_pr_clear(struct block_device *bdev, u64 key)
1575{
Dan Carpenter8c0b3912015-12-09 13:24:06 +03001576 u32 cdw10 = 1 | (key ? 1 << 3 : 0);
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001577 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_register);
1578}
1579
1580static int nvme_pr_release(struct block_device *bdev, u64 key, enum pr_type type)
1581{
1582 u32 cdw10 = nvme_pr_type(type) << 8 | key ? 1 << 3 : 0;
1583 return nvme_pr_command(bdev, cdw10, key, 0, nvme_cmd_resv_release);
1584}
1585
1586static const struct pr_ops nvme_pr_ops = {
1587 .pr_register = nvme_pr_register,
1588 .pr_reserve = nvme_pr_reserve,
1589 .pr_release = nvme_pr_release,
1590 .pr_preempt = nvme_pr_preempt,
1591 .pr_clear = nvme_pr_clear,
1592};
1593
Scott Bauera98e58e52017-02-03 12:50:32 -07001594#ifdef CONFIG_BLK_SED_OPAL
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001595int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
1596 bool send)
Scott Bauera98e58e52017-02-03 12:50:32 -07001597{
Christoph Hellwig4f1244c2017-02-17 13:59:39 +01001598 struct nvme_ctrl *ctrl = data;
Scott Bauera98e58e52017-02-03 12:50:32 -07001599 struct nvme_command cmd;
Scott Bauera98e58e52017-02-03 12:50:32 -07001600
1601 memset(&cmd, 0, sizeof(cmd));
1602 if (send)
1603 cmd.common.opcode = nvme_admin_security_send;
1604 else
1605 cmd.common.opcode = nvme_admin_security_recv;
Scott Bauera98e58e52017-02-03 12:50:32 -07001606 cmd.common.nsid = 0;
1607 cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
1608 cmd.common.cdw10[1] = cpu_to_le32(len);
1609
1610 return __nvme_submit_sync_cmd(ctrl->admin_q, &cmd, NULL, buffer, len,
1611 ADMIN_TIMEOUT, NVME_QID_ANY, 1, 0);
1612}
1613EXPORT_SYMBOL_GPL(nvme_sec_submit);
1614#endif /* CONFIG_BLK_SED_OPAL */
1615
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01001616static const struct block_device_operations nvme_fops = {
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001617 .owner = THIS_MODULE,
1618 .ioctl = nvme_ioctl,
Christoph Hellwig761f2e12017-10-05 18:46:46 +02001619 .compat_ioctl = nvme_ioctl,
Christoph Hellwig1673f1f2015-11-26 10:54:19 +01001620 .open = nvme_open,
1621 .release = nvme_release,
1622 .getgeo = nvme_getgeo,
1623 .revalidate_disk= nvme_revalidate_disk,
1624 .pr_ops = &nvme_pr_ops,
1625};
1626
Christoph Hellwig32acab32017-11-02 12:59:30 +01001627#ifdef CONFIG_NVME_MULTIPATH
1628static int nvme_ns_head_open(struct block_device *bdev, fmode_t mode)
1629{
1630 struct nvme_ns_head *head = bdev->bd_disk->private_data;
1631
1632 if (!kref_get_unless_zero(&head->ref))
1633 return -ENXIO;
1634 return 0;
1635}
1636
1637static void nvme_ns_head_release(struct gendisk *disk, fmode_t mode)
1638{
1639 nvme_put_ns_head(disk->private_data);
1640}
1641
1642const struct block_device_operations nvme_ns_head_ops = {
1643 .owner = THIS_MODULE,
1644 .open = nvme_ns_head_open,
1645 .release = nvme_ns_head_release,
1646 .ioctl = nvme_ioctl,
1647 .compat_ioctl = nvme_ioctl,
1648 .getgeo = nvme_getgeo,
1649 .pr_ops = &nvme_pr_ops,
1650};
1651#endif /* CONFIG_NVME_MULTIPATH */
1652
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001653static int nvme_wait_ready(struct nvme_ctrl *ctrl, u64 cap, bool enabled)
1654{
1655 unsigned long timeout =
1656 ((NVME_CAP_TIMEOUT(cap) + 1) * HZ / 2) + jiffies;
1657 u32 csts, bit = enabled ? NVME_CSTS_RDY : 0;
1658 int ret;
1659
1660 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
Keith Busch0df1e4f2016-10-11 13:31:58 -04001661 if (csts == ~0)
1662 return -ENODEV;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001663 if ((csts & NVME_CSTS_RDY) == bit)
1664 break;
1665
1666 msleep(100);
1667 if (fatal_signal_pending(current))
1668 return -EINTR;
1669 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001670 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001671 "Device not ready; aborting %s\n", enabled ?
1672 "initialisation" : "reset");
1673 return -ENODEV;
1674 }
1675 }
1676
1677 return ret;
1678}
1679
1680/*
1681 * If the device has been passed off to us in an enabled state, just clear
1682 * the enabled bit. The spec says we should set the 'shutdown notification
1683 * bits', but doing so may cause the device to complete commands to the
1684 * admin queue ... and we don't know what memory that might be pointing at!
1685 */
1686int nvme_disable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1687{
1688 int ret;
1689
1690 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1691 ctrl->ctrl_config &= ~NVME_CC_ENABLE;
1692
1693 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1694 if (ret)
1695 return ret;
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001696
Guilherme G. Piccolib5a10c52016-12-28 22:13:15 -02001697 if (ctrl->quirks & NVME_QUIRK_DELAY_BEFORE_CHK_RDY)
Guilherme G. Piccoli54adc012016-06-14 18:22:41 -03001698 msleep(NVME_QUIRK_DELAY_AMOUNT);
1699
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001700 return nvme_wait_ready(ctrl, cap, false);
1701}
Ming Lin576d55d2016-02-10 10:03:32 -08001702EXPORT_SYMBOL_GPL(nvme_disable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001703
1704int nvme_enable_ctrl(struct nvme_ctrl *ctrl, u64 cap)
1705{
1706 /*
1707 * Default to a 4K page size, with the intention to update this
1708 * path in the future to accomodate architectures with differing
1709 * kernel and IO page sizes.
1710 */
1711 unsigned dev_page_min = NVME_CAP_MPSMIN(cap) + 12, page_shift = 12;
1712 int ret;
1713
1714 if (page_shift < dev_page_min) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001715 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001716 "Minimum device page size %u too large for host (%u)\n",
1717 1 << dev_page_min, 1 << page_shift);
1718 return -ENODEV;
1719 }
1720
1721 ctrl->page_size = 1 << page_shift;
1722
1723 ctrl->ctrl_config = NVME_CC_CSS_NVM;
1724 ctrl->ctrl_config |= (page_shift - 12) << NVME_CC_MPS_SHIFT;
Max Gurtovoy60b43f62017-08-13 19:21:07 +03001725 ctrl->ctrl_config |= NVME_CC_AMS_RR | NVME_CC_SHN_NONE;
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001726 ctrl->ctrl_config |= NVME_CC_IOSQES | NVME_CC_IOCQES;
1727 ctrl->ctrl_config |= NVME_CC_ENABLE;
1728
1729 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1730 if (ret)
1731 return ret;
1732 return nvme_wait_ready(ctrl, cap, true);
1733}
Ming Lin576d55d2016-02-10 10:03:32 -08001734EXPORT_SYMBOL_GPL(nvme_enable_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001735
1736int nvme_shutdown_ctrl(struct nvme_ctrl *ctrl)
1737{
Martin K. Petersen07fbd322017-08-25 19:14:50 -04001738 unsigned long timeout = jiffies + (ctrl->shutdown_timeout * HZ);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001739 u32 csts;
1740 int ret;
1741
1742 ctrl->ctrl_config &= ~NVME_CC_SHN_MASK;
1743 ctrl->ctrl_config |= NVME_CC_SHN_NORMAL;
1744
1745 ret = ctrl->ops->reg_write32(ctrl, NVME_REG_CC, ctrl->ctrl_config);
1746 if (ret)
1747 return ret;
1748
1749 while ((ret = ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts)) == 0) {
1750 if ((csts & NVME_CSTS_SHST_MASK) == NVME_CSTS_SHST_CMPLT)
1751 break;
1752
1753 msleep(100);
1754 if (fatal_signal_pending(current))
1755 return -EINTR;
1756 if (time_after(jiffies, timeout)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07001757 dev_err(ctrl->device,
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001758 "Device shutdown incomplete; abort shutdown\n");
1759 return -ENODEV;
1760 }
1761 }
1762
1763 return ret;
1764}
Ming Lin576d55d2016-02-10 10:03:32 -08001765EXPORT_SYMBOL_GPL(nvme_shutdown_ctrl);
Christoph Hellwig5fd4ce12015-11-28 15:03:49 +01001766
Christoph Hellwigda358252016-03-02 18:07:11 +01001767static void nvme_set_queue_limits(struct nvme_ctrl *ctrl,
1768 struct request_queue *q)
1769{
Jens Axboe7c88cb02016-04-12 15:43:09 -06001770 bool vwc = false;
1771
Christoph Hellwigda358252016-03-02 18:07:11 +01001772 if (ctrl->max_hw_sectors) {
Christoph Hellwig45686b62016-03-02 18:07:12 +01001773 u32 max_segments =
1774 (ctrl->max_hw_sectors / (ctrl->page_size >> 9)) + 1;
1775
Christoph Hellwigda358252016-03-02 18:07:11 +01001776 blk_queue_max_hw_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwig45686b62016-03-02 18:07:12 +01001777 blk_queue_max_segments(q, min_t(u32, max_segments, USHRT_MAX));
Christoph Hellwigda358252016-03-02 18:07:11 +01001778 }
Keith Busch249159c2017-12-14 11:20:14 -07001779 if ((ctrl->quirks & NVME_QUIRK_STRIPE_SIZE) &&
1780 is_power_of_2(ctrl->max_hw_sectors))
Keith Busche6282ae2016-12-19 11:37:50 -05001781 blk_queue_chunk_sectors(q, ctrl->max_hw_sectors);
Christoph Hellwigda358252016-03-02 18:07:11 +01001782 blk_queue_virt_boundary(q, ctrl->page_size - 1);
Jens Axboe7c88cb02016-04-12 15:43:09 -06001783 if (ctrl->vwc & NVME_CTRL_VWC_PRESENT)
1784 vwc = true;
1785 blk_queue_write_cache(q, vwc, vwc);
Christoph Hellwigda358252016-03-02 18:07:11 +01001786}
1787
Jon Derrickdbf86b32017-08-16 09:51:29 +02001788static int nvme_configure_timestamp(struct nvme_ctrl *ctrl)
1789{
1790 __le64 ts;
1791 int ret;
1792
1793 if (!(ctrl->oncs & NVME_CTRL_ONCS_TIMESTAMP))
1794 return 0;
1795
1796 ts = cpu_to_le64(ktime_to_ms(ktime_get_real()));
1797 ret = nvme_set_features(ctrl, NVME_FEAT_TIMESTAMP, 0, &ts, sizeof(ts),
1798 NULL);
1799 if (ret)
1800 dev_warn_once(ctrl->device,
1801 "could not set timestamp (%d)\n", ret);
1802 return ret;
1803}
1804
Keith Busch634b8322017-08-10 11:23:31 +02001805static int nvme_configure_apst(struct nvme_ctrl *ctrl)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001806{
1807 /*
1808 * APST (Autonomous Power State Transition) lets us program a
1809 * table of power state transitions that the controller will
1810 * perform automatically. We configure it with a simple
1811 * heuristic: we are willing to spend at most 2% of the time
1812 * transitioning between power states. Therefore, when running
1813 * in any given state, we will enter the next lower-power
Andy Lutomirski76e4ad02017-04-21 16:19:22 -07001814 * non-operational state after waiting 50 * (enlat + exlat)
Kai-Heng Fengda875912017-06-07 15:25:42 +08001815 * microseconds, as long as that state's exit latency is under
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001816 * the requested maximum latency.
1817 *
1818 * We will not autonomously enter any non-operational state for
1819 * which the total latency exceeds ps_max_latency_us. Users
1820 * can set ps_max_latency_us to zero to turn off APST.
1821 */
1822
1823 unsigned apste;
1824 struct nvme_feat_auto_pst *table;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001825 u64 max_lat_us = 0;
1826 int max_ps = -1;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001827 int ret;
1828
1829 /*
1830 * If APST isn't supported or if we haven't been initialized yet,
1831 * then don't do anything.
1832 */
1833 if (!ctrl->apsta)
Keith Busch634b8322017-08-10 11:23:31 +02001834 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001835
1836 if (ctrl->npss > 31) {
1837 dev_warn(ctrl->device, "NPSS is invalid; not using APST\n");
Keith Busch634b8322017-08-10 11:23:31 +02001838 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001839 }
1840
1841 table = kzalloc(sizeof(*table), GFP_KERNEL);
1842 if (!table)
Keith Busch634b8322017-08-10 11:23:31 +02001843 return 0;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001844
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04001845 if (!ctrl->apst_enabled || ctrl->ps_max_latency_us == 0) {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001846 /* Turn off APST. */
1847 apste = 0;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001848 dev_dbg(ctrl->device, "APST disabled\n");
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001849 } else {
1850 __le64 target = cpu_to_le64(0);
1851 int state;
1852
1853 /*
1854 * Walk through all states from lowest- to highest-power.
1855 * According to the spec, lower-numbered states use more
1856 * power. NPSS, despite the name, is the index of the
1857 * lowest-power state, not the number of states.
1858 */
1859 for (state = (int)ctrl->npss; state >= 0; state--) {
Kai-Heng Fengda875912017-06-07 15:25:42 +08001860 u64 total_latency_us, exit_latency_us, transition_ms;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001861
1862 if (target)
1863 table->entries[state] = target;
1864
1865 /*
Andy Lutomirskiff5350a2017-04-20 13:37:55 -07001866 * Don't allow transitions to the deepest state
1867 * if it's quirked off.
1868 */
1869 if (state == ctrl->npss &&
1870 (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS))
1871 continue;
1872
1873 /*
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001874 * Is this state a useful non-operational state for
1875 * higher-power states to autonomously transition to?
1876 */
1877 if (!(ctrl->psd[state].flags &
1878 NVME_PS_FLAGS_NON_OP_STATE))
1879 continue;
1880
Kai-Heng Fengda875912017-06-07 15:25:42 +08001881 exit_latency_us =
1882 (u64)le32_to_cpu(ctrl->psd[state].exit_lat);
1883 if (exit_latency_us > ctrl->ps_max_latency_us)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001884 continue;
1885
Kai-Heng Fengda875912017-06-07 15:25:42 +08001886 total_latency_us =
1887 exit_latency_us +
1888 le32_to_cpu(ctrl->psd[state].entry_lat);
1889
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001890 /*
1891 * This state is good. Use it as the APST idle
1892 * target for higher power states.
1893 */
1894 transition_ms = total_latency_us + 19;
1895 do_div(transition_ms, 20);
1896 if (transition_ms > (1 << 24) - 1)
1897 transition_ms = (1 << 24) - 1;
1898
1899 target = cpu_to_le64((state << 3) |
1900 (transition_ms << 8));
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001901
1902 if (max_ps == -1)
1903 max_ps = state;
1904
1905 if (total_latency_us > max_lat_us)
1906 max_lat_us = total_latency_us;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001907 }
1908
1909 apste = 1;
Andy Lutomirskifb0dc392017-04-21 16:19:23 -07001910
1911 if (max_ps == -1) {
1912 dev_dbg(ctrl->device, "APST enabled but no non-operational states are available\n");
1913 } else {
1914 dev_dbg(ctrl->device, "APST enabled: max PS = %d, max round-trip latency = %lluus, table = %*phN\n",
1915 max_ps, max_lat_us, (int)sizeof(*table), table);
1916 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001917 }
1918
1919 ret = nvme_set_features(ctrl, NVME_FEAT_AUTO_PST, apste,
1920 table, sizeof(*table), NULL);
1921 if (ret)
1922 dev_err(ctrl->device, "failed to set APST feature (%d)\n", ret);
1923
1924 kfree(table);
Keith Busch634b8322017-08-10 11:23:31 +02001925 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001926}
1927
1928static void nvme_set_latency_tolerance(struct device *dev, s32 val)
1929{
1930 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1931 u64 latency;
1932
1933 switch (val) {
1934 case PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT:
1935 case PM_QOS_LATENCY_ANY:
1936 latency = U64_MAX;
1937 break;
1938
1939 default:
1940 latency = val;
1941 }
1942
1943 if (ctrl->ps_max_latency_us != latency) {
1944 ctrl->ps_max_latency_us = latency;
1945 nvme_configure_apst(ctrl);
1946 }
1947}
1948
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001949struct nvme_core_quirk_entry {
1950 /*
1951 * NVMe model and firmware strings are padded with spaces. For
1952 * simplicity, strings in the quirk table are padded with NULLs
1953 * instead.
1954 */
1955 u16 vid;
1956 const char *mn;
1957 const char *fr;
1958 unsigned long quirks;
1959};
1960
1961static const struct nvme_core_quirk_entry core_quirks[] = {
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001962 {
Andy Lutomirskibe569452017-04-20 13:37:56 -07001963 /*
1964 * This Toshiba device seems to die using any APST states. See:
1965 * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1678184/comments/11
1966 */
1967 .vid = 0x1179,
1968 .mn = "THNSF5256GPUK TOSHIBA",
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08001969 .quirks = NVME_QUIRK_NO_APST,
Andy Lutomirskibe569452017-04-20 13:37:56 -07001970 }
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07001971};
1972
1973/* match is null-terminated but idstr is space-padded. */
1974static bool string_matches(const char *idstr, const char *match, size_t len)
1975{
1976 size_t matchlen;
1977
1978 if (!match)
1979 return true;
1980
1981 matchlen = strlen(match);
1982 WARN_ON_ONCE(matchlen > len);
1983
1984 if (memcmp(idstr, match, matchlen))
1985 return false;
1986
1987 for (; matchlen < len; matchlen++)
1988 if (idstr[matchlen] != ' ')
1989 return false;
1990
1991 return true;
1992}
1993
1994static bool quirk_matches(const struct nvme_id_ctrl *id,
1995 const struct nvme_core_quirk_entry *q)
1996{
1997 return q->vid == le16_to_cpu(id->vid) &&
1998 string_matches(id->mn, q->mn, sizeof(id->mn)) &&
1999 string_matches(id->fr, q->fr, sizeof(id->fr));
2000}
2001
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002002static void nvme_init_subnqn(struct nvme_subsystem *subsys, struct nvme_ctrl *ctrl,
2003 struct nvme_id_ctrl *id)
Christoph Hellwig180de0072017-06-26 12:39:02 +02002004{
2005 size_t nqnlen;
2006 int off;
2007
2008 nqnlen = strnlen(id->subnqn, NVMF_NQN_SIZE);
2009 if (nqnlen > 0 && nqnlen < NVMF_NQN_SIZE) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002010 strncpy(subsys->subnqn, id->subnqn, NVMF_NQN_SIZE);
Christoph Hellwig180de0072017-06-26 12:39:02 +02002011 return;
2012 }
2013
2014 if (ctrl->vs >= NVME_VS(1, 2, 1))
2015 dev_warn(ctrl->device, "missing or invalid SUBNQN field.\n");
2016
2017 /* Generate a "fake" NQN per Figure 254 in NVMe 1.3 + ECN 001 */
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002018 off = snprintf(subsys->subnqn, NVMF_NQN_SIZE,
Christoph Hellwig180de0072017-06-26 12:39:02 +02002019 "nqn.2014.08.org.nvmexpress:%4x%4x",
2020 le16_to_cpu(id->vid), le16_to_cpu(id->ssvid));
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002021 memcpy(subsys->subnqn + off, id->sn, sizeof(id->sn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002022 off += sizeof(id->sn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002023 memcpy(subsys->subnqn + off, id->mn, sizeof(id->mn));
Christoph Hellwig180de0072017-06-26 12:39:02 +02002024 off += sizeof(id->mn);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002025 memset(subsys->subnqn + off, 0, sizeof(subsys->subnqn) - off);
2026}
2027
2028static void __nvme_release_subsystem(struct nvme_subsystem *subsys)
2029{
2030 ida_simple_remove(&nvme_subsystems_ida, subsys->instance);
2031 kfree(subsys);
2032}
2033
2034static void nvme_release_subsystem(struct device *dev)
2035{
2036 __nvme_release_subsystem(container_of(dev, struct nvme_subsystem, dev));
2037}
2038
2039static void nvme_destroy_subsystem(struct kref *ref)
2040{
2041 struct nvme_subsystem *subsys =
2042 container_of(ref, struct nvme_subsystem, ref);
2043
2044 mutex_lock(&nvme_subsystems_lock);
2045 list_del(&subsys->entry);
2046 mutex_unlock(&nvme_subsystems_lock);
2047
Christoph Hellwiged754e52017-11-09 13:50:43 +01002048 ida_destroy(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002049 device_del(&subsys->dev);
2050 put_device(&subsys->dev);
2051}
2052
2053static void nvme_put_subsystem(struct nvme_subsystem *subsys)
2054{
2055 kref_put(&subsys->ref, nvme_destroy_subsystem);
2056}
2057
2058static struct nvme_subsystem *__nvme_find_get_subsystem(const char *subsysnqn)
2059{
2060 struct nvme_subsystem *subsys;
2061
2062 lockdep_assert_held(&nvme_subsystems_lock);
2063
2064 list_for_each_entry(subsys, &nvme_subsystems, entry) {
2065 if (strcmp(subsys->subnqn, subsysnqn))
2066 continue;
2067 if (!kref_get_unless_zero(&subsys->ref))
2068 continue;
2069 return subsys;
2070 }
2071
2072 return NULL;
2073}
2074
Hannes Reinecke1e496932017-11-10 10:58:23 +01002075#define SUBSYS_ATTR_RO(_name, _mode, _show) \
2076 struct device_attribute subsys_attr_##_name = \
2077 __ATTR(_name, _mode, _show, NULL)
2078
2079static ssize_t nvme_subsys_show_nqn(struct device *dev,
2080 struct device_attribute *attr,
2081 char *buf)
2082{
2083 struct nvme_subsystem *subsys =
2084 container_of(dev, struct nvme_subsystem, dev);
2085
2086 return snprintf(buf, PAGE_SIZE, "%s\n", subsys->subnqn);
2087}
2088static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
2089
2090#define nvme_subsys_show_str_function(field) \
2091static ssize_t subsys_##field##_show(struct device *dev, \
2092 struct device_attribute *attr, char *buf) \
2093{ \
2094 struct nvme_subsystem *subsys = \
2095 container_of(dev, struct nvme_subsystem, dev); \
2096 return sprintf(buf, "%.*s\n", \
2097 (int)sizeof(subsys->field), subsys->field); \
2098} \
2099static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
2100
2101nvme_subsys_show_str_function(model);
2102nvme_subsys_show_str_function(serial);
2103nvme_subsys_show_str_function(firmware_rev);
2104
2105static struct attribute *nvme_subsys_attrs[] = {
2106 &subsys_attr_model.attr,
2107 &subsys_attr_serial.attr,
2108 &subsys_attr_firmware_rev.attr,
2109 &subsys_attr_subsysnqn.attr,
2110 NULL,
2111};
2112
2113static struct attribute_group nvme_subsys_attrs_group = {
2114 .attrs = nvme_subsys_attrs,
2115};
2116
2117static const struct attribute_group *nvme_subsys_attrs_groups[] = {
2118 &nvme_subsys_attrs_group,
2119 NULL,
2120};
2121
Israel Rukshinb837b282018-01-04 17:56:14 +02002122static int nvme_active_ctrls(struct nvme_subsystem *subsys)
2123{
2124 int count = 0;
2125 struct nvme_ctrl *ctrl;
2126
2127 mutex_lock(&subsys->lock);
2128 list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
2129 if (ctrl->state != NVME_CTRL_DELETING &&
2130 ctrl->state != NVME_CTRL_DEAD)
2131 count++;
2132 }
2133 mutex_unlock(&subsys->lock);
2134
2135 return count;
2136}
2137
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002138static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
2139{
2140 struct nvme_subsystem *subsys, *found;
2141 int ret;
2142
2143 subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
2144 if (!subsys)
2145 return -ENOMEM;
2146 ret = ida_simple_get(&nvme_subsystems_ida, 0, 0, GFP_KERNEL);
2147 if (ret < 0) {
2148 kfree(subsys);
2149 return ret;
2150 }
2151 subsys->instance = ret;
2152 mutex_init(&subsys->lock);
2153 kref_init(&subsys->ref);
2154 INIT_LIST_HEAD(&subsys->ctrls);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002155 INIT_LIST_HEAD(&subsys->nsheads);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002156 nvme_init_subnqn(subsys, ctrl, id);
2157 memcpy(subsys->serial, id->sn, sizeof(subsys->serial));
2158 memcpy(subsys->model, id->mn, sizeof(subsys->model));
2159 memcpy(subsys->firmware_rev, id->fr, sizeof(subsys->firmware_rev));
2160 subsys->vendor_id = le16_to_cpu(id->vid);
2161 subsys->cmic = id->cmic;
2162
2163 subsys->dev.class = nvme_subsys_class;
2164 subsys->dev.release = nvme_release_subsystem;
Hannes Reinecke1e496932017-11-10 10:58:23 +01002165 subsys->dev.groups = nvme_subsys_attrs_groups;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002166 dev_set_name(&subsys->dev, "nvme-subsys%d", subsys->instance);
2167 device_initialize(&subsys->dev);
2168
2169 mutex_lock(&nvme_subsystems_lock);
2170 found = __nvme_find_get_subsystem(subsys->subnqn);
2171 if (found) {
2172 /*
2173 * Verify that the subsystem actually supports multiple
2174 * controllers, else bail out.
2175 */
Israel Rukshinb837b282018-01-04 17:56:14 +02002176 if (nvme_active_ctrls(found) && !(id->cmic & (1 << 1))) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002177 dev_err(ctrl->device,
2178 "ignoring ctrl due to duplicate subnqn (%s).\n",
2179 found->subnqn);
2180 nvme_put_subsystem(found);
2181 ret = -EINVAL;
2182 goto out_unlock;
2183 }
2184
2185 __nvme_release_subsystem(subsys);
2186 subsys = found;
2187 } else {
2188 ret = device_add(&subsys->dev);
2189 if (ret) {
2190 dev_err(ctrl->device,
2191 "failed to register subsystem device.\n");
2192 goto out_unlock;
2193 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002194 ida_init(&subsys->ns_ida);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002195 list_add_tail(&subsys->entry, &nvme_subsystems);
2196 }
2197
2198 ctrl->subsys = subsys;
2199 mutex_unlock(&nvme_subsystems_lock);
2200
2201 if (sysfs_create_link(&subsys->dev.kobj, &ctrl->device->kobj,
2202 dev_name(ctrl->device))) {
2203 dev_err(ctrl->device,
2204 "failed to create sysfs link from subsystem.\n");
2205 /* the transport driver will eventually put the subsystem */
2206 return -EINVAL;
2207 }
2208
2209 mutex_lock(&subsys->lock);
2210 list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
2211 mutex_unlock(&subsys->lock);
2212
2213 return 0;
2214
2215out_unlock:
2216 mutex_unlock(&nvme_subsystems_lock);
2217 put_device(&subsys->dev);
2218 return ret;
Christoph Hellwig180de0072017-06-26 12:39:02 +02002219}
2220
Keith Buschc627c482017-11-07 10:28:31 -07002221static int nvme_get_log(struct nvme_ctrl *ctrl, u8 log_page, void *log,
2222 size_t size)
2223{
2224 struct nvme_command c = { };
2225
2226 c.common.opcode = nvme_admin_get_log_page;
2227 c.common.nsid = cpu_to_le32(NVME_NSID_ALL);
2228 c.common.cdw10[0] = nvme_get_log_dw10(log_page, size);
2229
2230 return nvme_submit_sync_cmd(ctrl->admin_q, &c, log, size);
2231}
2232
Keith Busch84fef622017-11-07 10:28:32 -07002233static int nvme_get_effects_log(struct nvme_ctrl *ctrl)
2234{
2235 int ret;
2236
2237 if (!ctrl->effects)
2238 ctrl->effects = kzalloc(sizeof(*ctrl->effects), GFP_KERNEL);
2239
2240 if (!ctrl->effects)
2241 return 0;
2242
2243 ret = nvme_get_log(ctrl, NVME_LOG_CMD_EFFECTS, ctrl->effects,
2244 sizeof(*ctrl->effects));
2245 if (ret) {
2246 kfree(ctrl->effects);
2247 ctrl->effects = NULL;
2248 }
2249 return ret;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002250}
2251
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002252/*
2253 * Initialize the cached copies of the Identify data and various controller
2254 * register in our nvme_ctrl structure. This should be called as soon as
2255 * the admin queue is fully up and running.
2256 */
2257int nvme_init_identify(struct nvme_ctrl *ctrl)
2258{
2259 struct nvme_id_ctrl *id;
2260 u64 cap;
2261 int ret, page_shift;
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002262 u32 max_hw_sectors;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002263 bool prev_apst_enabled;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002264
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002265 ret = ctrl->ops->reg_read32(ctrl, NVME_REG_VS, &ctrl->vs);
2266 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002267 dev_err(ctrl->device, "Reading VS failed (%d)\n", ret);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002268 return ret;
2269 }
2270
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002271 ret = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &cap);
2272 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002273 dev_err(ctrl->device, "Reading CAP failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002274 return ret;
2275 }
2276 page_shift = NVME_CAP_MPSMIN(cap) + 12;
2277
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06002278 if (ctrl->vs >= NVME_VS(1, 1, 0))
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002279 ctrl->subsystem = NVME_CAP_NSSRC(cap);
2280
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002281 ret = nvme_identify_ctrl(ctrl, &id);
2282 if (ret) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002283 dev_err(ctrl->device, "Identify Controller failed (%d)\n", ret);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002284 return -EIO;
2285 }
2286
Keith Busch84fef622017-11-07 10:28:32 -07002287 if (id->lpa & NVME_CTRL_LPA_CMD_EFFECTS_LOG) {
2288 ret = nvme_get_effects_log(ctrl);
2289 if (ret < 0)
2290 return ret;
2291 }
Christoph Hellwig180de0072017-06-26 12:39:02 +02002292
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002293 if (!ctrl->identified) {
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002294 int i;
2295
2296 ret = nvme_init_subsystem(ctrl, id);
2297 if (ret)
2298 goto out_free;
2299
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002300 /*
2301 * Check for quirks. Quirk can depend on firmware version,
2302 * so, in principle, the set of quirks present can change
2303 * across a reset. As a possible future enhancement, we
2304 * could re-scan for quirks every time we reinitialize
2305 * the device, but we'd have to make sure that the driver
2306 * behaves intelligently if the quirks change.
2307 */
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002308 for (i = 0; i < ARRAY_SIZE(core_quirks); i++) {
2309 if (quirk_matches(id, &core_quirks[i]))
2310 ctrl->quirks |= core_quirks[i].quirks;
2311 }
2312 }
2313
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002314 if (force_apst && (ctrl->quirks & NVME_QUIRK_NO_DEEPEST_PS)) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002315 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 -07002316 ctrl->quirks &= ~NVME_QUIRK_NO_DEEPEST_PS;
2317 }
2318
Scott Bauer8a9ae522017-02-17 13:59:40 +01002319 ctrl->oacs = le16_to_cpu(id->oacs);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002320 ctrl->oncs = le16_to_cpup(&id->oncs);
Christoph Hellwig6bf25d12015-11-20 09:36:44 +01002321 atomic_set(&ctrl->abort_limit, id->acl + 1);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002322 ctrl->vwc = id->vwc;
Ming Lin931e1c22016-02-26 13:24:19 -08002323 ctrl->cntlid = le16_to_cpup(&id->cntlid);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002324 if (id->mdts)
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002325 max_hw_sectors = 1 << (id->mdts + page_shift - 9);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002326 else
Christoph Hellwiga229dbf2016-06-06 23:20:48 +02002327 max_hw_sectors = UINT_MAX;
2328 ctrl->max_hw_sectors =
2329 min_not_zero(ctrl->max_hw_sectors, max_hw_sectors);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002330
Christoph Hellwigda358252016-03-02 18:07:11 +01002331 nvme_set_queue_limits(ctrl, ctrl->admin_q);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002332 ctrl->sgls = le32_to_cpu(id->sgls);
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002333 ctrl->kas = le16_to_cpu(id->kas);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002334
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002335 if (id->rtd3e) {
2336 /* us -> s */
2337 u32 transition_time = le32_to_cpu(id->rtd3e) / 1000000;
2338
2339 ctrl->shutdown_timeout = clamp_t(unsigned int, transition_time,
2340 shutdown_timeout, 60);
2341
2342 if (ctrl->shutdown_timeout != shutdown_timeout)
Max Gurtovoy1a3838d2017-12-31 15:33:27 +02002343 dev_info(ctrl->device,
Martin K. Petersen07fbd322017-08-25 19:14:50 -04002344 "Shutdown timeout set to %u seconds\n",
2345 ctrl->shutdown_timeout);
2346 } else
2347 ctrl->shutdown_timeout = shutdown_timeout;
2348
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002349 ctrl->npss = id->npss;
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002350 ctrl->apsta = id->apsta;
2351 prev_apst_enabled = ctrl->apst_enabled;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002352 if (ctrl->quirks & NVME_QUIRK_NO_APST) {
2353 if (force_apst && id->apsta) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002354 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 -04002355 ctrl->apst_enabled = true;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002356 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002357 ctrl->apst_enabled = false;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002358 }
2359 } else {
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002360 ctrl->apst_enabled = id->apsta;
Andy Lutomirskic35e30b2017-04-21 16:19:24 -07002361 }
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002362 memcpy(ctrl->psd, id->psd, sizeof(ctrl->psd));
2363
Christoph Hellwigd3d5b872017-05-20 15:14:44 +02002364 if (ctrl->ops->flags & NVME_F_FABRICS) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002365 ctrl->icdoff = le16_to_cpu(id->icdoff);
2366 ctrl->ioccsz = le32_to_cpu(id->ioccsz);
2367 ctrl->iorcsz = le32_to_cpu(id->iorcsz);
2368 ctrl->maxcmd = le16_to_cpu(id->maxcmd);
2369
2370 /*
2371 * In fabrics we need to verify the cntlid matches the
2372 * admin connect
2373 */
Keith Busch634b8322017-08-10 11:23:31 +02002374 if (ctrl->cntlid != le16_to_cpu(id->cntlid)) {
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002375 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002376 goto out_free;
2377 }
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002378
2379 if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
Johannes Thumshirnf0425db2017-06-09 16:17:21 +02002380 dev_err(ctrl->device,
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002381 "keep-alive support is mandatory for fabrics\n");
2382 ret = -EINVAL;
Keith Busch634b8322017-08-10 11:23:31 +02002383 goto out_free;
Sagi Grimberg038bd4c2016-06-13 16:45:28 +02002384 }
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002385 } else {
2386 ctrl->cntlid = le16_to_cpu(id->cntlid);
Christoph Hellwigfe6d53c2017-05-12 17:16:10 +02002387 ctrl->hmpre = le32_to_cpu(id->hmpre);
2388 ctrl->hmmin = le32_to_cpu(id->hmmin);
Christoph Hellwig044a9df2017-09-11 12:09:28 -04002389 ctrl->hmminds = le32_to_cpu(id->hmminds);
2390 ctrl->hmmaxd = le16_to_cpu(id->hmmaxd);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002391 }
Christoph Hellwigda358252016-03-02 18:07:11 +01002392
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002393 kfree(id);
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002394
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002395 if (ctrl->apst_enabled && !prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002396 dev_pm_qos_expose_latency_tolerance(ctrl->device);
Kai-Heng Feng76a5af82017-06-26 16:39:54 -04002397 else if (!ctrl->apst_enabled && prev_apst_enabled)
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002398 dev_pm_qos_hide_latency_tolerance(ctrl->device);
2399
Keith Busch634b8322017-08-10 11:23:31 +02002400 ret = nvme_configure_apst(ctrl);
2401 if (ret < 0)
2402 return ret;
Jon Derrickdbf86b32017-08-16 09:51:29 +02002403
2404 ret = nvme_configure_timestamp(ctrl);
2405 if (ret < 0)
2406 return ret;
Keith Busch634b8322017-08-10 11:23:31 +02002407
2408 ret = nvme_configure_directives(ctrl);
2409 if (ret < 0)
2410 return ret;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002411
Andy Lutomirskibd4da3a2017-02-22 13:32:36 -07002412 ctrl->identified = true;
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08002413
Keith Busch634b8322017-08-10 11:23:31 +02002414 return 0;
2415
2416out_free:
2417 kfree(id);
Christoph Hellwig07bfcd02016-06-13 16:45:26 +02002418 return ret;
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002419}
Ming Lin576d55d2016-02-10 10:03:32 -08002420EXPORT_SYMBOL_GPL(nvme_init_identify);
Christoph Hellwig7fd89302015-11-28 15:37:52 +01002421
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002422static int nvme_dev_open(struct inode *inode, struct file *file)
Christoph Hellwig21d34712015-11-26 09:08:36 +01002423{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002424 struct nvme_ctrl *ctrl =
2425 container_of(inode->i_cdev, struct nvme_ctrl, cdev);
Christoph Hellwig21d34712015-11-26 09:08:36 +01002426
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002427 switch (ctrl->state) {
2428 case NVME_CTRL_LIVE:
2429 case NVME_CTRL_ADMIN_ONLY:
2430 break;
2431 default:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002432 return -EWOULDBLOCK;
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002433 }
2434
Christoph Hellwiga6a51492017-10-18 16:59:25 +02002435 file->private_data = ctrl;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002436 return 0;
Christoph Hellwig21d34712015-11-26 09:08:36 +01002437}
2438
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002439static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
2440{
2441 struct nvme_ns *ns;
2442 int ret;
2443
2444 mutex_lock(&ctrl->namespaces_mutex);
2445 if (list_empty(&ctrl->namespaces)) {
2446 ret = -ENOTTY;
2447 goto out_unlock;
2448 }
2449
2450 ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
2451 if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002452 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002453 "NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
2454 ret = -EINVAL;
2455 goto out_unlock;
2456 }
2457
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002458 dev_warn(ctrl->device,
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002459 "using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
2460 kref_get(&ns->kref);
2461 mutex_unlock(&ctrl->namespaces_mutex);
2462
2463 ret = nvme_user_cmd(ctrl, ns, argp);
2464 nvme_put_ns(ns);
2465 return ret;
2466
2467out_unlock:
2468 mutex_unlock(&ctrl->namespaces_mutex);
2469 return ret;
2470}
2471
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002472static long nvme_dev_ioctl(struct file *file, unsigned int cmd,
2473 unsigned long arg)
2474{
2475 struct nvme_ctrl *ctrl = file->private_data;
2476 void __user *argp = (void __user *)arg;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002477
2478 switch (cmd) {
2479 case NVME_IOCTL_ADMIN_CMD:
2480 return nvme_user_cmd(ctrl, NULL, argp);
2481 case NVME_IOCTL_IO_CMD:
Christoph Hellwigbfd89472015-12-24 15:27:01 +01002482 return nvme_dev_user_cmd(ctrl, argp);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002483 case NVME_IOCTL_RESET:
Sagi Grimberg1b3c47c2016-02-10 08:51:15 -07002484 dev_warn(ctrl->device, "resetting controller\n");
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002485 return nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002486 case NVME_IOCTL_SUBSYS_RESET:
2487 return nvme_reset_subsystem(ctrl);
Keith Busch9ec3bb22016-04-29 15:45:18 -06002488 case NVME_IOCTL_RESCAN:
2489 nvme_queue_scan(ctrl);
2490 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002491 default:
2492 return -ENOTTY;
2493 }
2494}
2495
2496static const struct file_operations nvme_dev_fops = {
2497 .owner = THIS_MODULE,
2498 .open = nvme_dev_open,
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002499 .unlocked_ioctl = nvme_dev_ioctl,
2500 .compat_ioctl = nvme_dev_ioctl,
2501};
2502
2503static ssize_t nvme_sysfs_reset(struct device *dev,
2504 struct device_attribute *attr, const char *buf,
2505 size_t count)
2506{
2507 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2508 int ret;
2509
Christoph Hellwigd86c4d82017-06-15 15:41:08 +02002510 ret = nvme_reset_ctrl_sync(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01002511 if (ret < 0)
2512 return ret;
2513 return count;
2514}
2515static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
2516
Keith Busch9ec3bb22016-04-29 15:45:18 -06002517static ssize_t nvme_sysfs_rescan(struct device *dev,
2518 struct device_attribute *attr, const char *buf,
2519 size_t count)
2520{
2521 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2522
2523 nvme_queue_scan(ctrl);
2524 return count;
2525}
2526static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
2527
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002528static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
Keith Busch118472a2016-02-18 09:57:48 -07002529{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002530 struct gendisk *disk = dev_to_disk(dev);
Keith Busch118472a2016-02-18 09:57:48 -07002531
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002532 if (disk->fops == &nvme_fops)
2533 return nvme_get_ns_from_dev(dev)->head;
2534 else
2535 return disk->private_data;
2536}
Johannes Thumshirn6484f5d2017-07-12 15:38:56 +02002537
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002538static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
2539 char *buf)
2540{
2541 struct nvme_ns_head *head = dev_to_ns_head(dev);
2542 struct nvme_ns_ids *ids = &head->ids;
2543 struct nvme_subsystem *subsys = head->subsys;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002544 int serial_len = sizeof(subsys->serial);
2545 int model_len = sizeof(subsys->model);
Keith Busch118472a2016-02-18 09:57:48 -07002546
Christoph Hellwig002fab02017-11-09 13:50:16 +01002547 if (!uuid_is_null(&ids->uuid))
2548 return sprintf(buf, "uuid.%pU\n", &ids->uuid);
Keith Busch118472a2016-02-18 09:57:48 -07002549
Christoph Hellwig002fab02017-11-09 13:50:16 +01002550 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
2551 return sprintf(buf, "eui.%16phN\n", ids->nguid);
Keith Busch118472a2016-02-18 09:57:48 -07002552
Christoph Hellwig002fab02017-11-09 13:50:16 +01002553 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
2554 return sprintf(buf, "eui.%8phN\n", ids->eui64);
Keith Busch118472a2016-02-18 09:57:48 -07002555
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002556 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
2557 subsys->serial[serial_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002558 serial_len--;
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002559 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
2560 subsys->model[model_len - 1] == '\0'))
Keith Busch118472a2016-02-18 09:57:48 -07002561 model_len--;
2562
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002563 return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
2564 serial_len, subsys->serial, model_len, subsys->model,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002565 head->ns_id);
Keith Busch118472a2016-02-18 09:57:48 -07002566}
Joe Perchesc828a892017-12-19 10:15:08 -08002567static DEVICE_ATTR_RO(wwid);
Keith Busch118472a2016-02-18 09:57:48 -07002568
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002569static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002570 char *buf)
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002571{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002572 return sprintf(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002573}
Joe Perchesc828a892017-12-19 10:15:08 -08002574static DEVICE_ATTR_RO(nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002575
Keith Busch2b9b6e82015-12-22 10:10:45 -07002576static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002577 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002578{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002579 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002580
2581 /* For backward compatibility expose the NGUID to userspace if
2582 * we have no UUID set
2583 */
Christoph Hellwig002fab02017-11-09 13:50:16 +01002584 if (uuid_is_null(&ids->uuid)) {
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002585 printk_ratelimited(KERN_WARNING
2586 "No UUID available providing old NGUID\n");
Christoph Hellwig002fab02017-11-09 13:50:16 +01002587 return sprintf(buf, "%pU\n", ids->nguid);
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002588 }
Christoph Hellwig002fab02017-11-09 13:50:16 +01002589 return sprintf(buf, "%pU\n", &ids->uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002590}
Joe Perchesc828a892017-12-19 10:15:08 -08002591static DEVICE_ATTR_RO(uuid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002592
2593static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002594 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002595{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002596 return sprintf(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002597}
Joe Perchesc828a892017-12-19 10:15:08 -08002598static DEVICE_ATTR_RO(eui);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002599
2600static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002601 char *buf)
Keith Busch2b9b6e82015-12-22 10:10:45 -07002602{
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002603 return sprintf(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002604}
Joe Perchesc828a892017-12-19 10:15:08 -08002605static DEVICE_ATTR_RO(nsid);
Keith Busch2b9b6e82015-12-22 10:10:45 -07002606
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002607static struct attribute *nvme_ns_id_attrs[] = {
Keith Busch118472a2016-02-18 09:57:48 -07002608 &dev_attr_wwid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002609 &dev_attr_uuid.attr,
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002610 &dev_attr_nguid.attr,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002611 &dev_attr_eui.attr,
2612 &dev_attr_nsid.attr,
2613 NULL,
2614};
2615
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002616static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002617 struct attribute *a, int n)
2618{
2619 struct device *dev = container_of(kobj, struct device, kobj);
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002620 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
Keith Busch2b9b6e82015-12-22 10:10:45 -07002621
2622 if (a == &dev_attr_uuid.attr) {
Martin Wilcka04b5de2017-09-28 21:33:23 +02002623 if (uuid_is_null(&ids->uuid) &&
Christoph Hellwig002fab02017-11-09 13:50:16 +01002624 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Johannes Thumshirnd934f982017-06-07 11:45:35 +02002625 return 0;
2626 }
2627 if (a == &dev_attr_nguid.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002628 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002629 return 0;
2630 }
2631 if (a == &dev_attr_eui.attr) {
Christoph Hellwig002fab02017-11-09 13:50:16 +01002632 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
Keith Busch2b9b6e82015-12-22 10:10:45 -07002633 return 0;
2634 }
2635 return a->mode;
2636}
2637
Christoph Hellwig5b85b822017-11-09 13:51:03 +01002638const struct attribute_group nvme_ns_id_attr_group = {
2639 .attrs = nvme_ns_id_attrs,
2640 .is_visible = nvme_ns_id_attrs_are_visible,
Keith Busch2b9b6e82015-12-22 10:10:45 -07002641};
2642
Ming Lin931e1c22016-02-26 13:24:19 -08002643#define nvme_show_str_function(field) \
Keith Busch779ff7562016-01-12 15:09:31 -07002644static ssize_t field##_show(struct device *dev, \
2645 struct device_attribute *attr, char *buf) \
2646{ \
2647 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002648 return sprintf(buf, "%.*s\n", \
2649 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
Keith Busch779ff7562016-01-12 15:09:31 -07002650} \
2651static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2652
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002653nvme_show_str_function(model);
2654nvme_show_str_function(serial);
2655nvme_show_str_function(firmware_rev);
2656
Ming Lin931e1c22016-02-26 13:24:19 -08002657#define nvme_show_int_function(field) \
2658static ssize_t field##_show(struct device *dev, \
2659 struct device_attribute *attr, char *buf) \
2660{ \
2661 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
2662 return sprintf(buf, "%d\n", ctrl->field); \
2663} \
2664static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
2665
Ming Lin931e1c22016-02-26 13:24:19 -08002666nvme_show_int_function(cntlid);
Keith Busch779ff7562016-01-12 15:09:31 -07002667
Ming Lin1a353d82016-06-13 16:45:24 +02002668static ssize_t nvme_sysfs_delete(struct device *dev,
2669 struct device_attribute *attr, const char *buf,
2670 size_t count)
2671{
2672 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2673
2674 if (device_remove_file_self(dev, attr))
Christoph Hellwigc5017e82017-10-29 10:44:29 +02002675 nvme_delete_ctrl_sync(ctrl);
Ming Lin1a353d82016-06-13 16:45:24 +02002676 return count;
2677}
2678static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
2679
2680static ssize_t nvme_sysfs_show_transport(struct device *dev,
2681 struct device_attribute *attr,
2682 char *buf)
2683{
2684 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2685
2686 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
2687}
2688static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
2689
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002690static ssize_t nvme_sysfs_show_state(struct device *dev,
2691 struct device_attribute *attr,
2692 char *buf)
2693{
2694 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2695 static const char *const state_name[] = {
2696 [NVME_CTRL_NEW] = "new",
2697 [NVME_CTRL_LIVE] = "live",
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08002698 [NVME_CTRL_ADMIN_ONLY] = "only-admin",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002699 [NVME_CTRL_RESETTING] = "resetting",
Max Gurtovoyad6a0a52018-01-31 18:31:24 +02002700 [NVME_CTRL_CONNECTING] = "connecting",
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002701 [NVME_CTRL_DELETING] = "deleting",
2702 [NVME_CTRL_DEAD] = "dead",
2703 };
2704
2705 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
2706 state_name[ctrl->state])
2707 return sprintf(buf, "%s\n", state_name[ctrl->state]);
2708
2709 return sprintf(buf, "unknown state\n");
2710}
2711
2712static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
2713
Ming Lin1a353d82016-06-13 16:45:24 +02002714static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
2715 struct device_attribute *attr,
2716 char *buf)
2717{
2718 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2719
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01002720 return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->subsys->subnqn);
Ming Lin1a353d82016-06-13 16:45:24 +02002721}
2722static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
2723
2724static ssize_t nvme_sysfs_show_address(struct device *dev,
2725 struct device_attribute *attr,
2726 char *buf)
2727{
2728 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2729
2730 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
2731}
2732static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
2733
Keith Busch779ff7562016-01-12 15:09:31 -07002734static struct attribute *nvme_dev_attrs[] = {
2735 &dev_attr_reset_controller.attr,
Keith Busch9ec3bb22016-04-29 15:45:18 -06002736 &dev_attr_rescan_controller.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002737 &dev_attr_model.attr,
2738 &dev_attr_serial.attr,
2739 &dev_attr_firmware_rev.attr,
Ming Lin931e1c22016-02-26 13:24:19 -08002740 &dev_attr_cntlid.attr,
Ming Lin1a353d82016-06-13 16:45:24 +02002741 &dev_attr_delete_controller.attr,
2742 &dev_attr_transport.attr,
2743 &dev_attr_subsysnqn.attr,
2744 &dev_attr_address.attr,
Sagi Grimberg8432bdb22016-11-28 01:47:40 +02002745 &dev_attr_state.attr,
Keith Busch779ff7562016-01-12 15:09:31 -07002746 NULL
2747};
2748
Ming Lin1a353d82016-06-13 16:45:24 +02002749static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
2750 struct attribute *a, int n)
2751{
2752 struct device *dev = container_of(kobj, struct device, kobj);
2753 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
2754
Christoph Hellwig49d3d502017-06-26 12:39:03 +02002755 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
2756 return 0;
2757 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
2758 return 0;
Ming Lin1a353d82016-06-13 16:45:24 +02002759
2760 return a->mode;
2761}
2762
Keith Busch779ff7562016-01-12 15:09:31 -07002763static struct attribute_group nvme_dev_attrs_group = {
Ming Lin1a353d82016-06-13 16:45:24 +02002764 .attrs = nvme_dev_attrs,
2765 .is_visible = nvme_dev_attrs_are_visible,
Keith Busch779ff7562016-01-12 15:09:31 -07002766};
2767
2768static const struct attribute_group *nvme_dev_attr_groups[] = {
2769 &nvme_dev_attrs_group,
2770 NULL,
2771};
2772
Christoph Hellwiged754e52017-11-09 13:50:43 +01002773static struct nvme_ns_head *__nvme_find_ns_head(struct nvme_subsystem *subsys,
2774 unsigned nsid)
2775{
2776 struct nvme_ns_head *h;
2777
2778 lockdep_assert_held(&subsys->lock);
2779
2780 list_for_each_entry(h, &subsys->nsheads, entry) {
2781 if (h->ns_id == nsid && kref_get_unless_zero(&h->ref))
2782 return h;
2783 }
2784
2785 return NULL;
2786}
2787
2788static int __nvme_check_ids(struct nvme_subsystem *subsys,
2789 struct nvme_ns_head *new)
2790{
2791 struct nvme_ns_head *h;
2792
2793 lockdep_assert_held(&subsys->lock);
2794
2795 list_for_each_entry(h, &subsys->nsheads, entry) {
2796 if (nvme_ns_ids_valid(&new->ids) &&
2797 nvme_ns_ids_equal(&new->ids, &h->ids))
2798 return -EINVAL;
2799 }
2800
2801 return 0;
2802}
2803
2804static struct nvme_ns_head *nvme_alloc_ns_head(struct nvme_ctrl *ctrl,
2805 unsigned nsid, struct nvme_id_ns *id)
2806{
2807 struct nvme_ns_head *head;
2808 int ret = -ENOMEM;
2809
2810 head = kzalloc(sizeof(*head), GFP_KERNEL);
2811 if (!head)
2812 goto out;
2813 ret = ida_simple_get(&ctrl->subsys->ns_ida, 1, 0, GFP_KERNEL);
2814 if (ret < 0)
2815 goto out_free_head;
2816 head->instance = ret;
2817 INIT_LIST_HEAD(&head->list);
2818 init_srcu_struct(&head->srcu);
2819 head->subsys = ctrl->subsys;
2820 head->ns_id = nsid;
2821 kref_init(&head->ref);
2822
2823 nvme_report_ns_ids(ctrl, nsid, id, &head->ids);
2824
2825 ret = __nvme_check_ids(ctrl->subsys, head);
2826 if (ret) {
2827 dev_err(ctrl->device,
2828 "duplicate IDs for nsid %d\n", nsid);
2829 goto out_cleanup_srcu;
2830 }
2831
Christoph Hellwig32acab32017-11-02 12:59:30 +01002832 ret = nvme_mpath_alloc_disk(ctrl, head);
2833 if (ret)
2834 goto out_cleanup_srcu;
2835
Christoph Hellwiged754e52017-11-09 13:50:43 +01002836 list_add_tail(&head->entry, &ctrl->subsys->nsheads);
2837 return head;
2838out_cleanup_srcu:
2839 cleanup_srcu_struct(&head->srcu);
2840 ida_simple_remove(&ctrl->subsys->ns_ida, head->instance);
2841out_free_head:
2842 kfree(head);
2843out:
2844 return ERR_PTR(ret);
2845}
2846
2847static int nvme_init_ns_head(struct nvme_ns *ns, unsigned nsid,
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002848 struct nvme_id_ns *id)
Christoph Hellwiged754e52017-11-09 13:50:43 +01002849{
2850 struct nvme_ctrl *ctrl = ns->ctrl;
2851 bool is_shared = id->nmic & (1 << 0);
2852 struct nvme_ns_head *head = NULL;
2853 int ret = 0;
2854
2855 mutex_lock(&ctrl->subsys->lock);
2856 if (is_shared)
2857 head = __nvme_find_ns_head(ctrl->subsys, nsid);
2858 if (!head) {
2859 head = nvme_alloc_ns_head(ctrl, nsid, id);
2860 if (IS_ERR(head)) {
2861 ret = PTR_ERR(head);
2862 goto out_unlock;
2863 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002864 } else {
2865 struct nvme_ns_ids ids;
2866
2867 nvme_report_ns_ids(ctrl, nsid, id, &ids);
2868 if (!nvme_ns_ids_equal(&head->ids, &ids)) {
2869 dev_err(ctrl->device,
2870 "IDs don't match for shared namespace %d\n",
2871 nsid);
2872 ret = -EINVAL;
2873 goto out_unlock;
2874 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002875 }
2876
2877 list_add_tail(&ns->siblings, &head->list);
2878 ns->head = head;
2879
2880out_unlock:
2881 mutex_unlock(&ctrl->subsys->lock);
2882 return ret;
2883}
2884
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002885static int ns_cmp(void *priv, struct list_head *a, struct list_head *b)
2886{
2887 struct nvme_ns *nsa = container_of(a, struct nvme_ns, list);
2888 struct nvme_ns *nsb = container_of(b, struct nvme_ns, list);
2889
Christoph Hellwiged754e52017-11-09 13:50:43 +01002890 return nsa->head->ns_id - nsb->head->ns_id;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002891}
2892
Keith Busch32f0c4a2016-07-13 11:45:02 -06002893static struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002894{
Keith Busch32f0c4a2016-07-13 11:45:02 -06002895 struct nvme_ns *ns, *ret = NULL;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002896
Keith Busch32f0c4a2016-07-13 11:45:02 -06002897 mutex_lock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002898 list_for_each_entry(ns, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01002899 if (ns->head->ns_id == nsid) {
Christoph Hellwig2dd41222017-10-18 13:20:01 +02002900 if (!kref_get_unless_zero(&ns->kref))
2901 continue;
Keith Busch32f0c4a2016-07-13 11:45:02 -06002902 ret = ns;
2903 break;
2904 }
Christoph Hellwiged754e52017-11-09 13:50:43 +01002905 if (ns->head->ns_id > nsid)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002906 break;
2907 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06002908 mutex_unlock(&ctrl->namespaces_mutex);
2909 return ret;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002910}
2911
Jens Axboef5d11842017-06-27 12:03:06 -06002912static int nvme_setup_streams_ns(struct nvme_ctrl *ctrl, struct nvme_ns *ns)
2913{
2914 struct streams_directive_params s;
2915 int ret;
2916
2917 if (!ctrl->nr_streams)
2918 return 0;
2919
Christoph Hellwiged754e52017-11-09 13:50:43 +01002920 ret = nvme_get_stream_params(ctrl, &s, ns->head->ns_id);
Jens Axboef5d11842017-06-27 12:03:06 -06002921 if (ret)
2922 return ret;
2923
2924 ns->sws = le32_to_cpu(s.sws);
2925 ns->sgs = le16_to_cpu(s.sgs);
2926
2927 if (ns->sws) {
2928 unsigned int bs = 1 << ns->lba_shift;
2929
2930 blk_queue_io_min(ns->queue, bs * ns->sws);
2931 if (ns->sgs)
2932 blk_queue_io_opt(ns->queue, bs * ns->sws * ns->sgs);
2933 }
2934
2935 return 0;
2936}
2937
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002938static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
2939{
2940 struct nvme_ns *ns;
2941 struct gendisk *disk;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002942 struct nvme_id_ns *id;
2943 char disk_name[DISK_NAME_LEN];
Christoph Hellwig32acab32017-11-02 12:59:30 +01002944 int node = dev_to_node(ctrl->dev), flags = GENHD_FL_EXT_DEVT;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002945
2946 ns = kzalloc_node(sizeof(*ns), GFP_KERNEL, node);
2947 if (!ns)
2948 return;
2949
2950 ns->queue = blk_mq_init_queue(ctrl->tagset);
2951 if (IS_ERR(ns->queue))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002952 goto out_free_ns;
Bart Van Assche8b904b52018-03-07 17:10:10 -08002953 blk_queue_flag_set(QUEUE_FLAG_NONROT, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002954 ns->queue->queuedata = ns;
2955 ns->ctrl = ctrl;
2956
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002957 kref_init(&ns->kref);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002958 ns->lba_shift = 9; /* set to a default value for 512 until disk is validated */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002959
2960 blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
Christoph Hellwigda358252016-03-02 18:07:11 +01002961 nvme_set_queue_limits(ctrl, ns->queue);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01002962
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002963 id = nvme_identify_ns(ctrl, nsid);
2964 if (!id)
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02002965 goto out_free_queue;
2966
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002967 if (id->ncap == 0)
2968 goto out_free_id;
2969
Baegjae Sung9bd82b12018-02-28 16:06:04 +09002970 if (nvme_init_ns_head(ns, nsid, id))
Christoph Hellwiged754e52017-11-09 13:50:43 +01002971 goto out_free_id;
Keith Busch654b4a42017-12-14 11:20:32 -07002972 nvme_setup_streams_ns(ctrl, ns);
Christoph Hellwiged754e52017-11-09 13:50:43 +01002973
Christoph Hellwig32acab32017-11-02 12:59:30 +01002974#ifdef CONFIG_NVME_MULTIPATH
2975 /*
2976 * If multipathing is enabled we need to always use the subsystem
2977 * instance number for numbering our devices to avoid conflicts
2978 * between subsystems that have multiple controllers and thus use
2979 * the multipath-aware subsystem node and those that have a single
2980 * controller and use the controller node directly.
2981 */
2982 if (ns->head->disk) {
2983 sprintf(disk_name, "nvme%dc%dn%d", ctrl->subsys->instance,
2984 ctrl->cntlid, ns->head->instance);
2985 flags = GENHD_FL_HIDDEN;
2986 } else {
2987 sprintf(disk_name, "nvme%dn%d", ctrl->subsys->instance,
2988 ns->head->instance);
2989 }
2990#else
2991 /*
2992 * But without the multipath code enabled, multiple controller per
2993 * subsystems are visible as devices and thus we cannot use the
2994 * subsystem instance.
2995 */
Christoph Hellwiged754e52017-11-09 13:50:43 +01002996 sprintf(disk_name, "nvme%dn%d", ctrl->instance, ns->head->instance);
Christoph Hellwig32acab32017-11-02 12:59:30 +01002997#endif
Christoph Hellwigcdbff4f2017-08-16 16:14:47 +02002998
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02002999 if ((ctrl->quirks & NVME_QUIRK_LIGHTNVM) && id->vs[0] == 0x1) {
3000 if (nvme_nvm_register(ns, disk_name, node)) {
3001 dev_warn(ctrl->device, "LightNVM init failure\n");
Christoph Hellwiged754e52017-11-09 13:50:43 +01003002 goto out_unlink_ns;
Christoph Hellwig608cc4b2017-09-06 11:45:24 +02003003 }
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003004 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003005
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003006 disk = alloc_disk_node(0, node);
3007 if (!disk)
Christoph Hellwiged754e52017-11-09 13:50:43 +01003008 goto out_unlink_ns;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003009
3010 disk->fops = &nvme_fops;
3011 disk->private_data = ns;
3012 disk->queue = ns->queue;
Christoph Hellwig32acab32017-11-02 12:59:30 +01003013 disk->flags = flags;
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003014 memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
3015 ns->disk = disk;
3016
3017 __nvme_revalidate_disk(disk, id);
3018
Keith Busch32f0c4a2016-07-13 11:45:02 -06003019 mutex_lock(&ctrl->namespaces_mutex);
3020 list_add_tail(&ns->list, &ctrl->namespaces);
3021 mutex_unlock(&ctrl->namespaces_mutex);
3022
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003023 nvme_get_ctrl(ctrl);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003024
3025 kfree(id);
3026
Dan Williams0d52c7562016-06-15 19:44:20 -07003027 device_add_disk(ctrl->device, ns->disk);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003028 if (sysfs_create_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003029 &nvme_ns_id_attr_group))
Keith Busch2b9b6e82015-12-22 10:10:45 -07003030 pr_warn("%s: failed to create sysfs group for identification\n",
3031 ns->disk->disk_name);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003032 if (ns->ndev && nvme_nvm_register_sysfs(ns))
3033 pr_warn("%s: failed to register lightnvm sysfs group for identification\n",
3034 ns->disk->disk_name);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003035
Baegjae Sung9bd82b12018-02-28 16:06:04 +09003036 nvme_mpath_add_disk(ns->head);
Hannes Reineckee9a48032017-11-09 17:57:06 +01003037 nvme_mpath_add_disk_links(ns);
Thomas Taib9e03852018-02-08 13:38:29 -05003038 nvme_fault_inject_init(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003039 return;
Christoph Hellwiged754e52017-11-09 13:50:43 +01003040 out_unlink_ns:
3041 mutex_lock(&ctrl->subsys->lock);
3042 list_del_rcu(&ns->siblings);
3043 mutex_unlock(&ctrl->subsys->lock);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02003044 out_free_id:
3045 kfree(id);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003046 out_free_queue:
3047 blk_cleanup_queue(ns->queue);
3048 out_free_ns:
3049 kfree(ns);
3050}
3051
3052static void nvme_ns_remove(struct nvme_ns *ns)
3053{
Keith Busch646017a2016-02-24 09:15:54 -07003054 if (test_and_set_bit(NVME_NS_REMOVING, &ns->flags))
3055 return;
3056
Thomas Taib9e03852018-02-08 13:38:29 -05003057 nvme_fault_inject_fini(ns);
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003058 if (ns->disk && ns->disk->flags & GENHD_FL_UP) {
Hannes Reineckee9a48032017-11-09 17:57:06 +01003059 nvme_mpath_remove_disk_links(ns);
Keith Busch2b9b6e82015-12-22 10:10:45 -07003060 sysfs_remove_group(&disk_to_dev(ns->disk)->kobj,
Christoph Hellwig5b85b822017-11-09 13:51:03 +01003061 &nvme_ns_id_attr_group);
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01003062 if (ns->ndev)
3063 nvme_nvm_unregister_sysfs(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003064 del_gendisk(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003065 blk_cleanup_queue(ns->queue);
Ming Leibd9f5d62017-12-06 18:30:09 +08003066 if (blk_get_integrity(ns->disk))
3067 blk_integrity_unregister(ns->disk);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003068 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003069
Christoph Hellwiged754e52017-11-09 13:50:43 +01003070 mutex_lock(&ns->ctrl->subsys->lock);
Christoph Hellwig32acab32017-11-02 12:59:30 +01003071 nvme_mpath_clear_current_path(ns);
Keith Busch9941a862017-11-16 13:36:50 -07003072 list_del_rcu(&ns->siblings);
Christoph Hellwiged754e52017-11-09 13:50:43 +01003073 mutex_unlock(&ns->ctrl->subsys->lock);
3074
Keith Busch32f0c4a2016-07-13 11:45:02 -06003075 mutex_lock(&ns->ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003076 list_del_init(&ns->list);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003077 mutex_unlock(&ns->ctrl->namespaces_mutex);
3078
Keith Busch9941a862017-11-16 13:36:50 -07003079 synchronize_srcu(&ns->head->srcu);
Sagi Grimberg479a3222017-12-21 15:07:27 +02003080 nvme_mpath_check_last_path(ns);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003081 nvme_put_ns(ns);
3082}
3083
Keith Busch540c8012015-10-22 15:45:06 -06003084static void nvme_validate_ns(struct nvme_ctrl *ctrl, unsigned nsid)
3085{
3086 struct nvme_ns *ns;
3087
Keith Busch32f0c4a2016-07-13 11:45:02 -06003088 ns = nvme_find_get_ns(ctrl, nsid);
Keith Busch540c8012015-10-22 15:45:06 -06003089 if (ns) {
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02003090 if (ns->disk && revalidate_disk(ns->disk))
Keith Busch540c8012015-10-22 15:45:06 -06003091 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003092 nvme_put_ns(ns);
Keith Busch540c8012015-10-22 15:45:06 -06003093 } else
3094 nvme_alloc_ns(ctrl, nsid);
3095}
3096
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303097static void nvme_remove_invalid_namespaces(struct nvme_ctrl *ctrl,
3098 unsigned nsid)
3099{
3100 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003101 LIST_HEAD(rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303102
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003103 mutex_lock(&ctrl->namespaces_mutex);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303104 list_for_each_entry_safe(ns, next, &ctrl->namespaces, list) {
Christoph Hellwiged754e52017-11-09 13:50:43 +01003105 if (ns->head->ns_id > nsid)
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003106 list_move_tail(&ns->list, &rm_list);
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303107 }
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003108 mutex_unlock(&ctrl->namespaces_mutex);
3109
3110 list_for_each_entry_safe(ns, next, &rm_list, list)
3111 nvme_ns_remove(ns);
3112
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303113}
3114
Keith Busch540c8012015-10-22 15:45:06 -06003115static int nvme_scan_ns_list(struct nvme_ctrl *ctrl, unsigned nn)
3116{
3117 struct nvme_ns *ns;
3118 __le32 *ns_list;
3119 unsigned i, j, nsid, prev = 0, num_lists = DIV_ROUND_UP(nn, 1024);
3120 int ret = 0;
3121
Minwoo Im42595eb2018-02-08 22:56:31 +09003122 ns_list = kzalloc(NVME_IDENTIFY_DATA_SIZE, GFP_KERNEL);
Keith Busch540c8012015-10-22 15:45:06 -06003123 if (!ns_list)
3124 return -ENOMEM;
3125
3126 for (i = 0; i < num_lists; i++) {
3127 ret = nvme_identify_ns_list(ctrl, prev, ns_list);
3128 if (ret)
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303129 goto free;
Keith Busch540c8012015-10-22 15:45:06 -06003130
3131 for (j = 0; j < min(nn, 1024U); j++) {
3132 nsid = le32_to_cpu(ns_list[j]);
3133 if (!nsid)
3134 goto out;
3135
3136 nvme_validate_ns(ctrl, nsid);
3137
3138 while (++prev < nsid) {
Keith Busch32f0c4a2016-07-13 11:45:02 -06003139 ns = nvme_find_get_ns(ctrl, prev);
3140 if (ns) {
Keith Busch540c8012015-10-22 15:45:06 -06003141 nvme_ns_remove(ns);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003142 nvme_put_ns(ns);
3143 }
Keith Busch540c8012015-10-22 15:45:06 -06003144 }
3145 }
3146 nn -= j;
3147 }
3148 out:
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303149 nvme_remove_invalid_namespaces(ctrl, prev);
3150 free:
Keith Busch540c8012015-10-22 15:45:06 -06003151 kfree(ns_list);
3152 return ret;
3153}
3154
Christoph Hellwig5955be22016-04-26 13:51:59 +02003155static void nvme_scan_ns_sequential(struct nvme_ctrl *ctrl, unsigned nn)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003156{
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003157 unsigned i;
3158
Keith Busch540c8012015-10-22 15:45:06 -06003159 for (i = 1; i <= nn; i++)
3160 nvme_validate_ns(ctrl, i);
3161
Sunad Bhandary47b0e502016-05-27 15:59:43 +05303162 nvme_remove_invalid_namespaces(ctrl, nn);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003163}
3164
Christoph Hellwig5955be22016-04-26 13:51:59 +02003165static void nvme_scan_work(struct work_struct *work)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003166{
Christoph Hellwig5955be22016-04-26 13:51:59 +02003167 struct nvme_ctrl *ctrl =
3168 container_of(work, struct nvme_ctrl, scan_work);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003169 struct nvme_id_ctrl *id;
Keith Busch540c8012015-10-22 15:45:06 -06003170 unsigned nn;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003171
Christoph Hellwig5955be22016-04-26 13:51:59 +02003172 if (ctrl->state != NVME_CTRL_LIVE)
3173 return;
3174
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003175 WARN_ON_ONCE(!ctrl->tagset);
3176
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003177 if (nvme_identify_ctrl(ctrl, &id))
3178 return;
Keith Busch540c8012015-10-22 15:45:06 -06003179
3180 nn = le32_to_cpu(id->nn);
Gabriel Krisman Bertazi8ef20742016-10-19 09:51:05 -06003181 if (ctrl->vs >= NVME_VS(1, 1, 0) &&
Keith Busch540c8012015-10-22 15:45:06 -06003182 !(ctrl->quirks & NVME_QUIRK_IDENTIFY_CNS)) {
3183 if (!nvme_scan_ns_list(ctrl, nn))
3184 goto done;
3185 }
Christoph Hellwig5955be22016-04-26 13:51:59 +02003186 nvme_scan_ns_sequential(ctrl, nn);
Keith Busch540c8012015-10-22 15:45:06 -06003187 done:
Keith Busch32f0c4a2016-07-13 11:45:02 -06003188 mutex_lock(&ctrl->namespaces_mutex);
Keith Busch540c8012015-10-22 15:45:06 -06003189 list_sort(NULL, &ctrl->namespaces, ns_cmp);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01003190 mutex_unlock(&ctrl->namespaces_mutex);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003191 kfree(id);
3192}
Christoph Hellwig5955be22016-04-26 13:51:59 +02003193
3194void nvme_queue_scan(struct nvme_ctrl *ctrl)
3195{
3196 /*
Jianchao Wang2b1b7e72018-01-06 08:01:58 +08003197 * Only new queue scan work when admin and IO queues are both alive
Christoph Hellwig5955be22016-04-26 13:51:59 +02003198 */
3199 if (ctrl->state == NVME_CTRL_LIVE)
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003200 queue_work(nvme_wq, &ctrl->scan_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003201}
3202EXPORT_SYMBOL_GPL(nvme_queue_scan);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003203
Keith Busch32f0c4a2016-07-13 11:45:02 -06003204/*
3205 * This function iterates the namespace list unlocked to allow recovery from
3206 * controller failure. It is up to the caller to ensure the namespace list is
3207 * not modified by scan work while this function is executing.
3208 */
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003209void nvme_remove_namespaces(struct nvme_ctrl *ctrl)
3210{
3211 struct nvme_ns *ns, *next;
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003212 LIST_HEAD(ns_list);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003213
Keith Busch0ff9d4e2016-05-12 08:37:14 -06003214 /*
3215 * The dead states indicates the controller was not gracefully
3216 * disconnected. In that case, we won't be able to flush any data while
3217 * removing the namespaces' disks; fail all the queues now to avoid
3218 * potentially having to clean up the failed sync later.
3219 */
3220 if (ctrl->state == NVME_CTRL_DEAD)
3221 nvme_kill_queues(ctrl);
3222
Jianchao Wang6f8e0d72018-02-12 20:54:44 +08003223 mutex_lock(&ctrl->namespaces_mutex);
3224 list_splice_init(&ctrl->namespaces, &ns_list);
3225 mutex_unlock(&ctrl->namespaces_mutex);
3226
3227 list_for_each_entry_safe(ns, next, &ns_list, list)
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003228 nvme_ns_remove(ns);
3229}
Ming Lin576d55d2016-02-10 10:03:32 -08003230EXPORT_SYMBOL_GPL(nvme_remove_namespaces);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003231
Keith Busche3d78742017-11-07 15:13:14 -07003232static void nvme_aen_uevent(struct nvme_ctrl *ctrl)
3233{
3234 char *envp[2] = { NULL, NULL };
3235 u32 aen_result = ctrl->aen_result;
3236
3237 ctrl->aen_result = 0;
3238 if (!aen_result)
3239 return;
3240
3241 envp[0] = kasprintf(GFP_KERNEL, "NVME_AEN=%#08x", aen_result);
3242 if (!envp[0])
3243 return;
3244 kobject_uevent_env(&ctrl->device->kobj, KOBJ_CHANGE, envp);
3245 kfree(envp[0]);
3246}
3247
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003248static void nvme_async_event_work(struct work_struct *work)
3249{
3250 struct nvme_ctrl *ctrl =
3251 container_of(work, struct nvme_ctrl, async_event_work);
3252
Keith Busche3d78742017-11-07 15:13:14 -07003253 nvme_aen_uevent(ctrl);
Keith Buschad22c352017-11-07 15:13:12 -07003254 ctrl->ops->submit_async_event(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003255}
3256
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303257static bool nvme_ctrl_pp_status(struct nvme_ctrl *ctrl)
3258{
3259
3260 u32 csts;
3261
3262 if (ctrl->ops->reg_read32(ctrl, NVME_REG_CSTS, &csts))
3263 return false;
3264
3265 if (csts == ~0)
3266 return false;
3267
3268 return ((ctrl->ctrl_config & NVME_CC_ENABLE) && (csts & NVME_CSTS_PP));
3269}
3270
3271static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
3272{
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303273 struct nvme_fw_slot_info_log *log;
3274
3275 log = kmalloc(sizeof(*log), GFP_KERNEL);
3276 if (!log)
3277 return;
3278
Keith Buschc627c482017-11-07 10:28:31 -07003279 if (nvme_get_log(ctrl, NVME_LOG_FW_SLOT, log, sizeof(*log)))
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303280 dev_warn(ctrl->device,
3281 "Get FW SLOT INFO log error\n");
3282 kfree(log);
3283}
3284
3285static void nvme_fw_act_work(struct work_struct *work)
3286{
3287 struct nvme_ctrl *ctrl = container_of(work,
3288 struct nvme_ctrl, fw_act_work);
3289 unsigned long fw_act_timeout;
3290
3291 if (ctrl->mtfa)
3292 fw_act_timeout = jiffies +
3293 msecs_to_jiffies(ctrl->mtfa * 100);
3294 else
3295 fw_act_timeout = jiffies +
3296 msecs_to_jiffies(admin_timeout * 1000);
3297
3298 nvme_stop_queues(ctrl);
3299 while (nvme_ctrl_pp_status(ctrl)) {
3300 if (time_after(jiffies, fw_act_timeout)) {
3301 dev_warn(ctrl->device,
3302 "Fw activation timeout, reset controller\n");
3303 nvme_reset_ctrl(ctrl);
3304 break;
3305 }
3306 msleep(100);
3307 }
3308
3309 if (ctrl->state != NVME_CTRL_LIVE)
3310 return;
3311
3312 nvme_start_queues(ctrl);
Minwoo Ima806c6c2017-11-02 18:07:44 +09003313 /* read FW slot information to clear the AER */
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303314 nvme_get_fw_slot_info(ctrl);
3315}
3316
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003317void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
3318 union nvme_result *res)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003319{
Christoph Hellwig7bf58532016-11-10 07:32:34 -08003320 u32 result = le32_to_cpu(res->u32);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003321
Keith Buschad22c352017-11-07 15:13:12 -07003322 if (le16_to_cpu(status) >> 1 != NVME_SC_SUCCESS)
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003323 return;
3324
Keith Busche3d78742017-11-07 15:13:14 -07003325 switch (result & 0x7) {
3326 case NVME_AER_ERROR:
3327 case NVME_AER_SMART:
3328 case NVME_AER_CSS:
3329 case NVME_AER_VS:
3330 ctrl->aen_result = result;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003331 break;
3332 default:
3333 break;
3334 }
3335
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003336 switch (result & 0xff07) {
3337 case NVME_AER_NOTICE_NS_CHANGED:
3338 dev_info(ctrl->device, "rescanning\n");
3339 nvme_queue_scan(ctrl);
3340 break;
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303341 case NVME_AER_NOTICE_FW_ACT_STARTING:
Sagi Grimberg1a40d972017-09-21 17:01:36 +03003342 queue_work(nvme_wq, &ctrl->fw_act_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303343 break;
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003344 default:
3345 dev_warn(ctrl->device, "async event result %08x\n", result);
3346 }
Sagi Grimbergc669ccd2017-05-04 13:33:14 +03003347 queue_work(nvme_wq, &ctrl->async_event_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003348}
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003349EXPORT_SYMBOL_GPL(nvme_complete_async_event);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003350
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003351void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
Ming Lin576d55d2016-02-10 10:03:32 -08003352{
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003353 nvme_stop_keep_alive(ctrl);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003354 flush_work(&ctrl->async_event_work);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003355 flush_work(&ctrl->scan_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303356 cancel_work_sync(&ctrl->fw_act_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003357}
3358EXPORT_SYMBOL_GPL(nvme_stop_ctrl);
Christoph Hellwig5955be22016-04-26 13:51:59 +02003359
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003360void nvme_start_ctrl(struct nvme_ctrl *ctrl)
3361{
3362 if (ctrl->kato)
3363 nvme_start_keep_alive(ctrl);
3364
3365 if (ctrl->queue_count > 1) {
3366 nvme_queue_scan(ctrl);
Keith Buschd99ca602017-11-07 15:13:13 -07003367 queue_work(nvme_wq, &ctrl->async_event_work);
Sagi Grimbergd09f2b42017-07-02 10:56:43 +03003368 nvme_start_queues(ctrl);
3369 }
3370}
3371EXPORT_SYMBOL_GPL(nvme_start_ctrl);
3372
3373void nvme_uninit_ctrl(struct nvme_ctrl *ctrl)
3374{
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003375 cdev_device_del(&ctrl->cdev, ctrl->device);
Keith Busch53029b02015-11-28 15:41:02 +01003376}
Ming Lin576d55d2016-02-10 10:03:32 -08003377EXPORT_SYMBOL_GPL(nvme_uninit_ctrl);
Keith Busch53029b02015-11-28 15:41:02 +01003378
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003379static void nvme_free_ctrl(struct device *dev)
Keith Busch53029b02015-11-28 15:41:02 +01003380{
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003381 struct nvme_ctrl *ctrl =
3382 container_of(dev, struct nvme_ctrl, ctrl_device);
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003383 struct nvme_subsystem *subsys = ctrl->subsys;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003384
Christoph Hellwig9843f682017-10-18 13:10:01 +02003385 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Keith Busch84fef622017-11-07 10:28:32 -07003386 kfree(ctrl->effects);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003387
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003388 if (subsys) {
3389 mutex_lock(&subsys->lock);
3390 list_del(&ctrl->subsys_entry);
3391 mutex_unlock(&subsys->lock);
3392 sysfs_remove_link(&subsys->dev.kobj, dev_name(ctrl->device));
3393 }
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003394
3395 ctrl->ops->free_ctrl(ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003396
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003397 if (subsys)
3398 nvme_put_subsystem(subsys);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003399}
3400
3401/*
3402 * Initialize a NVMe controller structures. This needs to be called during
3403 * earliest initialization so that we have the initialized structured around
3404 * during probing.
3405 */
3406int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
3407 const struct nvme_ctrl_ops *ops, unsigned long quirks)
3408{
3409 int ret;
3410
Christoph Hellwigbb8d2612016-04-26 13:51:57 +02003411 ctrl->state = NVME_CTRL_NEW;
3412 spin_lock_init(&ctrl->lock);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003413 INIT_LIST_HEAD(&ctrl->namespaces);
Christoph Hellwig69d3b8a2015-12-24 15:27:00 +01003414 mutex_init(&ctrl->namespaces_mutex);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003415 ctrl->dev = dev;
3416 ctrl->ops = ops;
3417 ctrl->quirks = quirks;
Christoph Hellwig5955be22016-04-26 13:51:59 +02003418 INIT_WORK(&ctrl->scan_work, nvme_scan_work);
Christoph Hellwigf866fc422016-04-26 13:52:00 +02003419 INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
Arnav Dawnb6dccf72017-07-12 16:10:40 +05303420 INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
Christoph Hellwigc5017e82017-10-29 10:44:29 +02003421 INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003422
Christoph Hellwig9843f682017-10-18 13:10:01 +02003423 ret = ida_simple_get(&nvme_instance_ida, 0, 0, GFP_KERNEL);
3424 if (ret < 0)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003425 goto out;
Christoph Hellwig9843f682017-10-18 13:10:01 +02003426 ctrl->instance = ret;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003427
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003428 device_initialize(&ctrl->ctrl_device);
3429 ctrl->device = &ctrl->ctrl_device;
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003430 ctrl->device->devt = MKDEV(MAJOR(nvme_chr_devt), ctrl->instance);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003431 ctrl->device->class = nvme_class;
3432 ctrl->device->parent = ctrl->dev;
3433 ctrl->device->groups = nvme_dev_attr_groups;
3434 ctrl->device->release = nvme_free_ctrl;
3435 dev_set_drvdata(ctrl->device, ctrl);
3436 ret = dev_set_name(ctrl->device, "nvme%d", ctrl->instance);
3437 if (ret)
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003438 goto out_release_instance;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003439
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003440 cdev_init(&ctrl->cdev, &nvme_dev_fops);
3441 ctrl->cdev.owner = ops->module;
3442 ret = cdev_device_add(&ctrl->cdev, ctrl->device);
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003443 if (ret)
3444 goto out_free_name;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003445
Andy Lutomirskic5552fd2017-02-07 10:08:45 -08003446 /*
3447 * Initialize latency tolerance controls. The sysfs files won't
3448 * be visible to userspace unless the device actually supports APST.
3449 */
3450 ctrl->device->power.set_latency_tolerance = nvme_set_latency_tolerance;
3451 dev_pm_qos_update_user_latency_tolerance(ctrl->device,
3452 min(default_ps_max_latency_us, (unsigned long)S32_MAX));
3453
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003454 return 0;
Christoph Hellwigd22524a2017-10-18 13:25:42 +02003455out_free_name:
3456 kfree_const(dev->kobj.name);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003457out_release_instance:
Christoph Hellwig9843f682017-10-18 13:10:01 +02003458 ida_simple_remove(&nvme_instance_ida, ctrl->instance);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003459out:
3460 return ret;
3461}
Ming Lin576d55d2016-02-10 10:03:32 -08003462EXPORT_SYMBOL_GPL(nvme_init_ctrl);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003463
Keith Busch69d9a992016-02-24 09:15:56 -07003464/**
3465 * nvme_kill_queues(): Ends all namespace queues
3466 * @ctrl: the dead controller that needs to end
3467 *
3468 * Call this function when the driver determines it is unable to get the
3469 * controller in a state capable of servicing IO.
3470 */
3471void nvme_kill_queues(struct nvme_ctrl *ctrl)
3472{
3473 struct nvme_ns *ns;
3474
Keith Busch32f0c4a2016-07-13 11:45:02 -06003475 mutex_lock(&ctrl->namespaces_mutex);
Ming Lei82654b62017-06-02 16:32:08 +08003476
Ming Lei443bd902017-06-19 10:21:08 +08003477 /* Forcibly unquiesce queues to avoid blocking dispatch */
Scott Bauer7dd1ab12017-07-25 10:27:06 -06003478 if (ctrl->admin_q)
3479 blk_mq_unquiesce_queue(ctrl->admin_q);
Ming Lei443bd902017-06-19 10:21:08 +08003480
Keith Busch32f0c4a2016-07-13 11:45:02 -06003481 list_for_each_entry(ns, &ctrl->namespaces, list) {
Keith Busch69d9a992016-02-24 09:15:56 -07003482 /*
3483 * Revalidating a dead namespace sets capacity to 0. This will
3484 * end buffered writers dirtying pages that can't be synced.
3485 */
Keith Buschf33447b2017-02-10 18:15:51 -05003486 if (!ns->disk || test_and_set_bit(NVME_NS_DEAD, &ns->flags))
3487 continue;
3488 revalidate_disk(ns->disk);
Keith Busch69d9a992016-02-24 09:15:56 -07003489 blk_set_queue_dying(ns->queue);
Ming Lei806f0262017-05-22 23:05:03 +08003490
Ming Lei443bd902017-06-19 10:21:08 +08003491 /* Forcibly unquiesce queues to avoid blocking dispatch */
3492 blk_mq_unquiesce_queue(ns->queue);
Keith Busch69d9a992016-02-24 09:15:56 -07003493 }
Keith Busch32f0c4a2016-07-13 11:45:02 -06003494 mutex_unlock(&ctrl->namespaces_mutex);
Keith Busch69d9a992016-02-24 09:15:56 -07003495}
Linus Torvalds237045f2016-03-18 17:13:31 -07003496EXPORT_SYMBOL_GPL(nvme_kill_queues);
Keith Busch69d9a992016-02-24 09:15:56 -07003497
Keith Busch302ad8c2017-03-01 14:22:12 -05003498void nvme_unfreeze(struct nvme_ctrl *ctrl)
3499{
3500 struct nvme_ns *ns;
3501
3502 mutex_lock(&ctrl->namespaces_mutex);
3503 list_for_each_entry(ns, &ctrl->namespaces, list)
3504 blk_mq_unfreeze_queue(ns->queue);
3505 mutex_unlock(&ctrl->namespaces_mutex);
3506}
3507EXPORT_SYMBOL_GPL(nvme_unfreeze);
3508
3509void nvme_wait_freeze_timeout(struct nvme_ctrl *ctrl, long timeout)
3510{
3511 struct nvme_ns *ns;
3512
3513 mutex_lock(&ctrl->namespaces_mutex);
3514 list_for_each_entry(ns, &ctrl->namespaces, list) {
3515 timeout = blk_mq_freeze_queue_wait_timeout(ns->queue, timeout);
3516 if (timeout <= 0)
3517 break;
3518 }
3519 mutex_unlock(&ctrl->namespaces_mutex);
3520}
3521EXPORT_SYMBOL_GPL(nvme_wait_freeze_timeout);
3522
3523void nvme_wait_freeze(struct nvme_ctrl *ctrl)
3524{
3525 struct nvme_ns *ns;
3526
3527 mutex_lock(&ctrl->namespaces_mutex);
3528 list_for_each_entry(ns, &ctrl->namespaces, list)
3529 blk_mq_freeze_queue_wait(ns->queue);
3530 mutex_unlock(&ctrl->namespaces_mutex);
3531}
3532EXPORT_SYMBOL_GPL(nvme_wait_freeze);
3533
3534void nvme_start_freeze(struct nvme_ctrl *ctrl)
3535{
3536 struct nvme_ns *ns;
3537
3538 mutex_lock(&ctrl->namespaces_mutex);
3539 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Lei1671d522017-03-27 20:06:57 +08003540 blk_freeze_queue_start(ns->queue);
Keith Busch302ad8c2017-03-01 14:22:12 -05003541 mutex_unlock(&ctrl->namespaces_mutex);
3542}
3543EXPORT_SYMBOL_GPL(nvme_start_freeze);
3544
Keith Busch25646262016-01-04 09:10:57 -07003545void nvme_stop_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003546{
3547 struct nvme_ns *ns;
3548
Keith Busch32f0c4a2016-07-13 11:45:02 -06003549 mutex_lock(&ctrl->namespaces_mutex);
Bart Van Asschea6eaa882016-10-28 17:23:40 -07003550 list_for_each_entry(ns, &ctrl->namespaces, list)
Bart Van Assche3174dd32016-10-28 17:23:19 -07003551 blk_mq_quiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003552 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003553}
Ming Lin576d55d2016-02-10 10:03:32 -08003554EXPORT_SYMBOL_GPL(nvme_stop_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003555
Keith Busch25646262016-01-04 09:10:57 -07003556void nvme_start_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003557{
3558 struct nvme_ns *ns;
3559
Keith Busch32f0c4a2016-07-13 11:45:02 -06003560 mutex_lock(&ctrl->namespaces_mutex);
Sagi Grimberg8d7b8fa2017-07-04 18:16:58 +03003561 list_for_each_entry(ns, &ctrl->namespaces, list)
Ming Leif6601742017-06-06 23:22:04 +08003562 blk_mq_unquiesce_queue(ns->queue);
Keith Busch32f0c4a2016-07-13 11:45:02 -06003563 mutex_unlock(&ctrl->namespaces_mutex);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003564}
Ming Lin576d55d2016-02-10 10:03:32 -08003565EXPORT_SYMBOL_GPL(nvme_start_queues);
Sagi Grimberg363c9aa2015-12-24 15:26:59 +01003566
Sagi Grimberg31b84462017-10-11 12:53:07 +03003567int nvme_reinit_tagset(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
3568{
3569 if (!ctrl->ops->reinit_request)
3570 return 0;
3571
3572 return blk_mq_tagset_iter(set, set->driver_data,
3573 ctrl->ops->reinit_request);
3574}
3575EXPORT_SYMBOL_GPL(nvme_reinit_tagset);
3576
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003577int __init nvme_core_init(void)
3578{
Roy Shtermanb227c592018-01-14 12:39:02 +02003579 int result = -ENOMEM;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003580
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003581 nvme_wq = alloc_workqueue("nvme-wq",
3582 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3583 if (!nvme_wq)
Roy Shtermanb227c592018-01-14 12:39:02 +02003584 goto out;
3585
3586 nvme_reset_wq = alloc_workqueue("nvme-reset-wq",
3587 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3588 if (!nvme_reset_wq)
3589 goto destroy_wq;
3590
3591 nvme_delete_wq = alloc_workqueue("nvme-delete-wq",
3592 WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS, 0);
3593 if (!nvme_delete_wq)
3594 goto destroy_reset_wq;
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003595
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003596 result = alloc_chrdev_region(&nvme_chr_devt, 0, NVME_MINORS, "nvme");
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003597 if (result < 0)
Roy Shtermanb227c592018-01-14 12:39:02 +02003598 goto destroy_delete_wq;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003599
3600 nvme_class = class_create(THIS_MODULE, "nvme");
3601 if (IS_ERR(nvme_class)) {
3602 result = PTR_ERR(nvme_class);
3603 goto unregister_chrdev;
3604 }
3605
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003606 nvme_subsys_class = class_create(THIS_MODULE, "nvme-subsystem");
3607 if (IS_ERR(nvme_subsys_class)) {
3608 result = PTR_ERR(nvme_subsys_class);
3609 goto destroy_class;
3610 }
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003611 return 0;
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003612
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003613destroy_class:
3614 class_destroy(nvme_class);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003615unregister_chrdev:
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003616 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003617destroy_delete_wq:
3618 destroy_workqueue(nvme_delete_wq);
3619destroy_reset_wq:
3620 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003621destroy_wq:
3622 destroy_workqueue(nvme_wq);
Roy Shtermanb227c592018-01-14 12:39:02 +02003623out:
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003624 return result;
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003625}
3626
3627void nvme_core_exit(void)
3628{
Christoph Hellwigab9e00c2017-11-09 13:48:55 +01003629 ida_destroy(&nvme_subsystems_ida);
3630 class_destroy(nvme_subsys_class);
Christoph Hellwigf3ca80f2015-11-28 15:40:19 +01003631 class_destroy(nvme_class);
Christoph Hellwiga6a51492017-10-18 16:59:25 +02003632 unregister_chrdev_region(nvme_chr_devt, NVME_MINORS);
Roy Shtermanb227c592018-01-14 12:39:02 +02003633 destroy_workqueue(nvme_delete_wq);
3634 destroy_workqueue(nvme_reset_wq);
Sagi Grimberg9a6327d2017-06-07 20:31:55 +02003635 destroy_workqueue(nvme_wq);
Christoph Hellwig5bae7f72015-11-28 15:39:07 +01003636}
Ming Lin576d55d2016-02-10 10:03:32 -08003637
3638MODULE_LICENSE("GPL");
3639MODULE_VERSION("1.0");
3640module_init(nvme_core_init);
3641module_exit(nvme_core_exit);